WebRTC编码过载检测与帧率适应机制分析报告
1. 概述
本报告基于WebRTC源码分析,详细阐述了编码过载检测机制及其触发的帧率降级算法。分析范围涵盖从编码时间测量到最终帧率调整的完整调用链。
2. 编码使用率计算机制
2.1 基本计算公式
位置: android/cmake/src/video/adaptation/overuse_frame_detector.cc:163-164
float encode_usage_percent = 100.0f * filtered_processing_ms_->filtered() / frame_diff_ms;
2.2 编码时间测量
编码时长计算 (overuse_frame_detector.cc:143-144
):
encode_duration_us = timing.last_send_us - timing.capture_us;
timing.capture_us
: 帧捕获时间戳timing.last_send_us
: 帧发送完成时间戳- 测量范围: 从帧捕获到编码完成发送的总时间
2.3 滤波处理
指数滤波算法 (rtc_base/numerics/exp_filter.cc:24
):
y(k) = alpha^exp × y(k-1) + (1 - alpha^exp) × sample
数据处理流程 (overuse_frame_detector.cc:148
):
AddSample(1e-3 * (*encode_duration_us), 1e-3 * diff_us);
// 微秒转毫秒: 1e-3 = 0.001
3. 过载检测阈值与判断逻辑
3.1 默认阈值设置
软件编码器 (overuse_frame_detector.h:34
):
int high_encode_usage_threshold_percent = 85; // 85%
硬件编码器 (video_stream_encoder_resource_manager.cc:684-685
):
options.low_encode_usage_threshold_percent = 150; // 150%
options.high_encode_usage_threshold_percent = 200; // 200%
3.2 过载判断条件
单次检查 (overuse_frame_detector.cc:653-657
):
if (usage_percent >= options_.high_encode_usage_threshold_percent) {++checks_above_threshold_;
} else {checks_above_threshold_ = 0;
}
连续检查 (overuse_frame_detector.cc:658
):
return checks_above_threshold_ >= options_.high_threshold_consecutive_count;
// high_threshold_consecutive_count = 2 (默认值)
最小样本要求 (overuse_frame_detector.cc:586-587
):
if (num_process_times_ <= options_.min_process_count || // min_process_count = 3!encode_usage_percent_)return; // 不触发适应
4. 过载触发的调用链
4.1 检测到过载后的调用序列
// 1. 过载检测 (overuse_frame_detector.cc:616)
observer->AdaptDown();// 2. 资源适应 (encode_usage_resource.cc:93-95)
void EncodeUsageResource::AdaptDown() {OnResourceUsageStateMeasured(ResourceUsageState::kOveruse);
}// 3. 资源处理 (resource_adaptation_processor.cc:201)
case ResourceUsageState::kOveruse:result_and_message = OnResourceOveruse(resource);// 4. 获取适应方案 (resource_adaptation_processor.cc:287)
Adaptation adaptation = stream_adapter_->GetAdaptationDown();// 5. 应用适应 (resource_adaptation_processor.cc:305)
stream_adapter_->ApplyAdaptation(adaptation, reason_resource);
4.2 适应方案选择
降级偏好判断 (video_stream_adapter.cc:454-475
):
switch (degradation_preference_) {case DegradationPreference::BALANCED: {// 先尝试降帧率,再降分辨率RestrictionsOrState decrease_frame_rate = DecreaseFramerate(input_state, current_restrictions);if (成功) return decrease_frame_rate;// 否则降分辨率[[fallthrough]];}case DegradationPreference::MAINTAIN_FRAMERATE: {// 只降分辨率return DecreaseResolution(input_state, current_restrictions);}case DegradationPreference::MAINTAIN_RESOLUTION: {// 只降帧率return DecreaseFramerate(input_state, current_restrictions);}
}
5. 帧率降级算法
5.1 核心降级公式
位置: video_stream_adapter.cc:37-40
int GetLowerFrameRateThan(int fps) {RTC_DCHECK(fps != std::numeric_limits<int>::max());return (fps * 2) / 3; // 关键公式:降到原来的2/3
}
5.2 帧率降级实现
MAINTAIN_RESOLUTION模式 (video_stream_adapter.cc:507
):
if (degradation_preference_ == DegradationPreference::MAINTAIN_RESOLUTION) {max_frame_rate = GetLowerFrameRateThan(input_state.frames_per_second());
}
BALANCED模式 (video_stream_adapter.cc:511-512
):
else if (degradation_preference_ == DegradationPreference::BALANCED) {max_frame_rate = balanced_settings_.MinFps(input_state.video_codec_type(),frame_size_pixels);
}
5.3 最小帧率限制
最小值设定 (video_stream_adapter.cc:32
):
const int kMinFrameRateFps = 2;
限制应用 (video_stream_adapter.cc:522
):
max_frame_rate = std::max(kMinFrameRateFps, max_frame_rate);
6. 实际降级序列计算
6.1 MAINTAIN_RESOLUTION模式帧率序列
基于公式 new_fps = current_fps × 2/3
:
当前帧率 | 计算过程 | 降级后帧率 | 降幅 |
---|---|---|---|
30fps | 30 × 2/3 = 20 | 20fps | 33.3% |
20fps | 20 × 2/3 = 13.33 | 13fps | 35% |
13fps | 13 × 2/3 = 8.66 | 8fps | 38.5% |
8fps | 8 × 2/3 = 5.33 | 5fps | 37.5% |
5fps | 5 × 2/3 = 3.33 | 3fps | 40% |
3fps | 3 × 2/3 = 2 | 2fps | 33.3% |
2fps | 2 × 2/3 = 1.33 | 2fps | 0% (最小值) |
6.2 BALANCED模式帧率配置
默认配置 (balanced_degradation_settings.cc:25-55
):
std::vector<Config> DefaultConfigs() {return {{320 * 240, 7, ...}, // 76,800像素 → 7fps最小值{480 * 360, 10, ...}, // 172,800像素 → 10fps最小值{640 * 480, 15, ...} // 307,200像素 → 15fps最小值};
}
7. 编码使用率实例分析
7.1 30fps正常情况
理论帧间隔:1000ms / 30fps = 33.33ms
实际编码时间:25ms
编码使用率 = 100 × 25ms / 33.33ms = 75%
结果:75% < 85% → 正常运行
7.2 30fps过载情况
理论帧间隔:33.33ms
实际编码时间:30ms
编码使用率 = 100 × 30ms / 33.33ms = 90%
结果:90% > 85% → 触发降帧率(连续2次检测后)
7.3 硬件编码器容忍度
理论帧间隔:33.33ms
实际编码时间:60ms(包含流水线延迟)
编码使用率 = 100 × 60ms / 33.33ms = 180%
结果:180% < 200% → 正常运行(硬件编码器阈值)
8. 关键常量定义
常量名 | 数值 | 位置 | 说明 |
---|---|---|---|
high_encode_usage_threshold_percent |
85% | overuse_frame_detector.h:34 |
软件编码器过载阈值 |
high_encode_usage_threshold_percent |
200% | video_stream_encoder_resource_manager.cc:685 |
硬件编码器过载阈值 |
high_threshold_consecutive_count |
2 | overuse_frame_detector.h:53 |
连续过载检测次数 |
min_process_count |
3 | overuse_frame_detector.h:50 |
最小处理次数要求 |
min_frame_samples |
120 | overuse_frame_detector.h:46 |
最小帧样本数 |
kMinFrameRateFps |
2 | video_stream_adapter.cc:32 |
最小帧率限制 |
9. 总结
WebRTC的编码过载检测与帧率适应机制基于以下核心原理:
- 时间测量: 精确测量从帧捕获到发送完成的总时间
- 使用率计算: 编码时间占帧间隔时间的百分比
- 过载判断: 连续2次检测到使用率≥85%(软件)或≥200%(硬件)
- 帧率降级: 按2/3比例递减,最低至2fps
- 模式适应: 根据降级偏好选择降帧率或降分辨率
该机制确保在CPU资源紧张时,通过及时的帧率调整维持视频编码的实时性和稳定性。