Skip to content

shiguredo/webrtc-rs

Repository files navigation

webrtc-rs

shiguredo_webrtc Documentation License

About Shiguredo's open source software

We will not respond to PRs or issues that have not been discussed on Discord. Also, Discord is only available in Japanese.

Please read https://github.com/shiguredo/oss before use.

時雨堂のオープンソースソフトウェアについて

利用前に https://github.com/shiguredo/oss をお読みください。

概要

libwebrtc の C API バインディングを Rust から安全に利用するためのライブラリです。

特徴

  • webrtc-build でビルドされた libwebrtc を利用
  • C++ の薄い C API ラッパー層と Rust の安全な API 層の 2 層構造
  • cargo build だけでビルド
    • デフォルトでは prebuilt 済みライブラリを GitHub Releases から自動ダウンロード
    • CMake、libclang、C++ コンパイラなどのインストールは不要

サポートプラットフォーム

  • Ubuntu 24.04 LTS x86_64
  • Ubuntu 24.04 LTS arm64
  • Ubuntu 22.04 LTS x86_64
  • Ubuntu 22.04 LTS arm64
  • Windows 11 x86_64
  • Windows Server 2025 x86_64
  • macOS Tahoe 26 arm64
  • macOS Sequoia 15 arm64
  • Raspberry Pi OS (64-bit) arm64
    • Debian version: 13 (trixie)

Ubuntu の対応バージョン

直近の LTS 2 バージョンをサポートします。

macOS の対応バージョン

直近の 2 バージョンをサポートします。

Windows の対応バージョン

直近のバージョンをサポートします。

Raspberry Pi OS の対応バージョン

最新 Debian バージョンのみサポートします。

Note

Raspberry Pi OS (Legacy) や Raspberry Pi OS (32-bit) はサポート対象外です。

バージョニング

  • メジャーバージョンは常に 0
  • マイナーバージョンは libwebrtc の m バージョンと一致 (例: 0.146.x は m146)
  • パッチバージョンは同一 m バージョン内での変更時にインクリメント

使い方

依存の追加

[dependencies]
shiguredo_webrtc = "0.146"

PeerConnectionFactory の生成

use shiguredo_webrtc::{
    AudioDecoderFactory, AudioDeviceModule, AudioDeviceModuleAudioLayer,
    AudioEncoderFactory, AudioProcessingBuilder, ConnectionContext, Environment,
    PeerConnectionFactory, PeerConnectionFactoryDependencies,
    RtcEventLogFactory, Thread, VideoDecoderFactory, VideoEncoderFactory,
};
use std::sync::Arc;

pub struct FactoryHolder {
    factory: PeerConnectionFactory,
    connection_context: ConnectionContext,
    _network: Thread,
    _worker: Thread,
    _signaling: Thread,
}

impl FactoryHolder {
    pub fn new() -> Option<Arc<Self>> {
        let env = Environment::new();
        let mut network = Thread::new_with_socket_server();
        let mut worker = Thread::new();
        let mut signaling = Thread::new();
        network.start();
        worker.start();
        signaling.start();

        let mut deps = PeerConnectionFactoryDependencies::new();
        deps.set_network_thread(&network);
        deps.set_worker_thread(&worker);
        deps.set_signaling_thread(&signaling);
        let event_log = RtcEventLogFactory::new();
        deps.set_event_log_factory(event_log);
        let adm = AudioDeviceModule::new(
            &env, AudioDeviceModuleAudioLayer::Dummy,
        ).ok()?;
        deps.set_audio_device_module(&adm);
        let audio_enc = AudioEncoderFactory::builtin();
        let audio_dec = AudioDecoderFactory::builtin();
        deps.set_audio_encoder_factory(&audio_enc);
        deps.set_audio_decoder_factory(&audio_dec);
        let video_enc = VideoEncoderFactory::builtin();
        let video_dec = VideoDecoderFactory::builtin();
        deps.set_video_encoder_factory(video_enc);
        deps.set_video_decoder_factory(video_dec);
        let apb = AudioProcessingBuilder::new_builtin();
        deps.set_audio_processing_builder(apb);
        deps.enable_media();

        let (factory, connection_context) =
            PeerConnectionFactory::create_modular_with_context(&mut deps).ok()?;
        Some(Arc::new(Self {
            factory,
            connection_context,
            _network: network,
            _worker: worker,
            _signaling: signaling,
        }))
    }
}

対応 API

