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 21 matching lines...) Expand all Loading... |
55 void VCMTiming::UpdateHistograms() const { | 55 void VCMTiming::UpdateHistograms() const { |
56 CriticalSectionScoped cs(crit_sect_); | 56 CriticalSectionScoped cs(crit_sect_); |
57 if (num_decoded_frames_ == 0) { | 57 if (num_decoded_frames_ == 0) { |
58 return; | 58 return; |
59 } | 59 } |
60 int64_t elapsed_sec = | 60 int64_t elapsed_sec = |
61 (clock_->TimeInMilliseconds() - first_decoded_frame_ms_) / 1000; | 61 (clock_->TimeInMilliseconds() - first_decoded_frame_ms_) / 1000; |
62 if (elapsed_sec < metrics::kMinRunTimeInSeconds) { | 62 if (elapsed_sec < metrics::kMinRunTimeInSeconds) { |
63 return; | 63 return; |
64 } | 64 } |
65 RTC_HISTOGRAM_COUNTS_100("WebRTC.Video.DecodedFramesPerSecond", | 65 RTC_HISTOGRAM_COUNTS_100( |
| 66 "WebRTC.Video.DecodedFramesPerSecond", |
66 static_cast<int>((num_decoded_frames_ / elapsed_sec) + 0.5f)); | 67 static_cast<int>((num_decoded_frames_ / elapsed_sec) + 0.5f)); |
67 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.DelayedFramesToRenderer", | 68 RTC_HISTOGRAM_PERCENTAGE( |
| 69 "WebRTC.Video.DelayedFramesToRenderer", |
68 num_delayed_decoded_frames_ * 100 / num_decoded_frames_); | 70 num_delayed_decoded_frames_ * 100 / num_decoded_frames_); |
69 if (num_delayed_decoded_frames_ > 0) { | 71 if (num_delayed_decoded_frames_ > 0) { |
70 RTC_HISTOGRAM_COUNTS_1000( | 72 RTC_HISTOGRAM_COUNTS_1000( |
71 "WebRTC.Video.DelayedFramesToRenderer_AvgDelayInMs", | 73 "WebRTC.Video.DelayedFramesToRenderer_AvgDelayInMs", |
72 sum_missed_render_deadline_ms_ / num_delayed_decoded_frames_); | 74 sum_missed_render_deadline_ms_ / num_delayed_decoded_frames_); |
73 } | 75 } |
74 } | 76 } |
75 | 77 |
76 void VCMTiming::Reset() { | 78 void VCMTiming::Reset() { |
77 CriticalSectionScoped cs(crit_sect_); | 79 CriticalSectionScoped cs(crit_sect_); |
78 ts_extrapolator_->Reset(clock_->TimeInMilliseconds()); | 80 ts_extrapolator_->Reset(clock_->TimeInMilliseconds()); |
79 codec_timer_.Reset(); | 81 codec_timer_.Reset(); |
80 render_delay_ms_ = kDefaultRenderDelayMs; | 82 render_delay_ms_ = kDefaultRenderDelayMs; |
81 min_playout_delay_ms_ = 0; | 83 min_playout_delay_ms_ = 0; |
82 jitter_delay_ms_ = 0; | 84 jitter_delay_ms_ = 0; |
(...skipping 28 matching lines...) Expand all Loading... |
111 } | 113 } |
112 | 114 |
113 void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) { | 115 void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) { |
114 CriticalSectionScoped cs(crit_sect_); | 116 CriticalSectionScoped cs(crit_sect_); |
115 uint32_t target_delay_ms = TargetDelayInternal(); | 117 uint32_t target_delay_ms = TargetDelayInternal(); |
116 | 118 |
117 if (current_delay_ms_ == 0) { | 119 if (current_delay_ms_ == 0) { |
118 // Not initialized, set current delay to target. | 120 // Not initialized, set current delay to target. |
119 current_delay_ms_ = target_delay_ms; | 121 current_delay_ms_ = target_delay_ms; |
120 } else if (target_delay_ms != current_delay_ms_) { | 122 } else if (target_delay_ms != current_delay_ms_) { |
121 int64_t delay_diff_ms = static_cast<int64_t>(target_delay_ms) - | 123 int64_t delay_diff_ms = |
122 current_delay_ms_; | 124 static_cast<int64_t>(target_delay_ms) - current_delay_ms_; |
123 // 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 |
124 // 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 |
125 // 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 |
126 // 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 |
127 // the delay the video will be played at a faster pace. | 129 // the delay the video will be played at a faster pace. |
128 int64_t max_change_ms = 0; | 130 int64_t max_change_ms = 0; |
129 if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) { | 131 if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) { |
130 // wrap | 132 // wrap |
131 max_change_ms = kDelayMaxChangeMsPerS * (frame_timestamp + | 133 max_change_ms = kDelayMaxChangeMsPerS * |
132 (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; |
133 } else { | 137 } else { |
134 max_change_ms = kDelayMaxChangeMsPerS * | 138 max_change_ms = kDelayMaxChangeMsPerS * |
135 (frame_timestamp - prev_frame_timestamp_) / 90000; | 139 (frame_timestamp - prev_frame_timestamp_) / 90000; |
136 } | 140 } |
137 if (max_change_ms <= 0) { | 141 if (max_change_ms <= 0) { |
138 // Any changes less than 1 ms are truncated and | 142 // Any changes less than 1 ms are truncated and |
139 // will be postponed. Negative change will be due | 143 // will be postponed. Negative change will be due |
140 // to reordering and should be ignored. | 144 // to reordering and should be ignored. |
141 return; | 145 return; |
142 } | 146 } |
143 delay_diff_ms = std::max(delay_diff_ms, -max_change_ms); | 147 delay_diff_ms = std::max(delay_diff_ms, -max_change_ms); |
144 delay_diff_ms = std::min(delay_diff_ms, max_change_ms); | 148 delay_diff_ms = std::min(delay_diff_ms, max_change_ms); |
145 | 149 |
146 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); |
147 } | 151 } |
148 prev_frame_timestamp_ = frame_timestamp; | 152 prev_frame_timestamp_ = frame_timestamp; |
149 } | 153 } |
150 | 154 |
151 void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms, | 155 void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms, |
152 int64_t actual_decode_time_ms) { | 156 int64_t actual_decode_time_ms) { |
153 CriticalSectionScoped cs(crit_sect_); | 157 CriticalSectionScoped cs(crit_sect_); |
154 uint32_t target_delay_ms = TargetDelayInternal(); | 158 uint32_t target_delay_ms = TargetDelayInternal(); |
155 int64_t delayed_ms = actual_decode_time_ms - | 159 int64_t delayed_ms = actual_decode_time_ms - |
156 (render_time_ms - MaxDecodeTimeMs() - render_delay_ms_); | 160 (render_time_ms - MaxDecodeTimeMs() - render_delay_ms_); |
157 if (delayed_ms < 0) { | 161 if (delayed_ms < 0) { |
158 return; | 162 return; |
159 } | 163 } |
160 if (current_delay_ms_ + delayed_ms <= target_delay_ms) { | 164 if (current_delay_ms_ + delayed_ms <= target_delay_ms) { |
161 current_delay_ms_ += static_cast<uint32_t>(delayed_ms); | 165 current_delay_ms_ += static_cast<uint32_t>(delayed_ms); |
162 } else { | 166 } else { |
163 current_delay_ms_ = target_delay_ms; | 167 current_delay_ms_ = target_delay_ms; |
164 } | 168 } |
165 } | 169 } |
166 | 170 |
(...skipping 17 matching lines...) Expand all Loading... |
184 ++num_delayed_decoded_frames_; | 188 ++num_delayed_decoded_frames_; |
185 } | 189 } |
186 return 0; | 190 return 0; |
187 } | 191 } |
188 | 192 |
189 void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) { | 193 void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) { |
190 CriticalSectionScoped cs(crit_sect_); | 194 CriticalSectionScoped cs(crit_sect_); |
191 ts_extrapolator_->Update(now_ms, time_stamp); | 195 ts_extrapolator_->Update(now_ms, time_stamp); |
192 } | 196 } |
193 | 197 |
194 int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp, int64_t now_ms) | 198 int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp, |
195 const { | 199 int64_t now_ms) const { |
196 CriticalSectionScoped cs(crit_sect_); | 200 CriticalSectionScoped cs(crit_sect_); |
197 const int64_t render_time_ms = RenderTimeMsInternal(frame_timestamp, now_ms); | 201 const int64_t render_time_ms = RenderTimeMsInternal(frame_timestamp, now_ms); |
198 return render_time_ms; | 202 return render_time_ms; |
199 } | 203 } |
200 | 204 |
201 int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp, | 205 int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp, |
202 int64_t now_ms) const { | 206 int64_t now_ms) const { |
203 int64_t estimated_complete_time_ms = | 207 int64_t estimated_complete_time_ms = |
204 ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp); | 208 ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp); |
205 if (estimated_complete_time_ms == -1) { | 209 if (estimated_complete_time_ms == -1) { |
206 estimated_complete_time_ms = now_ms; | 210 estimated_complete_time_ms = now_ms; |
207 } | 211 } |
208 | 212 |
209 // Make sure that we have at least the playout delay. | 213 // Make sure that we have at least the playout delay. |
210 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_); |
211 return estimated_complete_time_ms + actual_delay; | 215 return estimated_complete_time_ms + actual_delay; |
212 } | 216 } |
213 | 217 |
214 // Must be called from inside a critical section. | 218 // Must be called from inside a critical section. |
215 int32_t VCMTiming::MaxDecodeTimeMs(FrameType frame_type /*= kVideoFrameDelta*/) | 219 int32_t VCMTiming::MaxDecodeTimeMs( |
216 const { | 220 FrameType frame_type /*= kVideoFrameDelta*/) const { |
217 const int32_t decode_time_ms = codec_timer_.RequiredDecodeTimeMs(frame_type); | 221 const int32_t decode_time_ms = codec_timer_.RequiredDecodeTimeMs(frame_type); |
218 assert(decode_time_ms >= 0); | 222 assert(decode_time_ms >= 0); |
219 return decode_time_ms; | 223 return decode_time_ms; |
220 } | 224 } |
221 | 225 |
222 uint32_t VCMTiming::MaxWaitingTime(int64_t render_time_ms, int64_t now_ms) | 226 uint32_t VCMTiming::MaxWaitingTime(int64_t render_time_ms, |
223 const { | 227 int64_t now_ms) const { |
224 CriticalSectionScoped cs(crit_sect_); | 228 CriticalSectionScoped cs(crit_sect_); |
225 | 229 |
226 const int64_t max_wait_time_ms = render_time_ms - now_ms - | 230 const int64_t max_wait_time_ms = |
227 MaxDecodeTimeMs() - render_delay_ms_; | 231 render_time_ms - now_ms - MaxDecodeTimeMs() - render_delay_ms_; |
228 | 232 |
229 if (max_wait_time_ms < 0) { | 233 if (max_wait_time_ms < 0) { |
230 return 0; | 234 return 0; |
231 } | 235 } |
232 return static_cast<uint32_t>(max_wait_time_ms); | 236 return static_cast<uint32_t>(max_wait_time_ms); |
233 } | 237 } |
234 | 238 |
235 bool VCMTiming::EnoughTimeToDecode(uint32_t available_processing_time_ms) | 239 bool VCMTiming::EnoughTimeToDecode( |
236 const { | 240 uint32_t available_processing_time_ms) const { |
237 CriticalSectionScoped cs(crit_sect_); | 241 CriticalSectionScoped cs(crit_sect_); |
238 int32_t max_decode_time_ms = MaxDecodeTimeMs(); | 242 int32_t max_decode_time_ms = MaxDecodeTimeMs(); |
239 if (max_decode_time_ms < 0) { | 243 if (max_decode_time_ms < 0) { |
240 // 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 |
241 // of the decode time. | 245 // of the decode time. |
242 return true; | 246 return true; |
243 } else if (max_decode_time_ms == 0) { | 247 } else if (max_decode_time_ms == 0) { |
244 // 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 |
245 // we don't have any better precision. Count ticks later? | 249 // we don't have any better precision. Count ticks later? |
246 max_decode_time_ms = 1; | 250 max_decode_time_ms = 1; |
247 } | 251 } |
248 return static_cast<int32_t>(available_processing_time_ms) - | 252 return static_cast<int32_t>(available_processing_time_ms) - |
249 max_decode_time_ms > 0; | 253 max_decode_time_ms > |
| 254 0; |
250 } | 255 } |
251 | 256 |
252 uint32_t VCMTiming::TargetVideoDelay() const { | 257 uint32_t VCMTiming::TargetVideoDelay() const { |
253 CriticalSectionScoped cs(crit_sect_); | 258 CriticalSectionScoped cs(crit_sect_); |
254 return TargetDelayInternal(); | 259 return TargetDelayInternal(); |
255 } | 260 } |
256 | 261 |
257 uint32_t VCMTiming::TargetDelayInternal() const { | 262 uint32_t VCMTiming::TargetDelayInternal() const { |
258 return std::max(min_playout_delay_ms_, | 263 return std::max(min_playout_delay_ms_, |
259 jitter_delay_ms_ + MaxDecodeTimeMs() + render_delay_ms_); | 264 jitter_delay_ms_ + MaxDecodeTimeMs() + render_delay_ms_); |
260 } | 265 } |
261 | 266 |
262 void VCMTiming::GetTimings(int* decode_ms, | 267 void VCMTiming::GetTimings(int* decode_ms, |
263 int* max_decode_ms, | 268 int* max_decode_ms, |
264 int* current_delay_ms, | 269 int* current_delay_ms, |
265 int* target_delay_ms, | 270 int* target_delay_ms, |
266 int* jitter_buffer_ms, | 271 int* jitter_buffer_ms, |
267 int* min_playout_delay_ms, | 272 int* min_playout_delay_ms, |
268 int* render_delay_ms) const { | 273 int* render_delay_ms) const { |
269 CriticalSectionScoped cs(crit_sect_); | 274 CriticalSectionScoped cs(crit_sect_); |
270 *decode_ms = last_decode_ms_; | 275 *decode_ms = last_decode_ms_; |
271 *max_decode_ms = MaxDecodeTimeMs(); | 276 *max_decode_ms = MaxDecodeTimeMs(); |
272 *current_delay_ms = current_delay_ms_; | 277 *current_delay_ms = current_delay_ms_; |
273 *target_delay_ms = TargetDelayInternal(); | 278 *target_delay_ms = TargetDelayInternal(); |
274 *jitter_buffer_ms = jitter_delay_ms_; | 279 *jitter_buffer_ms = jitter_delay_ms_; |
275 *min_playout_delay_ms = min_playout_delay_ms_; | 280 *min_playout_delay_ms = min_playout_delay_ms_; |
276 *render_delay_ms = render_delay_ms_; | 281 *render_delay_ms = render_delay_ms_; |
277 } | 282 } |
278 | 283 |
279 } // namespace webrtc | 284 } // namespace webrtc |
OLD | NEW |