philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
Danil Chapovalov | 471783f | 2019-03-11 14:26:02 +0100 | [diff] [blame] | 15 | #include "api/task_queue/queued_task.h" |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 16 | #include "rtc_base/logging.h" |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 17 | #include "rtc_base/numerics/mod_ops.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "rtc_base/time_utils.h" |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 21 | |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 22 | VideoStreamDecoderImpl::VideoStreamDecoderImpl( |
Danil Chapovalov | b703db9 | 2019-04-08 16:59:28 +0200 | [diff] [blame] | 23 | VideoStreamDecoderInterface::Callbacks* callbacks, |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 24 | VideoDecoderFactory* decoder_factory, |
Danil Chapovalov | b703db9 | 2019-04-08 16:59:28 +0200 | [diff] [blame] | 25 | TaskQueueFactory* task_queue_factory, |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 26 | std::map<int, std::pair<SdpVideoFormat, int>> decoder_settings) |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 27 | : timing_(Clock::GetRealTimeClock()), |
| 28 | decode_callbacks_(this), |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 29 | next_frame_info_index_(0), |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 30 | callbacks_(callbacks), |
| 31 | keyframe_required_(true), |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 32 | decoder_factory_(decoder_factory), |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 33 | decoder_settings_(std::move(decoder_settings)), |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 34 | shut_down_(false), |
| 35 | frame_buffer_(Clock::GetRealTimeClock(), &timing_, nullptr), |
Danil Chapovalov | b703db9 | 2019-04-08 16:59:28 +0200 | [diff] [blame] | 36 | bookkeeping_queue_(task_queue_factory->CreateTaskQueue( |
| 37 | "video_stream_decoder_bookkeeping_queue", |
| 38 | TaskQueueFactory::Priority::NORMAL)), |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 39 | decode_queue_(task_queue_factory->CreateTaskQueue( |
| 40 | "video_stream_decoder_decode_queue", |
| 41 | TaskQueueFactory::Priority::NORMAL)) { |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 42 | bookkeeping_queue_.PostTask([this]() { |
| 43 | RTC_DCHECK_RUN_ON(&bookkeeping_queue_); |
| 44 | StartNextDecode(); |
| 45 | }); |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 46 | } |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 47 | |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 48 | VideoStreamDecoderImpl::~VideoStreamDecoderImpl() { |
Markus Handell | 265931e | 2020-07-10 12:23:48 +0200 | [diff] [blame] | 49 | MutexLock lock(&shut_down_mutex_); |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 50 | shut_down_ = true; |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 51 | } |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 52 | |
philipel | ca18809 | 2021-03-23 12:00:49 +0100 | [diff] [blame] | 53 | void VideoStreamDecoderImpl::OnFrame(std::unique_ptr<EncodedFrame> frame) { |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 54 | if (!bookkeeping_queue_.IsCurrent()) { |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 55 | bookkeeping_queue_.PostTask([this, frame = std::move(frame)]() mutable { |
| 56 | OnFrame(std::move(frame)); |
| 57 | return true; |
| 58 | }); |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 59 | |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 60 | return; |
| 61 | } |
| 62 | |
| 63 | RTC_DCHECK_RUN_ON(&bookkeeping_queue_); |
| 64 | |
philipel | 9aa9b8d | 2021-02-15 13:31:29 +0100 | [diff] [blame] | 65 | 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_); |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
philipel | 781653c | 2019-06-04 17:10:37 +0200 | [diff] [blame] | 72 | void VideoStreamDecoderImpl::SetMinPlayoutDelay(TimeDelta min_delay) { |
| 73 | timing_.set_min_playout_delay(min_delay.ms()); |
| 74 | } |
| 75 | |
| 76 | void VideoStreamDecoderImpl::SetMaxPlayoutDelay(TimeDelta max_delay) { |
| 77 | timing_.set_max_playout_delay(max_delay.ms()); |
| 78 | } |
| 79 | |
philipel | 79aab3f | 2018-03-26 14:31:23 +0200 | [diff] [blame] | 80 | VideoDecoder* 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 Chapovalov | b6f19d7 | 2021-08-18 13:34:36 +0000 | [diff] [blame] | 105 | VideoDecoder::Settings settings; |
| 106 | settings.set_number_of_cores(decoder_settings_it->second.second); |
| 107 | if (!decoder->Configure(settings)) { |
philipel | 79aab3f | 2018-03-26 14:31:23 +0200 | [diff] [blame] | 108 | RTC_LOG(LS_WARNING) << "Failed to initialize decoder for payload type " |
| 109 | << payload_type << "."; |
| 110 | return nullptr; |
| 111 | } |
| 112 | |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 113 | int32_t register_result = |
| 114 | decoder->RegisterDecodeCompleteCallback(&decode_callbacks_); |
philipel | 79aab3f | 2018-03-26 14:31:23 +0200 | [diff] [blame] | 115 | 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 | |
philipel | ca18809 | 2021-03-23 12:00:49 +0100 | [diff] [blame] | 125 | void VideoStreamDecoderImpl::SaveFrameInfo(const EncodedFrame& frame) { |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 126 | 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_; |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 131 | |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 132 | next_frame_info_index_ = Add<kFrameInfoMemory>(next_frame_info_index_, 1); |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 133 | } |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 134 | |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 135 | void VideoStreamDecoderImpl::StartNextDecode() { |
| 136 | int64_t max_wait_time = keyframe_required_ ? 200 : 3000; |
| 137 | |
| 138 | frame_buffer_.NextFrame( |
| 139 | max_wait_time, keyframe_required_, &bookkeeping_queue_, |
philipel | ca18809 | 2021-03-23 12:00:49 +0100 | [diff] [blame] | 140 | [this](std::unique_ptr<EncodedFrame> frame, |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 141 | video_coding::FrameBuffer::ReturnReason res) mutable { |
| 142 | RTC_DCHECK_RUN_ON(&bookkeeping_queue_); |
| 143 | OnNextFrameCallback(std::move(frame), res); |
| 144 | }); |
| 145 | } |
| 146 | |
| 147 | void VideoStreamDecoderImpl::OnNextFrameCallback( |
philipel | ca18809 | 2021-03-23 12:00:49 +0100 | [diff] [blame] | 148 | std::unique_ptr<EncodedFrame> frame, |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 149 | video_coding::FrameBuffer::ReturnReason result) { |
| 150 | switch (result) { |
| 151 | case video_coding::FrameBuffer::kFrameFound: { |
| 152 | RTC_DCHECK(frame); |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 153 | SaveFrameInfo(*frame); |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 154 | |
Markus Handell | 265931e | 2020-07-10 12:23:48 +0200 | [diff] [blame] | 155 | MutexLock lock(&shut_down_mutex_); |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 156 | if (shut_down_) { |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 157 | return; |
| 158 | } |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 159 | |
| 160 | decode_queue_.PostTask([this, frame = std::move(frame)]() mutable { |
| 161 | RTC_DCHECK_RUN_ON(&decode_queue_); |
| 162 | DecodeResult decode_result = DecodeFrame(std::move(frame)); |
| 163 | bookkeeping_queue_.PostTask([this, decode_result]() { |
| 164 | RTC_DCHECK_RUN_ON(&bookkeeping_queue_); |
| 165 | switch (decode_result) { |
| 166 | case kOk: { |
| 167 | keyframe_required_ = false; |
| 168 | break; |
| 169 | } |
| 170 | case kOkRequestKeyframe: { |
| 171 | callbacks_->OnNonDecodableState(); |
| 172 | keyframe_required_ = false; |
| 173 | break; |
| 174 | } |
| 175 | case kDecodeFailure: { |
| 176 | callbacks_->OnNonDecodableState(); |
| 177 | keyframe_required_ = true; |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | StartNextDecode(); |
| 182 | }); |
| 183 | }); |
| 184 | break; |
| 185 | } |
| 186 | case video_coding::FrameBuffer::kTimeout: { |
| 187 | callbacks_->OnNonDecodableState(); |
Artem Titov | ab30d72 | 2021-07-27 16:22:11 +0200 | [diff] [blame] | 188 | // The `frame_buffer_` requires the frame callback function to complete |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 189 | // before NextFrame is called again. For this reason we call |
| 190 | // StartNextDecode in a later task to allow this task to complete first. |
| 191 | bookkeeping_queue_.PostTask([this]() { |
| 192 | RTC_DCHECK_RUN_ON(&bookkeeping_queue_); |
| 193 | StartNextDecode(); |
| 194 | }); |
| 195 | break; |
| 196 | } |
| 197 | case video_coding::FrameBuffer::kStopped: { |
| 198 | // We are shutting down, do nothing. |
| 199 | break; |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 204 | VideoStreamDecoderImpl::DecodeResult VideoStreamDecoderImpl::DecodeFrame( |
philipel | ca18809 | 2021-03-23 12:00:49 +0100 | [diff] [blame] | 205 | std::unique_ptr<EncodedFrame> frame) { |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 206 | RTC_DCHECK(frame); |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 207 | |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 208 | VideoDecoder* decoder = GetDecoder(frame->PayloadType()); |
| 209 | if (!decoder) { |
| 210 | return kDecodeFailure; |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 211 | } |
| 212 | |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 213 | int32_t decode_result = decoder->Decode(frame->EncodedImage(), // |
| 214 | /*missing_frames=*/false, // |
| 215 | frame->RenderTimeMs()); |
| 216 | switch (decode_result) { |
| 217 | case WEBRTC_VIDEO_CODEC_OK: { |
| 218 | return kOk; |
| 219 | } |
| 220 | case WEBRTC_VIDEO_CODEC_OK_REQUEST_KEYFRAME: { |
| 221 | return kOkRequestKeyframe; |
| 222 | } |
| 223 | default: |
| 224 | return kDecodeFailure; |
| 225 | } |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 226 | } |
| 227 | |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 228 | VideoStreamDecoderImpl::FrameInfo* VideoStreamDecoderImpl::GetFrameInfo( |
| 229 | int64_t timestamp) { |
| 230 | int start_time_index = next_frame_info_index_; |
| 231 | for (int i = 0; i < kFrameInfoMemory; ++i) { |
| 232 | start_time_index = Subtract<kFrameInfoMemory>(start_time_index, 1); |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 233 | |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 234 | if (frame_info_[start_time_index].timestamp == timestamp) |
| 235 | return &frame_info_[start_time_index]; |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | return nullptr; |
| 239 | } |
| 240 | |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 241 | void VideoStreamDecoderImpl::OnDecodedFrameCallback( |
| 242 | VideoFrame& decoded_image, |
| 243 | absl::optional<int32_t> decode_time_ms, |
| 244 | absl::optional<uint8_t> qp) { |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 245 | int64_t decode_stop_time_ms = rtc::TimeMillis(); |
| 246 | |
| 247 | bookkeeping_queue_.PostTask([this, decode_stop_time_ms, decoded_image, |
philipel | 85ddb23 | 2020-10-02 14:46:14 +0200 | [diff] [blame] | 248 | decode_time_ms, qp]() mutable { |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 249 | RTC_DCHECK_RUN_ON(&bookkeeping_queue_); |
| 250 | |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 251 | FrameInfo* frame_info = GetFrameInfo(decoded_image.timestamp()); |
| 252 | if (!frame_info) { |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 253 | RTC_LOG(LS_ERROR) << "No frame information found for frame with timestamp" |
| 254 | << decoded_image.timestamp(); |
| 255 | return; |
| 256 | } |
| 257 | |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 258 | Callbacks::FrameInfo callback_info; |
| 259 | callback_info.content_type = frame_info->content_type; |
| 260 | |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 261 | if (qp) |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 262 | callback_info.qp.emplace(*qp); |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 263 | |
philipel | 85ddb23 | 2020-10-02 14:46:14 +0200 | [diff] [blame] | 264 | if (!decode_time_ms) { |
| 265 | decode_time_ms = decode_stop_time_ms - frame_info->decode_start_time_ms; |
| 266 | } |
| 267 | decoded_image.set_processing_time( |
| 268 | {Timestamp::Millis(frame_info->decode_start_time_ms), |
| 269 | Timestamp::Millis(frame_info->decode_start_time_ms + |
| 270 | *decode_time_ms)}); |
| 271 | decoded_image.set_timestamp_us(frame_info->render_time_us); |
| 272 | timing_.StopDecodeTimer(*decode_time_ms, decode_stop_time_ms); |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 273 | |
philipel | 85ddb23 | 2020-10-02 14:46:14 +0200 | [diff] [blame] | 274 | callbacks_->OnDecodedFrame(decoded_image, callback_info); |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 275 | }); |
| 276 | } |
| 277 | |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 278 | VideoStreamDecoderImpl::DecodeCallbacks::DecodeCallbacks( |
| 279 | VideoStreamDecoderImpl* video_stream_decoder_impl) |
| 280 | : video_stream_decoder_impl_(video_stream_decoder_impl) {} |
| 281 | |
| 282 | int32_t VideoStreamDecoderImpl::DecodeCallbacks::Decoded( |
| 283 | VideoFrame& decoded_image) { |
| 284 | Decoded(decoded_image, absl::nullopt, absl::nullopt); |
| 285 | return WEBRTC_VIDEO_CODEC_OK; |
| 286 | } |
| 287 | |
| 288 | int32_t VideoStreamDecoderImpl::DecodeCallbacks::Decoded( |
| 289 | VideoFrame& decoded_image, |
| 290 | int64_t decode_time_ms) { |
| 291 | Decoded(decoded_image, decode_time_ms, absl::nullopt); |
| 292 | return WEBRTC_VIDEO_CODEC_OK; |
| 293 | } |
| 294 | |
| 295 | void VideoStreamDecoderImpl::DecodeCallbacks::Decoded( |
| 296 | VideoFrame& decoded_image, |
| 297 | absl::optional<int32_t> decode_time_ms, |
| 298 | absl::optional<uint8_t> qp) { |
| 299 | video_stream_decoder_impl_->OnDecodedFrameCallback(decoded_image, |
| 300 | decode_time_ms, qp); |
| 301 | } |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 302 | } // namespace webrtc |