AacAdtsExtractor.cpp revision 677c76347d9aaca4cf3746b3dbfc8a741281066b
1bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi/*
2bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * Copyright (C) 2011 The Android Open Source Project
3bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi *
4bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * Licensed under the Apache License, Version 2.0 (the "License");
5bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * you may not use this file except in compliance with the License.
6bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * You may obtain a copy of the License at
7bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi *
8bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi *      http://www.apache.org/licenses/LICENSE-2.0
9bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi *
10bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * Unless required by applicable law or agreed to in writing, software
11bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * distributed under the License is distributed on an "AS IS" BASIS,
12bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * See the License for the specific language governing permissions and
14bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * limitations under the License.
15bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi */
16bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
17bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#include "sllog.h"
18bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#include <utils/Log.h>
19bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
20bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#include "android/include/AacAdtsExtractor.h"
21bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#include "include/avc_utils.h"
22bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
23bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
24bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivinamespace android {
25bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
26bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#define ADTS_HEADER_LENGTH 7
27bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi// ADTS header size is 7, but frame size information ends on byte 6 (when counting from byte 1)
28bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#define ADTS_HEADER_SIZE_UP_TO_FRAMESIZE 6
29bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
30bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi////////////////////////////////////////////////////////////////////////////////
31bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
32bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi// Returns the sample rate based on the sampling frequency index
33bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivistatic uint32_t get_sample_rate(const uint8_t sf_index)
34bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi{
35bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    static const uint32_t sample_rates[] =
36bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    {
37bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        96000, 88200, 64000, 48000, 44100, 32000,
38bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        24000, 22050, 16000, 12000, 11025, 8000
39bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    };
40bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
41bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (sf_index < sizeof(sample_rates) / sizeof(sample_rates[0])) {
42bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return sample_rates[sf_index];
43bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
44bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
45bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return 0;
46bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
47bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
48bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivistatic size_t getFrameSize(const sp<DataSource> &source, off64_t offset) {
49bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    size_t frameSize = 0;
50bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
51bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    uint8_t syncHeader[ADTS_HEADER_SIZE_UP_TO_FRAMESIZE];
52bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    const uint8_t *syncword = syncHeader;
53bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    const uint8_t *header = syncHeader + 3;
54bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
55677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten    ssize_t readSize = source->readAt(offset, &syncHeader, ADTS_HEADER_SIZE_UP_TO_FRAMESIZE);
56677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten    if (readSize == 0) {
57677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten        // EOS is normal, not an error
58677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten        SL_LOGV("AacAdtsExtractor::getFrameSize EOS");
59677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten        return 0;
60677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten    }
61677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten    if (readSize != ADTS_HEADER_SIZE_UP_TO_FRAMESIZE) {
62677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten        SL_LOGE("AacAdtsExtractor:: getFrameSize() returns %d (syncword and header read error)",
63677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten                (int) readSize);
64bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return 0;
65bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
66bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
67bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if ((syncword[0] != 0xff) || ((syncword[1] & 0xf6) != 0xf0)) {
68bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        SL_LOGE("AacAdtsExtractor:: getFrameSize() returns 0 (syncword pb)");
69bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return 0;
70bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
71bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
72bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    const uint8_t protectionAbsent = syncword[1] & 0x1;
73bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
74bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    frameSize = (header[0] & 0x3) << 11 | header[1] << 3 | header[2] >> 5;
75bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    // the frame size read already contains the size of the ADTS header, so no need to add it here
76bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    frameSize += protectionAbsent ? 0 : 2;
77bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
78bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    //SL_LOGV("AacAdtsExtractor:: getFrameSize() returns %u", frameSize);
79bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
80bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return frameSize;
81bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
82bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
83bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
84bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel TriviAacAdtsExtractor::AacAdtsExtractor(const sp<DataSource> &source)
85bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    : mDataSource(source),
86bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi      mInitCheck(NO_INIT),
87bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi      mFrameDurationUs(0) {
88bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
89bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    // difference with framework's AAC Extractor: we have already validated the data
90bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    // upon enqueueing, so no need to sniff the data:
91bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    //    String8 mimeType;
92bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    //    float confidence;
93bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    //    if (!SniffAAC(mDataSource, &mimeType, &confidence, NULL)) {
94bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    //        return;
95bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    //    }
96bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
97bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    uint8_t profile, sf_index, channel, header[2];
98bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (mDataSource->readAt(2, &header, 2) < 2) {
99bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return;
100bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
101bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
102bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    profile = (header[0] >> 6) & 0x3;
103bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    sf_index = (header[0] >> 2) & 0xf;
104bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    uint32_t sr = get_sample_rate(sf_index);
105bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
106bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (sr == 0) {
107bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return;
108bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
109bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    channel = (header[0] & 0x1) << 2 | (header[1] >> 6);
110bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
111bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    SL_LOGV("AacAdtsExtractor has found sr=%d channel=%d", sr, channel);
112bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
113bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mMeta = MakeAACCodecSpecificData(profile, sf_index, channel);
114bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
115bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    off64_t offset = 0;
116bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    off64_t streamSize, numFrames = 0;
117bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    size_t frameSize = 0;
118bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    int64_t duration = 0;
119bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
120bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (mDataSource->getSize(&streamSize) == OK) {
121bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        while (offset < streamSize) {
122bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            if ((frameSize = getFrameSize(mDataSource, offset)) == 0) {
123bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                //SL_LOGV("AacAdtsExtractor() querying framesize at offset=%lld", offset);
124bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                return;
125bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            }
126bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
127bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            offset += frameSize;
128bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            numFrames ++;
129bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        }
130bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
131bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        // Round up and get the duration
132bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        mFrameDurationUs = (1024 * 1000000ll + (sr - 1)) / sr;
133bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        duration = numFrames * mFrameDurationUs;
134bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        mMeta->setInt64(kKeyDuration, duration);
135bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
136bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
137bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mInitCheck = OK;
138bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
139bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
140bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
141bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
142bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel TriviAacAdtsExtractor::~AacAdtsExtractor() {
143bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
144bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
145bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
146bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivisp<MetaData> AacAdtsExtractor::getMetaData() {
147bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    sp<MetaData> meta = new MetaData;
148bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
149bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (mInitCheck != OK) {
150bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return meta;
151bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
152bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
153bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC_ADTS);
154bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
155bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return meta;
156bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
157bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
158bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
159bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivisize_t AacAdtsExtractor::countTracks() {
160bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return mInitCheck == OK ? 1 : 0;
161bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
162bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
163bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
164bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivisp<MediaSource> AacAdtsExtractor::getTrack(size_t index) {
165bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (mInitCheck != OK || index != 0) {
166bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return NULL;
167bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
168bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
169bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return new AacAdtsSource(mDataSource, mMeta, mFrameDurationUs);
170bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
171bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
172bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
173bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivisp<MetaData> AacAdtsExtractor::getTrackMetaData(size_t index, uint32_t flags) {
174bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (mInitCheck != OK || index != 0) {
175bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return NULL;
176bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
177bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
178bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return mMeta;
179bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
180bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
181bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
182bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi////////////////////////////////////////////////////////////////////////////////
183bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
184bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi// 8192 = 2^13, 13bit AAC frame size (in bytes)
185bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Triviconst size_t AacAdtsSource::kMaxFrameSize = 8192;
186bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
187bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel TriviAacAdtsSource::AacAdtsSource(
188bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        const sp<DataSource> &source, const sp<MetaData> &meta,
189bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        int64_t frame_duration_us)
190bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    : mDataSource(source),
191bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi      mMeta(meta),
192bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi      mOffset(0),
193bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi      mCurrentTimeUs(0),
194bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi      mStarted(false),
195bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi      mGroup(NULL),
196bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi      mFrameDurationUs(frame_duration_us) {
197bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
198bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
199bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
200bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel TriviAacAdtsSource::~AacAdtsSource() {
201bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (mStarted) {
202bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        stop();
203bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
204bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
205bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
206bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
207bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivistatus_t AacAdtsSource::start(MetaData *params) {
208bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    CHECK(!mStarted);
209bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
210bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mOffset = 0;
211bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mCurrentTimeUs = 0;
212bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mGroup = new MediaBufferGroup;
213bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mGroup->add_buffer(new MediaBuffer(kMaxFrameSize));
214bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mStarted = true;
215bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
216bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return OK;
217bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
218bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
219bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
220bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivistatus_t AacAdtsSource::stop() {
221bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    CHECK(mStarted);
222bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
223bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    delete mGroup;
224bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mGroup = NULL;
225bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
226bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mStarted = false;
227bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return OK;
228bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
229bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
230bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
231bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivisp<MetaData> AacAdtsSource::getFormat() {
232bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return mMeta;
233bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
234bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
235bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
236bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivistatus_t AacAdtsSource::read(
237bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        MediaBuffer **out, const ReadOptions *options) {
238bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    *out = NULL;
239bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
240bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    int64_t seekTimeUs;
241bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    ReadOptions::SeekMode mode;
242bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (options && options->getSeekTo(&seekTimeUs, &mode)) {
243bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        // difference with framework's AAC Extractor: no seeking
244bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        SL_LOGE("Can't seek in AAC ADTS buffer queue");
245bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
246bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
247bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    size_t frameSize, frameSizeWithoutHeader;
248bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    SL_LOGV("AacAdtsSource::read() offset=%lld", mOffset);
249bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if ((frameSize = getFrameSize(mDataSource, mOffset)) == 0) {
250677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten        SL_LOGV("AacAdtsSource::read() returns EOS");
251bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return ERROR_END_OF_STREAM;
252bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        // FIXME if we return EOS here, verify we can restart decoding when we get new data
253bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        //  with start()
254bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        //LOGE("AacAdtsSource::read() should return EOS, but returning OK");
255bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        //return OK;
256bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
257bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
258bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    MediaBuffer *buffer;
259bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    status_t err = mGroup->acquire_buffer(&buffer);
260bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (err != OK) {
261bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return err;
262bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
263bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
264bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    frameSizeWithoutHeader = frameSize - ADTS_HEADER_LENGTH;
265677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten    ssize_t readSize = mDataSource->readAt(mOffset + ADTS_HEADER_LENGTH, buffer->data(),
266bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            frameSizeWithoutHeader);
267bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    //SL_LOGV("AacAdtsSource::read() readAt returned %u bytes", readSize);
268bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (readSize != (ssize_t)frameSizeWithoutHeader) {
269bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        SL_LOGW("AacAdtsSource::read() readSize != frameSizeWithoutHeader");
270bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        buffer->release();
271bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        buffer = NULL;
272bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return ERROR_IO;
273bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
274bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
275bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    buffer->set_range(0, frameSizeWithoutHeader);
276bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    buffer->meta_data()->setInt64(kKeyTime, mCurrentTimeUs);
277bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    buffer->meta_data()->setInt32(kKeyIsSyncFrame, 1);
278bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
279bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mOffset += frameSize;
280bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mCurrentTimeUs += mFrameDurationUs;
281bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
282bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    *out = buffer;
283bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return OK;
284bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
285bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
286bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}  // namespace android
287