Hey guys,
I'm using sdp transform to control bitrate. This is very important for us for performance reasons (room can be up to 8 people).
If I pass a running stream on setupPeer, everything goes fine. But, if I do not pass a valid stream, I cannot use sdpTransform.
Is there a way to add latter stream with sdp transform, after peer connection is established?
Thanks!
We do transform the SDP on renegotiation (which happens after addStream), even if you don't pass an initial stream.
Not all SDP attributes can be renegotiated and many of them have questionable behaviour on renegotiation. Knowing what SDP attributes you're trying to change would help.
export const setMediaBitrate = (sdp, mediaType, bitrate) => {
const sdpLines = sdp.split('\n');
let mediaLineIndex = -1;
const mediaLine = 'm=${mediaType}';
let bitrateLineIndex = -1;
const bitrateLine = 'b=AS:${bitrate}';
mediaLineIndex = sdpLines.findIndex(line => line.startsWith(mediaLine));
// If we find a line matching “m={mediaType}”
if (mediaLineIndex && mediaLineIndex < sdpLines.length) {
// Skip the media line
bitrateLineIndex = mediaLineIndex + 1;
// Skip both i=* and c=* lines (bandwidths limiters have to come afterwards)
while (sdpLines[bitrateLineIndex].startsWith('i=') || sdpLines[bitrateLineIndex].startsWith('c=')) {
bitrateLineIndex += 1;
}
if (sdpLines[bitrateLineIndex].startsWith('b=')) {
// If the next line is a b=* line, replace it with our new bandwidth
sdpLines[bitrateLineIndex] = bitrateLine;
} else {
// Otherwise insert a new bitrate line.
sdpLines.splice(bitrateLineIndex, 0, bitrateLine);
}
}
// Then return the updated sdp content as a string
return sdpLines.join('\n');
};`
I'm setting audio and video bitrate using sdpTransform...
const peer = new SimplePeer({
initiator,
channelName: peerUserId,
stream: this.stream,
config: {
// iceTransportPolicy: "relay",
iceServers: rtcServers,
},
trickle: false,
sdpTransform: (sdp) => {
const sdp2 = setMediaBitrate(setMediaBitrate(sdp, 'video', this.videoBandwidth), 'audio', this.audioBandwidth);
console.log(sdp2);
return sdp2;
},
// wrtc: {}, // RTCPeerConnection/RTCSessionDescription/RTCIceCandidate
reconnectTimer: 5000,
objectMode: false,
});
I'm fairly certain bitrate can be renegotiated.
Can check that the offer/answers emitted in the signal events actually have these bitrate lines? That would determine if the transform is actually being applied (it should be). Posting them would help too.
The problem is that before sending the stream I cannot use this function as sdpTransform, because signal events does not contains media bitrate lines.
What I need is something like setting sdpTransform in addStream.
I don't follow. The sdpTransform function supplied in the options is applied to ALL offers and answers, from addStream or otherwise.
Is your issue that the initial offer doesn't have any media lines (because you haven't added any stream) so you can't use your transform on it?
Exactly. And after I add stream later, I cannot add this sdpTransform function, disallowing bitrate control.
Your sdpTransform function just needs to check if there's media lines. Bitrate lines will be added when there are media lines, and bitrate will be controlled like you want.
if (mediaLineIndex > -1 && mediaLineIndex < sdpLines.length) {
findIndex returns -1 when it can't find anything, and -1 is truthy.
WoW. Nice. So this function will get called at each renegotiation. That's my point.
Thank you very much!
Yup, every offer/answer is passed through sdpTransform.
No problem 👍
Hi t-mullen, pardon me.
Now sdp is working, but I'm getting an error:
index.js:247 Uncaught TypeError: stream.getTracks is not a function
at Peer../node_modules/simple-peer/index.js.Peer.addStream (index.js:247)
when I
this.peer.addStream(stream);
Do you have some clue? Thank you very much and sorry again.
Can you confirm that stream is a MediaStream?
If so, what browser and version?
yes. MediaStream.
MediaStream
active: true
id: "ykMgyx8dKMuRHjfCcXv0IkpS6tPHEaRTEQc5"
onactive: null
onaddtrack: null
oninactive: null
onremovetrack: null
__proto__: MediaStream
Chrome, 69.0.
Safari also does not work.
Are you using some kind of webrtc shim or something that might have removed the getTracks method off of your MediaStream?
Removing webrtc-adapter package clears the error, but I can't still send stream to other peers.
Do you have some instruction? I do not want to waste your time, perhaps I will have to dig more.
You might want to open an issue with webrtc-adapter about why MediaStreams don't have the getTracks method.
As for not being able to send the stream, a lot of things could be causing that. Maybe your receiving peer is still using your webrtc-adapter build? Hard to tell.
@t-mullen Hello, i want to connect the two peers as well without adding the stream attribute when i defined them. i make sure the connexion has been established in theon('connect') event. at that moment i want to add the stream with the navigator.getUserMedia as you mentioned in the documentation and let the peers share the video. but that doesn't work !!
is that possible?
what if just one of the two peers want to add a stream somewhere and not both ? i mean one of them already defined the peer with stream attribute and the other doesn't.
is that possible?
@athina635 asking new questions in comments of old already closed issues is not productive.
Most helpful comment
Yup, every offer/answer is passed through sdpTransform.
No problem 👍