WebRTC-Streamer

repository·master·Indexed 25 days ago

https://github.com/mpromonet/webrtc-streamer

A tool for streaming WebRTC media sources, including capture devices, screen captures, MKV files, and RTSP/RTP sources. It supports a simple signaling mechanism, the WHEP interface, and integration with Janus Gateway and Jitsi Video Rooms. The tool provides a CLI, Docker deployment options, and a JavaScript API for embedding streams in HTML via WebComponents or a dedicated JS class.

Tokens
4.6K
Snippets
10
Records
25
Agent score
86%

What's inside webrtc-streamer

  1. Build WebRTC-Streamer from source

    master

    Building the project requires installing Chromium depot tools, downloading the WebRTC source, and using CMake with Clang.

    CMake Parameters:

    • WEBRTCROOT: Path to the WebRTC source directory (default is $(pwd)/../webrtc).
    • WEBRTCDESKTOPCAPTURE: Enables desktop capture if available (default is ON).
    # 1. Install Chromium depot tools
    pushd ..
    git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
    export PATH=$PATH:`realpath depot_tools`
    popd
    
    # 2. Download WebRTC
    mkdir ../webrtc
    pushd ../webrtc
    fetch webrtc 
    popd
    
    # 3. Build WebRTC Streamer
    export PATH=$PATH:`realpath ../webrtc/src/third_party/llvm-build/Release+Asserts/bin`
    cmake -DCMAKE_C_COMPILER=clang . 
    make
  2. Configure embedded STUN/TURN for NAT traversal

    master

    If running behind a NAT, you can start embedded STUN and TURN servers and publish their public URLs. Use curl -s ifconfig.me to dynamically retrieve your public IP.

    Start embedded STUN server:

    ./webrtc-streamer --stun-server=0.0.0.0:3478 --stun=$(curl -s ifconfig.me):3478

    Start embedded STUN and TURN servers:

    ./webrtc-streamer --stun-server=0.0.0.0:3478 --stun=$(curl -s ifconfig.me):3478 --turn-server=0.0.0.0:3479 --turn=turn:turn@$(curl -s ifconfig.me):3479

    To configure NAT rules using UPnP on your router, you can use upnpc:

    upnpc -r 8000 tcp 3478 tcp 3478 udp

    (Adjust ports to match your HTTP, STUN, and TURN configuration).

  3. Run WebRTC-Streamer via CLI

    master

    WebRTC-Streamer can be run from the command line to stream WebRTC media sources like capture devices, screen capture, MKV files, and RTSP/RTP sources. You can provide a JSON configuration file or specify streams via arguments.

    Supported URL schemes for capturers:

    • rtsp://: RTSP capturer (based on live555)
    • file://: MKV capturer (based on live555)
    • rmtp://: RMTP capturer (based on librmtp)
    • screen://: Desktop screen capture
    • window://: Desktop window capture
    • v4l2://: V4L2 H264 frame capture (not supported on Windows)
    • videocap://: Video capture device name
    • audiocap://: Audio capture device name
    ./webrtc-streamer -C config.json
  4. Stream using WHEP

    master

    WebRTC-Streamer supports the WHEP (WebRTC-HTTP Egress Protocol) draft standard, allowing you to stream content to WHEP-compatible players like webrtc-player.

    To use WHEP, point the player's src attribute to the /api/whep endpoint, providing the source URL and options via query parameters.

    <html
    <head>
        <script src="https://unpkg.com/@eyevinn/whep-video-component@latest/dist/whep-video.component.js"></script>
    </head>
    <body
        <whep-video id="video" muted autoplay></whep-video>
        <script>
            video.setAttribute('src', `${location.origin}/api/whep?url=Asahi&options=rtptransport%3dtcp%26timeout%3d60`);
        </script>
    </body
    </html>
  5. Run WebRTC-Streamer using Docker

    master

    You can deploy WebRTC-Streamer using Docker images.

    Basic run:

    docker run -p 8000:8000 -it mpromonet/webrtc-streamer

    Expose V4L2 devices:

    docker run --device=/dev/video0 -p 8000:8000 -it mpromonet/webrtc-streamer

    Register an RTSP URL via Docker:

    docker run --name webrtc -d -p 8000:8000 -it mpromonet/webrtc-streamer -n raspicam -u rtsp://pi2.local:8554/unicast

    Use a local config.json file:

    docker run --name webrtc -d -p 8000:8000 -v $PWD/config.json:/usr/local/share/webrtc-streamer/config.json mpromonet/webrtc-streamer

    Run with host networking:

    docker run --name webrtc -d --net host mpromonet/webrtc-streamer

    Add extra host for NAT/Docker environments:

    docker run --name webrtc -d -p 8000:8000 --add-host host.docker.internal:host-gateway mpromonet/webrtc-streamer -e host.docker.internal
  6. Understand the WebRTC-Streamer HTTP and WebSocket API

    master

    WebRTC-Streamer exposes its functionality through a RESTful HTTP API and a WebSocket interface.

    HTTP API

    • Endpoints: The server registers specific URI endpoints based on the provided function map. Each endpoint supports GET, POST, PATCH, and DELETE methods.
    • Input: For POST or PATCH requests, the server expects a JSON body. If the body is not valid JSON, it attempts to treat the raw body as the message.
    • Output: The server returns a JSON response body and custom HTTP headers. If the API implementation returns a non-JSON body (as a string), it is sent as text/plain.
    • CORS: The server automatically includes Access-Control-Allow-Origin: * in responses to allow cross-origin requests.

    WebSocket API

    • Endpoint: /ws
    • Usage: Clients can send JSON messages over the WebSocket connection. The message must contain a request field specifying the command and an optional body field.
    • Format:
      {
        "request": "command_name",
        "body": { ... }
      }
    • Response: The server responds with a JSON string containing the result of the requested command.
  7. Configure Embedded STUN/TURN Servers

    master

    WebRTC-Streamer can act as a STUN or TURN server.

    • To start an embedded STUN server, use the -S flag with a bind address (e.g., 0.0.0.0:3478).
    • To start an embedded TURN server, use the -T flag. The TURN server supports both UDP and TCP. The format for the TURN URL should be turn:turn@<address>.

    If you use an external TURN server instead, use the -t flag.

  8. Deploy WebRTC-Streamer with Docker Compose

    master

    You can deploy the WebRTC-Streamer stack using Docker Compose. This configuration includes the webrtc-streamer service, a prometheus instance for metrics, and a grafana instance for visualization.

    Key service configurations:

    • webrtc-streamer: Uses the mpromonet/webrtc-streamer:latest image. It maps port 8000 for HTTP and a range of UDP ports 50000-50010 for WebRTC traffic. It is configured via command-line flags to use a config.json and allows communication with the host via host.docker.internal.
    version: '3'
    
    services:
      webrtc-streamer:
        image: mpromonet/webrtc-streamer:latest
        extra_hosts:
        - "host.docker.internal:host-gateway"
        command: ["-C", "config.json", "-R", "50000:50010", "-e", "host.docker.internal"]
        ports:
          - 8000:8000
          - 50000-50010:50000-50010/udp
    
      prometheus:
        image: prom/prometheus
        volumes:
          - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
          - prometheus_data:/prometheus
        command:
          - '--config.file=/etc/prometheus/prometheus.yml'
          - '--storage.tsdb.path=/prometheus'
        ports:
          - 9090:9090
    
      grafana:
        image: grafana/grafana
        user: "472"
        depends_on:
          - prometheus
        ports:
          - 3000:3000
        volumes:
          - grafana_data:/var/lib/grafana
          - ./grafana/grafana.ini:/etc/grafana/grafana.ini
          - ./grafana/provisioning/datasources:/etc/grafana/provisioning/datasources
          - ./grafana/provisioning/dashboards:/etc/grafana/provisioning/dashboards
    
    volumes:
        prometheus_data: {}
        grafana_data: {}
  9. Use WebComponents to display WebRTC streams

    master

    WebRTC-Streamer provides Web Components as an alternative way to display streams in HTML.

    Basic Usage:

    <html>
    <head>
    	<script type="module" src="webrtc-streamer-element.js"></script>
    </head>
    <body>
    	<webrtc-streamer url="rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov"></webrtc-streamer>
    </body>
    </html>
  10. Embed WebRTC-Streamer in HTML using JavaScript

    master

    To display a WebRTC stream in an HTML page served by a different server, use the WebRtcStreamer JavaScript class.

    Usage: var webRtcServer = new WebRtcStreamer(<video tag>, <webrtc-streamer url>);

    Example Implementation:

    <html>
    <head>
    <script src="libs/adapter.min.js" ></script>
    <script src="webrtcstreamer.js" ></script>
    <script>        
    	var webRtcServer      = null;
    	window.onload         = function() { 
    		webRtcServer      = new WebRtcStreamer("video",location.protocol+"//"+location.hostname+":8000");
    		webRtcServer.connect("rtsp://196.21.92.82/axis-media/media.amp","", "rtptransport=tcp&timeout=60");
    	}
    	window.onbeforeunload = function() { webRtcServer.disconnect(); }
    </script>
    </head>
    <body> 
    	<video id="video" muted playsinline />
    </body>
    </html>
  11. Connect to Jitsi Video Room

    master

    To publish WebRTC streams to a Jitsi Video Room, use the XMPPVideoRoom interface. This requires several XMPP-related libraries (strophe, etc.) and the xmppvideoroom.js script.

    Constructor: var xmpp = new XMPPVideoRoom(<xmpp server url>, <webrtc-streamer url>)

    Method: xmpp.join(roomName, streamUrl, displayName)

    <html
    <head>
    <script src="libs/strophe.min.js" ></script>
    <script src="libs/strophe.muc.min.js" ></script>
    <script src="libs/strophe.disco.min.js" ></script>
    <script src="libs/strophe.jingle.sdp.js"></script>
    <script src="libs/jquery-3.5.1.min.js"></script>
    <script src="xmppvideoroom.js" ></script>
    <script>        
    	var xmpp = new XMPPVideoRoom("meet.jit.si", null);
    	xmpp.join("testroom", "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov","Bunny");	    
    </script>
    </head>
    </html>