You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

287 lines
9.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "video/video_stream_decoder_impl.h"
  11. #include <memory>
  12. #include "api/task_queue/queued_task.h"
  13. #include "rtc_base/logging.h"
  14. #include "rtc_base/numerics/mod_ops.h"
  15. #include "rtc_base/time_utils.h"
  16. namespace webrtc {
  17. VideoStreamDecoderImpl::VideoStreamDecoderImpl(
  18. VideoStreamDecoderInterface::Callbacks* callbacks,
  19. VideoDecoderFactory* decoder_factory,
  20. TaskQueueFactory* task_queue_factory,
  21. std::map<int, std::pair<SdpVideoFormat, int>> decoder_settings)
  22. : callbacks_(callbacks),
  23. decoder_factory_(decoder_factory),
  24. decoder_settings_(std::move(decoder_settings)),
  25. bookkeeping_queue_(task_queue_factory->CreateTaskQueue(
  26. "video_stream_decoder_bookkeeping_queue",
  27. TaskQueueFactory::Priority::NORMAL)),
  28. decode_thread_(&DecodeLoop,
  29. this,
  30. "video_stream_decoder_decode_thread",
  31. rtc::kHighestPriority),
  32. timing_(Clock::GetRealTimeClock()),
  33. frame_buffer_(Clock::GetRealTimeClock(), &timing_, nullptr),
  34. next_frame_timestamps_index_(0) {
  35. frame_timestamps_.fill({-1, -1, -1});
  36. decode_thread_.Start();
  37. }
  38. VideoStreamDecoderImpl::~VideoStreamDecoderImpl() {
  39. frame_buffer_.Stop();
  40. decode_thread_.Stop();
  41. }
  42. void VideoStreamDecoderImpl::OnFrame(
  43. std::unique_ptr<video_coding::EncodedFrame> frame) {
  44. if (!bookkeeping_queue_.IsCurrent()) {
  45. struct OnFrameTask : QueuedTask {
  46. OnFrameTask(std::unique_ptr<video_coding::EncodedFrame> frame,
  47. VideoStreamDecoderImpl* video_stream_decoder)
  48. : frame_(std::move(frame)),
  49. video_stream_decoder_(video_stream_decoder) {}
  50. bool Run() override {
  51. video_stream_decoder_->OnFrame(std::move(frame_));
  52. return true;
  53. }
  54. std::unique_ptr<video_coding::EncodedFrame> frame_;
  55. VideoStreamDecoderImpl* video_stream_decoder_;
  56. };
  57. bookkeeping_queue_.PostTask(
  58. std::make_unique<OnFrameTask>(std::move(frame), this));
  59. return;
  60. }
  61. RTC_DCHECK_RUN_ON(&bookkeeping_queue_);
  62. uint64_t continuous_pid = frame_buffer_.InsertFrame(std::move(frame));
  63. video_coding::VideoLayerFrameId continuous_id(continuous_pid, 0);
  64. if (last_continuous_id_ < continuous_id) {
  65. last_continuous_id_ = continuous_id;
  66. callbacks_->OnContinuousUntil(last_continuous_id_);
  67. }
  68. }
  69. void VideoStreamDecoderImpl::SetMinPlayoutDelay(TimeDelta min_delay) {
  70. timing_.set_min_playout_delay(min_delay.ms());
  71. }
  72. void VideoStreamDecoderImpl::SetMaxPlayoutDelay(TimeDelta max_delay) {
  73. timing_.set_max_playout_delay(max_delay.ms());
  74. }
  75. VideoDecoder* VideoStreamDecoderImpl::GetDecoder(int payload_type) {
  76. if (current_payload_type_ == payload_type) {
  77. RTC_DCHECK(decoder_);
  78. return decoder_.get();
  79. }
  80. current_payload_type_.reset();
  81. decoder_.reset();
  82. auto decoder_settings_it = decoder_settings_.find(payload_type);
  83. if (decoder_settings_it == decoder_settings_.end()) {
  84. RTC_LOG(LS_WARNING) << "Payload type " << payload_type
  85. << " not registered.";
  86. return nullptr;
  87. }
  88. const SdpVideoFormat& video_format = decoder_settings_it->second.first;
  89. std::unique_ptr<VideoDecoder> decoder =
  90. decoder_factory_->CreateVideoDecoder(video_format);
  91. if (!decoder) {
  92. RTC_LOG(LS_WARNING) << "Failed to create decoder for payload type "
  93. << payload_type << ".";
  94. return nullptr;
  95. }
  96. int num_cores = decoder_settings_it->second.second;
  97. int32_t init_result = decoder->InitDecode(nullptr, num_cores);
  98. if (init_result != WEBRTC_VIDEO_CODEC_OK) {
  99. RTC_LOG(LS_WARNING) << "Failed to initialize decoder for payload type "
  100. << payload_type << ".";
  101. return nullptr;
  102. }
  103. int32_t register_result = decoder->RegisterDecodeCompleteCallback(this);
  104. if (register_result != WEBRTC_VIDEO_CODEC_OK) {
  105. RTC_LOG(LS_WARNING) << "Failed to register decode callback.";
  106. return nullptr;
  107. }
  108. current_payload_type_.emplace(payload_type);
  109. decoder_ = std::move(decoder);
  110. return decoder_.get();
  111. }
  112. // static
  113. void VideoStreamDecoderImpl::DecodeLoop(void* ptr) {
  114. // TODO(philipel): Remove this and use rtc::Event::kForever when it's
  115. // supported by the |frame_buffer_|.
  116. static constexpr int kForever = 100000000;
  117. int max_wait_time_ms = kForever;
  118. bool keyframe_required = true;
  119. auto* vs_decoder = static_cast<VideoStreamDecoderImpl*>(ptr);
  120. while (true) {
  121. DecodeResult decode_result =
  122. vs_decoder->DecodeNextFrame(max_wait_time_ms, keyframe_required);
  123. switch (decode_result) {
  124. case kOk: {
  125. max_wait_time_ms = kForever;
  126. keyframe_required = false;
  127. break;
  128. }
  129. case kDecodeFailure: {
  130. max_wait_time_ms = 0;
  131. keyframe_required = true;
  132. break;
  133. }
  134. case kNoFrame: {
  135. max_wait_time_ms = kForever;
  136. // If we end up here it means that we got a decoding error and there is
  137. // no keyframe available in the |frame_buffer_|.
  138. vs_decoder->bookkeeping_queue_.PostTask([vs_decoder]() {
  139. RTC_DCHECK_RUN_ON(&vs_decoder->bookkeeping_queue_);
  140. vs_decoder->callbacks_->OnNonDecodableState();
  141. });
  142. break;
  143. }
  144. case kNoDecoder: {
  145. max_wait_time_ms = kForever;
  146. break;
  147. }
  148. case kShutdown: {
  149. return;
  150. }
  151. }
  152. }
  153. }
  154. VideoStreamDecoderImpl::DecodeResult VideoStreamDecoderImpl::DecodeNextFrame(
  155. int max_wait_time_ms,
  156. bool keyframe_required) {
  157. std::unique_ptr<video_coding::EncodedFrame> frame;
  158. video_coding::FrameBuffer::ReturnReason res =
  159. frame_buffer_.NextFrame(max_wait_time_ms, &frame, keyframe_required);
  160. if (res == video_coding::FrameBuffer::ReturnReason::kStopped)
  161. return kShutdown;
  162. if (frame) {
  163. VideoDecoder* decoder = GetDecoder(frame->PayloadType());
  164. if (!decoder) {
  165. RTC_LOG(LS_WARNING) << "Failed to get decoder, dropping frame ("
  166. << frame->id.picture_id << ":"
  167. << frame->id.spatial_layer << ").";
  168. return kNoDecoder;
  169. }
  170. int64_t decode_start_time_ms = rtc::TimeMillis();
  171. int64_t timestamp = frame->Timestamp();
  172. int64_t render_time_us = frame->RenderTimeMs() * 1000;
  173. bookkeeping_queue_.PostTask(
  174. [this, decode_start_time_ms, timestamp, render_time_us]() {
  175. RTC_DCHECK_RUN_ON(&bookkeeping_queue_);
  176. // Saving decode start time this way wont work if we decode spatial
  177. // layers sequentially.
  178. FrameTimestamps* frame_timestamps =
  179. &frame_timestamps_[next_frame_timestamps_index_];
  180. frame_timestamps->timestamp = timestamp;
  181. frame_timestamps->decode_start_time_ms = decode_start_time_ms;
  182. frame_timestamps->render_time_us = render_time_us;
  183. next_frame_timestamps_index_ =
  184. Add<kFrameTimestampsMemory>(next_frame_timestamps_index_, 1);
  185. });
  186. int32_t decode_result = decoder->Decode(frame->EncodedImage(),
  187. false, // missing_frame
  188. frame->RenderTimeMs());
  189. return decode_result == WEBRTC_VIDEO_CODEC_OK ? kOk : kDecodeFailure;
  190. }
  191. return kNoFrame;
  192. }
  193. VideoStreamDecoderImpl::FrameTimestamps*
  194. VideoStreamDecoderImpl::GetFrameTimestamps(int64_t timestamp) {
  195. RTC_DCHECK_RUN_ON(&bookkeeping_queue_);
  196. int start_time_index = next_frame_timestamps_index_;
  197. for (int i = 0; i < kFrameTimestampsMemory; ++i) {
  198. start_time_index = Subtract<kFrameTimestampsMemory>(start_time_index, 1);
  199. if (frame_timestamps_[start_time_index].timestamp == timestamp)
  200. return &frame_timestamps_[start_time_index];
  201. }
  202. return nullptr;
  203. }
  204. // VideoDecoder::DecodedImageCallback
  205. int32_t VideoStreamDecoderImpl::Decoded(VideoFrame& decoded_image) {
  206. Decoded(decoded_image, absl::nullopt, absl::nullopt);
  207. return WEBRTC_VIDEO_CODEC_OK;
  208. }
  209. // VideoDecoder::DecodedImageCallback
  210. int32_t VideoStreamDecoderImpl::Decoded(VideoFrame& decoded_image,
  211. int64_t decode_time_ms) {
  212. Decoded(decoded_image, decode_time_ms, absl::nullopt);
  213. return WEBRTC_VIDEO_CODEC_OK;
  214. }
  215. // VideoDecoder::DecodedImageCallback
  216. void VideoStreamDecoderImpl::Decoded(VideoFrame& decoded_image,
  217. absl::optional<int32_t> decode_time_ms,
  218. absl::optional<uint8_t> qp) {
  219. int64_t decode_stop_time_ms = rtc::TimeMillis();
  220. bookkeeping_queue_.PostTask([this, decode_stop_time_ms, decoded_image,
  221. decode_time_ms, qp]() {
  222. RTC_DCHECK_RUN_ON(&bookkeeping_queue_);
  223. FrameTimestamps* frame_timestamps =
  224. GetFrameTimestamps(decoded_image.timestamp());
  225. if (!frame_timestamps) {
  226. RTC_LOG(LS_ERROR) << "No frame information found for frame with timestamp"
  227. << decoded_image.timestamp();
  228. return;
  229. }
  230. absl::optional<int> casted_qp;
  231. if (qp)
  232. casted_qp.emplace(*qp);
  233. absl::optional<int> casted_decode_time_ms(decode_time_ms.value_or(
  234. decode_stop_time_ms - frame_timestamps->decode_start_time_ms));
  235. timing_.StopDecodeTimer(*casted_decode_time_ms, decode_stop_time_ms);
  236. VideoFrame copy = decoded_image;
  237. copy.set_timestamp_us(frame_timestamps->render_time_us);
  238. callbacks_->OnDecodedFrame(copy, casted_decode_time_ms, casted_qp);
  239. });
  240. }
  241. } // namespace webrtc