在 webRTC 中切换摄像头
switch camera in webRTC
如何在 RTCMulticonnection 中切换相机
我获取设备列表及其 ID
var videoDevices = document.getElementById('video-devices');
var audioDevices = document.getElementById('audio-devices');
connection.DetectRTC.load(function() {
connection.DetectRTC.audioInputDevices.forEach(function(device) {
var option = document.createElement('option');
option.innerHTML = device.label;
option.value = device.id;
document.querySelector('select').appendChild(option);
});
connection.DetectRTC.videoInputDevices.forEach(function(device) {
var option = document.createElement('option');
option.innerHTML = device.label;
option.value = device.id;
videoDevices.appendChild(option);
});
});
并尝试通过跟随更改相机。取自 GitHub issue
document.getElementById('switch-camera').onclick = function() {
var videoSourceId = videoDevices.value;
connection.codecs.video = 'VP8';
connection.bandwidth = { // all values in kbits/per/seconds
audio: 192,
video: 512,
screen: 512
};
connection.stopMediaAccess();
setTimeout(() => {
connection.mediaConstraints.video.optional = [{
sourceId: videoSourceId
}];
connection.addStream({audio: true, video: true});
},2000);
};
但不使用相机根本不会改变
我尝试了很多方法但都失败了
这里有一个关于 codepen
的例子
这个来自 WebRTC samples 的例子可能对它做我想做的事情有帮助,但对与 RTCMulticoonection 的集成感到困惑。
如果你想前后切换摄像头,RTCMulticonnection 的 documentation 中有解决方案。
如果您想切换摄像头(即 front/back),这里有适合您的解决方案
function openOrJOinRoom(roomid) {
connection.mediaConstraints = {
audio: true,
video: {
facingMode: {exact : 'environment'}
}
};
connection.openOrJoin(roomid);
}
function selectFrontCameraDuringActiveSession() {
// we need to stop previous video tracks because
// mobile device may not allow us capture
// both front and back camera streams
// at the same time
connection.attachStreams.forEach(function(stream) {
// stop only video tracks
// so that we can recapture video track
stream.getVideoTracks().forEach(function(track) {
track.stop();
});
});
var mediaConstraints = {
audio: false, // NO need to capture audio again
video: {
facingMode: {exact : 'user'}
}
};
navigator.mediaDevices.getUserMedia(mediaConstraints).then(function(frontCamera) {
var frontCameraTrack = frontCamera.getVideoTracks()[0];
connection.getAllParticipants().forEach(function(pid) {
connection.peers[pid].peer.getSenders().forEach(function(sender) {
if (sender.track.kind == 'video') {
sender.replaceTrack(frontCameraTrack);
}
});
});
});
}
function selectBackCameraDuringActiveSession() {
// we need to stop previous video tracks because
// mobile device may not allow us capture
// both front and back camera streams
// at the same time
connection.attachStreams.forEach(function(stream) {
// stop only video tracks
// so that we can recapture video track
stream.getVideoTracks().forEach(function(track) {
track.stop();
});
});
var mediaConstraints = {
audio: false, // NO need to capture audio again
video: {
facingMode: {exact : 'environment'}
}
};
navigator.mediaDevices.getUserMedia(mediaConstraints).then(function(frontCamera) {
var frontCameraTrack = frontCamera.getVideoTracks()[0];
connection.getAllParticipants().forEach(function(pid) {
connection.peers[pid].peer.getSenders().forEach(function(sender) {
if (sender.track.kind == 'video') {
sender.replaceTrack(frontCameraTrack);
}
});
});
});
}
我从 GitHub issue 复制了这个。
请不要忘记添加 adapter.js
希望这个答案对您有所帮助。
如何在 RTCMulticonnection 中切换相机
我获取设备列表及其 ID
var videoDevices = document.getElementById('video-devices');
var audioDevices = document.getElementById('audio-devices');
connection.DetectRTC.load(function() {
connection.DetectRTC.audioInputDevices.forEach(function(device) {
var option = document.createElement('option');
option.innerHTML = device.label;
option.value = device.id;
document.querySelector('select').appendChild(option);
});
connection.DetectRTC.videoInputDevices.forEach(function(device) {
var option = document.createElement('option');
option.innerHTML = device.label;
option.value = device.id;
videoDevices.appendChild(option);
});
});
并尝试通过跟随更改相机。取自 GitHub issue
document.getElementById('switch-camera').onclick = function() {
var videoSourceId = videoDevices.value;
connection.codecs.video = 'VP8';
connection.bandwidth = { // all values in kbits/per/seconds
audio: 192,
video: 512,
screen: 512
};
connection.stopMediaAccess();
setTimeout(() => {
connection.mediaConstraints.video.optional = [{
sourceId: videoSourceId
}];
connection.addStream({audio: true, video: true});
},2000);
};
但不使用相机根本不会改变 我尝试了很多方法但都失败了
这里有一个关于 codepen
的例子这个来自 WebRTC samples 的例子可能对它做我想做的事情有帮助,但对与 RTCMulticoonection 的集成感到困惑。
如果你想前后切换摄像头,RTCMulticonnection 的 documentation 中有解决方案。
如果您想切换摄像头(即 front/back),这里有适合您的解决方案
function openOrJOinRoom(roomid) {
connection.mediaConstraints = {
audio: true,
video: {
facingMode: {exact : 'environment'}
}
};
connection.openOrJoin(roomid);
}
function selectFrontCameraDuringActiveSession() {
// we need to stop previous video tracks because
// mobile device may not allow us capture
// both front and back camera streams
// at the same time
connection.attachStreams.forEach(function(stream) {
// stop only video tracks
// so that we can recapture video track
stream.getVideoTracks().forEach(function(track) {
track.stop();
});
});
var mediaConstraints = {
audio: false, // NO need to capture audio again
video: {
facingMode: {exact : 'user'}
}
};
navigator.mediaDevices.getUserMedia(mediaConstraints).then(function(frontCamera) {
var frontCameraTrack = frontCamera.getVideoTracks()[0];
connection.getAllParticipants().forEach(function(pid) {
connection.peers[pid].peer.getSenders().forEach(function(sender) {
if (sender.track.kind == 'video') {
sender.replaceTrack(frontCameraTrack);
}
});
});
});
}
function selectBackCameraDuringActiveSession() {
// we need to stop previous video tracks because
// mobile device may not allow us capture
// both front and back camera streams
// at the same time
connection.attachStreams.forEach(function(stream) {
// stop only video tracks
// so that we can recapture video track
stream.getVideoTracks().forEach(function(track) {
track.stop();
});
});
var mediaConstraints = {
audio: false, // NO need to capture audio again
video: {
facingMode: {exact : 'environment'}
}
};
navigator.mediaDevices.getUserMedia(mediaConstraints).then(function(frontCamera) {
var frontCameraTrack = frontCamera.getVideoTracks()[0];
connection.getAllParticipants().forEach(function(pid) {
connection.peers[pid].peer.getSenders().forEach(function(sender) {
if (sender.track.kind == 'video') {
sender.replaceTrack(frontCameraTrack);
}
});
});
});
}
我从 GitHub issue 复制了这个。 请不要忘记添加 adapter.js 希望这个答案对您有所帮助。