forked from IrishHill/js-video-calling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (89 loc) · 3.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
$("document").ready(function() {
sinchClient = new SinchClient({
applicationKey: "YOUR_APP_KEY",
capabilities: {calling: true, video: true},
supportActiveConnection: true,
onLogMessage: function(message) {
console.log(message.message);
},
});
var callClient;
var call;
$("#login").on("click", function (event) {
event.preventDefault();
var signUpObj = {};
signUpObj.username = $("input#username").val();
signUpObj.password = $("input#password").val();
sinchClient.start(signUpObj, afterStartSinchClient());
});
$("#signup").on("click", function (event) {
event.preventDefault();
var signUpObj = {};
signUpObj.username = $("input#username").val();
signUpObj.password = $("input#password").val();
sinchClient.newUser(signUpObj, function(ticket) {
sinchClient.start(ticket, afterStartSinchClient());
});
});
function afterStartSinchClient() {
// hide auth form
$("form#authForm").css("display", "none");
// show logged-in view
$("div#sinch").css("display", "inline");
// start listening for incoming calls
sinchClient.startActiveConnection();
// define call client (to handle incoming/outgoing calls)
callClient = sinchClient.getCallClient();
//initialize media streams, asks for microphone & video permission
callClient.initStream();
//what to do when there is an incoming call
callClient.addEventListener(incomingCallListener);
}
$("#call").on("click", function (event) {
event.preventDefault();
if (!call) {
usernameToCall = $("input#usernameToCall").val()
$("div#status").append("<div>Calling " + usernameToCall + "</div>");
call = callClient.callUser(usernameToCall);
call.addEventListener(callListeners);
}
});
$("#answer").click(function(event) {
event.preventDefault();
if (call) {
$("div#status").append("<div>You answered the call</div>");
call.answer();
}
});
$("#hangup").click(function(event) {
event.preventDefault();
if (call) {
$("div#status").append("<div>You hung up the call</div>");
call.hangup();
call = null
}
});
var incomingCallListener = {
onIncomingCall: function(incomingCall) {
$("div#status").append("<div>Incoming Call</div>");
call = incomingCall;
call.addEventListener(callListeners);
}
}
var callListeners = {
onCallProgressing: function(call) {
$("div#status").append("<div>Ringing</div>");
},
onCallEstablished: function(call) {
$("div#status").append("<div>Call established</div>");
$("video#outgoing").attr("src", call.outgoingStreamURL);
$("video#incoming").attr("src", call.incomingStreamURL);
},
onCallEnded: function(call) {
$("div#status").append("<div>Call ended</div>");
$("video#outgoing").attr("src", "");
$("video#incoming").attr("src", "");
call = null;
}
}
});