• Stars
    star
    173
  • Rank 220,124 (Top 5 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 2 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

LiveKit real-time and server SDKs for Rust
The LiveKit icon, the name of the repository and some sample code in the background.

πŸ“ΉπŸŽ™οΈπŸ¦€ Rust Client SDK for LiveKit

Use this SDK to add real-time video, audio and data features to your Rust app. By connecting to a self- or cloud-hosted LiveKit server, you can quickly build applications like interactive live streaming or video calls with just a few lines of code.

crates.io livekit docs.rs Builds Tests

Features

  • Receiving tracks
  • Publishing tracks
  • Data channels
  • Simulcast
  • SVC codecs (AV1/VP9)
  • Adaptive Streaming
  • Dynacast
  • Hardware video enc/dec
    • VideoToolbox for MacOS/iOS
  • Supported Platforms
    • Windows
    • MacOS
    • Linux
    • iOS
    • Android

Crates

  • livekit-api: Server APIs and auth token generation
  • livekit: LiveKit real-time SDK
  • livekit-ffi: Internal crate, used to generate bindings for other languages

When adding the SDK as a dependency to your project, make sure to add the necessary rustflags to your cargo config, otherwise linking may fail.

Also, please refer to the list of the supported platform toolkits.

Getting started

Currently, Tokio is required to use this SDK, however we plan to make the async executor runtime agnostic.

Using Server API

Generating an access token

use livekit_api::access_token;
use std::env;

fn create_token() -> Result<String, access_token::AccessTokenError> {
    let api_key = env::var("LIVEKIT_API_KEY").expect("LIVEKIT_API_KEY is not set");
    let api_secret = env::var("LIVEKIT_API_SECRET").expect("LIVEKIT_API_SECRET is not set");

    let token = access_token::AccessToken::with_api_key(&api_key, &api_secret)
        .with_identity("rust-bot")
        .with_name("Rust Bot")
        .with_grants(access_token::VideoGrants {
             room_join: true,
             room: "my-room".to_string(),
             ..Default::default()
        })
        .to_jwt();
    return token
}

Creating a room with RoomService API

use livekit_api::services::room::{CreateRoomOptions, RoomClient};

#[tokio::main]
async fn main() {
    let room_service = RoomClient::new("http://localhost:7880").unwrap();

    let room = room_service
        .create_room("my_room", CreateRoomOptions::default())
        .await
        .unwrap();

    println!("Created room: {:?}", room);
}

Using Real-time SDK

Connect to a Room and listen for events:

use livekit::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
    let (room, mut room_events) = Room::connect(&url, &token).await?;

    while let Some(event) = room_events.recv().await {
        match event {
            RoomEvent::TrackSubscribed { track, publication, participant } => {
                // ...
            }
            _ => {}
        }
    }

    Ok(())
}

Receive video frames of a subscribed track

...
use futures::StreamExt; // this trait is required for iterating on audio & video frames
use livekit::prelude::*;

match event {
    RoomEvent::TrackSubscribed { track, publication, participant } => {
        match track {
            RemoteTrack::Audio(audio_track) => {
                let rtc_track = audio_track.rtc_track();
                let mut audio_stream = NativeAudioStream::new(rtc_track);
                tokio::spawn(async move {
                    // Receive the audio frames in a new task 
                    while let Some(audio_frame) = audio_stream.next().await {
                        log::info!("received audio frame - {audio_frame:#?}");
                    }
                });
            },
            RemoteTrack::Video(video_track) => {
                let rtc_track = video_track.rtc_track();
                let mut video_stream = NativeVideoStream::new(rtc_track);
                tokio::spawn(async move {
                    // Receive the video frames in a new task 
                    while let Some(video_frame) = video_stream.next().await {
                        log::info!("received video frame - {video_frame:#?}");
                    }
                });
            },
        }
    },
    _ => {}
}

Examples

  • basic room: simple example connecting to a room.
  • wgpu_room: complete example app with video rendering using wgpu and egui.
  • mobile: mobile app targeting iOS and Android
  • play_from_disk: publish audio from a wav file
  • save_to_disk: save received audio to a wav file

Motivation and Design Goals

LiveKit aims to provide an open source, end-to-end WebRTC stack that works everywhere. We have two goals in mind with this SDK:

  1. Build a standalone, cross-platform LiveKit client SDK for Rustaceans.
  2. Build a common core for other platform-specific SDKs (e.g. Unity, Unreal, iOS, Android)

Regarding (2), we've already developed a number of client SDKs for several platforms and encountered a few challenges in the process:

  • There's a significant amount of business/control logic in our signaling protocol and WebRTC. Currently, this logic needs to be implemented in every new platform we support.
  • Interactions with media devices and encoding/decoding are specific to each platform and framework.
  • For multi-platform frameworks (e.g. Unity, Flutter, React Native), the aforementioned tasks proved to be extremely painful.

