From 35989a7454efa4e6b924f96ef2b7ef48fe3f1ab9 Mon Sep 17 00:00:00 2001 From: Hans-Peter Herzog Date: Thu, 10 Jan 2019 13:23:16 +0100 Subject: [PATCH] TT#45554 WebRTC media stack refactoring Change-Id: I6d22281587825c91a52207fd519010a8a047c68f --- .gitignore | 2 - .ngcpcfg_perms | 2 + src/config.template.js | 1 - src/plugins/call.js | 180 ++++++++++++++++------------------------- src/store/call.js | 6 +- 5 files changed, 74 insertions(+), 117 deletions(-) create mode 100755 .ngcpcfg_perms diff --git a/.gitignore b/.gitignore index 275275ed..5901ad2b 100644 --- a/.gitignore +++ b/.gitignore @@ -17,5 +17,3 @@ t/TESTS* csc/ src/config.js - -.ngcpcfg_perms diff --git a/.ngcpcfg_perms b/.ngcpcfg_perms new file mode 100755 index 00000000..e5f52152 --- /dev/null +++ b/.ngcpcfg_perms @@ -0,0 +1,2 @@ +# Generated by ngcpcfg. Do not edit. + diff --git a/src/config.template.js b/src/config.template.js index aa2c9e25..0c1be520 100644 --- a/src/config.template.js +++ b/src/config.template.js @@ -1,4 +1,3 @@ - export default { baseHttpUrl: 'https://' + window.location.host, baseWsUrl: 'wss://' + window.location.host diff --git a/src/plugins/call.js b/src/plugins/call.js index 00de2cc2..320578f6 100644 --- a/src/plugins/call.js +++ b/src/plugins/call.js @@ -1,8 +1,12 @@ import EventEmitter from 'events'; -import _ from 'lodash'; -import { loadCdkLib, connectCdkNetwork } from '../helpers/cdk-lib'; -import { createSessionToken } from '../api/rtcsession'; +import { + loadCdkLib, + connectCdkNetwork +} from '../helpers/cdk-lib'; +import { + createSessionToken +} from '../api/rtcsession'; export const LocalMedia = { audioOnly: 'audioOnly', @@ -37,7 +41,7 @@ export class CallAlreadyExists { } } -var rtcEngineCallInstance = null; +let rtcEngineCallInstance = null; export class RtcEngineCall { constructor() { @@ -45,10 +49,9 @@ export class RtcEngineCall { this.network = null; this.loadedLibrary = null; this.sessionToken = null; - this.localCall = null; this.localMedia = null; - this.remoteCall = null; this.remoteMedia = null; + this.currentCall = null; this.events = new EventEmitter(); this.endedReason = null; } @@ -66,14 +69,18 @@ export class RtcEngineCall { }).then(($network)=>{ this.network = $network; this.network.onIncomingCall((remoteCall)=>{ - if(this.network !== null && this.remoteCall === null) { - this.remoteCall = remoteCall; - this.remoteCall.onEnded(()=>{ - this.events.emit('ended', this.remoteCall.endedReason); + if(this.network !== null && this.currentCall === null) { + this.currentCall = remoteCall; + this.currentCall.onEnded(()=>{ + this.events.emit('ended', this.currentCall.endedReason); }).onRemoteMedia((remoteMediaStream)=>{ this.events.emit('remoteMedia', remoteMediaStream); }).onRemoteMediaEnded(()=>{ this.events.emit('remoteMediaEnded'); + }).onError((err)=>{ + console.error(err); + this.end(); + this.events.emit('ended', err.message); }); } this.events.emit('incoming'); @@ -90,7 +97,7 @@ export class RtcEngineCall { } hasRunningCall() { - return this.localCall !== null || this.remoteCall !== null; + return this.currentCall !== null; } loadLibrary() { @@ -107,60 +114,50 @@ export class RtcEngineCall { createLocalMedia(localMedia) { return new Promise((resolve, reject)=>{ - this.localMedia = new cdk.LocalMediaStream(); - var hasAudio = localMedia === LocalMedia.audioOnly || - localMedia === LocalMedia.audioVideo || - localMedia === LocalMedia.audioScreen; - var hasVideo = localMedia === LocalMedia.audioVideo || - localMedia === LocalMedia.videoOnly; - var hasScreen = localMedia === LocalMedia.audioScreen || - localMedia === LocalMedia.screenOnly; - - this.localMedia.queryMediaSources((sources) => { - if (hasAudio && _.isObject(sources.defaultAudio)) { - this.localMedia.setAudio(sources.defaultAudio); - } - if (hasVideo && _.isObject(sources.defaultVideo)) { - sources.defaultVideo.setQuality(cdk.MediaSourceQuality.HD); - this.localMedia.setVideo(sources.defaultVideo); - } - else if (hasScreen && _.isObject(sources.desktopSharing)) { - sources.desktopSharing.setQuality(cdk.MediaSourceQuality.HD); - this.localMedia.setVideo(sources.desktopSharing); - } - this.localMedia.build((err)=>{ - if(_.isObject(err)) { - reject(err); - } - else { - resolve(this.localMedia); - } - }); + let localMediaBuilder = cdk.media.create(); + if (localMedia === LocalMedia.audioOnly || localMedia === LocalMedia.audioVideo) { + localMediaBuilder.enableMicrophone(); + } + if (localMedia === LocalMedia.audioVideo || localMedia === LocalMedia.videoOnly) { + localMediaBuilder.enableCamera(); + } + else if (localMedia === LocalMedia.audioScreen || localMedia === LocalMedia.screenOnly) { + localMediaBuilder.enableScreen(); + } + localMediaBuilder.build().then((localMediaStream)=>{ + this.localMedia = localMediaStream; + resolve(this.localMedia); + }).catch((err)=>{ + reject(err); }); }); } start(peer, localMediaStream) { - if(this.network !== null && this.localCall === null) { + if(this.network !== null && this.currentCall === null) { peer = peer.replace(/(\s|\+)/,''); - this.localCall = this.network.call(peer, { + this.currentCall = this.network.call(peer, { localMediaStream: localMediaStream }); - this.localCall.onEnded(()=>{ - this.events.emit('ended', this.localCall.endedReason); + this.currentCall.onEnded(()=>{ + this.events.emit('ended', this.currentCall.endedReason); this.end(); - }).onAccepted(()=>{ - this.events.emit('accepted'); - }).onPending(()=>{ - this.events.emit('pending'); }).onRemoteMedia((remoteMediaStream)=>{ this.events.emit('remoteMedia', remoteMediaStream); }).onRemoteMediaEnded(()=>{ this.events.emit('remoteMediaEnded'); + }).onAccepted(()=>{ + this.events.emit('accepted'); + }).onPending(()=>{ + this.events.emit('pending'); }).onRingingStart(()=>{ this.events.emit('ringingStart'); }).onRingingStop(()=>{ this.events.emit('ringingStop'); + }).onError((err)=>{ + console.error(err); + this.end(); + this.events.emit('ended', err.message); }); } else if(this.network !== null) { @@ -172,27 +169,8 @@ export class RtcEngineCall { } getNumber() { - if(this.localCall !== null) { - return this.localCall.peer; - } - else if(this.remoteCall !== null) { - return this.remoteCall.peer; - } - else { - return null; - } - } - - getEndedReason() { - return this.endedReason; - } - - fetchEndedReason() { - if(this.localCall !== null) { - return this.localCall.endedReason; - } - else if(this.remoteCall !== null) { - return this.remoteCall.endedReason; + if(this.currentCall !== null) { + return this.currentCall.peer; } else { return null; @@ -240,9 +218,13 @@ export class RtcEngineCall { } accept(localMediaStream) { - if(this.remoteCall !== null) { - this.remoteCall.accept({ - localMediaStream: localMediaStream + if(this.currentCall !== null) { + this.currentCall.accept(localMediaStream).then(()=>{ + this.events.emit('locallyAccepted'); + }).catch((err)=>{ + console.error(err); + this.end(); + this.events.emit('ended', err.message); }); } else { @@ -255,15 +237,9 @@ export class RtcEngineCall { } end() { - if(this.localCall !== null) { - this.localCall.end(); - this.endedReason = this.fetchEndedReason(); - this.localCall = null; - } - if(this.remoteCall !== null) { - this.remoteCall.end(); - this.endedReason = this.fetchEndedReason(); - this.remoteCall = null; + if(this.currentCall !== null) { + this.currentCall.end(); + this.currentCall = null; } if(this.localMedia !== null) { this.localMedia.stop(); @@ -272,56 +248,38 @@ export class RtcEngineCall { } disableAudio() { - if(this.localCall !== null) { - this.localCall.disableAudio(); - } - else if (this.remoteCall !== null) { - this.remoteCall.disableAudio(); + if(this.currentCall !== null) { + this.currentCall.disableAudio(); } } enableAudio() { - if(this.localCall !== null) { - this.localCall.enableAudio(); - } - else if (this.remoteCall !== null) { - this.remoteCall.enableAudio(); + if(this.currentCall !== null) { + this.currentCall.enableAudio(); } } disableVideo() { - if(this.localCall !== null) { - this.localCall.disableVideo(); - } - else if (this.remoteCall !== null) { - this.remoteCall.disableVideo(); + if(this.currentCall !== null) { + this.currentCall.disableVideo(); } } enableVideo() { - if(this.localCall !== null) { - this.localCall.enableVideo(); - } - else if (this.remoteCall !== null) { - this.remoteCall.enableVideo(); + if(this.currentCall !== null) { + this.currentCall.enableVideo(); } } sendDTMF(char) { - if(this.localCall !== null) { - this.localCall.sendDTMF(char); - } - else if (this.remoteCall !== null) { - this.remoteCall.sendDTMF(char); + if(this.currentCall !== null) { + this.currentCall.sendDTMF(char); } } getCall() { - if(this.localCall !== null) { - return this.localCall; - } - else if (this.remoteCall !== null) { - return this.remoteCall; + if(this.currentCall !== null) { + return this.currentCall; } else { return null; diff --git a/src/store/call.js b/src/store/call.js index 7c042e47..79a5595d 100644 --- a/src/store/call.js +++ b/src/store/call.js @@ -247,7 +247,7 @@ export default { state.callState = CallState.ringing; }, stopRinging(state) { - state.callState = CallState.ended; + state.callState = CallState.established; }, establishCall(state, remoteMediaStream) { state.remoteMediaStream = remoteMediaStream; @@ -331,9 +331,9 @@ export default { }); }).onRemoteMedia((remoteMediaStream)=>{ context.commit('establishCall', remoteMediaStream); - }).onEnded(()=>{ + }).onEnded((reason)=>{ Vue.call.end(); - context.commit('endCall', Vue.call.getEndedReason()); + context.commit('endCall', reason); setTimeout(()=>{ context.commit('inputNumber'); }, errorVisibilityTimeout);