How fbsamples/canvas-streaming-example works
Project Type and Purpose
• Type: A lightweight full-stack streaming application/proxy.
• Purpose: Capture a <canvas> as a video stream in the browser, send it via WebSockets to a Node.js proxy that invokes FFmpeg, and push the re-wrapped/encoded stream to Facebook Live over RTMP.
File Structure
• server.js + package.json – the Node.js WebSocket‐to‐RTMP proxy
• www/index.html + www/js/canvasFill.js – the client static assets
• Configuration files (.gitignore, LICENSE, etc.) are not core runtime components.
Extract Key Architecture from the README
• Three logical parts:
1. Browser client (canvas capture + MediaRecorder + WebSocket)
2. Proxy server (Express for static, ws for WebSocket, child_process spawning FFmpeg)
3. Facebook Live API (RTMP endpoint)
• Data transforms and protocol transitions (WebM container → FLV container, Opus → AAC if needed, VP8/VP9 → H.264 copy or transcode).
Main Components and Their Interactions
– HTML5 Canvas
– CanvasCaptureMediaStream → MediaRecorder API
– Encoded video blobs
– WebSocket client connection to proxy
– Express static server (serves index.html + JS)
– ws WebSocket server listens on ws://…/rtmp/{encoded-URL}
– child_process.spawn(“ffmpeg”, …)
c. FFmpeg Process (inside proxy)
– Reads raw video from STDIN
– Wraps into FLV, transcodes audio to AAC if needed
– Pushes stream to Facebook Live RTMP endpoint
– RTMP ingest endpoint (rtmp:// or rtmps://)
– CDN distribution to viewers
Architectural Patterns and Principles
• Streaming pipeline pattern: discrete stages (capture → encode → transport → remux/transcode → publish)
• Separation of concerns: client handles capture, server handles transport and codec compatibility
• Proxy pattern: translate WebSocket stream into RTMP
• Child-process isolation: FFmpeg in its own process for security and stability