2017-03-31 23:46:33 +08:00
|
|
|
import BaseAudioBridge from './base';
|
2017-03-31 01:57:05 +08:00
|
|
|
|
2017-04-19 23:01:28 +08:00
|
|
|
import { callServer } from '/imports/ui/services/api';
|
2017-03-31 01:57:05 +08:00
|
|
|
|
|
|
|
const APP_CONFIG = Meteor.settings.public.app;
|
|
|
|
const MEDIA_CONFIG = Meteor.settings.public.media;
|
|
|
|
|
|
|
|
let triedHangup = false;
|
|
|
|
|
2017-03-31 23:46:33 +08:00
|
|
|
export default class SIPBridge extends BaseAudioBridge {
|
2017-04-19 23:01:28 +08:00
|
|
|
constructor(userData) {
|
2017-03-31 01:57:05 +08:00
|
|
|
super();
|
2017-04-19 23:01:28 +08:00
|
|
|
this.userData = userData;
|
2017-03-31 01:57:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
joinListenOnly() {
|
2017-04-01 03:22:44 +08:00
|
|
|
callServer('listenOnlyToggle', true);
|
2017-03-31 01:57:05 +08:00
|
|
|
this._joinVoiceCallSIP({ isListenOnly: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
joinMicrophone() {
|
|
|
|
this._joinVoiceCallSIP({ isListenOnly: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Periodically check the status of the WebRTC call, when a call has been established attempt to
|
|
|
|
// hangup, retry if a call is in progress, send the leave voice conference message to BBB
|
2017-04-25 05:17:53 +08:00
|
|
|
exitAudio(isListenOnly, afterExitCall = () => {}) {
|
|
|
|
// To be called when the hangup is confirmed
|
2017-03-31 01:57:05 +08:00
|
|
|
const hangupCallback = function () {
|
2017-04-25 05:17:53 +08:00
|
|
|
console.log('Exited Voice Conference, listenOnly=' + isListenOnly);
|
|
|
|
|
|
|
|
// notify BBB-apps we are leaving the call if we are in listen only mode
|
|
|
|
if (isListenOnly) {
|
|
|
|
callServer('listenOnlyToggle', false);
|
|
|
|
}
|
2017-03-31 01:57:05 +08:00
|
|
|
};
|
|
|
|
|
2017-04-25 05:17:53 +08:00
|
|
|
// Checks periodically until a call is established so we can successfully end the call clean state
|
2017-03-31 01:57:05 +08:00
|
|
|
triedHangup = false;
|
|
|
|
|
|
|
|
// function to initiate call
|
|
|
|
const checkToHangupCall = ((context, afterExitCall = () => {}) => {
|
|
|
|
// if an attempt to hang up the call is made when the current session is not yet finished,
|
2017-04-25 05:17:53 +08:00
|
|
|
// the request has no effect keep track in the session if we haven't tried a hangup
|
2017-03-31 01:57:05 +08:00
|
|
|
if (window.getCallStatus() != null && !triedHangup) {
|
|
|
|
console.log('Attempting to hangup on WebRTC call');
|
|
|
|
window.webrtc_hangup(hangupCallback);
|
|
|
|
|
|
|
|
// we have hung up, prevent retries
|
|
|
|
triedHangup = true;
|
|
|
|
|
|
|
|
if (afterExitCall) {
|
|
|
|
afterExitCall(this, APP_CONFIG.listenOnly);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log('RETRYING hangup on WebRTC call in ' +
|
|
|
|
`${MEDIA_CONFIG.WebRTCHangupRetryInterval} ms`);
|
|
|
|
|
|
|
|
// try again periodically
|
|
|
|
setTimeout(checkToHangupCall, MEDIA_CONFIG.WebRTCHangupRetryInterval);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// automatically run function
|
|
|
|
(this, afterExitCall);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// join the conference. If listen only send the request to the server
|
|
|
|
_joinVoiceCallSIP(options) {
|
2017-04-19 23:01:28 +08:00
|
|
|
const extension = this.userData.voiceBridge;
|
2017-03-31 01:57:05 +08:00
|
|
|
console.log(options);
|
|
|
|
|
|
|
|
// create voice call params
|
|
|
|
const joinCallback = function (message) {
|
|
|
|
console.log('Beginning WebRTC Conference Call');
|
|
|
|
};
|
|
|
|
|
2017-04-19 23:01:28 +08:00
|
|
|
const {
|
|
|
|
userId,
|
|
|
|
username,
|
|
|
|
} = this.userData;
|
|
|
|
|
2017-03-31 01:57:05 +08:00
|
|
|
window.BBB = {};
|
|
|
|
window.BBB.getMyUserInfo = function (callback) {
|
|
|
|
const result = {
|
2017-04-19 23:01:28 +08:00
|
|
|
myUserID: userId,
|
|
|
|
myUsername: username,
|
|
|
|
myInternalUserID: userId,
|
2017-03-31 01:57:05 +08:00
|
|
|
myAvatarURL: null,
|
|
|
|
myRole: 'getMyRole',
|
|
|
|
amIPresenter: 'false',
|
|
|
|
voiceBridge: extension,
|
|
|
|
dialNumber: null,
|
|
|
|
};
|
|
|
|
return callback(result);
|
|
|
|
};
|
|
|
|
|
2017-04-19 23:01:28 +08:00
|
|
|
const stunsAndTurns = {
|
|
|
|
stun: this.userData.stuns,
|
|
|
|
turn: this.userData.turns,
|
2017-03-31 01:57:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
callIntoConference(extension, function (audio) {
|
|
|
|
switch (audio.status) {
|
|
|
|
case 'failed':
|
|
|
|
let audioFailed = new CustomEvent('bbb.webrtc.failed', {
|
2017-04-20 04:52:50 +08:00
|
|
|
status: 'Failed', });
|
2017-03-31 01:57:05 +08:00
|
|
|
window.dispatchEvent(audioFailed);
|
|
|
|
break;
|
|
|
|
case 'mediafail':
|
|
|
|
let mediaFailed = new CustomEvent('bbb.webrtc.mediaFailed', {
|
2017-04-20 04:52:50 +08:00
|
|
|
status: 'MediaFailed', });
|
2017-03-31 01:57:05 +08:00
|
|
|
window.dispatchEvent(mediaFailed);
|
|
|
|
break;
|
|
|
|
case 'mediasuccess':
|
|
|
|
case 'started':
|
|
|
|
let connected = new CustomEvent('bbb.webrtc.connected', {
|
2017-04-20 04:52:50 +08:00
|
|
|
status: 'started', });
|
2017-03-31 01:57:05 +08:00
|
|
|
window.dispatchEvent(connected);
|
|
|
|
break;
|
|
|
|
}
|
2017-04-19 23:01:28 +08:00
|
|
|
}, options.isListenOnly, stunsAndTurns);
|
2017-03-31 01:57:05 +08:00
|
|
|
}
|
|
|
|
}
|