blob: 8da2b8588d918d9866b6752a051dbef9b53e6cb8 [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),
ilnik2edc6842017-07-06 03:06:50 -070031 prev_frame_timestamp_(0),
32 timing_frame_info_(),
Åsa Persson81327d52018-06-05 13:34:33 +020033 num_decoded_frames_(0) {
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000034 if (master_timing == NULL) {
35 master_ = true;
wu@webrtc.org66773a02014-05-07 17:09:44 +000036 ts_extrapolator_ = new TimestampExtrapolator(clock_->TimeInMilliseconds());
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000037 } else {
38 ts_extrapolator_ = master_timing->ts_extrapolator_;
39 }
niklase@google.com470e71d2011-07-07 08:21:25 +000040}
41
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000042VCMTiming::~VCMTiming() {
43 if (master_) {
44 delete ts_extrapolator_;
45 }
niklase@google.com470e71d2011-07-07 08:21:25 +000046}
47
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000048void VCMTiming::Reset() {
kthelgasond701dfd2017-03-27 07:24:57 -070049 rtc::CritScope cs(&crit_sect_);
wu@webrtc.orged4cb562014-05-06 04:50:49 +000050 ts_extrapolator_->Reset(clock_->TimeInMilliseconds());
magjed2943f012016-03-22 05:12:09 -070051 codec_timer_.reset(new VCMCodecTimer());
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000052 render_delay_ms_ = kDefaultRenderDelayMs;
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000053 min_playout_delay_ms_ = 0;
54 jitter_delay_ms_ = 0;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000055 current_delay_ms_ = 0;
56 prev_frame_timestamp_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000057}
58
isheriff6b4b5f32016-06-08 00:24:21 -070059void VCMTiming::set_render_delay(int render_delay_ms) {
kthelgasond701dfd2017-03-27 07:24:57 -070060 rtc::CritScope cs(&crit_sect_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000061 render_delay_ms_ = render_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +000062}
63
isheriff6b4b5f32016-06-08 00:24:21 -070064void VCMTiming::set_min_playout_delay(int min_playout_delay_ms) {
kthelgasond701dfd2017-03-27 07:24:57 -070065 rtc::CritScope cs(&crit_sect_);
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000066 min_playout_delay_ms_ = min_playout_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +000067}
68
isheriff6b4b5f32016-06-08 00:24:21 -070069int VCMTiming::min_playout_delay() {
kthelgasond701dfd2017-03-27 07:24:57 -070070 rtc::CritScope cs(&crit_sect_);
isheriff6b4b5f32016-06-08 00:24:21 -070071 return min_playout_delay_ms_;
72}
73
74void VCMTiming::set_max_playout_delay(int max_playout_delay_ms) {
kthelgasond701dfd2017-03-27 07:24:57 -070075 rtc::CritScope cs(&crit_sect_);
isheriff6b4b5f32016-06-08 00:24:21 -070076 max_playout_delay_ms_ = max_playout_delay_ms;
77}
78
79int VCMTiming::max_playout_delay() {
kthelgasond701dfd2017-03-27 07:24:57 -070080 rtc::CritScope cs(&crit_sect_);
isheriff6b4b5f32016-06-08 00:24:21 -070081 return max_playout_delay_ms_;
82}
83
84void VCMTiming::SetJitterDelay(int jitter_delay_ms) {
kthelgasond701dfd2017-03-27 07:24:57 -070085 rtc::CritScope cs(&crit_sect_);
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000086 if (jitter_delay_ms != jitter_delay_ms_) {
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000087 jitter_delay_ms_ = jitter_delay_ms;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000088 // When in initial state, set current delay to minimum delay.
89 if (current_delay_ms_ == 0) {
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000090 current_delay_ms_ = jitter_delay_ms_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000091 }
92 }
niklase@google.com470e71d2011-07-07 08:21:25 +000093}
94
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000095void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) {
kthelgasond701dfd2017-03-27 07:24:57 -070096 rtc::CritScope cs(&crit_sect_);
isheriff6b4b5f32016-06-08 00:24:21 -070097 int target_delay_ms = TargetDelayInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +000098
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000099 if (current_delay_ms_ == 0) {
100 // Not initialized, set current delay to target.
101 current_delay_ms_ = target_delay_ms;
102 } else if (target_delay_ms != current_delay_ms_) {
philipel5908c712015-12-21 08:23:20 -0800103 int64_t delay_diff_ms =
104 static_cast<int64_t>(target_delay_ms) - current_delay_ms_;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000105 // Never change the delay with more than 100 ms every second. If we're
106 // changing the delay in too large steps we will get noticeable freezes. By
107 // limiting the change we can increase the delay in smaller steps, which
108 // will be experienced as the video is played in slow motion. When lowering
109 // the delay the video will be played at a faster pace.
110 int64_t max_change_ms = 0;
111 if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) {
112 // wrap
philipel5908c712015-12-21 08:23:20 -0800113 max_change_ms = kDelayMaxChangeMsPerS *
114 (frame_timestamp + (static_cast<int64_t>(1) << 32) -
115 prev_frame_timestamp_) /
116 90000;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000117 } else {
118 max_change_ms = kDelayMaxChangeMsPerS *
philipel5908c712015-12-21 08:23:20 -0800119 (frame_timestamp - prev_frame_timestamp_) / 90000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000120 }
philipelfd5a20f2016-11-15 00:57:57 -0800121
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000122 if (max_change_ms <= 0) {
Åsa Persson8368d1a2018-01-05 12:44:45 +0100123 // Any changes less than 1 ms are truncated and will be postponed.
124 // Negative change will be due to reordering and should be ignored.
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000125 return;
126 }
127 delay_diff_ms = std::max(delay_diff_ms, -max_change_ms);
128 delay_diff_ms = std::min(delay_diff_ms, max_change_ms);
mikhal@webrtc.org6faba6e2013-04-30 15:39:34 +0000129
isheriff6b4b5f32016-06-08 00:24:21 -0700130 current_delay_ms_ = current_delay_ms_ + delay_diff_ms;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000131 }
132 prev_frame_timestamp_ = frame_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000133}
134
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000135void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms,
136 int64_t actual_decode_time_ms) {
kthelgasond701dfd2017-03-27 07:24:57 -0700137 rtc::CritScope cs(&crit_sect_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000138 uint32_t target_delay_ms = TargetDelayInternal();
magjed2943f012016-03-22 05:12:09 -0700139 int64_t delayed_ms =
140 actual_decode_time_ms -
141 (render_time_ms - RequiredDecodeTimeMs() - render_delay_ms_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000142 if (delayed_ms < 0) {
143 return;
144 }
145 if (current_delay_ms_ + delayed_ms <= target_delay_ms) {
isheriff6b4b5f32016-06-08 00:24:21 -0700146 current_delay_ms_ += delayed_ms;
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000147 } else {
148 current_delay_ms_ = target_delay_ms;
149 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000150}
151
Johannes Kronbfd343b2019-07-01 10:07:50 +0200152void VCMTiming::StopDecodeTimer(uint32_t /*time_stamp*/,
Åsa Persson8368d1a2018-01-05 12:44:45 +0100153 int32_t decode_time_ms,
154 int64_t now_ms,
Johannes Kronbfd343b2019-07-01 10:07:50 +0200155 int64_t /*render_time_ms*/) {
156 StopDecodeTimer(decode_time_ms, now_ms);
157}
158
159void VCMTiming::StopDecodeTimer(int32_t decode_time_ms, int64_t now_ms) {
kthelgasond701dfd2017-03-27 07:24:57 -0700160 rtc::CritScope cs(&crit_sect_);
magjed2943f012016-03-22 05:12:09 -0700161 codec_timer_->AddTiming(decode_time_ms, now_ms);
Per327d8ba2015-11-10 14:00:27 +0100162 assert(decode_time_ms >= 0);
asapersson@webrtc.orgf2447602014-12-09 14:13:26 +0000163 ++num_decoded_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000164}
165
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000166void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) {
kthelgasond701dfd2017-03-27 07:24:57 -0700167 rtc::CritScope cs(&crit_sect_);
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000168 ts_extrapolator_->Update(now_ms, time_stamp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000169}
170
philipel5908c712015-12-21 08:23:20 -0800171int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp,
172 int64_t now_ms) const {
kthelgasond701dfd2017-03-27 07:24:57 -0700173 rtc::CritScope cs(&crit_sect_);
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200174 return RenderTimeMsInternal(frame_timestamp, now_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000175}
176
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000177int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp,
178 int64_t now_ms) const {
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200179 if (min_playout_delay_ms_ == 0 && max_playout_delay_ms_ == 0) {
180 // Render as soon as possible.
181 return 0;
182 }
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000183 int64_t estimated_complete_time_ms =
philipel5908c712015-12-21 08:23:20 -0800184 ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000185 if (estimated_complete_time_ms == -1) {
186 estimated_complete_time_ms = now_ms;
187 }
mikhal@webrtc.org6faba6e2013-04-30 15:39:34 +0000188
isheriff6b4b5f32016-06-08 00:24:21 -0700189 // Make sure the actual delay stays in the range of |min_playout_delay_ms_|
190 // and |max_playout_delay_ms_|.
191 int actual_delay = std::max(current_delay_ms_, min_playout_delay_ms_);
192 actual_delay = std::min(actual_delay, max_playout_delay_ms_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000193 return estimated_complete_time_ms + actual_delay;
niklase@google.com470e71d2011-07-07 08:21:25 +0000194}
195
isheriff6b4b5f32016-06-08 00:24:21 -0700196int VCMTiming::RequiredDecodeTimeMs() const {
197 const int decode_time_ms = codec_timer_->RequiredDecodeTimeMs();
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000198 assert(decode_time_ms >= 0);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000199 return decode_time_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000200}
201
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100202int64_t VCMTiming::MaxWaitingTime(int64_t render_time_ms,
203 int64_t now_ms) const {
kthelgasond701dfd2017-03-27 07:24:57 -0700204 rtc::CritScope cs(&crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000205
philipel5908c712015-12-21 08:23:20 -0800206 const int64_t max_wait_time_ms =
magjed2943f012016-03-22 05:12:09 -0700207 render_time_ms - now_ms - RequiredDecodeTimeMs() - render_delay_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000208
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100209 return max_wait_time_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000210}
211
isheriff6b4b5f32016-06-08 00:24:21 -0700212int VCMTiming::TargetVideoDelay() const {
kthelgasond701dfd2017-03-27 07:24:57 -0700213 rtc::CritScope cs(&crit_sect_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000214 return TargetDelayInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000215}
216
isheriff6b4b5f32016-06-08 00:24:21 -0700217int VCMTiming::TargetDelayInternal() const {
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +0000218 return std::max(min_playout_delay_ms_,
isheriff6b4b5f32016-06-08 00:24:21 -0700219 jitter_delay_ms_ + RequiredDecodeTimeMs() + render_delay_ms_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000220}
221
Johannes Kronbfd343b2019-07-01 10:07:50 +0200222bool VCMTiming::GetTimings(int* max_decode_ms,
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000223 int* current_delay_ms,
224 int* target_delay_ms,
225 int* jitter_buffer_ms,
226 int* min_playout_delay_ms,
227 int* render_delay_ms) const {
kthelgasond701dfd2017-03-27 07:24:57 -0700228 rtc::CritScope cs(&crit_sect_);
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