blob: f57d42d40aa23d0243c072bc2a16e9d0cc9d9068 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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
Rasmus Brandt23772262022-05-23 09:53:15 +020011#include "modules/video_coding/timing/codec_timer.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <cstdint>
14
philipelcce46fc2015-12-21 03:04:49 -080015namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000016
magjed2943f012016-03-22 05:12:09 -070017namespace {
18
wuchengli@chromium.org30377c72013-09-28 06:06:18 +000019// The first kIgnoredSampleCount samples will be ignored.
magjed2943f012016-03-22 05:12:09 -070020const int kIgnoredSampleCount = 5;
Artem Titovdcd7fc72021-08-09 13:02:57 +020021// Return the `kPercentile` value in RequiredDecodeTimeMs().
magjed2943f012016-03-22 05:12:09 -070022const float kPercentile = 0.95f;
23// The window size in ms.
24const int64_t kTimeLimitMs = 10000;
25
26} // anonymous namespace
wuchengli@chromium.org30377c72013-09-28 06:06:18 +000027
Rasmus Brandt23772262022-05-23 09:53:15 +020028CodecTimer::CodecTimer() : ignored_sample_count_(0), filter_(kPercentile) {}
29CodecTimer::~CodecTimer() = default;
niklase@google.com470e71d2011-07-07 08:21:25 +000030
Rasmus Brandt23772262022-05-23 09:53:15 +020031void CodecTimer::AddTiming(int64_t decode_time_ms, int64_t now_ms) {
Artem Titovdcd7fc72021-08-09 13:02:57 +020032 // Ignore the first `kIgnoredSampleCount` samples.
magjed2943f012016-03-22 05:12:09 -070033 if (ignored_sample_count_ < kIgnoredSampleCount) {
34 ++ignored_sample_count_;
philipelcce46fc2015-12-21 03:04:49 -080035 return;
36 }
magjed2943f012016-03-22 05:12:09 -070037
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();
philipelcce46fc2015-12-21 03:04:49 -080047 }
niklase@google.com470e71d2011-07-07 08:21:25 +000048}
49
magjed2943f012016-03-22 05:12:09 -070050// Get the 95th percentile observed decode time within a time window.
Rasmus Brandt23772262022-05-23 09:53:15 +020051int64_t CodecTimer::RequiredDecodeTimeMs() const {
magjed2943f012016-03-22 05:12:09 -070052 return filter_.GetPercentileValue();
niklase@google.com470e71d2011-07-07 08:21:25 +000053}
magjed2943f012016-03-22 05:12:09 -070054
Rasmus Brandt23772262022-05-23 09:53:15 +020055CodecTimer::Sample::Sample(int64_t decode_time_ms, int64_t sample_time_ms)
magjed2943f012016-03-22 05:12:09 -070056 : decode_time_ms(decode_time_ms), sample_time_ms(sample_time_ms) {}
57
philipelcce46fc2015-12-21 03:04:49 -080058} // namespace webrtc