xref: /aosp_15_r20/external/webrtc/modules/video_coding/timing/codec_timer.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "modules/video_coding/timing/codec_timer.h"
12 
13 #include <cstdint>
14 
15 namespace webrtc {
16 
17 namespace {
18 
19 // The first kIgnoredSampleCount samples will be ignored.
20 const int kIgnoredSampleCount = 5;
21 // Return the `kPercentile` value in RequiredDecodeTimeMs().
22 const float kPercentile = 0.95f;
23 // The window size in ms.
24 const int64_t kTimeLimitMs = 10000;
25 
26 }  // anonymous namespace
27 
CodecTimer()28 CodecTimer::CodecTimer() : ignored_sample_count_(0), filter_(kPercentile) {}
29 CodecTimer::~CodecTimer() = default;
30 
AddTiming(int64_t decode_time_ms,int64_t now_ms)31 void CodecTimer::AddTiming(int64_t decode_time_ms, int64_t now_ms) {
32   // Ignore the first `kIgnoredSampleCount` samples.
33   if (ignored_sample_count_ < kIgnoredSampleCount) {
34     ++ignored_sample_count_;
35     return;
36   }
37 
38   // Insert new decode time value.
39   filter_.Insert(decode_time_ms);
40   history_.emplace(decode_time_ms, now_ms);
41 
42   // Pop old decode time values.
43   while (!history_.empty() &&
44          now_ms - history_.front().sample_time_ms > kTimeLimitMs) {
45     filter_.Erase(history_.front().decode_time_ms);
46     history_.pop();
47   }
48 }
49 
50 // Get the 95th percentile observed decode time within a time window.
RequiredDecodeTimeMs() const51 int64_t CodecTimer::RequiredDecodeTimeMs() const {
52   return filter_.GetPercentileValue();
53 }
54 
Sample(int64_t decode_time_ms,int64_t sample_time_ms)55 CodecTimer::Sample::Sample(int64_t decode_time_ms, int64_t sample_time_ms)
56     : decode_time_ms(decode_time_ms), sample_time_ms(sample_time_ms) {}
57 
58 }  // namespace webrtc
59