Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用WebRTC切换摄像机_Javascript_Node.js_Webrtc - Fatal编程技术网

Javascript 如何使用WebRTC切换摄像机

Javascript 如何使用WebRTC切换摄像机,javascript,node.js,webrtc,Javascript,Node.js,Webrtc,我目前正在处理WebRTC多EER连接。我希望能够切换正在通话中使用的相机,而不必在设置中更改所选相机。 我遵循了来自的代码,它可以工作,但只限于客户端 devices.js “严格使用”; const videoElement=document.querySelector(“#local”); const audioInputSelect=document.querySelector('select#audioSource'); const audioOutputSelect=document

我目前正在处理WebRTC多EER连接。我希望能够切换正在通话中使用的相机,而不必在设置中更改所选相机。 我遵循了来自的代码,它可以工作,但只限于客户端

devices.js

“严格使用”;
const videoElement=document.querySelector(“#local”);
const audioInputSelect=document.querySelector('select#audioSource');
const audioOutputSelect=document.querySelector('select#audioOutput');
const videoSelect=document.querySelector('select#videoSource');
常量选择器=[audioInputSelect,audioOutputSelect,videoSelect];
audioOutputSelect.disabled=!('sinkId'在htmlemedielement.prototype中);
功能设备(设备信息){
//多次调用句柄以更新标签。保留值。
const values=selectors.map(select=>select.value);
选择器。forEach(select=>{
while(选择.firstChild){
select.removeChild(select.firstChild);
}
});
for(设i=0;i!==deviceinfo.length;++i){
const deviceInfo=deviceInfo[i];
const option=document.createElement('option');
option.value=deviceInfo.deviceId;
如果(deviceInfo.kind=='audioinput'){
option.text=deviceInfo.label | | `麦克风${audioInputSelect.length+1}`;
audioInputSelect.appendChild(选项);
}else if(deviceInfo.kind=='audiooutput'){
option.text=deviceInfo.label | | `speaker${audioOutputSelect.length+1}`;
audioOutputSelect.appendChild(选项);
}else if(deviceInfo.kind=='videoinput'){
option.text=deviceInfo.label | |`camera${videoSelect.length+1}`;
videoSelect.appendChild(选项);
}否则{
log('某种其他类型的源/设备:',deviceInfo);
}
}
选择器。forEach((select,selectorIndex)=>{
if(Array.prototype.slice.call(select.childNodes).some(n=>n.value==values[selectorIndex])){
select.value=值[selectorIndex];
}
});
}
navigator.mediaDevices.enumerateDevices().then(gotDevices.catch)(handleError);
//使用设备/接收器ID将音频输出设备连接到视频元素。
函数attachSinkId(元素,sinkId){
if(typeof element.sinkId!=“未定义”){
元素。设置sinkId(sinkId)
.然后(()=>{
log(`Success,音频输出设备已连接:${sinkId}`);
})
.catch(错误=>{
让errorMessage=错误;
如果(error.name=='SecurityError'){
errorMessage=`您需要使用HTTPS选择音频输出设备:${error}`;
}
控制台错误(错误消息);
//跳回列表中的第一个输出设备,因为它是默认的。
audioOutputSelect.selectedIndex=0;
});
}否则{
console.warn('浏览器不支持输出设备选择');
}
}
函数changeAudioDestination(){
const audioDestination=audioOutputSelect.value;
attachSinkId(videoElement、audioDestination);
}
函数gotStream(stream){
window.stream=stream;//使流对控制台可用
videoElement.srcObject=流;
//如果标签可用,请刷新按钮列表
返回navigator.mediaDevices.enumerateDevices();
}
函数句柄错误(错误){
日志('navigator.MediaDevices.getUserMedia错误:',error.message,error.name);
}
函数start(){
if(window.stream){
window.stream.getTracks().forEach(track=>{
track.stop();
});
}
const audioSource=audioInputSelect.value;
const videoSource=videoSelect.value;
常量约束={
音频:{deviceId:audioSource?{exact:audioSource}:undefined},
视频:{deviceId:videoSource?{exact:videoSource}:undefined}
};
navigator.mediaDevices.getUserMedia(约束)。然后(gotStream)。然后(gotDevices)。catch(handleError);
}
audioInputSelect.onchange=开始;
audioOutputSelect.onchange=changeAudioDestination;
videoSelect.onchange=开始;
start();
有没有一个简单的方法可以做到这一点?我认为这可能与曲目有关,但不太确定,因为我刚刚开始与WebRTC合作

如果要查看存储库的完整代码,请单击


谢谢

要切换摄像机,必须通过停止第一台摄像机的所有曲目来释放其媒体流,然后必须使用getUserMedia()为另一台摄像机获取另一个媒体流。在这种情况下,浏览器不会再次提示用户请求权限;相机会自动切换。停止磁道时,在RTPeerConnection上调用.removeTrack()。然后,使用新流的跟踪,调用.addTrack()


您可能已经知道这一点,但是如果您有一个开放的媒体流,enumerateDevices()将返回更多有用的信息。这是因为用户已授予权限。

如果要替换发送到远程端的视频,需要调用rtpeerconnection.replaceTrack。像往常一样,

您能给我一个代码示例吗?谢谢另外,我将使用哪个文件RTPeerConnection?
rtc.js
文件与我尝试切换视频的文件不同,这会影响它吗?谢谢