PeerConnection

  • PeerConnectionFactory
    • PeerConnection の生成
  • PeerConnection
    • 接続の管理
  • PeerConnectionFactoryDependencies
    • ファクトリの依存関係設定
  • PeerConnectionFactoryOptions
    • ファクトリオプション (暗号化無効化)
  • PeerConnectionDependencies
    • PeerConnection の依存関係設定
    • set_proxy(...) で TURN 用の HTTP Proxy を設定
  • PeerConnectionRtcConfiguration
    • ICE / 接続設定
  • ConnectionContext
    • default_network_manager() / default_socket_factory() を取得
  • NetworkManagerRef / PacketSocketFactoryRef
    • ConnectionContext::default_network_manager() / default_socket_factory() で取得
  • PeerConnectionOfferAnswerOptions
    • Offer/Answer オプション (ICE リスタート、Simulcast レイヤー数など)
  • PeerConnectionObserver / PeerConnectionObserverHandler
    • イベントコールバック
  • PeerConnectionState
    • 接続状態
  • IceConnectionState
    • ICE 接続状態
  • IceGatheringState
    • ICE 収集状態
  • IceCandidateError
    • ICE 候補エラー
  • TlsCertPolicy
    • TLS 証明書ポリシー (Secure, InsecureNoCheck)
  • CreateSessionDescriptionObserver
    • SDP 生成コールバック
  • SetLocalDescriptionObserver
    • ローカル SDP 設定コールバック
  • SetRemoteDescriptionObserver
    • リモート SDP 設定コールバック

メディア

  • AudioTrackSource / AudioTrack
    • 音声トラック
  • VideoTrackSource / VideoTrack
    • 映像トラック
  • AdaptedVideoTrackSource
    • アダプティブ映像ソース
  • AdaptedSize / AdaptFrameResult
    • アダプティブフレーム結果
  • AudioDeviceModule
    • 音声デバイスインターフェース
  • AudioDeviceModuleAudioLayer
    • 音声デバイスレイヤー種別 (PlatformDefault, Dummy など)
  • AudioDeviceModuleHandler / AudioDeviceModuleStats
    • カスタム ADM handler と統計
  • AudioTransport / AudioTransportRef / AudioTransportHandler
    • 音声トランスポート
  • MediaStreamTrack
    • メディアストリームトラック
  • I420Buffer
    • I420 フォーマットの映像バッファ
  • VideoFrame / VideoFrameRef
    • 映像フレーム
  • VideoSink / VideoSinkHandler
    • 映像フレームシンク
  • VideoSinkWants
    • 映像シンク要求設定
  • SdpVideoFormat
    • 映像フォーマット

映像コーデック

  • VideoCodecRef / VideoCodecType / VideoCodecStatus
    • 映像コーデック共通型とステータス
  • ScalabilityMode
    • スケーラビリティーモード
  • VideoFrameType / VideoFrameTypeVector / VideoFrameTypeVectorRef
    • エンコード対象フレーム種別
  • EncodedImageBuffer / EncodedImage / EncodedImageRef
    • エンコード済み映像データ
  • CodecSpecificInfo / CodecSpecificInfoRef / H264PacketizationMode
    • コーデック固有情報
  • VideoEncoder
    • カスタム映像エンコーダー
  • VideoEncoderHandler / VideoEncoderFactoryHandler
    • エンコーダー / エンコーダーファクトリーの handler trait
  • VideoEncoderEncoderInfo / VideoEncoderSettingsRef / VideoEncoderRateControlParametersRef
    • エンコーダー設定とメタ情報
  • VideoEncoderEncodedImageCallback / VideoEncoderEncodedImageCallbackRef
    • エンコード完了 callback
  • VideoEncoderEncodedImageCallbackHandler
    • エンコード完了 callback の handler trait
  • VideoEncoderEncodedImageCallbackResult
    • エンコード完了 callback の戻り値
  • VideoEncoderEncodedImageCallbackResultError
    • エンコード完了 callback のエラーコード
  • VideoEncoderEncodedImageCallbackPtr
    • C API 側 callback ポインターのラッパー
  • VideoDecoder
    • カスタム映像デコーダー
  • VideoDecoderHandler / VideoDecoderFactoryHandler
    • デコーダー / デコーダーファクトリーの handler trait
  • VideoDecoderDecoderInfo / VideoDecoderSettingsRef
    • デコーダー設定とメタ情報
  • VideoDecoderDecodedImageCallbackRef / VideoDecoderDecodedImageCallbackPtr
    • デコード完了 callback

RTP

  • RtpCapabilities
    • コーデック能力
  • RtpCodecCapability
    • 個別コーデック設定
  • RtpCodecCapabilityVector
    • コーデック能力ベクタ
  • RtpEncodingParameters / RtpEncodingParametersVector
    • エンコーディング設定
  • RtpParameters
    • RTP 送信パラメータ
  • DegradationPreference
    • 映像劣化方針
  • RtpTransceiver / RtpSender / RtpReceiver
    • トランシーバー管理
  • RtpTransceiverDirection
    • 送受信方向
  • RtpTransceiverInit
    • トランシーバー初期化
  • RtpCodec
    • RTP コーデック情報
  • Resolution
    • 解像度
  • MediaType
    • メディア種別 (Audio / Video)