Thus, we posited a Rust SDK, something we wanted build anyway, encapsulating all our business logic and platform-specific APIs into a clean set of abstractions, could also serve as the foundation for our other SDKs!

We'll first use it as a basis for our Unity SDK (under development), but over time, it will power our other SDKs, as well.


LiveKit Ecosystem
Real-time SDKsReact Components Β· JavaScript Β· iOS/macOS Β· Android Β· Flutter Β· React Native Β· Rust Β· Python Β· Unity (web) Β· Unity (beta)
Server APIsNode.js Β· Golang Β· Ruby Β· Java/Kotlin Β· Python Β· Rust Β· PHP (community)
Agents FrameworksPython Β· Playground
ServicesLivekit server Β· Egress Β· Ingress Β· SIP
ResourcesDocs Β· Example apps Β· Cloud Β· Self-hosting Β· CLI

More Repositories

1

livekit

End-to-end stack for WebRTC. SFU media server and SDKs.
Go
9,214
star
2

agents

Build real-time multimodal AI applications πŸ€–πŸŽ™οΈπŸ“Ή
Python
750
star
3

client-sdk-js

LiveKit browser client SDK (javascript)
TypeScript
329
star
4

client-sdk-flutter

Flutter Client SDK for LiveKit
Dart
238
star
5

livekit-cli

Command line interface to LiveKit
Go
199
star
6

server-sdk-go

Client and server SDK for Golang
Go
189
star
7

client-sdk-swift

LiveKit Swift Client SDK. Easily build live audio or video experiences into your mobile app, game or website.
Swift
182
star
8

client-sdk-android

LiveKit SDK for Android
Kotlin
169
star
9

livekit-react

React component and library for LiveKit
TypeScript
169
star
10

egress

Export and record WebRTC sessions and tracks
Go
166
star
11

components-js

Official open source React components and examples for building with LiveKit.
TypeScript
145
star
12

node-sdks

LiveKit real-time and server SDKs for Node.JS
TypeScript
124
star
13

client-sdk-react-native

TypeScript
98
star
14

python-sdks

LiveKit real-time and server SDKs for Python
Python
80
star
15

sip

SIP to WebRTC bridge for LiveKit
Go
76
star
16

protocol

LiveKit protocol. Protobuf definitions for LiveKit's signaling protocol
Go
69
star
17

ingress

Ingest streams (RTMP/WHIP) or files (HLS, MP4) to LiveKit WebRTC
Go
68
star
18

agents-playground

TypeScript
61
star
19

client-sdk-unity-web

Client SDK for Unity WebGL
C#
50
star
20

livekit-helm

LiveKit Helm charts
Smarty
47
star
21

livekit-recorder

Go
32
star
22

client-sdk-unity

C#
31
star
23

track-processors-js

TypeScript
29
star
24

server-sdk-kotlin

Kotlin
28
star
25

livekit-server-sdk-python

LiveKit Server SDK for Python
Python
25
star
26

psrpc

Go
22
star
27

client-example-swift

Example app for LiveKit Swift SDK πŸ‘‰ https://github.com/livekit/client-sdk-swift
Swift
21
star
28

server-sdk-ruby

LiveKit Server SDK for Ruby
Ruby
21
star
29

meet

Open source video conferencing app built on LiveKit Components, LiveKit Cloud, and Next.js.
TypeScript
16
star
30

client-unity-demo

Demo for LiveKit Unity SDK
C#
11
star
31

client-sdk-cpp

C++
11
star
32

WebRTC-swift

Swift package for WebRTC
Objective-C
10
star
33

livekit-docs

JavaScript
10
star
34

deploy

Resources for deploying LiveKit
Go
9
star
35

webrtc-vmaf

VMAF benchmarking tool for WebRTC codecs
Python
9
star
36

chrometester

A livekit tester to simulate a subscriber in a room, uses headless Chromium
JavaScript
8
star
37

gstreamer-publisher

Command-line app that publishes any GStreamer pipeline to LiveKit
Go
8
star
38

nats-test

benchmarking app that emulates our usage patterns
Go
7
star
39

mediatransportutil

Media transport utilities
Go
6
star
40

rtcscore-go

Library to calculate Mean Opinion Score(MOS)
Go
4
star
41

components-swift

Swift
4
star
42

signal-proxy

Go
3
star
43

client-example-collection-swift

A collection of small examples for the LiveKit Swift SDK πŸ‘‰ https://github.com/livekit/client-sdk-swift
Swift
3
star
44

webrtc-chrome

Fork of Google's WebRTC repo, with LiveKit patches.
C++
3
star
45

components-android

Kotlin
3
star
46

gst-plugins

Go
3
star
47

client-sdk-react-native-expo-plugin

TypeScript
2
star
48

webrtc-xcframework

Ruby
2
star
49

mageutil

Go
2
star
50

ios-test-apps

Swift
1
star
51

gstreamer

C
1
star
52

websocket-bridge

Send and Receive Media to LiveKit via WebSocket
Go
1
star