blob: 3a25bf0e93ffde891de3c7c5def0b7163b553209 [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),
tommi0a735642017-03-14 06:23:57 -070049 new_continuous_frame_event_(false, false),
philipelbe7a9e52016-05-19 12:19:35 +020050 jitter_estimator_(jitter_estimator),
51 timing_(timing),
philipel4f6cd6a2016-08-03 10:59:32 +020052 inter_frame_delay_(clock_->TimeInMilliseconds()),
philipele0b2f152016-09-28 10:23:49 +020053 last_decoded_frame_it_(frames_.end()),
54 last_continuous_frame_it_(frames_.end()),
55 num_frames_history_(0),
56 num_frames_buffered_(0),
philipel29f730e2017-03-15 08:10:08 -070057 stopped_(false),
philipela45102f2017-02-22 05:30:39 -080058 protection_mode_(kProtectionNack),
philipel65e1f942017-07-24 08:26:53 -070059 stats_callback_(stats_callback),
60 last_log_non_decoded_ms_(-kLogNonDecodedIntervalMs) {}
philipel266f0a42016-11-28 08:49:07 -080061
philipela45102f2017-02-22 05:30:39 -080062FrameBuffer::~FrameBuffer() {}
philipelbe7a9e52016-05-19 12:19:35 +020063
philipel75562822016-09-05 10:57:41 +020064FrameBuffer::ReturnReason FrameBuffer::NextFrame(
65 int64_t max_wait_time_ms,
philipele7c891f2018-02-22 14:35:06 +010066 std::unique_ptr<EncodedFrame>* frame_out,
philipel3042c2d2017-08-18 04:55:02 -070067 bool keyframe_required) {
tommidb23ea62017-03-03 07:21:18 -080068 TRACE_EVENT0("webrtc", "FrameBuffer::NextFrame");
philipel1c056252017-01-31 09:53:12 -080069 int64_t latest_return_time_ms =
70 clock_->TimeInMilliseconds() + max_wait_time_ms;
philipel504c47d2016-06-30 17:33:02 +020071 int64_t wait_ms = max_wait_time_ms;
philipel29f730e2017-03-15 08:10:08 -070072 int64_t now_ms = 0;
philipele0b2f152016-09-28 10:23:49 +020073
74 do {
philipel29f730e2017-03-15 08:10:08 -070075 now_ms = clock_->TimeInMilliseconds();
philipel504c47d2016-06-30 17:33:02 +020076 {
77 rtc::CritScope lock(&crit_);
tommi0a735642017-03-14 06:23:57 -070078 new_continuous_frame_event_.Reset();
philipel29f730e2017-03-15 08:10:08 -070079 if (stopped_)
80 return kStopped;
81
82 wait_ms = max_wait_time_ms;
83
philipele0b2f152016-09-28 10:23:49 +020084 // Need to hold |crit_| in order to use |frames_|, therefore we
85 // set it here in the loop instead of outside the loop in order to not
86 // acquire the lock unnecesserily.
philipel1c056252017-01-31 09:53:12 -080087 next_frame_it_ = frames_.end();
philipelbe7a9e52016-05-19 12:19:35 +020088
philipele0b2f152016-09-28 10:23:49 +020089 // |frame_it| points to the first frame after the
90 // |last_decoded_frame_it_|.
91 auto frame_it = frames_.end();
92 if (last_decoded_frame_it_ == frames_.end()) {
93 frame_it = frames_.begin();
philipelbe7a9e52016-05-19 12:19:35 +020094 } else {
philipele0b2f152016-09-28 10:23:49 +020095 frame_it = last_decoded_frame_it_;
96 ++frame_it;
philipelbe7a9e52016-05-19 12:19:35 +020097 }
philipele0b2f152016-09-28 10:23:49 +020098
99 // |continuous_end_it| points to the first frame after the
100 // |last_continuous_frame_it_|.
101 auto continuous_end_it = last_continuous_frame_it_;
102 if (continuous_end_it != frames_.end())
103 ++continuous_end_it;
104
philipel146a48b2017-04-20 04:04:38 -0700105 for (; frame_it != continuous_end_it && frame_it != frames_.end();
106 ++frame_it) {
philipel93e451b2016-10-06 12:25:13 +0200107 if (!frame_it->second.continuous ||
108 frame_it->second.num_missing_decodable > 0) {
philipele0b2f152016-09-28 10:23:49 +0200109 continue;
philipel93e451b2016-10-06 12:25:13 +0200110 }
philipele0b2f152016-09-28 10:23:49 +0200111
philipele7c891f2018-02-22 14:35:06 +0100112 EncodedFrame* frame = frame_it->second.frame.get();
philipel3042c2d2017-08-18 04:55:02 -0700113
114 if (keyframe_required && !frame->is_keyframe())
115 continue;
116
philipel6d216502018-10-22 14:36:45 +0200117 if (last_decoded_frame_timestamp_ &&
118 AheadOf(*last_decoded_frame_timestamp_, frame->Timestamp())) {
119 continue;
120 }
121
philipel1c056252017-01-31 09:53:12 -0800122 next_frame_it_ = frame_it;
philipel6d216502018-10-22 14:36:45 +0200123 if (frame->RenderTime() == -1) {
Niels Möller23775882018-08-16 10:24:12 +0200124 frame->SetRenderTime(
125 timing_->RenderTimeMs(frame->Timestamp(), now_ms));
philipel6d216502018-10-22 14:36:45 +0200126 }
philipele0b2f152016-09-28 10:23:49 +0200127 wait_ms = timing_->MaxWaitingTime(frame->RenderTime(), now_ms);
128
129 // This will cause the frame buffer to prefer high framerate rather
130 // than high resolution in the case of the decoder not decoding fast
131 // enough and the stream has multiple spatial and temporal layers.
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100132 // For multiple temporal layers it may cause non-base layer frames to be
133 // skipped if they are late.
Ilya Nikolaevskiy7eef0072018-02-28 09:59:26 +0100134 if (wait_ms < -kMaxAllowedFrameDelayMs)
philipele0b2f152016-09-28 10:23:49 +0200135 continue;
136
137 break;
138 }
139 } // rtc::Critscope lock(&crit_);
140
philipel1c056252017-01-31 09:53:12 -0800141 wait_ms = std::min<int64_t>(wait_ms, latest_return_time_ms - now_ms);
philipele0b2f152016-09-28 10:23:49 +0200142 wait_ms = std::max<int64_t>(wait_ms, 0);
tommi0a735642017-03-14 06:23:57 -0700143 } while (new_continuous_frame_event_.Wait(wait_ms));
philipele0b2f152016-09-28 10:23:49 +0200144
philipel29f730e2017-03-15 08:10:08 -0700145 {
146 rtc::CritScope lock(&crit_);
147 now_ms = clock_->TimeInMilliseconds();
148 if (next_frame_it_ != frames_.end()) {
philipele7c891f2018-02-22 14:35:06 +0100149 std::unique_ptr<EncodedFrame> frame =
philipel29f730e2017-03-15 08:10:08 -0700150 std::move(next_frame_it_->second.frame);
philipele0b2f152016-09-28 10:23:49 +0200151
philipel29f730e2017-03-15 08:10:08 -0700152 if (!frame->delayed_by_retransmission()) {
153 int64_t frame_delay;
philipele0754302017-01-25 08:56:23 -0800154
Niels Möller23775882018-08-16 10:24:12 +0200155 if (inter_frame_delay_.CalculateDelay(frame->Timestamp(), &frame_delay,
philipel29f730e2017-03-15 08:10:08 -0700156 frame->ReceivedTime())) {
157 jitter_estimator_->UpdateEstimate(frame_delay, frame->size());
158 }
159
160 float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0;
“Michaelf9fc1712018-08-27 10:08:58 -0500161 if (RttMultExperiment::RttMultEnabled()) {
162 rtt_mult = RttMultExperiment::GetRttMultValue();
163 }
philipel29f730e2017-03-15 08:10:08 -0700164 timing_->SetJitterDelay(jitter_estimator_->GetJitterEstimate(rtt_mult));
165 timing_->UpdateCurrentDelay(frame->RenderTime(), now_ms);
philipele21be1d2017-09-25 06:37:12 -0700166 } else {
“Michaelf9fc1712018-08-27 10:08:58 -0500167 if (RttMultExperiment::RttMultEnabled() ||
168 webrtc::field_trial::IsEnabled("WebRTC-AddRttToPlayoutDelay"))
philipel707f2782017-10-02 14:10:28 +0200169 jitter_estimator_->FrameNacked();
philipele0754302017-01-25 08:56:23 -0800170 }
171
stefan95e97542017-05-23 09:52:18 -0700172 // Gracefully handle bad RTP timestamps and render time issues.
173 if (HasBadRenderTiming(*frame, now_ms)) {
174 jitter_estimator_->Reset();
175 timing_->Reset();
Niels Möller23775882018-08-16 10:24:12 +0200176 frame->SetRenderTime(timing_->RenderTimeMs(frame->Timestamp(), now_ms));
stefan95e97542017-05-23 09:52:18 -0700177 }
178
philipel29f730e2017-03-15 08:10:08 -0700179 UpdateJitterDelay();
ilnik2edc6842017-07-06 03:06:50 -0700180 UpdateTimingFrameInfo();
philipel29f730e2017-03-15 08:10:08 -0700181 PropagateDecodability(next_frame_it_->second);
brandtr9078d8c2017-04-27 07:07:27 -0700182
philipel29f730e2017-03-15 08:10:08 -0700183 AdvanceLastDecodedFrame(next_frame_it_);
Niels Möller23775882018-08-16 10:24:12 +0200184 last_decoded_frame_timestamp_ = frame->Timestamp();
philipel29f730e2017-03-15 08:10:08 -0700185 *frame_out = std::move(frame);
186 return kFrameFound;
philipelbe7a9e52016-05-19 12:19:35 +0200187 }
tommi0a735642017-03-14 06:23:57 -0700188 }
189
190 if (latest_return_time_ms - now_ms > 0) {
philipel1c056252017-01-31 09:53:12 -0800191 // If |next_frame_it_ == frames_.end()| and there is still time left, it
192 // means that the frame buffer was cleared as the thread in this function
193 // was waiting to acquire |crit_| in order to return. Wait for the
194 // remaining time and then return.
195 return NextFrame(latest_return_time_ms - now_ms, frame_out);
philipelbe7a9e52016-05-19 12:19:35 +0200196 }
tommi0a735642017-03-14 06:23:57 -0700197
198 return kTimeout;
philipelbe7a9e52016-05-19 12:19:35 +0200199}
200
philipele7c891f2018-02-22 14:35:06 +0100201bool FrameBuffer::HasBadRenderTiming(const EncodedFrame& frame,
202 int64_t now_ms) {
stefan95e97542017-05-23 09:52:18 -0700203 // Assume that render timing errors are due to changes in the video stream.
204 int64_t render_time_ms = frame.RenderTimeMs();
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200205 // Zero render time means render immediately.
206 if (render_time_ms == 0) {
207 return false;
208 }
stefan95e97542017-05-23 09:52:18 -0700209 if (render_time_ms < 0) {
210 return true;
211 }
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200212 const int64_t kMaxVideoDelayMs = 10000;
stefan95e97542017-05-23 09:52:18 -0700213 if (std::abs(render_time_ms - now_ms) > kMaxVideoDelayMs) {
214 int frame_delay = static_cast<int>(std::abs(render_time_ms - now_ms));
Mirko Bonadei675513b2017-11-09 11:09:25 +0100215 RTC_LOG(LS_WARNING)
216 << "A frame about to be decoded is out of the configured "
217 << "delay bounds (" << frame_delay << " > " << kMaxVideoDelayMs
218 << "). Resetting the video jitter buffer.";
stefan95e97542017-05-23 09:52:18 -0700219 return true;
220 }
221 if (static_cast<int>(timing_->TargetVideoDelay()) > kMaxVideoDelayMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100222 RTC_LOG(LS_WARNING) << "The video target delay has grown larger than "
223 << kMaxVideoDelayMs << " ms.";
stefan95e97542017-05-23 09:52:18 -0700224 return true;
225 }
226 return false;
227}
228
philipel4f6cd6a2016-08-03 10:59:32 +0200229void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) {
tommidb23ea62017-03-03 07:21:18 -0800230 TRACE_EVENT0("webrtc", "FrameBuffer::SetProtectionMode");
philipel4f6cd6a2016-08-03 10:59:32 +0200231 rtc::CritScope lock(&crit_);
232 protection_mode_ = mode;
233}
234
philipel504c47d2016-06-30 17:33:02 +0200235void FrameBuffer::Start() {
tommidb23ea62017-03-03 07:21:18 -0800236 TRACE_EVENT0("webrtc", "FrameBuffer::Start");
philipel29f730e2017-03-15 08:10:08 -0700237 rtc::CritScope lock(&crit_);
238 stopped_ = false;
philipel504c47d2016-06-30 17:33:02 +0200239}
240
241void FrameBuffer::Stop() {
tommidb23ea62017-03-03 07:21:18 -0800242 TRACE_EVENT0("webrtc", "FrameBuffer::Stop");
philipel29f730e2017-03-15 08:10:08 -0700243 rtc::CritScope lock(&crit_);
244 stopped_ = true;
tommi0a735642017-03-14 06:23:57 -0700245 new_continuous_frame_event_.Set();
philipel504c47d2016-06-30 17:33:02 +0200246}
247
philipele21be1d2017-09-25 06:37:12 -0700248void FrameBuffer::UpdateRtt(int64_t rtt_ms) {
249 rtc::CritScope lock(&crit_);
250 jitter_estimator_->UpdateRtt(rtt_ms);
251}
252
philipele7c891f2018-02-22 14:35:06 +0100253bool FrameBuffer::ValidReferences(const EncodedFrame& frame) const {
philipel0fa82a62018-03-19 15:34:53 +0100254 if (frame.id.picture_id < 0)
philipel3b3c9c42017-09-11 09:38:36 -0700255 return false;
256
philipel112adf92017-06-15 09:06:21 -0700257 for (size_t i = 0; i < frame.num_references; ++i) {
philipel0fa82a62018-03-19 15:34:53 +0100258 if (frame.references[i] < 0 || frame.references[i] >= frame.id.picture_id)
philipel112adf92017-06-15 09:06:21 -0700259 return false;
philipel3b3c9c42017-09-11 09:38:36 -0700260
philipel112adf92017-06-15 09:06:21 -0700261 for (size_t j = i + 1; j < frame.num_references; ++j) {
262 if (frame.references[i] == frame.references[j])
263 return false;
264 }
265 }
266
philipel0fa82a62018-03-19 15:34:53 +0100267 if (frame.inter_layer_predicted && frame.id.spatial_layer == 0)
philipel112adf92017-06-15 09:06:21 -0700268 return false;
269
270 return true;
271}
272
philipele7c891f2018-02-22 14:35:06 +0100273void FrameBuffer::UpdatePlayoutDelays(const EncodedFrame& frame) {
gnishb2a318b2017-05-10 09:21:33 -0700274 TRACE_EVENT0("webrtc", "FrameBuffer::UpdatePlayoutDelays");
275 PlayoutDelay playout_delay = frame.EncodedImage().playout_delay_;
276 if (playout_delay.min_ms >= 0)
277 timing_->set_min_playout_delay(playout_delay.min_ms);
278
279 if (playout_delay.max_ms >= 0)
280 timing_->set_max_playout_delay(playout_delay.max_ms);
philipel0a9f6de2018-02-28 11:29:47 +0100281
282 if (!frame.delayed_by_retransmission())
Niels Möller23775882018-08-16 10:24:12 +0200283 timing_->IncomingTimestamp(frame.Timestamp(), frame.ReceivedTime());
gnishb2a318b2017-05-10 09:21:33 -0700284}
285
philipele7c891f2018-02-22 14:35:06 +0100286int64_t FrameBuffer::InsertFrame(std::unique_ptr<EncodedFrame> frame) {
tommidb23ea62017-03-03 07:21:18 -0800287 TRACE_EVENT0("webrtc", "FrameBuffer::InsertFrame");
philipel93e451b2016-10-06 12:25:13 +0200288 RTC_DCHECK(frame);
philipela45102f2017-02-22 05:30:39 -0800289 if (stats_callback_)
ilnik6d5b4d62017-08-30 03:32:14 -0700290 stats_callback_->OnCompleteFrame(frame->is_keyframe(), frame->size(),
291 frame->contentType());
philipel0fa82a62018-03-19 15:34:53 +0100292 const VideoLayerFrameId& id = frame->id;
tommi0a735642017-03-14 06:23:57 -0700293
294 rtc::CritScope lock(&crit_);
philipel29f730e2017-03-15 08:10:08 -0700295
philipel1610f942017-12-12 13:58:31 +0100296 int64_t last_continuous_picture_id =
philipele0b2f152016-09-28 10:23:49 +0200297 last_continuous_frame_it_ == frames_.end()
298 ? -1
299 : last_continuous_frame_it_->first.picture_id;
300
philipel112adf92017-06-15 09:06:21 -0700301 if (!ValidReferences(*frame)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100302 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100303 << id.picture_id << ":"
304 << static_cast<int>(id.spatial_layer)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100305 << ") has invalid frame references, dropping frame.";
philipel112adf92017-06-15 09:06:21 -0700306 return last_continuous_picture_id;
307 }
308
philipele0b2f152016-09-28 10:23:49 +0200309 if (num_frames_buffered_ >= kMaxFramesBuffered) {
philipel9771c502018-03-02 11:06:27 +0100310 if (frame->is_keyframe()) {
311 RTC_LOG(LS_WARNING) << "Inserting keyframe (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100312 << id.picture_id << ":"
313 << static_cast<int>(id.spatial_layer)
philipel9771c502018-03-02 11:06:27 +0100314 << ") but buffer is full, clearing"
315 << " buffer and inserting the frame.";
316 ClearFramesAndHistory();
317 } else {
318 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100319 << id.picture_id << ":"
320 << static_cast<int>(id.spatial_layer)
philipel9771c502018-03-02 11:06:27 +0100321 << ") could not be inserted due to the frame "
322 << "buffer being full, dropping frame.";
323 return last_continuous_picture_id;
324 }
philipele0b2f152016-09-28 10:23:49 +0200325 }
326
philipele0b2f152016-09-28 10:23:49 +0200327 if (last_decoded_frame_it_ != frames_.end() &&
philipel0fa82a62018-03-19 15:34:53 +0100328 id <= last_decoded_frame_it_->first) {
philipel6d216502018-10-22 14:36:45 +0200329 if (AheadOf(frame->Timestamp(), *last_decoded_frame_timestamp_) &&
philipel3042c2d2017-08-18 04:55:02 -0700330 frame->is_keyframe()) {
philipelfcc60062017-01-18 05:35:20 -0800331 // If this frame has a newer timestamp but an earlier picture id then we
332 // assume there has been a jump in the picture id due to some encoder
333 // reconfiguration or some other reason. Even though this is not according
334 // to spec we can still continue to decode from this frame if it is a
335 // keyframe.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100336 RTC_LOG(LS_WARNING)
337 << "A jump in picture id was detected, clearing buffer.";
philipelfcc60062017-01-18 05:35:20 -0800338 ClearFramesAndHistory();
339 last_continuous_picture_id = -1;
340 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100341 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100342 << id.picture_id << ":"
343 << static_cast<int>(id.spatial_layer)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100344 << ") inserted after frame ("
345 << last_decoded_frame_it_->first.picture_id << ":"
346 << static_cast<int>(
347 last_decoded_frame_it_->first.spatial_layer)
348 << ") was handed off for decoding, dropping frame.";
philipelfcc60062017-01-18 05:35:20 -0800349 return last_continuous_picture_id;
350 }
philipele0b2f152016-09-28 10:23:49 +0200351 }
352
philipel146a48b2017-04-20 04:04:38 -0700353 // Test if inserting this frame would cause the order of the frames to become
354 // ambiguous (covering more than half the interval of 2^16). This can happen
355 // when the picture id make large jumps mid stream.
philipel0fa82a62018-03-19 15:34:53 +0100356 if (!frames_.empty() && id < frames_.begin()->first &&
357 frames_.rbegin()->first < id) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100358 RTC_LOG(LS_WARNING)
359 << "A jump in picture id was detected, clearing buffer.";
philipel146a48b2017-04-20 04:04:38 -0700360 ClearFramesAndHistory();
361 last_continuous_picture_id = -1;
362 }
363
philipel0fa82a62018-03-19 15:34:53 +0100364 auto info = frames_.emplace(id, FrameInfo()).first;
philipele0b2f152016-09-28 10:23:49 +0200365
philipel93e451b2016-10-06 12:25:13 +0200366 if (info->second.frame) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100367 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100368 << id.picture_id << ":"
369 << static_cast<int>(id.spatial_layer)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100370 << ") already inserted, dropping frame.";
philipele0b2f152016-09-28 10:23:49 +0200371 return last_continuous_picture_id;
372 }
373
philipel93e451b2016-10-06 12:25:13 +0200374 if (!UpdateFrameInfoWithIncomingFrame(*frame, info))
375 return last_continuous_picture_id;
gnishb2a318b2017-05-10 09:21:33 -0700376 UpdatePlayoutDelays(*frame);
philipel0a9f6de2018-02-28 11:29:47 +0100377
philipele0b2f152016-09-28 10:23:49 +0200378 info->second.frame = std::move(frame);
379 ++num_frames_buffered_;
380
381 if (info->second.num_missing_continuous == 0) {
382 info->second.continuous = true;
383 PropagateContinuity(info);
384 last_continuous_picture_id = last_continuous_frame_it_->first.picture_id;
385
386 // Since we now have new continuous frames there might be a better frame
387 // to return from NextFrame. Signal that thread so that it again can choose
388 // which frame to return.
tommi0a735642017-03-14 06:23:57 -0700389 new_continuous_frame_event_.Set();
philipele0b2f152016-09-28 10:23:49 +0200390 }
391
392 return last_continuous_picture_id;
philipelbe7a9e52016-05-19 12:19:35 +0200393}
394
philipele0b2f152016-09-28 10:23:49 +0200395void FrameBuffer::PropagateContinuity(FrameMap::iterator start) {
tommidb23ea62017-03-03 07:21:18 -0800396 TRACE_EVENT0("webrtc", "FrameBuffer::PropagateContinuity");
philipele0b2f152016-09-28 10:23:49 +0200397 RTC_DCHECK(start->second.continuous);
398 if (last_continuous_frame_it_ == frames_.end())
399 last_continuous_frame_it_ = start;
400
401 std::queue<FrameMap::iterator> continuous_frames;
402 continuous_frames.push(start);
403
404 // A simple BFS to traverse continuous frames.
405 while (!continuous_frames.empty()) {
406 auto frame = continuous_frames.front();
407 continuous_frames.pop();
408
409 if (last_continuous_frame_it_->first < frame->first)
410 last_continuous_frame_it_ = frame;
411
412 // Loop through all dependent frames, and if that frame no longer has
413 // any unfulfilled dependencies then that frame is continuous as well.
414 for (size_t d = 0; d < frame->second.num_dependent_frames; ++d) {
415 auto frame_ref = frames_.find(frame->second.dependent_frames[d]);
philipel112adf92017-06-15 09:06:21 -0700416 RTC_DCHECK(frame_ref != frames_.end());
philipele0b2f152016-09-28 10:23:49 +0200417
philipel112adf92017-06-15 09:06:21 -0700418 // TODO(philipel): Look into why we've seen this happen.
419 if (frame_ref != frames_.end()) {
420 --frame_ref->second.num_missing_continuous;
421 if (frame_ref->second.num_missing_continuous == 0) {
422 frame_ref->second.continuous = true;
423 continuous_frames.push(frame_ref);
424 }
philipele0b2f152016-09-28 10:23:49 +0200425 }
426 }
427 }
428}
429
430void FrameBuffer::PropagateDecodability(const FrameInfo& info) {
tommidb23ea62017-03-03 07:21:18 -0800431 TRACE_EVENT0("webrtc", "FrameBuffer::PropagateDecodability");
tommie95b78b2017-05-14 07:23:11 -0700432 RTC_CHECK(info.num_dependent_frames < FrameInfo::kMaxNumDependentFrames);
philipele0b2f152016-09-28 10:23:49 +0200433 for (size_t d = 0; d < info.num_dependent_frames; ++d) {
434 auto ref_info = frames_.find(info.dependent_frames[d]);
philipel93e451b2016-10-06 12:25:13 +0200435 RTC_DCHECK(ref_info != frames_.end());
tommie95b78b2017-05-14 07:23:11 -0700436 // TODO(philipel): Look into why we've seen this happen.
437 if (ref_info != frames_.end()) {
438 RTC_DCHECK_GT(ref_info->second.num_missing_decodable, 0U);
439 --ref_info->second.num_missing_decodable;
440 }
philipele0b2f152016-09-28 10:23:49 +0200441 }
442}
443
444void FrameBuffer::AdvanceLastDecodedFrame(FrameMap::iterator decoded) {
tommidb23ea62017-03-03 07:21:18 -0800445 TRACE_EVENT0("webrtc", "FrameBuffer::AdvanceLastDecodedFrame");
philipele0b2f152016-09-28 10:23:49 +0200446 if (last_decoded_frame_it_ == frames_.end()) {
447 last_decoded_frame_it_ = frames_.begin();
448 } else {
449 RTC_DCHECK(last_decoded_frame_it_->first < decoded->first);
450 ++last_decoded_frame_it_;
451 }
452 --num_frames_buffered_;
453 ++num_frames_history_;
454
455 // First, delete non-decoded frames from the history.
456 while (last_decoded_frame_it_ != decoded) {
457 if (last_decoded_frame_it_->second.frame)
458 --num_frames_buffered_;
459 last_decoded_frame_it_ = frames_.erase(last_decoded_frame_it_);
philipelbe7a9e52016-05-19 12:19:35 +0200460 }
461
philipele0b2f152016-09-28 10:23:49 +0200462 // Then remove old history if we have too much history saved.
463 if (num_frames_history_ > kMaxFramesHistory) {
464 frames_.erase(frames_.begin());
465 --num_frames_history_;
466 }
467}
468
philipele7c891f2018-02-22 14:35:06 +0100469bool FrameBuffer::UpdateFrameInfoWithIncomingFrame(const EncodedFrame& frame,
philipele0b2f152016-09-28 10:23:49 +0200470 FrameMap::iterator info) {
tommidb23ea62017-03-03 07:21:18 -0800471 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateFrameInfoWithIncomingFrame");
philipel0fa82a62018-03-19 15:34:53 +0100472 const VideoLayerFrameId& id = frame.id;
philipele0b2f152016-09-28 10:23:49 +0200473
474 RTC_DCHECK(last_decoded_frame_it_ == frames_.end() ||
475 last_decoded_frame_it_->first < info->first);
476
philipel798b2822018-06-11 13:10:14 +0200477 // In this function we determine how many missing dependencies this |frame|
478 // has to become continuous/decodable. If a frame that this |frame| depend
479 // on has already been decoded then we can ignore that dependency since it has
480 // already been fulfilled.
481 //
482 // For all other frames we will register a backwards reference to this |frame|
483 // so that |num_missing_continuous| and |num_missing_decodable| can be
484 // decremented as frames become continuous/are decoded.
485 struct Dependency {
486 VideoLayerFrameId id;
487 bool continuous;
488 };
489 std::vector<Dependency> not_yet_fulfilled_dependencies;
490
491 // Find all dependencies that have not yet been fulfilled.
philipele0b2f152016-09-28 10:23:49 +0200492 for (size_t i = 0; i < frame.num_references; ++i) {
philipel0fa82a62018-03-19 15:34:53 +0100493 VideoLayerFrameId ref_key(frame.references[i], frame.id.spatial_layer);
philipele0b2f152016-09-28 10:23:49 +0200494 auto ref_info = frames_.find(ref_key);
495
philipel798b2822018-06-11 13:10:14 +0200496 // Does |frame| depend on a frame earlier than the last decoded one?
philipele0b2f152016-09-28 10:23:49 +0200497 if (last_decoded_frame_it_ != frames_.end() &&
498 ref_key <= last_decoded_frame_it_->first) {
philipel798b2822018-06-11 13:10:14 +0200499 // Was that frame decoded? If not, this |frame| will never become
500 // decodable.
philipele0b2f152016-09-28 10:23:49 +0200501 if (ref_info == frames_.end()) {
philipel65e1f942017-07-24 08:26:53 -0700502 int64_t now_ms = clock_->TimeInMilliseconds();
503 if (last_log_non_decoded_ms_ + kLogNonDecodedIntervalMs < now_ms) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100504 RTC_LOG(LS_WARNING)
philipel0fa82a62018-03-19 15:34:53 +0100505 << "Frame with (picture_id:spatial_id) (" << id.picture_id << ":"
506 << static_cast<int>(id.spatial_layer)
philipel65e1f942017-07-24 08:26:53 -0700507 << ") depends on a non-decoded frame more previous than"
508 << " the last decoded frame, dropping frame.";
509 last_log_non_decoded_ms_ = now_ms;
510 }
philipele0b2f152016-09-28 10:23:49 +0200511 return false;
512 }
philipele0b2f152016-09-28 10:23:49 +0200513 } else {
philipel798b2822018-06-11 13:10:14 +0200514 bool ref_continuous =
515 ref_info != frames_.end() && ref_info->second.continuous;
516 not_yet_fulfilled_dependencies.push_back({ref_key, ref_continuous});
philipele0b2f152016-09-28 10:23:49 +0200517 }
philipelbe7a9e52016-05-19 12:19:35 +0200518 }
519
philipel798b2822018-06-11 13:10:14 +0200520 // Does |frame| depend on the lower spatial layer?
philipelbe7a9e52016-05-19 12:19:35 +0200521 if (frame.inter_layer_predicted) {
philipel0fa82a62018-03-19 15:34:53 +0100522 VideoLayerFrameId ref_key(frame.id.picture_id, frame.id.spatial_layer - 1);
philipel798b2822018-06-11 13:10:14 +0200523 auto ref_info = frames_.find(ref_key);
philipele0b2f152016-09-28 10:23:49 +0200524
philipel798b2822018-06-11 13:10:14 +0200525 bool lower_layer_continuous =
526 ref_info != frames_.end() && ref_info->second.continuous;
527 bool lower_layer_decoded = last_decoded_frame_it_ != frames_.end() &&
528 last_decoded_frame_it_->first == ref_key;
529
530 if (!lower_layer_continuous || !lower_layer_decoded) {
531 not_yet_fulfilled_dependencies.push_back(
532 {ref_key, lower_layer_continuous});
philipele0b2f152016-09-28 10:23:49 +0200533 }
philipelbe7a9e52016-05-19 12:19:35 +0200534 }
535
philipel798b2822018-06-11 13:10:14 +0200536 info->second.num_missing_continuous = not_yet_fulfilled_dependencies.size();
537 info->second.num_missing_decodable = not_yet_fulfilled_dependencies.size();
538
539 for (const Dependency& dep : not_yet_fulfilled_dependencies) {
540 if (dep.continuous)
541 --info->second.num_missing_continuous;
542
543 // At this point we know we want to insert this frame, so here we
544 // intentionally get or create the FrameInfo for this dependency.
545 FrameInfo* dep_info = &frames_[dep.id];
546
547 if (dep_info->num_dependent_frames <
548 (FrameInfo::kMaxNumDependentFrames - 1)) {
549 dep_info->dependent_frames[dep_info->num_dependent_frames] = id;
550 ++dep_info->num_dependent_frames;
551 } else {
552 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
553 << dep.id.picture_id << ":"
554 << static_cast<int>(dep.id.spatial_layer)
555 << ") is referenced by too many frames.";
556 }
557 }
philipel93e451b2016-10-06 12:25:13 +0200558
philipelbe7a9e52016-05-19 12:19:35 +0200559 return true;
560}
561
philipelbe742702016-11-30 01:31:40 -0800562void FrameBuffer::UpdateJitterDelay() {
tommidb23ea62017-03-03 07:21:18 -0800563 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateJitterDelay");
philipela45102f2017-02-22 05:30:39 -0800564 if (!stats_callback_)
565 return;
philipelbe742702016-11-30 01:31:40 -0800566
philipela45102f2017-02-22 05:30:39 -0800567 int decode_ms;
568 int max_decode_ms;
569 int current_delay_ms;
570 int target_delay_ms;
571 int jitter_buffer_ms;
572 int min_playout_delay_ms;
573 int render_delay_ms;
574 if (timing_->GetTimings(&decode_ms, &max_decode_ms, &current_delay_ms,
575 &target_delay_ms, &jitter_buffer_ms,
576 &min_playout_delay_ms, &render_delay_ms)) {
577 stats_callback_->OnFrameBufferTimingsUpdated(
578 decode_ms, max_decode_ms, current_delay_ms, target_delay_ms,
579 jitter_buffer_ms, min_playout_delay_ms, render_delay_ms);
philipelbe742702016-11-30 01:31:40 -0800580 }
philipel266f0a42016-11-28 08:49:07 -0800581}
582
ilnik2edc6842017-07-06 03:06:50 -0700583void FrameBuffer::UpdateTimingFrameInfo() {
584 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateTimingFrameInfo");
Danil Chapovalov0040b662018-06-18 10:48:16 +0200585 absl::optional<TimingFrameInfo> info = timing_->GetTimingFrameInfo();
philipel97187112018-03-23 10:43:21 +0100586 if (info && stats_callback_)
ilnik2edc6842017-07-06 03:06:50 -0700587 stats_callback_->OnTimingFrameInfoUpdated(*info);
588}
589
philipelfcc60062017-01-18 05:35:20 -0800590void FrameBuffer::ClearFramesAndHistory() {
ilnik2edc6842017-07-06 03:06:50 -0700591 TRACE_EVENT0("webrtc", "FrameBuffer::ClearFramesAndHistory");
philipelfcc60062017-01-18 05:35:20 -0800592 frames_.clear();
593 last_decoded_frame_it_ = frames_.end();
594 last_continuous_frame_it_ = frames_.end();
philipel1c056252017-01-31 09:53:12 -0800595 next_frame_it_ = frames_.end();
philipelfcc60062017-01-18 05:35:20 -0800596 num_frames_history_ = 0;
597 num_frames_buffered_ = 0;
598}
599
Niels Möllerbe682d42018-03-27 08:31:45 +0200600FrameBuffer::FrameInfo::FrameInfo() = default;
601FrameBuffer::FrameInfo::FrameInfo(FrameInfo&&) = default;
602FrameBuffer::FrameInfo::~FrameInfo() = default;
603
philipelbe7a9e52016-05-19 12:19:35 +0200604} // namespace video_coding
605} // namespace webrtc