blob: 380dab46156ea41f16231f71224bc2cb94e66eb2 [file] [log] [blame]
Andreas Huber20111aa2009-07-14 16:56:47 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "MP3Extractor"
19#include <utils/Log.h>
20
Andreas Huber66326a52009-10-23 09:55:10 -070021#include "include/MP3Extractor.h"
22
Andreas Huber386d6092011-05-19 08:37:39 -070023#include "include/avc_utils.h"
Andreas Huberfc9ba092010-01-11 15:35:19 -080024#include "include/ID3.h"
Andreas Huber4456da52010-11-09 08:57:45 -080025#include "include/VBRISeeker.h"
26#include "include/XINGSeeker.h"
Andreas Huberfc9ba092010-01-11 15:35:19 -080027
Andreas Huber2944eca2011-09-08 14:12:44 -070028#include <media/stagefright/foundation/ADebug.h>
Andreas Huber5a1c3522010-08-25 11:09:41 -070029#include <media/stagefright/foundation/AMessage.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070030#include <media/stagefright/DataSource.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070031#include <media/stagefright/MediaBuffer.h>
32#include <media/stagefright/MediaBufferGroup.h>
Andreas Huber18291bc2009-09-10 14:13:30 -070033#include <media/stagefright/MediaDefs.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070034#include <media/stagefright/MediaErrors.h>
35#include <media/stagefright/MediaSource.h>
36#include <media/stagefright/MetaData.h>
37#include <media/stagefright/Utils.h>
38#include <utils/String8.h>
39
40namespace android {
41
Andreas Huber54196292009-12-07 16:31:31 -080042// Everything must match except for
James Dong2eb6a472011-03-04 11:25:17 -080043// protection, bitrate, padding, private bits, mode, mode extension,
Andreas Huberba1f4812010-03-04 16:49:03 -080044// copyright bit, original bit and emphasis.
45// Yes ... there are things that must indeed match...
James Dong2eb6a472011-03-04 11:25:17 -080046static const uint32_t kMask = 0xfffe0c00;
Andreas Huber54196292009-12-07 16:31:31 -080047
Andreas Huber20111aa2009-07-14 16:56:47 -070048static bool Resync(
Andreas Huber693d2712009-08-14 14:37:10 -070049 const sp<DataSource> &source, uint32_t match_header,
James Dongc7fc37a2010-11-16 14:04:54 -080050 off64_t *inout_pos, off64_t *post_id3_pos, uint32_t *out_header) {
Andreas Huber4456da52010-11-09 08:57:45 -080051 if (post_id3_pos != NULL) {
52 *post_id3_pos = 0;
53 }
54
Andreas Huber0bf39212009-10-12 15:48:51 -070055 if (*inout_pos == 0) {
56 // Skip an optional ID3 header if syncing at the very beginning
57 // of the datasource.
58
Andreas Huberba0707d2010-03-31 11:08:23 -070059 for (;;) {
60 uint8_t id3header[10];
61 if (source->readAt(*inout_pos, id3header, sizeof(id3header))
62 < (ssize_t)sizeof(id3header)) {
63 // If we can't even read these 10 bytes, we might as well bail
64 // out, even if there _were_ 10 bytes of valid mp3 audio data...
65 return false;
66 }
Andreas Huber0bf39212009-10-12 15:48:51 -070067
Andreas Huberba0707d2010-03-31 11:08:23 -070068 if (memcmp("ID3", id3header, 3)) {
69 break;
70 }
71
Andreas Huber0bf39212009-10-12 15:48:51 -070072 // Skip the ID3v2 header.
73
74 size_t len =
75 ((id3header[6] & 0x7f) << 21)
76 | ((id3header[7] & 0x7f) << 14)
77 | ((id3header[8] & 0x7f) << 7)
78 | (id3header[9] & 0x7f);
79
80 len += 10;
81
82 *inout_pos += len;
Andreas Huberba0707d2010-03-31 11:08:23 -070083
Steve Block3856b092011-10-20 11:56:00 +010084 ALOGV("skipped ID3 tag, new starting offset is %lld (0x%016llx)",
Andreas Huberba0707d2010-03-31 11:08:23 -070085 *inout_pos, *inout_pos);
Andreas Huber0bf39212009-10-12 15:48:51 -070086 }
Andreas Huber4456da52010-11-09 08:57:45 -080087
88 if (post_id3_pos != NULL) {
89 *post_id3_pos = *inout_pos;
90 }
Andreas Huber0bf39212009-10-12 15:48:51 -070091 }
92
James Dongc7fc37a2010-11-16 14:04:54 -080093 off64_t pos = *inout_pos;
Andreas Huber20111aa2009-07-14 16:56:47 -070094 bool valid = false;
James Dong7cc49772011-01-11 18:59:32 -080095
96 const size_t kMaxReadBytes = 1024;
97 const size_t kMaxBytesChecked = 128 * 1024;
98 uint8_t buf[kMaxReadBytes];
99 ssize_t bytesToRead = kMaxReadBytes;
100 ssize_t totalBytesRead = 0;
101 ssize_t remainingBytes = 0;
102 bool reachEOS = false;
103 uint8_t *tmp = buf;
104
Andreas Huber20111aa2009-07-14 16:56:47 -0700105 do {
James Dong7cc49772011-01-11 18:59:32 -0800106 if (pos >= *inout_pos + kMaxBytesChecked) {
Andreas Huberba0707d2010-03-31 11:08:23 -0700107 // Don't scan forever.
Steve Block3856b092011-10-20 11:56:00 +0100108 ALOGV("giving up at offset %lld", pos);
Andreas Huberba0707d2010-03-31 11:08:23 -0700109 break;
Andreas Huber20111aa2009-07-14 16:56:47 -0700110 }
111
James Dong7cc49772011-01-11 18:59:32 -0800112 if (remainingBytes < 4) {
113 if (reachEOS) {
114 break;
115 } else {
116 memcpy(buf, tmp, remainingBytes);
117 bytesToRead = kMaxReadBytes - remainingBytes;
James Donge23da612011-02-24 09:16:43 -0800118
119 /*
120 * The next read position should start from the end of
121 * the last buffer, and thus should include the remaining
122 * bytes in the buffer.
123 */
124 totalBytesRead = source->readAt(pos + remainingBytes,
125 buf + remainingBytes,
126 bytesToRead);
James Dong7cc49772011-01-11 18:59:32 -0800127 if (totalBytesRead <= 0) {
128 break;
129 }
130 reachEOS = (totalBytesRead != bytesToRead);
131 totalBytesRead += remainingBytes;
132 remainingBytes = totalBytesRead;
133 tmp = buf;
134 continue;
135 }
Andreas Huberba0707d2010-03-31 11:08:23 -0700136 }
137
138 uint32_t header = U32_AT(tmp);
Andreas Huber20111aa2009-07-14 16:56:47 -0700139
James Dongd6e27292011-09-06 19:18:43 -0700140 if (match_header != 0 && (header & kMask) != (match_header & kMask)) {
141 ++pos;
142 ++tmp;
143 --remainingBytes;
144 continue;
145 }
146
Andreas Huber20111aa2009-07-14 16:56:47 -0700147 size_t frame_size;
James Dongd6e27292011-09-06 19:18:43 -0700148 int sample_rate, num_channels, bitrate;
149 if (!GetMPEGAudioFrameSize(
150 header, &frame_size,
151 &sample_rate, &num_channels, &bitrate)) {
152 ++pos;
153 ++tmp;
154 --remainingBytes;
155 continue;
Andreas Huberc8d6c8b2009-07-17 08:28:57 -0700156 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700157
Steve Block3856b092011-10-20 11:56:00 +0100158 ALOGV("found possible 1st frame at %lld (header = 0x%08x)", pos, header);
Andreas Huber20111aa2009-07-14 16:56:47 -0700159
Andreas Huberc8d6c8b2009-07-17 08:28:57 -0700160 // We found what looks like a valid frame,
161 // now find its successors.
Andreas Huber20111aa2009-07-14 16:56:47 -0700162
James Dongc7fc37a2010-11-16 14:04:54 -0800163 off64_t test_pos = pos + frame_size;
Andreas Huber20111aa2009-07-14 16:56:47 -0700164
Andreas Huberc8d6c8b2009-07-17 08:28:57 -0700165 valid = true;
166 for (int j = 0; j < 3; ++j) {
167 uint8_t tmp[4];
Andreas Huber34769bc2009-10-23 10:22:30 -0700168 if (source->readAt(test_pos, tmp, 4) < 4) {
Andreas Huberc8d6c8b2009-07-17 08:28:57 -0700169 valid = false;
170 break;
Andreas Huber20111aa2009-07-14 16:56:47 -0700171 }
Andreas Huber54196292009-12-07 16:31:31 -0800172
Andreas Huberc8d6c8b2009-07-17 08:28:57 -0700173 uint32_t test_header = U32_AT(tmp);
174
Steve Block3856b092011-10-20 11:56:00 +0100175 ALOGV("subsequent header is %08x", test_header);
Andreas Huberc8d6c8b2009-07-17 08:28:57 -0700176
177 if ((test_header & kMask) != (header & kMask)) {
178 valid = false;
179 break;
180 }
181
182 size_t test_frame_size;
Andreas Huber386d6092011-05-19 08:37:39 -0700183 if (!GetMPEGAudioFrameSize(
Andreas Huber4456da52010-11-09 08:57:45 -0800184 test_header, &test_frame_size)) {
Andreas Huberc8d6c8b2009-07-17 08:28:57 -0700185 valid = false;
186 break;
187 }
188
Steve Block3856b092011-10-20 11:56:00 +0100189 ALOGV("found subsequent frame #%d at %lld", j + 2, test_pos);
Andreas Huberc8d6c8b2009-07-17 08:28:57 -0700190
191 test_pos += test_frame_size;
Andreas Huber20111aa2009-07-14 16:56:47 -0700192 }
193
194 if (valid) {
Andreas Huberba0707d2010-03-31 11:08:23 -0700195 *inout_pos = pos;
Andreas Huber20111aa2009-07-14 16:56:47 -0700196
197 if (out_header != NULL) {
198 *out_header = header;
199 }
200 } else {
Steve Block3856b092011-10-20 11:56:00 +0100201 ALOGV("no dice, no valid sequence of frames found.");
Andreas Huber20111aa2009-07-14 16:56:47 -0700202 }
203
Andreas Huberba0707d2010-03-31 11:08:23 -0700204 ++pos;
James Dong7cc49772011-01-11 18:59:32 -0800205 ++tmp;
206 --remainingBytes;
Andreas Huber20111aa2009-07-14 16:56:47 -0700207 } while (!valid);
208
Andreas Huber20111aa2009-07-14 16:56:47 -0700209 return valid;
210}
211
212class MP3Source : public MediaSource {
213public:
214 MP3Source(
Andreas Huber693d2712009-08-14 14:37:10 -0700215 const sp<MetaData> &meta, const sp<DataSource> &source,
James Dongc7fc37a2010-11-16 14:04:54 -0800216 off64_t first_frame_pos, uint32_t fixed_header,
Andreas Huber4456da52010-11-09 08:57:45 -0800217 const sp<MP3Seeker> &seeker);
Andreas Huber20111aa2009-07-14 16:56:47 -0700218
Andreas Huber20111aa2009-07-14 16:56:47 -0700219 virtual status_t start(MetaData *params = NULL);
220 virtual status_t stop();
221
222 virtual sp<MetaData> getFormat();
223
224 virtual status_t read(
225 MediaBuffer **buffer, const ReadOptions *options = NULL);
226
Andreas Huber693d2712009-08-14 14:37:10 -0700227protected:
228 virtual ~MP3Source();
229
Andreas Huber20111aa2009-07-14 16:56:47 -0700230private:
John Grossmanab736e12012-08-29 14:51:01 -0700231 static const size_t kMaxFrameSize;
Andreas Huber20111aa2009-07-14 16:56:47 -0700232 sp<MetaData> mMeta;
Andreas Huber693d2712009-08-14 14:37:10 -0700233 sp<DataSource> mDataSource;
James Dongc7fc37a2010-11-16 14:04:54 -0800234 off64_t mFirstFramePos;
Andreas Huber20111aa2009-07-14 16:56:47 -0700235 uint32_t mFixedHeader;
James Dongc7fc37a2010-11-16 14:04:54 -0800236 off64_t mCurrentPos;
Andreas Huber20111aa2009-07-14 16:56:47 -0700237 int64_t mCurrentTimeUs;
238 bool mStarted;
Andreas Huber4456da52010-11-09 08:57:45 -0800239 sp<MP3Seeker> mSeeker;
Andreas Huber20111aa2009-07-14 16:56:47 -0700240 MediaBufferGroup *mGroup;
241
Jason Simmonsa9e05b92011-04-06 18:16:12 -0700242 int64_t mBasisTimeUs;
243 int64_t mSamplesRead;
244
Andreas Huber20111aa2009-07-14 16:56:47 -0700245 MP3Source(const MP3Source &);
246 MP3Source &operator=(const MP3Source &);
247};
248
Andreas Huber5a1c3522010-08-25 11:09:41 -0700249MP3Extractor::MP3Extractor(
250 const sp<DataSource> &source, const sp<AMessage> &meta)
Andreas Huber772bcc22010-09-09 09:48:41 -0700251 : mInitCheck(NO_INIT),
252 mDataSource(source),
Andreas Huber20111aa2009-07-14 16:56:47 -0700253 mFirstFramePos(-1),
Andreas Huber4456da52010-11-09 08:57:45 -0800254 mFixedHeader(0) {
James Dongc7fc37a2010-11-16 14:04:54 -0800255 off64_t pos = 0;
256 off64_t post_id3_pos;
Andreas Huber20111aa2009-07-14 16:56:47 -0700257 uint32_t header;
Andreas Huber5a1c3522010-08-25 11:09:41 -0700258 bool success;
259
260 int64_t meta_offset;
261 uint32_t meta_header;
Andreas Huber4456da52010-11-09 08:57:45 -0800262 int64_t meta_post_id3_offset;
Andreas Huber5a1c3522010-08-25 11:09:41 -0700263 if (meta != NULL
264 && meta->findInt64("offset", &meta_offset)
Andreas Huber4456da52010-11-09 08:57:45 -0800265 && meta->findInt32("header", (int32_t *)&meta_header)
266 && meta->findInt64("post-id3-offset", &meta_post_id3_offset)) {
Andreas Huber5a1c3522010-08-25 11:09:41 -0700267 // The sniffer has already done all the hard work for us, simply
268 // accept its judgement.
James Dongc7fc37a2010-11-16 14:04:54 -0800269 pos = (off64_t)meta_offset;
Andreas Huber5a1c3522010-08-25 11:09:41 -0700270 header = meta_header;
James Dongc7fc37a2010-11-16 14:04:54 -0800271 post_id3_pos = (off64_t)meta_post_id3_offset;
Andreas Huber5a1c3522010-08-25 11:09:41 -0700272
273 success = true;
274 } else {
Andreas Huber4456da52010-11-09 08:57:45 -0800275 success = Resync(mDataSource, 0, &pos, &post_id3_pos, &header);
Andreas Huber5a1c3522010-08-25 11:09:41 -0700276 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700277
Andreas Huber772bcc22010-09-09 09:48:41 -0700278 if (!success) {
279 // mInitCheck will remain NO_INIT
280 return;
281 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700282
Andreas Huber772bcc22010-09-09 09:48:41 -0700283 mFirstFramePos = pos;
284 mFixedHeader = header;
Andreas Huber20111aa2009-07-14 16:56:47 -0700285
Andreas Huber772bcc22010-09-09 09:48:41 -0700286 size_t frame_size;
287 int sample_rate;
288 int num_channels;
289 int bitrate;
Andreas Huber386d6092011-05-19 08:37:39 -0700290 GetMPEGAudioFrameSize(
Andreas Huber772bcc22010-09-09 09:48:41 -0700291 header, &frame_size, &sample_rate, &num_channels, &bitrate);
Andreas Huber20111aa2009-07-14 16:56:47 -0700292
Andreas Huber2944eca2011-09-08 14:12:44 -0700293 unsigned layer = 4 - ((header >> 17) & 3);
294
Andreas Huber772bcc22010-09-09 09:48:41 -0700295 mMeta = new MetaData;
Andreas Huber20111aa2009-07-14 16:56:47 -0700296
Andreas Huber2944eca2011-09-08 14:12:44 -0700297 switch (layer) {
298 case 1:
299 mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_I);
300 break;
301 case 2:
302 mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II);
303 break;
304 case 3:
305 mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG);
306 break;
307 default:
308 TRESPASS();
309 }
310
Andreas Huber772bcc22010-09-09 09:48:41 -0700311 mMeta->setInt32(kKeySampleRate, sample_rate);
312 mMeta->setInt32(kKeyBitRate, bitrate * 1000);
313 mMeta->setInt32(kKeyChannelCount, num_channels);
314
Marco Nelissenb636abd2012-03-19 13:49:43 -0700315 sp<XINGSeeker> seeker = XINGSeeker::CreateFromSource(mDataSource, mFirstFramePos);
Andreas Huber4456da52010-11-09 08:57:45 -0800316
Marco Nelissenb636abd2012-03-19 13:49:43 -0700317 if (seeker == NULL) {
Andreas Huber4456da52010-11-09 08:57:45 -0800318 mSeeker = VBRISeeker::CreateFromSource(mDataSource, post_id3_pos);
Marco Nelissenb636abd2012-03-19 13:49:43 -0700319 } else {
320 mSeeker = seeker;
321 int encd = seeker->getEncoderDelay();
322 int encp = seeker->getEncoderPadding();
323 if (encd != 0 || encp != 0) {
324 mMeta->setInt32(kKeyEncoderDelay, encd);
325 mMeta->setInt32(kKeyEncoderPadding, encp);
326 }
Andreas Huber4456da52010-11-09 08:57:45 -0800327 }
328
Marco Nelissen9e503852012-03-16 07:56:42 -0700329 if (mSeeker != NULL) {
330 // While it is safe to send the XING/VBRI frame to the decoder, this will
331 // result in an extra 1152 samples being output. The real first frame to
332 // decode is after the XING/VBRI frame, so skip there.
333 mFirstFramePos += frame_size;
334 }
335
Andreas Huber4456da52010-11-09 08:57:45 -0800336 int64_t durationUs;
337
338 if (mSeeker == NULL || !mSeeker->getDuration(&durationUs)) {
James Dongc7fc37a2010-11-16 14:04:54 -0800339 off64_t fileSize;
Andreas Huber772bcc22010-09-09 09:48:41 -0700340 if (mDataSource->getSize(&fileSize) == OK) {
Andreas Huber4456da52010-11-09 08:57:45 -0800341 durationUs = 8000LL * (fileSize - mFirstFramePos) / bitrate;
342 } else {
343 durationUs = -1;
Andreas Huber20111aa2009-07-14 16:56:47 -0700344 }
345 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700346
Andreas Huber4456da52010-11-09 08:57:45 -0800347 if (durationUs >= 0) {
348 mMeta->setInt64(kKeyDuration, durationUs);
349 }
350
Andreas Huber772bcc22010-09-09 09:48:41 -0700351 mInitCheck = OK;
Marco Nelissen092406a2012-03-20 09:48:02 -0700352
Dylan Powers6e8f0bc2012-11-27 16:06:38 -0800353 // Get iTunes-style gapless info if present.
354 // When getting the id3 tag, skip the V1 tags to prevent the source cache
355 // from being iterated to the end of the file.
356 ID3 id3(mDataSource, true);
Marco Nelissen092406a2012-03-20 09:48:02 -0700357 if (id3.isValid()) {
358 ID3::Iterator *com = new ID3::Iterator(id3, "COM");
359 if (com->done()) {
360 delete com;
361 com = new ID3::Iterator(id3, "COMM");
362 }
363 while(!com->done()) {
364 String8 commentdesc;
365 String8 commentvalue;
366 com->getString(&commentdesc, &commentvalue);
367 const char * desc = commentdesc.string();
368 const char * value = commentvalue.string();
369
370 // first 3 characters are the language, which we don't care about
371 if(strlen(desc) > 3 && strcmp(desc + 3, "iTunSMPB") == 0) {
372
373 int32_t delay, padding;
374 if (sscanf(value, " %*x %x %x %*x", &delay, &padding) == 2) {
375 mMeta->setInt32(kKeyEncoderDelay, delay);
376 mMeta->setInt32(kKeyEncoderPadding, padding);
377 }
378 break;
379 }
380 com->next();
381 }
382 delete com;
383 com = NULL;
384 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700385}
386
Andreas Huber693d2712009-08-14 14:37:10 -0700387size_t MP3Extractor::countTracks() {
Andreas Huber772bcc22010-09-09 09:48:41 -0700388 return mInitCheck != OK ? 0 : 1;
Andreas Huber20111aa2009-07-14 16:56:47 -0700389}
390
Andreas Huber693d2712009-08-14 14:37:10 -0700391sp<MediaSource> MP3Extractor::getTrack(size_t index) {
Andreas Huber772bcc22010-09-09 09:48:41 -0700392 if (mInitCheck != OK || index != 0) {
Andreas Huber693d2712009-08-14 14:37:10 -0700393 return NULL;
Andreas Huber20111aa2009-07-14 16:56:47 -0700394 }
395
Andreas Huber693d2712009-08-14 14:37:10 -0700396 return new MP3Source(
Gloria Wangac6e6562009-12-09 15:46:19 -0800397 mMeta, mDataSource, mFirstFramePos, mFixedHeader,
Andreas Huber4456da52010-11-09 08:57:45 -0800398 mSeeker);
Andreas Huber20111aa2009-07-14 16:56:47 -0700399}
400
Andreas Huber7e04dcf2009-10-22 13:49:30 -0700401sp<MetaData> MP3Extractor::getTrackMetaData(size_t index, uint32_t flags) {
Andreas Huber772bcc22010-09-09 09:48:41 -0700402 if (mInitCheck != OK || index != 0) {
Andreas Huber20111aa2009-07-14 16:56:47 -0700403 return NULL;
404 }
405
406 return mMeta;
407}
408
409////////////////////////////////////////////////////////////////////////////////
410
John Grossmanab736e12012-08-29 14:51:01 -0700411// The theoretical maximum frame size for an MPEG audio stream should occur
412// while playing a Layer 2, MPEGv2.5 audio stream at 160kbps (with padding).
413// The size of this frame should be...
414// ((1152 samples/frame * 160000 bits/sec) /
415// (8000 samples/sec * 8 bits/byte)) + 1 padding byte/frame = 2881 bytes/frame.
416// Set our max frame size to the nearest power of 2 above this size (aka, 4kB)
417const size_t MP3Source::kMaxFrameSize = (1 << 12); /* 4096 bytes */
Andreas Huber20111aa2009-07-14 16:56:47 -0700418MP3Source::MP3Source(
Andreas Huber693d2712009-08-14 14:37:10 -0700419 const sp<MetaData> &meta, const sp<DataSource> &source,
James Dongc7fc37a2010-11-16 14:04:54 -0800420 off64_t first_frame_pos, uint32_t fixed_header,
Andreas Huber4456da52010-11-09 08:57:45 -0800421 const sp<MP3Seeker> &seeker)
Andreas Huber20111aa2009-07-14 16:56:47 -0700422 : mMeta(meta),
423 mDataSource(source),
424 mFirstFramePos(first_frame_pos),
425 mFixedHeader(fixed_header),
426 mCurrentPos(0),
427 mCurrentTimeUs(0),
428 mStarted(false),
Andreas Huber4456da52010-11-09 08:57:45 -0800429 mSeeker(seeker),
Jason Simmonsa9e05b92011-04-06 18:16:12 -0700430 mGroup(NULL),
431 mBasisTimeUs(0),
432 mSamplesRead(0) {
Andreas Huber20111aa2009-07-14 16:56:47 -0700433}
434
435MP3Source::~MP3Source() {
436 if (mStarted) {
437 stop();
438 }
439}
440
441status_t MP3Source::start(MetaData *) {
Andreas Huber0c891992009-08-26 14:48:20 -0700442 CHECK(!mStarted);
Andreas Huber20111aa2009-07-14 16:56:47 -0700443
444 mGroup = new MediaBufferGroup;
445
Andreas Huber20111aa2009-07-14 16:56:47 -0700446 mGroup->add_buffer(new MediaBuffer(kMaxFrameSize));
447
448 mCurrentPos = mFirstFramePos;
449 mCurrentTimeUs = 0;
450
Jason Simmonsa9e05b92011-04-06 18:16:12 -0700451 mBasisTimeUs = mCurrentTimeUs;
452 mSamplesRead = 0;
453
Andreas Huber20111aa2009-07-14 16:56:47 -0700454 mStarted = true;
455
456 return OK;
457}
458
459status_t MP3Source::stop() {
Andreas Huber0c891992009-08-26 14:48:20 -0700460 CHECK(mStarted);
Andreas Huber20111aa2009-07-14 16:56:47 -0700461
462 delete mGroup;
463 mGroup = NULL;
464
465 mStarted = false;
466
467 return OK;
468}
469
470sp<MetaData> MP3Source::getFormat() {
471 return mMeta;
472}
473
474status_t MP3Source::read(
475 MediaBuffer **out, const ReadOptions *options) {
476 *out = NULL;
477
478 int64_t seekTimeUs;
Andreas Huberabd1f4f2010-07-20 15:04:28 -0700479 ReadOptions::SeekMode mode;
Gloria Wangf9d566e2011-07-27 14:10:53 -0700480 bool seekCBR = false;
481
Andreas Huberabd1f4f2010-07-20 15:04:28 -0700482 if (options != NULL && options->getSeekTo(&seekTimeUs, &mode)) {
Andreas Huber4456da52010-11-09 08:57:45 -0800483 int64_t actualSeekTimeUs = seekTimeUs;
484 if (mSeeker == NULL
485 || !mSeeker->getOffsetForTime(&actualSeekTimeUs, &mCurrentPos)) {
486 int32_t bitrate;
487 if (!mMeta->findInt32(kKeyBitRate, &bitrate)) {
488 // bitrate is in bits/sec.
Steve Blockdf64d152012-01-04 20:05:49 +0000489 ALOGI("no bitrate");
Andreas Huber20111aa2009-07-14 16:56:47 -0700490
Andreas Huber4456da52010-11-09 08:57:45 -0800491 return ERROR_UNSUPPORTED;
Gloria Wangac6e6562009-12-09 15:46:19 -0800492 }
Andreas Huber4456da52010-11-09 08:57:45 -0800493
494 mCurrentTimeUs = seekTimeUs;
Gloria Wangac6e6562009-12-09 15:46:19 -0800495 mCurrentPos = mFirstFramePos + seekTimeUs * bitrate / 8000000;
Gloria Wangf9d566e2011-07-27 14:10:53 -0700496 seekCBR = true;
Andreas Huber4456da52010-11-09 08:57:45 -0800497 } else {
498 mCurrentTimeUs = actualSeekTimeUs;
Gloria Wangac6e6562009-12-09 15:46:19 -0800499 }
Jason Simmonsa9e05b92011-04-06 18:16:12 -0700500
501 mBasisTimeUs = mCurrentTimeUs;
502 mSamplesRead = 0;
Andreas Huber20111aa2009-07-14 16:56:47 -0700503 }
504
505 MediaBuffer *buffer;
506 status_t err = mGroup->acquire_buffer(&buffer);
507 if (err != OK) {
508 return err;
509 }
510
511 size_t frame_size;
Andreas Huber3e0339f2010-06-23 11:11:58 -0700512 int bitrate;
Jason Simmonsa9e05b92011-04-06 18:16:12 -0700513 int num_samples;
514 int sample_rate;
Andreas Huber20111aa2009-07-14 16:56:47 -0700515 for (;;) {
Andreas Huber34769bc2009-10-23 10:22:30 -0700516 ssize_t n = mDataSource->readAt(mCurrentPos, buffer->data(), 4);
Andreas Huber20111aa2009-07-14 16:56:47 -0700517 if (n < 4) {
518 buffer->release();
519 buffer = NULL;
520
521 return ERROR_END_OF_STREAM;
522 }
523
524 uint32_t header = U32_AT((const uint8_t *)buffer->data());
Andreas Huber54196292009-12-07 16:31:31 -0800525
526 if ((header & kMask) == (mFixedHeader & kMask)
Andreas Huber386d6092011-05-19 08:37:39 -0700527 && GetMPEGAudioFrameSize(
528 header, &frame_size, &sample_rate, NULL,
529 &bitrate, &num_samples)) {
Gloria Wangf9d566e2011-07-27 14:10:53 -0700530
531 // re-calculate mCurrentTimeUs because we might have called Resync()
532 if (seekCBR) {
533 mCurrentTimeUs = (mCurrentPos - mFirstFramePos) * 8000 / bitrate;
534 mBasisTimeUs = mCurrentTimeUs;
535 }
536
Andreas Huber20111aa2009-07-14 16:56:47 -0700537 break;
538 }
539
540 // Lost sync.
Steve Block3856b092011-10-20 11:56:00 +0100541 ALOGV("lost sync! header = 0x%08x, old header = 0x%08x\n", header, mFixedHeader);
Andreas Huber20111aa2009-07-14 16:56:47 -0700542
James Dongc7fc37a2010-11-16 14:04:54 -0800543 off64_t pos = mCurrentPos;
Andreas Huber4456da52010-11-09 08:57:45 -0800544 if (!Resync(mDataSource, mFixedHeader, &pos, NULL, NULL)) {
Steve Block29357bc2012-01-06 19:20:56 +0000545 ALOGE("Unable to resync. Signalling end of stream.");
Andreas Huber20111aa2009-07-14 16:56:47 -0700546
547 buffer->release();
548 buffer = NULL;
549
550 return ERROR_END_OF_STREAM;
551 }
552
553 mCurrentPos = pos;
554
555 // Try again with the new position.
556 }
557
Andreas Huber0c891992009-08-26 14:48:20 -0700558 CHECK(frame_size <= buffer->size());
Andreas Huber20111aa2009-07-14 16:56:47 -0700559
Andreas Huber34769bc2009-10-23 10:22:30 -0700560 ssize_t n = mDataSource->readAt(mCurrentPos, buffer->data(), frame_size);
Andreas Huber20111aa2009-07-14 16:56:47 -0700561 if (n < (ssize_t)frame_size) {
562 buffer->release();
563 buffer = NULL;
564
565 return ERROR_END_OF_STREAM;
566 }
567
568 buffer->set_range(0, frame_size);
569
Andreas Huber48c948b2009-10-08 10:07:49 -0700570 buffer->meta_data()->setInt64(kKeyTime, mCurrentTimeUs);
Andreas Huber8bf59e72010-08-06 14:13:10 -0700571 buffer->meta_data()->setInt32(kKeyIsSyncFrame, 1);
Andreas Huber20111aa2009-07-14 16:56:47 -0700572
573 mCurrentPos += frame_size;
Jason Simmonsa9e05b92011-04-06 18:16:12 -0700574
575 mSamplesRead += num_samples;
576 mCurrentTimeUs = mBasisTimeUs + ((mSamplesRead * 1000000) / sample_rate);
Andreas Huber20111aa2009-07-14 16:56:47 -0700577
578 *out = buffer;
579
580 return OK;
581}
582
Andreas Huberfc9ba092010-01-11 15:35:19 -0800583sp<MetaData> MP3Extractor::getMetaData() {
584 sp<MetaData> meta = new MetaData;
585
Andreas Huber772bcc22010-09-09 09:48:41 -0700586 if (mInitCheck != OK) {
Andreas Huber7be64072010-01-13 11:25:10 -0800587 return meta;
588 }
589
Andreas Huberfc9ba092010-01-11 15:35:19 -0800590 meta->setCString(kKeyMIMEType, "audio/mpeg");
591
592 ID3 id3(mDataSource);
593
594 if (!id3.isValid()) {
595 return meta;
596 }
597
598 struct Map {
599 int key;
600 const char *tag1;
601 const char *tag2;
602 };
603 static const Map kMap[] = {
604 { kKeyAlbum, "TALB", "TAL" },
605 { kKeyArtist, "TPE1", "TP1" },
Marco Nelissen66ac4df2010-02-11 13:31:44 -0800606 { kKeyAlbumArtist, "TPE2", "TP2" },
Andreas Huberfc9ba092010-01-11 15:35:19 -0800607 { kKeyComposer, "TCOM", "TCM" },
608 { kKeyGenre, "TCON", "TCO" },
Andreas Huberbd4bc592010-01-13 10:45:49 -0800609 { kKeyTitle, "TIT2", "TT2" },
Andreas Huberfc9ba092010-01-11 15:35:19 -0800610 { kKeyYear, "TYE", "TYER" },
Andreas Huberbd4bc592010-01-13 10:45:49 -0800611 { kKeyAuthor, "TXT", "TEXT" },
612 { kKeyCDTrackNumber, "TRK", "TRCK" },
Marco Nelissen6c8a9a42010-02-08 14:50:19 -0800613 { kKeyDiscNumber, "TPA", "TPOS" },
Marco Nelissen11f81092011-01-06 11:12:17 -0800614 { kKeyCompilation, "TCP", "TCMP" },
Andreas Huberfc9ba092010-01-11 15:35:19 -0800615 };
616 static const size_t kNumMapEntries = sizeof(kMap) / sizeof(kMap[0]);
617
618 for (size_t i = 0; i < kNumMapEntries; ++i) {
619 ID3::Iterator *it = new ID3::Iterator(id3, kMap[i].tag1);
620 if (it->done()) {
621 delete it;
622 it = new ID3::Iterator(id3, kMap[i].tag2);
623 }
624
625 if (it->done()) {
626 delete it;
627 continue;
628 }
629
630 String8 s;
631 it->getString(&s);
632 delete it;
633
634 meta->setCString(kMap[i].key, s);
635 }
636
637 size_t dataSize;
638 String8 mime;
639 const void *data = id3.getAlbumArt(&dataSize, &mime);
640
641 if (data) {
642 meta->setData(kKeyAlbumArt, MetaData::TYPE_NONE, data, dataSize);
643 meta->setCString(kKeyAlbumArtMIME, mime.string());
644 }
645
646 return meta;
647}
648
Andreas Huber693d2712009-08-14 14:37:10 -0700649bool SniffMP3(
Andreas Huber5a1c3522010-08-25 11:09:41 -0700650 const sp<DataSource> &source, String8 *mimeType,
651 float *confidence, sp<AMessage> *meta) {
James Dongc7fc37a2010-11-16 14:04:54 -0800652 off64_t pos = 0;
653 off64_t post_id3_pos;
Andreas Huber20111aa2009-07-14 16:56:47 -0700654 uint32_t header;
Andreas Huber4456da52010-11-09 08:57:45 -0800655 if (!Resync(source, 0, &pos, &post_id3_pos, &header)) {
Andreas Huber20111aa2009-07-14 16:56:47 -0700656 return false;
657 }
658
Andreas Huber5a1c3522010-08-25 11:09:41 -0700659 *meta = new AMessage;
660 (*meta)->setInt64("offset", pos);
661 (*meta)->setInt32("header", header);
Andreas Huber4456da52010-11-09 08:57:45 -0800662 (*meta)->setInt64("post-id3-offset", post_id3_pos);
Andreas Huber5a1c3522010-08-25 11:09:41 -0700663
Andreas Huber18291bc2009-09-10 14:13:30 -0700664 *mimeType = MEDIA_MIMETYPE_AUDIO_MPEG;
Andreas Huber5a1c3522010-08-25 11:09:41 -0700665 *confidence = 0.2f;
Andreas Huber20111aa2009-07-14 16:56:47 -0700666
667 return true;
668}
669
670} // namespace android