腾讯实时音视频(Tencent Real-Time Communication,TRTC)将腾讯21年来在网络与音视频技术上的深度积累,以多人音视频通话和低延时互动直播两大场景化方案,经过腾讯云服务向开发者开放,致力于帮助开发者快速搭建低成本、低延时、高品质的音视频互动解决方案。git
this.client = TRTC.createClient({ mode: 'videoCall', sdkAppId, userId, userSig });
this.client .join({ roomId }) .catch(error => { console.error('进房失败 ' + error); }) .then(() => { console.log('进房成功'); });
this.localStream = TRTC.createStream({ userId: this.userId, audio: true, video: true });
this.localStream .initialize() .catch(err => { console.error('初始化本地流失败 ' + error); }) .then((res) => { console.log('初始化本地流成功'); this.localStream.play('localVideo'); });
this.client .publish(this.localStream) .catch(err => { console.error('本地流发布失败 ' + error); }) .then((res) => { console.log('本地流发布成功'); });
this.client.on('stream-added', event => { this.remoteStream = event.stream; //订阅远端流 this.client.subscribe(this.remoteStream); });
this.client.on('stream-subscribed', event => { console.log('log', 'onRemoteStreamUpdate:' + event); this.remoteStream = event.stream; this.id = this.remoteStream.getId(); const remoteVideoDom = document.querySelector('#remoteVideo'); if(!document.querySelector(`#remoteStream-${this.id}`)) { const div = document.createElement('div'); div.setAttribute('style', 'position: absolute; right: 0; left: 0; top: 0; width: 100%; height: 100%'); div.setAttribute('id', `remoteStream-${this.id}`); remoteVideoDom.appendChild(div); } const videoLoading = document.querySelector('#video-loading'); videoLoading.setAttribute('style', 'display: none;'); // 播放远端流 this.remoteStream.play(`remoteStream-${this.id}`); });
this.client.unpublish(this.localStream) .catch((err) => { console.log('error', 'unpublish error:' + err); }) .then((res) => { // 取消发布本地流成功 console.log('log', 'unpublish error:' + res); });
this.client.leave();
// 每隔3秒获取本地推流状况 this.localTimer = setInterval(() => { this.client.getLocalVideoStats().then(stats => { for (let userId in stats) { console.log(new Date(), 'getLocalVideoStats', 'userId: ' + userId + 'bytesSent: ' + stats[userId].bytesSent + 'local userId' + this.userId); if(this.userId == userId && stats[userId].bytesSent == 0) { this.onEvent('leave'); } const bytesSentSR = (stats[userId].bytesSent - this.bytesSent) / 3000; if(this.userId == userId && bytesSentSR >= 20 && bytesSentSR <= 59) { } if(this.userId == userId) { this.bytesSent = stats[userId].bytesSent; } } }); }, 3000);
this.remoteTimer = setInterval(() => { this.client.getRemoteVideoStats().then(stats => { for (let userId in stats) { console.log('getRemoteVideoStats', 'userId: ' + userId + ' bytesReceived: ' + stats[userId].bytesReceived + ' packetsReceived: ' + stats[userId].packetsReceived + ' packetsLost: ' + stats[userId].packetsLost); // const bytesReceived = (stats[userId].bytesReceived - this.bytesReceived) / 3000; // let title = ''; // if(this.agentId == userId && bytesReceived >= 120) { // title = '当前通话,对方网络良好'; // } // if(this.agentId == userId && bytesReceived >= 60 && bytesReceived <= 119) { // title = '当前通话,对方网络通常'; // } // if(this.agentId == userId && bytesReceived >= 20 && bytesReceived <= 59) { // title = '当前通话,对方网络不佳'; // } // if(this.agentId == userId) { // Taro.showToast({ // title, // icon: 'none', // duration: 1000 // }); // this.bytesReceived = stats[userId].bytesReceived; // } } }); }, 3000);
目前经过TRTC的事件通知,搭配Socket,能作到对异常处理有较好的支持。github
<Video id="remoteVideo" autoplay muted playsinline controls />
切换先后置摄像头须要根据Label标签进行区分,获取先后置摄像头的deviceId,切换流程以下:算法
一、获取摄像头json
TRTC.getCameras().then(devices => { this.cameras = devices; });
二、选择摄像头小程序
this.localStream.switchDevice('video', deviceId) .catch(err => { console.log('error', 'switchDevice error:' + err); }) .then((res) => { console.log('log', 'switchDevice success' + res); });
request({ url: `http://fcgi.video.qcloud.com/common_access?appid=${liveSign.appId}&interface=Mix_StreamV2&t=${liveSign.t}&sign=${liveSign.liveSign}`, method: 'POST', headers: { 'content-type': 'application/json', }, body: JSON.stringify(params) }, (error, response, body) => { res.send({errCode: 0}); });
经过http://fcgi.video.qcloud.com/common_access
接口,咱们可以完美的监听房间内发生的状况,录制好的视频,会上传到腾讯的云点播平台,同时也支持客户自行导出。网络