|
|
@ -1,8 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
import EventEmitter from 'events';
|
|
|
|
import EventEmitter from 'events';
|
|
|
|
import _ from 'lodash';
|
|
|
|
import {
|
|
|
|
import { loadCdkLib, connectCdkNetwork } from '../helpers/cdk-lib';
|
|
|
|
loadCdkLib,
|
|
|
|
import { createSessionToken } from '../api/rtcsession';
|
|
|
|
connectCdkNetwork
|
|
|
|
|
|
|
|
} from '../helpers/cdk-lib';
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
|
|
|
createSessionToken
|
|
|
|
|
|
|
|
} from '../api/rtcsession';
|
|
|
|
|
|
|
|
|
|
|
|
export const LocalMedia = {
|
|
|
|
export const LocalMedia = {
|
|
|
|
audioOnly: 'audioOnly',
|
|
|
|
audioOnly: 'audioOnly',
|
|
|
@ -37,7 +41,7 @@ export class CallAlreadyExists {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var rtcEngineCallInstance = null;
|
|
|
|
let rtcEngineCallInstance = null;
|
|
|
|
export class RtcEngineCall {
|
|
|
|
export class RtcEngineCall {
|
|
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
constructor() {
|
|
|
@ -45,10 +49,9 @@ export class RtcEngineCall {
|
|
|
|
this.network = null;
|
|
|
|
this.network = null;
|
|
|
|
this.loadedLibrary = null;
|
|
|
|
this.loadedLibrary = null;
|
|
|
|
this.sessionToken = null;
|
|
|
|
this.sessionToken = null;
|
|
|
|
this.localCall = null;
|
|
|
|
|
|
|
|
this.localMedia = null;
|
|
|
|
this.localMedia = null;
|
|
|
|
this.remoteCall = null;
|
|
|
|
|
|
|
|
this.remoteMedia = null;
|
|
|
|
this.remoteMedia = null;
|
|
|
|
|
|
|
|
this.currentCall = null;
|
|
|
|
this.events = new EventEmitter();
|
|
|
|
this.events = new EventEmitter();
|
|
|
|
this.endedReason = null;
|
|
|
|
this.endedReason = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -66,14 +69,18 @@ export class RtcEngineCall {
|
|
|
|
}).then(($network)=>{
|
|
|
|
}).then(($network)=>{
|
|
|
|
this.network = $network;
|
|
|
|
this.network = $network;
|
|
|
|
this.network.onIncomingCall((remoteCall)=>{
|
|
|
|
this.network.onIncomingCall((remoteCall)=>{
|
|
|
|
if(this.network !== null && this.remoteCall === null) {
|
|
|
|
if(this.network !== null && this.currentCall === null) {
|
|
|
|
this.remoteCall = remoteCall;
|
|
|
|
this.currentCall = remoteCall;
|
|
|
|
this.remoteCall.onEnded(()=>{
|
|
|
|
this.currentCall.onEnded(()=>{
|
|
|
|
this.events.emit('ended', this.remoteCall.endedReason);
|
|
|
|
this.events.emit('ended', this.currentCall.endedReason);
|
|
|
|
}).onRemoteMedia((remoteMediaStream)=>{
|
|
|
|
}).onRemoteMedia((remoteMediaStream)=>{
|
|
|
|
this.events.emit('remoteMedia', remoteMediaStream);
|
|
|
|
this.events.emit('remoteMedia', remoteMediaStream);
|
|
|
|
}).onRemoteMediaEnded(()=>{
|
|
|
|
}).onRemoteMediaEnded(()=>{
|
|
|
|
this.events.emit('remoteMediaEnded');
|
|
|
|
this.events.emit('remoteMediaEnded');
|
|
|
|
|
|
|
|
}).onError((err)=>{
|
|
|
|
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
this.end();
|
|
|
|
|
|
|
|
this.events.emit('ended', err.message);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.events.emit('incoming');
|
|
|
|
this.events.emit('incoming');
|
|
|
@ -90,7 +97,7 @@ export class RtcEngineCall {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
hasRunningCall() {
|
|
|
|
hasRunningCall() {
|
|
|
|
return this.localCall !== null || this.remoteCall !== null;
|
|
|
|
return this.currentCall !== null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
loadLibrary() {
|
|
|
|
loadLibrary() {
|
|
|
@ -107,60 +114,50 @@ export class RtcEngineCall {
|
|
|
|
|
|
|
|
|
|
|
|
createLocalMedia(localMedia) {
|
|
|
|
createLocalMedia(localMedia) {
|
|
|
|
return new Promise((resolve, reject)=>{
|
|
|
|
return new Promise((resolve, reject)=>{
|
|
|
|
this.localMedia = new cdk.LocalMediaStream();
|
|
|
|
let localMediaBuilder = cdk.media.create();
|
|
|
|
var hasAudio = localMedia === LocalMedia.audioOnly ||
|
|
|
|
if (localMedia === LocalMedia.audioOnly || localMedia === LocalMedia.audioVideo) {
|
|
|
|
localMedia === LocalMedia.audioVideo ||
|
|
|
|
localMediaBuilder.enableMicrophone();
|
|
|
|
localMedia === LocalMedia.audioScreen;
|
|
|
|
}
|
|
|
|
var hasVideo = localMedia === LocalMedia.audioVideo ||
|
|
|
|
if (localMedia === LocalMedia.audioVideo || localMedia === LocalMedia.videoOnly) {
|
|
|
|
localMedia === LocalMedia.videoOnly;
|
|
|
|
localMediaBuilder.enableCamera();
|
|
|
|
var hasScreen = localMedia === LocalMedia.audioScreen ||
|
|
|
|
}
|
|
|
|
localMedia === LocalMedia.screenOnly;
|
|
|
|
else if (localMedia === LocalMedia.audioScreen || localMedia === LocalMedia.screenOnly) {
|
|
|
|
|
|
|
|
localMediaBuilder.enableScreen();
|
|
|
|
this.localMedia.queryMediaSources((sources) => {
|
|
|
|
}
|
|
|
|
if (hasAudio && _.isObject(sources.defaultAudio)) {
|
|
|
|
localMediaBuilder.build().then((localMediaStream)=>{
|
|
|
|
this.localMedia.setAudio(sources.defaultAudio);
|
|
|
|
this.localMedia = localMediaStream;
|
|
|
|
}
|
|
|
|
resolve(this.localMedia);
|
|
|
|
if (hasVideo && _.isObject(sources.defaultVideo)) {
|
|
|
|
}).catch((err)=>{
|
|
|
|
sources.defaultVideo.setQuality(cdk.MediaSourceQuality.HD);
|
|
|
|
reject(err);
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
start(peer, localMediaStream) {
|
|
|
|
start(peer, localMediaStream) {
|
|
|
|
if(this.network !== null && this.localCall === null) {
|
|
|
|
if(this.network !== null && this.currentCall === null) {
|
|
|
|
peer = peer.replace(/(\s|\+)/,'');
|
|
|
|
peer = peer.replace(/(\s|\+)/,'');
|
|
|
|
this.localCall = this.network.call(peer, {
|
|
|
|
this.currentCall = this.network.call(peer, {
|
|
|
|
localMediaStream: localMediaStream
|
|
|
|
localMediaStream: localMediaStream
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this.localCall.onEnded(()=>{
|
|
|
|
this.currentCall.onEnded(()=>{
|
|
|
|
this.events.emit('ended', this.localCall.endedReason);
|
|
|
|
this.events.emit('ended', this.currentCall.endedReason);
|
|
|
|
this.end();
|
|
|
|
this.end();
|
|
|
|
}).onAccepted(()=>{
|
|
|
|
|
|
|
|
this.events.emit('accepted');
|
|
|
|
|
|
|
|
}).onPending(()=>{
|
|
|
|
|
|
|
|
this.events.emit('pending');
|
|
|
|
|
|
|
|
}).onRemoteMedia((remoteMediaStream)=>{
|
|
|
|
}).onRemoteMedia((remoteMediaStream)=>{
|
|
|
|
this.events.emit('remoteMedia', remoteMediaStream);
|
|
|
|
this.events.emit('remoteMedia', remoteMediaStream);
|
|
|
|
}).onRemoteMediaEnded(()=>{
|
|
|
|
}).onRemoteMediaEnded(()=>{
|
|
|
|
this.events.emit('remoteMediaEnded');
|
|
|
|
this.events.emit('remoteMediaEnded');
|
|
|
|
|
|
|
|
}).onAccepted(()=>{
|
|
|
|
|
|
|
|
this.events.emit('accepted');
|
|
|
|
|
|
|
|
}).onPending(()=>{
|
|
|
|
|
|
|
|
this.events.emit('pending');
|
|
|
|
}).onRingingStart(()=>{
|
|
|
|
}).onRingingStart(()=>{
|
|
|
|
this.events.emit('ringingStart');
|
|
|
|
this.events.emit('ringingStart');
|
|
|
|
}).onRingingStop(()=>{
|
|
|
|
}).onRingingStop(()=>{
|
|
|
|
this.events.emit('ringingStop');
|
|
|
|
this.events.emit('ringingStop');
|
|
|
|
|
|
|
|
}).onError((err)=>{
|
|
|
|
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
this.end();
|
|
|
|
|
|
|
|
this.events.emit('ended', err.message);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(this.network !== null) {
|
|
|
|
else if(this.network !== null) {
|
|
|
@ -172,27 +169,8 @@ export class RtcEngineCall {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getNumber() {
|
|
|
|
getNumber() {
|
|
|
|
if(this.localCall !== null) {
|
|
|
|
if(this.currentCall !== null) {
|
|
|
|
return this.localCall.peer;
|
|
|
|
return this.currentCall.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;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
@ -240,9 +218,13 @@ export class RtcEngineCall {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
accept(localMediaStream) {
|
|
|
|
accept(localMediaStream) {
|
|
|
|
if(this.remoteCall !== null) {
|
|
|
|
if(this.currentCall !== null) {
|
|
|
|
this.remoteCall.accept({
|
|
|
|
this.currentCall.accept(localMediaStream).then(()=>{
|
|
|
|
localMediaStream: localMediaStream
|
|
|
|
this.events.emit('locallyAccepted');
|
|
|
|
|
|
|
|
}).catch((err)=>{
|
|
|
|
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
this.end();
|
|
|
|
|
|
|
|
this.events.emit('ended', err.message);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
@ -255,15 +237,9 @@ export class RtcEngineCall {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
end() {
|
|
|
|
end() {
|
|
|
|
if(this.localCall !== null) {
|
|
|
|
if(this.currentCall !== null) {
|
|
|
|
this.localCall.end();
|
|
|
|
this.currentCall.end();
|
|
|
|
this.endedReason = this.fetchEndedReason();
|
|
|
|
this.currentCall = null;
|
|
|
|
this.localCall = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this.remoteCall !== null) {
|
|
|
|
|
|
|
|
this.remoteCall.end();
|
|
|
|
|
|
|
|
this.endedReason = this.fetchEndedReason();
|
|
|
|
|
|
|
|
this.remoteCall = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(this.localMedia !== null) {
|
|
|
|
if(this.localMedia !== null) {
|
|
|
|
this.localMedia.stop();
|
|
|
|
this.localMedia.stop();
|
|
|
@ -272,56 +248,38 @@ export class RtcEngineCall {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
disableAudio() {
|
|
|
|
disableAudio() {
|
|
|
|
if(this.localCall !== null) {
|
|
|
|
if(this.currentCall !== null) {
|
|
|
|
this.localCall.disableAudio();
|
|
|
|
this.currentCall.disableAudio();
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (this.remoteCall !== null) {
|
|
|
|
|
|
|
|
this.remoteCall.disableAudio();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
enableAudio() {
|
|
|
|
enableAudio() {
|
|
|
|
if(this.localCall !== null) {
|
|
|
|
if(this.currentCall !== null) {
|
|
|
|
this.localCall.enableAudio();
|
|
|
|
this.currentCall.enableAudio();
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (this.remoteCall !== null) {
|
|
|
|
|
|
|
|
this.remoteCall.enableAudio();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
disableVideo() {
|
|
|
|
disableVideo() {
|
|
|
|
if(this.localCall !== null) {
|
|
|
|
if(this.currentCall !== null) {
|
|
|
|
this.localCall.disableVideo();
|
|
|
|
this.currentCall.disableVideo();
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (this.remoteCall !== null) {
|
|
|
|
|
|
|
|
this.remoteCall.disableVideo();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
enableVideo() {
|
|
|
|
enableVideo() {
|
|
|
|
if(this.localCall !== null) {
|
|
|
|
if(this.currentCall !== null) {
|
|
|
|
this.localCall.enableVideo();
|
|
|
|
this.currentCall.enableVideo();
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (this.remoteCall !== null) {
|
|
|
|
|
|
|
|
this.remoteCall.enableVideo();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sendDTMF(char) {
|
|
|
|
sendDTMF(char) {
|
|
|
|
if(this.localCall !== null) {
|
|
|
|
if(this.currentCall !== null) {
|
|
|
|
this.localCall.sendDTMF(char);
|
|
|
|
this.currentCall.sendDTMF(char);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (this.remoteCall !== null) {
|
|
|
|
|
|
|
|
this.remoteCall.sendDTMF(char);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getCall() {
|
|
|
|
getCall() {
|
|
|
|
if(this.localCall !== null) {
|
|
|
|
if(this.currentCall !== null) {
|
|
|
|
return this.localCall;
|
|
|
|
return this.currentCall;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (this.remoteCall !== null) {
|
|
|
|
|
|
|
|
return this.remoteCall;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|