blob: b4bb0028022f8be34f7db55082512452f9f24900 [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"
24#include "system_wrappers/include/metrics.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
36constexpr int64_t kLogNonDecodedIntervalMs = 5000;
philipelbe7a9e52016-05-19 12:19:35 +020037} // namespace
38
philipelbe7a9e52016-05-19 12:19:35 +020039FrameBuffer::FrameBuffer(Clock* clock,
40 VCMJitterEstimator* jitter_estimator,
philipela45102f2017-02-22 05:30:39 -080041 VCMTiming* timing,
42 VCMReceiveStatisticsCallback* stats_callback)
philipelbe7a9e52016-05-19 12:19:35 +020043 : clock_(clock),
tommi0a735642017-03-14 06:23:57 -070044 new_continuous_frame_event_(false, false),
philipelbe7a9e52016-05-19 12:19:35 +020045 jitter_estimator_(jitter_estimator),
46 timing_(timing),
philipel4f6cd6a2016-08-03 10:59:32 +020047 inter_frame_delay_(clock_->TimeInMilliseconds()),
gnishb2a318b2017-05-10 09:21:33 -070048 last_decoded_frame_timestamp_(0),
philipele0b2f152016-09-28 10:23:49 +020049 last_decoded_frame_it_(frames_.end()),
50 last_continuous_frame_it_(frames_.end()),
51 num_frames_history_(0),
52 num_frames_buffered_(0),
philipel29f730e2017-03-15 08:10:08 -070053 stopped_(false),
philipela45102f2017-02-22 05:30:39 -080054 protection_mode_(kProtectionNack),
philipel65e1f942017-07-24 08:26:53 -070055 stats_callback_(stats_callback),
56 last_log_non_decoded_ms_(-kLogNonDecodedIntervalMs) {}
philipel266f0a42016-11-28 08:49:07 -080057
philipela45102f2017-02-22 05:30:39 -080058FrameBuffer::~FrameBuffer() {}
philipelbe7a9e52016-05-19 12:19:35 +020059
philipel75562822016-09-05 10:57:41 +020060FrameBuffer::ReturnReason FrameBuffer::NextFrame(
61 int64_t max_wait_time_ms,
philipel3042c2d2017-08-18 04:55:02 -070062 std::unique_ptr<FrameObject>* frame_out,
63 bool keyframe_required) {
tommidb23ea62017-03-03 07:21:18 -080064 TRACE_EVENT0("webrtc", "FrameBuffer::NextFrame");
philipel1c056252017-01-31 09:53:12 -080065 int64_t latest_return_time_ms =
66 clock_->TimeInMilliseconds() + max_wait_time_ms;
philipel504c47d2016-06-30 17:33:02 +020067 int64_t wait_ms = max_wait_time_ms;
philipel29f730e2017-03-15 08:10:08 -070068 int64_t now_ms = 0;
philipele0b2f152016-09-28 10:23:49 +020069
70 do {
philipel29f730e2017-03-15 08:10:08 -070071 now_ms = clock_->TimeInMilliseconds();
philipel504c47d2016-06-30 17:33:02 +020072 {
73 rtc::CritScope lock(&crit_);
tommi0a735642017-03-14 06:23:57 -070074 new_continuous_frame_event_.Reset();
philipel29f730e2017-03-15 08:10:08 -070075 if (stopped_)
76 return kStopped;
77
78 wait_ms = max_wait_time_ms;
79
philipele0b2f152016-09-28 10:23:49 +020080 // Need to hold |crit_| in order to use |frames_|, therefore we
81 // set it here in the loop instead of outside the loop in order to not
82 // acquire the lock unnecesserily.
philipel1c056252017-01-31 09:53:12 -080083 next_frame_it_ = frames_.end();
philipelbe7a9e52016-05-19 12:19:35 +020084
philipele0b2f152016-09-28 10:23:49 +020085 // |frame_it| points to the first frame after the
86 // |last_decoded_frame_it_|.
87 auto frame_it = frames_.end();
88 if (last_decoded_frame_it_ == frames_.end()) {
89 frame_it = frames_.begin();
philipelbe7a9e52016-05-19 12:19:35 +020090 } else {
philipele0b2f152016-09-28 10:23:49 +020091 frame_it = last_decoded_frame_it_;
92 ++frame_it;
philipelbe7a9e52016-05-19 12:19:35 +020093 }
philipele0b2f152016-09-28 10:23:49 +020094
95 // |continuous_end_it| points to the first frame after the
96 // |last_continuous_frame_it_|.
97 auto continuous_end_it = last_continuous_frame_it_;
98 if (continuous_end_it != frames_.end())
99 ++continuous_end_it;
100
philipel146a48b2017-04-20 04:04:38 -0700101 for (; frame_it != continuous_end_it && frame_it != frames_.end();
102 ++frame_it) {
philipel93e451b2016-10-06 12:25:13 +0200103 if (!frame_it->second.continuous ||
104 frame_it->second.num_missing_decodable > 0) {
philipele0b2f152016-09-28 10:23:49 +0200105 continue;
philipel93e451b2016-10-06 12:25:13 +0200106 }
philipele0b2f152016-09-28 10:23:49 +0200107
108 FrameObject* frame = frame_it->second.frame.get();
philipel3042c2d2017-08-18 04:55:02 -0700109
110 if (keyframe_required && !frame->is_keyframe())
111 continue;
112
philipel1c056252017-01-31 09:53:12 -0800113 next_frame_it_ = frame_it;
philipele0b2f152016-09-28 10:23:49 +0200114 if (frame->RenderTime() == -1)
115 frame->SetRenderTime(timing_->RenderTimeMs(frame->timestamp, now_ms));
116 wait_ms = timing_->MaxWaitingTime(frame->RenderTime(), now_ms);
117
118 // This will cause the frame buffer to prefer high framerate rather
119 // than high resolution in the case of the decoder not decoding fast
120 // enough and the stream has multiple spatial and temporal layers.
121 if (wait_ms == 0)
122 continue;
123
124 break;
125 }
126 } // rtc::Critscope lock(&crit_);
127
philipel1c056252017-01-31 09:53:12 -0800128 wait_ms = std::min<int64_t>(wait_ms, latest_return_time_ms - now_ms);
philipele0b2f152016-09-28 10:23:49 +0200129 wait_ms = std::max<int64_t>(wait_ms, 0);
tommi0a735642017-03-14 06:23:57 -0700130 } while (new_continuous_frame_event_.Wait(wait_ms));
philipele0b2f152016-09-28 10:23:49 +0200131
philipel29f730e2017-03-15 08:10:08 -0700132 {
133 rtc::CritScope lock(&crit_);
134 now_ms = clock_->TimeInMilliseconds();
135 if (next_frame_it_ != frames_.end()) {
136 std::unique_ptr<FrameObject> frame =
137 std::move(next_frame_it_->second.frame);
philipele0b2f152016-09-28 10:23:49 +0200138
philipel29f730e2017-03-15 08:10:08 -0700139 if (!frame->delayed_by_retransmission()) {
140 int64_t frame_delay;
philipele0754302017-01-25 08:56:23 -0800141
philipel29f730e2017-03-15 08:10:08 -0700142 if (inter_frame_delay_.CalculateDelay(frame->timestamp, &frame_delay,
143 frame->ReceivedTime())) {
144 jitter_estimator_->UpdateEstimate(frame_delay, frame->size());
145 }
146
147 float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0;
148 timing_->SetJitterDelay(jitter_estimator_->GetJitterEstimate(rtt_mult));
149 timing_->UpdateCurrentDelay(frame->RenderTime(), now_ms);
philipele21be1d2017-09-25 06:37:12 -0700150 } else {
151 jitter_estimator_->FrameNacked();
philipele0754302017-01-25 08:56:23 -0800152 }
153
stefan95e97542017-05-23 09:52:18 -0700154 // Gracefully handle bad RTP timestamps and render time issues.
155 if (HasBadRenderTiming(*frame, now_ms)) {
156 jitter_estimator_->Reset();
157 timing_->Reset();
158 frame->SetRenderTime(timing_->RenderTimeMs(frame->timestamp, now_ms));
159 }
160
philipel29f730e2017-03-15 08:10:08 -0700161 UpdateJitterDelay();
ilnik2edc6842017-07-06 03:06:50 -0700162 UpdateTimingFrameInfo();
philipel29f730e2017-03-15 08:10:08 -0700163 PropagateDecodability(next_frame_it_->second);
brandtr9078d8c2017-04-27 07:07:27 -0700164
165 // Sanity check for RTP timestamp monotonicity.
166 if (last_decoded_frame_it_ != frames_.end()) {
167 const FrameKey& last_decoded_frame_key = last_decoded_frame_it_->first;
168 const FrameKey& frame_key = next_frame_it_->first;
169
170 const bool frame_is_higher_spatial_layer_of_last_decoded_frame =
171 last_decoded_frame_timestamp_ == frame->timestamp &&
172 last_decoded_frame_key.picture_id == frame_key.picture_id &&
173 last_decoded_frame_key.spatial_layer < frame_key.spatial_layer;
174
175 if (AheadOrAt(last_decoded_frame_timestamp_, frame->timestamp) &&
176 !frame_is_higher_spatial_layer_of_last_decoded_frame) {
177 // TODO(brandtr): Consider clearing the entire buffer when we hit
178 // these conditions.
179 LOG(LS_WARNING) << "Frame with (timestamp:picture_id:spatial_id) ("
180 << frame->timestamp << ":" << frame->picture_id << ":"
181 << static_cast<int>(frame->spatial_layer) << ")"
182 << " sent to decoder after frame with"
183 << " (timestamp:picture_id:spatial_id) ("
184 << last_decoded_frame_timestamp_ << ":"
185 << last_decoded_frame_key.picture_id << ":"
186 << static_cast<int>(
187 last_decoded_frame_key.spatial_layer)
188 << ").";
189 }
190 }
191
philipel29f730e2017-03-15 08:10:08 -0700192 AdvanceLastDecodedFrame(next_frame_it_);
193 last_decoded_frame_timestamp_ = frame->timestamp;
194 *frame_out = std::move(frame);
195 return kFrameFound;
philipelbe7a9e52016-05-19 12:19:35 +0200196 }
tommi0a735642017-03-14 06:23:57 -0700197 }
198
199 if (latest_return_time_ms - now_ms > 0) {
philipel1c056252017-01-31 09:53:12 -0800200 // If |next_frame_it_ == frames_.end()| and there is still time left, it
201 // means that the frame buffer was cleared as the thread in this function
202 // was waiting to acquire |crit_| in order to return. Wait for the
203 // remaining time and then return.
204 return NextFrame(latest_return_time_ms - now_ms, frame_out);
philipelbe7a9e52016-05-19 12:19:35 +0200205 }
tommi0a735642017-03-14 06:23:57 -0700206
207 return kTimeout;
philipelbe7a9e52016-05-19 12:19:35 +0200208}
209
stefan95e97542017-05-23 09:52:18 -0700210bool FrameBuffer::HasBadRenderTiming(const FrameObject& frame, int64_t now_ms) {
211 // Assume that render timing errors are due to changes in the video stream.
212 int64_t render_time_ms = frame.RenderTimeMs();
213 const int64_t kMaxVideoDelayMs = 10000;
214 if (render_time_ms < 0) {
215 return true;
216 }
217 if (std::abs(render_time_ms - now_ms) > kMaxVideoDelayMs) {
218 int frame_delay = static_cast<int>(std::abs(render_time_ms - now_ms));
219 LOG(LS_WARNING) << "A frame about to be decoded is out of the configured "
220 << "delay bounds (" << frame_delay << " > "
221 << kMaxVideoDelayMs
222 << "). Resetting the video jitter buffer.";
223 return true;
224 }
225 if (static_cast<int>(timing_->TargetVideoDelay()) > kMaxVideoDelayMs) {
226 LOG(LS_WARNING) << "The video target delay has grown larger than "
227 << kMaxVideoDelayMs << " ms.";
228 return true;
229 }
230 return false;
231}
232
philipel4f6cd6a2016-08-03 10:59:32 +0200233void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) {
tommidb23ea62017-03-03 07:21:18 -0800234 TRACE_EVENT0("webrtc", "FrameBuffer::SetProtectionMode");
philipel4f6cd6a2016-08-03 10:59:32 +0200235 rtc::CritScope lock(&crit_);
236 protection_mode_ = mode;
237}
238
philipel504c47d2016-06-30 17:33:02 +0200239void FrameBuffer::Start() {
tommidb23ea62017-03-03 07:21:18 -0800240 TRACE_EVENT0("webrtc", "FrameBuffer::Start");
philipel29f730e2017-03-15 08:10:08 -0700241 rtc::CritScope lock(&crit_);
242 stopped_ = false;
philipel504c47d2016-06-30 17:33:02 +0200243}
244
245void FrameBuffer::Stop() {
tommidb23ea62017-03-03 07:21:18 -0800246 TRACE_EVENT0("webrtc", "FrameBuffer::Stop");
philipel29f730e2017-03-15 08:10:08 -0700247 rtc::CritScope lock(&crit_);
248 stopped_ = true;
tommi0a735642017-03-14 06:23:57 -0700249 new_continuous_frame_event_.Set();
philipel504c47d2016-06-30 17:33:02 +0200250}
251
philipele21be1d2017-09-25 06:37:12 -0700252void FrameBuffer::UpdateRtt(int64_t rtt_ms) {
253 rtc::CritScope lock(&crit_);
254 jitter_estimator_->UpdateRtt(rtt_ms);
255}
256
philipel112adf92017-06-15 09:06:21 -0700257bool FrameBuffer::ValidReferences(const FrameObject& frame) const {
philipel3b3c9c42017-09-11 09:38:36 -0700258 if (frame.picture_id < 0)
259 return false;
260
philipel112adf92017-06-15 09:06:21 -0700261 for (size_t i = 0; i < frame.num_references; ++i) {
philipel3b3c9c42017-09-11 09:38:36 -0700262 if (frame.references[i] < 0 || frame.references[i] >= frame.picture_id)
philipel112adf92017-06-15 09:06:21 -0700263 return false;
philipel3b3c9c42017-09-11 09:38:36 -0700264
philipel112adf92017-06-15 09:06:21 -0700265 for (size_t j = i + 1; j < frame.num_references; ++j) {
266 if (frame.references[i] == frame.references[j])
267 return false;
268 }
269 }
270
271 if (frame.inter_layer_predicted && frame.spatial_layer == 0)
272 return false;
273
274 return true;
275}
276
gnishb2a318b2017-05-10 09:21:33 -0700277void FrameBuffer::UpdatePlayoutDelays(const FrameObject& frame) {
278 TRACE_EVENT0("webrtc", "FrameBuffer::UpdatePlayoutDelays");
279 PlayoutDelay playout_delay = frame.EncodedImage().playout_delay_;
280 if (playout_delay.min_ms >= 0)
281 timing_->set_min_playout_delay(playout_delay.min_ms);
282
283 if (playout_delay.max_ms >= 0)
284 timing_->set_max_playout_delay(playout_delay.max_ms);
285}
286
philipele0b2f152016-09-28 10:23:49 +0200287int FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) {
tommidb23ea62017-03-03 07:21:18 -0800288 TRACE_EVENT0("webrtc", "FrameBuffer::InsertFrame");
philipel93e451b2016-10-06 12:25:13 +0200289 RTC_DCHECK(frame);
philipela45102f2017-02-22 05:30:39 -0800290 if (stats_callback_)
ilnik6d5b4d62017-08-30 03:32:14 -0700291 stats_callback_->OnCompleteFrame(frame->is_keyframe(), frame->size(),
292 frame->contentType());
philipelbe7a9e52016-05-19 12:19:35 +0200293 FrameKey key(frame->picture_id, frame->spatial_layer);
tommi0a735642017-03-14 06:23:57 -0700294
295 rtc::CritScope lock(&crit_);
philipel29f730e2017-03-15 08:10:08 -0700296
philipele0b2f152016-09-28 10:23:49 +0200297 int last_continuous_picture_id =
298 last_continuous_frame_it_ == frames_.end()
299 ? -1
300 : last_continuous_frame_it_->first.picture_id;
301
philipel112adf92017-06-15 09:06:21 -0700302 if (!ValidReferences(*frame)) {
303 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id
304 << ":" << static_cast<int>(key.spatial_layer)
305 << ") has invalid frame references, dropping frame.";
306 return last_continuous_picture_id;
307 }
308
philipele0b2f152016-09-28 10:23:49 +0200309 if (num_frames_buffered_ >= kMaxFramesBuffered) {
310 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id
311 << ":" << static_cast<int>(key.spatial_layer)
312 << ") could not be inserted due to the frame "
313 << "buffer being full, dropping frame.";
314 return last_continuous_picture_id;
315 }
316
philipele0b2f152016-09-28 10:23:49 +0200317 if (last_decoded_frame_it_ != frames_.end() &&
philipelf6842692017-04-28 03:29:15 -0700318 key <= last_decoded_frame_it_->first) {
philipelfcc60062017-01-18 05:35:20 -0800319 if (AheadOf(frame->timestamp, last_decoded_frame_timestamp_) &&
philipel3042c2d2017-08-18 04:55:02 -0700320 frame->is_keyframe()) {
philipelfcc60062017-01-18 05:35:20 -0800321 // If this frame has a newer timestamp but an earlier picture id then we
322 // assume there has been a jump in the picture id due to some encoder
323 // reconfiguration or some other reason. Even though this is not according
324 // to spec we can still continue to decode from this frame if it is a
325 // keyframe.
326 LOG(LS_WARNING) << "A jump in picture id was detected, clearing buffer.";
327 ClearFramesAndHistory();
328 last_continuous_picture_id = -1;
329 } else {
330 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
331 << key.picture_id << ":"
332 << static_cast<int>(key.spatial_layer)
333 << ") inserted after frame ("
334 << last_decoded_frame_it_->first.picture_id << ":"
335 << static_cast<int>(
336 last_decoded_frame_it_->first.spatial_layer)
337 << ") was handed off for decoding, dropping frame.";
338 return last_continuous_picture_id;
339 }
philipele0b2f152016-09-28 10:23:49 +0200340 }
341
philipel146a48b2017-04-20 04:04:38 -0700342 // Test if inserting this frame would cause the order of the frames to become
343 // ambiguous (covering more than half the interval of 2^16). This can happen
344 // when the picture id make large jumps mid stream.
345 if (!frames_.empty() &&
346 key < frames_.begin()->first &&
347 frames_.rbegin()->first < key) {
348 LOG(LS_WARNING) << "A jump in picture id was detected, clearing buffer.";
349 ClearFramesAndHistory();
350 last_continuous_picture_id = -1;
351 }
352
philipele0b2f152016-09-28 10:23:49 +0200353 auto info = frames_.insert(std::make_pair(key, FrameInfo())).first;
354
philipel93e451b2016-10-06 12:25:13 +0200355 if (info->second.frame) {
356 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id
357 << ":" << static_cast<int>(key.spatial_layer)
358 << ") already inserted, dropping frame.";
philipele0b2f152016-09-28 10:23:49 +0200359 return last_continuous_picture_id;
360 }
361
philipel93e451b2016-10-06 12:25:13 +0200362 if (!UpdateFrameInfoWithIncomingFrame(*frame, info))
363 return last_continuous_picture_id;
gnishb2a318b2017-05-10 09:21:33 -0700364 UpdatePlayoutDelays(*frame);
philipele0b2f152016-09-28 10:23:49 +0200365 info->second.frame = std::move(frame);
366 ++num_frames_buffered_;
367
368 if (info->second.num_missing_continuous == 0) {
369 info->second.continuous = true;
370 PropagateContinuity(info);
371 last_continuous_picture_id = last_continuous_frame_it_->first.picture_id;
372
373 // Since we now have new continuous frames there might be a better frame
374 // to return from NextFrame. Signal that thread so that it again can choose
375 // which frame to return.
tommi0a735642017-03-14 06:23:57 -0700376 new_continuous_frame_event_.Set();
philipele0b2f152016-09-28 10:23:49 +0200377 }
378
379 return last_continuous_picture_id;
philipelbe7a9e52016-05-19 12:19:35 +0200380}
381
philipele0b2f152016-09-28 10:23:49 +0200382void FrameBuffer::PropagateContinuity(FrameMap::iterator start) {
tommidb23ea62017-03-03 07:21:18 -0800383 TRACE_EVENT0("webrtc", "FrameBuffer::PropagateContinuity");
philipele0b2f152016-09-28 10:23:49 +0200384 RTC_DCHECK(start->second.continuous);
385 if (last_continuous_frame_it_ == frames_.end())
386 last_continuous_frame_it_ = start;
387
388 std::queue<FrameMap::iterator> continuous_frames;
389 continuous_frames.push(start);
390
391 // A simple BFS to traverse continuous frames.
392 while (!continuous_frames.empty()) {
393 auto frame = continuous_frames.front();
394 continuous_frames.pop();
395
396 if (last_continuous_frame_it_->first < frame->first)
397 last_continuous_frame_it_ = frame;
398
399 // Loop through all dependent frames, and if that frame no longer has
400 // any unfulfilled dependencies then that frame is continuous as well.
401 for (size_t d = 0; d < frame->second.num_dependent_frames; ++d) {
402 auto frame_ref = frames_.find(frame->second.dependent_frames[d]);
philipel112adf92017-06-15 09:06:21 -0700403 RTC_DCHECK(frame_ref != frames_.end());
philipele0b2f152016-09-28 10:23:49 +0200404
philipel112adf92017-06-15 09:06:21 -0700405 // TODO(philipel): Look into why we've seen this happen.
406 if (frame_ref != frames_.end()) {
407 --frame_ref->second.num_missing_continuous;
408 if (frame_ref->second.num_missing_continuous == 0) {
409 frame_ref->second.continuous = true;
410 continuous_frames.push(frame_ref);
411 }
philipele0b2f152016-09-28 10:23:49 +0200412 }
413 }
414 }
415}
416
417void FrameBuffer::PropagateDecodability(const FrameInfo& info) {
tommidb23ea62017-03-03 07:21:18 -0800418 TRACE_EVENT0("webrtc", "FrameBuffer::PropagateDecodability");
tommie95b78b2017-05-14 07:23:11 -0700419 RTC_CHECK(info.num_dependent_frames < FrameInfo::kMaxNumDependentFrames);
philipele0b2f152016-09-28 10:23:49 +0200420 for (size_t d = 0; d < info.num_dependent_frames; ++d) {
421 auto ref_info = frames_.find(info.dependent_frames[d]);
philipel93e451b2016-10-06 12:25:13 +0200422 RTC_DCHECK(ref_info != frames_.end());
tommie95b78b2017-05-14 07:23:11 -0700423 // TODO(philipel): Look into why we've seen this happen.
424 if (ref_info != frames_.end()) {
425 RTC_DCHECK_GT(ref_info->second.num_missing_decodable, 0U);
426 --ref_info->second.num_missing_decodable;
427 }
philipele0b2f152016-09-28 10:23:49 +0200428 }
429}
430
431void FrameBuffer::AdvanceLastDecodedFrame(FrameMap::iterator decoded) {
tommidb23ea62017-03-03 07:21:18 -0800432 TRACE_EVENT0("webrtc", "FrameBuffer::AdvanceLastDecodedFrame");
philipele0b2f152016-09-28 10:23:49 +0200433 if (last_decoded_frame_it_ == frames_.end()) {
434 last_decoded_frame_it_ = frames_.begin();
435 } else {
436 RTC_DCHECK(last_decoded_frame_it_->first < decoded->first);
437 ++last_decoded_frame_it_;
438 }
439 --num_frames_buffered_;
440 ++num_frames_history_;
441
442 // First, delete non-decoded frames from the history.
443 while (last_decoded_frame_it_ != decoded) {
444 if (last_decoded_frame_it_->second.frame)
445 --num_frames_buffered_;
446 last_decoded_frame_it_ = frames_.erase(last_decoded_frame_it_);
philipelbe7a9e52016-05-19 12:19:35 +0200447 }
448
philipele0b2f152016-09-28 10:23:49 +0200449 // Then remove old history if we have too much history saved.
450 if (num_frames_history_ > kMaxFramesHistory) {
451 frames_.erase(frames_.begin());
452 --num_frames_history_;
453 }
454}
455
456bool FrameBuffer::UpdateFrameInfoWithIncomingFrame(const FrameObject& frame,
457 FrameMap::iterator info) {
tommidb23ea62017-03-03 07:21:18 -0800458 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateFrameInfoWithIncomingFrame");
philipele0b2f152016-09-28 10:23:49 +0200459 FrameKey key(frame.picture_id, frame.spatial_layer);
460 info->second.num_missing_continuous = frame.num_references;
461 info->second.num_missing_decodable = frame.num_references;
462
463 RTC_DCHECK(last_decoded_frame_it_ == frames_.end() ||
464 last_decoded_frame_it_->first < info->first);
465
466 // Check how many dependencies that have already been fulfilled.
467 for (size_t i = 0; i < frame.num_references; ++i) {
468 FrameKey ref_key(frame.references[i], frame.spatial_layer);
469 auto ref_info = frames_.find(ref_key);
470
471 // Does |frame| depend on a frame earlier than the last decoded frame?
472 if (last_decoded_frame_it_ != frames_.end() &&
473 ref_key <= last_decoded_frame_it_->first) {
474 if (ref_info == frames_.end()) {
philipel65e1f942017-07-24 08:26:53 -0700475 int64_t now_ms = clock_->TimeInMilliseconds();
476 if (last_log_non_decoded_ms_ + kLogNonDecodedIntervalMs < now_ms) {
477 LOG(LS_WARNING)
478 << "Frame with (picture_id:spatial_id) (" << key.picture_id << ":"
479 << static_cast<int>(key.spatial_layer)
480 << ") depends on a non-decoded frame more previous than"
481 << " the last decoded frame, dropping frame.";
482 last_log_non_decoded_ms_ = now_ms;
483 }
philipele0b2f152016-09-28 10:23:49 +0200484 return false;
485 }
486
487 --info->second.num_missing_continuous;
488 --info->second.num_missing_decodable;
489 } else {
490 if (ref_info == frames_.end())
491 ref_info = frames_.insert(std::make_pair(ref_key, FrameInfo())).first;
492
493 if (ref_info->second.continuous)
494 --info->second.num_missing_continuous;
495
496 // Add backwards reference so |frame| can be updated when new
497 // frames are inserted or decoded.
498 ref_info->second.dependent_frames[ref_info->second.num_dependent_frames] =
499 key;
tommie95b78b2017-05-14 07:23:11 -0700500 RTC_DCHECK_LT(ref_info->second.num_dependent_frames,
501 (FrameInfo::kMaxNumDependentFrames - 1));
502 // TODO(philipel): Look into why this could happen and handle
503 // appropriately.
504 if (ref_info->second.num_dependent_frames <
505 (FrameInfo::kMaxNumDependentFrames - 1)) {
506 ++ref_info->second.num_dependent_frames;
507 }
philipele0b2f152016-09-28 10:23:49 +0200508 }
philipel93e451b2016-10-06 12:25:13 +0200509 RTC_DCHECK_LE(ref_info->second.num_missing_continuous,
510 ref_info->second.num_missing_decodable);
philipelbe7a9e52016-05-19 12:19:35 +0200511 }
512
philipele0b2f152016-09-28 10:23:49 +0200513 // Check if we have the lower spatial layer frame.
philipelbe7a9e52016-05-19 12:19:35 +0200514 if (frame.inter_layer_predicted) {
philipele0b2f152016-09-28 10:23:49 +0200515 ++info->second.num_missing_continuous;
516 ++info->second.num_missing_decodable;
517
philipelbe7a9e52016-05-19 12:19:35 +0200518 FrameKey ref_key(frame.picture_id, frame.spatial_layer - 1);
philipele0b2f152016-09-28 10:23:49 +0200519 // Gets or create the FrameInfo for the referenced frame.
520 auto ref_info = frames_.insert(std::make_pair(ref_key, FrameInfo())).first;
521 if (ref_info->second.continuous)
522 --info->second.num_missing_continuous;
523
524 if (ref_info == last_decoded_frame_it_) {
525 --info->second.num_missing_decodable;
526 } else {
527 ref_info->second.dependent_frames[ref_info->second.num_dependent_frames] =
528 key;
529 ++ref_info->second.num_dependent_frames;
530 }
philipel93e451b2016-10-06 12:25:13 +0200531 RTC_DCHECK_LE(ref_info->second.num_missing_continuous,
532 ref_info->second.num_missing_decodable);
philipelbe7a9e52016-05-19 12:19:35 +0200533 }
534
philipel93e451b2016-10-06 12:25:13 +0200535 RTC_DCHECK_LE(info->second.num_missing_continuous,
536 info->second.num_missing_decodable);
537
philipelbe7a9e52016-05-19 12:19:35 +0200538 return true;
539}
540
philipelbe742702016-11-30 01:31:40 -0800541void FrameBuffer::UpdateJitterDelay() {
tommidb23ea62017-03-03 07:21:18 -0800542 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateJitterDelay");
philipela45102f2017-02-22 05:30:39 -0800543 if (!stats_callback_)
544 return;
philipelbe742702016-11-30 01:31:40 -0800545
philipela45102f2017-02-22 05:30:39 -0800546 int decode_ms;
547 int max_decode_ms;
548 int current_delay_ms;
549 int target_delay_ms;
550 int jitter_buffer_ms;
551 int min_playout_delay_ms;
552 int render_delay_ms;
553 if (timing_->GetTimings(&decode_ms, &max_decode_ms, &current_delay_ms,
554 &target_delay_ms, &jitter_buffer_ms,
555 &min_playout_delay_ms, &render_delay_ms)) {
556 stats_callback_->OnFrameBufferTimingsUpdated(
557 decode_ms, max_decode_ms, current_delay_ms, target_delay_ms,
558 jitter_buffer_ms, min_playout_delay_ms, render_delay_ms);
philipelbe742702016-11-30 01:31:40 -0800559 }
philipel266f0a42016-11-28 08:49:07 -0800560}
561
ilnik2edc6842017-07-06 03:06:50 -0700562void FrameBuffer::UpdateTimingFrameInfo() {
563 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateTimingFrameInfo");
564 rtc::Optional<TimingFrameInfo> info = timing_->GetTimingFrameInfo();
565 if (info)
566 stats_callback_->OnTimingFrameInfoUpdated(*info);
567}
568
philipelfcc60062017-01-18 05:35:20 -0800569void FrameBuffer::ClearFramesAndHistory() {
ilnik2edc6842017-07-06 03:06:50 -0700570 TRACE_EVENT0("webrtc", "FrameBuffer::ClearFramesAndHistory");
philipelfcc60062017-01-18 05:35:20 -0800571 frames_.clear();
572 last_decoded_frame_it_ = frames_.end();
573 last_continuous_frame_it_ = frames_.end();
philipel1c056252017-01-31 09:53:12 -0800574 next_frame_it_ = frames_.end();
philipelfcc60062017-01-18 05:35:20 -0800575 num_frames_history_ = 0;
576 num_frames_buffered_ = 0;
577}
578
philipelbe7a9e52016-05-19 12:19:35 +0200579} // namespace video_coding
580} // namespace webrtc