Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / media / formats / mpeg / mpeg_audio_stream_parser_base.h
1 // Copyright 2014 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_FORMATS_MPEG_MPEG_AUDIO_STREAM_PARSER_BASE_H_
6 #define MEDIA_FORMATS_MPEG_MPEG_AUDIO_STREAM_PARSER_BASE_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/bit_reader.h"
16 #include "media/base/byte_queue.h"
17 #include "media/base/media_export.h"
18 #include "media/base/stream_parser.h"
19
20 namespace media {
21
22 class MEDIA_EXPORT MPEGAudioStreamParserBase : public StreamParser {
23  public:
24   // |start_code_mask| is used to find the start of each frame header.  Also
25   // referred to as the sync code in the MP3 and ADTS header specifications.
26   MPEGAudioStreamParserBase(uint32 start_code_mask, AudioCodec audio_codec);
27   virtual ~MPEGAudioStreamParserBase();
28
29   // StreamParser implementation.
30   virtual void Init(const InitCB& init_cb,
31                     const NewConfigCB& config_cb,
32                     const NewBuffersCB& new_buffers_cb,
33                     bool ignore_text_tracks,
34                     const NeedKeyCB& need_key_cb,
35                     const NewMediaSegmentCB& new_segment_cb,
36                     const base::Closure& end_of_segment_cb,
37                     const LogCB& log_cb) OVERRIDE;
38   virtual void Flush() OVERRIDE;
39   virtual bool Parse(const uint8* buf, int size) OVERRIDE;
40
41  protected:
42   // Subclasses implement this method to parse format specific frame headers.
43   // |data| & |size| describe the data available for parsing.
44   //
45   // Implementations are expected to consume an entire frame header.  It should
46   // only return a value greater than 0 when |data| has enough bytes to
47   // successfully parse & consume the entire frame header.
48   //
49   // |frame_size| - Required parameter that is set to the size of the frame, in
50   // bytes, including the frame header if the function returns a value > 0.
51   // |sample_rate| - Optional parameter that is set to the sample rate
52   // of the frame if this function returns a value > 0.
53   // |channel_layout| - Optional parameter that is set to the channel_layout
54   // of the frame if this function returns a value > 0.
55   // |sample_count| - Optional parameter that is set to the number of samples
56   // in the frame if this function returns a value > 0.
57   //
58   // |sample_rate|, |channel_layout|, |sample_count| may be NULL if the caller
59   // is not interested in receiving these values from the frame header.
60   //
61   // Returns:
62   // > 0 : The number of bytes parsed.
63   //   0 : If more data is needed to parse the entire frame header.
64   // < 0 : An error was encountered during parsing.
65   virtual int ParseFrameHeader(const uint8* data,
66                                int size,
67                                int* frame_size,
68                                int* sample_rate,
69                                ChannelLayout* channel_layout,
70                                int* sample_count) const = 0;
71
72   const LogCB& log_cb() const { return log_cb_; }
73
74  private:
75   enum State {
76     UNINITIALIZED,
77     INITIALIZED,
78     PARSE_ERROR
79   };
80
81   void ChangeState(State state);
82
83   // Parsing functions for various byte stream elements.  |data| & |size|
84   // describe the data available for parsing.
85   //
86   // Returns:
87   // > 0 : The number of bytes parsed.
88   //   0 : If more data is needed to parse the entire element.
89   // < 0 : An error was encountered during parsing.
90   int ParseFrame(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   State state_;
122
123   InitCB init_cb_;
124   NewConfigCB config_cb_;
125   NewBuffersCB new_buffers_cb_;
126   NewMediaSegmentCB new_segment_cb_;
127   base::Closure end_of_segment_cb_;
128   LogCB log_cb_;
129
130   ByteQueue queue_;
131
132   AudioDecoderConfig config_;
133   scoped_ptr<AudioTimestampHelper> timestamp_helper_;
134   bool in_media_segment_;
135   const uint32 start_code_mask_;
136   const AudioCodec audio_codec_;
137
138   DISALLOW_COPY_AND_ASSIGN(MPEGAudioStreamParserBase);
139 };
140
141 }  // namespace media
142
143 #endif  // MEDIA_FORMATS_MPEG_MPEG_AUDIO_STREAM_PARSER_BASE_H_