blob: f9a4f54ffcc937a0b092e5081558f9acb3e97ed1 [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>
philipele0b2f152016-09-28 10:23:49 +020014#include <cstring>
15#include <queue>
philipel798b2822018-06-11 13:10:14 +020016#include <vector>
philipelbe7a9e52016-05-19 12:19:35 +020017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/video_coding/include/video_coding_defines.h"
19#include "modules/video_coding/jitter_estimator.h"
20#include "modules/video_coding/timing.h"
21#include "rtc_base/checks.h"
22#include "rtc_base/logging.h"
23#include "rtc_base/trace_event.h"
24#include "system_wrappers/include/clock.h"
philipel707f2782017-10-02 14:10:28 +020025#include "system_wrappers/include/field_trial.h"
philipelbe7a9e52016-05-19 12:19:35 +020026
27namespace webrtc {
28namespace video_coding {
29
30namespace {
philipele0b2f152016-09-28 10:23:49 +020031// Max number of frames the buffer will hold.
32constexpr int kMaxFramesBuffered = 600;
philipelbe7a9e52016-05-19 12:19:35 +020033
philipele0b2f152016-09-28 10:23:49 +020034// Max number of decoded frame info that will be saved.
philipelfd5a20f2016-11-15 00:57:57 -080035constexpr int kMaxFramesHistory = 50;
philipel65e1f942017-07-24 08:26:53 -070036
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +010037// The time it's allowed for a frame to be late to its rendering prediction and
38// still be rendered.
Ilya Nikolaevskiy7eef0072018-02-28 09:59:26 +010039constexpr int kMaxAllowedFrameDelayMs = 5;
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +010040
philipel65e1f942017-07-24 08:26:53 -070041constexpr int64_t kLogNonDecodedIntervalMs = 5000;
philipelbe7a9e52016-05-19 12:19:35 +020042} // namespace
43
philipelbe7a9e52016-05-19 12:19:35 +020044FrameBuffer::FrameBuffer(Clock* clock,
45 VCMJitterEstimator* jitter_estimator,
philipela45102f2017-02-22 05:30:39 -080046 VCMTiming* timing,
47 VCMReceiveStatisticsCallback* stats_callback)
philipelbe7a9e52016-05-19 12:19:35 +020048 : clock_(clock),
philipelbe7a9e52016-05-19 12:19:35 +020049 jitter_estimator_(jitter_estimator),
50 timing_(timing),
philipel4f6cd6a2016-08-03 10:59:32 +020051 inter_frame_delay_(clock_->TimeInMilliseconds()),
philipele0b2f152016-09-28 10:23:49 +020052 last_decoded_frame_it_(frames_.end()),
53 last_continuous_frame_it_(frames_.end()),
54 num_frames_history_(0),
55 num_frames_buffered_(0),
philipel29f730e2017-03-15 08:10:08 -070056 stopped_(false),
philipela45102f2017-02-22 05:30:39 -080057 protection_mode_(kProtectionNack),
philipel65e1f942017-07-24 08:26:53 -070058 stats_callback_(stats_callback),
59 last_log_non_decoded_ms_(-kLogNonDecodedIntervalMs) {}
philipel266f0a42016-11-28 08:49:07 -080060
philipela45102f2017-02-22 05:30:39 -080061FrameBuffer::~FrameBuffer() {}
philipelbe7a9e52016-05-19 12:19:35 +020062
philipel75562822016-09-05 10:57:41 +020063FrameBuffer::ReturnReason FrameBuffer::NextFrame(
64 int64_t max_wait_time_ms,
philipele7c891f2018-02-22 14:35:06 +010065 std::unique_ptr<EncodedFrame>* frame_out,
philipel3042c2d2017-08-18 04:55:02 -070066 bool keyframe_required) {
tommidb23ea62017-03-03 07:21:18 -080067 TRACE_EVENT0("webrtc", "FrameBuffer::NextFrame");
philipel1c056252017-01-31 09:53:12 -080068 int64_t latest_return_time_ms =
69 clock_->TimeInMilliseconds() + max_wait_time_ms;
philipel504c47d2016-06-30 17:33:02 +020070 int64_t wait_ms = max_wait_time_ms;
philipel29f730e2017-03-15 08:10:08 -070071 int64_t now_ms = 0;
philipele0b2f152016-09-28 10:23:49 +020072
73 do {
philipel29f730e2017-03-15 08:10:08 -070074 now_ms = clock_->TimeInMilliseconds();
philipel504c47d2016-06-30 17:33:02 +020075 {
76 rtc::CritScope lock(&crit_);
tommi0a735642017-03-14 06:23:57 -070077 new_continuous_frame_event_.Reset();
philipel29f730e2017-03-15 08:10:08 -070078 if (stopped_)
79 return kStopped;
80
81 wait_ms = max_wait_time_ms;
82
philipele0b2f152016-09-28 10:23:49 +020083 // Need to hold |crit_| in order to use |frames_|, therefore we
84 // set it here in the loop instead of outside the loop in order to not
85 // acquire the lock unnecesserily.
philipel1c056252017-01-31 09:53:12 -080086 next_frame_it_ = frames_.end();
philipelbe7a9e52016-05-19 12:19:35 +020087
philipele0b2f152016-09-28 10:23:49 +020088 // |frame_it| points to the first frame after the
89 // |last_decoded_frame_it_|.
90 auto frame_it = frames_.end();
91 if (last_decoded_frame_it_ == frames_.end()) {
92 frame_it = frames_.begin();
philipelbe7a9e52016-05-19 12:19:35 +020093 } else {
philipele0b2f152016-09-28 10:23:49 +020094 frame_it = last_decoded_frame_it_;
95 ++frame_it;
philipelbe7a9e52016-05-19 12:19:35 +020096 }
philipele0b2f152016-09-28 10:23:49 +020097
98 // |continuous_end_it| points to the first frame after the
99 // |last_continuous_frame_it_|.
100 auto continuous_end_it = last_continuous_frame_it_;
101 if (continuous_end_it != frames_.end())
102 ++continuous_end_it;
103
philipel146a48b2017-04-20 04:04:38 -0700104 for (; frame_it != continuous_end_it && frame_it != frames_.end();
105 ++frame_it) {
philipel93e451b2016-10-06 12:25:13 +0200106 if (!frame_it->second.continuous ||
107 frame_it->second.num_missing_decodable > 0) {
philipele0b2f152016-09-28 10:23:49 +0200108 continue;
philipel93e451b2016-10-06 12:25:13 +0200109 }
philipele0b2f152016-09-28 10:23:49 +0200110
philipele7c891f2018-02-22 14:35:06 +0100111 EncodedFrame* frame = frame_it->second.frame.get();
philipel3042c2d2017-08-18 04:55:02 -0700112
113 if (keyframe_required && !frame->is_keyframe())
114 continue;
115
philipel6d216502018-10-22 14:36:45 +0200116 if (last_decoded_frame_timestamp_ &&
117 AheadOf(*last_decoded_frame_timestamp_, frame->Timestamp())) {
118 continue;
119 }
120
philipel1c056252017-01-31 09:53:12 -0800121 next_frame_it_ = frame_it;
philipel6d216502018-10-22 14:36:45 +0200122 if (frame->RenderTime() == -1) {
Niels Möller23775882018-08-16 10:24:12 +0200123 frame->SetRenderTime(
124 timing_->RenderTimeMs(frame->Timestamp(), now_ms));
philipel6d216502018-10-22 14:36:45 +0200125 }
philipele0b2f152016-09-28 10:23:49 +0200126 wait_ms = timing_->MaxWaitingTime(frame->RenderTime(), now_ms);
127
128 // This will cause the frame buffer to prefer high framerate rather
129 // than high resolution in the case of the decoder not decoding fast
130 // enough and the stream has multiple spatial and temporal layers.
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100131 // For multiple temporal layers it may cause non-base layer frames to be
132 // skipped if they are late.
Ilya Nikolaevskiy7eef0072018-02-28 09:59:26 +0100133 if (wait_ms < -kMaxAllowedFrameDelayMs)
philipele0b2f152016-09-28 10:23:49 +0200134 continue;
135
136 break;
137 }
138 } // rtc::Critscope lock(&crit_);
139
philipel1c056252017-01-31 09:53:12 -0800140 wait_ms = std::min<int64_t>(wait_ms, latest_return_time_ms - now_ms);
philipele0b2f152016-09-28 10:23:49 +0200141 wait_ms = std::max<int64_t>(wait_ms, 0);
tommi0a735642017-03-14 06:23:57 -0700142 } while (new_continuous_frame_event_.Wait(wait_ms));
philipele0b2f152016-09-28 10:23:49 +0200143
philipel29f730e2017-03-15 08:10:08 -0700144 {
145 rtc::CritScope lock(&crit_);
146 now_ms = clock_->TimeInMilliseconds();
147 if (next_frame_it_ != frames_.end()) {
philipele7c891f2018-02-22 14:35:06 +0100148 std::unique_ptr<EncodedFrame> frame =
philipel29f730e2017-03-15 08:10:08 -0700149 std::move(next_frame_it_->second.frame);
philipele0b2f152016-09-28 10:23:49 +0200150
philipel29f730e2017-03-15 08:10:08 -0700151 if (!frame->delayed_by_retransmission()) {
152 int64_t frame_delay;
philipele0754302017-01-25 08:56:23 -0800153
Niels Möller23775882018-08-16 10:24:12 +0200154 if (inter_frame_delay_.CalculateDelay(frame->Timestamp(), &frame_delay,
philipel29f730e2017-03-15 08:10:08 -0700155 frame->ReceivedTime())) {
156 jitter_estimator_->UpdateEstimate(frame_delay, frame->size());
157 }
158
159 float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0;
“Michaelf9fc1712018-08-27 10:08:58 -0500160 if (RttMultExperiment::RttMultEnabled()) {
161 rtt_mult = RttMultExperiment::GetRttMultValue();
162 }
philipel29f730e2017-03-15 08:10:08 -0700163 timing_->SetJitterDelay(jitter_estimator_->GetJitterEstimate(rtt_mult));
164 timing_->UpdateCurrentDelay(frame->RenderTime(), now_ms);
philipele21be1d2017-09-25 06:37:12 -0700165 } else {
“Michaelf9fc1712018-08-27 10:08:58 -0500166 if (RttMultExperiment::RttMultEnabled() ||
167 webrtc::field_trial::IsEnabled("WebRTC-AddRttToPlayoutDelay"))
philipel707f2782017-10-02 14:10:28 +0200168 jitter_estimator_->FrameNacked();
philipele0754302017-01-25 08:56:23 -0800169 }
170
stefan95e97542017-05-23 09:52:18 -0700171 // Gracefully handle bad RTP timestamps and render time issues.
172 if (HasBadRenderTiming(*frame, now_ms)) {
173 jitter_estimator_->Reset();
174 timing_->Reset();
Niels Möller23775882018-08-16 10:24:12 +0200175 frame->SetRenderTime(timing_->RenderTimeMs(frame->Timestamp(), now_ms));
stefan95e97542017-05-23 09:52:18 -0700176 }
177
philipel29f730e2017-03-15 08:10:08 -0700178 UpdateJitterDelay();
ilnik2edc6842017-07-06 03:06:50 -0700179 UpdateTimingFrameInfo();
philipel29f730e2017-03-15 08:10:08 -0700180 PropagateDecodability(next_frame_it_->second);
brandtr9078d8c2017-04-27 07:07:27 -0700181
philipel29f730e2017-03-15 08:10:08 -0700182 AdvanceLastDecodedFrame(next_frame_it_);
Niels Möller23775882018-08-16 10:24:12 +0200183 last_decoded_frame_timestamp_ = frame->Timestamp();
philipel29f730e2017-03-15 08:10:08 -0700184 *frame_out = std::move(frame);
185 return kFrameFound;
philipelbe7a9e52016-05-19 12:19:35 +0200186 }
tommi0a735642017-03-14 06:23:57 -0700187 }
188
189 if (latest_return_time_ms - now_ms > 0) {
philipel1c056252017-01-31 09:53:12 -0800190 // If |next_frame_it_ == frames_.end()| and there is still time left, it
191 // means that the frame buffer was cleared as the thread in this function
192 // was waiting to acquire |crit_| in order to return. Wait for the
193 // remaining time and then return.
194 return NextFrame(latest_return_time_ms - now_ms, frame_out);
philipelbe7a9e52016-05-19 12:19:35 +0200195 }
tommi0a735642017-03-14 06:23:57 -0700196
197 return kTimeout;
philipelbe7a9e52016-05-19 12:19:35 +0200198}
199
philipele7c891f2018-02-22 14:35:06 +0100200bool FrameBuffer::HasBadRenderTiming(const EncodedFrame& frame,
201 int64_t now_ms) {
stefan95e97542017-05-23 09:52:18 -0700202 // Assume that render timing errors are due to changes in the video stream.
203 int64_t render_time_ms = frame.RenderTimeMs();
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200204 // Zero render time means render immediately.
205 if (render_time_ms == 0) {
206 return false;
207 }
stefan95e97542017-05-23 09:52:18 -0700208 if (render_time_ms < 0) {
209 return true;
210 }
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200211 const int64_t kMaxVideoDelayMs = 10000;
stefan95e97542017-05-23 09:52:18 -0700212 if (std::abs(render_time_ms - now_ms) > kMaxVideoDelayMs) {
213 int frame_delay = static_cast<int>(std::abs(render_time_ms - now_ms));
Mirko Bonadei675513b2017-11-09 11:09:25 +0100214 RTC_LOG(LS_WARNING)
215 << "A frame about to be decoded is out of the configured "
216 << "delay bounds (" << frame_delay << " > " << kMaxVideoDelayMs
217 << "). Resetting the video jitter buffer.";
stefan95e97542017-05-23 09:52:18 -0700218 return true;
219 }
220 if (static_cast<int>(timing_->TargetVideoDelay()) > kMaxVideoDelayMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100221 RTC_LOG(LS_WARNING) << "The video target delay has grown larger than "
222 << kMaxVideoDelayMs << " ms.";
stefan95e97542017-05-23 09:52:18 -0700223 return true;
224 }
225 return false;
226}
227
philipel4f6cd6a2016-08-03 10:59:32 +0200228void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) {
tommidb23ea62017-03-03 07:21:18 -0800229 TRACE_EVENT0("webrtc", "FrameBuffer::SetProtectionMode");
philipel4f6cd6a2016-08-03 10:59:32 +0200230 rtc::CritScope lock(&crit_);
231 protection_mode_ = mode;
232}
233
philipel504c47d2016-06-30 17:33:02 +0200234void FrameBuffer::Start() {
tommidb23ea62017-03-03 07:21:18 -0800235 TRACE_EVENT0("webrtc", "FrameBuffer::Start");
philipel29f730e2017-03-15 08:10:08 -0700236 rtc::CritScope lock(&crit_);
237 stopped_ = false;
philipel504c47d2016-06-30 17:33:02 +0200238}
239
240void FrameBuffer::Stop() {
tommidb23ea62017-03-03 07:21:18 -0800241 TRACE_EVENT0("webrtc", "FrameBuffer::Stop");
philipel29f730e2017-03-15 08:10:08 -0700242 rtc::CritScope lock(&crit_);
243 stopped_ = true;
tommi0a735642017-03-14 06:23:57 -0700244 new_continuous_frame_event_.Set();
philipel504c47d2016-06-30 17:33:02 +0200245}
246
philipele21be1d2017-09-25 06:37:12 -0700247void FrameBuffer::UpdateRtt(int64_t rtt_ms) {
248 rtc::CritScope lock(&crit_);
249 jitter_estimator_->UpdateRtt(rtt_ms);
250}
251
philipele7c891f2018-02-22 14:35:06 +0100252bool FrameBuffer::ValidReferences(const EncodedFrame& frame) const {
philipel0fa82a62018-03-19 15:34:53 +0100253 if (frame.id.picture_id < 0)
philipel3b3c9c42017-09-11 09:38:36 -0700254 return false;
255
philipel112adf92017-06-15 09:06:21 -0700256 for (size_t i = 0; i < frame.num_references; ++i) {
philipel0fa82a62018-03-19 15:34:53 +0100257 if (frame.references[i] < 0 || frame.references[i] >= frame.id.picture_id)
philipel112adf92017-06-15 09:06:21 -0700258 return false;
philipel3b3c9c42017-09-11 09:38:36 -0700259
philipel112adf92017-06-15 09:06:21 -0700260 for (size_t j = i + 1; j < frame.num_references; ++j) {
261 if (frame.references[i] == frame.references[j])
262 return false;
263 }
264 }
265
philipel0fa82a62018-03-19 15:34:53 +0100266 if (frame.inter_layer_predicted && frame.id.spatial_layer == 0)
philipel112adf92017-06-15 09:06:21 -0700267 return false;
268
269 return true;
270}
271
philipele7c891f2018-02-22 14:35:06 +0100272void FrameBuffer::UpdatePlayoutDelays(const EncodedFrame& frame) {
gnishb2a318b2017-05-10 09:21:33 -0700273 TRACE_EVENT0("webrtc", "FrameBuffer::UpdatePlayoutDelays");
274 PlayoutDelay playout_delay = frame.EncodedImage().playout_delay_;
275 if (playout_delay.min_ms >= 0)
276 timing_->set_min_playout_delay(playout_delay.min_ms);
277
278 if (playout_delay.max_ms >= 0)
279 timing_->set_max_playout_delay(playout_delay.max_ms);
philipel0a9f6de2018-02-28 11:29:47 +0100280
281 if (!frame.delayed_by_retransmission())
Niels Möller23775882018-08-16 10:24:12 +0200282 timing_->IncomingTimestamp(frame.Timestamp(), frame.ReceivedTime());
gnishb2a318b2017-05-10 09:21:33 -0700283}
284
philipele7c891f2018-02-22 14:35:06 +0100285int64_t FrameBuffer::InsertFrame(std::unique_ptr<EncodedFrame> frame) {
tommidb23ea62017-03-03 07:21:18 -0800286 TRACE_EVENT0("webrtc", "FrameBuffer::InsertFrame");
philipel93e451b2016-10-06 12:25:13 +0200287 RTC_DCHECK(frame);
philipela45102f2017-02-22 05:30:39 -0800288 if (stats_callback_)
ilnik6d5b4d62017-08-30 03:32:14 -0700289 stats_callback_->OnCompleteFrame(frame->is_keyframe(), frame->size(),
290 frame->contentType());
philipel0fa82a62018-03-19 15:34:53 +0100291 const VideoLayerFrameId& id = frame->id;
tommi0a735642017-03-14 06:23:57 -0700292
293 rtc::CritScope lock(&crit_);
philipel29f730e2017-03-15 08:10:08 -0700294
philipel1610f942017-12-12 13:58:31 +0100295 int64_t last_continuous_picture_id =
philipele0b2f152016-09-28 10:23:49 +0200296 last_continuous_frame_it_ == frames_.end()
297 ? -1
298 : last_continuous_frame_it_->first.picture_id;
299
philipel112adf92017-06-15 09:06:21 -0700300 if (!ValidReferences(*frame)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100301 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100302 << id.picture_id << ":"
303 << static_cast<int>(id.spatial_layer)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100304 << ") has invalid frame references, dropping frame.";
philipel112adf92017-06-15 09:06:21 -0700305 return last_continuous_picture_id;
306 }
307
philipele0b2f152016-09-28 10:23:49 +0200308 if (num_frames_buffered_ >= kMaxFramesBuffered) {
philipel9771c502018-03-02 11:06:27 +0100309 if (frame->is_keyframe()) {
310 RTC_LOG(LS_WARNING) << "Inserting keyframe (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100311 << id.picture_id << ":"
312 << static_cast<int>(id.spatial_layer)
philipel9771c502018-03-02 11:06:27 +0100313 << ") but buffer is full, clearing"
314 << " buffer and inserting the frame.";
315 ClearFramesAndHistory();
316 } else {
317 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100318 << id.picture_id << ":"
319 << static_cast<int>(id.spatial_layer)
philipel9771c502018-03-02 11:06:27 +0100320 << ") could not be inserted due to the frame "
321 << "buffer being full, dropping frame.";
322 return last_continuous_picture_id;
323 }
philipele0b2f152016-09-28 10:23:49 +0200324 }
325
philipele0b2f152016-09-28 10:23:49 +0200326 if (last_decoded_frame_it_ != frames_.end() &&
philipel0fa82a62018-03-19 15:34:53 +0100327 id <= last_decoded_frame_it_->first) {
philipel6d216502018-10-22 14:36:45 +0200328 if (AheadOf(frame->Timestamp(), *last_decoded_frame_timestamp_) &&
philipel3042c2d2017-08-18 04:55:02 -0700329 frame->is_keyframe()) {
philipelfcc60062017-01-18 05:35:20 -0800330 // If this frame has a newer timestamp but an earlier picture id then we
331 // assume there has been a jump in the picture id due to some encoder
332 // reconfiguration or some other reason. Even though this is not according
333 // to spec we can still continue to decode from this frame if it is a
334 // keyframe.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100335 RTC_LOG(LS_WARNING)
336 << "A jump in picture id was detected, clearing buffer.";
philipelfcc60062017-01-18 05:35:20 -0800337 ClearFramesAndHistory();
338 last_continuous_picture_id = -1;
339 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100340 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100341 << id.picture_id << ":"
342 << static_cast<int>(id.spatial_layer)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100343 << ") inserted after frame ("
344 << last_decoded_frame_it_->first.picture_id << ":"
345 << static_cast<int>(
346 last_decoded_frame_it_->first.spatial_layer)
347 << ") was handed off for decoding, dropping frame.";
philipelfcc60062017-01-18 05:35:20 -0800348 return last_continuous_picture_id;
349 }
philipele0b2f152016-09-28 10:23:49 +0200350 }
351
philipel146a48b2017-04-20 04:04:38 -0700352 // Test if inserting this frame would cause the order of the frames to become
353 // ambiguous (covering more than half the interval of 2^16). This can happen
354 // when the picture id make large jumps mid stream.
philipel0fa82a62018-03-19 15:34:53 +0100355 if (!frames_.empty() && id < frames_.begin()->first &&
356 frames_.rbegin()->first < id) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100357 RTC_LOG(LS_WARNING)
358 << "A jump in picture id was detected, clearing buffer.";
philipel146a48b2017-04-20 04:04:38 -0700359 ClearFramesAndHistory();
360 last_continuous_picture_id = -1;
361 }
362
philipel0fa82a62018-03-19 15:34:53 +0100363 auto info = frames_.emplace(id, FrameInfo()).first;
philipele0b2f152016-09-28 10:23:49 +0200364
philipel93e451b2016-10-06 12:25:13 +0200365 if (info->second.frame) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100366 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100367 << id.picture_id << ":"
368 << static_cast<int>(id.spatial_layer)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100369 << ") already inserted, dropping frame.";
philipele0b2f152016-09-28 10:23:49 +0200370 return last_continuous_picture_id;
371 }
372
philipel93e451b2016-10-06 12:25:13 +0200373 if (!UpdateFrameInfoWithIncomingFrame(*frame, info))
374 return last_continuous_picture_id;
gnishb2a318b2017-05-10 09:21:33 -0700375 UpdatePlayoutDelays(*frame);
philipel0a9f6de2018-02-28 11:29:47 +0100376
philipele0b2f152016-09-28 10:23:49 +0200377 info->second.frame = std::move(frame);
378 ++num_frames_buffered_;
379
380 if (info->second.num_missing_continuous == 0) {
381 info->second.continuous = true;
382 PropagateContinuity(info);
383 last_continuous_picture_id = last_continuous_frame_it_->first.picture_id;
384
385 // Since we now have new continuous frames there might be a better frame
386 // to return from NextFrame. Signal that thread so that it again can choose
387 // which frame to return.
tommi0a735642017-03-14 06:23:57 -0700388 new_continuous_frame_event_.Set();
philipele0b2f152016-09-28 10:23:49 +0200389 }
390
391 return last_continuous_picture_id;
philipelbe7a9e52016-05-19 12:19:35 +0200392}
393
philipele0b2f152016-09-28 10:23:49 +0200394void FrameBuffer::PropagateContinuity(FrameMap::iterator start) {
tommidb23ea62017-03-03 07:21:18 -0800395 TRACE_EVENT0("webrtc", "FrameBuffer::PropagateContinuity");
philipele0b2f152016-09-28 10:23:49 +0200396 RTC_DCHECK(start->second.continuous);
397 if (last_continuous_frame_it_ == frames_.end())
398 last_continuous_frame_it_ = start;
399
400 std::queue<FrameMap::iterator> continuous_frames;
401 continuous_frames.push(start);
402
403 // A simple BFS to traverse continuous frames.
404 while (!continuous_frames.empty()) {
405 auto frame = continuous_frames.front();
406 continuous_frames.pop();
407
408 if (last_continuous_frame_it_->first < frame->first)
409 last_continuous_frame_it_ = frame;
410
411 // Loop through all dependent frames, and if that frame no longer has
412 // any unfulfilled dependencies then that frame is continuous as well.
413 for (size_t d = 0; d < frame->second.num_dependent_frames; ++d) {
414 auto frame_ref = frames_.find(frame->second.dependent_frames[d]);
philipel112adf92017-06-15 09:06:21 -0700415 RTC_DCHECK(frame_ref != frames_.end());
philipele0b2f152016-09-28 10:23:49 +0200416
philipel112adf92017-06-15 09:06:21 -0700417 // TODO(philipel): Look into why we've seen this happen.
418 if (frame_ref != frames_.end()) {
419 --frame_ref->second.num_missing_continuous;
420 if (frame_ref->second.num_missing_continuous == 0) {
421 frame_ref->second.continuous = true;
422 continuous_frames.push(frame_ref);
423 }
philipele0b2f152016-09-28 10:23:49 +0200424 }
425 }
426 }
427}
428
429void FrameBuffer::PropagateDecodability(const FrameInfo& info) {
tommidb23ea62017-03-03 07:21:18 -0800430 TRACE_EVENT0("webrtc", "FrameBuffer::PropagateDecodability");
tommie95b78b2017-05-14 07:23:11 -0700431 RTC_CHECK(info.num_dependent_frames < FrameInfo::kMaxNumDependentFrames);
philipele0b2f152016-09-28 10:23:49 +0200432 for (size_t d = 0; d < info.num_dependent_frames; ++d) {
433 auto ref_info = frames_.find(info.dependent_frames[d]);
philipel93e451b2016-10-06 12:25:13 +0200434 RTC_DCHECK(ref_info != frames_.end());
tommie95b78b2017-05-14 07:23:11 -0700435 // TODO(philipel): Look into why we've seen this happen.
436 if (ref_info != frames_.end()) {
437 RTC_DCHECK_GT(ref_info->second.num_missing_decodable, 0U);
438 --ref_info->second.num_missing_decodable;
439 }
philipele0b2f152016-09-28 10:23:49 +0200440 }
441}
442
443void FrameBuffer::AdvanceLastDecodedFrame(FrameMap::iterator decoded) {
tommidb23ea62017-03-03 07:21:18 -0800444 TRACE_EVENT0("webrtc", "FrameBuffer::AdvanceLastDecodedFrame");
philipele0b2f152016-09-28 10:23:49 +0200445 if (last_decoded_frame_it_ == frames_.end()) {
446 last_decoded_frame_it_ = frames_.begin();
447 } else {
448 RTC_DCHECK(last_decoded_frame_it_->first < decoded->first);
449 ++last_decoded_frame_it_;
450 }
451 --num_frames_buffered_;
452 ++num_frames_history_;
453
454 // First, delete non-decoded frames from the history.
455 while (last_decoded_frame_it_ != decoded) {
456 if (last_decoded_frame_it_->second.frame)
457 --num_frames_buffered_;
458 last_decoded_frame_it_ = frames_.erase(last_decoded_frame_it_);
philipelbe7a9e52016-05-19 12:19:35 +0200459 }
460
philipele0b2f152016-09-28 10:23:49 +0200461 // Then remove old history if we have too much history saved.
462 if (num_frames_history_ > kMaxFramesHistory) {
463 frames_.erase(frames_.begin());
464 --num_frames_history_;
465 }
466}
467
philipele7c891f2018-02-22 14:35:06 +0100468bool FrameBuffer::UpdateFrameInfoWithIncomingFrame(const EncodedFrame& frame,
philipele0b2f152016-09-28 10:23:49 +0200469 FrameMap::iterator info) {
tommidb23ea62017-03-03 07:21:18 -0800470 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateFrameInfoWithIncomingFrame");
philipel0fa82a62018-03-19 15:34:53 +0100471 const VideoLayerFrameId& id = frame.id;
philipele0b2f152016-09-28 10:23:49 +0200472
473 RTC_DCHECK(last_decoded_frame_it_ == frames_.end() ||
474 last_decoded_frame_it_->first < info->first);
475
philipel798b2822018-06-11 13:10:14 +0200476 // In this function we determine how many missing dependencies this |frame|
477 // has to become continuous/decodable. If a frame that this |frame| depend
478 // on has already been decoded then we can ignore that dependency since it has
479 // already been fulfilled.
480 //
481 // For all other frames we will register a backwards reference to this |frame|
482 // so that |num_missing_continuous| and |num_missing_decodable| can be
483 // decremented as frames become continuous/are decoded.
484 struct Dependency {
485 VideoLayerFrameId id;
486 bool continuous;
487 };
488 std::vector<Dependency> not_yet_fulfilled_dependencies;
489
490 // Find all dependencies that have not yet been fulfilled.
philipele0b2f152016-09-28 10:23:49 +0200491 for (size_t i = 0; i < frame.num_references; ++i) {
philipel0fa82a62018-03-19 15:34:53 +0100492 VideoLayerFrameId ref_key(frame.references[i], frame.id.spatial_layer);
philipele0b2f152016-09-28 10:23:49 +0200493 auto ref_info = frames_.find(ref_key);
494
philipel798b2822018-06-11 13:10:14 +0200495 // Does |frame| depend on a frame earlier than the last decoded one?
philipele0b2f152016-09-28 10:23:49 +0200496 if (last_decoded_frame_it_ != frames_.end() &&
497 ref_key <= last_decoded_frame_it_->first) {
philipel798b2822018-06-11 13:10:14 +0200498 // Was that frame decoded? If not, this |frame| will never become
499 // decodable.
philipele0b2f152016-09-28 10:23:49 +0200500 if (ref_info == frames_.end()) {
philipel65e1f942017-07-24 08:26:53 -0700501 int64_t now_ms = clock_->TimeInMilliseconds();
502 if (last_log_non_decoded_ms_ + kLogNonDecodedIntervalMs < now_ms) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100503 RTC_LOG(LS_WARNING)
philipel0fa82a62018-03-19 15:34:53 +0100504 << "Frame with (picture_id:spatial_id) (" << id.picture_id << ":"
505 << static_cast<int>(id.spatial_layer)
philipel65e1f942017-07-24 08:26:53 -0700506 << ") depends on a non-decoded frame more previous than"
507 << " the last decoded frame, dropping frame.";
508 last_log_non_decoded_ms_ = now_ms;
509 }
philipele0b2f152016-09-28 10:23:49 +0200510 return false;
511 }
philipele0b2f152016-09-28 10:23:49 +0200512 } else {
philipel798b2822018-06-11 13:10:14 +0200513 bool ref_continuous =
514 ref_info != frames_.end() && ref_info->second.continuous;
515 not_yet_fulfilled_dependencies.push_back({ref_key, ref_continuous});
philipele0b2f152016-09-28 10:23:49 +0200516 }
philipelbe7a9e52016-05-19 12:19:35 +0200517 }
518
philipel798b2822018-06-11 13:10:14 +0200519 // Does |frame| depend on the lower spatial layer?
philipelbe7a9e52016-05-19 12:19:35 +0200520 if (frame.inter_layer_predicted) {
philipel0fa82a62018-03-19 15:34:53 +0100521 VideoLayerFrameId ref_key(frame.id.picture_id, frame.id.spatial_layer - 1);
philipel798b2822018-06-11 13:10:14 +0200522 auto ref_info = frames_.find(ref_key);
philipele0b2f152016-09-28 10:23:49 +0200523
philipel798b2822018-06-11 13:10:14 +0200524 bool lower_layer_continuous =
525 ref_info != frames_.end() && ref_info->second.continuous;
526 bool lower_layer_decoded = last_decoded_frame_it_ != frames_.end() &&
527 last_decoded_frame_it_->first == ref_key;
528
529 if (!lower_layer_continuous || !lower_layer_decoded) {
530 not_yet_fulfilled_dependencies.push_back(
531 {ref_key, lower_layer_continuous});
philipele0b2f152016-09-28 10:23:49 +0200532 }
philipelbe7a9e52016-05-19 12:19:35 +0200533 }
534
philipel798b2822018-06-11 13:10:14 +0200535 info->second.num_missing_continuous = not_yet_fulfilled_dependencies.size();
536 info->second.num_missing_decodable = not_yet_fulfilled_dependencies.size();
537
538 for (const Dependency& dep : not_yet_fulfilled_dependencies) {
539 if (dep.continuous)
540 --info->second.num_missing_continuous;
541
542 // At this point we know we want to insert this frame, so here we
543 // intentionally get or create the FrameInfo for this dependency.
544 FrameInfo* dep_info = &frames_[dep.id];
545
546 if (dep_info->num_dependent_frames <
547 (FrameInfo::kMaxNumDependentFrames - 1)) {
548 dep_info->dependent_frames[dep_info->num_dependent_frames] = id;
549 ++dep_info->num_dependent_frames;
550 } else {
551 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
552 << dep.id.picture_id << ":"
553 << static_cast<int>(dep.id.spatial_layer)
554 << ") is referenced by too many frames.";
555 }
556 }
philipel93e451b2016-10-06 12:25:13 +0200557
philipelbe7a9e52016-05-19 12:19:35 +0200558 return true;
559}
560
philipelbe742702016-11-30 01:31:40 -0800561void FrameBuffer::UpdateJitterDelay() {
tommidb23ea62017-03-03 07:21:18 -0800562 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateJitterDelay");
philipela45102f2017-02-22 05:30:39 -0800563 if (!stats_callback_)
564 return;
philipelbe742702016-11-30 01:31:40 -0800565
philipela45102f2017-02-22 05:30:39 -0800566 int decode_ms;
567 int max_decode_ms;
568 int current_delay_ms;
569 int target_delay_ms;
570 int jitter_buffer_ms;
571 int min_playout_delay_ms;
572 int render_delay_ms;
573 if (timing_->GetTimings(&decode_ms, &max_decode_ms, &current_delay_ms,
574 &target_delay_ms, &jitter_buffer_ms,
575 &min_playout_delay_ms, &render_delay_ms)) {
576 stats_callback_->OnFrameBufferTimingsUpdated(
577 decode_ms, max_decode_ms, current_delay_ms, target_delay_ms,
578 jitter_buffer_ms, min_playout_delay_ms, render_delay_ms);
philipelbe742702016-11-30 01:31:40 -0800579 }
philipel266f0a42016-11-28 08:49:07 -0800580}
581
ilnik2edc6842017-07-06 03:06:50 -0700582void FrameBuffer::UpdateTimingFrameInfo() {
583 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateTimingFrameInfo");
Danil Chapovalov0040b662018-06-18 10:48:16 +0200584 absl::optional<TimingFrameInfo> info = timing_->GetTimingFrameInfo();
philipel97187112018-03-23 10:43:21 +0100585 if (info && stats_callback_)
ilnik2edc6842017-07-06 03:06:50 -0700586 stats_callback_->OnTimingFrameInfoUpdated(*info);
587}
588
philipelfcc60062017-01-18 05:35:20 -0800589void FrameBuffer::ClearFramesAndHistory() {
ilnik2edc6842017-07-06 03:06:50 -0700590 TRACE_EVENT0("webrtc", "FrameBuffer::ClearFramesAndHistory");
philipelfcc60062017-01-18 05:35:20 -0800591 frames_.clear();
592 last_decoded_frame_it_ = frames_.end();
593 last_continuous_frame_it_ = frames_.end();
philipel1c056252017-01-31 09:53:12 -0800594 next_frame_it_ = frames_.end();
philipelfcc60062017-01-18 05:35:20 -0800595 num_frames_history_ = 0;
596 num_frames_buffered_ = 0;
597}
598
Niels Möllerbe682d42018-03-27 08:31:45 +0200599FrameBuffer::FrameInfo::FrameInfo() = default;
600FrameBuffer::FrameInfo::FrameInfo(FrameInfo&&) = default;
601FrameBuffer::FrameInfo::~FrameInfo() = default;
602
philipelbe7a9e52016-05-19 12:19:35 +0200603} // namespace video_coding
604} // namespace webrtc