Add some instrumentation for jank in URLRequest::Start.
[chromium-blink-merge.git] / media / formats / mp2t / es_parser_mpeg1audio.cc
blobc092ccbddb0075b511b3fb7a8bde101b24e23955
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 #include "media/formats/mp2t/es_parser_mpeg1audio.h"
7 #include <list>
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "media/base/audio_timestamp_helper.h"
14 #include "media/base/bit_reader.h"
15 #include "media/base/buffers.h"
16 #include "media/base/channel_layout.h"
17 #include "media/base/stream_parser_buffer.h"
18 #include "media/formats/common/offset_byte_queue.h"
19 #include "media/formats/mp2t/mp2t_common.h"
20 #include "media/formats/mpeg/mpeg1_audio_stream_parser.h"
22 namespace media {
23 namespace mp2t {
25 struct EsParserMpeg1Audio::Mpeg1AudioFrame {
26 // Pointer to the ES data.
27 const uint8* data;
29 // Frame size.
30 int size;
32 // Number of samples in the frame.
33 int sample_count;
35 // Frame offset in the ES queue.
36 int64 queue_offset;
39 EsParserMpeg1Audio::EsParserMpeg1Audio(
40 const NewAudioConfigCB& new_audio_config_cb,
41 const EmitBufferCB& emit_buffer_cb,
42 const LogCB& log_cb)
43 : log_cb_(log_cb),
44 new_audio_config_cb_(new_audio_config_cb),
45 emit_buffer_cb_(emit_buffer_cb) {
48 EsParserMpeg1Audio::~EsParserMpeg1Audio() {
51 bool EsParserMpeg1Audio::ParseFromEsQueue() {
52 // Look for every MPEG1 audio frame in the ES buffer.
53 Mpeg1AudioFrame mpeg1audio_frame;
54 while (LookForMpeg1AudioFrame(&mpeg1audio_frame)) {
55 // Update the audio configuration if needed.
56 DCHECK_GE(mpeg1audio_frame.size, MPEG1AudioStreamParser::kHeaderSize);
57 if (!UpdateAudioConfiguration(mpeg1audio_frame.data))
58 return false;
60 // Get the PTS & the duration of this access unit.
61 TimingDesc current_timing_desc =
62 GetTimingDescriptor(mpeg1audio_frame.queue_offset);
63 if (current_timing_desc.pts != kNoTimestamp())
64 audio_timestamp_helper_->SetBaseTimestamp(current_timing_desc.pts);
66 if (audio_timestamp_helper_->base_timestamp() == kNoTimestamp()) {
67 DVLOG(1) << "Skipping audio frame with unknown timestamp";
68 SkipMpeg1AudioFrame(mpeg1audio_frame);
69 continue;
71 base::TimeDelta current_pts = audio_timestamp_helper_->GetTimestamp();
72 base::TimeDelta frame_duration =
73 audio_timestamp_helper_->GetFrameDuration(
74 mpeg1audio_frame.sample_count);
76 // Emit an audio frame.
77 bool is_key_frame = true;
79 // TODO(wolenetz/acolwell): Validate and use a common cross-parser TrackId
80 // type and allow multiple audio tracks. See https://crbug.com/341581.
81 scoped_refptr<StreamParserBuffer> stream_parser_buffer =
82 StreamParserBuffer::CopyFrom(
83 mpeg1audio_frame.data,
84 mpeg1audio_frame.size,
85 is_key_frame,
86 DemuxerStream::AUDIO, 0);
87 stream_parser_buffer->set_timestamp(current_pts);
88 stream_parser_buffer->set_duration(frame_duration);
89 emit_buffer_cb_.Run(stream_parser_buffer);
91 // Update the PTS of the next frame.
92 audio_timestamp_helper_->AddFrames(mpeg1audio_frame.sample_count);
94 // Skip the current frame.
95 SkipMpeg1AudioFrame(mpeg1audio_frame);
98 return true;
101 void EsParserMpeg1Audio::Flush() {
104 void EsParserMpeg1Audio::ResetInternal() {
105 last_audio_decoder_config_ = AudioDecoderConfig();
108 bool EsParserMpeg1Audio::LookForMpeg1AudioFrame(
109 Mpeg1AudioFrame* mpeg1audio_frame) {
110 int es_size;
111 const uint8* es;
112 es_queue_->Peek(&es, &es_size);
114 int max_offset = es_size - MPEG1AudioStreamParser::kHeaderSize;
115 if (max_offset <= 0)
116 return false;
118 for (int offset = 0; offset < max_offset; offset++) {
119 const uint8* cur_buf = &es[offset];
120 if (cur_buf[0] != 0xff)
121 continue;
123 int remaining_size = es_size - offset;
124 DCHECK_GE(remaining_size, MPEG1AudioStreamParser::kHeaderSize);
125 MPEG1AudioStreamParser::Header header;
126 if (!MPEG1AudioStreamParser::ParseHeader(log_cb_, cur_buf, &header))
127 continue;
129 if (remaining_size < header.frame_size) {
130 // Not a full frame: will resume when we have more data.
131 // Remove all the bytes located before the frame header,
132 // these bytes will not be used anymore.
133 es_queue_->Pop(offset);
134 return false;
137 // Check whether there is another frame
138 // |frame_size| apart from the current one.
139 if (remaining_size >= header.frame_size + 1 &&
140 cur_buf[header.frame_size] != 0xff) {
141 continue;
144 es_queue_->Pop(offset);
145 es_queue_->Peek(&mpeg1audio_frame->data, &es_size);
146 mpeg1audio_frame->queue_offset = es_queue_->head();
147 mpeg1audio_frame->size = header.frame_size;
148 mpeg1audio_frame->sample_count = header.sample_count;
149 DVLOG(LOG_LEVEL_ES)
150 << "MPEG1 audio syncword @ pos=" << mpeg1audio_frame->queue_offset
151 << " frame_size=" << mpeg1audio_frame->size;
152 DVLOG(LOG_LEVEL_ES)
153 << "MPEG1 audio header: "
154 << base::HexEncode(mpeg1audio_frame->data,
155 MPEG1AudioStreamParser::kHeaderSize);
156 return true;
159 es_queue_->Pop(max_offset);
160 return false;
163 bool EsParserMpeg1Audio::UpdateAudioConfiguration(
164 const uint8* mpeg1audio_header) {
165 MPEG1AudioStreamParser::Header header;
166 if (!MPEG1AudioStreamParser::ParseHeader(log_cb_,
167 mpeg1audio_header,
168 &header)) {
169 return false;
172 // TODO(damienv): Verify whether Android playback requires the extra data
173 // field for Mpeg1 audio. If yes, we should generate this field.
174 AudioDecoderConfig audio_decoder_config(
175 kCodecMP3,
176 kSampleFormatS16,
177 header.channel_layout,
178 header.sample_rate,
179 NULL, 0,
180 false);
182 if (!audio_decoder_config.Matches(last_audio_decoder_config_)) {
183 DVLOG(1) << "Sampling frequency: " << header.sample_rate;
184 DVLOG(1) << "Channel layout: " << header.channel_layout;
185 // Reset the timestamp helper to use a new time scale.
186 if (audio_timestamp_helper_ &&
187 audio_timestamp_helper_->base_timestamp() != kNoTimestamp()) {
188 base::TimeDelta base_timestamp = audio_timestamp_helper_->GetTimestamp();
189 audio_timestamp_helper_.reset(
190 new AudioTimestampHelper(header.sample_rate));
191 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
192 } else {
193 audio_timestamp_helper_.reset(
194 new AudioTimestampHelper(header.sample_rate));
196 // Audio config notification.
197 last_audio_decoder_config_ = audio_decoder_config;
198 new_audio_config_cb_.Run(audio_decoder_config);
201 return true;
204 void EsParserMpeg1Audio::SkipMpeg1AudioFrame(
205 const Mpeg1AudioFrame& mpeg1audio_frame) {
206 DCHECK_EQ(mpeg1audio_frame.queue_offset, es_queue_->head());
207 es_queue_->Pop(mpeg1audio_frame.size);
210 } // namespace mp2t
211 } // namespace media