未捕获的类型错误:peerConnection.addstream不是一个函数?

3

我正在尝试通过 getusermedia() 捕获的流附加到 startPeerConnection(stream) 上。我已经尝试了以下方式添加流。

   function startPeerConnection(stream) {
            var configuration = {
                // Uncomment this code to add custom iceServers    //
                "iceServers": [{ "url": "stun:stun.1.google.com:19302" }]
            };

            yourConnection = new RTCPeerConnection(configuration);
            theirConnection = new RTCPeerConnection(configuration);


            // Setup stream listening 
            yourConnection.addStream(stream);//***getting the error on this line***
            theirConnection.onaddstream = function (e) {
                theirVideo.srcObject = e.stream;
                theirVideo.play();
            };


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

            theirConnection.onicecandidate = function (event) {
                if (event.candidate) {
                    yourConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
                }
            };



            // Begin the offer 
            yourConnection.createOffer(function (offer) {
                yourConnection.setLocalDescription(offer);
                theirConnection.setRemoteDescription(offer);
                theirConnection.createAnswer(function (offer) {
                    theirConnection.setLocalDescription(offer);
                    yourConnection.setRemoteDescription(offer);
                });
            });
        };

RTCpeerconnection 是这样的:

var RTCPeerConnection = function(options) {

    var iceServers = options.iceServers || defaults.iceServers;
    var constraints = options.constraints || defaults.constraints;

    var peerConnection = new PeerConnection(iceServers);

    peerConnection.onicecandidate = onicecandidate;
    peerConnection.onaddstream = onaddstream;
    peerConnection.addStream(options.stream);//***getting error on here ***

    function onicecandidate(event) {
        if (!event.candidate || !peerConnection) return;
        if (options.getice) options.getice(event.candidate);
    }

    function onaddstream(event) {
        options.gotstream && options.gotstream(event);
    }



PeerConnection 是从哪里来的?它不在浏览器标准中。 - mahalde
我已经使用了这里的JS文件:https://github.com/muaz-khan/WebRTC-Experiment/tree/master/RTCPeerConnection - marceloo1223
嗯...你正在使用相同名称的函数覆盖“goes like this”中的RTCPeerConnection对象。这是一个不好的想法... - Philipp Hancke
依靠一个标记为“实验”的Github Repo,而这个Repo在6年内从未被碰过,对于WebRTC来说是不明智的选择。它存在着若干问题,我在我重复的问题中列出了其中的一些。 - jib
嗨@jib,感谢您的建议。我在这里尝试了您的演示:https://jsfiddle.net/aynr0k5q/,看起来非常酷。我只想知道如何为此设置远程连接,如果您能帮忙,我将不胜感激。 - marceloo1223
更新了那个 jsfiddle,使用 addTrack 替代了 addStream https://jsfiddle.net/p6d49haj/ - SSpoke
1个回答

7

addStream方法已被从标准中移除,Safari浏览器不实现该方法。 要么使用addTrack方法,通过将

替换为

来切换到替代方法。

peerConnection.addStream(options.stream);

使用

options.stream.getTracks().forEach(track => peerConnection.addTrack(track, options.stream))

或者在你的项目中包含adapter.js,它对addStream进行了填充


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接