blob: 907b8b0dbc58f25271fdaa5a82edcda06018b044 [file] [log] [blame]
philipel2fee4d62018-03-21 16:52:13 +01001/*
2 * Copyright (c) 2018 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 "video/video_stream_decoder_impl.h"
12
Mirko Bonadei317a1f02019-09-17 17:06:18 +020013#include <memory>
14
Danil Chapovalov471783f2019-03-11 14:26:02 +010015#include "api/task_queue/queued_task.h"
philipel97187112018-03-23 10:43:21 +010016#include "rtc_base/logging.h"
philipel844876d2018-04-05 11:02:54 +020017#include "rtc_base/numerics/mod_ops.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/time_utils.h"
philipel2fee4d62018-03-21 16:52:13 +010019
20namespace webrtc {
philipel97187112018-03-23 10:43:21 +010021
philipel2fee4d62018-03-21 16:52:13 +010022VideoStreamDecoderImpl::VideoStreamDecoderImpl(
Danil Chapovalovb703db92019-04-08 16:59:28 +020023 VideoStreamDecoderInterface::Callbacks* callbacks,
philipel2fee4d62018-03-21 16:52:13 +010024 VideoDecoderFactory* decoder_factory,
Danil Chapovalovb703db92019-04-08 16:59:28 +020025 TaskQueueFactory* task_queue_factory,
philipel2fee4d62018-03-21 16:52:13 +010026 std::map<int, std::pair<SdpVideoFormat, int>> decoder_settings)
philipel539f9b32020-01-09 16:12:25 +010027 : timing_(Clock::GetRealTimeClock()),
28 decode_callbacks_(this),
philipel3dc47802020-09-10 15:30:02 +020029 next_frame_info_index_(0),
philipel539f9b32020-01-09 16:12:25 +010030 callbacks_(callbacks),
31 keyframe_required_(true),
philipel2fee4d62018-03-21 16:52:13 +010032 decoder_factory_(decoder_factory),
philipel97187112018-03-23 10:43:21 +010033 decoder_settings_(std::move(decoder_settings)),
philipel539f9b32020-01-09 16:12:25 +010034 shut_down_(false),
35 frame_buffer_(Clock::GetRealTimeClock(), &timing_, nullptr),
Danil Chapovalovb703db92019-04-08 16:59:28 +020036 bookkeeping_queue_(task_queue_factory->CreateTaskQueue(
37 "video_stream_decoder_bookkeeping_queue",
38 TaskQueueFactory::Priority::NORMAL)),
philipel539f9b32020-01-09 16:12:25 +010039 decode_queue_(task_queue_factory->CreateTaskQueue(
40 "video_stream_decoder_decode_queue",
41 TaskQueueFactory::Priority::NORMAL)) {
philipel539f9b32020-01-09 16:12:25 +010042 bookkeeping_queue_.PostTask([this]() {
43 RTC_DCHECK_RUN_ON(&bookkeeping_queue_);
44 StartNextDecode();
45 });
philipel844876d2018-04-05 11:02:54 +020046}
philipel2fee4d62018-03-21 16:52:13 +010047
philipel97187112018-03-23 10:43:21 +010048VideoStreamDecoderImpl::~VideoStreamDecoderImpl() {
Markus Handell265931e2020-07-10 12:23:48 +020049 MutexLock lock(&shut_down_mutex_);
philipel539f9b32020-01-09 16:12:25 +010050 shut_down_ = true;
philipel97187112018-03-23 10:43:21 +010051}
philipel2fee4d62018-03-21 16:52:13 +010052
philipelca188092021-03-23 12:00:49 +010053void VideoStreamDecoderImpl::OnFrame(std::unique_ptr<EncodedFrame> frame) {
philipel97187112018-03-23 10:43:21 +010054 if (!bookkeeping_queue_.IsCurrent()) {
philipel539f9b32020-01-09 16:12:25 +010055 bookkeeping_queue_.PostTask([this, frame = std::move(frame)]() mutable {
56 OnFrame(std::move(frame));
57 return true;
58 });
philipel97187112018-03-23 10:43:21 +010059
philipel97187112018-03-23 10:43:21 +010060 return;
61 }
62
63 RTC_DCHECK_RUN_ON(&bookkeeping_queue_);
64
philipel9aa9b8d2021-02-15 13:31:29 +010065 int64_t continuous_frame_id = frame_buffer_.InsertFrame(std::move(frame));
66 if (last_continuous_frame_id_ < continuous_frame_id) {
67 last_continuous_frame_id_ = continuous_frame_id;
68 callbacks_->OnContinuousUntil(last_continuous_frame_id_);
philipel97187112018-03-23 10:43:21 +010069 }
70}
71
philipel781653c2019-06-04 17:10:37 +020072void VideoStreamDecoderImpl::SetMinPlayoutDelay(TimeDelta min_delay) {
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010073 timing_.set_min_playout_delay(min_delay);
philipel781653c2019-06-04 17:10:37 +020074}
75
76void VideoStreamDecoderImpl::SetMaxPlayoutDelay(TimeDelta max_delay) {
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010077 timing_.set_max_playout_delay(max_delay);
philipel781653c2019-06-04 17:10:37 +020078}
79
philipel79aab3f2018-03-26 14:31:23 +020080VideoDecoder* VideoStreamDecoderImpl::GetDecoder(int payload_type) {
81 if (current_payload_type_ == payload_type) {
82 RTC_DCHECK(decoder_);
83 return decoder_.get();
84 }
85
86 current_payload_type_.reset();
87 decoder_.reset();
88
89 auto decoder_settings_it = decoder_settings_.find(payload_type);
90 if (decoder_settings_it == decoder_settings_.end()) {
91 RTC_LOG(LS_WARNING) << "Payload type " << payload_type
92 << " not registered.";
93 return nullptr;
94 }
95
96 const SdpVideoFormat& video_format = decoder_settings_it->second.first;
97 std::unique_ptr<VideoDecoder> decoder =
98 decoder_factory_->CreateVideoDecoder(video_format);
99 if (!decoder) {
100 RTC_LOG(LS_WARNING) << "Failed to create decoder for payload type "
101 << payload_type << ".";
102 return nullptr;
103 }
104
Danil Chapovalovb6f19d72021-08-18 13:34:36 +0000105 VideoDecoder::Settings settings;
106 settings.set_number_of_cores(decoder_settings_it->second.second);
107 if (!decoder->Configure(settings)) {
philipel79aab3f2018-03-26 14:31:23 +0200108 RTC_LOG(LS_WARNING) << "Failed to initialize decoder for payload type "
109 << payload_type << ".";
110 return nullptr;
111 }
112
philipel539f9b32020-01-09 16:12:25 +0100113 int32_t register_result =
114 decoder->RegisterDecodeCompleteCallback(&decode_callbacks_);
philipel79aab3f2018-03-26 14:31:23 +0200115 if (register_result != WEBRTC_VIDEO_CODEC_OK) {
116 RTC_LOG(LS_WARNING) << "Failed to register decode callback.";
117 return nullptr;
118 }
119
120 current_payload_type_.emplace(payload_type);
121 decoder_ = std::move(decoder);
122 return decoder_.get();
123}
124
philipelca188092021-03-23 12:00:49 +0100125void VideoStreamDecoderImpl::SaveFrameInfo(const EncodedFrame& frame) {
philipel3dc47802020-09-10 15:30:02 +0200126 FrameInfo* frame_info = &frame_info_[next_frame_info_index_];
127 frame_info->timestamp = frame.Timestamp();
128 frame_info->decode_start_time_ms = rtc::TimeMillis();
129 frame_info->render_time_us = frame.RenderTimeMs() * 1000;
130 frame_info->content_type = frame.EncodedImage().content_type_;
philipel844876d2018-04-05 11:02:54 +0200131
philipel3dc47802020-09-10 15:30:02 +0200132 next_frame_info_index_ = Add<kFrameInfoMemory>(next_frame_info_index_, 1);
philipel539f9b32020-01-09 16:12:25 +0100133}
philipel844876d2018-04-05 11:02:54 +0200134
philipel539f9b32020-01-09 16:12:25 +0100135void VideoStreamDecoderImpl::StartNextDecode() {
136 int64_t max_wait_time = keyframe_required_ ? 200 : 3000;
137
Evan Shrubsole3d29efd2021-12-07 14:11:45 +0100138 frame_buffer_.NextFrame(max_wait_time, keyframe_required_,
139 &bookkeeping_queue_,
140 [this](std::unique_ptr<EncodedFrame> frame) {
141 RTC_DCHECK_RUN_ON(&bookkeeping_queue_);
142 OnNextFrameCallback(std::move(frame));
143 });
philipel539f9b32020-01-09 16:12:25 +0100144}
145
146void VideoStreamDecoderImpl::OnNextFrameCallback(
Evan Shrubsole3d29efd2021-12-07 14:11:45 +0100147 std::unique_ptr<EncodedFrame> frame) {
148 if (frame) {
149 RTC_DCHECK(frame);
150 SaveFrameInfo(*frame);
philipel539f9b32020-01-09 16:12:25 +0100151
Evan Shrubsole3d29efd2021-12-07 14:11:45 +0100152 MutexLock lock(&shut_down_mutex_);
153 if (shut_down_) {
154 return;
philipel539f9b32020-01-09 16:12:25 +0100155 }
Evan Shrubsole3d29efd2021-12-07 14:11:45 +0100156
157 decode_queue_.PostTask([this, frame = std::move(frame)]() mutable {
158 RTC_DCHECK_RUN_ON(&decode_queue_);
159 DecodeResult decode_result = DecodeFrame(std::move(frame));
160 bookkeeping_queue_.PostTask([this, decode_result]() {
philipel539f9b32020-01-09 16:12:25 +0100161 RTC_DCHECK_RUN_ON(&bookkeeping_queue_);
Evan Shrubsole3d29efd2021-12-07 14:11:45 +0100162 switch (decode_result) {
163 case kOk: {
164 keyframe_required_ = false;
165 break;
166 }
167 case kOkRequestKeyframe: {
168 callbacks_->OnNonDecodableState();
169 keyframe_required_ = false;
170 break;
171 }
172 case kDecodeFailure: {
173 callbacks_->OnNonDecodableState();
174 keyframe_required_ = true;
175 break;
176 }
177 }
philipel539f9b32020-01-09 16:12:25 +0100178 StartNextDecode();
179 });
Evan Shrubsole3d29efd2021-12-07 14:11:45 +0100180 });
181 } else {
182 callbacks_->OnNonDecodableState();
183 // The `frame_buffer_` requires the frame callback function to complete
184 // before NextFrame is called again. For this reason we call
185 // StartNextDecode in a later task to allow this task to complete first.
186 bookkeeping_queue_.PostTask([this]() {
187 RTC_DCHECK_RUN_ON(&bookkeeping_queue_);
188 StartNextDecode();
189 });
philipel844876d2018-04-05 11:02:54 +0200190 }
191}
192
philipel539f9b32020-01-09 16:12:25 +0100193VideoStreamDecoderImpl::DecodeResult VideoStreamDecoderImpl::DecodeFrame(
philipelca188092021-03-23 12:00:49 +0100194 std::unique_ptr<EncodedFrame> frame) {
philipel539f9b32020-01-09 16:12:25 +0100195 RTC_DCHECK(frame);
philipel844876d2018-04-05 11:02:54 +0200196
philipel539f9b32020-01-09 16:12:25 +0100197 VideoDecoder* decoder = GetDecoder(frame->PayloadType());
198 if (!decoder) {
199 return kDecodeFailure;
philipel844876d2018-04-05 11:02:54 +0200200 }
201
philipel539f9b32020-01-09 16:12:25 +0100202 int32_t decode_result = decoder->Decode(frame->EncodedImage(), //
203 /*missing_frames=*/false, //
204 frame->RenderTimeMs());
205 switch (decode_result) {
206 case WEBRTC_VIDEO_CODEC_OK: {
207 return kOk;
208 }
209 case WEBRTC_VIDEO_CODEC_OK_REQUEST_KEYFRAME: {
210 return kOkRequestKeyframe;
211 }
212 default:
213 return kDecodeFailure;
214 }
philipel844876d2018-04-05 11:02:54 +0200215}
216
philipel3dc47802020-09-10 15:30:02 +0200217VideoStreamDecoderImpl::FrameInfo* VideoStreamDecoderImpl::GetFrameInfo(
218 int64_t timestamp) {
219 int start_time_index = next_frame_info_index_;
220 for (int i = 0; i < kFrameInfoMemory; ++i) {
221 start_time_index = Subtract<kFrameInfoMemory>(start_time_index, 1);
philipel6847f9b2018-04-20 15:05:37 +0200222
philipel3dc47802020-09-10 15:30:02 +0200223 if (frame_info_[start_time_index].timestamp == timestamp)
224 return &frame_info_[start_time_index];
philipel6847f9b2018-04-20 15:05:37 +0200225 }
226
227 return nullptr;
228}
229
philipel539f9b32020-01-09 16:12:25 +0100230void VideoStreamDecoderImpl::OnDecodedFrameCallback(
231 VideoFrame& decoded_image,
232 absl::optional<int32_t> decode_time_ms,
233 absl::optional<uint8_t> qp) {
philipel6847f9b2018-04-20 15:05:37 +0200234 int64_t decode_stop_time_ms = rtc::TimeMillis();
235
236 bookkeeping_queue_.PostTask([this, decode_stop_time_ms, decoded_image,
philipel85ddb232020-10-02 14:46:14 +0200237 decode_time_ms, qp]() mutable {
philipel6847f9b2018-04-20 15:05:37 +0200238 RTC_DCHECK_RUN_ON(&bookkeeping_queue_);
239
philipel3dc47802020-09-10 15:30:02 +0200240 FrameInfo* frame_info = GetFrameInfo(decoded_image.timestamp());
241 if (!frame_info) {
philipel6847f9b2018-04-20 15:05:37 +0200242 RTC_LOG(LS_ERROR) << "No frame information found for frame with timestamp"
243 << decoded_image.timestamp();
244 return;
245 }
246
philipel3dc47802020-09-10 15:30:02 +0200247 Callbacks::FrameInfo callback_info;
248 callback_info.content_type = frame_info->content_type;
249
philipel6847f9b2018-04-20 15:05:37 +0200250 if (qp)
philipel3dc47802020-09-10 15:30:02 +0200251 callback_info.qp.emplace(*qp);
philipel6847f9b2018-04-20 15:05:37 +0200252
philipel85ddb232020-10-02 14:46:14 +0200253 if (!decode_time_ms) {
254 decode_time_ms = decode_stop_time_ms - frame_info->decode_start_time_ms;
255 }
256 decoded_image.set_processing_time(
257 {Timestamp::Millis(frame_info->decode_start_time_ms),
258 Timestamp::Millis(frame_info->decode_start_time_ms +
259 *decode_time_ms)});
260 decoded_image.set_timestamp_us(frame_info->render_time_us);
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100261 timing_.StopDecodeTimer(TimeDelta::Millis(*decode_time_ms),
262 Timestamp::Millis(decode_stop_time_ms));
philipel6847f9b2018-04-20 15:05:37 +0200263
philipel85ddb232020-10-02 14:46:14 +0200264 callbacks_->OnDecodedFrame(decoded_image, callback_info);
philipel6847f9b2018-04-20 15:05:37 +0200265 });
266}
267
philipel539f9b32020-01-09 16:12:25 +0100268VideoStreamDecoderImpl::DecodeCallbacks::DecodeCallbacks(
269 VideoStreamDecoderImpl* video_stream_decoder_impl)
270 : video_stream_decoder_impl_(video_stream_decoder_impl) {}
271
272int32_t VideoStreamDecoderImpl::DecodeCallbacks::Decoded(
273 VideoFrame& decoded_image) {
274 Decoded(decoded_image, absl::nullopt, absl::nullopt);
275 return WEBRTC_VIDEO_CODEC_OK;
276}
277
278int32_t VideoStreamDecoderImpl::DecodeCallbacks::Decoded(
279 VideoFrame& decoded_image,
280 int64_t decode_time_ms) {
281 Decoded(decoded_image, decode_time_ms, absl::nullopt);
282 return WEBRTC_VIDEO_CODEC_OK;
283}
284
285void VideoStreamDecoderImpl::DecodeCallbacks::Decoded(
286 VideoFrame& decoded_image,
287 absl::optional<int32_t> decode_time_ms,
288 absl::optional<uint8_t> qp) {
289 video_stream_decoder_impl_->OnDecodedFrameCallback(decoded_image,
290 decode_time_ms, qp);
291}
philipel2fee4d62018-03-21 16:52:13 +0100292} // namespace webrtc