-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrr_clicktocall.js
609 lines (541 loc) · 22.3 KB
/
rr_clicktocall.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
var sip_oSipStack, sip_oSipSessionRegister, sip_oSipSessionCall, sip_oSipSessionTransferCall;
var sip_videoRemote, ivr_videoLocal, ivr_audioRemote;
var ivr_bFullScreen = false;
var ivr_oNotifICall;
var bDisableVideo = false;
var viewVideoLocal, viewVideoRemote; // <video> (webrtc) or <div> (webrtc4all)
var oConfigCall;
var oReadyStateTimer;
var click_to_call_status;
var click_to_call;
var click_to_call_controls;
var sip_call_id;
var websocket_proxy_url='wss://ctc.ringroost.com:10062';
C = { divKeyPadWidth: 220 };
window.addEventListener("load", ivr_click_to_call_init, false);
function ivr_click_to_call_init(){
document.addEventListener("keyup", function (evt) {
if(click_to_call_status =="started"){
sipSendDTMF(String.fromCharCode(evt.keyCode));
}
});
ui_click_to_call();
ivr_audioRemote = document.getElementById("ivrd_audio_remote");
SIPml.init(ivr_postInit,initError);
};
function initError(){
click_to_call.innerHTML=click_to_call.getAttribute('notsupported_text');
}
function ui_click_to_call(){
click_to_call = document.getElementById("ringroost_c2c");
var click_html='<div id="rr_c2c_controls"><a href="#" onclick="ivrDesignerCall();">'+click_to_call.getAttribute("text")+'</a></div><div id="rr_phone_status"></div><span id="rr_call_icon">☎</span>';
click_html+='<audio style="display:none" id="ivrd_audio_remote" autoplay="autoplay" /></audio>';
click_html+='<audio style="display:none" id="ivrd_dtmfTone" src="//www.ringroost.com/dtmf.wav" /></audio>';
click_to_call.innerHTML = click_html;
click_to_call_controls=document.getElementById("rr_c2c_controls");
txtCallStatus =document.getElementById("rr_phone_status");
}
function getChromeVersion () {
var raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
return raw ? parseInt(raw[2], 10) : false;
}
function ivr_postInit() {
var is_chrome = navigator.userAgent.indexOf('Chrome') > -1;
var is_explorer = (navigator.userAgent.indexOf('MSIE') > -1 || (!!document.documentMode == true ));
var is_firefox = navigator.userAgent.indexOf('Firefox') > -1;
var is_safari = navigator.userAgent.indexOf("Safari") > -1 ;
var is_opera = navigator.userAgent.toLowerCase().indexOf("op") > -1;
if ((is_chrome)&&(is_safari)) {is_safari=false;}
if ((is_chrome)&&(is_opera)) {is_chrome=false;}
if(is_chrome){
if(getChromeVersion() > 46 && location.protocol === 'http:'){
initError();
return;
};
}
//will they be able to handle this??
if (!SIPml.isWebRtcSupported() || !SIPml.isWebSocketSupported() ){
initError();
return;
}
//safari && and ie don't currently work
if (is_safari || is_explorer){
initError();
return;
}
if (is_firefox){
// websocket_proxy_url='ws://ctc.ringroost.com:10060';
}
// FIXME: displays must be per session
document.body.style.cursor = 'default';
oConfigCall = {
audio_remote: ivr_audioRemote,
// video_local: viewVideoLocal,
// video_remote: viewVideoRemote,
bandwidth: { audio:undefined },
//video_size: { minWidth:undefined, minHeight:undefined, maxWidth:undefined, maxHeight:undefined },
events_listener: { events: '*', listener: onSipEventSession },
sip_caps: [
{ name: '+g.oma.sip-im' },
{ name: '+sip.ice' },
{ name: 'language', value: '\"en,fr\"' }
]
};
}
function ivrDesignerCall(){
sip_call_id=click_to_call.getAttribute("call");
ivr_sipRegister();
}
function sip_onSipEventStack(e /*SIPml.Stack.Event*/) {
tsk_utils_log_info('==stack event = ' + e.type);
switch (e.type) {
case 'started':
{
//we start the call as sson as we are started
ivr_sipCall("call-audio");
return;
//we dont need to register
// catch exception for IE (DOM not ready)
try {
// LogIn (REGISTER) as soon as the stack finish starting
sip_oSipSessionRegister = this.newSession('register', {
expires: 200,
events_listener: { events: '*', listener: onSipEventSession },
sip_caps: [
{ name: '+g.oma.sip-im', value: null },
{ name: '+audio', value: null },
{ name: 'language', value: '\"en,fr\"' }
]
});
sip_oSipSessionRegister.register();
}
catch (e) {
// txtRegStatus.value = txtRegStatus.innerHTML = "<b>1:" + e + "</b>";
}
break;
}
case 'stopping': case 'stopped': case 'failed_to_start': case 'failed_to_stop':
{
var bFailure = (e.type == 'failed_to_start') || (e.type == 'failed_to_stop');
sip_oSipStack = null;
sip_oSipSessionRegister = null;
sip_oSipSessionCall = null;
stopRingbackTone();
stopRingTone();
txtCallStatus.innerHTML = '';
//txtRegStatus.innerHTML = bFailure ? "<i>Disconnected: <b>" + e.description + "</b></i>" : "<i>Disconnected</i>";
break;
}
case 'i_new_call':
{
if (sip_oSipSessionCall) {
// do not accept the incoming call if we're already 'in call'
e.newSession.hangup(); // comment this line for multi-line support
}
else {
sip_oSipSessionCall = e.newSession;
// start listening for events
sip_oSipSessionCall.setConfiguration(oConfigCall);
startRingTone();
var sRemoteNumber = (sip_oSipSessionCall.getRemoteFriendlyName() || 'unknown');
txtCallStatus.innerHTML = "<i>Incoming call from [<b>" + sRemoteNumber + "</b>]</i>";
showNotifICall(sRemoteNumber);
}
break;
}
case 'm_permission_requested':
{
//divGlassPanel.style.visibility = 'visible';
break;
}
case 'm_permission_accepted':
case 'm_permission_refused':
{
//divGlassPanel.style.visibility = 'hidden';
if(e.type == 'm_permission_refused'){
}
break;
}
case 'starting': default: break;
}
};
//tay
function ivr_sipRegister() {
if (window.webkitNotifications && window.webkitNotifications.checkPermission() != 0) {
window.webkitNotifications.requestPermission();
}
SIPml.setDebugLevel("info"); //info || error
// create SIP stack
sip_oSipStack = new SIPml.Stack({
realm: 'ctcproxy.ringroost.com',
impi: sip_call_id,
impu: 'sip:[email protected]',
password: 'none',
display_name: 'ivr_plugin',
websocket_proxy_url:websocket_proxy_url,
outbound_proxy_url:null ,
ice_servers:"[{url:'stun:stun.l.google.com:19302'}]",
enable_rtcweb_breaker:true ,
events_listener: { events: '*', listener: sip_onSipEventStack }, //check this out tal
enable_early_ims: true,
enable_media_stream_cache: false,
bandwidth: null,
video_size: null,
sip_headers: [
{ name: 'User-Agent', value: 'IM-client/OMA1.0 sipML5-v1.2014.04.18' },
{ name: 'Organization', value: 'Doubango Telecom' }
]
}
);
if (sip_oSipStack.start() != 0) {
return false;
} else {
//were good
return true;
}
}
// sends SIP REGISTER (expires=0) to logout
function sipUnRegister() {
if (sip_oSipStack) {
sip_oSipStack.stop(); // shutdown all sessions
}
}
// makes a call (SIP INVITE)
function ivr_sipCall(s_type) {
/*if (!sip_oSipSessionCall) {*/
// create call session
sip_oSipSessionCall = sip_oSipStack.newSession(s_type, oConfigCall);
// make call
if (sip_oSipSessionCall.call(sip_call_id) != 0) {
return;
}else{
return;
}
/*
incomming calls
}else{
alert('accepting inbound session');
sip_oSipSessionCall.accept(oConfigCall);
}
*/
}
// terminates the call (SIP BYE or CANCEL)
function sipHangUp() {
if (sip_oSipSessionCall) {
txtCallStatus.innerHTML = '<i>Terminating the call...</i>';
sip_oSipSessionCall.hangup({events_listener: { events: '*', listener: onSipEventSession }});
}
}
function sipSendDTMF(c){
if(sip_oSipSessionCall && c){
if(sip_oSipSessionCall.dtmf(c) == 0){
try { ivrd_dtmfTone.play(); } catch(e){ }
}
}
}
function ivr_insertAfter(referenceNode, newNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
function startRingTone() {
try { ringtone.play(); }
catch (e) { }
}
function stopRingTone() {
try { ringtone.pause(); }
catch (e) { }
}
function startRingbackTone() {
try { ringbacktone.play(); }
catch (e) { }
}
function stopRingbackTone() {
try { ringbacktone.pause(); }
catch (e) { }
}
function toggleFullScreen() {
if (sip_videoRemote.webkitSupportsFullscreen) {
fullScreen(!sip_videoRemote.webkitDisplayingFullscreen);
}
else {
fullScreen(!ivr_bFullScreen);
}
}
function openKeyPad(){
divKeyPad.style.visibility = 'visible';
divKeyPad.style.left = ((document.body.clientWidth - C.divKeyPadWidth) >> 1) + 'px';
divKeyPad.style.top = '70px';
//divGlassPanel.style.visibility = 'visible';
}
function closeKeyPad(){
divKeyPad.style.left = '0px';
divKeyPad.style.top = '0px';
divKeyPad.style.visibility = 'hidden';
//divGlassPanel.style.visibility = 'hidden';
}
function fullScreen(b_fs) {
ivr_bFullScreen = b_fs;
if (tsk_utils_have_webrtc4native() && ivr_bFullScreen && sip_videoRemote.webkitSupportsFullscreen) {
if (ivr_bFullScreen) {
sip_videoRemote.webkitEnterFullScreen();
}
else {
sip_videoRemote.webkitExitFullscreen();
}
}
else {
if (tsk_utils_have_webrtc4npapi()) {
try { if(window.__o_display_remote) window.__o_display_remote.setFullScreen(b_fs); }
catch (e) { divVideo.setAttribute("class", b_fs ? "full-screen" : "normal-screen"); }
}
else {
divVideo.setAttribute("class", b_fs ? "full-screen" : "normal-screen");
}
}
}
function showNotifICall(s_number) {
// permission already asked when we registered
if (window.webkitNotifications && window.webkitNotifications.checkPermission() == 0) {
if (ivr_oNotifICall) {
ivr_oNotifICall.cancel();
}
ivr_oNotifICall = window.webkitNotifications.createNotification('images/sipml-34x39.png', 'Incaming call', 'Incoming call from ' + s_number);
ivr_oNotifICall.onclose = function () { ivr_oNotifICall = null; };
ivr_oNotifICall.show();
}
}
/*
function onKeyUp(evt) {
if(click_to_call_status =="started"){
sipSendDTMF(String.fromCharCode(evt.keyCode));
}
}
*/
function onDivCallCtrlMouseMove(evt) {
try { // IE: DOM not ready
if (tsk_utils_have_stream()) {
document.getElementById("divCallCtrl").onmousemove = null; // unsubscribe
}
}
catch (e) { }
}
// Callback function for SIP sessions (INVITE, REGISTER, MESSAGE...)
function onSipEventSession(e /* SIPml.Session.Event */) {
tsk_utils_log_info('==session event = ' + e.type);
switch (e.type) {
case 'connecting': case 'connected':
{
txtCallStatus.style.display='block';
var bConnected = (e.type == 'connected');
if (e.session == sip_oSipSessionRegister) {
//txtRegStatus.innerHTML = "<i>" + e.description + "</i>";
}
else if (e.session == sip_oSipSessionCall) {
if (bConnected) {
click_to_call_status='started';
click_to_call_controls.innerHTML = '<a href="#" onclick="sipHangUp();">Hangup</a>';
stopRingbackTone();
stopRingTone();
if (ivr_oNotifICall) {
ivr_oNotifICall.cancel();
ivr_oNotifICall = null;
}
}
txtCallStatus.innerHTML = "<i>" + e.description + "</i>";
}
break;
} // 'connecting' | 'connected'
case 'terminating': case 'terminated':
{
click_to_call_status='stopped';
txtCallStatus.style.display='none';
click_to_call_controls.innerHTML = '<a href="#" onclick="ivrDesignerCall();"> '+click_to_call.getAttribute("text")+' </a>';
if (e.session == sip_oSipSessionRegister) {
sip_oSipSessionCall = null;
sip_oSipSessionRegister = null;
txtCallStatus.innerHTML = e.description;
//txtRegStatus.innerHTML = "<i>" + e.description + "</i>";
}
else if (e.session == sip_oSipSessionCall) {
txtCallStatus.innerHTML = e.description;
}
break;
} // 'terminating' | 'terminated'
case 'm_stream_video_local_added':
{
if (e.session == sip_oSipSessionCall) {
}
break;
}
case 'm_stream_video_local_removed':
{
if (e.session == sip_oSipSessionCall) {
}
break;
}
case 'm_stream_video_remote_added':
{
if (e.session == sip_oSipSessionCall) {
}
break;
}
case 'm_stream_video_remote_removed':
{
if (e.session == sip_oSipSessionCall) {
}
break;
}
case 'm_stream_audio_local_added':
case 'm_stream_audio_local_removed':
case 'm_stream_audio_remote_added':
case 'm_stream_audio_remote_removed':
{
break;
}
case 'i_ect_new_call':
{
sip_oSipSessionTransferCall = e.session;
break;
}
case 'i_ao_request':
{
if(e.session == sip_oSipSessionCall){
var iSipResponseCode = e.getSipResponseCode();
if (iSipResponseCode == 180 || iSipResponseCode == 183) {
startRingbackTone();
txtCallStatus.innerHTML = '<i>Remote ringing...</i>';
}
}
break;
}
case 'm_early_media':
{
if(e.session == sip_oSipSessionCall){
stopRingbackTone();
stopRingTone();
txtCallStatus.innerHTML = '<i>Early media started</i>';
}
break;
}
case 'm_local_hold_ok':
{
if(e.session == sip_oSipSessionCall){
if (sip_oSipSessionCall.bTransfering) {
sip_oSipSessionCall.bTransfering = false;
// this.AVSession.TransferCall(this.transferUri);
}
btnHoldResume.value = 'Resume';
btnHoldResume.disabled = false;
txtCallStatus.innerHTML = '<i>Call placed on hold</i>';
sip_oSipSessionCall.bHeld = true;
}
break;
}
case 'm_local_hold_nok':
{
if(e.session == sip_oSipSessionCall){
sip_oSipSessionCall.bTransfering = false;
btnHoldResume.value = 'Hold';
btnHoldResume.disabled = false;
txtCallStatus.innerHTML = '<i>Failed to place remote party on hold</i>';
}
break;
}
case 'm_local_resume_ok':
{
if(e.session == sip_oSipSessionCall){
sip_oSipSessionCall.bTransfering = false;
btnHoldResume.value = 'Hold';
btnHoldResume.disabled = false;
txtCallStatus.innerHTML = '<i>Call taken off hold</i>';
sip_oSipSessionCall.bHeld = false;
if (SIPml.isWebRtc4AllSupported()) { // IE don't provide stream callback yet
}
}
break;
}
case 'm_local_resume_nok':
{
if(e.session == sip_oSipSessionCall){
sip_oSipSessionCall.bTransfering = false;
btnHoldResume.disabled = false;
txtCallStatus.innerHTML = '<i>Failed to unhold call</i>';
}
break;
}
case 'm_remote_hold':
{
if(e.session == sip_oSipSessionCall){
txtCallStatus.innerHTML = '<i>Placed on hold by remote party</i>';
}
break;
}
case 'm_remote_resume':
{
if(e.session == sip_oSipSessionCall){
txtCallStatus.innerHTML = '<i>Taken off hold by remote party</i>';
}
break;
}
case 'o_ect_trying':
{
if(e.session == sip_oSipSessionCall){
txtCallStatus.innerHTML = '<i>Call transfer in progress...</i>';
}
break;
}
case 'o_ect_accepted':
{
if(e.session == sip_oSipSessionCall){
txtCallStatus.innerHTML = '<i>Call transfer accepted</i>';
}
break;
}
case 'o_ect_completed':
case 'i_ect_completed':
{
if(e.session == sip_oSipSessionCall){
txtCallStatus.innerHTML = '<i>Call transfer completed</i>';
if (sip_oSipSessionTransferCall) {
sip_oSipSessionCall = sip_oSipSessionTransferCall;
}
sip_oSipSessionTransferCall = null;
}
break;
}
case 'o_ect_failed':
case 'i_ect_failed':
{
if(e.session == sip_oSipSessionCall){
txtCallStatus.innerHTML = '<i>Call transfer failed</i>';
}
break;
}
case 'o_ect_notify':
case 'i_ect_notify':
{
if(e.session == sip_oSipSessionCall){
txtCallStatus.innerHTML = "<i>Call Transfer: <b>" + e.getSipResponseCode() + " " + e.description + "</b></i>";
if (e.getSipResponseCode() >= 300) {
if (sip_oSipSessionCall.bHeld) {
sip_oSipSessionCall.resume();
}
}
}
break;
}
case 'i_ect_requested':
{
if(e.session == sip_oSipSessionCall){
var s_message = "Do you accept call transfer to [" + e.getTransferDestinationFriendlyName() + "]?";//FIXME
if (confirm(s_message)) {
txtCallStatus.innerHTML = "<i>Call transfer in progress...</i>";
sip_oSipSessionCall.acceptTransfer();
break;
}
sip_oSipSessionCall.rejectTransfer();
}
break;
}
}
}
//init the button