HTTPLiveSource.cpp revision 386d609dc513e838c7e7c4c46c604493ccd560be
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 "HTTPLiveSource"
195bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include <utils/Log.h>
205bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
215bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include "HTTPLiveSource.h"
225bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
235bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include "ATSParser.h"
245bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include "AnotherPacketSource.h"
255bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include "LiveDataSource.h"
265bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include "LiveSession.h"
275bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
285bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include <media/stagefright/foundation/ABuffer.h>
295bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include <media/stagefright/foundation/ADebug.h>
305bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include <media/stagefright/foundation/AMessage.h>
315bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include <media/stagefright/MediaErrors.h>
325bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include <media/stagefright/MetaData.h>
335bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
345bc087c573c70c84c6a39946457590b42d392a33Andreas Hubernamespace android {
355bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
36ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas HuberNuPlayer::HTTPLiveSource::HTTPLiveSource(
37ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber        const char *url,
38ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber        const KeyedVector<String8, String8> *headers)
395bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    : mURL(url),
40ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber      mFlags(0),
415bc087c573c70c84c6a39946457590b42d392a33Andreas Huber      mEOS(false),
425bc087c573c70c84c6a39946457590b42d392a33Andreas Huber      mOffset(0) {
43ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber    if (headers) {
44ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber        mExtraHeaders = *headers;
45ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber
46ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber        ssize_t index =
47ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber            mExtraHeaders.indexOfKey(String8("x-hide-urls-from-log"));
48ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber
49ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber        if (index >= 0) {
50ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber            mFlags |= kFlagIncognito;
51ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber
52ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber            mExtraHeaders.removeItemsAt(index);
53ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber        }
54ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber    }
555bc087c573c70c84c6a39946457590b42d392a33Andreas Huber}
565bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
575bc087c573c70c84c6a39946457590b42d392a33Andreas HuberNuPlayer::HTTPLiveSource::~HTTPLiveSource() {
585bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    mLiveSession->disconnect();
595bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    mLiveLooper->stop();
605bc087c573c70c84c6a39946457590b42d392a33Andreas Huber}
615bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
625bc087c573c70c84c6a39946457590b42d392a33Andreas Hubervoid NuPlayer::HTTPLiveSource::start() {
635bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    mLiveLooper = new ALooper;
645bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    mLiveLooper->setName("http live");
655bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    mLiveLooper->start();
665bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
677314fa17093d514199fedcb55ac41136a1b31cb3Andreas Huber    mLiveSession = new LiveSession(
687314fa17093d514199fedcb55ac41136a1b31cb3Andreas Huber            (mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0);
697314fa17093d514199fedcb55ac41136a1b31cb3Andreas Huber
705bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    mLiveLooper->registerHandler(mLiveSession);
715bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
72ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber    mLiveSession->connect(
73ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber            mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
745bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
755bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    mTSParser = new ATSParser;
765bc087c573c70c84c6a39946457590b42d392a33Andreas Huber}
775bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
785bc087c573c70c84c6a39946457590b42d392a33Andreas Hubersp<MetaData> NuPlayer::HTTPLiveSource::getFormat(bool audio) {
795bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    ATSParser::SourceType type =
80386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        audio ? ATSParser::AUDIO : ATSParser::VIDEO;
815bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
825bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    sp<AnotherPacketSource> source =
835bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
845bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
855bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    if (source == NULL) {
865bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        return NULL;
875bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    }
885bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
895bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    return source->getFormat();
905bc087c573c70c84c6a39946457590b42d392a33Andreas Huber}
915bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
925bc087c573c70c84c6a39946457590b42d392a33Andreas Huberbool NuPlayer::HTTPLiveSource::feedMoreTSData() {
935bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    if (mEOS) {
945bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        return false;
955bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    }
965bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
975bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    sp<LiveDataSource> source =
985bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        static_cast<LiveDataSource *>(mLiveSession->getDataSource().get());
995bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
10022fc52f6f72f39e33c3970d0291de3569118aa5cAndreas Huber    for (int32_t i = 0; i < 50; ++i) {
1015bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        char buffer[188];
1025bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        ssize_t n = source->readAtNonBlocking(mOffset, buffer, sizeof(buffer));
1035bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
1045bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        if (n == -EWOULDBLOCK) {
1055bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            break;
1065bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        } else if (n < 0) {
1075bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            LOGI("input data EOS reached.");
1081aef211b4e5dc952081727bfd2318b2cb5ca4506Andreas Huber            mTSParser->signalEOS(n);
1095bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            mEOS = true;
1105bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            break;
1115bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        } else {
1125bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            if (buffer[0] == 0x00) {
1135bc087c573c70c84c6a39946457590b42d392a33Andreas Huber                // XXX legacy
11432f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber                sp<AMessage> extra;
1155bc087c573c70c84c6a39946457590b42d392a33Andreas Huber                mTSParser->signalDiscontinuity(
1165bc087c573c70c84c6a39946457590b42d392a33Andreas Huber                        buffer[1] == 0x00
1175bc087c573c70c84c6a39946457590b42d392a33Andreas Huber                            ? ATSParser::DISCONTINUITY_SEEK
11832f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber                            : ATSParser::DISCONTINUITY_FORMATCHANGE,
11932f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber                        extra);
1205bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            } else {
1215bc087c573c70c84c6a39946457590b42d392a33Andreas Huber                mTSParser->feedTSPacket(buffer, sizeof(buffer));
1225bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            }
1235bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
1245bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            mOffset += n;
1255bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        }
1265bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    }
1275bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
1285bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    return true;
1295bc087c573c70c84c6a39946457590b42d392a33Andreas Huber}
1305bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
1315bc087c573c70c84c6a39946457590b42d392a33Andreas Huberstatus_t NuPlayer::HTTPLiveSource::dequeueAccessUnit(
1325bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        bool audio, sp<ABuffer> *accessUnit) {
1335bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    ATSParser::SourceType type =
134386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber        audio ? ATSParser::AUDIO : ATSParser::VIDEO;
1355bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
1365bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    sp<AnotherPacketSource> source =
1375bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
1385bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
1395bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    if (source == NULL) {
1405bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        return -EWOULDBLOCK;
1415bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    }
1425bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
1435bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    status_t finalResult;
1445bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    if (!source->hasBufferAvailable(&finalResult)) {
1455bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        return finalResult == OK ? -EWOULDBLOCK : finalResult;
1465bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    }
1475bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
1485bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    return source->dequeueAccessUnit(accessUnit);
1495bc087c573c70c84c6a39946457590b42d392a33Andreas Huber}
1505bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
15143c3e6ce02215ca99d506458f596cb1211639f29Andreas Huberstatus_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) {
15243c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber    return mLiveSession->getDuration(durationUs);
15343c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber}
15443c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber
15543c3e6ce02215ca99d506458f596cb1211639f29Andreas Huberstatus_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) {
15643c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber    // We need to make sure we're not seeking until we have seen the very first
15743c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber    // PTS timestamp in the whole stream (from the beginning of the stream).
15843c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber    while (!mTSParser->PTSTimeDeltaEstablished() && feedMoreTSData()) {
15943c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber        usleep(100000);
16043c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber    }
16143c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber
16243c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber    mLiveSession->seekTo(seekTimeUs);
16343c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber
16443c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber    return OK;
16543c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber}
16643c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber
16743c3e6ce02215ca99d506458f596cb1211639f29Andreas Huberbool NuPlayer::HTTPLiveSource::isSeekable() {
16843c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber    return mLiveSession->isSeekable();
16943c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber}
17043c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber
1715bc087c573c70c84c6a39946457590b42d392a33Andreas Huber}  // namespace android
1725bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
173