| Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 23 | #include "AnotherPacketSource.h" | 
|  | 24 | #include "LiveDataSource.h" | 
|  | 25 | #include "LiveSession.h" | 
|  | 26 |  | 
| Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 27 | #include <media/IMediaHTTPService.h> | 
| Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 28 | #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 |  | 
|  | 34 | namespace android { | 
|  | 35 |  | 
| Andreas Huber | ad0d9c9 | 2011-04-19 11:50:27 -0700 | [diff] [blame] | 36 | NuPlayer::HTTPLiveSource::HTTPLiveSource( | 
| Andreas Huber | b5f25f0 | 2013-02-05 10:14:26 -0800 | [diff] [blame] | 37 | const sp<AMessage> ¬ify, | 
| Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 38 | const sp<IMediaHTTPService> &httpService, | 
| Andreas Huber | ad0d9c9 | 2011-04-19 11:50:27 -0700 | [diff] [blame] | 39 | const char *url, | 
| Andreas Huber | 81e6844 | 2014-02-05 11:52:33 -0800 | [diff] [blame] | 40 | const KeyedVector<String8, String8> *headers) | 
| Andreas Huber | b5f25f0 | 2013-02-05 10:14:26 -0800 | [diff] [blame] | 41 | : Source(notify), | 
| Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 42 | mHTTPService(httpService), | 
| Andreas Huber | b5f25f0 | 2013-02-05 10:14:26 -0800 | [diff] [blame] | 43 | mURL(url), | 
| Andreas Huber | ad0d9c9 | 2011-04-19 11:50:27 -0700 | [diff] [blame] | 44 | mFlags(0), | 
| Andreas Huber | eac68ba | 2011-09-27 12:12:25 -0700 | [diff] [blame] | 45 | mFinalResult(OK), | 
| Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 46 | mOffset(0), | 
|  | 47 | mFetchSubtitleDataGeneration(0) { | 
| Andreas Huber | ad0d9c9 | 2011-04-19 11:50:27 -0700 | [diff] [blame] | 48 | 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 Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 60 | } | 
|  | 61 |  | 
|  | 62 | NuPlayer::HTTPLiveSource::~HTTPLiveSource() { | 
| Andreas Huber | 2048d0c | 2011-07-15 16:25:41 -0700 | [diff] [blame] | 63 | if (mLiveSession != NULL) { | 
|  | 64 | mLiveSession->disconnect(); | 
| Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 65 |  | 
| Chong Zhang | 1228d6b | 2014-08-12 21:25:48 -0700 | [diff] [blame] | 66 | mLiveLooper->unregisterHandler(mLiveSession->id()); | 
|  | 67 | mLiveLooper->unregisterHandler(id()); | 
| Andreas Huber | 2048d0c | 2011-07-15 16:25:41 -0700 | [diff] [blame] | 68 | mLiveLooper->stop(); | 
| Chong Zhang | 1228d6b | 2014-08-12 21:25:48 -0700 | [diff] [blame] | 69 |  | 
|  | 70 | mLiveSession.clear(); | 
| Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 71 | mLiveLooper.clear(); | 
| Andreas Huber | 2048d0c | 2011-07-15 16:25:41 -0700 | [diff] [blame] | 72 | } | 
| Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
| Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 75 | void NuPlayer::HTTPLiveSource::prepareAsync() { | 
| Chong Zhang | 1228d6b | 2014-08-12 21:25:48 -0700 | [diff] [blame] | 76 | if (mLiveLooper == NULL) { | 
|  | 77 | mLiveLooper = new ALooper; | 
|  | 78 | mLiveLooper->setName("http live"); | 
|  | 79 | mLiveLooper->start(); | 
|  | 80 |  | 
|  | 81 | mLiveLooper->registerHandler(this); | 
|  | 82 | } | 
| Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 83 |  | 
| Andreas Huber | 0df36ec | 2013-02-06 10:44:39 -0800 | [diff] [blame] | 84 | sp<AMessage> notify = new AMessage(kWhatSessionNotify, id()); | 
|  | 85 |  | 
| Andreas Huber | 7314fa1 | 2011-02-24 14:42:48 -0800 | [diff] [blame] | 86 | mLiveSession = new LiveSession( | 
| Andreas Huber | 0df36ec | 2013-02-06 10:44:39 -0800 | [diff] [blame] | 87 | notify, | 
| Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 88 | (mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0, | 
| Andreas Huber | 81e6844 | 2014-02-05 11:52:33 -0800 | [diff] [blame] | 89 | mHTTPService); | 
| Andreas Huber | 7314fa1 | 2011-02-24 14:42:48 -0800 | [diff] [blame] | 90 |  | 
| Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 91 | mLiveLooper->registerHandler(mLiveSession); | 
|  | 92 |  | 
| Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 93 | mLiveSession->connectAsync( | 
| Andreas Huber | ad0d9c9 | 2011-04-19 11:50:27 -0700 | [diff] [blame] | 94 | mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders); | 
| Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 95 | } | 
|  | 96 |  | 
|  | 97 | void NuPlayer::HTTPLiveSource::start() { | 
| Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 98 | } | 
|  | 99 |  | 
| Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 100 | sp<AMessage> NuPlayer::HTTPLiveSource::getFormat(bool audio) { | 
| Robert Shih | 1098d87 | 2014-11-06 13:58:48 -0800 | [diff] [blame^] | 101 | if (mLiveSession == NULL) { | 
|  | 102 | return NULL; | 
|  | 103 | } | 
|  | 104 |  | 
| Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 105 | sp<AMessage> format; | 
|  | 106 | status_t err = mLiveSession->getStreamFormat( | 
|  | 107 | audio ? LiveSession::STREAMTYPE_AUDIO | 
|  | 108 | : LiveSession::STREAMTYPE_VIDEO, | 
|  | 109 | &format); | 
| Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 110 |  | 
| Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 111 | if (err != OK) { | 
| Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 112 | return NULL; | 
|  | 113 | } | 
|  | 114 |  | 
| Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 115 | return format; | 
| Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 116 | } | 
|  | 117 |  | 
| Andreas Huber | eac68ba | 2011-09-27 12:12:25 -0700 | [diff] [blame] | 118 | status_t NuPlayer::HTTPLiveSource::feedMoreTSData() { | 
| Andreas Huber | eac68ba | 2011-09-27 12:12:25 -0700 | [diff] [blame] | 119 | return OK; | 
| Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 120 | } | 
|  | 121 |  | 
|  | 122 | status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit( | 
|  | 123 | bool audio, sp<ABuffer> *accessUnit) { | 
| Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 124 | return mLiveSession->dequeueAccessUnit( | 
|  | 125 | audio ? LiveSession::STREAMTYPE_AUDIO | 
|  | 126 | : LiveSession::STREAMTYPE_VIDEO, | 
|  | 127 | accessUnit); | 
| Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 128 | } | 
|  | 129 |  | 
| Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 130 | status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) { | 
|  | 131 | return mLiveSession->getDuration(durationUs); | 
|  | 132 | } | 
|  | 133 |  | 
| Chong Zhang | 404fced | 2014-06-11 14:45:31 -0700 | [diff] [blame] | 134 | size_t NuPlayer::HTTPLiveSource::getTrackCount() const { | 
|  | 135 | return mLiveSession->getTrackCount(); | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | sp<AMessage> NuPlayer::HTTPLiveSource::getTrackInfo(size_t trackIndex) const { | 
|  | 139 | return mLiveSession->getTrackInfo(trackIndex); | 
| Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 140 | } | 
|  | 141 |  | 
|  | 142 | status_t NuPlayer::HTTPLiveSource::selectTrack(size_t trackIndex, bool select) { | 
|  | 143 | status_t err = mLiveSession->selectTrack(trackIndex, select); | 
|  | 144 |  | 
|  | 145 | if (err == OK) { | 
|  | 146 | mFetchSubtitleDataGeneration++; | 
|  | 147 | if (select) { | 
|  | 148 | sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id()); | 
|  | 149 | msg->setInt32("generation", mFetchSubtitleDataGeneration); | 
|  | 150 | msg->post(); | 
|  | 151 | } | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | // LiveSession::selectTrack returns BAD_VALUE when selecting the currently | 
|  | 155 | // selected track, or unselecting a non-selected track. In this case it's an | 
|  | 156 | // no-op so we return OK. | 
| Andreas Huber | 84333e0 | 2014-02-07 15:36:10 -0800 | [diff] [blame] | 157 | return (err == OK || err == BAD_VALUE) ? (status_t)OK : err; | 
| Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 158 | } | 
|  | 159 |  | 
| Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 160 | status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) { | 
| Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 161 | return mLiveSession->seekTo(seekTimeUs); | 
| Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 162 | } | 
|  | 163 |  | 
| Andreas Huber | 0df36ec | 2013-02-06 10:44:39 -0800 | [diff] [blame] | 164 | void NuPlayer::HTTPLiveSource::onMessageReceived(const sp<AMessage> &msg) { | 
|  | 165 | switch (msg->what()) { | 
|  | 166 | case kWhatSessionNotify: | 
|  | 167 | { | 
|  | 168 | onSessionNotify(msg); | 
|  | 169 | break; | 
|  | 170 | } | 
|  | 171 |  | 
| Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 172 | case kWhatFetchSubtitleData: | 
|  | 173 | { | 
|  | 174 | int32_t generation; | 
|  | 175 | CHECK(msg->findInt32("generation", &generation)); | 
|  | 176 |  | 
|  | 177 | if (generation != mFetchSubtitleDataGeneration) { | 
|  | 178 | // stale | 
|  | 179 | break; | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | sp<ABuffer> buffer; | 
|  | 183 | if (mLiveSession->dequeueAccessUnit( | 
|  | 184 | LiveSession::STREAMTYPE_SUBTITLES, &buffer) == OK) { | 
|  | 185 | sp<AMessage> notify = dupNotify(); | 
|  | 186 | notify->setInt32("what", kWhatSubtitleData); | 
|  | 187 | notify->setBuffer("buffer", buffer); | 
|  | 188 | notify->post(); | 
|  | 189 |  | 
|  | 190 | int64_t timeUs, baseUs, durationUs, delayUs; | 
|  | 191 | CHECK(buffer->meta()->findInt64("baseUs", &baseUs)); | 
|  | 192 | CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); | 
|  | 193 | CHECK(buffer->meta()->findInt64("durationUs", &durationUs)); | 
|  | 194 | delayUs = baseUs + timeUs - ALooper::GetNowUs(); | 
|  | 195 |  | 
|  | 196 | msg->post(delayUs > 0ll ? delayUs : 0ll); | 
|  | 197 | } else { | 
|  | 198 | // try again in 1 second | 
|  | 199 | msg->post(1000000ll); | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 | break; | 
|  | 203 | } | 
|  | 204 |  | 
| Andreas Huber | 0df36ec | 2013-02-06 10:44:39 -0800 | [diff] [blame] | 205 | default: | 
|  | 206 | Source::onMessageReceived(msg); | 
|  | 207 | break; | 
|  | 208 | } | 
|  | 209 | } | 
|  | 210 |  | 
|  | 211 | void NuPlayer::HTTPLiveSource::onSessionNotify(const sp<AMessage> &msg) { | 
|  | 212 | int32_t what; | 
|  | 213 | CHECK(msg->findInt32("what", &what)); | 
|  | 214 |  | 
|  | 215 | switch (what) { | 
|  | 216 | case LiveSession::kWhatPrepared: | 
|  | 217 | { | 
| Marco Nelissen | 3e518fd | 2013-11-01 10:33:18 -0700 | [diff] [blame] | 218 | // notify the current size here if we have it, otherwise report an initial size of (0,0) | 
|  | 219 | sp<AMessage> format = getFormat(false /* audio */); | 
|  | 220 | int32_t width; | 
|  | 221 | int32_t height; | 
|  | 222 | if (format != NULL && | 
|  | 223 | format->findInt32("width", &width) && format->findInt32("height", &height)) { | 
| Chong Zhang | ced1c2f | 2014-08-08 15:22:35 -0700 | [diff] [blame] | 224 | notifyVideoSizeChanged(format); | 
| Marco Nelissen | 3e518fd | 2013-11-01 10:33:18 -0700 | [diff] [blame] | 225 | } else { | 
| Chong Zhang | ced1c2f | 2014-08-08 15:22:35 -0700 | [diff] [blame] | 226 | notifyVideoSizeChanged(); | 
| Marco Nelissen | 3e518fd | 2013-11-01 10:33:18 -0700 | [diff] [blame] | 227 | } | 
| Andreas Huber | 0df36ec | 2013-02-06 10:44:39 -0800 | [diff] [blame] | 228 |  | 
|  | 229 | uint32_t flags = FLAG_CAN_PAUSE; | 
|  | 230 | if (mLiveSession->isSeekable()) { | 
|  | 231 | flags |= FLAG_CAN_SEEK; | 
|  | 232 | flags |= FLAG_CAN_SEEK_BACKWARD; | 
|  | 233 | flags |= FLAG_CAN_SEEK_FORWARD; | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | if (mLiveSession->hasDynamicDuration()) { | 
|  | 237 | flags |= FLAG_DYNAMIC_DURATION; | 
|  | 238 | } | 
|  | 239 |  | 
|  | 240 | notifyFlagsChanged(flags); | 
|  | 241 |  | 
|  | 242 | notifyPrepared(); | 
|  | 243 | break; | 
|  | 244 | } | 
|  | 245 |  | 
|  | 246 | case LiveSession::kWhatPreparationFailed: | 
|  | 247 | { | 
|  | 248 | status_t err; | 
|  | 249 | CHECK(msg->findInt32("err", &err)); | 
|  | 250 |  | 
|  | 251 | notifyPrepared(err); | 
|  | 252 | break; | 
|  | 253 | } | 
|  | 254 |  | 
| Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 255 | case LiveSession::kWhatStreamsChanged: | 
|  | 256 | { | 
|  | 257 | uint32_t changedMask; | 
|  | 258 | CHECK(msg->findInt32( | 
|  | 259 | "changedMask", (int32_t *)&changedMask)); | 
|  | 260 |  | 
|  | 261 | bool audio = changedMask & LiveSession::STREAMTYPE_AUDIO; | 
|  | 262 | bool video = changedMask & LiveSession::STREAMTYPE_VIDEO; | 
|  | 263 |  | 
|  | 264 | sp<AMessage> reply; | 
|  | 265 | CHECK(msg->findMessage("reply", &reply)); | 
|  | 266 |  | 
|  | 267 | sp<AMessage> notify = dupNotify(); | 
|  | 268 | notify->setInt32("what", kWhatQueueDecoderShutdown); | 
|  | 269 | notify->setInt32("audio", audio); | 
|  | 270 | notify->setInt32("video", video); | 
|  | 271 | notify->setMessage("reply", reply); | 
|  | 272 | notify->post(); | 
|  | 273 | break; | 
|  | 274 | } | 
|  | 275 |  | 
|  | 276 | case LiveSession::kWhatError: | 
|  | 277 | { | 
|  | 278 | break; | 
|  | 279 | } | 
|  | 280 |  | 
| Andreas Huber | 0df36ec | 2013-02-06 10:44:39 -0800 | [diff] [blame] | 281 | default: | 
|  | 282 | TRESPASS(); | 
|  | 283 | } | 
|  | 284 | } | 
|  | 285 |  | 
| Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 286 | }  // namespace android | 
|  | 287 |  |