Skip to content

Commit

Permalink
Fix loop with onnegotiationneeded with offer with no video
Browse files Browse the repository at this point in the history
  • Loading branch information
giavac committed Sep 17, 2024
1 parent b449b89 commit f91d9d1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 70 deletions.
7 changes: 0 additions & 7 deletions packages/common/src/webrtc/BaseCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,6 @@ export default abstract class BaseCall implements IWebRTCCall {

const { localStream } = this.options
localStream.getAudioTracks().forEach(t => t.stop())


console.log('____________ getting video tracks <-------------------- (audio)')
localStream.getVideoTracks().forEach(t => newStream.addTrack(t))
this.options.localStream = newStream
}
Expand Down Expand Up @@ -203,10 +200,6 @@ export default abstract class BaseCall implements IWebRTCCall {
this.options.camId = deviceId

localStream.getAudioTracks().forEach(t => newStream.addTrack(t))

console.log('____________ getting video tracks <-------------------- (video)')


localStream.getVideoTracks().forEach(t => t.stop())
this.options.localStream = newStream
}
Expand Down
50 changes: 9 additions & 41 deletions packages/common/src/webrtc/Peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ export default class Peer {
}

startNegotiation() {
console.log('________startNegotiation... - setting _negotiating to true! <---------------------------')
this._negotiating = true

if (this._isOffer()) {
this._createOffer()
} else {
console.log('________startNegotiation... - calling _createAnswer() <--------------------------- connection state: ', this.instance.connectionState)
this._createAnswer()
}
}
Expand All @@ -40,73 +38,46 @@ export default class Peer {
this.instance.onsignalingstatechange = event => {
switch (this.instance.signalingState) {
case 'stable':
console.log('________onsignalingstatechange ====== (stable) state: ', this.instance.signalingState, ' - setting _negotiating to false.\\\\')
// Workaround to skip nested negotiations
// Chrome bug: https://bugs.chromium.org/p/chromium/issues/detail?id=740501
this._negotiating = false
console.log('________onsignalingstatechange ====== (stable) state: ', this.instance.signalingState, ' - negotiating: ', this._negotiating, '////')
break
case 'have-remote-offer':
console.log('________onsignalingstatechange ====== have-remote-offer! setting _negotiating to true... ++++++++++++++++++++++++++')
// console.log(' - Current local SDP: ', this.instance.currentLocalDescription?.sdp)
// console.log(' - Current remote SDP: ', this.instance.currentRemoteDescription?.sdp)
// console.log('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
this._negotiating = true
case 'closed':
this.instance = null
break
case 'closed':
this.instance = null
break
default:
console.log('________onsignalingstatechange ====== state: ', this.instance.signalingState, ' negotiating: ', this._negotiating, ' - setting it to true.')
default:
this._negotiating = true
}
}

this.instance.onnegotiationneeded = event => {
console.log('________________onnegotiationneeded________________ signalingState: ', this.instance.signalingState)
if (this._negotiating) {
logger.debug('Skip twice onnegotiationneeded..')
console.log('________________onnegotiationneeded_______________negotiating is TRUE, returning___ signalingState: ', this.instance.signalingState)
return
}
console.log('____onnegotiationneeded.... calling startNegotiation! - signalingState: ', this.instance.signalingState)
this.startNegotiation()
}

console.log('___________ calling _retrieveLocalStream ____________________ this.options: ', this.options)
this.options.localStream = await this._retrieveLocalStream()
.catch(error => {
trigger(SwEvent.MediaError, error, this.options.id)
return null
})

const { localElement, localStream = null, screenShare = false } = this.options

if (streamIsValid(localStream)) {
console.log('____localStream is VALID!.... signalingState: ', this.instance.signalingState)

if (typeof this.instance.addTrack === 'function') {
console.log('____localStream is VALID!....get all the tracks from the stream - signalingState: ', this.instance.signalingState)
localStream.getTracks().forEach(t => this.instance.addTrack(t, localStream))
} else {
// @ts-ignore
this.instance.addStream(localStream)
}

if (screenShare !== true) {
muteMediaElement(localElement)
console.log('____localStream is VALID!....attach localStream as media stream - signalingState: ', this.instance.signalingState)
attachMediaStream(localElement, localStream)
}


} else if (localStream === null) {
console.log('____localStream is nulll.... calling startNegotiation! - signalingState: ', this.instance.signalingState)
this.startNegotiation()
} else {
console.log('____localStream is not valid and NOT null.... signaling state: ', this.instance.signalingState, ' connection state: ', this.instance.connectionState)
}

}

