Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addStream and sdpTransform issue #367

Closed
igorcouto opened this issue Oct 13, 2018 · 17 comments
Closed

addStream and sdpTransform issue #367

igorcouto opened this issue Oct 13, 2018 · 17 comments
Labels

Comments

@igorcouto
Copy link

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!

@t-mullen
Copy link
Collaborator

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.

@igorcouto
Copy link
Author

igorcouto commented Oct 13, 2018

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,
    });

@t-mullen
Copy link
Collaborator

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.

@igorcouto
Copy link
Author

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.

@t-mullen
Copy link
Collaborator

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?

@igorcouto
Copy link
Author

Exactly. And after I add stream later, I cannot add this sdpTransform function, disallowing bitrate control.

@t-mullen
Copy link
Collaborator

t-mullen commented Oct 13, 2018

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.

@igorcouto
Copy link
Author

WoW. Nice. So this function will get called at each renegotiation. That's my point.

Thank you very much!

@t-mullen
Copy link
Collaborator

Yup, every offer/answer is passed through sdpTransform.

No problem 👍

@igorcouto
Copy link
Author

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.

@t-mullen
Copy link
Collaborator

Can you confirm that stream is a MediaStream?

If so, what browser and version?

@igorcouto
Copy link
Author

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.

@t-mullen
Copy link
Collaborator

Are you using some kind of webrtc shim or something that might have removed the getTracks method off of your MediaStream?

@igorcouto
Copy link
Author

igorcouto commented Oct 13, 2018

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.

@t-mullen
Copy link
Collaborator

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.

@athina635
Copy link

athina635 commented May 2, 2020

@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 the on('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?

@nazar-pc
Copy link
Collaborator

nazar-pc commented May 2, 2020

@athina635 asking new questions in comments of old already closed issues is not productive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants