blob: 04fba84caf55b447c057da93d06e654016ec1ec3 [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
Ilya Nikolaevskiye6a2d942018-11-07 14:32:28 +0100116 // TODO(https://bugs.webrtc.org/9974): consider removing this check
117 // as it may make a stream undecodable after a very long delay between
118 // frames.
philipel6d216502018-10-22 14:36:45 +0200119 if (last_decoded_frame_timestamp_ &&
120 AheadOf(*last_decoded_frame_timestamp_, frame->Timestamp())) {
121 continue;
122 }
123
philipel1c056252017-01-31 09:53:12 -0800124 next_frame_it_ = frame_it;
philipel6d216502018-10-22 14:36:45 +0200125 if (frame->RenderTime() == -1) {
Niels Möller23775882018-08-16 10:24:12 +0200126 frame->SetRenderTime(
127 timing_->RenderTimeMs(frame->Timestamp(), now_ms));
philipel6d216502018-10-22 14:36:45 +0200128 }
philipele0b2f152016-09-28 10:23:49 +0200129 wait_ms = timing_->MaxWaitingTime(frame->RenderTime(), now_ms);
130
131 // This will cause the frame buffer to prefer high framerate rather
132 // than high resolution in the case of the decoder not decoding fast
133 // enough and the stream has multiple spatial and temporal layers.
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100134 // For multiple temporal layers it may cause non-base layer frames to be
135 // skipped if they are late.
Ilya Nikolaevskiy7eef0072018-02-28 09:59:26 +0100136 if (wait_ms < -kMaxAllowedFrameDelayMs)
philipele0b2f152016-09-28 10:23:49 +0200137 continue;
138
139 break;
140 }
141 } // rtc::Critscope lock(&crit_);
142
philipel1c056252017-01-31 09:53:12 -0800143 wait_ms = std::min<int64_t>(wait_ms, latest_return_time_ms - now_ms);
philipele0b2f152016-09-28 10:23:49 +0200144 wait_ms = std::max<int64_t>(wait_ms, 0);
tommi0a735642017-03-14 06:23:57 -0700145 } while (new_continuous_frame_event_.Wait(wait_ms));
philipele0b2f152016-09-28 10:23:49 +0200146
philipel29f730e2017-03-15 08:10:08 -0700147 {
148 rtc::CritScope lock(&crit_);
149 now_ms = clock_->TimeInMilliseconds();
150 if (next_frame_it_ != frames_.end()) {
philipele7c891f2018-02-22 14:35:06 +0100151 std::unique_ptr<EncodedFrame> frame =
philipel29f730e2017-03-15 08:10:08 -0700152 std::move(next_frame_it_->second.frame);
philipele0b2f152016-09-28 10:23:49 +0200153
philipel29f730e2017-03-15 08:10:08 -0700154 if (!frame->delayed_by_retransmission()) {
155 int64_t frame_delay;
philipele0754302017-01-25 08:56:23 -0800156
Niels Möller23775882018-08-16 10:24:12 +0200157 if (inter_frame_delay_.CalculateDelay(frame->Timestamp(), &frame_delay,
philipel29f730e2017-03-15 08:10:08 -0700158 frame->ReceivedTime())) {
159 jitter_estimator_->UpdateEstimate(frame_delay, frame->size());
160 }
161
162 float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0;
“Michaelf9fc1712018-08-27 10:08:58 -0500163 if (RttMultExperiment::RttMultEnabled()) {
164 rtt_mult = RttMultExperiment::GetRttMultValue();
165 }
philipel29f730e2017-03-15 08:10:08 -0700166 timing_->SetJitterDelay(jitter_estimator_->GetJitterEstimate(rtt_mult));
167 timing_->UpdateCurrentDelay(frame->RenderTime(), now_ms);
philipele21be1d2017-09-25 06:37:12 -0700168 } else {
“Michaelf9fc1712018-08-27 10:08:58 -0500169 if (RttMultExperiment::RttMultEnabled() ||
170 webrtc::field_trial::IsEnabled("WebRTC-AddRttToPlayoutDelay"))
philipel707f2782017-10-02 14:10:28 +0200171 jitter_estimator_->FrameNacked();
philipele0754302017-01-25 08:56:23 -0800172 }
173
stefan95e97542017-05-23 09:52:18 -0700174 // Gracefully handle bad RTP timestamps and render time issues.
175 if (HasBadRenderTiming(*frame, now_ms)) {
176 jitter_estimator_->Reset();
177 timing_->Reset();
Niels Möller23775882018-08-16 10:24:12 +0200178 frame->SetRenderTime(timing_->RenderTimeMs(frame->Timestamp(), now_ms));
stefan95e97542017-05-23 09:52:18 -0700179 }
180
philipel29f730e2017-03-15 08:10:08 -0700181 UpdateJitterDelay();
ilnik2edc6842017-07-06 03:06:50 -0700182 UpdateTimingFrameInfo();
philipel29f730e2017-03-15 08:10:08 -0700183 PropagateDecodability(next_frame_it_->second);
brandtr9078d8c2017-04-27 07:07:27 -0700184
philipel29f730e2017-03-15 08:10:08 -0700185 AdvanceLastDecodedFrame(next_frame_it_);
Niels Möller23775882018-08-16 10:24:12 +0200186 last_decoded_frame_timestamp_ = frame->Timestamp();
philipel29f730e2017-03-15 08:10:08 -0700187 *frame_out = std::move(frame);
188 return kFrameFound;
philipelbe7a9e52016-05-19 12:19:35 +0200189 }
tommi0a735642017-03-14 06:23:57 -0700190 }
191
192 if (latest_return_time_ms - now_ms > 0) {
philipel1c056252017-01-31 09:53:12 -0800193 // If |next_frame_it_ == frames_.end()| and there is still time left, it
194 // means that the frame buffer was cleared as the thread in this function
195 // was waiting to acquire |crit_| in order to return. Wait for the
196 // remaining time and then return.
197 return NextFrame(latest_return_time_ms - now_ms, frame_out);
philipelbe7a9e52016-05-19 12:19:35 +0200198 }
tommi0a735642017-03-14 06:23:57 -0700199
200 return kTimeout;
philipelbe7a9e52016-05-19 12:19:35 +0200201}
202
philipele7c891f2018-02-22 14:35:06 +0100203bool FrameBuffer::HasBadRenderTiming(const EncodedFrame& frame,
204 int64_t now_ms) {
stefan95e97542017-05-23 09:52:18 -0700205 // Assume that render timing errors are due to changes in the video stream.
206 int64_t render_time_ms = frame.RenderTimeMs();
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200207 // Zero render time means render immediately.
208 if (render_time_ms == 0) {
209 return false;
210 }
stefan95e97542017-05-23 09:52:18 -0700211 if (render_time_ms < 0) {
212 return true;
213 }
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200214 const int64_t kMaxVideoDelayMs = 10000;
stefan95e97542017-05-23 09:52:18 -0700215 if (std::abs(render_time_ms - now_ms) > kMaxVideoDelayMs) {
216 int frame_delay = static_cast<int>(std::abs(render_time_ms - now_ms));
Mirko Bonadei675513b2017-11-09 11:09:25 +0100217 RTC_LOG(LS_WARNING)
218 << "A frame about to be decoded is out of the configured "
219 << "delay bounds (" << frame_delay << " > " << kMaxVideoDelayMs
220 << "). Resetting the video jitter buffer.";
stefan95e97542017-05-23 09:52:18 -0700221 return true;
222 }
223 if (static_cast<int>(timing_->TargetVideoDelay()) > kMaxVideoDelayMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100224 RTC_LOG(LS_WARNING) << "The video target delay has grown larger than "
225 << kMaxVideoDelayMs << " ms.";
stefan95e97542017-05-23 09:52:18 -0700226 return true;
227 }
228 return false;
229}
230
philipel4f6cd6a2016-08-03 10:59:32 +0200231void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) {
tommidb23ea62017-03-03 07:21:18 -0800232 TRACE_EVENT0("webrtc", "FrameBuffer::SetProtectionMode");
philipel4f6cd6a2016-08-03 10:59:32 +0200233 rtc::CritScope lock(&crit_);
234 protection_mode_ = mode;
235}
236
philipel504c47d2016-06-30 17:33:02 +0200237void FrameBuffer::Start() {
tommidb23ea62017-03-03 07:21:18 -0800238 TRACE_EVENT0("webrtc", "FrameBuffer::Start");
philipel29f730e2017-03-15 08:10:08 -0700239 rtc::CritScope lock(&crit_);
240 stopped_ = false;
philipel504c47d2016-06-30 17:33:02 +0200241}
242
243void FrameBuffer::Stop() {
tommidb23ea62017-03-03 07:21:18 -0800244 TRACE_EVENT0("webrtc", "FrameBuffer::Stop");
philipel29f730e2017-03-15 08:10:08 -0700245 rtc::CritScope lock(&crit_);
246 stopped_ = true;
tommi0a735642017-03-14 06:23:57 -0700247 new_continuous_frame_event_.Set();
philipel504c47d2016-06-30 17:33:02 +0200248}
249
Ilya Nikolaevskiye6a2d942018-11-07 14:32:28 +0100250void FrameBuffer::Clear() {
251 rtc::CritScope lock(&crit_);
252 ClearFramesAndHistory();
253}
254
philipele21be1d2017-09-25 06:37:12 -0700255void FrameBuffer::UpdateRtt(int64_t rtt_ms) {
256 rtc::CritScope lock(&crit_);
257 jitter_estimator_->UpdateRtt(rtt_ms);
258}
259
philipele7c891f2018-02-22 14:35:06 +0100260bool FrameBuffer::ValidReferences(const EncodedFrame& frame) const {
philipel0fa82a62018-03-19 15:34:53 +0100261 if (frame.id.picture_id < 0)
philipel3b3c9c42017-09-11 09:38:36 -0700262 return false;
263
philipel112adf92017-06-15 09:06:21 -0700264 for (size_t i = 0; i < frame.num_references; ++i) {
philipel0fa82a62018-03-19 15:34:53 +0100265 if (frame.references[i] < 0 || frame.references[i] >= frame.id.picture_id)
philipel112adf92017-06-15 09:06:21 -0700266 return false;
philipel3b3c9c42017-09-11 09:38:36 -0700267
philipel112adf92017-06-15 09:06:21 -0700268 for (size_t j = i + 1; j < frame.num_references; ++j) {
269 if (frame.references[i] == frame.references[j])
270 return false;
271 }
272 }
273
philipel0fa82a62018-03-19 15:34:53 +0100274 if (frame.inter_layer_predicted && frame.id.spatial_layer == 0)
philipel112adf92017-06-15 09:06:21 -0700275 return false;
276
277 return true;
278}
279
philipele7c891f2018-02-22 14:35:06 +0100280void FrameBuffer::UpdatePlayoutDelays(const EncodedFrame& frame) {
gnishb2a318b2017-05-10 09:21:33 -0700281 TRACE_EVENT0("webrtc", "FrameBuffer::UpdatePlayoutDelays");
282 PlayoutDelay playout_delay = frame.EncodedImage().playout_delay_;
283 if (playout_delay.min_ms >= 0)
284 timing_->set_min_playout_delay(playout_delay.min_ms);
285
286 if (playout_delay.max_ms >= 0)
287 timing_->set_max_playout_delay(playout_delay.max_ms);
philipel0a9f6de2018-02-28 11:29:47 +0100288
289 if (!frame.delayed_by_retransmission())
Niels Möller23775882018-08-16 10:24:12 +0200290 timing_->IncomingTimestamp(frame.Timestamp(), frame.ReceivedTime());
gnishb2a318b2017-05-10 09:21:33 -0700291}
292
philipele7c891f2018-02-22 14:35:06 +0100293int64_t FrameBuffer::InsertFrame(std::unique_ptr<EncodedFrame> frame) {
tommidb23ea62017-03-03 07:21:18 -0800294 TRACE_EVENT0("webrtc", "FrameBuffer::InsertFrame");
philipel93e451b2016-10-06 12:25:13 +0200295 RTC_DCHECK(frame);
philipela45102f2017-02-22 05:30:39 -0800296 if (stats_callback_)
ilnik6d5b4d62017-08-30 03:32:14 -0700297 stats_callback_->OnCompleteFrame(frame->is_keyframe(), frame->size(),
298 frame->contentType());
philipel0fa82a62018-03-19 15:34:53 +0100299 const VideoLayerFrameId& id = frame->id;
tommi0a735642017-03-14 06:23:57 -0700300
301 rtc::CritScope lock(&crit_);
philipel29f730e2017-03-15 08:10:08 -0700302
philipel1610f942017-12-12 13:58:31 +0100303 int64_t last_continuous_picture_id =
philipele0b2f152016-09-28 10:23:49 +0200304 last_continuous_frame_it_ == frames_.end()
305 ? -1
306 : last_continuous_frame_it_->first.picture_id;
307
philipel112adf92017-06-15 09:06:21 -0700308 if (!ValidReferences(*frame)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100309 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100310 << id.picture_id << ":"
311 << static_cast<int>(id.spatial_layer)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100312 << ") has invalid frame references, dropping frame.";
philipel112adf92017-06-15 09:06:21 -0700313 return last_continuous_picture_id;
314 }
315
philipele0b2f152016-09-28 10:23:49 +0200316 if (num_frames_buffered_ >= kMaxFramesBuffered) {
philipel9771c502018-03-02 11:06:27 +0100317 if (frame->is_keyframe()) {
318 RTC_LOG(LS_WARNING) << "Inserting keyframe (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 << ") but buffer is full, clearing"
322 << " buffer and inserting the frame.";
323 ClearFramesAndHistory();
324 } else {
325 RTC_LOG(LS_WARNING) << "Frame with (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 << ") could not be inserted due to the frame "
329 << "buffer being full, dropping frame.";
330 return last_continuous_picture_id;
331 }
philipele0b2f152016-09-28 10:23:49 +0200332 }
333
philipele0b2f152016-09-28 10:23:49 +0200334 if (last_decoded_frame_it_ != frames_.end() &&
philipel0fa82a62018-03-19 15:34:53 +0100335 id <= last_decoded_frame_it_->first) {
philipel6d216502018-10-22 14:36:45 +0200336 if (AheadOf(frame->Timestamp(), *last_decoded_frame_timestamp_) &&
philipel3042c2d2017-08-18 04:55:02 -0700337 frame->is_keyframe()) {
philipelfcc60062017-01-18 05:35:20 -0800338 // If this frame has a newer timestamp but an earlier picture id then we
339 // assume there has been a jump in the picture id due to some encoder
340 // reconfiguration or some other reason. Even though this is not according
341 // to spec we can still continue to decode from this frame if it is a
342 // keyframe.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100343 RTC_LOG(LS_WARNING)
344 << "A jump in picture id was detected, clearing buffer.";
philipelfcc60062017-01-18 05:35:20 -0800345 ClearFramesAndHistory();
346 last_continuous_picture_id = -1;
347 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100348 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100349 << id.picture_id << ":"
350 << static_cast<int>(id.spatial_layer)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100351 << ") inserted after frame ("
352 << last_decoded_frame_it_->first.picture_id << ":"
353 << static_cast<int>(
354 last_decoded_frame_it_->first.spatial_layer)
355 << ") was handed off for decoding, dropping frame.";
philipelfcc60062017-01-18 05:35:20 -0800356 return last_continuous_picture_id;
357 }
philipele0b2f152016-09-28 10:23:49 +0200358 }
359
philipel146a48b2017-04-20 04:04:38 -0700360 // Test if inserting this frame would cause the order of the frames to become
361 // ambiguous (covering more than half the interval of 2^16). This can happen
362 // when the picture id make large jumps mid stream.
philipel0fa82a62018-03-19 15:34:53 +0100363 if (!frames_.empty() && id < frames_.begin()->first &&
364 frames_.rbegin()->first < id) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100365 RTC_LOG(LS_WARNING)
366 << "A jump in picture id was detected, clearing buffer.";
philipel146a48b2017-04-20 04:04:38 -0700367 ClearFramesAndHistory();
368 last_continuous_picture_id = -1;
369 }
370
philipel0fa82a62018-03-19 15:34:53 +0100371 auto info = frames_.emplace(id, FrameInfo()).first;
philipele0b2f152016-09-28 10:23:49 +0200372
philipel93e451b2016-10-06 12:25:13 +0200373 if (info->second.frame) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100374 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100375 << id.picture_id << ":"
376 << static_cast<int>(id.spatial_layer)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100377 << ") already inserted, dropping frame.";
philipele0b2f152016-09-28 10:23:49 +0200378 return last_continuous_picture_id;
379 }
380
philipel93e451b2016-10-06 12:25:13 +0200381 if (!UpdateFrameInfoWithIncomingFrame(*frame, info))
382 return last_continuous_picture_id;
gnishb2a318b2017-05-10 09:21:33 -0700383 UpdatePlayoutDelays(*frame);
philipel0a9f6de2018-02-28 11:29:47 +0100384
philipele0b2f152016-09-28 10:23:49 +0200385 info->second.frame = std::move(frame);
386 ++num_frames_buffered_;
387
388 if (info->second.num_missing_continuous == 0) {
389 info->second.continuous = true;
390 PropagateContinuity(info);
391 last_continuous_picture_id = last_continuous_frame_it_->first.picture_id;
392
393 // Since we now have new continuous frames there might be a better frame
394 // to return from NextFrame. Signal that thread so that it again can choose
395 // which frame to return.
tommi0a735642017-03-14 06:23:57 -0700396 new_continuous_frame_event_.Set();
philipele0b2f152016-09-28 10:23:49 +0200397 }
398
399 return last_continuous_picture_id;
philipelbe7a9e52016-05-19 12:19:35 +0200400}
401
philipele0b2f152016-09-28 10:23:49 +0200402void FrameBuffer::PropagateContinuity(FrameMap::iterator start) {
tommidb23ea62017-03-03 07:21:18 -0800403 TRACE_EVENT0("webrtc", "FrameBuffer::PropagateContinuity");
philipele0b2f152016-09-28 10:23:49 +0200404 RTC_DCHECK(start->second.continuous);
405 if (last_continuous_frame_it_ == frames_.end())
406 last_continuous_frame_it_ = start;
407
408 std::queue<FrameMap::iterator> continuous_frames;
409 continuous_frames.push(start);
410
411 // A simple BFS to traverse continuous frames.
412 while (!continuous_frames.empty()) {
413 auto frame = continuous_frames.front();
414 continuous_frames.pop();
415
416 if (last_continuous_frame_it_->first < frame->first)
417 last_continuous_frame_it_ = frame;
418
419 // Loop through all dependent frames, and if that frame no longer has
420 // any unfulfilled dependencies then that frame is continuous as well.
421 for (size_t d = 0; d < frame->second.num_dependent_frames; ++d) {
422 auto frame_ref = frames_.find(frame->second.dependent_frames[d]);
philipel112adf92017-06-15 09:06:21 -0700423 RTC_DCHECK(frame_ref != frames_.end());
philipele0b2f152016-09-28 10:23:49 +0200424
philipel112adf92017-06-15 09:06:21 -0700425 // TODO(philipel): Look into why we've seen this happen.
426 if (frame_ref != frames_.end()) {
427 --frame_ref->second.num_missing_continuous;
428 if (frame_ref->second.num_missing_continuous == 0) {
429 frame_ref->second.continuous = true;
430 continuous_frames.push(frame_ref);
431 }
philipele0b2f152016-09-28 10:23:49 +0200432 }
433 }
434 }
435}
436
437void FrameBuffer::PropagateDecodability(const FrameInfo& info) {
tommidb23ea62017-03-03 07:21:18 -0800438 TRACE_EVENT0("webrtc", "FrameBuffer::PropagateDecodability");
tommie95b78b2017-05-14 07:23:11 -0700439 RTC_CHECK(info.num_dependent_frames < FrameInfo::kMaxNumDependentFrames);
philipele0b2f152016-09-28 10:23:49 +0200440 for (size_t d = 0; d < info.num_dependent_frames; ++d) {
441 auto ref_info = frames_.find(info.dependent_frames[d]);
philipel93e451b2016-10-06 12:25:13 +0200442 RTC_DCHECK(ref_info != frames_.end());
tommie95b78b2017-05-14 07:23:11 -0700443 // TODO(philipel): Look into why we've seen this happen.
444 if (ref_info != frames_.end()) {
445 RTC_DCHECK_GT(ref_info->second.num_missing_decodable, 0U);
446 --ref_info->second.num_missing_decodable;
447 }
philipele0b2f152016-09-28 10:23:49 +0200448 }
449}
450
451void FrameBuffer::AdvanceLastDecodedFrame(FrameMap::iterator decoded) {
tommidb23ea62017-03-03 07:21:18 -0800452 TRACE_EVENT0("webrtc", "FrameBuffer::AdvanceLastDecodedFrame");
philipele0b2f152016-09-28 10:23:49 +0200453 if (last_decoded_frame_it_ == frames_.end()) {
454 last_decoded_frame_it_ = frames_.begin();
455 } else {
456 RTC_DCHECK(last_decoded_frame_it_->first < decoded->first);
457 ++last_decoded_frame_it_;
458 }
459 --num_frames_buffered_;
460 ++num_frames_history_;
461
462 // First, delete non-decoded frames from the history.
463 while (last_decoded_frame_it_ != decoded) {
464 if (last_decoded_frame_it_->second.frame)
465 --num_frames_buffered_;
466 last_decoded_frame_it_ = frames_.erase(last_decoded_frame_it_);
philipelbe7a9e52016-05-19 12:19:35 +0200467 }
468
philipele0b2f152016-09-28 10:23:49 +0200469 // Then remove old history if we have too much history saved.
470 if (num_frames_history_ > kMaxFramesHistory) {
471 frames_.erase(frames_.begin());
472 --num_frames_history_;
473 }
474}
475
philipele7c891f2018-02-22 14:35:06 +0100476bool FrameBuffer::UpdateFrameInfoWithIncomingFrame(const EncodedFrame& frame,
philipele0b2f152016-09-28 10:23:49 +0200477 FrameMap::iterator info) {
tommidb23ea62017-03-03 07:21:18 -0800478 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateFrameInfoWithIncomingFrame");
philipel0fa82a62018-03-19 15:34:53 +0100479 const VideoLayerFrameId& id = frame.id;
philipele0b2f152016-09-28 10:23:49 +0200480
481 RTC_DCHECK(last_decoded_frame_it_ == frames_.end() ||
482 last_decoded_frame_it_->first < info->first);
483
philipel798b2822018-06-11 13:10:14 +0200484 // In this function we determine how many missing dependencies this |frame|
485 // has to become continuous/decodable. If a frame that this |frame| depend
486 // on has already been decoded then we can ignore that dependency since it has
487 // already been fulfilled.
488 //
489 // For all other frames we will register a backwards reference to this |frame|
490 // so that |num_missing_continuous| and |num_missing_decodable| can be
491 // decremented as frames become continuous/are decoded.
492 struct Dependency {
493 VideoLayerFrameId id;
494 bool continuous;
495 };
496 std::vector<Dependency> not_yet_fulfilled_dependencies;
497
498 // Find all dependencies that have not yet been fulfilled.
philipele0b2f152016-09-28 10:23:49 +0200499 for (size_t i = 0; i < frame.num_references; ++i) {
philipel0fa82a62018-03-19 15:34:53 +0100500 VideoLayerFrameId ref_key(frame.references[i], frame.id.spatial_layer);
philipele0b2f152016-09-28 10:23:49 +0200501 auto ref_info = frames_.find(ref_key);
502
philipel798b2822018-06-11 13:10:14 +0200503 // Does |frame| depend on a frame earlier than the last decoded one?
philipele0b2f152016-09-28 10:23:49 +0200504 if (last_decoded_frame_it_ != frames_.end() &&
505 ref_key <= last_decoded_frame_it_->first) {
philipel798b2822018-06-11 13:10:14 +0200506 // Was that frame decoded? If not, this |frame| will never become
507 // decodable.
philipele0b2f152016-09-28 10:23:49 +0200508 if (ref_info == frames_.end()) {
philipel65e1f942017-07-24 08:26:53 -0700509 int64_t now_ms = clock_->TimeInMilliseconds();
510 if (last_log_non_decoded_ms_ + kLogNonDecodedIntervalMs < now_ms) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100511 RTC_LOG(LS_WARNING)
philipel0fa82a62018-03-19 15:34:53 +0100512 << "Frame with (picture_id:spatial_id) (" << id.picture_id << ":"
513 << static_cast<int>(id.spatial_layer)
philipel65e1f942017-07-24 08:26:53 -0700514 << ") depends on a non-decoded frame more previous than"
515 << " the last decoded frame, dropping frame.";
516 last_log_non_decoded_ms_ = now_ms;
517 }
philipele0b2f152016-09-28 10:23:49 +0200518 return false;
519 }
philipele0b2f152016-09-28 10:23:49 +0200520 } else {
philipel798b2822018-06-11 13:10:14 +0200521 bool ref_continuous =
522 ref_info != frames_.end() && ref_info->second.continuous;
523 not_yet_fulfilled_dependencies.push_back({ref_key, ref_continuous});
philipele0b2f152016-09-28 10:23:49 +0200524 }
philipelbe7a9e52016-05-19 12:19:35 +0200525 }
526
philipel798b2822018-06-11 13:10:14 +0200527 // Does |frame| depend on the lower spatial layer?
philipelbe7a9e52016-05-19 12:19:35 +0200528 if (frame.inter_layer_predicted) {
philipel0fa82a62018-03-19 15:34:53 +0100529 VideoLayerFrameId ref_key(frame.id.picture_id, frame.id.spatial_layer - 1);
philipel798b2822018-06-11 13:10:14 +0200530 auto ref_info = frames_.find(ref_key);
philipele0b2f152016-09-28 10:23:49 +0200531
philipel798b2822018-06-11 13:10:14 +0200532 bool lower_layer_continuous =
533 ref_info != frames_.end() && ref_info->second.continuous;
534 bool lower_layer_decoded = last_decoded_frame_it_ != frames_.end() &&
535 last_decoded_frame_it_->first == ref_key;
536
537 if (!lower_layer_continuous || !lower_layer_decoded) {
538 not_yet_fulfilled_dependencies.push_back(
539 {ref_key, lower_layer_continuous});
philipele0b2f152016-09-28 10:23:49 +0200540 }
philipelbe7a9e52016-05-19 12:19:35 +0200541 }
542
philipel798b2822018-06-11 13:10:14 +0200543 info->second.num_missing_continuous = not_yet_fulfilled_dependencies.size();
544 info->second.num_missing_decodable = not_yet_fulfilled_dependencies.size();
545
546 for (const Dependency& dep : not_yet_fulfilled_dependencies) {
547 if (dep.continuous)
548 --info->second.num_missing_continuous;
549
550 // At this point we know we want to insert this frame, so here we
551 // intentionally get or create the FrameInfo for this dependency.
552 FrameInfo* dep_info = &frames_[dep.id];
553
554 if (dep_info->num_dependent_frames <
555 (FrameInfo::kMaxNumDependentFrames - 1)) {
556 dep_info->dependent_frames[dep_info->num_dependent_frames] = id;
557 ++dep_info->num_dependent_frames;
558 } else {
559 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
560 << dep.id.picture_id << ":"
561 << static_cast<int>(dep.id.spatial_layer)
562 << ") is referenced by too many frames.";
563 }
564 }
philipel93e451b2016-10-06 12:25:13 +0200565
philipelbe7a9e52016-05-19 12:19:35 +0200566 return true;
567}
568
philipelbe742702016-11-30 01:31:40 -0800569void FrameBuffer::UpdateJitterDelay() {
tommidb23ea62017-03-03 07:21:18 -0800570 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateJitterDelay");
philipela45102f2017-02-22 05:30:39 -0800571 if (!stats_callback_)
572 return;
philipelbe742702016-11-30 01:31:40 -0800573
philipela45102f2017-02-22 05:30:39 -0800574 int decode_ms;
575 int max_decode_ms;
576 int current_delay_ms;
577 int target_delay_ms;
578 int jitter_buffer_ms;
579 int min_playout_delay_ms;
580 int render_delay_ms;
581 if (timing_->GetTimings(&decode_ms, &max_decode_ms, &current_delay_ms,
582 &target_delay_ms, &jitter_buffer_ms,
583 &min_playout_delay_ms, &render_delay_ms)) {
584 stats_callback_->OnFrameBufferTimingsUpdated(
585 decode_ms, max_decode_ms, current_delay_ms, target_delay_ms,
586 jitter_buffer_ms, min_playout_delay_ms, render_delay_ms);
philipelbe742702016-11-30 01:31:40 -0800587 }
philipel266f0a42016-11-28 08:49:07 -0800588}
589
ilnik2edc6842017-07-06 03:06:50 -0700590void FrameBuffer::UpdateTimingFrameInfo() {
591 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateTimingFrameInfo");
Danil Chapovalov0040b662018-06-18 10:48:16 +0200592 absl::optional<TimingFrameInfo> info = timing_->GetTimingFrameInfo();
philipel97187112018-03-23 10:43:21 +0100593 if (info && stats_callback_)
ilnik2edc6842017-07-06 03:06:50 -0700594 stats_callback_->OnTimingFrameInfoUpdated(*info);
595}
596
philipelfcc60062017-01-18 05:35:20 -0800597void FrameBuffer::ClearFramesAndHistory() {
ilnik2edc6842017-07-06 03:06:50 -0700598 TRACE_EVENT0("webrtc", "FrameBuffer::ClearFramesAndHistory");
philipelfcc60062017-01-18 05:35:20 -0800599 frames_.clear();
600 last_decoded_frame_it_ = frames_.end();
601 last_continuous_frame_it_ = frames_.end();
philipel1c056252017-01-31 09:53:12 -0800602 next_frame_it_ = frames_.end();
philipelfcc60062017-01-18 05:35:20 -0800603 num_frames_history_ = 0;
604 num_frames_buffered_ = 0;
605}
606
Niels Möllerbe682d42018-03-27 08:31:45 +0200607FrameBuffer::FrameInfo::FrameInfo() = default;
608FrameBuffer::FrameInfo::FrameInfo(FrameInfo&&) = default;
609FrameBuffer::FrameInfo::~FrameInfo() = default;
610
philipelbe7a9e52016-05-19 12:19:35 +0200611} // namespace video_coding
612} // namespace webrtc