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