Skip to content

Commit

Permalink
Fix loop when an SDP offer is received without video (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
giavac authored Sep 24, 2024
1 parent f5d3fdb commit 09cfb69
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
34 changes: 32 additions & 2 deletions packages/common/src/webrtc/Peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { isFunction } from '../util/helpers'
import { CallOptions } from './interfaces'
import { trigger } from '../services/Handler'
import { filterIceServers } from './helpers'
import { sdpHasAudio, sdpHasVideo } from './helpers'

export default class Peer {
public instance: RTCPeerConnection
Expand Down Expand Up @@ -123,12 +124,41 @@ export default class Peer {
}
}

private _getSharedConstraints(localConstraints, sdp: string = '') {
const localAudio = localConstraints?.audio ?? false
const remoteAudio = sdp ? sdpHasAudio(sdp) : false
const localVideo = localConstraints?.video ?? false
const remoteVideo = sdp ? sdpHasVideo(sdp) : false

const sharedConstraints = {
audio: localAudio && remoteAudio,
video: localVideo && remoteVideo
}

return sharedConstraints
}

private async _retrieveLocalStream() {
if (streamIsValid(this.options.localStream)) {
return this.options.localStream
}
const constraints = await getMediaConstraints(this.options)
return getUserMedia(constraints)
const localConstraints = await getMediaConstraints(this.options)
let sharedConstraints = localConstraints

// If the local stream is requested for an answer,
// then check whether the offer contains audio and/or video
// and only call getUserMedia with the capabilities that match
// local constraints with the offer constraints.
// The most typical scenario is an offer with audio only: we
// don't want to call getUserMedia with video in this case.
if (this._isAnswer()) {
const { remoteSdp, useStereo } = this.options
const sdp = useStereo ? sdpStereoHack(remoteSdp) : remoteSdp
const sessionDescr: RTCSessionDescription = sdpToJsonHack({ sdp, type: PeerType.Offer })
sharedConstraints = this._getSharedConstraints(localConstraints, sessionDescr.sdp)
}

return getUserMedia(sharedConstraints)
}

private _isOffer(): boolean {
Expand Down
12 changes: 11 additions & 1 deletion packages/common/src/webrtc/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,14 @@ const filterIceServers = (servers: RTCIceServer[], options: FilterIceServersOpti
}))
}

const sdpHasAudio = (sdp: string = ''): boolean => {
return /m=audio/.test(sdp)
}

const sdpHasVideo = (sdp: string = ''): boolean => {
return /m=video/.test(sdp)
}

export {
getUserMedia,
getDevices,
Expand All @@ -329,5 +337,7 @@ export {
enableVideoTracks,
disableVideoTracks,
toggleVideoTracks,
filterIceServers
filterIceServers,
sdpHasAudio,
sdpHasVideo
}

0 comments on commit 09cfb69

Please sign in to comment.