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/timing.h"
12 
13 #include <algorithm>
14 
15 #include "webrtc/modules/video_coding/internal_defines.h"
16 #include "webrtc/modules/video_coding/jitter_buffer_common.h"
17 #include "webrtc/system_wrappers/include/clock.h"
18 #include "webrtc/system_wrappers/include/metrics.h"
19 #include "webrtc/system_wrappers/include/timestamp_extrapolator.h"
20 
21 namespace webrtc {
22 
VCMTiming(Clock * clock,VCMTiming * master_timing)23 VCMTiming::VCMTiming(Clock* clock, 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 
~VCMTiming()47 VCMTiming::~VCMTiming() {
48   UpdateHistograms();
49   if (master_) {
50     delete ts_extrapolator_;
51   }
52   delete crit_sect_;
53 }
54 
UpdateHistograms() const55 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_SPARSE_100(
66       "WebRTC.Video.DecodedFramesPerSecond",
67       static_cast<int>((num_decoded_frames_ / elapsed_sec) + 0.5f));
68   RTC_HISTOGRAM_PERCENTAGE_SPARSE(
69       "WebRTC.Video.DelayedFramesToRenderer",
70       num_delayed_decoded_frames_ * 100 / num_decoded_frames_);
71   if (num_delayed_decoded_frames_ > 0) {
72     RTC_HISTOGRAM_COUNTS_SPARSE_1000(
73         "WebRTC.Video.DelayedFramesToRenderer_AvgDelayInMs",
74         sum_missed_render_deadline_ms_ / num_delayed_decoded_frames_);
75   }
76 }
77 
Reset()78 void VCMTiming::Reset() {
79   CriticalSectionScoped cs(crit_sect_);
80   ts_extrapolator_->Reset(clock_->TimeInMilliseconds());
81   codec_timer_.Reset();
82   render_delay_ms_ = kDefaultRenderDelayMs;
83   min_playout_delay_ms_ = 0;
84   jitter_delay_ms_ = 0;
85   current_delay_ms_ = 0;
86   prev_frame_timestamp_ = 0;
87 }
88 
ResetDecodeTime()89 void VCMTiming::ResetDecodeTime() {
90   CriticalSectionScoped lock(crit_sect_);
91   codec_timer_.Reset();
92 }
93 
set_render_delay(uint32_t render_delay_ms)94 void VCMTiming::set_render_delay(uint32_t render_delay_ms) {
95   CriticalSectionScoped cs(crit_sect_);
96   render_delay_ms_ = render_delay_ms;
97 }
98 
set_min_playout_delay(uint32_t min_playout_delay_ms)99 void VCMTiming::set_min_playout_delay(uint32_t min_playout_delay_ms) {
100   CriticalSectionScoped cs(crit_sect_);
101   min_playout_delay_ms_ = min_playout_delay_ms;
102 }
103 
SetJitterDelay(uint32_t jitter_delay_ms)104 void VCMTiming::SetJitterDelay(uint32_t jitter_delay_ms) {
105   CriticalSectionScoped cs(crit_sect_);
106   if (jitter_delay_ms != jitter_delay_ms_) {
107     jitter_delay_ms_ = jitter_delay_ms;
108     // When in initial state, set current delay to minimum delay.
109     if (current_delay_ms_ == 0) {
110       current_delay_ms_ = jitter_delay_ms_;
111     }
112   }
113 }
114 
UpdateCurrentDelay(uint32_t frame_timestamp)115 void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) {
116   CriticalSectionScoped cs(crit_sect_);
117   uint32_t target_delay_ms = TargetDelayInternal();
118 
119   if (current_delay_ms_ == 0) {
120     // Not initialized, set current delay to target.
121     current_delay_ms_ = target_delay_ms;
122   } else if (target_delay_ms != current_delay_ms_) {
123     int64_t delay_diff_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
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
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.
130     int64_t max_change_ms = 0;
131     if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) {
132       // wrap
133       max_change_ms = kDelayMaxChangeMsPerS *
134                       (frame_timestamp + (static_cast<int64_t>(1) << 32) -
135                        prev_frame_timestamp_) /
136                       90000;
137     } else {
138       max_change_ms = kDelayMaxChangeMsPerS *
139                       (frame_timestamp - prev_frame_timestamp_) / 90000;
140     }
141     if (max_change_ms <= 0) {
142       // Any changes less than 1 ms are truncated and
143       // will be postponed. Negative change will be due
144       // to reordering and should be ignored.
145       return;
146     }
147     delay_diff_ms = std::max(delay_diff_ms, -max_change_ms);
148     delay_diff_ms = std::min(delay_diff_ms, max_change_ms);
149 
150     current_delay_ms_ = current_delay_ms_ + static_cast<int32_t>(delay_diff_ms);
151   }
152   prev_frame_timestamp_ = frame_timestamp;
153 }
154 
UpdateCurrentDelay(int64_t render_time_ms,int64_t actual_decode_time_ms)155 void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms,
156                                    int64_t actual_decode_time_ms) {
157   CriticalSectionScoped cs(crit_sect_);
158   uint32_t target_delay_ms = TargetDelayInternal();
159   int64_t delayed_ms = actual_decode_time_ms -
160                        (render_time_ms - MaxDecodeTimeMs() - render_delay_ms_);
161   if (delayed_ms < 0) {
162     return;
163   }
164   if (current_delay_ms_ + delayed_ms <= target_delay_ms) {
165     current_delay_ms_ += static_cast<uint32_t>(delayed_ms);
166   } else {
167     current_delay_ms_ = target_delay_ms;
168   }
169 }
170 
StopDecodeTimer(uint32_t time_stamp,int32_t decode_time_ms,int64_t now_ms,int64_t render_time_ms)171 int32_t VCMTiming::StopDecodeTimer(uint32_t time_stamp,
172                                    int32_t decode_time_ms,
173                                    int64_t now_ms,
174                                    int64_t render_time_ms) {
175   CriticalSectionScoped cs(crit_sect_);
176   codec_timer_.MaxFilter(decode_time_ms, now_ms);
177   assert(decode_time_ms >= 0);
178   last_decode_ms_ = decode_time_ms;
179 
180   // Update stats.
181   ++num_decoded_frames_;
182   if (num_decoded_frames_ == 1) {
183     first_decoded_frame_ms_ = now_ms;
184   }
185   int time_until_rendering_ms = render_time_ms - render_delay_ms_ - now_ms;
186   if (time_until_rendering_ms < 0) {
187     sum_missed_render_deadline_ms_ += -time_until_rendering_ms;
188     ++num_delayed_decoded_frames_;
189   }
190   return 0;
191 }
192 
IncomingTimestamp(uint32_t time_stamp,int64_t now_ms)193 void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) {
194   CriticalSectionScoped cs(crit_sect_);
195   ts_extrapolator_->Update(now_ms, time_stamp);
196 }
197 
RenderTimeMs(uint32_t frame_timestamp,int64_t now_ms) const198 int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp,
199                                 int64_t now_ms) const {
200   CriticalSectionScoped cs(crit_sect_);
201   const int64_t render_time_ms = RenderTimeMsInternal(frame_timestamp, now_ms);
202   return render_time_ms;
203 }
204 
RenderTimeMsInternal(uint32_t frame_timestamp,int64_t now_ms) const205 int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp,
206                                         int64_t now_ms) const {
207   int64_t estimated_complete_time_ms =
208       ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp);
209   if (estimated_complete_time_ms == -1) {
210     estimated_complete_time_ms = now_ms;
211   }
212 
213   // Make sure that we have at least the playout delay.
214   uint32_t actual_delay = std::max(current_delay_ms_, min_playout_delay_ms_);
215   return estimated_complete_time_ms + actual_delay;
216 }
217 
218 // Must be called from inside a critical section.
MaxDecodeTimeMs(FrameType frame_type) const219 int32_t VCMTiming::MaxDecodeTimeMs(
220     FrameType frame_type /*= kVideoFrameDelta*/) const {
221   const int32_t decode_time_ms = codec_timer_.RequiredDecodeTimeMs(frame_type);
222   assert(decode_time_ms >= 0);
223   return decode_time_ms;
224 }
225 
MaxWaitingTime(int64_t render_time_ms,int64_t now_ms) const226 uint32_t VCMTiming::MaxWaitingTime(int64_t render_time_ms,
227                                    int64_t now_ms) const {
228   CriticalSectionScoped cs(crit_sect_);
229 
230   const int64_t max_wait_time_ms =
231       render_time_ms - now_ms - MaxDecodeTimeMs() - render_delay_ms_;
232 
233   if (max_wait_time_ms < 0) {
234     return 0;
235   }
236   return static_cast<uint32_t>(max_wait_time_ms);
237 }
238 
EnoughTimeToDecode(uint32_t available_processing_time_ms) const239 bool VCMTiming::EnoughTimeToDecode(
240     uint32_t available_processing_time_ms) const {
241   CriticalSectionScoped cs(crit_sect_);
242   int32_t max_decode_time_ms = MaxDecodeTimeMs();
243   if (max_decode_time_ms < 0) {
244     // Haven't decoded any frames yet, try decoding one to get an estimate
245     // of the decode time.
246     return true;
247   } else if (max_decode_time_ms == 0) {
248     // Decode time is less than 1, set to 1 for now since
249     // we don't have any better precision. Count ticks later?
250     max_decode_time_ms = 1;
251   }
252   return static_cast<int32_t>(available_processing_time_ms) -
253              max_decode_time_ms >
254          0;
255 }
256 
TargetVideoDelay() const257 uint32_t VCMTiming::TargetVideoDelay() const {
258   CriticalSectionScoped cs(crit_sect_);
259   return TargetDelayInternal();
260 }
261 
TargetDelayInternal() const262 uint32_t VCMTiming::TargetDelayInternal() const {
263   return std::max(min_playout_delay_ms_,
264                   jitter_delay_ms_ + MaxDecodeTimeMs() + render_delay_ms_);
265 }
266 
GetTimings(int * decode_ms,int * max_decode_ms,int * current_delay_ms,int * target_delay_ms,int * jitter_buffer_ms,int * min_playout_delay_ms,int * render_delay_ms) const267 void VCMTiming::GetTimings(int* decode_ms,
268                            int* max_decode_ms,
269                            int* current_delay_ms,
270                            int* target_delay_ms,
271                            int* jitter_buffer_ms,
272                            int* min_playout_delay_ms,
273                            int* render_delay_ms) const {
274   CriticalSectionScoped cs(crit_sect_);
275   *decode_ms = last_decode_ms_;
276   *max_decode_ms = MaxDecodeTimeMs();
277   *current_delay_ms = current_delay_ms_;
278   *target_delay_ms = TargetDelayInternal();
279   *jitter_buffer_ms = jitter_delay_ms_;
280   *min_playout_delay_ms = min_playout_delay_ms_;
281   *render_delay_ms = render_delay_ms_;
282 }
283 
284 }  // namespace webrtc
285