How to choose input video device for webrtc?

前端 未结 4 1436
無奈伤痛
無奈伤痛 2020-12-01 14:05

I am studying webRTC application.

My reference is this software

apprtc https://code.google.com/p/webrtc/source/browse/trunk/samples/js/apprtc/

demo

4条回答
  •  盖世英雄少女心
    2020-12-01 14:49

    It turns out that Chrome does support MediaStreamTrack API which allows you to do this. In Firefox this API is still experimental. Here is the Chrome implementation:

    if (typeof MediaStreamTrack === 'undefined'){
      alert('This browser does not support MediaStreamTrack.\n\nTry Chrome Canary.');
    } else {
      MediaStreamTrack.getSources( onSourcesAcquired);
    }
    
    function onSourcesAcquired(sources) {
      for (var i = 0; i != sources.length; ++i) {
        var source = sources[i];
        // source.id -> DEVICE ID
        // source.label -> DEVICE NAME
        // source.kind = "audio" OR "video"
        // TODO: add this to some datastructure of yours or a selection dialog
      }
    }
    
    ....
    
    And then when calling getUserMedia, specify the id in the constraints:
    
    var constraints = {
      audio: {
        optional: [{sourceId: selected_audio_source_id}]
      },
      video: {
        optional: [{sourceId: selected_video_source_id}]
      }
    };
    navigator.getUserMedia(constraints, onSuccessCallback, onErrorCallback);
    

提交回复
热议问题