### Capture camera video and record canvas stream with MediaRecorder Source: https://github.com/howardchung/watchparty/blob/master/dev/test.html Gets user media with rear-facing camera preference, draws the video to a canvas, and records the canvas stream as webm/vp9 chunks. The recorded chunks are passed to addVideoChunk for playback. ```javascript var video = document.getElementById("video"); if (navigator.mediaDevices.getUserMedia) { var successCallback = function (stream) { video.srcObject = stream; var canvas = document.querySelector("canvas"); // Optional frames per second argument. var stream2 = canvas.captureStream(25); var recordedChunks = []; console.log(stream); var options = { mimeType: "video/webm; codecs=vp9" }; mediaRecorder = new MediaRecorder(stream, options); mediaRecorder.ondataavailable = handleDataAvailable; mediaRecorder.start(1000); async function handleDataAvailable(event) { console.log(event.data, await event.data.arrayBuffer()); if (event.data.size > 0) { // recordedChunks.push(event.data); addVideoChunk({ data: event.data }); } } }; var errorCallback = function (error) { console.log(error); }; navigator.mediaDevices .getUserMedia({ audio: false, video: { facingMode: { ideal: "environment" } } }) .then(successCallback, errorCallback); } ``` -------------------------------- ### Playback recorded chunks with MediaSource Source: https://github.com/howardchung/watchparty/blob/master/dev/test.html Creates a video element and MediaSource to play back recorded webm chunks. Appends each chunk to the source buffer as it arrives. ```javascript const videoRec = document.createElement("video"); const ms = new MediaSource(); videoRec.autoplay = true; videoRec.width = 320; videoRec.height = 240; // console.log(MediaSource.isTypeSupported('video/webm; codecs=vp9')); ms.onsourceopen = () => { console.log("onsourceopen"); ms.addSourceBuffer("video/webm; codecs=vp9"); }; ms.onsourceclose = () => { console.log("onsourceclose"); }; videoRec.src = window.URL.createObjectURL(ms); document.body.appendChild(videoRec); async function addVideoChunk(data) { ms.sourceBuffers[0].appendBuffer(await data.data.arrayBuffer()); } ``` -------------------------------- ### MediaSource with WebSocket audio streaming Source: https://github.com/howardchung/watchparty/blob/master/dev/audio.html Sets up a MediaSource with an Opus source buffer, opens a WebSocket to receive binary audio data, queues incoming chunks, and appends merged buffers to the source buffer. Logs events and errors to the console. This is part of a commented-out experimental block. ```javascript const ms = new MediaSource(); let queue = []; let buffSize = 0; let first = true; ms.onsourceopen = () => { console.log('onsourceopen'); ms.addSourceBuffer('audio/mp4; codecs=opus'); const sourceBuffer = ms.sourceBuffers[0]; const updateNext = () => { if (queue.length) { console.log('premerge', queue.length, buffSize); const merged = mergeBuffers(); console.log('merged', merged.map(num => num.toString(16).padStart(2, '0'))); // debugger; try { sourceBuffer.appendBuffer(merged); } catch (e) { console.log(document.getElementById('audio').error); } } }; sourceBuffer.addEventListener('updateend', updateNext); const socket = new WebSocket('ws://212.47.251.184:5001'); socket.binaryType = 'arraybuffer'; socket.onmessage = (event) => { const buffer = new Uint8Array(event.data); queue.push(buffer); buffSize += buffer.length; if (!sourceBuffer.updating) { updateNext(); } } }; ms.onsourceclose = () => { console.log('onsourceclose'); } videoRec.src = window.URL.createObjectURL(ms); ``` -------------------------------- ### Create audio element for Ogg stream Source: https://github.com/howardchung/watchparty/blob/master/dev/audio.html Creates an audio element with autoplay and controls, sets its source to a remote Ogg Opus stream, and appends it to the document body. This is part of a commented-out experimental block for streaming audio. ```javascript const videoRec = document.createElement('audio'); videoRec.autoplay = true; videoRec.controls = true; videoRec.id = 'audio'; videoRec.type = 'audio/ogg; codec=opus'; videoRec.src = 'http://212.47.251.184:5902'; document.body.appendChild(videoRec); ``` -------------------------------- ### Render camera video to canvas with requestAnimationFrame Source: https://github.com/howardchung/watchparty/blob/master/dev/test.html Draws the captured video onto a canvas, scaling and centering it. Uses requestAnimationFrame for continuous rendering and checks video readiness before drawing. ```javascript var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); requestAnimationFrame(renderFrame); function renderFrame() { // re-register callback requestAnimationFrame(renderFrame); // set internal canvas size to match HTML element size canvas.width = canvas.scrollWidth; canvas.height = canvas.scrollHeight; if (video.readyState === video.HAVE_ENOUGH_DATA) { // scale and horizontally center the camera image var videoSize = { width: video.videoWidth, height: video.videoHeight }; var canvasSize = { width: canvas.width, height: canvas.height }; var renderSize = calculateSize(videoSize, canvasSize); var xOffset = (canvasSize.width - renderSize.width) / 2; context.drawImage(video, xOffset, 0, renderSize.width, renderSize.height); } } function calculateSize(srcSize, dstSize) { var srcRatio = srcSize.width / srcSize.height; var dstRatio = dstSize.width / dstSize.height; if (dstRatio > srcRatio) { return { width: dstSize.height * srcRatio, height: dstSize.height, }; } else { return { width: dstSize.width, height: dstSize.width / srcRatio, }; } } ``` -------------------------------- ### Merge queued audio buffers Source: https://github.com/howardchung/watchparty/blob/master/dev/audio.html Merges queued Uint8Array buffers into a single buffer, skipping empty ones, then resets the queue and buffer size. Used to combine WebSocket chunks before appending to a MediaSource. ```javascript function mergeBuffers() { var merged; if (queue.length == 1) { merged = queue[0]; } else { merged = new Uint8Array(buffSize); var length = queue.length; var offset = 0; for (var i = 0; i < length; i++) { var curr = queue[i]; if (curr.length <= 0) { continue; } merged.set(curr, offset); offset += curr.length; } } queue = []; buffSize = 0; return merged; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.