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), |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 42 | new_countinuous_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()), |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 46 | last_decoded_frame_it_(frames_.end()), |
| 47 | last_continuous_frame_it_(frames_.end()), |
| 48 | num_frames_history_(0), |
| 49 | num_frames_buffered_(0), |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 50 | stopped_(false), |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 51 | protection_mode_(kProtectionNack), |
| 52 | stats_callback_(stats_callback) {} |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 53 | |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 54 | FrameBuffer::~FrameBuffer() {} |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 55 | |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame] | 56 | FrameBuffer::ReturnReason FrameBuffer::NextFrame( |
| 57 | int64_t max_wait_time_ms, |
| 58 | std::unique_ptr<FrameObject>* frame_out) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame^] | 59 | TRACE_EVENT0("webrtc", "FrameBuffer::NextFrame"); |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 60 | int64_t latest_return_time_ms = |
| 61 | clock_->TimeInMilliseconds() + max_wait_time_ms; |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 62 | int64_t wait_ms = max_wait_time_ms; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 63 | |
| 64 | do { |
| 65 | int64_t now_ms = clock_->TimeInMilliseconds(); |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 66 | { |
| 67 | rtc::CritScope lock(&crit_); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 68 | new_countinuous_frame_event_.Reset(); |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 69 | if (stopped_) |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame] | 70 | return kStopped; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 71 | |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 72 | wait_ms = max_wait_time_ms; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 73 | |
| 74 | // Need to hold |crit_| in order to use |frames_|, therefore we |
| 75 | // set it here in the loop instead of outside the loop in order to not |
| 76 | // acquire the lock unnecesserily. |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 77 | next_frame_it_ = frames_.end(); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 78 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 79 | // |frame_it| points to the first frame after the |
| 80 | // |last_decoded_frame_it_|. |
| 81 | auto frame_it = frames_.end(); |
| 82 | if (last_decoded_frame_it_ == frames_.end()) { |
| 83 | frame_it = frames_.begin(); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 84 | } else { |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 85 | frame_it = last_decoded_frame_it_; |
| 86 | ++frame_it; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 87 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 88 | |
| 89 | // |continuous_end_it| points to the first frame after the |
| 90 | // |last_continuous_frame_it_|. |
| 91 | auto continuous_end_it = last_continuous_frame_it_; |
| 92 | if (continuous_end_it != frames_.end()) |
| 93 | ++continuous_end_it; |
| 94 | |
| 95 | for (; frame_it != continuous_end_it; ++frame_it) { |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 96 | if (!frame_it->second.continuous || |
| 97 | frame_it->second.num_missing_decodable > 0) { |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 98 | continue; |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 99 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 100 | |
| 101 | FrameObject* frame = frame_it->second.frame.get(); |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 102 | next_frame_it_ = frame_it; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 103 | if (frame->RenderTime() == -1) |
| 104 | frame->SetRenderTime(timing_->RenderTimeMs(frame->timestamp, now_ms)); |
| 105 | wait_ms = timing_->MaxWaitingTime(frame->RenderTime(), now_ms); |
| 106 | |
| 107 | // This will cause the frame buffer to prefer high framerate rather |
| 108 | // than high resolution in the case of the decoder not decoding fast |
| 109 | // enough and the stream has multiple spatial and temporal layers. |
| 110 | if (wait_ms == 0) |
| 111 | continue; |
| 112 | |
| 113 | break; |
| 114 | } |
| 115 | } // rtc::Critscope lock(&crit_); |
| 116 | |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 117 | 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] | 118 | wait_ms = std::max<int64_t>(wait_ms, 0); |
| 119 | } while (new_countinuous_frame_event_.Wait(wait_ms)); |
| 120 | |
| 121 | rtc::CritScope lock(&crit_); |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 122 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 123 | if (next_frame_it_ != frames_.end()) { |
| 124 | std::unique_ptr<FrameObject> frame = |
| 125 | std::move(next_frame_it_->second.frame); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 126 | |
philipel | e075430 | 2017-01-25 08:56:23 -0800 | [diff] [blame] | 127 | if (!frame->delayed_by_retransmission()) { |
| 128 | int64_t frame_delay; |
| 129 | |
| 130 | if (inter_frame_delay_.CalculateDelay(frame->timestamp, &frame_delay, |
| 131 | frame->ReceivedTime())) { |
| 132 | jitter_estimator_->UpdateEstimate(frame_delay, frame->size()); |
| 133 | } |
| 134 | |
| 135 | float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0; |
| 136 | timing_->SetJitterDelay(jitter_estimator_->GetJitterEstimate(rtt_mult)); |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 137 | timing_->UpdateCurrentDelay(frame->RenderTime(), now_ms); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 138 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 139 | |
philipel | be74270 | 2016-11-30 01:31:40 -0800 | [diff] [blame] | 140 | UpdateJitterDelay(); |
| 141 | |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 142 | PropagateDecodability(next_frame_it_->second); |
| 143 | AdvanceLastDecodedFrame(next_frame_it_); |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 144 | last_decoded_frame_timestamp_ = frame->timestamp; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 145 | *frame_out = std::move(frame); |
| 146 | return kFrameFound; |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 147 | } else if (latest_return_time_ms - now_ms > 0) { |
| 148 | // If |next_frame_it_ == frames_.end()| and there is still time left, it |
| 149 | // means that the frame buffer was cleared as the thread in this function |
| 150 | // was waiting to acquire |crit_| in order to return. Wait for the |
| 151 | // remaining time and then return. |
| 152 | return NextFrame(latest_return_time_ms - now_ms, frame_out); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 153 | } else { |
| 154 | return kTimeout; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 158 | void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame^] | 159 | TRACE_EVENT0("webrtc", "FrameBuffer::SetProtectionMode"); |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 160 | rtc::CritScope lock(&crit_); |
| 161 | protection_mode_ = mode; |
| 162 | } |
| 163 | |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 164 | void FrameBuffer::Start() { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame^] | 165 | TRACE_EVENT0("webrtc", "FrameBuffer::Start"); |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 166 | rtc::CritScope lock(&crit_); |
| 167 | stopped_ = false; |
| 168 | } |
| 169 | |
| 170 | void FrameBuffer::Stop() { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame^] | 171 | TRACE_EVENT0("webrtc", "FrameBuffer::Stop"); |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 172 | rtc::CritScope lock(&crit_); |
| 173 | stopped_ = true; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 174 | new_countinuous_frame_event_.Set(); |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 175 | } |
| 176 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 177 | int FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame^] | 178 | TRACE_EVENT0("webrtc", "FrameBuffer::InsertFrame"); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 179 | rtc::CritScope lock(&crit_); |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 180 | RTC_DCHECK(frame); |
| 181 | |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 182 | if (stats_callback_) |
| 183 | stats_callback_->OnCompleteFrame(frame->num_references == 0, frame->size()); |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 184 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 185 | FrameKey key(frame->picture_id, frame->spatial_layer); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 186 | int last_continuous_picture_id = |
| 187 | last_continuous_frame_it_ == frames_.end() |
| 188 | ? -1 |
| 189 | : last_continuous_frame_it_->first.picture_id; |
| 190 | |
| 191 | if (num_frames_buffered_ >= kMaxFramesBuffered) { |
| 192 | LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id |
| 193 | << ":" << static_cast<int>(key.spatial_layer) |
| 194 | << ") could not be inserted due to the frame " |
| 195 | << "buffer being full, dropping frame."; |
| 196 | return last_continuous_picture_id; |
| 197 | } |
| 198 | |
| 199 | if (frame->inter_layer_predicted && frame->spatial_layer == 0) { |
| 200 | LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id |
| 201 | << ":" << static_cast<int>(key.spatial_layer) |
| 202 | << ") is marked as inter layer predicted, dropping frame."; |
| 203 | return last_continuous_picture_id; |
| 204 | } |
| 205 | |
| 206 | if (last_decoded_frame_it_ != frames_.end() && |
| 207 | key < last_decoded_frame_it_->first) { |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 208 | if (AheadOf(frame->timestamp, last_decoded_frame_timestamp_) && |
| 209 | frame->num_references == 0) { |
| 210 | // If this frame has a newer timestamp but an earlier picture id then we |
| 211 | // assume there has been a jump in the picture id due to some encoder |
| 212 | // reconfiguration or some other reason. Even though this is not according |
| 213 | // to spec we can still continue to decode from this frame if it is a |
| 214 | // keyframe. |
| 215 | LOG(LS_WARNING) << "A jump in picture id was detected, clearing buffer."; |
| 216 | ClearFramesAndHistory(); |
| 217 | last_continuous_picture_id = -1; |
| 218 | } else { |
| 219 | LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" |
| 220 | << key.picture_id << ":" |
| 221 | << static_cast<int>(key.spatial_layer) |
| 222 | << ") inserted after frame (" |
| 223 | << last_decoded_frame_it_->first.picture_id << ":" |
| 224 | << static_cast<int>( |
| 225 | last_decoded_frame_it_->first.spatial_layer) |
| 226 | << ") was handed off for decoding, dropping frame."; |
| 227 | return last_continuous_picture_id; |
| 228 | } |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | auto info = frames_.insert(std::make_pair(key, FrameInfo())).first; |
| 232 | |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 233 | if (info->second.frame) { |
| 234 | LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id |
| 235 | << ":" << static_cast<int>(key.spatial_layer) |
| 236 | << ") already inserted, dropping frame."; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 237 | return last_continuous_picture_id; |
| 238 | } |
| 239 | |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 240 | if (!UpdateFrameInfoWithIncomingFrame(*frame, info)) |
| 241 | return last_continuous_picture_id; |
| 242 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 243 | info->second.frame = std::move(frame); |
| 244 | ++num_frames_buffered_; |
| 245 | |
| 246 | if (info->second.num_missing_continuous == 0) { |
| 247 | info->second.continuous = true; |
| 248 | PropagateContinuity(info); |
| 249 | last_continuous_picture_id = last_continuous_frame_it_->first.picture_id; |
| 250 | |
| 251 | // Since we now have new continuous frames there might be a better frame |
| 252 | // to return from NextFrame. Signal that thread so that it again can choose |
| 253 | // which frame to return. |
| 254 | new_countinuous_frame_event_.Set(); |
| 255 | } |
| 256 | |
| 257 | return last_continuous_picture_id; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 258 | } |
| 259 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 260 | void FrameBuffer::PropagateContinuity(FrameMap::iterator start) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame^] | 261 | TRACE_EVENT0("webrtc", "FrameBuffer::PropagateContinuity"); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 262 | RTC_DCHECK(start->second.continuous); |
| 263 | if (last_continuous_frame_it_ == frames_.end()) |
| 264 | last_continuous_frame_it_ = start; |
| 265 | |
| 266 | std::queue<FrameMap::iterator> continuous_frames; |
| 267 | continuous_frames.push(start); |
| 268 | |
| 269 | // A simple BFS to traverse continuous frames. |
| 270 | while (!continuous_frames.empty()) { |
| 271 | auto frame = continuous_frames.front(); |
| 272 | continuous_frames.pop(); |
| 273 | |
| 274 | if (last_continuous_frame_it_->first < frame->first) |
| 275 | last_continuous_frame_it_ = frame; |
| 276 | |
| 277 | // Loop through all dependent frames, and if that frame no longer has |
| 278 | // any unfulfilled dependencies then that frame is continuous as well. |
| 279 | for (size_t d = 0; d < frame->second.num_dependent_frames; ++d) { |
| 280 | auto frame_ref = frames_.find(frame->second.dependent_frames[d]); |
| 281 | --frame_ref->second.num_missing_continuous; |
| 282 | |
| 283 | if (frame_ref->second.num_missing_continuous == 0) { |
| 284 | frame_ref->second.continuous = true; |
| 285 | continuous_frames.push(frame_ref); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | void FrameBuffer::PropagateDecodability(const FrameInfo& info) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame^] | 292 | TRACE_EVENT0("webrtc", "FrameBuffer::PropagateDecodability"); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 293 | for (size_t d = 0; d < info.num_dependent_frames; ++d) { |
| 294 | auto ref_info = frames_.find(info.dependent_frames[d]); |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 295 | RTC_DCHECK(ref_info != frames_.end()); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 296 | RTC_DCHECK_GT(ref_info->second.num_missing_decodable, 0U); |
| 297 | --ref_info->second.num_missing_decodable; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | void FrameBuffer::AdvanceLastDecodedFrame(FrameMap::iterator decoded) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame^] | 302 | TRACE_EVENT0("webrtc", "FrameBuffer::AdvanceLastDecodedFrame"); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 303 | if (last_decoded_frame_it_ == frames_.end()) { |
| 304 | last_decoded_frame_it_ = frames_.begin(); |
| 305 | } else { |
| 306 | RTC_DCHECK(last_decoded_frame_it_->first < decoded->first); |
| 307 | ++last_decoded_frame_it_; |
| 308 | } |
| 309 | --num_frames_buffered_; |
| 310 | ++num_frames_history_; |
| 311 | |
| 312 | // First, delete non-decoded frames from the history. |
| 313 | while (last_decoded_frame_it_ != decoded) { |
| 314 | if (last_decoded_frame_it_->second.frame) |
| 315 | --num_frames_buffered_; |
| 316 | last_decoded_frame_it_ = frames_.erase(last_decoded_frame_it_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 317 | } |
| 318 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 319 | // Then remove old history if we have too much history saved. |
| 320 | if (num_frames_history_ > kMaxFramesHistory) { |
| 321 | frames_.erase(frames_.begin()); |
| 322 | --num_frames_history_; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | bool FrameBuffer::UpdateFrameInfoWithIncomingFrame(const FrameObject& frame, |
| 327 | FrameMap::iterator info) { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame^] | 328 | TRACE_EVENT0("webrtc", "FrameBuffer::UpdateFrameInfoWithIncomingFrame"); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 329 | FrameKey key(frame.picture_id, frame.spatial_layer); |
| 330 | info->second.num_missing_continuous = frame.num_references; |
| 331 | info->second.num_missing_decodable = frame.num_references; |
| 332 | |
| 333 | RTC_DCHECK(last_decoded_frame_it_ == frames_.end() || |
| 334 | last_decoded_frame_it_->first < info->first); |
| 335 | |
| 336 | // Check how many dependencies that have already been fulfilled. |
| 337 | for (size_t i = 0; i < frame.num_references; ++i) { |
| 338 | FrameKey ref_key(frame.references[i], frame.spatial_layer); |
| 339 | auto ref_info = frames_.find(ref_key); |
| 340 | |
| 341 | // Does |frame| depend on a frame earlier than the last decoded frame? |
| 342 | if (last_decoded_frame_it_ != frames_.end() && |
| 343 | ref_key <= last_decoded_frame_it_->first) { |
| 344 | if (ref_info == frames_.end()) { |
| 345 | LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" |
| 346 | << key.picture_id << ":" |
| 347 | << static_cast<int>(key.spatial_layer) |
| 348 | << " depends on a non-decoded frame more previous than " |
| 349 | << "the last decoded frame, dropping frame."; |
| 350 | return false; |
| 351 | } |
| 352 | |
| 353 | --info->second.num_missing_continuous; |
| 354 | --info->second.num_missing_decodable; |
| 355 | } else { |
| 356 | if (ref_info == frames_.end()) |
| 357 | ref_info = frames_.insert(std::make_pair(ref_key, FrameInfo())).first; |
| 358 | |
| 359 | if (ref_info->second.continuous) |
| 360 | --info->second.num_missing_continuous; |
| 361 | |
| 362 | // Add backwards reference so |frame| can be updated when new |
| 363 | // frames are inserted or decoded. |
| 364 | ref_info->second.dependent_frames[ref_info->second.num_dependent_frames] = |
| 365 | key; |
| 366 | ++ref_info->second.num_dependent_frames; |
| 367 | } |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 368 | RTC_DCHECK_LE(ref_info->second.num_missing_continuous, |
| 369 | ref_info->second.num_missing_decodable); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 370 | } |
| 371 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 372 | // Check if we have the lower spatial layer frame. |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 373 | if (frame.inter_layer_predicted) { |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 374 | ++info->second.num_missing_continuous; |
| 375 | ++info->second.num_missing_decodable; |
| 376 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 377 | FrameKey ref_key(frame.picture_id, frame.spatial_layer - 1); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 378 | // Gets or create the FrameInfo for the referenced frame. |
| 379 | auto ref_info = frames_.insert(std::make_pair(ref_key, FrameInfo())).first; |
| 380 | if (ref_info->second.continuous) |
| 381 | --info->second.num_missing_continuous; |
| 382 | |
| 383 | if (ref_info == last_decoded_frame_it_) { |
| 384 | --info->second.num_missing_decodable; |
| 385 | } else { |
| 386 | ref_info->second.dependent_frames[ref_info->second.num_dependent_frames] = |
| 387 | key; |
| 388 | ++ref_info->second.num_dependent_frames; |
| 389 | } |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 390 | RTC_DCHECK_LE(ref_info->second.num_missing_continuous, |
| 391 | ref_info->second.num_missing_decodable); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 392 | } |
| 393 | |
philipel | 93e451b | 2016-10-06 12:25:13 +0200 | [diff] [blame] | 394 | RTC_DCHECK_LE(info->second.num_missing_continuous, |
| 395 | info->second.num_missing_decodable); |
| 396 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 397 | return true; |
| 398 | } |
| 399 | |
philipel | be74270 | 2016-11-30 01:31:40 -0800 | [diff] [blame] | 400 | void FrameBuffer::UpdateJitterDelay() { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame^] | 401 | TRACE_EVENT0("webrtc", "FrameBuffer::UpdateJitterDelay"); |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 402 | if (!stats_callback_) |
| 403 | return; |
philipel | be74270 | 2016-11-30 01:31:40 -0800 | [diff] [blame] | 404 | |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 405 | int decode_ms; |
| 406 | int max_decode_ms; |
| 407 | int current_delay_ms; |
| 408 | int target_delay_ms; |
| 409 | int jitter_buffer_ms; |
| 410 | int min_playout_delay_ms; |
| 411 | int render_delay_ms; |
| 412 | if (timing_->GetTimings(&decode_ms, &max_decode_ms, ¤t_delay_ms, |
| 413 | &target_delay_ms, &jitter_buffer_ms, |
| 414 | &min_playout_delay_ms, &render_delay_ms)) { |
| 415 | stats_callback_->OnFrameBufferTimingsUpdated( |
| 416 | decode_ms, max_decode_ms, current_delay_ms, target_delay_ms, |
| 417 | jitter_buffer_ms, min_playout_delay_ms, render_delay_ms); |
philipel | be74270 | 2016-11-30 01:31:40 -0800 | [diff] [blame] | 418 | } |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 419 | } |
| 420 | |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 421 | void FrameBuffer::ClearFramesAndHistory() { |
tommi | db23ea6 | 2017-03-03 07:21:18 -0800 | [diff] [blame^] | 422 | TRACE_EVENT0("webrtc", "FrameBuffer::UpdateJitterDelay"); |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 423 | frames_.clear(); |
| 424 | last_decoded_frame_it_ = frames_.end(); |
| 425 | last_continuous_frame_it_ = frames_.end(); |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 426 | next_frame_it_ = frames_.end(); |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 427 | num_frames_history_ = 0; |
| 428 | num_frames_buffered_ = 0; |
| 429 | } |
| 430 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 431 | } // namespace video_coding |
| 432 | } // namespace webrtc |