-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0d1a40
commit 92757e2
Showing
2 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
<template> | ||
<div class="canvas-container"> | ||
<canvas ref="canvasRef" :width="width" :height="height"></canvas> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { WebRTCStats } from '@peermetrics/webrtc-stats' | ||
import { onMounted, onUnmounted, ref, watch } from 'vue' | ||
import { useVideoStore } from '@/stores/video' | ||
import { WebRTCStatsEvent } from '@/types/video' | ||
const videoStore = useVideoStore() | ||
const props = defineProps({ | ||
width: { | ||
type: Number, | ||
default: 110, | ||
}, | ||
height: { | ||
type: Number, | ||
default: 140, | ||
}, | ||
updateInterval: { | ||
type: Number, | ||
default: 20, | ||
}, | ||
streamName: { | ||
type: String, | ||
default: '', | ||
}, | ||
}) | ||
const canvasRef = ref(null) | ||
const framerateData = ref([]) | ||
const bitrateData = ref([]) | ||
let animationFrameId = null | ||
let intervalId = null | ||
let bitrate = 0 | ||
let packetsLost = 0 | ||
let jitterBufferLatency = 0 | ||
let freezes = 0 | ||
let frozenTime = 0 | ||
let framedrops = 0 | ||
let framerate = 0 | ||
const maxDataPoints = 100 | ||
const maxBitrate = 40000 // 50Mbps, adjust as needed | ||
const maxFramerate = 60 // 60fps, adjust as needed | ||
/** | ||
* | ||
* @param value | ||
* @param max | ||
*/ | ||
function normalizeValue(value: number, max: number): number { | ||
return Math.min(value / max, 1) * 100 | ||
} | ||
/** | ||
* Draw the line plots and stats | ||
*/ | ||
function draw(): void { | ||
const canvas = canvasRef.value | ||
const ctx = canvas.getContext('2d') | ||
const { width, height } = props | ||
ctx.clearRect(0, 0, width, height) | ||
// Draw bitrate plot | ||
ctx.strokeStyle = 'rgb(255, 165, 0)' // Orange | ||
ctx.lineWidth = 1 | ||
ctx.beginPath() | ||
for (let i = 0; i < bitrateData.value.length; i++) { | ||
const x = (i / (maxDataPoints - 1)) * width | ||
const y = height - (normalizeValue(bitrateData.value[i], maxBitrate) / 100) * (height - 20) | ||
if (i === 0) ctx.moveTo(x, y) | ||
else ctx.lineTo(x, y) | ||
} | ||
ctx.stroke() | ||
// Draw framerate plot | ||
ctx.strokeStyle = 'rgb(0, 255, 0)' // Green | ||
ctx.beginPath() | ||
for (let i = 0; i < framerateData.value.length; i++) { | ||
const x = (i / (maxDataPoints - 1)) * width | ||
const y = height - (normalizeValue(framerateData.value[i], maxFramerate) / 100) * (height - 20) | ||
if (i === 0) ctx.moveTo(x, y) | ||
else ctx.lineTo(x, y) | ||
} | ||
ctx.stroke() | ||
// Display stats | ||
const stats = [ | ||
{ label: 'Packets Lost', value: packetsLost, color: 'white' }, | ||
{ label: 'Framedrops', value: framedrops, color: 'white' }, | ||
{ label: 'Jitter(ms)', value: jitterBufferLatency.toFixed(0), color: 'white' }, | ||
{ label: 'Freezes', value: `${freezes}(${frozenTime.toFixed(1)}s)`, color: 'white' }, | ||
{ label: 'Bitrate', value: `${bitrate.toFixed(0)}kbps`, color: 'rgb(255, 165, 0)' }, | ||
{ label: 'FPS', value: framerate.toFixed(2), color: 'rgb(0, 255, 0)' }, | ||
] | ||
ctx.font = '10px Arial' | ||
stats.forEach((stat, index) => { | ||
ctx.fillStyle = stat.color | ||
ctx.fillText(`${stat.label}: ${stat.value}`, 5, 12 + index * 12) | ||
}) | ||
animationFrameId = requestAnimationFrame(draw) | ||
} | ||
const webrtcStats = new WebRTCStats({ getStatsInterval: 100 }) | ||
/** | ||
* | ||
*/ | ||
function update() { | ||
framerateData.value.push(framerate) | ||
bitrateData.value.push(bitrate) | ||
if (framerateData.value.length > maxDataPoints) framerateData.value.shift() | ||
if (bitrateData.value.length > maxDataPoints) bitrateData.value.shift() | ||
} | ||
watch(videoStore.activeStreams, (streams): void => { | ||
Object.keys(streams).forEach((streamName) => { | ||
if (streamName !== props.streamName) return | ||
const session = streams[streamName]?.webRtcManager.session | ||
if (!session || !session.peerConnection) return | ||
if (webrtcStats.peersToMonitor[session.consumerId]) return | ||
webrtcStats.addConnection({ | ||
pc: session.peerConnection, | ||
peerId: session.consumerId, | ||
connectionId: session.id, | ||
remote: false, | ||
}) | ||
}) | ||
}) | ||
onMounted(() => { | ||
intervalId = setInterval(update, props.updateInterval) | ||
draw() | ||
webrtcStats.on('stats', (ev: WebRTCStatsEvent) => { | ||
try { | ||
const videoData = ev.data.video.inbound[0] | ||
if (videoData === undefined) return | ||
if (videoData.bitrate) { | ||
const newBitrate = videoData.bitrate / 1000 | ||
bitrate = bitrate * 0.8 + newBitrate * 0.2 | ||
jitterBufferLatency = (1000 * videoData.jitterBufferDelay) / videoData.jitterBufferEmittedCount | ||
packetsLost = videoData.packetsLost | ||
freezes = videoData.freezeCount | ||
frozenTime = videoData.totalFreezesDuration | ||
framedrops = videoData.framesDropped | ||
framerate = videoData.framesPerSecond ?? 0 | ||
} | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
}) | ||
}) | ||
onUnmounted(() => { | ||
clearInterval(intervalId) | ||
cancelAnimationFrame(animationFrameId) | ||
}) | ||
</script> | ||
|
||
<style scoped> | ||
.canvas-container { | ||
position: absolute; | ||
top: 50px; | ||
left: 10px; | ||
background-color: rgba(0, 0, 0, 0.5); | ||
z-index: 2; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters