Skip to content

Refactor: Modular architecture with pluggable balancers and config support#7

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

Refactor: Modular architecture with pluggable balancers and config support#7
andrescera merged 13 commits intomainfrom
refactor/extract-bitrate-control-module

Conversation

@andrescera
Copy link
Copy Markdown
Member

Summary

This PR refactors belacoder into a modular, testable architecture while adding significant new features:

New Features

  • Pluggable balancer algorithms: adaptive (default), fixed, and AIMD (TCP-style)
  • Configuration file support: INI-format config with runtime reload via SIGHUP
  • Packet loss detection: Adaptive algorithm now responds to packet loss
  • Integration tests: 16 cmocka tests covering balancer logic and edge cases

Architecture Changes

  • Reorganized src/ into subdirectories:
    • core/ - config, balancer algorithms, runner
    • io/ - CLI options, pipeline loader
    • net/ - SRT client
    • gst/ - encoder control, overlay UI
  • Extracted focused modules with clear interfaces
  • Added tests/ directory with cmocka integration tests

Module Overview

Module Location Purpose
CLI Options src/io/cli_options.c/h Command-line parsing
Config src/core/config.c/h INI config + SIGHUP reload
SRT Client src/net/srt_client.c/h SRT connection management
Encoder Control src/gst/encoder_control.c/h Bitrate updates
Balancer Runner src/core/balancer_runner.c/h Algorithm orchestration

Test Coverage

  • Balancer algorithms (adaptive, fixed, AIMD)
  • Congestion response and recovery
  • Bounds enforcement (min/max bitrate)
  • Edge cases (min==max, rapid network changes)
  • Config parsing and defaults

Test plan

  • make builds without warnings
  • make test passes all 16 tests
  • Manual testing with actual SRT stream
  • Verify config reload with kill -HUP

- 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
- Reorganize src/ into core/, io/, net/, gst/ subdirectories
- Extract modules: cli_options, pipeline_loader, srt_client,
  encoder_control, overlay_ui, balancer_runner
- Add cmocka integration tests (16 tests covering balancer
  algorithms, config handling, bounds, edge cases)
- Update Makefile with new paths and test targets
- Update documentation with new structure

The modular structure improves maintainability and testability
while preserving all existing functionality.
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.

1 participant