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 | |
| 53 | void VideoStreamDecoderImpl::OnFrame( |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 54 | std::unique_ptr<video_coding::EncodedFrame> frame) { |
| 55 | if (!bookkeeping_queue_.IsCurrent()) { |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 56 | bookkeeping_queue_.PostTask([this, frame = std::move(frame)]() mutable { |
| 57 | OnFrame(std::move(frame)); |
| 58 | return true; |
| 59 | }); |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 60 | |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 61 | return; |
| 62 | } |
| 63 | |
| 64 | RTC_DCHECK_RUN_ON(&bookkeeping_queue_); |
| 65 | |
| 66 | uint64_t continuous_pid = frame_buffer_.InsertFrame(std::move(frame)); |
| 67 | video_coding::VideoLayerFrameId continuous_id(continuous_pid, 0); |
| 68 | if (last_continuous_id_ < continuous_id) { |
| 69 | last_continuous_id_ = continuous_id; |
| 70 | callbacks_->OnContinuousUntil(last_continuous_id_); |
| 71 | } |
| 72 | } |
| 73 | |
philipel | 781653c | 2019-06-04 17:10:37 +0200 | [diff] [blame] | 74 | void VideoStreamDecoderImpl::SetMinPlayoutDelay(TimeDelta min_delay) { |
| 75 | timing_.set_min_playout_delay(min_delay.ms()); |
| 76 | } |
| 77 | |
| 78 | void VideoStreamDecoderImpl::SetMaxPlayoutDelay(TimeDelta max_delay) { |
| 79 | timing_.set_max_playout_delay(max_delay.ms()); |
| 80 | } |
| 81 | |
philipel | 79aab3f | 2018-03-26 14:31:23 +0200 | [diff] [blame] | 82 | VideoDecoder* VideoStreamDecoderImpl::GetDecoder(int payload_type) { |
| 83 | if (current_payload_type_ == payload_type) { |
| 84 | RTC_DCHECK(decoder_); |
| 85 | return decoder_.get(); |
| 86 | } |
| 87 | |
| 88 | current_payload_type_.reset(); |
| 89 | decoder_.reset(); |
| 90 | |
| 91 | auto decoder_settings_it = decoder_settings_.find(payload_type); |
| 92 | if (decoder_settings_it == decoder_settings_.end()) { |
| 93 | RTC_LOG(LS_WARNING) << "Payload type " << payload_type |
| 94 | << " not registered."; |
| 95 | return nullptr; |
| 96 | } |
| 97 | |
| 98 | const SdpVideoFormat& video_format = decoder_settings_it->second.first; |
| 99 | std::unique_ptr<VideoDecoder> decoder = |
| 100 | decoder_factory_->CreateVideoDecoder(video_format); |
| 101 | if (!decoder) { |
| 102 | RTC_LOG(LS_WARNING) << "Failed to create decoder for payload type " |
| 103 | << payload_type << "."; |
| 104 | return nullptr; |
| 105 | } |
| 106 | |
| 107 | int num_cores = decoder_settings_it->second.second; |
| 108 | int32_t init_result = decoder->InitDecode(nullptr, num_cores); |
| 109 | if (init_result != WEBRTC_VIDEO_CODEC_OK) { |
| 110 | RTC_LOG(LS_WARNING) << "Failed to initialize decoder for payload type " |
| 111 | << payload_type << "."; |
| 112 | return nullptr; |
| 113 | } |
| 114 | |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 115 | int32_t register_result = |
| 116 | decoder->RegisterDecodeCompleteCallback(&decode_callbacks_); |
philipel | 79aab3f | 2018-03-26 14:31:23 +0200 | [diff] [blame] | 117 | if (register_result != WEBRTC_VIDEO_CODEC_OK) { |
| 118 | RTC_LOG(LS_WARNING) << "Failed to register decode callback."; |
| 119 | return nullptr; |
| 120 | } |
| 121 | |
| 122 | current_payload_type_.emplace(payload_type); |
| 123 | decoder_ = std::move(decoder); |
| 124 | return decoder_.get(); |
| 125 | } |
| 126 | |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 127 | void VideoStreamDecoderImpl::SaveFrameInfo( |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 128 | const video_coding::EncodedFrame& frame) { |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 129 | FrameInfo* frame_info = &frame_info_[next_frame_info_index_]; |
| 130 | frame_info->timestamp = frame.Timestamp(); |
| 131 | frame_info->decode_start_time_ms = rtc::TimeMillis(); |
| 132 | frame_info->render_time_us = frame.RenderTimeMs() * 1000; |
| 133 | frame_info->content_type = frame.EncodedImage().content_type_; |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 134 | |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 135 | next_frame_info_index_ = Add<kFrameInfoMemory>(next_frame_info_index_, 1); |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 136 | } |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 137 | |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 138 | void VideoStreamDecoderImpl::StartNextDecode() { |
| 139 | int64_t max_wait_time = keyframe_required_ ? 200 : 3000; |
| 140 | |
| 141 | frame_buffer_.NextFrame( |
| 142 | max_wait_time, keyframe_required_, &bookkeeping_queue_, |
| 143 | [this](std::unique_ptr<video_coding::EncodedFrame> frame, |
| 144 | video_coding::FrameBuffer::ReturnReason res) mutable { |
| 145 | RTC_DCHECK_RUN_ON(&bookkeeping_queue_); |
| 146 | OnNextFrameCallback(std::move(frame), res); |
| 147 | }); |
| 148 | } |
| 149 | |
| 150 | void VideoStreamDecoderImpl::OnNextFrameCallback( |
| 151 | std::unique_ptr<video_coding::EncodedFrame> frame, |
| 152 | video_coding::FrameBuffer::ReturnReason result) { |
| 153 | switch (result) { |
| 154 | case video_coding::FrameBuffer::kFrameFound: { |
| 155 | RTC_DCHECK(frame); |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 156 | SaveFrameInfo(*frame); |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 157 | |
Markus Handell | 265931e | 2020-07-10 12:23:48 +0200 | [diff] [blame] | 158 | MutexLock lock(&shut_down_mutex_); |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 159 | if (shut_down_) { |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 160 | return; |
| 161 | } |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 162 | |
| 163 | decode_queue_.PostTask([this, frame = std::move(frame)]() mutable { |
| 164 | RTC_DCHECK_RUN_ON(&decode_queue_); |
| 165 | DecodeResult decode_result = DecodeFrame(std::move(frame)); |
| 166 | bookkeeping_queue_.PostTask([this, decode_result]() { |
| 167 | RTC_DCHECK_RUN_ON(&bookkeeping_queue_); |
| 168 | switch (decode_result) { |
| 169 | case kOk: { |
| 170 | keyframe_required_ = false; |
| 171 | break; |
| 172 | } |
| 173 | case kOkRequestKeyframe: { |
| 174 | callbacks_->OnNonDecodableState(); |
| 175 | keyframe_required_ = false; |
| 176 | break; |
| 177 | } |
| 178 | case kDecodeFailure: { |
| 179 | callbacks_->OnNonDecodableState(); |
| 180 | keyframe_required_ = true; |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | StartNextDecode(); |
| 185 | }); |
| 186 | }); |
| 187 | break; |
| 188 | } |
| 189 | case video_coding::FrameBuffer::kTimeout: { |
| 190 | callbacks_->OnNonDecodableState(); |
| 191 | // The |frame_buffer_| requires the frame callback function to complete |
| 192 | // before NextFrame is called again. For this reason we call |
| 193 | // StartNextDecode in a later task to allow this task to complete first. |
| 194 | bookkeeping_queue_.PostTask([this]() { |
| 195 | RTC_DCHECK_RUN_ON(&bookkeeping_queue_); |
| 196 | StartNextDecode(); |
| 197 | }); |
| 198 | break; |
| 199 | } |
| 200 | case video_coding::FrameBuffer::kStopped: { |
| 201 | // We are shutting down, do nothing. |
| 202 | break; |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 207 | VideoStreamDecoderImpl::DecodeResult VideoStreamDecoderImpl::DecodeFrame( |
| 208 | std::unique_ptr<video_coding::EncodedFrame> frame) { |
| 209 | RTC_DCHECK(frame); |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 210 | |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 211 | VideoDecoder* decoder = GetDecoder(frame->PayloadType()); |
| 212 | if (!decoder) { |
| 213 | return kDecodeFailure; |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 214 | } |
| 215 | |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 216 | int32_t decode_result = decoder->Decode(frame->EncodedImage(), // |
| 217 | /*missing_frames=*/false, // |
| 218 | frame->RenderTimeMs()); |
| 219 | switch (decode_result) { |
| 220 | case WEBRTC_VIDEO_CODEC_OK: { |
| 221 | return kOk; |
| 222 | } |
| 223 | case WEBRTC_VIDEO_CODEC_OK_REQUEST_KEYFRAME: { |
| 224 | return kOkRequestKeyframe; |
| 225 | } |
| 226 | default: |
| 227 | return kDecodeFailure; |
| 228 | } |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 229 | } |
| 230 | |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 231 | VideoStreamDecoderImpl::FrameInfo* VideoStreamDecoderImpl::GetFrameInfo( |
| 232 | int64_t timestamp) { |
| 233 | int start_time_index = next_frame_info_index_; |
| 234 | for (int i = 0; i < kFrameInfoMemory; ++i) { |
| 235 | start_time_index = Subtract<kFrameInfoMemory>(start_time_index, 1); |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 236 | |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 237 | if (frame_info_[start_time_index].timestamp == timestamp) |
| 238 | return &frame_info_[start_time_index]; |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | return nullptr; |
| 242 | } |
| 243 | |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 244 | void VideoStreamDecoderImpl::OnDecodedFrameCallback( |
| 245 | VideoFrame& decoded_image, |
| 246 | absl::optional<int32_t> decode_time_ms, |
| 247 | absl::optional<uint8_t> qp) { |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 248 | int64_t decode_stop_time_ms = rtc::TimeMillis(); |
| 249 | |
| 250 | bookkeeping_queue_.PostTask([this, decode_stop_time_ms, decoded_image, |
| 251 | decode_time_ms, qp]() { |
| 252 | RTC_DCHECK_RUN_ON(&bookkeeping_queue_); |
| 253 | |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 254 | FrameInfo* frame_info = GetFrameInfo(decoded_image.timestamp()); |
| 255 | if (!frame_info) { |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 256 | RTC_LOG(LS_ERROR) << "No frame information found for frame with timestamp" |
| 257 | << decoded_image.timestamp(); |
| 258 | return; |
| 259 | } |
| 260 | |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 261 | Callbacks::FrameInfo callback_info; |
| 262 | callback_info.content_type = frame_info->content_type; |
| 263 | |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 264 | if (qp) |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 265 | callback_info.qp.emplace(*qp); |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 266 | |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 267 | callback_info.decode_time_ms = decode_time_ms.value_or( |
| 268 | decode_stop_time_ms - frame_info->decode_start_time_ms); |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 269 | |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 270 | timing_.StopDecodeTimer(*callback_info.decode_time_ms, decode_stop_time_ms); |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 271 | |
Ilya Nikolaevskiy | 4fc0855 | 2019-06-05 15:59:12 +0200 | [diff] [blame] | 272 | VideoFrame copy = decoded_image; |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 273 | copy.set_timestamp_us(frame_info->render_time_us); |
| 274 | callbacks_->OnDecodedFrame(copy, 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 |