generic_decoder.h 4.0 KB

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