Simple-peer: AddStream to the peers in the on('Connect') event ?

Created on 2 May 2020  Â·  8Comments  Â·  Source: feross/simple-peer

@nazar-pc 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?

Thank you.

question

All 8 comments

It is possible and should just work if your signaling still works after connection (you can't really use trickle: false for that).

Also "doesn't work !!" is not something I can help with, unless you provide additional specific details.

Hello, that's what i did.

  • I delete trickle: false from both peers.
  • the init peer declared without stream: stream
  • the no init peer declared with stream: stream
  • addStream in the init peer like the following when the connexion established.
peer.on('connect', function (stream) {
                                if (navigator.mediaDevices === undefined) {
                                    navigator.mediaDevices = {};
                                    navigator.mediaDevices.getUserMedia = function (constraintObj) {
                                        let getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
                                        if (!getUserMedia) {
                                            return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
                                        }
                                        return new Promise(function (resolve, reject) {
                                            getUserMedia.call(navigator, constraintObj, resolve, reject);
                                        });
                                    }
                                }
                                navigator.mediaDevices.getUserMedia(constraintObj)
                                    .then(stream => {
                                        video.srcObject = stream
                                        video.volume = 0;
                                        video.play();
                                        peer.addStream(stream);
                                    });
                            });

that worked but i noticed something new:

the signaling data appears more than one time i put a console and i found that console papers a lot of time instead of the first configuration with the stream at the declaration of the to peers and with the trickle: falseoption.

am i right for what I'm doing? is this console appears more and more for trickle;false doesn't exist?

the signaling data appears more than one time

This always happen if trickle: true and is expected. Moreover, that is also happening when you add/remove streams/tracks in runtime as that triggers renegotiation. Which is why I said your signaling should work all the time.

like that, I could connect one peer to another but when I want to add the second peer, the process was hard in terms of performance, the 3rd peer connect just for a few seconds, and connexion failed. and the signaling data occurs a lot and that requests the server in each time and makes it so active between just 2 peers what if we have more than that? for each time the server will request the signaling data to all peers

I am in a similar situation as @athina635 (I need to dynamically add video/audio streams to an established connection).

A "data channel" for text/binary communication is always established, because it's cheap and often useful.

âť” Would it be possible to reuse that data-only channel to "promote" to data + streams channels (leaving the server out of the equation)?

Are there some issues that might arise doing this?

  • security: peer A might "force" streams to peer B ? 🤔
  • escaping: this "hijacking" of the data channel requires escaping of this control message to distinguish it from genuine data message

It is possible, I've done that successfully numerous times.
Hijacking data channel is unlikely because it is encrypted.

Indeed, just read that is was the purpose of the Data channel underlying protocol in the first place!

Currently, it's not practical to use RTCDataChannel for messages larger than 64kiB (16kiB if you want to support cross-browser exchange of data). The problem arises from the fact that SCTP—the protocol used for sending and receiving data on an RTCDataChannel—was originally designed for use as a signaling protocol. It was expected that messages would be relatively small. Support for messages larger than the network layer's MTU was added almost as an afterthought, in case signaling messages needed to be larger than the MTU.

đź“– Source: https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Using_data_channels

Looks like question was answered and there is nothing to be done in simple-peer, so closing, but feel free to continue discussion.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gopchi picture gopchi  Â·  4Comments

mmorainville picture mmorainville  Â·  4Comments

onel picture onel  Â·  5Comments

feross picture feross  Â·  3Comments

jimmywarting picture jimmywarting  Â·  3Comments