Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CanvasToVirtualVideo

Overview

CanvasToVirtualVideo is a demo project showcasing how to stream HTML5 canvas content from a browser to a virtual video feed using WebSockets. The canvas displays a counter incrementing every 50ms, and its content is streamed as JPEG images at approximately 30 FPS to a WebSocket server. This project demonstrates the potential to capture browser-based canvas animations as a virtual video feed, which can be read and integrated with custom processing pipelines (e.g., video analysis, streaming, or recording).

Purpose

This project serves as a proof-of-concept for:

  • Streaming real-time canvas content from a browser to a server.
  • Treating canvas output as a virtual video feed for further processing.
  • Enabling integration with external systems for video processing, such as computer vision, live streaming, or video encoding.

Features

  • Displays a counter on a canvas, incrementing every 50ms.
  • Streams canvas content as JPEG images over a WebSocket connection at ~30 FPS.
  • Randomly changes the canvas background color for visual effect.
  • Includes a simple Node.js WebSocket server that logs client connections and outputs received binary data to stdout, simulating a virtual video feed.

Prerequisites

  • Node.js (v14 or higher recommended)
  • A modern web browser (e.g., Chrome, Firefox)

Installation

  1. Clone or download the project repository.
  2. Navigate to the project directory:
    cd CanvasToVirtualVideo
  3. Install dependencies:
    npm install

Usage

  1. Start the WebSocket server:

    node server.js

    The server will start on ws://localhost:8080 and log a message: WebSocket server started on port 8080.

  2. Open index.html in a web browser. You can either:

    • Serve index.html using a local web server (e.g., npx http-server).
    • Open index.html directly in the browser (e.g., file:///path/to/index.html).
  3. The canvas will display a counter incrementing every 50ms with a randomly colored background. The canvas content is streamed to the WebSocket server as a virtual video feed, and the server outputs the received binary data to the console.

Project Structure

  • index.html: The front-end HTML file containing the canvas and JavaScript for rendering the counter and streaming to the WebSocket server.
  • server.js: The Node.js WebSocket server that handles client connections and logs received canvas frames as a virtual video feed.
  • package.json: Specifies project dependencies, including the ws WebSocket library.

Dependencies

  • ws: WebSocket library for Node.js (version 8.18.3 or higher).

Integration Potential

This demo can be extended to integrate with various processing pipelines, such as:

  • Video Processing: Pipe the received frames into tools like FFmpeg for encoding or streaming.
  • Computer Vision: Feed the frames into libraries like OpenCV for real-time analysis.
  • Live Streaming: Forward the virtual video feed to platforms like RTMP servers for broadcasting.

To integrate with custom processing, modify server.js to handle the received binary data (JPEG frames) according to your needs, such as saving them as a video file or passing them to a processing library.

Notes

  • The counter increments every 50ms, and the canvas is streamed as JPEG images with 0.9 quality to balance performance and image clarity.
  • The WebSocket server currently outputs binary frame data to stdout. Modify server.js to process or save the frames as part of a virtual video pipeline.
  • Ensure the WebSocket server is running before opening index.html to establish the connection.

Troubleshooting

  • If the WebSocket connection fails, verify that the server is running on ws://localhost:8080 and that no other process is using port 8080.
  • If index.html is opened directly (file://), some browsers may restrict WebSocket connections due to security policies. Use a local web server to avoid this issue.

Future Improvements

  • Add server-side processing to reconstruct the virtual video feed (e.g., saving as MP4).
  • Implement frame buffering to handle high frame rates or network latency.
  • Enhance the canvas with more complex animations or real-time user interactions.
  • Integrate with a video processing library for real-time analysis or streaming.

License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages