Home | History | Annotate | Download | only in filters
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      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 "base/basictypes.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "media/base/media_export.h"
     11 #include "media/filters/ffmpeg_glue.h"
     12 
     13 struct AVCodecContext;
     14 struct AVPacket;
     15 struct AVStream;
     16 
     17 namespace base { class TimeDelta; }
     18 
     19 namespace media {
     20 
     21 class AudioBus;
     22 class FFmpegURLProtocol;
     23 
     24 class MEDIA_EXPORT AudioFileReader {
     25  public:
     26   // Audio file data will be read using the given protocol.
     27   // The AudioFileReader does not take ownership of |protocol| and
     28   // simply maintains a weak reference to it.
     29   explicit AudioFileReader(FFmpegURLProtocol* protocol);
     30   virtual ~AudioFileReader();
     31 
     32   // Open() reads the audio data format so that the sample_rate(),
     33   // channels(), GetDuration(), and GetNumberOfFrames() methods can be called.
     34   // It returns |true| on success.
     35   bool Open();
     36   void Close();
     37 
     38   // After a call to Open(), attempts to fully fill |audio_bus| with decoded
     39   // audio data.  Any unfilled frames will be zeroed out.
     40   // |audio_data| must be of the same size as channels().
     41   // The audio data will be decoded as floating-point linear PCM with
     42   // a nominal range of -1.0 -> +1.0.
     43   // Returns the number of sample-frames actually read which will always be
     44   // <= audio_bus->frames()
     45   int Read(AudioBus* audio_bus);
     46 
     47   // These methods can be called once Open() has been called.
     48   int channels() const { return channels_; }
     49   int sample_rate() const { return sample_rate_; }
     50 
     51   // Please note that GetDuration() and GetNumberOfFrames() attempt to be
     52   // accurate, but are only estimates.  For some encoded formats, the actual
     53   // duration of the file can only be determined once all the file data has been
     54   // read. The Read() method returns the actual number of sample-frames it has
     55   // read.
     56   base::TimeDelta GetDuration() const;
     57   int GetNumberOfFrames() const;
     58 
     59   // The methods below are helper methods which allow AudioFileReader to double
     60   // as a test utility for demuxing audio files.
     61   // --------------------------------------------------------------------------
     62 
     63   // Similar to Open() but does not initialize the decoder.
     64   bool OpenDemuxerForTesting();
     65 
     66   // Returns true if a packet could be demuxed from the first audio stream in
     67   // the file, |output_packet| will contain the demuxed packet then.
     68   bool ReadPacketForTesting(AVPacket* output_packet);
     69 
     70   // Seeks to the given point and returns true if successful.  |seek_time| will
     71   // be converted to the stream's time base automatically.
     72   bool SeekForTesting(base::TimeDelta seek_time);
     73 
     74   const AVStream* GetAVStreamForTesting() const;
     75   const AVCodecContext* codec_context_for_testing() const {
     76     return codec_context_;
     77   }
     78 
     79  private:
     80   bool OpenDemuxer();
     81   bool OpenDecoder();
     82   bool ReadPacket(AVPacket* output_packet);
     83 
     84   scoped_ptr<FFmpegGlue> glue_;
     85   AVCodecContext* codec_context_;
     86   int stream_index_;
     87   FFmpegURLProtocol* protocol_;
     88   int channels_;
     89   int sample_rate_;
     90   int64_t end_padding_;
     91 
     92   // AVSampleFormat initially requested; not Chrome's SampleFormat.
     93   int av_sample_format_;
     94 
     95   DISALLOW_COPY_AND_ASSIGN(AudioFileReader);
     96 };
     97 
     98 }  // namespace media
     99 
    100 #endif  // MEDIA_FILTERS_AUDIO_FILE_READER_H_
    101