RTSPSource.cpp revision 2bfdd428c56c7524d1a11979f200a1762866032d
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 "RTSPSource" 19#include <utils/Log.h> 20 21#include "RTSPSource.h" 22 23#include "AnotherPacketSource.h" 24#include "MyHandler.h" 25 26#include <media/stagefright/MetaData.h> 27 28namespace android { 29 30NuPlayer::RTSPSource::RTSPSource( 31 const char *url, 32 const KeyedVector<String8, String8> *headers, 33 bool uidValid, 34 uid_t uid) 35 : mURL(url), 36 mUIDValid(uidValid), 37 mUID(uid), 38 mFlags(0), 39 mState(DISCONNECTED), 40 mFinalResult(OK), 41 mDisconnectReplyID(0) { 42 if (headers) { 43 mExtraHeaders = *headers; 44 45 ssize_t index = 46 mExtraHeaders.indexOfKey(String8("x-hide-urls-from-log")); 47 48 if (index >= 0) { 49 mFlags |= kFlagIncognito; 50 51 mExtraHeaders.removeItemsAt(index); 52 } 53 } 54} 55 56NuPlayer::RTSPSource::~RTSPSource() { 57 if (mLooper != NULL) { 58 mLooper->stop(); 59 } 60} 61 62void NuPlayer::RTSPSource::start() { 63 if (mLooper == NULL) { 64 mLooper = new ALooper; 65 mLooper->setName("rtsp"); 66 mLooper->start(); 67 68 mReflector = new AHandlerReflector<RTSPSource>(this); 69 mLooper->registerHandler(mReflector); 70 } 71 72 CHECK(mHandler == NULL); 73 74 sp<AMessage> notify = new AMessage(kWhatNotify, mReflector->id()); 75 76 mHandler = new MyHandler(mURL.c_str(), notify, mUIDValid, mUID); 77 mLooper->registerHandler(mHandler); 78 79 CHECK_EQ(mState, (int)DISCONNECTED); 80 mState = CONNECTING; 81 82 mHandler->connect(); 83} 84 85void NuPlayer::RTSPSource::stop() { 86 sp<AMessage> msg = new AMessage(kWhatDisconnect, mReflector->id()); 87 88 sp<AMessage> dummy; 89 msg->postAndAwaitResponse(&dummy); 90} 91 92status_t NuPlayer::RTSPSource::feedMoreTSData() { 93 return mFinalResult; 94} 95 96sp<MetaData> NuPlayer::RTSPSource::getFormat(bool audio) { 97 sp<AnotherPacketSource> source = getSource(audio); 98 99 if (source == NULL) { 100 return NULL; 101 } 102 103 return source->getFormat(); 104} 105 106status_t NuPlayer::RTSPSource::dequeueAccessUnit( 107 bool audio, sp<ABuffer> *accessUnit) { 108 sp<AnotherPacketSource> source = getSource(audio); 109 110 if (source == NULL) { 111 return -EWOULDBLOCK; 112 } 113 114 status_t finalResult; 115 if (!source->hasBufferAvailable(&finalResult)) { 116 return finalResult == OK ? -EWOULDBLOCK : finalResult; 117 } 118 119 return source->dequeueAccessUnit(accessUnit); 120} 121 122sp<AnotherPacketSource> NuPlayer::RTSPSource::getSource(bool audio) { 123 return audio ? mAudioTrack : mVideoTrack; 124} 125 126status_t NuPlayer::RTSPSource::getDuration(int64_t *durationUs) { 127 *durationUs = 0ll; 128 129 int64_t audioDurationUs; 130 if (mAudioTrack != NULL 131 && mAudioTrack->getFormat()->findInt64( 132 kKeyDuration, &audioDurationUs) 133 && audioDurationUs > *durationUs) { 134 *durationUs = audioDurationUs; 135 } 136 137 int64_t videoDurationUs; 138 if (mVideoTrack != NULL 139 && mVideoTrack->getFormat()->findInt64( 140 kKeyDuration, &videoDurationUs) 141 && videoDurationUs > *durationUs) { 142 *durationUs = videoDurationUs; 143 } 144 145 return OK; 146} 147 148status_t NuPlayer::RTSPSource::seekTo(int64_t seekTimeUs) { 149 if (mState != CONNECTED) { 150 return UNKNOWN_ERROR; 151 } 152 153 mState = SEEKING; 154 mHandler->seek(seekTimeUs); 155 156 return OK; 157} 158 159bool NuPlayer::RTSPSource::isSeekable() { 160 return true; 161} 162 163void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) { 164 if (msg->what() == kWhatDisconnect) { 165 uint32_t replyID; 166 CHECK(msg->senderAwaitsResponse(&replyID)); 167 168 mDisconnectReplyID = replyID; 169 finishDisconnectIfPossible(); 170 return; 171 } 172 173 CHECK_EQ(msg->what(), (int)kWhatNotify); 174 175 int32_t what; 176 CHECK(msg->findInt32("what", &what)); 177 178 switch (what) { 179 case MyHandler::kWhatConnected: 180 onConnected(); 181 break; 182 183 case MyHandler::kWhatDisconnected: 184 onDisconnected(msg); 185 break; 186 187 case MyHandler::kWhatSeekDone: 188 { 189 mState = CONNECTED; 190 break; 191 } 192 193 case MyHandler::kWhatAccessUnit: 194 { 195 size_t trackIndex; 196 CHECK(msg->findSize("trackIndex", &trackIndex)); 197 CHECK_LT(trackIndex, mTracks.size()); 198 199 sp<RefBase> obj; 200 CHECK(msg->findObject("accessUnit", &obj)); 201 202 sp<ABuffer> accessUnit = static_cast<ABuffer *>(obj.get()); 203 204 int32_t damaged; 205 if (accessUnit->meta()->findInt32("damaged", &damaged) 206 && damaged) { 207 LOGI("dropping damaged access unit."); 208 break; 209 } 210 211 const TrackInfo &info = mTracks.editItemAt(trackIndex); 212 sp<AnotherPacketSource> source = info.mSource; 213 if (source != NULL) { 214#if 1 215 uint32_t rtpTime; 216 CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime)); 217 218 int64_t nptUs = 219 ((double)rtpTime - (double)info.mRTPTime) 220 / info.mTimeScale 221 * 1000000ll 222 + info.mNormalPlaytimeUs; 223 224 accessUnit->meta()->setInt64("timeUs", nptUs); 225#endif 226 227 source->queueAccessUnit(accessUnit); 228 } 229 break; 230 } 231 232 case MyHandler::kWhatEOS: 233 { 234 size_t trackIndex; 235 CHECK(msg->findSize("trackIndex", &trackIndex)); 236 CHECK_LT(trackIndex, mTracks.size()); 237 238 int32_t finalResult; 239 CHECK(msg->findInt32("finalResult", &finalResult)); 240 CHECK_NE(finalResult, (status_t)OK); 241 242 TrackInfo *info = &mTracks.editItemAt(trackIndex); 243 sp<AnotherPacketSource> source = info->mSource; 244 if (source != NULL) { 245 source->signalEOS(finalResult); 246 } 247 248 break; 249 } 250 251 case MyHandler::kWhatSeekDiscontinuity: 252 { 253 size_t trackIndex; 254 CHECK(msg->findSize("trackIndex", &trackIndex)); 255 CHECK_LT(trackIndex, mTracks.size()); 256 257 TrackInfo *info = &mTracks.editItemAt(trackIndex); 258 sp<AnotherPacketSource> source = info->mSource; 259 if (source != NULL) { 260 source->queueDiscontinuity(ATSParser::DISCONTINUITY_SEEK, NULL); 261 } 262 263 break; 264 } 265 266 case MyHandler::kWhatNormalPlayTimeMapping: 267 { 268 size_t trackIndex; 269 CHECK(msg->findSize("trackIndex", &trackIndex)); 270 CHECK_LT(trackIndex, mTracks.size()); 271 272 uint32_t rtpTime; 273 CHECK(msg->findInt32("rtpTime", (int32_t *)&rtpTime)); 274 275 int64_t nptUs; 276 CHECK(msg->findInt64("nptUs", &nptUs)); 277 278 TrackInfo *info = &mTracks.editItemAt(trackIndex); 279 info->mRTPTime = rtpTime; 280 info->mNormalPlaytimeUs = nptUs; 281 break; 282 } 283 284 default: 285 TRESPASS(); 286 } 287} 288 289void NuPlayer::RTSPSource::onConnected() { 290 CHECK(mAudioTrack == NULL); 291 CHECK(mVideoTrack == NULL); 292 293 size_t numTracks = mHandler->countTracks(); 294 for (size_t i = 0; i < numTracks; ++i) { 295 int32_t timeScale; 296 sp<MetaData> format = mHandler->getTrackFormat(i, &timeScale); 297 298 const char *mime; 299 CHECK(format->findCString(kKeyMIMEType, &mime)); 300 301 bool isAudio = !strncasecmp(mime, "audio/", 6); 302 bool isVideo = !strncasecmp(mime, "video/", 6); 303 304 TrackInfo info; 305 info.mTimeScale = timeScale; 306 info.mRTPTime = 0; 307 info.mNormalPlaytimeUs = 0ll; 308 309 if ((isAudio && mAudioTrack == NULL) 310 || (isVideo && mVideoTrack == NULL)) { 311 sp<AnotherPacketSource> source = new AnotherPacketSource(format); 312 313 if (isAudio) { 314 mAudioTrack = source; 315 } else { 316 mVideoTrack = source; 317 } 318 319 info.mSource = source; 320 } 321 322 mTracks.push(info); 323 } 324 325 mState = CONNECTED; 326} 327 328void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) { 329 status_t err; 330 CHECK(msg->findInt32("result", &err)); 331 CHECK_NE(err, (status_t)OK); 332 333 mLooper->unregisterHandler(mHandler->id()); 334 mHandler.clear(); 335 336 mState = DISCONNECTED; 337 mFinalResult = err; 338 339 if (mDisconnectReplyID != 0) { 340 finishDisconnectIfPossible(); 341 } 342} 343 344void NuPlayer::RTSPSource::finishDisconnectIfPossible() { 345 if (mState != DISCONNECTED) { 346 mHandler->disconnect(); 347 return; 348 } 349 350 (new AMessage)->postReply(mDisconnectReplyID); 351 mDisconnectReplyID = 0; 352} 353 354} // namespace android 355