philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/video_coding/frame_buffer2.h" |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 14 | #include <cstring> |
| 15 | #include <queue> |
philipel | 798b282 | 2018-06-11 13:10:14 +0200 | [diff] [blame] | 16 | #include <vector> |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "modules/video_coding/include/video_coding_defines.h" |
| 19 | #include "modules/video_coding/jitter_estimator.h" |
| 20 | #include "modules/video_coding/timing.h" |
| 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/logging.h" |
| 23 | #include "rtc_base/trace_event.h" |
| 24 | #include "system_wrappers/include/clock.h" |
philipel | 707f278 | 2017-10-02 14:10:28 +0200 | [diff] [blame] | 25 | #include "system_wrappers/include/field_trial.h" |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | namespace video_coding { |
| 29 | |
| 30 | namespace { |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 31 | // Max number of frames the buffer will hold. |
| 32 | constexpr int kMaxFramesBuffered = 600; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 33 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 34 | // Max number of decoded frame info that will be saved. |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 35 | constexpr int kMaxFramesHistory = 50; |
philipel | 65e1f94 | 2017-07-24 08:26:53 -0700 | [diff] [blame] | 36 | |
Ilya Nikolaevskiy | 8c4fe16 | 2018-02-27 15:49:47 +0100 | [diff] [blame] | 37 | // The time it's allowed for a frame to be late to its rendering prediction and |
| 38 | // still be rendered. |
Ilya Nikolaevskiy | 7eef007 | 2018-02-28 09:59:26 +0100 | [diff] [blame] | 39 | constexpr int kMaxAllowedFrameDelayMs = 5; |
Ilya Nikolaevskiy | 8c4fe16 | 2018-02-27 15:49:47 +0100 | [diff] [blame] | 40 | |
philipel | 65e1f94 | 2017-07-24 08:26:53 -0700 | [diff] [blame] | 41 | constexpr int64_t kLogNonDecodedIntervalMs = 5000; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 42 | } // namespace |
| 43 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 44 | FrameBuffer::FrameBuffer(Clock* clock, |
| 45 | VCMJitterEstimator* jitter_estimator, |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 46 | VCMTiming* timing, |
| 47 | VCMReceiveStatisticsCallback* stats_callback) |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 48 | : clock_(clock), |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 49 | new_continuous_frame_event_(false, false), |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 50 | jitter_estimator_(jitter_estimator), |
| 51 | timing_(timing), |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 52 | inter_frame_delay_(clock_->TimeInMilliseconds()), |
gnish | b2a318b | 2017-05-10 09:21:33 -0700 | [diff] [blame] | 53 | last_decoded_frame_timestamp_(0), |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 54 | last_decoded_frame_it_(frames_.end()), |
| 55 | last_continuous_frame_it_(frames_.end()), |
| 56 | num_frames_history_(0), |
| 57 | num_frames_buffered_(0), |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 58 | stopped_(false), |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 59 | protection_mode_(kProtectionNack), |
philipel | 65e1f94 | 2017-07-24 08:26:53 -0700 | [diff] [blame] | 60 | stats_callback_(stats_callback), |
| 61 | last_log_non_decoded_ms_(-kLogNonDecodedIntervalMs) {} |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 62 | |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 63 | FrameBuffer::~FrameBuffer() {} |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 64 | |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame] | 65 | FrameBuffer::ReturnReason FrameBuffer::NextFrame( |
| 66 | int64_t max_wait_time_ms, |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 67 | std::unique_ptr<EncodedFrame>* frame_out, |
philipel | 3042c2d | 2017-08-18 04:55:02 -0700 | [diff] [blame] | 68 | bool keyframe_required) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 69 | TRACE_EVENT0("webrtc", "FrameBuffer::NextFrame"); |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 70 | int64_t latest_return_time_ms = |
| 71 | clock_->TimeInMilliseconds() + max_wait_time_ms; |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 72 | int64_t wait_ms = max_wait_time_ms; |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 73 | int64_t now_ms = 0; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 74 | |
| 75 | do { |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 76 | now_ms = clock_->TimeInMilliseconds(); |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 77 | { |
| 78 | rtc::CritScope lock(&crit_); |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 79 | new_continuous_frame_event_.Reset(); |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 80 | if (stopped_) |
| 81 | return kStopped; |
| 82 | |
| 83 | wait_ms = max_wait_time_ms; |
| 84 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 85 | // 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. |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 88 | next_frame_it_ = frames_.end(); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 89 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 90 | // |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(); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 95 | } else { |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 96 | frame_it = last_decoded_frame_it_; |
| 97 | ++frame_it; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 98 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 99 | |
| 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 | |
philipel | 146a48b | 2017-04-20 04:04:38 -0700 | [diff] [blame] | 106 | for (; frame_it != continuous_end_it && frame_it != frames_.end(); |
| 107 | ++frame_it) { |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 108 | if (!frame_it->second.continuous || |
| 109 | frame_it->second.num_missing_decodable > 0) { |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 110 | continue; |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 111 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 112 | |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 113 | EncodedFrame* frame = frame_it->second.frame.get(); |
philipel | 3042c2d | 2017-08-18 04:55:02 -0700 | [diff] [blame] | 114 | |
| 115 | if (keyframe_required && !frame->is_keyframe()) |
| 116 | continue; |
| 117 | |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 118 | next_frame_it_ = frame_it; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 119 | 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 Nikolaevskiy | 8c4fe16 | 2018-02-27 15:49:47 +0100 | [diff] [blame] | 126 | // For multiple temporal layers it may cause non-base layer frames to be |
| 127 | // skipped if they are late. |
Ilya Nikolaevskiy | 7eef007 | 2018-02-28 09:59:26 +0100 | [diff] [blame] | 128 | if (wait_ms < -kMaxAllowedFrameDelayMs) |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 129 | continue; |
| 130 | |
| 131 | break; |
| 132 | } |
| 133 | } // rtc::Critscope lock(&crit_); |
| 134 | |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 135 | wait_ms = std::min<int64_t>(wait_ms, latest_return_time_ms - now_ms); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 136 | wait_ms = std::max<int64_t>(wait_ms, 0); |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 137 | } while (new_continuous_frame_event_.Wait(wait_ms)); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 138 | |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 139 | { |
| 140 | rtc::CritScope lock(&crit_); |
| 141 | now_ms = clock_->TimeInMilliseconds(); |
| 142 | if (next_frame_it_ != frames_.end()) { |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 143 | std::unique_ptr<EncodedFrame> frame = |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 144 | std::move(next_frame_it_->second.frame); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 145 | |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 146 | if (!frame->delayed_by_retransmission()) { |
| 147 | int64_t frame_delay; |
philipel | e075430 | 2017-01-25 08:56:23 -0800 | [diff] [blame] | 148 | |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 149 | 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); |
philipel | e21be1d | 2017-09-25 06:37:12 -0700 | [diff] [blame] | 157 | } else { |
philipel | 707f278 | 2017-10-02 14:10:28 +0200 | [diff] [blame] | 158 | if (webrtc::field_trial::IsEnabled("WebRTC-AddRttToPlayoutDelay")) |
| 159 | jitter_estimator_->FrameNacked(); |
philipel | e075430 | 2017-01-25 08:56:23 -0800 | [diff] [blame] | 160 | } |
| 161 | |
stefan | 95e9754 | 2017-05-23 09:52:18 -0700 | [diff] [blame] | 162 | // 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 | |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 169 | UpdateJitterDelay(); |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 170 | UpdateTimingFrameInfo(); |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 171 | PropagateDecodability(next_frame_it_->second); |
brandtr | 9078d8c | 2017-04-27 07:07:27 -0700 | [diff] [blame] | 172 | |
| 173 | // Sanity check for RTP timestamp monotonicity. |
| 174 | if (last_decoded_frame_it_ != frames_.end()) { |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 175 | const VideoLayerFrameId& last_decoded_frame_key = |
| 176 | last_decoded_frame_it_->first; |
| 177 | const VideoLayerFrameId& frame_key = next_frame_it_->first; |
brandtr | 9078d8c | 2017-04-27 07:07:27 -0700 | [diff] [blame] | 178 | |
| 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 188 | RTC_LOG(LS_WARNING) |
| 189 | << "Frame with (timestamp:picture_id:spatial_id) (" |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 190 | << frame->timestamp << ":" << frame->id.picture_id << ":" |
| 191 | << static_cast<int>(frame->id.spatial_layer) << ")" |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 192 | << " 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) << ")."; |
brandtr | 9078d8c | 2017-04-27 07:07:27 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 200 | AdvanceLastDecodedFrame(next_frame_it_); |
| 201 | last_decoded_frame_timestamp_ = frame->timestamp; |
| 202 | *frame_out = std::move(frame); |
| 203 | return kFrameFound; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 204 | } |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | if (latest_return_time_ms - now_ms > 0) { |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 208 | // 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); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 213 | } |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 214 | |
| 215 | return kTimeout; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 216 | } |
| 217 | |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 218 | bool FrameBuffer::HasBadRenderTiming(const EncodedFrame& frame, |
| 219 | int64_t now_ms) { |
stefan | 95e9754 | 2017-05-23 09:52:18 -0700 | [diff] [blame] | 220 | // Assume that render timing errors are due to changes in the video stream. |
| 221 | int64_t render_time_ms = frame.RenderTimeMs(); |
Stefan Holmer | 812ceaf | 2018-05-15 13:00:10 +0200 | [diff] [blame] | 222 | // Zero render time means render immediately. |
| 223 | if (render_time_ms == 0) { |
| 224 | return false; |
| 225 | } |
stefan | 95e9754 | 2017-05-23 09:52:18 -0700 | [diff] [blame] | 226 | if (render_time_ms < 0) { |
| 227 | return true; |
| 228 | } |
Stefan Holmer | 812ceaf | 2018-05-15 13:00:10 +0200 | [diff] [blame] | 229 | const int64_t kMaxVideoDelayMs = 10000; |
stefan | 95e9754 | 2017-05-23 09:52:18 -0700 | [diff] [blame] | 230 | if (std::abs(render_time_ms - now_ms) > kMaxVideoDelayMs) { |
| 231 | int frame_delay = static_cast<int>(std::abs(render_time_ms - now_ms)); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 232 | RTC_LOG(LS_WARNING) |
| 233 | << "A frame about to be decoded is out of the configured " |
| 234 | << "delay bounds (" << frame_delay << " > " << kMaxVideoDelayMs |
| 235 | << "). Resetting the video jitter buffer."; |
stefan | 95e9754 | 2017-05-23 09:52:18 -0700 | [diff] [blame] | 236 | return true; |
| 237 | } |
| 238 | if (static_cast<int>(timing_->TargetVideoDelay()) > kMaxVideoDelayMs) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 239 | RTC_LOG(LS_WARNING) << "The video target delay has grown larger than " |
| 240 | << kMaxVideoDelayMs << " ms."; |
stefan | 95e9754 | 2017-05-23 09:52:18 -0700 | [diff] [blame] | 241 | return true; |
| 242 | } |
| 243 | return false; |
| 244 | } |
| 245 | |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 246 | void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 247 | TRACE_EVENT0("webrtc", "FrameBuffer::SetProtectionMode"); |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 248 | rtc::CritScope lock(&crit_); |
| 249 | protection_mode_ = mode; |
| 250 | } |
| 251 | |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 252 | void FrameBuffer::Start() { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 253 | TRACE_EVENT0("webrtc", "FrameBuffer::Start"); |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 254 | rtc::CritScope lock(&crit_); |
| 255 | stopped_ = false; |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void FrameBuffer::Stop() { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 259 | TRACE_EVENT0("webrtc", "FrameBuffer::Stop"); |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 260 | rtc::CritScope lock(&crit_); |
| 261 | stopped_ = true; |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 262 | new_continuous_frame_event_.Set(); |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 263 | } |
| 264 | |
philipel | e21be1d | 2017-09-25 06:37:12 -0700 | [diff] [blame] | 265 | void FrameBuffer::UpdateRtt(int64_t rtt_ms) { |
| 266 | rtc::CritScope lock(&crit_); |
| 267 | jitter_estimator_->UpdateRtt(rtt_ms); |
| 268 | } |
| 269 | |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 270 | bool FrameBuffer::ValidReferences(const EncodedFrame& frame) const { |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 271 | if (frame.id.picture_id < 0) |
philipel | 3b3c9c4 | 2017-09-11 09:38:36 -0700 | [diff] [blame] | 272 | return false; |
| 273 | |
philipel | 112adf9 | 2017-06-15 09:06:21 -0700 | [diff] [blame] | 274 | for (size_t i = 0; i < frame.num_references; ++i) { |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 275 | if (frame.references[i] < 0 || frame.references[i] >= frame.id.picture_id) |
philipel | 112adf9 | 2017-06-15 09:06:21 -0700 | [diff] [blame] | 276 | return false; |
philipel | 3b3c9c4 | 2017-09-11 09:38:36 -0700 | [diff] [blame] | 277 | |
philipel | 112adf9 | 2017-06-15 09:06:21 -0700 | [diff] [blame] | 278 | for (size_t j = i + 1; j < frame.num_references; ++j) { |
| 279 | if (frame.references[i] == frame.references[j]) |
| 280 | return false; |
| 281 | } |
| 282 | } |
| 283 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 284 | if (frame.inter_layer_predicted && frame.id.spatial_layer == 0) |
philipel | 112adf9 | 2017-06-15 09:06:21 -0700 | [diff] [blame] | 285 | return false; |
| 286 | |
| 287 | return true; |
| 288 | } |
| 289 | |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 290 | void FrameBuffer::UpdatePlayoutDelays(const EncodedFrame& frame) { |
gnish | b2a318b | 2017-05-10 09:21:33 -0700 | [diff] [blame] | 291 | TRACE_EVENT0("webrtc", "FrameBuffer::UpdatePlayoutDelays"); |
| 292 | PlayoutDelay playout_delay = frame.EncodedImage().playout_delay_; |
| 293 | if (playout_delay.min_ms >= 0) |
| 294 | timing_->set_min_playout_delay(playout_delay.min_ms); |
| 295 | |
| 296 | if (playout_delay.max_ms >= 0) |
| 297 | timing_->set_max_playout_delay(playout_delay.max_ms); |
philipel | 0a9f6de | 2018-02-28 11:29:47 +0100 | [diff] [blame] | 298 | |
| 299 | if (!frame.delayed_by_retransmission()) |
| 300 | timing_->IncomingTimestamp(frame.timestamp, frame.ReceivedTime()); |
gnish | b2a318b | 2017-05-10 09:21:33 -0700 | [diff] [blame] | 301 | } |
| 302 | |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 303 | int64_t FrameBuffer::InsertFrame(std::unique_ptr<EncodedFrame> frame) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 304 | TRACE_EVENT0("webrtc", "FrameBuffer::InsertFrame"); |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 305 | RTC_DCHECK(frame); |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 306 | if (stats_callback_) |
ilnik | 6d5b4d6 | 2017-08-30 03:32:14 -0700 | [diff] [blame] | 307 | stats_callback_->OnCompleteFrame(frame->is_keyframe(), frame->size(), |
| 308 | frame->contentType()); |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 309 | const VideoLayerFrameId& id = frame->id; |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 310 | |
| 311 | rtc::CritScope lock(&crit_); |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 312 | |
philipel | 1610f94 | 2017-12-12 13:58:31 +0100 | [diff] [blame] | 313 | int64_t last_continuous_picture_id = |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 314 | last_continuous_frame_it_ == frames_.end() |
| 315 | ? -1 |
| 316 | : last_continuous_frame_it_->first.picture_id; |
| 317 | |
philipel | 112adf9 | 2017-06-15 09:06:21 -0700 | [diff] [blame] | 318 | if (!ValidReferences(*frame)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 319 | RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 320 | << id.picture_id << ":" |
| 321 | << static_cast<int>(id.spatial_layer) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 322 | << ") has invalid frame references, dropping frame."; |
philipel | 112adf9 | 2017-06-15 09:06:21 -0700 | [diff] [blame] | 323 | return last_continuous_picture_id; |
| 324 | } |
| 325 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 326 | if (num_frames_buffered_ >= kMaxFramesBuffered) { |
philipel | 9771c50 | 2018-03-02 11:06:27 +0100 | [diff] [blame] | 327 | if (frame->is_keyframe()) { |
| 328 | RTC_LOG(LS_WARNING) << "Inserting keyframe (picture_id:spatial_id) (" |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 329 | << id.picture_id << ":" |
| 330 | << static_cast<int>(id.spatial_layer) |
philipel | 9771c50 | 2018-03-02 11:06:27 +0100 | [diff] [blame] | 331 | << ") but buffer is full, clearing" |
| 332 | << " buffer and inserting the frame."; |
| 333 | ClearFramesAndHistory(); |
| 334 | } else { |
| 335 | RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 336 | << id.picture_id << ":" |
| 337 | << static_cast<int>(id.spatial_layer) |
philipel | 9771c50 | 2018-03-02 11:06:27 +0100 | [diff] [blame] | 338 | << ") could not be inserted due to the frame " |
| 339 | << "buffer being full, dropping frame."; |
| 340 | return last_continuous_picture_id; |
| 341 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 342 | } |
| 343 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 344 | if (last_decoded_frame_it_ != frames_.end() && |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 345 | id <= last_decoded_frame_it_->first) { |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 346 | if (AheadOf(frame->timestamp, last_decoded_frame_timestamp_) && |
philipel | 3042c2d | 2017-08-18 04:55:02 -0700 | [diff] [blame] | 347 | frame->is_keyframe()) { |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 348 | // If this frame has a newer timestamp but an earlier picture id then we |
| 349 | // assume there has been a jump in the picture id due to some encoder |
| 350 | // reconfiguration or some other reason. Even though this is not according |
| 351 | // to spec we can still continue to decode from this frame if it is a |
| 352 | // keyframe. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 353 | RTC_LOG(LS_WARNING) |
| 354 | << "A jump in picture id was detected, clearing buffer."; |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 355 | ClearFramesAndHistory(); |
| 356 | last_continuous_picture_id = -1; |
| 357 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 358 | RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 359 | << id.picture_id << ":" |
| 360 | << static_cast<int>(id.spatial_layer) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 361 | << ") inserted after frame (" |
| 362 | << last_decoded_frame_it_->first.picture_id << ":" |
| 363 | << static_cast<int>( |
| 364 | last_decoded_frame_it_->first.spatial_layer) |
| 365 | << ") was handed off for decoding, dropping frame."; |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 366 | return last_continuous_picture_id; |
| 367 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 368 | } |
| 369 | |
philipel | 146a48b | 2017-04-20 04:04:38 -0700 | [diff] [blame] | 370 | // Test if inserting this frame would cause the order of the frames to become |
| 371 | // ambiguous (covering more than half the interval of 2^16). This can happen |
| 372 | // when the picture id make large jumps mid stream. |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 373 | if (!frames_.empty() && id < frames_.begin()->first && |
| 374 | frames_.rbegin()->first < id) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 375 | RTC_LOG(LS_WARNING) |
| 376 | << "A jump in picture id was detected, clearing buffer."; |
philipel | 146a48b | 2017-04-20 04:04:38 -0700 | [diff] [blame] | 377 | ClearFramesAndHistory(); |
| 378 | last_continuous_picture_id = -1; |
| 379 | } |
| 380 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 381 | auto info = frames_.emplace(id, FrameInfo()).first; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 382 | |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 383 | if (info->second.frame) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 384 | RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 385 | << id.picture_id << ":" |
| 386 | << static_cast<int>(id.spatial_layer) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 387 | << ") already inserted, dropping frame."; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 388 | return last_continuous_picture_id; |
| 389 | } |
| 390 | |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 391 | if (!UpdateFrameInfoWithIncomingFrame(*frame, info)) |
| 392 | return last_continuous_picture_id; |
gnish | b2a318b | 2017-05-10 09:21:33 -0700 | [diff] [blame] | 393 | UpdatePlayoutDelays(*frame); |
philipel | 0a9f6de | 2018-02-28 11:29:47 +0100 | [diff] [blame] | 394 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 395 | info->second.frame = std::move(frame); |
| 396 | ++num_frames_buffered_; |
| 397 | |
| 398 | if (info->second.num_missing_continuous == 0) { |
| 399 | info->second.continuous = true; |
| 400 | PropagateContinuity(info); |
| 401 | last_continuous_picture_id = last_continuous_frame_it_->first.picture_id; |
| 402 | |
| 403 | // Since we now have new continuous frames there might be a better frame |
| 404 | // to return from NextFrame. Signal that thread so that it again can choose |
| 405 | // which frame to return. |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 406 | new_continuous_frame_event_.Set(); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | return last_continuous_picture_id; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 410 | } |
| 411 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 412 | void FrameBuffer::PropagateContinuity(FrameMap::iterator start) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 413 | TRACE_EVENT0("webrtc", "FrameBuffer::PropagateContinuity"); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 414 | RTC_DCHECK(start->second.continuous); |
| 415 | if (last_continuous_frame_it_ == frames_.end()) |
| 416 | last_continuous_frame_it_ = start; |
| 417 | |
| 418 | std::queue<FrameMap::iterator> continuous_frames; |
| 419 | continuous_frames.push(start); |
| 420 | |
| 421 | // A simple BFS to traverse continuous frames. |
| 422 | while (!continuous_frames.empty()) { |
| 423 | auto frame = continuous_frames.front(); |
| 424 | continuous_frames.pop(); |
| 425 | |
| 426 | if (last_continuous_frame_it_->first < frame->first) |
| 427 | last_continuous_frame_it_ = frame; |
| 428 | |
| 429 | // Loop through all dependent frames, and if that frame no longer has |
| 430 | // any unfulfilled dependencies then that frame is continuous as well. |
| 431 | for (size_t d = 0; d < frame->second.num_dependent_frames; ++d) { |
| 432 | auto frame_ref = frames_.find(frame->second.dependent_frames[d]); |
philipel | 112adf9 | 2017-06-15 09:06:21 -0700 | [diff] [blame] | 433 | RTC_DCHECK(frame_ref != frames_.end()); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 434 | |
philipel | 112adf9 | 2017-06-15 09:06:21 -0700 | [diff] [blame] | 435 | // TODO(philipel): Look into why we've seen this happen. |
| 436 | if (frame_ref != frames_.end()) { |
| 437 | --frame_ref->second.num_missing_continuous; |
| 438 | if (frame_ref->second.num_missing_continuous == 0) { |
| 439 | frame_ref->second.continuous = true; |
| 440 | continuous_frames.push(frame_ref); |
| 441 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | void FrameBuffer::PropagateDecodability(const FrameInfo& info) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 448 | TRACE_EVENT0("webrtc", "FrameBuffer::PropagateDecodability"); |
tommi | e95b78b | 2017-05-14 07:23:11 -0700 | [diff] [blame] | 449 | RTC_CHECK(info.num_dependent_frames < FrameInfo::kMaxNumDependentFrames); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 450 | for (size_t d = 0; d < info.num_dependent_frames; ++d) { |
| 451 | auto ref_info = frames_.find(info.dependent_frames[d]); |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 452 | RTC_DCHECK(ref_info != frames_.end()); |
tommi | e95b78b | 2017-05-14 07:23:11 -0700 | [diff] [blame] | 453 | // TODO(philipel): Look into why we've seen this happen. |
| 454 | if (ref_info != frames_.end()) { |
| 455 | RTC_DCHECK_GT(ref_info->second.num_missing_decodable, 0U); |
| 456 | --ref_info->second.num_missing_decodable; |
| 457 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
| 461 | void FrameBuffer::AdvanceLastDecodedFrame(FrameMap::iterator decoded) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 462 | TRACE_EVENT0("webrtc", "FrameBuffer::AdvanceLastDecodedFrame"); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 463 | if (last_decoded_frame_it_ == frames_.end()) { |
| 464 | last_decoded_frame_it_ = frames_.begin(); |
| 465 | } else { |
| 466 | RTC_DCHECK(last_decoded_frame_it_->first < decoded->first); |
| 467 | ++last_decoded_frame_it_; |
| 468 | } |
| 469 | --num_frames_buffered_; |
| 470 | ++num_frames_history_; |
| 471 | |
| 472 | // First, delete non-decoded frames from the history. |
| 473 | while (last_decoded_frame_it_ != decoded) { |
| 474 | if (last_decoded_frame_it_->second.frame) |
| 475 | --num_frames_buffered_; |
| 476 | last_decoded_frame_it_ = frames_.erase(last_decoded_frame_it_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 477 | } |
| 478 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 479 | // Then remove old history if we have too much history saved. |
| 480 | if (num_frames_history_ > kMaxFramesHistory) { |
| 481 | frames_.erase(frames_.begin()); |
| 482 | --num_frames_history_; |
| 483 | } |
| 484 | } |
| 485 | |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 486 | bool FrameBuffer::UpdateFrameInfoWithIncomingFrame(const EncodedFrame& frame, |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 487 | FrameMap::iterator info) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 488 | TRACE_EVENT0("webrtc", "FrameBuffer::UpdateFrameInfoWithIncomingFrame"); |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 489 | const VideoLayerFrameId& id = frame.id; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 490 | |
| 491 | RTC_DCHECK(last_decoded_frame_it_ == frames_.end() || |
| 492 | last_decoded_frame_it_->first < info->first); |
| 493 | |
philipel | 798b282 | 2018-06-11 13:10:14 +0200 | [diff] [blame] | 494 | // In this function we determine how many missing dependencies this |frame| |
| 495 | // has to become continuous/decodable. If a frame that this |frame| depend |
| 496 | // on has already been decoded then we can ignore that dependency since it has |
| 497 | // already been fulfilled. |
| 498 | // |
| 499 | // For all other frames we will register a backwards reference to this |frame| |
| 500 | // so that |num_missing_continuous| and |num_missing_decodable| can be |
| 501 | // decremented as frames become continuous/are decoded. |
| 502 | struct Dependency { |
| 503 | VideoLayerFrameId id; |
| 504 | bool continuous; |
| 505 | }; |
| 506 | std::vector<Dependency> not_yet_fulfilled_dependencies; |
| 507 | |
| 508 | // Find all dependencies that have not yet been fulfilled. |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 509 | for (size_t i = 0; i < frame.num_references; ++i) { |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 510 | VideoLayerFrameId ref_key(frame.references[i], frame.id.spatial_layer); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 511 | auto ref_info = frames_.find(ref_key); |
| 512 | |
philipel | 798b282 | 2018-06-11 13:10:14 +0200 | [diff] [blame] | 513 | // Does |frame| depend on a frame earlier than the last decoded one? |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 514 | if (last_decoded_frame_it_ != frames_.end() && |
| 515 | ref_key <= last_decoded_frame_it_->first) { |
philipel | 798b282 | 2018-06-11 13:10:14 +0200 | [diff] [blame] | 516 | // Was that frame decoded? If not, this |frame| will never become |
| 517 | // decodable. |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 518 | if (ref_info == frames_.end()) { |
philipel | 65e1f94 | 2017-07-24 08:26:53 -0700 | [diff] [blame] | 519 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 520 | if (last_log_non_decoded_ms_ + kLogNonDecodedIntervalMs < now_ms) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 521 | RTC_LOG(LS_WARNING) |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 522 | << "Frame with (picture_id:spatial_id) (" << id.picture_id << ":" |
| 523 | << static_cast<int>(id.spatial_layer) |
philipel | 65e1f94 | 2017-07-24 08:26:53 -0700 | [diff] [blame] | 524 | << ") depends on a non-decoded frame more previous than" |
| 525 | << " the last decoded frame, dropping frame."; |
| 526 | last_log_non_decoded_ms_ = now_ms; |
| 527 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 528 | return false; |
| 529 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 530 | } else { |
philipel | 798b282 | 2018-06-11 13:10:14 +0200 | [diff] [blame] | 531 | bool ref_continuous = |
| 532 | ref_info != frames_.end() && ref_info->second.continuous; |
| 533 | not_yet_fulfilled_dependencies.push_back({ref_key, ref_continuous}); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 534 | } |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 535 | } |
| 536 | |
philipel | 798b282 | 2018-06-11 13:10:14 +0200 | [diff] [blame] | 537 | // Does |frame| depend on the lower spatial layer? |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 538 | if (frame.inter_layer_predicted) { |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 539 | VideoLayerFrameId ref_key(frame.id.picture_id, frame.id.spatial_layer - 1); |
philipel | 798b282 | 2018-06-11 13:10:14 +0200 | [diff] [blame] | 540 | auto ref_info = frames_.find(ref_key); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 541 | |
philipel | 798b282 | 2018-06-11 13:10:14 +0200 | [diff] [blame] | 542 | bool lower_layer_continuous = |
| 543 | ref_info != frames_.end() && ref_info->second.continuous; |
| 544 | bool lower_layer_decoded = last_decoded_frame_it_ != frames_.end() && |
| 545 | last_decoded_frame_it_->first == ref_key; |
| 546 | |
| 547 | if (!lower_layer_continuous || !lower_layer_decoded) { |
| 548 | not_yet_fulfilled_dependencies.push_back( |
| 549 | {ref_key, lower_layer_continuous}); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 550 | } |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 551 | } |
| 552 | |
philipel | 798b282 | 2018-06-11 13:10:14 +0200 | [diff] [blame] | 553 | info->second.num_missing_continuous = not_yet_fulfilled_dependencies.size(); |
| 554 | info->second.num_missing_decodable = not_yet_fulfilled_dependencies.size(); |
| 555 | |
| 556 | for (const Dependency& dep : not_yet_fulfilled_dependencies) { |
| 557 | if (dep.continuous) |
| 558 | --info->second.num_missing_continuous; |
| 559 | |
| 560 | // At this point we know we want to insert this frame, so here we |
| 561 | // intentionally get or create the FrameInfo for this dependency. |
| 562 | FrameInfo* dep_info = &frames_[dep.id]; |
| 563 | |
| 564 | if (dep_info->num_dependent_frames < |
| 565 | (FrameInfo::kMaxNumDependentFrames - 1)) { |
| 566 | dep_info->dependent_frames[dep_info->num_dependent_frames] = id; |
| 567 | ++dep_info->num_dependent_frames; |
| 568 | } else { |
| 569 | RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" |
| 570 | << dep.id.picture_id << ":" |
| 571 | << static_cast<int>(dep.id.spatial_layer) |
| 572 | << ") is referenced by too many frames."; |
| 573 | } |
| 574 | } |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 575 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 576 | return true; |
| 577 | } |
| 578 | |
philipel | be74270 | 2016-11-30 01:31:40 -0800 | [diff] [blame] | 579 | void FrameBuffer::UpdateJitterDelay() { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 580 | TRACE_EVENT0("webrtc", "FrameBuffer::UpdateJitterDelay"); |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 581 | if (!stats_callback_) |
| 582 | return; |
philipel | be74270 | 2016-11-30 01:31:40 -0800 | [diff] [blame] | 583 | |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 584 | int decode_ms; |
| 585 | int max_decode_ms; |
| 586 | int current_delay_ms; |
| 587 | int target_delay_ms; |
| 588 | int jitter_buffer_ms; |
| 589 | int min_playout_delay_ms; |
| 590 | int render_delay_ms; |
| 591 | if (timing_->GetTimings(&decode_ms, &max_decode_ms, ¤t_delay_ms, |
| 592 | &target_delay_ms, &jitter_buffer_ms, |
| 593 | &min_playout_delay_ms, &render_delay_ms)) { |
| 594 | stats_callback_->OnFrameBufferTimingsUpdated( |
| 595 | decode_ms, max_decode_ms, current_delay_ms, target_delay_ms, |
| 596 | jitter_buffer_ms, min_playout_delay_ms, render_delay_ms); |
philipel | be74270 | 2016-11-30 01:31:40 -0800 | [diff] [blame] | 597 | } |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 598 | } |
| 599 | |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 600 | void FrameBuffer::UpdateTimingFrameInfo() { |
| 601 | TRACE_EVENT0("webrtc", "FrameBuffer::UpdateTimingFrameInfo"); |
| 602 | rtc::Optional<TimingFrameInfo> info = timing_->GetTimingFrameInfo(); |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 603 | if (info && stats_callback_) |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 604 | stats_callback_->OnTimingFrameInfoUpdated(*info); |
| 605 | } |
| 606 | |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 607 | void FrameBuffer::ClearFramesAndHistory() { |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 608 | TRACE_EVENT0("webrtc", "FrameBuffer::ClearFramesAndHistory"); |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 609 | frames_.clear(); |
| 610 | last_decoded_frame_it_ = frames_.end(); |
| 611 | last_continuous_frame_it_ = frames_.end(); |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 612 | next_frame_it_ = frames_.end(); |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 613 | num_frames_history_ = 0; |
| 614 | num_frames_buffered_ = 0; |
| 615 | } |
| 616 | |
Niels Möller | be682d4 | 2018-03-27 08:31:45 +0200 | [diff] [blame] | 617 | FrameBuffer::FrameInfo::FrameInfo() = default; |
| 618 | FrameBuffer::FrameInfo::FrameInfo(FrameInfo&&) = default; |
| 619 | FrameBuffer::FrameInfo::~FrameInfo() = default; |
| 620 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 621 | } // namespace video_coding |
| 622 | } // namespace webrtc |