SDP add remote answer.

This commit is contained in:
Miroslav Šedivý 2021-11-28 18:36:36 +01:00
parent ed8fab1afa
commit b03fad914b
2 changed files with 36 additions and 12 deletions

View File

@ -141,27 +141,45 @@ export class NekoMessages extends EventEmitter<NekoEvents> {
protected async [EVENT.SIGNAL_PROVIDE]({ sdp, video, iceservers }: message.SignalProvide) { protected async [EVENT.SIGNAL_PROVIDE]({ sdp, video, iceservers }: message.SignalProvide) {
this._localLog.debug(`EVENT.SIGNAL_PROVIDE`) 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) Vue.set(this._state.connection.webrtc, 'video', video)
} }
protected async [EVENT.SIGNAL_OFFER]({ sdp }: message.SignalDescription) { protected async [EVENT.SIGNAL_OFFER]({ sdp }: message.SignalDescription) {
this._localLog.debug(`EVENT.SIGNAL_OFFER`) this._localLog.debug(`EVENT.SIGNAL_OFFER`)
this.emit('connection.webrtc.sdp', 'remote', sdp)
// set remote offer
await this._connection.webrtc.setOffer(sdp) 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) { protected async [EVENT.SIGNAL_RESTART]({ sdp }: message.SignalDescription) {
this[EVENT.SIGNAL_OFFER]({ sdp }) 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._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) this.emit('connection.webrtc.sdp.candidate', 'remote', candidate)
} }

View File

@ -63,11 +63,11 @@ export class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
return return
} }
this._peer.addIceCandidate(candidate) await this._peer.addIceCandidate(candidate)
this._log.debug(`adding remote ICE candidate`, { candidate }) this._log.debug(`adding remote ICE candidate`, { candidate })
} }
public async connect(sdp: string, iceServers: ICEServer[]) { public async connect(iceServers: ICEServer[]) {
if (!this.supported) { if (!this.supported) {
throw new Error('browser does not support webrtc') throw new Error('browser does not support webrtc')
} }
@ -170,8 +170,6 @@ export class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
this._peer.ontrack = this.onTrack.bind(this) this._peer.ontrack = this.onTrack.bind(this)
this._peer.ondatachannel = this.onDataChannel.bind(this) this._peer.ondatachannel = this.onDataChannel.bind(this)
await this.setOffer(sdp)
} }
public async setOffer(sdp: string) { public async setOffer(sdp: string) {
@ -183,7 +181,7 @@ export class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
if (this._candidates.length > 0) { if (this._candidates.length > 0) {
for (const candidate of this._candidates) { 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 }) this._log.debug(`added ${this._candidates.length} remote ICE candidates`, { candidates: this._candidates })
@ -191,7 +189,7 @@ export class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
} }
const answer = await this._peer.createAnswer() const answer = await this._peer.createAnswer()
this._peer!.setLocalDescription(answer) this._peer.setLocalDescription(answer)
if (answer) { if (answer) {
this.emit('negotiation', answer) this.emit('negotiation', answer)
@ -200,6 +198,14 @@ export class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
} }
} }
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 { public addTrack(track: MediaStreamTrack, ...streams: MediaStream[]): RTCRtpSender {
if (!this._peer) { if (!this._peer) {
throw new Error('attempting to add track for nonexistent peer') throw new Error('attempting to add track for nonexistent peer')