【发布时间】:2021-04-20 13:15:25
【问题描述】:
我有一个RTCPeerConnection 的实例,其中定义了ontrack:
newConnection.ontrack = receivedStream // remote
一旦 SDP 交换完成并且对等方添加其本地流:
connection.addStream(stream); // local
我看到receivedStream 在每个流中被调用两次 - 检查e.track 显示第一次调用是针对audio 轨道,第二次是针对video 轨道。
奇怪的是,检查e.streams[0] 并为此调用getTracks 给了我两个MediaStreamTracks - 一个用于音频,另一个用于视频:
因此,尽管调用了一次 addStream,但我在 两次 调用 receivedStream 时获得了 四个 MediaStreamTracks。
receivedStream 在这里:
function receivedStream(e) {
var stream = e.streams[0]; // this gets invoked twice when adding one stream!
if (!stream) {
throw new Error("no stream found");
};
// this gets me the corresponding connection
waitForStream(stream.id).then(function (connection) {
// get element
targetVideoElement[0].srcObject = stream; // this now gets called twice per stream - once for audio, once for video
}).catch(function (error) {
// log
});
}
【问题讨论】:
-
“我不记得在以前的实现中发生过这种情况”你能包括你以前在问题中实现的代码吗?
-
@guest271314 - 这是旧代码以某种方式成功配置目标
<video>元素的srcObject以输出音频和视频;我不记得是怎么回事。
标签: javascript webrtc