StreamingSource.cpp revision 87f2a558dd12043631e12c361abef301bf603140
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 "StreamingSource"
19#include <utils/Log.h>
20
21#include "StreamingSource.h"
22
23#include "ATSParser.h"
24#include "AnotherPacketSource.h"
25#include "NuPlayerStreamListener.h"
26
27#include <media/stagefright/foundation/ABuffer.h>
28#include <media/stagefright/foundation/ADebug.h>
29#include <media/stagefright/foundation/AMessage.h>
30#include <media/stagefright/MediaSource.h>
31#include <media/stagefright/MetaData.h>
32
33namespace android {
34
35NuPlayer::StreamingSource::StreamingSource(const sp<IStreamSource> &source)
36    : mSource(source),
37      mFinalResult(OK) {
38}
39
40NuPlayer::StreamingSource::~StreamingSource() {
41}
42
43void NuPlayer::StreamingSource::start() {
44    mStreamListener = new NuPlayerStreamListener(mSource, 0);
45
46    uint32_t sourceFlags = mSource->flags();
47
48    uint32_t parserFlags = ATSParser::TS_TIMESTAMPS_ARE_ABSOLUTE;
49    if (sourceFlags & IStreamSource::kFlagAlignedVideoData) {
50        parserFlags |= ATSParser::ALIGNED_VIDEO_DATA;
51    }
52
53    mTSParser = new ATSParser(parserFlags);
54
55    mStreamListener->start();
56}
57
58status_t NuPlayer::StreamingSource::feedMoreTSData() {
59    if (mFinalResult != OK) {
60        return mFinalResult;
61    }
62
63    for (int32_t i = 0; i < 50; ++i) {
64        char buffer[188];
65        sp<AMessage> extra;
66        ssize_t n = mStreamListener->read(buffer, sizeof(buffer), &extra);
67
68        if (n == 0) {
69            ALOGI("input data EOS reached.");
70            mTSParser->signalEOS(ERROR_END_OF_STREAM);
71            mFinalResult = ERROR_END_OF_STREAM;
72            break;
73        } else if (n == INFO_DISCONTINUITY) {
74            int32_t type = ATSParser::DISCONTINUITY_SEEK;
75
76            int32_t mask;
77            if (extra != NULL
78                    && extra->findInt32(
79                        IStreamListener::kKeyDiscontinuityMask, &mask)) {
80                if (mask == 0) {
81                    ALOGE("Client specified an illegal discontinuity type.");
82                    return ERROR_UNSUPPORTED;
83                }
84
85                type = mask;
86            }
87
88            mTSParser->signalDiscontinuity(
89                    (ATSParser::DiscontinuityType)type, extra);
90        } else if (n < 0) {
91            CHECK_EQ(n, -EWOULDBLOCK);
92            break;
93        } else {
94            if (buffer[0] == 0x00) {
95                // XXX legacy
96                mTSParser->signalDiscontinuity(
97                        buffer[1] == 0x00
98                            ? ATSParser::DISCONTINUITY_SEEK
99                            : ATSParser::DISCONTINUITY_FORMATCHANGE,
100                        extra);
101            } else {
102                status_t err = mTSParser->feedTSPacket(buffer, sizeof(buffer));
103
104                if (err != OK) {
105                    ALOGE("TS Parser returned error %d", err);
106
107                    mTSParser->signalEOS(err);
108                    mFinalResult = err;
109                    break;
110                }
111            }
112        }
113    }
114
115    return OK;
116}
117
118sp<MetaData> NuPlayer::StreamingSource::getFormatMeta(bool audio) {
119    ATSParser::SourceType type =
120        audio ? ATSParser::AUDIO : ATSParser::VIDEO;
121
122    sp<AnotherPacketSource> source =
123        static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
124
125    if (source == NULL) {
126        return NULL;
127    }
128
129    return source->getFormat();
130}
131
132status_t NuPlayer::StreamingSource::dequeueAccessUnit(
133        bool audio, sp<ABuffer> *accessUnit) {
134    ATSParser::SourceType type =
135        audio ? ATSParser::AUDIO : ATSParser::VIDEO;
136
137    sp<AnotherPacketSource> source =
138        static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
139
140    if (source == NULL) {
141        return -EWOULDBLOCK;
142    }
143
144    status_t finalResult;
145    if (!source->hasBufferAvailable(&finalResult)) {
146        return finalResult == OK ? -EWOULDBLOCK : finalResult;
147    }
148
149    status_t err = source->dequeueAccessUnit(accessUnit);
150
151#if !defined(LOG_NDEBUG) || LOG_NDEBUG == 0
152    if (err == OK) {
153        int64_t timeUs;
154        CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
155        ALOGV("dequeueAccessUnit timeUs=%lld us", timeUs);
156    }
157#endif
158
159    return err;
160}
161
162}  // namespace android
163
164