| 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 |  | 
|  | 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 |  | 
|  | 33 | namespace android { | 
|  | 34 |  | 
| Andreas Huber | ad0d9c9 | 2011-04-19 11:50:27 -0700 | [diff] [blame] | 35 | NuPlayer::HTTPLiveSource::HTTPLiveSource( | 
| Andreas Huber | b5f25f0 | 2013-02-05 10:14:26 -0800 | [diff] [blame] | 36 | const sp<AMessage> ¬ify, | 
| Andreas Huber | ad0d9c9 | 2011-04-19 11:50:27 -0700 | [diff] [blame] | 37 | const char *url, | 
| Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 38 | const KeyedVector<String8, String8> *headers, | 
|  | 39 | bool uidValid, uid_t uid) | 
| Andreas Huber | b5f25f0 | 2013-02-05 10:14:26 -0800 | [diff] [blame] | 40 | : Source(notify), | 
|  | 41 | mURL(url), | 
| Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 42 | mUIDValid(uidValid), | 
|  | 43 | mUID(uid), | 
| 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), | 
| Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 46 | mOffset(0) { | 
| Andreas Huber | ad0d9c9 | 2011-04-19 11:50:27 -0700 | [diff] [blame] | 47 | 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 Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 59 | } | 
|  | 60 |  | 
|  | 61 | NuPlayer::HTTPLiveSource::~HTTPLiveSource() { | 
| Andreas Huber | 2048d0c | 2011-07-15 16:25:41 -0700 | [diff] [blame] | 62 | if (mLiveSession != NULL) { | 
|  | 63 | mLiveSession->disconnect(); | 
| Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 64 | mLiveSession.clear(); | 
|  | 65 |  | 
| Andreas Huber | 2048d0c | 2011-07-15 16:25:41 -0700 | [diff] [blame] | 66 | mLiveLooper->stop(); | 
| Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 67 | mLiveLooper.clear(); | 
| Andreas Huber | 2048d0c | 2011-07-15 16:25:41 -0700 | [diff] [blame] | 68 | } | 
| Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 69 | } | 
|  | 70 |  | 
| Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 71 | void NuPlayer::HTTPLiveSource::prepareAsync() { | 
| Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 72 | mLiveLooper = new ALooper; | 
|  | 73 | mLiveLooper->setName("http live"); | 
|  | 74 | mLiveLooper->start(); | 
|  | 75 |  | 
| Andreas Huber | 0df36ec | 2013-02-06 10:44:39 -0800 | [diff] [blame] | 76 | sp<AMessage> notify = new AMessage(kWhatSessionNotify, id()); | 
|  | 77 |  | 
| Andreas Huber | 7314fa1 | 2011-02-24 14:42:48 -0800 | [diff] [blame] | 78 | mLiveSession = new LiveSession( | 
| Andreas Huber | 0df36ec | 2013-02-06 10:44:39 -0800 | [diff] [blame] | 79 | notify, | 
| Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 80 | (mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0, | 
| Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 81 | mUIDValid, | 
|  | 82 | mUID); | 
| 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 |  | 
|  | 123 | status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) { | 
| Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 124 | return mLiveSession->seekTo(seekTimeUs); | 
| Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 125 | } | 
|  | 126 |  | 
| Andreas Huber | 0df36ec | 2013-02-06 10:44:39 -0800 | [diff] [blame] | 127 | void 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 |  | 
|  | 141 | void 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 Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 176 | 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 Huber | 0df36ec | 2013-02-06 10:44:39 -0800 | [diff] [blame] | 202 | default: | 
|  | 203 | TRESPASS(); | 
|  | 204 | } | 
|  | 205 | } | 
|  | 206 |  | 
| Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 207 | }  // namespace android | 
|  | 208 |  |