Ok, now everything for testing is set up.
<head>
<title>WebCodecs webcam stream in Worker</title>
</head>
Nothing, look into the console.log
<script>
let frameCount = 0
function start() {
if (typeof MediaStreamTrackProcessor === 'undefined' ||
typeof MediaStreamTrackGenerator === 'undefined') {
console.log(
'Your browser does not support the experimental MediaStreamTrack API. ' +
'Please launch with the --enable-blink-features=WebCodecs,MediaStreamInsertableStreams flag');
return;
}
var constraints = {
audio: false,
video: {
width: 1280,
height: 720
}
};
console.log('dom contentloaded')
// Get a MediaStream from the webcam.
navigator.mediaDevices.getUserMedia(constraints).then(function(mediaStream) {
console.log('getusermedia')
// Create a MediaStreamTrackProcessor, which exposes frames from the track
// as a ReadableStream of VideoFrames.
var track = mediaStream.getVideoTracks()[0];
var processor = new MediaStreamTrackProcessor(track);
var frameStream = processor.readable;
const frameReader = frameStream.getReader();
const output = (frame) => {
console.log('got frame', frame);
}
const videocodec = new VideoEncoder({
output: output,
error(error) {
console.log('encoder error',
error.name, error.message)
}
})
const cur = {} // current frame data
console.log('before frame reader')
frameReader.read().then(function processFrame({
done,
value
}) {
console.log('process frame', value)
if (done) {
console.log("Stream is done");
return;
}
var frame = value;
if (
frame.displayHeight !== cur.displayHeight ||
frame.displayWidth !== cur.displayWidth ||
frame.codedHeight !== cur.height ||
frame.codedWidth !== cur.width
) {
const curcodec = 'avc1.42402A'
videocodec.configure({
codec: curcodec /* 'avc1.420034' */ , // aka h264, maybe add profile
avc: {
format: 'annexb'
},
framerate: 25,
displayWidth: frame.displayWidth,
displayHeight: frame.displayHeight,
width: frame.codedWidth,
height: frame.codedHeight,
hardwareAcceleration: 'prefer-hardware',
bitrate: 300000,
scalabilityMode: 'L1T3',
latencyMode: 'realtime'
})
cur.displayHeight = frame.displayHeight
cur.displayWidth = frame.displayWidth
cur.height = frame.codedHeight
cur.width = frame.codedWidth
console.log('current config', cur)
}
// NOTE: all paths below must call frame.close(). Otherwise, the GC won't
// be fast enoug to recollect VideoFrames, and decoding can stall.
videocodec.encode(frame, {
keyFrame: (frameCount % 60 == 0)
})
// Processing on 'frame' goes here!
// E.g. this is where encoding via a VideoEncoder could be set up, or
// rendering to an OffscreenCanvas.
// For now, simply confirm we are receiving frames.
if (++frameCount % 20 == 0) {
console.log("Read 20 frames");
}
frame.close();
frameReader.read().then(processFrame);
}).catch(function(err) {
console.log(
err.name + ": " + err.message)
});
console.log('after frame reader')
}, false).catch((error) => {
console.log('error caught', error)
});
console.log('after getusermedia')
}
start()
</script>