blob: c62c848c09a90cdfb92f813fe719be73f67e20af [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
philipel5908c712015-12-21 08:23:20 -080015#include <algorithm>
16
Karl Wiberg76b7f512018-03-22 15:29:03 +010017#include "rtc_base/time/timestamp_extrapolator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "system_wrappers/include/clock.h"
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000019
niklase@google.com470e71d2011-07-07 08:21:25 +000020namespace webrtc {
21
philipel5908c712015-12-21 08:23:20 -080022VCMTiming::VCMTiming(Clock* clock, VCMTiming* master_timing)
ilnik2edc6842017-07-06 03:06:50 -070023 : clock_(clock),
24 master_(false),
25 ts_extrapolator_(),
26 codec_timer_(new VCMCodecTimer()),
27 render_delay_ms_(kDefaultRenderDelayMs),
28 min_playout_delay_ms_(0),
29 max_playout_delay_ms_(10000),
30 jitter_delay_ms_(0),
31 current_delay_ms_(0),
ilnik2edc6842017-07-06 03:06:50 -070032 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
Johannes Kronbfd343b2019-07-01 10:07:50 +0200153void VCMTiming::StopDecodeTimer(uint32_t /*time_stamp*/,
Åsa Persson8368d1a2018-01-05 12:44:45 +0100154 int32_t decode_time_ms,
155 int64_t now_ms,
Johannes Kronbfd343b2019-07-01 10:07:50 +0200156 int64_t /*render_time_ms*/) {
157 StopDecodeTimer(decode_time_ms, now_ms);
158}
159
160void VCMTiming::StopDecodeTimer(int32_t decode_time_ms, int64_t now_ms) {
kthelgasond701dfd2017-03-27 07:24:57 -0700161 rtc::CritScope cs(&crit_sect_);
magjed2943f012016-03-22 05:12:09 -0700162 codec_timer_->AddTiming(decode_time_ms, now_ms);
Per327d8ba2015-11-10 14:00:27 +0100163 assert(decode_time_ms >= 0);
asapersson@webrtc.orgf2447602014-12-09 14:13:26 +0000164 ++num_decoded_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000165}
166
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000167void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) {
kthelgasond701dfd2017-03-27 07:24:57 -0700168 rtc::CritScope cs(&crit_sect_);
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000169 ts_extrapolator_->Update(now_ms, time_stamp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000170}
171
philipel5908c712015-12-21 08:23:20 -0800172int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp,
173 int64_t now_ms) const {
kthelgasond701dfd2017-03-27 07:24:57 -0700174 rtc::CritScope cs(&crit_sect_);
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200175 return RenderTimeMsInternal(frame_timestamp, now_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000176}
177
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000178int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp,
179 int64_t now_ms) const {
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200180 if (min_playout_delay_ms_ == 0 && max_playout_delay_ms_ == 0) {
181 // Render as soon as possible.
182 return 0;
183 }
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000184 int64_t estimated_complete_time_ms =
philipel5908c712015-12-21 08:23:20 -0800185 ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000186 if (estimated_complete_time_ms == -1) {
187 estimated_complete_time_ms = now_ms;
188 }
mikhal@webrtc.org6faba6e2013-04-30 15:39:34 +0000189
isheriff6b4b5f32016-06-08 00:24:21 -0700190 // Make sure the actual delay stays in the range of |min_playout_delay_ms_|
191 // and |max_playout_delay_ms_|.
192 int actual_delay = std::max(current_delay_ms_, min_playout_delay_ms_);
193 actual_delay = std::min(actual_delay, max_playout_delay_ms_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000194 return estimated_complete_time_ms + actual_delay;
niklase@google.com470e71d2011-07-07 08:21:25 +0000195}
196
isheriff6b4b5f32016-06-08 00:24:21 -0700197int VCMTiming::RequiredDecodeTimeMs() const {
198 const int decode_time_ms = codec_timer_->RequiredDecodeTimeMs();
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000199 assert(decode_time_ms >= 0);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000200 return decode_time_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000201}
202
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100203int64_t VCMTiming::MaxWaitingTime(int64_t render_time_ms,
204 int64_t now_ms) const {
kthelgasond701dfd2017-03-27 07:24:57 -0700205 rtc::CritScope cs(&crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000206
philipel5908c712015-12-21 08:23:20 -0800207 const int64_t max_wait_time_ms =
magjed2943f012016-03-22 05:12:09 -0700208 render_time_ms - now_ms - RequiredDecodeTimeMs() - render_delay_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000209
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100210 return max_wait_time_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000211}
212
isheriff6b4b5f32016-06-08 00:24:21 -0700213int VCMTiming::TargetVideoDelay() const {
kthelgasond701dfd2017-03-27 07:24:57 -0700214 rtc::CritScope cs(&crit_sect_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000215 return TargetDelayInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000216}
217
isheriff6b4b5f32016-06-08 00:24:21 -0700218int VCMTiming::TargetDelayInternal() const {
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +0000219 return std::max(min_playout_delay_ms_,
isheriff6b4b5f32016-06-08 00:24:21 -0700220 jitter_delay_ms_ + RequiredDecodeTimeMs() + render_delay_ms_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000221}
222
Johannes Kronbfd343b2019-07-01 10:07:50 +0200223bool VCMTiming::GetTimings(int* max_decode_ms,
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000224 int* current_delay_ms,
225 int* target_delay_ms,
226 int* jitter_buffer_ms,
227 int* min_playout_delay_ms,
228 int* render_delay_ms) const {
kthelgasond701dfd2017-03-27 07:24:57 -0700229 rtc::CritScope cs(&crit_sect_);
isheriff6b4b5f32016-06-08 00:24:21 -0700230 *max_decode_ms = RequiredDecodeTimeMs();
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000231 *current_delay_ms = current_delay_ms_;
232 *target_delay_ms = TargetDelayInternal();
233 *jitter_buffer_ms = jitter_delay_ms_;
234 *min_playout_delay_ms = min_playout_delay_ms_;
235 *render_delay_ms = render_delay_ms_;
asapersson8d560882016-12-22 01:26:18 -0800236 return (num_decoded_frames_ > 0);
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000237}
238
ilnik2edc6842017-07-06 03:06:50 -0700239void VCMTiming::SetTimingFrameInfo(const TimingFrameInfo& info) {
240 rtc::CritScope cs(&crit_sect_);
241 timing_frame_info_.emplace(info);
242}
243
Danil Chapovalov0040b662018-06-18 10:48:16 +0200244absl::optional<TimingFrameInfo> VCMTiming::GetTimingFrameInfo() {
ilnik2edc6842017-07-06 03:06:50 -0700245 rtc::CritScope cs(&crit_sect_);
246 return timing_frame_info_;
247}
248
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000249} // namespace webrtc