blob: 40b8340e30a9833a3c44dba3bfdfccbcff4fc4f9 [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
philipel5908c712015-12-21 08:23:20 -080013#include <algorithm>
14
Karl Wiberg76b7f512018-03-22 15:29:03 +010015#include "rtc_base/time/timestamp_extrapolator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "system_wrappers/include/clock.h"
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +000017
niklase@google.com470e71d2011-07-07 08:21:25 +000018namespace webrtc {
19
philipel5908c712015-12-21 08:23:20 -080020VCMTiming::VCMTiming(Clock* clock, VCMTiming* master_timing)
ilnik2edc6842017-07-06 03:06:50 -070021 : clock_(clock),
22 master_(false),
23 ts_extrapolator_(),
24 codec_timer_(new VCMCodecTimer()),
25 render_delay_ms_(kDefaultRenderDelayMs),
26 min_playout_delay_ms_(0),
27 max_playout_delay_ms_(10000),
28 jitter_delay_ms_(0),
29 current_delay_ms_(0),
30 last_decode_ms_(0),
31 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
Åsa Persson8368d1a2018-01-05 12:44:45 +0100152void VCMTiming::StopDecodeTimer(uint32_t time_stamp,
153 int32_t decode_time_ms,
154 int64_t now_ms,
155 int64_t render_time_ms) {
kthelgasond701dfd2017-03-27 07:24:57 -0700156 rtc::CritScope cs(&crit_sect_);
magjed2943f012016-03-22 05:12:09 -0700157 codec_timer_->AddTiming(decode_time_ms, now_ms);
Per327d8ba2015-11-10 14:00:27 +0100158 assert(decode_time_ms >= 0);
159 last_decode_ms_ = decode_time_ms;
asapersson@webrtc.orgf2447602014-12-09 14:13:26 +0000160 ++num_decoded_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000161}
162
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000163void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) {
kthelgasond701dfd2017-03-27 07:24:57 -0700164 rtc::CritScope cs(&crit_sect_);
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000165 ts_extrapolator_->Update(now_ms, time_stamp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000166}
167
philipel5908c712015-12-21 08:23:20 -0800168int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp,
169 int64_t now_ms) const {
kthelgasond701dfd2017-03-27 07:24:57 -0700170 rtc::CritScope cs(&crit_sect_);
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200171 return RenderTimeMsInternal(frame_timestamp, now_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000172}
173
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000174int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp,
175 int64_t now_ms) const {
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200176 if (min_playout_delay_ms_ == 0 && max_playout_delay_ms_ == 0) {
177 // Render as soon as possible.
178 return 0;
179 }
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000180 int64_t estimated_complete_time_ms =
philipel5908c712015-12-21 08:23:20 -0800181 ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000182 if (estimated_complete_time_ms == -1) {
183 estimated_complete_time_ms = now_ms;
184 }
mikhal@webrtc.org6faba6e2013-04-30 15:39:34 +0000185
isheriff6b4b5f32016-06-08 00:24:21 -0700186 // Make sure the actual delay stays in the range of |min_playout_delay_ms_|
187 // and |max_playout_delay_ms_|.
188 int actual_delay = std::max(current_delay_ms_, min_playout_delay_ms_);
189 actual_delay = std::min(actual_delay, max_playout_delay_ms_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000190 return estimated_complete_time_ms + actual_delay;
niklase@google.com470e71d2011-07-07 08:21:25 +0000191}
192
isheriff6b4b5f32016-06-08 00:24:21 -0700193int VCMTiming::RequiredDecodeTimeMs() const {
194 const int decode_time_ms = codec_timer_->RequiredDecodeTimeMs();
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000195 assert(decode_time_ms >= 0);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000196 return decode_time_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000197}
198
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100199int64_t VCMTiming::MaxWaitingTime(int64_t render_time_ms,
200 int64_t now_ms) const {
kthelgasond701dfd2017-03-27 07:24:57 -0700201 rtc::CritScope cs(&crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000202
philipel5908c712015-12-21 08:23:20 -0800203 const int64_t max_wait_time_ms =
magjed2943f012016-03-22 05:12:09 -0700204 render_time_ms - now_ms - RequiredDecodeTimeMs() - render_delay_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000205
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100206 return max_wait_time_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000207}
208
isheriff6b4b5f32016-06-08 00:24:21 -0700209int VCMTiming::TargetVideoDelay() const {
kthelgasond701dfd2017-03-27 07:24:57 -0700210 rtc::CritScope cs(&crit_sect_);
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000211 return TargetDelayInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000212}
213
isheriff6b4b5f32016-06-08 00:24:21 -0700214int VCMTiming::TargetDelayInternal() const {
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +0000215 return std::max(min_playout_delay_ms_,
isheriff6b4b5f32016-06-08 00:24:21 -0700216 jitter_delay_ms_ + RequiredDecodeTimeMs() + render_delay_ms_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000217}
218
asapersson8d560882016-12-22 01:26:18 -0800219bool VCMTiming::GetTimings(int* decode_ms,
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000220 int* max_decode_ms,
221 int* current_delay_ms,
222 int* target_delay_ms,
223 int* jitter_buffer_ms,
224 int* min_playout_delay_ms,
225 int* render_delay_ms) const {
kthelgasond701dfd2017-03-27 07:24:57 -0700226 rtc::CritScope cs(&crit_sect_);
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000227 *decode_ms = last_decode_ms_;
isheriff6b4b5f32016-06-08 00:24:21 -0700228 *max_decode_ms = RequiredDecodeTimeMs();
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000229 *current_delay_ms = current_delay_ms_;
230 *target_delay_ms = TargetDelayInternal();
231 *jitter_buffer_ms = jitter_delay_ms_;
232 *min_playout_delay_ms = min_playout_delay_ms_;
233 *render_delay_ms = render_delay_ms_;
asapersson8d560882016-12-22 01:26:18 -0800234 return (num_decoded_frames_ > 0);
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000235}
236
ilnik2edc6842017-07-06 03:06:50 -0700237void VCMTiming::SetTimingFrameInfo(const TimingFrameInfo& info) {
238 rtc::CritScope cs(&crit_sect_);
239 timing_frame_info_.emplace(info);
240}
241
242rtc::Optional<TimingFrameInfo> VCMTiming::GetTimingFrameInfo() {
243 rtc::CritScope cs(&crit_sect_);
244 return timing_frame_info_;
245}
246
mikhal@webrtc.org2eaf98b2013-05-21 17:58:43 +0000247} // namespace webrtc