diff --git a/src/component/internal/messages.ts b/src/component/internal/messages.ts index d4ab2de6..de24b28f 100644 --- a/src/component/internal/messages.ts +++ b/src/component/internal/messages.ts @@ -141,27 +141,45 @@ export class NekoMessages extends EventEmitter { protected async [EVENT.SIGNAL_PROVIDE]({ sdp, video, iceservers }: message.SignalProvide) { this._localLog.debug(`EVENT.SIGNAL_PROVIDE`) - this.emit('connection.webrtc.sdp', 'remote', sdp) - await this._connection.webrtc.connect(sdp, iceservers) + // create WebRTC connection + await this._connection.webrtc.connect(iceservers) + + // set remote offer + await this._connection.webrtc.setOffer(sdp) + + this.emit('connection.webrtc.sdp', 'remote', sdp) Vue.set(this._state.connection.webrtc, 'video', video) } protected async [EVENT.SIGNAL_OFFER]({ sdp }: message.SignalDescription) { this._localLog.debug(`EVENT.SIGNAL_OFFER`) - this.emit('connection.webrtc.sdp', 'remote', sdp) + // set remote offer await this._connection.webrtc.setOffer(sdp) + + this.emit('connection.webrtc.sdp', 'remote', sdp) } - // Todo: Use offer event intead. + protected async [EVENT.SIGNAL_ANSWER]({ sdp }: message.SignalDescription) { + this._localLog.debug(`EVENT.SIGNAL_ANSWER`) + this.emit('connection.webrtc.sdp', 'remote', sdp) + + // set remote answer + await this._connection.webrtc.setAnswer(sdp) + } + + // TODO: Use offer event intead. protected async [EVENT.SIGNAL_RESTART]({ sdp }: message.SignalDescription) { this[EVENT.SIGNAL_OFFER]({ sdp }) } - protected [EVENT.SIGNAL_CANDIDATE](candidate: message.SignalCandidate) { + protected async [EVENT.SIGNAL_CANDIDATE](candidate: message.SignalCandidate) { this._localLog.debug(`EVENT.SIGNAL_CANDIDATE`) - this._connection.webrtc.setCandidate(candidate) + + // set remote candidate + await this._connection.webrtc.setCandidate(candidate) + this.emit('connection.webrtc.sdp.candidate', 'remote', candidate) } diff --git a/src/component/internal/webrtc.ts b/src/component/internal/webrtc.ts index 1708a344..827aecc7 100644 --- a/src/component/internal/webrtc.ts +++ b/src/component/internal/webrtc.ts @@ -63,11 +63,11 @@ export class NekoWebRTC extends EventEmitter { return } - this._peer.addIceCandidate(candidate) + await this._peer.addIceCandidate(candidate) this._log.debug(`adding remote ICE candidate`, { candidate }) } - public async connect(sdp: string, iceServers: ICEServer[]) { + public async connect(iceServers: ICEServer[]) { if (!this.supported) { throw new Error('browser does not support webrtc') } @@ -170,8 +170,6 @@ export class NekoWebRTC extends EventEmitter { this._peer.ontrack = this.onTrack.bind(this) this._peer.ondatachannel = this.onDataChannel.bind(this) - - await this.setOffer(sdp) } public async setOffer(sdp: string) { @@ -183,7 +181,7 @@ export class NekoWebRTC extends EventEmitter { if (this._candidates.length > 0) { for (const candidate of this._candidates) { - this._peer.addIceCandidate(candidate) + await this._peer.addIceCandidate(candidate) } this._log.debug(`added ${this._candidates.length} remote ICE candidates`, { candidates: this._candidates }) @@ -191,7 +189,7 @@ export class NekoWebRTC extends EventEmitter { } const answer = await this._peer.createAnswer() - this._peer!.setLocalDescription(answer) + this._peer.setLocalDescription(answer) if (answer) { this.emit('negotiation', answer) @@ -200,6 +198,14 @@ export class NekoWebRTC extends EventEmitter { } } + public async setAnswer(sdp: string) { + if (!this._peer) { + throw new Error('attempting to set answer for nonexistent peer') + } + + this._peer.setRemoteDescription({ type: 'answer', sdp }) + } + public addTrack(track: MediaStreamTrack, ...streams: MediaStream[]): RTCRtpSender { if (!this._peer) { throw new Error('attempting to add track for nonexistent peer')