private _createOffer() {
Expand All @@ -124,17 +95,9 @@ export default class Peer {
if (!this._isAnswer()) {
return
}

console.log('>>>>>>>>>>>>>> _createAnswer <<<<<<<<<<<<<<<<< signaling state: ', this.instance.signalingState, ' connection state: ', this.instance.connectionState)

const { remoteSdp, useStereo } = this.options
const sdp = useStereo ? sdpStereoHack(remoteSdp) : remoteSdp
const sessionDescr: RTCSessionDescription = sdpToJsonHack({ sdp, type: PeerType.Offer })


console.log('>>>>>>>>>>>>>> _createAnswer <<<<<<<<<<<<<<<<< remote description: ', sessionDescr.sdp)


this.instance.setRemoteDescription(sessionDescr)
.then(() => this.instance.createAnswer())
.then(this._setLocalDescription.bind(this))
Expand Down Expand Up @@ -189,11 +152,16 @@ export default class Peer {
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.
if (this._isOffer() === false) {
const { remoteSdp, useStereo } = this.options
const sdp = useStereo ? sdpStereoHack(remoteSdp) : remoteSdp
const sessionDescr: RTCSessionDescription = sdpToJsonHack({ sdp, type: PeerType.Offer })
console.log(']]]]]]]]]]]]]]]]]]]]_____________ _retrieveLocalStream - remote description: ', sessionDescr.sdp)
sharedConstraints = this._getSharedConstraints(localConstraints, sessionDescr.sdp)
}

Expand Down
22 changes: 0 additions & 22 deletions packages/common/src/webrtc/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,12 @@ import { DeviceType } from './constants'
import { CallOptions } from './interfaces'

const getUserMedia = async (constraints: MediaStreamConstraints): Promise<MediaStream | null> => {


// constraints = { audio: true, video: false}


logger.info('RTCService.getUserMedia', constraints)
const { audio, video } = constraints
if (!audio && !video) {
return null
}
try {


console.log('__________ calling gUM with constraints: ', constraints)

return await WebRTC.getUserMedia(constraints)
} catch (error) {
logger.error('getUserMedia error: ', error)
Expand All @@ -28,7 +19,6 @@ console.log('__________ calling gUM with constraints: ', constraints)
}

const _constraintsByKind = (kind: string = null): { audio: boolean, video: boolean } => {
console.log('________ kind:', kind)
return {
audio: !kind || kind === DeviceType.AudioIn,
video: !kind || kind === DeviceType.Video
Expand All @@ -47,11 +37,6 @@ const getDevices = async (kind: string = null, fullList: boolean = false): Promi
}
const valid: boolean = devices.length && devices.every((d: MediaDeviceInfo) => (d.deviceId && d.label))
if (!valid) {



console.log('___________ calling gUM inside getDevices _________ kind:', kind)

const stream = await WebRTC.getUserMedia(_constraintsByKind(kind))
WebRTC.stopStream(stream)
return getDevices(kind)
Expand All @@ -78,11 +63,6 @@ console.log('___________ calling gUM inside getDevices _________ kind:', kind)
const resolutionList = [[320, 240], [640, 360], [640, 480], [1280, 720], [1920, 1080]]
const scanResolutions = async (deviceId: string) => {
const supported = []


console.log('___________ calling gUM inside scanResolutions _________')


const stream = await getUserMedia({ video: { deviceId: { exact: deviceId } } })
const videoTrack = stream.getVideoTracks()[0]
for (let i = 0; i < resolutionList.length; i++) {
Expand Down Expand Up @@ -124,8 +104,6 @@ const getMediaConstraints = async (options: CallOptions): Promise<MediaStreamCon
}
}

console.log('_______INSIDE getMediaContraints________ audio: ', audio, ' - video: ', video)

return { audio, video }
}

Expand Down

0 comments on commit f91d9d1

Please sign in to comment.