blob: e3350558e181b6a47f285f94d798aee83c25c47a [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) {
Robert Shih1098d872014-11-06 13:58:48 -0800101 if (mLiveSession == NULL) {
102 return NULL;
103 }
104
Andreas Huber14f76722013-01-15 09:04:18 -0800105 sp<AMessage> format;
106 status_t err = mLiveSession->getStreamFormat(
107 audio ? LiveSession::STREAMTYPE_AUDIO
108 : LiveSession::STREAMTYPE_VIDEO,
109 &format);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800110
Andreas Huber14f76722013-01-15 09:04:18 -0800111 if (err != OK) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800112 return NULL;
113 }
114
Andreas Huber14f76722013-01-15 09:04:18 -0800115 return format;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800116}
117
Andreas Hubereac68ba2011-09-27 12:12:25 -0700118status_t NuPlayer::HTTPLiveSource::feedMoreTSData() {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700119 return OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800120}
121
122status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit(
123 bool audio, sp<ABuffer> *accessUnit) {
Andreas Huber14f76722013-01-15 09:04:18 -0800124 return mLiveSession->dequeueAccessUnit(
125 audio ? LiveSession::STREAMTYPE_AUDIO
126 : LiveSession::STREAMTYPE_VIDEO,
127 accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800128}
129
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800130status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) {
131 return mLiveSession->getDuration(durationUs);
132}
133
Chong Zhang404fced2014-06-11 14:45:31 -0700134size_t NuPlayer::HTTPLiveSource::getTrackCount() const {
135 return mLiveSession->getTrackCount();
136}
137
138sp<AMessage> NuPlayer::HTTPLiveSource::getTrackInfo(size_t trackIndex) const {
139 return mLiveSession->getTrackInfo(trackIndex);
Chong Zhangdcb89b32013-08-06 09:44:47 -0700140}
141
Robert Shih89bf2522014-07-29 19:25:10 -0700142ssize_t NuPlayer::HTTPLiveSource::getSelectedTrack(media_track_type type) const {
143 if (mLiveSession == NULL) {
144 return -1;
145 } else {
146 return mLiveSession->getSelectedTrack(type);
147 }
148}
149
Chong Zhangdcb89b32013-08-06 09:44:47 -0700150status_t NuPlayer::HTTPLiveSource::selectTrack(size_t trackIndex, bool select) {
151 status_t err = mLiveSession->selectTrack(trackIndex, select);
152
153 if (err == OK) {
154 mFetchSubtitleDataGeneration++;
155 if (select) {
156 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
157 msg->setInt32("generation", mFetchSubtitleDataGeneration);
158 msg->post();
159 }
160 }
161
162 // LiveSession::selectTrack returns BAD_VALUE when selecting the currently
163 // selected track, or unselecting a non-selected track. In this case it's an
164 // no-op so we return OK.
Andreas Huber84333e02014-02-07 15:36:10 -0800165 return (err == OK || err == BAD_VALUE) ? (status_t)OK : err;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700166}
167
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800168status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) {
Andreas Huber14f76722013-01-15 09:04:18 -0800169 return mLiveSession->seekTo(seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800170}
171
Andreas Huber0df36ec2013-02-06 10:44:39 -0800172void NuPlayer::HTTPLiveSource::onMessageReceived(const sp<AMessage> &msg) {
173 switch (msg->what()) {
174 case kWhatSessionNotify:
175 {
176 onSessionNotify(msg);
177 break;
178 }
179
Chong Zhangdcb89b32013-08-06 09:44:47 -0700180 case kWhatFetchSubtitleData:
181 {
182 int32_t generation;
183 CHECK(msg->findInt32("generation", &generation));
184
185 if (generation != mFetchSubtitleDataGeneration) {
186 // stale
187 break;
188 }
189
190 sp<ABuffer> buffer;
191 if (mLiveSession->dequeueAccessUnit(
192 LiveSession::STREAMTYPE_SUBTITLES, &buffer) == OK) {
193 sp<AMessage> notify = dupNotify();
194 notify->setInt32("what", kWhatSubtitleData);
195 notify->setBuffer("buffer", buffer);
196 notify->post();
197
198 int64_t timeUs, baseUs, durationUs, delayUs;
199 CHECK(buffer->meta()->findInt64("baseUs", &baseUs));
200 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
201 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
202 delayUs = baseUs + timeUs - ALooper::GetNowUs();
203
204 msg->post(delayUs > 0ll ? delayUs : 0ll);
205 } else {
206 // try again in 1 second
207 msg->post(1000000ll);
208 }
209
210 break;
211 }
212
Andreas Huber0df36ec2013-02-06 10:44:39 -0800213 default:
214 Source::onMessageReceived(msg);
215 break;
216 }
217}
218
219void NuPlayer::HTTPLiveSource::onSessionNotify(const sp<AMessage> &msg) {
220 int32_t what;
221 CHECK(msg->findInt32("what", &what));
222
223 switch (what) {
224 case LiveSession::kWhatPrepared:
225 {
Marco Nelissen3e518fd2013-11-01 10:33:18 -0700226 // notify the current size here if we have it, otherwise report an initial size of (0,0)
227 sp<AMessage> format = getFormat(false /* audio */);
228 int32_t width;
229 int32_t height;
230 if (format != NULL &&
231 format->findInt32("width", &width) && format->findInt32("height", &height)) {
Chong Zhangced1c2f2014-08-08 15:22:35 -0700232 notifyVideoSizeChanged(format);
Marco Nelissen3e518fd2013-11-01 10:33:18 -0700233 } else {
Chong Zhangced1c2f2014-08-08 15:22:35 -0700234 notifyVideoSizeChanged();
Marco Nelissen3e518fd2013-11-01 10:33:18 -0700235 }
Andreas Huber0df36ec2013-02-06 10:44:39 -0800236
237 uint32_t flags = FLAG_CAN_PAUSE;
238 if (mLiveSession->isSeekable()) {
239 flags |= FLAG_CAN_SEEK;
240 flags |= FLAG_CAN_SEEK_BACKWARD;
241 flags |= FLAG_CAN_SEEK_FORWARD;
242 }
243
244 if (mLiveSession->hasDynamicDuration()) {
245 flags |= FLAG_DYNAMIC_DURATION;
246 }
247
248 notifyFlagsChanged(flags);
249
250 notifyPrepared();
251 break;
252 }
253
254 case LiveSession::kWhatPreparationFailed:
255 {
256 status_t err;
257 CHECK(msg->findInt32("err", &err));
258
259 notifyPrepared(err);
260 break;
261 }
262
Andreas Huber14f76722013-01-15 09:04:18 -0800263 case LiveSession::kWhatStreamsChanged:
264 {
265 uint32_t changedMask;
266 CHECK(msg->findInt32(
267 "changedMask", (int32_t *)&changedMask));
268
269 bool audio = changedMask & LiveSession::STREAMTYPE_AUDIO;
270 bool video = changedMask & LiveSession::STREAMTYPE_VIDEO;
271
272 sp<AMessage> reply;
273 CHECK(msg->findMessage("reply", &reply));
274
275 sp<AMessage> notify = dupNotify();
276 notify->setInt32("what", kWhatQueueDecoderShutdown);
277 notify->setInt32("audio", audio);
278 notify->setInt32("video", video);
279 notify->setMessage("reply", reply);
280 notify->post();
281 break;
282 }
283
284 case LiveSession::kWhatError:
285 {
286 break;
287 }
288
Andreas Huber0df36ec2013-02-06 10:44:39 -0800289 default:
290 TRESPASS();
291 }
292}
293
Andreas Huber5bc087c2010-12-23 10:27:40 -0800294} // namespace android
295