decoder_stream_traits.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef MEDIA_FILTERS_DECODER_STREAM_TRAITS_H_
  5. #define MEDIA_FILTERS_DECODER_STREAM_TRAITS_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/containers/flat_map.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/time/time.h"
  11. #include "media/base/audio_decoder.h"
  12. #include "media/base/audio_decoder_config.h"
  13. #include "media/base/channel_layout.h"
  14. #include "media/base/demuxer_stream.h"
  15. #include "media/base/media_log_properties.h"
  16. #include "media/base/moving_average.h"
  17. #include "media/base/pipeline_status.h"
  18. #include "media/base/sample_format.h"
  19. #include "media/base/video_decoder.h"
  20. #include "media/filters/audio_timestamp_validator.h"
  21. namespace media {
  22. class AudioBuffer;
  23. class CdmContext;
  24. class DemuxerStream;
  25. class VideoDecoderConfig;
  26. class VideoFrame;
  27. template <DemuxerStream::Type StreamType>
  28. class DecoderStreamTraits {};
  29. enum class PostDecodeAction { DELIVER, DROP };
  30. template <>
  31. class MEDIA_EXPORT DecoderStreamTraits<DemuxerStream::AUDIO> {
  32. public:
  33. using OutputType = AudioBuffer;
  34. using DecoderType = AudioDecoder;
  35. using DecoderConfigType = AudioDecoderConfig;
  36. using InitCB = AudioDecoder::InitCB;
  37. using OutputCB = AudioDecoder::OutputCB;
  38. static const MediaLogProperty kDecoderName =
  39. MediaLogProperty::kAudioDecoderName;
  40. static const MediaLogProperty kIsPlatformDecoder =
  41. MediaLogProperty::kIsPlatformAudioDecoder;
  42. static const MediaLogProperty kIsDecryptingDemuxerStream =
  43. MediaLogProperty::kIsAudioDecryptingDemuxerStream;
  44. static std::string ToString();
  45. static bool NeedsBitstreamConversion(DecoderType* decoder);
  46. static scoped_refptr<OutputType> CreateEOSOutput();
  47. DecoderStreamTraits(MediaLog* media_log,
  48. ChannelLayout initial_hw_layout,
  49. SampleFormat initial_hw_sample_format);
  50. void ReportStatistics(const StatisticsCB& statistics_cb, int bytes_decoded);
  51. void SetIsPlatformDecoder(bool is_platform_decoder);
  52. void SetIsDecryptingDemuxerStream(bool is_dds);
  53. void SetEncryptionType(EncryptionType decryption_type);
  54. void InitializeDecoder(DecoderType* decoder,
  55. const DecoderConfigType& config,
  56. bool low_delay,
  57. CdmContext* cdm_context,
  58. InitCB init_cb,
  59. const OutputCB& output_cb,
  60. const WaitingCB& waiting_cb);
  61. void OnDecoderInitialized(DecoderType* decoder,
  62. InitCB cb,
  63. DecoderStatus status);
  64. DecoderConfigType GetDecoderConfig(DemuxerStream* stream);
  65. void OnDecode(const DecoderBuffer& buffer);
  66. PostDecodeAction OnDecodeDone(OutputType* buffer);
  67. void OnStreamReset(DemuxerStream* stream);
  68. void OnOutputReady(OutputType* output);
  69. private:
  70. void OnConfigChanged(const AudioDecoderConfig& config);
  71. // Validates encoded timestamps match decoded output duration. MEDIA_LOG warns
  72. // if timestamp gaps are detected. Sufficiently large gaps can lead to AV sync
  73. // drift.
  74. std::unique_ptr<AudioTimestampValidator> audio_ts_validator_;
  75. raw_ptr<MediaLog> media_log_;
  76. // HW layout at the time pipeline was started. Will not reflect possible
  77. // device changes.
  78. ChannelLayout initial_hw_layout_;
  79. // HW sample format at the time pipeline was started. Will not reflect
  80. // possible device changes.
  81. SampleFormat initial_hw_sample_format_;
  82. PipelineStatistics stats_;
  83. AudioDecoderConfig config_;
  84. base::WeakPtr<DecoderStreamTraits<DemuxerStream::AUDIO>> weak_this_;
  85. base::WeakPtrFactory<DecoderStreamTraits<DemuxerStream::AUDIO>> weak_factory_{
  86. this};
  87. };
  88. template <>
  89. class MEDIA_EXPORT DecoderStreamTraits<DemuxerStream::VIDEO> {
  90. public:
  91. using OutputType = VideoFrame;
  92. using DecoderType = VideoDecoder;
  93. using DecoderConfigType = VideoDecoderConfig;
  94. using InitCB = VideoDecoder::InitCB;
  95. using OutputCB = VideoDecoder::OutputCB;
  96. static const MediaLogProperty kDecoderName =
  97. MediaLogProperty::kVideoDecoderName;
  98. static const MediaLogProperty kIsPlatformDecoder =
  99. MediaLogProperty::kIsPlatformVideoDecoder;
  100. static const MediaLogProperty kIsDecryptingDemuxerStream =
  101. MediaLogProperty::kIsVideoDecryptingDemuxerStream;
  102. static std::string ToString();
  103. static bool NeedsBitstreamConversion(DecoderType* decoder);
  104. static scoped_refptr<OutputType> CreateEOSOutput();
  105. explicit DecoderStreamTraits(MediaLog* media_log);
  106. DecoderConfigType GetDecoderConfig(DemuxerStream* stream);
  107. void ReportStatistics(const StatisticsCB& statistics_cb, int bytes_decoded);
  108. void SetIsPlatformDecoder(bool is_platform_decoder);
  109. void SetIsDecryptingDemuxerStream(bool is_dds);
  110. void SetEncryptionType(EncryptionType decryption_type);
  111. void InitializeDecoder(DecoderType* decoder,
  112. const DecoderConfigType& config,
  113. bool low_delay,
  114. CdmContext* cdm_context,
  115. InitCB init_cb,
  116. const OutputCB& output_cb,
  117. const WaitingCB& waiting_cb);
  118. void OnDecoderInitialized(DecoderType* decoder,
  119. InitCB cb,
  120. DecoderStatus status);
  121. void OnDecode(const DecoderBuffer& buffer);
  122. PostDecodeAction OnDecodeDone(OutputType* buffer);
  123. void OnStreamReset(DemuxerStream* stream);
  124. void OnOutputReady(OutputType* output);
  125. // Set whether or not software decoder implementations will be preferred.
  126. void SetPreferNonPlatformDecoders(bool);
  127. bool GetPreferNonPlatformDecoders() const;
  128. private:
  129. base::TimeDelta last_keyframe_timestamp_;
  130. MovingAverage keyframe_distance_average_;
  131. // Tracks the duration of incoming packets over time.
  132. struct FrameMetadata {
  133. bool should_drop = false;
  134. base::TimeDelta duration = kNoTimestamp;
  135. base::TimeTicks decode_begin_time;
  136. };
  137. base::flat_map<base::TimeDelta, FrameMetadata> frame_metadata_;
  138. PipelineStatistics stats_;
  139. VideoTransformation transform_ = kNoTransformation;
  140. bool prefer_non_platform_decoders_ = false;
  141. base::WeakPtr<DecoderStreamTraits<DemuxerStream::VIDEO>> weak_this_;
  142. base::WeakPtrFactory<DecoderStreamTraits<DemuxerStream::VIDEO>> weak_factory_{
  143. this};
  144. };
  145. } // namespace media
  146. #endif // MEDIA_FILTERS_DECODER_STREAM_TRAITS_H_