【发布时间】:2020-11-14 08:02:16
【问题描述】:
我目前正在为 WebRTC 多点连接工作。我想实现在通话时从前到后切换摄像头的功能。 这是我用来切换相机的代码
async function changevideo() {
const audioSource = audioInputSelect.value;
const videoSource = videoSelect.options[videoSelect.selectedIndex].value;
var tempconstraints ={
video: {
deviceId: videoSource ? { exact: videoSource } : undefined,
width: { max: 320 },
height: { max: 240 }
},
audio: { deviceId: audioSource ? { exact: audioSource } : undefined },
};
var newstream = await navigator.mediaDevices.getUserMedia(tempconstraints);
if (connections[socketId]) {
Promise.all(connections[socketId].getSenders().map(function (sender) {
debugger;
return sender.replaceTrack(newstream.getTracks().find(function (track) {
debugger;
return track.kind === sender.track.kind;
})).then(data =>
{
console.log(data);
});;
}));
var track = localStream.getTracks().find(function (track) { return track.kind == videoTrack.kind });
localStream.removeTrack(track);
localStream.addTrack(videoTrack);
connections[tempsocketid].onnegotiationneeded = function () {
connections[tempsocketid].createOffer().then(function (offer) {
return connections[tempsocketid].setLocalDescription(offer);
}).then(function () {
socket.emit('signal', socketId, JSON.stringify({ 'sdp': connections[tempsocketid].localDescription, 'room': roomNumber }), roomNumber);
}).catch(e => console.log(e));
}
}
}
这里connections包含了所有连接类型的RTCpeerconnection详细信息。
socketId 是我要切换摄像头的主用户 ID。所以,connections[socketId] 给了我带有 socketId 的用户的 RTCPeerConnection 详细信息。
newstream是切换相机后的流。
如果我直接将视频的 src 更新到 newstream,那么我的相机只会在我的设备上发生变化。
我进行了很多搜索,但到处都可以找到使用 replaceTrack 的解决方案,但在我的情况下它并不适用。每次我使用它时,屏幕上都没有任何反应,控制台中也没有任何错误。
更新
我使用了onnegotiationneeded 与删除和添加轨道。
tempsocketid 是另一个已连接用户的 socketId。
所以我有 2 个用户,一个用户的 socketid 存储在 socketId 中,另一个用户的 socketid 存储在 tempsocketid 中。所以目前我正在尝试使用 socketid socketId 切换用户的相机
当调用协商时,我在另一个用户控制台中遇到错误。
DOMException: Failed to execute 'addIceCandidate' on 'RTCPeerConnection': Error processing ICE candidate
【问题讨论】:
标签: javascript camera webrtc