[M120 Migration][hbbtv] Audio tracks count notification
[platform/framework/web/chromium-efl.git] / media / filters / decoder_stream_traits.h
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.
4
5 #ifndef MEDIA_FILTERS_DECODER_STREAM_TRAITS_H_
6 #define MEDIA_FILTERS_DECODER_STREAM_TRAITS_H_
7
8 #include <memory>
9 #include <string>
10
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"
24
25 namespace media {
26
27 class AudioBuffer;
28 class CdmContext;
29 class DemuxerStream;
30 class VideoDecoderConfig;
31 class VideoFrame;
32
33 template <DemuxerStream::Type StreamType>
34 class DecoderStreamTraits {};
35
36 enum class PostDecodeAction { DELIVER, DROP };
37
38 template <>
39 class MEDIA_EXPORT DecoderStreamTraits<DemuxerStream::AUDIO> {
40  public:
41   using OutputType = AudioBuffer;
42   using DecoderType = AudioDecoder;
43   using DecoderConfigType = AudioDecoderConfig;
44   using InitCB = AudioDecoder::InitCB;
45   using OutputCB = AudioDecoder::OutputCB;
46
47   static const MediaLogProperty kDecoderName =
48       MediaLogProperty::kAudioDecoderName;
49   static const MediaLogProperty kIsPlatformDecoder =
50       MediaLogProperty::kIsPlatformAudioDecoder;
51   static const MediaLogProperty kIsDecryptingDemuxerStream =
52       MediaLogProperty::kIsAudioDecryptingDemuxerStream;
53
54   static std::string ToString();
55   static bool NeedsBitstreamConversion(DecoderType* decoder);
56   static scoped_refptr<OutputType> CreateEOSOutput();
57
58   DecoderStreamTraits(MediaLog* media_log,
59                       ChannelLayout initial_hw_layout,
60                       SampleFormat initial_hw_sample_format);
61
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,
68                          bool low_delay,
69                          CdmContext* cdm_context,
70                          InitCB init_cb,
71                          const OutputCB& output_cb,
72                          const WaitingCB& waiting_cb);
73   void OnDecoderInitialized(DecoderType* decoder,
74                             InitCB cb,
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);
81
82  private:
83   void OnConfigChanged(const AudioDecoderConfig& config);
84
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
87   // drift.
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
91   // device changes.
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_;
98
99   base::WeakPtr<DecoderStreamTraits<DemuxerStream::AUDIO>> weak_this_;
100   base::WeakPtrFactory<DecoderStreamTraits<DemuxerStream::AUDIO>> weak_factory_{
101       this};
102 };
103
104 template <>
105 class MEDIA_EXPORT DecoderStreamTraits<DemuxerStream::VIDEO> {
106  public:
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;
118
119   static std::string ToString();
120   static bool NeedsBitstreamConversion(DecoderType* decoder);
121   static scoped_refptr<OutputType> CreateEOSOutput();
122
123   explicit DecoderStreamTraits(MediaLog* media_log);
124
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,
132                          bool low_delay,
133                          CdmContext* cdm_context,
134                          InitCB init_cb,
135                          const OutputCB& output_cb,
136                          const WaitingCB& waiting_cb);
137   void OnDecoderInitialized(DecoderType* decoder,
138                             InitCB cb,
139                             DecoderStatus status);
140   void OnDecode(const DecoderBuffer& buffer);
141   PostDecodeAction OnDecodeDone(OutputType* buffer);
142   void OnStreamReset(DemuxerStream* stream);
143   void OnOutputReady(OutputType* output);
144
145   // Set whether or not software decoder implementations will be preferred.
146   void SetPreferNonPlatformDecoders(bool);
147   bool GetPreferNonPlatformDecoders() const;
148
149  private:
150   base::TimeDelta last_keyframe_timestamp_;
151   base::MovingAverage<base::TimeDelta, base::TimeDelta>
152       keyframe_distance_average_;
153
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;
159   };
160   base::flat_map<base::TimeDelta, FrameMetadata> frame_metadata_;
161
162   PipelineStatistics stats_;
163
164   VideoTransformation transform_ = kNoTransformation;
165
166   bool prefer_non_platform_decoders_ = false;
167
168   base::WeakPtr<DecoderStreamTraits<DemuxerStream::VIDEO>> weak_this_;
169   base::WeakPtrFactory<DecoderStreamTraits<DemuxerStream::VIDEO>> weak_factory_{
170       this};
171 };
172
173 }  // namespace media
174
175 #endif  // MEDIA_FILTERS_DECODER_STREAM_TRAITS_H_