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.

119 lines
3.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2012 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 MODULES_VIDEO_CODING_GENERIC_DECODER_H_
  11. #define MODULES_VIDEO_CODING_GENERIC_DECODER_H_
  12. #include <memory>
  13. #include "api/units/time_delta.h"
  14. #include "modules/video_coding/encoded_frame.h"
  15. #include "modules/video_coding/include/video_codec_interface.h"
  16. #include "modules/video_coding/timestamp_map.h"
  17. #include "modules/video_coding/timing.h"
  18. #include "rtc_base/critical_section.h"
  19. #include "rtc_base/experiments/field_trial_parser.h"
  20. #include "rtc_base/thread_checker.h"
  21. namespace webrtc {
  22. class VCMReceiveCallback;
  23. enum { kDecoderFrameMemoryLength = 10 };
  24. struct VCMFrameInformation {
  25. int64_t renderTimeMs;
  26. int64_t decodeStartTimeMs;
  27. void* userData;
  28. VideoRotation rotation;
  29. VideoContentType content_type;
  30. EncodedImage::Timing timing;
  31. int64_t ntp_time_ms;
  32. RtpPacketInfos packet_infos;
  33. // ColorSpace is not storred here, as it might be modified by decoders.
  34. };
  35. class VCMDecodedFrameCallback : public DecodedImageCallback {
  36. public:
  37. VCMDecodedFrameCallback(VCMTiming* timing, Clock* clock);
  38. ~VCMDecodedFrameCallback() override;
  39. void SetUserReceiveCallback(VCMReceiveCallback* receiveCallback);
  40. VCMReceiveCallback* UserReceiveCallback();
  41. int32_t Decoded(VideoFrame& decodedImage) override;
  42. int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override;
  43. void Decoded(VideoFrame& decodedImage,
  44. absl::optional<int32_t> decode_time_ms,
  45. absl::optional<uint8_t> qp) override;
  46. void OnDecoderImplementationName(const char* implementation_name);
  47. void Map(uint32_t timestamp, VCMFrameInformation* frameInfo);
  48. int32_t Pop(uint32_t timestamp);
  49. private:
  50. rtc::ThreadChecker construction_thread_;
  51. // Protect |_timestampMap|.
  52. Clock* const _clock;
  53. // This callback must be set before the decoder thread starts running
  54. // and must only be unset when external threads (e.g decoder thread)
  55. // have been stopped. Due to that, the variable should regarded as const
  56. // while there are more than one threads involved, it must be set
  57. // from the same thread, and therfore a lock is not required to access it.
  58. VCMReceiveCallback* _receiveCallback = nullptr;
  59. VCMTiming* _timing;
  60. rtc::CriticalSection lock_;
  61. VCMTimestampMap _timestampMap RTC_GUARDED_BY(lock_);
  62. int64_t ntp_offset_;
  63. // Set by the field trial WebRTC-SlowDownDecoder to simulate a slow decoder.
  64. FieldTrialOptional<TimeDelta> _extra_decode_time;
  65. };
  66. class VCMGenericDecoder {
  67. public:
  68. explicit VCMGenericDecoder(std::unique_ptr<VideoDecoder> decoder);
  69. explicit VCMGenericDecoder(VideoDecoder* decoder, bool isExternal = false);
  70. ~VCMGenericDecoder();
  71. /**
  72. * Initialize the decoder with the information from the VideoCodec
  73. */
  74. int32_t InitDecode(const VideoCodec* settings, int32_t numberOfCores);
  75. /**
  76. * Decode to a raw I420 frame,
  77. *
  78. * inputVideoBuffer reference to encoded video frame
  79. */
  80. int32_t Decode(const VCMEncodedFrame& inputFrame, int64_t nowMs);
  81. /**
  82. * Set decode callback. Deregistering while decoding is illegal.
  83. */
  84. int32_t RegisterDecodeCompleteCallback(VCMDecodedFrameCallback* callback);
  85. bool PrefersLateDecoding() const;
  86. bool IsSameDecoder(VideoDecoder* decoder) const {
  87. return decoder_.get() == decoder;
  88. }
  89. private:
  90. VCMDecodedFrameCallback* _callback;
  91. VCMFrameInformation _frameInfos[kDecoderFrameMemoryLength];
  92. uint32_t _nextFrameInfoIdx;
  93. std::unique_ptr<VideoDecoder> decoder_;
  94. VideoCodecType _codecType;
  95. const bool _isExternal;
  96. VideoContentType _last_keyframe_content_type;
  97. };
  98. } // namespace webrtc
  99. #endif // MODULES_VIDEO_CODING_GENERIC_DECODER_H_