blob: 03854603102919007c9e7f11ab51911e328afad8 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/frame_buffer2.h"
philipelbe7a9e52016-05-19 12:19:35 +020012
13#include <algorithm>
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <cstdlib>
15#include <iterator>
philipele0b2f152016-09-28 10:23:49 +020016#include <queue>
Yves Gerey3e707812018-11-28 16:47:49 +010017#include <utility>
philipel798b2822018-06-11 13:10:14 +020018#include <vector>
philipelbe7a9e52016-05-19 12:19:35 +020019
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "api/video/encoded_image.h"
21#include "api/video/video_timing.h"
22#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/video_coding/include/video_coding_defines.h"
24#include "modules/video_coding/jitter_estimator.h"
25#include "modules/video_coding/timing.h"
26#include "rtc_base/checks.h"
Yves Gerey3e707812018-11-28 16:47:49 +010027#include "rtc_base/experiments/rtt_mult_experiment.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/logging.h"
Yves Gerey3e707812018-11-28 16:47:49 +010029#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/trace_event.h"
31#include "system_wrappers/include/clock.h"
philipel707f2782017-10-02 14:10:28 +020032#include "system_wrappers/include/field_trial.h"
philipelbe7a9e52016-05-19 12:19:35 +020033
34namespace webrtc {
35namespace video_coding {
36
37namespace {
philipele0b2f152016-09-28 10:23:49 +020038// Max number of frames the buffer will hold.
39constexpr int kMaxFramesBuffered = 600;
philipelbe7a9e52016-05-19 12:19:35 +020040
philipele0b2f152016-09-28 10:23:49 +020041// Max number of decoded frame info that will be saved.
philipelfd5a20f2016-11-15 00:57:57 -080042constexpr int kMaxFramesHistory = 50;
philipel65e1f942017-07-24 08:26:53 -070043
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +010044// The time it's allowed for a frame to be late to its rendering prediction and
45// still be rendered.
Ilya Nikolaevskiy7eef0072018-02-28 09:59:26 +010046constexpr int kMaxAllowedFrameDelayMs = 5;
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +010047
philipel65e1f942017-07-24 08:26:53 -070048constexpr int64_t kLogNonDecodedIntervalMs = 5000;
philipelbe7a9e52016-05-19 12:19:35 +020049} // namespace
50
philipelbe7a9e52016-05-19 12:19:35 +020051FrameBuffer::FrameBuffer(Clock* clock,
52 VCMJitterEstimator* jitter_estimator,
philipela45102f2017-02-22 05:30:39 -080053 VCMTiming* timing,
54 VCMReceiveStatisticsCallback* stats_callback)
philipelbe7a9e52016-05-19 12:19:35 +020055 : clock_(clock),
philipelbe7a9e52016-05-19 12:19:35 +020056 jitter_estimator_(jitter_estimator),
57 timing_(timing),
philipel4f6cd6a2016-08-03 10:59:32 +020058 inter_frame_delay_(clock_->TimeInMilliseconds()),
philipele0b2f152016-09-28 10:23:49 +020059 last_decoded_frame_it_(frames_.end()),
60 last_continuous_frame_it_(frames_.end()),
61 num_frames_history_(0),
62 num_frames_buffered_(0),
philipel29f730e2017-03-15 08:10:08 -070063 stopped_(false),
philipela45102f2017-02-22 05:30:39 -080064 protection_mode_(kProtectionNack),
philipel65e1f942017-07-24 08:26:53 -070065 stats_callback_(stats_callback),
66 last_log_non_decoded_ms_(-kLogNonDecodedIntervalMs) {}
philipel266f0a42016-11-28 08:49:07 -080067
philipela45102f2017-02-22 05:30:39 -080068FrameBuffer::~FrameBuffer() {}
philipelbe7a9e52016-05-19 12:19:35 +020069
philipel75562822016-09-05 10:57:41 +020070FrameBuffer::ReturnReason FrameBuffer::NextFrame(
71 int64_t max_wait_time_ms,
philipele7c891f2018-02-22 14:35:06 +010072 std::unique_ptr<EncodedFrame>* frame_out,
philipel3042c2d2017-08-18 04:55:02 -070073 bool keyframe_required) {
tommidb23ea62017-03-03 07:21:18 -080074 TRACE_EVENT0("webrtc", "FrameBuffer::NextFrame");
philipel1c056252017-01-31 09:53:12 -080075 int64_t latest_return_time_ms =
76 clock_->TimeInMilliseconds() + max_wait_time_ms;
philipel504c47d2016-06-30 17:33:02 +020077 int64_t wait_ms = max_wait_time_ms;
philipel29f730e2017-03-15 08:10:08 -070078 int64_t now_ms = 0;
philipele0b2f152016-09-28 10:23:49 +020079
80 do {
philipel29f730e2017-03-15 08:10:08 -070081 now_ms = clock_->TimeInMilliseconds();
philipel504c47d2016-06-30 17:33:02 +020082 {
83 rtc::CritScope lock(&crit_);
tommi0a735642017-03-14 06:23:57 -070084 new_continuous_frame_event_.Reset();
philipel29f730e2017-03-15 08:10:08 -070085 if (stopped_)
86 return kStopped;
87
88 wait_ms = max_wait_time_ms;
89
philipele0b2f152016-09-28 10:23:49 +020090 // Need to hold |crit_| in order to use |frames_|, therefore we
91 // set it here in the loop instead of outside the loop in order to not
92 // acquire the lock unnecesserily.
philipel1c056252017-01-31 09:53:12 -080093 next_frame_it_ = frames_.end();
philipelbe7a9e52016-05-19 12:19:35 +020094
philipele0b2f152016-09-28 10:23:49 +020095 // |frame_it| points to the first frame after the
96 // |last_decoded_frame_it_|.
97 auto frame_it = frames_.end();
98 if (last_decoded_frame_it_ == frames_.end()) {
99 frame_it = frames_.begin();
philipelbe7a9e52016-05-19 12:19:35 +0200100 } else {
philipele0b2f152016-09-28 10:23:49 +0200101 frame_it = last_decoded_frame_it_;
102 ++frame_it;
philipelbe7a9e52016-05-19 12:19:35 +0200103 }
philipele0b2f152016-09-28 10:23:49 +0200104
105 // |continuous_end_it| points to the first frame after the
106 // |last_continuous_frame_it_|.
107 auto continuous_end_it = last_continuous_frame_it_;
108 if (continuous_end_it != frames_.end())
109 ++continuous_end_it;
110
philipel146a48b2017-04-20 04:04:38 -0700111 for (; frame_it != continuous_end_it && frame_it != frames_.end();
112 ++frame_it) {
philipel93e451b2016-10-06 12:25:13 +0200113 if (!frame_it->second.continuous ||
114 frame_it->second.num_missing_decodable > 0) {
philipele0b2f152016-09-28 10:23:49 +0200115 continue;
philipel93e451b2016-10-06 12:25:13 +0200116 }
philipele0b2f152016-09-28 10:23:49 +0200117
philipele7c891f2018-02-22 14:35:06 +0100118 EncodedFrame* frame = frame_it->second.frame.get();
philipel3042c2d2017-08-18 04:55:02 -0700119
120 if (keyframe_required && !frame->is_keyframe())
121 continue;
122
Ilya Nikolaevskiye6a2d942018-11-07 14:32:28 +0100123 // TODO(https://bugs.webrtc.org/9974): consider removing this check
124 // as it may make a stream undecodable after a very long delay between
125 // frames.
philipel6d216502018-10-22 14:36:45 +0200126 if (last_decoded_frame_timestamp_ &&
127 AheadOf(*last_decoded_frame_timestamp_, frame->Timestamp())) {
128 continue;
129 }
130
philipel1c056252017-01-31 09:53:12 -0800131 next_frame_it_ = frame_it;
philipel6d216502018-10-22 14:36:45 +0200132 if (frame->RenderTime() == -1) {
Niels Möller23775882018-08-16 10:24:12 +0200133 frame->SetRenderTime(
134 timing_->RenderTimeMs(frame->Timestamp(), now_ms));
philipel6d216502018-10-22 14:36:45 +0200135 }
philipele0b2f152016-09-28 10:23:49 +0200136 wait_ms = timing_->MaxWaitingTime(frame->RenderTime(), now_ms);
137
138 // This will cause the frame buffer to prefer high framerate rather
139 // than high resolution in the case of the decoder not decoding fast
140 // enough and the stream has multiple spatial and temporal layers.
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100141 // For multiple temporal layers it may cause non-base layer frames to be
142 // skipped if they are late.
Ilya Nikolaevskiy7eef0072018-02-28 09:59:26 +0100143 if (wait_ms < -kMaxAllowedFrameDelayMs)
philipele0b2f152016-09-28 10:23:49 +0200144 continue;
145
146 break;
147 }
148 } // rtc::Critscope lock(&crit_);
149
philipel1c056252017-01-31 09:53:12 -0800150 wait_ms = std::min<int64_t>(wait_ms, latest_return_time_ms - now_ms);
philipele0b2f152016-09-28 10:23:49 +0200151 wait_ms = std::max<int64_t>(wait_ms, 0);
tommi0a735642017-03-14 06:23:57 -0700152 } while (new_continuous_frame_event_.Wait(wait_ms));
philipele0b2f152016-09-28 10:23:49 +0200153
philipel29f730e2017-03-15 08:10:08 -0700154 {
155 rtc::CritScope lock(&crit_);
156 now_ms = clock_->TimeInMilliseconds();
157 if (next_frame_it_ != frames_.end()) {
philipele7c891f2018-02-22 14:35:06 +0100158 std::unique_ptr<EncodedFrame> frame =
philipel29f730e2017-03-15 08:10:08 -0700159 std::move(next_frame_it_->second.frame);
philipele0b2f152016-09-28 10:23:49 +0200160
philipel29f730e2017-03-15 08:10:08 -0700161 if (!frame->delayed_by_retransmission()) {
162 int64_t frame_delay;
philipele0754302017-01-25 08:56:23 -0800163
Niels Möller23775882018-08-16 10:24:12 +0200164 if (inter_frame_delay_.CalculateDelay(frame->Timestamp(), &frame_delay,
philipel29f730e2017-03-15 08:10:08 -0700165 frame->ReceivedTime())) {
166 jitter_estimator_->UpdateEstimate(frame_delay, frame->size());
167 }
168
169 float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0;
“Michaelf9fc1712018-08-27 10:08:58 -0500170 if (RttMultExperiment::RttMultEnabled()) {
171 rtt_mult = RttMultExperiment::GetRttMultValue();
172 }
philipel29f730e2017-03-15 08:10:08 -0700173 timing_->SetJitterDelay(jitter_estimator_->GetJitterEstimate(rtt_mult));
174 timing_->UpdateCurrentDelay(frame->RenderTime(), now_ms);
philipele21be1d2017-09-25 06:37:12 -0700175 } else {
“Michaelf9fc1712018-08-27 10:08:58 -0500176 if (RttMultExperiment::RttMultEnabled() ||
177 webrtc::field_trial::IsEnabled("WebRTC-AddRttToPlayoutDelay"))
philipel707f2782017-10-02 14:10:28 +0200178 jitter_estimator_->FrameNacked();
philipele0754302017-01-25 08:56:23 -0800179 }
180
stefan95e97542017-05-23 09:52:18 -0700181 // Gracefully handle bad RTP timestamps and render time issues.
182 if (HasBadRenderTiming(*frame, now_ms)) {
183 jitter_estimator_->Reset();
184 timing_->Reset();
Niels Möller23775882018-08-16 10:24:12 +0200185 frame->SetRenderTime(timing_->RenderTimeMs(frame->Timestamp(), now_ms));
stefan95e97542017-05-23 09:52:18 -0700186 }
187
philipel29f730e2017-03-15 08:10:08 -0700188 UpdateJitterDelay();
ilnik2edc6842017-07-06 03:06:50 -0700189 UpdateTimingFrameInfo();
philipel29f730e2017-03-15 08:10:08 -0700190 PropagateDecodability(next_frame_it_->second);
brandtr9078d8c2017-04-27 07:07:27 -0700191
philipel29f730e2017-03-15 08:10:08 -0700192 AdvanceLastDecodedFrame(next_frame_it_);
Niels Möller23775882018-08-16 10:24:12 +0200193 last_decoded_frame_timestamp_ = frame->Timestamp();
philipel29f730e2017-03-15 08:10:08 -0700194 *frame_out = std::move(frame);
195 return kFrameFound;
philipelbe7a9e52016-05-19 12:19:35 +0200196 }
tommi0a735642017-03-14 06:23:57 -0700197 }
198
199 if (latest_return_time_ms - now_ms > 0) {
philipel1c056252017-01-31 09:53:12 -0800200 // If |next_frame_it_ == frames_.end()| and there is still time left, it
201 // means that the frame buffer was cleared as the thread in this function
202 // was waiting to acquire |crit_| in order to return. Wait for the
203 // remaining time and then return.
204 return NextFrame(latest_return_time_ms - now_ms, frame_out);
philipelbe7a9e52016-05-19 12:19:35 +0200205 }
tommi0a735642017-03-14 06:23:57 -0700206
207 return kTimeout;
philipelbe7a9e52016-05-19 12:19:35 +0200208}
209
philipele7c891f2018-02-22 14:35:06 +0100210bool FrameBuffer::HasBadRenderTiming(const EncodedFrame& frame,
211 int64_t now_ms) {
stefan95e97542017-05-23 09:52:18 -0700212 // Assume that render timing errors are due to changes in the video stream.
213 int64_t render_time_ms = frame.RenderTimeMs();
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200214 // Zero render time means render immediately.
215 if (render_time_ms == 0) {
216 return false;
217 }
stefan95e97542017-05-23 09:52:18 -0700218 if (render_time_ms < 0) {
219 return true;
220 }
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200221 const int64_t kMaxVideoDelayMs = 10000;
stefan95e97542017-05-23 09:52:18 -0700222 if (std::abs(render_time_ms - now_ms) > kMaxVideoDelayMs) {
223 int frame_delay = static_cast<int>(std::abs(render_time_ms - now_ms));
Mirko Bonadei675513b2017-11-09 11:09:25 +0100224 RTC_LOG(LS_WARNING)
225 << "A frame about to be decoded is out of the configured "
226 << "delay bounds (" << frame_delay << " > " << kMaxVideoDelayMs
227 << "). Resetting the video jitter buffer.";
stefan95e97542017-05-23 09:52:18 -0700228 return true;
229 }
230 if (static_cast<int>(timing_->TargetVideoDelay()) > kMaxVideoDelayMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100231 RTC_LOG(LS_WARNING) << "The video target delay has grown larger than "
232 << kMaxVideoDelayMs << " ms.";
stefan95e97542017-05-23 09:52:18 -0700233 return true;
234 }
235 return false;
236}
237
philipel4f6cd6a2016-08-03 10:59:32 +0200238void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) {
tommidb23ea62017-03-03 07:21:18 -0800239 TRACE_EVENT0("webrtc", "FrameBuffer::SetProtectionMode");
philipel4f6cd6a2016-08-03 10:59:32 +0200240 rtc::CritScope lock(&crit_);
241 protection_mode_ = mode;
242}
243
philipel504c47d2016-06-30 17:33:02 +0200244void FrameBuffer::Start() {
tommidb23ea62017-03-03 07:21:18 -0800245 TRACE_EVENT0("webrtc", "FrameBuffer::Start");
philipel29f730e2017-03-15 08:10:08 -0700246 rtc::CritScope lock(&crit_);
247 stopped_ = false;
philipel504c47d2016-06-30 17:33:02 +0200248}
249
250void FrameBuffer::Stop() {
tommidb23ea62017-03-03 07:21:18 -0800251 TRACE_EVENT0("webrtc", "FrameBuffer::Stop");
philipel29f730e2017-03-15 08:10:08 -0700252 rtc::CritScope lock(&crit_);
253 stopped_ = true;
tommi0a735642017-03-14 06:23:57 -0700254 new_continuous_frame_event_.Set();
philipel504c47d2016-06-30 17:33:02 +0200255}
256
Ilya Nikolaevskiye6a2d942018-11-07 14:32:28 +0100257void FrameBuffer::Clear() {
258 rtc::CritScope lock(&crit_);
259 ClearFramesAndHistory();
260}
261
philipele21be1d2017-09-25 06:37:12 -0700262void FrameBuffer::UpdateRtt(int64_t rtt_ms) {
263 rtc::CritScope lock(&crit_);
264 jitter_estimator_->UpdateRtt(rtt_ms);
265}
266
philipele7c891f2018-02-22 14:35:06 +0100267bool FrameBuffer::ValidReferences(const EncodedFrame& frame) const {
philipel0fa82a62018-03-19 15:34:53 +0100268 if (frame.id.picture_id < 0)
philipel3b3c9c42017-09-11 09:38:36 -0700269 return false;
270
philipel112adf92017-06-15 09:06:21 -0700271 for (size_t i = 0; i < frame.num_references; ++i) {
philipel0fa82a62018-03-19 15:34:53 +0100272 if (frame.references[i] < 0 || frame.references[i] >= frame.id.picture_id)
philipel112adf92017-06-15 09:06:21 -0700273 return false;
philipel3b3c9c42017-09-11 09:38:36 -0700274
philipel112adf92017-06-15 09:06:21 -0700275 for (size_t j = i + 1; j < frame.num_references; ++j) {
276 if (frame.references[i] == frame.references[j])
277 return false;
278 }
279 }
280
philipel0fa82a62018-03-19 15:34:53 +0100281 if (frame.inter_layer_predicted && frame.id.spatial_layer == 0)
philipel112adf92017-06-15 09:06:21 -0700282 return false;
283
284 return true;
285}
286
philipele7c891f2018-02-22 14:35:06 +0100287void FrameBuffer::UpdatePlayoutDelays(const EncodedFrame& frame) {
gnishb2a318b2017-05-10 09:21:33 -0700288 TRACE_EVENT0("webrtc", "FrameBuffer::UpdatePlayoutDelays");
289 PlayoutDelay playout_delay = frame.EncodedImage().playout_delay_;
290 if (playout_delay.min_ms >= 0)
291 timing_->set_min_playout_delay(playout_delay.min_ms);
292
293 if (playout_delay.max_ms >= 0)
294 timing_->set_max_playout_delay(playout_delay.max_ms);
philipel0a9f6de2018-02-28 11:29:47 +0100295
296 if (!frame.delayed_by_retransmission())
Niels Möller23775882018-08-16 10:24:12 +0200297 timing_->IncomingTimestamp(frame.Timestamp(), frame.ReceivedTime());
gnishb2a318b2017-05-10 09:21:33 -0700298}
299
philipele7c891f2018-02-22 14:35:06 +0100300int64_t FrameBuffer::InsertFrame(std::unique_ptr<EncodedFrame> frame) {
tommidb23ea62017-03-03 07:21:18 -0800301 TRACE_EVENT0("webrtc", "FrameBuffer::InsertFrame");
philipel93e451b2016-10-06 12:25:13 +0200302 RTC_DCHECK(frame);
philipela45102f2017-02-22 05:30:39 -0800303 if (stats_callback_)
ilnik6d5b4d62017-08-30 03:32:14 -0700304 stats_callback_->OnCompleteFrame(frame->is_keyframe(), frame->size(),
305 frame->contentType());
philipel0fa82a62018-03-19 15:34:53 +0100306 const VideoLayerFrameId& id = frame->id;
tommi0a735642017-03-14 06:23:57 -0700307
308 rtc::CritScope lock(&crit_);
philipel29f730e2017-03-15 08:10:08 -0700309
philipel1610f942017-12-12 13:58:31 +0100310 int64_t last_continuous_picture_id =
philipele0b2f152016-09-28 10:23:49 +0200311 last_continuous_frame_it_ == frames_.end()
312 ? -1
313 : last_continuous_frame_it_->first.picture_id;
314
philipel112adf92017-06-15 09:06:21 -0700315 if (!ValidReferences(*frame)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100316 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100317 << id.picture_id << ":"
318 << static_cast<int>(id.spatial_layer)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100319 << ") has invalid frame references, dropping frame.";
philipel112adf92017-06-15 09:06:21 -0700320 return last_continuous_picture_id;
321 }
322
philipele0b2f152016-09-28 10:23:49 +0200323 if (num_frames_buffered_ >= kMaxFramesBuffered) {
philipel9771c502018-03-02 11:06:27 +0100324 if (frame->is_keyframe()) {
325 RTC_LOG(LS_WARNING) << "Inserting keyframe (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100326 << id.picture_id << ":"
327 << static_cast<int>(id.spatial_layer)
philipel9771c502018-03-02 11:06:27 +0100328 << ") but buffer is full, clearing"
329 << " buffer and inserting the frame.";
330 ClearFramesAndHistory();
331 } else {
332 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100333 << id.picture_id << ":"
334 << static_cast<int>(id.spatial_layer)
philipel9771c502018-03-02 11:06:27 +0100335 << ") could not be inserted due to the frame "
336 << "buffer being full, dropping frame.";
337 return last_continuous_picture_id;
338 }
philipele0b2f152016-09-28 10:23:49 +0200339 }
340
philipele0b2f152016-09-28 10:23:49 +0200341 if (last_decoded_frame_it_ != frames_.end() &&
philipel0fa82a62018-03-19 15:34:53 +0100342 id <= last_decoded_frame_it_->first) {
philipel6d216502018-10-22 14:36:45 +0200343 if (AheadOf(frame->Timestamp(), *last_decoded_frame_timestamp_) &&
philipel3042c2d2017-08-18 04:55:02 -0700344 frame->is_keyframe()) {
philipelfcc60062017-01-18 05:35:20 -0800345 // If this frame has a newer timestamp but an earlier picture id then we
346 // assume there has been a jump in the picture id due to some encoder
347 // reconfiguration or some other reason. Even though this is not according
348 // to spec we can still continue to decode from this frame if it is a
349 // keyframe.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100350 RTC_LOG(LS_WARNING)
351 << "A jump in picture id was detected, clearing buffer.";
philipelfcc60062017-01-18 05:35:20 -0800352 ClearFramesAndHistory();
353 last_continuous_picture_id = -1;
354 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100355 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100356 << id.picture_id << ":"
357 << static_cast<int>(id.spatial_layer)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100358 << ") inserted after frame ("
359 << last_decoded_frame_it_->first.picture_id << ":"
360 << static_cast<int>(
361 last_decoded_frame_it_->first.spatial_layer)
362 << ") was handed off for decoding, dropping frame.";
philipelfcc60062017-01-18 05:35:20 -0800363 return last_continuous_picture_id;
364 }
philipele0b2f152016-09-28 10:23:49 +0200365 }
366
philipel146a48b2017-04-20 04:04:38 -0700367 // Test if inserting this frame would cause the order of the frames to become
368 // ambiguous (covering more than half the interval of 2^16). This can happen
369 // when the picture id make large jumps mid stream.
philipel0fa82a62018-03-19 15:34:53 +0100370 if (!frames_.empty() && id < frames_.begin()->first &&
371 frames_.rbegin()->first < id) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100372 RTC_LOG(LS_WARNING)
373 << "A jump in picture id was detected, clearing buffer.";
philipel146a48b2017-04-20 04:04:38 -0700374 ClearFramesAndHistory();
375 last_continuous_picture_id = -1;
376 }
377
philipel0fa82a62018-03-19 15:34:53 +0100378 auto info = frames_.emplace(id, FrameInfo()).first;
philipele0b2f152016-09-28 10:23:49 +0200379
philipel93e451b2016-10-06 12:25:13 +0200380 if (info->second.frame) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100381 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100382 << id.picture_id << ":"
383 << static_cast<int>(id.spatial_layer)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100384 << ") already inserted, dropping frame.";
philipele0b2f152016-09-28 10:23:49 +0200385 return last_continuous_picture_id;
386 }
387
philipel93e451b2016-10-06 12:25:13 +0200388 if (!UpdateFrameInfoWithIncomingFrame(*frame, info))
389 return last_continuous_picture_id;
gnishb2a318b2017-05-10 09:21:33 -0700390 UpdatePlayoutDelays(*frame);
philipel0a9f6de2018-02-28 11:29:47 +0100391
philipele0b2f152016-09-28 10:23:49 +0200392 info->second.frame = std::move(frame);
393 ++num_frames_buffered_;
394
395 if (info->second.num_missing_continuous == 0) {
396 info->second.continuous = true;
397 PropagateContinuity(info);
398 last_continuous_picture_id = last_continuous_frame_it_->first.picture_id;
399
400 // Since we now have new continuous frames there might be a better frame
401 // to return from NextFrame. Signal that thread so that it again can choose
402 // which frame to return.
tommi0a735642017-03-14 06:23:57 -0700403 new_continuous_frame_event_.Set();
philipele0b2f152016-09-28 10:23:49 +0200404 }
405
406 return last_continuous_picture_id;
philipelbe7a9e52016-05-19 12:19:35 +0200407}
408
philipele0b2f152016-09-28 10:23:49 +0200409void FrameBuffer::PropagateContinuity(FrameMap::iterator start) {
tommidb23ea62017-03-03 07:21:18 -0800410 TRACE_EVENT0("webrtc", "FrameBuffer::PropagateContinuity");
philipele0b2f152016-09-28 10:23:49 +0200411 RTC_DCHECK(start->second.continuous);
412 if (last_continuous_frame_it_ == frames_.end())
413 last_continuous_frame_it_ = start;
414
415 std::queue<FrameMap::iterator> continuous_frames;
416 continuous_frames.push(start);
417
418 // A simple BFS to traverse continuous frames.
419 while (!continuous_frames.empty()) {
420 auto frame = continuous_frames.front();
421 continuous_frames.pop();
422
423 if (last_continuous_frame_it_->first < frame->first)
424 last_continuous_frame_it_ = frame;
425
426 // Loop through all dependent frames, and if that frame no longer has
427 // any unfulfilled dependencies then that frame is continuous as well.
428 for (size_t d = 0; d < frame->second.num_dependent_frames; ++d) {
429 auto frame_ref = frames_.find(frame->second.dependent_frames[d]);
philipel112adf92017-06-15 09:06:21 -0700430 RTC_DCHECK(frame_ref != frames_.end());
philipele0b2f152016-09-28 10:23:49 +0200431
philipel112adf92017-06-15 09:06:21 -0700432 // TODO(philipel): Look into why we've seen this happen.
433 if (frame_ref != frames_.end()) {
434 --frame_ref->second.num_missing_continuous;
435 if (frame_ref->second.num_missing_continuous == 0) {
436 frame_ref->second.continuous = true;
437 continuous_frames.push(frame_ref);
438 }
philipele0b2f152016-09-28 10:23:49 +0200439 }
440 }
441 }
442}
443
444void FrameBuffer::PropagateDecodability(const FrameInfo& info) {
tommidb23ea62017-03-03 07:21:18 -0800445 TRACE_EVENT0("webrtc", "FrameBuffer::PropagateDecodability");
tommie95b78b2017-05-14 07:23:11 -0700446 RTC_CHECK(info.num_dependent_frames < FrameInfo::kMaxNumDependentFrames);
philipele0b2f152016-09-28 10:23:49 +0200447 for (size_t d = 0; d < info.num_dependent_frames; ++d) {
448 auto ref_info = frames_.find(info.dependent_frames[d]);
philipel93e451b2016-10-06 12:25:13 +0200449 RTC_DCHECK(ref_info != frames_.end());
tommie95b78b2017-05-14 07:23:11 -0700450 // TODO(philipel): Look into why we've seen this happen.
451 if (ref_info != frames_.end()) {
452 RTC_DCHECK_GT(ref_info->second.num_missing_decodable, 0U);
453 --ref_info->second.num_missing_decodable;
454 }
philipele0b2f152016-09-28 10:23:49 +0200455 }
456}
457
458void FrameBuffer::AdvanceLastDecodedFrame(FrameMap::iterator decoded) {
tommidb23ea62017-03-03 07:21:18 -0800459 TRACE_EVENT0("webrtc", "FrameBuffer::AdvanceLastDecodedFrame");
philipele0b2f152016-09-28 10:23:49 +0200460 if (last_decoded_frame_it_ == frames_.end()) {
461 last_decoded_frame_it_ = frames_.begin();
462 } else {
463 RTC_DCHECK(last_decoded_frame_it_->first < decoded->first);
464 ++last_decoded_frame_it_;
465 }
466 --num_frames_buffered_;
467 ++num_frames_history_;
468
469 // First, delete non-decoded frames from the history.
470 while (last_decoded_frame_it_ != decoded) {
471 if (last_decoded_frame_it_->second.frame)
472 --num_frames_buffered_;
473 last_decoded_frame_it_ = frames_.erase(last_decoded_frame_it_);
philipelbe7a9e52016-05-19 12:19:35 +0200474 }
475
philipele0b2f152016-09-28 10:23:49 +0200476 // Then remove old history if we have too much history saved.
477 if (num_frames_history_ > kMaxFramesHistory) {
478 frames_.erase(frames_.begin());
479 --num_frames_history_;
480 }
481}
482
philipele7c891f2018-02-22 14:35:06 +0100483bool FrameBuffer::UpdateFrameInfoWithIncomingFrame(const EncodedFrame& frame,
philipele0b2f152016-09-28 10:23:49 +0200484 FrameMap::iterator info) {
tommidb23ea62017-03-03 07:21:18 -0800485 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateFrameInfoWithIncomingFrame");
philipel0fa82a62018-03-19 15:34:53 +0100486 const VideoLayerFrameId& id = frame.id;
philipele0b2f152016-09-28 10:23:49 +0200487
488 RTC_DCHECK(last_decoded_frame_it_ == frames_.end() ||
489 last_decoded_frame_it_->first < info->first);
490
philipel798b2822018-06-11 13:10:14 +0200491 // In this function we determine how many missing dependencies this |frame|
492 // has to become continuous/decodable. If a frame that this |frame| depend
493 // on has already been decoded then we can ignore that dependency since it has
494 // already been fulfilled.
495 //
496 // For all other frames we will register a backwards reference to this |frame|
497 // so that |num_missing_continuous| and |num_missing_decodable| can be
498 // decremented as frames become continuous/are decoded.
499 struct Dependency {
500 VideoLayerFrameId id;
501 bool continuous;
502 };
503 std::vector<Dependency> not_yet_fulfilled_dependencies;
504
505 // Find all dependencies that have not yet been fulfilled.
philipele0b2f152016-09-28 10:23:49 +0200506 for (size_t i = 0; i < frame.num_references; ++i) {
philipel0fa82a62018-03-19 15:34:53 +0100507 VideoLayerFrameId ref_key(frame.references[i], frame.id.spatial_layer);
philipele0b2f152016-09-28 10:23:49 +0200508 auto ref_info = frames_.find(ref_key);
509
philipel798b2822018-06-11 13:10:14 +0200510 // Does |frame| depend on a frame earlier than the last decoded one?
philipele0b2f152016-09-28 10:23:49 +0200511 if (last_decoded_frame_it_ != frames_.end() &&
512 ref_key <= last_decoded_frame_it_->first) {
philipel798b2822018-06-11 13:10:14 +0200513 // Was that frame decoded? If not, this |frame| will never become
514 // decodable.
philipele0b2f152016-09-28 10:23:49 +0200515 if (ref_info == frames_.end()) {
philipel65e1f942017-07-24 08:26:53 -0700516 int64_t now_ms = clock_->TimeInMilliseconds();
517 if (last_log_non_decoded_ms_ + kLogNonDecodedIntervalMs < now_ms) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100518 RTC_LOG(LS_WARNING)
philipel0fa82a62018-03-19 15:34:53 +0100519 << "Frame with (picture_id:spatial_id) (" << id.picture_id << ":"
520 << static_cast<int>(id.spatial_layer)
philipel65e1f942017-07-24 08:26:53 -0700521 << ") depends on a non-decoded frame more previous than"
522 << " the last decoded frame, dropping frame.";
523 last_log_non_decoded_ms_ = now_ms;
524 }
philipele0b2f152016-09-28 10:23:49 +0200525 return false;
526 }
philipele0b2f152016-09-28 10:23:49 +0200527 } else {
philipel798b2822018-06-11 13:10:14 +0200528 bool ref_continuous =
529 ref_info != frames_.end() && ref_info->second.continuous;
530 not_yet_fulfilled_dependencies.push_back({ref_key, ref_continuous});
philipele0b2f152016-09-28 10:23:49 +0200531 }
philipelbe7a9e52016-05-19 12:19:35 +0200532 }
533
philipel798b2822018-06-11 13:10:14 +0200534 // Does |frame| depend on the lower spatial layer?
philipelbe7a9e52016-05-19 12:19:35 +0200535 if (frame.inter_layer_predicted) {
philipel0fa82a62018-03-19 15:34:53 +0100536 VideoLayerFrameId ref_key(frame.id.picture_id, frame.id.spatial_layer - 1);
philipel798b2822018-06-11 13:10:14 +0200537 auto ref_info = frames_.find(ref_key);
philipele0b2f152016-09-28 10:23:49 +0200538
philipel798b2822018-06-11 13:10:14 +0200539 bool lower_layer_continuous =
540 ref_info != frames_.end() && ref_info->second.continuous;
541 bool lower_layer_decoded = last_decoded_frame_it_ != frames_.end() &&
542 last_decoded_frame_it_->first == ref_key;
543
544 if (!lower_layer_continuous || !lower_layer_decoded) {
545 not_yet_fulfilled_dependencies.push_back(
546 {ref_key, lower_layer_continuous});
philipele0b2f152016-09-28 10:23:49 +0200547 }
philipelbe7a9e52016-05-19 12:19:35 +0200548 }
549
philipel798b2822018-06-11 13:10:14 +0200550 info->second.num_missing_continuous = not_yet_fulfilled_dependencies.size();
551 info->second.num_missing_decodable = not_yet_fulfilled_dependencies.size();
552
553 for (const Dependency& dep : not_yet_fulfilled_dependencies) {
554 if (dep.continuous)
555 --info->second.num_missing_continuous;
556
557 // At this point we know we want to insert this frame, so here we
558 // intentionally get or create the FrameInfo for this dependency.
559 FrameInfo* dep_info = &frames_[dep.id];
560
561 if (dep_info->num_dependent_frames <
562 (FrameInfo::kMaxNumDependentFrames - 1)) {
563 dep_info->dependent_frames[dep_info->num_dependent_frames] = id;
564 ++dep_info->num_dependent_frames;
565 } else {
566 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
567 << dep.id.picture_id << ":"
568 << static_cast<int>(dep.id.spatial_layer)
569 << ") is referenced by too many frames.";
570 }
571 }
philipel93e451b2016-10-06 12:25:13 +0200572
philipelbe7a9e52016-05-19 12:19:35 +0200573 return true;
574}
575
philipelbe742702016-11-30 01:31:40 -0800576void FrameBuffer::UpdateJitterDelay() {
tommidb23ea62017-03-03 07:21:18 -0800577 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateJitterDelay");
philipela45102f2017-02-22 05:30:39 -0800578 if (!stats_callback_)
579 return;
philipelbe742702016-11-30 01:31:40 -0800580
philipela45102f2017-02-22 05:30:39 -0800581 int decode_ms;
582 int max_decode_ms;
583 int current_delay_ms;
584 int target_delay_ms;
585 int jitter_buffer_ms;
586 int min_playout_delay_ms;
587 int render_delay_ms;
588 if (timing_->GetTimings(&decode_ms, &max_decode_ms, &current_delay_ms,
589 &target_delay_ms, &jitter_buffer_ms,
590 &min_playout_delay_ms, &render_delay_ms)) {
591 stats_callback_->OnFrameBufferTimingsUpdated(
592 decode_ms, max_decode_ms, current_delay_ms, target_delay_ms,
593 jitter_buffer_ms, min_playout_delay_ms, render_delay_ms);
philipelbe742702016-11-30 01:31:40 -0800594 }
philipel266f0a42016-11-28 08:49:07 -0800595}
596
ilnik2edc6842017-07-06 03:06:50 -0700597void FrameBuffer::UpdateTimingFrameInfo() {
598 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateTimingFrameInfo");
Danil Chapovalov0040b662018-06-18 10:48:16 +0200599 absl::optional<TimingFrameInfo> info = timing_->GetTimingFrameInfo();
philipel97187112018-03-23 10:43:21 +0100600 if (info && stats_callback_)
ilnik2edc6842017-07-06 03:06:50 -0700601 stats_callback_->OnTimingFrameInfoUpdated(*info);
602}
603
philipelfcc60062017-01-18 05:35:20 -0800604void FrameBuffer::ClearFramesAndHistory() {
ilnik2edc6842017-07-06 03:06:50 -0700605 TRACE_EVENT0("webrtc", "FrameBuffer::ClearFramesAndHistory");
philipelfcc60062017-01-18 05:35:20 -0800606 frames_.clear();
607 last_decoded_frame_it_ = frames_.end();
608 last_continuous_frame_it_ = frames_.end();
philipel1c056252017-01-31 09:53:12 -0800609 next_frame_it_ = frames_.end();
philipelfcc60062017-01-18 05:35:20 -0800610 num_frames_history_ = 0;
611 num_frames_buffered_ = 0;
612}
613
Niels Möllerbe682d42018-03-27 08:31:45 +0200614FrameBuffer::FrameInfo::FrameInfo() = default;
615FrameBuffer::FrameInfo::FrameInfo(FrameInfo&&) = default;
616FrameBuffer::FrameInfo::~FrameInfo() = default;
617
philipelbe7a9e52016-05-19 12:19:35 +0200618} // namespace video_coding
619} // namespace webrtc