blob: ffacb8fa2c678cf8c8d1da133f6ebed6fe55b801 [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 Huber1b86fe02014-01-29 11:13:26 -080027#include <media/IMediaHTTPService.h>
Andreas Huber49694682012-08-31 10:27:46 -070028#include <media/stagefright/MediaDefs.h>
Andreas Huber2bfdd422011-10-11 15:24:07 -070029#include <media/stagefright/MetaData.h>
30
31namespace android {
32
Roger Jönssoncfc30832013-01-21 16:26:41 +010033const int64_t kNearEOSTimeoutUs = 2000000ll; // 2 secs
34
Andreas Huber2bfdd422011-10-11 15:24:07 -070035NuPlayer::RTSPSource::RTSPSource(
Andreas Huber5ab368a2013-02-05 10:14:26 -080036 const sp<AMessage> &notify,
Andreas Huber1b86fe02014-01-29 11:13:26 -080037 const sp<IMediaHTTPService> &httpService,
Andreas Huber2bfdd422011-10-11 15:24:07 -070038 const char *url,
39 const KeyedVector<String8, String8> *headers,
40 bool uidValid,
Oscar Rydhé81dd60e2012-02-20 10:15:48 +010041 uid_t uid,
42 bool isSDP)
Andreas Huber5ab368a2013-02-05 10:14:26 -080043 : Source(notify),
Andreas Huber1b86fe02014-01-29 11:13:26 -080044 mHTTPService(httpService),
Andreas Huber5ab368a2013-02-05 10:14:26 -080045 mURL(url),
Andreas Huber2bfdd422011-10-11 15:24:07 -070046 mUIDValid(uidValid),
47 mUID(uid),
48 mFlags(0),
Oscar Rydhé81dd60e2012-02-20 10:15:48 +010049 mIsSDP(isSDP),
Andreas Huber2bfdd422011-10-11 15:24:07 -070050 mState(DISCONNECTED),
51 mFinalResult(OK),
Andreas Huberee736e92011-12-08 13:04:50 -080052 mDisconnectReplyID(0),
Roger Jönssoncfc30832013-01-21 16:26:41 +010053 mBuffering(true),
54 mSeekGeneration(0),
55 mEOSTimeoutAudio(0),
56 mEOSTimeoutVideo(0) {
Andreas Huber2bfdd422011-10-11 15:24:07 -070057 if (headers) {
58 mExtraHeaders = *headers;
59
60 ssize_t index =
61 mExtraHeaders.indexOfKey(String8("x-hide-urls-from-log"));
62
63 if (index >= 0) {
64 mFlags |= kFlagIncognito;
65
66 mExtraHeaders.removeItemsAt(index);
67 }
68 }
69}
70
71NuPlayer::RTSPSource::~RTSPSource() {
Andreas Huber602f5bb2013-04-15 16:18:56 -070072 if (mLooper != NULL) {
Chong Zhang1228d6b2014-08-12 21:25:48 -070073 mLooper->unregisterHandler(id());
Andreas Huber602f5bb2013-04-15 16:18:56 -070074 mLooper->stop();
75 }
Andreas Huber2bfdd422011-10-11 15:24:07 -070076}
77
Andreas Huber57cea552013-02-05 13:59:56 -080078void NuPlayer::RTSPSource::prepareAsync() {
Andreas Huber2bfdd422011-10-11 15:24:07 -070079 if (mLooper == NULL) {
80 mLooper = new ALooper;
81 mLooper->setName("rtsp");
82 mLooper->start();
83
Chong Zhang1228d6b2014-08-12 21:25:48 -070084 mLooper->registerHandler(this);
Andreas Huber2bfdd422011-10-11 15:24:07 -070085 }
86
87 CHECK(mHandler == NULL);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +010088 CHECK(mSDPLoader == NULL);
Andreas Huber2bfdd422011-10-11 15:24:07 -070089
Chong Zhang1228d6b2014-08-12 21:25:48 -070090 sp<AMessage> notify = new AMessage(kWhatNotify, id());
Andreas Huber2bfdd422011-10-11 15:24:07 -070091
Andreas Huber2bfdd422011-10-11 15:24:07 -070092 CHECK_EQ(mState, (int)DISCONNECTED);
93 mState = CONNECTING;
94
Oscar Rydhé81dd60e2012-02-20 10:15:48 +010095 if (mIsSDP) {
96 mSDPLoader = new SDPLoader(notify,
97 (mFlags & kFlagIncognito) ? SDPLoader::kFlagIncognito : 0,
Andreas Huber81e68442014-02-05 11:52:33 -080098 mHTTPService);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +010099
Andreas Huber57cea552013-02-05 13:59:56 -0800100 mSDPLoader->load(
101 mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100102 } else {
103 mHandler = new MyHandler(mURL.c_str(), notify, mUIDValid, mUID);
104 mLooper->registerHandler(mHandler);
105
106 mHandler->connect();
107 }
Roger Jönssoncfc30832013-01-21 16:26:41 +0100108
109 sp<AMessage> notifyStart = dupNotify();
110 notifyStart->setInt32("what", kWhatBufferingStart);
111 notifyStart->post();
Andreas Huber57cea552013-02-05 13:59:56 -0800112}
113
114void NuPlayer::RTSPSource::start() {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700115}
116
117void NuPlayer::RTSPSource::stop() {
James Dong58341812012-11-16 14:31:15 -0800118 if (mLooper == NULL) {
119 return;
120 }
Chong Zhang1228d6b2014-08-12 21:25:48 -0700121 sp<AMessage> msg = new AMessage(kWhatDisconnect, id());
Andreas Huber2bfdd422011-10-11 15:24:07 -0700122
123 sp<AMessage> dummy;
124 msg->postAndAwaitResponse(&dummy);
125}
126
Roger Jönsson46d13e32013-01-21 17:15:45 +0100127void NuPlayer::RTSPSource::pause() {
128 int64_t mediaDurationUs = 0;
129 getDuration(&mediaDurationUs);
130 for (size_t index = 0; index < mTracks.size(); index++) {
131 TrackInfo *info = &mTracks.editItemAt(index);
132 sp<AnotherPacketSource> source = info->mSource;
133
134 // Check if EOS or ERROR is received
135 if (source != NULL && source->isFinished(mediaDurationUs)) {
136 return;
137 }
138 }
139 mHandler->pause();
140}
141
142void NuPlayer::RTSPSource::resume() {
143 mHandler->resume();
144}
145
Andreas Huber2bfdd422011-10-11 15:24:07 -0700146status_t NuPlayer::RTSPSource::feedMoreTSData() {
147 return mFinalResult;
148}
149
Andreas Huber84066782011-08-16 09:34:26 -0700150sp<MetaData> NuPlayer::RTSPSource::getFormatMeta(bool audio) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700151 sp<AnotherPacketSource> source = getSource(audio);
152
153 if (source == NULL) {
154 return NULL;
155 }
156
157 return source->getFormat();
158}
159
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700160bool NuPlayer::RTSPSource::haveSufficientDataOnAllTracks() {
161 // We're going to buffer at least 2 secs worth data on all tracks before
162 // starting playback (both at startup and after a seek).
163
164 static const int64_t kMinDurationUs = 2000000ll;
165
Roger Jönssoncfc30832013-01-21 16:26:41 +0100166 int64_t mediaDurationUs = 0;
167 getDuration(&mediaDurationUs);
168 if ((mAudioTrack != NULL && mAudioTrack->isFinished(mediaDurationUs))
169 || (mVideoTrack != NULL && mVideoTrack->isFinished(mediaDurationUs))) {
170 return true;
171 }
172
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700173 status_t err;
174 int64_t durationUs;
175 if (mAudioTrack != NULL
176 && (durationUs = mAudioTrack->getBufferedDurationUs(&err))
177 < kMinDurationUs
178 && err == OK) {
179 ALOGV("audio track doesn't have enough data yet. (%.2f secs buffered)",
180 durationUs / 1E6);
181 return false;
182 }
183
184 if (mVideoTrack != NULL
185 && (durationUs = mVideoTrack->getBufferedDurationUs(&err))
186 < kMinDurationUs
187 && err == OK) {
188 ALOGV("video track doesn't have enough data yet. (%.2f secs buffered)",
189 durationUs / 1E6);
190 return false;
191 }
192
193 return true;
194}
195
Andreas Huber2bfdd422011-10-11 15:24:07 -0700196status_t NuPlayer::RTSPSource::dequeueAccessUnit(
197 bool audio, sp<ABuffer> *accessUnit) {
Roger Jönssoncfc30832013-01-21 16:26:41 +0100198 if (mBuffering) {
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700199 if (!haveSufficientDataOnAllTracks()) {
200 return -EWOULDBLOCK;
201 }
202
Roger Jönssoncfc30832013-01-21 16:26:41 +0100203 mBuffering = false;
204
205 sp<AMessage> notify = dupNotify();
206 notify->setInt32("what", kWhatBufferingEnd);
207 notify->post();
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700208 }
209
Andreas Huber2bfdd422011-10-11 15:24:07 -0700210 sp<AnotherPacketSource> source = getSource(audio);
211
212 if (source == NULL) {
213 return -EWOULDBLOCK;
214 }
215
216 status_t finalResult;
217 if (!source->hasBufferAvailable(&finalResult)) {
Roger Jönssoncfc30832013-01-21 16:26:41 +0100218 if (finalResult == OK) {
219 int64_t mediaDurationUs = 0;
220 getDuration(&mediaDurationUs);
221 sp<AnotherPacketSource> otherSource = getSource(!audio);
222 status_t otherFinalResult;
223
224 // If other source already signaled EOS, this source should also signal EOS
225 if (otherSource != NULL &&
226 !otherSource->hasBufferAvailable(&otherFinalResult) &&
227 otherFinalResult == ERROR_END_OF_STREAM) {
228 source->signalEOS(ERROR_END_OF_STREAM);
229 return ERROR_END_OF_STREAM;
230 }
231
232 // If this source has detected near end, give it some time to retrieve more
233 // data before signaling EOS
234 if (source->isFinished(mediaDurationUs)) {
235 int64_t eosTimeout = audio ? mEOSTimeoutAudio : mEOSTimeoutVideo;
236 if (eosTimeout == 0) {
237 setEOSTimeout(audio, ALooper::GetNowUs());
238 } else if ((ALooper::GetNowUs() - eosTimeout) > kNearEOSTimeoutUs) {
239 setEOSTimeout(audio, 0);
240 source->signalEOS(ERROR_END_OF_STREAM);
241 return ERROR_END_OF_STREAM;
242 }
243 return -EWOULDBLOCK;
244 }
245
246 if (!(otherSource != NULL && otherSource->isFinished(mediaDurationUs))) {
247 // We should not enter buffering mode
248 // if any of the sources already have detected EOS.
249 mBuffering = true;
250
251 sp<AMessage> notify = dupNotify();
252 notify->setInt32("what", kWhatBufferingStart);
253 notify->post();
254 }
255
256 return -EWOULDBLOCK;
257 }
258 return finalResult;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700259 }
260
Roger Jönssoncfc30832013-01-21 16:26:41 +0100261 setEOSTimeout(audio, 0);
262
Andreas Huber2bfdd422011-10-11 15:24:07 -0700263 return source->dequeueAccessUnit(accessUnit);
264}
265
266sp<AnotherPacketSource> NuPlayer::RTSPSource::getSource(bool audio) {
Andreas Huber49694682012-08-31 10:27:46 -0700267 if (mTSParser != NULL) {
268 sp<MediaSource> source = mTSParser->getSource(
269 audio ? ATSParser::AUDIO : ATSParser::VIDEO);
270
271 return static_cast<AnotherPacketSource *>(source.get());
272 }
273
Andreas Huber2bfdd422011-10-11 15:24:07 -0700274 return audio ? mAudioTrack : mVideoTrack;
275}
276
Roger Jönssoncfc30832013-01-21 16:26:41 +0100277void NuPlayer::RTSPSource::setEOSTimeout(bool audio, int64_t timeout) {
278 if (audio) {
279 mEOSTimeoutAudio = timeout;
280 } else {
281 mEOSTimeoutVideo = timeout;
282 }
283}
284
Andreas Huber2bfdd422011-10-11 15:24:07 -0700285status_t NuPlayer::RTSPSource::getDuration(int64_t *durationUs) {
286 *durationUs = 0ll;
287
288 int64_t audioDurationUs;
289 if (mAudioTrack != NULL
290 && mAudioTrack->getFormat()->findInt64(
291 kKeyDuration, &audioDurationUs)
292 && audioDurationUs > *durationUs) {
293 *durationUs = audioDurationUs;
294 }
295
296 int64_t videoDurationUs;
297 if (mVideoTrack != NULL
298 && mVideoTrack->getFormat()->findInt64(
299 kKeyDuration, &videoDurationUs)
300 && videoDurationUs > *durationUs) {
301 *durationUs = videoDurationUs;
302 }
303
304 return OK;
305}
306
307status_t NuPlayer::RTSPSource::seekTo(int64_t seekTimeUs) {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700308 sp<AMessage> msg = new AMessage(kWhatPerformSeek, id());
Andreas Huberee736e92011-12-08 13:04:50 -0800309 msg->setInt32("generation", ++mSeekGeneration);
310 msg->setInt64("timeUs", seekTimeUs);
311 msg->post(200000ll);
312
313 return OK;
314}
315
316void NuPlayer::RTSPSource::performSeek(int64_t seekTimeUs) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700317 if (mState != CONNECTED) {
Andreas Huberee736e92011-12-08 13:04:50 -0800318 return;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700319 }
320
321 mState = SEEKING;
322 mHandler->seek(seekTimeUs);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700323}
324
Andreas Huber2bfdd422011-10-11 15:24:07 -0700325void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) {
326 if (msg->what() == kWhatDisconnect) {
327 uint32_t replyID;
328 CHECK(msg->senderAwaitsResponse(&replyID));
329
330 mDisconnectReplyID = replyID;
331 finishDisconnectIfPossible();
332 return;
Andreas Huberee736e92011-12-08 13:04:50 -0800333 } else if (msg->what() == kWhatPerformSeek) {
334 int32_t generation;
335 CHECK(msg->findInt32("generation", &generation));
336
337 if (generation != mSeekGeneration) {
338 // obsolete.
339 return;
340 }
341
342 int64_t seekTimeUs;
343 CHECK(msg->findInt64("timeUs", &seekTimeUs));
344
345 performSeek(seekTimeUs);
346 return;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700347 }
348
349 CHECK_EQ(msg->what(), (int)kWhatNotify);
350
351 int32_t what;
352 CHECK(msg->findInt32("what", &what));
353
354 switch (what) {
355 case MyHandler::kWhatConnected:
Andreas Huber7f475c32013-02-05 14:47:13 -0800356 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700357 onConnected();
Andreas Huber7f475c32013-02-05 14:47:13 -0800358
Chong Zhangced1c2f2014-08-08 15:22:35 -0700359 notifyVideoSizeChanged();
Andreas Huber7f475c32013-02-05 14:47:13 -0800360
361 uint32_t flags = 0;
362
363 if (mHandler->isSeekable()) {
Chong Zhang4b7069d2013-09-11 12:52:43 -0700364 flags = FLAG_CAN_PAUSE
365 | FLAG_CAN_SEEK
366 | FLAG_CAN_SEEK_BACKWARD
367 | FLAG_CAN_SEEK_FORWARD;
Andreas Huber7f475c32013-02-05 14:47:13 -0800368 }
369
370 notifyFlagsChanged(flags);
371 notifyPrepared();
Andreas Huber2bfdd422011-10-11 15:24:07 -0700372 break;
Andreas Huber7f475c32013-02-05 14:47:13 -0800373 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700374
375 case MyHandler::kWhatDisconnected:
Andreas Huber7f475c32013-02-05 14:47:13 -0800376 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700377 onDisconnected(msg);
378 break;
Andreas Huber7f475c32013-02-05 14:47:13 -0800379 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700380
381 case MyHandler::kWhatSeekDone:
382 {
383 mState = CONNECTED;
384 break;
385 }
386
387 case MyHandler::kWhatAccessUnit:
388 {
389 size_t trackIndex;
390 CHECK(msg->findSize("trackIndex", &trackIndex));
Andreas Huber49694682012-08-31 10:27:46 -0700391
392 if (mTSParser == NULL) {
393 CHECK_LT(trackIndex, mTracks.size());
394 } else {
395 CHECK_EQ(trackIndex, 0u);
396 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700397
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800398 sp<ABuffer> accessUnit;
399 CHECK(msg->findBuffer("accessUnit", &accessUnit));
Andreas Huber2bfdd422011-10-11 15:24:07 -0700400
401 int32_t damaged;
402 if (accessUnit->meta()->findInt32("damaged", &damaged)
403 && damaged) {
Steve Blockdf64d152012-01-04 20:05:49 +0000404 ALOGI("dropping damaged access unit.");
Andreas Huber2bfdd422011-10-11 15:24:07 -0700405 break;
406 }
407
Andreas Huber49694682012-08-31 10:27:46 -0700408 if (mTSParser != NULL) {
409 size_t offset = 0;
410 status_t err = OK;
411 while (offset + 188 <= accessUnit->size()) {
412 err = mTSParser->feedTSPacket(
413 accessUnit->data() + offset, 188);
414 if (err != OK) {
415 break;
416 }
417
418 offset += 188;
419 }
420
421 if (offset < accessUnit->size()) {
422 err = ERROR_MALFORMED;
423 }
424
425 if (err != OK) {
426 sp<AnotherPacketSource> source = getSource(false /* audio */);
427 if (source != NULL) {
428 source->signalEOS(err);
429 }
430
431 source = getSource(true /* audio */);
432 if (source != NULL) {
433 source->signalEOS(err);
434 }
435 }
436 break;
437 }
438
Andreas Huber1906e5c2011-12-08 12:27:47 -0800439 TrackInfo *info = &mTracks.editItemAt(trackIndex);
440
441 sp<AnotherPacketSource> source = info->mSource;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700442 if (source != NULL) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700443 uint32_t rtpTime;
444 CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
445
Andreas Huber1906e5c2011-12-08 12:27:47 -0800446 if (!info->mNPTMappingValid) {
447 // This is a live stream, we didn't receive any normal
Andreas Huberc9d16962012-05-21 11:12:40 -0700448 // playtime mapping. We won't map to npt time.
449 source->queueAccessUnit(accessUnit);
450 break;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800451 }
452
Andreas Huber2bfdd422011-10-11 15:24:07 -0700453 int64_t nptUs =
Andreas Huber1906e5c2011-12-08 12:27:47 -0800454 ((double)rtpTime - (double)info->mRTPTime)
455 / info->mTimeScale
Andreas Huber2bfdd422011-10-11 15:24:07 -0700456 * 1000000ll
Andreas Huber1906e5c2011-12-08 12:27:47 -0800457 + info->mNormalPlaytimeUs;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700458
459 accessUnit->meta()->setInt64("timeUs", nptUs);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700460
461 source->queueAccessUnit(accessUnit);
462 }
463 break;
464 }
465
466 case MyHandler::kWhatEOS:
467 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700468 int32_t finalResult;
469 CHECK(msg->findInt32("finalResult", &finalResult));
470 CHECK_NE(finalResult, (status_t)OK);
471
Andreas Huber49694682012-08-31 10:27:46 -0700472 if (mTSParser != NULL) {
473 sp<AnotherPacketSource> source = getSource(false /* audio */);
474 if (source != NULL) {
475 source->signalEOS(finalResult);
476 }
477
478 source = getSource(true /* audio */);
479 if (source != NULL) {
480 source->signalEOS(finalResult);
481 }
482
483 return;
484 }
485
486 size_t trackIndex;
487 CHECK(msg->findSize("trackIndex", &trackIndex));
488 CHECK_LT(trackIndex, mTracks.size());
489
Andreas Huber2bfdd422011-10-11 15:24:07 -0700490 TrackInfo *info = &mTracks.editItemAt(trackIndex);
491 sp<AnotherPacketSource> source = info->mSource;
492 if (source != NULL) {
493 source->signalEOS(finalResult);
494 }
495
496 break;
497 }
498
499 case MyHandler::kWhatSeekDiscontinuity:
500 {
501 size_t trackIndex;
502 CHECK(msg->findSize("trackIndex", &trackIndex));
503 CHECK_LT(trackIndex, mTracks.size());
504
505 TrackInfo *info = &mTracks.editItemAt(trackIndex);
506 sp<AnotherPacketSource> source = info->mSource;
507 if (source != NULL) {
Chong Zhang632740c2014-06-26 13:03:47 -0700508 source->queueDiscontinuity(
509 ATSParser::DISCONTINUITY_SEEK,
510 NULL,
511 true /* discard */);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700512 }
513
514 break;
515 }
516
517 case MyHandler::kWhatNormalPlayTimeMapping:
518 {
519 size_t trackIndex;
520 CHECK(msg->findSize("trackIndex", &trackIndex));
521 CHECK_LT(trackIndex, mTracks.size());
522
523 uint32_t rtpTime;
524 CHECK(msg->findInt32("rtpTime", (int32_t *)&rtpTime));
525
526 int64_t nptUs;
527 CHECK(msg->findInt64("nptUs", &nptUs));
528
529 TrackInfo *info = &mTracks.editItemAt(trackIndex);
530 info->mRTPTime = rtpTime;
531 info->mNormalPlaytimeUs = nptUs;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800532 info->mNPTMappingValid = true;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700533 break;
534 }
535
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100536 case SDPLoader::kWhatSDPLoaded:
537 {
538 onSDPLoaded(msg);
539 break;
540 }
541
Andreas Huber2bfdd422011-10-11 15:24:07 -0700542 default:
543 TRESPASS();
544 }
545}
546
547void NuPlayer::RTSPSource::onConnected() {
548 CHECK(mAudioTrack == NULL);
549 CHECK(mVideoTrack == NULL);
550
551 size_t numTracks = mHandler->countTracks();
552 for (size_t i = 0; i < numTracks; ++i) {
553 int32_t timeScale;
554 sp<MetaData> format = mHandler->getTrackFormat(i, &timeScale);
555
556 const char *mime;
557 CHECK(format->findCString(kKeyMIMEType, &mime));
558
Andreas Huber49694682012-08-31 10:27:46 -0700559 if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) {
560 // Very special case for MPEG2 Transport Streams.
561 CHECK_EQ(numTracks, 1u);
562
563 mTSParser = new ATSParser;
564 return;
565 }
566
Andreas Huber2bfdd422011-10-11 15:24:07 -0700567 bool isAudio = !strncasecmp(mime, "audio/", 6);
568 bool isVideo = !strncasecmp(mime, "video/", 6);
569
570 TrackInfo info;
571 info.mTimeScale = timeScale;
572 info.mRTPTime = 0;
573 info.mNormalPlaytimeUs = 0ll;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800574 info.mNPTMappingValid = false;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700575
576 if ((isAudio && mAudioTrack == NULL)
577 || (isVideo && mVideoTrack == NULL)) {
578 sp<AnotherPacketSource> source = new AnotherPacketSource(format);
579
580 if (isAudio) {
581 mAudioTrack = source;
582 } else {
583 mVideoTrack = source;
584 }
585
586 info.mSource = source;
587 }
588
589 mTracks.push(info);
590 }
591
592 mState = CONNECTED;
593}
594
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100595void NuPlayer::RTSPSource::onSDPLoaded(const sp<AMessage> &msg) {
596 status_t err;
597 CHECK(msg->findInt32("result", &err));
598
599 mSDPLoader.clear();
600
601 if (mDisconnectReplyID != 0) {
602 err = UNKNOWN_ERROR;
603 }
604
605 if (err == OK) {
606 sp<ASessionDescription> desc;
607 sp<RefBase> obj;
608 CHECK(msg->findObject("description", &obj));
609 desc = static_cast<ASessionDescription *>(obj.get());
610
611 AString rtspUri;
612 if (!desc->findAttribute(0, "a=control", &rtspUri)) {
613 ALOGE("Unable to find url in SDP");
614 err = UNKNOWN_ERROR;
615 } else {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700616 sp<AMessage> notify = new AMessage(kWhatNotify, id());
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100617
618 mHandler = new MyHandler(rtspUri.c_str(), notify, mUIDValid, mUID);
619 mLooper->registerHandler(mHandler);
620
621 mHandler->loadSDP(desc);
622 }
623 }
624
625 if (err != OK) {
Andreas Huber7f475c32013-02-05 14:47:13 -0800626 if (mState == CONNECTING) {
627 // We're still in the preparation phase, signal that it
628 // failed.
629 notifyPrepared(err);
630 }
631
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100632 mState = DISCONNECTED;
633 mFinalResult = err;
634
635 if (mDisconnectReplyID != 0) {
636 finishDisconnectIfPossible();
637 }
638 }
639}
640
Andreas Huber2bfdd422011-10-11 15:24:07 -0700641void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) {
Fredrik Rosin0ad03bc2013-03-06 13:42:53 +0100642 if (mState == DISCONNECTED) {
643 return;
644 }
645
Andreas Huber2bfdd422011-10-11 15:24:07 -0700646 status_t err;
647 CHECK(msg->findInt32("result", &err));
648 CHECK_NE(err, (status_t)OK);
649
650 mLooper->unregisterHandler(mHandler->id());
651 mHandler.clear();
652
Andreas Huber7f475c32013-02-05 14:47:13 -0800653 if (mState == CONNECTING) {
654 // We're still in the preparation phase, signal that it
655 // failed.
656 notifyPrepared(err);
657 }
658
Andreas Huber2bfdd422011-10-11 15:24:07 -0700659 mState = DISCONNECTED;
660 mFinalResult = err;
661
662 if (mDisconnectReplyID != 0) {
663 finishDisconnectIfPossible();
664 }
665}
666
667void NuPlayer::RTSPSource::finishDisconnectIfPossible() {
668 if (mState != DISCONNECTED) {
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100669 if (mHandler != NULL) {
670 mHandler->disconnect();
671 } else if (mSDPLoader != NULL) {
672 mSDPLoader->cancel();
673 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700674 return;
675 }
676
677 (new AMessage)->postReply(mDisconnectReplyID);
678 mDisconnectReplyID = 0;
679}
680
681} // namespace android