gpt4 book ai didi

webrtc - 即使用户没有网络摄像头,我也可以发送视频流吗?

转载 作者:行者123 更新时间:2023-12-04 02:00:57 24 4
gpt4 key购买 nike

我正在参加一个视频 session ,我正在使用 connection.waitUntilRemoteStreamStartsFlowing = true;,然后再做其他事情。它工作正常,除非用户没有网络摄像头。有什么方法可以在没有网络摄像头的情况下从该用户发送视频流吗?

最佳答案

那将是对良好带宽的浪费。我不熟悉您正在使用的库,但不熟悉普通的 WebRTC,就像这本教科书一样 WebRTC sample使用 adapter.js ,你可以这样做:

调用 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"; });
...
})

有了这些信息,如果用户没有相机,就不要询问他们:

var constraints = { video: hasCam, audio: hasMic };

navigator.mediaDevices.getUserMedia(constraints)
.then(function(stream) {
myPeerConnection.addStream(stream);
})

最后,如果你不发送视频,那么默认也不会接收视频(愚蠢的默认),所以如果对方有摄像头,使用RTCOfferOptions :

var options = { offerToReceiveAudio: true, offerToReceiveVideo: true };

myPeerConnection.createOffer(options)
.then(function(offer) { ... })

在 Chrome 中你需要 adapter.js对于除最后一点以外的所有内容,但在最新的 Firefox 中它应该是 just work (注意:使用 arrow-functions ):

var pc1 = new mozRTCPeerConnection(), pc2 = new mozRTCPeerConnection();

navigator.mediaDevices.enumerateDevices()
.then(devices => navigator.mediaDevices.getUserMedia({
video: devices.some(device => device.kind == "videoinput"),
audio: devices.some(device => device.kind == "audioinput")
}))
.then(stream => pc1.addStream(v1.mozSrcObject = stream))
.then(() => pc1.createOffer({ offerToReceiveAudio: true,
offerToReceiveVideo: true }))
.then(offer => pc1.setLocalDescription(offer))
.then(() => pc2.setRemoteDescription(pc1.localDescription))
.then(() => pc2.createAnswer())
.then(answer => pc2.setLocalDescription(answer))
.then(() => pc1.setRemoteDescription(pc2.localDescription))
.then(() => log("Connected!"))
.catch(failed);

pc1.onicecandidate = e => !e.candidate ||
pc2.addIceCandidate(e.candidate).catch(failed);
pc2.onicecandidate = e => !e.candidate ||
pc1.addIceCandidate(e.candidate).catch(failed);
pc2.onaddstream = e => v2.mozSrcObject = e.stream;

var log = msg => div.innerHTML += msg + "<br>";
var failed = e => log(e.toString() +", line "+ e.lineNumber);
<video id="v1" height="120" width="160" autoplay></video>
<video id="v2" height="120" width="160" autoplay></video>
<br><div id="div"></div>

部分内容是全新的,所以我不确定它与您目前使用的库的集成程度如何,但随着时间的推移应该会如此。

Chrome 有这个 API 的旧版本,我不会在这里提及,因为它不是标准的。

关于webrtc - 即使用户没有网络摄像头,我也可以发送视频流吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31259205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com