blob: 0251baf5f6c13a4bc4139524c0cc1ace9140a25e [file] [log] [blame]
Andreas Huber5bc087c2010-12-23 10:27:40 -08001/*
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
Andreas Huberad0d9c92011-04-19 11:50:27 -070036NuPlayer::HTTPLiveSource::HTTPLiveSource(
37 const char *url,
Andreas Huber9b80c2b2011-06-30 15:47:02 -070038 const KeyedVector<String8, String8> *headers,
39 bool uidValid, uid_t uid)
Andreas Huber5bc087c2010-12-23 10:27:40 -080040 : mURL(url),
Andreas Huber9b80c2b2011-06-30 15:47:02 -070041 mUIDValid(uidValid),
42 mUID(uid),
Andreas Huberad0d9c92011-04-19 11:50:27 -070043 mFlags(0),
Andreas Huber5bc087c2010-12-23 10:27:40 -080044 mEOS(false),
45 mOffset(0) {
Andreas Huberad0d9c92011-04-19 11:50:27 -070046 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 }
Andreas Huber5bc087c2010-12-23 10:27:40 -080058}
59
60NuPlayer::HTTPLiveSource::~HTTPLiveSource() {
Andreas Huber2048d0c2011-07-15 16:25:41 -070061 if (mLiveSession != NULL) {
62 mLiveSession->disconnect();
63 mLiveLooper->stop();
64 }
Andreas Huber5bc087c2010-12-23 10:27:40 -080065}
66
67void NuPlayer::HTTPLiveSource::start() {
68 mLiveLooper = new ALooper;
69 mLiveLooper->setName("http live");
70 mLiveLooper->start();
71
Andreas Huber7314fa12011-02-24 14:42:48 -080072 mLiveSession = new LiveSession(
Andreas Huber9b80c2b2011-06-30 15:47:02 -070073 (mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0,
74 mUIDValid, mUID);
Andreas Huber7314fa12011-02-24 14:42:48 -080075
Andreas Huber5bc087c2010-12-23 10:27:40 -080076 mLiveLooper->registerHandler(mLiveSession);
77
Andreas Huberad0d9c92011-04-19 11:50:27 -070078 mLiveSession->connect(
79 mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
Andreas Huber5bc087c2010-12-23 10:27:40 -080080
81 mTSParser = new ATSParser;
82}
83
84sp<MetaData> NuPlayer::HTTPLiveSource::getFormat(bool audio) {
85 ATSParser::SourceType type =
Andreas Huber386d6092011-05-19 08:37:39 -070086 audio ? ATSParser::AUDIO : ATSParser::VIDEO;
Andreas Huber5bc087c2010-12-23 10:27:40 -080087
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
98bool NuPlayer::HTTPLiveSource::feedMoreTSData() {
99 if (mEOS) {
100 return false;
101 }
102
103 sp<LiveDataSource> source =
104 static_cast<LiveDataSource *>(mLiveSession->getDataSource().get());
105
Andreas Huber22fc52f2011-01-05 16:24:27 -0800106 for (int32_t i = 0; i < 50; ++i) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800107 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 LOGI("input data EOS reached.");
Andreas Huber1aef2112011-01-04 14:01:29 -0800114 mTSParser->signalEOS(n);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800115 mEOS = true;
116 break;
117 } else {
118 if (buffer[0] == 0x00) {
119 // XXX legacy
Andreas Huber32f3cef2011-03-02 15:34:46 -0800120 sp<AMessage> extra;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800121 mTSParser->signalDiscontinuity(
122 buffer[1] == 0x00
123 ? ATSParser::DISCONTINUITY_SEEK
Andreas Huber32f3cef2011-03-02 15:34:46 -0800124 : ATSParser::DISCONTINUITY_FORMATCHANGE,
125 extra);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800126 } else {
127 mTSParser->feedTSPacket(buffer, sizeof(buffer));
128 }
129
130 mOffset += n;
131 }
132 }
133
134 return true;
135}
136
137status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit(
138 bool audio, sp<ABuffer> *accessUnit) {
139 ATSParser::SourceType type =
Andreas Huber386d6092011-05-19 08:37:39 -0700140 audio ? ATSParser::AUDIO : ATSParser::VIDEO;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800141
142 sp<AnotherPacketSource> source =
143 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
144
145 if (source == NULL) {
146 return -EWOULDBLOCK;
147 }
148
149 status_t finalResult;
150 if (!source->hasBufferAvailable(&finalResult)) {
151 return finalResult == OK ? -EWOULDBLOCK : finalResult;
152 }
153
154 return source->dequeueAccessUnit(accessUnit);
155}
156
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800157status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) {
158 return mLiveSession->getDuration(durationUs);
159}
160
161status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) {
162 // We need to make sure we're not seeking until we have seen the very first
163 // PTS timestamp in the whole stream (from the beginning of the stream).
164 while (!mTSParser->PTSTimeDeltaEstablished() && feedMoreTSData()) {
165 usleep(100000);
166 }
167
168 mLiveSession->seekTo(seekTimeUs);
169
170 return OK;
171}
172
173bool NuPlayer::HTTPLiveSource::isSeekable() {
174 return mLiveSession->isSeekable();
175}
176
Andreas Huber5bc087c2010-12-23 10:27:40 -0800177} // namespace android
178