blob: a2a4e473c5951ca3660ca2ae31a293b2697b2eba [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/timing.h"
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <assert.h>
philipel5908c712015-12-21 08:23:20 -080014#include <algorithm>
15
Karl Wiberg76b7f512018-03-22 15:29:03 +010016#include "rtc_base/time/timestamp_extrapolator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "system_wrappers/include/clock.h"
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000018
niklase@google.com470e71d2011-07-07 08:21:25 +000019namespace webrtc {
20
philipel5908c712015-12-21 08:23:20 -080021VCMTiming::VCMTiming(Clock* clock, VCMTiming* master_timing)
ilnik2edc6842017-07-06 03:06:50 -070022 : clock_(clock),
23 master_(false),
24 ts_extrapolator_(),
25 codec_timer_(new VCMCodecTimer()),
26 render_delay_ms_(kDefaultRenderDelayMs),
27 min_playout_delay_ms_(0),
28 max_playout_delay_ms_(10000),
29 jitter_delay_ms_(0),
30 current_delay_ms_(0),
31 last_decode_ms_(0),
32 prev_frame_timestamp_(0),
33 timing_frame_info_(),
Åsa Persson81327d52018-06-05 13:34:33 +020034 num_decoded_frames_(0) {
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000035 if (master_timing == NULL) {
36 master_ = true;
wu@webrtc.org66773a02014-05-07 17:09:44 +000037 ts_extrapolator_ = new TimestampExtrapolator(clock_->TimeInMilliseconds());
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000038 } else {
39 ts_extrapolator_ = master_timing->ts_extrapolator_;
40 }
niklase@google.com470e71d2011-07-07 08:21:25 +000041}
42
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000043VCMTiming::~VCMTiming() {
44 if (master_) {
45 delete ts_extrapolator_;
46 }
niklase@google.com470e71d2011-07-07 08:21:25 +000047}
48
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000049void VCMTiming::Reset() {
kthelgasond701dfd2017-03-27 07:24:57 -070050 rtc::CritScope cs(&crit_sect_);
wu@webrtc.orged4cb562014-05-06 04:50:49 +000051 ts_extrapolator_->Reset(clock_->TimeInMilliseconds());
magjed2943f012016-03-22 05:12:09 -070052 codec_timer_.reset(new VCMCodecTimer());
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000053 render_delay_ms_ = kDefaultRenderDelayMs;
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000054 min_playout_delay_ms_ = 0;
55 jitter_delay_ms_ = 0;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000056 current_delay_ms_ = 0;
57 prev_frame_timestamp_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000058}
59
isheriff6b4b5f32016-06-08 00:24:21 -070060void VCMTiming::set_render_delay(int render_delay_ms) {
kthelgasond701dfd2017-03-27 07:24:57 -070061 rtc::CritScope cs(&crit_sect_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000062 render_delay_ms_ = render_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +000063}
64
isheriff6b4b5f32016-06-08 00:24:21 -070065void VCMTiming::set_min_playout_delay(int min_playout_delay_ms) {
kthelgasond701dfd2017-03-27 07:24:57 -070066 rtc::CritScope cs(&crit_sect_);
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000067 min_playout_delay_ms_ = min_playout_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +000068}
69
isheriff6b4b5f32016-06-08 00:24:21 -070070int VCMTiming::min_playout_delay() {
kthelgasond701dfd2017-03-27 07:24:57 -070071 rtc::CritScope cs(&crit_sect_);
isheriff6b4b5f32016-06-08 00:24:21 -070072 return min_playout_delay_ms_;
73}
74
75void VCMTiming::set_max_playout_delay(int max_playout_delay_ms) {
kthelgasond701dfd2017-03-27 07:24:57 -070076 rtc::CritScope cs(&crit_sect_);
isheriff6b4b5f32016-06-08 00:24:21 -070077 max_playout_delay_ms_ = max_playout_delay_ms;
78}
79
80int VCMTiming::max_playout_delay() {
kthelgasond701dfd2017-03-27 07:24:57 -070081 rtc::CritScope cs(&crit_sect_);
isheriff6b4b5f32016-06-08 00:24:21 -070082 return max_playout_delay_ms_;
83}
84
85void VCMTiming::SetJitterDelay(int jitter_delay_ms) {
kthelgasond701dfd2017-03-27 07:24:57 -070086 rtc::CritScope cs(&crit_sect_);
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000087 if (jitter_delay_ms != jitter_delay_ms_) {
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000088 jitter_delay_ms_ = jitter_delay_ms;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000089 // When in initial state, set current delay to minimum delay.
90 if (current_delay_ms_ == 0) {
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000091 current_delay_ms_ = jitter_delay_ms_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000092 }
93 }
niklase@google.com470e71d2011-07-07 08:21:25 +000094}
95
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000096void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) {
kthelgasond701dfd2017-03-27 07:24:57 -070097 rtc::CritScope cs(&crit_sect_);
isheriff6b4b5f32016-06-08 00:24:21 -070098 int target_delay_ms = TargetDelayInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +000099
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000100 if (current_delay_ms_ == 0) {
101 // Not initialized, set current delay to target.
102 current_delay_ms_ = target_delay_ms;
103 } else if (target_delay_ms != current_delay_ms_) {
philipel5908c712015-12-21 08:23:20 -0800104 int64_t delay_diff_ms =
105 static_cast<int64_t>(target_delay_ms) - current_delay_ms_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000106 // Never change the delay with more than 100 ms every second. If we're
107 // changing the delay in too large steps we will get noticeable freezes. By
108 // limiting the change we can increase the delay in smaller steps, which
109 // will be experienced as the video is played in slow motion. When lowering
110 // the delay the video will be played at a faster pace.
111 int64_t max_change_ms = 0;
112 if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) {
113 // wrap
philipel5908c712015-12-21 08:23:20 -0800114 max_change_ms = kDelayMaxChangeMsPerS *
115 (frame_timestamp + (static_cast<int64_t>(1) << 32) -
116 prev_frame_timestamp_) /
117 90000;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000118 } else {
119 max_change_ms = kDelayMaxChangeMsPerS *
philipel5908c712015-12-21 08:23:20 -0800120 (frame_timestamp - prev_frame_timestamp_) / 90000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000121 }
philipelfd5a20f2016-11-15 00:57:57 -0800122
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000123 if (max_change_ms <= 0) {
Åsa Persson8368d1a2018-01-05 12:44:45 +0100124 // Any changes less than 1 ms are truncated and will be postponed.
125 // Negative change will be due to reordering and should be ignored.
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000126 return;
127 }
128 delay_diff_ms = std::max(delay_diff_ms, -max_change_ms);
129 delay_diff_ms = std::min(delay_diff_ms, max_change_ms);
mikhal@webrtc.org6faba6e2013-04-30 15:39:34 +0000130
isheriff6b4b5f32016-06-08 00:24:21 -0700131 current_delay_ms_ = current_delay_ms_ + delay_diff_ms;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000132 }
133 prev_frame_timestamp_ = frame_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000134}
135
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000136void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms,
137 int64_t actual_decode_time_ms) {
kthelgasond701dfd2017-03-27 07:24:57 -0700138 rtc::CritScope cs(&crit_sect_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000139 uint32_t target_delay_ms = TargetDelayInternal();
magjed2943f012016-03-22 05:12:09 -0700140 int64_t delayed_ms =
141 actual_decode_time_ms -
142 (render_time_ms - RequiredDecodeTimeMs() - render_delay_ms_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000143 if (delayed_ms < 0) {
144 return;
145 }
146 if (current_delay_ms_ + delayed_ms <= target_delay_ms) {
isheriff6b4b5f32016-06-08 00:24:21 -0700147 current_delay_ms_ += delayed_ms;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000148 } else {
149 current_delay_ms_ = target_delay_ms;
150 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000151}
152
Åsa Persson8368d1a2018-01-05 12:44:45 +0100153void VCMTiming::StopDecodeTimer(uint32_t time_stamp,
154 int32_t decode_time_ms,
155 int64_t now_ms,
156 int64_t render_time_ms) {
kthelgasond701dfd2017-03-27 07:24:57 -0700157 rtc::CritScope cs(&crit_sect_);
magjed2943f012016-03-22 05:12:09 -0700158 codec_timer_->AddTiming(decode_time_ms, now_ms);
Per327d8ba2015-11-10 14:00:27 +0100159 assert(decode_time_ms >= 0);
160 last_decode_ms_ = decode_time_ms;
asapersson@webrtc.orgf2447602014-12-09 14:13:26 +0000161 ++num_decoded_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000162}
163
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000164void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) {
kthelgasond701dfd2017-03-27 07:24:57 -0700165 rtc::CritScope cs(&crit_sect_);
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000166 ts_extrapolator_->Update(now_ms, time_stamp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000167}
168
philipel5908c712015-12-21 08:23:20 -0800169int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp,
170 int64_t now_ms) const {
kthelgasond701dfd2017-03-27 07:24:57 -0700171 rtc::CritScope cs(&crit_sect_);
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200172 return RenderTimeMsInternal(frame_timestamp, now_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000173}
174
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000175int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp,
176 int64_t now_ms) const {
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200177 if (min_playout_delay_ms_ == 0 && max_playout_delay_ms_ == 0) {
178 // Render as soon as possible.
179 return 0;
180 }
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000181 int64_t estimated_complete_time_ms =
philipel5908c712015-12-21 08:23:20 -0800182 ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000183 if (estimated_complete_time_ms == -1) {
184 estimated_complete_time_ms = now_ms;
185 }
mikhal@webrtc.org6faba6e2013-04-30 15:39:34 +0000186
isheriff6b4b5f32016-06-08 00:24:21 -0700187 // Make sure the actual delay stays in the range of |min_playout_delay_ms_|
188 // and |max_playout_delay_ms_|.
189 int actual_delay = std::max(current_delay_ms_, min_playout_delay_ms_);
190 actual_delay = std::min(actual_delay, max_playout_delay_ms_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000191 return estimated_complete_time_ms + actual_delay;
niklase@google.com470e71d2011-07-07 08:21:25 +0000192}
193
isheriff6b4b5f32016-06-08 00:24:21 -0700194int VCMTiming::RequiredDecodeTimeMs() const {
195 const int decode_time_ms = codec_timer_->RequiredDecodeTimeMs();
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000196 assert(decode_time_ms >= 0);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000197 return decode_time_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000198}
199
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100200int64_t VCMTiming::MaxWaitingTime(int64_t render_time_ms,
201 int64_t now_ms) const {
kthelgasond701dfd2017-03-27 07:24:57 -0700202 rtc::CritScope cs(&crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000203
philipel5908c712015-12-21 08:23:20 -0800204 const int64_t max_wait_time_ms =
magjed2943f012016-03-22 05:12:09 -0700205 render_time_ms - now_ms - RequiredDecodeTimeMs() - render_delay_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000206
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100207 return max_wait_time_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000208}
209
isheriff6b4b5f32016-06-08 00:24:21 -0700210int VCMTiming::TargetVideoDelay() const {
kthelgasond701dfd2017-03-27 07:24:57 -0700211 rtc::CritScope cs(&crit_sect_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000212 return TargetDelayInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000213}
214
isheriff6b4b5f32016-06-08 00:24:21 -0700215int VCMTiming::TargetDelayInternal() const {
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +0000216 return std::max(min_playout_delay_ms_,
isheriff6b4b5f32016-06-08 00:24:21 -0700217 jitter_delay_ms_ + RequiredDecodeTimeMs() + render_delay_ms_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000218}
219
asapersson8d560882016-12-22 01:26:18 -0800220bool VCMTiming::GetTimings(int* decode_ms,
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000221 int* max_decode_ms,
222 int* current_delay_ms,
223 int* target_delay_ms,
224 int* jitter_buffer_ms,
225 int* min_playout_delay_ms,
226 int* render_delay_ms) const {
kthelgasond701dfd2017-03-27 07:24:57 -0700227 rtc::CritScope cs(&crit_sect_);
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000228 *decode_ms = last_decode_ms_;
isheriff6b4b5f32016-06-08 00:24:21 -0700229 *max_decode_ms = RequiredDecodeTimeMs();
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000230 *current_delay_ms = current_delay_ms_;
231 *target_delay_ms = TargetDelayInternal();
232 *jitter_buffer_ms = jitter_delay_ms_;
233 *min_playout_delay_ms = min_playout_delay_ms_;
234 *render_delay_ms = render_delay_ms_;
asapersson8d560882016-12-22 01:26:18 -0800235 return (num_decoded_frames_ > 0);
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000236}
237
ilnik2edc6842017-07-06 03:06:50 -0700238void VCMTiming::SetTimingFrameInfo(const TimingFrameInfo& info) {
239 rtc::CritScope cs(&crit_sect_);
240 timing_frame_info_.emplace(info);
241}
242
Danil Chapovalov0040b662018-06-18 10:48:16 +0200243absl::optional<TimingFrameInfo> VCMTiming::GetTimingFrameInfo() {
ilnik2edc6842017-07-06 03:06:50 -0700244 rtc::CritScope cs(&crit_sect_);
245 return timing_frame_info_;
246}
247
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000248} // namespace webrtc