Skip to content

refactor: modular architecture with multi-algorithm bitrate control and config file#6

Merged
andrescera merged 12 commits intomainfrom
refactor/extract-bitrate-control-module
Jan 10, 2026
Merged

refactor: modular architecture with multi-algorithm bitrate control and config file#6
andrescera merged 12 commits intomainfrom
refactor/extract-bitrate-control-module

Conversation

@andrescera
Copy link
Copy Markdown
Member

@andrescera andrescera commented Jan 10, 2026

Summary

Major refactoring of belacoder to introduce modular architecture, pluggable bitrate control algorithms, and INI config file support.

Changes

Project Structure Reorganization

  • All source files moved to src/ - Clean root directory
  • Updated Makefile for new structure
  • Updated .gitignore for src/*.o

Multi-Algorithm Bitrate Control (Closes #3)

  • balancer.h - Generic BalancerAlgorithm interface with init/step/cleanup
  • balancer_registry.c - Algorithm lookup by name
  • balancer_adaptive.c - Default algorithm (RTT + buffer-based)
  • balancer_fixed.c - Constant bitrate, no adaptation
  • balancer_aimd.c - TCP-style Additive Increase Multiplicative Decrease

Select algorithm at runtime:

./belacoder -a adaptive pipeline.txt host 4000  # Default
./belacoder -a fixed pipeline.txt host 4000
./belacoder -a aimd pipeline.txt host 4000

Configuration File Support

  • config.h/c - INI config file parser
  • belacoder.conf.example - Documented example config
  • -c CLI flag - Load config from file
  • SIGHUP - Reload config at runtime
[general]
min_bitrate = 500    # Kbps (500 Kbps)
max_bitrate = 6000   # Kbps (6 Mbps)
balancer = adaptive

[srt]
latency = 2000       # ms

[adaptive]
incr_step = 30       # Kbps increase step
decr_step = 100      # Kbps decrease step

[aimd]
incr_step = 50       # Kbps
decr_mult = 0.75     # Reduce to 75% on congestion

Algorithm Tuning Wired Up

  • All algorithm-specific config values now actually affect behavior
  • Adaptive: incr_step, decr_step, incr_interval, decr_interval
  • AIMD: incr_step, decr_mult, incr_interval, decr_interval

Bitrate Module Extraction (Part of #2)

  • bitrate_control.h/c - Adaptive algorithm internals
  • Pure logic, no GStreamer dependency
  • BitrateContext struct encapsulates all state
  • Testable in isolation

Packet Loss Detection

  • Added packet loss as congestion signal
  • Triggers fast decrease when loss detected

Documentation Updates

  • docs/architecture.md - Updated with new structure, modules
  • docs/bitrate-control.md - Multi-algorithm docs, updated API

New File Structure

belacoder/
├── src/
│   ├── belacoder.c           # Main application
│   ├── config.c/h            # Config file parser
│   ├── balancer.h            # Algorithm interface
│   ├── balancer_adaptive.c   # Default algorithm
│   ├── balancer_fixed.c      # Fixed bitrate
│   ├── balancer_aimd.c       # AIMD algorithm
│   ├── balancer_registry.c   # Algorithm lookup
│   └── bitrate_control.c/h   # Adaptive internals
├── pipeline/
├── docs/
├── Makefile
├── belacoder.conf.example
└── README.md

Verification

  • make clean && make - Builds with zero warnings
  • ./belacoder -v - Returns version
  • ✅ All three algorithms selectable via -a
  • ✅ Config file loads and applies correctly
  • ✅ SIGHUP reloads config at runtime
  • ✅ Documentation updated

Related Issues

- Create bitrate_control.h with BitrateContext struct and public API
- Create bitrate_control.c with algorithm implementation
- Move all bitrate-related constants to named #defines in header
- Refactor belacoder.c to use BitrateContext for state management
- Update Makefile to compile new module

This is the first step in modularizing the codebase as outlined in Issue #2.
The bitrate algorithm logic is now testable in isolation and the main file
is reduced from 915 to 785 lines.

No functional changes - algorithm behavior is identical.
- Add bitrate_control.c/h to repository structure
- Add Module Overview table showing file responsibilities
- Update Key Abstractions table with file locations
- Document BitrateContext struct and API in bitrate-control.md
- Note that module structure enables future algorithm swapping
Preserve original BELABOX attribution while adding CERALIVE as
additional copyright holder for 2026 contributions.
Introduces a pluggable architecture for bitrate control algorithms:

New files:
- balancer.h: BalancerAlgorithm interface with init/step/cleanup
- balancer_adaptive.c: Wraps existing RTT+buffer algorithm as 'adaptive'
- balancer_registry.c: Algorithm registry and lookup functions

Changes to belacoder.c:
- Add -a <algorithm> CLI flag for algorithm selection
- Use balancer interface instead of direct BitrateContext
- Print available algorithms in help text
- Default to 'adaptive' when no flag specified (preserves existing behavior)

The 'adaptive' algorithm is the default and provides identical behavior
to the previous implementation. This lays the groundwork for adding
alternative algorithms (fixed, aimd, latency_target) in Phase 2.

Addresses Issue #3: Multi-Algorithm Bitrate Control Support
New balancer algorithms:

1. 'fixed' - Constant bitrate with no adaptation
   - Outputs max_bitrate constantly
   - Useful for testing or stable networks

2. 'aimd' - Additive Increase Multiplicative Decrease
   - Classic TCP-style congestion control
   - Linear increase (+50 Kbps) when stable
   - Multiplicative decrease (75%) on congestion
   - Provides fair bandwidth sharing

Usage:
  ./belacoder -a fixed  pipeline.txt host 4000
  ./belacoder -a aimd   pipeline.txt host 4000

Both algorithms integrate with the balancer interface from Phase 1.
The 'adaptive' algorithm remains the default.

Continues work on Issue #3: Multi-Algorithm Bitrate Control Support
Enhances the adaptive balancer with packet loss as a congestion signal:

- Add pkt_loss_total and pkt_retrans_total to BalancerInput
- Track packet loss rate with EMA smoothing in BitrateContext
- Trigger heavy congestion decrease when loss_rate > 0.5 packets/interval
- Block bitrate increase while experiencing packet loss

This makes the adaptive algorithm more responsive to network issues,
especially on mobile networks where packet loss often precedes
RTT increases.

SRT stats used:
- pktSndLossTotal: Cumulative packets reported lost
- pktRetransTotal: Cumulative packets retransmitted
- Add config.h/config.c for INI-style config parser
- Support -c <config> CLI option
- Config file uses Kbps units (500 = 500 Kbps, 6000 = 6 Mbps)
- SIGHUP reloads full config (not just bitrate file)
- CLI flags override config values (-a overrides balancer=)
- Include belacoder.conf.example with all options documented
- Update .gitignore to ignore all .o files
- Add Kbps unit examples (500 = 500 Kbps, 6000 = 6 Mbps)
- Document balancer algorithm options
- Explain SRT latency purpose and typical values
- Mark algorithm tuning sections as future/not yet implemented
- Comment out unused sections to avoid confusion
- stream_id was parsed from config but never applied
- Now g_config.stream_id is used if set and -s flag not provided
- CLI -s flag still overrides config value
stream_id rarely changes, keep it as -s flag only
- Add adaptive/aimd tuning params to BalancerConfig
- Pass config values from belacoder.c to each algorithm
- Adaptive: configurable incr_step, decr_step, intervals
- AIMD: configurable incr_step, decr_mult, intervals
- Update bitrate_context_init to accept tuning params
- Use ctx fields instead of hardcoded defines in bitrate_update
- Update documentation (architecture.md, bitrate-control.md)
- Config file [adaptive] and [aimd] sections now functional
- Move all .c and .h files to src/
- Update Makefile for new structure
- Update documentation (architecture.md, bitrate-control.md)
- Cleaner root directory with only config files and docs
@andrescera andrescera changed the title refactor: extract bitrate control into separate module refactor: modular architecture with multi-algorithm bitrate control and config file Jan 10, 2026
@andrescera andrescera merged commit db88c4c into main Jan 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Multi-Algorithm Bitrate Control Support

1 participant