video_stream_decoder_impl.h 4.8 KB

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