libpeer Documentation

repository·main·Indexed 23 days ago

https://github.com/sepfy/libpeer

A portable WebRTC implementation written in C using BSD sockets, optimized for IoT and embedded devices such as ESP32, Raspberry Pi, and RP2040 Pico W. It supports video, audio, and data streaming with codecs including H264, OPUS, and G.711 PCM, and signaling via WHIP and MQTT. The library provides platform-specific examples for MJPEG over datachannel on ESP32, ping-pong functionality on Pico W, and two-way audio/video streaming on Raspberry Pi.

Tokens
2K
Snippets
10
Records
15
Agent score
79%

What's inside libpeer

  1. Overview of libpeer features

    main
    libpeer is a portable WebRTC implementation written in C using BSD sockets, designed for IoT and embedded devices like ESP32 and Raspberry Pi. It supports video/audio streaming, data channels, and various signaling methods.
  2. Configure and build the Pico W project

    main

    Set the required environment variables for the SDK paths and your WiFi credentials, then use CMake to configure the build in the libpeer/examples/pico/build directory.

    $ export PICO_SDK_PATH=<your PICO SDK path>/pico-sdk
    $ export FREERTOS_KERNEL_PATH=<your FreeRTOS kernel path>/FreeRTOS-Kernel
    $ export WIFI_SSID=<your WIFI SSID>
    $ export WIFI_PASSWORD=<your WIFI password>
    
    $ cd libpeer/examples/pico
    $ mkdir build
    $ cd build
    $ cmake ..
  3. Build the Raspberry Pi example

    main

    To build the Raspberry Pi camera and audio example, clone the libpeer repository recursively, navigate to the example directory, and use cmake to generate the build files.

    $ git clone --recursive https://github.com/sepfy/libpeer
    $ cd libpeer/examples/raspberrypi
    $ mkdir build && cd build
    $ cmake ..
    $ make
  4. Patch libsrtp for RP2040 PICO W compatibility

    main

    Before building, you must modify libpeer/third_party/libsrtp/include/srtp.h to change the signature of srtp_remove_stream from unsigned int to uint32_t for the ssrc parameter.

    @@ -614,7 +614,7 @@ srtp_err_status_t srtp_add_stream(srtp_t session, const srtp_policy_t *policy);
      *    - [other]           otherwise.
      *
      */
    -srtp_err_status_t srtp_remove_stream(srtp_t session, unsigned int ssrc);
    +srtp_err_status_t srtp_remove_stream(srtp_t session, uint32_t ssrc);
     
     /**
      * @brief srtp_update() updates all streams in the session.
  5. Platform-specific examples for libpeer

    main

    libpeer provides specialized examples for different hardware platforms:

    • ESP32: Demonstrates MJPEG over datachannel.
    • PICO: Demonstrates ping-pong functionality using datachannel.
    • Raspberry Pi: Demonstrates video and two-way audio streaming.
  6. Setup libpeer for ESP32

    main

    To use libpeer on ESP32 hardware for streaming JPEG images over WebRTC data channels, follow these steps:

    1. Install ESP-IDF: Ensure you have ESP-IDF version 5.2 or higher installed.
    2. Download libpeer: Clone the repository and navigate to the ESP32 example directory.
    3. Obtain Signaling URL: Visit the libpeer test website and copy the provided URL.
    4. Configure Build: Use idf.py menuconfig to set your Signaling URL and WiFi credentials.
    5. Build and Flash: Compile the project and flash it to your device using the ESP-IDF tools.
    # Install esp-idf (v5.2 or higher)
    $ git clone -b v5.2.2 https://github.com/espressif/esp-idf.git
    $ cd esp-idf
    $ source install.sh
    $ source export.sh
    
    # Download Required Libraries
    $ git clone https://github.com/sepfy/libpeer
    $ cd libpeer/examples/esp32
    
    # Build the Project
    $ idf.py build
    
    # Flash and Test
    $ idf.py flash
  7. Setup libpeer for RP2040 PICO W

    main

    To establish a DataChannel connection between a Raspberry Pi Pico W and the web, you must first clone the necessary repositories recursively to ensure all submodules are included.

    Required repositories:

    1. libpeer
    2. pico-sdk
    3. FreeRTOS-Kernel
    $ git clone --recursive https://github.com/sepfy/libpeer
    $ git clone --recurisve https://github.com/raspberrypi/pico-sdk
    $ git clone --recursive https://github.com/FreeRTOS/FreeRTOS-Kernel
  8. Build and run the libpeer generic example

    main

    To test libpeer on a Linux-based system, follow these steps to install dependencies, build the library, download test media, and run the generic sample application. You will need a URL from the libpeer test website to connect.

    $ sudo apt -y install git cmake
    $ git clone --recursive https://github.com/sepfy/libpeer
    $ cd libpeer
    $ cmake -S . -B build && cmake --build build
    $ wget http://www.live555.com/liveMedia/public/264/test.264 # Download test video file
    $ wget https://mauvecloud.net/sounds/alaw08m.wav # Download test audio file
    $ ./examples/generic/sample -u <URL>
  9. Install dependencies for Raspberry Pi example

    main

    Before building the example, you must install the necessary GStreamer plugins, development libraries, and build tools on your Raspberry Pi OS Lite image.

    $ sudo apt update
    $ sudo apt install -y gstreamer1.0-libcamera gstreamer1.0-alsa gstreamer1.0-tools gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-plugins-good libgstreamer1.0-dev git cmake
  10. Configure ESP32 build settings

    main

    After downloading the example, you must configure the connection parameters using the ESP-IDF configuration tool. Run idf.py menuconfig and update the following sections:

    • Peer Connection Configuration -> Signaling URL: Enter the URL copied from the libpeer test website.
    • Example Connection Configuration -> WiFi SSID and WiFi Password: Enter your local network credentials.
    $ idf.py menuconfig