我正在召开视频会议,connection.waitUntilRemoteStreamStartsFlowing = true;在做其他事情之前我正在使用。它工作正常,除非用户没有网络摄像头。有什么办法可以在没有网络摄像头的情况下从该用户发送视频流?
那将浪费良好的带宽。我不熟悉您正在使用的库,但是对于普通的 WebRTC,就像在使用adapter.js 的教科书WebRTC 示例中一样,您可以执行以下操作:
致电navigator.mediaDevices.enumerateDevices()了解用户拥有多少个摄像头和麦克风:
navigator.mediaDevices.enumerateDevices()
.then(function(devices) {
var hasCam = devices.some(function(d) { return d.kind == "videoinput"; });
var hasMic = devices.some(function(d) { return d.kind == "audioinput"; });
...
})
Run Code Online (Sandbox Code Playgroud)
有了这些信息,如果用户没有相机,请跳过询问他们的相机:
var constraints = { video: hasCam, audio: hasMic };
navigator.mediaDevices.getUserMedia(constraints)
.then(function(stream) {
myPeerConnection.addStream(stream);
})
Run Code Online (Sandbox Code Playgroud)
最后,如果你不发送视频,那么默认也是不接收视频(愚蠢的默认),所以如果对方有摄像头,使用RTCOfferOptions:
var options = { offerToReceiveAudio: true, offerToReceiveVideo: true };
myPeerConnection.createOffer(options)
.then(function(offer) { ... })
Run Code Online (Sandbox Code Playgroud)
在 Chrome 中,除了最后一点之外,您将需要adapter.js,但在最新的 Firefox 中它应该可以正常工作(注意:使用箭头函数):
navigator.mediaDevices.enumerateDevices()
.then(function(devices) {
var hasCam = devices.some(function(d) { return d.kind == "videoinput"; });
var hasMic = devices.some(function(d) { return d.kind == "audioinput"; });
...
})
Run Code Online (Sandbox Code Playgroud)
var constraints = { video: hasCam, audio: hasMic };
navigator.mediaDevices.getUserMedia(constraints)
.then(function(stream) {
myPeerConnection.addStream(stream);
})
Run Code Online (Sandbox Code Playgroud)
其中的一部分是全新的,所以我不确定它与您目前使用的库的集成程度如何,但随着时间的推移应该会。
Chrome 有这个 API 的旧版本,我不会在这里提到它,因为它不是标准的。