AacAdtsExtractor.cpp revision bb832e853d4afb11b0a3287b2eb0cad87696d631
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
55bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (source->readAt(offset, &syncHeader, ADTS_HEADER_SIZE_UP_TO_FRAMESIZE)
56bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            != ADTS_HEADER_SIZE_UP_TO_FRAMESIZE) {
57bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        SL_LOGE("AacAdtsExtractor:: getFrameSize() returns 0 (syncword and header read error)");
58bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return 0;
59bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
60bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
61bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if ((syncword[0] != 0xff) || ((syncword[1] & 0xf6) != 0xf0)) {
62bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        SL_LOGE("AacAdtsExtractor:: getFrameSize() returns 0 (syncword pb)");
63bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return 0;
64bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
65bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
66bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    const uint8_t protectionAbsent = syncword[1] & 0x1;
67bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
68bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    frameSize = (header[0] & 0x3) << 11 | header[1] << 3 | header[2] >> 5;
69bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    // the frame size read already contains the size of the ADTS header, so no need to add it here
70bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    frameSize += protectionAbsent ? 0 : 2;
71bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
72bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    //SL_LOGV("AacAdtsExtractor:: getFrameSize() returns %u", frameSize);
73bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
74bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return frameSize;
75bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
76bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
77bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
78bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel TriviAacAdtsExtractor::AacAdtsExtractor(const sp<DataSource> &source)
79bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    : mDataSource(source),
80bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi      mInitCheck(NO_INIT),
81bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi      mFrameDurationUs(0) {
82bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
83bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    // difference with framework's AAC Extractor: we have already validated the data
84bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    // upon enqueueing, so no need to sniff the data:
85bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    //    String8 mimeType;
86bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    //    float confidence;
87bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    //    if (!SniffAAC(mDataSource, &mimeType, &confidence, NULL)) {
88bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    //        return;
89bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    //    }
90bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
91bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    uint8_t profile, sf_index, channel, header[2];
92bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (mDataSource->readAt(2, &header, 2) < 2) {
93bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return;
94bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
95bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
96bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    profile = (header[0] >> 6) & 0x3;
97bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    sf_index = (header[0] >> 2) & 0xf;
98bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    uint32_t sr = get_sample_rate(sf_index);
99bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
100bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (sr == 0) {
101bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return;
102bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
103bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    channel = (header[0] & 0x1) << 2 | (header[1] >> 6);
104bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
105bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    SL_LOGV("AacAdtsExtractor has found sr=%d channel=%d", sr, channel);
106bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
107bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mMeta = MakeAACCodecSpecificData(profile, sf_index, channel);
108bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
109bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    off64_t offset = 0;
110bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    off64_t streamSize, numFrames = 0;
111bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    size_t frameSize = 0;
112bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    int64_t duration = 0;
113bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
114bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (mDataSource->getSize(&streamSize) == OK) {
115bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        while (offset < streamSize) {
116bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            if ((frameSize = getFrameSize(mDataSource, offset)) == 0) {
117bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                //SL_LOGV("AacAdtsExtractor() querying framesize at offset=%lld", offset);
118bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                return;
119bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            }
120bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
121bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            offset += frameSize;
122bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            numFrames ++;
123bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        }
124bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
125bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        // Round up and get the duration
126bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        mFrameDurationUs = (1024 * 1000000ll + (sr - 1)) / sr;
127bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        duration = numFrames * mFrameDurationUs;
128bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        mMeta->setInt64(kKeyDuration, duration);
129bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
130bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
131bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mInitCheck = OK;
132bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
133bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
134bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
135bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
136bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel TriviAacAdtsExtractor::~AacAdtsExtractor() {
137bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
138bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
139bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
140bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivisp<MetaData> AacAdtsExtractor::getMetaData() {
141bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    sp<MetaData> meta = new MetaData;
142bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
143bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (mInitCheck != OK) {
144bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return meta;
145bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
146bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
147bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC_ADTS);
148bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
149bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return meta;
150bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
151bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
152bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
153bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivisize_t AacAdtsExtractor::countTracks() {
154bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return mInitCheck == OK ? 1 : 0;
155bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
156bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
157bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
158bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivisp<MediaSource> AacAdtsExtractor::getTrack(size_t index) {
159bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (mInitCheck != OK || index != 0) {
160bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return NULL;
161bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
162bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
163bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return new AacAdtsSource(mDataSource, mMeta, mFrameDurationUs);
164bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
165bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
166bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
167bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivisp<MetaData> AacAdtsExtractor::getTrackMetaData(size_t index, uint32_t flags) {
168bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (mInitCheck != OK || index != 0) {
169bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return NULL;
170bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
171bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
172bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return mMeta;
173bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
174bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
175bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
176bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi////////////////////////////////////////////////////////////////////////////////
177bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
178bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi// 8192 = 2^13, 13bit AAC frame size (in bytes)
179bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Triviconst size_t AacAdtsSource::kMaxFrameSize = 8192;
180bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
181bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel TriviAacAdtsSource::AacAdtsSource(
182bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        const sp<DataSource> &source, const sp<MetaData> &meta,
183bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        int64_t frame_duration_us)
184bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    : mDataSource(source),
185bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi      mMeta(meta),
186bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi      mOffset(0),
187bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi      mCurrentTimeUs(0),
188bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi      mStarted(false),
189bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi      mGroup(NULL),
190bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi      mFrameDurationUs(frame_duration_us) {
191bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
192bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
193bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
194bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel TriviAacAdtsSource::~AacAdtsSource() {
195bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (mStarted) {
196bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        stop();
197bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
198bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
199bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
200bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
201bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivistatus_t AacAdtsSource::start(MetaData *params) {
202bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    CHECK(!mStarted);
203bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
204bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mOffset = 0;
205bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mCurrentTimeUs = 0;
206bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mGroup = new MediaBufferGroup;
207bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mGroup->add_buffer(new MediaBuffer(kMaxFrameSize));
208bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mStarted = true;
209bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
210bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return OK;
211bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
212bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
213bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
214bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivistatus_t AacAdtsSource::stop() {
215bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    CHECK(mStarted);
216bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
217bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    delete mGroup;
218bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mGroup = NULL;
219bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
220bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mStarted = false;
221bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return OK;
222bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
223bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
224bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
225bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivisp<MetaData> AacAdtsSource::getFormat() {
226bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return mMeta;
227bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
228bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
229bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
230bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivistatus_t AacAdtsSource::read(
231bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        MediaBuffer **out, const ReadOptions *options) {
232bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    *out = NULL;
233bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
234bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    int64_t seekTimeUs;
235bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    ReadOptions::SeekMode mode;
236bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (options && options->getSeekTo(&seekTimeUs, &mode)) {
237bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        // difference with framework's AAC Extractor: no seeking
238bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        SL_LOGE("Can't seek in AAC ADTS buffer queue");
239bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
240bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
241bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    size_t frameSize, frameSizeWithoutHeader;
242bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    SL_LOGV("AacAdtsSource::read() offset=%lld", mOffset);
243bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if ((frameSize = getFrameSize(mDataSource, mOffset)) == 0) {
244bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        LOGE("AacAdtsSource::read() returns EOS");
245bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return ERROR_END_OF_STREAM;
246bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        // FIXME if we return EOS here, verify we can restart decoding when we get new data
247bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        //  with start()
248bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        //LOGE("AacAdtsSource::read() should return EOS, but returning OK");
249bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        //return OK;
250bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
251bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
252bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    MediaBuffer *buffer;
253bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    status_t err = mGroup->acquire_buffer(&buffer);
254bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (err != OK) {
255bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return err;
256bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
257bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
258bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    frameSizeWithoutHeader = frameSize - ADTS_HEADER_LENGTH;
259bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    size_t readSize = mDataSource->readAt(mOffset + ADTS_HEADER_LENGTH, buffer->data(),
260bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            frameSizeWithoutHeader);
261bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    //SL_LOGV("AacAdtsSource::read() readAt returned %u bytes", readSize);
262bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (readSize != (ssize_t)frameSizeWithoutHeader) {
263bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        SL_LOGW("AacAdtsSource::read() readSize != frameSizeWithoutHeader");
264bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        buffer->release();
265bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        buffer = NULL;
266bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return ERROR_IO;
267bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
268bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
269bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    buffer->set_range(0, frameSizeWithoutHeader);
270bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    buffer->meta_data()->setInt64(kKeyTime, mCurrentTimeUs);
271bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    buffer->meta_data()->setInt32(kKeyIsSyncFrame, 1);
272bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
273bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mOffset += frameSize;
274bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    mCurrentTimeUs += mFrameDurationUs;
275bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
276bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    *out = buffer;
277bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return OK;
278bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
279bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
280bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}  // namespace android
281