blob: 3385a197803f4322555f50a3a10a3cb44edbd4a1 [file] [log] [blame]
Andreas Huber2bfdd422011-10-11 15:24:07 -07001/*
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"
Oscar Rydhé81dd60e2012-02-20 10:15:48 +010025#include "SDPLoader.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070026
Andreas Huber49694682012-08-31 10:27:46 -070027#include <media/stagefright/MediaDefs.h>
Andreas Huber2bfdd422011-10-11 15:24:07 -070028#include <media/stagefright/MetaData.h>
29
30namespace android {
31
Roger Jönssoncfc30832013-01-21 16:26:41 +010032const int64_t kNearEOSTimeoutUs = 2000000ll; // 2 secs
33
Andreas Huber2bfdd422011-10-11 15:24:07 -070034NuPlayer::RTSPSource::RTSPSource(
Andreas Huber5ab368a2013-02-05 10:14:26 -080035 const sp<AMessage> &notify,
Andreas Huber2bfdd422011-10-11 15:24:07 -070036 const char *url,
37 const KeyedVector<String8, String8> *headers,
38 bool uidValid,
Oscar Rydhé81dd60e2012-02-20 10:15:48 +010039 uid_t uid,
40 bool isSDP)
Andreas Huber5ab368a2013-02-05 10:14:26 -080041 : Source(notify),
42 mURL(url),
Andreas Huber2bfdd422011-10-11 15:24:07 -070043 mUIDValid(uidValid),
44 mUID(uid),
45 mFlags(0),
Oscar Rydhé81dd60e2012-02-20 10:15:48 +010046 mIsSDP(isSDP),
Andreas Huber2bfdd422011-10-11 15:24:07 -070047 mState(DISCONNECTED),
48 mFinalResult(OK),
Andreas Huberee736e92011-12-08 13:04:50 -080049 mDisconnectReplyID(0),
Roger Jönssoncfc30832013-01-21 16:26:41 +010050 mBuffering(true),
51 mSeekGeneration(0),
52 mEOSTimeoutAudio(0),
53 mEOSTimeoutVideo(0) {
Andreas Huber2bfdd422011-10-11 15:24:07 -070054 if (headers) {
55 mExtraHeaders = *headers;
56
57 ssize_t index =
58 mExtraHeaders.indexOfKey(String8("x-hide-urls-from-log"));
59
60 if (index >= 0) {
61 mFlags |= kFlagIncognito;
62
63 mExtraHeaders.removeItemsAt(index);
64 }
65 }
66}
67
68NuPlayer::RTSPSource::~RTSPSource() {
Andreas Huber602f5bb2013-04-15 16:18:56 -070069 if (mLooper != NULL) {
70 mLooper->stop();
71 }
Andreas Huber2bfdd422011-10-11 15:24:07 -070072}
73
Andreas Huber57cea552013-02-05 13:59:56 -080074void NuPlayer::RTSPSource::prepareAsync() {
Andreas Huber2bfdd422011-10-11 15:24:07 -070075 if (mLooper == NULL) {
76 mLooper = new ALooper;
77 mLooper->setName("rtsp");
78 mLooper->start();
79
80 mReflector = new AHandlerReflector<RTSPSource>(this);
81 mLooper->registerHandler(mReflector);
82 }
83
84 CHECK(mHandler == NULL);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +010085 CHECK(mSDPLoader == NULL);
Andreas Huber2bfdd422011-10-11 15:24:07 -070086
87 sp<AMessage> notify = new AMessage(kWhatNotify, mReflector->id());
88
Andreas Huber2bfdd422011-10-11 15:24:07 -070089 CHECK_EQ(mState, (int)DISCONNECTED);
90 mState = CONNECTING;
91
Oscar Rydhé81dd60e2012-02-20 10:15:48 +010092 if (mIsSDP) {
93 mSDPLoader = new SDPLoader(notify,
94 (mFlags & kFlagIncognito) ? SDPLoader::kFlagIncognito : 0,
95 mUIDValid, mUID);
96
Andreas Huber57cea552013-02-05 13:59:56 -080097 mSDPLoader->load(
98 mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +010099 } else {
100 mHandler = new MyHandler(mURL.c_str(), notify, mUIDValid, mUID);
101 mLooper->registerHandler(mHandler);
102
103 mHandler->connect();
104 }
Roger Jönssoncfc30832013-01-21 16:26:41 +0100105
106 sp<AMessage> notifyStart = dupNotify();
107 notifyStart->setInt32("what", kWhatBufferingStart);
108 notifyStart->post();
Andreas Huber57cea552013-02-05 13:59:56 -0800109}
110
111void NuPlayer::RTSPSource::start() {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700112}
113
114void NuPlayer::RTSPSource::stop() {
James Dong58341812012-11-16 14:31:15 -0800115 if (mLooper == NULL) {
116 return;
117 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700118 sp<AMessage> msg = new AMessage(kWhatDisconnect, mReflector->id());
119
120 sp<AMessage> dummy;
121 msg->postAndAwaitResponse(&dummy);
122}
123
Roger Jönsson46d13e32013-01-21 17:15:45 +0100124void NuPlayer::RTSPSource::pause() {
125 int64_t mediaDurationUs = 0;
126 getDuration(&mediaDurationUs);
127 for (size_t index = 0; index < mTracks.size(); index++) {
128 TrackInfo *info = &mTracks.editItemAt(index);
129 sp<AnotherPacketSource> source = info->mSource;
130
131 // Check if EOS or ERROR is received
132 if (source != NULL && source->isFinished(mediaDurationUs)) {
133 return;
134 }
135 }
136 mHandler->pause();
137}
138
139void NuPlayer::RTSPSource::resume() {
140 mHandler->resume();
141}
142
Andreas Huber2bfdd422011-10-11 15:24:07 -0700143status_t NuPlayer::RTSPSource::feedMoreTSData() {
144 return mFinalResult;
145}
146
Andreas Huber84066782011-08-16 09:34:26 -0700147sp<MetaData> NuPlayer::RTSPSource::getFormatMeta(bool audio) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700148 sp<AnotherPacketSource> source = getSource(audio);
149
150 if (source == NULL) {
151 return NULL;
152 }
153
154 return source->getFormat();
155}
156
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700157bool NuPlayer::RTSPSource::haveSufficientDataOnAllTracks() {
158 // We're going to buffer at least 2 secs worth data on all tracks before
159 // starting playback (both at startup and after a seek).
160
161 static const int64_t kMinDurationUs = 2000000ll;
162
Roger Jönssoncfc30832013-01-21 16:26:41 +0100163 int64_t mediaDurationUs = 0;
164 getDuration(&mediaDurationUs);
165 if ((mAudioTrack != NULL && mAudioTrack->isFinished(mediaDurationUs))
166 || (mVideoTrack != NULL && mVideoTrack->isFinished(mediaDurationUs))) {
167 return true;
168 }
169
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700170 status_t err;
171 int64_t durationUs;
172 if (mAudioTrack != NULL
173 && (durationUs = mAudioTrack->getBufferedDurationUs(&err))
174 < kMinDurationUs
175 && err == OK) {
176 ALOGV("audio track doesn't have enough data yet. (%.2f secs buffered)",
177 durationUs / 1E6);
178 return false;
179 }
180
181 if (mVideoTrack != NULL
182 && (durationUs = mVideoTrack->getBufferedDurationUs(&err))
183 < kMinDurationUs
184 && err == OK) {
185 ALOGV("video track doesn't have enough data yet. (%.2f secs buffered)",
186 durationUs / 1E6);
187 return false;
188 }
189
190 return true;
191}
192
Andreas Huber2bfdd422011-10-11 15:24:07 -0700193status_t NuPlayer::RTSPSource::dequeueAccessUnit(
194 bool audio, sp<ABuffer> *accessUnit) {
Roger Jönssoncfc30832013-01-21 16:26:41 +0100195 if (mBuffering) {
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700196 if (!haveSufficientDataOnAllTracks()) {
197 return -EWOULDBLOCK;
198 }
199
Roger Jönssoncfc30832013-01-21 16:26:41 +0100200 mBuffering = false;
201
202 sp<AMessage> notify = dupNotify();
203 notify->setInt32("what", kWhatBufferingEnd);
204 notify->post();
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700205 }
206
Andreas Huber2bfdd422011-10-11 15:24:07 -0700207 sp<AnotherPacketSource> source = getSource(audio);
208
209 if (source == NULL) {
210 return -EWOULDBLOCK;
211 }
212
213 status_t finalResult;
214 if (!source->hasBufferAvailable(&finalResult)) {
Roger Jönssoncfc30832013-01-21 16:26:41 +0100215 if (finalResult == OK) {
216 int64_t mediaDurationUs = 0;
217 getDuration(&mediaDurationUs);
218 sp<AnotherPacketSource> otherSource = getSource(!audio);
219 status_t otherFinalResult;
220
221 // If other source already signaled EOS, this source should also signal EOS
222 if (otherSource != NULL &&
223 !otherSource->hasBufferAvailable(&otherFinalResult) &&
224 otherFinalResult == ERROR_END_OF_STREAM) {
225 source->signalEOS(ERROR_END_OF_STREAM);
226 return ERROR_END_OF_STREAM;
227 }
228
229 // If this source has detected near end, give it some time to retrieve more
230 // data before signaling EOS
231 if (source->isFinished(mediaDurationUs)) {
232 int64_t eosTimeout = audio ? mEOSTimeoutAudio : mEOSTimeoutVideo;
233 if (eosTimeout == 0) {
234 setEOSTimeout(audio, ALooper::GetNowUs());
235 } else if ((ALooper::GetNowUs() - eosTimeout) > kNearEOSTimeoutUs) {
236 setEOSTimeout(audio, 0);
237 source->signalEOS(ERROR_END_OF_STREAM);
238 return ERROR_END_OF_STREAM;
239 }
240 return -EWOULDBLOCK;
241 }
242
243 if (!(otherSource != NULL && otherSource->isFinished(mediaDurationUs))) {
244 // We should not enter buffering mode
245 // if any of the sources already have detected EOS.
246 mBuffering = true;
247
248 sp<AMessage> notify = dupNotify();
249 notify->setInt32("what", kWhatBufferingStart);
250 notify->post();
251 }
252
253 return -EWOULDBLOCK;
254 }
255 return finalResult;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700256 }
257
Roger Jönssoncfc30832013-01-21 16:26:41 +0100258 setEOSTimeout(audio, 0);
259
Andreas Huber2bfdd422011-10-11 15:24:07 -0700260 return source->dequeueAccessUnit(accessUnit);
261}
262
263sp<AnotherPacketSource> NuPlayer::RTSPSource::getSource(bool audio) {
Andreas Huber49694682012-08-31 10:27:46 -0700264 if (mTSParser != NULL) {
265 sp<MediaSource> source = mTSParser->getSource(
266 audio ? ATSParser::AUDIO : ATSParser::VIDEO);
267
268 return static_cast<AnotherPacketSource *>(source.get());
269 }
270
Andreas Huber2bfdd422011-10-11 15:24:07 -0700271 return audio ? mAudioTrack : mVideoTrack;
272}
273
Roger Jönssoncfc30832013-01-21 16:26:41 +0100274void NuPlayer::RTSPSource::setEOSTimeout(bool audio, int64_t timeout) {
275 if (audio) {
276 mEOSTimeoutAudio = timeout;
277 } else {
278 mEOSTimeoutVideo = timeout;
279 }
280}
281
Andreas Huber2bfdd422011-10-11 15:24:07 -0700282status_t NuPlayer::RTSPSource::getDuration(int64_t *durationUs) {
283 *durationUs = 0ll;
284
285 int64_t audioDurationUs;
286 if (mAudioTrack != NULL
287 && mAudioTrack->getFormat()->findInt64(
288 kKeyDuration, &audioDurationUs)
289 && audioDurationUs > *durationUs) {
290 *durationUs = audioDurationUs;
291 }
292
293 int64_t videoDurationUs;
294 if (mVideoTrack != NULL
295 && mVideoTrack->getFormat()->findInt64(
296 kKeyDuration, &videoDurationUs)
297 && videoDurationUs > *durationUs) {
298 *durationUs = videoDurationUs;
299 }
300
301 return OK;
302}
303
304status_t NuPlayer::RTSPSource::seekTo(int64_t seekTimeUs) {
Andreas Huberee736e92011-12-08 13:04:50 -0800305 sp<AMessage> msg = new AMessage(kWhatPerformSeek, mReflector->id());
306 msg->setInt32("generation", ++mSeekGeneration);
307 msg->setInt64("timeUs", seekTimeUs);
308 msg->post(200000ll);
309
310 return OK;
311}
312
313void NuPlayer::RTSPSource::performSeek(int64_t seekTimeUs) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700314 if (mState != CONNECTED) {
Andreas Huberee736e92011-12-08 13:04:50 -0800315 return;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700316 }
317
318 mState = SEEKING;
319 mHandler->seek(seekTimeUs);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700320}
321
Andreas Huber2bfdd422011-10-11 15:24:07 -0700322void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) {
323 if (msg->what() == kWhatDisconnect) {
324 uint32_t replyID;
325 CHECK(msg->senderAwaitsResponse(&replyID));
326
327 mDisconnectReplyID = replyID;
328 finishDisconnectIfPossible();
329 return;
Andreas Huberee736e92011-12-08 13:04:50 -0800330 } else if (msg->what() == kWhatPerformSeek) {
331 int32_t generation;
332 CHECK(msg->findInt32("generation", &generation));
333
334 if (generation != mSeekGeneration) {
335 // obsolete.
336 return;
337 }
338
339 int64_t seekTimeUs;
340 CHECK(msg->findInt64("timeUs", &seekTimeUs));
341
342 performSeek(seekTimeUs);
343 return;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700344 }
345
346 CHECK_EQ(msg->what(), (int)kWhatNotify);
347
348 int32_t what;
349 CHECK(msg->findInt32("what", &what));
350
351 switch (what) {
352 case MyHandler::kWhatConnected:
Andreas Huber7f475c32013-02-05 14:47:13 -0800353 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700354 onConnected();
Andreas Huber7f475c32013-02-05 14:47:13 -0800355
356 notifyVideoSizeChanged(0, 0);
357
358 uint32_t flags = 0;
359
360 if (mHandler->isSeekable()) {
361 flags = FLAG_CAN_PAUSE | FLAG_CAN_SEEK;
362
363 // Seeking 10secs forward or backward is a very expensive
364 // operation for rtsp, so let's not enable that.
365 // The user can always use the seek bar.
366 }
367
368 notifyFlagsChanged(flags);
369 notifyPrepared();
Andreas Huber2bfdd422011-10-11 15:24:07 -0700370 break;
Andreas Huber7f475c32013-02-05 14:47:13 -0800371 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700372
373 case MyHandler::kWhatDisconnected:
Andreas Huber7f475c32013-02-05 14:47:13 -0800374 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700375 onDisconnected(msg);
376 break;
Andreas Huber7f475c32013-02-05 14:47:13 -0800377 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700378
379 case MyHandler::kWhatSeekDone:
380 {
381 mState = CONNECTED;
382 break;
383 }
384
385 case MyHandler::kWhatAccessUnit:
386 {
387 size_t trackIndex;
388 CHECK(msg->findSize("trackIndex", &trackIndex));
Andreas Huber49694682012-08-31 10:27:46 -0700389
390 if (mTSParser == NULL) {
391 CHECK_LT(trackIndex, mTracks.size());
392 } else {
393 CHECK_EQ(trackIndex, 0u);
394 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700395
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800396 sp<ABuffer> accessUnit;
397 CHECK(msg->findBuffer("accessUnit", &accessUnit));
Andreas Huber2bfdd422011-10-11 15:24:07 -0700398
399 int32_t damaged;
400 if (accessUnit->meta()->findInt32("damaged", &damaged)
401 && damaged) {
Steve Blockdf64d152012-01-04 20:05:49 +0000402 ALOGI("dropping damaged access unit.");
Andreas Huber2bfdd422011-10-11 15:24:07 -0700403 break;
404 }
405
Andreas Huber49694682012-08-31 10:27:46 -0700406 if (mTSParser != NULL) {
407 size_t offset = 0;
408 status_t err = OK;
409 while (offset + 188 <= accessUnit->size()) {
410 err = mTSParser->feedTSPacket(
411 accessUnit->data() + offset, 188);
412 if (err != OK) {
413 break;
414 }
415
416 offset += 188;
417 }
418
419 if (offset < accessUnit->size()) {
420 err = ERROR_MALFORMED;
421 }
422
423 if (err != OK) {
424 sp<AnotherPacketSource> source = getSource(false /* audio */);
425 if (source != NULL) {
426 source->signalEOS(err);
427 }
428
429 source = getSource(true /* audio */);
430 if (source != NULL) {
431 source->signalEOS(err);
432 }
433 }
434 break;
435 }
436
Andreas Huber1906e5c2011-12-08 12:27:47 -0800437 TrackInfo *info = &mTracks.editItemAt(trackIndex);
438
439 sp<AnotherPacketSource> source = info->mSource;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700440 if (source != NULL) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700441 uint32_t rtpTime;
442 CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
443
Andreas Huber1906e5c2011-12-08 12:27:47 -0800444 if (!info->mNPTMappingValid) {
445 // This is a live stream, we didn't receive any normal
Andreas Huberc9d16962012-05-21 11:12:40 -0700446 // playtime mapping. We won't map to npt time.
447 source->queueAccessUnit(accessUnit);
448 break;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800449 }
450
Andreas Huber2bfdd422011-10-11 15:24:07 -0700451 int64_t nptUs =
Andreas Huber1906e5c2011-12-08 12:27:47 -0800452 ((double)rtpTime - (double)info->mRTPTime)
453 / info->mTimeScale
Andreas Huber2bfdd422011-10-11 15:24:07 -0700454 * 1000000ll
Andreas Huber1906e5c2011-12-08 12:27:47 -0800455 + info->mNormalPlaytimeUs;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700456
457 accessUnit->meta()->setInt64("timeUs", nptUs);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700458
459 source->queueAccessUnit(accessUnit);
460 }
461 break;
462 }
463
464 case MyHandler::kWhatEOS:
465 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700466 int32_t finalResult;
467 CHECK(msg->findInt32("finalResult", &finalResult));
468 CHECK_NE(finalResult, (status_t)OK);
469
Andreas Huber49694682012-08-31 10:27:46 -0700470 if (mTSParser != NULL) {
471 sp<AnotherPacketSource> source = getSource(false /* audio */);
472 if (source != NULL) {
473 source->signalEOS(finalResult);
474 }
475
476 source = getSource(true /* audio */);
477 if (source != NULL) {
478 source->signalEOS(finalResult);
479 }
480
481 return;
482 }
483
484 size_t trackIndex;
485 CHECK(msg->findSize("trackIndex", &trackIndex));
486 CHECK_LT(trackIndex, mTracks.size());
487
Andreas Huber2bfdd422011-10-11 15:24:07 -0700488 TrackInfo *info = &mTracks.editItemAt(trackIndex);
489 sp<AnotherPacketSource> source = info->mSource;
490 if (source != NULL) {
491 source->signalEOS(finalResult);
492 }
493
494 break;
495 }
496
497 case MyHandler::kWhatSeekDiscontinuity:
498 {
499 size_t trackIndex;
500 CHECK(msg->findSize("trackIndex", &trackIndex));
501 CHECK_LT(trackIndex, mTracks.size());
502
503 TrackInfo *info = &mTracks.editItemAt(trackIndex);
504 sp<AnotherPacketSource> source = info->mSource;
505 if (source != NULL) {
506 source->queueDiscontinuity(ATSParser::DISCONTINUITY_SEEK, NULL);
507 }
508
509 break;
510 }
511
512 case MyHandler::kWhatNormalPlayTimeMapping:
513 {
514 size_t trackIndex;
515 CHECK(msg->findSize("trackIndex", &trackIndex));
516 CHECK_LT(trackIndex, mTracks.size());
517
518 uint32_t rtpTime;
519 CHECK(msg->findInt32("rtpTime", (int32_t *)&rtpTime));
520
521 int64_t nptUs;
522 CHECK(msg->findInt64("nptUs", &nptUs));
523
524 TrackInfo *info = &mTracks.editItemAt(trackIndex);
525 info->mRTPTime = rtpTime;
526 info->mNormalPlaytimeUs = nptUs;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800527 info->mNPTMappingValid = true;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700528 break;
529 }
530
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100531 case SDPLoader::kWhatSDPLoaded:
532 {
533 onSDPLoaded(msg);
534 break;
535 }
536
Andreas Huber2bfdd422011-10-11 15:24:07 -0700537 default:
538 TRESPASS();
539 }
540}
541
542void NuPlayer::RTSPSource::onConnected() {
543 CHECK(mAudioTrack == NULL);
544 CHECK(mVideoTrack == NULL);
545
546 size_t numTracks = mHandler->countTracks();
547 for (size_t i = 0; i < numTracks; ++i) {
548 int32_t timeScale;
549 sp<MetaData> format = mHandler->getTrackFormat(i, &timeScale);
550
551 const char *mime;
552 CHECK(format->findCString(kKeyMIMEType, &mime));
553
Andreas Huber49694682012-08-31 10:27:46 -0700554 if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) {
555 // Very special case for MPEG2 Transport Streams.
556 CHECK_EQ(numTracks, 1u);
557
558 mTSParser = new ATSParser;
559 return;
560 }
561
Andreas Huber2bfdd422011-10-11 15:24:07 -0700562 bool isAudio = !strncasecmp(mime, "audio/", 6);
563 bool isVideo = !strncasecmp(mime, "video/", 6);
564
565 TrackInfo info;
566 info.mTimeScale = timeScale;
567 info.mRTPTime = 0;
568 info.mNormalPlaytimeUs = 0ll;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800569 info.mNPTMappingValid = false;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700570
571 if ((isAudio && mAudioTrack == NULL)
572 || (isVideo && mVideoTrack == NULL)) {
573 sp<AnotherPacketSource> source = new AnotherPacketSource(format);
574
575 if (isAudio) {
576 mAudioTrack = source;
577 } else {
578 mVideoTrack = source;
579 }
580
581 info.mSource = source;
582 }
583
584 mTracks.push(info);
585 }
586
587 mState = CONNECTED;
588}
589
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100590void NuPlayer::RTSPSource::onSDPLoaded(const sp<AMessage> &msg) {
591 status_t err;
592 CHECK(msg->findInt32("result", &err));
593
594 mSDPLoader.clear();
595
596 if (mDisconnectReplyID != 0) {
597 err = UNKNOWN_ERROR;
598 }
599
600 if (err == OK) {
601 sp<ASessionDescription> desc;
602 sp<RefBase> obj;
603 CHECK(msg->findObject("description", &obj));
604 desc = static_cast<ASessionDescription *>(obj.get());
605
606 AString rtspUri;
607 if (!desc->findAttribute(0, "a=control", &rtspUri)) {
608 ALOGE("Unable to find url in SDP");
609 err = UNKNOWN_ERROR;
610 } else {
611 sp<AMessage> notify = new AMessage(kWhatNotify, mReflector->id());
612
613 mHandler = new MyHandler(rtspUri.c_str(), notify, mUIDValid, mUID);
614 mLooper->registerHandler(mHandler);
615
616 mHandler->loadSDP(desc);
617 }
618 }
619
620 if (err != OK) {
Andreas Huber7f475c32013-02-05 14:47:13 -0800621 if (mState == CONNECTING) {
622 // We're still in the preparation phase, signal that it
623 // failed.
624 notifyPrepared(err);
625 }
626
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100627 mState = DISCONNECTED;
628 mFinalResult = err;
629
630 if (mDisconnectReplyID != 0) {
631 finishDisconnectIfPossible();
632 }
633 }
634}
635
Andreas Huber2bfdd422011-10-11 15:24:07 -0700636void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) {
Fredrik Rosin0ad03bc2013-03-06 13:42:53 +0100637 if (mState == DISCONNECTED) {
638 return;
639 }
640
Andreas Huber2bfdd422011-10-11 15:24:07 -0700641 status_t err;
642 CHECK(msg->findInt32("result", &err));
643 CHECK_NE(err, (status_t)OK);
644
645 mLooper->unregisterHandler(mHandler->id());
646 mHandler.clear();
647
Andreas Huber7f475c32013-02-05 14:47:13 -0800648 if (mState == CONNECTING) {
649 // We're still in the preparation phase, signal that it
650 // failed.
651 notifyPrepared(err);
652 }
653
Andreas Huber2bfdd422011-10-11 15:24:07 -0700654 mState = DISCONNECTED;
655 mFinalResult = err;
656
657 if (mDisconnectReplyID != 0) {
658 finishDisconnectIfPossible();
659 }
660}
661
662void NuPlayer::RTSPSource::finishDisconnectIfPossible() {
663 if (mState != DISCONNECTED) {
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100664 if (mHandler != NULL) {
665 mHandler->disconnect();
666 } else if (mSDPLoader != NULL) {
667 mSDPLoader->cancel();
668 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700669 return;
670 }
671
672 (new AMessage)->postReply(mDisconnectReplyID);
673 mDisconnectReplyID = 0;
674}
675
676} // namespace android