15bc087c573c70c84c6a39946457590b42d392a33Andreas Huber/*
25bc087c573c70c84c6a39946457590b42d392a33Andreas Huber * Copyright (C) 2010 The Android Open Source Project
35bc087c573c70c84c6a39946457590b42d392a33Andreas Huber *
45bc087c573c70c84c6a39946457590b42d392a33Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
55bc087c573c70c84c6a39946457590b42d392a33Andreas Huber * you may not use this file except in compliance with the License.
65bc087c573c70c84c6a39946457590b42d392a33Andreas Huber * You may obtain a copy of the License at
75bc087c573c70c84c6a39946457590b42d392a33Andreas Huber *
85bc087c573c70c84c6a39946457590b42d392a33Andreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
95bc087c573c70c84c6a39946457590b42d392a33Andreas Huber *
105bc087c573c70c84c6a39946457590b42d392a33Andreas Huber * Unless required by applicable law or agreed to in writing, software
115bc087c573c70c84c6a39946457590b42d392a33Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
125bc087c573c70c84c6a39946457590b42d392a33Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135bc087c573c70c84c6a39946457590b42d392a33Andreas Huber * See the License for the specific language governing permissions and
145bc087c573c70c84c6a39946457590b42d392a33Andreas Huber * limitations under the License.
155bc087c573c70c84c6a39946457590b42d392a33Andreas Huber */
165bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
175bc087c573c70c84c6a39946457590b42d392a33Andreas Huber//#define LOG_NDEBUG 0
185bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#define LOG_TAG "StreamingSource"
195bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include <utils/Log.h>
205bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
215bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include "StreamingSource.h"
225bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
235bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include "ATSParser.h"
245bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include "AnotherPacketSource.h"
255bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include "NuPlayerStreamListener.h"
265bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
275bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include <media/stagefright/foundation/ABuffer.h>
285bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include <media/stagefright/foundation/ADebug.h>
295bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include <media/stagefright/foundation/AMessage.h>
305bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include <media/stagefright/MediaSource.h>
315bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include <media/stagefright/MetaData.h>
325bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
335bc087c573c70c84c6a39946457590b42d392a33Andreas Hubernamespace android {
345bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
355bc087c573c70c84c6a39946457590b42d392a33Andreas HuberNuPlayer::StreamingSource::StreamingSource(const sp<IStreamSource> &source)
365bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    : mSource(source),
37eac68baf095aeef54865c28b6888924dc6295cbdAndreas Huber      mFinalResult(OK) {
385bc087c573c70c84c6a39946457590b42d392a33Andreas Huber}
395bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
405bc087c573c70c84c6a39946457590b42d392a33Andreas HuberNuPlayer::StreamingSource::~StreamingSource() {
415bc087c573c70c84c6a39946457590b42d392a33Andreas Huber}
425bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
435bc087c573c70c84c6a39946457590b42d392a33Andreas Hubervoid NuPlayer::StreamingSource::start() {
445bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    mStreamListener = new NuPlayerStreamListener(mSource, 0);
4587f2a558dd12043631e12c361abef301bf603140Andreas Huber
4687f2a558dd12043631e12c361abef301bf603140Andreas Huber    uint32_t sourceFlags = mSource->flags();
4787f2a558dd12043631e12c361abef301bf603140Andreas Huber
4887f2a558dd12043631e12c361abef301bf603140Andreas Huber    uint32_t parserFlags = ATSParser::TS_TIMESTAMPS_ARE_ABSOLUTE;
4987f2a558dd12043631e12c361abef301bf603140Andreas Huber    if (sourceFlags & IStreamSource::kFlagAlignedVideoData) {
5087f2a558dd12043631e12c361abef301bf603140Andreas Huber        parserFlags |= ATSParser::ALIGNED_VIDEO_DATA;
5187f2a558dd12043631e12c361abef301bf603140Andreas Huber    }
5287f2a558dd12043631e12c361abef301bf603140Andreas Huber
5387f2a558dd12043631e12c361abef301bf603140Andreas Huber    mTSParser = new ATSParser(parserFlags);
545bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
555bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    mStreamListener->start();
565bc087c573c70c84c6a39946457590b42d392a33Andreas Huber}
575bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
58eac68baf095aeef54865c28b6888924dc6295cbdAndreas Huberstatus_t NuPlayer::StreamingSource::feedMoreTSData() {
59eac68baf095aeef54865c28b6888924dc6295cbdAndreas Huber    if (mFinalResult != OK) {
60eac68baf095aeef54865c28b6888924dc6295cbdAndreas Huber        return mFinalResult;
615bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    }
625bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
63078cfcf7cce9185ec7559910d08b0bc02bfc88a3Andreas Huber    for (int32_t i = 0; i < 50; ++i) {
645bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        char buffer[188];
6532f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber        sp<AMessage> extra;
6632f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber        ssize_t n = mStreamListener->read(buffer, sizeof(buffer), &extra);
675bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
685bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        if (n == 0) {
69df64d15042bbd5e0e4933ac49bf3c177dd94752cSteve Block            ALOGI("input data EOS reached.");
705bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            mTSParser->signalEOS(ERROR_END_OF_STREAM);
71eac68baf095aeef54865c28b6888924dc6295cbdAndreas Huber            mFinalResult = ERROR_END_OF_STREAM;
725bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            break;
735bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        } else if (n == INFO_DISCONTINUITY) {
74bfcc8d8ab7c56bc013bd221a29e1ecf3a6390813Andreas Huber            int32_t type = ATSParser::DISCONTINUITY_SEEK;
7542e549e4ab54802d788c43e3a04a85b7a1a95e97Andreas Huber
76bfcc8d8ab7c56bc013bd221a29e1ecf3a6390813Andreas Huber            int32_t mask;
7742e549e4ab54802d788c43e3a04a85b7a1a95e97Andreas Huber            if (extra != NULL
7842e549e4ab54802d788c43e3a04a85b7a1a95e97Andreas Huber                    && extra->findInt32(
79bfcc8d8ab7c56bc013bd221a29e1ecf3a6390813Andreas Huber                        IStreamListener::kKeyDiscontinuityMask, &mask)) {
80bfcc8d8ab7c56bc013bd221a29e1ecf3a6390813Andreas Huber                if (mask == 0) {
8129357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block                    ALOGE("Client specified an illegal discontinuity type.");
82bfcc8d8ab7c56bc013bd221a29e1ecf3a6390813Andreas Huber                    return ERROR_UNSUPPORTED;
83bfcc8d8ab7c56bc013bd221a29e1ecf3a6390813Andreas Huber                }
84bfcc8d8ab7c56bc013bd221a29e1ecf3a6390813Andreas Huber
85bfcc8d8ab7c56bc013bd221a29e1ecf3a6390813Andreas Huber                type = mask;
8642e549e4ab54802d788c43e3a04a85b7a1a95e97Andreas Huber            }
8742e549e4ab54802d788c43e3a04a85b7a1a95e97Andreas Huber
88bfcc8d8ab7c56bc013bd221a29e1ecf3a6390813Andreas Huber            mTSParser->signalDiscontinuity(
89bfcc8d8ab7c56bc013bd221a29e1ecf3a6390813Andreas Huber                    (ATSParser::DiscontinuityType)type, extra);
905bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        } else if (n < 0) {
915bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            CHECK_EQ(n, -EWOULDBLOCK);
925bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            break;
935bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        } else {
945bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            if (buffer[0] == 0x00) {
955bc087c573c70c84c6a39946457590b42d392a33Andreas Huber                // XXX legacy
965bc087c573c70c84c6a39946457590b42d392a33Andreas Huber                mTSParser->signalDiscontinuity(
975bc087c573c70c84c6a39946457590b42d392a33Andreas Huber                        buffer[1] == 0x00
985bc087c573c70c84c6a39946457590b42d392a33Andreas Huber                            ? ATSParser::DISCONTINUITY_SEEK
9932f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber                            : ATSParser::DISCONTINUITY_FORMATCHANGE,
10032f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber                        extra);
1015bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            } else {
10206528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                status_t err = mTSParser->feedTSPacket(buffer, sizeof(buffer));
10306528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
10406528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                if (err != OK) {
10529357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block                    ALOGE("TS Parser returned error %d", err);
10606528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
10706528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                    mTSParser->signalEOS(err);
108eac68baf095aeef54865c28b6888924dc6295cbdAndreas Huber                    mFinalResult = err;
10906528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                    break;
11006528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                }
1115bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            }
1125bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        }
1135bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    }
1145bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
115eac68baf095aeef54865c28b6888924dc6295cbdAndreas Huber    return OK;
1165bc087c573c70c84c6a39946457590b42d392a33Andreas Huber}
1175bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
118840667883fd09d44015716d79bc3ac4d60edc0f0Andreas Hubersp<MetaData> NuPlayer::StreamingSource::getFormatMeta(bool audio) {
1195bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    ATSParser::SourceType type =
120386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        audio ? ATSParser::AUDIO : ATSParser::VIDEO;
1215bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
1225bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    sp<AnotherPacketSource> source =
1235bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
1245bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
1255bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    if (source == NULL) {
1265bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        return NULL;
1275bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    }
1285bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
1295bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    return source->getFormat();
1305bc087c573c70c84c6a39946457590b42d392a33Andreas Huber}
1315bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
1325bc087c573c70c84c6a39946457590b42d392a33Andreas Huberstatus_t NuPlayer::StreamingSource::dequeueAccessUnit(
1335bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        bool audio, sp<ABuffer> *accessUnit) {
1345bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    ATSParser::SourceType type =
135386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        audio ? ATSParser::AUDIO : ATSParser::VIDEO;
1365bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
1375bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    sp<AnotherPacketSource> source =
1385bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
1395bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
1405bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    if (source == NULL) {
1415bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        return -EWOULDBLOCK;
1425bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    }
1435bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
1445bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    status_t finalResult;
1455bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    if (!source->hasBufferAvailable(&finalResult)) {
1465bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        return finalResult == OK ? -EWOULDBLOCK : finalResult;
1475bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    }
1485bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
14987f2a558dd12043631e12c361abef301bf603140Andreas Huber    status_t err = source->dequeueAccessUnit(accessUnit);
15087f2a558dd12043631e12c361abef301bf603140Andreas Huber
15187f2a558dd12043631e12c361abef301bf603140Andreas Huber#if !defined(LOG_NDEBUG) || LOG_NDEBUG == 0
15287f2a558dd12043631e12c361abef301bf603140Andreas Huber    if (err == OK) {
15387f2a558dd12043631e12c361abef301bf603140Andreas Huber        int64_t timeUs;
15487f2a558dd12043631e12c361abef301bf603140Andreas Huber        CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
15587f2a558dd12043631e12c361abef301bf603140Andreas Huber        ALOGV("dequeueAccessUnit timeUs=%lld us", timeUs);
15687f2a558dd12043631e12c361abef301bf603140Andreas Huber    }
15787f2a558dd12043631e12c361abef301bf603140Andreas Huber#endif
15887f2a558dd12043631e12c361abef301bf603140Andreas Huber
15987f2a558dd12043631e12c361abef301bf603140Andreas Huber    return err;
1605bc087c573c70c84c6a39946457590b42d392a33Andreas Huber}
1615bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
1625bc087c573c70c84c6a39946457590b42d392a33Andreas Huber}  // namespace android
1635bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
164