Convert Figma logo to code with AI

webrtc-rs logowebrtc

A pure Rust implementation of WebRTC

4,041
358
4,041
120

Top Related Projects

13,375

Pure Go implementation of the WebRTC API

Janus WebRTC Server

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

📡 Simple WebRTC video, voice, and data channels

12,311

Simple peer-to-peer with WebRTC.

Quick Overview

The webrtc-rs project is a Rust implementation of the WebRTC (Web Real-Time Communication) protocol, which enables real-time communication between web browsers and devices. It provides a high-level API for building WebRTC-based applications in Rust.

Pros

  • Cross-platform: The library is designed to be cross-platform, supporting Windows, macOS, and Linux.
  • Asynchronous: The library uses Rust's async/await syntax, making it easy to integrate with other asynchronous Rust code.
  • Modular: The library is divided into several modules, allowing developers to use only the parts they need.
  • Actively Maintained: The project has regular updates and a responsive community.

Cons

  • Limited Documentation: The project's documentation could be more comprehensive, especially for beginners.
  • Steep Learning Curve: Integrating WebRTC into a Rust application can be challenging, especially for developers new to both Rust and WebRTC.
  • Dependency on C/C++ Libraries: The library relies on C/C++ libraries for some low-level functionality, which can make it more difficult to deploy and maintain.
  • Lack of Feature Parity: The library may not yet have feature parity with the official WebRTC implementation in C++.

Code Examples

Here are a few code examples demonstrating the usage of the webrtc-rs library:

Establishing a WebRTC Connection

use webrtc::api::APIBuilder;
use webrtc::ice_transport::ice_server::RTCIceServer;
use webrtc::peer_connection::configuration::RTCConfiguration;
use webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState;

#[tokio::main]
async fn main() {
    let api = APIBuilder::new().build();
    let configuration = RTCConfiguration {
        ice_servers: vec![RTCIceServer {
            urls: vec!["stun:stun.l.google.com:19302".to_string()],
            ..Default::default()
        }],
        ..Default::default()
    };

    let peer_connection = api.new_peer_connection(configuration).await.unwrap();

    // Add event listeners and handle signaling
    peer_connection.on_connection_state_change(|state| {
        println!("Connection state changed: {:?}", state);
        if state == RTCPeerConnectionState::Connected {
            println!("Connected!");
        }
    });

    // Perform signaling and exchange ICE candidates
    // ...
}

Sending and Receiving Data Channels

use webrtc::data_channel::data_channel_init::RTCDataChannelInit;
use webrtc::data_channel::RTCDataChannel;
use webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState;

#[tokio::main]
async fn main() {
    // Establish a WebRTC connection (see previous example)
    let data_channel = peer_connection
        .create_data_channel("my-channel", &RTCDataChannelInit::default())
        .await
        .unwrap();

    data_channel.on_message(|message| {
        println!("Received message: {:?}", message);
    });

    data_channel.send("Hello, WebRTC!").await.unwrap();
}

Capturing and Sending Video

use webrtc::media::track::track_local::kind::video::RTCVideoTrackInit;
use webrtc::media::track::track_local::RTCLocalTrack;
use webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState;

#[tokio::main]
async fn main() {
    // Establish a WebRTC connection (see previous example)
    let video_track = RTCLocalTrack::new(
        RTCVideoTrackInit {
            track_identifier: "video_track".to_string(),
            ..Default::default()
        },
        None,
    )
    .await
    .unwrap();

    peer_connection.add_track

Competitor Comparisons

13,375

Pure Go implementation of the WebRTC API

Pros of Pion

  • Written in Go, which offers better performance and concurrency handling compared to Rust
  • More mature project with a larger community and ecosystem
  • Extensive documentation and examples available

Cons of Pion

  • Less memory-safe than Rust-based WebRTC
  • Potentially higher memory usage due to Go's garbage collection
  • May have slightly longer compile times compared to Rust

Code Comparison

WebRTC-rs:

use webrtc::peer_connection::RTCPeerConnection;
use webrtc::peer_connection::configuration::RTCConfiguration;

let config = RTCConfiguration::default();
let pc = RTCPeerConnection::new(&config)?;

Pion:

import (
    "github.com/pion/webrtc/v3"
)

peerConnection, err := webrtc.NewPeerConnection(webrtc.Configuration{})
if err != nil {
    panic(err)
}

Both repositories provide WebRTC implementations in their respective languages. WebRTC-rs offers the benefits of Rust's memory safety and performance, while Pion leverages Go's simplicity and extensive standard library. The choice between them often depends on the developer's language preference and specific project requirements.

Janus WebRTC Server

Pros of Janus-gateway

  • Mature and widely-used WebRTC server implementation with extensive features
  • Supports multiple programming languages through its REST API and WebSocket interface
  • Highly scalable and suitable for large-scale deployments

Cons of Janus-gateway

  • Written in C, which may be less accessible for developers more familiar with higher-level languages
  • Steeper learning curve compared to WebRTC-rs due to its comprehensive feature set

Code Comparison

Janus-gateway (C):

janus_plugin *create(void) {
    janus_plugin *plugin = (janus_plugin *)calloc(1, sizeof(janus_plugin));
    plugin->init = janus_echotest_init;
    plugin->destroy = janus_echotest_destroy;
    plugin->handle_message = janus_echotest_handle_message;
    return plugin;
}

WebRTC-rs (Rust):

pub async fn create_peer_connection(
    config: RTCConfiguration,
) -> Result<RTCPeerConnection> {
    RTCPeerConnection::new(config).await
}

The code snippets demonstrate the different approaches and languages used in each project. Janus-gateway uses C for low-level control, while WebRTC-rs leverages Rust's safety and modern syntax.

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

Pros of jitsi-meet

  • Full-featured video conferencing solution with a complete user interface
  • Supports large-scale meetings with multiple participants
  • Includes additional features like screen sharing, chat, and recording

Cons of jitsi-meet

  • More complex setup and deployment compared to a lightweight library
  • Higher resource requirements due to its comprehensive feature set
  • Less flexibility for custom implementations or integrations

Code Comparison

jitsi-meet (JavaScript):

import { JitsiMeetJS } from 'lib-jitsi-meet';

const connection = new JitsiMeetJS.JitsiConnection(null, null, options);
connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_ESTABLISHED, onConnectionSuccess);
connection.connect();

