| 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 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 namespace webrtc { | 21 namespace webrtc { |
| 22 | 22 |
| 23 VCMTiming::VCMTiming(Clock* clock, VCMTiming* master_timing) | 23 VCMTiming::VCMTiming(Clock* clock, 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_(new VCMCodecTimer()), | 28 codec_timer_(new VCMCodecTimer()), |
| 29 render_delay_ms_(kDefaultRenderDelayMs), | 29 render_delay_ms_(kDefaultRenderDelayMs), |
| 30 min_playout_delay_ms_(0), | 30 min_playout_delay_ms_(0), |
| 31 max_playout_delay_ms_(10000), |
| 31 jitter_delay_ms_(0), | 32 jitter_delay_ms_(0), |
| 32 current_delay_ms_(0), | 33 current_delay_ms_(0), |
| 33 last_decode_ms_(0), | 34 last_decode_ms_(0), |
| 34 prev_frame_timestamp_(0), | 35 prev_frame_timestamp_(0), |
| 35 num_decoded_frames_(0), | 36 num_decoded_frames_(0), |
| 36 num_delayed_decoded_frames_(0), | 37 num_delayed_decoded_frames_(0), |
| 37 first_decoded_frame_ms_(-1), | 38 first_decoded_frame_ms_(-1), |
| 38 sum_missed_render_deadline_ms_(0) { | 39 sum_missed_render_deadline_ms_(0) { |
| 39 if (master_timing == NULL) { | 40 if (master_timing == NULL) { |
| 40 master_ = true; | 41 master_ = true; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 jitter_delay_ms_ = 0; | 85 jitter_delay_ms_ = 0; |
| 85 current_delay_ms_ = 0; | 86 current_delay_ms_ = 0; |
| 86 prev_frame_timestamp_ = 0; | 87 prev_frame_timestamp_ = 0; |
| 87 } | 88 } |
| 88 | 89 |
| 89 void VCMTiming::ResetDecodeTime() { | 90 void VCMTiming::ResetDecodeTime() { |
| 90 CriticalSectionScoped lock(crit_sect_); | 91 CriticalSectionScoped lock(crit_sect_); |
| 91 codec_timer_.reset(new VCMCodecTimer()); | 92 codec_timer_.reset(new VCMCodecTimer()); |
| 92 } | 93 } |
| 93 | 94 |
| 94 void VCMTiming::set_render_delay(uint32_t render_delay_ms) { | 95 void VCMTiming::set_render_delay(int render_delay_ms) { |
| 95 CriticalSectionScoped cs(crit_sect_); | 96 CriticalSectionScoped cs(crit_sect_); |
| 96 render_delay_ms_ = render_delay_ms; | 97 render_delay_ms_ = render_delay_ms; |
| 97 } | 98 } |
| 98 | 99 |
| 99 void VCMTiming::set_min_playout_delay(uint32_t min_playout_delay_ms) { | 100 void VCMTiming::set_min_playout_delay(int min_playout_delay_ms) { |
| 100 CriticalSectionScoped cs(crit_sect_); | 101 CriticalSectionScoped cs(crit_sect_); |
| 101 min_playout_delay_ms_ = min_playout_delay_ms; | 102 min_playout_delay_ms_ = min_playout_delay_ms; |
| 102 } | 103 } |
| 103 | 104 |
| 104 void VCMTiming::SetJitterDelay(uint32_t jitter_delay_ms) { | 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) { |
| 105 CriticalSectionScoped cs(crit_sect_); | 121 CriticalSectionScoped cs(crit_sect_); |
| 106 if (jitter_delay_ms != jitter_delay_ms_) { | 122 if (jitter_delay_ms != jitter_delay_ms_) { |
| 107 jitter_delay_ms_ = jitter_delay_ms; | 123 jitter_delay_ms_ = jitter_delay_ms; |
| 108 // When in initial state, set current delay to minimum delay. | 124 // When in initial state, set current delay to minimum delay. |
| 109 if (current_delay_ms_ == 0) { | 125 if (current_delay_ms_ == 0) { |
| 110 current_delay_ms_ = jitter_delay_ms_; | 126 current_delay_ms_ = jitter_delay_ms_; |
| 111 } | 127 } |
| 112 } | 128 } |
| 113 } | 129 } |
| 114 | 130 |
| 115 void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) { | 131 void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) { |
| 116 CriticalSectionScoped cs(crit_sect_); | 132 CriticalSectionScoped cs(crit_sect_); |
| 117 uint32_t target_delay_ms = TargetDelayInternal(); | 133 int target_delay_ms = TargetDelayInternal(); |
| 118 | 134 |
| 119 if (current_delay_ms_ == 0) { | 135 if (current_delay_ms_ == 0) { |
| 120 // Not initialized, set current delay to target. | 136 // Not initialized, set current delay to target. |
| 121 current_delay_ms_ = target_delay_ms; | 137 current_delay_ms_ = target_delay_ms; |
| 122 } else if (target_delay_ms != current_delay_ms_) { | 138 } else if (target_delay_ms != current_delay_ms_) { |
| 123 int64_t delay_diff_ms = | 139 int64_t delay_diff_ms = |
| 124 static_cast<int64_t>(target_delay_ms) - current_delay_ms_; | 140 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 | 141 // 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 | 142 // 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 | 143 // limiting the change we can increase the delay in smaller steps, which |
| (...skipping 12 matching lines...) Expand all Loading... |
| 140 } | 156 } |
| 141 if (max_change_ms <= 0) { | 157 if (max_change_ms <= 0) { |
| 142 // Any changes less than 1 ms are truncated and | 158 // Any changes less than 1 ms are truncated and |
| 143 // will be postponed. Negative change will be due | 159 // will be postponed. Negative change will be due |
| 144 // to reordering and should be ignored. | 160 // to reordering and should be ignored. |
| 145 return; | 161 return; |
| 146 } | 162 } |
| 147 delay_diff_ms = std::max(delay_diff_ms, -max_change_ms); | 163 delay_diff_ms = std::max(delay_diff_ms, -max_change_ms); |
| 148 delay_diff_ms = std::min(delay_diff_ms, max_change_ms); | 164 delay_diff_ms = std::min(delay_diff_ms, max_change_ms); |
| 149 | 165 |
| 150 current_delay_ms_ = current_delay_ms_ + static_cast<int32_t>(delay_diff_ms); | 166 current_delay_ms_ = current_delay_ms_ + delay_diff_ms; |
| 151 } | 167 } |
| 152 prev_frame_timestamp_ = frame_timestamp; | 168 prev_frame_timestamp_ = frame_timestamp; |
| 153 } | 169 } |
| 154 | 170 |
| 155 void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms, | 171 void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms, |
| 156 int64_t actual_decode_time_ms) { | 172 int64_t actual_decode_time_ms) { |
| 157 CriticalSectionScoped cs(crit_sect_); | 173 CriticalSectionScoped cs(crit_sect_); |
| 158 uint32_t target_delay_ms = TargetDelayInternal(); | 174 uint32_t target_delay_ms = TargetDelayInternal(); |
| 159 int64_t delayed_ms = | 175 int64_t delayed_ms = |
| 160 actual_decode_time_ms - | 176 actual_decode_time_ms - |
| 161 (render_time_ms - RequiredDecodeTimeMs() - render_delay_ms_); | 177 (render_time_ms - RequiredDecodeTimeMs() - render_delay_ms_); |
| 162 if (delayed_ms < 0) { | 178 if (delayed_ms < 0) { |
| 163 return; | 179 return; |
| 164 } | 180 } |
| 165 if (current_delay_ms_ + delayed_ms <= target_delay_ms) { | 181 if (current_delay_ms_ + delayed_ms <= target_delay_ms) { |
| 166 current_delay_ms_ += static_cast<uint32_t>(delayed_ms); | 182 current_delay_ms_ += delayed_ms; |
| 167 } else { | 183 } else { |
| 168 current_delay_ms_ = target_delay_ms; | 184 current_delay_ms_ = target_delay_ms; |
| 169 } | 185 } |
| 170 } | 186 } |
| 171 | 187 |
| 172 int32_t VCMTiming::StopDecodeTimer(uint32_t time_stamp, | 188 int32_t VCMTiming::StopDecodeTimer(uint32_t time_stamp, |
| 173 int32_t decode_time_ms, | 189 int32_t decode_time_ms, |
| 174 int64_t now_ms, | 190 int64_t now_ms, |
| 175 int64_t render_time_ms) { | 191 int64_t render_time_ms) { |
| 176 CriticalSectionScoped cs(crit_sect_); | 192 CriticalSectionScoped cs(crit_sect_); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 204 } | 220 } |
| 205 | 221 |
| 206 int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp, | 222 int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp, |
| 207 int64_t now_ms) const { | 223 int64_t now_ms) const { |
| 208 int64_t estimated_complete_time_ms = | 224 int64_t estimated_complete_time_ms = |
| 209 ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp); | 225 ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp); |
| 210 if (estimated_complete_time_ms == -1) { | 226 if (estimated_complete_time_ms == -1) { |
| 211 estimated_complete_time_ms = now_ms; | 227 estimated_complete_time_ms = now_ms; |
| 212 } | 228 } |
| 213 | 229 |
| 214 // Make sure that we have at least the playout delay. | 230 if (min_playout_delay_ms_ == 0 && max_playout_delay_ms_ == 0) { |
| 215 uint32_t actual_delay = std::max(current_delay_ms_, min_playout_delay_ms_); | 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_); |
| 216 return estimated_complete_time_ms + actual_delay; | 239 return estimated_complete_time_ms + actual_delay; |
| 217 } | 240 } |
| 218 | 241 |
| 219 // Must be called from inside a critical section. | 242 // Must be called from inside a critical section. |
| 220 int64_t VCMTiming::RequiredDecodeTimeMs() const { | 243 int VCMTiming::RequiredDecodeTimeMs() const { |
| 221 const int64_t decode_time_ms = codec_timer_->RequiredDecodeTimeMs(); | 244 const int decode_time_ms = codec_timer_->RequiredDecodeTimeMs(); |
| 222 assert(decode_time_ms >= 0); | 245 assert(decode_time_ms >= 0); |
| 223 return decode_time_ms; | 246 return decode_time_ms; |
| 224 } | 247 } |
| 225 | 248 |
| 226 uint32_t VCMTiming::MaxWaitingTime(int64_t render_time_ms, | 249 uint32_t VCMTiming::MaxWaitingTime(int64_t render_time_ms, |
| 227 int64_t now_ms) const { | 250 int64_t now_ms) const { |
| 228 CriticalSectionScoped cs(crit_sect_); | 251 CriticalSectionScoped cs(crit_sect_); |
| 229 | 252 |
| 230 const int64_t max_wait_time_ms = | 253 const int64_t max_wait_time_ms = |
| 231 render_time_ms - now_ms - RequiredDecodeTimeMs() - render_delay_ms_; | 254 render_time_ms - now_ms - RequiredDecodeTimeMs() - render_delay_ms_; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 247 } else if (required_decode_time_ms == 0) { | 270 } else if (required_decode_time_ms == 0) { |
| 248 // Decode time is less than 1, set to 1 for now since | 271 // Decode time is less than 1, set to 1 for now since |
| 249 // we don't have any better precision. Count ticks later? | 272 // we don't have any better precision. Count ticks later? |
| 250 required_decode_time_ms = 1; | 273 required_decode_time_ms = 1; |
| 251 } | 274 } |
| 252 return static_cast<int64_t>(available_processing_time_ms) - | 275 return static_cast<int64_t>(available_processing_time_ms) - |
| 253 required_decode_time_ms > | 276 required_decode_time_ms > |
| 254 0; | 277 0; |
| 255 } | 278 } |
| 256 | 279 |
| 257 uint32_t VCMTiming::TargetVideoDelay() const { | 280 int VCMTiming::TargetVideoDelay() const { |
| 258 CriticalSectionScoped cs(crit_sect_); | 281 CriticalSectionScoped cs(crit_sect_); |
| 259 return TargetDelayInternal(); | 282 return TargetDelayInternal(); |
| 260 } | 283 } |
| 261 | 284 |
| 262 uint32_t VCMTiming::TargetDelayInternal() const { | 285 int VCMTiming::TargetDelayInternal() const { |
| 263 return std::max(min_playout_delay_ms_, | 286 return std::max(min_playout_delay_ms_, |
| 264 jitter_delay_ms_ + | 287 jitter_delay_ms_ + RequiredDecodeTimeMs() + render_delay_ms_); |
| 265 static_cast<uint32_t>(RequiredDecodeTimeMs()) + | |
| 266 render_delay_ms_); | |
| 267 } | 288 } |
| 268 | 289 |
| 269 void VCMTiming::GetTimings(int* decode_ms, | 290 void VCMTiming::GetTimings(int* decode_ms, |
| 270 int* max_decode_ms, | 291 int* max_decode_ms, |
| 271 int* current_delay_ms, | 292 int* current_delay_ms, |
| 272 int* target_delay_ms, | 293 int* target_delay_ms, |
| 273 int* jitter_buffer_ms, | 294 int* jitter_buffer_ms, |
| 274 int* min_playout_delay_ms, | 295 int* min_playout_delay_ms, |
| 275 int* render_delay_ms) const { | 296 int* render_delay_ms) const { |
| 276 CriticalSectionScoped cs(crit_sect_); | 297 CriticalSectionScoped cs(crit_sect_); |
| 277 *decode_ms = last_decode_ms_; | 298 *decode_ms = last_decode_ms_; |
| 278 *max_decode_ms = static_cast<int>(RequiredDecodeTimeMs()); | 299 *max_decode_ms = RequiredDecodeTimeMs(); |
| 279 *current_delay_ms = current_delay_ms_; | 300 *current_delay_ms = current_delay_ms_; |
| 280 *target_delay_ms = TargetDelayInternal(); | 301 *target_delay_ms = TargetDelayInternal(); |
| 281 *jitter_buffer_ms = jitter_delay_ms_; | 302 *jitter_buffer_ms = jitter_delay_ms_; |
| 282 *min_playout_delay_ms = min_playout_delay_ms_; | 303 *min_playout_delay_ms = min_playout_delay_ms_; |
| 283 *render_delay_ms = render_delay_ms_; | 304 *render_delay_ms = render_delay_ms_; |
| 284 } | 305 } |
| 285 | 306 |
| 286 } // namespace webrtc | 307 } // namespace webrtc |
| OLD | NEW |