MPEG2TSExtractor.cpp revision b3f9759c8c9437c45b9a34519ce2ea38a8314d4e
1/*
2 * Copyright (C) 2010 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 "MPEG2TSExtractor"
19#include <utils/Log.h>
20
21#include "include/MPEG2TSExtractor.h"
22#include "include/NuCachedSource2.h"
23
24#include <media/stagefright/foundation/ADebug.h>
25#include <media/stagefright/DataSource.h>
26#include <media/stagefright/MediaDefs.h>
27#include <media/stagefright/MediaErrors.h>
28#include <media/stagefright/MediaSource.h>
29#include <media/stagefright/MetaData.h>
30#include <utils/String8.h>
31
32#include "AnotherPacketSource.h"
33#include "ATSParser.h"
34
35namespace android {
36
37static const size_t kTSPacketSize = 188;
38
39struct MPEG2TSSource : public MediaSource {
40    MPEG2TSSource(
41            const sp<MPEG2TSExtractor> &extractor,
42            const sp<AnotherPacketSource> &impl,
43            bool seekable);
44
45    virtual status_t start(MetaData *params = NULL);
46    virtual status_t stop();
47    virtual sp<MetaData> getFormat();
48
49    virtual status_t read(
50            MediaBuffer **buffer, const ReadOptions *options = NULL);
51
52private:
53    sp<MPEG2TSExtractor> mExtractor;
54    sp<AnotherPacketSource> mImpl;
55
56    // If there are both audio and video streams, only the video stream
57    // will be seekable, otherwise the single stream will be seekable.
58    bool mSeekable;
59
60    DISALLOW_EVIL_CONSTRUCTORS(MPEG2TSSource);
61};
62
63MPEG2TSSource::MPEG2TSSource(
64        const sp<MPEG2TSExtractor> &extractor,
65        const sp<AnotherPacketSource> &impl,
66        bool seekable)
67    : mExtractor(extractor),
68      mImpl(impl),
69      mSeekable(seekable) {
70}
71
72status_t MPEG2TSSource::start(MetaData *params) {
73    return mImpl->start(params);
74}
75
76status_t MPEG2TSSource::stop() {
77    return mImpl->stop();
78}
79
80sp<MetaData> MPEG2TSSource::getFormat() {
81    return mImpl->getFormat();
82}
83
84status_t MPEG2TSSource::read(
85        MediaBuffer **out, const ReadOptions *options) {
86    *out = NULL;
87
88    int64_t seekTimeUs;
89    ReadOptions::SeekMode seekMode;
90    if (mSeekable && options && options->getSeekTo(&seekTimeUs, &seekMode)) {
91        return ERROR_UNSUPPORTED;
92    }
93
94    status_t finalResult;
95    while (!mImpl->hasBufferAvailable(&finalResult)) {
96        if (finalResult != OK) {
97            return ERROR_END_OF_STREAM;
98        }
99
100        status_t err = mExtractor->feedMore();
101        if (err != OK) {
102            mImpl->signalEOS(err);
103        }
104    }
105
106    return mImpl->read(out, options);
107}
108
109////////////////////////////////////////////////////////////////////////////////
110
111MPEG2TSExtractor::MPEG2TSExtractor(const sp<DataSource> &source)
112    : mDataSource(source),
113      mParser(new ATSParser),
114      mOffset(0) {
115    init();
116}
117
118size_t MPEG2TSExtractor::countTracks() {
119    return mSourceImpls.size();
120}
121
122sp<MediaSource> MPEG2TSExtractor::getTrack(size_t index) {
123    if (index >= mSourceImpls.size()) {
124        return NULL;
125    }
126
127    bool seekable = true;
128    if (mSourceImpls.size() > 1) {
129        CHECK_EQ(mSourceImpls.size(), 2u);
130
131        sp<MetaData> meta = mSourceImpls.editItemAt(index)->getFormat();
132        const char *mime;
133        CHECK(meta->findCString(kKeyMIMEType, &mime));
134
135        if (!strncasecmp("audio/", mime, 6)) {
136            seekable = false;
137        }
138    }
139
140    return new MPEG2TSSource(this, mSourceImpls.editItemAt(index), seekable);
141}
142
143sp<MetaData> MPEG2TSExtractor::getTrackMetaData(
144        size_t index, uint32_t /* flags */) {
145    return index < mSourceImpls.size()
146        ? mSourceImpls.editItemAt(index)->getFormat() : NULL;
147}
148
149sp<MetaData> MPEG2TSExtractor::getMetaData() {
150    sp<MetaData> meta = new MetaData;
151    meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_CONTAINER_MPEG2TS);
152
153    return meta;
154}
155
156void MPEG2TSExtractor::init() {
157    bool haveAudio = false;
158    bool haveVideo = false;
159    int numPacketsParsed = 0;
160
161    while (feedMore() == OK) {
162        if (haveAudio && haveVideo) {
163            break;
164        }
165        if (!haveVideo) {
166            sp<AnotherPacketSource> impl =
167                (AnotherPacketSource *)mParser->getSource(
168                        ATSParser::VIDEO).get();
169
170            if (impl != NULL) {
171                haveVideo = true;
172                mSourceImpls.push(impl);
173            }
174        }
175
176        if (!haveAudio) {
177            sp<AnotherPacketSource> impl =
178                (AnotherPacketSource *)mParser->getSource(
179                        ATSParser::AUDIO).get();
180
181            if (impl != NULL) {
182                haveAudio = true;
183                mSourceImpls.push(impl);
184            }
185        }
186
187        if (++numPacketsParsed > 10000) {
188            break;
189        }
190    }
191
192    ALOGI("haveAudio=%d, haveVideo=%d", haveAudio, haveVideo);
193}
194
195status_t MPEG2TSExtractor::feedMore() {
196    Mutex::Autolock autoLock(mLock);
197
198    uint8_t packet[kTSPacketSize];
199    ssize_t n = mDataSource->readAt(mOffset, packet, kTSPacketSize);
200
201    if (n < (ssize_t)kTSPacketSize) {
202        return (n < 0) ? (status_t)n : ERROR_END_OF_STREAM;
203    }
204
205    mOffset += n;
206    return mParser->feedTSPacket(packet, kTSPacketSize);
207}
208
209uint32_t MPEG2TSExtractor::flags() const {
210    return CAN_PAUSE;
211}
212
213////////////////////////////////////////////////////////////////////////////////
214
215bool SniffMPEG2TS(
216        const sp<DataSource> &source, String8 *mimeType, float *confidence,
217        sp<AMessage> *) {
218    for (int i = 0; i < 5; ++i) {
219        char header;
220        if (source->readAt(kTSPacketSize * i, &header, 1) != 1
221                || header != 0x47) {
222            return false;
223        }
224    }
225
226    *confidence = 0.1f;
227    mimeType->setTo(MEDIA_MIMETYPE_CONTAINER_MPEG2TS);
228
229    return true;
230}
231
232}  // namespace android
233