blob: 65c158d0c220b53ad165a1f1ad633cbd402498f7 [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 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),
47 mFetchSubtitleDataGeneration(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();
Andreas Huber14f76722013-01-15 09:04:18 -080065
Chong Zhang1228d6b2014-08-12 21:25:48 -070066 mLiveLooper->unregisterHandler(mLiveSession->id());
67 mLiveLooper->unregisterHandler(id());
Andreas Huber2048d0c2011-07-15 16:25:41 -070068 mLiveLooper->stop();
Chong Zhang1228d6b2014-08-12 21:25:48 -070069
70 mLiveSession.clear();
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() {
Chong Zhang1228d6b2014-08-12 21:25:48 -070076 if (mLiveLooper == NULL) {
77 mLiveLooper = new ALooper;
78 mLiveLooper->setName("http live");
79 mLiveLooper->start();
80
81 mLiveLooper->registerHandler(this);
82 }
Andreas Huber5bc087c2010-12-23 10:27:40 -080083
Andreas Huber0df36ec2013-02-06 10:44:39 -080084 sp<AMessage> notify = new AMessage(kWhatSessionNotify, id());
85
Andreas Huber7314fa12011-02-24 14:42:48 -080086 mLiveSession = new LiveSession(
Andreas Huber0df36ec2013-02-06 10:44:39 -080087 notify,
Andreas Huber9b80c2b2011-06-30 15:47:02 -070088 (mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0,
Andreas Huber81e68442014-02-05 11:52:33 -080089 mHTTPService);
Andreas Huber7314fa12011-02-24 14:42:48 -080090
Andreas Huber5bc087c2010-12-23 10:27:40 -080091 mLiveLooper->registerHandler(mLiveSession);
92
Andreas Huber14f76722013-01-15 09:04:18 -080093 mLiveSession->connectAsync(
Andreas Huberad0d9c92011-04-19 11:50:27 -070094 mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
Andreas Huber9575c962013-02-05 13:59:56 -080095}
96
97void NuPlayer::HTTPLiveSource::start() {
Andreas Huber5bc087c2010-12-23 10:27:40 -080098}
99
Andreas Huber14f76722013-01-15 09:04:18 -0800100sp<AMessage> NuPlayer::HTTPLiveSource::getFormat(bool audio) {
101 sp<AMessage> format;
102 status_t err = mLiveSession->getStreamFormat(
103 audio ? LiveSession::STREAMTYPE_AUDIO
104 : LiveSession::STREAMTYPE_VIDEO,
105 &format);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800106
Andreas Huber14f76722013-01-15 09:04:18 -0800107 if (err != OK) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800108 return NULL;
109 }
110
Andreas Huber14f76722013-01-15 09:04:18 -0800111 return format;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800112}
113
Andreas Hubereac68ba2011-09-27 12:12:25 -0700114status_t NuPlayer::HTTPLiveSource::feedMoreTSData() {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700115 return OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800116}
117
118status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit(
119 bool audio, sp<ABuffer> *accessUnit) {
Andreas Huber14f76722013-01-15 09:04:18 -0800120 return mLiveSession->dequeueAccessUnit(
121 audio ? LiveSession::STREAMTYPE_AUDIO
122 : LiveSession::STREAMTYPE_VIDEO,
123 accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800124}
125
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800126status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) {
127 return mLiveSession->getDuration(durationUs);
128}
129
Chong Zhang404fced2014-06-11 14:45:31 -0700130size_t NuPlayer::HTTPLiveSource::getTrackCount() const {
131 return mLiveSession->getTrackCount();
132}
133
134sp<AMessage> NuPlayer::HTTPLiveSource::getTrackInfo(size_t trackIndex) const {
135 return mLiveSession->getTrackInfo(trackIndex);
Chong Zhangdcb89b32013-08-06 09:44:47 -0700136}
137
Robert Shih89bf2522014-07-29 19:25:10 -0700138ssize_t NuPlayer::HTTPLiveSource::getSelectedTrack(media_track_type type) const {
139 if (mLiveSession == NULL) {
140 return -1;
141 } else {
142 return mLiveSession->getSelectedTrack(type);
143 }
144}
145
Chong Zhangdcb89b32013-08-06 09:44:47 -0700146status_t NuPlayer::HTTPLiveSource::selectTrack(size_t trackIndex, bool select) {
147 status_t err = mLiveSession->selectTrack(trackIndex, select);
148
149 if (err == OK) {
150 mFetchSubtitleDataGeneration++;
151 if (select) {
152 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
153 msg->setInt32("generation", mFetchSubtitleDataGeneration);
154 msg->post();
155 }
156 }
157
158 // LiveSession::selectTrack returns BAD_VALUE when selecting the currently
159 // selected track, or unselecting a non-selected track. In this case it's an
160 // no-op so we return OK.
Andreas Huber84333e02014-02-07 15:36:10 -0800161 return (err == OK || err == BAD_VALUE) ? (status_t)OK : err;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700162}
163
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800164status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) {
Andreas Huber14f76722013-01-15 09:04:18 -0800165 return mLiveSession->seekTo(seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800166}
167
Andreas Huber0df36ec2013-02-06 10:44:39 -0800168void NuPlayer::HTTPLiveSource::onMessageReceived(const sp<AMessage> &msg) {
169 switch (msg->what()) {
170 case kWhatSessionNotify:
171 {
172 onSessionNotify(msg);
173 break;
174 }
175
Chong Zhangdcb89b32013-08-06 09:44:47 -0700176 case kWhatFetchSubtitleData:
177 {
178 int32_t generation;
179 CHECK(msg->findInt32("generation", &generation));
180
181 if (generation != mFetchSubtitleDataGeneration) {
182 // stale
183 break;
184 }
185
186 sp<ABuffer> buffer;
187 if (mLiveSession->dequeueAccessUnit(
188 LiveSession::STREAMTYPE_SUBTITLES, &buffer) == OK) {
189 sp<AMessage> notify = dupNotify();
190 notify->setInt32("what", kWhatSubtitleData);
191 notify->setBuffer("buffer", buffer);
192 notify->post();
193
194 int64_t timeUs, baseUs, durationUs, delayUs;
195 CHECK(buffer->meta()->findInt64("baseUs", &baseUs));
196 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
197 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
198 delayUs = baseUs + timeUs - ALooper::GetNowUs();
199
200 msg->post(delayUs > 0ll ? delayUs : 0ll);
201 } else {
202 // try again in 1 second
203 msg->post(1000000ll);
204 }
205
206 break;
207 }
208
Andreas Huber0df36ec2013-02-06 10:44:39 -0800209 default:
210 Source::onMessageReceived(msg);
211 break;
212 }
213}
214
215void NuPlayer::HTTPLiveSource::onSessionNotify(const sp<AMessage> &msg) {
216 int32_t what;
217 CHECK(msg->findInt32("what", &what));
218
219 switch (what) {
220 case LiveSession::kWhatPrepared:
221 {
Marco Nelissen3e518fd2013-11-01 10:33:18 -0700222 // notify the current size here if we have it, otherwise report an initial size of (0,0)
223 sp<AMessage> format = getFormat(false /* audio */);
224 int32_t width;
225 int32_t height;
226 if (format != NULL &&
227 format->findInt32("width", &width) && format->findInt32("height", &height)) {
Chong Zhangced1c2f2014-08-08 15:22:35 -0700228 notifyVideoSizeChanged(format);
Marco Nelissen3e518fd2013-11-01 10:33:18 -0700229 } else {
Chong Zhangced1c2f2014-08-08 15:22:35 -0700230 notifyVideoSizeChanged();
Marco Nelissen3e518fd2013-11-01 10:33:18 -0700231 }
Andreas Huber0df36ec2013-02-06 10:44:39 -0800232
233 uint32_t flags = FLAG_CAN_PAUSE;
234 if (mLiveSession->isSeekable()) {
235 flags |= FLAG_CAN_SEEK;
236 flags |= FLAG_CAN_SEEK_BACKWARD;
237 flags |= FLAG_CAN_SEEK_FORWARD;
238 }
239
240 if (mLiveSession->hasDynamicDuration()) {
241 flags |= FLAG_DYNAMIC_DURATION;
242 }
243
244 notifyFlagsChanged(flags);
245
246 notifyPrepared();
247 break;
248 }
249
250 case LiveSession::kWhatPreparationFailed:
251 {
252 status_t err;
253 CHECK(msg->findInt32("err", &err));
254
255 notifyPrepared(err);
256 break;
257 }
258
Andreas Huber14f76722013-01-15 09:04:18 -0800259 case LiveSession::kWhatStreamsChanged:
260 {
261 uint32_t changedMask;
262 CHECK(msg->findInt32(
263 "changedMask", (int32_t *)&changedMask));
264
265 bool audio = changedMask & LiveSession::STREAMTYPE_AUDIO;
266 bool video = changedMask & LiveSession::STREAMTYPE_VIDEO;
267
268 sp<AMessage> reply;
269 CHECK(msg->findMessage("reply", &reply));
270
271 sp<AMessage> notify = dupNotify();
272 notify->setInt32("what", kWhatQueueDecoderShutdown);
273 notify->setInt32("audio", audio);
274 notify->setInt32("video", video);
275 notify->setMessage("reply", reply);
276 notify->post();
277 break;
278 }
279
280 case LiveSession::kWhatError:
281 {
282 break;
283 }
284
Andreas Huber0df36ec2013-02-06 10:44:39 -0800285 default:
286 TRESPASS();
287 }
288}
289
Andreas Huber5bc087c2010-12-23 10:27:40 -0800290} // namespace android
291