Multi-threaded HLS Server in Rust

CK CK 3788 
Created at
Updated at  
774 0 0

For implementing a server application in Rust, you'll primarily need the tokio runtime for asynchronous programming, and a networking library like hyper (for HTTP) or tokio-tungstenite (for WebSockets), depending on your application's protocol. Additional crates might be necessary for specific functionalities such as database interaction (e.g., sqlx for SQL databases), serialization (e.g., serde for JSON), and logging (e.g., log and a logger implementation like env_logger). Finally, consider crates for handling configuration (e.g., config) and error handling (e.g., thiserror).
 

Delivering an HLS (HTTP Live Streaming) video stream using Rust can be implemented by serving the .m3u8 playlist and associated media segment files (.ts) over HTTP. Below is a simplified example demonstrating how to use Rust's multi-threading capabilities to handle multiple client requests simultaneously.

This example uses the hyper crate to create an HTTP server and spawns threads for processing requests.

 

Example: Multi-threaded HLS Server in Rust

use hyper::{service::{make_service_fn, service_fn}, Body, Request, Response, Server};
use std::fs;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use tokio::runtime::Runtime;

async fn handle_request(req: Request<Body>, hls_dir: Arc<String>) -> Result<Response<Body>, hyper::Error> {
    let path = format!("{}{}", hls_dir, req.uri().path());

    if path.ends_with(".m3u8") || path.ends_with(".ts") {
        match fs::read(&path) {
            Ok(contents) => Ok(Response::new(Body::from(contents))),
            Err(_) => Ok(Response::builder()
                .status(404)
                .body(Body::from("File not found"))
                .unwrap()),
        }
    } else {
        Ok(Response::builder()
            .status(400)
            .body(Body::from("Unsupported file type"))
            .unwrap())
    }
}

fn main() {
    // Path to the directory containing HLS files
    let hls_dir = Arc::new("./hls/".to_string());

    // Create a thread pool
    let thread_pool_size = 4;
    let mut threads = vec![];

    for _ in 0..thread_pool_size {
        let hls_dir = Arc::clone(&hls_dir);
        threads.push(thread::spawn(move || {
            let runtime = Runtime::new().unwrap();
            runtime.block_on(async {
                let make_svc = make_service_fn(move |_conn| {
                    let hls_dir = Arc::clone(&hls_dir);
                    async move {
                        Ok::<_, hyper::Error>(service_fn(move |req| {
                            handle_request(req, Arc::clone(&hls_dir))
                        }))
                    }
                });

                let addr = ([127, 0, 0, 1], 8080).into();
                let server = Server::bind(&addr).serve(make_svc);

                println!("Server running on http://{}", addr);

                if let Err(e) = server.await {
                    eprintln!("Server error: {}", e);
                }
            });
        }));
    }

    // Wait for threads to finish
    for thread in threads {
        let _ = thread.join();
    }
}

 

Steps to Run the Example

  1. Install Dependencies: Add the following dependencies to your Cargo.toml:

    [dependencies]
    hyper = "0.14"
    tokio = { version = "1", features = ["full"] }
  2. Prepare HLS Files: Place your .m3u8 and .ts files in the ./hls/ directory or update the path in the code.
  3. Run the Server: Build and run the program:

    cargo run
  4. Test the Stream: Access the HLS playlist using a media player like VLC or via a browser:

    http://127.0.0.1:8080/your_playlist.m3u8

 

Explanation

  • Multithreading: The server spawns a pool of threads, each capable of serving multiple client requests.
  • Asynchronous Handling: hyper and tokio enable asynchronous request handling, ensuring efficient processing.
  • HLS Files: The server serves .m3u8 playlists and .ts segments from the specified directory.

This setup is scalable and can handle multiple requests concurrently, making it suitable for streaming video content.

 

Others

  • In Rust, make_service_fn is a function that creates a service, which is a type that handles incoming connections in a server. It takes a MakeService as input, often configured with parameters, and returns a future that resolves to a Service ready to handle requests. This allows for asynchronous setup and configuration of the service, enabling features like connection pooling or dynamic resource allocation before handling client connections.
  • In the provided code, service_fn is a function from the hyper crate that creates a service from a closure. This closure, move |req| { handle_request(req, Arc::clone(&hls_dir)) }, defines how to handle incoming HTTP requests (req), passing each request along with a cloned Arc reference to the hls_dir to the handle_request function. Essentially, service_fn transforms a request-handling function into a hyper Service object that the server can use to process incoming connections.
  • In this code, move is a keyword that creates a closure that *moves* ownership of its captured variables into the closure's environment. Instead of borrowing the variables, the closure takes ownership, meaning the variables are moved from their original scope into the closure's scope. This is crucial for preventing data races and ensuring that the variables are accessible and valid throughout the lifetime of the spawned thread and within the asynchronous functions it executes. Without move, the compiler would likely issue errors due to borrow checker limitations within the asynchronous context.
  • In the given code, runtime.block_on(async { ... }) runs the provided asynchronous block of code (async { ... }) within the Tokio runtime. block_on essentially waits for the future returned by the async block to complete, blocking the current thread until the server either starts successfully or encounters an error, thereby ensuring the spawned thread doesn't exit prematurely before the server is fully initialized and running.
  • The code uses _conn as a placeholder for the connection object within the make_service_fn closure because the connection information isn't actually used within the function. Similarly, req is used as a parameter name in the service_fn closure to represent the incoming hyper request; it's a standard naming convention and the actual identifier isn't crucial for the functionality beyond being accessible within handle_request. In essence, the underscores indicate that these parameters are intentionally unused within their immediate scope.
  • In the Rust code, Request represents an incoming HTTP request where Body is the type of the request's body. The Body type, provided by the hyper crate, is a stream of bytes representing the request's content (e.g., the data sent with a POST request). Therefore, Request is a generic type that holds both the request's headers and its potentially streamed body.
     
Tags HLS Server HTTP Live Streaming Rust Server block_on hyper make_service_fn move service_fn spawn tokio Facebook X
Comments 0
Similar posts
  1. Basic Rust Thread Example to Perform Concurrent Computation
    618
  2. The || syntax in Rust - also known as a closure
    601
  3. HTTP Live Streaming (HLS) was developed by Apple as part of QuickTime X in iOS3.0, and became an official standard (RFC 8126) in 2017
    597
  1. How do I implement a heap in Rust?
    685
  2. How do I implement a stack in Rust?
    771
  3. Rust-GPU-tools for interacting with various GPU APIs
    757
  4. Rust array is fixed size. Can I modify its content?
    586
  5. Rust Example for Array and Dyamic Element Management (aka Vector-based Element Management)
    624
  6. Run a specific module in a external file with Rust
    571
  7. Question(?) Operator in Rust
    573
  8. Rust code example to access MySQL
    1,100
  9. Rust Documentation
    315
  10. Rust Compatibility
    726
  11. Rust Unsafe Operations
    369
  12. Rust Testing
    1,246
  13. Rust Std misc
    375
  14. Rust Std library types
    370
  15. Rust Error Handling
    350
  16. Rust Macro Rules
    1,525
  17. Rust mechanism for defining shared behavior across different types - Traits
    1,774
  18. Rust Scoping Rules - only accessible within the block of code
    1,075
  19. Rust Generics - Generalizing types and functionalities
    1,358
  20. Rust Attributes for conditional compilation, crate, lints, mark functions, macros
    318
Recently updated
  1. Google OAuth Token Expiration: Causes and Solutions
    31
  2. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    57
  3. Why SPF and DKIM Are Essential for Linux Email Servers?
    51
  4. How do I create animated background for Google Meet?
    23,228
  5. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    273
  6. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    104
  7. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    170
  8. Array of arguments passed to script in PHP
    99
  9. Invalid Auction Storms in Ad Tech
    107
  10. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    208
  11. The Two Clocks of Ad Server Architecture
    78
  12. Frequency Cap Architecture Overview
    527
  13. Collecting Proof-of-Play from 100,000 Screens
    153
  14. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    303
  15. What is RedisVL?
    116
  16. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    247
  17. TPU & GPU Cluster Management Tools
    258
  18. The Concept of Scaffolding in AI Agents
    245
  19. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    598
  20. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    269
  21. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    354
  22. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    207
  23. Machine Learning: Training vs. Ranking
    230
  24. The Continuous Learning Machine: Designing a Loop That Actually Turns
    228
  25. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    203
  26. Designing a Data Curation System Around a Vector Database
    230
  27. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    263
  28. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    674
  29. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    201
  30. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    233