blob: d38ee62800fc3eb5ad174affea4dca4b1d8421d1 [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
69void NuPlayer::HTTPLiveSource::start() {
70 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;
84}
85
Andreas Huber84066782011-08-16 09:34:26 -070086sp<MetaData> NuPlayer::HTTPLiveSource::getFormatMeta(bool audio) {
Andreas Huber5bc087c2010-12-23 10:27:40 -080087 ATSParser::SourceType type =
Andreas Huber386d6092011-05-19 08:37:39 -070088 audio ? ATSParser::AUDIO : ATSParser::VIDEO;
Andreas Huber5bc087c2010-12-23 10:27:40 -080089
90 sp<AnotherPacketSource> source =
91 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
92
93 if (source == NULL) {
94 return NULL;
95 }
96
97 return source->getFormat();
98}
99
Andreas Hubereac68ba2011-09-27 12:12:25 -0700100status_t NuPlayer::HTTPLiveSource::feedMoreTSData() {
101 if (mFinalResult != OK) {
102 return mFinalResult;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800103 }
104
105 sp<LiveDataSource> source =
106 static_cast<LiveDataSource *>(mLiveSession->getDataSource().get());
107
Andreas Huber22fc52f2011-01-05 16:24:27 -0800108 for (int32_t i = 0; i < 50; ++i) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800109 char buffer[188];
110 ssize_t n = source->readAtNonBlocking(mOffset, buffer, sizeof(buffer));
111
112 if (n == -EWOULDBLOCK) {
113 break;
114 } else if (n < 0) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700115 if (n != ERROR_END_OF_STREAM) {
Steve Blockdf64d152012-01-04 20:05:49 +0000116 ALOGI("input data EOS reached, error %ld", n);
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700117 } else {
Steve Blockdf64d152012-01-04 20:05:49 +0000118 ALOGI("input data EOS reached.");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700119 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800120 mTSParser->signalEOS(n);
Andreas Hubereac68ba2011-09-27 12:12:25 -0700121 mFinalResult = n;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800122 break;
123 } else {
124 if (buffer[0] == 0x00) {
125 // XXX legacy
Andreas Huberb7c8e912012-11-27 15:02:53 -0800126
127 uint8_t type = buffer[1];
128
129 sp<AMessage> extra = new AMessage;
130
131 if (type & 2) {
132 int64_t mediaTimeUs;
133 memcpy(&mediaTimeUs, &buffer[2], sizeof(mediaTimeUs));
134
135 extra->setInt64(IStreamListener::kKeyMediaTimeUs, mediaTimeUs);
136 }
137
Andreas Huber5bc087c2010-12-23 10:27:40 -0800138 mTSParser->signalDiscontinuity(
Andreas Huberb7c8e912012-11-27 15:02:53 -0800139 ((type & 1) == 0)
Andreas Huber5bc087c2010-12-23 10:27:40 -0800140 ? ATSParser::DISCONTINUITY_SEEK
Andreas Huber32f3cef2011-03-02 15:34:46 -0800141 : ATSParser::DISCONTINUITY_FORMATCHANGE,
142 extra);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800143 } else {
Andreas Huber06528d72011-08-31 16:29:05 -0700144 status_t err = mTSParser->feedTSPacket(buffer, sizeof(buffer));
145
146 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000147 ALOGE("TS Parser returned error %d", err);
Andreas Huber06528d72011-08-31 16:29:05 -0700148 mTSParser->signalEOS(err);
Andreas Hubereac68ba2011-09-27 12:12:25 -0700149 mFinalResult = err;
Andreas Huber06528d72011-08-31 16:29:05 -0700150 break;
151 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800152 }
153
154 mOffset += n;
155 }
156 }
157
Andreas Hubereac68ba2011-09-27 12:12:25 -0700158 return OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800159}
160
161status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit(
162 bool audio, sp<ABuffer> *accessUnit) {
163 ATSParser::SourceType type =
Andreas Huber386d6092011-05-19 08:37:39 -0700164 audio ? ATSParser::AUDIO : ATSParser::VIDEO;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800165
166 sp<AnotherPacketSource> source =
167 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
168
169 if (source == NULL) {
170 return -EWOULDBLOCK;
171 }
172
173 status_t finalResult;
174 if (!source->hasBufferAvailable(&finalResult)) {
175 return finalResult == OK ? -EWOULDBLOCK : finalResult;
176 }
177
178 return source->dequeueAccessUnit(accessUnit);
179}
180
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800181status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) {
182 return mLiveSession->getDuration(durationUs);
183}
184
185status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) {
186 // We need to make sure we're not seeking until we have seen the very first
187 // PTS timestamp in the whole stream (from the beginning of the stream).
Andreas Hubereac68ba2011-09-27 12:12:25 -0700188 while (!mTSParser->PTSTimeDeltaEstablished() && feedMoreTSData() == OK) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800189 usleep(100000);
190 }
191
192 mLiveSession->seekTo(seekTimeUs);
193
194 return OK;
195}
196
Andreas Huberb7c8e912012-11-27 15:02:53 -0800197uint32_t NuPlayer::HTTPLiveSource::flags() const {
198 uint32_t flags = 0;
199 if (mLiveSession->isSeekable()) {
200 flags |= FLAG_SEEKABLE;
201 }
202
203 if (mLiveSession->hasDynamicDuration()) {
204 flags |= FLAG_DYNAMIC_DURATION;
205 }
206
207 return flags;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800208}
209
Andreas Huber5bc087c2010-12-23 10:27:40 -0800210} // namespace android
211