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