騰訊實時音視頻(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
接口,咱們可以完美的監聽房間內發生的狀況,錄製好的視頻,會上傳到騰訊的雲點播平臺,同時也支持客戶自行導出。網絡