最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to swap camera sources in webrtc while in a call (javascript APIs) - Stack Overflow

programmeradmin3浏览0评论

On iOS I can do:

// 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];
}

to switch between the front and back camera. I can call above code while in a call and the remote will briefly stop receiving frames and then continue receiving frames, but now from the other camera. How can I do the same in javascript? You don't specifically create a videotrack like I do in iOS, so how do I tell the stream to use a different camera device without starting a new call?

On iOS I can do:

// 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];
}

to switch between the front and back camera. I can call above code while in a call and the remote will briefly stop receiving frames and then continue receiving frames, but now from the other camera. How can I do the same in javascript? You don't specifically create a videotrack like I do in iOS, so how do I tell the stream to use a different camera device without starting a new call?

Share Improve this question edited Sep 8, 2016 at 8:51 Kevin asked Sep 8, 2016 at 8:02 KevinKevin 2,73037 silver badges60 bronze badges 1
  • If you just want to swap between front and back camera you can use facingMode. – jib Commented Sep 8, 2016 at 14:07
Add a ment  | 

1 Answer 1

Reset to default 5

This is an experimental technology


WebRTC Javascript code samples contain an example of camera selection :

  • https://webrtc.github.io/samples/src/content/devices/input-output/

with source code available on github :

  • https://github./webrtc/samples
    • https://github./webrtc/samples/blob/gh-pages/src/js/adapter.js

Get video|audio sources using MediaDevices.enumerateDevices (on chrome and firefox)

  • https://developer.mozilla/en-US/docs/Web/API/MediaDevices/enumerateDevices

  • MediaStreamTrack.getSources (only chrome) was deprecated in favour of 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;
        });
      };
    }
  }

to swap cameras, @omar-khaled answer from @wpp Changing a MediaStream of RTCPeerConnection

  • https://www.webrtc-experiment./docs/how-to-switch-streams.html

_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);
发布评论

评论列表(0)

  1. 暂无评论