blob: c8901ce1b8efcc12b0ae5eba97e86a49db88d7d9 [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),
Andreas Huber5bc087c2010-12-23 10:27:40 -080046 mOffset(0) {
Andreas Huberad0d9c92011-04-19 11:50:27 -070047 if (headers) {
48 mExtraHeaders = *headers;
49
50 ssize_t index =
51 mExtraHeaders.indexOfKey(String8("x-hide-urls-from-log"));
52
53 if (index >= 0) {
54 mFlags |= kFlagIncognito;
55
56 mExtraHeaders.removeItemsAt(index);
57 }
58 }
Andreas Huber5bc087c2010-12-23 10:27:40 -080059}
60
61NuPlayer::HTTPLiveSource::~HTTPLiveSource() {
Andreas Huber2048d0c2011-07-15 16:25:41 -070062 if (mLiveSession != NULL) {
63 mLiveSession->disconnect();
Andreas Huber14f76722013-01-15 09:04:18 -080064 mLiveSession.clear();
65
Andreas Huber2048d0c2011-07-15 16:25:41 -070066 mLiveLooper->stop();
Andreas Huber14f76722013-01-15 09:04:18 -080067 mLiveLooper.clear();
Andreas Huber2048d0c2011-07-15 16:25:41 -070068 }
Andreas Huber5bc087c2010-12-23 10:27:40 -080069}
70
Andreas Huber9575c962013-02-05 13:59:56 -080071void NuPlayer::HTTPLiveSource::prepareAsync() {
Andreas Huber5bc087c2010-12-23 10:27:40 -080072 mLiveLooper = new ALooper;
73 mLiveLooper->setName("http live");
74 mLiveLooper->start();
75
Andreas Huber0df36ec2013-02-06 10:44:39 -080076 sp<AMessage> notify = new AMessage(kWhatSessionNotify, id());
77
Andreas Huber7314fa12011-02-24 14:42:48 -080078 mLiveSession = new LiveSession(
Andreas Huber0df36ec2013-02-06 10:44:39 -080079 notify,
Andreas Huber9b80c2b2011-06-30 15:47:02 -070080 (mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0,
Andreas Huber14f76722013-01-15 09:04:18 -080081 mUIDValid,
82 mUID);
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
123status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) {
Andreas Huber14f76722013-01-15 09:04:18 -0800124 return mLiveSession->seekTo(seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800125}
126
Andreas Huber0df36ec2013-02-06 10:44:39 -0800127void NuPlayer::HTTPLiveSource::onMessageReceived(const sp<AMessage> &msg) {
128 switch (msg->what()) {
129 case kWhatSessionNotify:
130 {
131 onSessionNotify(msg);
132 break;
133 }
134
135 default:
136 Source::onMessageReceived(msg);
137 break;
138 }
139}
140
141void NuPlayer::HTTPLiveSource::onSessionNotify(const sp<AMessage> &msg) {
142 int32_t what;
143 CHECK(msg->findInt32("what", &what));
144
145 switch (what) {
146 case LiveSession::kWhatPrepared:
147 {
148 notifyVideoSizeChanged(0, 0);
149
150 uint32_t flags = FLAG_CAN_PAUSE;
151 if (mLiveSession->isSeekable()) {
152 flags |= FLAG_CAN_SEEK;
153 flags |= FLAG_CAN_SEEK_BACKWARD;
154 flags |= FLAG_CAN_SEEK_FORWARD;
155 }
156
157 if (mLiveSession->hasDynamicDuration()) {
158 flags |= FLAG_DYNAMIC_DURATION;
159 }
160
161 notifyFlagsChanged(flags);
162
163 notifyPrepared();
164 break;
165 }
166
167 case LiveSession::kWhatPreparationFailed:
168 {
169 status_t err;
170 CHECK(msg->findInt32("err", &err));
171
172 notifyPrepared(err);
173 break;
174 }
175
Andreas Huber14f76722013-01-15 09:04:18 -0800176 case LiveSession::kWhatStreamsChanged:
177 {
178 uint32_t changedMask;
179 CHECK(msg->findInt32(
180 "changedMask", (int32_t *)&changedMask));
181
182 bool audio = changedMask & LiveSession::STREAMTYPE_AUDIO;
183 bool video = changedMask & LiveSession::STREAMTYPE_VIDEO;
184
185 sp<AMessage> reply;
186 CHECK(msg->findMessage("reply", &reply));
187
188 sp<AMessage> notify = dupNotify();
189 notify->setInt32("what", kWhatQueueDecoderShutdown);
190 notify->setInt32("audio", audio);
191 notify->setInt32("video", video);
192 notify->setMessage("reply", reply);
193 notify->post();
194 break;
195 }
196
197 case LiveSession::kWhatError:
198 {
199 break;
200 }
201
Andreas Huber0df36ec2013-02-06 10:44:39 -0800202 default:
203 TRESPASS();
204 }
205}
206
Andreas Huber5bc087c2010-12-23 10:27:40 -0800207} // namespace android
208