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

mar*_*223 3 javascript webrtc

我正在尝试附加使用getusermedia()on捕获的流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);
                });
            });
        };

Run Code Online (Sandbox Code Playgroud)

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);
    }


Run Code Online (Sandbox Code Playgroud)

Phi*_*cke 6

addStream is a method that has been removed from the standard and Safari doesn't implement it. Either switch to the addTrack method by replacing

peerConnection.addStream(options.stream);
Run Code Online (Sandbox Code Playgroud)

with

options.stream.getTracks().forEach(track => peerConnection.addTrack(track, options.stream))
Run Code Online (Sandbox Code Playgroud)

or include adapter.js in your project which polyfills addStream