sipsorcery-org / SIPSorceryMedia.FFmpeg

GNU Lesser General Public License v2.1
31 stars 24 forks source link

Add dynamic encoding bitrate capability #62

Closed ha-ves closed 11 months ago

ha-ves commented 11 months ago

This will allow dynamic bitrate changes throughout the runtime by re-initializing the encoder with specified bitrate options values.

resolves #46

ChristopheI commented 11 months ago

Thanks a lot for this ! Could you give a short snippet ? With some comments about values to set in SetVideoEncoderBitrate(..)

ha-ves commented 11 months ago

Sure, though for now, I only actually understand the average bitrate, which in ffmpeg cli will use -b:v to set the video bitrate.

The other parameters are for advanced rate control which I have yet to understand. Setting them doesn't seem to have any effect for now.

// Find the desired camera
var cam = FFmpegCameraManager.GetCameraDevices()!.First(cam => cam.Name == "Integrated Camera");
// Create a ffmpeg source using the camera path
var ffmpegsrc = new FFmpegCameraSource(cam.Path);

// Add it to the mediastreams and other stuff
...

// Start the source
ffmpegsrc.StartVideo();

...

During runtime,

// On bitrate control slider value changed
bitrateCtrl.ValueChanged += (sender, e) =>
{
    // here e.NewValue is of type double, and in thousands of bits/second (Kbps)

    logger.LogInformation("new bitrate: " + (long)e.NewValue + "K");

    // set only the average bitrate of the encoder, It has the same effect as the `-b:v` option in ffmpeg cli
    // NOTE: If you set it too low, it will only encode into a grayscale/looks corrupted video.
    ffmpegsrc.SetVideoEncoderBitrate(avgBitrate: (long?)(e.NewValue * 1000));
};