video_stream_decoder_impl.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #ifndef VIDEO_VIDEO_STREAM_DECODER_IMPL_H_
  11. #define VIDEO_VIDEO_STREAM_DECODER_IMPL_H_
  12. #include <map>
  13. #include <memory>
  14. #include <utility>
  15. #include "absl/types/optional.h"
  16. #include "api/video/video_stream_decoder.h"
  17. #include "modules/video_coding/frame_buffer2.h"
  18. #include "modules/video_coding/timing.h"
  19. #include "rtc_base/platform_thread.h"
  20. #include "rtc_base/synchronization/mutex.h"
  21. #include "rtc_base/task_queue.h"
  22. #include "rtc_base/thread_checker.h"
  23. #include "system_wrappers/include/clock.h"
  24. namespace webrtc {
  25. class VideoStreamDecoderImpl : public VideoStreamDecoderInterface {
  26. public:
  27. VideoStreamDecoderImpl(
  28. VideoStreamDecoderInterface::Callbacks* callbacks,
  29. VideoDecoderFactory* decoder_factory,
  30. TaskQueueFactory* task_queue_factory,
  31. std::map<int, std::pair<SdpVideoFormat, int>> decoder_settings);
  32. ~VideoStreamDecoderImpl() override;
  33. void OnFrame(std::unique_ptr<video_coding::EncodedFrame> frame) override;
  34. void SetMinPlayoutDelay(TimeDelta min_delay) override;
  35. void SetMaxPlayoutDelay(TimeDelta max_delay) override;
  36. private:
  37. class DecodeCallbacks : public DecodedImageCallback {
  38. public:
  39. explicit DecodeCallbacks(VideoStreamDecoderImpl* video_stream_decoder_impl);
  40. int32_t Decoded(VideoFrame& decodedImage) override;
  41. int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override;
  42. void Decoded(VideoFrame& decodedImage,
  43. absl::optional<int32_t> decode_time_ms,
  44. absl::optional<uint8_t> qp) override;
  45. private:
  46. VideoStreamDecoderImpl* const video_stream_decoder_impl_;
  47. };
  48. enum DecodeResult {
  49. kOk,
  50. kOkRequestKeyframe,
  51. kDecodeFailure,
  52. };
  53. struct FrameInfo {
  54. int64_t timestamp = -1;
  55. int64_t decode_start_time_ms;
  56. int64_t render_time_us;
  57. VideoContentType content_type;
  58. };
  59. void SaveFrameInfo(const video_coding::EncodedFrame& frame)
  60. RTC_RUN_ON(bookkeeping_queue_);
  61. FrameInfo* GetFrameInfo(int64_t timestamp) RTC_RUN_ON(bookkeeping_queue_);
  62. void StartNextDecode() RTC_RUN_ON(bookkeeping_queue_);
  63. void OnNextFrameCallback(std::unique_ptr<video_coding::EncodedFrame> frame,
  64. video_coding::FrameBuffer::ReturnReason res)
  65. RTC_RUN_ON(bookkeeping_queue_);
  66. void OnDecodedFrameCallback(VideoFrame& decodedImage, // NOLINT
  67. absl::optional<int32_t> decode_time_ms,
  68. absl::optional<uint8_t> qp);
  69. VideoDecoder* GetDecoder(int payload_type) RTC_RUN_ON(decode_queue_);
  70. VideoStreamDecoderImpl::DecodeResult DecodeFrame(
  71. std::unique_ptr<video_coding::EncodedFrame> frame)
  72. RTC_RUN_ON(decode_queue_);
  73. VCMTiming timing_;
  74. DecodeCallbacks decode_callbacks_;
  75. // Some decoders are pipelined so it is not sufficient to save frame info
  76. // for the last frame only.
  77. static constexpr int kFrameInfoMemory = 8;
  78. std::array<FrameInfo, kFrameInfoMemory> frame_info_
  79. RTC_GUARDED_BY(bookkeeping_queue_);
  80. int next_frame_info_index_ RTC_GUARDED_BY(bookkeeping_queue_);
  81. VideoStreamDecoderInterface::Callbacks* const callbacks_
  82. RTC_PT_GUARDED_BY(bookkeeping_queue_);
  83. video_coding::VideoLayerFrameId last_continuous_id_
  84. RTC_GUARDED_BY(bookkeeping_queue_);
  85. bool keyframe_required_ RTC_GUARDED_BY(bookkeeping_queue_);
  86. absl::optional<int> current_payload_type_ RTC_GUARDED_BY(decode_queue_);
  87. VideoDecoderFactory* const decoder_factory_ RTC_PT_GUARDED_BY(decode_queue_);
  88. std::map<int, std::pair<SdpVideoFormat, int>> decoder_settings_
  89. RTC_GUARDED_BY(decode_queue_);
  90. // The |bookkeeping_queue_| use the |frame_buffer_| and also posts tasks to
  91. // the |decode_queue_|. The |decode_queue_| in turn use the |decoder_| to
  92. // decode frames. When the |decoder_| is done it will post back to the
  93. // |bookkeeping_queue_| with the decoded frame. During shutdown we start by
  94. // isolating the |bookkeeping_queue_| from the |decode_queue_|, so now it's
  95. // safe for the |decode_queue_| to be destructed. After that the |decoder_|
  96. // can be destructed, and then the |bookkeeping_queue_|. Finally the
  97. // |frame_buffer_| can be destructed.
  98. Mutex shut_down_mutex_;
  99. bool shut_down_ RTC_GUARDED_BY(shut_down_mutex_);
  100. video_coding::FrameBuffer frame_buffer_ RTC_GUARDED_BY(bookkeeping_queue_);
  101. rtc::TaskQueue bookkeeping_queue_;
  102. std::unique_ptr<VideoDecoder> decoder_ RTC_GUARDED_BY(decode_queue_);
  103. rtc::TaskQueue decode_queue_;
  104. };
  105. } // namespace webrtc
  106. #endif // VIDEO_VIDEO_STREAM_DECODER_IMPL_H_