blob: e4145e68cd89ff068baafbd4a809c0344cf2d6b7 [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
Andreas Huber5bc087c2010-12-23 10:27:40 -080023#include "AnotherPacketSource.h"
24#include "LiveDataSource.h"
25#include "LiveSession.h"
26
Andreas Huber1b86fe02014-01-29 11:13:26 -080027#include <media/IMediaHTTPService.h>
Andreas Huber5bc087c2010-12-23 10:27:40 -080028#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 Huber1b86fe02014-01-29 11:13:26 -080038 const sp<IMediaHTTPService> &httpService,
Andreas Huberad0d9c92011-04-19 11:50:27 -070039 const char *url,
Andreas Huber9b80c2b2011-06-30 15:47:02 -070040 const KeyedVector<String8, String8> *headers,
41 bool uidValid, uid_t uid)
Andreas Huberb5f25f02013-02-05 10:14:26 -080042 : Source(notify),
Andreas Huber1b86fe02014-01-29 11:13:26 -080043 mHTTPService(httpService),
Andreas Huberb5f25f02013-02-05 10:14:26 -080044 mURL(url),
Andreas Huber9b80c2b2011-06-30 15:47:02 -070045 mUIDValid(uidValid),
46 mUID(uid),
Andreas Huberad0d9c92011-04-19 11:50:27 -070047 mFlags(0),
Andreas Hubereac68ba2011-09-27 12:12:25 -070048 mFinalResult(OK),
Chong Zhangdcb89b32013-08-06 09:44:47 -070049 mOffset(0),
50 mFetchSubtitleDataGeneration(0) {
Andreas Huberad0d9c92011-04-19 11:50:27 -070051 if (headers) {
52 mExtraHeaders = *headers;
53
54 ssize_t index =
55 mExtraHeaders.indexOfKey(String8("x-hide-urls-from-log"));
56
57 if (index >= 0) {
58 mFlags |= kFlagIncognito;
59
60 mExtraHeaders.removeItemsAt(index);
61 }
62 }
Andreas Huber5bc087c2010-12-23 10:27:40 -080063}
64
65NuPlayer::HTTPLiveSource::~HTTPLiveSource() {
Andreas Huber2048d0c2011-07-15 16:25:41 -070066 if (mLiveSession != NULL) {
67 mLiveSession->disconnect();
Andreas Huber14f76722013-01-15 09:04:18 -080068 mLiveSession.clear();
69
Andreas Huber2048d0c2011-07-15 16:25:41 -070070 mLiveLooper->stop();
Andreas Huber14f76722013-01-15 09:04:18 -080071 mLiveLooper.clear();
Andreas Huber2048d0c2011-07-15 16:25:41 -070072 }
Andreas Huber5bc087c2010-12-23 10:27:40 -080073}
74
Andreas Huber9575c962013-02-05 13:59:56 -080075void NuPlayer::HTTPLiveSource::prepareAsync() {
Andreas Huber5bc087c2010-12-23 10:27:40 -080076 mLiveLooper = new ALooper;
77 mLiveLooper->setName("http live");
78 mLiveLooper->start();
79
Andreas Huber0df36ec2013-02-06 10:44:39 -080080 sp<AMessage> notify = new AMessage(kWhatSessionNotify, id());
81
Andreas Huber7314fa12011-02-24 14:42:48 -080082 mLiveSession = new LiveSession(
Andreas Huber0df36ec2013-02-06 10:44:39 -080083 notify,
Andreas Huber9b80c2b2011-06-30 15:47:02 -070084 (mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0,
Andreas Huber1b86fe02014-01-29 11:13:26 -080085 mHTTPService,
Andreas Huber14f76722013-01-15 09:04:18 -080086 mUIDValid,
87 mUID);
Andreas Huber7314fa12011-02-24 14:42:48 -080088
Andreas Huber5bc087c2010-12-23 10:27:40 -080089 mLiveLooper->registerHandler(mLiveSession);
90
Andreas Huber14f76722013-01-15 09:04:18 -080091 mLiveSession->connectAsync(
Andreas Huberad0d9c92011-04-19 11:50:27 -070092 mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
Andreas Huber9575c962013-02-05 13:59:56 -080093}
94
95void NuPlayer::HTTPLiveSource::start() {
Andreas Huber5bc087c2010-12-23 10:27:40 -080096}
97
Andreas Huber14f76722013-01-15 09:04:18 -080098sp<AMessage> NuPlayer::HTTPLiveSource::getFormat(bool audio) {
99 sp<AMessage> format;
100 status_t err = mLiveSession->getStreamFormat(
101 audio ? LiveSession::STREAMTYPE_AUDIO
102 : LiveSession::STREAMTYPE_VIDEO,
103 &format);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800104
Andreas Huber14f76722013-01-15 09:04:18 -0800105 if (err != OK) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800106 return NULL;
107 }
108
Andreas Huber14f76722013-01-15 09:04:18 -0800109 return format;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800110}
111
Andreas Hubereac68ba2011-09-27 12:12:25 -0700112status_t NuPlayer::HTTPLiveSource::feedMoreTSData() {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700113 return OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800114}
115
116status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit(
117 bool audio, sp<ABuffer> *accessUnit) {
Andreas Huber14f76722013-01-15 09:04:18 -0800118 return mLiveSession->dequeueAccessUnit(
119 audio ? LiveSession::STREAMTYPE_AUDIO
120 : LiveSession::STREAMTYPE_VIDEO,
121 accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800122}
123
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800124status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) {
125 return mLiveSession->getDuration(durationUs);
126}
127
Chong Zhangdcb89b32013-08-06 09:44:47 -0700128status_t NuPlayer::HTTPLiveSource::getTrackInfo(Parcel *reply) const {
129 return mLiveSession->getTrackInfo(reply);
130}
131
132status_t NuPlayer::HTTPLiveSource::selectTrack(size_t trackIndex, bool select) {
133 status_t err = mLiveSession->selectTrack(trackIndex, select);
134
135 if (err == OK) {
136 mFetchSubtitleDataGeneration++;
137 if (select) {
138 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
139 msg->setInt32("generation", mFetchSubtitleDataGeneration);
140 msg->post();
141 }
142 }
143
144 // LiveSession::selectTrack returns BAD_VALUE when selecting the currently
145 // selected track, or unselecting a non-selected track. In this case it's an
146 // no-op so we return OK.
147 return (err == OK || err == BAD_VALUE) ? OK : err;
148}
149
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800150status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) {
Andreas Huber14f76722013-01-15 09:04:18 -0800151 return mLiveSession->seekTo(seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800152}
153
Andreas Huber0df36ec2013-02-06 10:44:39 -0800154void NuPlayer::HTTPLiveSource::onMessageReceived(const sp<AMessage> &msg) {
155 switch (msg->what()) {
156 case kWhatSessionNotify:
157 {
158 onSessionNotify(msg);
159 break;
160 }
161
Chong Zhangdcb89b32013-08-06 09:44:47 -0700162 case kWhatFetchSubtitleData:
163 {
164 int32_t generation;
165 CHECK(msg->findInt32("generation", &generation));
166
167 if (generation != mFetchSubtitleDataGeneration) {
168 // stale
169 break;
170 }
171
172 sp<ABuffer> buffer;
173 if (mLiveSession->dequeueAccessUnit(
174 LiveSession::STREAMTYPE_SUBTITLES, &buffer) == OK) {
175 sp<AMessage> notify = dupNotify();
176 notify->setInt32("what", kWhatSubtitleData);
177 notify->setBuffer("buffer", buffer);
178 notify->post();
179
180 int64_t timeUs, baseUs, durationUs, delayUs;
181 CHECK(buffer->meta()->findInt64("baseUs", &baseUs));
182 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
183 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
184 delayUs = baseUs + timeUs - ALooper::GetNowUs();
185
186 msg->post(delayUs > 0ll ? delayUs : 0ll);
187 } else {
188 // try again in 1 second
189 msg->post(1000000ll);
190 }
191
192 break;
193 }
194
Andreas Huber0df36ec2013-02-06 10:44:39 -0800195 default:
196 Source::onMessageReceived(msg);
197 break;
198 }
199}
200
201void NuPlayer::HTTPLiveSource::onSessionNotify(const sp<AMessage> &msg) {
202 int32_t what;
203 CHECK(msg->findInt32("what", &what));
204
205 switch (what) {
206 case LiveSession::kWhatPrepared:
207 {
Marco Nelissen3e518fd2013-11-01 10:33:18 -0700208 // notify the current size here if we have it, otherwise report an initial size of (0,0)
209 sp<AMessage> format = getFormat(false /* audio */);
210 int32_t width;
211 int32_t height;
212 if (format != NULL &&
213 format->findInt32("width", &width) && format->findInt32("height", &height)) {
214 notifyVideoSizeChanged(width, height);
215 } else {
216 notifyVideoSizeChanged(0, 0);
217 }
Andreas Huber0df36ec2013-02-06 10:44:39 -0800218
219 uint32_t flags = FLAG_CAN_PAUSE;
220 if (mLiveSession->isSeekable()) {
221 flags |= FLAG_CAN_SEEK;
222 flags |= FLAG_CAN_SEEK_BACKWARD;
223 flags |= FLAG_CAN_SEEK_FORWARD;
224 }
225
226 if (mLiveSession->hasDynamicDuration()) {
227 flags |= FLAG_DYNAMIC_DURATION;
228 }
229
230 notifyFlagsChanged(flags);
231
232 notifyPrepared();
233 break;
234 }
235
236 case LiveSession::kWhatPreparationFailed:
237 {
238 status_t err;
239 CHECK(msg->findInt32("err", &err));
240
241 notifyPrepared(err);
242 break;
243 }
244
Andreas Huber14f76722013-01-15 09:04:18 -0800245 case LiveSession::kWhatStreamsChanged:
246 {
247 uint32_t changedMask;
248 CHECK(msg->findInt32(
249 "changedMask", (int32_t *)&changedMask));
250
251 bool audio = changedMask & LiveSession::STREAMTYPE_AUDIO;
252 bool video = changedMask & LiveSession::STREAMTYPE_VIDEO;
253
254 sp<AMessage> reply;
255 CHECK(msg->findMessage("reply", &reply));
256
257 sp<AMessage> notify = dupNotify();
258 notify->setInt32("what", kWhatQueueDecoderShutdown);
259 notify->setInt32("audio", audio);
260 notify->setInt32("video", video);
261 notify->setMessage("reply", reply);
262 notify->post();
263 break;
264 }
265
266 case LiveSession::kWhatError:
267 {
268 break;
269 }
270
Andreas Huber0df36ec2013-02-06 10:44:39 -0800271 default:
272 TRESPASS();
273 }
274}
275
Andreas Huber5bc087c2010-12-23 10:27:40 -0800276} // namespace android
277