如何选择输入的视频设备,以实现WebRTC?(How to choose input video d

2019-07-19 00:47发布

我学习WebRTC应用。

我引用这个软件

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

演示https://apprtc.appspot.com/

我的电脑有bult的视频设备和apprtc使用该视频设备。 不过,我想使用USB视频摄像机来代替。

我在寻找改变输入视频设备的方式。 但我找不到在源文件中的任何线索。

没有任何人有信息?

Answer 1:

在Chrome:

chrome://settings/content/camera
chrome://settings/content/microphone

在Firefox:media.navigator.permission.disabled = FALSE



Answer 2:

试试这个演示其捕获所有的音频/视频输入设备:

https://www.webrtc-experiment.com/demos/MediaStreamTrack.getSources.html

您可以使用相同的API捕获任何“特殊”设备。

编辑在2014年3月1日:

MediaStreamTrack.getSources(function (media_sources) {
    for (var i = 0; i < media_sources.length; i++) {
        var media_source = media_sources[i];
        var constraints = {};

        // if audio device
        if (media_source.kind == 'audio') {
            constraints.audio = {
                optional: [{
                    sourceId: media_source.id
                }]
            };
        }

        // if video device
        if (media_source.kind == 'video') {
            constraints.video = {
                optional: [{
                    sourceId: media_source.id
                }]
            };
        }


        // invoke getUserMedia to capture this device
        navigator.webkitGetUserMedia(constraints, function (stream) {
            console.log(stream.id, stream);
        }, console.error);
    }
});

更新时间2015年9月5日:

现在微软边缘,镀铬44+,火狐38+,所有这些浏览器都支持navigator.mediaDevices.enumerateDevices API。

这里是一个可重复使用的脚本,所有这些媒体来源的API提供跨浏览器的垫片 。 它将工作,即使在旧的铬(43岁以上)(即使在Android设备上):

if (navigator.mediaDevices && navigator.mediaDevices.enumerateDevices) {
    // Firefox 38+, Microsoft Edge, and Chrome 44+ seems having support of enumerateDevices
    navigator.enumerateDevices = function(callback) {
        navigator.mediaDevices.enumerateDevices().then(callback);
    };
}

function getAllAudioVideoDevices(successCallback, failureCallback) {
    if (!navigator.enumerateDevices && window.MediaStreamTrack && window.MediaStreamTrack.getSources) {
        navigator.enumerateDevices = window.MediaStreamTrack.getSources.bind(window.MediaStreamTrack);
    }

    if (!navigator.enumerateDevices && navigator.mediaDevices.enumerateDevices) {
        navigator.enumerateDevices = navigator.mediaDevices.enumerateDevices.bind(navigator);
    }

    if (!navigator.enumerateDevices) {
        failureCallback(null, 'Neither navigator.mediaDevices.enumerateDevices NOR MediaStreamTrack.getSources are available.');
        return;
    }

    var allMdiaDevices = [];
    var allAudioDevices = [];
    var allVideoDevices = [];

    var audioInputDevices = [];
    var audioOutputDevices = [];
    var videoInputDevices = [];
    var videoOutputDevices = [];

    navigator.enumerateDevices(function(devices) {
        devices.forEach(function(_device) {
            var device = {};
            for (var d in _device) {
                device[d] = _device[d];
            }

            // make sure that we are not fetching duplicate devics
            var skip;
            allMdiaDevices.forEach(function(d) {
                if (d.id === device.id) {
                    skip = true;
                }
            });

            if (skip) {
                return;
            }

            // if it is MediaStreamTrack.getSources
            if (device.kind === 'audio') {
                device.kind = 'audioinput';
            }

            if (device.kind === 'video') {
                device.kind = 'videoinput';
            }

            if (!device.deviceId) {
                device.deviceId = device.id;
            }

            if (!device.id) {
                device.id = device.deviceId;
            }

            if (!device.label) {
                device.label = 'Please invoke getUserMedia once.';
            }

            if (device.kind === 'audioinput' || device.kind === 'audio') {
                audioInputDevices.push(device);
            }

            if (device.kind === 'audiooutput') {
                audioOutputDevices.push(device);
            }

            if (device.kind === 'videoinput' || device.kind === 'video') {
                videoInputDevices.push(device);
            }

            if (device.kind.indexOf('audio') !== -1) {
                allAudioDevices.push(device);
            }

            if (device.kind.indexOf('video') !== -1) {
                allVideoDevices.push(device);
            }

            // there is no 'videoouput' in the spec.
            // so videoOutputDevices will always be [empty]

            allMdiaDevices.push(device);
        });

        if (successCallback) {
            successCallback({
                allMdiaDevices: allMdiaDevices,
                allVideoDevices: allVideoDevices,
                allAudioDevices: allAudioDevices,
                videoInputDevices: videoInputDevices,
                audioInputDevices: audioInputDevices,
                audioOutputDevices: audioOutputDevices
            });
        }
    });
}

下面是如何使用可重复使用的上述跨浏览器的垫片:

getAllAudioVideoDevices(function(result) {
    if (result.allMdiaDevices.length) {
        console.debug('Number of audio/video devices available:', result.allMdiaDevices.length);
    }

    if (result.allVideoDevices.length) {
        console.debug('Number of video devices available:', result.allVideoDevices.length);
    }

    if (result.allAudioDevices.length) {
        console.debug('Number of audio devices available:', result.allAudioDevices.length);
    }

    if (result.videoInputDevices.length) {
        console.debug('Number of video-input devices available:', result.videoInputDevices.length);
    }

    if (result.audioInputDevices.length) {
        console.debug('Number of audio-input devices available:', result.audioInputDevices.length);
    }

    if (result.audioOutputDevices.length) {
        console.debug('Number of audio-output devices available:', result.audioOutputDevices.length);
    }

    if (result.allMdiaDevices.length && result.allMdiaDevices[0].label === 'Please invoke getUserMedia once.') {
        console.warn('It seems you did not invoke navigator-getUserMedia before using these API.');
    }

    console.info('All audio input devices:');
    result.audioInputDevices.forEach(function(device) {
        console.log('Audio input device id:', device.id, 'Device label:', device.label);
    });

    console.info('All audio output devices:');
    result.audioOutputDevices.forEach(function(device) {
        console.log('Audio output device id:', device.id, 'Device label:', device.label);
    });

    console.info('All video input devices:');
    result.videoInputDevices.forEach(function(device) {
        console.log('Video input device id:', device.id, 'Device label:', device.label);
    });
}, function(error) {
    alert(error);
});


Answer 3:

事实证明,Chrome浏览器不支持MediaStreamTrack API,它可以让你做到这一点。 在Firefox这个API仍然处于试验阶段。 这里是Chrome执行:

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);


Answer 4:

这听起来我你正在寻找facingMode 。 你可以看看这个文件: http://www.w3.org/TR/2013/WD-mediacapture-streams-20130516/#idl-def-AllVideoCapabilities

不知道它是如何尚未虽然支持。



文章来源: How to choose input video device for webrtc?
标签: webrtc