blob: ae67906d50415d859b017b1955c2de8d5c47f79d [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 Huber7314fa12011-02-24 14:42:48 -080074 mLiveSession = new LiveSession(
Andreas Huber9b80c2b2011-06-30 15:47:02 -070075 (mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0,
76 mUIDValid, mUID);
Andreas Huber7314fa12011-02-24 14:42:48 -080077
Andreas Huber5bc087c2010-12-23 10:27:40 -080078 mLiveLooper->registerHandler(mLiveSession);
79
Andreas Huberad0d9c92011-04-19 11:50:27 -070080 mLiveSession->connect(
81 mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
Andreas Huber5bc087c2010-12-23 10:27:40 -080082
83 mTSParser = new ATSParser;
Andreas Huber9575c962013-02-05 13:59:56 -080084
85 notifyVideoSizeChanged(0, 0);
86
87 uint32_t flags = FLAG_CAN_PAUSE;
88 if (mLiveSession->isSeekable()) {
89 flags |= FLAG_CAN_SEEK;
90 flags |= FLAG_CAN_SEEK_BACKWARD;
91 flags |= FLAG_CAN_SEEK_FORWARD;
92 }
93
94 if (mLiveSession->hasDynamicDuration()) {
95 flags |= FLAG_DYNAMIC_DURATION;
96 }
97
98 notifyFlagsChanged(flags);
99
100 notifyPrepared();
101}
102
103void NuPlayer::HTTPLiveSource::start() {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800104}
105
Andreas Huber84066782011-08-16 09:34:26 -0700106sp<MetaData> NuPlayer::HTTPLiveSource::getFormatMeta(bool audio) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800107 ATSParser::SourceType type =
Andreas Huber386d6092011-05-19 08:37:39 -0700108 audio ? ATSParser::AUDIO : ATSParser::VIDEO;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800109
110 sp<AnotherPacketSource> source =
111 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
112
113 if (source == NULL) {
114 return NULL;
115 }
116
117 return source->getFormat();
118}
119
Andreas Hubereac68ba2011-09-27 12:12:25 -0700120status_t NuPlayer::HTTPLiveSource::feedMoreTSData() {
121 if (mFinalResult != OK) {
122 return mFinalResult;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800123 }
124
125 sp<LiveDataSource> source =
126 static_cast<LiveDataSource *>(mLiveSession->getDataSource().get());
127
Andreas Huber22fc52f2011-01-05 16:24:27 -0800128 for (int32_t i = 0; i < 50; ++i) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800129 char buffer[188];
130 ssize_t n = source->readAtNonBlocking(mOffset, buffer, sizeof(buffer));
131
132 if (n == -EWOULDBLOCK) {
133 break;
134 } else if (n < 0) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700135 if (n != ERROR_END_OF_STREAM) {
Steve Blockdf64d152012-01-04 20:05:49 +0000136 ALOGI("input data EOS reached, error %ld", n);
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700137 } else {
Steve Blockdf64d152012-01-04 20:05:49 +0000138 ALOGI("input data EOS reached.");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700139 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800140 mTSParser->signalEOS(n);
Andreas Hubereac68ba2011-09-27 12:12:25 -0700141 mFinalResult = n;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800142 break;
143 } else {
144 if (buffer[0] == 0x00) {
145 // XXX legacy
Andreas Huberb7c8e912012-11-27 15:02:53 -0800146
147 uint8_t type = buffer[1];
148
149 sp<AMessage> extra = new AMessage;
150
151 if (type & 2) {
152 int64_t mediaTimeUs;
153 memcpy(&mediaTimeUs, &buffer[2], sizeof(mediaTimeUs));
154
155 extra->setInt64(IStreamListener::kKeyMediaTimeUs, mediaTimeUs);
156 }
157
Andreas Huber5bc087c2010-12-23 10:27:40 -0800158 mTSParser->signalDiscontinuity(
Andreas Huberb7c8e912012-11-27 15:02:53 -0800159 ((type & 1) == 0)
Andreas Huber5bc087c2010-12-23 10:27:40 -0800160 ? ATSParser::DISCONTINUITY_SEEK
Andreas Huber32f3cef2011-03-02 15:34:46 -0800161 : ATSParser::DISCONTINUITY_FORMATCHANGE,
162 extra);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800163 } else {
Andreas Huber06528d72011-08-31 16:29:05 -0700164 status_t err = mTSParser->feedTSPacket(buffer, sizeof(buffer));
165
166 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000167 ALOGE("TS Parser returned error %d", err);
Andreas Huber06528d72011-08-31 16:29:05 -0700168 mTSParser->signalEOS(err);
Andreas Hubereac68ba2011-09-27 12:12:25 -0700169 mFinalResult = err;
Andreas Huber06528d72011-08-31 16:29:05 -0700170 break;
171 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800172 }
173
174 mOffset += n;
175 }
176 }
177
Andreas Hubereac68ba2011-09-27 12:12:25 -0700178 return OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800179}
180
181status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit(
182 bool audio, sp<ABuffer> *accessUnit) {
183 ATSParser::SourceType type =
Andreas Huber386d6092011-05-19 08:37:39 -0700184 audio ? ATSParser::AUDIO : ATSParser::VIDEO;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800185
186 sp<AnotherPacketSource> source =
187 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
188
189 if (source == NULL) {
190 return -EWOULDBLOCK;
191 }
192
193 status_t finalResult;
194 if (!source->hasBufferAvailable(&finalResult)) {
195 return finalResult == OK ? -EWOULDBLOCK : finalResult;
196 }
197
198 return source->dequeueAccessUnit(accessUnit);
199}
200
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800201status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) {
202 return mLiveSession->getDuration(durationUs);
203}
204
205status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) {
206 // We need to make sure we're not seeking until we have seen the very first
207 // PTS timestamp in the whole stream (from the beginning of the stream).
Andreas Hubereac68ba2011-09-27 12:12:25 -0700208 while (!mTSParser->PTSTimeDeltaEstablished() && feedMoreTSData() == OK) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800209 usleep(100000);
210 }
211
212 mLiveSession->seekTo(seekTimeUs);
213
214 return OK;
215}
216
Andreas Huber5bc087c2010-12-23 10:27:40 -0800217} // namespace android
218