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.

101 lines
3.3 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. #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. private DecodedImageCallback {
  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. enum DecodeResult {
  38. kOk,
  39. kDecodeFailure,
  40. kNoFrame,
  41. kNoDecoder,
  42. kShutdown,
  43. };
  44. struct FrameTimestamps {
  45. int64_t timestamp;
  46. int64_t decode_start_time_ms;
  47. int64_t render_time_us;
  48. };
  49. VideoDecoder* GetDecoder(int payload_type);
  50. static void DecodeLoop(void* ptr);
  51. DecodeResult DecodeNextFrame(int max_wait_time_ms, bool keyframe_required);
  52. FrameTimestamps* GetFrameTimestamps(int64_t timestamp);
  53. // Implements DecodedImageCallback interface
  54. int32_t Decoded(VideoFrame& decodedImage) override;
  55. int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override;
  56. void Decoded(VideoFrame& decodedImage,
  57. absl::optional<int32_t> decode_time_ms,
  58. absl::optional<uint8_t> qp) override;
  59. VideoStreamDecoderInterface::Callbacks* const callbacks_
  60. RTC_PT_GUARDED_BY(bookkeeping_queue_);
  61. VideoDecoderFactory* const decoder_factory_;
  62. std::map<int, std::pair<SdpVideoFormat, int>> decoder_settings_;
  63. // The |bookkeeping_queue_| is used to:
  64. // - Make |callbacks_|.
  65. // - Insert/extract frames from the |frame_buffer_|
  66. // - Synchronize with whatever thread that makes the Decoded callback.
  67. rtc::TaskQueue bookkeeping_queue_;
  68. rtc::PlatformThread decode_thread_;
  69. VCMTiming timing_;
  70. video_coding::FrameBuffer frame_buffer_;
  71. video_coding::VideoLayerFrameId last_continuous_id_;
  72. absl::optional<int> current_payload_type_;
  73. std::unique_ptr<VideoDecoder> decoder_;
  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. };
  81. } // namespace webrtc
  82. #endif // VIDEO_VIDEO_STREAM_DECODER_IMPL_H_