blob: 86f6e42c46eedff6d5471a62e56ff51a2c12cb4f [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>
philipelbe7a9e52016-05-19 12:19:35 +020016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/video_coding/include/video_coding_defines.h"
18#include "modules/video_coding/jitter_estimator.h"
19#include "modules/video_coding/timing.h"
20#include "rtc_base/checks.h"
21#include "rtc_base/logging.h"
22#include "rtc_base/trace_event.h"
23#include "system_wrappers/include/clock.h"
philipel707f2782017-10-02 14:10:28 +020024#include "system_wrappers/include/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "system_wrappers/include/metrics.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()),
gnishb2a318b2017-05-10 09:21:33 -070053 last_decoded_frame_timestamp_(0),
philipele0b2f152016-09-28 10:23:49 +020054 last_decoded_frame_it_(frames_.end()),
55 last_continuous_frame_it_(frames_.end()),
56 num_frames_history_(0),
57 num_frames_buffered_(0),
philipel29f730e2017-03-15 08:10:08 -070058 stopped_(false),
philipela45102f2017-02-22 05:30:39 -080059 protection_mode_(kProtectionNack),
philipel65e1f942017-07-24 08:26:53 -070060 stats_callback_(stats_callback),
61 last_log_non_decoded_ms_(-kLogNonDecodedIntervalMs) {}
philipel266f0a42016-11-28 08:49:07 -080062
philipela45102f2017-02-22 05:30:39 -080063FrameBuffer::~FrameBuffer() {}
philipelbe7a9e52016-05-19 12:19:35 +020064
philipel75562822016-09-05 10:57:41 +020065FrameBuffer::ReturnReason FrameBuffer::NextFrame(
66 int64_t max_wait_time_ms,
philipele7c891f2018-02-22 14:35:06 +010067 std::unique_ptr<EncodedFrame>* frame_out,
philipel3042c2d2017-08-18 04:55:02 -070068 bool keyframe_required) {
tommidb23ea62017-03-03 07:21:18 -080069 TRACE_EVENT0("webrtc", "FrameBuffer::NextFrame");
philipel1c056252017-01-31 09:53:12 -080070 int64_t latest_return_time_ms =
71 clock_->TimeInMilliseconds() + max_wait_time_ms;
philipel504c47d2016-06-30 17:33:02 +020072 int64_t wait_ms = max_wait_time_ms;
philipel29f730e2017-03-15 08:10:08 -070073 int64_t now_ms = 0;
philipele0b2f152016-09-28 10:23:49 +020074
75 do {
philipel29f730e2017-03-15 08:10:08 -070076 now_ms = clock_->TimeInMilliseconds();
philipel504c47d2016-06-30 17:33:02 +020077 {
78 rtc::CritScope lock(&crit_);
tommi0a735642017-03-14 06:23:57 -070079 new_continuous_frame_event_.Reset();
philipel29f730e2017-03-15 08:10:08 -070080 if (stopped_)
81 return kStopped;
82
83 wait_ms = max_wait_time_ms;
84
philipele0b2f152016-09-28 10:23:49 +020085 // Need to hold |crit_| in order to use |frames_|, therefore we
86 // set it here in the loop instead of outside the loop in order to not
87 // acquire the lock unnecesserily.
philipel1c056252017-01-31 09:53:12 -080088 next_frame_it_ = frames_.end();
philipelbe7a9e52016-05-19 12:19:35 +020089
philipele0b2f152016-09-28 10:23:49 +020090 // |frame_it| points to the first frame after the
91 // |last_decoded_frame_it_|.
92 auto frame_it = frames_.end();
93 if (last_decoded_frame_it_ == frames_.end()) {
94 frame_it = frames_.begin();
philipelbe7a9e52016-05-19 12:19:35 +020095 } else {
philipele0b2f152016-09-28 10:23:49 +020096 frame_it = last_decoded_frame_it_;
97 ++frame_it;
philipelbe7a9e52016-05-19 12:19:35 +020098 }
philipele0b2f152016-09-28 10:23:49 +020099
100 // |continuous_end_it| points to the first frame after the
101 // |last_continuous_frame_it_|.
102 auto continuous_end_it = last_continuous_frame_it_;
103 if (continuous_end_it != frames_.end())
104 ++continuous_end_it;
105
philipel146a48b2017-04-20 04:04:38 -0700106 for (; frame_it != continuous_end_it && frame_it != frames_.end();
107 ++frame_it) {
philipel93e451b2016-10-06 12:25:13 +0200108 if (!frame_it->second.continuous ||
109 frame_it->second.num_missing_decodable > 0) {
philipele0b2f152016-09-28 10:23:49 +0200110 continue;
philipel93e451b2016-10-06 12:25:13 +0200111 }
philipele0b2f152016-09-28 10:23:49 +0200112
philipele7c891f2018-02-22 14:35:06 +0100113 EncodedFrame* frame = frame_it->second.frame.get();
philipel3042c2d2017-08-18 04:55:02 -0700114
115 if (keyframe_required && !frame->is_keyframe())
116 continue;
117
philipel1c056252017-01-31 09:53:12 -0800118 next_frame_it_ = frame_it;
philipele0b2f152016-09-28 10:23:49 +0200119 if (frame->RenderTime() == -1)
120 frame->SetRenderTime(timing_->RenderTimeMs(frame->timestamp, now_ms));
121 wait_ms = timing_->MaxWaitingTime(frame->RenderTime(), now_ms);
122
123 // This will cause the frame buffer to prefer high framerate rather
124 // than high resolution in the case of the decoder not decoding fast
125 // enough and the stream has multiple spatial and temporal layers.
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100126 // For multiple temporal layers it may cause non-base layer frames to be
127 // skipped if they are late.
Ilya Nikolaevskiy7eef0072018-02-28 09:59:26 +0100128 if (wait_ms < -kMaxAllowedFrameDelayMs)
philipele0b2f152016-09-28 10:23:49 +0200129 continue;
130
131 break;
132 }
133 } // rtc::Critscope lock(&crit_);
134
philipel1c056252017-01-31 09:53:12 -0800135 wait_ms = std::min<int64_t>(wait_ms, latest_return_time_ms - now_ms);
philipele0b2f152016-09-28 10:23:49 +0200136 wait_ms = std::max<int64_t>(wait_ms, 0);
tommi0a735642017-03-14 06:23:57 -0700137 } while (new_continuous_frame_event_.Wait(wait_ms));
philipele0b2f152016-09-28 10:23:49 +0200138
philipel29f730e2017-03-15 08:10:08 -0700139 {
140 rtc::CritScope lock(&crit_);
141 now_ms = clock_->TimeInMilliseconds();
142 if (next_frame_it_ != frames_.end()) {
philipele7c891f2018-02-22 14:35:06 +0100143 std::unique_ptr<EncodedFrame> frame =
philipel29f730e2017-03-15 08:10:08 -0700144 std::move(next_frame_it_->second.frame);
philipele0b2f152016-09-28 10:23:49 +0200145
philipel29f730e2017-03-15 08:10:08 -0700146 if (!frame->delayed_by_retransmission()) {
147 int64_t frame_delay;
philipele0754302017-01-25 08:56:23 -0800148
philipel29f730e2017-03-15 08:10:08 -0700149 if (inter_frame_delay_.CalculateDelay(frame->timestamp, &frame_delay,
150 frame->ReceivedTime())) {
151 jitter_estimator_->UpdateEstimate(frame_delay, frame->size());
152 }
153
154 float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0;
155 timing_->SetJitterDelay(jitter_estimator_->GetJitterEstimate(rtt_mult));
156 timing_->UpdateCurrentDelay(frame->RenderTime(), now_ms);
philipele21be1d2017-09-25 06:37:12 -0700157 } else {
philipel707f2782017-10-02 14:10:28 +0200158 if (webrtc::field_trial::IsEnabled("WebRTC-AddRttToPlayoutDelay"))
159 jitter_estimator_->FrameNacked();
philipele0754302017-01-25 08:56:23 -0800160 }
161
stefan95e97542017-05-23 09:52:18 -0700162 // Gracefully handle bad RTP timestamps and render time issues.
163 if (HasBadRenderTiming(*frame, now_ms)) {
164 jitter_estimator_->Reset();
165 timing_->Reset();
166 frame->SetRenderTime(timing_->RenderTimeMs(frame->timestamp, now_ms));
167 }
168
philipel29f730e2017-03-15 08:10:08 -0700169 UpdateJitterDelay();
ilnik2edc6842017-07-06 03:06:50 -0700170 UpdateTimingFrameInfo();
philipel29f730e2017-03-15 08:10:08 -0700171 PropagateDecodability(next_frame_it_->second);
brandtr9078d8c2017-04-27 07:07:27 -0700172
173 // Sanity check for RTP timestamp monotonicity.
174 if (last_decoded_frame_it_ != frames_.end()) {
philipel0fa82a62018-03-19 15:34:53 +0100175 const VideoLayerFrameId& last_decoded_frame_key =
176 last_decoded_frame_it_->first;
177 const VideoLayerFrameId& frame_key = next_frame_it_->first;
brandtr9078d8c2017-04-27 07:07:27 -0700178
179 const bool frame_is_higher_spatial_layer_of_last_decoded_frame =
180 last_decoded_frame_timestamp_ == frame->timestamp &&
181 last_decoded_frame_key.picture_id == frame_key.picture_id &&
182 last_decoded_frame_key.spatial_layer < frame_key.spatial_layer;
183
184 if (AheadOrAt(last_decoded_frame_timestamp_, frame->timestamp) &&
185 !frame_is_higher_spatial_layer_of_last_decoded_frame) {
186 // TODO(brandtr): Consider clearing the entire buffer when we hit
187 // these conditions.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100188 RTC_LOG(LS_WARNING)
189 << "Frame with (timestamp:picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100190 << frame->timestamp << ":" << frame->id.picture_id << ":"
191 << static_cast<int>(frame->id.spatial_layer) << ")"
Mirko Bonadei675513b2017-11-09 11:09:25 +0100192 << " sent to decoder after frame with"
193 << " (timestamp:picture_id:spatial_id) ("
194 << last_decoded_frame_timestamp_ << ":"
195 << last_decoded_frame_key.picture_id << ":"
196 << static_cast<int>(last_decoded_frame_key.spatial_layer) << ").";
brandtr9078d8c2017-04-27 07:07:27 -0700197 }
198 }
199
philipel29f730e2017-03-15 08:10:08 -0700200 AdvanceLastDecodedFrame(next_frame_it_);
201 last_decoded_frame_timestamp_ = frame->timestamp;
202 *frame_out = std::move(frame);
203 return kFrameFound;
philipelbe7a9e52016-05-19 12:19:35 +0200204 }
tommi0a735642017-03-14 06:23:57 -0700205 }
206
207 if (latest_return_time_ms - now_ms > 0) {
philipel1c056252017-01-31 09:53:12 -0800208 // If |next_frame_it_ == frames_.end()| and there is still time left, it
209 // means that the frame buffer was cleared as the thread in this function
210 // was waiting to acquire |crit_| in order to return. Wait for the
211 // remaining time and then return.
212 return NextFrame(latest_return_time_ms - now_ms, frame_out);
philipelbe7a9e52016-05-19 12:19:35 +0200213 }
tommi0a735642017-03-14 06:23:57 -0700214
215 return kTimeout;
philipelbe7a9e52016-05-19 12:19:35 +0200216}
217
philipele7c891f2018-02-22 14:35:06 +0100218bool FrameBuffer::HasBadRenderTiming(const EncodedFrame& frame,
219 int64_t now_ms) {
stefan95e97542017-05-23 09:52:18 -0700220 // Assume that render timing errors are due to changes in the video stream.
221 int64_t render_time_ms = frame.RenderTimeMs();
222 const int64_t kMaxVideoDelayMs = 10000;
223 if (render_time_ms < 0) {
224 return true;
225 }
226 if (std::abs(render_time_ms - now_ms) > kMaxVideoDelayMs) {
227 int frame_delay = static_cast<int>(std::abs(render_time_ms - now_ms));
Mirko Bonadei675513b2017-11-09 11:09:25 +0100228 RTC_LOG(LS_WARNING)
229 << "A frame about to be decoded is out of the configured "
230 << "delay bounds (" << frame_delay << " > " << kMaxVideoDelayMs
231 << "). Resetting the video jitter buffer.";
stefan95e97542017-05-23 09:52:18 -0700232 return true;
233 }
234 if (static_cast<int>(timing_->TargetVideoDelay()) > kMaxVideoDelayMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100235 RTC_LOG(LS_WARNING) << "The video target delay has grown larger than "
236 << kMaxVideoDelayMs << " ms.";
stefan95e97542017-05-23 09:52:18 -0700237 return true;
238 }
239 return false;
240}
241
philipel4f6cd6a2016-08-03 10:59:32 +0200242void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) {
tommidb23ea62017-03-03 07:21:18 -0800243 TRACE_EVENT0("webrtc", "FrameBuffer::SetProtectionMode");
philipel4f6cd6a2016-08-03 10:59:32 +0200244 rtc::CritScope lock(&crit_);
245 protection_mode_ = mode;
246}
247
philipel504c47d2016-06-30 17:33:02 +0200248void FrameBuffer::Start() {
tommidb23ea62017-03-03 07:21:18 -0800249 TRACE_EVENT0("webrtc", "FrameBuffer::Start");
philipel29f730e2017-03-15 08:10:08 -0700250 rtc::CritScope lock(&crit_);
251 stopped_ = false;
philipel504c47d2016-06-30 17:33:02 +0200252}
253
254void FrameBuffer::Stop() {
tommidb23ea62017-03-03 07:21:18 -0800255 TRACE_EVENT0("webrtc", "FrameBuffer::Stop");
philipel29f730e2017-03-15 08:10:08 -0700256 rtc::CritScope lock(&crit_);
257 stopped_ = true;
tommi0a735642017-03-14 06:23:57 -0700258 new_continuous_frame_event_.Set();
philipel504c47d2016-06-30 17:33:02 +0200259}
260
philipele21be1d2017-09-25 06:37:12 -0700261void FrameBuffer::UpdateRtt(int64_t rtt_ms) {
262 rtc::CritScope lock(&crit_);
263 jitter_estimator_->UpdateRtt(rtt_ms);
264}
265
philipele7c891f2018-02-22 14:35:06 +0100266bool FrameBuffer::ValidReferences(const EncodedFrame& frame) const {
philipel0fa82a62018-03-19 15:34:53 +0100267 if (frame.id.picture_id < 0)
philipel3b3c9c42017-09-11 09:38:36 -0700268 return false;
269
philipel112adf92017-06-15 09:06:21 -0700270 for (size_t i = 0; i < frame.num_references; ++i) {
philipel0fa82a62018-03-19 15:34:53 +0100271 if (frame.references[i] < 0 || frame.references[i] >= frame.id.picture_id)
philipel112adf92017-06-15 09:06:21 -0700272 return false;
philipel3b3c9c42017-09-11 09:38:36 -0700273
philipel112adf92017-06-15 09:06:21 -0700274 for (size_t j = i + 1; j < frame.num_references; ++j) {
275 if (frame.references[i] == frame.references[j])
276 return false;
277 }
278 }
279
philipel0fa82a62018-03-19 15:34:53 +0100280 if (frame.inter_layer_predicted && frame.id.spatial_layer == 0)
philipel112adf92017-06-15 09:06:21 -0700281 return false;
282
283 return true;
284}
285
philipele7c891f2018-02-22 14:35:06 +0100286void FrameBuffer::UpdatePlayoutDelays(const EncodedFrame& frame) {
gnishb2a318b2017-05-10 09:21:33 -0700287 TRACE_EVENT0("webrtc", "FrameBuffer::UpdatePlayoutDelays");
288 PlayoutDelay playout_delay = frame.EncodedImage().playout_delay_;
289 if (playout_delay.min_ms >= 0)
290 timing_->set_min_playout_delay(playout_delay.min_ms);
291
292 if (playout_delay.max_ms >= 0)
293 timing_->set_max_playout_delay(playout_delay.max_ms);
philipel0a9f6de2018-02-28 11:29:47 +0100294
295 if (!frame.delayed_by_retransmission())
296 timing_->IncomingTimestamp(frame.timestamp, frame.ReceivedTime());
gnishb2a318b2017-05-10 09:21:33 -0700297}
298
philipele7c891f2018-02-22 14:35:06 +0100299int64_t FrameBuffer::InsertFrame(std::unique_ptr<EncodedFrame> frame) {
tommidb23ea62017-03-03 07:21:18 -0800300 TRACE_EVENT0("webrtc", "FrameBuffer::InsertFrame");
philipel93e451b2016-10-06 12:25:13 +0200301 RTC_DCHECK(frame);
philipela45102f2017-02-22 05:30:39 -0800302 if (stats_callback_)
ilnik6d5b4d62017-08-30 03:32:14 -0700303 stats_callback_->OnCompleteFrame(frame->is_keyframe(), frame->size(),
304 frame->contentType());
philipel0fa82a62018-03-19 15:34:53 +0100305 const VideoLayerFrameId& id = frame->id;
tommi0a735642017-03-14 06:23:57 -0700306
307 rtc::CritScope lock(&crit_);
philipel29f730e2017-03-15 08:10:08 -0700308
philipel1610f942017-12-12 13:58:31 +0100309 int64_t last_continuous_picture_id =
philipele0b2f152016-09-28 10:23:49 +0200310 last_continuous_frame_it_ == frames_.end()
311 ? -1
312 : last_continuous_frame_it_->first.picture_id;
313
philipel112adf92017-06-15 09:06:21 -0700314 if (!ValidReferences(*frame)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100315 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100316 << id.picture_id << ":"
317 << static_cast<int>(id.spatial_layer)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100318 << ") has invalid frame references, dropping frame.";
philipel112adf92017-06-15 09:06:21 -0700319 return last_continuous_picture_id;
320 }
321
philipele0b2f152016-09-28 10:23:49 +0200322 if (num_frames_buffered_ >= kMaxFramesBuffered) {
philipel9771c502018-03-02 11:06:27 +0100323 if (frame->is_keyframe()) {
324 RTC_LOG(LS_WARNING) << "Inserting keyframe (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100325 << id.picture_id << ":"
326 << static_cast<int>(id.spatial_layer)
philipel9771c502018-03-02 11:06:27 +0100327 << ") but buffer is full, clearing"
328 << " buffer and inserting the frame.";
329 ClearFramesAndHistory();
330 } else {
331 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100332 << id.picture_id << ":"
333 << static_cast<int>(id.spatial_layer)
philipel9771c502018-03-02 11:06:27 +0100334 << ") could not be inserted due to the frame "
335 << "buffer being full, dropping frame.";
336 return last_continuous_picture_id;
337 }
philipele0b2f152016-09-28 10:23:49 +0200338 }
339
philipele0b2f152016-09-28 10:23:49 +0200340 if (last_decoded_frame_it_ != frames_.end() &&
philipel0fa82a62018-03-19 15:34:53 +0100341 id <= last_decoded_frame_it_->first) {
philipelfcc60062017-01-18 05:35:20 -0800342 if (AheadOf(frame->timestamp, last_decoded_frame_timestamp_) &&
philipel3042c2d2017-08-18 04:55:02 -0700343 frame->is_keyframe()) {
philipelfcc60062017-01-18 05:35:20 -0800344 // If this frame has a newer timestamp but an earlier picture id then we
345 // assume there has been a jump in the picture id due to some encoder
346 // reconfiguration or some other reason. Even though this is not according
347 // to spec we can still continue to decode from this frame if it is a
348 // keyframe.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100349 RTC_LOG(LS_WARNING)
350 << "A jump in picture id was detected, clearing buffer.";
philipelfcc60062017-01-18 05:35:20 -0800351 ClearFramesAndHistory();
352 last_continuous_picture_id = -1;
353 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100354 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100355 << id.picture_id << ":"
356 << static_cast<int>(id.spatial_layer)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100357 << ") inserted after frame ("
358 << last_decoded_frame_it_->first.picture_id << ":"
359 << static_cast<int>(
360 last_decoded_frame_it_->first.spatial_layer)
361 << ") was handed off for decoding, dropping frame.";
philipelfcc60062017-01-18 05:35:20 -0800362 return last_continuous_picture_id;
363 }
philipele0b2f152016-09-28 10:23:49 +0200364 }
365
philipel146a48b2017-04-20 04:04:38 -0700366 // Test if inserting this frame would cause the order of the frames to become
367 // ambiguous (covering more than half the interval of 2^16). This can happen
368 // when the picture id make large jumps mid stream.
philipel0fa82a62018-03-19 15:34:53 +0100369 if (!frames_.empty() && id < frames_.begin()->first &&
370 frames_.rbegin()->first < id) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100371 RTC_LOG(LS_WARNING)
372 << "A jump in picture id was detected, clearing buffer.";
philipel146a48b2017-04-20 04:04:38 -0700373 ClearFramesAndHistory();
374 last_continuous_picture_id = -1;
375 }
376
philipel0fa82a62018-03-19 15:34:53 +0100377 auto info = frames_.emplace(id, FrameInfo()).first;
philipele0b2f152016-09-28 10:23:49 +0200378
philipel93e451b2016-10-06 12:25:13 +0200379 if (info->second.frame) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100380 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100381 << id.picture_id << ":"
382 << static_cast<int>(id.spatial_layer)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100383 << ") already inserted, dropping frame.";
philipele0b2f152016-09-28 10:23:49 +0200384 return last_continuous_picture_id;
385 }
386
philipel93e451b2016-10-06 12:25:13 +0200387 if (!UpdateFrameInfoWithIncomingFrame(*frame, info))
388 return last_continuous_picture_id;
gnishb2a318b2017-05-10 09:21:33 -0700389 UpdatePlayoutDelays(*frame);
philipel0a9f6de2018-02-28 11:29:47 +0100390
philipele0b2f152016-09-28 10:23:49 +0200391 info->second.frame = std::move(frame);
392 ++num_frames_buffered_;
393
394 if (info->second.num_missing_continuous == 0) {
395 info->second.continuous = true;
396 PropagateContinuity(info);
397 last_continuous_picture_id = last_continuous_frame_it_->first.picture_id;
398
399 // Since we now have new continuous frames there might be a better frame
400 // to return from NextFrame. Signal that thread so that it again can choose
401 // which frame to return.
tommi0a735642017-03-14 06:23:57 -0700402 new_continuous_frame_event_.Set();
philipele0b2f152016-09-28 10:23:49 +0200403 }
404
405 return last_continuous_picture_id;
philipelbe7a9e52016-05-19 12:19:35 +0200406}
407
philipele0b2f152016-09-28 10:23:49 +0200408void FrameBuffer::PropagateContinuity(FrameMap::iterator start) {
tommidb23ea62017-03-03 07:21:18 -0800409 TRACE_EVENT0("webrtc", "FrameBuffer::PropagateContinuity");
philipele0b2f152016-09-28 10:23:49 +0200410 RTC_DCHECK(start->second.continuous);
411 if (last_continuous_frame_it_ == frames_.end())
412 last_continuous_frame_it_ = start;
413
414 std::queue<FrameMap::iterator> continuous_frames;
415 continuous_frames.push(start);
416
417 // A simple BFS to traverse continuous frames.
418 while (!continuous_frames.empty()) {
419 auto frame = continuous_frames.front();
420 continuous_frames.pop();
421
422 if (last_continuous_frame_it_->first < frame->first)
423 last_continuous_frame_it_ = frame;
424
425 // Loop through all dependent frames, and if that frame no longer has
426 // any unfulfilled dependencies then that frame is continuous as well.
427 for (size_t d = 0; d < frame->second.num_dependent_frames; ++d) {
428 auto frame_ref = frames_.find(frame->second.dependent_frames[d]);
philipel112adf92017-06-15 09:06:21 -0700429 RTC_DCHECK(frame_ref != frames_.end());
philipele0b2f152016-09-28 10:23:49 +0200430
philipel112adf92017-06-15 09:06:21 -0700431 // TODO(philipel): Look into why we've seen this happen.
432 if (frame_ref != frames_.end()) {
433 --frame_ref->second.num_missing_continuous;
434 if (frame_ref->second.num_missing_continuous == 0) {
435 frame_ref->second.continuous = true;
436 continuous_frames.push(frame_ref);
437 }
philipele0b2f152016-09-28 10:23:49 +0200438 }
439 }
440 }
441}
442
443void FrameBuffer::PropagateDecodability(const FrameInfo& info) {
tommidb23ea62017-03-03 07:21:18 -0800444 TRACE_EVENT0("webrtc", "FrameBuffer::PropagateDecodability");
tommie95b78b2017-05-14 07:23:11 -0700445 RTC_CHECK(info.num_dependent_frames < FrameInfo::kMaxNumDependentFrames);
philipele0b2f152016-09-28 10:23:49 +0200446 for (size_t d = 0; d < info.num_dependent_frames; ++d) {
447 auto ref_info = frames_.find(info.dependent_frames[d]);
philipel93e451b2016-10-06 12:25:13 +0200448 RTC_DCHECK(ref_info != frames_.end());
tommie95b78b2017-05-14 07:23:11 -0700449 // TODO(philipel): Look into why we've seen this happen.
450 if (ref_info != frames_.end()) {
451 RTC_DCHECK_GT(ref_info->second.num_missing_decodable, 0U);
452 --ref_info->second.num_missing_decodable;
453 }
philipele0b2f152016-09-28 10:23:49 +0200454 }
455}
456
457void FrameBuffer::AdvanceLastDecodedFrame(FrameMap::iterator decoded) {
tommidb23ea62017-03-03 07:21:18 -0800458 TRACE_EVENT0("webrtc", "FrameBuffer::AdvanceLastDecodedFrame");
philipele0b2f152016-09-28 10:23:49 +0200459 if (last_decoded_frame_it_ == frames_.end()) {
460 last_decoded_frame_it_ = frames_.begin();
461 } else {
462 RTC_DCHECK(last_decoded_frame_it_->first < decoded->first);
463 ++last_decoded_frame_it_;
464 }
465 --num_frames_buffered_;
466 ++num_frames_history_;
467
468 // First, delete non-decoded frames from the history.
469 while (last_decoded_frame_it_ != decoded) {
470 if (last_decoded_frame_it_->second.frame)
471 --num_frames_buffered_;
472 last_decoded_frame_it_ = frames_.erase(last_decoded_frame_it_);
philipelbe7a9e52016-05-19 12:19:35 +0200473 }
474
philipele0b2f152016-09-28 10:23:49 +0200475 // Then remove old history if we have too much history saved.
476 if (num_frames_history_ > kMaxFramesHistory) {
477 frames_.erase(frames_.begin());
478 --num_frames_history_;
479 }
480}
481
philipele7c891f2018-02-22 14:35:06 +0100482bool FrameBuffer::UpdateFrameInfoWithIncomingFrame(const EncodedFrame& frame,
philipele0b2f152016-09-28 10:23:49 +0200483 FrameMap::iterator info) {
tommidb23ea62017-03-03 07:21:18 -0800484 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateFrameInfoWithIncomingFrame");
philipel0fa82a62018-03-19 15:34:53 +0100485 const VideoLayerFrameId& id = frame.id;
philipele0b2f152016-09-28 10:23:49 +0200486 info->second.num_missing_continuous = frame.num_references;
487 info->second.num_missing_decodable = frame.num_references;
488
489 RTC_DCHECK(last_decoded_frame_it_ == frames_.end() ||
490 last_decoded_frame_it_->first < info->first);
491
492 // Check how many dependencies that have already been fulfilled.
493 for (size_t i = 0; i < frame.num_references; ++i) {
philipel0fa82a62018-03-19 15:34:53 +0100494 VideoLayerFrameId ref_key(frame.references[i], frame.id.spatial_layer);
philipele0b2f152016-09-28 10:23:49 +0200495 auto ref_info = frames_.find(ref_key);
496
497 // Does |frame| depend on a frame earlier than the last decoded frame?
498 if (last_decoded_frame_it_ != frames_.end() &&
499 ref_key <= last_decoded_frame_it_->first) {
500 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 }
512
513 --info->second.num_missing_continuous;
514 --info->second.num_missing_decodable;
515 } else {
516 if (ref_info == frames_.end())
517 ref_info = frames_.insert(std::make_pair(ref_key, FrameInfo())).first;
518
519 if (ref_info->second.continuous)
520 --info->second.num_missing_continuous;
521
522 // Add backwards reference so |frame| can be updated when new
523 // frames are inserted or decoded.
524 ref_info->second.dependent_frames[ref_info->second.num_dependent_frames] =
philipel0fa82a62018-03-19 15:34:53 +0100525 id;
tommie95b78b2017-05-14 07:23:11 -0700526 RTC_DCHECK_LT(ref_info->second.num_dependent_frames,
527 (FrameInfo::kMaxNumDependentFrames - 1));
528 // TODO(philipel): Look into why this could happen and handle
529 // appropriately.
530 if (ref_info->second.num_dependent_frames <
531 (FrameInfo::kMaxNumDependentFrames - 1)) {
532 ++ref_info->second.num_dependent_frames;
533 }
philipele0b2f152016-09-28 10:23:49 +0200534 }
philipel93e451b2016-10-06 12:25:13 +0200535 RTC_DCHECK_LE(ref_info->second.num_missing_continuous,
536 ref_info->second.num_missing_decodable);
philipelbe7a9e52016-05-19 12:19:35 +0200537 }
538
philipele0b2f152016-09-28 10:23:49 +0200539 // Check if we have the lower spatial layer frame.
philipelbe7a9e52016-05-19 12:19:35 +0200540 if (frame.inter_layer_predicted) {
philipele0b2f152016-09-28 10:23:49 +0200541 ++info->second.num_missing_continuous;
542 ++info->second.num_missing_decodable;
543
philipel0fa82a62018-03-19 15:34:53 +0100544 VideoLayerFrameId ref_key(frame.id.picture_id, frame.id.spatial_layer - 1);
philipele0b2f152016-09-28 10:23:49 +0200545 // Gets or create the FrameInfo for the referenced frame.
546 auto ref_info = frames_.insert(std::make_pair(ref_key, FrameInfo())).first;
547 if (ref_info->second.continuous)
548 --info->second.num_missing_continuous;
549
550 if (ref_info == last_decoded_frame_it_) {
551 --info->second.num_missing_decodable;
552 } else {
553 ref_info->second.dependent_frames[ref_info->second.num_dependent_frames] =
philipel0fa82a62018-03-19 15:34:53 +0100554 id;
philipele0b2f152016-09-28 10:23:49 +0200555 ++ref_info->second.num_dependent_frames;
556 }
philipel93e451b2016-10-06 12:25:13 +0200557 RTC_DCHECK_LE(ref_info->second.num_missing_continuous,
558 ref_info->second.num_missing_decodable);
philipelbe7a9e52016-05-19 12:19:35 +0200559 }
560
philipel93e451b2016-10-06 12:25:13 +0200561 RTC_DCHECK_LE(info->second.num_missing_continuous,
562 info->second.num_missing_decodable);
563
philipelbe7a9e52016-05-19 12:19:35 +0200564 return true;
565}
566
philipelbe742702016-11-30 01:31:40 -0800567void FrameBuffer::UpdateJitterDelay() {
tommidb23ea62017-03-03 07:21:18 -0800568 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateJitterDelay");
philipela45102f2017-02-22 05:30:39 -0800569 if (!stats_callback_)
570 return;
philipelbe742702016-11-30 01:31:40 -0800571
philipela45102f2017-02-22 05:30:39 -0800572 int decode_ms;
573 int max_decode_ms;
574 int current_delay_ms;
575 int target_delay_ms;
576 int jitter_buffer_ms;
577 int min_playout_delay_ms;
578 int render_delay_ms;
579 if (timing_->GetTimings(&decode_ms, &max_decode_ms, &current_delay_ms,
580 &target_delay_ms, &jitter_buffer_ms,
581 &min_playout_delay_ms, &render_delay_ms)) {
582 stats_callback_->OnFrameBufferTimingsUpdated(
583 decode_ms, max_decode_ms, current_delay_ms, target_delay_ms,
584 jitter_buffer_ms, min_playout_delay_ms, render_delay_ms);
philipelbe742702016-11-30 01:31:40 -0800585 }
philipel266f0a42016-11-28 08:49:07 -0800586}
587
ilnik2edc6842017-07-06 03:06:50 -0700588void FrameBuffer::UpdateTimingFrameInfo() {
589 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateTimingFrameInfo");
590 rtc::Optional<TimingFrameInfo> info = timing_->GetTimingFrameInfo();
philipel97187112018-03-23 10:43:21 +0100591 if (info && stats_callback_)
ilnik2edc6842017-07-06 03:06:50 -0700592 stats_callback_->OnTimingFrameInfoUpdated(*info);
593}
594
philipelfcc60062017-01-18 05:35:20 -0800595void FrameBuffer::ClearFramesAndHistory() {
ilnik2edc6842017-07-06 03:06:50 -0700596 TRACE_EVENT0("webrtc", "FrameBuffer::ClearFramesAndHistory");
philipelfcc60062017-01-18 05:35:20 -0800597 frames_.clear();
598 last_decoded_frame_it_ = frames_.end();
599 last_continuous_frame_it_ = frames_.end();
philipel1c056252017-01-31 09:53:12 -0800600 next_frame_it_ = frames_.end();
philipelfcc60062017-01-18 05:35:20 -0800601 num_frames_history_ = 0;
602 num_frames_buffered_ = 0;
603}
604
philipelbe7a9e52016-05-19 12:19:35 +0200605} // namespace video_coding
606} // namespace webrtc