blob: 2a7e85e667792c0e4243907e7aba0dec6e348310 [file] [log] [blame]
philipelbe7a9e52016-05-19 12:19:35 +02001/*
2 * Copyright (c) 2016 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/frame_buffer2.h"
12
13#include <algorithm>
philipele0b2f152016-09-28 10:23:49 +020014#include <cstring>
15#include <queue>
philipelbe7a9e52016-05-19 12:19:35 +020016
17#include "webrtc/base/checks.h"
philipele0b2f152016-09-28 10:23:49 +020018#include "webrtc/base/logging.h"
philipel09d6ef02017-01-26 02:59:33 -080019#include "webrtc/modules/video_coding/include/video_coding_defines.h"
philipelbe7a9e52016-05-19 12:19:35 +020020#include "webrtc/modules/video_coding/jitter_estimator.h"
philipelbe7a9e52016-05-19 12:19:35 +020021#include "webrtc/modules/video_coding/timing.h"
22#include "webrtc/system_wrappers/include/clock.h"
philipel266f0a42016-11-28 08:49:07 -080023#include "webrtc/system_wrappers/include/metrics.h"
philipelbe7a9e52016-05-19 12:19:35 +020024
25namespace webrtc {
26namespace video_coding {
27
28namespace {
philipele0b2f152016-09-28 10:23:49 +020029// Max number of frames the buffer will hold.
30constexpr int kMaxFramesBuffered = 600;
philipelbe7a9e52016-05-19 12:19:35 +020031
philipele0b2f152016-09-28 10:23:49 +020032// Max number of decoded frame info that will be saved.
philipelfd5a20f2016-11-15 00:57:57 -080033constexpr int kMaxFramesHistory = 50;
philipelbe7a9e52016-05-19 12:19:35 +020034} // namespace
35
philipelbe7a9e52016-05-19 12:19:35 +020036FrameBuffer::FrameBuffer(Clock* clock,
37 VCMJitterEstimator* jitter_estimator,
philipel09d6ef02017-01-26 02:59:33 -080038 VCMTiming* timing,
39 VCMReceiveStatisticsCallback* stats_callback)
philipelbe7a9e52016-05-19 12:19:35 +020040 : clock_(clock),
philipele0b2f152016-09-28 10:23:49 +020041 new_countinuous_frame_event_(false, false),
philipelbe7a9e52016-05-19 12:19:35 +020042 jitter_estimator_(jitter_estimator),
43 timing_(timing),
philipel4f6cd6a2016-08-03 10:59:32 +020044 inter_frame_delay_(clock_->TimeInMilliseconds()),
philipele0b2f152016-09-28 10:23:49 +020045 last_decoded_frame_it_(frames_.end()),
46 last_continuous_frame_it_(frames_.end()),
47 num_frames_history_(0),
48 num_frames_buffered_(0),
philipel4f6cd6a2016-08-03 10:59:32 +020049 stopped_(false),
philipel09d6ef02017-01-26 02:59:33 -080050 protection_mode_(kProtectionNack),
51 stats_callback_(stats_callback) {}
philipel266f0a42016-11-28 08:49:07 -080052
philipel09d6ef02017-01-26 02:59:33 -080053FrameBuffer::~FrameBuffer() {}
philipelbe7a9e52016-05-19 12:19:35 +020054
philipel75562822016-09-05 10:57:41 +020055FrameBuffer::ReturnReason FrameBuffer::NextFrame(
56 int64_t max_wait_time_ms,
57 std::unique_ptr<FrameObject>* frame_out) {
philipelbe7a9e52016-05-19 12:19:35 +020058 int64_t latest_return_time = clock_->TimeInMilliseconds() + max_wait_time_ms;
philipel504c47d2016-06-30 17:33:02 +020059 int64_t wait_ms = max_wait_time_ms;
philipele0b2f152016-09-28 10:23:49 +020060 FrameMap::iterator next_frame_it;
61
62 do {
63 int64_t now_ms = clock_->TimeInMilliseconds();
philipel504c47d2016-06-30 17:33:02 +020064 {
65 rtc::CritScope lock(&crit_);
philipele0b2f152016-09-28 10:23:49 +020066 new_countinuous_frame_event_.Reset();
philipel504c47d2016-06-30 17:33:02 +020067 if (stopped_)
philipel75562822016-09-05 10:57:41 +020068 return kStopped;
philipelbe7a9e52016-05-19 12:19:35 +020069
philipel504c47d2016-06-30 17:33:02 +020070 wait_ms = max_wait_time_ms;
philipele0b2f152016-09-28 10:23:49 +020071
72 // Need to hold |crit_| in order to use |frames_|, therefore we
73 // set it here in the loop instead of outside the loop in order to not
74 // acquire the lock unnecesserily.
philipel4f6cd6a2016-08-03 10:59:32 +020075 next_frame_it = frames_.end();
philipelbe7a9e52016-05-19 12:19:35 +020076
philipele0b2f152016-09-28 10:23:49 +020077 // |frame_it| points to the first frame after the
78 // |last_decoded_frame_it_|.
79 auto frame_it = frames_.end();
80 if (last_decoded_frame_it_ == frames_.end()) {
81 frame_it = frames_.begin();
philipelbe7a9e52016-05-19 12:19:35 +020082 } else {
philipele0b2f152016-09-28 10:23:49 +020083 frame_it = last_decoded_frame_it_;
84 ++frame_it;
philipelbe7a9e52016-05-19 12:19:35 +020085 }
philipele0b2f152016-09-28 10:23:49 +020086
87 // |continuous_end_it| points to the first frame after the
88 // |last_continuous_frame_it_|.
89 auto continuous_end_it = last_continuous_frame_it_;
90 if (continuous_end_it != frames_.end())
91 ++continuous_end_it;
92
93 for (; frame_it != continuous_end_it; ++frame_it) {
philipel93e451b2016-10-06 12:25:13 +020094 if (!frame_it->second.continuous ||
95 frame_it->second.num_missing_decodable > 0) {
philipele0b2f152016-09-28 10:23:49 +020096 continue;
philipel93e451b2016-10-06 12:25:13 +020097 }
philipele0b2f152016-09-28 10:23:49 +020098
99 FrameObject* frame = frame_it->second.frame.get();
100 next_frame_it = frame_it;
101 if (frame->RenderTime() == -1)
102 frame->SetRenderTime(timing_->RenderTimeMs(frame->timestamp, now_ms));
103 wait_ms = timing_->MaxWaitingTime(frame->RenderTime(), now_ms);
104
105 // This will cause the frame buffer to prefer high framerate rather
106 // than high resolution in the case of the decoder not decoding fast
107 // enough and the stream has multiple spatial and temporal layers.
108 if (wait_ms == 0)
109 continue;
110
111 break;
112 }
113 } // rtc::Critscope lock(&crit_);
114
115 wait_ms = std::min<int64_t>(wait_ms, latest_return_time - now_ms);
116 wait_ms = std::max<int64_t>(wait_ms, 0);
117 } while (new_countinuous_frame_event_.Wait(wait_ms));
118
119 rtc::CritScope lock(&crit_);
120 if (next_frame_it != frames_.end()) {
121 std::unique_ptr<FrameObject> frame = std::move(next_frame_it->second.frame);
philipele0b2f152016-09-28 10:23:49 +0200122
philipele0754302017-01-25 08:56:23 -0800123 if (!frame->delayed_by_retransmission()) {
124 int64_t frame_delay;
125
126 if (inter_frame_delay_.CalculateDelay(frame->timestamp, &frame_delay,
127 frame->ReceivedTime())) {
128 jitter_estimator_->UpdateEstimate(frame_delay, frame->size());
129 }
130
131 float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0;
132 timing_->SetJitterDelay(jitter_estimator_->GetJitterEstimate(rtt_mult));
133 timing_->UpdateCurrentDelay(frame->RenderTime(),
134 clock_->TimeInMilliseconds());
philipelbe7a9e52016-05-19 12:19:35 +0200135 }
philipele0b2f152016-09-28 10:23:49 +0200136
philipelbe742702016-11-30 01:31:40 -0800137 UpdateJitterDelay();
138
philipele0b2f152016-09-28 10:23:49 +0200139 PropagateDecodability(next_frame_it->second);
140 AdvanceLastDecodedFrame(next_frame_it);
philipelfcc60062017-01-18 05:35:20 -0800141 last_decoded_frame_timestamp_ = frame->timestamp;
philipele0b2f152016-09-28 10:23:49 +0200142 *frame_out = std::move(frame);
143 return kFrameFound;
144 } else {
145 return kTimeout;
philipelbe7a9e52016-05-19 12:19:35 +0200146 }
147}
148
philipel4f6cd6a2016-08-03 10:59:32 +0200149void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) {
150 rtc::CritScope lock(&crit_);
151 protection_mode_ = mode;
152}
153
philipel504c47d2016-06-30 17:33:02 +0200154void FrameBuffer::Start() {
155 rtc::CritScope lock(&crit_);
156 stopped_ = false;
157}
158
159void FrameBuffer::Stop() {
160 rtc::CritScope lock(&crit_);
161 stopped_ = true;
philipele0b2f152016-09-28 10:23:49 +0200162 new_countinuous_frame_event_.Set();
philipel504c47d2016-06-30 17:33:02 +0200163}
164
philipele0b2f152016-09-28 10:23:49 +0200165int FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) {
philipelbe7a9e52016-05-19 12:19:35 +0200166 rtc::CritScope lock(&crit_);
philipel93e451b2016-10-06 12:25:13 +0200167 RTC_DCHECK(frame);
168
philipel09d6ef02017-01-26 02:59:33 -0800169 if (stats_callback_)
170 stats_callback_->OnCompleteFrame(frame->num_references == 0, frame->size());
philipel266f0a42016-11-28 08:49:07 -0800171
philipelbe7a9e52016-05-19 12:19:35 +0200172 FrameKey key(frame->picture_id, frame->spatial_layer);
philipele0b2f152016-09-28 10:23:49 +0200173 int last_continuous_picture_id =
174 last_continuous_frame_it_ == frames_.end()
175 ? -1
176 : last_continuous_frame_it_->first.picture_id;
177
178 if (num_frames_buffered_ >= kMaxFramesBuffered) {
179 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id
180 << ":" << static_cast<int>(key.spatial_layer)
181 << ") could not be inserted due to the frame "
182 << "buffer being full, dropping frame.";
183 return last_continuous_picture_id;
184 }
185
186 if (frame->inter_layer_predicted && frame->spatial_layer == 0) {
187 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id
188 << ":" << static_cast<int>(key.spatial_layer)
189 << ") is marked as inter layer predicted, dropping frame.";
190 return last_continuous_picture_id;
191 }
192
193 if (last_decoded_frame_it_ != frames_.end() &&
194 key < last_decoded_frame_it_->first) {
philipelfcc60062017-01-18 05:35:20 -0800195 if (AheadOf(frame->timestamp, last_decoded_frame_timestamp_) &&
196 frame->num_references == 0) {
197 // If this frame has a newer timestamp but an earlier picture id then we
198 // assume there has been a jump in the picture id due to some encoder
199 // reconfiguration or some other reason. Even though this is not according
200 // to spec we can still continue to decode from this frame if it is a
201 // keyframe.
202 LOG(LS_WARNING) << "A jump in picture id was detected, clearing buffer.";
203 ClearFramesAndHistory();
204 last_continuous_picture_id = -1;
205 } else {
206 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
207 << key.picture_id << ":"
208 << static_cast<int>(key.spatial_layer)
209 << ") inserted after frame ("
210 << last_decoded_frame_it_->first.picture_id << ":"
211 << static_cast<int>(
212 last_decoded_frame_it_->first.spatial_layer)
213 << ") was handed off for decoding, dropping frame.";
214 return last_continuous_picture_id;
215 }
philipele0b2f152016-09-28 10:23:49 +0200216 }
217
218 auto info = frames_.insert(std::make_pair(key, FrameInfo())).first;
219
philipel93e451b2016-10-06 12:25:13 +0200220 if (info->second.frame) {
221 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id
222 << ":" << static_cast<int>(key.spatial_layer)
223 << ") already inserted, dropping frame.";
philipele0b2f152016-09-28 10:23:49 +0200224 return last_continuous_picture_id;
225 }
226
philipel93e451b2016-10-06 12:25:13 +0200227 if (!UpdateFrameInfoWithIncomingFrame(*frame, info))
228 return last_continuous_picture_id;
229
philipele0b2f152016-09-28 10:23:49 +0200230 info->second.frame = std::move(frame);
231 ++num_frames_buffered_;
232
233 if (info->second.num_missing_continuous == 0) {
234 info->second.continuous = true;
235 PropagateContinuity(info);
236 last_continuous_picture_id = last_continuous_frame_it_->first.picture_id;
237
238 // Since we now have new continuous frames there might be a better frame
239 // to return from NextFrame. Signal that thread so that it again can choose
240 // which frame to return.
241 new_countinuous_frame_event_.Set();
242 }
243
244 return last_continuous_picture_id;
philipelbe7a9e52016-05-19 12:19:35 +0200245}
246
philipele0b2f152016-09-28 10:23:49 +0200247void FrameBuffer::PropagateContinuity(FrameMap::iterator start) {
248 RTC_DCHECK(start->second.continuous);
249 if (last_continuous_frame_it_ == frames_.end())
250 last_continuous_frame_it_ = start;
251
252 std::queue<FrameMap::iterator> continuous_frames;
253 continuous_frames.push(start);
254
255 // A simple BFS to traverse continuous frames.
256 while (!continuous_frames.empty()) {
257 auto frame = continuous_frames.front();
258 continuous_frames.pop();
259
260 if (last_continuous_frame_it_->first < frame->first)
261 last_continuous_frame_it_ = frame;
262
263 // Loop through all dependent frames, and if that frame no longer has
264 // any unfulfilled dependencies then that frame is continuous as well.
265 for (size_t d = 0; d < frame->second.num_dependent_frames; ++d) {
266 auto frame_ref = frames_.find(frame->second.dependent_frames[d]);
267 --frame_ref->second.num_missing_continuous;
268
269 if (frame_ref->second.num_missing_continuous == 0) {
270 frame_ref->second.continuous = true;
271 continuous_frames.push(frame_ref);
272 }
273 }
274 }
275}
276
277void FrameBuffer::PropagateDecodability(const FrameInfo& info) {
278 for (size_t d = 0; d < info.num_dependent_frames; ++d) {
279 auto ref_info = frames_.find(info.dependent_frames[d]);
philipel93e451b2016-10-06 12:25:13 +0200280 RTC_DCHECK(ref_info != frames_.end());
philipele0b2f152016-09-28 10:23:49 +0200281 RTC_DCHECK_GT(ref_info->second.num_missing_decodable, 0U);
282 --ref_info->second.num_missing_decodable;
283 }
284}
285
286void FrameBuffer::AdvanceLastDecodedFrame(FrameMap::iterator decoded) {
287 if (last_decoded_frame_it_ == frames_.end()) {
288 last_decoded_frame_it_ = frames_.begin();
289 } else {
290 RTC_DCHECK(last_decoded_frame_it_->first < decoded->first);
291 ++last_decoded_frame_it_;
292 }
293 --num_frames_buffered_;
294 ++num_frames_history_;
295
296 // First, delete non-decoded frames from the history.
297 while (last_decoded_frame_it_ != decoded) {
298 if (last_decoded_frame_it_->second.frame)
299 --num_frames_buffered_;
300 last_decoded_frame_it_ = frames_.erase(last_decoded_frame_it_);
philipelbe7a9e52016-05-19 12:19:35 +0200301 }
302
philipele0b2f152016-09-28 10:23:49 +0200303 // Then remove old history if we have too much history saved.
304 if (num_frames_history_ > kMaxFramesHistory) {
305 frames_.erase(frames_.begin());
306 --num_frames_history_;
307 }
308}
309
310bool FrameBuffer::UpdateFrameInfoWithIncomingFrame(const FrameObject& frame,
311 FrameMap::iterator info) {
312 FrameKey key(frame.picture_id, frame.spatial_layer);
313 info->second.num_missing_continuous = frame.num_references;
314 info->second.num_missing_decodable = frame.num_references;
315
316 RTC_DCHECK(last_decoded_frame_it_ == frames_.end() ||
317 last_decoded_frame_it_->first < info->first);
318
319 // Check how many dependencies that have already been fulfilled.
320 for (size_t i = 0; i < frame.num_references; ++i) {
321 FrameKey ref_key(frame.references[i], frame.spatial_layer);
322 auto ref_info = frames_.find(ref_key);
323
324 // Does |frame| depend on a frame earlier than the last decoded frame?
325 if (last_decoded_frame_it_ != frames_.end() &&
326 ref_key <= last_decoded_frame_it_->first) {
327 if (ref_info == frames_.end()) {
328 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
329 << key.picture_id << ":"
330 << static_cast<int>(key.spatial_layer)
331 << " depends on a non-decoded frame more previous than "
332 << "the last decoded frame, dropping frame.";
333 return false;
334 }
335
336 --info->second.num_missing_continuous;
337 --info->second.num_missing_decodable;
338 } else {
339 if (ref_info == frames_.end())
340 ref_info = frames_.insert(std::make_pair(ref_key, FrameInfo())).first;
341
342 if (ref_info->second.continuous)
343 --info->second.num_missing_continuous;
344
345 // Add backwards reference so |frame| can be updated when new
346 // frames are inserted or decoded.
347 ref_info->second.dependent_frames[ref_info->second.num_dependent_frames] =
348 key;
349 ++ref_info->second.num_dependent_frames;
350 }
philipel93e451b2016-10-06 12:25:13 +0200351 RTC_DCHECK_LE(ref_info->second.num_missing_continuous,
352 ref_info->second.num_missing_decodable);
philipelbe7a9e52016-05-19 12:19:35 +0200353 }
354
philipele0b2f152016-09-28 10:23:49 +0200355 // Check if we have the lower spatial layer frame.
philipelbe7a9e52016-05-19 12:19:35 +0200356 if (frame.inter_layer_predicted) {
philipele0b2f152016-09-28 10:23:49 +0200357 ++info->second.num_missing_continuous;
358 ++info->second.num_missing_decodable;
359
philipelbe7a9e52016-05-19 12:19:35 +0200360 FrameKey ref_key(frame.picture_id, frame.spatial_layer - 1);
philipele0b2f152016-09-28 10:23:49 +0200361 // Gets or create the FrameInfo for the referenced frame.
362 auto ref_info = frames_.insert(std::make_pair(ref_key, FrameInfo())).first;
363 if (ref_info->second.continuous)
364 --info->second.num_missing_continuous;
365
366 if (ref_info == last_decoded_frame_it_) {
367 --info->second.num_missing_decodable;
368 } else {
369 ref_info->second.dependent_frames[ref_info->second.num_dependent_frames] =
370 key;
371 ++ref_info->second.num_dependent_frames;
372 }
philipel93e451b2016-10-06 12:25:13 +0200373 RTC_DCHECK_LE(ref_info->second.num_missing_continuous,
374 ref_info->second.num_missing_decodable);
philipelbe7a9e52016-05-19 12:19:35 +0200375 }
376
philipel93e451b2016-10-06 12:25:13 +0200377 RTC_DCHECK_LE(info->second.num_missing_continuous,
378 info->second.num_missing_decodable);
379
philipelbe7a9e52016-05-19 12:19:35 +0200380 return true;
381}
382
philipelbe742702016-11-30 01:31:40 -0800383void FrameBuffer::UpdateJitterDelay() {
philipel09d6ef02017-01-26 02:59:33 -0800384 if (!stats_callback_)
385 return;
philipelbe742702016-11-30 01:31:40 -0800386
philipel09d6ef02017-01-26 02:59:33 -0800387 int decode_ms;
388 int max_decode_ms;
389 int current_delay_ms;
390 int target_delay_ms;
391 int jitter_buffer_ms;
392 int min_playout_delay_ms;
393 int render_delay_ms;
394 if (timing_->GetTimings(&decode_ms, &max_decode_ms, &current_delay_ms,
395 &target_delay_ms, &jitter_buffer_ms,
396 &min_playout_delay_ms, &render_delay_ms)) {
397 stats_callback_->OnFrameBufferTimingsUpdated(
398 decode_ms, max_decode_ms, current_delay_ms, target_delay_ms,
399 jitter_buffer_ms, min_playout_delay_ms, render_delay_ms);
philipelbe742702016-11-30 01:31:40 -0800400 }
philipel266f0a42016-11-28 08:49:07 -0800401}
402
philipelfcc60062017-01-18 05:35:20 -0800403void FrameBuffer::ClearFramesAndHistory() {
404 frames_.clear();
405 last_decoded_frame_it_ = frames_.end();
406 last_continuous_frame_it_ = frames_.end();
407 num_frames_history_ = 0;
408 num_frames_buffered_ = 0;
409}
410
philipelbe7a9e52016-05-19 12:19:35 +0200411} // namespace video_coding
412} // namespace webrtc