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