webcodec-player is a lightweight, browser-based video player designed to demonstrate low-latency video streaming using the modern WebCodecs API. It supports FLV streams delivered over WebSockets or HTTP (HTTP-FLV), decoding H.264 video content and rendering it to a Canvas.
The project emphasizes performance by offloading the intensive tasks of demuxing and decoding to a Web Worker, ensuring the main thread remains responsive.
- WebCodecs API: Used for hardware-accelerated video decoding (
VideoDecoder). - Web Workers: Handles stream fetching, FLV demuxing, and decoding in a background thread.
- OffscreenCanvas: Allows the Web Worker to render decoded frames directly without blocking the DOM.
- WebSocket: Used for receiving the live video stream.
- FLV (Flash Video): The supported container format for the video stream.
The player is structured into three main layers:
-
UI & Controller (
index.html,player.js):index.htmlprovides the controls (Play, Pause, Destroy) and the container for the video.player.jsdefines thePlayerclass. It initializes the Canvas, spawns the Web Worker, and manages the communication (messaging) between the UI and the worker.
-
Worker Layer (
demux_decode_worker.js):- Acts as the engine of the player.
- Receives the
OffscreenCanvasfrom the main thread. - Initializes the
FLVDemuxerandVideoDecoder. - Draws decoded frames onto the canvas.
- Calculates and renders stats like FPS and bitrate.
-
Data Processing (
flv_demuxer.js):- Responsible for parsing the incoming FLV binary stream.
- Extracts H.264 video packets and configuration (AVCC) for the decoder.
This project consists of static files (HTML, JS) but uses ES Modules and Web Workers, which means it must be served via an HTTP server. It cannot be run by simply opening index.html as a file.
- A modern browser with WebCodecs support (e.g., Chrome 94+).
- A method to serve static files (e.g., Python, Node.js, VS Code Live Server).
-
Start a local server. If you have Python installed:
python3 -m http.server 8000
Or using Node.js
http-server:npx http-server . -
Access the player. Open your browser and navigate to:
http://localhost:8000 -
Test Stream: The player requires a WebSocket URL serving an FLV stream.
- Enter a valid WebSocket URL in the input box (e.g.,
ws://your-server/live/stream.flv). - Click 播放 (Play).
- Enter a valid WebSocket URL in the input box (e.g.,
The README.md suggests using ffmpeg to simulate a stream if you have an RTSP source:
ffmpeg -re -stream_loop -1 -i test.mp4 -c:v copy -rtsp_transport tcp -f rtsp rtsp://<server_ip>:8554/live/test121- Modularization: Code is split into logical modules.
common.jscontains shared utilities (inferred). - Worker Communication: All control commands (
play,destroy,showVbps) are sent to the worker viapostMessage. - Error Handling: Basic error logging is present in the
VideoDecoderconfiguration (error: e => console.error(e)). - Canvas Transfer: The
transferControlToOffscreen()pattern is strictly used to decouple rendering from the main thread.
According to the documentation, the following features are planned:
- Fullscreen support & player event callbacks.
- Automatic reconnection logic on disconnect.
- H.265 (HEVC) hardware decoding support.
- HTTP-FLV protocol support.
- Code packaging/bundling.