This function gets video chunk
function registerPlayer(mediaSource) {
const videoBuffer = mediaSource.addSourceBuffer('video/webm;codecs=vp8');
let countDownloadChunk = 0
setInterval(() => {
fetch(/main/get/${STREAM_NAME}/${countDownloadChunk})
.then((response) => {
if (response.status !== 200) {
throw Error('no such file')
}
return response
}).then((buffer) => {
countDownloadChunk++
videoBuffer.appendBuffer(buffer)
}).catch(() => {})
}, 2000)
}
Then I call this function and set video.src
function processStream(stream, mediaSource) {
registerRecord(stream)
registerPlayer(mediaSource)
}
video.src = URL.createObjectURL(mediaSource);
navigator.mediaDevices.getUserMedia({
video: true,
}).then((stream) => {processStream(stream, mediaSource)});