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