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