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.
5 #ifndef MEDIA_FORMATS_MPEG_MPEG_AUDIO_STREAM_PARSER_BASE_H_
6 #define MEDIA_FORMATS_MPEG_MPEG_AUDIO_STREAM_PARSER_BASE_H_
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"
22 class MEDIA_EXPORT MPEGAudioStreamParserBase : public StreamParser {
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();
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;
42 // Subclasses implement this method to parse format specific frame headers.
43 // |data| & |size| describe the data available for parsing.
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.
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.
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.
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,
69 ChannelLayout* channel_layout,
70 int* sample_count) const = 0;
72 const LogCB& log_cb() const { return log_cb_; }
81 void ChangeState(State state);
83 // Parsing functions for various byte stream elements. |data| & |size|
84 // describe the data available for parsing.
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);
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.
99 // Returns true if the integer was successfully parsed and |value|
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);
105 // Scans |data| for the next valid start code.
107 // > 0 : The number of bytes that should be skipped to reach the
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;
113 // Sends the buffers in |buffers| to |new_buffers_cb_| and then clears
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
118 // Returns true if the buffers are sent successfully.
119 bool SendBuffers(BufferQueue* buffers, bool end_of_segment);
124 NewConfigCB config_cb_;
125 NewBuffersCB new_buffers_cb_;
126 NewMediaSegmentCB new_segment_cb_;
127 base::Closure end_of_segment_cb_;
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_;
138 DISALLOW_COPY_AND_ASSIGN(MPEGAudioStreamParserBase);
143 #endif // MEDIA_FORMATS_MPEG_MPEG_AUDIO_STREAM_PARSER_BASE_H_