[M120 Migration][hbbtv] Audio tracks count notification
[platform/framework/web/chromium-efl.git] / media / filters / ffmpeg_audio_decoder.h
1 // Copyright 2012 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_FFMPEG_AUDIO_DECODER_H_
6 #define MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_
7
8 #include <memory>
9
10 #include "base/functional/callback.h"
11 #include "base/memory/raw_ptr.h"
12 #include "base/sequence_checker.h"
13 #include "media/base/audio_buffer.h"
14 #include "media/base/audio_decoder.h"
15 #include "media/base/demuxer_stream.h"
16 #include "media/base/media_log.h"
17 #include "media/base/sample_format.h"
18 #include "media/ffmpeg/ffmpeg_deleters.h"
19
20 struct AVCodecContext;
21 struct AVFrame;
22
23 namespace base {
24 class SequencedTaskRunner;
25 }
26
27 namespace media {
28
29 class AudioDiscardHelper;
30 class DecoderBuffer;
31 class FFmpegDecodingLoop;
32
33 class MEDIA_EXPORT FFmpegAudioDecoder : public AudioDecoder {
34  public:
35   FFmpegAudioDecoder() = delete;
36
37   FFmpegAudioDecoder(
38       const scoped_refptr<base::SequencedTaskRunner>& task_runner,
39       MediaLog* media_log);
40
41   FFmpegAudioDecoder(const FFmpegAudioDecoder&) = delete;
42   FFmpegAudioDecoder& operator=(const FFmpegAudioDecoder&) = delete;
43
44   ~FFmpegAudioDecoder() override;
45
46   // AudioDecoder implementation.
47   AudioDecoderType GetDecoderType() const override;
48   void Initialize(const AudioDecoderConfig& config,
49                   CdmContext* cdm_context,
50                   InitCB init_cb,
51                   const OutputCB& output_cb,
52                   const WaitingCB& waiting_cb) override;
53   void Decode(scoped_refptr<DecoderBuffer> buffer, DecodeCB decode_cb) override;
54   void Reset(base::OnceClosure closure) override;
55
56   // Callback called from within FFmpeg to allocate a buffer based on the
57   // properties of |codec_context| and |frame|. See AVCodecContext.get_buffer2
58   // documentation inside FFmpeg.
59   int GetAudioBuffer(struct AVCodecContext* s, AVFrame* frame, int flags);
60
61  private:
62   // There are four states the decoder can be in:
63   //
64   // - kUninitialized: The decoder is not initialized.
65   // - kNormal: This is the normal state. The decoder is idle and ready to
66   //            decode input buffers, or is decoding an input buffer.
67   // - kDecodeFinished: EOS buffer received, codec flushed and decode finished.
68   //                    No further Decode() call should be made.
69   // - kError: Unexpected error happened.
70   //
71   // These are the possible state transitions.
72   //
73   // kUninitialized -> kNormal:
74   //     The decoder is successfully initialized and is ready to decode buffers.
75   // kNormal -> kDecodeFinished:
76   //     When buffer->end_of_stream() is true.
77   // kNormal -> kError:
78   //     A decoding error occurs and decoding needs to stop.
79   // (any state) -> kNormal:
80   //     Any time Reset() is called.
81   enum class DecoderState { kUninitialized, kNormal, kDecodeFinished, kError };
82
83   // Reset decoder and call |reset_cb_|.
84   void DoReset();
85
86   // Handles decoding an unencrypted encoded buffer.
87   void DecodeBuffer(const DecoderBuffer& buffer, DecodeCB decode_cb);
88   bool FFmpegDecode(const DecoderBuffer& buffer);
89   bool OnNewFrame(const DecoderBuffer& buffer,
90                   bool* decoded_frame_this_loop,
91                   AVFrame* frame);
92
93   // Handles (re-)initializing the decoder with a (new) config.
94   // Returns true if initialization was successful.
95   bool ConfigureDecoder(const AudioDecoderConfig& config);
96
97   // Releases resources associated with |codec_context_| .
98   void ReleaseFFmpegResources();
99
100   void ResetTimestampState(const AudioDecoderConfig& config);
101
102   scoped_refptr<base::SequencedTaskRunner> task_runner_;
103   SEQUENCE_CHECKER(sequence_checker_);
104
105   OutputCB output_cb_;
106
107   DecoderState state_;
108
109   // FFmpeg structures owned by this object.
110   std::unique_ptr<AVCodecContext, ScopedPtrAVFreeContext> codec_context_;
111
112   AudioDecoderConfig config_;
113
114   // AVSampleFormat initially requested; not Chrome's SampleFormat.
115   int av_sample_format_;
116
117   std::unique_ptr<AudioDiscardHelper> discard_helper_;
118
119   raw_ptr<MediaLog, DanglingUntriaged> media_log_;
120
121   scoped_refptr<AudioBufferMemoryPool> pool_;
122
123   std::unique_ptr<FFmpegDecodingLoop> decoding_loop_;
124 };
125
126 }  // namespace media
127
128 #endif  // MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_