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 | |
| 11 | #include "webrtc/modules/video_coding/frame_buffer2.h" |
| 12 | |
| 13 | #include <algorithm> |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 14 | #include <cstring> |
| 15 | #include <queue> |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 16 | |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 17 | #include "webrtc/modules/video_coding/include/video_coding_defines.h" |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 18 | #include "webrtc/modules/video_coding/jitter_estimator.h" |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 19 | #include "webrtc/modules/video_coding/timing.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 20 | #include "webrtc/rtc_base/checks.h" |
| 21 | #include "webrtc/rtc_base/logging.h" |
| 22 | #include "webrtc/rtc_base/trace_event.h" |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 23 | #include "webrtc/system_wrappers/include/clock.h" |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 24 | #include "webrtc/system_wrappers/include/metrics.h" |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | namespace video_coding { |
| 28 | |
| 29 | namespace { |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 30 | // Max number of frames the buffer will hold. |
| 31 | constexpr int kMaxFramesBuffered = 600; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 32 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 33 | // Max number of decoded frame info that will be saved. |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 34 | constexpr int kMaxFramesHistory = 50; |
philipel | 65e1f94 | 2017-07-24 08:26:53 -0700 | [diff] [blame] | 35 | |
| 36 | constexpr int64_t kLogNonDecodedIntervalMs = 5000; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 37 | } // namespace |
| 38 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 39 | FrameBuffer::FrameBuffer(Clock* clock, |
| 40 | VCMJitterEstimator* jitter_estimator, |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 41 | VCMTiming* timing, |
| 42 | VCMReceiveStatisticsCallback* stats_callback) |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 43 | : clock_(clock), |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 44 | new_continuous_frame_event_(false, false), |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 45 | jitter_estimator_(jitter_estimator), |
| 46 | timing_(timing), |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 47 | inter_frame_delay_(clock_->TimeInMilliseconds()), |
gnish | b2a318b | 2017-05-10 09:21:33 -0700 | [diff] [blame] | 48 | last_decoded_frame_timestamp_(0), |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 49 | last_decoded_frame_it_(frames_.end()), |
| 50 | last_continuous_frame_it_(frames_.end()), |
| 51 | num_frames_history_(0), |
| 52 | num_frames_buffered_(0), |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 53 | stopped_(false), |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 54 | protection_mode_(kProtectionNack), |
philipel | 65e1f94 | 2017-07-24 08:26:53 -0700 | [diff] [blame] | 55 | stats_callback_(stats_callback), |
| 56 | last_log_non_decoded_ms_(-kLogNonDecodedIntervalMs) {} |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 57 | |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 58 | FrameBuffer::~FrameBuffer() {} |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 59 | |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame] | 60 | FrameBuffer::ReturnReason FrameBuffer::NextFrame( |
| 61 | int64_t max_wait_time_ms, |
philipel | 3042c2d | 2017-08-18 04:55:02 -0700 | [diff] [blame^] | 62 | std::unique_ptr<FrameObject>* frame_out, |
| 63 | bool keyframe_required) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 64 | TRACE_EVENT0("webrtc", "FrameBuffer::NextFrame"); |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 65 | int64_t latest_return_time_ms = |
| 66 | clock_->TimeInMilliseconds() + max_wait_time_ms; |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 67 | int64_t wait_ms = max_wait_time_ms; |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 68 | int64_t now_ms = 0; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 69 | |
| 70 | do { |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 71 | now_ms = clock_->TimeInMilliseconds(); |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 72 | { |
| 73 | rtc::CritScope lock(&crit_); |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 74 | new_continuous_frame_event_.Reset(); |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 75 | if (stopped_) |
| 76 | return kStopped; |
| 77 | |
| 78 | wait_ms = max_wait_time_ms; |
| 79 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 80 | // 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. |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 83 | next_frame_it_ = frames_.end(); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 84 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 85 | // |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(); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 90 | } else { |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 91 | frame_it = last_decoded_frame_it_; |
| 92 | ++frame_it; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 93 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 94 | |
| 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 | |
philipel | 146a48b | 2017-04-20 04:04:38 -0700 | [diff] [blame] | 101 | for (; frame_it != continuous_end_it && frame_it != frames_.end(); |
| 102 | ++frame_it) { |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 103 | if (!frame_it->second.continuous || |
| 104 | frame_it->second.num_missing_decodable > 0) { |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 105 | continue; |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 106 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 107 | |
| 108 | FrameObject* frame = frame_it->second.frame.get(); |
philipel | 3042c2d | 2017-08-18 04:55:02 -0700 | [diff] [blame^] | 109 | |
| 110 | if (keyframe_required && !frame->is_keyframe()) |
| 111 | continue; |
| 112 | |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 113 | next_frame_it_ = frame_it; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 114 | 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 | |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 128 | 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] | 129 | wait_ms = std::max<int64_t>(wait_ms, 0); |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 130 | } while (new_continuous_frame_event_.Wait(wait_ms)); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 131 | |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 132 | { |
| 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); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 138 | |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 139 | if (!frame->delayed_by_retransmission()) { |
| 140 | int64_t frame_delay; |
philipel | e075430 | 2017-01-25 08:56:23 -0800 | [diff] [blame] | 141 | |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 142 | 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); |
gustavogb | f1e08d0 | 2017-08-09 05:43:08 -0700 | [diff] [blame] | 150 | } else { |
| 151 | jitter_estimator_->FrameNacked(); |
philipel | e075430 | 2017-01-25 08:56:23 -0800 | [diff] [blame] | 152 | } |
| 153 | |
stefan | 95e9754 | 2017-05-23 09:52:18 -0700 | [diff] [blame] | 154 | // 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 | |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 161 | UpdateJitterDelay(); |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 162 | UpdateTimingFrameInfo(); |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 163 | PropagateDecodability(next_frame_it_->second); |
brandtr | 9078d8c | 2017-04-27 07:07:27 -0700 | [diff] [blame] | 164 | |
| 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 | |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 192 | AdvanceLastDecodedFrame(next_frame_it_); |
| 193 | last_decoded_frame_timestamp_ = frame->timestamp; |
| 194 | *frame_out = std::move(frame); |
| 195 | return kFrameFound; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 196 | } |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | if (latest_return_time_ms - now_ms > 0) { |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 200 | // 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); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 205 | } |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 206 | |
| 207 | return kTimeout; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 208 | } |
| 209 | |
stefan | 95e9754 | 2017-05-23 09:52:18 -0700 | [diff] [blame] | 210 | bool 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 | |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 233 | void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 234 | TRACE_EVENT0("webrtc", "FrameBuffer::SetProtectionMode"); |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 235 | rtc::CritScope lock(&crit_); |
| 236 | protection_mode_ = mode; |
| 237 | } |
| 238 | |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 239 | void FrameBuffer::Start() { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 240 | TRACE_EVENT0("webrtc", "FrameBuffer::Start"); |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 241 | rtc::CritScope lock(&crit_); |
| 242 | stopped_ = false; |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | void FrameBuffer::Stop() { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 246 | TRACE_EVENT0("webrtc", "FrameBuffer::Stop"); |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 247 | rtc::CritScope lock(&crit_); |
| 248 | stopped_ = true; |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 249 | new_continuous_frame_event_.Set(); |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 250 | } |
| 251 | |
gustavogb | f1e08d0 | 2017-08-09 05:43:08 -0700 | [diff] [blame] | 252 | void FrameBuffer::UpdateRtt(int64_t rtt_ms) { |
| 253 | rtc::CritScope lock(&crit_); |
| 254 | jitter_estimator_->UpdateRtt(rtt_ms); |
| 255 | } |
| 256 | |
philipel | 112adf9 | 2017-06-15 09:06:21 -0700 | [diff] [blame] | 257 | bool FrameBuffer::ValidReferences(const FrameObject& frame) const { |
| 258 | for (size_t i = 0; i < frame.num_references; ++i) { |
| 259 | if (AheadOrAt(frame.references[i], frame.picture_id)) |
| 260 | return false; |
| 261 | for (size_t j = i + 1; j < frame.num_references; ++j) { |
| 262 | if (frame.references[i] == frame.references[j]) |
| 263 | return false; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | if (frame.inter_layer_predicted && frame.spatial_layer == 0) |
| 268 | return false; |
| 269 | |
| 270 | return true; |
| 271 | } |
| 272 | |
gnish | b2a318b | 2017-05-10 09:21:33 -0700 | [diff] [blame] | 273 | void FrameBuffer::UpdatePlayoutDelays(const FrameObject& frame) { |
| 274 | TRACE_EVENT0("webrtc", "FrameBuffer::UpdatePlayoutDelays"); |
| 275 | PlayoutDelay playout_delay = frame.EncodedImage().playout_delay_; |
| 276 | if (playout_delay.min_ms >= 0) |
| 277 | timing_->set_min_playout_delay(playout_delay.min_ms); |
| 278 | |
| 279 | if (playout_delay.max_ms >= 0) |
| 280 | timing_->set_max_playout_delay(playout_delay.max_ms); |
| 281 | } |
| 282 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 283 | int FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 284 | TRACE_EVENT0("webrtc", "FrameBuffer::InsertFrame"); |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 285 | RTC_DCHECK(frame); |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 286 | if (stats_callback_) |
philipel | 3042c2d | 2017-08-18 04:55:02 -0700 | [diff] [blame^] | 287 | stats_callback_->OnCompleteFrame(frame->is_keyframe(), frame->size()); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 288 | FrameKey key(frame->picture_id, frame->spatial_layer); |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 289 | |
| 290 | rtc::CritScope lock(&crit_); |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 291 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 292 | int last_continuous_picture_id = |
| 293 | last_continuous_frame_it_ == frames_.end() |
| 294 | ? -1 |
| 295 | : last_continuous_frame_it_->first.picture_id; |
| 296 | |
philipel | 112adf9 | 2017-06-15 09:06:21 -0700 | [diff] [blame] | 297 | if (!ValidReferences(*frame)) { |
| 298 | LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id |
| 299 | << ":" << static_cast<int>(key.spatial_layer) |
| 300 | << ") has invalid frame references, dropping frame."; |
| 301 | return last_continuous_picture_id; |
| 302 | } |
| 303 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 304 | if (num_frames_buffered_ >= kMaxFramesBuffered) { |
| 305 | LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id |
| 306 | << ":" << static_cast<int>(key.spatial_layer) |
| 307 | << ") could not be inserted due to the frame " |
| 308 | << "buffer being full, dropping frame."; |
| 309 | return last_continuous_picture_id; |
| 310 | } |
| 311 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 312 | if (last_decoded_frame_it_ != frames_.end() && |
philipel | f684269 | 2017-04-28 03:29:15 -0700 | [diff] [blame] | 313 | key <= last_decoded_frame_it_->first) { |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 314 | if (AheadOf(frame->timestamp, last_decoded_frame_timestamp_) && |
philipel | 3042c2d | 2017-08-18 04:55:02 -0700 | [diff] [blame^] | 315 | frame->is_keyframe()) { |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 316 | // If this frame has a newer timestamp but an earlier picture id then we |
| 317 | // assume there has been a jump in the picture id due to some encoder |
| 318 | // reconfiguration or some other reason. Even though this is not according |
| 319 | // to spec we can still continue to decode from this frame if it is a |
| 320 | // keyframe. |
| 321 | LOG(LS_WARNING) << "A jump in picture id was detected, clearing buffer."; |
| 322 | ClearFramesAndHistory(); |
| 323 | last_continuous_picture_id = -1; |
| 324 | } else { |
| 325 | LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" |
| 326 | << key.picture_id << ":" |
| 327 | << static_cast<int>(key.spatial_layer) |
| 328 | << ") inserted after frame (" |
| 329 | << last_decoded_frame_it_->first.picture_id << ":" |
| 330 | << static_cast<int>( |
| 331 | last_decoded_frame_it_->first.spatial_layer) |
| 332 | << ") was handed off for decoding, dropping frame."; |
| 333 | return last_continuous_picture_id; |
| 334 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 335 | } |
| 336 | |
philipel | 146a48b | 2017-04-20 04:04:38 -0700 | [diff] [blame] | 337 | // Test if inserting this frame would cause the order of the frames to become |
| 338 | // ambiguous (covering more than half the interval of 2^16). This can happen |
| 339 | // when the picture id make large jumps mid stream. |
| 340 | if (!frames_.empty() && |
| 341 | key < frames_.begin()->first && |
| 342 | frames_.rbegin()->first < key) { |
| 343 | LOG(LS_WARNING) << "A jump in picture id was detected, clearing buffer."; |
| 344 | ClearFramesAndHistory(); |
| 345 | last_continuous_picture_id = -1; |
| 346 | } |
| 347 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 348 | auto info = frames_.insert(std::make_pair(key, FrameInfo())).first; |
| 349 | |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 350 | if (info->second.frame) { |
| 351 | LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id |
| 352 | << ":" << static_cast<int>(key.spatial_layer) |
| 353 | << ") already inserted, dropping frame."; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 354 | return last_continuous_picture_id; |
| 355 | } |
| 356 | |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 357 | if (!UpdateFrameInfoWithIncomingFrame(*frame, info)) |
| 358 | return last_continuous_picture_id; |
gnish | b2a318b | 2017-05-10 09:21:33 -0700 | [diff] [blame] | 359 | UpdatePlayoutDelays(*frame); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 360 | info->second.frame = std::move(frame); |
| 361 | ++num_frames_buffered_; |
| 362 | |
| 363 | if (info->second.num_missing_continuous == 0) { |
| 364 | info->second.continuous = true; |
| 365 | PropagateContinuity(info); |
| 366 | last_continuous_picture_id = last_continuous_frame_it_->first.picture_id; |
| 367 | |
| 368 | // Since we now have new continuous frames there might be a better frame |
| 369 | // to return from NextFrame. Signal that thread so that it again can choose |
| 370 | // which frame to return. |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 371 | new_continuous_frame_event_.Set(); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | return last_continuous_picture_id; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 375 | } |
| 376 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 377 | void FrameBuffer::PropagateContinuity(FrameMap::iterator start) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 378 | TRACE_EVENT0("webrtc", "FrameBuffer::PropagateContinuity"); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 379 | RTC_DCHECK(start->second.continuous); |
| 380 | if (last_continuous_frame_it_ == frames_.end()) |
| 381 | last_continuous_frame_it_ = start; |
| 382 | |
| 383 | std::queue<FrameMap::iterator> continuous_frames; |
| 384 | continuous_frames.push(start); |
| 385 | |
| 386 | // A simple BFS to traverse continuous frames. |
| 387 | while (!continuous_frames.empty()) { |
| 388 | auto frame = continuous_frames.front(); |
| 389 | continuous_frames.pop(); |
| 390 | |
| 391 | if (last_continuous_frame_it_->first < frame->first) |
| 392 | last_continuous_frame_it_ = frame; |
| 393 | |
| 394 | // Loop through all dependent frames, and if that frame no longer has |
| 395 | // any unfulfilled dependencies then that frame is continuous as well. |
| 396 | for (size_t d = 0; d < frame->second.num_dependent_frames; ++d) { |
| 397 | auto frame_ref = frames_.find(frame->second.dependent_frames[d]); |
philipel | 112adf9 | 2017-06-15 09:06:21 -0700 | [diff] [blame] | 398 | RTC_DCHECK(frame_ref != frames_.end()); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 399 | |
philipel | 112adf9 | 2017-06-15 09:06:21 -0700 | [diff] [blame] | 400 | // TODO(philipel): Look into why we've seen this happen. |
| 401 | if (frame_ref != frames_.end()) { |
| 402 | --frame_ref->second.num_missing_continuous; |
| 403 | if (frame_ref->second.num_missing_continuous == 0) { |
| 404 | frame_ref->second.continuous = true; |
| 405 | continuous_frames.push(frame_ref); |
| 406 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | void FrameBuffer::PropagateDecodability(const FrameInfo& info) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 413 | TRACE_EVENT0("webrtc", "FrameBuffer::PropagateDecodability"); |
tommi | e95b78b | 2017-05-14 07:23:11 -0700 | [diff] [blame] | 414 | RTC_CHECK(info.num_dependent_frames < FrameInfo::kMaxNumDependentFrames); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 415 | for (size_t d = 0; d < info.num_dependent_frames; ++d) { |
| 416 | auto ref_info = frames_.find(info.dependent_frames[d]); |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 417 | RTC_DCHECK(ref_info != frames_.end()); |
tommi | e95b78b | 2017-05-14 07:23:11 -0700 | [diff] [blame] | 418 | // TODO(philipel): Look into why we've seen this happen. |
| 419 | if (ref_info != frames_.end()) { |
| 420 | RTC_DCHECK_GT(ref_info->second.num_missing_decodable, 0U); |
| 421 | --ref_info->second.num_missing_decodable; |
| 422 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
| 426 | void FrameBuffer::AdvanceLastDecodedFrame(FrameMap::iterator decoded) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 427 | TRACE_EVENT0("webrtc", "FrameBuffer::AdvanceLastDecodedFrame"); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 428 | if (last_decoded_frame_it_ == frames_.end()) { |
| 429 | last_decoded_frame_it_ = frames_.begin(); |
| 430 | } else { |
| 431 | RTC_DCHECK(last_decoded_frame_it_->first < decoded->first); |
| 432 | ++last_decoded_frame_it_; |
| 433 | } |
| 434 | --num_frames_buffered_; |
| 435 | ++num_frames_history_; |
| 436 | |
| 437 | // First, delete non-decoded frames from the history. |
| 438 | while (last_decoded_frame_it_ != decoded) { |
| 439 | if (last_decoded_frame_it_->second.frame) |
| 440 | --num_frames_buffered_; |
| 441 | last_decoded_frame_it_ = frames_.erase(last_decoded_frame_it_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 442 | } |
| 443 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 444 | // Then remove old history if we have too much history saved. |
| 445 | if (num_frames_history_ > kMaxFramesHistory) { |
| 446 | frames_.erase(frames_.begin()); |
| 447 | --num_frames_history_; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | bool FrameBuffer::UpdateFrameInfoWithIncomingFrame(const FrameObject& frame, |
| 452 | FrameMap::iterator info) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 453 | TRACE_EVENT0("webrtc", "FrameBuffer::UpdateFrameInfoWithIncomingFrame"); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 454 | FrameKey key(frame.picture_id, frame.spatial_layer); |
| 455 | info->second.num_missing_continuous = frame.num_references; |
| 456 | info->second.num_missing_decodable = frame.num_references; |
| 457 | |
| 458 | RTC_DCHECK(last_decoded_frame_it_ == frames_.end() || |
| 459 | last_decoded_frame_it_->first < info->first); |
| 460 | |
| 461 | // Check how many dependencies that have already been fulfilled. |
| 462 | for (size_t i = 0; i < frame.num_references; ++i) { |
| 463 | FrameKey ref_key(frame.references[i], frame.spatial_layer); |
| 464 | auto ref_info = frames_.find(ref_key); |
| 465 | |
| 466 | // Does |frame| depend on a frame earlier than the last decoded frame? |
| 467 | if (last_decoded_frame_it_ != frames_.end() && |
| 468 | ref_key <= last_decoded_frame_it_->first) { |
| 469 | if (ref_info == frames_.end()) { |
philipel | 65e1f94 | 2017-07-24 08:26:53 -0700 | [diff] [blame] | 470 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 471 | if (last_log_non_decoded_ms_ + kLogNonDecodedIntervalMs < now_ms) { |
| 472 | LOG(LS_WARNING) |
| 473 | << "Frame with (picture_id:spatial_id) (" << key.picture_id << ":" |
| 474 | << static_cast<int>(key.spatial_layer) |
| 475 | << ") depends on a non-decoded frame more previous than" |
| 476 | << " the last decoded frame, dropping frame."; |
| 477 | last_log_non_decoded_ms_ = now_ms; |
| 478 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 479 | return false; |
| 480 | } |
| 481 | |
| 482 | --info->second.num_missing_continuous; |
| 483 | --info->second.num_missing_decodable; |
| 484 | } else { |
| 485 | if (ref_info == frames_.end()) |
| 486 | ref_info = frames_.insert(std::make_pair(ref_key, FrameInfo())).first; |
| 487 | |
| 488 | if (ref_info->second.continuous) |
| 489 | --info->second.num_missing_continuous; |
| 490 | |
| 491 | // Add backwards reference so |frame| can be updated when new |
| 492 | // frames are inserted or decoded. |
| 493 | ref_info->second.dependent_frames[ref_info->second.num_dependent_frames] = |
| 494 | key; |
tommi | e95b78b | 2017-05-14 07:23:11 -0700 | [diff] [blame] | 495 | RTC_DCHECK_LT(ref_info->second.num_dependent_frames, |
| 496 | (FrameInfo::kMaxNumDependentFrames - 1)); |
| 497 | // TODO(philipel): Look into why this could happen and handle |
| 498 | // appropriately. |
| 499 | if (ref_info->second.num_dependent_frames < |
| 500 | (FrameInfo::kMaxNumDependentFrames - 1)) { |
| 501 | ++ref_info->second.num_dependent_frames; |
| 502 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 503 | } |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 504 | RTC_DCHECK_LE(ref_info->second.num_missing_continuous, |
| 505 | ref_info->second.num_missing_decodable); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 506 | } |
| 507 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 508 | // Check if we have the lower spatial layer frame. |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 509 | if (frame.inter_layer_predicted) { |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 510 | ++info->second.num_missing_continuous; |
| 511 | ++info->second.num_missing_decodable; |
| 512 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 513 | FrameKey ref_key(frame.picture_id, frame.spatial_layer - 1); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 514 | // Gets or create the FrameInfo for the referenced frame. |
| 515 | auto ref_info = frames_.insert(std::make_pair(ref_key, FrameInfo())).first; |
| 516 | if (ref_info->second.continuous) |
| 517 | --info->second.num_missing_continuous; |
| 518 | |
| 519 | if (ref_info == last_decoded_frame_it_) { |
| 520 | --info->second.num_missing_decodable; |
| 521 | } else { |
| 522 | ref_info->second.dependent_frames[ref_info->second.num_dependent_frames] = |
| 523 | key; |
| 524 | ++ref_info->second.num_dependent_frames; |
| 525 | } |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 526 | RTC_DCHECK_LE(ref_info->second.num_missing_continuous, |
| 527 | ref_info->second.num_missing_decodable); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 528 | } |
| 529 | |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 530 | RTC_DCHECK_LE(info->second.num_missing_continuous, |
| 531 | info->second.num_missing_decodable); |
| 532 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 533 | return true; |
| 534 | } |
| 535 | |
philipel | be74270 | 2016-11-30 01:31:40 -0800 | [diff] [blame] | 536 | void FrameBuffer::UpdateJitterDelay() { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 537 | TRACE_EVENT0("webrtc", "FrameBuffer::UpdateJitterDelay"); |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 538 | if (!stats_callback_) |
| 539 | return; |
philipel | be74270 | 2016-11-30 01:31:40 -0800 | [diff] [blame] | 540 | |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 541 | int decode_ms; |
| 542 | int max_decode_ms; |
| 543 | int current_delay_ms; |
| 544 | int target_delay_ms; |
| 545 | int jitter_buffer_ms; |
| 546 | int min_playout_delay_ms; |
| 547 | int render_delay_ms; |
| 548 | if (timing_->GetTimings(&decode_ms, &max_decode_ms, ¤t_delay_ms, |
| 549 | &target_delay_ms, &jitter_buffer_ms, |
| 550 | &min_playout_delay_ms, &render_delay_ms)) { |
| 551 | stats_callback_->OnFrameBufferTimingsUpdated( |
| 552 | decode_ms, max_decode_ms, current_delay_ms, target_delay_ms, |
| 553 | jitter_buffer_ms, min_playout_delay_ms, render_delay_ms); |
philipel | be74270 | 2016-11-30 01:31:40 -0800 | [diff] [blame] | 554 | } |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 555 | } |
| 556 | |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 557 | void FrameBuffer::UpdateTimingFrameInfo() { |
| 558 | TRACE_EVENT0("webrtc", "FrameBuffer::UpdateTimingFrameInfo"); |
| 559 | rtc::Optional<TimingFrameInfo> info = timing_->GetTimingFrameInfo(); |
| 560 | if (info) |
| 561 | stats_callback_->OnTimingFrameInfoUpdated(*info); |
| 562 | } |
| 563 | |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 564 | void FrameBuffer::ClearFramesAndHistory() { |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 565 | TRACE_EVENT0("webrtc", "FrameBuffer::ClearFramesAndHistory"); |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 566 | frames_.clear(); |
| 567 | last_decoded_frame_it_ = frames_.end(); |
| 568 | last_continuous_frame_it_ = frames_.end(); |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 569 | next_frame_it_ = frames_.end(); |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 570 | num_frames_history_ = 0; |
| 571 | num_frames_buffered_ = 0; |
| 572 | } |
| 573 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 574 | } // namespace video_coding |
| 575 | } // namespace webrtc |