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 | |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 13 | #include "rtc_base/logging.h" |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame^] | 14 | #include "rtc_base/numerics/mod_ops.h" |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 15 | #include "rtc_base/ptr_util.h" |
| 16 | |
| 17 | namespace webrtc { |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 18 | |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 19 | VideoStreamDecoderImpl::VideoStreamDecoderImpl( |
| 20 | VideoStreamDecoder::Callbacks* callbacks, |
| 21 | VideoDecoderFactory* decoder_factory, |
| 22 | std::map<int, std::pair<SdpVideoFormat, int>> decoder_settings) |
| 23 | : callbacks_(callbacks), |
| 24 | decoder_factory_(decoder_factory), |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 25 | decoder_settings_(std::move(decoder_settings)), |
| 26 | bookkeeping_queue_("video_stream_decoder_bookkeeping_queue"), |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame^] | 27 | decode_thread_(&DecodeLoop, |
| 28 | this, |
| 29 | "video_stream_decoder_decode_thread", |
| 30 | rtc::kHighestPriority), |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 31 | jitter_estimator_(Clock::GetRealTimeClock()), |
| 32 | timing_(Clock::GetRealTimeClock()), |
| 33 | frame_buffer_(Clock::GetRealTimeClock(), |
| 34 | &jitter_estimator_, |
| 35 | &timing_, |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame^] | 36 | nullptr), |
| 37 | next_start_time_index_(0) { |
| 38 | decode_start_time_.fill({-1, 0}); |
| 39 | decode_thread_.Start(); |
| 40 | } |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 41 | |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 42 | VideoStreamDecoderImpl::~VideoStreamDecoderImpl() { |
| 43 | frame_buffer_.Stop(); |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame^] | 44 | decode_thread_.Stop(); |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 45 | } |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 46 | |
| 47 | void VideoStreamDecoderImpl::OnFrame( |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 48 | std::unique_ptr<video_coding::EncodedFrame> frame) { |
| 49 | if (!bookkeeping_queue_.IsCurrent()) { |
| 50 | struct OnFrameTask : rtc::QueuedTask { |
| 51 | OnFrameTask(std::unique_ptr<video_coding::EncodedFrame> frame, |
| 52 | VideoStreamDecoderImpl* video_stream_decoder) |
| 53 | : frame_(std::move(frame)), |
| 54 | video_stream_decoder_(video_stream_decoder) {} |
| 55 | |
Niels Möller | be682d4 | 2018-03-27 08:31:45 +0200 | [diff] [blame] | 56 | bool Run() override { |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 57 | video_stream_decoder_->OnFrame(std::move(frame_)); |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | std::unique_ptr<video_coding::EncodedFrame> frame_; |
| 62 | VideoStreamDecoderImpl* video_stream_decoder_; |
| 63 | }; |
| 64 | |
| 65 | bookkeeping_queue_.PostTask( |
| 66 | rtc::MakeUnique<OnFrameTask>(std::move(frame), this)); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | RTC_DCHECK_RUN_ON(&bookkeeping_queue_); |
| 71 | |
| 72 | uint64_t continuous_pid = frame_buffer_.InsertFrame(std::move(frame)); |
| 73 | video_coding::VideoLayerFrameId continuous_id(continuous_pid, 0); |
| 74 | if (last_continuous_id_ < continuous_id) { |
| 75 | last_continuous_id_ = continuous_id; |
| 76 | callbacks_->OnContinuousUntil(last_continuous_id_); |
| 77 | } |
| 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 | |
| 105 | int num_cores = decoder_settings_it->second.second; |
| 106 | int32_t init_result = decoder->InitDecode(nullptr, num_cores); |
| 107 | if (init_result != WEBRTC_VIDEO_CODEC_OK) { |
| 108 | RTC_LOG(LS_WARNING) << "Failed to initialize decoder for payload type " |
| 109 | << payload_type << "."; |
| 110 | return nullptr; |
| 111 | } |
| 112 | |
| 113 | int32_t register_result = decoder->RegisterDecodeCompleteCallback(this); |
| 114 | if (register_result != WEBRTC_VIDEO_CODEC_OK) { |
| 115 | RTC_LOG(LS_WARNING) << "Failed to register decode callback."; |
| 116 | return nullptr; |
| 117 | } |
| 118 | |
| 119 | current_payload_type_.emplace(payload_type); |
| 120 | decoder_ = std::move(decoder); |
| 121 | return decoder_.get(); |
| 122 | } |
| 123 | |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame^] | 124 | // static |
| 125 | void VideoStreamDecoderImpl::DecodeLoop(void* ptr) { |
| 126 | // TODO(philipel): Remove this and use rtc::Event::kForever when it's |
| 127 | // supported by the |frame_buffer_|. |
| 128 | static constexpr int kForever = 100000000; |
| 129 | |
| 130 | int max_wait_time_ms = kForever; |
| 131 | bool keyframe_required = true; |
| 132 | auto* vs_decoder = static_cast<VideoStreamDecoderImpl*>(ptr); |
| 133 | while (true) { |
| 134 | DecodeResult decode_result = |
| 135 | vs_decoder->DecodeNextFrame(max_wait_time_ms, keyframe_required); |
| 136 | |
| 137 | switch (decode_result) { |
| 138 | case kOk: { |
| 139 | max_wait_time_ms = kForever; |
| 140 | keyframe_required = false; |
| 141 | break; |
| 142 | } |
| 143 | case kDecodeFailure: { |
| 144 | max_wait_time_ms = 0; |
| 145 | keyframe_required = true; |
| 146 | break; |
| 147 | } |
| 148 | case kNoFrame: { |
| 149 | max_wait_time_ms = kForever; |
| 150 | // If we end up here it means that we got a decoding error and there is |
| 151 | // no keyframe available in the |frame_buffer_|. |
| 152 | vs_decoder->bookkeeping_queue_.PostTask([vs_decoder]() { |
| 153 | RTC_DCHECK_RUN_ON(&vs_decoder->bookkeeping_queue_); |
| 154 | vs_decoder->callbacks_->OnNonDecodableState(); |
| 155 | }); |
| 156 | break; |
| 157 | } |
| 158 | case kNoDecoder: { |
| 159 | max_wait_time_ms = kForever; |
| 160 | break; |
| 161 | } |
| 162 | case kShutdown: { |
| 163 | return; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | VideoStreamDecoderImpl::DecodeResult VideoStreamDecoderImpl::DecodeNextFrame( |
| 170 | int max_wait_time_ms, |
| 171 | bool keyframe_required) { |
| 172 | std::unique_ptr<video_coding::EncodedFrame> frame; |
| 173 | video_coding::FrameBuffer::ReturnReason res = |
| 174 | frame_buffer_.NextFrame(max_wait_time_ms, &frame, keyframe_required); |
| 175 | |
| 176 | if (res == video_coding::FrameBuffer::ReturnReason::kStopped) |
| 177 | return kShutdown; |
| 178 | |
| 179 | if (frame) { |
| 180 | VideoDecoder* decoder = GetDecoder(frame->PayloadType()); |
| 181 | if (!decoder) { |
| 182 | RTC_LOG(LS_WARNING) << "Failed to get decoder, dropping frame (" |
| 183 | << frame->id.picture_id << ":" |
| 184 | << frame->id.spatial_layer << ")."; |
| 185 | return kNoDecoder; |
| 186 | } |
| 187 | |
| 188 | int64_t decode_start_time_ms = rtc::TimeMillis(); |
| 189 | uint32_t frame_timestamp = frame->timestamp; |
| 190 | bookkeeping_queue_.PostTask( |
| 191 | [this, decode_start_time_ms, frame_timestamp]() { |
| 192 | RTC_DCHECK_RUN_ON(&bookkeeping_queue_); |
| 193 | // Saving decode start time this way wont work if we decode spatial |
| 194 | // layers sequentially. |
| 195 | decode_start_time_[next_start_time_index_] = {frame_timestamp, |
| 196 | decode_start_time_ms}; |
| 197 | next_start_time_index_ = |
| 198 | Add<kDecodeTimeMemory>(next_start_time_index_, 1); |
| 199 | }); |
| 200 | |
| 201 | int32_t decode_result = |
| 202 | decoder->Decode(frame->EncodedImage(), |
| 203 | false, // missing_frame |
| 204 | nullptr, // rtp fragmentation header |
| 205 | nullptr, // codec specific info |
| 206 | frame->RenderTimeMs()); |
| 207 | |
| 208 | return decode_result == WEBRTC_VIDEO_CODEC_OK ? kOk : kDecodeFailure; |
| 209 | } |
| 210 | |
| 211 | return kNoFrame; |
| 212 | } |
| 213 | |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 214 | } // namespace webrtc |