blob: 655ee559c0616d39bffb6d2f7fd026b017ddfd58 [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(
Andreas Huberb5f25f02013-02-05 10:14:26 -080037 const sp<AMessage> &notify,
Andreas Huberad0d9c92011-04-19 11:50:27 -070038 const char *url,
Andreas Huber9b80c2b2011-06-30 15:47:02 -070039 const KeyedVector<String8, String8> *headers,
40 bool uidValid, uid_t uid)
Andreas Huberb5f25f02013-02-05 10:14:26 -080041 : Source(notify),
42 mURL(url),
Andreas Huber9b80c2b2011-06-30 15:47:02 -070043 mUIDValid(uidValid),
44 mUID(uid),
Andreas Huberad0d9c92011-04-19 11:50:27 -070045 mFlags(0),
Andreas Hubereac68ba2011-09-27 12:12:25 -070046 mFinalResult(OK),
Andreas Huber5bc087c2010-12-23 10:27:40 -080047 mOffset(0) {
Andreas Huberad0d9c92011-04-19 11:50:27 -070048 if (headers) {
49 mExtraHeaders = *headers;
50
51 ssize_t index =
52 mExtraHeaders.indexOfKey(String8("x-hide-urls-from-log"));
53
54 if (index >= 0) {
55 mFlags |= kFlagIncognito;
56
57 mExtraHeaders.removeItemsAt(index);
58 }
59 }
Andreas Huber5bc087c2010-12-23 10:27:40 -080060}
61
62NuPlayer::HTTPLiveSource::~HTTPLiveSource() {
Andreas Huber2048d0c2011-07-15 16:25:41 -070063 if (mLiveSession != NULL) {
64 mLiveSession->disconnect();
65 mLiveLooper->stop();
66 }
Andreas Huber5bc087c2010-12-23 10:27:40 -080067}
68
Andreas Huber9575c962013-02-05 13:59:56 -080069void NuPlayer::HTTPLiveSource::prepareAsync() {
Andreas Huber5bc087c2010-12-23 10:27:40 -080070 mLiveLooper = new ALooper;
71 mLiveLooper->setName("http live");
72 mLiveLooper->start();
73
Andreas Huber0df36ec2013-02-06 10:44:39 -080074 sp<AMessage> notify = new AMessage(kWhatSessionNotify, id());
75
Andreas Huber7314fa12011-02-24 14:42:48 -080076 mLiveSession = new LiveSession(
Andreas Huber0df36ec2013-02-06 10:44:39 -080077 notify,
Andreas Huber9b80c2b2011-06-30 15:47:02 -070078 (mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0,
79 mUIDValid, mUID);
Andreas Huber7314fa12011-02-24 14:42:48 -080080
Andreas Huber5bc087c2010-12-23 10:27:40 -080081 mLiveLooper->registerHandler(mLiveSession);
82
Andreas Huberad0d9c92011-04-19 11:50:27 -070083 mLiveSession->connect(
84 mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
Andreas Huber5bc087c2010-12-23 10:27:40 -080085
86 mTSParser = new ATSParser;
Andreas Huber9575c962013-02-05 13:59:56 -080087}
88
89void NuPlayer::HTTPLiveSource::start() {
Andreas Huber5bc087c2010-12-23 10:27:40 -080090}
91
Andreas Huber84066782011-08-16 09:34:26 -070092sp<MetaData> NuPlayer::HTTPLiveSource::getFormatMeta(bool audio) {
Andreas Huber5bc087c2010-12-23 10:27:40 -080093 ATSParser::SourceType type =
Andreas Huber386d6092011-05-19 08:37:39 -070094 audio ? ATSParser::AUDIO : ATSParser::VIDEO;
Andreas Huber5bc087c2010-12-23 10:27:40 -080095
96 sp<AnotherPacketSource> source =
97 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
98
99 if (source == NULL) {
100 return NULL;
101 }
102
103 return source->getFormat();
104}
105
Andreas Hubereac68ba2011-09-27 12:12:25 -0700106status_t NuPlayer::HTTPLiveSource::feedMoreTSData() {
107 if (mFinalResult != OK) {
108 return mFinalResult;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800109 }
110
111 sp<LiveDataSource> source =
112 static_cast<LiveDataSource *>(mLiveSession->getDataSource().get());
113
Andreas Huber22fc52f2011-01-05 16:24:27 -0800114 for (int32_t i = 0; i < 50; ++i) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800115 char buffer[188];
116 ssize_t n = source->readAtNonBlocking(mOffset, buffer, sizeof(buffer));
117
118 if (n == -EWOULDBLOCK) {
119 break;
120 } else if (n < 0) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700121 if (n != ERROR_END_OF_STREAM) {
Steve Blockdf64d152012-01-04 20:05:49 +0000122 ALOGI("input data EOS reached, error %ld", n);
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700123 } else {
Steve Blockdf64d152012-01-04 20:05:49 +0000124 ALOGI("input data EOS reached.");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700125 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800126 mTSParser->signalEOS(n);
Andreas Hubereac68ba2011-09-27 12:12:25 -0700127 mFinalResult = n;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800128 break;
129 } else {
130 if (buffer[0] == 0x00) {
131 // XXX legacy
Andreas Huberb7c8e912012-11-27 15:02:53 -0800132
133 uint8_t type = buffer[1];
134
135 sp<AMessage> extra = new AMessage;
136
137 if (type & 2) {
138 int64_t mediaTimeUs;
139 memcpy(&mediaTimeUs, &buffer[2], sizeof(mediaTimeUs));
140
141 extra->setInt64(IStreamListener::kKeyMediaTimeUs, mediaTimeUs);
142 }
143
Andreas Huber5bc087c2010-12-23 10:27:40 -0800144 mTSParser->signalDiscontinuity(
Andreas Huberb7c8e912012-11-27 15:02:53 -0800145 ((type & 1) == 0)
Andreas Huber5bc087c2010-12-23 10:27:40 -0800146 ? ATSParser::DISCONTINUITY_SEEK
Andreas Huber32f3cef2011-03-02 15:34:46 -0800147 : ATSParser::DISCONTINUITY_FORMATCHANGE,
148 extra);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800149 } else {
Andreas Huber06528d72011-08-31 16:29:05 -0700150 status_t err = mTSParser->feedTSPacket(buffer, sizeof(buffer));
151
152 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000153 ALOGE("TS Parser returned error %d", err);
Andreas Huber06528d72011-08-31 16:29:05 -0700154 mTSParser->signalEOS(err);
Andreas Hubereac68ba2011-09-27 12:12:25 -0700155 mFinalResult = err;
Andreas Huber06528d72011-08-31 16:29:05 -0700156 break;
157 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800158 }
159
160 mOffset += n;
161 }
162 }
163
Andreas Hubereac68ba2011-09-27 12:12:25 -0700164 return OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800165}
166
167status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit(
168 bool audio, sp<ABuffer> *accessUnit) {
169 ATSParser::SourceType type =
Andreas Huber386d6092011-05-19 08:37:39 -0700170 audio ? ATSParser::AUDIO : ATSParser::VIDEO;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800171
172 sp<AnotherPacketSource> source =
173 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
174
175 if (source == NULL) {
176 return -EWOULDBLOCK;
177 }
178
179 status_t finalResult;
180 if (!source->hasBufferAvailable(&finalResult)) {
181 return finalResult == OK ? -EWOULDBLOCK : finalResult;
182 }
183
184 return source->dequeueAccessUnit(accessUnit);
185}
186
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800187status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) {
188 return mLiveSession->getDuration(durationUs);
189}
190
191status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) {
192 // We need to make sure we're not seeking until we have seen the very first
193 // PTS timestamp in the whole stream (from the beginning of the stream).
Andreas Hubereac68ba2011-09-27 12:12:25 -0700194 while (!mTSParser->PTSTimeDeltaEstablished() && feedMoreTSData() == OK) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800195 usleep(100000);
196 }
197
198 mLiveSession->seekTo(seekTimeUs);
199
200 return OK;
201}
202
Andreas Huber0df36ec2013-02-06 10:44:39 -0800203void NuPlayer::HTTPLiveSource::onMessageReceived(const sp<AMessage> &msg) {
204 switch (msg->what()) {
205 case kWhatSessionNotify:
206 {
207 onSessionNotify(msg);
208 break;
209 }
210
211 default:
212 Source::onMessageReceived(msg);
213 break;
214 }
215}
216
217void NuPlayer::HTTPLiveSource::onSessionNotify(const sp<AMessage> &msg) {
218 int32_t what;
219 CHECK(msg->findInt32("what", &what));
220
221 switch (what) {
222 case LiveSession::kWhatPrepared:
223 {
224 notifyVideoSizeChanged(0, 0);
225
226 uint32_t flags = FLAG_CAN_PAUSE;
227 if (mLiveSession->isSeekable()) {
228 flags |= FLAG_CAN_SEEK;
229 flags |= FLAG_CAN_SEEK_BACKWARD;
230 flags |= FLAG_CAN_SEEK_FORWARD;
231 }
232
233 if (mLiveSession->hasDynamicDuration()) {
234 flags |= FLAG_DYNAMIC_DURATION;
235 }
236
237 notifyFlagsChanged(flags);
238
239 notifyPrepared();
240 break;
241 }
242
243 case LiveSession::kWhatPreparationFailed:
244 {
245 status_t err;
246 CHECK(msg->findInt32("err", &err));
247
248 notifyPrepared(err);
249 break;
250 }
251
252 default:
253 TRESPASS();
254 }
255}
256
Andreas Huber5bc087c2010-12-23 10:27:40 -0800257} // namespace android
258