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.
5 #ifndef MEDIA_FILTERS_AUDIO_FILE_READER_H_
6 #define MEDIA_FILTERS_AUDIO_FILE_READER_H_
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"
18 struct AVCodecContext;
23 namespace base { class TimeDelta; }
28 class FFmpegURLProtocol;
30 class MEDIA_EXPORT AudioFileReader {
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);
37 AudioFileReader(const AudioFileReader&) = delete;
38 AudioFileReader& operator=(const AudioFileReader&) = delete;
40 virtual ~AudioFileReader();
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.
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
58 int Read(std::vector<std::unique_ptr<AudioBus>>* decoded_audio_packets,
59 int packets_to_read = std::numeric_limits<int>::max());
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_; }
65 // Returns true if (an estimated) duration of the audio data is
66 // known. Must be called after Open();
67 bool HasKnownDuration() const;
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
74 base::TimeDelta GetDuration() const;
75 int GetNumberOfFrames() const;
77 // The methods below are helper methods which allow AudioFileReader to double
78 // as a test utility for demuxing audio files.
79 // --------------------------------------------------------------------------
81 // Similar to Open() but does not initialize the decoder.
82 bool OpenDemuxerForTesting();
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);
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);
92 const AVStream* GetAVStreamForTesting() const;
93 const AVCodecContext* codec_context_for_testing() const {
94 return codec_context_.get();
100 bool ReadPacket(AVPacket* output_packet);
101 bool OnNewFrame(int* total_frames,
102 std::vector<std::unique_ptr<AudioBus>>* decoded_audio_packets,
105 // Destruct |glue_| after |codec_context_|.
106 std::unique_ptr<FFmpegGlue> glue_;
107 std::unique_ptr<AVCodecContext, ScopedPtrAVFreeContext> codec_context_;
110 raw_ptr<FFmpegURLProtocol> protocol_;
111 AudioCodec audio_codec_;
115 // AVSampleFormat initially requested; not Chrome's SampleFormat.
116 int av_sample_format_;
121 #endif // MEDIA_FILTERS_AUDIO_FILE_READER_H_