blob: eab1c94983e5286cd63c1a454112c0e7d510cdc4 [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
53void VideoStreamDecoderImpl::OnFrame(
philipel97187112018-03-23 10:43:21 +010054 std::unique_ptr<video_coding::EncodedFrame> frame) {
55 if (!bookkeeping_queue_.IsCurrent()) {
philipel539f9b32020-01-09 16:12:25 +010056 bookkeeping_queue_.PostTask([this, frame = std::move(frame)]() mutable {
57 OnFrame(std::move(frame));
58 return true;
59 });
philipel97187112018-03-23 10:43:21 +010060
philipel97187112018-03-23 10:43:21 +010061 return;
62 }
63
64 RTC_DCHECK_RUN_ON(&bookkeeping_queue_);
65
philipel9aa9b8d2021-02-15 13:31:29 +010066 int64_t continuous_frame_id = frame_buffer_.InsertFrame(std::move(frame));
67 if (last_continuous_frame_id_ < continuous_frame_id) {
68 last_continuous_frame_id_ = continuous_frame_id;
69 callbacks_->OnContinuousUntil(last_continuous_frame_id_);
philipel97187112018-03-23 10:43:21 +010070 }
71}
72
philipel781653c2019-06-04 17:10:37 +020073void VideoStreamDecoderImpl::SetMinPlayoutDelay(TimeDelta min_delay) {
74 timing_.set_min_playout_delay(min_delay.ms());
75}
76
77void VideoStreamDecoderImpl::SetMaxPlayoutDelay(TimeDelta max_delay) {
78 timing_.set_max_playout_delay(max_delay.ms());
79}
80
philipel79aab3f2018-03-26 14:31:23 +020081VideoDecoder* VideoStreamDecoderImpl::GetDecoder(int payload_type) {
82 if (current_payload_type_ == payload_type) {
83 RTC_DCHECK(decoder_);
84 return decoder_.get();
85 }
86
87 current_payload_type_.reset();
88 decoder_.reset();
89
90 auto decoder_settings_it = decoder_settings_.find(payload_type);
91 if (decoder_settings_it == decoder_settings_.end()) {
92 RTC_LOG(LS_WARNING) << "Payload type " << payload_type
93 << " not registered.";
94 return nullptr;
95 }
96
97 const SdpVideoFormat& video_format = decoder_settings_it->second.first;
98 std::unique_ptr<VideoDecoder> decoder =
99 decoder_factory_->CreateVideoDecoder(video_format);
100 if (!decoder) {
101 RTC_LOG(LS_WARNING) << "Failed to create decoder for payload type "
102 << payload_type << ".";
103 return nullptr;
104 }
105
106 int num_cores = decoder_settings_it->second.second;
107 int32_t init_result = decoder->InitDecode(nullptr, num_cores);
108 if (init_result != WEBRTC_VIDEO_CODEC_OK) {
109 RTC_LOG(LS_WARNING) << "Failed to initialize decoder for payload type "
110 << payload_type << ".";
111 return nullptr;
112 }
113
philipel539f9b32020-01-09 16:12:25 +0100114 int32_t register_result =
115 decoder->RegisterDecodeCompleteCallback(&decode_callbacks_);
philipel79aab3f2018-03-26 14:31:23 +0200116 if (register_result != WEBRTC_VIDEO_CODEC_OK) {
117 RTC_LOG(LS_WARNING) << "Failed to register decode callback.";
118 return nullptr;
119 }
120
121 current_payload_type_.emplace(payload_type);
122 decoder_ = std::move(decoder);
123 return decoder_.get();
124}
125
philipel3dc47802020-09-10 15:30:02 +0200126void VideoStreamDecoderImpl::SaveFrameInfo(
philipel539f9b32020-01-09 16:12:25 +0100127 const video_coding::EncodedFrame& frame) {
philipel3dc47802020-09-10 15:30:02 +0200128 FrameInfo* frame_info = &frame_info_[next_frame_info_index_];
129 frame_info->timestamp = frame.Timestamp();
130 frame_info->decode_start_time_ms = rtc::TimeMillis();
131 frame_info->render_time_us = frame.RenderTimeMs() * 1000;
132 frame_info->content_type = frame.EncodedImage().content_type_;
philipel844876d2018-04-05 11:02:54 +0200133
philipel3dc47802020-09-10 15:30:02 +0200134 next_frame_info_index_ = Add<kFrameInfoMemory>(next_frame_info_index_, 1);
philipel539f9b32020-01-09 16:12:25 +0100135}
philipel844876d2018-04-05 11:02:54 +0200136
philipel539f9b32020-01-09 16:12:25 +0100137void VideoStreamDecoderImpl::StartNextDecode() {
138 int64_t max_wait_time = keyframe_required_ ? 200 : 3000;
139
140 frame_buffer_.NextFrame(
141 max_wait_time, keyframe_required_, &bookkeeping_queue_,
142 [this](std::unique_ptr<video_coding::EncodedFrame> frame,
143 video_coding::FrameBuffer::ReturnReason res) mutable {
144 RTC_DCHECK_RUN_ON(&bookkeeping_queue_);
145 OnNextFrameCallback(std::move(frame), res);
146 });
147}
148
149void VideoStreamDecoderImpl::OnNextFrameCallback(
150 std::unique_ptr<video_coding::EncodedFrame> frame,
151 video_coding::FrameBuffer::ReturnReason result) {
152 switch (result) {
153 case video_coding::FrameBuffer::kFrameFound: {
154 RTC_DCHECK(frame);
philipel3dc47802020-09-10 15:30:02 +0200155 SaveFrameInfo(*frame);
philipel539f9b32020-01-09 16:12:25 +0100156
Markus Handell265931e2020-07-10 12:23:48 +0200157 MutexLock lock(&shut_down_mutex_);
philipel539f9b32020-01-09 16:12:25 +0100158 if (shut_down_) {
philipel844876d2018-04-05 11:02:54 +0200159 return;
160 }
philipel539f9b32020-01-09 16:12:25 +0100161
162 decode_queue_.PostTask([this, frame = std::move(frame)]() mutable {
163 RTC_DCHECK_RUN_ON(&decode_queue_);
164 DecodeResult decode_result = DecodeFrame(std::move(frame));
165 bookkeeping_queue_.PostTask([this, decode_result]() {
166 RTC_DCHECK_RUN_ON(&bookkeeping_queue_);
167 switch (decode_result) {
168 case kOk: {
169 keyframe_required_ = false;
170 break;
171 }
172 case kOkRequestKeyframe: {
173 callbacks_->OnNonDecodableState();
174 keyframe_required_ = false;
175 break;
176 }
177 case kDecodeFailure: {
178 callbacks_->OnNonDecodableState();
179 keyframe_required_ = true;
180 break;
181 }
182 }
183 StartNextDecode();
184 });
185 });
186 break;
187 }
188 case video_coding::FrameBuffer::kTimeout: {
189 callbacks_->OnNonDecodableState();
190 // The |frame_buffer_| requires the frame callback function to complete
191 // before NextFrame is called again. For this reason we call
192 // StartNextDecode in a later task to allow this task to complete first.
193 bookkeeping_queue_.PostTask([this]() {
194 RTC_DCHECK_RUN_ON(&bookkeeping_queue_);
195 StartNextDecode();
196 });
197 break;
198 }
199 case video_coding::FrameBuffer::kStopped: {
200 // We are shutting down, do nothing.
201 break;
philipel844876d2018-04-05 11:02:54 +0200202 }
203 }
204}
205
philipel539f9b32020-01-09 16:12:25 +0100206VideoStreamDecoderImpl::DecodeResult VideoStreamDecoderImpl::DecodeFrame(
207 std::unique_ptr<video_coding::EncodedFrame> frame) {
208 RTC_DCHECK(frame);
philipel844876d2018-04-05 11:02:54 +0200209
philipel539f9b32020-01-09 16:12:25 +0100210 VideoDecoder* decoder = GetDecoder(frame->PayloadType());
211 if (!decoder) {
212 return kDecodeFailure;
philipel844876d2018-04-05 11:02:54 +0200213 }
214
philipel539f9b32020-01-09 16:12:25 +0100215 int32_t decode_result = decoder->Decode(frame->EncodedImage(), //
216 /*missing_frames=*/false, //
217 frame->RenderTimeMs());
218 switch (decode_result) {
219 case WEBRTC_VIDEO_CODEC_OK: {
220 return kOk;
221 }
222 case WEBRTC_VIDEO_CODEC_OK_REQUEST_KEYFRAME: {
223 return kOkRequestKeyframe;
224 }
225 default:
226 return kDecodeFailure;
227 }
philipel844876d2018-04-05 11:02:54 +0200228}
229
philipel3dc47802020-09-10 15:30:02 +0200230VideoStreamDecoderImpl::FrameInfo* VideoStreamDecoderImpl::GetFrameInfo(
231 int64_t timestamp) {
232 int start_time_index = next_frame_info_index_;
233 for (int i = 0; i < kFrameInfoMemory; ++i) {
234 start_time_index = Subtract<kFrameInfoMemory>(start_time_index, 1);
philipel6847f9b2018-04-20 15:05:37 +0200235
philipel3dc47802020-09-10 15:30:02 +0200236 if (frame_info_[start_time_index].timestamp == timestamp)
237 return &frame_info_[start_time_index];
philipel6847f9b2018-04-20 15:05:37 +0200238 }
239
240 return nullptr;
241}
242
philipel539f9b32020-01-09 16:12:25 +0100243void VideoStreamDecoderImpl::OnDecodedFrameCallback(
244 VideoFrame& decoded_image,
245 absl::optional<int32_t> decode_time_ms,
246 absl::optional<uint8_t> qp) {
philipel6847f9b2018-04-20 15:05:37 +0200247 int64_t decode_stop_time_ms = rtc::TimeMillis();
248
249 bookkeeping_queue_.PostTask([this, decode_stop_time_ms, decoded_image,
philipel85ddb232020-10-02 14:46:14 +0200250 decode_time_ms, qp]() mutable {
philipel6847f9b2018-04-20 15:05:37 +0200251 RTC_DCHECK_RUN_ON(&bookkeeping_queue_);
252
philipel3dc47802020-09-10 15:30:02 +0200253 FrameInfo* frame_info = GetFrameInfo(decoded_image.timestamp());
254 if (!frame_info) {
philipel6847f9b2018-04-20 15:05:37 +0200255 RTC_LOG(LS_ERROR) << "No frame information found for frame with timestamp"
256 << decoded_image.timestamp();
257 return;
258 }
259
philipel3dc47802020-09-10 15:30:02 +0200260 Callbacks::FrameInfo callback_info;
261 callback_info.content_type = frame_info->content_type;
262
philipel6847f9b2018-04-20 15:05:37 +0200263 if (qp)
philipel3dc47802020-09-10 15:30:02 +0200264 callback_info.qp.emplace(*qp);
philipel6847f9b2018-04-20 15:05:37 +0200265
philipel85ddb232020-10-02 14:46:14 +0200266 if (!decode_time_ms) {
267 decode_time_ms = decode_stop_time_ms - frame_info->decode_start_time_ms;
268 }
269 decoded_image.set_processing_time(
270 {Timestamp::Millis(frame_info->decode_start_time_ms),
271 Timestamp::Millis(frame_info->decode_start_time_ms +
272 *decode_time_ms)});
273 decoded_image.set_timestamp_us(frame_info->render_time_us);
274 timing_.StopDecodeTimer(*decode_time_ms, decode_stop_time_ms);
philipel6847f9b2018-04-20 15:05:37 +0200275
philipel85ddb232020-10-02 14:46:14 +0200276 callbacks_->OnDecodedFrame(decoded_image, callback_info);
philipel6847f9b2018-04-20 15:05:37 +0200277 });
278}
279
philipel539f9b32020-01-09 16:12:25 +0100280VideoStreamDecoderImpl::DecodeCallbacks::DecodeCallbacks(
281 VideoStreamDecoderImpl* video_stream_decoder_impl)
282 : video_stream_decoder_impl_(video_stream_decoder_impl) {}
283
284int32_t VideoStreamDecoderImpl::DecodeCallbacks::Decoded(
285 VideoFrame& decoded_image) {
286 Decoded(decoded_image, absl::nullopt, absl::nullopt);
287 return WEBRTC_VIDEO_CODEC_OK;
288}
289
290int32_t VideoStreamDecoderImpl::DecodeCallbacks::Decoded(
291 VideoFrame& decoded_image,
292 int64_t decode_time_ms) {
293 Decoded(decoded_image, decode_time_ms, absl::nullopt);
294 return WEBRTC_VIDEO_CODEC_OK;
295}
296
297void VideoStreamDecoderImpl::DecodeCallbacks::Decoded(
298 VideoFrame& decoded_image,
299 absl::optional<int32_t> decode_time_ms,
300 absl::optional<uint8_t> qp) {
301 video_stream_decoder_impl_->OnDecodedFrameCallback(decoded_image,
302 decode_time_ms, qp);
303}
philipel2fee4d62018-03-21 16:52:13 +0100304} // namespace webrtc