blob: a003c81372d9cf35591f1ae57c32339c676b0c21 [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
138status_t NuPlayer::HTTPLiveSource::selectTrack(size_t trackIndex, bool select) {
139 status_t err = mLiveSession->selectTrack(trackIndex, select);
140
141 if (err == OK) {
142 mFetchSubtitleDataGeneration++;
143 if (select) {
144 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
145 msg->setInt32("generation", mFetchSubtitleDataGeneration);
146 msg->post();
147 }
148 }
149
150 // LiveSession::selectTrack returns BAD_VALUE when selecting the currently
151 // selected track, or unselecting a non-selected track. In this case it's an
152 // no-op so we return OK.
Andreas Huber84333e02014-02-07 15:36:10 -0800153 return (err == OK || err == BAD_VALUE) ? (status_t)OK : err;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700154}
155
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800156status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) {
Andreas Huber14f76722013-01-15 09:04:18 -0800157 return mLiveSession->seekTo(seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800158}
159
Andreas Huber0df36ec2013-02-06 10:44:39 -0800160void NuPlayer::HTTPLiveSource::onMessageReceived(const sp<AMessage> &msg) {
161 switch (msg->what()) {
162 case kWhatSessionNotify:
163 {
164 onSessionNotify(msg);
165 break;
166 }
167
Chong Zhangdcb89b32013-08-06 09:44:47 -0700168 case kWhatFetchSubtitleData:
169 {
170 int32_t generation;
171 CHECK(msg->findInt32("generation", &generation));
172
173 if (generation != mFetchSubtitleDataGeneration) {
174 // stale
175 break;
176 }
177
178 sp<ABuffer> buffer;
179 if (mLiveSession->dequeueAccessUnit(
180 LiveSession::STREAMTYPE_SUBTITLES, &buffer) == OK) {
181 sp<AMessage> notify = dupNotify();
182 notify->setInt32("what", kWhatSubtitleData);
183 notify->setBuffer("buffer", buffer);
184 notify->post();
185
186 int64_t timeUs, baseUs, durationUs, delayUs;
187 CHECK(buffer->meta()->findInt64("baseUs", &baseUs));
188 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
189 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
190 delayUs = baseUs + timeUs - ALooper::GetNowUs();
191
192 msg->post(delayUs > 0ll ? delayUs : 0ll);
193 } else {
194 // try again in 1 second
195 msg->post(1000000ll);
196 }
197
198 break;
199 }
200
Andreas Huber0df36ec2013-02-06 10:44:39 -0800201 default:
202 Source::onMessageReceived(msg);
203 break;
204 }
205}
206
207void NuPlayer::HTTPLiveSource::onSessionNotify(const sp<AMessage> &msg) {
208 int32_t what;
209 CHECK(msg->findInt32("what", &what));
210
211 switch (what) {
212 case LiveSession::kWhatPrepared:
213 {
Marco Nelissen3e518fd2013-11-01 10:33:18 -0700214 // notify the current size here if we have it, otherwise report an initial size of (0,0)
215 sp<AMessage> format = getFormat(false /* audio */);
216 int32_t width;
217 int32_t height;
218 if (format != NULL &&
219 format->findInt32("width", &width) && format->findInt32("height", &height)) {
Chong Zhangced1c2f2014-08-08 15:22:35 -0700220 notifyVideoSizeChanged(format);
Marco Nelissen3e518fd2013-11-01 10:33:18 -0700221 } else {
Chong Zhangced1c2f2014-08-08 15:22:35 -0700222 notifyVideoSizeChanged();
Marco Nelissen3e518fd2013-11-01 10:33:18 -0700223 }
Andreas Huber0df36ec2013-02-06 10:44:39 -0800224
225 uint32_t flags = FLAG_CAN_PAUSE;
226 if (mLiveSession->isSeekable()) {
227 flags |= FLAG_CAN_SEEK;
228 flags |= FLAG_CAN_SEEK_BACKWARD;
229 flags |= FLAG_CAN_SEEK_FORWARD;
230 }
231
232 if (mLiveSession->hasDynamicDuration()) {
233 flags |= FLAG_DYNAMIC_DURATION;
234 }
235
236 notifyFlagsChanged(flags);
237
238 notifyPrepared();
239 break;
240 }
241
242 case LiveSession::kWhatPreparationFailed:
243 {
244 status_t err;
245 CHECK(msg->findInt32("err", &err));
246
247 notifyPrepared(err);
248 break;
249 }
250
Andreas Huber14f76722013-01-15 09:04:18 -0800251 case LiveSession::kWhatStreamsChanged:
252 {
253 uint32_t changedMask;
254 CHECK(msg->findInt32(
255 "changedMask", (int32_t *)&changedMask));
256
257 bool audio = changedMask & LiveSession::STREAMTYPE_AUDIO;
258 bool video = changedMask & LiveSession::STREAMTYPE_VIDEO;
259
260 sp<AMessage> reply;
261 CHECK(msg->findMessage("reply", &reply));
262
263 sp<AMessage> notify = dupNotify();
264 notify->setInt32("what", kWhatQueueDecoderShutdown);
265 notify->setInt32("audio", audio);
266 notify->setInt32("video", video);
267 notify->setMessage("reply", reply);
268 notify->post();
269 break;
270 }
271
272 case LiveSession::kWhatError:
273 {
274 break;
275 }
276
Andreas Huber0df36ec2013-02-06 10:44:39 -0800277 default:
278 TRESPASS();
279 }
280}
281
Andreas Huber5bc087c2010-12-23 10:27:40 -0800282} // namespace android
283