-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#3 Add an initial implementation of WebCodecs
- Loading branch information
Showing
6 changed files
with
129 additions
and
5 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
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
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
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,29 @@ | ||
<template> | ||
<div> | ||
<md-switch v-model="userEnabledWebCodecs">Enable WebCodecs<small>(experimental)</small></md-switch> | ||
<component :is="isWebCodecAvailable ? 'web-codec' : 'jmuxer'" :device="device" /> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import Jmuxer from './Jmuxer.vue'; | ||
import WebCodec from './WebCodec.vue'; | ||
export default { | ||
props: ['device'], | ||
data() { | ||
return { | ||
userEnabledWebCodecs: false, | ||
} | ||
}, | ||
components: {Jmuxer, WebCodec}, | ||
computed: { | ||
webCodecsEnabled() { | ||
return this.isWebCodecAvailable && this.userEnabledWebCodecs; | ||
}, | ||
isWebCodecAvailable() { | ||
return 'VideoEncoder' in window; | ||
} | ||
} | ||
} | ||
</script> |
2 changes: 1 addition & 1 deletion
2
src/ui/views/ViewUSB/Goggle.vue → src/ui/views/ViewUSB/VideoPlayer/Jmuxer.vue
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
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,92 @@ | ||
<template> | ||
<div> | ||
<canvas ref="player" style="width:50%;border: 1px solid #bababa" width="720" height="576"></canvas> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
function dec2hex(array) { | ||
return array.map((val) => val.toString(16).padStart(2, '0')).join(' ').toUpperCase() | ||
} | ||
const MICROSECONDS_PER_FRAME = 16700; | ||
export default { | ||
props: ['device'], | ||
data() { | ||
return { | ||
isPlaying: false, | ||
} | ||
}, | ||
computed: { }, | ||
methods: { | ||
stopVideo() { | ||
this.device.stopPolling(); | ||
}, | ||
startVideo() { | ||
this.device.startPolling(); | ||
} | ||
}, | ||
mounted() { | ||
const canvasContext = this.$refs.player.getContext('2d'); | ||
const handleFrame = (frame) => { | ||
canvasContext.drawImage(frame, 0, 0); | ||
frame.close(); | ||
} | ||
const init = { | ||
output: handleFrame, | ||
error: (e) => { | ||
console.log(e); | ||
this.device.close(); | ||
}, | ||
}; | ||
let decoder = new VideoDecoder(init); | ||
decoder.configure({ codec: 'avc1.64002A' }); | ||
let currentTimestamp = 0; | ||
this.device.onData = (data) => { | ||
if (data.buffer.byteLength === 0) { | ||
return; | ||
} | ||
const chunkData = new Uint8Array(data.buffer); | ||
let NALUnitsFound = 0; | ||
chunkData.forEach((val, index, self) => { | ||
if (self.length > index + 2 && self[index] === 0 && self[index + 1] === 0 && self[index + 2] === 0) { | ||
NALUnitsFound++; | ||
} | ||
}); | ||
if (NALUnitsFound === 4 && currentTimestamp !== undefined && currentTimestamp !== 0) { | ||
return; | ||
} | ||
if (NALUnitsFound % 2 === 1) { | ||
console.log(`Uneven number of NAL units found: ${NALUnitsFound}`); | ||
return; | ||
} | ||
currentTimestamp += MICROSECONDS_PER_FRAME; | ||
const chunk = new EncodedVideoChunk({ | ||
type: "delta", | ||
timestamp: currentTimestamp, | ||
data: chunkData, | ||
}) | ||
decoder.decode(chunk); | ||
}; | ||
this.device.startPolling(); | ||
}, | ||
} | ||
</script> | ||
|
||
<style scoped> | ||
.hidden { | ||
display: none; | ||
} | ||
.text-center { | ||
text-align: center; | ||
} | ||
</style> |