我正在Windows機器上開發,但目標平臺是使用Chromium的Raspberry Pi(這是可選的,任何瀏覽器都可以)。我將多臺攝像機(比如一打)連接到Pi,顯然甚至必須使用USB集線器。在下面的代碼中,每次插入攝像頭或甚至在新的瀏覽器會話中,我都會看到不同的設備ID。我需要能夠從特定的相機錄製,錄製視頻或圖像,打開/關閉相機等,但除非我能爲每臺相機獲取某種唯一標識符,否則我無法做到這一點。如何爲WebRTC(javascript)獲取多個USB設備(攝像頭)的唯一ID?
我也可以使用視頻輸入標識符來識別相機(例如/ dev/video0,/ dev/video1,.../dev/video n),但不知道如何在瀏覽器。
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 /火狐),但在Raspbian /鉻我做獲取標籤。不幸的是,它們並不是唯一的(例如,USB2.0 Camera(1871:0142))如果我能以某種方式使用這些標籤......這個page表示標籤「返回一個DOMString,它是描述此設備的標籤(例如」External USB網絡攝像機「)。只有在活動MediaStream使用或授予永久權限時纔可用。」我不知道什麼或如何做「持久性權限」。
任何幫助表示讚賞。我願意使用不同的技術,即fswebcam以某種方式鏈接到瀏覽器(自定義URL)或PHP或其他任何方式。
Thanx @jib但是ID確實發生了變化,它們如何被重用? Pi可能會重新啓動,然後我們必須重新編程以使用新的ID。 – Chiwda
@Chiwda我會隔離問題。 ID應該存儲在類似cookie的存儲中。它是否適用於常規桌面瀏覽器?如果是這樣,那麼你的覆盆子pi版本有一個錯誤。它有存儲嗎?最壞的情況下,使用其他人所建議的標籤(他們還首先要求口香糖成功,就像我提到的那樣)。 – jib
他們(ID)在Windows 10和Pi上的工作方式相同。是的,我知道他們需要一個成功的getUsermedia(),但是根據定義,每次調用它時都會重新創建它們。 – Chiwda