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