StreamingSource.cpp revision 5bc087c573c70c84c6a39946457590b42d392a33
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      mEOS(false) {
38}
39
40NuPlayer::StreamingSource::~StreamingSource() {
41}
42
43void NuPlayer::StreamingSource::start() {
44    mStreamListener = new NuPlayerStreamListener(mSource, 0);
45    mTSParser = new ATSParser;
46
47    mStreamListener->start();
48}
49
50bool NuPlayer::StreamingSource::feedMoreTSData() {
51    if (mEOS) {
52        return false;
53    }
54
55    for (int32_t i = 0; i < 10; ++i) {
56        char buffer[188];
57        ssize_t n = mStreamListener->read(buffer, sizeof(buffer));
58
59        if (n == 0) {
60            LOGI("input data EOS reached.");
61            mTSParser->signalEOS(ERROR_END_OF_STREAM);
62            mEOS = true;
63            break;
64        } else if (n == INFO_DISCONTINUITY) {
65            mTSParser->signalDiscontinuity(ATSParser::DISCONTINUITY_SEEK);
66        } else if (n < 0) {
67            CHECK_EQ(n, -EWOULDBLOCK);
68            break;
69        } else {
70            if (buffer[0] == 0x00) {
71                // XXX legacy
72                mTSParser->signalDiscontinuity(
73                        buffer[1] == 0x00
74                            ? ATSParser::DISCONTINUITY_SEEK
75                            : ATSParser::DISCONTINUITY_FORMATCHANGE);
76            } else {
77                mTSParser->feedTSPacket(buffer, sizeof(buffer));
78            }
79        }
80    }
81
82    return true;
83}
84
85sp<MetaData> NuPlayer::StreamingSource::getFormat(bool audio) {
86    ATSParser::SourceType type =
87        audio ? ATSParser::MPEG2ADTS_AUDIO : ATSParser::AVC_VIDEO;
88
89    sp<AnotherPacketSource> source =
90        static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
91
92    if (source == NULL) {
93        return NULL;
94    }
95
96    return source->getFormat();
97}
98
99status_t NuPlayer::StreamingSource::dequeueAccessUnit(
100        bool audio, sp<ABuffer> *accessUnit) {
101    ATSParser::SourceType type =
102        audio ? ATSParser::MPEG2ADTS_AUDIO : ATSParser::AVC_VIDEO;
103
104    sp<AnotherPacketSource> source =
105        static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
106
107    if (source == NULL) {
108        return -EWOULDBLOCK;
109    }
110
111    status_t finalResult;
112    if (!source->hasBufferAvailable(&finalResult)) {
113        return finalResult == OK ? -EWOULDBLOCK : finalResult;
114    }
115
116    return source->dequeueAccessUnit(accessUnit);
117}
118
119}  // namespace android
120
121