Skip to content

Future Improvements: Code Structure and Testing #2

@andrescera

Description

@andrescera

Future Improvements: Code Structure and Testing

Overview

This issue tracks structural improvements and testing additions planned for belacoder.

Refactoring Tasks

1. Consolidate Global Variables into Context Struct

Currently ~15 global variables are used for runtime state. Consolidate into a single context struct:

typedef struct {
    GstPipeline *pipeline;
    GMainLoop *loop;
    GstElement *encoder;
    GstElement *overlay;
    SRTSOCKET sock;
    int quit;
    int cur_bitrate;
    int min_bitrate;
    int max_bitrate;
    // ... etc
} BelacoderContext;

Benefits:

  • Easier to test (can create isolated contexts)
  • Clearer ownership and lifecycle
  • Enables future multi-instance support

2. Split into Modules ✅

Break belacoder.c into logical modules:

  • bitrate_control.c / bitrate_control.h - Adaptive bitrate algorithm
  • srt_sender.c / srt_sender.h - SRT socket handling and packet sending
  • gst_helpers.c / gst_helpers.h - GStreamer pipeline utilities
  • main.c - CLI parsing, initialization, main loop

Completed in PR #7 - Reorganized into src/core/, src/io/, src/net/, src/gst/ with clear module boundaries.

Benefits:

3. Implement Controller Interface for Multi-Algorithm Support ✅

Add runtime selection of bitrate algorithms (see #3):

typedef struct {
    const char *name;
    void* (*init)(BalancerConfig *config);
    BalancerOutput (*step)(void *state, BalancerInput *input);
    void (*cleanup)(void *state);
} BalancerAlgorithm;
  • Define controller interface
  • Refactor current algorithm to use interface
  • Add --balancer=<name> CLI flag
  • Implement at least one alternative (e.g., fixed or aimd)

Completed in PR #7 - Implemented adaptive, fixed, and aimd algorithms with pluggable interface.

Testing Tasks

4. Add Unit Tests for Bitrate Controller ✅

The bitrate controller logic is pure computation and can be tested in isolation:

  • Create test harness that feeds synthetic SRT stats
  • Test emergency drop to min_bitrate
  • Test heavy/light congestion responses
  • Test stable condition increase
  • Test threshold calculations

Completed in PR #7 - 16 cmocka integration tests covering all balancer algorithms and edge cases.

5. Add Integration Test

  • Test connecting to a local SRT listener
  • Verify stream is received correctly
  • Test connection failure handling

6. Add Static Analysis to CI

  • Add cppcheck or clang-tidy to build
  • Fix any warnings found

Related Issues

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