Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions rust/bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,20 @@ pub extern "C" fn evaluate_upscaling_ffi(
static QUALITY_CONTROLLER: Lazy<Mutex<media_logic::quality::QualityController>> =
Lazy::new(|| Mutex::new(media_logic::quality::QualityController::new()));

/// Lets the governor pick a display refresh rate that is a whole multiple of the video's
/// frame rate; 0.0 means unknown.
fn set_quality_content_fps(fps: f32) {
if let Ok(mut qc) = QUALITY_CONTROLLER.lock() {
qc.set_content_fps(fps);
}
}

fn reset_quality_for_new_video() {
if let Ok(mut qc) = QUALITY_CONTROLLER.lock() {
qc.reset_for_new_content();
}
}

#[no_mangle]
pub extern "C" fn quality_controller_set_enabled(enabled: u32) {
if let Ok(mut qc) = QUALITY_CONTROLLER.lock() {
Expand Down Expand Up @@ -1323,6 +1337,7 @@ pub extern "C" fn start_smb_playback(
}

reset_3d_mode();
reset_quality_for_new_video();
DOWNLOAD_MANAGER.set_playback_active(true);
if let Ok(mut controller) = CONTROLLER.lock() {
controller.stop();
Expand All @@ -1332,8 +1347,10 @@ pub extern "C" fn start_smb_playback(
log(6, &format!("Error loading SMB video: {:?}", e));
}
set_last_playback_error(format!("{:?}", e));
set_quality_content_fps(0.0);
} else {
apply_screen_mode_after_load(&controller);
set_quality_content_fps(controller.video_fps());
unsafe {
log(4, "SMB video loaded successfully!");
}
Expand Down Expand Up @@ -1535,6 +1552,7 @@ pub extern "C" fn start_ftp_playback(
}

reset_3d_mode();
reset_quality_for_new_video();
DOWNLOAD_MANAGER.set_playback_active(true);
if let Ok(mut controller) = CONTROLLER.lock() {
controller.stop();
Expand All @@ -1544,8 +1562,10 @@ pub extern "C" fn start_ftp_playback(
log(6, &format!("Error loading FTP video: {:?}", e));
}
set_last_playback_error(format!("{:?}", e));
set_quality_content_fps(0.0);
} else {
apply_screen_mode_after_load(&controller);
set_quality_content_fps(controller.video_fps());
unsafe {
log(4, "FTP video loaded successfully!");
}
Expand Down Expand Up @@ -1701,6 +1721,7 @@ pub extern "C" fn start_sftp_playback(
}

reset_3d_mode();
reset_quality_for_new_video();
DOWNLOAD_MANAGER.set_playback_active(true);
if let Ok(mut controller) = CONTROLLER.lock() {
controller.stop();
Expand All @@ -1710,8 +1731,10 @@ pub extern "C" fn start_sftp_playback(
log(6, &format!("Error loading SFTP video: {:?}", e));
}
set_last_playback_error(format!("{:?}", e));
set_quality_content_fps(0.0);
} else {
apply_screen_mode_after_load(&controller);
set_quality_content_fps(controller.video_fps());
unsafe {
log(4, "SFTP video loaded successfully!");
}
Expand Down Expand Up @@ -1920,6 +1943,7 @@ pub extern "C" fn start_nfs_playback(
}

reset_3d_mode();
reset_quality_for_new_video();
DOWNLOAD_MANAGER.set_playback_active(true);
if let Ok(mut controller) = CONTROLLER.lock() {
controller.stop();
Expand All @@ -1929,8 +1953,10 @@ pub extern "C" fn start_nfs_playback(
log(6, &format!("Error loading NFS video: {:?}", e));
}
set_last_playback_error(format!("{:?}", e));
set_quality_content_fps(0.0);
} else {
apply_screen_mode_after_load(&controller);
set_quality_content_fps(controller.video_fps());
unsafe {
log(4, "NFS video loaded successfully!");
}
Expand Down Expand Up @@ -2098,6 +2124,7 @@ pub extern "C" fn start_webdav_playback(
}

reset_3d_mode();
reset_quality_for_new_video();
DOWNLOAD_MANAGER.set_playback_active(true);
if let Ok(mut controller) = CONTROLLER.lock() {
controller.stop();
Expand All @@ -2107,8 +2134,10 @@ pub extern "C" fn start_webdav_playback(
log(6, &format!("Error loading WebDAV video: {:?}", e));
}
set_last_playback_error(format!("{:?}", e));
set_quality_content_fps(0.0);
} else {
apply_screen_mode_after_load(&controller);
set_quality_content_fps(controller.video_fps());
unsafe {
log(4, "WebDAV video loaded successfully!");
}
Expand Down Expand Up @@ -2430,6 +2459,7 @@ pub extern "C" fn start_video_playback(path: *const std::os::raw::c_char, start_
}

reset_3d_mode();
reset_quality_for_new_video();
if path_str.starts_with("http://") || path_str.starts_with("https://") {
DOWNLOAD_MANAGER.set_playback_active(true);
}
Expand All @@ -2441,14 +2471,10 @@ pub extern "C" fn start_video_playback(path: *const std::os::raw::c_char, start_
log(6, &format!("Error loading video: {:?}", e));
}
set_last_playback_error(format!("{:?}", e));
if let Ok(mut qc) = QUALITY_CONTROLLER.lock() {
qc.set_content_fps(0.0);
}
set_quality_content_fps(0.0);
} else {
apply_screen_mode_after_load(&controller);
if let Ok(mut qc) = QUALITY_CONTROLLER.lock() {
qc.set_content_fps(controller.video_fps());
}
set_quality_content_fps(controller.video_fps());
unsafe {
log(4, "Video loaded successfully!");
}
Expand Down
7 changes: 7 additions & 0 deletions rust/core/src/playback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,13 @@ impl PlaybackController {
format.set_i32("max-input-size", max_input_size);
// Prioridade de decodificaΓ§Γ£o realtime (0 = realtime / baixa latΓͺncia)
format.set_i32("priority", 0);
// With realtime priority the codec sizes its clocks and bandwidth from the frame rate
// (MediaFormat KEY_FRAME_RATE / KEY_OPERATING_RATE). Without it, an 8192x4096 HEVC
// stream at ~122 Mbps decoded at ~47-57 of 60 fps on Quest 3.
if video_fps > 0.0 {
format.set_f32("frame-rate", video_fps);
format.set_f32("operating-rate", video_fps);
}

// Repassa ao MediaCodec o espaco de cor declarado pelo container: o
// dataspace do buffer decodificado vira o `suggestedYcbcrModel/Range`
Expand Down
116 changes: 105 additions & 11 deletions rust/media-logic/src/quality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,18 @@ impl QualityController {
self.stable_healthy_samples = 0;
}

/// Starts a new video from the default level. A degradation earned by the previous file
/// says nothing about the next one, which would otherwise spend ~30 s per level recovering.
/// With adaptive mode off the level is the user's choice and stays.
pub fn reset_for_new_content(&mut self) {
if !self.enabled {
return;
}
self.current_level = QualityLevel::High;
self.last_reason = QualityTransitionReason::None;
self.reset_counters();
}

/// Avalia a amostra de performance e atualiza o estado interno do controlador.
pub fn evaluate(&mut self, sample: &QualitySample) -> QualityAction {
if !self.enabled {
Expand Down Expand Up @@ -341,8 +353,15 @@ impl QualityController {
return QualityAction::Degrade(self.current_level, self.last_reason);
}

// Video frames are also dropped when the hardware decoder cannot keep up with the
// stream's bitrate. With GPU time and compositor pacing both inside their healthy
// budgets the renderer is not the cause, and lowering render scale or refresh rate
// would only cost image quality, so drops count against rendering only otherwise.
let render_has_headroom = sample.frame_time_ms < pacing_healthy_ms
&& sample.smoothed_gpu_time_ms < gpu_healthy_ms;

// 2. VerificaΓ§Γ£o de Descarte Severo de Frames:
if sample.dropped_fps >= Self::DROPPED_FPS_STRESS_THRESHOLD {
if sample.dropped_fps >= Self::DROPPED_FPS_STRESS_THRESHOLD && !render_has_headroom {
self.dropped_fps_stress_count += 1;
self.stable_healthy_samples = 0;
if self.dropped_fps_stress_count >= Self::DROPPED_FPS_STRESS_THRESHOLD_SAMPLES
Expand Down Expand Up @@ -400,10 +419,7 @@ impl QualityController {
QualityLevel::High | QualityLevel::Ultra => sample.thermal_level <= 1,
};

let is_healthy = is_thermal_permitting_upgrade
&& sample.dropped_fps < 0.5f32
&& sample.frame_time_ms < pacing_healthy_ms
&& sample.smoothed_gpu_time_ms < gpu_healthy_ms;
let is_healthy = is_thermal_permitting_upgrade && render_has_headroom;

if is_healthy {
self.stable_healthy_samples += 1;
Expand All @@ -416,10 +432,7 @@ impl QualityController {
self.reset_counters();
return QualityAction::Upgrade(self.current_level, self.last_reason);
}
} else if sample.smoothed_gpu_time_ms > gpu_healthy_ms
|| sample.frame_time_ms > pacing_healthy_ms
|| sample.dropped_fps >= 0.5f32
{
} else if !render_has_headroom {
// CondiΓ§Γ£o marginal ou estresse: reinicia o acΓΊmulo de estabilidade para evitar flapping
self.stable_healthy_samples = 0;
}
Expand Down Expand Up @@ -594,9 +607,10 @@ mod tests {
let mut controller = QualityController::new();
assert_eq!(controller.current_level(), QualityLevel::High);

// Marginal GPU (above gpu_healthy_ms, below gpu_budget_ms): drops count as render stress.
let sample_drop = QualitySample {
thermal_level: 0,
smoothed_gpu_time_ms: 6.0,
smoothed_gpu_time_ms: 8.0,
frame_time_ms: 11.0,
dropped_fps: 4.5,
target_fps: 90.0,
Expand Down Expand Up @@ -633,6 +647,7 @@ mod tests {
target_fps: 90.0,
};
let sample_spike = QualitySample {
smoothed_gpu_time_ms: 8.0,
dropped_fps: 56.0,
..sample_healthy
};
Expand All @@ -658,7 +673,7 @@ mod tests {
let mut controller = QualityController::new();
let sample_drop = QualitySample {
thermal_level: 0,
smoothed_gpu_time_ms: 6.0,
smoothed_gpu_time_ms: 8.0,
frame_time_ms: 11.0,
dropped_fps: 5.0,
target_fps: 90.0,
Expand Down Expand Up @@ -922,4 +937,83 @@ mod tests {
assert_eq!(qc.target_refresh_rate(QualityLevel::Low), 72.0);
assert_eq!(qc.target_refresh_rate(QualityLevel::Emergency), 72.0);
}

// Telemetry of an 8192x4096 HEVC Main 10 file at ~122 Mbps on Quest 3: the hardware decoder
// delivered ~47 of 60 fps while the GPU took ~1.9 ms of an 11.1 ms frame. Lowering render
// scale or refresh rate cannot help a decoder-bound stream.
fn decode_bound_sample() -> QualitySample {
QualitySample {
thermal_level: 0,
smoothed_gpu_time_ms: 1.9,
frame_time_ms: 1000.0 / 90.0,
dropped_fps: 11.0,
target_fps: 90.0,
}
}

#[test]
fn test_dropped_frames_with_render_headroom_do_not_degrade() {
let mut controller = QualityController::new();

for i in 0..20 {
assert_eq!(
controller.evaluate(&decode_bound_sample()),
QualityAction::Maintain,
"sample {i}: decoder-bound drops must not lower render quality"
);
}
assert_eq!(controller.current_level(), QualityLevel::High);
}

#[test]
fn test_dropped_frames_with_render_headroom_do_not_block_recovery() {
let mut controller = QualityController::new();
controller.set_manual_level(QualityLevel::Medium);

let actions: Vec<_> = (0..QualityController::UPGRADE_STABILITY_REQUIRED_SAMPLES)
.map(|_| controller.evaluate(&decode_bound_sample()))
.collect();

assert_eq!(
actions.last(),
Some(&QualityAction::Upgrade(
QualityLevel::High,
QualityTransitionReason::StableRecovery
))
);
}

#[test]
fn test_reset_for_new_content_drops_previous_video_degradation() {
let mut controller = QualityController::new();
let gpu_overload = QualitySample {
thermal_level: 0,
smoothed_gpu_time_ms: 10.5,
frame_time_ms: 1000.0 / 90.0,
dropped_fps: 0.0,
target_fps: 90.0,
};
controller.evaluate(&gpu_overload);
controller.evaluate(&gpu_overload);
assert_eq!(controller.current_level(), QualityLevel::Medium);
// One stress sample pending: without a counter reset, the next one would degrade at once.
controller.evaluate(&gpu_overload);

controller.reset_for_new_content();

assert_eq!(controller.current_level(), QualityLevel::High);
assert_eq!(controller.last_reason(), QualityTransitionReason::None);
assert_eq!(controller.evaluate(&gpu_overload), QualityAction::Maintain);
}

#[test]
fn test_reset_for_new_content_keeps_level_when_adaptive_mode_is_off() {
let mut controller = QualityController::new();
controller.set_enabled(false);
controller.set_manual_level(QualityLevel::Low);

controller.reset_for_new_content();

assert_eq!(controller.current_level(), QualityLevel::Low);
}
}