blob: d8b35d7ff142fd77db3f026259012371d50012b3 [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
27#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>
32
33namespace android {
34
Andreas Huberad0d9c92011-04-19 11:50:27 -070035NuPlayer::HTTPLiveSource::HTTPLiveSource(
Andreas Huberb5f25f02013-02-05 10:14:26 -080036 const sp<AMessage> &notify,
Andreas Huberad0d9c92011-04-19 11:50:27 -070037 const char *url,
Andreas Huber9b80c2b2011-06-30 15:47:02 -070038 const KeyedVector<String8, String8> *headers,
39 bool uidValid, uid_t uid)
Andreas Huberb5f25f02013-02-05 10:14:26 -080040 : Source(notify),
41 mURL(url),
Andreas Huber9b80c2b2011-06-30 15:47:02 -070042 mUIDValid(uidValid),
43 mUID(uid),
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 Huber14f76722013-01-15 09:04:18 -080082 mUIDValid,
83 mUID);
Andreas Huber7314fa12011-02-24 14:42:48 -080084
Andreas Huber5bc087c2010-12-23 10:27:40 -080085 mLiveLooper->registerHandler(mLiveSession);
86
Andreas Huber14f76722013-01-15 09:04:18 -080087 mLiveSession->connectAsync(
Andreas Huberad0d9c92011-04-19 11:50:27 -070088 mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
Andreas Huber9575c962013-02-05 13:59:56 -080089}
90
91void NuPlayer::HTTPLiveSource::start() {
Andreas Huber5bc087c2010-12-23 10:27:40 -080092}
93
Andreas Huber14f76722013-01-15 09:04:18 -080094sp<AMessage> NuPlayer::HTTPLiveSource::getFormat(bool audio) {
95 sp<AMessage> format;
96 status_t err = mLiveSession->getStreamFormat(
97 audio ? LiveSession::STREAMTYPE_AUDIO
98 : LiveSession::STREAMTYPE_VIDEO,
99 &format);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800100
Andreas Huber14f76722013-01-15 09:04:18 -0800101 if (err != OK) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800102 return NULL;
103 }
104
Andreas Huber14f76722013-01-15 09:04:18 -0800105 return format;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800106}
107
Andreas Hubereac68ba2011-09-27 12:12:25 -0700108status_t NuPlayer::HTTPLiveSource::feedMoreTSData() {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700109 return OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800110}
111
112status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit(
113 bool audio, sp<ABuffer> *accessUnit) {
Andreas Huber14f76722013-01-15 09:04:18 -0800114 return mLiveSession->dequeueAccessUnit(
115 audio ? LiveSession::STREAMTYPE_AUDIO
116 : LiveSession::STREAMTYPE_VIDEO,
117 accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800118}
119
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800120status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) {
121 return mLiveSession->getDuration(durationUs);
122}
123
Chong Zhangdcb89b32013-08-06 09:44:47 -0700124status_t NuPlayer::HTTPLiveSource::getTrackInfo(Parcel *reply) const {
125 return mLiveSession->getTrackInfo(reply);
126}
127
128status_t NuPlayer::HTTPLiveSource::selectTrack(size_t trackIndex, bool select) {
129 status_t err = mLiveSession->selectTrack(trackIndex, select);
130
131 if (err == OK) {
132 mFetchSubtitleDataGeneration++;
133 if (select) {
134 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
135 msg->setInt32("generation", mFetchSubtitleDataGeneration);
136 msg->post();
137 }
138 }
139
140 // LiveSession::selectTrack returns BAD_VALUE when selecting the currently
141 // selected track, or unselecting a non-selected track. In this case it's an
142 // no-op so we return OK.
143 return (err == OK || err == BAD_VALUE) ? OK : err;
144}
145
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800146status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) {
Andreas Huber14f76722013-01-15 09:04:18 -0800147 return mLiveSession->seekTo(seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800148}
149
Andreas Huber0df36ec2013-02-06 10:44:39 -0800150void NuPlayer::HTTPLiveSource::onMessageReceived(const sp<AMessage> &msg) {
151 switch (msg->what()) {
152 case kWhatSessionNotify:
153 {
154 onSessionNotify(msg);
155 break;
156 }
157
Chong Zhangdcb89b32013-08-06 09:44:47 -0700158 case kWhatFetchSubtitleData:
159 {
160 int32_t generation;
161 CHECK(msg->findInt32("generation", &generation));
162
163 if (generation != mFetchSubtitleDataGeneration) {
164 // stale
165 break;
166 }
167
168 sp<ABuffer> buffer;
169 if (mLiveSession->dequeueAccessUnit(
170 LiveSession::STREAMTYPE_SUBTITLES, &buffer) == OK) {
171 sp<AMessage> notify = dupNotify();
172 notify->setInt32("what", kWhatSubtitleData);
173 notify->setBuffer("buffer", buffer);
174 notify->post();
175
176 int64_t timeUs, baseUs, durationUs, delayUs;
177 CHECK(buffer->meta()->findInt64("baseUs", &baseUs));
178 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
179 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
180 delayUs = baseUs + timeUs - ALooper::GetNowUs();
181
182 msg->post(delayUs > 0ll ? delayUs : 0ll);
183 } else {
184 // try again in 1 second
185 msg->post(1000000ll);
186 }
187
188 break;
189 }
190
Andreas Huber0df36ec2013-02-06 10:44:39 -0800191 default:
192 Source::onMessageReceived(msg);
193 break;
194 }
195}
196
197void NuPlayer::HTTPLiveSource::onSessionNotify(const sp<AMessage> &msg) {
198 int32_t what;
199 CHECK(msg->findInt32("what", &what));
200
201 switch (what) {
202 case LiveSession::kWhatPrepared:
203 {
204 notifyVideoSizeChanged(0, 0);
205
206 uint32_t flags = FLAG_CAN_PAUSE;
207 if (mLiveSession->isSeekable()) {
208 flags |= FLAG_CAN_SEEK;
209 flags |= FLAG_CAN_SEEK_BACKWARD;
210 flags |= FLAG_CAN_SEEK_FORWARD;
211 }
212
213 if (mLiveSession->hasDynamicDuration()) {
214 flags |= FLAG_DYNAMIC_DURATION;
215 }
216
217 notifyFlagsChanged(flags);
218
219 notifyPrepared();
220 break;
221 }
222
223 case LiveSession::kWhatPreparationFailed:
224 {
225 status_t err;
226 CHECK(msg->findInt32("err", &err));
227
228 notifyPrepared(err);
229 break;
230 }
231
Andreas Huber14f76722013-01-15 09:04:18 -0800232 case LiveSession::kWhatStreamsChanged:
233 {
234 uint32_t changedMask;
235 CHECK(msg->findInt32(
236 "changedMask", (int32_t *)&changedMask));
237
238 bool audio = changedMask & LiveSession::STREAMTYPE_AUDIO;
239 bool video = changedMask & LiveSession::STREAMTYPE_VIDEO;
240
241 sp<AMessage> reply;
242 CHECK(msg->findMessage("reply", &reply));
243
244 sp<AMessage> notify = dupNotify();
245 notify->setInt32("what", kWhatQueueDecoderShutdown);
246 notify->setInt32("audio", audio);
247 notify->setInt32("video", video);
248 notify->setMessage("reply", reply);
249 notify->post();
250 break;
251 }
252
253 case LiveSession::kWhatError:
254 {
255 break;
256 }
257
Andreas Huber0df36ec2013-02-06 10:44:39 -0800258 default:
259 TRESPASS();
260 }
261}
262
Andreas Huber5bc087c2010-12-23 10:27:40 -0800263} // namespace android
264