|
@@ -19,6 +19,10 @@ export default class WebRtcService {
|
|
|
|
|
|
|
|
private store = useStore()
|
|
private store = useStore()
|
|
|
|
|
|
|
|
|
|
+ private lastStats: RTCStatsReport | null = null
|
|
|
|
|
+
|
|
|
|
|
+ private sleep = (ms: number) => new Promise((resolve) => { setTimeout(resolve, ms) })
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 初始化
|
|
* 初始化
|
|
|
* @param DOM HTMLVideoElement
|
|
* @param DOM HTMLVideoElement
|
|
@@ -66,6 +70,9 @@ export default class WebRtcService {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // get stats
|
|
|
|
|
+ this.getStats()
|
|
|
|
|
+
|
|
|
console.log('RTC success')
|
|
console.log('RTC success')
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
console.log('RTC 初始化失败', error)
|
|
console.log('RTC 初始化失败', error)
|
|
@@ -83,6 +90,41 @@ export default class WebRtcService {
|
|
|
this.audioTack!.enabled = mute
|
|
this.audioTack!.enabled = mute
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 比特率|丢包率
|
|
|
|
|
+ * @returns
|
|
|
|
|
+ */
|
|
|
|
|
+ async getStats() {
|
|
|
|
|
+ if (this.Peer) {
|
|
|
|
|
+ const stats = await this.Peer.getStats()
|
|
|
|
|
+ let packetLoss = 0
|
|
|
|
|
+ let bitrate = 0
|
|
|
|
|
+ if (this.lastStats) {
|
|
|
|
|
+ stats.forEach((report) => {
|
|
|
|
|
+ if (report.type === 'inbound-rtp' && report.kind === 'video') {
|
|
|
|
|
+ if (this.lastStats && this.lastStats.has(report.id)) {
|
|
|
|
|
+ const lastReport = this.lastStats.get(report.id)
|
|
|
|
|
+ const duration = (report.timestamp - lastReport.timestamp) / 1000
|
|
|
|
|
+ bitrate = (report.bytesReceived - lastReport.bytesReceived) / duration / 1000
|
|
|
|
|
+ const lostPackets = (report.packetsLost - lastReport.packetsLost)
|
|
|
|
|
+ if ((report.packetsReceived - lastReport.packetsReceived + lostPackets) === 0) {
|
|
|
|
|
+ packetLoss = 0
|
|
|
|
|
+ } else {
|
|
|
|
|
+ packetLoss = (lostPackets / (report.packetsReceived - lastReport.packetsReceived + lostPackets)) * 100
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ this.lastStats = stats
|
|
|
|
|
+ this.store.setMqttMessage({ type: 'WebRtcStats', data: { packetLoss: Math.floor(packetLoss * 100) / 100, bitrate: Math.floor(bitrate * 100) / 100 } })
|
|
|
|
|
+ } else {
|
|
|
|
|
+ this.store.setMqttMessage({ type: 'WebRtcStats', data: { packetLoss: 0, bitrate: 0 } })
|
|
|
|
|
+ }
|
|
|
|
|
+ await this.sleep(1000)
|
|
|
|
|
+ this.getStats()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
distory() {
|
|
distory() {
|
|
|
this.Peer?.close()
|
|
this.Peer?.close()
|
|
|
this.Peer = null
|
|
this.Peer = null
|