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) |
| 27 | : callbacks_(callbacks), |
| 28 | decoder_factory_(decoder_factory), |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 29 | decoder_settings_(std::move(decoder_settings)), |
Danil Chapovalov | b703db9 | 2019-04-08 16:59:28 +0200 | [diff] [blame] | 30 | bookkeeping_queue_(task_queue_factory->CreateTaskQueue( |
| 31 | "video_stream_decoder_bookkeeping_queue", |
| 32 | TaskQueueFactory::Priority::NORMAL)), |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 33 | decode_thread_(&DecodeLoop, |
| 34 | this, |
| 35 | "video_stream_decoder_decode_thread", |
| 36 | rtc::kHighestPriority), |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 37 | timing_(Clock::GetRealTimeClock()), |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 38 | frame_buffer_(Clock::GetRealTimeClock(), &timing_, nullptr), |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 39 | next_frame_timestamps_index_(0) { |
| 40 | frame_timestamps_.fill({-1, -1, -1}); |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 41 | decode_thread_.Start(); |
| 42 | } |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 43 | |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 44 | VideoStreamDecoderImpl::~VideoStreamDecoderImpl() { |
| 45 | frame_buffer_.Stop(); |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 46 | decode_thread_.Stop(); |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 47 | } |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 48 | |
| 49 | void VideoStreamDecoderImpl::OnFrame( |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 50 | std::unique_ptr<video_coding::EncodedFrame> frame) { |
| 51 | if (!bookkeeping_queue_.IsCurrent()) { |
Danil Chapovalov | 471783f | 2019-03-11 14:26:02 +0100 | [diff] [blame] | 52 | struct OnFrameTask : QueuedTask { |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 53 | OnFrameTask(std::unique_ptr<video_coding::EncodedFrame> frame, |
| 54 | VideoStreamDecoderImpl* video_stream_decoder) |
| 55 | : frame_(std::move(frame)), |
| 56 | video_stream_decoder_(video_stream_decoder) {} |
| 57 | |
Niels Möller | be682d4 | 2018-03-27 08:31:45 +0200 | [diff] [blame] | 58 | bool Run() override { |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 59 | video_stream_decoder_->OnFrame(std::move(frame_)); |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | std::unique_ptr<video_coding::EncodedFrame> frame_; |
| 64 | VideoStreamDecoderImpl* video_stream_decoder_; |
| 65 | }; |
| 66 | |
| 67 | bookkeeping_queue_.PostTask( |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 68 | std::make_unique<OnFrameTask>(std::move(frame), this)); |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 69 | return; |
| 70 | } |
| 71 | |
| 72 | RTC_DCHECK_RUN_ON(&bookkeeping_queue_); |
| 73 | |
| 74 | uint64_t continuous_pid = frame_buffer_.InsertFrame(std::move(frame)); |
| 75 | video_coding::VideoLayerFrameId continuous_id(continuous_pid, 0); |
| 76 | if (last_continuous_id_ < continuous_id) { |
| 77 | last_continuous_id_ = continuous_id; |
| 78 | callbacks_->OnContinuousUntil(last_continuous_id_); |
| 79 | } |
| 80 | } |
| 81 | |
philipel | 781653c | 2019-06-04 17:10:37 +0200 | [diff] [blame] | 82 | void VideoStreamDecoderImpl::SetMinPlayoutDelay(TimeDelta min_delay) { |
| 83 | timing_.set_min_playout_delay(min_delay.ms()); |
| 84 | } |
| 85 | |
| 86 | void VideoStreamDecoderImpl::SetMaxPlayoutDelay(TimeDelta max_delay) { |
| 87 | timing_.set_max_playout_delay(max_delay.ms()); |
| 88 | } |
| 89 | |
philipel | 79aab3f | 2018-03-26 14:31:23 +0200 | [diff] [blame] | 90 | VideoDecoder* VideoStreamDecoderImpl::GetDecoder(int payload_type) { |
| 91 | if (current_payload_type_ == payload_type) { |
| 92 | RTC_DCHECK(decoder_); |
| 93 | return decoder_.get(); |
| 94 | } |
| 95 | |
| 96 | current_payload_type_.reset(); |
| 97 | decoder_.reset(); |
| 98 | |
| 99 | auto decoder_settings_it = decoder_settings_.find(payload_type); |
| 100 | if (decoder_settings_it == decoder_settings_.end()) { |
| 101 | RTC_LOG(LS_WARNING) << "Payload type " << payload_type |
| 102 | << " not registered."; |
| 103 | return nullptr; |
| 104 | } |
| 105 | |
| 106 | const SdpVideoFormat& video_format = decoder_settings_it->second.first; |
| 107 | std::unique_ptr<VideoDecoder> decoder = |
| 108 | decoder_factory_->CreateVideoDecoder(video_format); |
| 109 | if (!decoder) { |
| 110 | RTC_LOG(LS_WARNING) << "Failed to create decoder for payload type " |
| 111 | << payload_type << "."; |
| 112 | return nullptr; |
| 113 | } |
| 114 | |
| 115 | int num_cores = decoder_settings_it->second.second; |
| 116 | int32_t init_result = decoder->InitDecode(nullptr, num_cores); |
| 117 | if (init_result != WEBRTC_VIDEO_CODEC_OK) { |
| 118 | RTC_LOG(LS_WARNING) << "Failed to initialize decoder for payload type " |
| 119 | << payload_type << "."; |
| 120 | return nullptr; |
| 121 | } |
| 122 | |
| 123 | int32_t register_result = decoder->RegisterDecodeCompleteCallback(this); |
| 124 | if (register_result != WEBRTC_VIDEO_CODEC_OK) { |
| 125 | RTC_LOG(LS_WARNING) << "Failed to register decode callback."; |
| 126 | return nullptr; |
| 127 | } |
| 128 | |
| 129 | current_payload_type_.emplace(payload_type); |
| 130 | decoder_ = std::move(decoder); |
| 131 | return decoder_.get(); |
| 132 | } |
| 133 | |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 134 | // static |
| 135 | void VideoStreamDecoderImpl::DecodeLoop(void* ptr) { |
| 136 | // TODO(philipel): Remove this and use rtc::Event::kForever when it's |
| 137 | // supported by the |frame_buffer_|. |
| 138 | static constexpr int kForever = 100000000; |
| 139 | |
| 140 | int max_wait_time_ms = kForever; |
| 141 | bool keyframe_required = true; |
| 142 | auto* vs_decoder = static_cast<VideoStreamDecoderImpl*>(ptr); |
| 143 | while (true) { |
| 144 | DecodeResult decode_result = |
| 145 | vs_decoder->DecodeNextFrame(max_wait_time_ms, keyframe_required); |
| 146 | |
| 147 | switch (decode_result) { |
| 148 | case kOk: { |
| 149 | max_wait_time_ms = kForever; |
| 150 | keyframe_required = false; |
| 151 | break; |
| 152 | } |
| 153 | case kDecodeFailure: { |
| 154 | max_wait_time_ms = 0; |
| 155 | keyframe_required = true; |
| 156 | break; |
| 157 | } |
| 158 | case kNoFrame: { |
| 159 | max_wait_time_ms = kForever; |
| 160 | // If we end up here it means that we got a decoding error and there is |
| 161 | // no keyframe available in the |frame_buffer_|. |
| 162 | vs_decoder->bookkeeping_queue_.PostTask([vs_decoder]() { |
| 163 | RTC_DCHECK_RUN_ON(&vs_decoder->bookkeeping_queue_); |
| 164 | vs_decoder->callbacks_->OnNonDecodableState(); |
| 165 | }); |
| 166 | break; |
| 167 | } |
| 168 | case kNoDecoder: { |
| 169 | max_wait_time_ms = kForever; |
| 170 | break; |
| 171 | } |
| 172 | case kShutdown: { |
| 173 | return; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | VideoStreamDecoderImpl::DecodeResult VideoStreamDecoderImpl::DecodeNextFrame( |
| 180 | int max_wait_time_ms, |
| 181 | bool keyframe_required) { |
| 182 | std::unique_ptr<video_coding::EncodedFrame> frame; |
| 183 | video_coding::FrameBuffer::ReturnReason res = |
| 184 | frame_buffer_.NextFrame(max_wait_time_ms, &frame, keyframe_required); |
| 185 | |
| 186 | if (res == video_coding::FrameBuffer::ReturnReason::kStopped) |
| 187 | return kShutdown; |
| 188 | |
| 189 | if (frame) { |
| 190 | VideoDecoder* decoder = GetDecoder(frame->PayloadType()); |
| 191 | if (!decoder) { |
| 192 | RTC_LOG(LS_WARNING) << "Failed to get decoder, dropping frame (" |
| 193 | << frame->id.picture_id << ":" |
| 194 | << frame->id.spatial_layer << ")."; |
| 195 | return kNoDecoder; |
| 196 | } |
| 197 | |
| 198 | int64_t decode_start_time_ms = rtc::TimeMillis(); |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 199 | int64_t timestamp = frame->Timestamp(); |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 200 | int64_t render_time_us = frame->RenderTimeMs() * 1000; |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 201 | bookkeeping_queue_.PostTask( |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 202 | [this, decode_start_time_ms, timestamp, render_time_us]() { |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 203 | RTC_DCHECK_RUN_ON(&bookkeeping_queue_); |
| 204 | // Saving decode start time this way wont work if we decode spatial |
| 205 | // layers sequentially. |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 206 | FrameTimestamps* frame_timestamps = |
| 207 | &frame_timestamps_[next_frame_timestamps_index_]; |
| 208 | frame_timestamps->timestamp = timestamp; |
| 209 | frame_timestamps->decode_start_time_ms = decode_start_time_ms; |
| 210 | frame_timestamps->render_time_us = render_time_us; |
| 211 | |
| 212 | next_frame_timestamps_index_ = |
| 213 | Add<kFrameTimestampsMemory>(next_frame_timestamps_index_, 1); |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 214 | }); |
| 215 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 216 | int32_t decode_result = decoder->Decode(frame->EncodedImage(), |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 217 | false, // missing_frame |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 218 | frame->RenderTimeMs()); |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 219 | |
| 220 | return decode_result == WEBRTC_VIDEO_CODEC_OK ? kOk : kDecodeFailure; |
| 221 | } |
| 222 | |
| 223 | return kNoFrame; |
| 224 | } |
| 225 | |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 226 | VideoStreamDecoderImpl::FrameTimestamps* |
| 227 | VideoStreamDecoderImpl::GetFrameTimestamps(int64_t timestamp) { |
| 228 | RTC_DCHECK_RUN_ON(&bookkeeping_queue_); |
| 229 | |
| 230 | int start_time_index = next_frame_timestamps_index_; |
| 231 | for (int i = 0; i < kFrameTimestampsMemory; ++i) { |
| 232 | start_time_index = Subtract<kFrameTimestampsMemory>(start_time_index, 1); |
| 233 | |
| 234 | if (frame_timestamps_[start_time_index].timestamp == timestamp) |
| 235 | return &frame_timestamps_[start_time_index]; |
| 236 | } |
| 237 | |
| 238 | return nullptr; |
| 239 | } |
| 240 | |
| 241 | // VideoDecoder::DecodedImageCallback |
| 242 | int32_t VideoStreamDecoderImpl::Decoded(VideoFrame& decoded_image) { |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 243 | Decoded(decoded_image, absl::nullopt, absl::nullopt); |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 244 | return WEBRTC_VIDEO_CODEC_OK; |
| 245 | } |
| 246 | |
| 247 | // VideoDecoder::DecodedImageCallback |
| 248 | int32_t VideoStreamDecoderImpl::Decoded(VideoFrame& decoded_image, |
| 249 | int64_t decode_time_ms) { |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 250 | Decoded(decoded_image, decode_time_ms, absl::nullopt); |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 251 | return WEBRTC_VIDEO_CODEC_OK; |
| 252 | } |
| 253 | |
| 254 | // VideoDecoder::DecodedImageCallback |
| 255 | void VideoStreamDecoderImpl::Decoded(VideoFrame& decoded_image, |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 256 | absl::optional<int32_t> decode_time_ms, |
| 257 | absl::optional<uint8_t> qp) { |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 258 | int64_t decode_stop_time_ms = rtc::TimeMillis(); |
| 259 | |
| 260 | bookkeeping_queue_.PostTask([this, decode_stop_time_ms, decoded_image, |
| 261 | decode_time_ms, qp]() { |
| 262 | RTC_DCHECK_RUN_ON(&bookkeeping_queue_); |
| 263 | |
| 264 | FrameTimestamps* frame_timestamps = |
| 265 | GetFrameTimestamps(decoded_image.timestamp()); |
| 266 | if (!frame_timestamps) { |
| 267 | RTC_LOG(LS_ERROR) << "No frame information found for frame with timestamp" |
| 268 | << decoded_image.timestamp(); |
| 269 | return; |
| 270 | } |
| 271 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 272 | absl::optional<int> casted_qp; |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 273 | if (qp) |
| 274 | casted_qp.emplace(*qp); |
| 275 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 276 | absl::optional<int> casted_decode_time_ms(decode_time_ms.value_or( |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 277 | decode_stop_time_ms - frame_timestamps->decode_start_time_ms)); |
| 278 | |
Johannes Kron | bfd343b | 2019-07-01 10:07:50 +0200 | [diff] [blame] | 279 | timing_.StopDecodeTimer(*casted_decode_time_ms, decode_stop_time_ms); |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 280 | |
Ilya Nikolaevskiy | 4fc0855 | 2019-06-05 15:59:12 +0200 | [diff] [blame] | 281 | VideoFrame copy = decoded_image; |
| 282 | copy.set_timestamp_us(frame_timestamps->render_time_us); |
| 283 | callbacks_->OnDecodedFrame(copy, casted_decode_time_ms, casted_qp); |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 284 | }); |
| 285 | } |
| 286 | |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 287 | } // namespace webrtc |