#web-rtc #p2p #define

datachannel

Rust wrappers for libdatachannel

41 releases (12 breaking)

0.13.0 Apr 30, 2024
0.12.1 Mar 17, 2024
0.12.0 Feb 17, 2024
0.11.2 Dec 21, 2023
0.1.1 Jul 25, 2020

#264 in Network programming

Download history 42/week @ 2024-01-17 34/week @ 2024-01-24 22/week @ 2024-01-31 39/week @ 2024-02-07 274/week @ 2024-02-14 281/week @ 2024-02-21 103/week @ 2024-02-28 99/week @ 2024-03-06 211/week @ 2024-03-13 115/week @ 2024-03-20 85/week @ 2024-03-27 71/week @ 2024-04-03 33/week @ 2024-04-10 44/week @ 2024-04-17 95/week @ 2024-04-24 149/week @ 2024-05-01

321 downloads per month
Used in 3 crates (2 directly)

MPL-2.0 license

4.5MB
115K SLoC

C 97K SLoC // 0.2% comments C++ 16K SLoC // 0.1% comments Rust 1.5K SLoC // 0.0% comments Shell 265 SLoC // 0.3% comments Python 159 SLoC // 0.1% comments Automake 145 SLoC // 0.4% comments Objective-C++ 21 SLoC // 0.1% comments INI 13 SLoC // 0.4% comments

datachannel-rs   latest doc

Rust wrappers for libdatachannel, a WebRTC Data Channels standalone implementation in C++.

Usage

This crate provides two traits that end user must implement, DataChannelHandler and PeerConnectionHandler, which defined all callbacks for RtcPeerConnection and RtcDataChannel structs respectively.

Aforementioned traits are defined as follows:

pub trait DataChannelHandler {
    fn on_open(&mut self) {}
    fn on_closed(&mut self) {}
    fn on_error(&mut self, err: &str) {}
    fn on_message(&mut self, msg: &[u8]) {}
    fn on_buffered_amount_low(&mut self) {}
    fn on_available(&mut self) {}
}

pub trait PeerConnectionHandler {
    type DCH;

    fn data_channel_handler(&mut self, info: DataChannelInfo) -> Self::DCH;

    fn on_description(&mut self, sess_desc: SessionDescription) {}
    fn on_candidate(&mut self, cand: IceCandidate) {}
    fn on_connection_state_change(&mut self, state: ConnectionState) {}
    fn on_gathering_state_change(&mut self, state: GatheringState) {}
    fn on_data_channel(&mut self, data_channel: Box<RtcDataChannel<Self::DCH>>) {}
}

Note that all on_* methods have a default no-operation implementation.

The main struct, RtcPeerconnection, takes a RtcConfig (which defines ICE servers) and a instance of PeerConnectionHandler.

Here is the basic workflow:

use datachannel::{DataChannelHandler, DataChannelInfo, PeerConnectionHandler, RtcConfig, RtcPeerConnection};

struct MyChannel;

impl DataChannelHandler for MyChannel {
    fn on_open(&mut self) {
        // TODO: notify that the data channel is ready (optional)
    }

    fn on_message(&mut self, msg: &[u8]) {
        // TODO: process the received message
    }
}

struct MyConnection;

impl PeerConnectionHandler for MyConnection {
    type DCH = MyChannel;

    /// Used to create the `RtcDataChannel` received through `on_data_channel`.
    fn data_channel_handler(&mut self, _info: DataChannelInfo) -> Self::DCH {
        MyChannel
    }

    fn on_data_channel(&mut self, mut dc: Box<RtcDataChannel<Self::DCH>>) {
        // TODO: store `dc` to keep receiving its messages (otherwise it will be dropped)
    }
}

let ice_servers = vec!["stun:stun.l.google.com:19302"];
let conf = RtcConfig::new(&ice_servers);

let mut pc = RtcPeerConnection::new(&conf, MyConnection)?;

let mut dc = pc.create_data_channel("test-dc", MyChannel)?;
// TODO: exchange `SessionDescription` and `IceCandidate` with remote peer
// TODO: wait for `dc` to be opened (should be signaled through `on_open`)
// ...
// Then send a message
dc.send("Hello Peer!".as_bytes())?;

Complete implementation example can be found in the tests.

See also async-datachannel for an async-based implementation.

Cargo features

  • log (default) Enables logging provided by the log crate (mutually exclusive with tracing).
  • tracing Enables logging provided by the tracing crate (mutally exclusive with log).
  • vendored Builds libdatachannel and its dependencies statically and bundles them in the build (including OpenSSL).
  • media Enables media support through libdatachannel.

Building

Note that CMake is required to compile libdatachannel through datachannel-sys.

Apple macOS

You probably need to set the following environment variables if your build fails with an OpenSSL related error.

export OPENSSL_ROOT_DIR=/usr/local/opt/openssl@3
export OPENSSL_LIBRARIES=/usr/local/opt/openssl@3/lib

With the paths of your local OpenSSL installation.

Ubuntu

Required dependencies:

# Needed to compile libdatachannel
sudo apt install build-essential cmake pkg-config libssl-dev clang

Dependencies

~3–12MB
~157K SLoC