【发布时间】:2017-05-26 11:24:25
【问题描述】:
我正在 Windows 机器上开发,但目标平台是使用 Chromium 的 Raspberry Pi(这是可选的,任何浏览器都可以)。我将多个相机(比如十几个)连接到 Pi,显然甚至必须使用 USB 集线器。在下面的代码中,每次插入相机甚至是新的浏览器会话时,我似乎都会获得不同的设备 ID。我需要能够从特定的摄像头进行录制、录制视频或图像、打开/关闭摄像头等,但我无法做到这一点,除非我能为每个摄像头获取某种唯一标识符。
我也可以使用视频源标识符来识别摄像机(例如 /dev/video0、/dev/video1、... /dev/videon),但不确定如何在浏览器中执行此操作。
html:
<div id="container">
<h1>Test Page</h1>
<div id="List"></div>
<div class="select">
<p><label for="videoSource">Video source: </label><select id="videoSource"></select></p>
<p><label for="audioOutput">audioOutput source: </label><select id="audioOutput"></select></p>
<p><label for="audioInput">audioInput source: </label><select id="audioInput"></select></p>
</div>
<video muted autoplay></video>
</div>
javascript:
<script>
var DeviceInfo = "";
var videoSelect = document.getElementById("videoSource");
var audioOutputSelect = document.getElementById("audioOutput");
var audioInputSelect = document.getElementById("audioInput");
navigator.mediaDevices.enumerateDevices()
.then(gotDevices)
.catch(errorCallback);
function gotDevices(deviceInfos) {
alert("deviceInfos.length: " + deviceInfos.length);
for (var i = 0; i !== deviceInfos.length; ++i) {
var deviceInfo = deviceInfos[i];
DeviceInfo += "<br>=================";
for (var key in deviceInfo) {
DeviceInfo += "<br>" + key + " => " + deviceInfo[key];
}
var option = document.createElement('option');
option.value = deviceInfo.deviceId;
if (deviceInfo.kind === 'audioinput') {
option.text = deviceInfo.label ||
'Microphone ' + (audioInputSelect.length + 1);
audioInputSelect.appendChild(option);
} else if (deviceInfo.kind === 'audiooutput') {
option.text = deviceInfo.label || 'Speaker ' +
(audioOutputSelect.length + 1);
audioOutputSelect.appendChild(option);
} else if (deviceInfo.kind === 'videoinput') {
option.text = deviceInfo.label || 'Camera ' +
(videoSelect.length + 1);
videoSelect.appendChild(option);
}
}
document.getElementById("List").innerHTML = DeviceInfo;
}
function errorCallback(err) {
alert(err.name + ": " + err.message);
}
</script>
样本输出:
=================
toJSON => function toJSON() { [native code] }
deviceId => 68KeeWjqTyTiECj/vjwuwWSMNXraaUu/sz5CDSnbNg0=
kind => videoinput
label =>
groupId =>
=================
toJSON => function toJSON() { [native code] }
deviceId => wuJ0e0dyB7bUyO3L6MHV6CD8v+FQRRZ0V9oSS/IMebg=
kind => videoinput
label =>
groupId =>
=================
toJSON => function toJSON() { [native code] }
deviceId => BdtXeGDVhh2g68rfu4cOg9yZoS7WdgTNr8nXOThLfPU=
kind => videoinput
label =>
groupId =>
=================
toJSON => function toJSON() { [native code] }
deviceId => mAc/SogzkQKpq8O3Zto64+SlOwsg1kKdXJLxua5t328=
kind => audioinput
label =>
groupId =>
=================
toJSON => function toJSON() { [native code] }
deviceId => n1or62DRNYW0zC4yQVox75nQhBZb0BYR9C/VWB1GLkM=
kind => audioinput
label =>
groupId =>
=================
toJSON => function toJSON() { [native code] }
deviceId => 74K5qAhhroD8esqAYW+9P8jxs4yvdWnPQ1Ia8OYJZqc=
kind => audioinput
label =>
groupId =>
=================
toJSON => function toJSON() { [native code] }
deviceId => rHIqRAFL4ZcfTqJc214llo5XxeDLm+pTG/DoicpOryM=
kind => audioinput
label =>
groupId =>
还要注意标签是空白的(Windows/Firefox),但在 Raspbian/Chromium 中我确实得到了标签。不幸的是它们不是唯一的(例如USB2.0相机(1871:0142))如果我能够以某种方式使用这些标签......这个page说标签“返回一个描述这个设备的标签的DOMString(例如”外部 USB 网络摄像头")。仅在活动 MediaStream 使用期间或已授予持久权限时可用。我不知道什么或如何做“持久权限”。
任何帮助表示赞赏。而且我愿意使用不同的技术,即 fswebcam 以某种方式链接到浏览器(自定义 URL)或 php 或其他任何东西。
【问题讨论】:
标签: javascript raspberry-pi usb webrtc