blob: 2a437d78b20c6d196dcd4849ee633ba9d471215c [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"
philipelbe7a9e52016-05-19 12:19:35 +020025
26namespace webrtc {
27namespace video_coding {
28
29namespace {
philipele0b2f152016-09-28 10:23:49 +020030// Max number of frames the buffer will hold.
31constexpr int kMaxFramesBuffered = 600;
philipelbe7a9e52016-05-19 12:19:35 +020032
philipele0b2f152016-09-28 10:23:49 +020033// Max number of decoded frame info that will be saved.
philipelfd5a20f2016-11-15 00:57:57 -080034constexpr int kMaxFramesHistory = 50;
philipel65e1f942017-07-24 08:26:53 -070035
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +010036// The time it's allowed for a frame to be late to its rendering prediction and
37// still be rendered.
Ilya Nikolaevskiy7eef0072018-02-28 09:59:26 +010038constexpr int kMaxAllowedFrameDelayMs = 5;
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +010039
philipel65e1f942017-07-24 08:26:53 -070040constexpr int64_t kLogNonDecodedIntervalMs = 5000;
philipelbe7a9e52016-05-19 12:19:35 +020041} // namespace
42
philipelbe7a9e52016-05-19 12:19:35 +020043FrameBuffer::FrameBuffer(Clock* clock,
44 VCMJitterEstimator* jitter_estimator,
philipela45102f2017-02-22 05:30:39 -080045 VCMTiming* timing,
46 VCMReceiveStatisticsCallback* stats_callback)
philipelbe7a9e52016-05-19 12:19:35 +020047 : clock_(clock),
tommi0a735642017-03-14 06:23:57 -070048 new_continuous_frame_event_(false, false),
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()),
gnishb2a318b2017-05-10 09:21:33 -070052 last_decoded_frame_timestamp_(0),
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
philipel1c056252017-01-31 09:53:12 -0800117 next_frame_it_ = frame_it;
philipele0b2f152016-09-28 10:23:49 +0200118 if (frame->RenderTime() == -1)
119 frame->SetRenderTime(timing_->RenderTimeMs(frame->timestamp, now_ms));
120 wait_ms = timing_->MaxWaitingTime(frame->RenderTime(), now_ms);
121
122 // This will cause the frame buffer to prefer high framerate rather
123 // than high resolution in the case of the decoder not decoding fast
124 // enough and the stream has multiple spatial and temporal layers.
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100125 // For multiple temporal layers it may cause non-base layer frames to be
126 // skipped if they are late.
Ilya Nikolaevskiy7eef0072018-02-28 09:59:26 +0100127 if (wait_ms < -kMaxAllowedFrameDelayMs)
philipele0b2f152016-09-28 10:23:49 +0200128 continue;
129
130 break;
131 }
132 } // rtc::Critscope lock(&crit_);
133
philipel1c056252017-01-31 09:53:12 -0800134 wait_ms = std::min<int64_t>(wait_ms, latest_return_time_ms - now_ms);
philipele0b2f152016-09-28 10:23:49 +0200135 wait_ms = std::max<int64_t>(wait_ms, 0);
tommi0a735642017-03-14 06:23:57 -0700136 } while (new_continuous_frame_event_.Wait(wait_ms));
philipele0b2f152016-09-28 10:23:49 +0200137
philipel29f730e2017-03-15 08:10:08 -0700138 {
139 rtc::CritScope lock(&crit_);
140 now_ms = clock_->TimeInMilliseconds();
141 if (next_frame_it_ != frames_.end()) {
philipele7c891f2018-02-22 14:35:06 +0100142 std::unique_ptr<EncodedFrame> frame =
philipel29f730e2017-03-15 08:10:08 -0700143 std::move(next_frame_it_->second.frame);
philipele0b2f152016-09-28 10:23:49 +0200144
philipel29f730e2017-03-15 08:10:08 -0700145 if (!frame->delayed_by_retransmission()) {
146 int64_t frame_delay;
philipele0754302017-01-25 08:56:23 -0800147
philipel29f730e2017-03-15 08:10:08 -0700148 if (inter_frame_delay_.CalculateDelay(frame->timestamp, &frame_delay,
149 frame->ReceivedTime())) {
150 jitter_estimator_->UpdateEstimate(frame_delay, frame->size());
151 }
152
153 float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0;
154 timing_->SetJitterDelay(jitter_estimator_->GetJitterEstimate(rtt_mult));
155 timing_->UpdateCurrentDelay(frame->RenderTime(), now_ms);
philipele21be1d2017-09-25 06:37:12 -0700156 } else {
philipel707f2782017-10-02 14:10:28 +0200157 if (webrtc::field_trial::IsEnabled("WebRTC-AddRttToPlayoutDelay"))
158 jitter_estimator_->FrameNacked();
philipele0754302017-01-25 08:56:23 -0800159 }
160
stefan95e97542017-05-23 09:52:18 -0700161 // Gracefully handle bad RTP timestamps and render time issues.
162 if (HasBadRenderTiming(*frame, now_ms)) {
163 jitter_estimator_->Reset();
164 timing_->Reset();
165 frame->SetRenderTime(timing_->RenderTimeMs(frame->timestamp, now_ms));
166 }
167
philipel29f730e2017-03-15 08:10:08 -0700168 UpdateJitterDelay();
ilnik2edc6842017-07-06 03:06:50 -0700169 UpdateTimingFrameInfo();
philipel29f730e2017-03-15 08:10:08 -0700170 PropagateDecodability(next_frame_it_->second);
brandtr9078d8c2017-04-27 07:07:27 -0700171
172 // Sanity check for RTP timestamp monotonicity.
173 if (last_decoded_frame_it_ != frames_.end()) {
philipel0fa82a62018-03-19 15:34:53 +0100174 const VideoLayerFrameId& last_decoded_frame_key =
175 last_decoded_frame_it_->first;
176 const VideoLayerFrameId& frame_key = next_frame_it_->first;
brandtr9078d8c2017-04-27 07:07:27 -0700177
178 const bool frame_is_higher_spatial_layer_of_last_decoded_frame =
179 last_decoded_frame_timestamp_ == frame->timestamp &&
180 last_decoded_frame_key.picture_id == frame_key.picture_id &&
181 last_decoded_frame_key.spatial_layer < frame_key.spatial_layer;
182
183 if (AheadOrAt(last_decoded_frame_timestamp_, frame->timestamp) &&
184 !frame_is_higher_spatial_layer_of_last_decoded_frame) {
185 // TODO(brandtr): Consider clearing the entire buffer when we hit
186 // these conditions.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100187 RTC_LOG(LS_WARNING)
188 << "Frame with (timestamp:picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100189 << frame->timestamp << ":" << frame->id.picture_id << ":"
190 << static_cast<int>(frame->id.spatial_layer) << ")"
Mirko Bonadei675513b2017-11-09 11:09:25 +0100191 << " sent to decoder after frame with"
192 << " (timestamp:picture_id:spatial_id) ("
193 << last_decoded_frame_timestamp_ << ":"
194 << last_decoded_frame_key.picture_id << ":"
195 << static_cast<int>(last_decoded_frame_key.spatial_layer) << ").";
brandtr9078d8c2017-04-27 07:07:27 -0700196 }
197 }
198
philipel29f730e2017-03-15 08:10:08 -0700199 AdvanceLastDecodedFrame(next_frame_it_);
200 last_decoded_frame_timestamp_ = frame->timestamp;
201 *frame_out = std::move(frame);
202 return kFrameFound;
philipelbe7a9e52016-05-19 12:19:35 +0200203 }
tommi0a735642017-03-14 06:23:57 -0700204 }
205
206 if (latest_return_time_ms - now_ms > 0) {
philipel1c056252017-01-31 09:53:12 -0800207 // If |next_frame_it_ == frames_.end()| and there is still time left, it
208 // means that the frame buffer was cleared as the thread in this function
209 // was waiting to acquire |crit_| in order to return. Wait for the
210 // remaining time and then return.
211 return NextFrame(latest_return_time_ms - now_ms, frame_out);
philipelbe7a9e52016-05-19 12:19:35 +0200212 }
tommi0a735642017-03-14 06:23:57 -0700213
214 return kTimeout;
philipelbe7a9e52016-05-19 12:19:35 +0200215}
216
philipele7c891f2018-02-22 14:35:06 +0100217bool FrameBuffer::HasBadRenderTiming(const EncodedFrame& frame,
218 int64_t now_ms) {
stefan95e97542017-05-23 09:52:18 -0700219 // Assume that render timing errors are due to changes in the video stream.
220 int64_t render_time_ms = frame.RenderTimeMs();
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200221 // Zero render time means render immediately.
222 if (render_time_ms == 0) {
223 return false;
224 }
stefan95e97542017-05-23 09:52:18 -0700225 if (render_time_ms < 0) {
226 return true;
227 }
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200228 const int64_t kMaxVideoDelayMs = 10000;
stefan95e97542017-05-23 09:52:18 -0700229 if (std::abs(render_time_ms - now_ms) > kMaxVideoDelayMs) {
230 int frame_delay = static_cast<int>(std::abs(render_time_ms - now_ms));
Mirko Bonadei675513b2017-11-09 11:09:25 +0100231 RTC_LOG(LS_WARNING)
232 << "A frame about to be decoded is out of the configured "
233 << "delay bounds (" << frame_delay << " > " << kMaxVideoDelayMs
234 << "). Resetting the video jitter buffer.";
stefan95e97542017-05-23 09:52:18 -0700235 return true;
236 }
237 if (static_cast<int>(timing_->TargetVideoDelay()) > kMaxVideoDelayMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100238 RTC_LOG(LS_WARNING) << "The video target delay has grown larger than "
239 << kMaxVideoDelayMs << " ms.";
stefan95e97542017-05-23 09:52:18 -0700240 return true;
241 }
242 return false;
243}
244
philipel4f6cd6a2016-08-03 10:59:32 +0200245void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) {
tommidb23ea62017-03-03 07:21:18 -0800246 TRACE_EVENT0("webrtc", "FrameBuffer::SetProtectionMode");
philipel4f6cd6a2016-08-03 10:59:32 +0200247 rtc::CritScope lock(&crit_);
248 protection_mode_ = mode;
249}
250
philipel504c47d2016-06-30 17:33:02 +0200251void FrameBuffer::Start() {
tommidb23ea62017-03-03 07:21:18 -0800252 TRACE_EVENT0("webrtc", "FrameBuffer::Start");
philipel29f730e2017-03-15 08:10:08 -0700253 rtc::CritScope lock(&crit_);
254 stopped_ = false;
philipel504c47d2016-06-30 17:33:02 +0200255}
256
257void FrameBuffer::Stop() {
tommidb23ea62017-03-03 07:21:18 -0800258 TRACE_EVENT0("webrtc", "FrameBuffer::Stop");
philipel29f730e2017-03-15 08:10:08 -0700259 rtc::CritScope lock(&crit_);
260 stopped_ = true;
tommi0a735642017-03-14 06:23:57 -0700261 new_continuous_frame_event_.Set();
philipel504c47d2016-06-30 17:33:02 +0200262}
263
philipele21be1d2017-09-25 06:37:12 -0700264void FrameBuffer::UpdateRtt(int64_t rtt_ms) {
265 rtc::CritScope lock(&crit_);
266 jitter_estimator_->UpdateRtt(rtt_ms);
267}
268
philipele7c891f2018-02-22 14:35:06 +0100269bool FrameBuffer::ValidReferences(const EncodedFrame& frame) const {
philipel0fa82a62018-03-19 15:34:53 +0100270 if (frame.id.picture_id < 0)
philipel3b3c9c42017-09-11 09:38:36 -0700271 return false;
272
philipel112adf92017-06-15 09:06:21 -0700273 for (size_t i = 0; i < frame.num_references; ++i) {
philipel0fa82a62018-03-19 15:34:53 +0100274 if (frame.references[i] < 0 || frame.references[i] >= frame.id.picture_id)
philipel112adf92017-06-15 09:06:21 -0700275 return false;
philipel3b3c9c42017-09-11 09:38:36 -0700276
philipel112adf92017-06-15 09:06:21 -0700277 for (size_t j = i + 1; j < frame.num_references; ++j) {
278 if (frame.references[i] == frame.references[j])
279 return false;
280 }
281 }
282
philipel0fa82a62018-03-19 15:34:53 +0100283 if (frame.inter_layer_predicted && frame.id.spatial_layer == 0)
philipel112adf92017-06-15 09:06:21 -0700284 return false;
285
286 return true;
287}
288
philipele7c891f2018-02-22 14:35:06 +0100289void FrameBuffer::UpdatePlayoutDelays(const EncodedFrame& frame) {
gnishb2a318b2017-05-10 09:21:33 -0700290 TRACE_EVENT0("webrtc", "FrameBuffer::UpdatePlayoutDelays");
291 PlayoutDelay playout_delay = frame.EncodedImage().playout_delay_;
292 if (playout_delay.min_ms >= 0)
293 timing_->set_min_playout_delay(playout_delay.min_ms);
294
295 if (playout_delay.max_ms >= 0)
296 timing_->set_max_playout_delay(playout_delay.max_ms);
philipel0a9f6de2018-02-28 11:29:47 +0100297
298 if (!frame.delayed_by_retransmission())
299 timing_->IncomingTimestamp(frame.timestamp, frame.ReceivedTime());
gnishb2a318b2017-05-10 09:21:33 -0700300}
301
philipele7c891f2018-02-22 14:35:06 +0100302int64_t FrameBuffer::InsertFrame(std::unique_ptr<EncodedFrame> frame) {
tommidb23ea62017-03-03 07:21:18 -0800303 TRACE_EVENT0("webrtc", "FrameBuffer::InsertFrame");
philipel93e451b2016-10-06 12:25:13 +0200304 RTC_DCHECK(frame);
philipela45102f2017-02-22 05:30:39 -0800305 if (stats_callback_)
ilnik6d5b4d62017-08-30 03:32:14 -0700306 stats_callback_->OnCompleteFrame(frame->is_keyframe(), frame->size(),
307 frame->contentType());
philipel0fa82a62018-03-19 15:34:53 +0100308 const VideoLayerFrameId& id = frame->id;
tommi0a735642017-03-14 06:23:57 -0700309
310 rtc::CritScope lock(&crit_);
philipel29f730e2017-03-15 08:10:08 -0700311
philipel1610f942017-12-12 13:58:31 +0100312 int64_t last_continuous_picture_id =
philipele0b2f152016-09-28 10:23:49 +0200313 last_continuous_frame_it_ == frames_.end()
314 ? -1
315 : last_continuous_frame_it_->first.picture_id;
316
philipel112adf92017-06-15 09:06:21 -0700317 if (!ValidReferences(*frame)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100318 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)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100321 << ") has invalid frame references, dropping frame.";
philipel112adf92017-06-15 09:06:21 -0700322 return last_continuous_picture_id;
323 }
324
philipele0b2f152016-09-28 10:23:49 +0200325 if (num_frames_buffered_ >= kMaxFramesBuffered) {
philipel9771c502018-03-02 11:06:27 +0100326 if (frame->is_keyframe()) {
327 RTC_LOG(LS_WARNING) << "Inserting keyframe (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100328 << id.picture_id << ":"
329 << static_cast<int>(id.spatial_layer)
philipel9771c502018-03-02 11:06:27 +0100330 << ") but buffer is full, clearing"
331 << " buffer and inserting the frame.";
332 ClearFramesAndHistory();
333 } else {
334 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100335 << id.picture_id << ":"
336 << static_cast<int>(id.spatial_layer)
philipel9771c502018-03-02 11:06:27 +0100337 << ") could not be inserted due to the frame "
338 << "buffer being full, dropping frame.";
339 return last_continuous_picture_id;
340 }
philipele0b2f152016-09-28 10:23:49 +0200341 }
342
philipele0b2f152016-09-28 10:23:49 +0200343 if (last_decoded_frame_it_ != frames_.end() &&
philipel0fa82a62018-03-19 15:34:53 +0100344 id <= last_decoded_frame_it_->first) {
philipelfcc60062017-01-18 05:35:20 -0800345 if (AheadOf(frame->timestamp, last_decoded_frame_timestamp_) &&
philipel3042c2d2017-08-18 04:55:02 -0700346 frame->is_keyframe()) {
philipelfcc60062017-01-18 05:35:20 -0800347 // If this frame has a newer timestamp but an earlier picture id then we
348 // assume there has been a jump in the picture id due to some encoder
349 // reconfiguration or some other reason. Even though this is not according
350 // to spec we can still continue to decode from this frame if it is a
351 // keyframe.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100352 RTC_LOG(LS_WARNING)
353 << "A jump in picture id was detected, clearing buffer.";
philipelfcc60062017-01-18 05:35:20 -0800354 ClearFramesAndHistory();
355 last_continuous_picture_id = -1;
356 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100357 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100358 << id.picture_id << ":"
359 << static_cast<int>(id.spatial_layer)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100360 << ") inserted after frame ("
361 << last_decoded_frame_it_->first.picture_id << ":"
362 << static_cast<int>(
363 last_decoded_frame_it_->first.spatial_layer)
364 << ") was handed off for decoding, dropping frame.";
philipelfcc60062017-01-18 05:35:20 -0800365 return last_continuous_picture_id;
366 }
philipele0b2f152016-09-28 10:23:49 +0200367 }
368
philipel146a48b2017-04-20 04:04:38 -0700369 // Test if inserting this frame would cause the order of the frames to become
370 // ambiguous (covering more than half the interval of 2^16). This can happen
371 // when the picture id make large jumps mid stream.
philipel0fa82a62018-03-19 15:34:53 +0100372 if (!frames_.empty() && id < frames_.begin()->first &&
373 frames_.rbegin()->first < id) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100374 RTC_LOG(LS_WARNING)
375 << "A jump in picture id was detected, clearing buffer.";
philipel146a48b2017-04-20 04:04:38 -0700376 ClearFramesAndHistory();
377 last_continuous_picture_id = -1;
378 }
379
philipel0fa82a62018-03-19 15:34:53 +0100380 auto info = frames_.emplace(id, FrameInfo()).first;
philipele0b2f152016-09-28 10:23:49 +0200381
philipel93e451b2016-10-06 12:25:13 +0200382 if (info->second.frame) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100383 RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
philipel0fa82a62018-03-19 15:34:53 +0100384 << id.picture_id << ":"
385 << static_cast<int>(id.spatial_layer)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100386 << ") already inserted, dropping frame.";
philipele0b2f152016-09-28 10:23:49 +0200387 return last_continuous_picture_id;
388 }
389
philipel93e451b2016-10-06 12:25:13 +0200390 if (!UpdateFrameInfoWithIncomingFrame(*frame, info))
391 return last_continuous_picture_id;
gnishb2a318b2017-05-10 09:21:33 -0700392 UpdatePlayoutDelays(*frame);
philipel0a9f6de2018-02-28 11:29:47 +0100393
philipele0b2f152016-09-28 10:23:49 +0200394 info->second.frame = std::move(frame);
395 ++num_frames_buffered_;
396
397 if (info->second.num_missing_continuous == 0) {
398 info->second.continuous = true;
399 PropagateContinuity(info);
400 last_continuous_picture_id = last_continuous_frame_it_->first.picture_id;
401
402 // Since we now have new continuous frames there might be a better frame
403 // to return from NextFrame. Signal that thread so that it again can choose
404 // which frame to return.
tommi0a735642017-03-14 06:23:57 -0700405 new_continuous_frame_event_.Set();
philipele0b2f152016-09-28 10:23:49 +0200406 }
407
408 return last_continuous_picture_id;
philipelbe7a9e52016-05-19 12:19:35 +0200409}
410
philipele0b2f152016-09-28 10:23:49 +0200411void FrameBuffer::PropagateContinuity(FrameMap::iterator start) {
tommidb23ea62017-03-03 07:21:18 -0800412 TRACE_EVENT0("webrtc", "FrameBuffer::PropagateContinuity");
philipele0b2f152016-09-28 10:23:49 +0200413 RTC_DCHECK(start->second.continuous);
414 if (last_continuous_frame_it_ == frames_.end())
415 last_continuous_frame_it_ = start;
416
417 std::queue<FrameMap::iterator> continuous_frames;
418 continuous_frames.push(start);
419
420 // A simple BFS to traverse continuous frames.
421 while (!continuous_frames.empty()) {
422 auto frame = continuous_frames.front();
423 continuous_frames.pop();
424
425 if (last_continuous_frame_it_->first < frame->first)
426 last_continuous_frame_it_ = frame;
427
428 // Loop through all dependent frames, and if that frame no longer has
429 // any unfulfilled dependencies then that frame is continuous as well.
430 for (size_t d = 0; d < frame->second.num_dependent_frames; ++d) {
431 auto frame_ref = frames_.find(frame->second.dependent_frames[d]);
philipel112adf92017-06-15 09:06:21 -0700432 RTC_DCHECK(frame_ref != frames_.end());
philipele0b2f152016-09-28 10:23:49 +0200433
philipel112adf92017-06-15 09:06:21 -0700434 // TODO(philipel): Look into why we've seen this happen.
435 if (frame_ref != frames_.end()) {
436 --frame_ref->second.num_missing_continuous;
437 if (frame_ref->second.num_missing_continuous == 0) {
438 frame_ref->second.continuous = true;
439 continuous_frames.push(frame_ref);
440 }
philipele0b2f152016-09-28 10:23:49 +0200441 }
442 }
443 }
444}
445
446void FrameBuffer::PropagateDecodability(const FrameInfo& info) {
tommidb23ea62017-03-03 07:21:18 -0800447 TRACE_EVENT0("webrtc", "FrameBuffer::PropagateDecodability");
tommie95b78b2017-05-14 07:23:11 -0700448 RTC_CHECK(info.num_dependent_frames < FrameInfo::kMaxNumDependentFrames);
philipele0b2f152016-09-28 10:23:49 +0200449 for (size_t d = 0; d < info.num_dependent_frames; ++d) {
450 auto ref_info = frames_.find(info.dependent_frames[d]);
philipel93e451b2016-10-06 12:25:13 +0200451 RTC_DCHECK(ref_info != frames_.end());
tommie95b78b2017-05-14 07:23:11 -0700452 // TODO(philipel): Look into why we've seen this happen.
453 if (ref_info != frames_.end()) {
454 RTC_DCHECK_GT(ref_info->second.num_missing_decodable, 0U);
455 --ref_info->second.num_missing_decodable;
456 }
philipele0b2f152016-09-28 10:23:49 +0200457 }
458}
459
460void FrameBuffer::AdvanceLastDecodedFrame(FrameMap::iterator decoded) {
tommidb23ea62017-03-03 07:21:18 -0800461 TRACE_EVENT0("webrtc", "FrameBuffer::AdvanceLastDecodedFrame");
philipele0b2f152016-09-28 10:23:49 +0200462 if (last_decoded_frame_it_ == frames_.end()) {
463 last_decoded_frame_it_ = frames_.begin();
464 } else {
465 RTC_DCHECK(last_decoded_frame_it_->first < decoded->first);
466 ++last_decoded_frame_it_;
467 }
468 --num_frames_buffered_;
469 ++num_frames_history_;
470
471 // First, delete non-decoded frames from the history.
472 while (last_decoded_frame_it_ != decoded) {
473 if (last_decoded_frame_it_->second.frame)
474 --num_frames_buffered_;
475 last_decoded_frame_it_ = frames_.erase(last_decoded_frame_it_);
philipelbe7a9e52016-05-19 12:19:35 +0200476 }
477
philipele0b2f152016-09-28 10:23:49 +0200478 // Then remove old history if we have too much history saved.
479 if (num_frames_history_ > kMaxFramesHistory) {
480 frames_.erase(frames_.begin());
481 --num_frames_history_;
482 }
483}
484
philipele7c891f2018-02-22 14:35:06 +0100485bool FrameBuffer::UpdateFrameInfoWithIncomingFrame(const EncodedFrame& frame,
philipele0b2f152016-09-28 10:23:49 +0200486 FrameMap::iterator info) {
tommidb23ea62017-03-03 07:21:18 -0800487 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateFrameInfoWithIncomingFrame");
philipel0fa82a62018-03-19 15:34:53 +0100488 const VideoLayerFrameId& id = frame.id;
philipele0b2f152016-09-28 10:23:49 +0200489 info->second.num_missing_continuous = frame.num_references;
490 info->second.num_missing_decodable = frame.num_references;
491
492 RTC_DCHECK(last_decoded_frame_it_ == frames_.end() ||
493 last_decoded_frame_it_->first < info->first);
494
495 // Check how many dependencies that have already been fulfilled.
496 for (size_t i = 0; i < frame.num_references; ++i) {
philipel0fa82a62018-03-19 15:34:53 +0100497 VideoLayerFrameId ref_key(frame.references[i], frame.id.spatial_layer);
philipele0b2f152016-09-28 10:23:49 +0200498 auto ref_info = frames_.find(ref_key);
499
500 // Does |frame| depend on a frame earlier than the last decoded frame?
501 if (last_decoded_frame_it_ != frames_.end() &&
502 ref_key <= last_decoded_frame_it_->first) {
503 if (ref_info == frames_.end()) {
philipel65e1f942017-07-24 08:26:53 -0700504 int64_t now_ms = clock_->TimeInMilliseconds();
505 if (last_log_non_decoded_ms_ + kLogNonDecodedIntervalMs < now_ms) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100506 RTC_LOG(LS_WARNING)
philipel0fa82a62018-03-19 15:34:53 +0100507 << "Frame with (picture_id:spatial_id) (" << id.picture_id << ":"
508 << static_cast<int>(id.spatial_layer)
philipel65e1f942017-07-24 08:26:53 -0700509 << ") depends on a non-decoded frame more previous than"
510 << " the last decoded frame, dropping frame.";
511 last_log_non_decoded_ms_ = now_ms;
512 }
philipele0b2f152016-09-28 10:23:49 +0200513 return false;
514 }
515
516 --info->second.num_missing_continuous;
517 --info->second.num_missing_decodable;
518 } else {
519 if (ref_info == frames_.end())
520 ref_info = frames_.insert(std::make_pair(ref_key, FrameInfo())).first;
521
522 if (ref_info->second.continuous)
523 --info->second.num_missing_continuous;
524
525 // Add backwards reference so |frame| can be updated when new
526 // frames are inserted or decoded.
527 ref_info->second.dependent_frames[ref_info->second.num_dependent_frames] =
philipel0fa82a62018-03-19 15:34:53 +0100528 id;
tommie95b78b2017-05-14 07:23:11 -0700529 RTC_DCHECK_LT(ref_info->second.num_dependent_frames,
530 (FrameInfo::kMaxNumDependentFrames - 1));
531 // TODO(philipel): Look into why this could happen and handle
532 // appropriately.
533 if (ref_info->second.num_dependent_frames <
534 (FrameInfo::kMaxNumDependentFrames - 1)) {
535 ++ref_info->second.num_dependent_frames;
536 }
philipele0b2f152016-09-28 10:23:49 +0200537 }
philipel93e451b2016-10-06 12:25:13 +0200538 RTC_DCHECK_LE(ref_info->second.num_missing_continuous,
539 ref_info->second.num_missing_decodable);
philipelbe7a9e52016-05-19 12:19:35 +0200540 }
541
philipele0b2f152016-09-28 10:23:49 +0200542 // Check if we have the lower spatial layer frame.
philipelbe7a9e52016-05-19 12:19:35 +0200543 if (frame.inter_layer_predicted) {
philipele0b2f152016-09-28 10:23:49 +0200544 ++info->second.num_missing_continuous;
545 ++info->second.num_missing_decodable;
546
philipel0fa82a62018-03-19 15:34:53 +0100547 VideoLayerFrameId ref_key(frame.id.picture_id, frame.id.spatial_layer - 1);
philipele0b2f152016-09-28 10:23:49 +0200548 // Gets or create the FrameInfo for the referenced frame.
549 auto ref_info = frames_.insert(std::make_pair(ref_key, FrameInfo())).first;
550 if (ref_info->second.continuous)
551 --info->second.num_missing_continuous;
552
553 if (ref_info == last_decoded_frame_it_) {
554 --info->second.num_missing_decodable;
555 } else {
556 ref_info->second.dependent_frames[ref_info->second.num_dependent_frames] =
philipel0fa82a62018-03-19 15:34:53 +0100557 id;
philipele0b2f152016-09-28 10:23:49 +0200558 ++ref_info->second.num_dependent_frames;
559 }
philipel93e451b2016-10-06 12:25:13 +0200560 RTC_DCHECK_LE(ref_info->second.num_missing_continuous,
561 ref_info->second.num_missing_decodable);
philipelbe7a9e52016-05-19 12:19:35 +0200562 }
563
philipel93e451b2016-10-06 12:25:13 +0200564 RTC_DCHECK_LE(info->second.num_missing_continuous,
565 info->second.num_missing_decodable);
566
philipelbe7a9e52016-05-19 12:19:35 +0200567 return true;
568}
569
philipelbe742702016-11-30 01:31:40 -0800570void FrameBuffer::UpdateJitterDelay() {
tommidb23ea62017-03-03 07:21:18 -0800571 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateJitterDelay");
philipela45102f2017-02-22 05:30:39 -0800572 if (!stats_callback_)
573 return;
philipelbe742702016-11-30 01:31:40 -0800574
philipela45102f2017-02-22 05:30:39 -0800575 int decode_ms;
576 int max_decode_ms;
577 int current_delay_ms;
578 int target_delay_ms;
579 int jitter_buffer_ms;
580 int min_playout_delay_ms;
581 int render_delay_ms;
582 if (timing_->GetTimings(&decode_ms, &max_decode_ms, &current_delay_ms,
583 &target_delay_ms, &jitter_buffer_ms,
584 &min_playout_delay_ms, &render_delay_ms)) {
585 stats_callback_->OnFrameBufferTimingsUpdated(
586 decode_ms, max_decode_ms, current_delay_ms, target_delay_ms,
587 jitter_buffer_ms, min_playout_delay_ms, render_delay_ms);
philipelbe742702016-11-30 01:31:40 -0800588 }
philipel266f0a42016-11-28 08:49:07 -0800589}
590
ilnik2edc6842017-07-06 03:06:50 -0700591void FrameBuffer::UpdateTimingFrameInfo() {
592 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateTimingFrameInfo");
593 rtc::Optional<TimingFrameInfo> info = timing_->GetTimingFrameInfo();
philipel97187112018-03-23 10:43:21 +0100594 if (info && stats_callback_)
ilnik2edc6842017-07-06 03:06:50 -0700595 stats_callback_->OnTimingFrameInfoUpdated(*info);
596}
597
philipelfcc60062017-01-18 05:35:20 -0800598void FrameBuffer::ClearFramesAndHistory() {
ilnik2edc6842017-07-06 03:06:50 -0700599 TRACE_EVENT0("webrtc", "FrameBuffer::ClearFramesAndHistory");
philipelfcc60062017-01-18 05:35:20 -0800600 frames_.clear();
601 last_decoded_frame_it_ = frames_.end();
602 last_continuous_frame_it_ = frames_.end();
philipel1c056252017-01-31 09:53:12 -0800603 next_frame_it_ = frames_.end();
philipelfcc60062017-01-18 05:35:20 -0800604 num_frames_history_ = 0;
605 num_frames_buffered_ = 0;
606}
607
Niels Möllerbe682d42018-03-27 08:31:45 +0200608FrameBuffer::FrameInfo::FrameInfo() = default;
609FrameBuffer::FrameInfo::FrameInfo(FrameInfo&&) = default;
610FrameBuffer::FrameInfo::~FrameInfo() = default;
611
philipelbe7a9e52016-05-19 12:19:35 +0200612} // namespace video_coding
613} // namespace webrtc