blob: 027b94304d79b3982a3f8f5b61cb462bda1bb439 [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"
philipelbe7a9e52016-05-19 12:19:35 +020019#include "webrtc/modules/video_coding/jitter_estimator.h"
philipelbe7a9e52016-05-19 12:19:35 +020020#include "webrtc/modules/video_coding/timing.h"
21#include "webrtc/system_wrappers/include/clock.h"
philipel266f0a42016-11-28 08:49:07 -080022#include "webrtc/system_wrappers/include/metrics.h"
philipelbe7a9e52016-05-19 12:19:35 +020023
24namespace webrtc {
25namespace video_coding {
26
27namespace {
philipele0b2f152016-09-28 10:23:49 +020028// Max number of frames the buffer will hold.
29constexpr int kMaxFramesBuffered = 600;
philipelbe7a9e52016-05-19 12:19:35 +020030
philipele0b2f152016-09-28 10:23:49 +020031// Max number of decoded frame info that will be saved.
philipelfd5a20f2016-11-15 00:57:57 -080032constexpr int kMaxFramesHistory = 50;
philipelbe7a9e52016-05-19 12:19:35 +020033} // namespace
34
philipelbe7a9e52016-05-19 12:19:35 +020035FrameBuffer::FrameBuffer(Clock* clock,
36 VCMJitterEstimator* jitter_estimator,
philipel27378f32017-01-27 02:19:05 -080037 VCMTiming* timing)
philipelbe7a9e52016-05-19 12:19:35 +020038 : clock_(clock),
philipele0b2f152016-09-28 10:23:49 +020039 new_countinuous_frame_event_(false, false),
philipelbe7a9e52016-05-19 12:19:35 +020040 jitter_estimator_(jitter_estimator),
41 timing_(timing),
philipel4f6cd6a2016-08-03 10:59:32 +020042 inter_frame_delay_(clock_->TimeInMilliseconds()),
philipele0b2f152016-09-28 10:23:49 +020043 last_decoded_frame_it_(frames_.end()),
44 last_continuous_frame_it_(frames_.end()),
45 num_frames_history_(0),
46 num_frames_buffered_(0),
philipel4f6cd6a2016-08-03 10:59:32 +020047 stopped_(false),
philipel27378f32017-01-27 02:19:05 -080048 protection_mode_(kProtectionNack) {}
philipel266f0a42016-11-28 08:49:07 -080049
philipel27378f32017-01-27 02:19:05 -080050FrameBuffer::~FrameBuffer() {
51 UpdateHistograms();
52}
philipelbe7a9e52016-05-19 12:19:35 +020053
philipel75562822016-09-05 10:57:41 +020054FrameBuffer::ReturnReason FrameBuffer::NextFrame(
55 int64_t max_wait_time_ms,
56 std::unique_ptr<FrameObject>* frame_out) {
philipel1c056252017-01-31 09:53:12 -080057 int64_t latest_return_time_ms =
58 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
61 do {
62 int64_t now_ms = clock_->TimeInMilliseconds();
philipel504c47d2016-06-30 17:33:02 +020063 {
64 rtc::CritScope lock(&crit_);
philipele0b2f152016-09-28 10:23:49 +020065 new_countinuous_frame_event_.Reset();
philipel504c47d2016-06-30 17:33:02 +020066 if (stopped_)
philipel75562822016-09-05 10:57:41 +020067 return kStopped;
philipelbe7a9e52016-05-19 12:19:35 +020068
philipel504c47d2016-06-30 17:33:02 +020069 wait_ms = max_wait_time_ms;
philipele0b2f152016-09-28 10:23:49 +020070
71 // Need to hold |crit_| in order to use |frames_|, therefore we
72 // set it here in the loop instead of outside the loop in order to not
73 // acquire the lock unnecesserily.
philipel1c056252017-01-31 09:53:12 -080074 next_frame_it_ = frames_.end();
philipelbe7a9e52016-05-19 12:19:35 +020075
philipele0b2f152016-09-28 10:23:49 +020076 // |frame_it| points to the first frame after the
77 // |last_decoded_frame_it_|.
78 auto frame_it = frames_.end();
79 if (last_decoded_frame_it_ == frames_.end()) {
80 frame_it = frames_.begin();
philipelbe7a9e52016-05-19 12:19:35 +020081 } else {
philipele0b2f152016-09-28 10:23:49 +020082 frame_it = last_decoded_frame_it_;
83 ++frame_it;
philipelbe7a9e52016-05-19 12:19:35 +020084 }
philipele0b2f152016-09-28 10:23:49 +020085
86 // |continuous_end_it| points to the first frame after the
87 // |last_continuous_frame_it_|.
88 auto continuous_end_it = last_continuous_frame_it_;
89 if (continuous_end_it != frames_.end())
90 ++continuous_end_it;
91
92 for (; frame_it != continuous_end_it; ++frame_it) {
philipel93e451b2016-10-06 12:25:13 +020093 if (!frame_it->second.continuous ||
94 frame_it->second.num_missing_decodable > 0) {
philipele0b2f152016-09-28 10:23:49 +020095 continue;
philipel93e451b2016-10-06 12:25:13 +020096 }
philipele0b2f152016-09-28 10:23:49 +020097
98 FrameObject* frame = frame_it->second.frame.get();
philipel1c056252017-01-31 09:53:12 -080099 next_frame_it_ = frame_it;
philipele0b2f152016-09-28 10:23:49 +0200100 if (frame->RenderTime() == -1)
101 frame->SetRenderTime(timing_->RenderTimeMs(frame->timestamp, now_ms));
102 wait_ms = timing_->MaxWaitingTime(frame->RenderTime(), now_ms);
103
104 // This will cause the frame buffer to prefer high framerate rather
105 // than high resolution in the case of the decoder not decoding fast
106 // enough and the stream has multiple spatial and temporal layers.
107 if (wait_ms == 0)
108 continue;
109
110 break;
111 }
112 } // rtc::Critscope lock(&crit_);
113
philipel1c056252017-01-31 09:53:12 -0800114 wait_ms = std::min<int64_t>(wait_ms, latest_return_time_ms - now_ms);
philipele0b2f152016-09-28 10:23:49 +0200115 wait_ms = std::max<int64_t>(wait_ms, 0);
116 } while (new_countinuous_frame_event_.Wait(wait_ms));
117
118 rtc::CritScope lock(&crit_);
philipel1c056252017-01-31 09:53:12 -0800119 int64_t now_ms = clock_->TimeInMilliseconds();
120 if (next_frame_it_ != frames_.end()) {
121 std::unique_ptr<FrameObject> frame =
122 std::move(next_frame_it_->second.frame);
philipele0b2f152016-09-28 10:23:49 +0200123
philipele0754302017-01-25 08:56:23 -0800124 if (!frame->delayed_by_retransmission()) {
125 int64_t frame_delay;
126
127 if (inter_frame_delay_.CalculateDelay(frame->timestamp, &frame_delay,
128 frame->ReceivedTime())) {
129 jitter_estimator_->UpdateEstimate(frame_delay, frame->size());
130 }
131
132 float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0;
133 timing_->SetJitterDelay(jitter_estimator_->GetJitterEstimate(rtt_mult));
philipel1c056252017-01-31 09:53:12 -0800134 timing_->UpdateCurrentDelay(frame->RenderTime(), now_ms);
philipelbe7a9e52016-05-19 12:19:35 +0200135 }
philipele0b2f152016-09-28 10:23:49 +0200136
philipelbe742702016-11-30 01:31:40 -0800137 UpdateJitterDelay();
138
philipel1c056252017-01-31 09:53:12 -0800139 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;
philipel1c056252017-01-31 09:53:12 -0800144 } else if (latest_return_time_ms - now_ms > 0) {
145 // If |next_frame_it_ == frames_.end()| and there is still time left, it
146 // means that the frame buffer was cleared as the thread in this function
147 // was waiting to acquire |crit_| in order to return. Wait for the
148 // remaining time and then return.
149 return NextFrame(latest_return_time_ms - now_ms, frame_out);
philipele0b2f152016-09-28 10:23:49 +0200150 } else {
151 return kTimeout;
philipelbe7a9e52016-05-19 12:19:35 +0200152 }
153}
154
philipel4f6cd6a2016-08-03 10:59:32 +0200155void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) {
156 rtc::CritScope lock(&crit_);
157 protection_mode_ = mode;
158}
159
philipel504c47d2016-06-30 17:33:02 +0200160void FrameBuffer::Start() {
161 rtc::CritScope lock(&crit_);
162 stopped_ = false;
163}
164
165void FrameBuffer::Stop() {
166 rtc::CritScope lock(&crit_);
167 stopped_ = true;
philipele0b2f152016-09-28 10:23:49 +0200168 new_countinuous_frame_event_.Set();
philipel504c47d2016-06-30 17:33:02 +0200169}
170
philipele0b2f152016-09-28 10:23:49 +0200171int FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) {
philipelbe7a9e52016-05-19 12:19:35 +0200172 rtc::CritScope lock(&crit_);
philipel93e451b2016-10-06 12:25:13 +0200173 RTC_DCHECK(frame);
174
philipel27378f32017-01-27 02:19:05 -0800175 ++num_total_frames_;
176 if (frame->num_references == 0)
177 ++num_key_frames_;
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() {
philipel27378f32017-01-27 02:19:05 -0800391 int unused;
392 int delay;
393 timing_->GetTimings(&unused, &unused, &unused, &unused, &delay, &unused,
394 &unused);
philipelbe742702016-11-30 01:31:40 -0800395
philipel27378f32017-01-27 02:19:05 -0800396 accumulated_delay_ += delay;
397 ++accumulated_delay_samples_;
398}
399
400void FrameBuffer::UpdateHistograms() const {
401 rtc::CritScope lock(&crit_);
402 if (num_total_frames_ > 0) {
403 int key_frames_permille = (static_cast<float>(num_key_frames_) * 1000.0f /
404 static_cast<float>(num_total_frames_) +
405 0.5f);
406 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.KeyFramesReceivedInPermille",
407 key_frames_permille);
408 }
409
410 if (accumulated_delay_samples_ > 0) {
411 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.JitterBufferDelayInMs",
412 accumulated_delay_ / accumulated_delay_samples_);
philipelbe742702016-11-30 01:31:40 -0800413 }
philipel266f0a42016-11-28 08:49:07 -0800414}
415
philipelfcc60062017-01-18 05:35:20 -0800416void FrameBuffer::ClearFramesAndHistory() {
417 frames_.clear();
418 last_decoded_frame_it_ = frames_.end();
419 last_continuous_frame_it_ = frames_.end();
philipel1c056252017-01-31 09:53:12 -0800420 next_frame_it_ = frames_.end();
philipelfcc60062017-01-18 05:35:20 -0800421 num_frames_history_ = 0;
422 num_frames_buffered_ = 0;
423}
424
philipelbe7a9e52016-05-19 12:19:35 +0200425} // namespace video_coding
426} // namespace webrtc