Home | History | Annotate | Download | only in mp3
      1 // Copyright 2013 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_MP3_MP3_STREAM_PARSER_H_
      6 #define MEDIA_MP3_MP3_STREAM_PARSER_H_
      7 
      8 #include <set>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/callback.h"
     13 #include "media/base/audio_decoder_config.h"
     14 #include "media/base/audio_timestamp_helper.h"
     15 #include "media/base/byte_queue.h"
     16 #include "media/base/media_export.h"
     17 #include "media/base/stream_parser.h"
     18 
     19 namespace media {
     20 
     21 class BitReader;
     22 
     23 class MEDIA_EXPORT MP3StreamParser : public StreamParser {
     24  public:
     25   MP3StreamParser();
     26   virtual ~MP3StreamParser();
     27 
     28   // StreamParser implementation.
     29   virtual void Init(const InitCB& init_cb, const NewConfigCB& config_cb,
     30                     const NewBuffersCB& new_buffers_cb,
     31                     const NewTextBuffersCB& text_cb,
     32                     const NeedKeyCB& need_key_cb,
     33                     const NewMediaSegmentCB& new_segment_cb,
     34                     const base::Closure& end_of_segment_cb,
     35                     const LogCB& log_cb) OVERRIDE;
     36   virtual void Flush() OVERRIDE;
     37   virtual bool Parse(const uint8* buf, int size) OVERRIDE;
     38 
     39  private:
     40   enum State {
     41     UNINITIALIZED,
     42     INITIALIZED,
     43     PARSE_ERROR
     44   };
     45 
     46   State state_;
     47 
     48   InitCB init_cb_;
     49   NewConfigCB config_cb_;
     50   NewBuffersCB new_buffers_cb_;
     51   NewMediaSegmentCB new_segment_cb_;
     52   base::Closure end_of_segment_cb_;
     53   LogCB log_cb_;
     54 
     55   ByteQueue queue_;
     56 
     57   AudioDecoderConfig config_;
     58   scoped_ptr<AudioTimestampHelper> timestamp_helper_;
     59   bool in_media_segment_;
     60 
     61   void ChangeState(State state);
     62 
     63   // Parsing functions for various byte stream elements.
     64   // |data| & |size| describe the data available for parsing.
     65   // These functions are expected to consume an entire frame/header.
     66   // It should only return a value greater than 0 when |data| has
     67   // enough bytes to successfully parse & consume the entire element.
     68   //
     69   // |frame_size| - Required parameter that is set to the size of the frame, in
     70   // bytes, including the frame header if the function returns a value > 0.
     71   // |sample_rate| - Optional parameter that is set to the sample rate
     72   // of the frame if this function returns a value > 0.
     73   // |channel_layout| - Optional parameter that is set to the channel_layout
     74   // of the frame if this function returns a value > 0.
     75   // |sample_count| - Optional parameter that is set to the number of samples
     76   // in the frame if this function returns a value > 0.
     77   //
     78   // |sample_rate|, |channel_layout|, |sample_count| may be NULL if the caller
     79   // is not interested in receiving these values from the frame header.
     80   //
     81   // Returns:
     82   // > 0 : The number of bytes parsed.
     83   //   0 : If more data is needed to parse the entire element.
     84   // < 0 : An error was encountered during parsing.
     85   int ParseFrameHeader(const uint8* data, int size,
     86                        int* frame_size,
     87                        int* sample_rate,
     88                        ChannelLayout* channel_layout,
     89                        int* sample_count) const;
     90   int ParseMP3Frame(const uint8* data, int size, BufferQueue* buffers);
     91   int ParseIcecastHeader(const uint8* data, int size);
     92   int ParseID3v1(const uint8* data, int size);
     93   int ParseID3v2(const uint8* data, int size);
     94 
     95   // Parses an ID3v2 "sync safe" integer.
     96   // |reader| - A BitReader to read from.
     97   // |value| - Set to the integer value read, if true is returned.
     98   //
     99   // Returns true if the integer was successfully parsed and |value|
    100   // was set.
    101   // Returns false if an error was encountered. The state of |value| is
    102   // undefined when false is returned.
    103   bool ParseSyncSafeInt(BitReader* reader, int32* value);
    104 
    105   // Scans |data| for the next valid start code.
    106   // Returns:
    107   // > 0 : The number of bytes that should be skipped to reach the
    108   //       next start code..
    109   //   0 : If a valid start code was not found and more data is needed.
    110   // < 0 : An error was encountered during parsing.
    111   int FindNextValidStartCode(const uint8* data, int size) const;
    112 
    113   // Sends the buffers in |buffers| to |new_buffers_cb_| and then clears
    114   // |buffers|.
    115   // If |end_of_segment| is set to true, then |end_of_segment_cb_| is called
    116   // after |new_buffers_cb_| to signal that these buffers represent the end of a
    117   // media segment.
    118   // Returns true if the buffers are sent successfully.
    119   bool SendBuffers(BufferQueue* buffers, bool end_of_segment);
    120 
    121   DISALLOW_COPY_AND_ASSIGN(MP3StreamParser);
    122 };
    123 
    124 }  // namespace media
    125 
    126 #endif  // MEDIA_MP3_MP3_STREAM_PARSER_H_
    127