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
2 changes: 1 addition & 1 deletion entry/src/main/ets/service/streaming/StreamingSession.ets
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export interface StreamStats {
renderedFps: number; // 渲染帧率 (Rd)
bitrate: number; // Kbps
latency: number; // 解码延迟 ms (EMA 瞬时值)
hostLatency: number; // 主机处理延迟(编码时间)ms
hostLatency: number; // 最近 1 秒主机处理延迟(编码时间)ms
networkLatency: number; // 网络延迟(RTT)ms
packetLoss: number; // %
decodedFrames: number;
Expand Down
4 changes: 2 additions & 2 deletions nativelib/src/main/cpp/moonlight_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1737,7 +1737,7 @@ napi_value MoonBridge_GetVideoStats(napi_env env, napi_callback_info info) {
napi_create_double(env, stats.currentFps, &fps); // 接收帧率 (Rx)
napi_create_double(env, stats.renderedFps, &renderedFps); // 渲染帧率 (Rd)
napi_create_double(env, stats.currentBitrate, &bitrate);
napi_create_double(env, stats.avgHostProcessingLatency, &hostLatency); // 主机处理延迟
napi_create_double(env, stats.recentHostProcessingLatency, &hostLatency); // 最近 1 秒主机处理延迟

// 网络丢帧统计
napi_value framesLost, totalFrames;
Expand All @@ -1758,7 +1758,7 @@ napi_value MoonBridge_GetVideoStats(napi_env env, napi_callback_info info) {
napi_set_named_property(env, result, "fps", fps); // 接收帧率 (Rx)
napi_set_named_property(env, result, "renderedFps", renderedFps); // 渲染帧率 (Rd)
napi_set_named_property(env, result, "bitrate", bitrate);
napi_set_named_property(env, result, "hostLatency", hostLatency); // 主机处理延迟(编码时间)
napi_set_named_property(env, result, "hostLatency", hostLatency); // 最近 1 秒主机处理延迟(编码时间)
napi_set_named_property(env, result, "framesLost", framesLost);
napi_set_named_property(env, result, "totalFrames", totalFrames);

Expand Down
25 changes: 21 additions & 4 deletions nativelib/src/main/cpp/video_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,27 @@ void VideoDecoder::UpdateReceivedStats(int size, int frameNumber, uint16_t hostP

auto currentTimeMs = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();

// Keep the live overlay responsive to recovery instead of letting old
// high-latency frames affect the value for the rest of the session.
if (recentHostLatencyWindowStartTimeMs_ == 0) {
recentHostLatencyWindowStartTimeMs_ = currentTimeMs;
} else if (currentTimeMs - recentHostLatencyWindowStartTimeMs_ >= kStatsUpdateIntervalMs) {
stats_.recentHostProcessingLatency = 0.0;
recentHostLatencyWindowFrames_ = 0;
recentHostLatencyWindowTotalMs_ = 0.0;
recentHostLatencyWindowStartTimeMs_ = currentTimeMs;
}

if (hostProcessingLatency > 0) {
double hostLatencyMs = static_cast<double>(hostProcessingLatency) / 10.0;
stats_.framesWithHostLatency++;
stats_.totalHostProcessingLatency += hostLatencyMs;
recentHostLatencyWindowFrames_++;
recentHostLatencyWindowTotalMs_ += hostLatencyMs;
stats_.recentHostProcessingLatency = recentHostLatencyWindowTotalMs_ /
static_cast<double>(recentHostLatencyWindowFrames_);
}

if (stats_.lastFpsCalculationTime == 0) {
// 初始化统计基线 - 注意:这是在增加 totalFrames 之后
Expand Down Expand Up @@ -1401,10 +1422,6 @@ void VideoDecoder::UpdateReceivedStats(int size, int frameNumber, uint16_t hostP
}
}

if (hostProcessingLatency > 0) {
stats_.framesWithHostLatency++;
stats_.totalHostProcessingLatency += static_cast<double>(hostProcessingLatency) / 10.0;
}
}

VideoDecoderStats VideoDecoder::GetStats() const {
Expand Down
4 changes: 4 additions & 0 deletions nativelib/src/main/cpp/video_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ struct VideoDecoderStats {
uint64_t framesWithHostLatency; // 有主机延迟数据的帧数
double totalHostProcessingLatency; // 累计主机处理延迟(ms)
double avgHostProcessingLatency; // 平均主机处理延迟(ms)
double recentHostProcessingLatency; // 最近 1 秒主机处理延迟(ms)
// 分类丢帧统计(按丢弃机制分类,用于诊断性能问题)
uint64_t droppedByL1; // L1: sync drain-to-latest 跳过的中间帧
uint64_t droppedByL2; // L2: async 模式延迟过高跳帧
Expand Down Expand Up @@ -359,6 +360,9 @@ class VideoDecoder {
// 统计信息
mutable std::mutex statsMutex_;
VideoDecoderStats stats_;
uint64_t recentHostLatencyWindowFrames_{0};
double recentHostLatencyWindowTotalMs_{0.0};
int64_t recentHostLatencyWindowStartTimeMs_{0};

// 运行状态
std::atomic<bool> running_{false};
Expand Down
Loading