Adapt Muninn bitrate to receiver pressure
This commit is contained in:
parent
08cbc8cc3d
commit
0a414c4e81
4 changed files with 212 additions and 16 deletions
|
|
@ -2033,6 +2033,10 @@ fn run_rudp_mux_once(
|
|||
let mut payloads_send_expired = 0_u64;
|
||||
let mut receiver_feedback = MuninnRudpReceiverFeedbackStats::default();
|
||||
let mut handled_keyframe_requests = 0_u64;
|
||||
let mut video_bitrate_controller = MuninnVideoBitrateController::new(
|
||||
media_profile.video_bitrate_kbps,
|
||||
Instant::now(),
|
||||
);
|
||||
let mut repair_cache =
|
||||
RecentVideoChunkRepairCache::new(MUNINN_RUDP_MEDIA_REPAIR_CACHE_CHUNKS);
|
||||
let mut repair_budget = MuninnRudpRepairBudget::new(
|
||||
|
|
@ -2075,12 +2079,14 @@ fn run_rudp_mux_once(
|
|||
&mut video_send_pacer,
|
||||
payloads_dropped,
|
||||
)?;
|
||||
if record_receiver_keyframe_pressure(
|
||||
apply_video_encoder_feedback_control(
|
||||
&receiver_feedback,
|
||||
&mut handled_keyframe_requests,
|
||||
) {
|
||||
request_video_encoder_idr(video_encoder_control.as_mut())?;
|
||||
}
|
||||
&mut video_bitrate_controller,
|
||||
video_encoder_control.as_mut(),
|
||||
payloads_dropped,
|
||||
media_profile.receiver_assembly_deadline_ms,
|
||||
)?;
|
||||
poll_rudp_resends_with_backpressure(&mut video_transport)?;
|
||||
poll_rudp_resends_with_backpressure(&mut audio_transport)?;
|
||||
republish_running_stream_if_due(
|
||||
|
|
@ -2151,12 +2157,14 @@ fn run_rudp_mux_once(
|
|||
&mut video_send_pacer,
|
||||
payloads_dropped,
|
||||
)?;
|
||||
if record_receiver_keyframe_pressure(
|
||||
apply_video_encoder_feedback_control(
|
||||
&receiver_feedback,
|
||||
&mut handled_keyframe_requests,
|
||||
) {
|
||||
request_video_encoder_idr(video_encoder_control.as_mut())?;
|
||||
}
|
||||
&mut video_bitrate_controller,
|
||||
video_encoder_control.as_mut(),
|
||||
payloads_dropped,
|
||||
media_profile.receiver_assembly_deadline_ms,
|
||||
)?;
|
||||
poll_rudp_resends_with_backpressure(&mut video_transport)?;
|
||||
poll_rudp_resends_with_backpressure(&mut audio_transport)?;
|
||||
republish_running_stream_if_due(
|
||||
|
|
@ -2223,12 +2231,14 @@ fn run_rudp_mux_once(
|
|||
&mut video_send_pacer,
|
||||
payloads_dropped,
|
||||
)?;
|
||||
if record_receiver_keyframe_pressure(
|
||||
apply_video_encoder_feedback_control(
|
||||
&receiver_feedback,
|
||||
&mut handled_keyframe_requests,
|
||||
) {
|
||||
request_video_encoder_idr(video_encoder_control.as_mut())?;
|
||||
}
|
||||
&mut video_bitrate_controller,
|
||||
video_encoder_control.as_mut(),
|
||||
payloads_dropped,
|
||||
media_profile.receiver_assembly_deadline_ms,
|
||||
)?;
|
||||
poll_rudp_resends_with_backpressure(&mut video_transport)?;
|
||||
poll_rudp_resends_with_backpressure(&mut audio_transport)?;
|
||||
republish_running_stream_if_due(
|
||||
|
|
@ -2335,6 +2345,78 @@ struct MuninnRudpReceiverFeedbackStats {
|
|||
deferred_repair_chunks: u64,
|
||||
repair_chunks_per_second: usize,
|
||||
highest_decodable_frame_id: Option<u64>,
|
||||
latest_jitter_us: u64,
|
||||
latest_decode_queue_us: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct MuninnVideoBitrateController {
|
||||
current_kbps: u32,
|
||||
min_kbps: u32,
|
||||
max_kbps: u32,
|
||||
last_late_frames: u64,
|
||||
last_keyframes: u64,
|
||||
last_deferred_repairs: u64,
|
||||
last_queue_dropped: u64,
|
||||
last_sample_at: Instant,
|
||||
stable_since: Instant,
|
||||
}
|
||||
|
||||
impl MuninnVideoBitrateController {
|
||||
fn new(max_kbps: u32, now: Instant) -> Self {
|
||||
let max_kbps = max_kbps.max(1_000);
|
||||
Self {
|
||||
current_kbps: max_kbps,
|
||||
min_kbps: (max_kbps / 4).max(1_000),
|
||||
max_kbps,
|
||||
last_late_frames: 0,
|
||||
last_keyframes: 0,
|
||||
last_deferred_repairs: 0,
|
||||
last_queue_dropped: 0,
|
||||
last_sample_at: now,
|
||||
stable_since: now,
|
||||
}
|
||||
}
|
||||
|
||||
fn observe(
|
||||
&mut self,
|
||||
feedback: &MuninnRudpReceiverFeedbackStats,
|
||||
queue_dropped: u64,
|
||||
deadline_us: u64,
|
||||
now: Instant,
|
||||
) -> Option<u32> {
|
||||
if now.duration_since(self.last_sample_at) < Duration::from_millis(500) {
|
||||
return None;
|
||||
}
|
||||
self.last_sample_at = now;
|
||||
let receiver_pressure = feedback
|
||||
.latest_jitter_us
|
||||
.saturating_add(feedback.latest_decode_queue_us)
|
||||
>= deadline_us.saturating_mul(3) / 4;
|
||||
let damaged = feedback.late_frames > self.last_late_frames
|
||||
|| feedback.requested_keyframes > self.last_keyframes
|
||||
|| feedback.deferred_repair_chunks > self.last_deferred_repairs
|
||||
|| queue_dropped > self.last_queue_dropped
|
||||
|| receiver_pressure;
|
||||
self.last_late_frames = feedback.late_frames;
|
||||
self.last_keyframes = feedback.requested_keyframes;
|
||||
self.last_deferred_repairs = feedback.deferred_repair_chunks;
|
||||
self.last_queue_dropped = queue_dropped;
|
||||
|
||||
let previous = self.current_kbps;
|
||||
if damaged {
|
||||
self.current_kbps = (self.current_kbps.saturating_mul(85) / 100)
|
||||
.max(self.min_kbps);
|
||||
self.stable_since = now;
|
||||
} else if now.duration_since(self.stable_since) >= Duration::from_secs(2) {
|
||||
self.current_kbps = self
|
||||
.current_kbps
|
||||
.saturating_add((self.max_kbps / 20).max(250))
|
||||
.min(self.max_kbps);
|
||||
self.stable_since = now;
|
||||
}
|
||||
(self.current_kbps != previous).then_some(self.current_kbps)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -2669,6 +2751,40 @@ fn request_video_encoder_idr<W: std::io::Write + ?Sized>(control: Option<&mut W>
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn request_video_encoder_bitrate<W: std::io::Write + ?Sized>(
|
||||
control: Option<&mut W>,
|
||||
bitrate_kbps: u32,
|
||||
) -> Result<()> {
|
||||
if let Some(control) = control {
|
||||
writeln!(control, "BITRATE {bitrate_kbps}")?;
|
||||
control.flush()?;
|
||||
eprintln!("Muninn adjusted live NVENC bitrate to {bitrate_kbps} kbps.");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn apply_video_encoder_feedback_control(
|
||||
feedback: &MuninnRudpReceiverFeedbackStats,
|
||||
handled_keyframe_requests: &mut u64,
|
||||
bitrate: &mut MuninnVideoBitrateController,
|
||||
mut control: Option<&mut std::process::ChildStdin>,
|
||||
queue_dropped: u64,
|
||||
deadline_ms: u64,
|
||||
) -> Result<()> {
|
||||
if record_receiver_keyframe_pressure(feedback, handled_keyframe_requests) {
|
||||
request_video_encoder_idr(control.as_deref_mut())?;
|
||||
}
|
||||
if let Some(kbps) = bitrate.observe(
|
||||
feedback,
|
||||
queue_dropped,
|
||||
deadline_ms.saturating_mul(1_000),
|
||||
Instant::now(),
|
||||
) {
|
||||
request_video_encoder_bitrate(control.as_deref_mut(), kbps)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn rudp_media_progress_detail(
|
||||
sent: u64,
|
||||
queue_dropped: u64,
|
||||
|
|
@ -2765,6 +2881,8 @@ fn record_rudp_media_receiver_feedback(
|
|||
stats.missing_video_chunks = stats
|
||||
.missing_video_chunks
|
||||
.saturating_add(feedback.missing_video_chunk_keys.len() as u64);
|
||||
stats.latest_jitter_us = feedback.jitter_us.max(0) as u64;
|
||||
stats.latest_decode_queue_us = feedback.decode_queue_us.max(0) as u64;
|
||||
if let Some(frame_id) = feedback.highest_decodable_frame_id {
|
||||
stats.highest_decodable_frame_id = Some(
|
||||
stats
|
||||
|
|
@ -11957,6 +12075,7 @@ mod tests {
|
|||
deferred_repair_chunks: 5,
|
||||
repair_chunks_per_second: 64,
|
||||
highest_decodable_frame_id: Some(88),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
|
|
@ -12034,6 +12153,48 @@ mod tests {
|
|||
assert_eq!(commands, b"IDR\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn video_bitrate_controller_backs_off_fast_and_recovers_slowly() {
|
||||
let start = Instant::now();
|
||||
let mut controller = MuninnVideoBitrateController::new(12_000, start);
|
||||
let mut feedback = MuninnRudpReceiverFeedbackStats::default();
|
||||
feedback.late_frames = 1;
|
||||
assert_eq!(
|
||||
controller.observe(&feedback, 0, 100_000, start + Duration::from_millis(500)),
|
||||
Some(10_200)
|
||||
);
|
||||
assert_eq!(
|
||||
controller.observe(&feedback, 0, 100_000, start + Duration::from_secs(1)),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
controller.observe(&feedback, 0, 100_000, start + Duration::from_millis(2500)),
|
||||
Some(10_800)
|
||||
);
|
||||
assert_eq!(
|
||||
controller.observe(&feedback, 0, 100_000, start + Duration::from_secs(3)),
|
||||
None
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn receiver_decode_pressure_drives_bitrate_and_command_shape() {
|
||||
let start = Instant::now();
|
||||
let mut controller = MuninnVideoBitrateController::new(16_000, start);
|
||||
let feedback = MuninnRudpReceiverFeedbackStats {
|
||||
latest_jitter_us: 30_000,
|
||||
latest_decode_queue_us: 50_000,
|
||||
..Default::default()
|
||||
};
|
||||
let adjusted = controller
|
||||
.observe(&feedback, 0, 100_000, start + Duration::from_millis(500))
|
||||
.unwrap();
|
||||
assert_eq!(adjusted, 13_600);
|
||||
let mut commands = Vec::new();
|
||||
request_video_encoder_bitrate(Some(&mut commands), adjusted).unwrap();
|
||||
assert_eq!(commands, b"BITRATE 13600\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rudp_media_receiver_feedback_updates_sender_pressure_stats() {
|
||||
let feedback = crate::media_packetizer::build_receiver_feedback(
|
||||
|
|
|
|||
|
|
@ -99,10 +99,11 @@ The machine is reliable when each class loses only what its contract permits.
|
|||
- Recoverable missing video chunks remain repair/FEC damage. Only explicit or
|
||||
dependency-derived decode-chain invalidation requests a keyframe.
|
||||
|
||||
Still open: adaptive bitrate/parity from receiver pressure, installing the
|
||||
controllable encoder bundle on each capture body, and field acceptance runs
|
||||
through the impairment harness. The scheduled IDR every quarter second remains
|
||||
a fallback ceiling.
|
||||
Still open: installing the controllable encoder bundle on each capture body and
|
||||
field acceptance runs through the impairment harness. The scheduled IDR every
|
||||
quarter second remains a fallback ceiling. Video parity is already fixed at up
|
||||
to 16 interleaved stripes per frame; field evidence must decide whether making
|
||||
that overhead adaptive would improve the visible timeline or merely add churn.
|
||||
|
||||
Receiver audit correction on 2026-07-16: the native Mimir/OBS receiver already
|
||||
contains production XOR parity reconstruction. Its early repair feedback was
|
||||
|
|
@ -139,6 +140,16 @@ received `IDR` over stdin and emitted a second cleanly decoded IDR at frame 22.
|
|||
The synthetic 640x360 verification emitted IDRs at frames 0 and 10 and decoded
|
||||
without errors. The quarter-second scheduled GOP stays as independent fallback.
|
||||
|
||||
Receiver pressure also controls the live NVENC rate without restarting the
|
||||
session. Muninn samples at 500 ms, backs bitrate off multiplicatively to 85%
|
||||
when late frames, keyframe recovery, deferred repairs, sender queue drops, or
|
||||
75%-of-deadline jitter/decode pressure appear, and adds only 5% of the configured
|
||||
ceiling after each two stable seconds. The floor is one quarter of the requested
|
||||
rate or 1 Mbps. `BITRATE <kbps>` changes `AVCodecContext` average/max bitrate and
|
||||
two-frame VBV; FFmpeg's NVENC backend reconfigures the existing encoder and
|
||||
forces an IDR. A live 2560x1440 D3D11 run changed 12 Mbps to 6 Mbps at frame 35,
|
||||
emitted the reconfiguration IDR at that same frame, and decoded cleanly.
|
||||
|
||||
Disconnected input edge admission is now structurally bounded. Muninn retains
|
||||
at most 256 unacknowledged button edges per controller. Crossing that boundary
|
||||
rotates the source epoch, discards obsolete transition history, and rebases on
|
||||
|
|
|
|||
|
|
@ -7,6 +7,9 @@ one command per line:
|
|||
- `IDR` marks the next submitted frame `AV_PICTURE_TYPE_I`. With NVENC
|
||||
`forced-idr=1`, FFmpeg lowers that request to an IDR rather than an ordinary
|
||||
intra frame.
|
||||
- `BITRATE <kbps>` reconfigures average/max bitrate and the two-frame VBV on
|
||||
the next submitted frame. FFmpeg's NVENC backend performs an in-session
|
||||
`nvEncReconfigureEncoder` and forces an IDR for the rate transition.
|
||||
- `QUIT` ends capture cleanly.
|
||||
|
||||
The process does not own transport, audio, packetization, deadlines, or repair.
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#include <atomic>
|
||||
#include <charconv>
|
||||
#include <cerrno>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
|
|
@ -153,11 +154,20 @@ int main(int argc, char **argv)
|
|||
if (!input_packet || !output_packet || !frame) fail("allocating frame pipeline");
|
||||
|
||||
std::atomic_bool force_next_idr{false};
|
||||
std::atomic_uint64_t requested_bitrate_kbps{0};
|
||||
std::atomic_bool quit{false};
|
||||
std::thread command_reader([&] {
|
||||
std::string command;
|
||||
while (std::getline(std::cin, command)) {
|
||||
if (command == "IDR") force_next_idr.store(true, std::memory_order_release);
|
||||
else if (command.starts_with("BITRATE ")) {
|
||||
std::uint64_t bitrate = 0;
|
||||
const auto value = std::string_view(command).substr(8);
|
||||
const auto parsed = std::from_chars(value.data(), value.data() + value.size(), bitrate);
|
||||
if (parsed.ec == std::errc{} && parsed.ptr == value.data() + value.size() &&
|
||||
bitrate >= 250 && bitrate <= 100'000)
|
||||
requested_bitrate_kbps.store(bitrate, std::memory_order_release);
|
||||
}
|
||||
else if (command == "QUIT") { quit.store(true, std::memory_order_release); break; }
|
||||
}
|
||||
});
|
||||
|
|
@ -217,6 +227,17 @@ int main(int argc, char **argv)
|
|||
}
|
||||
|
||||
const bool scheduled = options.force_idr_frame == frame_number;
|
||||
const std::uint64_t requested_bitrate =
|
||||
requested_bitrate_kbps.exchange(0, std::memory_order_acq_rel);
|
||||
if (requested_bitrate > 0 &&
|
||||
encoder->bit_rate != static_cast<int64_t>(requested_bitrate * 1000)) {
|
||||
encoder->bit_rate = static_cast<int64_t>(requested_bitrate * 1000);
|
||||
encoder->rc_max_rate = encoder->bit_rate;
|
||||
encoder->rc_buffer_size = static_cast<int>(
|
||||
encoder->bit_rate / options.framerate * 2);
|
||||
std::cerr << "muninn-video-encoder: bitrate=" << requested_bitrate
|
||||
<< "kbps frame=" << frame_number << '\n';
|
||||
}
|
||||
if (scheduled || force_next_idr.exchange(false, std::memory_order_acq_rel))
|
||||
frame->pict_type = AV_PICTURE_TYPE_I;
|
||||
else
|
||||
|
|
|
|||
Loading…
Reference in a new issue