feross / simple-peer

📡 Simple WebRTC video, voice, and data channels
MIT License
7.32k stars 972 forks source link

hello, i use firebase and simplepeer to pass the stream but it doesn't work? #349

Closed james14123 closed 5 years ago

james14123 commented 6 years ago

hello i'm new!

I choose the Firebase to transfer data between peer and peer. It seems like the connection is established But the stream doesn't play :( Is anything i missed?

thanks!


navigator.getUserMedia({ video: true, audio: false }, gotMedia, function (error) {console.log("failed")});

        function gotMedia(stream){  
            var myvideo= document.getElementById("myvideo");
            myvideo.srcObject = stream;
            myvideo.play();

        const peer = new SimplePeer({initiator: true, stream: stream });
        peer.on('signal', (signalData) => {

            fbRef.child( "Players/" + playerID ).set({
                id: playerID,
                data: JSON.stringify(signalData)
            });

        });

        // Listen for new players
        fbRef.child( "Players/").on('child_added', (res) => {   

            if(res.val().id != playerID){
                // Create Peer channel
                const peer1 = new SimplePeer();
                // Listen for signaling data from specific player
                peer1.signal(res.val().data);           

                peer1.on('stream', function (stream) {
                // got remote video stream, now let's show it in a video tag
                if(stream != undefined){    
                    console.log("RemoteVideo Play "); 
                    var remotevideo = document.getElementById('remotevideo');
                    remotevideo.srcObject = stream;
                    remotevideo.play();
                    }else console.log("stream = undefined!")
                })      
        }
});
t-mullen commented 5 years ago

You are only listening to the signal event on one peer. Both peers will fire the signal event, and the data from this event needs to be passed into the other peer through peer.signal().

If you're going to manually pass signaling data like this, you should disable trickle with trickle:false in both peer's options. Otherwise you will need to pass dozens of signalling messages.

james14123 commented 5 years ago

ohh i see! Is there any simple way to pass signaling data ?

t-mullen commented 5 years ago

You're almost there, just make sure all signal events are listened to on both sides and that the data makes into a call to signal() on the other peer.

Also keep in mind that each peer can only connect to one other peer, you don't want to reuse signaling data for different peers. (Not familiar with firebase, so not sure if that's happening here).

t-mullen commented 5 years ago

Closing due due to inactivity, and not sure this is an issue. Feel free to continue commenting.