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 | |
| 17 | #include "webrtc/base/checks.h" |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 18 | #include "webrtc/base/logging.h" |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 19 | #include "webrtc/base/trace_event.h" |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 20 | #include "webrtc/modules/video_coding/include/video_coding_defines.h" |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 21 | #include "webrtc/modules/video_coding/jitter_estimator.h" |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 22 | #include "webrtc/modules/video_coding/timing.h" |
| 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 | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 35 | } // namespace |
| 36 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 37 | FrameBuffer::FrameBuffer(Clock* clock, |
| 38 | VCMJitterEstimator* jitter_estimator, |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 39 | VCMTiming* timing, |
| 40 | VCMReceiveStatisticsCallback* stats_callback) |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 41 | : clock_(clock), |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 42 | new_continuous_frame_event_(false, false), |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 43 | jitter_estimator_(jitter_estimator), |
| 44 | timing_(timing), |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 45 | inter_frame_delay_(clock_->TimeInMilliseconds()), |
gnish | b2a318b | 2017-05-10 09:21:33 -0700 | [diff] [blame] | 46 | last_decoded_frame_timestamp_(0), |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 47 | last_decoded_frame_it_(frames_.end()), |
| 48 | last_continuous_frame_it_(frames_.end()), |
| 49 | num_frames_history_(0), |
| 50 | num_frames_buffered_(0), |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 51 | stopped_(false), |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 52 | protection_mode_(kProtectionNack), |
| 53 | stats_callback_(stats_callback) {} |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 54 | |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 55 | FrameBuffer::~FrameBuffer() {} |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 56 | |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame] | 57 | FrameBuffer::ReturnReason FrameBuffer::NextFrame( |
| 58 | int64_t max_wait_time_ms, |
| 59 | std::unique_ptr<FrameObject>* frame_out) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 60 | TRACE_EVENT0("webrtc", "FrameBuffer::NextFrame"); |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 61 | int64_t latest_return_time_ms = |
| 62 | clock_->TimeInMilliseconds() + max_wait_time_ms; |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 63 | int64_t wait_ms = max_wait_time_ms; |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 64 | int64_t now_ms = 0; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 65 | |
| 66 | do { |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 67 | now_ms = clock_->TimeInMilliseconds(); |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 68 | { |
| 69 | rtc::CritScope lock(&crit_); |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 70 | new_continuous_frame_event_.Reset(); |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 71 | if (stopped_) |
| 72 | return kStopped; |
| 73 | |
| 74 | wait_ms = max_wait_time_ms; |
| 75 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 76 | // 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. |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 79 | next_frame_it_ = frames_.end(); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 80 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 81 | // |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(); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 86 | } else { |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 87 | frame_it = last_decoded_frame_it_; |
| 88 | ++frame_it; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 89 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 90 | |
| 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 | |
philipel | 146a48b | 2017-04-20 04:04:38 -0700 | [diff] [blame] | 97 | for (; frame_it != continuous_end_it && frame_it != frames_.end(); |
| 98 | ++frame_it) { |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 99 | if (!frame_it->second.continuous || |
| 100 | frame_it->second.num_missing_decodable > 0) { |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 101 | continue; |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 102 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 103 | |
| 104 | FrameObject* frame = frame_it->second.frame.get(); |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 105 | next_frame_it_ = frame_it; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 106 | 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 | |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 120 | 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] | 121 | wait_ms = std::max<int64_t>(wait_ms, 0); |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 122 | } while (new_continuous_frame_event_.Wait(wait_ms)); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 123 | |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 124 | { |
| 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); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 130 | |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 131 | if (!frame->delayed_by_retransmission()) { |
| 132 | int64_t frame_delay; |
philipel | e075430 | 2017-01-25 08:56:23 -0800 | [diff] [blame] | 133 | |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 134 | 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); |
philipel | e075430 | 2017-01-25 08:56:23 -0800 | [diff] [blame] | 142 | } |
| 143 | |
stefan | 95e9754 | 2017-05-23 09:52:18 -0700 | [diff] [blame^] | 144 | // 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 | |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 151 | UpdateJitterDelay(); |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 152 | PropagateDecodability(next_frame_it_->second); |
brandtr | 9078d8c | 2017-04-27 07:07:27 -0700 | [diff] [blame] | 153 | |
| 154 | // Sanity check for RTP timestamp monotonicity. |
| 155 | if (last_decoded_frame_it_ != frames_.end()) { |
| 156 | const FrameKey& last_decoded_frame_key = last_decoded_frame_it_->first; |
| 157 | const FrameKey& frame_key = next_frame_it_->first; |
| 158 | |
| 159 | const bool frame_is_higher_spatial_layer_of_last_decoded_frame = |
| 160 | last_decoded_frame_timestamp_ == frame->timestamp && |
| 161 | last_decoded_frame_key.picture_id == frame_key.picture_id && |
| 162 | last_decoded_frame_key.spatial_layer < frame_key.spatial_layer; |
| 163 | |
| 164 | if (AheadOrAt(last_decoded_frame_timestamp_, frame->timestamp) && |
| 165 | !frame_is_higher_spatial_layer_of_last_decoded_frame) { |
| 166 | // TODO(brandtr): Consider clearing the entire buffer when we hit |
| 167 | // these conditions. |
| 168 | LOG(LS_WARNING) << "Frame with (timestamp:picture_id:spatial_id) (" |
| 169 | << frame->timestamp << ":" << frame->picture_id << ":" |
| 170 | << static_cast<int>(frame->spatial_layer) << ")" |
| 171 | << " sent to decoder after frame with" |
| 172 | << " (timestamp:picture_id:spatial_id) (" |
| 173 | << last_decoded_frame_timestamp_ << ":" |
| 174 | << last_decoded_frame_key.picture_id << ":" |
| 175 | << static_cast<int>( |
| 176 | last_decoded_frame_key.spatial_layer) |
| 177 | << ")."; |
| 178 | } |
| 179 | } |
| 180 | |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 181 | AdvanceLastDecodedFrame(next_frame_it_); |
| 182 | last_decoded_frame_timestamp_ = frame->timestamp; |
| 183 | *frame_out = std::move(frame); |
| 184 | return kFrameFound; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 185 | } |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | if (latest_return_time_ms - now_ms > 0) { |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 189 | // If |next_frame_it_ == frames_.end()| and there is still time left, it |
| 190 | // means that the frame buffer was cleared as the thread in this function |
| 191 | // was waiting to acquire |crit_| in order to return. Wait for the |
| 192 | // remaining time and then return. |
| 193 | return NextFrame(latest_return_time_ms - now_ms, frame_out); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 194 | } |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 195 | |
| 196 | return kTimeout; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 197 | } |
| 198 | |
stefan | 95e9754 | 2017-05-23 09:52:18 -0700 | [diff] [blame^] | 199 | bool FrameBuffer::HasBadRenderTiming(const FrameObject& frame, int64_t now_ms) { |
| 200 | // Assume that render timing errors are due to changes in the video stream. |
| 201 | int64_t render_time_ms = frame.RenderTimeMs(); |
| 202 | const int64_t kMaxVideoDelayMs = 10000; |
| 203 | if (render_time_ms < 0) { |
| 204 | return true; |
| 205 | } |
| 206 | if (std::abs(render_time_ms - now_ms) > kMaxVideoDelayMs) { |
| 207 | int frame_delay = static_cast<int>(std::abs(render_time_ms - now_ms)); |
| 208 | LOG(LS_WARNING) << "A frame about to be decoded is out of the configured " |
| 209 | << "delay bounds (" << frame_delay << " > " |
| 210 | << kMaxVideoDelayMs |
| 211 | << "). Resetting the video jitter buffer."; |
| 212 | return true; |
| 213 | } |
| 214 | if (static_cast<int>(timing_->TargetVideoDelay()) > kMaxVideoDelayMs) { |
| 215 | LOG(LS_WARNING) << "The video target delay has grown larger than " |
| 216 | << kMaxVideoDelayMs << " ms."; |
| 217 | return true; |
| 218 | } |
| 219 | return false; |
| 220 | } |
| 221 | |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 222 | void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 223 | TRACE_EVENT0("webrtc", "FrameBuffer::SetProtectionMode"); |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 224 | rtc::CritScope lock(&crit_); |
| 225 | protection_mode_ = mode; |
| 226 | } |
| 227 | |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 228 | void FrameBuffer::Start() { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 229 | TRACE_EVENT0("webrtc", "FrameBuffer::Start"); |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 230 | rtc::CritScope lock(&crit_); |
| 231 | stopped_ = false; |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void FrameBuffer::Stop() { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 235 | TRACE_EVENT0("webrtc", "FrameBuffer::Stop"); |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 236 | rtc::CritScope lock(&crit_); |
| 237 | stopped_ = true; |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 238 | new_continuous_frame_event_.Set(); |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 239 | } |
| 240 | |
gnish | b2a318b | 2017-05-10 09:21:33 -0700 | [diff] [blame] | 241 | void FrameBuffer::UpdatePlayoutDelays(const FrameObject& frame) { |
| 242 | TRACE_EVENT0("webrtc", "FrameBuffer::UpdatePlayoutDelays"); |
| 243 | PlayoutDelay playout_delay = frame.EncodedImage().playout_delay_; |
| 244 | if (playout_delay.min_ms >= 0) |
| 245 | timing_->set_min_playout_delay(playout_delay.min_ms); |
| 246 | |
| 247 | if (playout_delay.max_ms >= 0) |
| 248 | timing_->set_max_playout_delay(playout_delay.max_ms); |
| 249 | } |
| 250 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 251 | int FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 252 | TRACE_EVENT0("webrtc", "FrameBuffer::InsertFrame"); |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 253 | RTC_DCHECK(frame); |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 254 | if (stats_callback_) |
| 255 | stats_callback_->OnCompleteFrame(frame->num_references == 0, frame->size()); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 256 | FrameKey key(frame->picture_id, frame->spatial_layer); |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 257 | |
| 258 | rtc::CritScope lock(&crit_); |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 259 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 260 | int last_continuous_picture_id = |
| 261 | last_continuous_frame_it_ == frames_.end() |
| 262 | ? -1 |
| 263 | : last_continuous_frame_it_->first.picture_id; |
| 264 | |
| 265 | if (num_frames_buffered_ >= kMaxFramesBuffered) { |
| 266 | LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id |
| 267 | << ":" << static_cast<int>(key.spatial_layer) |
| 268 | << ") could not be inserted due to the frame " |
| 269 | << "buffer being full, dropping frame."; |
| 270 | return last_continuous_picture_id; |
| 271 | } |
| 272 | |
| 273 | if (frame->inter_layer_predicted && frame->spatial_layer == 0) { |
| 274 | LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id |
| 275 | << ":" << static_cast<int>(key.spatial_layer) |
| 276 | << ") is marked as inter layer predicted, dropping frame."; |
| 277 | return last_continuous_picture_id; |
| 278 | } |
| 279 | |
| 280 | if (last_decoded_frame_it_ != frames_.end() && |
philipel | f684269 | 2017-04-28 03:29:15 -0700 | [diff] [blame] | 281 | key <= last_decoded_frame_it_->first) { |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 282 | if (AheadOf(frame->timestamp, last_decoded_frame_timestamp_) && |
| 283 | frame->num_references == 0) { |
| 284 | // If this frame has a newer timestamp but an earlier picture id then we |
| 285 | // assume there has been a jump in the picture id due to some encoder |
| 286 | // reconfiguration or some other reason. Even though this is not according |
| 287 | // to spec we can still continue to decode from this frame if it is a |
| 288 | // keyframe. |
| 289 | LOG(LS_WARNING) << "A jump in picture id was detected, clearing buffer."; |
| 290 | ClearFramesAndHistory(); |
| 291 | last_continuous_picture_id = -1; |
| 292 | } else { |
| 293 | LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" |
| 294 | << key.picture_id << ":" |
| 295 | << static_cast<int>(key.spatial_layer) |
| 296 | << ") inserted after frame (" |
| 297 | << last_decoded_frame_it_->first.picture_id << ":" |
| 298 | << static_cast<int>( |
| 299 | last_decoded_frame_it_->first.spatial_layer) |
| 300 | << ") was handed off for decoding, dropping frame."; |
| 301 | return last_continuous_picture_id; |
| 302 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 303 | } |
| 304 | |
philipel | 146a48b | 2017-04-20 04:04:38 -0700 | [diff] [blame] | 305 | // Test if inserting this frame would cause the order of the frames to become |
| 306 | // ambiguous (covering more than half the interval of 2^16). This can happen |
| 307 | // when the picture id make large jumps mid stream. |
| 308 | if (!frames_.empty() && |
| 309 | key < frames_.begin()->first && |
| 310 | frames_.rbegin()->first < key) { |
| 311 | LOG(LS_WARNING) << "A jump in picture id was detected, clearing buffer."; |
| 312 | ClearFramesAndHistory(); |
| 313 | last_continuous_picture_id = -1; |
| 314 | } |
| 315 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 316 | auto info = frames_.insert(std::make_pair(key, FrameInfo())).first; |
| 317 | |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 318 | if (info->second.frame) { |
| 319 | LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id |
| 320 | << ":" << static_cast<int>(key.spatial_layer) |
| 321 | << ") already inserted, dropping frame."; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 322 | return last_continuous_picture_id; |
| 323 | } |
| 324 | |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 325 | if (!UpdateFrameInfoWithIncomingFrame(*frame, info)) |
| 326 | return last_continuous_picture_id; |
gnish | b2a318b | 2017-05-10 09:21:33 -0700 | [diff] [blame] | 327 | UpdatePlayoutDelays(*frame); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 328 | info->second.frame = std::move(frame); |
| 329 | ++num_frames_buffered_; |
| 330 | |
| 331 | if (info->second.num_missing_continuous == 0) { |
| 332 | info->second.continuous = true; |
| 333 | PropagateContinuity(info); |
| 334 | last_continuous_picture_id = last_continuous_frame_it_->first.picture_id; |
| 335 | |
| 336 | // Since we now have new continuous frames there might be a better frame |
| 337 | // to return from NextFrame. Signal that thread so that it again can choose |
| 338 | // which frame to return. |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 339 | new_continuous_frame_event_.Set(); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | return last_continuous_picture_id; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 343 | } |
| 344 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 345 | void FrameBuffer::PropagateContinuity(FrameMap::iterator start) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 346 | TRACE_EVENT0("webrtc", "FrameBuffer::PropagateContinuity"); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 347 | RTC_DCHECK(start->second.continuous); |
| 348 | if (last_continuous_frame_it_ == frames_.end()) |
| 349 | last_continuous_frame_it_ = start; |
| 350 | |
| 351 | std::queue<FrameMap::iterator> continuous_frames; |
| 352 | continuous_frames.push(start); |
| 353 | |
| 354 | // A simple BFS to traverse continuous frames. |
| 355 | while (!continuous_frames.empty()) { |
| 356 | auto frame = continuous_frames.front(); |
| 357 | continuous_frames.pop(); |
| 358 | |
| 359 | if (last_continuous_frame_it_->first < frame->first) |
| 360 | last_continuous_frame_it_ = frame; |
| 361 | |
| 362 | // Loop through all dependent frames, and if that frame no longer has |
| 363 | // any unfulfilled dependencies then that frame is continuous as well. |
| 364 | for (size_t d = 0; d < frame->second.num_dependent_frames; ++d) { |
| 365 | auto frame_ref = frames_.find(frame->second.dependent_frames[d]); |
| 366 | --frame_ref->second.num_missing_continuous; |
| 367 | |
| 368 | if (frame_ref->second.num_missing_continuous == 0) { |
| 369 | frame_ref->second.continuous = true; |
| 370 | continuous_frames.push(frame_ref); |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | void FrameBuffer::PropagateDecodability(const FrameInfo& info) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 377 | TRACE_EVENT0("webrtc", "FrameBuffer::PropagateDecodability"); |
tommi | e95b78b | 2017-05-14 07:23:11 -0700 | [diff] [blame] | 378 | RTC_CHECK(info.num_dependent_frames < FrameInfo::kMaxNumDependentFrames); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 379 | for (size_t d = 0; d < info.num_dependent_frames; ++d) { |
| 380 | auto ref_info = frames_.find(info.dependent_frames[d]); |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 381 | RTC_DCHECK(ref_info != frames_.end()); |
tommi | e95b78b | 2017-05-14 07:23:11 -0700 | [diff] [blame] | 382 | // TODO(philipel): Look into why we've seen this happen. |
| 383 | if (ref_info != frames_.end()) { |
| 384 | RTC_DCHECK_GT(ref_info->second.num_missing_decodable, 0U); |
| 385 | --ref_info->second.num_missing_decodable; |
| 386 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
| 390 | void FrameBuffer::AdvanceLastDecodedFrame(FrameMap::iterator decoded) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 391 | TRACE_EVENT0("webrtc", "FrameBuffer::AdvanceLastDecodedFrame"); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 392 | if (last_decoded_frame_it_ == frames_.end()) { |
| 393 | last_decoded_frame_it_ = frames_.begin(); |
| 394 | } else { |
| 395 | RTC_DCHECK(last_decoded_frame_it_->first < decoded->first); |
| 396 | ++last_decoded_frame_it_; |
| 397 | } |
| 398 | --num_frames_buffered_; |
| 399 | ++num_frames_history_; |
| 400 | |
| 401 | // First, delete non-decoded frames from the history. |
| 402 | while (last_decoded_frame_it_ != decoded) { |
| 403 | if (last_decoded_frame_it_->second.frame) |
| 404 | --num_frames_buffered_; |
| 405 | last_decoded_frame_it_ = frames_.erase(last_decoded_frame_it_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 406 | } |
| 407 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 408 | // Then remove old history if we have too much history saved. |
| 409 | if (num_frames_history_ > kMaxFramesHistory) { |
| 410 | frames_.erase(frames_.begin()); |
| 411 | --num_frames_history_; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | bool FrameBuffer::UpdateFrameInfoWithIncomingFrame(const FrameObject& frame, |
| 416 | FrameMap::iterator info) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 417 | TRACE_EVENT0("webrtc", "FrameBuffer::UpdateFrameInfoWithIncomingFrame"); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 418 | FrameKey key(frame.picture_id, frame.spatial_layer); |
| 419 | info->second.num_missing_continuous = frame.num_references; |
| 420 | info->second.num_missing_decodable = frame.num_references; |
| 421 | |
| 422 | RTC_DCHECK(last_decoded_frame_it_ == frames_.end() || |
| 423 | last_decoded_frame_it_->first < info->first); |
| 424 | |
| 425 | // Check how many dependencies that have already been fulfilled. |
| 426 | for (size_t i = 0; i < frame.num_references; ++i) { |
| 427 | FrameKey ref_key(frame.references[i], frame.spatial_layer); |
| 428 | auto ref_info = frames_.find(ref_key); |
| 429 | |
| 430 | // Does |frame| depend on a frame earlier than the last decoded frame? |
| 431 | if (last_decoded_frame_it_ != frames_.end() && |
| 432 | ref_key <= last_decoded_frame_it_->first) { |
| 433 | if (ref_info == frames_.end()) { |
| 434 | LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" |
| 435 | << key.picture_id << ":" |
| 436 | << static_cast<int>(key.spatial_layer) |
| 437 | << " depends on a non-decoded frame more previous than " |
| 438 | << "the last decoded frame, dropping frame."; |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | --info->second.num_missing_continuous; |
| 443 | --info->second.num_missing_decodable; |
| 444 | } else { |
| 445 | if (ref_info == frames_.end()) |
| 446 | ref_info = frames_.insert(std::make_pair(ref_key, FrameInfo())).first; |
| 447 | |
| 448 | if (ref_info->second.continuous) |
| 449 | --info->second.num_missing_continuous; |
| 450 | |
| 451 | // Add backwards reference so |frame| can be updated when new |
| 452 | // frames are inserted or decoded. |
| 453 | ref_info->second.dependent_frames[ref_info->second.num_dependent_frames] = |
| 454 | key; |
tommi | e95b78b | 2017-05-14 07:23:11 -0700 | [diff] [blame] | 455 | RTC_DCHECK_LT(ref_info->second.num_dependent_frames, |
| 456 | (FrameInfo::kMaxNumDependentFrames - 1)); |
| 457 | // TODO(philipel): Look into why this could happen and handle |
| 458 | // appropriately. |
| 459 | if (ref_info->second.num_dependent_frames < |
| 460 | (FrameInfo::kMaxNumDependentFrames - 1)) { |
| 461 | ++ref_info->second.num_dependent_frames; |
| 462 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 463 | } |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 464 | RTC_DCHECK_LE(ref_info->second.num_missing_continuous, |
| 465 | ref_info->second.num_missing_decodable); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 466 | } |
| 467 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 468 | // Check if we have the lower spatial layer frame. |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 469 | if (frame.inter_layer_predicted) { |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 470 | ++info->second.num_missing_continuous; |
| 471 | ++info->second.num_missing_decodable; |
| 472 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 473 | FrameKey ref_key(frame.picture_id, frame.spatial_layer - 1); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 474 | // Gets or create the FrameInfo for the referenced frame. |
| 475 | auto ref_info = frames_.insert(std::make_pair(ref_key, FrameInfo())).first; |
| 476 | if (ref_info->second.continuous) |
| 477 | --info->second.num_missing_continuous; |
| 478 | |
| 479 | if (ref_info == last_decoded_frame_it_) { |
| 480 | --info->second.num_missing_decodable; |
| 481 | } else { |
| 482 | ref_info->second.dependent_frames[ref_info->second.num_dependent_frames] = |
| 483 | key; |
| 484 | ++ref_info->second.num_dependent_frames; |
| 485 | } |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 486 | RTC_DCHECK_LE(ref_info->second.num_missing_continuous, |
| 487 | ref_info->second.num_missing_decodable); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 488 | } |
| 489 | |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 490 | RTC_DCHECK_LE(info->second.num_missing_continuous, |
| 491 | info->second.num_missing_decodable); |
| 492 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 493 | return true; |
| 494 | } |
| 495 | |
philipel | be74270 | 2016-11-30 01:31:40 -0800 | [diff] [blame] | 496 | void FrameBuffer::UpdateJitterDelay() { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 497 | TRACE_EVENT0("webrtc", "FrameBuffer::UpdateJitterDelay"); |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 498 | if (!stats_callback_) |
| 499 | return; |
philipel | be74270 | 2016-11-30 01:31:40 -0800 | [diff] [blame] | 500 | |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 501 | int decode_ms; |
| 502 | int max_decode_ms; |
| 503 | int current_delay_ms; |
| 504 | int target_delay_ms; |
| 505 | int jitter_buffer_ms; |
| 506 | int min_playout_delay_ms; |
| 507 | int render_delay_ms; |
| 508 | if (timing_->GetTimings(&decode_ms, &max_decode_ms, ¤t_delay_ms, |
| 509 | &target_delay_ms, &jitter_buffer_ms, |
| 510 | &min_playout_delay_ms, &render_delay_ms)) { |
| 511 | stats_callback_->OnFrameBufferTimingsUpdated( |
| 512 | decode_ms, max_decode_ms, current_delay_ms, target_delay_ms, |
| 513 | jitter_buffer_ms, min_playout_delay_ms, render_delay_ms); |
philipel | be74270 | 2016-11-30 01:31:40 -0800 | [diff] [blame] | 514 | } |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 515 | } |
| 516 | |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 517 | void FrameBuffer::ClearFramesAndHistory() { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame] | 518 | TRACE_EVENT0("webrtc", "FrameBuffer::UpdateJitterDelay"); |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 519 | frames_.clear(); |
| 520 | last_decoded_frame_it_ = frames_.end(); |
| 521 | last_continuous_frame_it_ = frames_.end(); |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 522 | next_frame_it_ = frames_.end(); |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 523 | num_frames_history_ = 0; |
| 524 | num_frames_buffered_ = 0; |
| 525 | } |
| 526 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 527 | } // namespace video_coding |
| 528 | } // namespace webrtc |