DataChannel

  • DataChannel
    • 双方向データ転送
  • DataChannelInit
    • DataChannel 初期化設定 (ordered, protocol)
  • DataChannelObserver / DataChannelObserverHandler
    • データチャネルイベント
  • DataChannelState
    • チャネル状態

JSEP

  • SessionDescription
    • SDP Offer/Answer
  • SdpType
    • SDP タイプ (Offer, Answer, PrAnswer, Rollback)
  • IceCandidate
    • ICE 候補
  • IceServer / IceServerVector
    • ICE サーバー設定
  • IceTransportsType
    • ICE トランスポートモード
  • SdpParseError
    • SDP パースエラー

統計

  • RTCStatsReport
    • 統計レポート

SSL/TLS

  • SSLCertificateVerifier / SSLCertificateVerifierHandler
    • TLS 証明書のカスタム検証
  • SSLCertificateRef
    • SSL 証明書の参照
  • SSLCertChainRef
    • SSL 証明書チェーンの参照
  • SSLIdentity
    • TURN-TLS 接続でのクライアント認証 (mTLS) 用の SSL アイデンティティ

エラー

  • RtcError
    • libwebrtc のエラー型
  • Error / Result
    • Rust エラー型

ユーティリティ

  • Environment
    • WebRTC 環境の初期化
  • Thread
    • スレッド管理
  • AudioEncoderFactory / AudioDecoderFactory
    • 音声コーデックファクトリ
  • VideoEncoderFactory / VideoDecoderFactory
    • 映像コーデックファクトリ
  • AudioProcessingBuilder
    • 音声処理パイプライン
  • RtcEventLogFactory
    • イベントログ
  • TimestampAligner
    • タイムスタンプ調整
  • abgr_to_i420 / convert_from_i420 / i420_to_nv12 / nv12_to_i420 / yuy2_to_i420
    • カラーフォーマット変換 (libyuv)
  • random_bytes / random_string
    • ランダム生成
  • time_millis / thread_sleep_ms
    • 時間ユーティリティ
  • version
    • クレートのバージョン取得
  • log / log::Severity
    • ログ機能 (log_to_debug, enable_timestamps, enable_threads, print)
  • rtc_log_verbose! / rtc_log_info! / rtc_log_warning! / rtc_log_error!
    • ログ出力マクロ

ビルド

デフォルト (prebuilt)

デフォルトでは、リリース時に GitHub Actions でビルドされた prebuilt ライブラリ (Linux/macOS は libwebrtc_c.a、Windows は webrtc_c.lib) と生成済みバインディング (bindings.rs) を GitHub Releases から自動ダウンロードします。

CMake、libclang、C++ コンパイラなどのインストールは不要です。

必要なもの:

  • Rust 1.88 以上
  • curl (prebuilt ダウンロードに使用)
  • tar (prebuilt 展開に使用)
  • Linux の場合: libx11-dev (リンク時に必要)
# Linux
sudo apt-get install curl tar libx11-dev

# macOS は curl と tar がプリインストール済みのため追加インストール不要

ソースビルド

C++ ラッパーのコード変更や、prebuilt が提供されていないプラットフォームでビルドする場合は source-build feature を有効にしてください。

[dependencies]
shiguredo_webrtc = { version = "0.146", features = ["source-build"] }

または cargo build 時に指定:

cargo build --features source-build

ソースビルドでは以下が自動的に行われます:

  1. CMake のダウンロード
  2. libwebrtc のダウンロード (webrtc-build の GitHub Releases から)
  3. C++ ラッパーのコンパイル
  4. bindgen によるバインディング生成

ソースビルドに必要なもの:

  • Rust 1.88 以上
  • Linux の場合:
    • C++ コンパイラ (build-essential)
    • libclang-dev (bindgen が利用)
    • libx11-dev
  • macOS の場合:
    • Xcode Command Line Tools
  • Windows の場合:
    • Visual Studio 2022 (Desktop development with C++)
    • C++ Clang tools for Windows (Microsoft.VisualStudio.Component.VC.Llvm.Clang)
      • bindgen 用の libclang として利用します (C++ コンパイルは MSVC を利用)
# Linux
sudo apt-get install build-essential libclang-dev libx11-dev

# macOS は Xcode Command Line Tools があれば OK

# Windows は Visual Studio Installer から
#   - Desktop development with C++
#   - C++ Clang tools for Windows
# を有効化してください

環境変数

  • WEBRTC_C_TARGET
    • ビルドターゲットを変更する場合に指定 (デフォルトはホスト環境に応じて自動判定)

ライセンス

Apache License 2.0

Copyright 2026-2026, Wandbox LLC (Original Author)
Copyright 2026-2026, Shiguredo Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

Rust bindings for libwebrtc

Resources

License

Stars

Watchers

Forks

Contributors