[英]How to swap camera sources in webrtc while in a call (javascript APIs)
在iOS上,我可以执行以下操作:
// set a new camera id
cameraId = ([cameraId isEqualToString:frontCameraId]) ? backCameraId : frontCameraId;
// determine if the stream has a video track
BOOL hasActiveVideoTrack = ([self.localMediaStream.videoTracks count] > 0);
// remove video track from the stream
if (hasActiveVideoTrack) {
[self.localMediaStream removeVideoTrack:self.localVideoTrack];
}
// remove renderer from the video track
[self.localVideoTrack removeRenderer:self.localVideoView];
// re init the capturer, video source and video track
localVideoCapturer = nil;
localVideoSource = nil;
localVideoCapturer = [RTCVideoCapturer capturerWithDeviceName:cameraId];
localVideoSource = [peerConnectionFactory videoSourceWithCapturer:localVideoCapturer constraints:mediaConstraints];
// create a new video track
self.localVideoTrack = [peerConnectionFactory videoTrackWithID:@"ARDAMSv0" source:localVideoSource];
[self.localVideoTrack addRenderer:self.localVideoView];
// add video track back to the stream
if (hasActiveVideoTrack) {
[self.localMediaStream addVideoTrack:self.localVideoTrack];
}
在前后摄像头之间切换。 我可以在通话中调用上述代码,遥控器将暂时停止接收帧,然后继续接收帧,但现在从其他摄像机开始。 如何在javascript中做同样的事情? 您没有像在iOS中那样专门创建视频轨道,那么如何告诉流使用其他摄像头设备而不启动新呼叫?
这是一项实验技术
WebRTC Javascript代码示例包含一个摄像机选择示例:
github上有可用的源代码:
使用MediaDevices.enumerateDevices (在chrome和Firefox上)获取视频|音频源
https://developer.mozilla.org/zh-CN/docs/Web/API/MediaDevices/enumerateDevices
不推荐使用MediaStreamTrack.getSources(仅适用于Chrome),而推荐使用 MediaDevices.enumerateDevices()
navigator.mediaDevices.enumerateDevices =
navigator.mediaDevices.enumerateDevices || function() {
return new Promise(function(resolve) {
var infos = [
{kind: 'audioinput', deviceId: 'default', label: '', groupId: ''},
{kind: 'videoinput', deviceId: 'default', label: '', groupId: ''}
];
resolve(infos);
});
};
if (browserDetails.version < 41) {
// Work around http://bugzil.la/1169665
var orgEnumerateDevices =
navigator.mediaDevices.enumerateDevices.bind(navigator.mediaDevices);
navigator.mediaDevices.enumerateDevices = function() {
return orgEnumerateDevices().then(undefined, function(e) {
if (e.name === 'NotFoundError') {
return [];
}
throw e;
});
};
}
}
交换相机, @wpp的@ omar-khaled答案更改 RTCPeerConnection 的MediaStream
_this.rtc.localstream.stop();
_this.rtc.pc.removeStream(_this.rtc.localstream);
gotStream = function (localstream_aud){
var constraints_audio={
audio:true
}
_this.rtc.localstream_aud = localstream_aud;
_this.rtc.mediaConstraints= constraints_audio;
_this.rtc.createOffer();
}
getUserMedia(constraints_audio, gotStream);
gotStream = function (localstream){
var constraints_screen={
audio:false,
video:{
mandatory:{
chromeMediaSource: 'screen'
}
}
}
_this.rtc.localstream = localstream;
_this.rtc.mediaConstraints=constraints_video;
_this.rtc.createStream();
_this.rtc.createOffer();
}
getUserMedia(constraints_video, gotStream);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.