blob: dab783254684e2ff140ed598bd3fdc022f9dbd57 [file] [log] [blame]
philipelbe7a9e52016-05-19 12:19:35 +02001/*
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
22namespace webrtc {
23namespace video_coding {
24
25namespace {
26// The maximum age of decoded frames tracked by frame buffer, compared to
27// |newest_picture_id_|.
28constexpr int kMaxFrameAge = 4096;
29
30// The maximum number of decoded frames being tracked by the frame buffer.
31constexpr int kMaxNumHistoryFrames = 256;
32} // namespace
33
34bool 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
43FrameBuffer::FrameBuffer(Clock* clock,
44 VCMJitterEstimator* jitter_estimator,
philipel4f6cd6a2016-08-03 10:59:32 +020045 VCMTiming* timing)
philipelbe7a9e52016-05-19 12:19:35 +020046 : clock_(clock),
47 frame_inserted_event_(false, false),
48 jitter_estimator_(jitter_estimator),
49 timing_(timing),
philipel4f6cd6a2016-08-03 10:59:32 +020050 inter_frame_delay_(clock_->TimeInMilliseconds()),
philipel504c47d2016-06-30 17:33:02 +020051 newest_picture_id_(-1),
philipel4f6cd6a2016-08-03 10:59:32 +020052 stopped_(false),
53 protection_mode_(kProtectionNack) {}
philipelbe7a9e52016-05-19 12:19:35 +020054
55std::unique_ptr<FrameObject> FrameBuffer::NextFrame(int64_t max_wait_time_ms) {
56 int64_t latest_return_time = clock_->TimeInMilliseconds() + max_wait_time_ms;
philipel504c47d2016-06-30 17:33:02 +020057 int64_t now = clock_->TimeInMilliseconds();
58 int64_t wait_ms = max_wait_time_ms;
philipelbe7a9e52016-05-19 12:19:35 +020059 while (true) {
philipel504c47d2016-06-30 17:33:02 +020060 std::map<FrameKey, std::unique_ptr<FrameObject>, FrameComp>::iterator
philipel4f6cd6a2016-08-03 10:59:32 +020061 next_frame_it;
philipel504c47d2016-06-30 17:33:02 +020062 {
63 rtc::CritScope lock(&crit_);
64 frame_inserted_event_.Reset();
65 if (stopped_)
66 return std::unique_ptr<FrameObject>();
philipelbe7a9e52016-05-19 12:19:35 +020067
philipel504c47d2016-06-30 17:33:02 +020068 now = clock_->TimeInMilliseconds();
69 wait_ms = max_wait_time_ms;
philipel4f6cd6a2016-08-03 10:59:32 +020070 next_frame_it = frames_.end();
philipel504c47d2016-06-30 17:33:02 +020071 for (auto frame_it = frames_.begin(); frame_it != frames_.end();
72 ++frame_it) {
73 const FrameObject& frame = *frame_it->second;
74 if (IsContinuous(frame)) {
philipel4f6cd6a2016-08-03 10:59:32 +020075 next_frame_it = frame_it;
76 int64_t render_time =
77 next_frame_it->second->RenderTime() == -1
78 ? timing_->RenderTimeMs(frame.timestamp, now)
79 : next_frame_it->second->RenderTime();
philipel504c47d2016-06-30 17:33:02 +020080 wait_ms = timing_->MaxWaitingTime(render_time, now);
philipel4f6cd6a2016-08-03 10:59:32 +020081 frame_it->second->SetRenderTime(render_time);
philipelbe7a9e52016-05-19 12:19:35 +020082
philipel504c47d2016-06-30 17:33:02 +020083 // This will cause the frame buffer to prefer high framerate rather
84 // than high resolution in the case of the decoder not decoding fast
85 // enough and the stream has multiple spatial and temporal layers.
86 if (wait_ms == 0)
87 continue;
philipelbe7a9e52016-05-19 12:19:35 +020088
philipel504c47d2016-06-30 17:33:02 +020089 break;
90 }
philipelbe7a9e52016-05-19 12:19:35 +020091 }
92 }
philipelbe7a9e52016-05-19 12:19:35 +020093
philipelbe7a9e52016-05-19 12:19:35 +020094 wait_ms = std::min<int64_t>(wait_ms, latest_return_time - now);
95 wait_ms = std::max<int64_t>(wait_ms, 0);
philipel4f6cd6a2016-08-03 10:59:32 +020096 // If the timeout occurs, return. Otherwise a new frame has been inserted
97 // and the best frame to decode next will be selected again.
philipelbe7a9e52016-05-19 12:19:35 +020098 if (!frame_inserted_event_.Wait(wait_ms)) {
philipel504c47d2016-06-30 17:33:02 +020099 rtc::CritScope lock(&crit_);
philipel4f6cd6a2016-08-03 10:59:32 +0200100 if (next_frame_it != frames_.end()) {
101 int64_t received_timestamp = next_frame_it->second->ReceivedTime();
102 uint32_t timestamp = next_frame_it->second->Timestamp();
philipelbe7a9e52016-05-19 12:19:35 +0200103
philipel4f6cd6a2016-08-03 10:59:32 +0200104 int64_t frame_delay;
105 if (inter_frame_delay_.CalculateDelay(timestamp, &frame_delay,
106 received_timestamp)) {
107 jitter_estimator_->UpdateEstimate(frame_delay,
108 next_frame_it->second->size);
109 }
110 float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0;
111 timing_->SetJitterDelay(jitter_estimator_->GetJitterEstimate(rtt_mult));
112 timing_->UpdateCurrentDelay(next_frame_it->second->RenderTime(),
113 clock_->TimeInMilliseconds());
114
115 decoded_frames_.insert(next_frame_it->first);
116 std::unique_ptr<FrameObject> frame = std::move(next_frame_it->second);
117 frames_.erase(frames_.begin(), ++next_frame_it);
philipelbe7a9e52016-05-19 12:19:35 +0200118 return frame;
119 } else {
philipelbe7a9e52016-05-19 12:19:35 +0200120 return std::unique_ptr<FrameObject>();
121 }
122 }
123 }
124}
125
philipel4f6cd6a2016-08-03 10:59:32 +0200126void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) {
127 rtc::CritScope lock(&crit_);
128 protection_mode_ = mode;
129}
130
philipel504c47d2016-06-30 17:33:02 +0200131void FrameBuffer::Start() {
132 rtc::CritScope lock(&crit_);
133 stopped_ = false;
134}
135
136void FrameBuffer::Stop() {
137 rtc::CritScope lock(&crit_);
138 stopped_ = true;
139 frame_inserted_event_.Set();
140}
141
philipelbe7a9e52016-05-19 12:19:35 +0200142void FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) {
143 rtc::CritScope lock(&crit_);
philipel4f6cd6a2016-08-03 10:59:32 +0200144 // If |newest_picture_id_| is -1 then this is the first frame we received.
philipelbe7a9e52016-05-19 12:19:35 +0200145 if (newest_picture_id_ == -1)
146 newest_picture_id_ = frame->picture_id;
147
148 if (AheadOf<uint16_t>(frame->picture_id, newest_picture_id_))
149 newest_picture_id_ = frame->picture_id;
150
151 // Remove frames as long as we have too many, |kMaxNumHistoryFrames|.
152 while (decoded_frames_.size() > kMaxNumHistoryFrames)
153 decoded_frames_.erase(decoded_frames_.begin());
154
philipel4f6cd6a2016-08-03 10:59:32 +0200155 // Remove frames that are too old.
philipelbe7a9e52016-05-19 12:19:35 +0200156 uint16_t old_picture_id = Subtract<1 << 16>(newest_picture_id_, kMaxFrameAge);
157 auto old_decoded_it =
158 decoded_frames_.lower_bound(FrameKey(old_picture_id, 0));
159 decoded_frames_.erase(decoded_frames_.begin(), old_decoded_it);
160
161 FrameKey key(frame->picture_id, frame->spatial_layer);
162 frames_[key] = std::move(frame);
163 frame_inserted_event_.Set();
164}
165
166bool FrameBuffer::IsContinuous(const FrameObject& frame) const {
167 // If a frame with an earlier picture id was inserted compared to the last
168 // decoded frames picture id then that frame arrived too late.
169 if (!decoded_frames_.empty() &&
170 AheadOf(decoded_frames_.rbegin()->first, frame.picture_id)) {
171 return false;
172 }
173
174 // Have we decoded all frames that this frame depend on?
175 for (size_t r = 0; r < frame.num_references; ++r) {
176 FrameKey ref_key(frame.references[r], frame.spatial_layer);
177 if (decoded_frames_.find(ref_key) == decoded_frames_.end())
178 return false;
179 }
180
181 // If this is a layer frame, have we decoded the lower layer of this
182 // super frame.
183 if (frame.inter_layer_predicted) {
184 RTC_DCHECK_GT(frame.spatial_layer, 0);
185 FrameKey ref_key(frame.picture_id, frame.spatial_layer - 1);
186 if (decoded_frames_.find(ref_key) == decoded_frames_.end())
187 return false;
188 }
189
190 return true;
191}
192
193} // namespace video_coding
194} // namespace webrtc