webrtc (Rust):

use webrtc::peer_connection::RTCPeerConnection;

let peer_connection = RTCPeerConnection::new()?;
let data_channel = peer_connection.create_data_channel("data", None)?;

Summary

jitsi-meet is a comprehensive video conferencing solution with a full user interface, while webrtc is a lower-level WebRTC implementation in Rust. jitsi-meet offers a ready-to-use platform but may be less flexible for custom integrations. webrtc provides more control over the WebRTC implementation but requires more development effort to create a complete application.

📡 Simple WebRTC video, voice, and data channels

Pros of simple-peer

  • Easier to use and more beginner-friendly
  • Provides a higher-level abstraction for WebRTC
  • Supports both Node.js and browser environments

Cons of simple-peer

  • Less flexible and customizable than webrtc
  • May not support all advanced WebRTC features
  • Written in JavaScript, which may have performance limitations compared to Rust

Code Comparison

simple-peer (JavaScript):

const peer = new SimplePeer({ initiator: true })
peer.on('signal', data => {
  // send signal data to peer
})
peer.on('connect', () => {
  peer.send('hello')
})

webrtc (Rust):

let pc = RTCPeerConnection::new()?;
let dc = pc.create_data_channel("data")?;
dc.on_open(Box::new(move || {
    dc.send_text("hello").unwrap();
}));

Both libraries provide abstractions for WebRTC functionality, but simple-peer offers a more straightforward API for basic use cases. webrtc, being a Rust implementation, provides lower-level control and potentially better performance, but requires more setup and understanding of WebRTC concepts.

simple-peer is ideal for quick prototyping and simpler applications, while webrtc is better suited for more complex, performance-critical projects that require fine-grained control over WebRTC functionality.

12,311

Simple peer-to-peer with WebRTC.

Pros of PeerJS

  • Written in JavaScript, making it more accessible for web developers
  • Simpler API, abstracting away much of WebRTC's complexity
  • Extensive documentation and examples available

Cons of PeerJS

  • Less flexible and customizable compared to WebRTC
  • Limited to browser environments, unlike WebRTC which can be used in various contexts
  • May have higher latency due to additional abstraction layer

Code Comparison

PeerJS:

const peer = new Peer();
peer.on('open', (id) => {
  const conn = peer.connect('remote-peer-id');
  conn.on('open', () => {
    conn.send('Hello!');
  });
});

WebRTC:

let peer_connection = RTCPeerConnection::new()?;
let data_channel = peer_connection.create_data_channel("data")?;
data_channel.on_open(Box::new(move || {
    data_channel.send_text("Hello!").unwrap();
}));

Summary

PeerJS offers a more straightforward approach for web developers, with a focus on ease of use and browser compatibility. WebRTC, on the other hand, provides greater flexibility and can be used in various environments beyond browsers. The choice between the two depends on the specific requirements of the project and the developer's familiarity with the respective languages and ecosystems.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

WebRTC.rs

License: MIT/Apache 2.0 Discord Twitter

A pure Rust implementation of WebRTC stack. Rewrite Pion WebRTC stack in Rust

Sponsored with 💖 by

Silver Sponsors:
Stream Chat
ChannelTalk
Bronze Sponsors:
KittyCAD
AdrianEddy

Table of Content

Overview

WebRTC.rs is a pure Rust implementation of WebRTC stack, which rewrites Pion stack in Rust. This project is still in active and early development stage, please refer to the Roadmap to track the major milestones and releases. Examples provide code samples to show how to use webrtc-rs to build media and data channel applications.

Features

WebRTC
Media Interceptor Data
RTP RTCP SRTP SCTP
DTLS
mDNS STUN TURN ICE
SDP Util

WebRTC Crates Dependency Graph

WebRTC Stack

Building

Toolchain

Minimum Supported Rust Version: 1.65.0

Our minimum supported rust version(MSRV) policy is to support versions of the compiler released within the last six months. We don't eagerly bump the minimum version we support, instead the minimum will be bumped on a needed by needed basis, usually because downstream dependencies force us to.

Note: Changes to the minimum supported version are not consider breaking from a semver perspective.

Monorepo Setup

All webrtc dependent crates and examples are included in this repository at the top level in a Cargo workspace.

To build all webrtc examples:

cd examples
cargo test # build all examples (maybe very slow)
#[ or just build single example (much faster)
cargo build --example play-from-disk-vpx # build play-from-disk-vpx example only
cargo build --example play-from-disk-h264 # build play-from-disk-h264 example only
#...
#]

To build webrtc crate:

cargo build [or clippy or test or fmt]

Open Source License

Dual licensing under both MIT and Apache-2.0 is the currently accepted standard by the Rust language community and has been used for both the compiler and many public libraries since (see https://doc.rust-lang.org/1.6.0/complement-project-faq.html#why-dual-mitasl2-license). In order to match the community standards, webrtc-rs is using the dual MIT+Apache-2.0 license.

Contributing

Contributors or Pull Requests are Welcome!!!