HTTPLiveSource.cpp revision b7c8e91880463ff4981e3e53e98e45d68e2fe374
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(
37        const char *url,
38        const KeyedVector<String8, String8> *headers,
39        bool uidValid, uid_t uid)
40    : mURL(url),
41      mUIDValid(uidValid),
42      mUID(uid),
43      mFlags(0),
44      mFinalResult(OK),
45      mOffset(0) {
46    if (headers) {
47        mExtraHeaders = *headers;
48
49        ssize_t index =
50            mExtraHeaders.indexOfKey(String8("x-hide-urls-from-log"));
51
52        if (index >= 0) {
53            mFlags |= kFlagIncognito;
54
55            mExtraHeaders.removeItemsAt(index);
56        }
57    }
58}
59
60NuPlayer::HTTPLiveSource::~HTTPLiveSource() {
61    if (mLiveSession != NULL) {
62        mLiveSession->disconnect();
63        mLiveLooper->stop();
64    }
65}
66
67void NuPlayer::HTTPLiveSource::start() {
68    mLiveLooper = new ALooper;
69    mLiveLooper->setName("http live");
70    mLiveLooper->start();
71
72    mLiveSession = new LiveSession(
73            (mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0,
74            mUIDValid, mUID);
75
76    mLiveLooper->registerHandler(mLiveSession);
77
78    mLiveSession->connect(
79            mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
80
81    mTSParser = new ATSParser;
82}
83
84sp<MetaData> NuPlayer::HTTPLiveSource::getFormatMeta(bool audio) {
85    ATSParser::SourceType type =
86        audio ? ATSParser::AUDIO : ATSParser::VIDEO;
87
88    sp<AnotherPacketSource> source =
89        static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
90
91    if (source == NULL) {
92        return NULL;
93    }
94
95    return source->getFormat();
96}
97
98status_t NuPlayer::HTTPLiveSource::feedMoreTSData() {
99    if (mFinalResult != OK) {
100        return mFinalResult;
101    }
102
103    sp<LiveDataSource> source =
104        static_cast<LiveDataSource *>(mLiveSession->getDataSource().get());
105
106    for (int32_t i = 0; i < 50; ++i) {
107        char buffer[188];
108        ssize_t n = source->readAtNonBlocking(mOffset, buffer, sizeof(buffer));
109
110        if (n == -EWOULDBLOCK) {
111            break;
112        } else if (n < 0) {
113            if (n != ERROR_END_OF_STREAM) {
114                ALOGI("input data EOS reached, error %ld", n);
115            } else {
116                ALOGI("input data EOS reached.");
117            }
118            mTSParser->signalEOS(n);
119            mFinalResult = n;
120            break;
121        } else {
122            if (buffer[0] == 0x00) {
123                // XXX legacy
124
125                uint8_t type = buffer[1];
126
127                sp<AMessage> extra = new AMessage;
128
129                if (type & 2) {
130                    int64_t mediaTimeUs;
131                    memcpy(&mediaTimeUs, &buffer[2], sizeof(mediaTimeUs));
132
133                    extra->setInt64(IStreamListener::kKeyMediaTimeUs, mediaTimeUs);
134                }
135
136                mTSParser->signalDiscontinuity(
137                        ((type & 1) == 0)
138                            ? ATSParser::DISCONTINUITY_SEEK
139                            : ATSParser::DISCONTINUITY_FORMATCHANGE,
140                        extra);
141            } else {
142                status_t err = mTSParser->feedTSPacket(buffer, sizeof(buffer));
143
144                if (err != OK) {
145                    ALOGE("TS Parser returned error %d", err);
146                    mTSParser->signalEOS(err);
147                    mFinalResult = err;
148                    break;
149                }
150            }
151
152            mOffset += n;
153        }
154    }
155
156    return OK;
157}
158
159status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit(
160        bool audio, sp<ABuffer> *accessUnit) {
161    ATSParser::SourceType type =
162        audio ? ATSParser::AUDIO : ATSParser::VIDEO;
163
164    sp<AnotherPacketSource> source =
165        static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
166
167    if (source == NULL) {
168        return -EWOULDBLOCK;
169    }
170
171    status_t finalResult;
172    if (!source->hasBufferAvailable(&finalResult)) {
173        return finalResult == OK ? -EWOULDBLOCK : finalResult;
174    }
175
176    return source->dequeueAccessUnit(accessUnit);
177}
178
179status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) {
180    return mLiveSession->getDuration(durationUs);
181}
182
183status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) {
184    // We need to make sure we're not seeking until we have seen the very first
185    // PTS timestamp in the whole stream (from the beginning of the stream).
186    while (!mTSParser->PTSTimeDeltaEstablished() && feedMoreTSData() == OK) {
187        usleep(100000);
188    }
189
190    mLiveSession->seekTo(seekTimeUs);
191
192    return OK;
193}
194
195uint32_t NuPlayer::HTTPLiveSource::flags() const {
196    uint32_t flags = 0;
197    if (mLiveSession->isSeekable()) {
198        flags |= FLAG_SEEKABLE;
199    }
200
201    if (mLiveSession->hasDynamicDuration()) {
202        flags |= FLAG_DYNAMIC_DURATION;
203    }
204
205    return flags;
206}
207
208}  // namespace android
209
210