refactor: modular architecture with multi-algorithm bitrate control and config file#6
Merged
andrescera merged 12 commits intomainfrom Jan 10, 2026
Merged
Conversation
- 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
This was referenced Jan 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Major refactoring of belacoder to introduce modular architecture, pluggable bitrate control algorithms, and INI config file support.
Changes
Project Structure Reorganization
src/- Clean root directory.gitignoreforsrc/*.oMulti-Algorithm Bitrate Control (Closes #3)
balancer.h- GenericBalancerAlgorithminterface with init/step/cleanupbalancer_registry.c- Algorithm lookup by namebalancer_adaptive.c- Default algorithm (RTT + buffer-based)balancer_fixed.c- Constant bitrate, no adaptationbalancer_aimd.c- TCP-style Additive Increase Multiplicative DecreaseSelect algorithm at runtime:
./belacoder -a adaptive pipeline.txt host 4000 # Default ./belacoder -a fixed pipeline.txt host 4000 ./belacoder -a aimd pipeline.txt host 4000Configuration File Support
config.h/c- INI config file parserbelacoder.conf.example- Documented example config-cCLI flag - Load config from fileAlgorithm Tuning Wired Up
incr_step,decr_step,incr_interval,decr_intervalincr_step,decr_mult,incr_interval,decr_intervalBitrate Module Extraction (Part of #2)
bitrate_control.h/c- Adaptive algorithm internalsBitrateContextstruct encapsulates all statePacket Loss Detection
Documentation Updates
docs/architecture.md- Updated with new structure, modulesdocs/bitrate-control.md- Multi-algorithm docs, updated APINew File Structure
Verification
make clean && make- Builds with zero warnings./belacoder -v- Returns version-aRelated Issues