为什么webRTC没有在Firefox中显示远程视频流?

时间:2017-04-26 05:02:08

标签: javascript firefox webrtc getusermedia

我正在尝试使用Mozilla Firefox浏览器在我的电脑上运行示例webRTC程序。我有一个带有JavaScript的HTML文件。我试图在页面中显示客户端和同行视频。这是我的HTML文件:

<body>
<div id="container">
    <video id="client"  autoplay></video>
    <video id="peer" autoplay/></video>
</div>
    <script src="script.js"></script>
</body>

和java脚本文件:

function hasUserMedia() {
    navigator.getUserMedia = navigator.getUserMedia ||
                         navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
    return !!navigator.getUserMedia;
};

function hasRTCPeerConnection(){
    window.RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
    window.RTCSessionDescription = window.RTCSessionDescription || window.webkitRTCSessionDescription || window.mozRTCSessionDescription;
    window.RTCIceCandidate = window.RTCIceCandidate || window.webkitRTCIceCandidate || window.mozRTCIceCandidate;

    return !!window.RTCPeerConnection;
};


function startPeerConnection(stream) {
    var configuration = {
        //"iceServers": [{ "url": "stun:stun.1.google.com:19302" }]
        iceServers: [{ url: 'stun:stun.1.google.com:19302' }]
    };
    clientConnection = new RTCPeerConnection(configuration);
    peeerConnection = new RTCPeerConnection(configuration);

    // Setup stream listening
    clientConnection.addStream(stream);
    peeerConnection.onaddstream = function (e) {
        peervideo.src = window.URL.createObjectURL(e.stream);
    };

    // Setup ice handling
    clientConnection.onicecandidate = function (event) {
        if (event.candidate){
            peeerConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
        }
    };

    peeerConnection.onicecandidate = function (event) {
        if (event.candidate) {
            clientConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
        }
    };

    // Begin the offer
    clientConnection.createOffer(function (offer) {
        clientConnection.setLocalDescription(offer);
        peeerConnection.setRemoteDescription(offer);

        peeerConnection.createAnswer(function (offer) {
            peeerConnection.setLocalDescription(offer);
            clientConnection.setRemoteDescription(offer);
        });
    });
}


var clientvideo = document.querySelector("#client"),
    peervideo = document.querySelector("#peer"),
    clientConnection, peeerConnection,stream;

if (hasUserMedia()) {
    navigator.getUserMedia({ video: true, audio: false }, function (mystream) {
            stream = mystream ;
            clientvideo.src = window.URL.createObjectURL(stream);
            if (hasRTCPeerConnection()) {
                startPeerConnection(stream);
            } else {
                alert("Sorry, your browser does not support WebRTC.");
            }
        }, function (error) {
            console.log(error);
        }
    );
} else {
    alert("Sorry, your browser does not support WebRTC.");
} 

我不知道我错过了什么。但客户端视频工作正常,即getUsermedia()。但我看不到任何远程视频。我在这里缺少什么?有什么建议吗?

0 个答案:

没有答案