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