| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_FORMATS_MPEG_MPEG_AUDIO_STREAM_PARSER_BASE_H_ | 5 #ifndef MEDIA_FORMATS_MPEG_MPEG_AUDIO_STREAM_PARSER_BASE_H_ |
| 6 #define MEDIA_FORMATS_MPEG_MPEG_AUDIO_STREAM_PARSER_BASE_H_ | 6 #define MEDIA_FORMATS_MPEG_MPEG_AUDIO_STREAM_PARSER_BASE_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/callback.h" | 11 #include "base/callback.h" |
| 13 #include "media/base/audio_decoder_config.h" | 12 #include "media/base/audio_decoder_config.h" |
| 14 #include "media/base/audio_timestamp_helper.h" | 13 #include "media/base/audio_timestamp_helper.h" |
| 15 #include "media/base/bit_reader.h" | 14 #include "media/base/bit_reader.h" |
| 16 #include "media/base/byte_queue.h" | 15 #include "media/base/byte_queue.h" |
| 17 #include "media/base/media_export.h" | 16 #include "media/base/media_export.h" |
| 18 #include "media/base/stream_parser.h" | 17 #include "media/base/stream_parser.h" |
| 19 | 18 |
| 20 namespace media { | 19 namespace media { |
| 21 | 20 |
| 22 class MEDIA_EXPORT MPEGAudioStreamParserBase : public StreamParser { | 21 class MEDIA_EXPORT MPEGAudioStreamParserBase : public StreamParser { |
| 23 public: | 22 public: |
| 24 // |start_code_mask| is used to find the start of each frame header. Also | 23 // |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. | 24 // 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 | 25 // |codec_delay| is the number of samples the decoder will output before the |
| 27 // first real frame. | 26 // first real frame. |
| 28 MPEGAudioStreamParserBase(uint32 start_code_mask, | 27 MPEGAudioStreamParserBase(uint32_t start_code_mask, |
| 29 AudioCodec audio_codec, | 28 AudioCodec audio_codec, |
| 30 int codec_delay); | 29 int codec_delay); |
| 31 ~MPEGAudioStreamParserBase() override; | 30 ~MPEGAudioStreamParserBase() override; |
| 32 | 31 |
| 33 // StreamParser implementation. | 32 // StreamParser implementation. |
| 34 void Init(const InitCB& init_cb, | 33 void Init(const InitCB& init_cb, |
| 35 const NewConfigCB& config_cb, | 34 const NewConfigCB& config_cb, |
| 36 const NewBuffersCB& new_buffers_cb, | 35 const NewBuffersCB& new_buffers_cb, |
| 37 bool ignore_text_tracks, | 36 bool ignore_text_tracks, |
| 38 const EncryptedMediaInitDataCB& encrypted_media_init_data_cb, | 37 const EncryptedMediaInitDataCB& encrypted_media_init_data_cb, |
| 39 const NewMediaSegmentCB& new_segment_cb, | 38 const NewMediaSegmentCB& new_segment_cb, |
| 40 const base::Closure& end_of_segment_cb, | 39 const base::Closure& end_of_segment_cb, |
| 41 const scoped_refptr<MediaLog>& media_log) override; | 40 const scoped_refptr<MediaLog>& media_log) override; |
| 42 void Flush() override; | 41 void Flush() override; |
| 43 bool Parse(const uint8* buf, int size) override; | 42 bool Parse(const uint8_t* buf, int size) override; |
| 44 | 43 |
| 45 protected: | 44 protected: |
| 46 // Subclasses implement this method to parse format specific frame headers. | 45 // Subclasses implement this method to parse format specific frame headers. |
| 47 // |data| & |size| describe the data available for parsing. | 46 // |data| & |size| describe the data available for parsing. |
| 48 // | 47 // |
| 49 // Implementations are expected to consume an entire frame header. It should | 48 // 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 | 49 // only return a value greater than 0 when |data| has enough bytes to |
| 51 // successfully parse & consume the entire frame header. | 50 // successfully parse & consume the entire frame header. |
| 52 // | 51 // |
| 53 // |frame_size| - Required parameter that is set to the size of the frame, in | 52 // |frame_size| - Required parameter that is set to the size of the frame, in |
| (...skipping 12 matching lines...) Expand all Loading... |
| 66 // NULL if the caller is not interested in receiving these values from the | 65 // NULL if the caller is not interested in receiving these values from the |
| 67 // frame header. | 66 // frame header. |
| 68 // | 67 // |
| 69 // If |metadata_frame| is true, the MPEGAudioStreamParserBase will discard the | 68 // If |metadata_frame| is true, the MPEGAudioStreamParserBase will discard the |
| 70 // frame after consuming the metadata values above. | 69 // frame after consuming the metadata values above. |
| 71 // | 70 // |
| 72 // Returns: | 71 // Returns: |
| 73 // > 0 : The number of bytes parsed. | 72 // > 0 : The number of bytes parsed. |
| 74 // 0 : If more data is needed to parse the entire frame header. | 73 // 0 : If more data is needed to parse the entire frame header. |
| 75 // < 0 : An error was encountered during parsing. | 74 // < 0 : An error was encountered during parsing. |
| 76 virtual int ParseFrameHeader(const uint8* data, | 75 virtual int ParseFrameHeader(const uint8_t* data, |
| 77 int size, | 76 int size, |
| 78 int* frame_size, | 77 int* frame_size, |
| 79 int* sample_rate, | 78 int* sample_rate, |
| 80 ChannelLayout* channel_layout, | 79 ChannelLayout* channel_layout, |
| 81 int* sample_count, | 80 int* sample_count, |
| 82 bool* metadata_frame) const = 0; | 81 bool* metadata_frame) const = 0; |
| 83 | 82 |
| 84 const scoped_refptr<MediaLog>& media_log() const { return media_log_; } | 83 const scoped_refptr<MediaLog>& media_log() const { return media_log_; } |
| 85 | 84 |
| 86 private: | 85 private: |
| 87 enum State { | 86 enum State { |
| 88 UNINITIALIZED, | 87 UNINITIALIZED, |
| 89 INITIALIZED, | 88 INITIALIZED, |
| 90 PARSE_ERROR | 89 PARSE_ERROR |
| 91 }; | 90 }; |
| 92 | 91 |
| 93 void ChangeState(State state); | 92 void ChangeState(State state); |
| 94 | 93 |
| 95 // Parsing functions for various byte stream elements. |data| & |size| | 94 // Parsing functions for various byte stream elements. |data| & |size| |
| 96 // describe the data available for parsing. | 95 // describe the data available for parsing. |
| 97 // | 96 // |
| 98 // Returns: | 97 // Returns: |
| 99 // > 0 : The number of bytes parsed. | 98 // > 0 : The number of bytes parsed. |
| 100 // 0 : If more data is needed to parse the entire element. | 99 // 0 : If more data is needed to parse the entire element. |
| 101 // < 0 : An error was encountered during parsing. | 100 // < 0 : An error was encountered during parsing. |
| 102 int ParseFrame(const uint8* data, int size, BufferQueue* buffers); | 101 int ParseFrame(const uint8_t* data, int size, BufferQueue* buffers); |
| 103 int ParseIcecastHeader(const uint8* data, int size); | 102 int ParseIcecastHeader(const uint8_t* data, int size); |
| 104 int ParseID3v1(const uint8* data, int size); | 103 int ParseID3v1(const uint8_t* data, int size); |
| 105 int ParseID3v2(const uint8* data, int size); | 104 int ParseID3v2(const uint8_t* data, int size); |
| 106 | 105 |
| 107 // Parses an ID3v2 "sync safe" integer. | 106 // Parses an ID3v2 "sync safe" integer. |
| 108 // |reader| - A BitReader to read from. | 107 // |reader| - A BitReader to read from. |
| 109 // |value| - Set to the integer value read, if true is returned. | 108 // |value| - Set to the integer value read, if true is returned. |
| 110 // | 109 // |
| 111 // Returns true if the integer was successfully parsed and |value| | 110 // Returns true if the integer was successfully parsed and |value| |
| 112 // was set. | 111 // was set. |
| 113 // Returns false if an error was encountered. The state of |value| is | 112 // Returns false if an error was encountered. The state of |value| is |
| 114 // undefined when false is returned. | 113 // undefined when false is returned. |
| 115 bool ParseSyncSafeInt(BitReader* reader, int32* value); | 114 bool ParseSyncSafeInt(BitReader* reader, int32_t* value); |
| 116 | 115 |
| 117 // Scans |data| for the next valid start code. | 116 // Scans |data| for the next valid start code. |
| 118 // Returns: | 117 // Returns: |
| 119 // > 0 : The number of bytes that should be skipped to reach the | 118 // > 0 : The number of bytes that should be skipped to reach the |
| 120 // next start code.. | 119 // next start code.. |
| 121 // 0 : If a valid start code was not found and more data is needed. | 120 // 0 : If a valid start code was not found and more data is needed. |
| 122 // < 0 : An error was encountered during parsing. | 121 // < 0 : An error was encountered during parsing. |
| 123 int FindNextValidStartCode(const uint8* data, int size) const; | 122 int FindNextValidStartCode(const uint8_t* data, int size) const; |
| 124 | 123 |
| 125 // Sends the buffers in |buffers| to |new_buffers_cb_| and then clears | 124 // Sends the buffers in |buffers| to |new_buffers_cb_| and then clears |
| 126 // |buffers|. | 125 // |buffers|. |
| 127 // If |end_of_segment| is set to true, then |end_of_segment_cb_| is called | 126 // 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 | 127 // after |new_buffers_cb_| to signal that these buffers represent the end of a |
| 129 // media segment. | 128 // media segment. |
| 130 // Returns true if the buffers are sent successfully. | 129 // Returns true if the buffers are sent successfully. |
| 131 bool SendBuffers(BufferQueue* buffers, bool end_of_segment); | 130 bool SendBuffers(BufferQueue* buffers, bool end_of_segment); |
| 132 | 131 |
| 133 State state_; | 132 State state_; |
| 134 | 133 |
| 135 InitCB init_cb_; | 134 InitCB init_cb_; |
| 136 NewConfigCB config_cb_; | 135 NewConfigCB config_cb_; |
| 137 NewBuffersCB new_buffers_cb_; | 136 NewBuffersCB new_buffers_cb_; |
| 138 NewMediaSegmentCB new_segment_cb_; | 137 NewMediaSegmentCB new_segment_cb_; |
| 139 base::Closure end_of_segment_cb_; | 138 base::Closure end_of_segment_cb_; |
| 140 scoped_refptr<MediaLog> media_log_; | 139 scoped_refptr<MediaLog> media_log_; |
| 141 | 140 |
| 142 ByteQueue queue_; | 141 ByteQueue queue_; |
| 143 | 142 |
| 144 AudioDecoderConfig config_; | 143 AudioDecoderConfig config_; |
| 145 scoped_ptr<AudioTimestampHelper> timestamp_helper_; | 144 scoped_ptr<AudioTimestampHelper> timestamp_helper_; |
| 146 bool in_media_segment_; | 145 bool in_media_segment_; |
| 147 const uint32 start_code_mask_; | 146 const uint32_t start_code_mask_; |
| 148 const AudioCodec audio_codec_; | 147 const AudioCodec audio_codec_; |
| 149 const int codec_delay_; | 148 const int codec_delay_; |
| 150 | 149 |
| 151 DISALLOW_COPY_AND_ASSIGN(MPEGAudioStreamParserBase); | 150 DISALLOW_COPY_AND_ASSIGN(MPEGAudioStreamParserBase); |
| 152 }; | 151 }; |
| 153 | 152 |
| 154 } // namespace media | 153 } // namespace media |
| 155 | 154 |
| 156 #endif // MEDIA_FORMATS_MPEG_MPEG_AUDIO_STREAM_PARSER_BASE_H_ | 155 #endif // MEDIA_FORMATS_MPEG_MPEG_AUDIO_STREAM_PARSER_BASE_H_ |
| OLD | NEW |