@@ -12,27 +12,8 @@ const props = withDefaults(defineProps<{
12
12
height: 512 ,
13
13
})
14
14
15
- const kiloBytesFormatter = new Intl .NumberFormat (' en-US' , {
16
- style: ' unit' ,
17
- unit: ' kilobyte-per-second' ,
18
- unitDisplay: ' short' ,
19
- })
20
-
21
- // All Bandwidth calculation variables
22
- const bytesReceivedInLastSecond = ref (0 )
23
- const currentValidBandwidth = ref (0 )
24
- const currentValidBandwidthFormatted = computed (() => {
25
- return kiloBytesFormatter .format (currentValidBandwidth .value )
26
- })
27
- // Valid Bandwidth calculation variables
28
- const validBytesReceivedInLastSecond = ref (0 )
29
- const currentBandwidth = ref (0 )
30
- const currentBandwidthFormatted = computed (() => {
31
- return kiloBytesFormatter .format (currentBandwidth .value )
32
- })
33
-
34
- let lastUpdateTime = 0
35
- let animationFrameId: number | null = null
15
+ const { bytesReceived : validBytesReceivedInLastSecond, currentFormatted : currentValidBandwidthFormatted } = useBandwidth ()
16
+ const { bytesReceived : bytesReceivedInLastSecond, currentFormatted : currentBandwidthFormatted } = useBandwidth ()
36
17
37
18
const { devices } = useDevicesList ({
38
19
requestPermissions: true ,
@@ -78,16 +59,6 @@ onMounted(async () => {
78
59
)
79
60
})
80
61
81
- onMounted (() => {
82
- animationFrameId = requestAnimationFrame (updateBandwidth )
83
- })
84
-
85
- onUnmounted (() => {
86
- if (animationFrameId !== null ) {
87
- cancelAnimationFrame (animationFrameId )
88
- }
89
- })
90
-
91
62
function disconnectCamera() {
92
63
stream ?.getTracks ().forEach (track => track .stop ())
93
64
stream = undefined
@@ -112,36 +83,6 @@ async function connectCamera() {
112
83
}
113
84
}
114
85
115
- function updateBandwidth(timestamp : number ) {
116
- const now = timestamp
117
- const elapsedTime = now - lastUpdateTime
118
-
119
- if (elapsedTime >= 1000 ) {
120
- // Calculate bandwidth for the last second
121
- currentValidBandwidth .value = Number .parseFloat (
122
- (
123
- (validBytesReceivedInLastSecond .value / 1024 )
124
- / (elapsedTime / 1000 )
125
- ).toFixed (2 ),
126
- )
127
-
128
- currentBandwidth .value = Number .parseFloat (
129
- (
130
- (bytesReceivedInLastSecond .value / 1024 )
131
- / (elapsedTime / 1000 )
132
- ).toFixed (2 ),
133
- )
134
-
135
- // Reset for the next second
136
- validBytesReceivedInLastSecond .value = 0
137
- bytesReceivedInLastSecond .value = 0
138
-
139
- lastUpdateTime = now
140
- }
141
-
142
- requestAnimationFrame (updateBandwidth )
143
- }
144
-
145
86
const chunks: SliceData [] = reactive ([])
146
87
147
88
const length = computed (() => chunks .find (i => i ?.[1 ])?.[1 ] || 0 )
@@ -268,7 +209,7 @@ watch(() => results.value.size, (size) => {
268
209
</template >
269
210
</div >
270
211
<p absolute right-1 top-1 border border-gray:50 rounded-md bg-black:75 px2 py1 text-sm text-white font-mono shadow >
271
- {{ shutterCount }} | {{ fps.toFixed(0) }} hz | {{ currentValidBandwidthFormatted }} ( <span text-neutral-400 >{{ currentBandwidthFormatted }}</span >)
212
+ {{ shutterCount }} | {{ fps.toFixed(0) }} hz | {{ currentValidBandwidthFormatted }} <span text-neutral-400 >( {{ currentBandwidthFormatted }}) </span >
272
213
</p >
273
214
</div >
274
215
</div >
0 commit comments