HTTPLiveSource.cpp revision 32f3cefa373cd55e63deda36ca9d07c7fe22eaaf
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 "HTTPLiveSource"
19#include <utils/Log.h>
20
21#include "HTTPLiveSource.h"
22
23#include "ATSParser.h"
24#include "AnotherPacketSource.h"
25#include "LiveDataSource.h"
26#include "LiveSession.h"
27
28#include <media/stagefright/foundation/ABuffer.h>
29#include <media/stagefright/foundation/ADebug.h>
30#include <media/stagefright/foundation/AMessage.h>
31#include <media/stagefright/MediaErrors.h>
32#include <media/stagefright/MetaData.h>
33
34namespace android {
35
36NuPlayer::HTTPLiveSource::HTTPLiveSource(const char *url, uint32_t flags)
37    : mURL(url),
38      mFlags(flags),
39      mEOS(false),
40      mOffset(0) {
41}
42
43NuPlayer::HTTPLiveSource::~HTTPLiveSource() {
44    mLiveSession->disconnect();
45    mLiveLooper->stop();
46}
47
48void NuPlayer::HTTPLiveSource::start() {
49    mLiveLooper = new ALooper;
50    mLiveLooper->setName("http live");
51    mLiveLooper->start();
52
53    mLiveSession = new LiveSession(
54            (mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0);
55
56    mLiveLooper->registerHandler(mLiveSession);
57
58    mLiveSession->connect(mURL.c_str());
59
60    mTSParser = new ATSParser;
61}
62
63sp<MetaData> NuPlayer::HTTPLiveSource::getFormat(bool audio) {
64    ATSParser::SourceType type =
65        audio ? ATSParser::MPEG2ADTS_AUDIO : ATSParser::AVC_VIDEO;
66
67    sp<AnotherPacketSource> source =
68        static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
69
70    if (source == NULL) {
71        return NULL;
72    }
73
74    return source->getFormat();
75}
76
77bool NuPlayer::HTTPLiveSource::feedMoreTSData() {
78    if (mEOS) {
79        return false;
80    }
81
82    sp<LiveDataSource> source =
83        static_cast<LiveDataSource *>(mLiveSession->getDataSource().get());
84
85    for (int32_t i = 0; i < 50; ++i) {
86        char buffer[188];
87        ssize_t n = source->readAtNonBlocking(mOffset, buffer, sizeof(buffer));
88
89        if (n == -EWOULDBLOCK) {
90            break;
91        } else if (n < 0) {
92            LOGI("input data EOS reached.");
93            mTSParser->signalEOS(n);
94            mEOS = true;
95            break;
96        } else {
97            if (buffer[0] == 0x00) {
98                // XXX legacy
99                sp<AMessage> extra;
100                mTSParser->signalDiscontinuity(
101                        buffer[1] == 0x00
102                            ? ATSParser::DISCONTINUITY_SEEK
103                            : ATSParser::DISCONTINUITY_FORMATCHANGE,
104                        extra);
105            } else {
106                mTSParser->feedTSPacket(buffer, sizeof(buffer));
107            }
108
109            mOffset += n;
110        }
111    }
112
113    return true;
114}
115
116status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit(
117        bool audio, sp<ABuffer> *accessUnit) {
118    ATSParser::SourceType type =
119        audio ? ATSParser::MPEG2ADTS_AUDIO : ATSParser::AVC_VIDEO;
120
121    sp<AnotherPacketSource> source =
122        static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
123
124    if (source == NULL) {
125        return -EWOULDBLOCK;
126    }
127
128    status_t finalResult;
129    if (!source->hasBufferAvailable(&finalResult)) {
130        return finalResult == OK ? -EWOULDBLOCK : finalResult;
131    }
132
133    return source->dequeueAccessUnit(accessUnit);
134}
135
136status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) {
137    return mLiveSession->getDuration(durationUs);
138}
139
140status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) {
141    // We need to make sure we're not seeking until we have seen the very first
142    // PTS timestamp in the whole stream (from the beginning of the stream).
143    while (!mTSParser->PTSTimeDeltaEstablished() && feedMoreTSData()) {
144        usleep(100000);
145    }
146
147    mLiveSession->seekTo(seekTimeUs);
148
149    return OK;
150}
151
152bool NuPlayer::HTTPLiveSource::isSeekable() {
153    return mLiveSession->isSeekable();
154}
155
156}  // namespace android
157
158