blob: c861fd1483179bb0150875af597647878cb6bece [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),
Chong Zhang180d1b92014-12-02 18:35:35 -080053 mBuffering(false),
Roger Jönssoncfc30832013-01-21 16:26:41 +010054 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() {
Wei Jiace84b222016-01-14 14:39:42 -080079 if (mIsSDP && mHTTPService == NULL) {
80 notifyPrepared(BAD_VALUE);
81 return;
82 }
83
Andreas Huber2bfdd422011-10-11 15:24:07 -070084 if (mLooper == NULL) {
85 mLooper = new ALooper;
86 mLooper->setName("rtsp");
87 mLooper->start();
88
Chong Zhang1228d6b2014-08-12 21:25:48 -070089 mLooper->registerHandler(this);
Andreas Huber2bfdd422011-10-11 15:24:07 -070090 }
91
92 CHECK(mHandler == NULL);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +010093 CHECK(mSDPLoader == NULL);
Andreas Huber2bfdd422011-10-11 15:24:07 -070094
Lajos Molnar1d15ab52015-03-04 16:46:34 -080095 sp<AMessage> notify = new AMessage(kWhatNotify, this);
Andreas Huber2bfdd422011-10-11 15:24:07 -070096
Andreas Huber2bfdd422011-10-11 15:24:07 -070097 CHECK_EQ(mState, (int)DISCONNECTED);
98 mState = CONNECTING;
99
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100100 if (mIsSDP) {
101 mSDPLoader = new SDPLoader(notify,
102 (mFlags & kFlagIncognito) ? SDPLoader::kFlagIncognito : 0,
Andreas Huber81e68442014-02-05 11:52:33 -0800103 mHTTPService);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100104
Andreas Huber57cea552013-02-05 13:59:56 -0800105 mSDPLoader->load(
106 mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100107 } else {
108 mHandler = new MyHandler(mURL.c_str(), notify, mUIDValid, mUID);
109 mLooper->registerHandler(mHandler);
110
111 mHandler->connect();
112 }
Roger Jönssoncfc30832013-01-21 16:26:41 +0100113
Chong Zhang180d1b92014-12-02 18:35:35 -0800114 startBufferingIfNecessary();
Andreas Huber57cea552013-02-05 13:59:56 -0800115}
116
117void NuPlayer::RTSPSource::start() {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700118}
119
120void NuPlayer::RTSPSource::stop() {
James Dong58341812012-11-16 14:31:15 -0800121 if (mLooper == NULL) {
122 return;
123 }
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800124 sp<AMessage> msg = new AMessage(kWhatDisconnect, this);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700125
126 sp<AMessage> dummy;
127 msg->postAndAwaitResponse(&dummy);
128}
129
Roger Jönsson46d13e32013-01-21 17:15:45 +0100130void NuPlayer::RTSPSource::pause() {
131 int64_t mediaDurationUs = 0;
132 getDuration(&mediaDurationUs);
133 for (size_t index = 0; index < mTracks.size(); index++) {
134 TrackInfo *info = &mTracks.editItemAt(index);
135 sp<AnotherPacketSource> source = info->mSource;
136
137 // Check if EOS or ERROR is received
138 if (source != NULL && source->isFinished(mediaDurationUs)) {
139 return;
140 }
141 }
Robert Shih6d3cd2e2015-08-13 12:09:52 -0700142 if (mHandler != NULL) {
143 mHandler->pause();
144 }
Roger Jönsson46d13e32013-01-21 17:15:45 +0100145}
146
147void NuPlayer::RTSPSource::resume() {
Robert Shih8d237a52015-07-13 17:59:36 -0700148 if (mHandler != NULL) {
149 mHandler->resume();
150 }
Roger Jönsson46d13e32013-01-21 17:15:45 +0100151}
152
Andreas Huber2bfdd422011-10-11 15:24:07 -0700153status_t NuPlayer::RTSPSource::feedMoreTSData() {
Chong Zhang180d1b92014-12-02 18:35:35 -0800154 Mutex::Autolock _l(mBufferingLock);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700155 return mFinalResult;
156}
157
Andreas Huber84066782011-08-16 09:34:26 -0700158sp<MetaData> NuPlayer::RTSPSource::getFormatMeta(bool audio) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700159 sp<AnotherPacketSource> source = getSource(audio);
160
161 if (source == NULL) {
162 return NULL;
163 }
164
165 return source->getFormat();
166}
167
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700168bool NuPlayer::RTSPSource::haveSufficientDataOnAllTracks() {
169 // We're going to buffer at least 2 secs worth data on all tracks before
170 // starting playback (both at startup and after a seek).
171
172 static const int64_t kMinDurationUs = 2000000ll;
173
Roger Jönssoncfc30832013-01-21 16:26:41 +0100174 int64_t mediaDurationUs = 0;
175 getDuration(&mediaDurationUs);
176 if ((mAudioTrack != NULL && mAudioTrack->isFinished(mediaDurationUs))
177 || (mVideoTrack != NULL && mVideoTrack->isFinished(mediaDurationUs))) {
178 return true;
179 }
180
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700181 status_t err;
182 int64_t durationUs;
183 if (mAudioTrack != NULL
184 && (durationUs = mAudioTrack->getBufferedDurationUs(&err))
185 < kMinDurationUs
186 && err == OK) {
187 ALOGV("audio track doesn't have enough data yet. (%.2f secs buffered)",
188 durationUs / 1E6);
189 return false;
190 }
191
192 if (mVideoTrack != NULL
193 && (durationUs = mVideoTrack->getBufferedDurationUs(&err))
194 < kMinDurationUs
195 && err == OK) {
196 ALOGV("video track doesn't have enough data yet. (%.2f secs buffered)",
197 durationUs / 1E6);
198 return false;
199 }
200
201 return true;
202}
203
Andreas Huber2bfdd422011-10-11 15:24:07 -0700204status_t NuPlayer::RTSPSource::dequeueAccessUnit(
205 bool audio, sp<ABuffer> *accessUnit) {
Chong Zhang180d1b92014-12-02 18:35:35 -0800206 if (!stopBufferingIfNecessary()) {
207 return -EWOULDBLOCK;
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.
Chong Zhang180d1b92014-12-02 18:35:35 -0800249 startBufferingIfNecessary();
Roger Jönssoncfc30832013-01-21 16:26:41 +0100250 }
251
252 return -EWOULDBLOCK;
253 }
254 return finalResult;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700255 }
256
Roger Jönssoncfc30832013-01-21 16:26:41 +0100257 setEOSTimeout(audio, 0);
258
Andreas Huber2bfdd422011-10-11 15:24:07 -0700259 return source->dequeueAccessUnit(accessUnit);
260}
261
262sp<AnotherPacketSource> NuPlayer::RTSPSource::getSource(bool audio) {
Andreas Huber49694682012-08-31 10:27:46 -0700263 if (mTSParser != NULL) {
264 sp<MediaSource> source = mTSParser->getSource(
265 audio ? ATSParser::AUDIO : ATSParser::VIDEO);
266
267 return static_cast<AnotherPacketSource *>(source.get());
268 }
269
Andreas Huber2bfdd422011-10-11 15:24:07 -0700270 return audio ? mAudioTrack : mVideoTrack;
271}
272
Roger Jönssoncfc30832013-01-21 16:26:41 +0100273void NuPlayer::RTSPSource::setEOSTimeout(bool audio, int64_t timeout) {
274 if (audio) {
275 mEOSTimeoutAudio = timeout;
276 } else {
277 mEOSTimeoutVideo = timeout;
278 }
279}
280
Andreas Huber2bfdd422011-10-11 15:24:07 -0700281status_t NuPlayer::RTSPSource::getDuration(int64_t *durationUs) {
282 *durationUs = 0ll;
283
284 int64_t audioDurationUs;
285 if (mAudioTrack != NULL
286 && mAudioTrack->getFormat()->findInt64(
287 kKeyDuration, &audioDurationUs)
288 && audioDurationUs > *durationUs) {
289 *durationUs = audioDurationUs;
290 }
291
292 int64_t videoDurationUs;
293 if (mVideoTrack != NULL
294 && mVideoTrack->getFormat()->findInt64(
295 kKeyDuration, &videoDurationUs)
296 && videoDurationUs > *durationUs) {
297 *durationUs = videoDurationUs;
298 }
299
300 return OK;
301}
302
303status_t NuPlayer::RTSPSource::seekTo(int64_t seekTimeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800304 sp<AMessage> msg = new AMessage(kWhatPerformSeek, this);
Andreas Huberee736e92011-12-08 13:04:50 -0800305 msg->setInt32("generation", ++mSeekGeneration);
306 msg->setInt64("timeUs", seekTimeUs);
Andreas Huberee736e92011-12-08 13:04:50 -0800307
Robert Shih8d237a52015-07-13 17:59:36 -0700308 sp<AMessage> response;
309 status_t err = msg->postAndAwaitResponse(&response);
310 if (err == OK && response != NULL) {
311 CHECK(response->findInt32("err", &err));
312 }
313
314 return err;
Andreas Huberee736e92011-12-08 13:04:50 -0800315}
316
317void NuPlayer::RTSPSource::performSeek(int64_t seekTimeUs) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700318 if (mState != CONNECTED) {
Robert Shih8d237a52015-07-13 17:59:36 -0700319 finishSeek(INVALID_OPERATION);
Andreas Huberee736e92011-12-08 13:04:50 -0800320 return;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700321 }
322
323 mState = SEEKING;
324 mHandler->seek(seekTimeUs);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700325}
326
Andreas Huber2bfdd422011-10-11 15:24:07 -0700327void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) {
328 if (msg->what() == kWhatDisconnect) {
Lajos Molnar3f274362015-03-05 14:35:41 -0800329 sp<AReplyToken> replyID;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700330 CHECK(msg->senderAwaitsResponse(&replyID));
331
332 mDisconnectReplyID = replyID;
333 finishDisconnectIfPossible();
334 return;
Andreas Huberee736e92011-12-08 13:04:50 -0800335 } else if (msg->what() == kWhatPerformSeek) {
336 int32_t generation;
337 CHECK(msg->findInt32("generation", &generation));
Robert Shih8d237a52015-07-13 17:59:36 -0700338 CHECK(msg->senderAwaitsResponse(&mSeekReplyID));
Andreas Huberee736e92011-12-08 13:04:50 -0800339
340 if (generation != mSeekGeneration) {
341 // obsolete.
Robert Shih8d237a52015-07-13 17:59:36 -0700342 finishSeek(OK);
Andreas Huberee736e92011-12-08 13:04:50 -0800343 return;
344 }
345
346 int64_t seekTimeUs;
347 CHECK(msg->findInt64("timeUs", &seekTimeUs));
348
349 performSeek(seekTimeUs);
350 return;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700351 }
352
353 CHECK_EQ(msg->what(), (int)kWhatNotify);
354
355 int32_t what;
356 CHECK(msg->findInt32("what", &what));
357
358 switch (what) {
359 case MyHandler::kWhatConnected:
Andreas Huber7f475c32013-02-05 14:47:13 -0800360 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700361 onConnected();
Andreas Huber7f475c32013-02-05 14:47:13 -0800362
Chong Zhangced1c2f2014-08-08 15:22:35 -0700363 notifyVideoSizeChanged();
Andreas Huber7f475c32013-02-05 14:47:13 -0800364
365 uint32_t flags = 0;
366
367 if (mHandler->isSeekable()) {
Chong Zhang4b7069d2013-09-11 12:52:43 -0700368 flags = FLAG_CAN_PAUSE
369 | FLAG_CAN_SEEK
370 | FLAG_CAN_SEEK_BACKWARD
371 | FLAG_CAN_SEEK_FORWARD;
Andreas Huber7f475c32013-02-05 14:47:13 -0800372 }
373
374 notifyFlagsChanged(flags);
375 notifyPrepared();
Andreas Huber2bfdd422011-10-11 15:24:07 -0700376 break;
Andreas Huber7f475c32013-02-05 14:47:13 -0800377 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700378
379 case MyHandler::kWhatDisconnected:
Andreas Huber7f475c32013-02-05 14:47:13 -0800380 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700381 onDisconnected(msg);
382 break;
Andreas Huber7f475c32013-02-05 14:47:13 -0800383 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700384
385 case MyHandler::kWhatSeekDone:
386 {
387 mState = CONNECTED;
Robert Shih8d237a52015-07-13 17:59:36 -0700388 if (mSeekReplyID != NULL) {
389 // Unblock seekTo here in case we attempted to seek in a live stream
390 finishSeek(OK);
391 }
392 break;
393 }
394
395 case MyHandler::kWhatSeekPaused:
396 {
397 sp<AnotherPacketSource> source = getSource(true /* audio */);
398 if (source != NULL) {
399 source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
400 /* extra */ NULL,
401 /* discard */ true);
402 }
403 source = getSource(false /* video */);
404 if (source != NULL) {
405 source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
406 /* extra */ NULL,
407 /* discard */ true);
408 };
409
410 status_t err = OK;
411 msg->findInt32("err", &err);
412 finishSeek(err);
413
414 if (err == OK) {
415 int64_t timeUs;
416 CHECK(msg->findInt64("time", &timeUs));
417 mHandler->continueSeekAfterPause(timeUs);
418 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700419 break;
420 }
421
422 case MyHandler::kWhatAccessUnit:
423 {
424 size_t trackIndex;
425 CHECK(msg->findSize("trackIndex", &trackIndex));
Andreas Huber49694682012-08-31 10:27:46 -0700426
427 if (mTSParser == NULL) {
428 CHECK_LT(trackIndex, mTracks.size());
429 } else {
430 CHECK_EQ(trackIndex, 0u);
431 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700432
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800433 sp<ABuffer> accessUnit;
434 CHECK(msg->findBuffer("accessUnit", &accessUnit));
Andreas Huber2bfdd422011-10-11 15:24:07 -0700435
436 int32_t damaged;
437 if (accessUnit->meta()->findInt32("damaged", &damaged)
438 && damaged) {
Steve Blockdf64d152012-01-04 20:05:49 +0000439 ALOGI("dropping damaged access unit.");
Andreas Huber2bfdd422011-10-11 15:24:07 -0700440 break;
441 }
442
Andreas Huber49694682012-08-31 10:27:46 -0700443 if (mTSParser != NULL) {
444 size_t offset = 0;
445 status_t err = OK;
446 while (offset + 188 <= accessUnit->size()) {
447 err = mTSParser->feedTSPacket(
448 accessUnit->data() + offset, 188);
449 if (err != OK) {
450 break;
451 }
452
453 offset += 188;
454 }
455
456 if (offset < accessUnit->size()) {
457 err = ERROR_MALFORMED;
458 }
459
460 if (err != OK) {
461 sp<AnotherPacketSource> source = getSource(false /* audio */);
462 if (source != NULL) {
463 source->signalEOS(err);
464 }
465
466 source = getSource(true /* audio */);
467 if (source != NULL) {
468 source->signalEOS(err);
469 }
470 }
471 break;
472 }
473
Andreas Huber1906e5c2011-12-08 12:27:47 -0800474 TrackInfo *info = &mTracks.editItemAt(trackIndex);
475
476 sp<AnotherPacketSource> source = info->mSource;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700477 if (source != NULL) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700478 uint32_t rtpTime;
479 CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
480
Andreas Huber1906e5c2011-12-08 12:27:47 -0800481 if (!info->mNPTMappingValid) {
482 // This is a live stream, we didn't receive any normal
Andreas Huberc9d16962012-05-21 11:12:40 -0700483 // playtime mapping. We won't map to npt time.
484 source->queueAccessUnit(accessUnit);
485 break;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800486 }
487
Andreas Huber2bfdd422011-10-11 15:24:07 -0700488 int64_t nptUs =
Andreas Huber1906e5c2011-12-08 12:27:47 -0800489 ((double)rtpTime - (double)info->mRTPTime)
490 / info->mTimeScale
Andreas Huber2bfdd422011-10-11 15:24:07 -0700491 * 1000000ll
Andreas Huber1906e5c2011-12-08 12:27:47 -0800492 + info->mNormalPlaytimeUs;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700493
494 accessUnit->meta()->setInt64("timeUs", nptUs);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700495
496 source->queueAccessUnit(accessUnit);
497 }
498 break;
499 }
500
501 case MyHandler::kWhatEOS:
502 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700503 int32_t finalResult;
504 CHECK(msg->findInt32("finalResult", &finalResult));
505 CHECK_NE(finalResult, (status_t)OK);
506
Andreas Huber49694682012-08-31 10:27:46 -0700507 if (mTSParser != NULL) {
508 sp<AnotherPacketSource> source = getSource(false /* audio */);
509 if (source != NULL) {
510 source->signalEOS(finalResult);
511 }
512
513 source = getSource(true /* audio */);
514 if (source != NULL) {
515 source->signalEOS(finalResult);
516 }
517
518 return;
519 }
520
521 size_t trackIndex;
522 CHECK(msg->findSize("trackIndex", &trackIndex));
523 CHECK_LT(trackIndex, mTracks.size());
524
Andreas Huber2bfdd422011-10-11 15:24:07 -0700525 TrackInfo *info = &mTracks.editItemAt(trackIndex);
526 sp<AnotherPacketSource> source = info->mSource;
527 if (source != NULL) {
528 source->signalEOS(finalResult);
529 }
530
531 break;
532 }
533
534 case MyHandler::kWhatSeekDiscontinuity:
535 {
536 size_t trackIndex;
537 CHECK(msg->findSize("trackIndex", &trackIndex));
538 CHECK_LT(trackIndex, mTracks.size());
539
540 TrackInfo *info = &mTracks.editItemAt(trackIndex);
541 sp<AnotherPacketSource> source = info->mSource;
542 if (source != NULL) {
Chong Zhang632740c2014-06-26 13:03:47 -0700543 source->queueDiscontinuity(
Wei Jiafef808d2014-10-31 17:57:05 -0700544 ATSParser::DISCONTINUITY_TIME,
Chong Zhang632740c2014-06-26 13:03:47 -0700545 NULL,
546 true /* discard */);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700547 }
548
549 break;
550 }
551
552 case MyHandler::kWhatNormalPlayTimeMapping:
553 {
554 size_t trackIndex;
555 CHECK(msg->findSize("trackIndex", &trackIndex));
556 CHECK_LT(trackIndex, mTracks.size());
557
558 uint32_t rtpTime;
559 CHECK(msg->findInt32("rtpTime", (int32_t *)&rtpTime));
560
561 int64_t nptUs;
562 CHECK(msg->findInt64("nptUs", &nptUs));
563
564 TrackInfo *info = &mTracks.editItemAt(trackIndex);
565 info->mRTPTime = rtpTime;
566 info->mNormalPlaytimeUs = nptUs;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800567 info->mNPTMappingValid = true;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700568 break;
569 }
570
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100571 case SDPLoader::kWhatSDPLoaded:
572 {
573 onSDPLoaded(msg);
574 break;
575 }
576
Andreas Huber2bfdd422011-10-11 15:24:07 -0700577 default:
578 TRESPASS();
579 }
580}
581
582void NuPlayer::RTSPSource::onConnected() {
583 CHECK(mAudioTrack == NULL);
584 CHECK(mVideoTrack == NULL);
585
586 size_t numTracks = mHandler->countTracks();
587 for (size_t i = 0; i < numTracks; ++i) {
588 int32_t timeScale;
589 sp<MetaData> format = mHandler->getTrackFormat(i, &timeScale);
590
591 const char *mime;
592 CHECK(format->findCString(kKeyMIMEType, &mime));
593
Andreas Huber49694682012-08-31 10:27:46 -0700594 if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) {
595 // Very special case for MPEG2 Transport Streams.
596 CHECK_EQ(numTracks, 1u);
597
598 mTSParser = new ATSParser;
599 return;
600 }
601
Andreas Huber2bfdd422011-10-11 15:24:07 -0700602 bool isAudio = !strncasecmp(mime, "audio/", 6);
603 bool isVideo = !strncasecmp(mime, "video/", 6);
604
605 TrackInfo info;
606 info.mTimeScale = timeScale;
607 info.mRTPTime = 0;
608 info.mNormalPlaytimeUs = 0ll;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800609 info.mNPTMappingValid = false;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700610
611 if ((isAudio && mAudioTrack == NULL)
612 || (isVideo && mVideoTrack == NULL)) {
613 sp<AnotherPacketSource> source = new AnotherPacketSource(format);
614
615 if (isAudio) {
616 mAudioTrack = source;
617 } else {
618 mVideoTrack = source;
619 }
620
621 info.mSource = source;
622 }
623
624 mTracks.push(info);
625 }
626
627 mState = CONNECTED;
628}
629
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100630void NuPlayer::RTSPSource::onSDPLoaded(const sp<AMessage> &msg) {
631 status_t err;
632 CHECK(msg->findInt32("result", &err));
633
634 mSDPLoader.clear();
635
636 if (mDisconnectReplyID != 0) {
637 err = UNKNOWN_ERROR;
638 }
639
640 if (err == OK) {
641 sp<ASessionDescription> desc;
642 sp<RefBase> obj;
643 CHECK(msg->findObject("description", &obj));
644 desc = static_cast<ASessionDescription *>(obj.get());
645
646 AString rtspUri;
647 if (!desc->findAttribute(0, "a=control", &rtspUri)) {
648 ALOGE("Unable to find url in SDP");
649 err = UNKNOWN_ERROR;
650 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800651 sp<AMessage> notify = new AMessage(kWhatNotify, this);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100652
653 mHandler = new MyHandler(rtspUri.c_str(), notify, mUIDValid, mUID);
654 mLooper->registerHandler(mHandler);
655
656 mHandler->loadSDP(desc);
657 }
658 }
659
660 if (err != OK) {
Andreas Huber7f475c32013-02-05 14:47:13 -0800661 if (mState == CONNECTING) {
662 // We're still in the preparation phase, signal that it
663 // failed.
664 notifyPrepared(err);
665 }
666
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100667 mState = DISCONNECTED;
Chong Zhang180d1b92014-12-02 18:35:35 -0800668 setError(err);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100669
670 if (mDisconnectReplyID != 0) {
671 finishDisconnectIfPossible();
672 }
673 }
674}
675
Andreas Huber2bfdd422011-10-11 15:24:07 -0700676void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) {
Fredrik Rosin0ad03bc2013-03-06 13:42:53 +0100677 if (mState == DISCONNECTED) {
678 return;
679 }
680
Andreas Huber2bfdd422011-10-11 15:24:07 -0700681 status_t err;
682 CHECK(msg->findInt32("result", &err));
683 CHECK_NE(err, (status_t)OK);
684
685 mLooper->unregisterHandler(mHandler->id());
686 mHandler.clear();
687
Andreas Huber7f475c32013-02-05 14:47:13 -0800688 if (mState == CONNECTING) {
689 // We're still in the preparation phase, signal that it
690 // failed.
691 notifyPrepared(err);
692 }
693
Andreas Huber2bfdd422011-10-11 15:24:07 -0700694 mState = DISCONNECTED;
Chong Zhang180d1b92014-12-02 18:35:35 -0800695 setError(err);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700696
697 if (mDisconnectReplyID != 0) {
698 finishDisconnectIfPossible();
699 }
700}
701
702void NuPlayer::RTSPSource::finishDisconnectIfPossible() {
703 if (mState != DISCONNECTED) {
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100704 if (mHandler != NULL) {
705 mHandler->disconnect();
706 } else if (mSDPLoader != NULL) {
707 mSDPLoader->cancel();
708 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700709 return;
710 }
711
712 (new AMessage)->postReply(mDisconnectReplyID);
713 mDisconnectReplyID = 0;
714}
715
Chong Zhang180d1b92014-12-02 18:35:35 -0800716void NuPlayer::RTSPSource::setError(status_t err) {
717 Mutex::Autolock _l(mBufferingLock);
718 mFinalResult = err;
719}
720
721void NuPlayer::RTSPSource::startBufferingIfNecessary() {
722 Mutex::Autolock _l(mBufferingLock);
723
724 if (!mBuffering) {
725 mBuffering = true;
726
727 sp<AMessage> notify = dupNotify();
Wei Jiae67ba382016-02-03 01:41:49 +0000728 notify->setInt32("what", kWhatBufferingStart);
Chong Zhang180d1b92014-12-02 18:35:35 -0800729 notify->post();
730 }
731}
732
733bool NuPlayer::RTSPSource::stopBufferingIfNecessary() {
734 Mutex::Autolock _l(mBufferingLock);
735
736 if (mBuffering) {
737 if (!haveSufficientDataOnAllTracks()) {
738 return false;
739 }
740
741 mBuffering = false;
742
743 sp<AMessage> notify = dupNotify();
Wei Jiae67ba382016-02-03 01:41:49 +0000744 notify->setInt32("what", kWhatBufferingEnd);
Chong Zhang180d1b92014-12-02 18:35:35 -0800745 notify->post();
746 }
747
748 return true;
749}
750
Robert Shih8d237a52015-07-13 17:59:36 -0700751void NuPlayer::RTSPSource::finishSeek(status_t err) {
752 CHECK(mSeekReplyID != NULL);
753 sp<AMessage> seekReply = new AMessage;
754 seekReply->setInt32("err", err);
755 seekReply->postReply(mSeekReplyID);
756 mSeekReplyID = NULL;
757}
Chong Zhang180d1b92014-12-02 18:35:35 -0800758
Andreas Huber2bfdd422011-10-11 15:24:07 -0700759} // namespace android