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 | 1d02d3e | 2016-09-09 22:40:25 -0700 | [diff] [blame] | 66 | RTC_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 | 1d02d3e | 2016-09-09 22:40:25 -0700 | [diff] [blame] | 69 | RTC_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 | 1d02d3e | 2016-09-09 22:40:25 -0700 | [diff] [blame] | 73 | RTC_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 | } |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame^] | 157 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 158 | if (max_change_ms <= 0) { |
| 159 | // Any changes less than 1 ms are truncated and |
| 160 | // will be postponed. Negative change will be due |
| 161 | // to reordering and should be ignored. |
| 162 | return; |
| 163 | } |
| 164 | delay_diff_ms = std::max(delay_diff_ms, -max_change_ms); |
| 165 | delay_diff_ms = std::min(delay_diff_ms, max_change_ms); |
mikhal@webrtc.org | 6faba6e | 2013-04-30 15:39:34 +0000 | [diff] [blame] | 166 | |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 167 | current_delay_ms_ = current_delay_ms_ + delay_diff_ms; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 168 | } |
| 169 | prev_frame_timestamp_ = frame_timestamp; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 170 | } |
| 171 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 172 | void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms, |
| 173 | int64_t actual_decode_time_ms) { |
| 174 | CriticalSectionScoped cs(crit_sect_); |
| 175 | uint32_t target_delay_ms = TargetDelayInternal(); |
magjed | 2943f01 | 2016-03-22 05:12:09 -0700 | [diff] [blame] | 176 | int64_t delayed_ms = |
| 177 | actual_decode_time_ms - |
| 178 | (render_time_ms - RequiredDecodeTimeMs() - render_delay_ms_); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 179 | if (delayed_ms < 0) { |
| 180 | return; |
| 181 | } |
| 182 | if (current_delay_ms_ + delayed_ms <= target_delay_ms) { |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 183 | current_delay_ms_ += delayed_ms; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 184 | } else { |
| 185 | current_delay_ms_ = target_delay_ms; |
| 186 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 187 | } |
| 188 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 189 | int32_t VCMTiming::StopDecodeTimer(uint32_t time_stamp, |
Per | 327d8ba | 2015-11-10 14:00:27 +0100 | [diff] [blame] | 190 | int32_t decode_time_ms, |
asapersson@webrtc.org | f244760 | 2014-12-09 14:13:26 +0000 | [diff] [blame] | 191 | int64_t now_ms, |
| 192 | int64_t render_time_ms) { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 193 | CriticalSectionScoped cs(crit_sect_); |
magjed | 2943f01 | 2016-03-22 05:12:09 -0700 | [diff] [blame] | 194 | codec_timer_->AddTiming(decode_time_ms, now_ms); |
Per | 327d8ba | 2015-11-10 14:00:27 +0100 | [diff] [blame] | 195 | assert(decode_time_ms >= 0); |
| 196 | last_decode_ms_ = decode_time_ms; |
asapersson@webrtc.org | f244760 | 2014-12-09 14:13:26 +0000 | [diff] [blame] | 197 | |
| 198 | // Update stats. |
| 199 | ++num_decoded_frames_; |
| 200 | if (num_decoded_frames_ == 1) { |
| 201 | first_decoded_frame_ms_ = now_ms; |
| 202 | } |
| 203 | int time_until_rendering_ms = render_time_ms - render_delay_ms_ - now_ms; |
| 204 | if (time_until_rendering_ms < 0) { |
| 205 | sum_missed_render_deadline_ms_ += -time_until_rendering_ms; |
| 206 | ++num_delayed_decoded_frames_; |
| 207 | } |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 208 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 209 | } |
| 210 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 211 | void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) { |
| 212 | CriticalSectionScoped cs(crit_sect_); |
stefan@webrtc.org | 34c5da6 | 2014-04-11 14:08:35 +0000 | [diff] [blame] | 213 | ts_extrapolator_->Update(now_ms, time_stamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 214 | } |
| 215 | |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 216 | int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp, |
| 217 | int64_t now_ms) const { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 218 | CriticalSectionScoped cs(crit_sect_); |
| 219 | const int64_t render_time_ms = RenderTimeMsInternal(frame_timestamp, now_ms); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 220 | return render_time_ms; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 221 | } |
| 222 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 223 | int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp, |
| 224 | int64_t now_ms) const { |
| 225 | int64_t estimated_complete_time_ms = |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 226 | ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 227 | if (estimated_complete_time_ms == -1) { |
| 228 | estimated_complete_time_ms = now_ms; |
| 229 | } |
mikhal@webrtc.org | 6faba6e | 2013-04-30 15:39:34 +0000 | [diff] [blame] | 230 | |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 231 | if (min_playout_delay_ms_ == 0 && max_playout_delay_ms_ == 0) { |
| 232 | // Render as soon as possible |
| 233 | return now_ms; |
| 234 | } |
| 235 | |
| 236 | // Make sure the actual delay stays in the range of |min_playout_delay_ms_| |
| 237 | // and |max_playout_delay_ms_|. |
| 238 | int actual_delay = std::max(current_delay_ms_, min_playout_delay_ms_); |
| 239 | actual_delay = std::min(actual_delay, max_playout_delay_ms_); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 240 | return estimated_complete_time_ms + actual_delay; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 241 | } |
| 242 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 243 | // Must be called from inside a critical section. |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 244 | int VCMTiming::RequiredDecodeTimeMs() const { |
| 245 | const int decode_time_ms = codec_timer_->RequiredDecodeTimeMs(); |
stefan@webrtc.org | 34c5da6 | 2014-04-11 14:08:35 +0000 | [diff] [blame] | 246 | assert(decode_time_ms >= 0); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 247 | return decode_time_ms; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 248 | } |
| 249 | |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 250 | uint32_t VCMTiming::MaxWaitingTime(int64_t render_time_ms, |
| 251 | int64_t now_ms) const { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 252 | CriticalSectionScoped cs(crit_sect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 253 | |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 254 | const int64_t max_wait_time_ms = |
magjed | 2943f01 | 2016-03-22 05:12:09 -0700 | [diff] [blame] | 255 | render_time_ms - now_ms - RequiredDecodeTimeMs() - render_delay_ms_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 256 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 257 | if (max_wait_time_ms < 0) { |
| 258 | return 0; |
| 259 | } |
| 260 | return static_cast<uint32_t>(max_wait_time_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 261 | } |
| 262 | |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 263 | bool VCMTiming::EnoughTimeToDecode( |
| 264 | uint32_t available_processing_time_ms) const { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 265 | CriticalSectionScoped cs(crit_sect_); |
magjed | 2943f01 | 2016-03-22 05:12:09 -0700 | [diff] [blame] | 266 | int64_t required_decode_time_ms = RequiredDecodeTimeMs(); |
| 267 | if (required_decode_time_ms < 0) { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 268 | // Haven't decoded any frames yet, try decoding one to get an estimate |
| 269 | // of the decode time. |
| 270 | return true; |
magjed | 2943f01 | 2016-03-22 05:12:09 -0700 | [diff] [blame] | 271 | } else if (required_decode_time_ms == 0) { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 272 | // Decode time is less than 1, set to 1 for now since |
| 273 | // we don't have any better precision. Count ticks later? |
magjed | 2943f01 | 2016-03-22 05:12:09 -0700 | [diff] [blame] | 274 | required_decode_time_ms = 1; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 275 | } |
magjed | 2943f01 | 2016-03-22 05:12:09 -0700 | [diff] [blame] | 276 | return static_cast<int64_t>(available_processing_time_ms) - |
| 277 | required_decode_time_ms > |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 278 | 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 279 | } |
| 280 | |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 281 | int VCMTiming::TargetVideoDelay() const { |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 282 | CriticalSectionScoped cs(crit_sect_); |
| 283 | return TargetDelayInternal(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 284 | } |
| 285 | |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 286 | int VCMTiming::TargetDelayInternal() const { |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 287 | return std::max(min_playout_delay_ms_, |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 288 | jitter_delay_ms_ + RequiredDecodeTimeMs() + render_delay_ms_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 289 | } |
| 290 | |
fischman@webrtc.org | 37bb497 | 2013-10-23 23:59:45 +0000 | [diff] [blame] | 291 | void VCMTiming::GetTimings(int* decode_ms, |
| 292 | int* max_decode_ms, |
| 293 | int* current_delay_ms, |
| 294 | int* target_delay_ms, |
| 295 | int* jitter_buffer_ms, |
| 296 | int* min_playout_delay_ms, |
| 297 | int* render_delay_ms) const { |
| 298 | CriticalSectionScoped cs(crit_sect_); |
| 299 | *decode_ms = last_decode_ms_; |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 300 | *max_decode_ms = RequiredDecodeTimeMs(); |
fischman@webrtc.org | 37bb497 | 2013-10-23 23:59:45 +0000 | [diff] [blame] | 301 | *current_delay_ms = current_delay_ms_; |
| 302 | *target_delay_ms = TargetDelayInternal(); |
| 303 | *jitter_buffer_ms = jitter_delay_ms_; |
| 304 | *min_playout_delay_ms = min_playout_delay_ms_; |
| 305 | *render_delay_ms = render_delay_ms_; |
| 306 | } |
| 307 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 308 | } // namespace webrtc |