Revert 224458 "Enabling MediaStreamInfoBarTest.DenyingCameraDoes..."
[chromium-blink-merge.git] / media / mp3 / mp3_stream_parser.h
blob97730ae6e82400ee1fc689e2d380d1c92e784f44
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.
5 #ifndef MEDIA_MP3_MP3_STREAM_PARSER_H_
6 #define MEDIA_MP3_MP3_STREAM_PARSER_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/byte_queue.h"
16 #include "media/base/media_export.h"
17 #include "media/base/stream_parser.h"
19 namespace media {
21 class BitReader;
23 class MEDIA_EXPORT MP3StreamParser : public StreamParser {
24 public:
25 MP3StreamParser();
26 virtual ~MP3StreamParser();
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 AddTextTrackCB& add_text_track_cb,
34 const NewMediaSegmentCB& new_segment_cb,
35 const base::Closure& end_of_segment_cb,
36 const LogCB& log_cb) OVERRIDE;
37 virtual void Flush() OVERRIDE;
38 virtual bool Parse(const uint8* buf, int size) OVERRIDE;
40 private:
41 enum State {
42 UNINITIALIZED,
43 INITIALIZED,
44 PARSE_ERROR
47 State state_;
49 InitCB init_cb_;
50 NewConfigCB config_cb_;
51 NewBuffersCB new_buffers_cb_;
52 NewMediaSegmentCB new_segment_cb_;
53 base::Closure end_of_segment_cb_;
54 LogCB log_cb_;
56 ByteQueue queue_;
58 AudioDecoderConfig config_;
59 scoped_ptr<AudioTimestampHelper> timestamp_helper_;
60 bool in_media_segment_;
62 void ChangeState(State state);
64 // Parsing functions for various byte stream elements.
65 // |data| & |size| describe the data available for parsing.
66 // These functions are expected to consume an entire frame/header.
67 // It should only return a value greater than 0 when |data| has
68 // enough bytes to successfully parse & consume the entire element.
70 // |frame_size| - Required parameter that is set to the size of the frame, in
71 // bytes, including the frame header if the function returns a value > 0.
72 // |sample_rate| - Optional parameter that is set to the sample rate
73 // of the frame if this function returns a value > 0.
74 // |channel_layout| - Optional parameter that is set to the channel_layout
75 // of the frame if this function returns a value > 0.
76 // |sample_count| - Optional parameter that is set to the number of samples
77 // in the frame if this function returns a value > 0.
79 // |sample_rate|, |channel_layout|, |sample_count| may be NULL if the caller
80 // is not interested in receiving these values from the frame header.
82 // Returns:
83 // > 0 : The number of bytes parsed.
84 // 0 : If more data is needed to parse the entire element.
85 // < 0 : An error was encountered during parsing.
86 int ParseFrameHeader(const uint8* data, int size,
87 int* frame_size,
88 int* sample_rate,
89 ChannelLayout* channel_layout,
90 int* sample_count) const;
91 int ParseMP3Frame(const uint8* data, int size, BufferQueue* buffers);
92 int ParseIcecastHeader(const uint8* data, int size);
93 int ParseID3v1(const uint8* data, int size);
94 int ParseID3v2(const uint8* data, int size);
96 // Parses an ID3v2 "sync safe" integer.
97 // |reader| - A BitReader to read from.
98 // |value| - Set to the integer value read, if true is returned.
100 // Returns true if the integer was successfully parsed and |value|
101 // was set.
102 // Returns false if an error was encountered. The state of |value| is
103 // undefined when false is returned.
104 bool ParseSyncSafeInt(BitReader* reader, int32* value);
106 // Scans |data| for the next valid start code.
107 // Returns:
108 // > 0 : The number of bytes that should be skipped to reach the
109 // next start code..
110 // 0 : If a valid start code was not found and more data is needed.
111 // < 0 : An error was encountered during parsing.
112 int FindNextValidStartCode(const uint8* data, int size) const;
114 // Sends the buffers in |buffers| to |new_buffers_cb_| and then clears
115 // |buffers|.
116 // If |end_of_segment| is set to true, then |end_of_segment_cb_| is called
117 // after |new_buffers_cb_| to signal that these buffers represent the end of a
118 // media segment.
119 // Returns true if the buffers are sent successfully.
120 bool SendBuffers(BufferQueue* buffers, bool end_of_segment);
122 DISALLOW_COPY_AND_ASSIGN(MP3StreamParser);
125 } // namespace media
127 #endif // MEDIA_MP3_MP3_STREAM_PARSER_H_