blob: cbedf5cb82b3f4f85fe981b3ff490ce27d64bcb2 [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 mLiveSession.clear();
66
Andreas Huber2048d0c2011-07-15 16:25:41 -070067 mLiveLooper->stop();
Andreas Huber14f76722013-01-15 09:04:18 -080068 mLiveLooper.clear();
Andreas Huber2048d0c2011-07-15 16:25:41 -070069 }
Andreas Huber5bc087c2010-12-23 10:27:40 -080070}
71
Andreas Huber9575c962013-02-05 13:59:56 -080072void NuPlayer::HTTPLiveSource::prepareAsync() {
Andreas Huber5bc087c2010-12-23 10:27:40 -080073 mLiveLooper = new ALooper;
74 mLiveLooper->setName("http live");
75 mLiveLooper->start();
76
Andreas Huber0df36ec2013-02-06 10:44:39 -080077 sp<AMessage> notify = new AMessage(kWhatSessionNotify, id());
78
Andreas Huber7314fa12011-02-24 14:42:48 -080079 mLiveSession = new LiveSession(
Andreas Huber0df36ec2013-02-06 10:44:39 -080080 notify,
Andreas Huber9b80c2b2011-06-30 15:47:02 -070081 (mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0,
Andreas Huber81e68442014-02-05 11:52:33 -080082 mHTTPService);
Andreas Huber7314fa12011-02-24 14:42:48 -080083
Andreas Huber5bc087c2010-12-23 10:27:40 -080084 mLiveLooper->registerHandler(mLiveSession);
85
Andreas Huber14f76722013-01-15 09:04:18 -080086 mLiveSession->connectAsync(
Andreas Huberad0d9c92011-04-19 11:50:27 -070087 mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
Andreas Huber9575c962013-02-05 13:59:56 -080088}
89
90void NuPlayer::HTTPLiveSource::start() {
Andreas Huber5bc087c2010-12-23 10:27:40 -080091}
92
Andreas Huber14f76722013-01-15 09:04:18 -080093sp<AMessage> NuPlayer::HTTPLiveSource::getFormat(bool audio) {
94 sp<AMessage> format;
95 status_t err = mLiveSession->getStreamFormat(
96 audio ? LiveSession::STREAMTYPE_AUDIO
97 : LiveSession::STREAMTYPE_VIDEO,
98 &format);
Andreas Huber5bc087c2010-12-23 10:27:40 -080099
Andreas Huber14f76722013-01-15 09:04:18 -0800100 if (err != OK) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800101 return NULL;
102 }
103
Andreas Huber14f76722013-01-15 09:04:18 -0800104 return format;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800105}
106
Andreas Hubereac68ba2011-09-27 12:12:25 -0700107status_t NuPlayer::HTTPLiveSource::feedMoreTSData() {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700108 return OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800109}
110
111status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit(
112 bool audio, sp<ABuffer> *accessUnit) {
Andreas Huber14f76722013-01-15 09:04:18 -0800113 return mLiveSession->dequeueAccessUnit(
114 audio ? LiveSession::STREAMTYPE_AUDIO
115 : LiveSession::STREAMTYPE_VIDEO,
116 accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800117}
118
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800119status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) {
120 return mLiveSession->getDuration(durationUs);
121}
122
Chong Zhangdcb89b32013-08-06 09:44:47 -0700123status_t NuPlayer::HTTPLiveSource::getTrackInfo(Parcel *reply) const {
124 return mLiveSession->getTrackInfo(reply);
125}
126
127status_t NuPlayer::HTTPLiveSource::selectTrack(size_t trackIndex, bool select) {
128 status_t err = mLiveSession->selectTrack(trackIndex, select);
129
130 if (err == OK) {
131 mFetchSubtitleDataGeneration++;
132 if (select) {
133 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
134 msg->setInt32("generation", mFetchSubtitleDataGeneration);
135 msg->post();
136 }
137 }
138
139 // LiveSession::selectTrack returns BAD_VALUE when selecting the currently
140 // selected track, or unselecting a non-selected track. In this case it's an
141 // no-op so we return OK.
Andreas Huber84333e02014-02-07 15:36:10 -0800142 return (err == OK || err == BAD_VALUE) ? (status_t)OK : err;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700143}
144
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800145status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) {
Andreas Huber14f76722013-01-15 09:04:18 -0800146 return mLiveSession->seekTo(seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800147}
148
Andreas Huber0df36ec2013-02-06 10:44:39 -0800149void NuPlayer::HTTPLiveSource::onMessageReceived(const sp<AMessage> &msg) {
150 switch (msg->what()) {
151 case kWhatSessionNotify:
152 {
153 onSessionNotify(msg);
154 break;
155 }
156
Chong Zhangdcb89b32013-08-06 09:44:47 -0700157 case kWhatFetchSubtitleData:
158 {
159 int32_t generation;
160 CHECK(msg->findInt32("generation", &generation));
161
162 if (generation != mFetchSubtitleDataGeneration) {
163 // stale
164 break;
165 }
166
167 sp<ABuffer> buffer;
168 if (mLiveSession->dequeueAccessUnit(
169 LiveSession::STREAMTYPE_SUBTITLES, &buffer) == OK) {
170 sp<AMessage> notify = dupNotify();
171 notify->setInt32("what", kWhatSubtitleData);
172 notify->setBuffer("buffer", buffer);
173 notify->post();
174
175 int64_t timeUs, baseUs, durationUs, delayUs;
176 CHECK(buffer->meta()->findInt64("baseUs", &baseUs));
177 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
178 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
179 delayUs = baseUs + timeUs - ALooper::GetNowUs();
180
181 msg->post(delayUs > 0ll ? delayUs : 0ll);
182 } else {
183 // try again in 1 second
184 msg->post(1000000ll);
185 }
186
187 break;
188 }
189
Andreas Huber0df36ec2013-02-06 10:44:39 -0800190 default:
191 Source::onMessageReceived(msg);
192 break;
193 }
194}
195
196void NuPlayer::HTTPLiveSource::onSessionNotify(const sp<AMessage> &msg) {
197 int32_t what;
198 CHECK(msg->findInt32("what", &what));
199
200 switch (what) {
201 case LiveSession::kWhatPrepared:
202 {
Marco Nelissen3e518fd2013-11-01 10:33:18 -0700203 // notify the current size here if we have it, otherwise report an initial size of (0,0)
204 sp<AMessage> format = getFormat(false /* audio */);
205 int32_t width;
206 int32_t height;
207 if (format != NULL &&
208 format->findInt32("width", &width) && format->findInt32("height", &height)) {
209 notifyVideoSizeChanged(width, height);
210 } else {
211 notifyVideoSizeChanged(0, 0);
212 }
Andreas Huber0df36ec2013-02-06 10:44:39 -0800213
214 uint32_t flags = FLAG_CAN_PAUSE;
215 if (mLiveSession->isSeekable()) {
216 flags |= FLAG_CAN_SEEK;
217 flags |= FLAG_CAN_SEEK_BACKWARD;
218 flags |= FLAG_CAN_SEEK_FORWARD;
219 }
220
221 if (mLiveSession->hasDynamicDuration()) {
222 flags |= FLAG_DYNAMIC_DURATION;
223 }
224
225 notifyFlagsChanged(flags);
226
227 notifyPrepared();
228 break;
229 }
230
231 case LiveSession::kWhatPreparationFailed:
232 {
233 status_t err;
234 CHECK(msg->findInt32("err", &err));
235
236 notifyPrepared(err);
237 break;
238 }
239
Andreas Huber14f76722013-01-15 09:04:18 -0800240 case LiveSession::kWhatStreamsChanged:
241 {
242 uint32_t changedMask;
243 CHECK(msg->findInt32(
244 "changedMask", (int32_t *)&changedMask));
245
246 bool audio = changedMask & LiveSession::STREAMTYPE_AUDIO;
247 bool video = changedMask & LiveSession::STREAMTYPE_VIDEO;
248
249 sp<AMessage> reply;
250 CHECK(msg->findMessage("reply", &reply));
251
252 sp<AMessage> notify = dupNotify();
253 notify->setInt32("what", kWhatQueueDecoderShutdown);
254 notify->setInt32("audio", audio);
255 notify->setInt32("video", video);
256 notify->setMessage("reply", reply);
257 notify->post();
258 break;
259 }
260
261 case LiveSession::kWhatError:
262 {
263 break;
264 }
265
Andreas Huber0df36ec2013-02-06 10:44:39 -0800266 default:
267 TRESPASS();
268 }
269}
270
Andreas Huber5bc087c2010-12-23 10:27:40 -0800271} // namespace android
272