| OLD | NEW |
| (Empty) |
| 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 | |
| 11 #include "webrtc/modules/video_coding/main/source/timing.h" | |
| 12 | |
| 13 #include "webrtc/modules/video_coding/main/source/internal_defines.h" | |
| 14 #include "webrtc/modules/video_coding/main/source/jitter_buffer_common.h" | |
| 15 #include "webrtc/system_wrappers/include/clock.h" | |
| 16 #include "webrtc/system_wrappers/include/metrics.h" | |
| 17 #include "webrtc/system_wrappers/include/timestamp_extrapolator.h" | |
| 18 | |
| 19 | |
| 20 namespace webrtc { | |
| 21 | |
| 22 VCMTiming::VCMTiming(Clock* clock, | |
| 23 VCMTiming* master_timing) | |
| 24 : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), | |
| 25 clock_(clock), | |
| 26 master_(false), | |
| 27 ts_extrapolator_(), | |
| 28 codec_timer_(), | |
| 29 render_delay_ms_(kDefaultRenderDelayMs), | |
| 30 min_playout_delay_ms_(0), | |
| 31 jitter_delay_ms_(0), | |
| 32 current_delay_ms_(0), | |
| 33 last_decode_ms_(0), | |
| 34 prev_frame_timestamp_(0), | |
| 35 num_decoded_frames_(0), | |
| 36 num_delayed_decoded_frames_(0), | |
| 37 first_decoded_frame_ms_(-1), | |
| 38 sum_missed_render_deadline_ms_(0) { | |
| 39 if (master_timing == NULL) { | |
| 40 master_ = true; | |
| 41 ts_extrapolator_ = new TimestampExtrapolator(clock_->TimeInMilliseconds()); | |
| 42 } else { | |
| 43 ts_extrapolator_ = master_timing->ts_extrapolator_; | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 VCMTiming::~VCMTiming() { | |
| 48 UpdateHistograms(); | |
| 49 if (master_) { | |
| 50 delete ts_extrapolator_; | |
| 51 } | |
| 52 delete crit_sect_; | |
| 53 } | |
| 54 | |
| 55 void VCMTiming::UpdateHistograms() const { | |
| 56 CriticalSectionScoped cs(crit_sect_); | |
| 57 if (num_decoded_frames_ == 0) { | |
| 58 return; | |
| 59 } | |
| 60 int64_t elapsed_sec = | |
| 61 (clock_->TimeInMilliseconds() - first_decoded_frame_ms_) / 1000; | |
| 62 if (elapsed_sec < metrics::kMinRunTimeInSeconds) { | |
| 63 return; | |
| 64 } | |
| 65 RTC_HISTOGRAM_COUNTS_100("WebRTC.Video.DecodedFramesPerSecond", | |
| 66 static_cast<int>((num_decoded_frames_ / elapsed_sec) + 0.5f)); | |
| 67 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.DelayedFramesToRenderer", | |
| 68 num_delayed_decoded_frames_ * 100 / num_decoded_frames_); | |
| 69 if (num_delayed_decoded_frames_ > 0) { | |
| 70 RTC_HISTOGRAM_COUNTS_1000( | |
| 71 "WebRTC.Video.DelayedFramesToRenderer_AvgDelayInMs", | |
| 72 sum_missed_render_deadline_ms_ / num_delayed_decoded_frames_); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 void VCMTiming::Reset() { | |
| 77 CriticalSectionScoped cs(crit_sect_); | |
| 78 ts_extrapolator_->Reset(clock_->TimeInMilliseconds()); | |
| 79 codec_timer_.Reset(); | |
| 80 render_delay_ms_ = kDefaultRenderDelayMs; | |
| 81 min_playout_delay_ms_ = 0; | |
| 82 jitter_delay_ms_ = 0; | |
| 83 current_delay_ms_ = 0; | |
| 84 prev_frame_timestamp_ = 0; | |
| 85 } | |
| 86 | |
| 87 void VCMTiming::ResetDecodeTime() { | |
| 88 CriticalSectionScoped lock(crit_sect_); | |
| 89 codec_timer_.Reset(); | |
| 90 } | |
| 91 | |
| 92 void VCMTiming::set_render_delay(uint32_t render_delay_ms) { | |
| 93 CriticalSectionScoped cs(crit_sect_); | |
| 94 render_delay_ms_ = render_delay_ms; | |
| 95 } | |
| 96 | |
| 97 void VCMTiming::set_min_playout_delay(uint32_t min_playout_delay_ms) { | |
| 98 CriticalSectionScoped cs(crit_sect_); | |
| 99 min_playout_delay_ms_ = min_playout_delay_ms; | |
| 100 } | |
| 101 | |
| 102 void VCMTiming::SetJitterDelay(uint32_t jitter_delay_ms) { | |
| 103 CriticalSectionScoped cs(crit_sect_); | |
| 104 if (jitter_delay_ms != jitter_delay_ms_) { | |
| 105 jitter_delay_ms_ = jitter_delay_ms; | |
| 106 // When in initial state, set current delay to minimum delay. | |
| 107 if (current_delay_ms_ == 0) { | |
| 108 current_delay_ms_ = jitter_delay_ms_; | |
| 109 } | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) { | |
| 114 CriticalSectionScoped cs(crit_sect_); | |
| 115 uint32_t target_delay_ms = TargetDelayInternal(); | |
| 116 | |
| 117 if (current_delay_ms_ == 0) { | |
| 118 // Not initialized, set current delay to target. | |
| 119 current_delay_ms_ = target_delay_ms; | |
| 120 } else if (target_delay_ms != current_delay_ms_) { | |
| 121 int64_t delay_diff_ms = static_cast<int64_t>(target_delay_ms) - | |
| 122 current_delay_ms_; | |
| 123 // Never change the delay with more than 100 ms every second. If we're | |
| 124 // changing the delay in too large steps we will get noticeable freezes. By | |
| 125 // limiting the change we can increase the delay in smaller steps, which | |
| 126 // will be experienced as the video is played in slow motion. When lowering | |
| 127 // the delay the video will be played at a faster pace. | |
| 128 int64_t max_change_ms = 0; | |
| 129 if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) { | |
| 130 // wrap | |
| 131 max_change_ms = kDelayMaxChangeMsPerS * (frame_timestamp + | |
| 132 (static_cast<int64_t>(1) << 32) - prev_frame_timestamp_) / 90000; | |
| 133 } else { | |
| 134 max_change_ms = kDelayMaxChangeMsPerS * | |
| 135 (frame_timestamp - prev_frame_timestamp_) / 90000; | |
| 136 } | |
| 137 if (max_change_ms <= 0) { | |
| 138 // Any changes less than 1 ms are truncated and | |
| 139 // will be postponed. Negative change will be due | |
| 140 // to reordering and should be ignored. | |
| 141 return; | |
| 142 } | |
| 143 delay_diff_ms = std::max(delay_diff_ms, -max_change_ms); | |
| 144 delay_diff_ms = std::min(delay_diff_ms, max_change_ms); | |
| 145 | |
| 146 current_delay_ms_ = current_delay_ms_ + static_cast<int32_t>(delay_diff_ms); | |
| 147 } | |
| 148 prev_frame_timestamp_ = frame_timestamp; | |
| 149 } | |
| 150 | |
| 151 void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms, | |
| 152 int64_t actual_decode_time_ms) { | |
| 153 CriticalSectionScoped cs(crit_sect_); | |
| 154 uint32_t target_delay_ms = TargetDelayInternal(); | |
| 155 int64_t delayed_ms = actual_decode_time_ms - | |
| 156 (render_time_ms - MaxDecodeTimeMs() - render_delay_ms_); | |
| 157 if (delayed_ms < 0) { | |
| 158 return; | |
| 159 } | |
| 160 if (current_delay_ms_ + delayed_ms <= target_delay_ms) { | |
| 161 current_delay_ms_ += static_cast<uint32_t>(delayed_ms); | |
| 162 } else { | |
| 163 current_delay_ms_ = target_delay_ms; | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 int32_t VCMTiming::StopDecodeTimer(uint32_t time_stamp, | |
| 168 int32_t decode_time_ms, | |
| 169 int64_t now_ms, | |
| 170 int64_t render_time_ms) { | |
| 171 CriticalSectionScoped cs(crit_sect_); | |
| 172 codec_timer_.MaxFilter(decode_time_ms, now_ms); | |
| 173 assert(decode_time_ms >= 0); | |
| 174 last_decode_ms_ = decode_time_ms; | |
| 175 | |
| 176 // Update stats. | |
| 177 ++num_decoded_frames_; | |
| 178 if (num_decoded_frames_ == 1) { | |
| 179 first_decoded_frame_ms_ = now_ms; | |
| 180 } | |
| 181 int time_until_rendering_ms = render_time_ms - render_delay_ms_ - now_ms; | |
| 182 if (time_until_rendering_ms < 0) { | |
| 183 sum_missed_render_deadline_ms_ += -time_until_rendering_ms; | |
| 184 ++num_delayed_decoded_frames_; | |
| 185 } | |
| 186 return 0; | |
| 187 } | |
| 188 | |
| 189 void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) { | |
| 190 CriticalSectionScoped cs(crit_sect_); | |
| 191 ts_extrapolator_->Update(now_ms, time_stamp); | |
| 192 } | |
| 193 | |
| 194 int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp, int64_t now_ms) | |
| 195 const { | |
| 196 CriticalSectionScoped cs(crit_sect_); | |
| 197 const int64_t render_time_ms = RenderTimeMsInternal(frame_timestamp, now_ms); | |
| 198 return render_time_ms; | |
| 199 } | |
| 200 | |
| 201 int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp, | |
| 202 int64_t now_ms) const { | |
| 203 int64_t estimated_complete_time_ms = | |
| 204 ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp); | |
| 205 if (estimated_complete_time_ms == -1) { | |
| 206 estimated_complete_time_ms = now_ms; | |
| 207 } | |
| 208 | |
| 209 // Make sure that we have at least the playout delay. | |
| 210 uint32_t actual_delay = std::max(current_delay_ms_, min_playout_delay_ms_); | |
| 211 return estimated_complete_time_ms + actual_delay; | |
| 212 } | |
| 213 | |
| 214 // Must be called from inside a critical section. | |
| 215 int32_t VCMTiming::MaxDecodeTimeMs(FrameType frame_type /*= kVideoFrameDelta*/) | |
| 216 const { | |
| 217 const int32_t decode_time_ms = codec_timer_.RequiredDecodeTimeMs(frame_type); | |
| 218 assert(decode_time_ms >= 0); | |
| 219 return decode_time_ms; | |
| 220 } | |
| 221 | |
| 222 uint32_t VCMTiming::MaxWaitingTime(int64_t render_time_ms, int64_t now_ms) | |
| 223 const { | |
| 224 CriticalSectionScoped cs(crit_sect_); | |
| 225 | |
| 226 const int64_t max_wait_time_ms = render_time_ms - now_ms - | |
| 227 MaxDecodeTimeMs() - render_delay_ms_; | |
| 228 | |
| 229 if (max_wait_time_ms < 0) { | |
| 230 return 0; | |
| 231 } | |
| 232 return static_cast<uint32_t>(max_wait_time_ms); | |
| 233 } | |
| 234 | |
| 235 bool VCMTiming::EnoughTimeToDecode(uint32_t available_processing_time_ms) | |
| 236 const { | |
| 237 CriticalSectionScoped cs(crit_sect_); | |
| 238 int32_t max_decode_time_ms = MaxDecodeTimeMs(); | |
| 239 if (max_decode_time_ms < 0) { | |
| 240 // Haven't decoded any frames yet, try decoding one to get an estimate | |
| 241 // of the decode time. | |
| 242 return true; | |
| 243 } else if (max_decode_time_ms == 0) { | |
| 244 // Decode time is less than 1, set to 1 for now since | |
| 245 // we don't have any better precision. Count ticks later? | |
| 246 max_decode_time_ms = 1; | |
| 247 } | |
| 248 return static_cast<int32_t>(available_processing_time_ms) - | |
| 249 max_decode_time_ms > 0; | |
| 250 } | |
| 251 | |
| 252 uint32_t VCMTiming::TargetVideoDelay() const { | |
| 253 CriticalSectionScoped cs(crit_sect_); | |
| 254 return TargetDelayInternal(); | |
| 255 } | |
| 256 | |
| 257 uint32_t VCMTiming::TargetDelayInternal() const { | |
| 258 return std::max(min_playout_delay_ms_, | |
| 259 jitter_delay_ms_ + MaxDecodeTimeMs() + render_delay_ms_); | |
| 260 } | |
| 261 | |
| 262 void VCMTiming::GetTimings(int* decode_ms, | |
| 263 int* max_decode_ms, | |
| 264 int* current_delay_ms, | |
| 265 int* target_delay_ms, | |
| 266 int* jitter_buffer_ms, | |
| 267 int* min_playout_delay_ms, | |
| 268 int* render_delay_ms) const { | |
| 269 CriticalSectionScoped cs(crit_sect_); | |
| 270 *decode_ms = last_decode_ms_; | |
| 271 *max_decode_ms = MaxDecodeTimeMs(); | |
| 272 *current_delay_ms = current_delay_ms_; | |
| 273 *target_delay_ms = TargetDelayInternal(); | |
| 274 *jitter_buffer_ms = jitter_delay_ms_; | |
| 275 *min_playout_delay_ms = min_playout_delay_ms_; | |
| 276 *render_delay_ms = render_delay_ms_; | |
| 277 } | |
| 278 | |
| 279 } // namespace webrtc | |
| OLD | NEW |