我试图限制用户,应该允许使用相机在一个房间(最多9),所以我想禁用所有用户的相机按钮,一旦达到限制。但我目前无法确定当前有相机的用户数量。我已经搜索了有关房间和参与者API的Twilio,但是在那里找不到答案。
有一个简单的方法,我可以用来确定用户的数量,有他们的相机在一个房间?一旦我能够确定这一点,我可以使用它来启用/禁用其他用户的相机按钮。我正在使用JavaScript SDK,我的应用程序是基于https://github.com/twilio/twilio-video-app-react的。
发布于 2020-12-03 00:05:45
由于我无法使用javascript/react找到解决方案,所以我决定从后端使用一个旧的Laravel版本(4.2)来解决这个问题。我创建了一条路线,每当有人加入房间或打开/关闭相机时,所有参与者都会呼叫:
// routes.php
Route::get('room/{name}/participants/all', function ($name) {
$twilioConfig = \Config::get('app.twilio.video');
$twilioApiKey = $twilioConfig['apiKey'];
$twilioApiSecret = $twilioConfig['apiSecret'];
$client = new \Twilio\Rest\Client($twilioApiKey, $twilioApiSecret);
$participants = $client->video->rooms($name)
->participants->read(["status" => "connected"]);
$allParticipants = [];
$count = count($participants);
foreach ($participants as $participant) {
$publishedTracks = $client->video->rooms($name)
->participants($participant->sid)
->publishedTracks->read();
$videoOn = false;
foreach ($publishedTracks as $publishedTrack) {
if ('video' === $publishedTrack->kind && $publishedTrack->enabled) {
$videoOn = true;
break;
}
}
$format = 'H:i:s';
$allParticipants[$participant->sid] = [
'identity' => $participant->identity,
'created' => $participant->dateCreated->format($format),
'updated' => $participant->dateUpdated->format($format),
'started' => $participant->startTime->format($format),
'videoOn' => $videoOn,
'order' => $count--,
];
}
return \Illuminate\Support\Facades\Response::json($allParticipants);
});顺序设置为降序,因为这是我从Twilio的参与者列表中得到的响应(最后一次加入参与者第一次),这对我来说并不重要,因为在前面可以很容易地进行解析。
基本上:
particular
video轨道的状态答复如下:
[
{
"identity": "morcen1@example.com",
"created": "17:05:17",
"updated": "17:07:11",
"started": "17:05:17",
"videoOn": true,
"order": 4
},
{
"identity": "morcen2@example.com",
"created": "17:05:16",
"updated": "17:07:08",
"started": "17:05:16",
"videoOn": true,
"order": 3
},
{
"identity": "morcen3@example.com",
"created": "17:05:15",
"updated": "17:07:04",
"started": "17:05:15",
"videoOn": true,
"order": 2
},
{
"identity": "morcen4@example.com",
"created": "16:46:32",
"updated": "17:07:14",
"started": "16:46:32",
"videoOn": true,
"order": 1
}
]此脚本还可用于确定audio跟踪的状态。
发布于 2020-11-30 23:06:07
两位开发人员在这里传道。
不同的设备和不同的操作系统对于同时请求同一设备具有不同的能力。
听起来你想确定谁开着他们的相机,然后你可以把它们加起来。当浏览器试图访问网络摄像头时,getUserMedia会抛出一个NotReadableError,但它已经在使用中了(并不是所有浏览器/OS都是一致的)。
您可以使用新的基于承诺的getUserMedia()来处理该错误,查看摄像头何时已经在使用。
// both the video and audio track set to false to immediately trigger TypeError
var constraints = {
video: false,
audio: false
}
navigator.mediaDevices.getUserMedia(constraints).then(function success(stream) {
/* do stuff */
}).catch(function(err) {
//log to console first
console.log(err); /* handle the error */
if (err.name == "NotReadableError" || err.name == "TrackStartError") {
//webcam or mic are already in use
} else {
//other errors
}
});或者,您似乎可以检查readystate是否设置为live--more on that here。
navigator.getUserMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
if (navigator.getUserMedia) {
navigator.getUserMedia({
audio: true,
video: true
},
function(stream) {
// returns true if any tracks have active state of true
var result = stream.getVideoTracks().some(function(track) {
return track.enabled && track.readyState === 'live';
});
if (result) {
alert('Your webcam is busy!');
} else {
alert('Not busy');
}
},
function(e) {
alert("Error: " + e.name);
});
}类似地,您可以使用video.onplaying来检测视频何时打开--more on that here。
要停止赛道,你可以做一些类似的事情
stream.getTracks().forEach(track => track.stop())如果这有帮助的话请告诉我!
https://stackoverflow.com/questions/65062878
复制相似问题