seepur/resources/scripts/applications/home/classes/call.manager.ts

171 lines
5.6 KiB
TypeScript
Raw Normal View History

2020-04-12 23:33:24 +00:00
import WebSocketService from "../scripts/websocket.service";
import { EventEmitter } from "events";
export default class CallManager {
2020-04-12 14:25:42 +00:00
private inCall: boolean;
private peerId: number;
private localStream: MediaStream;
private remoteStream: MediaStream;
2020-04-12 23:33:24 +00:00
private signalingChannel;
2020-04-12 14:25:42 +00:00
private needToAddStream: boolean = true;
2020-04-12 23:33:24 +00:00
private emitter = new EventEmitter();
2020-04-12 14:25:42 +00:00
private pc: RTCPeerConnection;
2020-04-12 23:33:24 +00:00
constructor(private ws: WebSocketService, private callId: number) {
2020-04-12 14:25:42 +00:00
this.inCall = false;
this.peerId = -1;
this.pc = null;
this.remoteStream = new MediaStream();
}
2020-04-12 23:33:24 +00:00
async connectToCall(mediaConstraints: MediaStreamConstraints): Promise<boolean> {
2020-04-12 14:25:42 +00:00
if (this.inCall) throw new Error('Already connected to call');
2020-04-12 23:33:24 +00:00
console.log('connecting to call');
await this.getUserMedia(mediaConstraints);
this.signalingChannel = this.ws.subscribe(`call:${this.callId}`);
const signalingChannel = this.signalingChannel;
2020-04-12 14:25:42 +00:00
const self = this;
return new Promise((resolve, reject) => {
2020-04-12 23:33:24 +00:00
signalingChannel.on('close', self.close.bind(self));
signalingChannel.on('call:start', self.onCallStart.bind(self));
signalingChannel.on('call:standby', self.onCallStandby.bind(self));
signalingChannel.on('wrtc:sdp:offer', self.onRemoteOffer.bind(self));
signalingChannel.on('wrtc:sdp:answer', self.onRemoteAnswer.bind(self));
signalingChannel.on('wrtc:ice', self.onRemoteIce.bind(self));
signalingChannel.on('error', (e) => {
2020-04-12 14:25:42 +00:00
console.error(e);
resolve(false)
});
2020-04-12 23:33:24 +00:00
signalingChannel.on('ready', () => {
console.log('in Ready');
self.inCall = true;
2020-04-12 14:25:42 +00:00
resolve(true)
});
});
}
2020-04-12 23:33:24 +00:00
on(event: ECallEvents, callback: (...args) => void) {
this.emitter.on(event, callback);
}
private emit(event: ECallEvents, data: any) {
this.emitter.emit(event, data);
}
2020-04-12 14:25:42 +00:00
private send(event: string, payload: { [key: string]: any }) {
2020-04-12 23:33:24 +00:00
this.signalingChannel.emit(event, {
2020-04-12 14:25:42 +00:00
id: this.peerId,
...payload
})
}
async onCallStart(payload: { iceServers: RTCIceServer[], id: number }) {
2020-04-12 23:33:24 +00:00
console.log('onCallStart');
2020-04-12 14:25:42 +00:00
console.log(payload);
this.peerId = payload.id;
this.pc = new RTCPeerConnection({ iceServers: payload.iceServers });
console.log('Created PeerConnection');
2020-04-12 23:33:24 +00:00
console.log('adding tracks to pc');
this.localStream.getTracks().forEach(t => this.pc.addTrack(t, this.localStream));
2020-04-12 14:25:42 +00:00
this.setupPeerConnectionListeners();
const sdp = await this.pc.createOffer();
await this.pc.setLocalDescription(sdp);
2020-04-12 23:33:24 +00:00
console.log('Local description Set', sdp.sdp);
2020-04-12 14:25:42 +00:00
this.send('wrtc:sdp:offer', {
sdp
});
return true;
}
async onCallStandby(payload: { iceServers: RTCIceServer[], id: number }) {
2020-04-12 23:33:24 +00:00
console.log('onCallStandby');
2020-04-12 14:25:42 +00:00
console.log(payload);
this.peerId = payload.id;
this.pc = new RTCPeerConnection({ iceServers: payload.iceServers });
console.log('Created PeerConnection');
2020-04-12 23:33:24 +00:00
console.log('adding tracks to pc');
this.localStream.getTracks().forEach(t => this.pc.addTrack(t, this.localStream));
2020-04-12 14:25:42 +00:00
this.setupPeerConnectionListeners();
return true;
}
setupPeerConnectionListeners() {
this.pc.addEventListener("icecandidate", this.onLocalIce.bind(this));
this.pc.addEventListener('connectionstatechange', event => {
console.log(`PC Connection state: ${this.pc.connectionState}`);
// if (this.pc.connectionState === 'connected') {
// // Peers connected!
// }
});
this.pc.addEventListener('iceconnectionstatechange', event => {
console.log('iceconnectionstatechange');
console.log(this.pc.iceConnectionState);
})
this.pc.addEventListener('track', async (event) => {
console.log('On remote track!');
this.remoteStream.addTrack(event.track);
});
this.pc.addEventListener('icegatheringstatechange', event => {
console.log('icegatheringstatechange', this.pc.iceGatheringState);
});
}
onLocalIce(event) {
if (event.candidate) {
console.log('Sending candidate');
this.send('wrtc:ice', {
2020-04-12 23:33:24 +00:00
ice: event.candidate,
2020-04-12 14:25:42 +00:00
});
}
}
async onRemoteOffer(payload) {
const offer = new RTCSessionDescription(payload.sdp);
await this.pc.setRemoteDescription(offer);
2020-04-12 23:33:24 +00:00
console.log('Remote offer Set', offer.sdp);
2020-04-12 14:25:42 +00:00
const sdp = await this.pc.createAnswer();
this.send('wrtc:sdp:answer', {
sdp
});
await this.pc.setLocalDescription(sdp);
2020-04-12 23:33:24 +00:00
console.log('Local answer Set', sdp.sdp);
2020-04-12 14:25:42 +00:00
return true;
}
async onRemoteAnswer(payload) {
const answer = new RTCSessionDescription(payload.sdp);
await this.pc.setRemoteDescription(answer);
2020-04-12 23:33:24 +00:00
console.log('Remote answer Set', answer.sdp);
2020-04-12 14:25:42 +00:00
return true;
}
async onRemoteIce(payload) {
const ice = payload.ice;
await this.pc.addIceCandidate(ice);
return true;
}
2020-04-12 23:33:24 +00:00
async getUserMedia(constraints: MediaStreamConstraints = { video: true, audio: true }) {
2020-04-12 14:25:42 +00:00
if (this.localStream) return this.localStream;
this.localStream = await navigator.mediaDevices.getUserMedia(constraints);
return this.localStream;
}
getRemoteStream() {
return this.remoteStream;
}
close() {
2020-04-12 23:33:24 +00:00
console.log('Closing...');
if (!this.inCall) return;
this.emit(ECallEvents.CLOSE, this.callId);
if (this.signalingChannel) this.signalingChannel.close();
this.signalingChannel = null;
if (this.pc) this.pc.close();
if (this.localStream) this.localStream.getTracks().forEach(t => t.stop());
this.localStream = null;
this.remoteStream = null;
2020-04-12 14:25:42 +00:00
this.inCall = false;
}
}
2020-04-12 23:33:24 +00:00
export enum ECallEvents {
CLOSE = 'CLOSE',
REMOTE_STREAM = 'REMOTE_STREAM'
}