Created
December 5, 2018 11:27
-
-
Save afrokick/a14d58a8a18fbb30e03ea9e59d109bcf to your computer and use it in GitHub Desktop.
webconf.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {EventBus} from "./eventBus"; | |
const sdk = VoxImplant.getInstance(); | |
// assign handlers | |
sdk.on(VoxImplant.Events.ConnectionEstablished, onConnectionEstablished); | |
sdk.on(VoxImplant.Events.ConnectionFailed, onConnectionFailed); | |
sdk.on(VoxImplant.Events.ConnectionClosed, onConnectionClosed); | |
sdk.on(VoxImplant.Events.AuthResult, onAuthResult); | |
sdk.on(VoxImplant.Events.IncomingCall, onIncomingCall); | |
sdk.on(VoxImplant.Events.MicAccessResult, onMicAccessResult); | |
sdk.on(VoxImplant.Events.SourcesInfoUpdated, onSourcesInfoUpdated); | |
const log = message => console.log(`[webConf]${message}`); | |
export const WebCallEvents = { | |
connected: 'connected', | |
loggedIn: 'loggedIn', | |
loggedOut: 'loggedOut', | |
incomingCall: 'incomingCall', | |
mediaCreated: 'mediaCreated', | |
}; | |
export class WebCall { | |
constructor(call) { | |
this.isMicrophoneMuted = new ReactiveVar(false); | |
this.isShareScreen = new ReactiveVar(false); | |
this.isShareVideo = new ReactiveVar(false); | |
if (call) { | |
this.subscribeToCallbacks(call); | |
} | |
this.currentCall = call; | |
} | |
subscribeToCallbacks(call) { | |
const self = this; | |
call.on(VoxImplant.CallEvents.Connected, | |
() => { | |
log("CallConnected: " + self.currentCall.id()); | |
self.isShareVideo.set(true); | |
}); | |
call.on(VoxImplant.CallEvents.Disconnected, () => { | |
log("CallDisconnected: " + self.currentCall.id() + " Call state: " + self.currentCall.state()); | |
self.currentCall = null; | |
self.isMicrophoneMuted.set(false); | |
}); | |
call.on(VoxImplant.CallEvents.Failed, (e) => { | |
log("CallFailed: " + self.currentCall.id() + " code: " + e.code + " reason: " + e.reason); | |
}); | |
call.on(VoxImplant.CallEvents.MediaElementCreated, (e) => { | |
// For WebRTC just using JS/CSS for transformation | |
log(e); | |
EventBus.dispatch(WebCallEvents.mediaCreated, self, e); | |
}); | |
call.on(VoxImplant.CallEvents.LocalVideoStreamAdded, (e) => { | |
log("LOCAL VIDEO STREAM"); | |
log(e); | |
}); | |
} | |
static loggedIn = false; | |
static connect() { | |
sdk.connect(); | |
} | |
static login(username, password = 'pass') { | |
log(`login with username:${username} password:${password}`); | |
sdk.login(`${username}@test.afrokick.voximplant.com`, password) | |
.then(() => { | |
log('logged in'); | |
}) | |
.catch(() => { | |
// This code is executed on error (wrong name, pass etc) | |
}); | |
} | |
static isConnected() { | |
return sdk.connected(); | |
} | |
// Show/hide local video | |
static showLocalVideo(flag) { | |
sdk.showLocalVideo(flag); | |
} | |
sendVideo(flag) { | |
if (!this.currentCall) return; | |
this.currentCall.sendVideo(flag); | |
this.isShareVideo.set(flag); | |
} | |
// Create outbound call | |
createCall(target) { | |
if (this.currentCall) throw new Error('call already exists'); | |
target += '@test.afrokick.voximplant.com'; | |
// currentCall.hangup(); | |
log("Calling to " + target); | |
this.currentCall = sdk.call( | |
target, | |
{ | |
receiveVideo: true, | |
sendVideo: true | |
}, | |
"TEST CUSTOM DATA" | |
); | |
this.subscribeToCallbacks(this.currentCall); | |
}; | |
// Disconnect current call | |
disconnectCall() { | |
if (this.currentCall != null) { | |
log("Disconnect"); | |
this.hangup(); | |
} | |
} | |
hangup() { | |
if (!this.currentCall) return; | |
this.currentCall.hangup(); | |
} | |
startShareScreen() { | |
if (!this.currentCall) return; | |
this.currentCall.shareScreen(true); | |
this.isShareScreen.set(true); | |
} | |
stopShareScreen() { | |
if (!this.currentCall) return; | |
this.currentCall.stopSharingScreen(); | |
this.isShareScreen.set(false); | |
} | |
// Show/hide remote video | |
showRemoteVideo(flag) { | |
if (!this.currentCall) return; | |
this.currentCall.showRemoteVideo(flag); | |
} | |
muteMicrophone() { | |
if (!this.currentCall) return; | |
this.currentCall.muteMicrophone(); | |
this.isMicrophoneMuted.set(true); | |
} | |
unmuteMicrophone() { | |
if (!this.currentCall) return; | |
this.currentCall.unmuteMicrophone(); | |
this.isMicrophoneMuted.set(false); | |
} | |
} | |
Meteor.startup(() => { | |
Tracker.autorun(() => { | |
const userId = Meteor.userId(); | |
if (!userId) return; | |
start(); | |
}); | |
}); | |
const start = () => { | |
sdk.init({ | |
micRequired: true, // force microphone/camera access request | |
videoSupport: true, // enable video support | |
progressTone: false, // play progress tone | |
}) | |
.then(() => { | |
log("onSDKReady version " + VoxImplant.version); | |
log("WebRTC supported: " + sdk.isRTCsupported()); | |
WebCall.connect(); | |
}) | |
.catch(() => { | |
// This code is executed on error (broken JavaScript etc) | |
}); | |
}; | |
// Connection with VoxImplant established | |
async function onConnectionEstablished() { | |
log("Connection established: " + WebCall.isConnected()); | |
WebCall.loggedIn = false; | |
if (!WebCall.isConnected()) return; | |
EventBus.dispatch(WebCallEvents.connected); | |
await WebCall.login(Meteor.userId()); | |
} | |
// Connection with VoxImplant failed | |
function onConnectionFailed() { | |
WebCall.loggedIn = false; | |
EventBus.dispatch(WebCallEvents.loggedOut); | |
log("Connection failed"); | |
setTimeout(function () { | |
WebCall.connect(); | |
}, 1000); | |
} | |
// Connection with VoxImplant closed | |
function onConnectionClosed() { | |
log("Connection closed"); | |
WebCall.loggedIn = false; | |
EventBus.dispatch(WebCallEvents.loggedOut); | |
setTimeout(function () { | |
WebCall.connect(); | |
}, 1000); | |
} | |
// Handle authorization result | |
function onAuthResult(e) { | |
log("AuthResult: " + e.result); | |
if (e.result) { | |
WebCall.loggedIn = true; | |
EventBus.dispatch(WebCallEvents.loggedIn); | |
} else { | |
WebCall.loggedIn = false; | |
log("Code: " + e.code); | |
} | |
} | |
// Audio & video sources info available | |
function onSourcesInfoUpdated() { | |
var audioSources = sdk.audioSources(), | |
videoSources = sdk.videoSources(); | |
} | |
// Camera/mic access result | |
function onMicAccessResult(e) { | |
log("Mic/Cam access allowed: " + e.result); | |
} | |
// Incoming call | |
function onIncomingCall(e) { | |
const webCall = new WebCall(e.call); | |
log("Incoming call from: " + webCall.currentCall.number()); | |
// Answer automatically | |
webCall.currentCall.answer(null, {}, {receiveVideo: true, sendVideo: true}); | |
console.log('dispatch incomingcall'); | |
EventBus.dispatch(WebCallEvents.incomingCall, webCall); | |
} | |
// Close connection with VoxImplant | |
function closeConnection() { | |
sdk.disconnect(); | |
} | |
// Enable fullscreen | |
// function fullScreenmode(flag) { | |
// if (mode == 'webrtc') { | |
// if (flag === true && currentCall != null) { | |
// var elem = document.getElementById(currentCall.getVideoElementId()); | |
// if (elem.requestFullscreen) { | |
// elem.requestFullscreen(); | |
// } else if (elem.msRequestFullscreen) { | |
// elem.msRequestFullscreen(); | |
// } else if (elem.mozRequestFullScreen) { | |
// elem.mozRequestFullScreen(); | |
// } else if (elem.webkitRequestFullscreen) { | |
// elem.webkitRequestFullscreen(); | |
// } | |
// } | |
// } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment