Skip to content

Rust Migration: Feasibility Analysis and Roadmap #4

@andrescera

Description

@andrescera

Overview

This issue tracks the potential migration of belacoder from C to Rust.

Current State

belacoder is a ~800-line C application that:

  1. Parses CLI arguments and a GStreamer pipeline file
  2. Constructs and runs a GStreamer pipeline
  3. Pulls samples from an appsink and sends them over SRT
  4. Polls SRT statistics and adjusts encoder bitrate
  5. Handles signals for graceful shutdown and config reload

Why Rust?

  1. Memory safety - Eliminates buffer overflows, use-after-free at compile time
  2. Better error handling - Result<T, E> forces explicit error handling
  3. No undefined behavior - Safer signal handling
  4. GStreamer bindings are mature - gstreamer-rs is well-maintained
  5. No GC latency - Critical for real-time video
  6. Performance parity with C - Zero-cost abstractions

Key Dependencies

Dependency C Binding Rust Binding
GStreamer Native gstreamer-rs (mature)
GLib Native glib-rs (mature)
libsrt Native srt-rs (exists, less active)
POSIX signals Native signal-hook (mature)

Risks

  1. SRT bindings maturity - srt-rs less actively maintained than libsrt
  2. Build complexity - Cross-compilation for Jetson ARM needs setup
  3. Team familiarity - Rust learning curve

Incremental Migration Path

Phase 1: Preparation (C) ✅

  • Fix signal handling safety
  • Add proper error handling
  • Clean up resource leaks
  • Refactor into modules with clear interfaces
  • Add integration tests

Phase 2: Bitrate Controller in Rust

  • Create Rust crate for bitrate controller (no GStreamer dependency)
  • Expose via C FFI
  • Integrate into C main
  • Test in production

Phase 3: SRT Sender in Rust

  • Evaluate srt-rs bindings
  • Implement SRT sender module
  • Wire up appsink callback to call Rust
  • Test reliability and performance

Phase 4: Full Port

  • GStreamer pipeline construction in Rust
  • CLI parsing with clap
  • Signal handling with signal-hook
  • Deprecate C version

Recommended Rust Crates

[dependencies]
gstreamer = "0.22"
gstreamer-app = "0.22"
glib = "0.19"
clap = "4"
anyhow = "1"
signal-hook = "0.3"
log = "0.4"

Effort Estimate

Component Complexity Estimate
CLI parsing Low 2-4 hours
Pipeline construction Medium 1-2 days
Appsink + SRT send Medium 1-2 days
Bitrate controller Low 4-8 hours
Signal handling Low 2-4 hours
Testing High 2-3 days
Total 1-2 weeks

Recommendation

Short-term (0-6 months): Stay with C, focus on code quality and tests

Medium-term (6-12 months): Start with bitrate controller in Rust (self-contained, easy to test)

Long-term: Full port only if incremental modules succeed

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions