gpt4 book ai didi

javascript - 未捕获的 TypeError : peerConnection. addstream 不是一个函数?

转载 作者:行者123 更新时间:2023-12-02 17:25:17 25 4
gpt4 key购买 nike

我正在尝试附加使用 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);
});
});
};

RTCpeer连接如下:

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


最佳答案

addStream 是一个已从标准中删除的方法,Safari 未实现它。通过替换切换到 addTrack 方法

peerConnection.addStream(options.stream);

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

或包括adapter.js在你的项目中,polyfills addStream

关于javascript - 未捕获的 TypeError : peerConnection. addstream 不是一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57106846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com