blob: 5dcca122a46a3adcf3943bda09d6b603a117878a [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 Hubereac68ba2011-09-27 12:12:25 -070044 mFinalResult(OK),
Andreas Huber5bc087c2010-12-23 10:27:40 -080045 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
Andreas Huber84066782011-08-16 09:34:26 -070084sp<MetaData> NuPlayer::HTTPLiveSource::getFormatMeta(bool audio) {
Andreas Huber5bc087c2010-12-23 10:27:40 -080085 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
Andreas Hubereac68ba2011-09-27 12:12:25 -070098status_t NuPlayer::HTTPLiveSource::feedMoreTSData() {
99 if (mFinalResult != OK) {
100 return mFinalResult;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800101 }
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) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700113 if (n != ERROR_END_OF_STREAM) {
Steve Blockdf64d152012-01-04 20:05:49 +0000114 ALOGI("input data EOS reached, error %ld", n);
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700115 } else {
Steve Blockdf64d152012-01-04 20:05:49 +0000116 ALOGI("input data EOS reached.");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700117 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800118 mTSParser->signalEOS(n);
Andreas Hubereac68ba2011-09-27 12:12:25 -0700119 mFinalResult = n;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800120 break;
121 } else {
122 if (buffer[0] == 0x00) {
123 // XXX legacy
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800124
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
Andreas Huber5bc087c2010-12-23 10:27:40 -0800136 mTSParser->signalDiscontinuity(
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800137 ((type & 1) == 0)
Andreas Huber5bc087c2010-12-23 10:27:40 -0800138 ? ATSParser::DISCONTINUITY_SEEK
Andreas Huber32f3cef2011-03-02 15:34:46 -0800139 : ATSParser::DISCONTINUITY_FORMATCHANGE,
140 extra);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800141 } else {
Andreas Huber06528d72011-08-31 16:29:05 -0700142 status_t err = mTSParser->feedTSPacket(buffer, sizeof(buffer));
143
144 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000145 ALOGE("TS Parser returned error %d", err);
Andreas Huber06528d72011-08-31 16:29:05 -0700146 mTSParser->signalEOS(err);
Andreas Hubereac68ba2011-09-27 12:12:25 -0700147 mFinalResult = err;
Andreas Huber06528d72011-08-31 16:29:05 -0700148 break;
149 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800150 }
151
152 mOffset += n;
153 }
154 }
155
Andreas Hubereac68ba2011-09-27 12:12:25 -0700156 return OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800157}
158
159status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit(
160 bool audio, sp<ABuffer> *accessUnit) {
161 ATSParser::SourceType type =
Andreas Huber386d6092011-05-19 08:37:39 -0700162 audio ? ATSParser::AUDIO : ATSParser::VIDEO;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800163
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
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800179status_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).
Andreas Hubereac68ba2011-09-27 12:12:25 -0700186 while (!mTSParser->PTSTimeDeltaEstablished() && feedMoreTSData() == OK) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800187 usleep(100000);
188 }
189
190 mLiveSession->seekTo(seekTimeUs);
191
192 return OK;
193}
194
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800195uint32_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;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800206}
207
Andreas Huber5bc087c2010-12-23 10:27:40 -0800208} // namespace android
209