I have a couple of mountpoints that are set up and I try to switch between them. I realize that I can use the switch component of the API, but for now I am trying to stop the existing stream and watch the new stream. Sometimes this works and sometimes not and there are a few failure situations when it doesnt.
Initially I was doing a stopStream() and a startStream() along the lines of the streaming demo, but (I think) I was getting into trouble with the asynchronous nature of the stop and it was plausible that the watch operation issued from startStream() was happening before the stop operation of the stopStream() completed.
So, now I issue the have the notion of a "pendingStartStream" that once the stopStream has received the "stopping" or "stopped" status, it issues the startStream().
What follows is a bit of the console log where I get an error:
WebRTC error: DOMException: Failed to execute 'setLocalDescription' on 'RTCPeerConnection': Failed to set local answer sdp: Called in wrong state: kStable
error
Prior to the error it does look like there are two instances of "remote description accepted" but I am not sure what would lead to multiple instances of that. I am pretty sure that I only issue a single watch for each stream.
fwiw, whenever I get notification that a stream is stopping or stopped I issue a streaming.hangup. Is that an appropriate strategy?
A variant of this is that I get multiple "preparing" notifications (again, I think only a single watch) which leads to:
WebRTC error: DOMException: Failed to execute 'createAnswer' on 'RTCPeerConnection': PeerConnection cannot create an answer in a state other than have-remote-offer or have-local-pranswer.
Here is the console log after the first viewed stream has been through streaming.hangup.
ca:749 Selected start video id #722562488618236 streamStatus: stopped
ca:387 Stream status preparing
janus.js:1578 Creating PeerConnection
janus.js:1586 Preparing local SDP and gathering candidates (trickle=true)
janus.js:1618 Handling Remote Track
ca:486 Starting bitrate timer
janus.js:1625 Adding onended callback to track: MediaStreamTrack {kind: "audio", id: "janusa0", label: "janusa0", enabled: true, muted: false, …}
janus.js:1618 Handling Remote Track
janus.js:1625 Adding onended callback to track: MediaStreamTrack {kind: "video", id: "janusv0", label: "janusv0", enabled: true, muted: false, …}
janus.js:1677 Remote description accepted!
janus.js:2436 Creating answer (iceDone=false)
ca:387 Stream status preparing
janus.js:1723 Updating existing media session
janus.js:2561 Setting local description
janus.js:1677 Remote description accepted!
janus.js:2436 Creating answer (iceDone=false)
ca:428 WebRTC error: DOMException: Failed to execute 'createAnswer' on 'RTCPeerConnection': PeerConnection cannot create an answer in a state other than have-remote-offer or have-local-pranswer.
error @ ca:428