bigbluebutton-Github/bigbluebutton-html5/imports/api/audio/client/bridge/sip.js

131 lines
3.7 KiB
JavaScript
Raw Normal View History

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
exitAudio(afterExitCall = () => {}) {
// To be called when the hangup is initiated
const hangupCallback = function () {
console.log('Exiting Voice Conference');
};
// Checks periodically until a call is established so we can successfully end the call
// clean state
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,
// the request has no effect
// keep track in the session if we haven't tried a hangup
if (window.getCallStatus() != null && !triedHangup) {
console.log('Attempting to hangup on WebRTC call');
// notify BBB-apps we are leaving the call call if we are listen only
2017-04-21 04:53:55 +08:00
if (this.userData.listenOnly) {
2017-04-01 03:22:44 +08:00
callServer('listenOnlyToggle', false);
2017-03-31 01:57:05 +08:00
}
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', {
status: 'Failed', });
2017-03-31 01:57:05 +08:00
window.dispatchEvent(audioFailed);
break;
case 'mediafail':
let mediaFailed = new CustomEvent('bbb.webrtc.mediaFailed', {
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', {
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
}
}