blob: 766e6a05d722ddc2811f47763d9a1f49711093ec [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
philipel75562822016-09-05 10:57:41 +020055FrameBuffer::ReturnReason FrameBuffer::NextFrame(
56 int64_t max_wait_time_ms,
57 std::unique_ptr<FrameObject>* frame_out) {
philipelbe7a9e52016-05-19 12:19:35 +020058 int64_t latest_return_time = clock_->TimeInMilliseconds() + max_wait_time_ms;
philipel504c47d2016-06-30 17:33:02 +020059 int64_t now = clock_->TimeInMilliseconds();
60 int64_t wait_ms = max_wait_time_ms;
philipelbe7a9e52016-05-19 12:19:35 +020061 while (true) {
philipel504c47d2016-06-30 17:33:02 +020062 std::map<FrameKey, std::unique_ptr<FrameObject>, FrameComp>::iterator
philipel4f6cd6a2016-08-03 10:59:32 +020063 next_frame_it;
philipel504c47d2016-06-30 17:33:02 +020064 {
65 rtc::CritScope lock(&crit_);
66 frame_inserted_event_.Reset();
67 if (stopped_)
philipel75562822016-09-05 10:57:41 +020068 return kStopped;
philipelbe7a9e52016-05-19 12:19:35 +020069
philipel504c47d2016-06-30 17:33:02 +020070 now = clock_->TimeInMilliseconds();
71 wait_ms = max_wait_time_ms;
philipel4f6cd6a2016-08-03 10:59:32 +020072 next_frame_it = frames_.end();
philipel504c47d2016-06-30 17:33:02 +020073 for (auto frame_it = frames_.begin(); frame_it != frames_.end();
74 ++frame_it) {
75 const FrameObject& frame = *frame_it->second;
76 if (IsContinuous(frame)) {
philipel4f6cd6a2016-08-03 10:59:32 +020077 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();
philipel504c47d2016-06-30 17:33:02 +020082 wait_ms = timing_->MaxWaitingTime(render_time, now);
philipel4f6cd6a2016-08-03 10:59:32 +020083 frame_it->second->SetRenderTime(render_time);
philipelbe7a9e52016-05-19 12:19:35 +020084
philipel504c47d2016-06-30 17:33:02 +020085 // 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;
philipelbe7a9e52016-05-19 12:19:35 +020090
philipel504c47d2016-06-30 17:33:02 +020091 break;
92 }
philipelbe7a9e52016-05-19 12:19:35 +020093 }
94 }
philipelbe7a9e52016-05-19 12:19:35 +020095
philipelbe7a9e52016-05-19 12:19:35 +020096 wait_ms = std::min<int64_t>(wait_ms, latest_return_time - now);
97 wait_ms = std::max<int64_t>(wait_ms, 0);
philipel4f6cd6a2016-08-03 10:59:32 +020098 // If the timeout occurs, return. Otherwise a new frame has been inserted
99 // and the best frame to decode next will be selected again.
philipelbe7a9e52016-05-19 12:19:35 +0200100 if (!frame_inserted_event_.Wait(wait_ms)) {
philipel504c47d2016-06-30 17:33:02 +0200101 rtc::CritScope lock(&crit_);
philipel4f6cd6a2016-08-03 10:59:32 +0200102 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();
philipelbe7a9e52016-05-19 12:19:35 +0200105
philipel4f6cd6a2016-08-03 10:59:32 +0200106 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);
philipel75562822016-09-05 10:57:41 +0200120 *frame_out = std::move(frame);
121 return kFrameFound;
philipelbe7a9e52016-05-19 12:19:35 +0200122 } else {
philipel75562822016-09-05 10:57:41 +0200123 return kTimeout;
philipelbe7a9e52016-05-19 12:19:35 +0200124 }
125 }
126 }
127}
128
philipel4f6cd6a2016-08-03 10:59:32 +0200129void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) {
130 rtc::CritScope lock(&crit_);
131 protection_mode_ = mode;
132}
133
philipel504c47d2016-06-30 17:33:02 +0200134void FrameBuffer::Start() {
135 rtc::CritScope lock(&crit_);
136 stopped_ = false;
137}
138
139void FrameBuffer::Stop() {
140 rtc::CritScope lock(&crit_);
141 stopped_ = true;
142 frame_inserted_event_.Set();
143}
144
philipelbe7a9e52016-05-19 12:19:35 +0200145void FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) {
146 rtc::CritScope lock(&crit_);
philipel4f6cd6a2016-08-03 10:59:32 +0200147 // If |newest_picture_id_| is -1 then this is the first frame we received.
philipelbe7a9e52016-05-19 12:19:35 +0200148 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
philipel4f6cd6a2016-08-03 10:59:32 +0200158 // Remove frames that are too old.
philipelbe7a9e52016-05-19 12:19:35 +0200159 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
169bool 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