Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / formats / mpeg / mpeg_audio_stream_parser_base.h
blob832199389eb4b8d0add77118f991ed19e6dff344
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_
8 #include <set>
9 #include <vector>
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"
20 namespace media {
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 // |codec_delay| is the number of samples the decoder will output before the
27 // first real frame.
28 MPEGAudioStreamParserBase(uint32 start_code_mask,
29 AudioCodec audio_codec,
30 int codec_delay);
31 ~MPEGAudioStreamParserBase() override;
33 // StreamParser implementation.
34 void Init(const InitCB& init_cb,
35 const NewConfigCB& config_cb,
36 const NewBuffersCB& new_buffers_cb,
37 bool ignore_text_tracks,
38 const EncryptedMediaInitDataCB& encrypted_media_init_data_cb,
39 const NewMediaSegmentCB& new_segment_cb,
40 const base::Closure& end_of_segment_cb,
41 const scoped_refptr<MediaLog>& media_log) override;
42 void Flush() override;
43 bool Parse(const uint8* buf, int size) override;
45 protected:
46 // Subclasses implement this method to parse format specific frame headers.
47 // |data| & |size| describe the data available for parsing.
49 // Implementations are expected to consume an entire frame header. It should
50 // only return a value greater than 0 when |data| has enough bytes to
51 // successfully parse & consume the entire frame header.
53 // |frame_size| - Required parameter that is set to the size of the frame, in
54 // bytes, including the frame header if the function returns a value > 0.
55 // |sample_rate| - Optional parameter that is set to the sample rate
56 // of the frame if this function returns a value > 0.
57 // |channel_layout| - Optional parameter that is set to the channel_layout
58 // of the frame if this function returns a value > 0.
59 // |sample_count| - Optional parameter that is set to the number of samples
60 // in the frame if this function returns a value > 0.
61 // |metadata_frame| - Optional parameter that is set to true if the frame has
62 // valid values for the above parameters, but no usable encoded data; only set
63 // to true if this function returns a value > 0.
65 // |sample_rate|, |channel_layout|, |sample_count|, |metadata_frame| may be
66 // NULL if the caller is not interested in receiving these values from the
67 // frame header.
69 // If |metadata_frame| is true, the MPEGAudioStreamParserBase will discard the
70 // frame after consuming the metadata values above.
72 // Returns:
73 // > 0 : The number of bytes parsed.
74 // 0 : If more data is needed to parse the entire frame header.
75 // < 0 : An error was encountered during parsing.
76 virtual int ParseFrameHeader(const uint8* data,
77 int size,
78 int* frame_size,
79 int* sample_rate,
80 ChannelLayout* channel_layout,
81 int* sample_count,
82 bool* metadata_frame) const = 0;
84 const scoped_refptr<MediaLog>& media_log() const { return media_log_; }
86 private:
87 enum State {
88 UNINITIALIZED,
89 INITIALIZED,
90 PARSE_ERROR
93 void ChangeState(State state);
95 // Parsing functions for various byte stream elements. |data| & |size|
96 // describe the data available for parsing.
98 // Returns:
99 // > 0 : The number of bytes parsed.
100 // 0 : If more data is needed to parse the entire element.
101 // < 0 : An error was encountered during parsing.
102 int ParseFrame(const uint8* data, int size, BufferQueue* buffers);
103 int ParseIcecastHeader(const uint8* data, int size);
104 int ParseID3v1(const uint8* data, int size);
105 int ParseID3v2(const uint8* data, int size);
107 // Parses an ID3v2 "sync safe" integer.
108 // |reader| - A BitReader to read from.
109 // |value| - Set to the integer value read, if true is returned.
111 // Returns true if the integer was successfully parsed and |value|
112 // was set.
113 // Returns false if an error was encountered. The state of |value| is
114 // undefined when false is returned.
115 bool ParseSyncSafeInt(BitReader* reader, int32* value);
117 // Scans |data| for the next valid start code.
118 // Returns:
119 // > 0 : The number of bytes that should be skipped to reach the
120 // next start code..
121 // 0 : If a valid start code was not found and more data is needed.
122 // < 0 : An error was encountered during parsing.
123 int FindNextValidStartCode(const uint8* data, int size) const;
125 // Sends the buffers in |buffers| to |new_buffers_cb_| and then clears
126 // |buffers|.
127 // If |end_of_segment| is set to true, then |end_of_segment_cb_| is called
128 // after |new_buffers_cb_| to signal that these buffers represent the end of a
129 // media segment.
130 // Returns true if the buffers are sent successfully.
131 bool SendBuffers(BufferQueue* buffers, bool end_of_segment);
133 State state_;
135 InitCB init_cb_;
136 NewConfigCB config_cb_;
137 NewBuffersCB new_buffers_cb_;
138 NewMediaSegmentCB new_segment_cb_;
139 base::Closure end_of_segment_cb_;
140 scoped_refptr<MediaLog> media_log_;
142 ByteQueue queue_;
144 AudioDecoderConfig config_;
145 scoped_ptr<AudioTimestampHelper> timestamp_helper_;
146 bool in_media_segment_;
147 const uint32 start_code_mask_;
148 const AudioCodec audio_codec_;
149 const int codec_delay_;
151 DISALLOW_COPY_AND_ASSIGN(MPEGAudioStreamParserBase);
154 } // namespace media
156 #endif // MEDIA_FORMATS_MPEG_MPEG_AUDIO_STREAM_PARSER_BASE_H_