Upload upstream chromium 108.0.5359.1
[platform/framework/web/chromium-efl.git] / media / filters / audio_file_reader.h
1 // Copyright 2011 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_AUDIO_FILE_READER_H_
6 #define MEDIA_FILTERS_AUDIO_FILE_READER_H_
7
8 #include <limits>
9 #include <memory>
10 #include <vector>
11
12 #include "base/memory/raw_ptr.h"
13 #include "media/base/audio_codecs.h"
14 #include "media/base/media_export.h"
15 #include "media/ffmpeg/ffmpeg_deleters.h"
16 #include "media/filters/ffmpeg_glue.h"
17
18 struct AVCodecContext;
19 struct AVFrame;
20 struct AVPacket;
21 struct AVStream;
22
23 namespace base { class TimeDelta; }
24
25 namespace media {
26
27 class AudioBus;
28 class FFmpegURLProtocol;
29
30 class MEDIA_EXPORT AudioFileReader {
31  public:
32   // Audio file data will be read using the given protocol.
33   // The AudioFileReader does not take ownership of |protocol| and
34   // simply maintains a weak reference to it.
35   explicit AudioFileReader(FFmpegURLProtocol* protocol);
36
37   AudioFileReader(const AudioFileReader&) = delete;
38   AudioFileReader& operator=(const AudioFileReader&) = delete;
39
40   virtual ~AudioFileReader();
41
42   // Open() reads the audio data format so that the sample_rate(),
43   // channels(), GetDuration(), and GetNumberOfFrames() methods can be called.
44   // It returns |true| on success.
45   bool Open();
46   void Close();
47
48   // After a call to Open(), attempts to decode the data of |packets_to_read|,
49   // updating |decodedAudioPackets| with each decoded packet in order.
50   // The caller must convert these packets into one complete set of
51   // decoded audio data.  The audio data will be decoded as
52   // floating-point linear PCM with a nominal range of -1.0 -> +1.0.
53   // Returns the number of sample-frames actually read which will
54   // always be the total size of all the frames in
55   // |decodedAudioPackets|.
56   // If |packets_to_read| is std::numeric_limits<int>::max(), decodes the entire
57   // data.
58   int Read(std::vector<std::unique_ptr<AudioBus>>* decoded_audio_packets,
59            int packets_to_read = std::numeric_limits<int>::max());
60
61   // These methods can be called once Open() has been called.
62   int channels() const { return channels_; }
63   int sample_rate() const { return sample_rate_; }
64
65   // Returns true if (an estimated) duration of the audio data is
66   // known.  Must be called after Open();
67   bool HasKnownDuration() const;
68
69   // Please note that GetDuration() and GetNumberOfFrames() attempt to be
70   // accurate, but are only estimates.  For some encoded formats, the actual
71   // duration of the file can only be determined once all the file data has been
72   // read. The Read() method returns the actual number of sample-frames it has
73   // read.
74   base::TimeDelta GetDuration() const;
75   int GetNumberOfFrames() const;
76
77   // The methods below are helper methods which allow AudioFileReader to double
78   // as a test utility for demuxing audio files.
79   // --------------------------------------------------------------------------
80
81   // Similar to Open() but does not initialize the decoder.
82   bool OpenDemuxerForTesting();
83
84   // Returns true if a packet could be demuxed from the first audio stream in
85   // the file, |output_packet| will contain the demuxed packet then.
86   bool ReadPacketForTesting(AVPacket* output_packet);
87
88   // Seeks to the given point and returns true if successful.  |seek_time| will
89   // be converted to the stream's time base automatically.
90   bool SeekForTesting(base::TimeDelta seek_time);
91
92   const AVStream* GetAVStreamForTesting() const;
93   const AVCodecContext* codec_context_for_testing() const {
94     return codec_context_.get();
95   }
96
97  private:
98   bool OpenDemuxer();
99   bool OpenDecoder();
100   bool ReadPacket(AVPacket* output_packet);
101   bool OnNewFrame(int* total_frames,
102                   std::vector<std::unique_ptr<AudioBus>>* decoded_audio_packets,
103                   AVFrame* frame);
104
105   // Destruct |glue_| after |codec_context_|.
106   std::unique_ptr<FFmpegGlue> glue_;
107   std::unique_ptr<AVCodecContext, ScopedPtrAVFreeContext> codec_context_;
108
109   int stream_index_;
110   raw_ptr<FFmpegURLProtocol> protocol_;
111   AudioCodec audio_codec_;
112   int channels_;
113   int sample_rate_;
114
115   // AVSampleFormat initially requested; not Chrome's SampleFormat.
116   int av_sample_format_;
117 };
118
119 }  // namespace media
120
121 #endif  // MEDIA_FILTERS_AUDIO_FILE_READER_H_