v4l2rtspserver

repository·master·Indexed 24 days ago

https://github.com/mpromonet/v4l2rtspserver

A lightweight streaming server that captures video from V4L2 devices (H264, HEVC, JPEG, VP8, VP9) and audio from ALSA devices (PCM S16/S32 BE/LE), serving them via RTSP and HTTP (HLS/DASH) protocols. It supports RTP/UDP unicast and multicast, RTP/TCP, and RTSPS/SRTP via OpenSSL.

Tokens
2K
Snippets
4
Records
14
Agent score
35%

What's inside v4l2rtspserver

  1. Overview of v4l2rtspserver capabilities

    master

    v4l2rtspserver is a streaming server that captures feeds from Video4Linux (V4L2) devices and ALSA audio devices and serves them via RTSP or HTTP.

    Supported Video Capture Formats (V4L2):

    • H264, HEVC, JPEG, VP8, or VP9

    Supported Audio Capture Formats (ALSA):

    • PCM S16_BE, S16_LE, S32_BE, or S32_LE

    Supported Streaming Protocols:

    • RTSP: RTP/UDP unicast, RTP/UDP multicast, RTP/TCP, and RTP/RTSP/HTTP.
    • HTTP: HLS and MPEG-DASH (enabled via the -S option for compatible capture formats).
  2. Setup v4l2loopback for Uncompressed Formats

    master

    If your camera provides an uncompressed format, you can use v4l2tools to compress it to an intermediate virtual device using v4l2loopback before feeding it to v4l2rtspserver.

    Workflow:

    1. Load v4l2loopback.
    2. Compress /dev/video0 to /dev/video10.
    3. Stream from /dev/video10.
    modprobe v4l2loopback video_nr=10
    v4l2compress -fH264 /dev/video0 /dev/video10 &
    v4l2rtspserver /dev/video10 &
  3. Configure RTSP Authentication

    master

    Authentication can be enabled by defining users with the -U flag. You can use plain text passwords or MD5-hashed passwords.

    Plain Text: -U user:password

    MD5 Password: To use MD5, you must define a realm with -R. The password must be the result of md5(<username>:<realm>:<password>).

    Example (MD5):

    -R myrealm -U foo:$(echo -n foo:myrealm:bar | md5sum | cut -d- -f1)
  4. Use v4l2rtspserver with Docker

    master

    You can run the server using a Docker image. To access hardware devices like cameras, you must use the --device flag.

    Run with default settings:

    docker run -p 8554:8554 -it mpromonet/v4l2rtspserver

    Run with a specific V4L2 device:

    docker run --device=/dev/video0 -p 8554:8554 -it mpromonet/v4l2rtspserver

    Run with custom parameters:

    docker run --device=/dev/video0 -p 8554:8554 -it mpromonet/v4l2rtspserver -u "" -H640 -W480
  5. Install and Build v4l2rtspserver

    master

    To build the project from source, use cmake and make. The build process will automatically attempt to download and compile live555 if it is not present on your system.

    Note on Dependencies:

    • libasound2-dev: If missing, audio support will be disabled.
    • libssl-dev: If missing, rtsps and srtp will not be available.
    • liblog4cpp5-dev: If missing, logging defaults to std::cout.
    cmake . && make
    # Optional: install to system
    sudo make install
  6. Play HTTP (HLS/DASH) Streams

    master

    When started with the -S argument, the server enables HLS and MPEG-DASH via HTTP.

    MPEG-DASH: Use MP4Client <URL>/unicast.mpd.

    HLS:

    • VLC: vlc <URL>/unicast.m3u8
    • GStreamer: gst-launch-1.0 playbin uri=<URL>/unicast.m3u8
    • Browser: Use Firefox with [Native HLS addons] or Chrome with [Native HLS playback].
  7. Run v4l2rtspserver via CLI

    master

    The v4l2rtspserver executable provides a command-line interface to stream video and audio from V4L2 (Video4Linux2) and ALSA devices via RTSP, HLS, or MPEG-DASH.

    Basic Usage

    To start a basic stream, provide the V4L2 device path. You can also specify an ALSA device for audio using a comma-separated string.

    # Stream from /dev/video0
    v4l2rtspserver /dev/video0
    
    # Stream from /dev/video0 (video) and hw:1,0 (audio)
    v4l2rtspserver /dev/video0,/dev/hw:1,0

    Device Configuration

    • Video/Audio Split: Use a comma to separate the video device and the audio device: [V4L2 device],[ALSA device].
    • Resolution and Framerate: Use -G <w>x<h>[x<f>] to set width, height, and FPS in one command, or use -W, -H, and -F individually.
    • Capture Mode: Use -s to use the live555 mainloop instead of the default reader thread.
  8. Configure V4L2 and ALSA Capture

    master

    Use the following flags to tune the capture behavior:

    V4L2 Options:

    • -r: Use the read interface (default is memory mapped buffers).
    • -w: Use the write interface.
    • -B: Use blocking mode.
    • -s: Use live555 mainloop.
    • -f: Use current capture format (ignores -W, -H).
    • -fformat: Use a specific format (respects -W, -H).
    • -W <width>: Set capture width (default 640).
    • -H <height>: Set capture height (default 480).
    • -F <fps>: Set framerate (default 25, 0 to disable).
    • -G <w>x<h>[x<f>]: Set capture format.

    ALSA Options:

    • -A <freq>: Capture frequency and channel (default 44100).
    • -C <channels>: Capture channels (default 2).
    • -a <fmt>: Capture audio format (default S16_LE).
  9. Use v4l2rtspserver CLI

    master

    The server is invoked via a command-line interface that allows configuring RTSP, V4L2, and ALSA parameters.

    Basic Syntax: ./v4l2rtspserver [options] [device1] [device2]

    Session Composition Examples:

    • Single Video Session: ./v4l2rtspserver /dev/video0
    • Single Audio Session: ./v4l2rtspserver ,default
    • Combined Audio and Video (Single Session): ./v4l2rtspserver /dev/video0,default
    • Combined Audio and Video (Two Sessions): ./v4l2rtspserver /dev/video0 ,default
    • Two Video Sessions: ./v4l2rtspserver /dev/video0 /dev/video1
    • Audio associated with specific V4L2 device: ./v4l2rtspserver /dev/video0,/dev/video0
  10. Reference: General and Output CLI Options

    master

    Use these flags for logging, output files, and queue management.

    FlagArgumentDescription
    -v[v]Verbose (-v) or Very Verbose (-vv)
    -Q<length>Number of frame queue (default 5)
    -O<output>Copy captured frame to a file or a V4L2 device
    -VPrint version and exit
    -hPrint help and exit
  11. Reference: V4L2 CLI Options

    master

    Use these flags to configure how the server interacts with V4L2 video devices.

    FlagArgumentDescription
    -rV4L2 capture using read interface (default use memory mapped buffers)
    -wV4L2 capture using write interface (default use memory mapped buffers)
    -BV4L2 capture using blocking mode (default use non-blocking mode)
    -sV4L2 capture using live555 mainloop (default use a reader thread)
    -f[format]V4L2 capture using current capture format (if no arg) or specific format (if arg)
    -W<width>V4L2 capture width (default 0)
    -H<height>V4L2 capture height (default 0)
    -F<fps>V4L2 capture framerate (default 25)
    -G<w>x<h>[x<f>]V4L2 capture format (e.g., 1280x720x30)