blob: 2beffc7f082173cdab98ace2d9d406bc61a308eb [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
11#include "webrtc/modules/video_coding/frame_buffer2.h"
12
13#include <algorithm>
philipele0b2f152016-09-28 10:23:49 +020014#include <cstring>
15#include <queue>
philipelbe7a9e52016-05-19 12:19:35 +020016
Henrik Kjellanderdca1e092017-07-01 16:42:22 +020017#include "webrtc/base/checks.h"
18#include "webrtc/base/logging.h"
19#include "webrtc/base/trace_event.h"
philipela45102f2017-02-22 05:30:39 -080020#include "webrtc/modules/video_coding/include/video_coding_defines.h"
philipelbe7a9e52016-05-19 12:19:35 +020021#include "webrtc/modules/video_coding/jitter_estimator.h"
philipelbe7a9e52016-05-19 12:19:35 +020022#include "webrtc/modules/video_coding/timing.h"
23#include "webrtc/system_wrappers/include/clock.h"
philipel266f0a42016-11-28 08:49:07 -080024#include "webrtc/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;
philipelbe7a9e52016-05-19 12:19:35 +020035} // namespace
36
philipelbe7a9e52016-05-19 12:19:35 +020037FrameBuffer::FrameBuffer(Clock* clock,
38 VCMJitterEstimator* jitter_estimator,
philipela45102f2017-02-22 05:30:39 -080039 VCMTiming* timing,
40 VCMReceiveStatisticsCallback* stats_callback)
philipelbe7a9e52016-05-19 12:19:35 +020041 : clock_(clock),
tommi0a735642017-03-14 06:23:57 -070042 new_continuous_frame_event_(false, false),
philipelbe7a9e52016-05-19 12:19:35 +020043 jitter_estimator_(jitter_estimator),
44 timing_(timing),
philipel4f6cd6a2016-08-03 10:59:32 +020045 inter_frame_delay_(clock_->TimeInMilliseconds()),
gnishb2a318b2017-05-10 09:21:33 -070046 last_decoded_frame_timestamp_(0),
philipele0b2f152016-09-28 10:23:49 +020047 last_decoded_frame_it_(frames_.end()),
48 last_continuous_frame_it_(frames_.end()),
49 num_frames_history_(0),
50 num_frames_buffered_(0),
philipel29f730e2017-03-15 08:10:08 -070051 stopped_(false),
philipela45102f2017-02-22 05:30:39 -080052 protection_mode_(kProtectionNack),
53 stats_callback_(stats_callback) {}
philipel266f0a42016-11-28 08:49:07 -080054
philipela45102f2017-02-22 05:30:39 -080055FrameBuffer::~FrameBuffer() {}
philipelbe7a9e52016-05-19 12:19:35 +020056
philipel75562822016-09-05 10:57:41 +020057FrameBuffer::ReturnReason FrameBuffer::NextFrame(
58 int64_t max_wait_time_ms,
59 std::unique_ptr<FrameObject>* frame_out) {
tommidb23ea62017-03-03 07:21:18 -080060 TRACE_EVENT0("webrtc", "FrameBuffer::NextFrame");
philipel1c056252017-01-31 09:53:12 -080061 int64_t latest_return_time_ms =
62 clock_->TimeInMilliseconds() + max_wait_time_ms;
philipel504c47d2016-06-30 17:33:02 +020063 int64_t wait_ms = max_wait_time_ms;
philipel29f730e2017-03-15 08:10:08 -070064 int64_t now_ms = 0;
philipele0b2f152016-09-28 10:23:49 +020065
66 do {
philipel29f730e2017-03-15 08:10:08 -070067 now_ms = clock_->TimeInMilliseconds();
philipel504c47d2016-06-30 17:33:02 +020068 {
69 rtc::CritScope lock(&crit_);
tommi0a735642017-03-14 06:23:57 -070070 new_continuous_frame_event_.Reset();
philipel29f730e2017-03-15 08:10:08 -070071 if (stopped_)
72 return kStopped;
73
74 wait_ms = max_wait_time_ms;
75
philipele0b2f152016-09-28 10:23:49 +020076 // Need to hold |crit_| in order to use |frames_|, therefore we
77 // set it here in the loop instead of outside the loop in order to not
78 // acquire the lock unnecesserily.
philipel1c056252017-01-31 09:53:12 -080079 next_frame_it_ = frames_.end();
philipelbe7a9e52016-05-19 12:19:35 +020080
philipele0b2f152016-09-28 10:23:49 +020081 // |frame_it| points to the first frame after the
82 // |last_decoded_frame_it_|.
83 auto frame_it = frames_.end();
84 if (last_decoded_frame_it_ == frames_.end()) {
85 frame_it = frames_.begin();
philipelbe7a9e52016-05-19 12:19:35 +020086 } else {
philipele0b2f152016-09-28 10:23:49 +020087 frame_it = last_decoded_frame_it_;
88 ++frame_it;
philipelbe7a9e52016-05-19 12:19:35 +020089 }
philipele0b2f152016-09-28 10:23:49 +020090
91 // |continuous_end_it| points to the first frame after the
92 // |last_continuous_frame_it_|.
93 auto continuous_end_it = last_continuous_frame_it_;
94 if (continuous_end_it != frames_.end())
95 ++continuous_end_it;
96
philipel146a48b2017-04-20 04:04:38 -070097 for (; frame_it != continuous_end_it && frame_it != frames_.end();
98 ++frame_it) {
philipel93e451b2016-10-06 12:25:13 +020099 if (!frame_it->second.continuous ||
100 frame_it->second.num_missing_decodable > 0) {
philipele0b2f152016-09-28 10:23:49 +0200101 continue;
philipel93e451b2016-10-06 12:25:13 +0200102 }
philipele0b2f152016-09-28 10:23:49 +0200103
104 FrameObject* frame = frame_it->second.frame.get();
philipel1c056252017-01-31 09:53:12 -0800105 next_frame_it_ = frame_it;
philipele0b2f152016-09-28 10:23:49 +0200106 if (frame->RenderTime() == -1)
107 frame->SetRenderTime(timing_->RenderTimeMs(frame->timestamp, now_ms));
108 wait_ms = timing_->MaxWaitingTime(frame->RenderTime(), now_ms);
109
110 // This will cause the frame buffer to prefer high framerate rather
111 // than high resolution in the case of the decoder not decoding fast
112 // enough and the stream has multiple spatial and temporal layers.
113 if (wait_ms == 0)
114 continue;
115
116 break;
117 }
118 } // rtc::Critscope lock(&crit_);
119
philipel1c056252017-01-31 09:53:12 -0800120 wait_ms = std::min<int64_t>(wait_ms, latest_return_time_ms - now_ms);
philipele0b2f152016-09-28 10:23:49 +0200121 wait_ms = std::max<int64_t>(wait_ms, 0);
tommi0a735642017-03-14 06:23:57 -0700122 } while (new_continuous_frame_event_.Wait(wait_ms));
philipele0b2f152016-09-28 10:23:49 +0200123
philipel29f730e2017-03-15 08:10:08 -0700124 {
125 rtc::CritScope lock(&crit_);
126 now_ms = clock_->TimeInMilliseconds();
127 if (next_frame_it_ != frames_.end()) {
128 std::unique_ptr<FrameObject> frame =
129 std::move(next_frame_it_->second.frame);
philipele0b2f152016-09-28 10:23:49 +0200130
philipel29f730e2017-03-15 08:10:08 -0700131 if (!frame->delayed_by_retransmission()) {
132 int64_t frame_delay;
philipele0754302017-01-25 08:56:23 -0800133
philipel29f730e2017-03-15 08:10:08 -0700134 if (inter_frame_delay_.CalculateDelay(frame->timestamp, &frame_delay,
135 frame->ReceivedTime())) {
136 jitter_estimator_->UpdateEstimate(frame_delay, frame->size());
137 }
138
139 float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0;
140 timing_->SetJitterDelay(jitter_estimator_->GetJitterEstimate(rtt_mult));
141 timing_->UpdateCurrentDelay(frame->RenderTime(), now_ms);
philipele0754302017-01-25 08:56:23 -0800142 }
143
stefan95e97542017-05-23 09:52:18 -0700144 // Gracefully handle bad RTP timestamps and render time issues.
145 if (HasBadRenderTiming(*frame, now_ms)) {
146 jitter_estimator_->Reset();
147 timing_->Reset();
148 frame->SetRenderTime(timing_->RenderTimeMs(frame->timestamp, now_ms));
149 }
150
philipel29f730e2017-03-15 08:10:08 -0700151 UpdateJitterDelay();
ilnik2edc6842017-07-06 03:06:50 -0700152 UpdateTimingFrameInfo();
philipel29f730e2017-03-15 08:10:08 -0700153 PropagateDecodability(next_frame_it_->second);
brandtr9078d8c2017-04-27 07:07:27 -0700154
155 // Sanity check for RTP timestamp monotonicity.
156 if (last_decoded_frame_it_ != frames_.end()) {
157 const FrameKey& last_decoded_frame_key = last_decoded_frame_it_->first;
158 const FrameKey& frame_key = next_frame_it_->first;
159
160 const bool frame_is_higher_spatial_layer_of_last_decoded_frame =
161 last_decoded_frame_timestamp_ == frame->timestamp &&
162 last_decoded_frame_key.picture_id == frame_key.picture_id &&
163 last_decoded_frame_key.spatial_layer < frame_key.spatial_layer;
164
165 if (AheadOrAt(last_decoded_frame_timestamp_, frame->timestamp) &&
166 !frame_is_higher_spatial_layer_of_last_decoded_frame) {
167 // TODO(brandtr): Consider clearing the entire buffer when we hit
168 // these conditions.
169 LOG(LS_WARNING) << "Frame with (timestamp:picture_id:spatial_id) ("
170 << frame->timestamp << ":" << frame->picture_id << ":"
171 << static_cast<int>(frame->spatial_layer) << ")"
172 << " sent to decoder after frame with"
173 << " (timestamp:picture_id:spatial_id) ("
174 << last_decoded_frame_timestamp_ << ":"
175 << last_decoded_frame_key.picture_id << ":"
176 << static_cast<int>(
177 last_decoded_frame_key.spatial_layer)
178 << ").";
179 }
180 }
181
philipel29f730e2017-03-15 08:10:08 -0700182 AdvanceLastDecodedFrame(next_frame_it_);
183 last_decoded_frame_timestamp_ = frame->timestamp;
184 *frame_out = std::move(frame);
185 return kFrameFound;
philipelbe7a9e52016-05-19 12:19:35 +0200186 }
tommi0a735642017-03-14 06:23:57 -0700187 }
188
189 if (latest_return_time_ms - now_ms > 0) {
philipel1c056252017-01-31 09:53:12 -0800190 // If |next_frame_it_ == frames_.end()| and there is still time left, it
191 // means that the frame buffer was cleared as the thread in this function
192 // was waiting to acquire |crit_| in order to return. Wait for the
193 // remaining time and then return.
194 return NextFrame(latest_return_time_ms - now_ms, frame_out);
philipelbe7a9e52016-05-19 12:19:35 +0200195 }
tommi0a735642017-03-14 06:23:57 -0700196
197 return kTimeout;
philipelbe7a9e52016-05-19 12:19:35 +0200198}
199
stefan95e97542017-05-23 09:52:18 -0700200bool FrameBuffer::HasBadRenderTiming(const FrameObject& frame, int64_t now_ms) {
201 // Assume that render timing errors are due to changes in the video stream.
202 int64_t render_time_ms = frame.RenderTimeMs();
203 const int64_t kMaxVideoDelayMs = 10000;
204 if (render_time_ms < 0) {
205 return true;
206 }
207 if (std::abs(render_time_ms - now_ms) > kMaxVideoDelayMs) {
208 int frame_delay = static_cast<int>(std::abs(render_time_ms - now_ms));
209 LOG(LS_WARNING) << "A frame about to be decoded is out of the configured "
210 << "delay bounds (" << frame_delay << " > "
211 << kMaxVideoDelayMs
212 << "). Resetting the video jitter buffer.";
213 return true;
214 }
215 if (static_cast<int>(timing_->TargetVideoDelay()) > kMaxVideoDelayMs) {
216 LOG(LS_WARNING) << "The video target delay has grown larger than "
217 << kMaxVideoDelayMs << " ms.";
218 return true;
219 }
220 return false;
221}
222
philipel4f6cd6a2016-08-03 10:59:32 +0200223void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) {
tommidb23ea62017-03-03 07:21:18 -0800224 TRACE_EVENT0("webrtc", "FrameBuffer::SetProtectionMode");
philipel4f6cd6a2016-08-03 10:59:32 +0200225 rtc::CritScope lock(&crit_);
226 protection_mode_ = mode;
227}
228
philipel504c47d2016-06-30 17:33:02 +0200229void FrameBuffer::Start() {
tommidb23ea62017-03-03 07:21:18 -0800230 TRACE_EVENT0("webrtc", "FrameBuffer::Start");
philipel29f730e2017-03-15 08:10:08 -0700231 rtc::CritScope lock(&crit_);
232 stopped_ = false;
philipel504c47d2016-06-30 17:33:02 +0200233}
234
235void FrameBuffer::Stop() {
tommidb23ea62017-03-03 07:21:18 -0800236 TRACE_EVENT0("webrtc", "FrameBuffer::Stop");
philipel29f730e2017-03-15 08:10:08 -0700237 rtc::CritScope lock(&crit_);
238 stopped_ = true;
tommi0a735642017-03-14 06:23:57 -0700239 new_continuous_frame_event_.Set();
philipel504c47d2016-06-30 17:33:02 +0200240}
241
philipel112adf92017-06-15 09:06:21 -0700242bool FrameBuffer::ValidReferences(const FrameObject& frame) const {
243 for (size_t i = 0; i < frame.num_references; ++i) {
244 if (AheadOrAt(frame.references[i], frame.picture_id))
245 return false;
246 for (size_t j = i + 1; j < frame.num_references; ++j) {
247 if (frame.references[i] == frame.references[j])
248 return false;
249 }
250 }
251
252 if (frame.inter_layer_predicted && frame.spatial_layer == 0)
253 return false;
254
255 return true;
256}
257
gnishb2a318b2017-05-10 09:21:33 -0700258void FrameBuffer::UpdatePlayoutDelays(const FrameObject& frame) {
259 TRACE_EVENT0("webrtc", "FrameBuffer::UpdatePlayoutDelays");
260 PlayoutDelay playout_delay = frame.EncodedImage().playout_delay_;
261 if (playout_delay.min_ms >= 0)
262 timing_->set_min_playout_delay(playout_delay.min_ms);
263
264 if (playout_delay.max_ms >= 0)
265 timing_->set_max_playout_delay(playout_delay.max_ms);
266}
267
philipele0b2f152016-09-28 10:23:49 +0200268int FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) {
tommidb23ea62017-03-03 07:21:18 -0800269 TRACE_EVENT0("webrtc", "FrameBuffer::InsertFrame");
philipel93e451b2016-10-06 12:25:13 +0200270 RTC_DCHECK(frame);
philipela45102f2017-02-22 05:30:39 -0800271 if (stats_callback_)
272 stats_callback_->OnCompleteFrame(frame->num_references == 0, frame->size());
philipelbe7a9e52016-05-19 12:19:35 +0200273 FrameKey key(frame->picture_id, frame->spatial_layer);
tommi0a735642017-03-14 06:23:57 -0700274
275 rtc::CritScope lock(&crit_);
philipel29f730e2017-03-15 08:10:08 -0700276
philipele0b2f152016-09-28 10:23:49 +0200277 int last_continuous_picture_id =
278 last_continuous_frame_it_ == frames_.end()
279 ? -1
280 : last_continuous_frame_it_->first.picture_id;
281
philipel112adf92017-06-15 09:06:21 -0700282 if (!ValidReferences(*frame)) {
283 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id
284 << ":" << static_cast<int>(key.spatial_layer)
285 << ") has invalid frame references, dropping frame.";
286 return last_continuous_picture_id;
287 }
288
philipele0b2f152016-09-28 10:23:49 +0200289 if (num_frames_buffered_ >= kMaxFramesBuffered) {
290 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id
291 << ":" << static_cast<int>(key.spatial_layer)
292 << ") could not be inserted due to the frame "
293 << "buffer being full, dropping frame.";
294 return last_continuous_picture_id;
295 }
296
philipele0b2f152016-09-28 10:23:49 +0200297 if (last_decoded_frame_it_ != frames_.end() &&
philipelf6842692017-04-28 03:29:15 -0700298 key <= last_decoded_frame_it_->first) {
philipelfcc60062017-01-18 05:35:20 -0800299 if (AheadOf(frame->timestamp, last_decoded_frame_timestamp_) &&
300 frame->num_references == 0) {
301 // If this frame has a newer timestamp but an earlier picture id then we
302 // assume there has been a jump in the picture id due to some encoder
303 // reconfiguration or some other reason. Even though this is not according
304 // to spec we can still continue to decode from this frame if it is a
305 // keyframe.
306 LOG(LS_WARNING) << "A jump in picture id was detected, clearing buffer.";
307 ClearFramesAndHistory();
308 last_continuous_picture_id = -1;
309 } else {
310 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
311 << key.picture_id << ":"
312 << static_cast<int>(key.spatial_layer)
313 << ") inserted after frame ("
314 << last_decoded_frame_it_->first.picture_id << ":"
315 << static_cast<int>(
316 last_decoded_frame_it_->first.spatial_layer)
317 << ") was handed off for decoding, dropping frame.";
318 return last_continuous_picture_id;
319 }
philipele0b2f152016-09-28 10:23:49 +0200320 }
321
philipel146a48b2017-04-20 04:04:38 -0700322 // Test if inserting this frame would cause the order of the frames to become
323 // ambiguous (covering more than half the interval of 2^16). This can happen
324 // when the picture id make large jumps mid stream.
325 if (!frames_.empty() &&
326 key < frames_.begin()->first &&
327 frames_.rbegin()->first < key) {
328 LOG(LS_WARNING) << "A jump in picture id was detected, clearing buffer.";
329 ClearFramesAndHistory();
330 last_continuous_picture_id = -1;
331 }
332
philipele0b2f152016-09-28 10:23:49 +0200333 auto info = frames_.insert(std::make_pair(key, FrameInfo())).first;
334
philipel93e451b2016-10-06 12:25:13 +0200335 if (info->second.frame) {
336 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id
337 << ":" << static_cast<int>(key.spatial_layer)
338 << ") already inserted, dropping frame.";
philipele0b2f152016-09-28 10:23:49 +0200339 return last_continuous_picture_id;
340 }
341
philipel93e451b2016-10-06 12:25:13 +0200342 if (!UpdateFrameInfoWithIncomingFrame(*frame, info))
343 return last_continuous_picture_id;
gnishb2a318b2017-05-10 09:21:33 -0700344 UpdatePlayoutDelays(*frame);
philipele0b2f152016-09-28 10:23:49 +0200345 info->second.frame = std::move(frame);
346 ++num_frames_buffered_;
347
348 if (info->second.num_missing_continuous == 0) {
349 info->second.continuous = true;
350 PropagateContinuity(info);
351 last_continuous_picture_id = last_continuous_frame_it_->first.picture_id;
352
353 // Since we now have new continuous frames there might be a better frame
354 // to return from NextFrame. Signal that thread so that it again can choose
355 // which frame to return.
tommi0a735642017-03-14 06:23:57 -0700356 new_continuous_frame_event_.Set();
philipele0b2f152016-09-28 10:23:49 +0200357 }
358
359 return last_continuous_picture_id;
philipelbe7a9e52016-05-19 12:19:35 +0200360}
361
philipele0b2f152016-09-28 10:23:49 +0200362void FrameBuffer::PropagateContinuity(FrameMap::iterator start) {
tommidb23ea62017-03-03 07:21:18 -0800363 TRACE_EVENT0("webrtc", "FrameBuffer::PropagateContinuity");
philipele0b2f152016-09-28 10:23:49 +0200364 RTC_DCHECK(start->second.continuous);
365 if (last_continuous_frame_it_ == frames_.end())
366 last_continuous_frame_it_ = start;
367
368 std::queue<FrameMap::iterator> continuous_frames;
369 continuous_frames.push(start);
370
371 // A simple BFS to traverse continuous frames.
372 while (!continuous_frames.empty()) {
373 auto frame = continuous_frames.front();
374 continuous_frames.pop();
375
376 if (last_continuous_frame_it_->first < frame->first)
377 last_continuous_frame_it_ = frame;
378
379 // Loop through all dependent frames, and if that frame no longer has
380 // any unfulfilled dependencies then that frame is continuous as well.
381 for (size_t d = 0; d < frame->second.num_dependent_frames; ++d) {
382 auto frame_ref = frames_.find(frame->second.dependent_frames[d]);
philipel112adf92017-06-15 09:06:21 -0700383 RTC_DCHECK(frame_ref != frames_.end());
philipele0b2f152016-09-28 10:23:49 +0200384
philipel112adf92017-06-15 09:06:21 -0700385 // TODO(philipel): Look into why we've seen this happen.
386 if (frame_ref != frames_.end()) {
387 --frame_ref->second.num_missing_continuous;
388 if (frame_ref->second.num_missing_continuous == 0) {
389 frame_ref->second.continuous = true;
390 continuous_frames.push(frame_ref);
391 }
philipele0b2f152016-09-28 10:23:49 +0200392 }
393 }
394 }
395}
396
397void FrameBuffer::PropagateDecodability(const FrameInfo& info) {
tommidb23ea62017-03-03 07:21:18 -0800398 TRACE_EVENT0("webrtc", "FrameBuffer::PropagateDecodability");
tommie95b78b2017-05-14 07:23:11 -0700399 RTC_CHECK(info.num_dependent_frames < FrameInfo::kMaxNumDependentFrames);
philipele0b2f152016-09-28 10:23:49 +0200400 for (size_t d = 0; d < info.num_dependent_frames; ++d) {
401 auto ref_info = frames_.find(info.dependent_frames[d]);
philipel93e451b2016-10-06 12:25:13 +0200402 RTC_DCHECK(ref_info != frames_.end());
tommie95b78b2017-05-14 07:23:11 -0700403 // TODO(philipel): Look into why we've seen this happen.
404 if (ref_info != frames_.end()) {
405 RTC_DCHECK_GT(ref_info->second.num_missing_decodable, 0U);
406 --ref_info->second.num_missing_decodable;
407 }
philipele0b2f152016-09-28 10:23:49 +0200408 }
409}
410
411void FrameBuffer::AdvanceLastDecodedFrame(FrameMap::iterator decoded) {
tommidb23ea62017-03-03 07:21:18 -0800412 TRACE_EVENT0("webrtc", "FrameBuffer::AdvanceLastDecodedFrame");
philipele0b2f152016-09-28 10:23:49 +0200413 if (last_decoded_frame_it_ == frames_.end()) {
414 last_decoded_frame_it_ = frames_.begin();
415 } else {
416 RTC_DCHECK(last_decoded_frame_it_->first < decoded->first);
417 ++last_decoded_frame_it_;
418 }
419 --num_frames_buffered_;
420 ++num_frames_history_;
421
422 // First, delete non-decoded frames from the history.
423 while (last_decoded_frame_it_ != decoded) {
424 if (last_decoded_frame_it_->second.frame)
425 --num_frames_buffered_;
426 last_decoded_frame_it_ = frames_.erase(last_decoded_frame_it_);
philipelbe7a9e52016-05-19 12:19:35 +0200427 }
428
philipele0b2f152016-09-28 10:23:49 +0200429 // Then remove old history if we have too much history saved.
430 if (num_frames_history_ > kMaxFramesHistory) {
431 frames_.erase(frames_.begin());
432 --num_frames_history_;
433 }
434}
435
436bool FrameBuffer::UpdateFrameInfoWithIncomingFrame(const FrameObject& frame,
437 FrameMap::iterator info) {
tommidb23ea62017-03-03 07:21:18 -0800438 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateFrameInfoWithIncomingFrame");
philipele0b2f152016-09-28 10:23:49 +0200439 FrameKey key(frame.picture_id, frame.spatial_layer);
440 info->second.num_missing_continuous = frame.num_references;
441 info->second.num_missing_decodable = frame.num_references;
442
443 RTC_DCHECK(last_decoded_frame_it_ == frames_.end() ||
444 last_decoded_frame_it_->first < info->first);
445
446 // Check how many dependencies that have already been fulfilled.
447 for (size_t i = 0; i < frame.num_references; ++i) {
448 FrameKey ref_key(frame.references[i], frame.spatial_layer);
449 auto ref_info = frames_.find(ref_key);
450
451 // Does |frame| depend on a frame earlier than the last decoded frame?
452 if (last_decoded_frame_it_ != frames_.end() &&
453 ref_key <= last_decoded_frame_it_->first) {
454 if (ref_info == frames_.end()) {
455 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) ("
456 << key.picture_id << ":"
457 << static_cast<int>(key.spatial_layer)
458 << " depends on a non-decoded frame more previous than "
459 << "the last decoded frame, dropping frame.";
460 return false;
461 }
462
463 --info->second.num_missing_continuous;
464 --info->second.num_missing_decodable;
465 } else {
466 if (ref_info == frames_.end())
467 ref_info = frames_.insert(std::make_pair(ref_key, FrameInfo())).first;
468
469 if (ref_info->second.continuous)
470 --info->second.num_missing_continuous;
471
472 // Add backwards reference so |frame| can be updated when new
473 // frames are inserted or decoded.
474 ref_info->second.dependent_frames[ref_info->second.num_dependent_frames] =
475 key;
tommie95b78b2017-05-14 07:23:11 -0700476 RTC_DCHECK_LT(ref_info->second.num_dependent_frames,
477 (FrameInfo::kMaxNumDependentFrames - 1));
478 // TODO(philipel): Look into why this could happen and handle
479 // appropriately.
480 if (ref_info->second.num_dependent_frames <
481 (FrameInfo::kMaxNumDependentFrames - 1)) {
482 ++ref_info->second.num_dependent_frames;
483 }
philipele0b2f152016-09-28 10:23:49 +0200484 }
philipel93e451b2016-10-06 12:25:13 +0200485 RTC_DCHECK_LE(ref_info->second.num_missing_continuous,
486 ref_info->second.num_missing_decodable);
philipelbe7a9e52016-05-19 12:19:35 +0200487 }
488
philipele0b2f152016-09-28 10:23:49 +0200489 // Check if we have the lower spatial layer frame.
philipelbe7a9e52016-05-19 12:19:35 +0200490 if (frame.inter_layer_predicted) {
philipele0b2f152016-09-28 10:23:49 +0200491 ++info->second.num_missing_continuous;
492 ++info->second.num_missing_decodable;
493
philipelbe7a9e52016-05-19 12:19:35 +0200494 FrameKey ref_key(frame.picture_id, frame.spatial_layer - 1);
philipele0b2f152016-09-28 10:23:49 +0200495 // Gets or create the FrameInfo for the referenced frame.
496 auto ref_info = frames_.insert(std::make_pair(ref_key, FrameInfo())).first;
497 if (ref_info->second.continuous)
498 --info->second.num_missing_continuous;
499
500 if (ref_info == last_decoded_frame_it_) {
501 --info->second.num_missing_decodable;
502 } else {
503 ref_info->second.dependent_frames[ref_info->second.num_dependent_frames] =
504 key;
505 ++ref_info->second.num_dependent_frames;
506 }
philipel93e451b2016-10-06 12:25:13 +0200507 RTC_DCHECK_LE(ref_info->second.num_missing_continuous,
508 ref_info->second.num_missing_decodable);
philipelbe7a9e52016-05-19 12:19:35 +0200509 }
510
philipel93e451b2016-10-06 12:25:13 +0200511 RTC_DCHECK_LE(info->second.num_missing_continuous,
512 info->second.num_missing_decodable);
513
philipelbe7a9e52016-05-19 12:19:35 +0200514 return true;
515}
516
philipelbe742702016-11-30 01:31:40 -0800517void FrameBuffer::UpdateJitterDelay() {
tommidb23ea62017-03-03 07:21:18 -0800518 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateJitterDelay");
philipela45102f2017-02-22 05:30:39 -0800519 if (!stats_callback_)
520 return;
philipelbe742702016-11-30 01:31:40 -0800521
philipela45102f2017-02-22 05:30:39 -0800522 int decode_ms;
523 int max_decode_ms;
524 int current_delay_ms;
525 int target_delay_ms;
526 int jitter_buffer_ms;
527 int min_playout_delay_ms;
528 int render_delay_ms;
529 if (timing_->GetTimings(&decode_ms, &max_decode_ms, &current_delay_ms,
530 &target_delay_ms, &jitter_buffer_ms,
531 &min_playout_delay_ms, &render_delay_ms)) {
532 stats_callback_->OnFrameBufferTimingsUpdated(
533 decode_ms, max_decode_ms, current_delay_ms, target_delay_ms,
534 jitter_buffer_ms, min_playout_delay_ms, render_delay_ms);
philipelbe742702016-11-30 01:31:40 -0800535 }
philipel266f0a42016-11-28 08:49:07 -0800536}
537
ilnik2edc6842017-07-06 03:06:50 -0700538void FrameBuffer::UpdateTimingFrameInfo() {
539 TRACE_EVENT0("webrtc", "FrameBuffer::UpdateTimingFrameInfo");
540 rtc::Optional<TimingFrameInfo> info = timing_->GetTimingFrameInfo();
541 if (info)
542 stats_callback_->OnTimingFrameInfoUpdated(*info);
543}
544
philipelfcc60062017-01-18 05:35:20 -0800545void FrameBuffer::ClearFramesAndHistory() {
ilnik2edc6842017-07-06 03:06:50 -0700546 TRACE_EVENT0("webrtc", "FrameBuffer::ClearFramesAndHistory");
philipelfcc60062017-01-18 05:35:20 -0800547 frames_.clear();
548 last_decoded_frame_it_ = frames_.end();
549 last_continuous_frame_it_ = frames_.end();
philipel1c056252017-01-31 09:53:12 -0800550 next_frame_it_ = frames_.end();
philipelfcc60062017-01-18 05:35:20 -0800551 num_frames_history_ = 0;
552 num_frames_buffered_ = 0;
553}
554
philipelbe7a9e52016-05-19 12:19:35 +0200555} // namespace video_coding
556} // namespace webrtc