| OLD | NEW |
| (Empty) |
| 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, | |
| 45 const VCMTiming* timing) | |
| 46 : clock_(clock), | |
| 47 frame_inserted_event_(false, false), | |
| 48 jitter_estimator_(jitter_estimator), | |
| 49 timing_(timing), | |
| 50 newest_picture_id_(-1) {} | |
| 51 | |
| 52 std::unique_ptr<FrameObject> FrameBuffer::NextFrame(int64_t max_wait_time_ms) { | |
| 53 int64_t latest_return_time = clock_->TimeInMilliseconds() + max_wait_time_ms; | |
| 54 while (true) { | |
| 55 int64_t now = clock_->TimeInMilliseconds(); | |
| 56 int64_t wait_ms = max_wait_time_ms; | |
| 57 | |
| 58 crit_.Enter(); | |
| 59 frame_inserted_event_.Reset(); | |
| 60 auto next_frame = frames_.end(); | |
| 61 for (auto frame_it = frames_.begin(); frame_it != frames_.end(); | |
| 62 ++frame_it) { | |
| 63 const FrameObject& frame = *frame_it->second; | |
| 64 if (IsContinuous(frame)) { | |
| 65 next_frame = frame_it; | |
| 66 int64_t render_time = timing_->RenderTimeMs(frame.timestamp, now); | |
| 67 wait_ms = timing_->MaxWaitingTime(render_time, now); | |
| 68 | |
| 69 // This will cause the frame buffer to prefer high framerate rather | |
| 70 // than high resolution in the case of the decoder not decoding fast | |
| 71 // enough and the stream has multiple spatial and temporal layers. | |
| 72 if (wait_ms == 0) | |
| 73 continue; | |
| 74 | |
| 75 break; | |
| 76 } | |
| 77 } | |
| 78 crit_.Leave(); | |
| 79 | |
| 80 // If the timout occures, return. Otherwise a new frame has been inserted | |
| 81 // and the best frame to decode next will be selected again. | |
| 82 wait_ms = std::min<int64_t>(wait_ms, latest_return_time - now); | |
| 83 wait_ms = std::max<int64_t>(wait_ms, 0); | |
| 84 if (!frame_inserted_event_.Wait(wait_ms)) { | |
| 85 crit_.Enter(); | |
| 86 if (next_frame != frames_.end()) { | |
| 87 // TODO(philipel): update jitter estimator with correct values. | |
| 88 jitter_estimator_->UpdateEstimate(100, 100); | |
| 89 | |
| 90 decoded_frames_.insert(next_frame->first); | |
| 91 std::unique_ptr<FrameObject> frame = std::move(next_frame->second); | |
| 92 frames_.erase(frames_.begin(), ++next_frame); | |
| 93 crit_.Leave(); | |
| 94 return frame; | |
| 95 } else { | |
| 96 crit_.Leave(); | |
| 97 return std::unique_ptr<FrameObject>(); | |
| 98 } | |
| 99 } | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 void FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) { | |
| 104 rtc::CritScope lock(&crit_); | |
| 105 if (newest_picture_id_ == -1) | |
| 106 newest_picture_id_ = frame->picture_id; | |
| 107 | |
| 108 if (AheadOf<uint16_t>(frame->picture_id, newest_picture_id_)) | |
| 109 newest_picture_id_ = frame->picture_id; | |
| 110 | |
| 111 // Remove frames as long as we have too many, |kMaxNumHistoryFrames|. | |
| 112 while (decoded_frames_.size() > kMaxNumHistoryFrames) | |
| 113 decoded_frames_.erase(decoded_frames_.begin()); | |
| 114 | |
| 115 // Remove frames that are too old, |kMaxNumHistoryFrames|. | |
| 116 uint16_t old_picture_id = Subtract<1 << 16>(newest_picture_id_, kMaxFrameAge); | |
| 117 auto old_decoded_it = | |
| 118 decoded_frames_.lower_bound(FrameKey(old_picture_id, 0)); | |
| 119 decoded_frames_.erase(decoded_frames_.begin(), old_decoded_it); | |
| 120 | |
| 121 FrameKey key(frame->picture_id, frame->spatial_layer); | |
| 122 frames_[key] = std::move(frame); | |
| 123 frame_inserted_event_.Set(); | |
| 124 } | |
| 125 | |
| 126 bool FrameBuffer::IsContinuous(const FrameObject& frame) const { | |
| 127 // If a frame with an earlier picture id was inserted compared to the last | |
| 128 // decoded frames picture id then that frame arrived too late. | |
| 129 if (!decoded_frames_.empty() && | |
| 130 AheadOf(decoded_frames_.rbegin()->first, frame.picture_id)) { | |
| 131 return false; | |
| 132 } | |
| 133 | |
| 134 // Have we decoded all frames that this frame depend on? | |
| 135 for (size_t r = 0; r < frame.num_references; ++r) { | |
| 136 FrameKey ref_key(frame.references[r], frame.spatial_layer); | |
| 137 if (decoded_frames_.find(ref_key) == decoded_frames_.end()) | |
| 138 return false; | |
| 139 } | |
| 140 | |
| 141 // If this is a layer frame, have we decoded the lower layer of this | |
| 142 // super frame. | |
| 143 if (frame.inter_layer_predicted) { | |
| 144 RTC_DCHECK_GT(frame.spatial_layer, 0); | |
| 145 FrameKey ref_key(frame.picture_id, frame.spatial_layer - 1); | |
| 146 if (decoded_frames_.find(ref_key) == decoded_frames_.end()) | |
| 147 return false; | |
| 148 } | |
| 149 | |
| 150 return true; | |
| 151 } | |
| 152 | |
| 153 } // namespace video_coding | |
| 154 } // namespace webrtc | |
| OLD | NEW |