blob: 126625aba7a75567c555099eff9a7810f867338a [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"
Andreas Huber5bc087c2010-12-23 10:27:40 -080025
Andreas Huber1b86fe02014-01-29 11:13:26 -080026#include <media/IMediaHTTPService.h>
Andreas Huber5bc087c2010-12-23 10:27:40 -080027#include <media/stagefright/foundation/ABuffer.h>
28#include <media/stagefright/foundation/ADebug.h>
29#include <media/stagefright/foundation/AMessage.h>
30#include <media/stagefright/MediaErrors.h>
31#include <media/stagefright/MetaData.h>
Robert Shih08528432015-04-08 09:06:54 -070032#include <media/stagefright/MediaDefs.h>
Andreas Huber5bc087c2010-12-23 10:27:40 -080033
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 Huber81e68442014-02-05 11:52:33 -080040 const KeyedVector<String8, String8> *headers)
Andreas Huberb5f25f02013-02-05 10:14:26 -080041 : Source(notify),
Andreas Huber1b86fe02014-01-29 11:13:26 -080042 mHTTPService(httpService),
Andreas Huberb5f25f02013-02-05 10:14:26 -080043 mURL(url),
Andreas Huberad0d9c92011-04-19 11:50:27 -070044 mFlags(0),
Andreas Hubereac68ba2011-09-27 12:12:25 -070045 mFinalResult(OK),
Chong Zhangdcb89b32013-08-06 09:44:47 -070046 mOffset(0),
Robert Shih08528432015-04-08 09:06:54 -070047 mFetchSubtitleDataGeneration(0),
48 mFetchMetaDataGeneration(0),
49 mHasMetadata(false),
50 mMetadataSelected(false) {
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
Chong Zhang1228d6b2014-08-12 21:25:48 -070069 mLiveLooper->unregisterHandler(mLiveSession->id());
70 mLiveLooper->unregisterHandler(id());
Andreas Huber2048d0c2011-07-15 16:25:41 -070071 mLiveLooper->stop();
Chong Zhang1228d6b2014-08-12 21:25:48 -070072
73 mLiveSession.clear();
Andreas Huber14f76722013-01-15 09:04:18 -080074 mLiveLooper.clear();
Andreas Huber2048d0c2011-07-15 16:25:41 -070075 }
Andreas Huber5bc087c2010-12-23 10:27:40 -080076}
77
Andreas Huber9575c962013-02-05 13:59:56 -080078void NuPlayer::HTTPLiveSource::prepareAsync() {
Chong Zhang1228d6b2014-08-12 21:25:48 -070079 if (mLiveLooper == NULL) {
80 mLiveLooper = new ALooper;
81 mLiveLooper->setName("http live");
82 mLiveLooper->start();
83
84 mLiveLooper->registerHandler(this);
85 }
Andreas Huber5bc087c2010-12-23 10:27:40 -080086
Lajos Molnar1d15ab52015-03-04 16:46:34 -080087 sp<AMessage> notify = new AMessage(kWhatSessionNotify, this);
Andreas Huber0df36ec2013-02-06 10:44:39 -080088
Andreas Huber7314fa12011-02-24 14:42:48 -080089 mLiveSession = new LiveSession(
Andreas Huber0df36ec2013-02-06 10:44:39 -080090 notify,
Andreas Huber9b80c2b2011-06-30 15:47:02 -070091 (mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0,
Andreas Huber81e68442014-02-05 11:52:33 -080092 mHTTPService);
Andreas Huber7314fa12011-02-24 14:42:48 -080093
Andreas Huber5bc087c2010-12-23 10:27:40 -080094 mLiveLooper->registerHandler(mLiveSession);
95
Andreas Huber14f76722013-01-15 09:04:18 -080096 mLiveSession->connectAsync(
Andreas Huberad0d9c92011-04-19 11:50:27 -070097 mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
Andreas Huber9575c962013-02-05 13:59:56 -080098}
99
100void NuPlayer::HTTPLiveSource::start() {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800101}
102
Andreas Huber14f76722013-01-15 09:04:18 -0800103sp<AMessage> NuPlayer::HTTPLiveSource::getFormat(bool audio) {
Robert Shih1098d872014-11-06 13:58:48 -0800104 if (mLiveSession == NULL) {
105 return NULL;
106 }
107
Andreas Huber14f76722013-01-15 09:04:18 -0800108 sp<AMessage> format;
109 status_t err = mLiveSession->getStreamFormat(
110 audio ? LiveSession::STREAMTYPE_AUDIO
111 : LiveSession::STREAMTYPE_VIDEO,
112 &format);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800113
Andreas Huber14f76722013-01-15 09:04:18 -0800114 if (err != OK) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800115 return NULL;
116 }
117
Andreas Huber14f76722013-01-15 09:04:18 -0800118 return format;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800119}
120
Andreas Hubereac68ba2011-09-27 12:12:25 -0700121status_t NuPlayer::HTTPLiveSource::feedMoreTSData() {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700122 return OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800123}
124
125status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit(
126 bool audio, sp<ABuffer> *accessUnit) {
Andreas Huber14f76722013-01-15 09:04:18 -0800127 return mLiveSession->dequeueAccessUnit(
128 audio ? LiveSession::STREAMTYPE_AUDIO
129 : LiveSession::STREAMTYPE_VIDEO,
130 accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800131}
132
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800133status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) {
134 return mLiveSession->getDuration(durationUs);
135}
136
Chong Zhang404fced2014-06-11 14:45:31 -0700137size_t NuPlayer::HTTPLiveSource::getTrackCount() const {
138 return mLiveSession->getTrackCount();
139}
140
141sp<AMessage> NuPlayer::HTTPLiveSource::getTrackInfo(size_t trackIndex) const {
142 return mLiveSession->getTrackInfo(trackIndex);
Chong Zhangdcb89b32013-08-06 09:44:47 -0700143}
144
Robert Shih89bf2522014-07-29 19:25:10 -0700145ssize_t NuPlayer::HTTPLiveSource::getSelectedTrack(media_track_type type) const {
146 if (mLiveSession == NULL) {
147 return -1;
Robert Shih08528432015-04-08 09:06:54 -0700148 } else if (type == MEDIA_TRACK_TYPE_METADATA) {
149 // MEDIA_TRACK_TYPE_METADATA is always last track
150 // mMetadataSelected can only be true when mHasMetadata is true
151 return mMetadataSelected ? (mLiveSession->getTrackCount() - 1) : -1;
Robert Shih89bf2522014-07-29 19:25:10 -0700152 } else {
153 return mLiveSession->getSelectedTrack(type);
154 }
155}
156
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700157status_t NuPlayer::HTTPLiveSource::selectTrack(size_t trackIndex, bool select, int64_t /*timeUs*/) {
Robert Shih08528432015-04-08 09:06:54 -0700158 if (mLiveSession == NULL) {
159 return INVALID_OPERATION;
160 }
161
162 status_t err = INVALID_OPERATION;
163 bool postFetchMsg = false, isSub = false;
Robert Shih055404e2015-05-15 10:12:21 -0700164 if (!mHasMetadata || trackIndex != mLiveSession->getTrackCount() - 1) {
Robert Shih08528432015-04-08 09:06:54 -0700165 err = mLiveSession->selectTrack(trackIndex, select);
166 postFetchMsg = select;
167 isSub = true;
168 } else {
Robert Shih055404e2015-05-15 10:12:21 -0700169 // metadata track; i.e. (mHasMetadata && trackIndex == mLiveSession->getTrackCount() - 1)
170 if (mMetadataSelected && !select) {
171 err = OK;
172 } else if (!mMetadataSelected && select) {
173 postFetchMsg = true;
174 err = OK;
175 } else {
176 err = BAD_VALUE; // behave as LiveSession::selectTrack
Robert Shih08528432015-04-08 09:06:54 -0700177 }
Robert Shih055404e2015-05-15 10:12:21 -0700178
179 mMetadataSelected = select;
Robert Shih08528432015-04-08 09:06:54 -0700180 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700181
182 if (err == OK) {
Robert Shih08528432015-04-08 09:06:54 -0700183 int32_t &generation = isSub ? mFetchSubtitleDataGeneration : mFetchMetaDataGeneration;
184 generation++;
185 if (postFetchMsg) {
186 int32_t what = isSub ? kWhatFetchSubtitleData : kWhatFetchMetaData;
187 sp<AMessage> msg = new AMessage(what, this);
188 msg->setInt32("generation", generation);
Chong Zhangdcb89b32013-08-06 09:44:47 -0700189 msg->post();
190 }
191 }
192
193 // LiveSession::selectTrack returns BAD_VALUE when selecting the currently
194 // selected track, or unselecting a non-selected track. In this case it's an
195 // no-op so we return OK.
Andreas Huber84333e02014-02-07 15:36:10 -0800196 return (err == OK || err == BAD_VALUE) ? (status_t)OK : err;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700197}
198
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800199status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) {
Andreas Huber14f76722013-01-15 09:04:18 -0800200 return mLiveSession->seekTo(seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800201}
202
Robert Shih08528432015-04-08 09:06:54 -0700203void NuPlayer::HTTPLiveSource::pollForRawData(
204 const sp<AMessage> &msg, int32_t currentGeneration,
205 LiveSession::StreamType fetchType, int32_t pushWhat) {
206
207 int32_t generation;
208 CHECK(msg->findInt32("generation", &generation));
209
210 if (generation != currentGeneration) {
211 return;
212 }
213
214 sp<ABuffer> buffer;
215 while (mLiveSession->dequeueAccessUnit(fetchType, &buffer) == OK) {
216
217 sp<AMessage> notify = dupNotify();
218 notify->setInt32("what", pushWhat);
219 notify->setBuffer("buffer", buffer);
220
221 int64_t timeUs, baseUs, delayUs;
222 CHECK(buffer->meta()->findInt64("baseUs", &baseUs));
223 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
224 delayUs = baseUs + timeUs - ALooper::GetNowUs();
225
226 if (fetchType == LiveSession::STREAMTYPE_SUBTITLES) {
227 notify->post();
228 msg->post(delayUs > 0ll ? delayUs : 0ll);
229 return;
230 } else if (fetchType == LiveSession::STREAMTYPE_METADATA) {
231 if (delayUs < -1000000ll) { // 1 second
232 continue;
233 }
234 notify->post();
235 // push all currently available metadata buffers in each invocation of pollForRawData
236 // continue;
237 } else {
238 TRESPASS();
239 }
240 }
241
242 // try again in 1 second
243 msg->post(1000000ll);
244}
245
Andreas Huber0df36ec2013-02-06 10:44:39 -0800246void NuPlayer::HTTPLiveSource::onMessageReceived(const sp<AMessage> &msg) {
247 switch (msg->what()) {
248 case kWhatSessionNotify:
249 {
250 onSessionNotify(msg);
251 break;
252 }
253
Chong Zhangdcb89b32013-08-06 09:44:47 -0700254 case kWhatFetchSubtitleData:
255 {
Robert Shih08528432015-04-08 09:06:54 -0700256 pollForRawData(
257 msg, mFetchSubtitleDataGeneration,
258 /* fetch */ LiveSession::STREAMTYPE_SUBTITLES,
259 /* push */ kWhatSubtitleData);
Chong Zhangdcb89b32013-08-06 09:44:47 -0700260
Robert Shih08528432015-04-08 09:06:54 -0700261 break;
262 }
263
264 case kWhatFetchMetaData:
265 {
266 if (!mMetadataSelected) {
Chong Zhangdcb89b32013-08-06 09:44:47 -0700267 break;
268 }
269
Robert Shih08528432015-04-08 09:06:54 -0700270 pollForRawData(
271 msg, mFetchMetaDataGeneration,
272 /* fetch */ LiveSession::STREAMTYPE_METADATA,
273 /* push */ kWhatTimedMetaData);
Chong Zhangdcb89b32013-08-06 09:44:47 -0700274
275 break;
276 }
277
Andreas Huber0df36ec2013-02-06 10:44:39 -0800278 default:
279 Source::onMessageReceived(msg);
280 break;
281 }
282}
283
284void NuPlayer::HTTPLiveSource::onSessionNotify(const sp<AMessage> &msg) {
285 int32_t what;
286 CHECK(msg->findInt32("what", &what));
287
288 switch (what) {
289 case LiveSession::kWhatPrepared:
290 {
Marco Nelissen3e518fd2013-11-01 10:33:18 -0700291 // notify the current size here if we have it, otherwise report an initial size of (0,0)
292 sp<AMessage> format = getFormat(false /* audio */);
293 int32_t width;
294 int32_t height;
295 if (format != NULL &&
296 format->findInt32("width", &width) && format->findInt32("height", &height)) {
Chong Zhangced1c2f2014-08-08 15:22:35 -0700297 notifyVideoSizeChanged(format);
Marco Nelissen3e518fd2013-11-01 10:33:18 -0700298 } else {
Chong Zhangced1c2f2014-08-08 15:22:35 -0700299 notifyVideoSizeChanged();
Marco Nelissen3e518fd2013-11-01 10:33:18 -0700300 }
Andreas Huber0df36ec2013-02-06 10:44:39 -0800301
302 uint32_t flags = FLAG_CAN_PAUSE;
303 if (mLiveSession->isSeekable()) {
304 flags |= FLAG_CAN_SEEK;
305 flags |= FLAG_CAN_SEEK_BACKWARD;
306 flags |= FLAG_CAN_SEEK_FORWARD;
307 }
308
309 if (mLiveSession->hasDynamicDuration()) {
310 flags |= FLAG_DYNAMIC_DURATION;
311 }
312
313 notifyFlagsChanged(flags);
314
315 notifyPrepared();
316 break;
317 }
318
319 case LiveSession::kWhatPreparationFailed:
320 {
321 status_t err;
322 CHECK(msg->findInt32("err", &err));
323
324 notifyPrepared(err);
325 break;
326 }
327
Andreas Huber14f76722013-01-15 09:04:18 -0800328 case LiveSession::kWhatStreamsChanged:
329 {
330 uint32_t changedMask;
331 CHECK(msg->findInt32(
332 "changedMask", (int32_t *)&changedMask));
333
334 bool audio = changedMask & LiveSession::STREAMTYPE_AUDIO;
335 bool video = changedMask & LiveSession::STREAMTYPE_VIDEO;
336
337 sp<AMessage> reply;
338 CHECK(msg->findMessage("reply", &reply));
339
340 sp<AMessage> notify = dupNotify();
341 notify->setInt32("what", kWhatQueueDecoderShutdown);
342 notify->setInt32("audio", audio);
343 notify->setInt32("video", video);
344 notify->setMessage("reply", reply);
345 notify->post();
346 break;
347 }
348
Chong Zhang7c870802015-03-17 16:27:56 -0700349 case LiveSession::kWhatBufferingStart:
350 {
351 sp<AMessage> notify = dupNotify();
352 notify->setInt32("what", kWhatPauseOnBufferingStart);
353 notify->post();
354 break;
355 }
356
357 case LiveSession::kWhatBufferingEnd:
358 {
359 sp<AMessage> notify = dupNotify();
360 notify->setInt32("what", kWhatResumeOnBufferingEnd);
361 notify->post();
362 break;
363 }
364
365
366 case LiveSession::kWhatBufferingUpdate:
367 {
368 sp<AMessage> notify = dupNotify();
369 int32_t percentage;
370 CHECK(msg->findInt32("percentage", &percentage));
371 notify->setInt32("what", kWhatBufferingUpdate);
372 notify->setInt32("percentage", percentage);
373 notify->post();
374 break;
375 }
376
Robert Shih08528432015-04-08 09:06:54 -0700377 case LiveSession::kWhatMetadataDetected:
378 {
379 if (!mHasMetadata) {
380 mHasMetadata = true;
381
382 sp<AMessage> notify = dupNotify();
383 // notification without buffer triggers MEDIA_INFO_METADATA_UPDATE
384 notify->setInt32("what", kWhatTimedMetaData);
385 notify->post();
386 }
387 break;
388 }
389
Andreas Huber14f76722013-01-15 09:04:18 -0800390 case LiveSession::kWhatError:
391 {
392 break;
393 }
394
Andreas Huber0df36ec2013-02-06 10:44:39 -0800395 default:
396 TRESPASS();
397 }
398}
399
Andreas Huber5bc087c2010-12-23 10:27:40 -0800400} // namespace android
401