blob: 18cf6d14e03de9466ee30c00ac9a33f149eda5ca [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()) {
Chong Zhang4b7069d2013-09-11 12:52:43 -0700361 flags = FLAG_CAN_PAUSE
362 | FLAG_CAN_SEEK
363 | FLAG_CAN_SEEK_BACKWARD
364 | FLAG_CAN_SEEK_FORWARD;
Andreas Huber7f475c32013-02-05 14:47:13 -0800365 }
366
367 notifyFlagsChanged(flags);
368 notifyPrepared();
Andreas Huber2bfdd422011-10-11 15:24:07 -0700369 break;
Andreas Huber7f475c32013-02-05 14:47:13 -0800370 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700371
372 case MyHandler::kWhatDisconnected:
Andreas Huber7f475c32013-02-05 14:47:13 -0800373 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700374 onDisconnected(msg);
375 break;
Andreas Huber7f475c32013-02-05 14:47:13 -0800376 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700377
378 case MyHandler::kWhatSeekDone:
379 {
380 mState = CONNECTED;
381 break;
382 }
383
384 case MyHandler::kWhatAccessUnit:
385 {
386 size_t trackIndex;
387 CHECK(msg->findSize("trackIndex", &trackIndex));
Andreas Huber49694682012-08-31 10:27:46 -0700388
389 if (mTSParser == NULL) {
390 CHECK_LT(trackIndex, mTracks.size());
391 } else {
392 CHECK_EQ(trackIndex, 0u);
393 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700394
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800395 sp<ABuffer> accessUnit;
396 CHECK(msg->findBuffer("accessUnit", &accessUnit));
Andreas Huber2bfdd422011-10-11 15:24:07 -0700397
398 int32_t damaged;
399 if (accessUnit->meta()->findInt32("damaged", &damaged)
400 && damaged) {
Steve Blockdf64d152012-01-04 20:05:49 +0000401 ALOGI("dropping damaged access unit.");
Andreas Huber2bfdd422011-10-11 15:24:07 -0700402 break;
403 }
404
Andreas Huber49694682012-08-31 10:27:46 -0700405 if (mTSParser != NULL) {
406 size_t offset = 0;
407 status_t err = OK;
408 while (offset + 188 <= accessUnit->size()) {
409 err = mTSParser->feedTSPacket(
410 accessUnit->data() + offset, 188);
411 if (err != OK) {
412 break;
413 }
414
415 offset += 188;
416 }
417
418 if (offset < accessUnit->size()) {
419 err = ERROR_MALFORMED;
420 }
421
422 if (err != OK) {
423 sp<AnotherPacketSource> source = getSource(false /* audio */);
424 if (source != NULL) {
425 source->signalEOS(err);
426 }
427
428 source = getSource(true /* audio */);
429 if (source != NULL) {
430 source->signalEOS(err);
431 }
432 }
433 break;
434 }
435
Andreas Huber1906e5c2011-12-08 12:27:47 -0800436 TrackInfo *info = &mTracks.editItemAt(trackIndex);
437
438 sp<AnotherPacketSource> source = info->mSource;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700439 if (source != NULL) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700440 uint32_t rtpTime;
441 CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
442
Andreas Huber1906e5c2011-12-08 12:27:47 -0800443 if (!info->mNPTMappingValid) {
444 // This is a live stream, we didn't receive any normal
Andreas Huberc9d16962012-05-21 11:12:40 -0700445 // playtime mapping. We won't map to npt time.
446 source->queueAccessUnit(accessUnit);
447 break;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800448 }
449
Andreas Huber2bfdd422011-10-11 15:24:07 -0700450 int64_t nptUs =
Andreas Huber1906e5c2011-12-08 12:27:47 -0800451 ((double)rtpTime - (double)info->mRTPTime)
452 / info->mTimeScale
Andreas Huber2bfdd422011-10-11 15:24:07 -0700453 * 1000000ll
Andreas Huber1906e5c2011-12-08 12:27:47 -0800454 + info->mNormalPlaytimeUs;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700455
456 accessUnit->meta()->setInt64("timeUs", nptUs);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700457
458 source->queueAccessUnit(accessUnit);
459 }
460 break;
461 }
462
463 case MyHandler::kWhatEOS:
464 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700465 int32_t finalResult;
466 CHECK(msg->findInt32("finalResult", &finalResult));
467 CHECK_NE(finalResult, (status_t)OK);
468
Andreas Huber49694682012-08-31 10:27:46 -0700469 if (mTSParser != NULL) {
470 sp<AnotherPacketSource> source = getSource(false /* audio */);
471 if (source != NULL) {
472 source->signalEOS(finalResult);
473 }
474
475 source = getSource(true /* audio */);
476 if (source != NULL) {
477 source->signalEOS(finalResult);
478 }
479
480 return;
481 }
482
483 size_t trackIndex;
484 CHECK(msg->findSize("trackIndex", &trackIndex));
485 CHECK_LT(trackIndex, mTracks.size());
486
Andreas Huber2bfdd422011-10-11 15:24:07 -0700487 TrackInfo *info = &mTracks.editItemAt(trackIndex);
488 sp<AnotherPacketSource> source = info->mSource;
489 if (source != NULL) {
490 source->signalEOS(finalResult);
491 }
492
493 break;
494 }
495
496 case MyHandler::kWhatSeekDiscontinuity:
497 {
498 size_t trackIndex;
499 CHECK(msg->findSize("trackIndex", &trackIndex));
500 CHECK_LT(trackIndex, mTracks.size());
501
502 TrackInfo *info = &mTracks.editItemAt(trackIndex);
503 sp<AnotherPacketSource> source = info->mSource;
504 if (source != NULL) {
505 source->queueDiscontinuity(ATSParser::DISCONTINUITY_SEEK, NULL);
506 }
507
508 break;
509 }
510
511 case MyHandler::kWhatNormalPlayTimeMapping:
512 {
513 size_t trackIndex;
514 CHECK(msg->findSize("trackIndex", &trackIndex));
515 CHECK_LT(trackIndex, mTracks.size());
516
517 uint32_t rtpTime;
518 CHECK(msg->findInt32("rtpTime", (int32_t *)&rtpTime));
519
520 int64_t nptUs;
521 CHECK(msg->findInt64("nptUs", &nptUs));
522
523 TrackInfo *info = &mTracks.editItemAt(trackIndex);
524 info->mRTPTime = rtpTime;
525 info->mNormalPlaytimeUs = nptUs;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800526 info->mNPTMappingValid = true;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700527 break;
528 }
529
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100530 case SDPLoader::kWhatSDPLoaded:
531 {
532 onSDPLoaded(msg);
533 break;
534 }
535
Andreas Huber2bfdd422011-10-11 15:24:07 -0700536 default:
537 TRESPASS();
538 }
539}
540
541void NuPlayer::RTSPSource::onConnected() {
542 CHECK(mAudioTrack == NULL);
543 CHECK(mVideoTrack == NULL);
544
545 size_t numTracks = mHandler->countTracks();
546 for (size_t i = 0; i < numTracks; ++i) {
547 int32_t timeScale;
548 sp<MetaData> format = mHandler->getTrackFormat(i, &timeScale);
549
550 const char *mime;
551 CHECK(format->findCString(kKeyMIMEType, &mime));
552
Andreas Huber49694682012-08-31 10:27:46 -0700553 if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) {
554 // Very special case for MPEG2 Transport Streams.
555 CHECK_EQ(numTracks, 1u);
556
557 mTSParser = new ATSParser;
558 return;
559 }
560
Andreas Huber2bfdd422011-10-11 15:24:07 -0700561 bool isAudio = !strncasecmp(mime, "audio/", 6);
562 bool isVideo = !strncasecmp(mime, "video/", 6);
563
564 TrackInfo info;
565 info.mTimeScale = timeScale;
566 info.mRTPTime = 0;
567 info.mNormalPlaytimeUs = 0ll;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800568 info.mNPTMappingValid = false;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700569
570 if ((isAudio && mAudioTrack == NULL)
571 || (isVideo && mVideoTrack == NULL)) {
572 sp<AnotherPacketSource> source = new AnotherPacketSource(format);
573
574 if (isAudio) {
575 mAudioTrack = source;
576 } else {
577 mVideoTrack = source;
578 }
579
580 info.mSource = source;
581 }
582
583 mTracks.push(info);
584 }
585
586 mState = CONNECTED;
587}
588
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100589void NuPlayer::RTSPSource::onSDPLoaded(const sp<AMessage> &msg) {
590 status_t err;
591 CHECK(msg->findInt32("result", &err));
592
593 mSDPLoader.clear();
594
595 if (mDisconnectReplyID != 0) {
596 err = UNKNOWN_ERROR;
597 }
598
599 if (err == OK) {
600 sp<ASessionDescription> desc;
601 sp<RefBase> obj;
602 CHECK(msg->findObject("description", &obj));
603 desc = static_cast<ASessionDescription *>(obj.get());
604
605 AString rtspUri;
606 if (!desc->findAttribute(0, "a=control", &rtspUri)) {
607 ALOGE("Unable to find url in SDP");
608 err = UNKNOWN_ERROR;
609 } else {
610 sp<AMessage> notify = new AMessage(kWhatNotify, mReflector->id());
611
612 mHandler = new MyHandler(rtspUri.c_str(), notify, mUIDValid, mUID);
613 mLooper->registerHandler(mHandler);
614
615 mHandler->loadSDP(desc);
616 }
617 }
618
619 if (err != OK) {
Andreas Huber7f475c32013-02-05 14:47:13 -0800620 if (mState == CONNECTING) {
621 // We're still in the preparation phase, signal that it
622 // failed.
623 notifyPrepared(err);
624 }
625
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100626 mState = DISCONNECTED;
627 mFinalResult = err;
628
629 if (mDisconnectReplyID != 0) {
630 finishDisconnectIfPossible();
631 }
632 }
633}
634
Andreas Huber2bfdd422011-10-11 15:24:07 -0700635void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) {
Fredrik Rosin0ad03bc2013-03-06 13:42:53 +0100636 if (mState == DISCONNECTED) {
637 return;
638 }
639
Andreas Huber2bfdd422011-10-11 15:24:07 -0700640 status_t err;
641 CHECK(msg->findInt32("result", &err));
642 CHECK_NE(err, (status_t)OK);
643
644 mLooper->unregisterHandler(mHandler->id());
645 mHandler.clear();
646
Andreas Huber7f475c32013-02-05 14:47:13 -0800647 if (mState == CONNECTING) {
648 // We're still in the preparation phase, signal that it
649 // failed.
650 notifyPrepared(err);
651 }
652
Andreas Huber2bfdd422011-10-11 15:24:07 -0700653 mState = DISCONNECTED;
654 mFinalResult = err;
655
656 if (mDisconnectReplyID != 0) {
657 finishDisconnectIfPossible();
658 }
659}
660
661void NuPlayer::RTSPSource::finishDisconnectIfPossible() {
662 if (mState != DISCONNECTED) {
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100663 if (mHandler != NULL) {
664 mHandler->disconnect();
665 } else if (mSDPLoader != NULL) {
666 mSDPLoader->cancel();
667 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700668 return;
669 }
670
671 (new AMessage)->postReply(mDisconnectReplyID);
672 mDisconnectReplyID = 0;
673}
674
675} // namespace android