Home >Web Front-end >JS Tutorial >WebCodec - Sending and Receiving
Hello! ?
In this tutorial I will show you how to use the WebCodec API to both send and receive video.
First lets get coding the server.
In order to send and receive packets between peers we will need a websocket server.
For this we will create a very basic server using nodejs. First initialize the project:
npm init -y
Then install the required modules:
npm i ws express
Next create a new file called "index.js" and populate it with the following code:
// server.js const WebSocket = require('ws'); const express = require('express'); const app = express(); const port = 3000; const connectedClients = new Set(); app.use(express.static(__dirname + '/public')); const wss = new WebSocket.Server({ noServer: true }); wss.on('connection', ws => { console.log('new connection'); connectedClients.add(ws); ws.on('message', message => { connectedClients.forEach(client => { if (client !== ws && client.readyState === WebSocket.OPEN) { client.send(message); } }); }); ws.once('close', () => { connectedClients.delete(ws); console.log('connection closed'); }); }); const server = app.listen(port, () => { console.log(`server running on port ${port}`); }); server.on('upgrade', (request, socket, head) => { wss.handleUpgrade(request, socket, head, (ws) => { wss.emit('connection', ws, request); }); });
Nothing too complicated the above code serves the public directory and handles the websocket connection sending packets to all connected peers. ?
Next we will handle the sender part, but first create a new directory called "public"
mkdir public
The first front end file we will create is the one that is broadcasting, create a new file called "sender.html" under public and populate it with the following HTML:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>Sender</title> <style> video, canvas { width: 640px; height: 480px; border: 2px solid black; margin: 10px; } </style> </head> <body> <video id="video" autoplay playsinline></video> <canvas id="canvas" width="640" height="480"></canvas> <script> const videoElement = document.getElementById('video'); const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); let videoEncoder; let socket; const initWebSocket = () => { socket = new WebSocket('ws://localhost:3000'); socket.onopen = () => console.log('WebSocket connected'); socket.onerror = error => console.error('WebSocket error:', error); }; const initEncoder = () => { videoEncoder = new VideoEncoder({ output: (encodedChunk) => { const chunkData = new Uint8Array(encodedChunk.byteLength); encodedChunk.copyTo(chunkData); if (socket.readyState === WebSocket.OPEN) { socket.send(chunkData.buffer); } }, error: (error) => console.error('Encoding error:', error) }); videoEncoder.configure({ codec: 'vp8', width: 640, height: 480, bitrate: 1_000_000, framerate: 30 }); }; navigator.mediaDevices.getUserMedia({ video: true }) .then((stream) => { videoElement.srcObject = stream; const videoTrack = stream.getVideoTracks()[0]; const processor = new MediaStreamTrackProcessor(videoTrack); const reader = processor.readable.getReader(); const processFrames = async () => { while (true) { const { value: videoFrame, done } = await reader.read(); if (done) break; ctx.drawImage(videoFrame, 0, 0, canvas.width, canvas.height); videoEncoder.encode(videoFrame, { keyFrame: true }); videoFrame.close(); } }; processFrames(); }) .catch((error) => console.error('Failed to get camera', error)); initEncoder(); initWebSocket(); </script> </body> </html>
To breakdown and explain what the code does.
Phew! Hopefully that was understandable to you. Next we will create the file that will receive the stream. ?
This file receives the encoded video chunks via WebSocket, decodes them, and displays them on a canvas element.
Create a new file under the public directory called "receiver.html" and populate it with the following:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>Receiver</title> <style> canvas { width: 640px; height: 480px; border: 2px solid black; margin: 10px; } </style> </head> <body> <canvas id="canvas" width="640" height="480"></canvas> <script> const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); let videoDecoder; const initWebSocket = () => { const socket = new WebSocket('ws://localhost:3000'); socket.binaryType = 'arraybuffer'; socket.onmessage = event => { decodeFrame(event.data); }; socket.onerror = error => console.error('WebSocket error:', error); }; const initDecoder = () => { videoDecoder = new VideoDecoder({ output: (videoFrame) => { ctx.drawImage(videoFrame, 0, 0, canvas.width, canvas.height); videoFrame.close(); }, error: (error) => console.error('Decoding error:', error) }); videoDecoder.configure({ codec: 'vp8', width: 640, height: 480 }); }; const decodeFrame = (encodedData) => { const chunk = new EncodedVideoChunk({ type: 'key', timestamp: performance.now(), data: new Uint8Array(encodedData) }); videoDecoder.decode(chunk); }; initDecoder(); initWebSocket(); </script> </body> </html>
To break down the above file:
Phew! Now that we have all the pieces needed, lets actually run it! ?
To run the code simply run the following command:
node index.js
Then point your browser to http://localhost:3000/sender.html
Allow access to your camera and then open another tab to
http://localhost:3000/receiver.html
Like the below you should see the stream being sent from the sender.
In this tutorial I have shown how to get access to the camera, encode it, send the chunks over WebSocket and decode and display them on the receiver side. I hope this tutorial was of use to you. ?
As always you can get the code via my github:
https://github.com/ethand91/webcodec-stream
Happy Coding! ?
Like my work? I post about a variety of topics, if you would like to see more please like and follow me.
Also I love coffee.
If you are looking to learn Algorithm Patterns to ace the coding interview I recommend the [following course](https://algolab.so/p/algorithms-and-data-structure-video-course?affcode=1413380_bzrepgch
The above is the detailed content of WebCodec - Sending and Receiving. For more information, please follow other related articles on the PHP Chinese website!