| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/video_coding/timing.h" | 11 #include "webrtc/modules/video_coding/timing.h" |
| 12 | 12 |
| 13 #include <algorithm> |
| 14 |
| 13 #include "webrtc/modules/video_coding/internal_defines.h" | 15 #include "webrtc/modules/video_coding/internal_defines.h" |
| 14 #include "webrtc/modules/video_coding/jitter_buffer_common.h" | 16 #include "webrtc/modules/video_coding/jitter_buffer_common.h" |
| 15 #include "webrtc/system_wrappers/include/clock.h" | 17 #include "webrtc/system_wrappers/include/clock.h" |
| 16 #include "webrtc/system_wrappers/include/metrics.h" | 18 #include "webrtc/system_wrappers/include/metrics.h" |
| 17 #include "webrtc/system_wrappers/include/timestamp_extrapolator.h" | 19 #include "webrtc/system_wrappers/include/timestamp_extrapolator.h" |
| 18 | 20 |
| 19 | |
| 20 namespace webrtc { | 21 namespace webrtc { |
| 21 | 22 |
| 22 VCMTiming::VCMTiming(Clock* clock, | 23 VCMTiming::VCMTiming(Clock* clock, VCMTiming* master_timing) |
| 23 VCMTiming* master_timing) | |
| 24 : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), | 24 : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), |
| 25 clock_(clock), | 25 clock_(clock), |
| 26 master_(false), | 26 master_(false), |
| 27 ts_extrapolator_(), | 27 ts_extrapolator_(), |
| 28 codec_timer_(), | 28 codec_timer_(), |
| 29 render_delay_ms_(kDefaultRenderDelayMs), | 29 render_delay_ms_(kDefaultRenderDelayMs), |
| 30 min_playout_delay_ms_(0), | 30 min_playout_delay_ms_(0), |
| 31 jitter_delay_ms_(0), | 31 jitter_delay_ms_(0), |
| 32 current_delay_ms_(0), | 32 current_delay_ms_(0), |
| 33 last_decode_ms_(0), | 33 last_decode_ms_(0), |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 } | 113 } |
| 114 | 114 |
| 115 void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) { | 115 void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) { |
| 116 CriticalSectionScoped cs(crit_sect_); | 116 CriticalSectionScoped cs(crit_sect_); |
| 117 uint32_t target_delay_ms = TargetDelayInternal(); | 117 uint32_t target_delay_ms = TargetDelayInternal(); |
| 118 | 118 |
| 119 if (current_delay_ms_ == 0) { | 119 if (current_delay_ms_ == 0) { |
| 120 // Not initialized, set current delay to target. | 120 // Not initialized, set current delay to target. |
| 121 current_delay_ms_ = target_delay_ms; | 121 current_delay_ms_ = target_delay_ms; |
| 122 } else if (target_delay_ms != current_delay_ms_) { | 122 } else if (target_delay_ms != current_delay_ms_) { |
| 123 int64_t delay_diff_ms = static_cast<int64_t>(target_delay_ms) - | 123 int64_t delay_diff_ms = |
| 124 current_delay_ms_; | 124 static_cast<int64_t>(target_delay_ms) - current_delay_ms_; |
| 125 // Never change the delay with more than 100 ms every second. If we're | 125 // Never change the delay with more than 100 ms every second. If we're |
| 126 // changing the delay in too large steps we will get noticeable freezes. By | 126 // changing the delay in too large steps we will get noticeable freezes. By |
| 127 // limiting the change we can increase the delay in smaller steps, which | 127 // limiting the change we can increase the delay in smaller steps, which |
| 128 // will be experienced as the video is played in slow motion. When lowering | 128 // will be experienced as the video is played in slow motion. When lowering |
| 129 // the delay the video will be played at a faster pace. | 129 // the delay the video will be played at a faster pace. |
| 130 int64_t max_change_ms = 0; | 130 int64_t max_change_ms = 0; |
| 131 if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) { | 131 if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) { |
| 132 // wrap | 132 // wrap |
| 133 max_change_ms = kDelayMaxChangeMsPerS * (frame_timestamp + | 133 max_change_ms = kDelayMaxChangeMsPerS * |
| 134 (static_cast<int64_t>(1) << 32) - prev_frame_timestamp_) / 90000; | 134 (frame_timestamp + (static_cast<int64_t>(1) << 32) - |
| 135 prev_frame_timestamp_) / |
| 136 90000; |
| 135 } else { | 137 } else { |
| 136 max_change_ms = kDelayMaxChangeMsPerS * | 138 max_change_ms = kDelayMaxChangeMsPerS * |
| 137 (frame_timestamp - prev_frame_timestamp_) / 90000; | 139 (frame_timestamp - prev_frame_timestamp_) / 90000; |
| 138 } | 140 } |
| 139 if (max_change_ms <= 0) { | 141 if (max_change_ms <= 0) { |
| 140 // Any changes less than 1 ms are truncated and | 142 // Any changes less than 1 ms are truncated and |
| 141 // will be postponed. Negative change will be due | 143 // will be postponed. Negative change will be due |
| 142 // to reordering and should be ignored. | 144 // to reordering and should be ignored. |
| 143 return; | 145 return; |
| 144 } | 146 } |
| 145 delay_diff_ms = std::max(delay_diff_ms, -max_change_ms); | 147 delay_diff_ms = std::max(delay_diff_ms, -max_change_ms); |
| 146 delay_diff_ms = std::min(delay_diff_ms, max_change_ms); | 148 delay_diff_ms = std::min(delay_diff_ms, max_change_ms); |
| 147 | 149 |
| 148 current_delay_ms_ = current_delay_ms_ + static_cast<int32_t>(delay_diff_ms); | 150 current_delay_ms_ = current_delay_ms_ + static_cast<int32_t>(delay_diff_ms); |
| 149 } | 151 } |
| 150 prev_frame_timestamp_ = frame_timestamp; | 152 prev_frame_timestamp_ = frame_timestamp; |
| 151 } | 153 } |
| 152 | 154 |
| 153 void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms, | 155 void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms, |
| 154 int64_t actual_decode_time_ms) { | 156 int64_t actual_decode_time_ms) { |
| 155 CriticalSectionScoped cs(crit_sect_); | 157 CriticalSectionScoped cs(crit_sect_); |
| 156 uint32_t target_delay_ms = TargetDelayInternal(); | 158 uint32_t target_delay_ms = TargetDelayInternal(); |
| 157 int64_t delayed_ms = actual_decode_time_ms - | 159 int64_t delayed_ms = actual_decode_time_ms - |
| 158 (render_time_ms - MaxDecodeTimeMs() - render_delay_ms_); | 160 (render_time_ms - MaxDecodeTimeMs() - render_delay_ms_); |
| 159 if (delayed_ms < 0) { | 161 if (delayed_ms < 0) { |
| 160 return; | 162 return; |
| 161 } | 163 } |
| 162 if (current_delay_ms_ + delayed_ms <= target_delay_ms) { | 164 if (current_delay_ms_ + delayed_ms <= target_delay_ms) { |
| 163 current_delay_ms_ += static_cast<uint32_t>(delayed_ms); | 165 current_delay_ms_ += static_cast<uint32_t>(delayed_ms); |
| 164 } else { | 166 } else { |
| 165 current_delay_ms_ = target_delay_ms; | 167 current_delay_ms_ = target_delay_ms; |
| 166 } | 168 } |
| 167 } | 169 } |
| 168 | 170 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 186 ++num_delayed_decoded_frames_; | 188 ++num_delayed_decoded_frames_; |
| 187 } | 189 } |
| 188 return 0; | 190 return 0; |
| 189 } | 191 } |
| 190 | 192 |
| 191 void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) { | 193 void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) { |
| 192 CriticalSectionScoped cs(crit_sect_); | 194 CriticalSectionScoped cs(crit_sect_); |
| 193 ts_extrapolator_->Update(now_ms, time_stamp); | 195 ts_extrapolator_->Update(now_ms, time_stamp); |
| 194 } | 196 } |
| 195 | 197 |
| 196 int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp, int64_t now_ms) | 198 int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp, |
| 197 const { | 199 int64_t now_ms) const { |
| 198 CriticalSectionScoped cs(crit_sect_); | 200 CriticalSectionScoped cs(crit_sect_); |
| 199 const int64_t render_time_ms = RenderTimeMsInternal(frame_timestamp, now_ms); | 201 const int64_t render_time_ms = RenderTimeMsInternal(frame_timestamp, now_ms); |
| 200 return render_time_ms; | 202 return render_time_ms; |
| 201 } | 203 } |
| 202 | 204 |
| 203 int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp, | 205 int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp, |
| 204 int64_t now_ms) const { | 206 int64_t now_ms) const { |
| 205 int64_t estimated_complete_time_ms = | 207 int64_t estimated_complete_time_ms = |
| 206 ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp); | 208 ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp); |
| 207 if (estimated_complete_time_ms == -1) { | 209 if (estimated_complete_time_ms == -1) { |
| 208 estimated_complete_time_ms = now_ms; | 210 estimated_complete_time_ms = now_ms; |
| 209 } | 211 } |
| 210 | 212 |
| 211 // Make sure that we have at least the playout delay. | 213 // Make sure that we have at least the playout delay. |
| 212 uint32_t actual_delay = std::max(current_delay_ms_, min_playout_delay_ms_); | 214 uint32_t actual_delay = std::max(current_delay_ms_, min_playout_delay_ms_); |
| 213 return estimated_complete_time_ms + actual_delay; | 215 return estimated_complete_time_ms + actual_delay; |
| 214 } | 216 } |
| 215 | 217 |
| 216 // Must be called from inside a critical section. | 218 // Must be called from inside a critical section. |
| 217 int32_t VCMTiming::MaxDecodeTimeMs(FrameType frame_type /*= kVideoFrameDelta*/) | 219 int32_t VCMTiming::MaxDecodeTimeMs( |
| 218 const { | 220 FrameType frame_type /*= kVideoFrameDelta*/) const { |
| 219 const int32_t decode_time_ms = codec_timer_.RequiredDecodeTimeMs(frame_type); | 221 const int32_t decode_time_ms = codec_timer_.RequiredDecodeTimeMs(frame_type); |
| 220 assert(decode_time_ms >= 0); | 222 assert(decode_time_ms >= 0); |
| 221 return decode_time_ms; | 223 return decode_time_ms; |
| 222 } | 224 } |
| 223 | 225 |
| 224 uint32_t VCMTiming::MaxWaitingTime(int64_t render_time_ms, int64_t now_ms) | 226 uint32_t VCMTiming::MaxWaitingTime(int64_t render_time_ms, |
| 225 const { | 227 int64_t now_ms) const { |
| 226 CriticalSectionScoped cs(crit_sect_); | 228 CriticalSectionScoped cs(crit_sect_); |
| 227 | 229 |
| 228 const int64_t max_wait_time_ms = render_time_ms - now_ms - | 230 const int64_t max_wait_time_ms = |
| 229 MaxDecodeTimeMs() - render_delay_ms_; | 231 render_time_ms - now_ms - MaxDecodeTimeMs() - render_delay_ms_; |
| 230 | 232 |
| 231 if (max_wait_time_ms < 0) { | 233 if (max_wait_time_ms < 0) { |
| 232 return 0; | 234 return 0; |
| 233 } | 235 } |
| 234 return static_cast<uint32_t>(max_wait_time_ms); | 236 return static_cast<uint32_t>(max_wait_time_ms); |
| 235 } | 237 } |
| 236 | 238 |
| 237 bool VCMTiming::EnoughTimeToDecode(uint32_t available_processing_time_ms) | 239 bool VCMTiming::EnoughTimeToDecode( |
| 238 const { | 240 uint32_t available_processing_time_ms) const { |
| 239 CriticalSectionScoped cs(crit_sect_); | 241 CriticalSectionScoped cs(crit_sect_); |
| 240 int32_t max_decode_time_ms = MaxDecodeTimeMs(); | 242 int32_t max_decode_time_ms = MaxDecodeTimeMs(); |
| 241 if (max_decode_time_ms < 0) { | 243 if (max_decode_time_ms < 0) { |
| 242 // Haven't decoded any frames yet, try decoding one to get an estimate | 244 // Haven't decoded any frames yet, try decoding one to get an estimate |
| 243 // of the decode time. | 245 // of the decode time. |
| 244 return true; | 246 return true; |
| 245 } else if (max_decode_time_ms == 0) { | 247 } else if (max_decode_time_ms == 0) { |
| 246 // Decode time is less than 1, set to 1 for now since | 248 // Decode time is less than 1, set to 1 for now since |
| 247 // we don't have any better precision. Count ticks later? | 249 // we don't have any better precision. Count ticks later? |
| 248 max_decode_time_ms = 1; | 250 max_decode_time_ms = 1; |
| 249 } | 251 } |
| 250 return static_cast<int32_t>(available_processing_time_ms) - | 252 return static_cast<int32_t>(available_processing_time_ms) - |
| 251 max_decode_time_ms > 0; | 253 max_decode_time_ms > |
| 254 0; |
| 252 } | 255 } |
| 253 | 256 |
| 254 uint32_t VCMTiming::TargetVideoDelay() const { | 257 uint32_t VCMTiming::TargetVideoDelay() const { |
| 255 CriticalSectionScoped cs(crit_sect_); | 258 CriticalSectionScoped cs(crit_sect_); |
| 256 return TargetDelayInternal(); | 259 return TargetDelayInternal(); |
| 257 } | 260 } |
| 258 | 261 |
| 259 uint32_t VCMTiming::TargetDelayInternal() const { | 262 uint32_t VCMTiming::TargetDelayInternal() const { |
| 260 return std::max(min_playout_delay_ms_, | 263 return std::max(min_playout_delay_ms_, |
| 261 jitter_delay_ms_ + MaxDecodeTimeMs() + render_delay_ms_); | 264 jitter_delay_ms_ + MaxDecodeTimeMs() + render_delay_ms_); |
| 262 } | 265 } |
| 263 | 266 |
| 264 void VCMTiming::GetTimings(int* decode_ms, | 267 void VCMTiming::GetTimings(int* decode_ms, |
| 265 int* max_decode_ms, | 268 int* max_decode_ms, |
| 266 int* current_delay_ms, | 269 int* current_delay_ms, |
| 267 int* target_delay_ms, | 270 int* target_delay_ms, |
| 268 int* jitter_buffer_ms, | 271 int* jitter_buffer_ms, |
| 269 int* min_playout_delay_ms, | 272 int* min_playout_delay_ms, |
| 270 int* render_delay_ms) const { | 273 int* render_delay_ms) const { |
| 271 CriticalSectionScoped cs(crit_sect_); | 274 CriticalSectionScoped cs(crit_sect_); |
| 272 *decode_ms = last_decode_ms_; | 275 *decode_ms = last_decode_ms_; |
| 273 *max_decode_ms = MaxDecodeTimeMs(); | 276 *max_decode_ms = MaxDecodeTimeMs(); |
| 274 *current_delay_ms = current_delay_ms_; | 277 *current_delay_ms = current_delay_ms_; |
| 275 *target_delay_ms = TargetDelayInternal(); | 278 *target_delay_ms = TargetDelayInternal(); |
| 276 *jitter_buffer_ms = jitter_delay_ms_; | 279 *jitter_buffer_ms = jitter_delay_ms_; |
| 277 *min_playout_delay_ms = min_playout_delay_ms_; | 280 *min_playout_delay_ms = min_playout_delay_ms_; |
| 278 *render_delay_ms = render_delay_ms_; | 281 *render_delay_ms = render_delay_ms_; |
| 279 } | 282 } |
| 280 | 283 |
| 281 } // namespace webrtc | 284 } // namespace webrtc |
| OLD | NEW |