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> |
| 14 | |
| 15 | #include "webrtc/base/checks.h" |
| 16 | #include "webrtc/modules/video_coding/frame_object.h" |
| 17 | #include "webrtc/modules/video_coding/jitter_estimator.h" |
| 18 | #include "webrtc/modules/video_coding/sequence_number_util.h" |
| 19 | #include "webrtc/modules/video_coding/timing.h" |
| 20 | #include "webrtc/system_wrappers/include/clock.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | namespace video_coding { |
| 24 | |
| 25 | namespace { |
| 26 | // The maximum age of decoded frames tracked by frame buffer, compared to |
| 27 | // |newest_picture_id_|. |
| 28 | constexpr int kMaxFrameAge = 4096; |
| 29 | |
| 30 | // The maximum number of decoded frames being tracked by the frame buffer. |
| 31 | constexpr int kMaxNumHistoryFrames = 256; |
| 32 | } // namespace |
| 33 | |
| 34 | bool FrameBuffer::FrameComp::operator()(const FrameKey& f1, |
| 35 | const FrameKey& f2) const { |
| 36 | // first = picture id |
| 37 | // second = spatial layer |
| 38 | if (f1.first == f2.first) |
| 39 | return f1.second < f2.second; |
| 40 | return AheadOf(f2.first, f1.first); |
| 41 | } |
| 42 | |
| 43 | FrameBuffer::FrameBuffer(Clock* clock, |
| 44 | VCMJitterEstimator* jitter_estimator, |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 45 | VCMTiming* timing) |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 46 | : clock_(clock), |
| 47 | frame_inserted_event_(false, false), |
| 48 | jitter_estimator_(jitter_estimator), |
| 49 | timing_(timing), |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 50 | inter_frame_delay_(clock_->TimeInMilliseconds()), |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 51 | newest_picture_id_(-1), |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 52 | stopped_(false), |
| 53 | protection_mode_(kProtectionNack) {} |
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 now = clock_->TimeInMilliseconds(); |
| 60 | int64_t wait_ms = max_wait_time_ms; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 61 | while (true) { |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 62 | std::map<FrameKey, std::unique_ptr<FrameObject>, FrameComp>::iterator |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 63 | next_frame_it; |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 64 | { |
| 65 | rtc::CritScope lock(&crit_); |
| 66 | frame_inserted_event_.Reset(); |
| 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 | now = clock_->TimeInMilliseconds(); |
| 71 | wait_ms = max_wait_time_ms; |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 72 | next_frame_it = frames_.end(); |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 73 | for (auto frame_it = frames_.begin(); frame_it != frames_.end(); |
| 74 | ++frame_it) { |
| 75 | const FrameObject& frame = *frame_it->second; |
| 76 | if (IsContinuous(frame)) { |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 77 | next_frame_it = frame_it; |
| 78 | int64_t render_time = |
| 79 | next_frame_it->second->RenderTime() == -1 |
| 80 | ? timing_->RenderTimeMs(frame.timestamp, now) |
| 81 | : next_frame_it->second->RenderTime(); |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 82 | wait_ms = timing_->MaxWaitingTime(render_time, now); |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 83 | frame_it->second->SetRenderTime(render_time); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 84 | |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 85 | // This will cause the frame buffer to prefer high framerate rather |
| 86 | // than high resolution in the case of the decoder not decoding fast |
| 87 | // enough and the stream has multiple spatial and temporal layers. |
| 88 | if (wait_ms == 0) |
| 89 | continue; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 90 | |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 91 | break; |
| 92 | } |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 93 | } |
| 94 | } |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 95 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 96 | wait_ms = std::min<int64_t>(wait_ms, latest_return_time - now); |
| 97 | wait_ms = std::max<int64_t>(wait_ms, 0); |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 98 | // If the timeout occurs, return. Otherwise a new frame has been inserted |
| 99 | // and the best frame to decode next will be selected again. |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 100 | if (!frame_inserted_event_.Wait(wait_ms)) { |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 101 | rtc::CritScope lock(&crit_); |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 102 | if (next_frame_it != frames_.end()) { |
| 103 | int64_t received_timestamp = next_frame_it->second->ReceivedTime(); |
| 104 | uint32_t timestamp = next_frame_it->second->Timestamp(); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 105 | |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 106 | int64_t frame_delay; |
| 107 | if (inter_frame_delay_.CalculateDelay(timestamp, &frame_delay, |
| 108 | received_timestamp)) { |
| 109 | jitter_estimator_->UpdateEstimate(frame_delay, |
| 110 | next_frame_it->second->size); |
| 111 | } |
| 112 | float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0; |
| 113 | timing_->SetJitterDelay(jitter_estimator_->GetJitterEstimate(rtt_mult)); |
| 114 | timing_->UpdateCurrentDelay(next_frame_it->second->RenderTime(), |
| 115 | clock_->TimeInMilliseconds()); |
| 116 | |
| 117 | decoded_frames_.insert(next_frame_it->first); |
| 118 | std::unique_ptr<FrameObject> frame = std::move(next_frame_it->second); |
| 119 | frames_.erase(frames_.begin(), ++next_frame_it); |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame^] | 120 | *frame_out = std::move(frame); |
| 121 | return kFrameFound; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 122 | } else { |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame^] | 123 | return kTimeout; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 129 | void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) { |
| 130 | rtc::CritScope lock(&crit_); |
| 131 | protection_mode_ = mode; |
| 132 | } |
| 133 | |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 134 | void FrameBuffer::Start() { |
| 135 | rtc::CritScope lock(&crit_); |
| 136 | stopped_ = false; |
| 137 | } |
| 138 | |
| 139 | void FrameBuffer::Stop() { |
| 140 | rtc::CritScope lock(&crit_); |
| 141 | stopped_ = true; |
| 142 | frame_inserted_event_.Set(); |
| 143 | } |
| 144 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 145 | void FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) { |
| 146 | rtc::CritScope lock(&crit_); |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 147 | // If |newest_picture_id_| is -1 then this is the first frame we received. |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 148 | if (newest_picture_id_ == -1) |
| 149 | newest_picture_id_ = frame->picture_id; |
| 150 | |
| 151 | if (AheadOf<uint16_t>(frame->picture_id, newest_picture_id_)) |
| 152 | newest_picture_id_ = frame->picture_id; |
| 153 | |
| 154 | // Remove frames as long as we have too many, |kMaxNumHistoryFrames|. |
| 155 | while (decoded_frames_.size() > kMaxNumHistoryFrames) |
| 156 | decoded_frames_.erase(decoded_frames_.begin()); |
| 157 | |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 158 | // Remove frames that are too old. |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 159 | uint16_t old_picture_id = Subtract<1 << 16>(newest_picture_id_, kMaxFrameAge); |
| 160 | auto old_decoded_it = |
| 161 | decoded_frames_.lower_bound(FrameKey(old_picture_id, 0)); |
| 162 | decoded_frames_.erase(decoded_frames_.begin(), old_decoded_it); |
| 163 | |
| 164 | FrameKey key(frame->picture_id, frame->spatial_layer); |
| 165 | frames_[key] = std::move(frame); |
| 166 | frame_inserted_event_.Set(); |
| 167 | } |
| 168 | |
| 169 | bool FrameBuffer::IsContinuous(const FrameObject& frame) const { |
| 170 | // If a frame with an earlier picture id was inserted compared to the last |
| 171 | // decoded frames picture id then that frame arrived too late. |
| 172 | if (!decoded_frames_.empty() && |
| 173 | AheadOf(decoded_frames_.rbegin()->first, frame.picture_id)) { |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | // Have we decoded all frames that this frame depend on? |
| 178 | for (size_t r = 0; r < frame.num_references; ++r) { |
| 179 | FrameKey ref_key(frame.references[r], frame.spatial_layer); |
| 180 | if (decoded_frames_.find(ref_key) == decoded_frames_.end()) |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | // If this is a layer frame, have we decoded the lower layer of this |
| 185 | // super frame. |
| 186 | if (frame.inter_layer_predicted) { |
| 187 | RTC_DCHECK_GT(frame.spatial_layer, 0); |
| 188 | FrameKey ref_key(frame.picture_id, frame.spatial_layer - 1); |
| 189 | if (decoded_frames_.find(ref_key) == decoded_frames_.end()) |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | } // namespace video_coding |
| 197 | } // namespace webrtc |