niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 11 | #include "webrtc/modules/video_coding/timing.h" |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 12 | |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 15 | #include "webrtc/modules/video_coding/internal_defines.h" |
| 16 | #include "webrtc/modules/video_coding/jitter_buffer_common.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 17 | #include "webrtc/system_wrappers/include/clock.h" |
| 18 | #include "webrtc/system_wrappers/include/metrics.h" |
| 19 | #include "webrtc/system_wrappers/include/timestamp_extrapolator.h" |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 20 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | namespace webrtc { |
| 22 | |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 23 | VCMTiming::VCMTiming(Clock* clock, VCMTiming* master_timing) |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 24 | : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 25 | clock_(clock), |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 26 | master_(false), |
| 27 | ts_extrapolator_(), |
magjed | 2943f01 | 2016-03-22 05:12:09 -0700 | [diff] [blame] | 28 | codec_timer_(new VCMCodecTimer()), |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 29 | render_delay_ms_(kDefaultRenderDelayMs), |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 30 | min_playout_delay_ms_(0), |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 31 | max_playout_delay_ms_(10000), |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 32 | jitter_delay_ms_(0), |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 33 | current_delay_ms_(0), |
fischman@webrtc.org | 37bb497 | 2013-10-23 23:59:45 +0000 | [diff] [blame] | 34 | last_decode_ms_(0), |
asapersson@webrtc.org | f244760 | 2014-12-09 14:13:26 +0000 | [diff] [blame] | 35 | prev_frame_timestamp_(0), |
| 36 | num_decoded_frames_(0), |
| 37 | num_delayed_decoded_frames_(0), |
| 38 | first_decoded_frame_ms_(-1), |
| 39 | sum_missed_render_deadline_ms_(0) { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 40 | if (master_timing == NULL) { |
| 41 | master_ = true; |
wu@webrtc.org | 66773a0 | 2014-05-07 17:09:44 +0000 | [diff] [blame] | 42 | ts_extrapolator_ = new TimestampExtrapolator(clock_->TimeInMilliseconds()); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 43 | } else { |
| 44 | ts_extrapolator_ = master_timing->ts_extrapolator_; |
| 45 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | } |
| 47 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 48 | VCMTiming::~VCMTiming() { |
asapersson@webrtc.org | f244760 | 2014-12-09 14:13:26 +0000 | [diff] [blame] | 49 | UpdateHistograms(); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 50 | if (master_) { |
| 51 | delete ts_extrapolator_; |
| 52 | } |
| 53 | delete crit_sect_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | } |
| 55 | |
asapersson@webrtc.org | f244760 | 2014-12-09 14:13:26 +0000 | [diff] [blame] | 56 | void VCMTiming::UpdateHistograms() const { |
| 57 | CriticalSectionScoped cs(crit_sect_); |
| 58 | if (num_decoded_frames_ == 0) { |
| 59 | return; |
| 60 | } |
| 61 | int64_t elapsed_sec = |
| 62 | (clock_->TimeInMilliseconds() - first_decoded_frame_ms_) / 1000; |
| 63 | if (elapsed_sec < metrics::kMinRunTimeInSeconds) { |
| 64 | return; |
| 65 | } |
asapersson | 58d992e | 2016-03-29 02:15:06 -0700 | [diff] [blame] | 66 | RTC_LOGGED_HISTOGRAM_COUNTS_100( |
asapersson | 5380532 | 2015-12-21 01:46:20 -0800 | [diff] [blame] | 67 | "WebRTC.Video.DecodedFramesPerSecond", |
asapersson@webrtc.org | f244760 | 2014-12-09 14:13:26 +0000 | [diff] [blame] | 68 | static_cast<int>((num_decoded_frames_ / elapsed_sec) + 0.5f)); |
asapersson | 58d992e | 2016-03-29 02:15:06 -0700 | [diff] [blame] | 69 | RTC_LOGGED_HISTOGRAM_PERCENTAGE( |
asapersson | 5380532 | 2015-12-21 01:46:20 -0800 | [diff] [blame] | 70 | "WebRTC.Video.DelayedFramesToRenderer", |
asapersson@webrtc.org | f244760 | 2014-12-09 14:13:26 +0000 | [diff] [blame] | 71 | num_delayed_decoded_frames_ * 100 / num_decoded_frames_); |
| 72 | if (num_delayed_decoded_frames_ > 0) { |
asapersson | 58d992e | 2016-03-29 02:15:06 -0700 | [diff] [blame] | 73 | RTC_LOGGED_HISTOGRAM_COUNTS_1000( |
asapersson@webrtc.org | f244760 | 2014-12-09 14:13:26 +0000 | [diff] [blame] | 74 | "WebRTC.Video.DelayedFramesToRenderer_AvgDelayInMs", |
asapersson | 5380532 | 2015-12-21 01:46:20 -0800 | [diff] [blame] | 75 | sum_missed_render_deadline_ms_ / num_delayed_decoded_frames_); |
asapersson@webrtc.org | f244760 | 2014-12-09 14:13:26 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 79 | void VCMTiming::Reset() { |
| 80 | CriticalSectionScoped cs(crit_sect_); |
wu@webrtc.org | ed4cb56 | 2014-05-06 04:50:49 +0000 | [diff] [blame] | 81 | ts_extrapolator_->Reset(clock_->TimeInMilliseconds()); |
magjed | 2943f01 | 2016-03-22 05:12:09 -0700 | [diff] [blame] | 82 | codec_timer_.reset(new VCMCodecTimer()); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 83 | render_delay_ms_ = kDefaultRenderDelayMs; |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 84 | min_playout_delay_ms_ = 0; |
| 85 | jitter_delay_ms_ = 0; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 86 | current_delay_ms_ = 0; |
| 87 | prev_frame_timestamp_ = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | } |
| 89 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 90 | void VCMTiming::ResetDecodeTime() { |
pbos@webrtc.org | 0422100 | 2014-07-10 15:25:37 +0000 | [diff] [blame] | 91 | CriticalSectionScoped lock(crit_sect_); |
magjed | 2943f01 | 2016-03-22 05:12:09 -0700 | [diff] [blame] | 92 | codec_timer_.reset(new VCMCodecTimer()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | } |
| 94 | |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 95 | void VCMTiming::set_render_delay(int render_delay_ms) { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 96 | CriticalSectionScoped cs(crit_sect_); |
| 97 | render_delay_ms_ = render_delay_ms; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | } |
| 99 | |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 100 | void VCMTiming::set_min_playout_delay(int min_playout_delay_ms) { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 101 | CriticalSectionScoped cs(crit_sect_); |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 102 | min_playout_delay_ms_ = min_playout_delay_ms; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 103 | } |
| 104 | |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 105 | int VCMTiming::min_playout_delay() { |
| 106 | CriticalSectionScoped cs(crit_sect_); |
| 107 | return min_playout_delay_ms_; |
| 108 | } |
| 109 | |
| 110 | void VCMTiming::set_max_playout_delay(int max_playout_delay_ms) { |
| 111 | CriticalSectionScoped cs(crit_sect_); |
| 112 | max_playout_delay_ms_ = max_playout_delay_ms; |
| 113 | } |
| 114 | |
| 115 | int VCMTiming::max_playout_delay() { |
| 116 | CriticalSectionScoped cs(crit_sect_); |
| 117 | return max_playout_delay_ms_; |
| 118 | } |
| 119 | |
| 120 | void VCMTiming::SetJitterDelay(int jitter_delay_ms) { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 121 | CriticalSectionScoped cs(crit_sect_); |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 122 | if (jitter_delay_ms != jitter_delay_ms_) { |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 123 | jitter_delay_ms_ = jitter_delay_ms; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 124 | // When in initial state, set current delay to minimum delay. |
| 125 | if (current_delay_ms_ == 0) { |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 126 | current_delay_ms_ = jitter_delay_ms_; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 129 | } |
| 130 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 131 | void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) { |
| 132 | CriticalSectionScoped cs(crit_sect_); |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 133 | int target_delay_ms = TargetDelayInternal(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 134 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 135 | if (current_delay_ms_ == 0) { |
| 136 | // Not initialized, set current delay to target. |
| 137 | current_delay_ms_ = target_delay_ms; |
| 138 | } else if (target_delay_ms != current_delay_ms_) { |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 139 | int64_t delay_diff_ms = |
| 140 | static_cast<int64_t>(target_delay_ms) - current_delay_ms_; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 141 | // Never change the delay with more than 100 ms every second. If we're |
| 142 | // changing the delay in too large steps we will get noticeable freezes. By |
| 143 | // limiting the change we can increase the delay in smaller steps, which |
| 144 | // will be experienced as the video is played in slow motion. When lowering |
| 145 | // the delay the video will be played at a faster pace. |
| 146 | int64_t max_change_ms = 0; |
| 147 | if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) { |
| 148 | // wrap |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 149 | max_change_ms = kDelayMaxChangeMsPerS * |
| 150 | (frame_timestamp + (static_cast<int64_t>(1) << 32) - |
| 151 | prev_frame_timestamp_) / |
| 152 | 90000; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 153 | } else { |
| 154 | max_change_ms = kDelayMaxChangeMsPerS * |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 155 | (frame_timestamp - prev_frame_timestamp_) / 90000; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 156 | } |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 157 | if (max_change_ms <= 0) { |
| 158 | // Any changes less than 1 ms are truncated and |
| 159 | // will be postponed. Negative change will be due |
| 160 | // to reordering and should be ignored. |
| 161 | return; |
| 162 | } |
| 163 | delay_diff_ms = std::max(delay_diff_ms, -max_change_ms); |
| 164 | delay_diff_ms = std::min(delay_diff_ms, max_change_ms); |
mikhal@webrtc.org | 6faba6e | 2013-04-30 15:39:34 +0000 | [diff] [blame] | 165 | |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 166 | current_delay_ms_ = current_delay_ms_ + delay_diff_ms; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 167 | } |
| 168 | prev_frame_timestamp_ = frame_timestamp; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 169 | } |
| 170 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 171 | void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms, |
| 172 | int64_t actual_decode_time_ms) { |
| 173 | CriticalSectionScoped cs(crit_sect_); |
| 174 | uint32_t target_delay_ms = TargetDelayInternal(); |
magjed | 2943f01 | 2016-03-22 05:12:09 -0700 | [diff] [blame] | 175 | int64_t delayed_ms = |
| 176 | actual_decode_time_ms - |
| 177 | (render_time_ms - RequiredDecodeTimeMs() - render_delay_ms_); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 178 | if (delayed_ms < 0) { |
| 179 | return; |
| 180 | } |
| 181 | if (current_delay_ms_ + delayed_ms <= target_delay_ms) { |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 182 | current_delay_ms_ += delayed_ms; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 183 | } else { |
| 184 | current_delay_ms_ = target_delay_ms; |
| 185 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 186 | } |
| 187 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 188 | int32_t VCMTiming::StopDecodeTimer(uint32_t time_stamp, |
Per | 327d8ba | 2015-11-10 14:00:27 +0100 | [diff] [blame] | 189 | int32_t decode_time_ms, |
asapersson@webrtc.org | f244760 | 2014-12-09 14:13:26 +0000 | [diff] [blame] | 190 | int64_t now_ms, |
| 191 | int64_t render_time_ms) { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 192 | CriticalSectionScoped cs(crit_sect_); |
magjed | 2943f01 | 2016-03-22 05:12:09 -0700 | [diff] [blame] | 193 | codec_timer_->AddTiming(decode_time_ms, now_ms); |
Per | 327d8ba | 2015-11-10 14:00:27 +0100 | [diff] [blame] | 194 | assert(decode_time_ms >= 0); |
| 195 | last_decode_ms_ = decode_time_ms; |
asapersson@webrtc.org | f244760 | 2014-12-09 14:13:26 +0000 | [diff] [blame] | 196 | |
| 197 | // Update stats. |
| 198 | ++num_decoded_frames_; |
| 199 | if (num_decoded_frames_ == 1) { |
| 200 | first_decoded_frame_ms_ = now_ms; |
| 201 | } |
| 202 | int time_until_rendering_ms = render_time_ms - render_delay_ms_ - now_ms; |
| 203 | if (time_until_rendering_ms < 0) { |
| 204 | sum_missed_render_deadline_ms_ += -time_until_rendering_ms; |
| 205 | ++num_delayed_decoded_frames_; |
| 206 | } |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 207 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 208 | } |
| 209 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 210 | void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) { |
| 211 | CriticalSectionScoped cs(crit_sect_); |
stefan@webrtc.org | 34c5da6 | 2014-04-11 14:08:35 +0000 | [diff] [blame] | 212 | ts_extrapolator_->Update(now_ms, time_stamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 213 | } |
| 214 | |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 215 | int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp, |
| 216 | int64_t now_ms) const { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 217 | CriticalSectionScoped cs(crit_sect_); |
| 218 | const int64_t render_time_ms = RenderTimeMsInternal(frame_timestamp, now_ms); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 219 | return render_time_ms; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 220 | } |
| 221 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 222 | int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp, |
| 223 | int64_t now_ms) const { |
| 224 | int64_t estimated_complete_time_ms = |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 225 | ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 226 | if (estimated_complete_time_ms == -1) { |
| 227 | estimated_complete_time_ms = now_ms; |
| 228 | } |
mikhal@webrtc.org | 6faba6e | 2013-04-30 15:39:34 +0000 | [diff] [blame] | 229 | |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 230 | if (min_playout_delay_ms_ == 0 && max_playout_delay_ms_ == 0) { |
| 231 | // Render as soon as possible |
| 232 | return now_ms; |
| 233 | } |
| 234 | |
| 235 | // Make sure the actual delay stays in the range of |min_playout_delay_ms_| |
| 236 | // and |max_playout_delay_ms_|. |
| 237 | int actual_delay = std::max(current_delay_ms_, min_playout_delay_ms_); |
| 238 | actual_delay = std::min(actual_delay, max_playout_delay_ms_); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 239 | return estimated_complete_time_ms + actual_delay; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 240 | } |
| 241 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 242 | // Must be called from inside a critical section. |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 243 | int VCMTiming::RequiredDecodeTimeMs() const { |
| 244 | const int decode_time_ms = codec_timer_->RequiredDecodeTimeMs(); |
stefan@webrtc.org | 34c5da6 | 2014-04-11 14:08:35 +0000 | [diff] [blame] | 245 | assert(decode_time_ms >= 0); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 246 | return decode_time_ms; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 247 | } |
| 248 | |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 249 | uint32_t VCMTiming::MaxWaitingTime(int64_t render_time_ms, |
| 250 | int64_t now_ms) const { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 251 | CriticalSectionScoped cs(crit_sect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 252 | |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 253 | const int64_t max_wait_time_ms = |
magjed | 2943f01 | 2016-03-22 05:12:09 -0700 | [diff] [blame] | 254 | render_time_ms - now_ms - RequiredDecodeTimeMs() - render_delay_ms_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 255 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 256 | if (max_wait_time_ms < 0) { |
| 257 | return 0; |
| 258 | } |
| 259 | return static_cast<uint32_t>(max_wait_time_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 260 | } |
| 261 | |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 262 | bool VCMTiming::EnoughTimeToDecode( |
| 263 | uint32_t available_processing_time_ms) const { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 264 | CriticalSectionScoped cs(crit_sect_); |
magjed | 2943f01 | 2016-03-22 05:12:09 -0700 | [diff] [blame] | 265 | int64_t required_decode_time_ms = RequiredDecodeTimeMs(); |
| 266 | if (required_decode_time_ms < 0) { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 267 | // Haven't decoded any frames yet, try decoding one to get an estimate |
| 268 | // of the decode time. |
| 269 | return true; |
magjed | 2943f01 | 2016-03-22 05:12:09 -0700 | [diff] [blame] | 270 | } else if (required_decode_time_ms == 0) { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 271 | // Decode time is less than 1, set to 1 for now since |
| 272 | // we don't have any better precision. Count ticks later? |
magjed | 2943f01 | 2016-03-22 05:12:09 -0700 | [diff] [blame] | 273 | required_decode_time_ms = 1; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 274 | } |
magjed | 2943f01 | 2016-03-22 05:12:09 -0700 | [diff] [blame] | 275 | return static_cast<int64_t>(available_processing_time_ms) - |
| 276 | required_decode_time_ms > |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 277 | 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 278 | } |
| 279 | |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 280 | int VCMTiming::TargetVideoDelay() const { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 281 | CriticalSectionScoped cs(crit_sect_); |
| 282 | return TargetDelayInternal(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 283 | } |
| 284 | |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 285 | int VCMTiming::TargetDelayInternal() const { |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 286 | return std::max(min_playout_delay_ms_, |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 287 | jitter_delay_ms_ + RequiredDecodeTimeMs() + render_delay_ms_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 288 | } |
| 289 | |
fischman@webrtc.org | 37bb497 | 2013-10-23 23:59:45 +0000 | [diff] [blame] | 290 | void VCMTiming::GetTimings(int* decode_ms, |
| 291 | int* max_decode_ms, |
| 292 | int* current_delay_ms, |
| 293 | int* target_delay_ms, |
| 294 | int* jitter_buffer_ms, |
| 295 | int* min_playout_delay_ms, |
| 296 | int* render_delay_ms) const { |
| 297 | CriticalSectionScoped cs(crit_sect_); |
| 298 | *decode_ms = last_decode_ms_; |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 299 | *max_decode_ms = RequiredDecodeTimeMs(); |
fischman@webrtc.org | 37bb497 | 2013-10-23 23:59:45 +0000 | [diff] [blame] | 300 | *current_delay_ms = current_delay_ms_; |
| 301 | *target_delay_ms = TargetDelayInternal(); |
| 302 | *jitter_buffer_ms = jitter_delay_ms_; |
| 303 | *min_playout_delay_ms = min_playout_delay_ms_; |
| 304 | *render_delay_ms = render_delay_ms_; |
| 305 | } |
| 306 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 307 | } // namespace webrtc |