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> | 13 #include <algorithm> |
14 | 14 |
15 #include "webrtc/modules/video_coding/internal_defines.h" | 15 #include "webrtc/modules/video_coding/internal_defines.h" |
16 #include "webrtc/modules/video_coding/jitter_buffer_common.h" | 16 #include "webrtc/modules/video_coding/jitter_buffer_common.h" |
17 #include "webrtc/system_wrappers/include/clock.h" | 17 #include "webrtc/system_wrappers/include/clock.h" |
18 #include "webrtc/system_wrappers/include/metrics.h" | 18 #include "webrtc/system_wrappers/include/metrics.h" |
19 #include "webrtc/system_wrappers/include/timestamp_extrapolator.h" | 19 #include "webrtc/system_wrappers/include/timestamp_extrapolator.h" |
20 | 20 |
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 : clock_(clock), |
25 clock_(clock), | 25 master_(false), |
26 master_(false), | 26 ts_extrapolator_(), |
27 ts_extrapolator_(), | 27 codec_timer_(new VCMCodecTimer()), |
28 codec_timer_(new VCMCodecTimer()), | 28 render_delay_ms_(kDefaultRenderDelayMs), |
29 render_delay_ms_(kDefaultRenderDelayMs), | 29 min_playout_delay_ms_(0), |
30 min_playout_delay_ms_(0), | 30 max_playout_delay_ms_(10000), |
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) { | |
40 if (master_timing == NULL) { | 39 if (master_timing == NULL) { |
41 master_ = true; | 40 master_ = true; |
42 ts_extrapolator_ = new TimestampExtrapolator(clock_->TimeInMilliseconds()); | 41 ts_extrapolator_ = new TimestampExtrapolator(clock_->TimeInMilliseconds()); |
43 } else { | 42 } else { |
44 ts_extrapolator_ = master_timing->ts_extrapolator_; | 43 ts_extrapolator_ = master_timing->ts_extrapolator_; |
45 } | 44 } |
46 } | 45 } |
47 | 46 |
48 VCMTiming::~VCMTiming() { | 47 VCMTiming::~VCMTiming() { |
49 UpdateHistograms(); | 48 UpdateHistograms(); |
50 if (master_) { | 49 if (master_) { |
51 delete ts_extrapolator_; | 50 delete ts_extrapolator_; |
52 } | 51 } |
53 delete crit_sect_; | |
54 } | 52 } |
55 | 53 |
56 void VCMTiming::UpdateHistograms() const { | 54 void VCMTiming::UpdateHistograms() const { |
57 CriticalSectionScoped cs(crit_sect_); | 55 rtc::CritScope cs(&crit_sect_); |
58 if (num_decoded_frames_ == 0) { | 56 if (num_decoded_frames_ == 0) { |
59 return; | 57 return; |
60 } | 58 } |
61 int64_t elapsed_sec = | 59 int64_t elapsed_sec = |
62 (clock_->TimeInMilliseconds() - first_decoded_frame_ms_) / 1000; | 60 (clock_->TimeInMilliseconds() - first_decoded_frame_ms_) / 1000; |
63 if (elapsed_sec < metrics::kMinRunTimeInSeconds) { | 61 if (elapsed_sec < metrics::kMinRunTimeInSeconds) { |
64 return; | 62 return; |
65 } | 63 } |
66 RTC_HISTOGRAM_COUNTS_100( | 64 RTC_HISTOGRAM_COUNTS_100( |
67 "WebRTC.Video.DecodedFramesPerSecond", | 65 "WebRTC.Video.DecodedFramesPerSecond", |
68 static_cast<int>((num_decoded_frames_ / elapsed_sec) + 0.5f)); | 66 static_cast<int>((num_decoded_frames_ / elapsed_sec) + 0.5f)); |
69 RTC_HISTOGRAM_PERCENTAGE( | 67 RTC_HISTOGRAM_PERCENTAGE( |
70 "WebRTC.Video.DelayedFramesToRenderer", | 68 "WebRTC.Video.DelayedFramesToRenderer", |
71 num_delayed_decoded_frames_ * 100 / num_decoded_frames_); | 69 num_delayed_decoded_frames_ * 100 / num_decoded_frames_); |
72 if (num_delayed_decoded_frames_ > 0) { | 70 if (num_delayed_decoded_frames_ > 0) { |
73 RTC_HISTOGRAM_COUNTS_1000( | 71 RTC_HISTOGRAM_COUNTS_1000( |
74 "WebRTC.Video.DelayedFramesToRenderer_AvgDelayInMs", | 72 "WebRTC.Video.DelayedFramesToRenderer_AvgDelayInMs", |
75 sum_missed_render_deadline_ms_ / num_delayed_decoded_frames_); | 73 sum_missed_render_deadline_ms_ / num_delayed_decoded_frames_); |
76 } | 74 } |
77 } | 75 } |
78 | 76 |
79 void VCMTiming::Reset() { | 77 void VCMTiming::Reset() { |
80 CriticalSectionScoped cs(crit_sect_); | 78 rtc::CritScope cs(&crit_sect_); |
81 ts_extrapolator_->Reset(clock_->TimeInMilliseconds()); | 79 ts_extrapolator_->Reset(clock_->TimeInMilliseconds()); |
82 codec_timer_.reset(new VCMCodecTimer()); | 80 codec_timer_.reset(new VCMCodecTimer()); |
83 render_delay_ms_ = kDefaultRenderDelayMs; | 81 render_delay_ms_ = kDefaultRenderDelayMs; |
84 min_playout_delay_ms_ = 0; | 82 min_playout_delay_ms_ = 0; |
85 jitter_delay_ms_ = 0; | 83 jitter_delay_ms_ = 0; |
86 current_delay_ms_ = 0; | 84 current_delay_ms_ = 0; |
87 prev_frame_timestamp_ = 0; | 85 prev_frame_timestamp_ = 0; |
88 } | 86 } |
89 | 87 |
90 void VCMTiming::ResetDecodeTime() { | 88 void VCMTiming::ResetDecodeTime() { |
91 CriticalSectionScoped lock(crit_sect_); | 89 rtc::CritScope cs(&crit_sect_); |
92 codec_timer_.reset(new VCMCodecTimer()); | 90 codec_timer_.reset(new VCMCodecTimer()); |
93 } | 91 } |
94 | 92 |
95 void VCMTiming::set_render_delay(int render_delay_ms) { | 93 void VCMTiming::set_render_delay(int render_delay_ms) { |
96 CriticalSectionScoped cs(crit_sect_); | 94 rtc::CritScope cs(&crit_sect_); |
97 render_delay_ms_ = render_delay_ms; | 95 render_delay_ms_ = render_delay_ms; |
98 } | 96 } |
99 | 97 |
100 void VCMTiming::set_min_playout_delay(int min_playout_delay_ms) { | 98 void VCMTiming::set_min_playout_delay(int min_playout_delay_ms) { |
101 CriticalSectionScoped cs(crit_sect_); | 99 rtc::CritScope cs(&crit_sect_); |
102 min_playout_delay_ms_ = min_playout_delay_ms; | 100 min_playout_delay_ms_ = min_playout_delay_ms; |
103 } | 101 } |
104 | 102 |
105 int VCMTiming::min_playout_delay() { | 103 int VCMTiming::min_playout_delay() { |
106 CriticalSectionScoped cs(crit_sect_); | 104 rtc::CritScope cs(&crit_sect_); |
107 return min_playout_delay_ms_; | 105 return min_playout_delay_ms_; |
108 } | 106 } |
109 | 107 |
110 void VCMTiming::set_max_playout_delay(int max_playout_delay_ms) { | 108 void VCMTiming::set_max_playout_delay(int max_playout_delay_ms) { |
111 CriticalSectionScoped cs(crit_sect_); | 109 rtc::CritScope cs(&crit_sect_); |
112 max_playout_delay_ms_ = max_playout_delay_ms; | 110 max_playout_delay_ms_ = max_playout_delay_ms; |
113 } | 111 } |
114 | 112 |
115 int VCMTiming::max_playout_delay() { | 113 int VCMTiming::max_playout_delay() { |
116 CriticalSectionScoped cs(crit_sect_); | 114 rtc::CritScope cs(&crit_sect_); |
117 return max_playout_delay_ms_; | 115 return max_playout_delay_ms_; |
118 } | 116 } |
119 | 117 |
120 void VCMTiming::SetJitterDelay(int jitter_delay_ms) { | 118 void VCMTiming::SetJitterDelay(int jitter_delay_ms) { |
121 CriticalSectionScoped cs(crit_sect_); | 119 rtc::CritScope cs(&crit_sect_); |
122 if (jitter_delay_ms != jitter_delay_ms_) { | 120 if (jitter_delay_ms != jitter_delay_ms_) { |
123 jitter_delay_ms_ = jitter_delay_ms; | 121 jitter_delay_ms_ = jitter_delay_ms; |
124 // When in initial state, set current delay to minimum delay. | 122 // When in initial state, set current delay to minimum delay. |
125 if (current_delay_ms_ == 0) { | 123 if (current_delay_ms_ == 0) { |
126 current_delay_ms_ = jitter_delay_ms_; | 124 current_delay_ms_ = jitter_delay_ms_; |
127 } | 125 } |
128 } | 126 } |
129 } | 127 } |
130 | 128 |
131 void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) { | 129 void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) { |
132 CriticalSectionScoped cs(crit_sect_); | 130 rtc::CritScope cs(&crit_sect_); |
133 int target_delay_ms = TargetDelayInternal(); | 131 int target_delay_ms = TargetDelayInternal(); |
134 | 132 |
135 if (current_delay_ms_ == 0) { | 133 if (current_delay_ms_ == 0) { |
136 // Not initialized, set current delay to target. | 134 // Not initialized, set current delay to target. |
137 current_delay_ms_ = target_delay_ms; | 135 current_delay_ms_ = target_delay_ms; |
138 } else if (target_delay_ms != current_delay_ms_) { | 136 } else if (target_delay_ms != current_delay_ms_) { |
139 int64_t delay_diff_ms = | 137 int64_t delay_diff_ms = |
140 static_cast<int64_t>(target_delay_ms) - current_delay_ms_; | 138 static_cast<int64_t>(target_delay_ms) - current_delay_ms_; |
141 // Never change the delay with more than 100 ms every second. If we're | 139 // 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 | 140 // changing the delay in too large steps we will get noticeable freezes. By |
(...skipping 21 matching lines...) Expand all Loading... |
164 delay_diff_ms = std::max(delay_diff_ms, -max_change_ms); | 162 delay_diff_ms = std::max(delay_diff_ms, -max_change_ms); |
165 delay_diff_ms = std::min(delay_diff_ms, max_change_ms); | 163 delay_diff_ms = std::min(delay_diff_ms, max_change_ms); |
166 | 164 |
167 current_delay_ms_ = current_delay_ms_ + delay_diff_ms; | 165 current_delay_ms_ = current_delay_ms_ + delay_diff_ms; |
168 } | 166 } |
169 prev_frame_timestamp_ = frame_timestamp; | 167 prev_frame_timestamp_ = frame_timestamp; |
170 } | 168 } |
171 | 169 |
172 void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms, | 170 void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms, |
173 int64_t actual_decode_time_ms) { | 171 int64_t actual_decode_time_ms) { |
174 CriticalSectionScoped cs(crit_sect_); | 172 rtc::CritScope cs(&crit_sect_); |
175 uint32_t target_delay_ms = TargetDelayInternal(); | 173 uint32_t target_delay_ms = TargetDelayInternal(); |
176 int64_t delayed_ms = | 174 int64_t delayed_ms = |
177 actual_decode_time_ms - | 175 actual_decode_time_ms - |
178 (render_time_ms - RequiredDecodeTimeMs() - render_delay_ms_); | 176 (render_time_ms - RequiredDecodeTimeMs() - render_delay_ms_); |
179 if (delayed_ms < 0) { | 177 if (delayed_ms < 0) { |
180 return; | 178 return; |
181 } | 179 } |
182 if (current_delay_ms_ + delayed_ms <= target_delay_ms) { | 180 if (current_delay_ms_ + delayed_ms <= target_delay_ms) { |
183 current_delay_ms_ += delayed_ms; | 181 current_delay_ms_ += delayed_ms; |
184 } else { | 182 } else { |
185 current_delay_ms_ = target_delay_ms; | 183 current_delay_ms_ = target_delay_ms; |
186 } | 184 } |
187 } | 185 } |
188 | 186 |
189 int32_t VCMTiming::StopDecodeTimer(uint32_t time_stamp, | 187 int32_t VCMTiming::StopDecodeTimer(uint32_t time_stamp, |
190 int32_t decode_time_ms, | 188 int32_t decode_time_ms, |
191 int64_t now_ms, | 189 int64_t now_ms, |
192 int64_t render_time_ms) { | 190 int64_t render_time_ms) { |
193 CriticalSectionScoped cs(crit_sect_); | 191 rtc::CritScope cs(&crit_sect_); |
194 codec_timer_->AddTiming(decode_time_ms, now_ms); | 192 codec_timer_->AddTiming(decode_time_ms, now_ms); |
195 assert(decode_time_ms >= 0); | 193 assert(decode_time_ms >= 0); |
196 last_decode_ms_ = decode_time_ms; | 194 last_decode_ms_ = decode_time_ms; |
197 | 195 |
198 // Update stats. | 196 // Update stats. |
199 ++num_decoded_frames_; | 197 ++num_decoded_frames_; |
200 if (num_decoded_frames_ == 1) { | 198 if (num_decoded_frames_ == 1) { |
201 first_decoded_frame_ms_ = now_ms; | 199 first_decoded_frame_ms_ = now_ms; |
202 } | 200 } |
203 int time_until_rendering_ms = render_time_ms - render_delay_ms_ - now_ms; | 201 int time_until_rendering_ms = render_time_ms - render_delay_ms_ - now_ms; |
204 if (time_until_rendering_ms < 0) { | 202 if (time_until_rendering_ms < 0) { |
205 sum_missed_render_deadline_ms_ += -time_until_rendering_ms; | 203 sum_missed_render_deadline_ms_ += -time_until_rendering_ms; |
206 ++num_delayed_decoded_frames_; | 204 ++num_delayed_decoded_frames_; |
207 } | 205 } |
208 return 0; | 206 return 0; |
209 } | 207 } |
210 | 208 |
211 void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) { | 209 void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) { |
212 CriticalSectionScoped cs(crit_sect_); | 210 rtc::CritScope cs(&crit_sect_); |
213 ts_extrapolator_->Update(now_ms, time_stamp); | 211 ts_extrapolator_->Update(now_ms, time_stamp); |
214 } | 212 } |
215 | 213 |
216 int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp, | 214 int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp, |
217 int64_t now_ms) const { | 215 int64_t now_ms) const { |
218 CriticalSectionScoped cs(crit_sect_); | 216 rtc::CritScope cs(&crit_sect_); |
219 const int64_t render_time_ms = RenderTimeMsInternal(frame_timestamp, now_ms); | 217 const int64_t render_time_ms = RenderTimeMsInternal(frame_timestamp, now_ms); |
220 return render_time_ms; | 218 return render_time_ms; |
221 } | 219 } |
222 | 220 |
223 int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp, | 221 int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp, |
224 int64_t now_ms) const { | 222 int64_t now_ms) const { |
225 int64_t estimated_complete_time_ms = | 223 int64_t estimated_complete_time_ms = |
226 ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp); | 224 ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp); |
227 if (estimated_complete_time_ms == -1) { | 225 if (estimated_complete_time_ms == -1) { |
228 estimated_complete_time_ms = now_ms; | 226 estimated_complete_time_ms = now_ms; |
(...skipping 13 matching lines...) Expand all Loading... |
242 | 240 |
243 // Must be called from inside a critical section. | 241 // Must be called from inside a critical section. |
244 int VCMTiming::RequiredDecodeTimeMs() const { | 242 int VCMTiming::RequiredDecodeTimeMs() const { |
245 const int decode_time_ms = codec_timer_->RequiredDecodeTimeMs(); | 243 const int decode_time_ms = codec_timer_->RequiredDecodeTimeMs(); |
246 assert(decode_time_ms >= 0); | 244 assert(decode_time_ms >= 0); |
247 return decode_time_ms; | 245 return decode_time_ms; |
248 } | 246 } |
249 | 247 |
250 uint32_t VCMTiming::MaxWaitingTime(int64_t render_time_ms, | 248 uint32_t VCMTiming::MaxWaitingTime(int64_t render_time_ms, |
251 int64_t now_ms) const { | 249 int64_t now_ms) const { |
252 CriticalSectionScoped cs(crit_sect_); | 250 rtc::CritScope cs(&crit_sect_); |
253 | 251 |
254 const int64_t max_wait_time_ms = | 252 const int64_t max_wait_time_ms = |
255 render_time_ms - now_ms - RequiredDecodeTimeMs() - render_delay_ms_; | 253 render_time_ms - now_ms - RequiredDecodeTimeMs() - render_delay_ms_; |
256 | 254 |
257 if (max_wait_time_ms < 0) { | 255 if (max_wait_time_ms < 0) { |
258 return 0; | 256 return 0; |
259 } | 257 } |
260 return static_cast<uint32_t>(max_wait_time_ms); | 258 return static_cast<uint32_t>(max_wait_time_ms); |
261 } | 259 } |
262 | 260 |
263 bool VCMTiming::EnoughTimeToDecode( | 261 bool VCMTiming::EnoughTimeToDecode( |
264 uint32_t available_processing_time_ms) const { | 262 uint32_t available_processing_time_ms) const { |
265 CriticalSectionScoped cs(crit_sect_); | 263 rtc::CritScope cs(&crit_sect_); |
266 int64_t required_decode_time_ms = RequiredDecodeTimeMs(); | 264 int64_t required_decode_time_ms = RequiredDecodeTimeMs(); |
267 if (required_decode_time_ms < 0) { | 265 if (required_decode_time_ms < 0) { |
268 // Haven't decoded any frames yet, try decoding one to get an estimate | 266 // Haven't decoded any frames yet, try decoding one to get an estimate |
269 // of the decode time. | 267 // of the decode time. |
270 return true; | 268 return true; |
271 } else if (required_decode_time_ms == 0) { | 269 } else if (required_decode_time_ms == 0) { |
272 // Decode time is less than 1, set to 1 for now since | 270 // Decode time is less than 1, set to 1 for now since |
273 // we don't have any better precision. Count ticks later? | 271 // we don't have any better precision. Count ticks later? |
274 required_decode_time_ms = 1; | 272 required_decode_time_ms = 1; |
275 } | 273 } |
276 return static_cast<int64_t>(available_processing_time_ms) - | 274 return static_cast<int64_t>(available_processing_time_ms) - |
277 required_decode_time_ms > | 275 required_decode_time_ms > |
278 0; | 276 0; |
279 } | 277 } |
280 | 278 |
281 int VCMTiming::TargetVideoDelay() const { | 279 int VCMTiming::TargetVideoDelay() const { |
282 CriticalSectionScoped cs(crit_sect_); | 280 rtc::CritScope cs(&crit_sect_); |
283 return TargetDelayInternal(); | 281 return TargetDelayInternal(); |
284 } | 282 } |
285 | 283 |
286 int VCMTiming::TargetDelayInternal() const { | 284 int VCMTiming::TargetDelayInternal() const { |
287 return std::max(min_playout_delay_ms_, | 285 return std::max(min_playout_delay_ms_, |
288 jitter_delay_ms_ + RequiredDecodeTimeMs() + render_delay_ms_); | 286 jitter_delay_ms_ + RequiredDecodeTimeMs() + render_delay_ms_); |
289 } | 287 } |
290 | 288 |
291 bool VCMTiming::GetTimings(int* decode_ms, | 289 bool VCMTiming::GetTimings(int* decode_ms, |
292 int* max_decode_ms, | 290 int* max_decode_ms, |
293 int* current_delay_ms, | 291 int* current_delay_ms, |
294 int* target_delay_ms, | 292 int* target_delay_ms, |
295 int* jitter_buffer_ms, | 293 int* jitter_buffer_ms, |
296 int* min_playout_delay_ms, | 294 int* min_playout_delay_ms, |
297 int* render_delay_ms) const { | 295 int* render_delay_ms) const { |
298 CriticalSectionScoped cs(crit_sect_); | 296 rtc::CritScope cs(&crit_sect_); |
299 *decode_ms = last_decode_ms_; | 297 *decode_ms = last_decode_ms_; |
300 *max_decode_ms = RequiredDecodeTimeMs(); | 298 *max_decode_ms = RequiredDecodeTimeMs(); |
301 *current_delay_ms = current_delay_ms_; | 299 *current_delay_ms = current_delay_ms_; |
302 *target_delay_ms = TargetDelayInternal(); | 300 *target_delay_ms = TargetDelayInternal(); |
303 *jitter_buffer_ms = jitter_delay_ms_; | 301 *jitter_buffer_ms = jitter_delay_ms_; |
304 *min_playout_delay_ms = min_playout_delay_ms_; | 302 *min_playout_delay_ms = min_playout_delay_ms_; |
305 *render_delay_ms = render_delay_ms_; | 303 *render_delay_ms = render_delay_ms_; |
306 return (num_decoded_frames_ > 0); | 304 return (num_decoded_frames_ > 0); |
307 } | 305 } |
308 | 306 |
309 } // namespace webrtc | 307 } // namespace webrtc |
OLD | NEW |