简体   繁体   中英

WebRTC video stream error

I'm trying to make a WebRTC video-conference service, and it seems to work, I've receive the other peer stream, but the video is not reproducing.

This is the JavaScript code:

    var localStream, localPeerConnection, remotePeerConnection;

    localPeerConnection = new RTCPeerConnection( null );

    localPeerConnection.onaddstream = function ( event ) {
        // Here is where I receive the other Peer stream
        remote.src = URL.createObjectURL( event.stream ); 
        remote.play();
    }; 

    var local  = document.getElementById('person1'),
        remote = document.getElementById('person2');

    navigator.getUserMedia({ video: true, audio: false }, function ( stream ) {
        local.src = URL.createObjectURL( stream );
        localPeerConnection.addStream( stream );
        localStream = stream;
    }, function ( error ) {
        alert("Error getting your data");
        console.log( "ERROR OCURRED: " + error );
    });


    function createUser () {
        localPeerConnection.createOffer( function ( desc ){
            localPeerConnection.setLocalDescription( desc );
            socket.emit('newConnection', { description: desc });
        });
    };

    socket.on('newUser', function ( description ) {

        localPeerConnection.setRemoteDescription( new RTCSessionDescription( description.description ), function () {

            localPeerConnection.createAnswer( function ( desc ) { 
                localPeerConnection.setLocalDescription( desc ); 
            }, function ( err ) { 
                console.log( err ) 
            });

        }, function ( err ) { 
            console.log(err); 
        });

    });

I don't know why this is happening. The function createUser() is called to start the call. The peer who start the call doesn't get the event onaddstream , maybe that's the problem. When the peer who answer the call, gets the onaddstream event, the function is called, and the stream which receives is the same that the other peer is generating.

Thank's advanced!

I don't see you are handling 'newConnection' on clientside. You are just emitting that only. I think

socket.on('newUser', function ( description ) {

should be changed to

 socket.on('newConnection', function ( description ) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM