audio_file_reader.h 4.2 KB

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