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