37个版本

0.21.2 2024年7月30日
0.20.2 2024年3月17日
0.19.4 2023年12月8日
0.19.1 2023年9月23日
0.7.2 2020年7月25日

#2 in #libdatachannel

Download history 59/week @ 2024-04-22 212/week @ 2024-04-29 59/week @ 2024-05-06 98/week @ 2024-05-13 71/week @ 2024-05-20 39/week @ 2024-05-27 120/week @ 2024-06-03 75/week @ 2024-06-10 2/week @ 2024-06-17 10/week @ 2024-06-24 1/week @ 2024-07-01 17/week @ 2024-07-15 14/week @ 2024-07-22 156/week @ 2024-07-29 30/week @ 2024-08-05

217 每月下载量
4 个crate中使用 (通过 datachannel)

MPL-2.0 许可证

4.5MB
114K SLoC

C 97K SLoC // 0.2% comments C++ 16K SLoC // 0.1% comments Shell 266 SLoC // 0.3% comments Python 160 SLoC // 0.1% comments Automake 146 SLoC // 0.4% comments Rust 125 SLoC // 0.1% comments Objective-C++ 22 SLoC // 0.1% comments INI 14 SLoC // 0.4% comments

包含 (自动工具混淆代码, 225KB) libdatachannel/deps/libsrtp/configure, (模糊autoconf代码, 15KB) libdatachannel/deps/libsrtp/configure.ac, (模糊autoconf代码, 7KB) libdatachannel/deps/usrsctp/configure.ac

datachannel-rs   最新版本 文档

Rust的封装,用于 libdatachannel,这是一个独立的C++实现,用于WebRTC数据通道。

用法

此crate提供了两个必须由最终用户实现的特质,DataChannelHandlerPeerConnectionHandler,分别定义了 RtcPeerConnectionRtcDataChannel 结构体的所有回调。

上述特质定义如下

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>>) {}
}

注意,所有 on_* 方法都有一个默认的无操作实现。

主结构体 RtcPeerconnection 接受一个 RtcConfig(定义ICE服务器)和一个 PeerConnectionHandler 实例。

以下是基本工作流程

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())?;

完整的实现示例可以在 测试 中找到。

有关基于异步实现的更多信息,请参阅 async-datachannel

Cargo功能

  • log默认)启用 log crate 提供的日志记录(与 tracing 互斥)。
  • tracing 启用 tracing crate 提供的日志记录(与 log 互斥)。
  • 内部集成 将 libdatachannel 及其依赖项静态编译并打包到构建中(包括 OpenSSL)。
  • 媒体 通过 libdatachannel 启用媒体支持。

构建

注意,编译 libdatachannel 需要使用 datachannel-sys,并需要 CMake

Apple macOS

如果你的构建失败,并且出现与 OpenSSL 相关的错误,你可能需要设置以下环境变量。

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

使用你本地 OpenSSL 安装的路径。

Ubuntu

所需依赖

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

依赖项