blob: dcbcb1f9bdfd56b1f8cc3c3caa1904d4dfe1dc82 [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"
philipele5bd7022017-02-02 09:53:00 -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,
philipele5bd7022017-02-02 09:53:00 -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),
philipele5bd7022017-02-02 09:53:00 -080050 protection_mode_(kProtectionNack),
51 stats_callback_(stats_callback) {}
philipel266f0a42016-11-28 08:49:07 -080052
philipele5bd7022017-02-02 09:53:00 -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) {
philipel1c056252017-01-31 09:53:12 -080058 int64_t latest_return_time_ms =
59 clock_->TimeInMilliseconds() + max_wait_time_ms;
philipel504c47d2016-06-30 17:33:02 +020060 int64_t wait_ms = max_wait_time_ms;
philipele0b2f152016-09-28 10:23:49 +020061
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.
philipel1c056252017-01-31 09:53:12 -080075 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();
philipel1c056252017-01-31 09:53:12 -0800100 next_frame_it_ = frame_it;
philipele0b2f152016-09-28 10:23:49 +0200101 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
philipel1c056252017-01-31 09:53:12 -0800115 wait_ms = std::min<int64_t>(wait_ms, latest_return_time_ms - now_ms);
philipele0b2f152016-09-28 10:23:49 +0200116 wait_ms = std::max<int64_t>(wait_ms, 0);
117 } while (new_countinuous_frame_event_.Wait(wait_ms));
118
119 rtc::CritScope lock(&crit_);
philipel1c056252017-01-31 09:53:12 -0800120 int64_t now_ms = clock_->TimeInMilliseconds();
121 if (next_frame_it_ != frames_.end()) {
122 std::unique_ptr<FrameObject> frame =
123 std::move(next_frame_it_->second.frame);
philipele0b2f152016-09-28 10:23:49 +0200124
philipele0754302017-01-25 08:56:23 -0800125 if (!frame->delayed_by_retransmission()) {
126 int64_t frame_delay;
127
128 if (inter_frame_delay_.CalculateDelay(frame->timestamp, &frame_delay,
129 frame->ReceivedTime())) {
130 jitter_estimator_->UpdateEstimate(frame_delay, frame->size());
131 }
132
133 float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0;
134 timing_->SetJitterDelay(jitter_estimator_->GetJitterEstimate(rtt_mult));
philipel1c056252017-01-31 09:53:12 -0800135 timing_->UpdateCurrentDelay(frame->RenderTime(), now_ms);
philipelbe7a9e52016-05-19 12:19:35 +0200136 }
philipele0b2f152016-09-28 10:23:49 +0200137
philipelbe742702016-11-30 01:31:40 -0800138 UpdateJitterDelay();
139
philipel1c056252017-01-31 09:53:12 -0800140 PropagateDecodability(next_frame_it_->second);
141 AdvanceLastDecodedFrame(next_frame_it_);
philipelfcc60062017-01-18 05:35:20 -0800142 last_decoded_frame_timestamp_ = frame->timestamp;
philipele0b2f152016-09-28 10:23:49 +0200143 *frame_out = std::move(frame);
144 return kFrameFound;
philipel1c056252017-01-31 09:53:12 -0800145 } else if (latest_return_time_ms - now_ms > 0) {
146 // If |next_frame_it_ == frames_.end()| and there is still time left, it
147 // means that the frame buffer was cleared as the thread in this function
148 // was waiting to acquire |crit_| in order to return. Wait for the
149 // remaining time and then return.
150 return NextFrame(latest_return_time_ms - now_ms, frame_out);
philipele0b2f152016-09-28 10:23:49 +0200151 } else {
152 return kTimeout;
philipelbe7a9e52016-05-19 12:19:35 +0200153 }
154}
155
philipel4f6cd6a2016-08-03 10:59:32 +0200156void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) {
157 rtc::CritScope lock(&crit_);
158 protection_mode_ = mode;
159}
160
philipel504c47d2016-06-30 17:33:02 +0200161void FrameBuffer::Start() {
162 rtc::CritScope lock(&crit_);
163 stopped_ = false;
164}
165
166void FrameBuffer::Stop() {
167 rtc::CritScope lock(&crit_);
168 stopped_ = true;
philipele0b2f152016-09-28 10:23:49 +0200169 new_countinuous_frame_event_.Set();
philipel504c47d2016-06-30 17:33:02 +0200170}
171
philipele0b2f152016-09-28 10:23:49 +0200172int FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) {
philipelbe7a9e52016-05-19 12:19:35 +0200173 rtc::CritScope lock(&crit_);
philipel93e451b2016-10-06 12:25:13 +0200174 RTC_DCHECK(frame);
175
philipele5bd7022017-02-02 09:53:00 -0800176 if (stats_callback_)
177 stats_callback_->OnCompleteFrame(frame->num_references == 0, frame->size());
philipel266f0a42016-11-28 08:49:07 -0800178
philipelbe7a9e52016-05-19 12:19:35 +0200179 FrameKey key(frame->picture_id, frame->spatial_layer);
philipele0b2f152016-09-28 10:23:49 +0200180 int last_continuous_picture_id =
181 last_continuous_frame_it_ == frames_.end()
182 ? -1
183 : last_continuous_frame_it_->first.picture_id;
184
185 if (num_frames_buffered_ >= kMaxFramesBuffered) {
186 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id
187 << ":" << static_cast<int>(key.spatial_layer)
188 << ") could not be inserted due to the frame "
189 << "buffer being full, dropping frame.";
190 return last_continuous_picture_id;
191 }
192
193 if (frame->inter_layer_predicted && frame->spatial_layer == 0) {
194 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id
195 << ":" << static_cast<int>(key.spatial_layer)
196 << ") is marked as inter layer predicted, dropping frame.";
197 return last_continuous_picture_id;
198 }
199
200 if (last_decoded_frame_it_ != frames_.end() &&
201 key < last_decoded_frame_it_->first) {
philipelfcc60062017-01-18 05:35:20 -0800202 if (AheadOf(frame->timestamp, last_decoded_frame_timestamp_) &&
203 frame->num_references == 0) {
204 // If this frame has a newer timestamp but an earlier picture id then we
205 // assume there has been a jump in the picture id due to some encoder
206 // reconfiguration or some other reason. Even though this is not according
207 // to spec we can still continue to decode from this frame if it is a
208 // keyframe.
209 LOG(LS_WARNING) << "A jump in picture id was detected, clearing buffer.";
210 ClearFramesAndHistory();
211 last_continuous_picture_id = -1;
212 } else {
213 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
214 << key.picture_id << ":"
215 << static_cast<int>(key.spatial_layer)
216 << ") inserted after frame ("
217 << last_decoded_frame_it_->first.picture_id << ":"
218 << static_cast<int>(
219 last_decoded_frame_it_->first.spatial_layer)
220 << ") was handed off for decoding, dropping frame.";
221 return last_continuous_picture_id;
222 }
philipele0b2f152016-09-28 10:23:49 +0200223 }
224
225 auto info = frames_.insert(std::make_pair(key, FrameInfo())).first;
226
philipel93e451b2016-10-06 12:25:13 +0200227 if (info->second.frame) {
228 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id
229 << ":" << static_cast<int>(key.spatial_layer)
230 << ") already inserted, dropping frame.";
philipele0b2f152016-09-28 10:23:49 +0200231 return last_continuous_picture_id;
232 }
233
philipel93e451b2016-10-06 12:25:13 +0200234 if (!UpdateFrameInfoWithIncomingFrame(*frame, info))
235 return last_continuous_picture_id;
236
philipele0b2f152016-09-28 10:23:49 +0200237 info->second.frame = std::move(frame);
238 ++num_frames_buffered_;
239
240 if (info->second.num_missing_continuous == 0) {
241 info->second.continuous = true;
242 PropagateContinuity(info);
243 last_continuous_picture_id = last_continuous_frame_it_->first.picture_id;
244
245 // Since we now have new continuous frames there might be a better frame
246 // to return from NextFrame. Signal that thread so that it again can choose
247 // which frame to return.
248 new_countinuous_frame_event_.Set();
249 }
250
251 return last_continuous_picture_id;
philipelbe7a9e52016-05-19 12:19:35 +0200252}
253
philipele0b2f152016-09-28 10:23:49 +0200254void FrameBuffer::PropagateContinuity(FrameMap::iterator start) {
255 RTC_DCHECK(start->second.continuous);
256 if (last_continuous_frame_it_ == frames_.end())
257 last_continuous_frame_it_ = start;
258
259 std::queue<FrameMap::iterator> continuous_frames;
260 continuous_frames.push(start);
261
262 // A simple BFS to traverse continuous frames.
263 while (!continuous_frames.empty()) {
264 auto frame = continuous_frames.front();
265 continuous_frames.pop();
266
267 if (last_continuous_frame_it_->first < frame->first)
268 last_continuous_frame_it_ = frame;
269
270 // Loop through all dependent frames, and if that frame no longer has
271 // any unfulfilled dependencies then that frame is continuous as well.
272 for (size_t d = 0; d < frame->second.num_dependent_frames; ++d) {
273 auto frame_ref = frames_.find(frame->second.dependent_frames[d]);
274 --frame_ref->second.num_missing_continuous;
275
276 if (frame_ref->second.num_missing_continuous == 0) {
277 frame_ref->second.continuous = true;
278 continuous_frames.push(frame_ref);
279 }
280 }
281 }
282}
283
284void FrameBuffer::PropagateDecodability(const FrameInfo& info) {
285 for (size_t d = 0; d < info.num_dependent_frames; ++d) {
286 auto ref_info = frames_.find(info.dependent_frames[d]);
philipel93e451b2016-10-06 12:25:13 +0200287 RTC_DCHECK(ref_info != frames_.end());
philipele0b2f152016-09-28 10:23:49 +0200288 RTC_DCHECK_GT(ref_info->second.num_missing_decodable, 0U);
289 --ref_info->second.num_missing_decodable;
290 }
291}
292
293void FrameBuffer::AdvanceLastDecodedFrame(FrameMap::iterator decoded) {
294 if (last_decoded_frame_it_ == frames_.end()) {
295 last_decoded_frame_it_ = frames_.begin();
296 } else {
297 RTC_DCHECK(last_decoded_frame_it_->first < decoded->first);
298 ++last_decoded_frame_it_;
299 }
300 --num_frames_buffered_;
301 ++num_frames_history_;
302
303 // First, delete non-decoded frames from the history.
304 while (last_decoded_frame_it_ != decoded) {
305 if (last_decoded_frame_it_->second.frame)
306 --num_frames_buffered_;
307 last_decoded_frame_it_ = frames_.erase(last_decoded_frame_it_);
philipelbe7a9e52016-05-19 12:19:35 +0200308 }
309
philipele0b2f152016-09-28 10:23:49 +0200310 // Then remove old history if we have too much history saved.
311 if (num_frames_history_ > kMaxFramesHistory) {
312 frames_.erase(frames_.begin());
313 --num_frames_history_;
314 }
315}
316
317bool FrameBuffer::UpdateFrameInfoWithIncomingFrame(const FrameObject& frame,
318 FrameMap::iterator info) {
319 FrameKey key(frame.picture_id, frame.spatial_layer);
320 info->second.num_missing_continuous = frame.num_references;
321 info->second.num_missing_decodable = frame.num_references;
322
323 RTC_DCHECK(last_decoded_frame_it_ == frames_.end() ||
324 last_decoded_frame_it_->first < info->first);
325
326 // Check how many dependencies that have already been fulfilled.
327 for (size_t i = 0; i < frame.num_references; ++i) {
328 FrameKey ref_key(frame.references[i], frame.spatial_layer);
329 auto ref_info = frames_.find(ref_key);
330
331 // Does |frame| depend on a frame earlier than the last decoded frame?
332 if (last_decoded_frame_it_ != frames_.end() &&
333 ref_key <= last_decoded_frame_it_->first) {
334 if (ref_info == frames_.end()) {
335 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
336 << key.picture_id << ":"
337 << static_cast<int>(key.spatial_layer)
338 << " depends on a non-decoded frame more previous than "
339 << "the last decoded frame, dropping frame.";
340 return false;
341 }
342
343 --info->second.num_missing_continuous;
344 --info->second.num_missing_decodable;
345 } else {
346 if (ref_info == frames_.end())
347 ref_info = frames_.insert(std::make_pair(ref_key, FrameInfo())).first;
348
349 if (ref_info->second.continuous)
350 --info->second.num_missing_continuous;
351
352 // Add backwards reference so |frame| can be updated when new
353 // frames are inserted or decoded.
354 ref_info->second.dependent_frames[ref_info->second.num_dependent_frames] =
355 key;
356 ++ref_info->second.num_dependent_frames;
357 }
philipel93e451b2016-10-06 12:25:13 +0200358 RTC_DCHECK_LE(ref_info->second.num_missing_continuous,
359 ref_info->second.num_missing_decodable);
philipelbe7a9e52016-05-19 12:19:35 +0200360 }
361
philipele0b2f152016-09-28 10:23:49 +0200362 // Check if we have the lower spatial layer frame.
philipelbe7a9e52016-05-19 12:19:35 +0200363 if (frame.inter_layer_predicted) {
philipele0b2f152016-09-28 10:23:49 +0200364 ++info->second.num_missing_continuous;
365 ++info->second.num_missing_decodable;
366
philipelbe7a9e52016-05-19 12:19:35 +0200367 FrameKey ref_key(frame.picture_id, frame.spatial_layer - 1);
philipele0b2f152016-09-28 10:23:49 +0200368 // Gets or create the FrameInfo for the referenced frame.
369 auto ref_info = frames_.insert(std::make_pair(ref_key, FrameInfo())).first;
370 if (ref_info->second.continuous)
371 --info->second.num_missing_continuous;
372
373 if (ref_info == last_decoded_frame_it_) {
374 --info->second.num_missing_decodable;
375 } else {
376 ref_info->second.dependent_frames[ref_info->second.num_dependent_frames] =
377 key;
378 ++ref_info->second.num_dependent_frames;
379 }
philipel93e451b2016-10-06 12:25:13 +0200380 RTC_DCHECK_LE(ref_info->second.num_missing_continuous,
381 ref_info->second.num_missing_decodable);
philipelbe7a9e52016-05-19 12:19:35 +0200382 }
383
philipel93e451b2016-10-06 12:25:13 +0200384 RTC_DCHECK_LE(info->second.num_missing_continuous,
385 info->second.num_missing_decodable);
386
philipelbe7a9e52016-05-19 12:19:35 +0200387 return true;
388}
389
philipelbe742702016-11-30 01:31:40 -0800390void FrameBuffer::UpdateJitterDelay() {
philipele5bd7022017-02-02 09:53:00 -0800391 if (!stats_callback_)
392 return;
philipelbe742702016-11-30 01:31:40 -0800393
philipele5bd7022017-02-02 09:53:00 -0800394 int decode_ms;
395 int max_decode_ms;
396 int current_delay_ms;
397 int target_delay_ms;
398 int jitter_buffer_ms;
399 int min_playout_delay_ms;
400 int render_delay_ms;
401 if (timing_->GetTimings(&decode_ms, &max_decode_ms, &current_delay_ms,
402 &target_delay_ms, &jitter_buffer_ms,
403 &min_playout_delay_ms, &render_delay_ms)) {
404 stats_callback_->OnFrameBufferTimingsUpdated(
405 decode_ms, max_decode_ms, current_delay_ms, target_delay_ms,
406 jitter_buffer_ms, min_playout_delay_ms, render_delay_ms);
philipelbe742702016-11-30 01:31:40 -0800407 }
philipel266f0a42016-11-28 08:49:07 -0800408}
409
philipelfcc60062017-01-18 05:35:20 -0800410void FrameBuffer::ClearFramesAndHistory() {
411 frames_.clear();
412 last_decoded_frame_it_ = frames_.end();
413 last_continuous_frame_it_ = frames_.end();
philipel1c056252017-01-31 09:53:12 -0800414 next_frame_it_ = frames_.end();
philipelfcc60062017-01-18 05:35:20 -0800415 num_frames_history_ = 0;
416 num_frames_buffered_ = 0;
417}
418
philipelbe7a9e52016-05-19 12:19:35 +0200419} // namespace video_coding
420} // namespace webrtc