blob: ec33478e776e68e2681b0c8dd7288397feed9bb1 [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;
Wei Jia4ad74b22016-02-05 17:11:20 -0800388 // Unblock seekTo here in case we attempted to seek in a live stream
389 finishSeek(OK);
Robert Shih8d237a52015-07-13 17:59:36 -0700390 break;
391 }
392
393 case MyHandler::kWhatSeekPaused:
394 {
395 sp<AnotherPacketSource> source = getSource(true /* audio */);
396 if (source != NULL) {
397 source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
398 /* extra */ NULL,
399 /* discard */ true);
400 }
401 source = getSource(false /* video */);
402 if (source != NULL) {
403 source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
404 /* extra */ NULL,
405 /* discard */ true);
406 };
407
408 status_t err = OK;
409 msg->findInt32("err", &err);
Robert Shih8d237a52015-07-13 17:59:36 -0700410
411 if (err == OK) {
412 int64_t timeUs;
413 CHECK(msg->findInt64("time", &timeUs));
414 mHandler->continueSeekAfterPause(timeUs);
Wei Jia4ad74b22016-02-05 17:11:20 -0800415 } else {
416 finishSeek(err);
Robert Shih8d237a52015-07-13 17:59:36 -0700417 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700418 break;
419 }
420
421 case MyHandler::kWhatAccessUnit:
422 {
423 size_t trackIndex;
424 CHECK(msg->findSize("trackIndex", &trackIndex));
Andreas Huber49694682012-08-31 10:27:46 -0700425
426 if (mTSParser == NULL) {
427 CHECK_LT(trackIndex, mTracks.size());
428 } else {
429 CHECK_EQ(trackIndex, 0u);
430 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700431
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800432 sp<ABuffer> accessUnit;
433 CHECK(msg->findBuffer("accessUnit", &accessUnit));
Andreas Huber2bfdd422011-10-11 15:24:07 -0700434
435 int32_t damaged;
436 if (accessUnit->meta()->findInt32("damaged", &damaged)
437 && damaged) {
Steve Blockdf64d152012-01-04 20:05:49 +0000438 ALOGI("dropping damaged access unit.");
Andreas Huber2bfdd422011-10-11 15:24:07 -0700439 break;
440 }
441
Andreas Huber49694682012-08-31 10:27:46 -0700442 if (mTSParser != NULL) {
443 size_t offset = 0;
444 status_t err = OK;
445 while (offset + 188 <= accessUnit->size()) {
446 err = mTSParser->feedTSPacket(
447 accessUnit->data() + offset, 188);
448 if (err != OK) {
449 break;
450 }
451
452 offset += 188;
453 }
454
455 if (offset < accessUnit->size()) {
456 err = ERROR_MALFORMED;
457 }
458
459 if (err != OK) {
460 sp<AnotherPacketSource> source = getSource(false /* audio */);
461 if (source != NULL) {
462 source->signalEOS(err);
463 }
464
465 source = getSource(true /* audio */);
466 if (source != NULL) {
467 source->signalEOS(err);
468 }
469 }
470 break;
471 }
472
Andreas Huber1906e5c2011-12-08 12:27:47 -0800473 TrackInfo *info = &mTracks.editItemAt(trackIndex);
474
475 sp<AnotherPacketSource> source = info->mSource;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700476 if (source != NULL) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700477 uint32_t rtpTime;
478 CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
479
Andreas Huber1906e5c2011-12-08 12:27:47 -0800480 if (!info->mNPTMappingValid) {
481 // This is a live stream, we didn't receive any normal
Andreas Huberc9d16962012-05-21 11:12:40 -0700482 // playtime mapping. We won't map to npt time.
483 source->queueAccessUnit(accessUnit);
484 break;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800485 }
486
Andreas Huber2bfdd422011-10-11 15:24:07 -0700487 int64_t nptUs =
Andreas Huber1906e5c2011-12-08 12:27:47 -0800488 ((double)rtpTime - (double)info->mRTPTime)
489 / info->mTimeScale
Andreas Huber2bfdd422011-10-11 15:24:07 -0700490 * 1000000ll
Andreas Huber1906e5c2011-12-08 12:27:47 -0800491 + info->mNormalPlaytimeUs;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700492
493 accessUnit->meta()->setInt64("timeUs", nptUs);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700494
495 source->queueAccessUnit(accessUnit);
496 }
497 break;
498 }
499
500 case MyHandler::kWhatEOS:
501 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700502 int32_t finalResult;
503 CHECK(msg->findInt32("finalResult", &finalResult));
504 CHECK_NE(finalResult, (status_t)OK);
505
Andreas Huber49694682012-08-31 10:27:46 -0700506 if (mTSParser != NULL) {
507 sp<AnotherPacketSource> source = getSource(false /* audio */);
508 if (source != NULL) {
509 source->signalEOS(finalResult);
510 }
511
512 source = getSource(true /* audio */);
513 if (source != NULL) {
514 source->signalEOS(finalResult);
515 }
516
517 return;
518 }
519
520 size_t trackIndex;
521 CHECK(msg->findSize("trackIndex", &trackIndex));
522 CHECK_LT(trackIndex, mTracks.size());
523
Andreas Huber2bfdd422011-10-11 15:24:07 -0700524 TrackInfo *info = &mTracks.editItemAt(trackIndex);
525 sp<AnotherPacketSource> source = info->mSource;
526 if (source != NULL) {
527 source->signalEOS(finalResult);
528 }
529
530 break;
531 }
532
533 case MyHandler::kWhatSeekDiscontinuity:
534 {
535 size_t trackIndex;
536 CHECK(msg->findSize("trackIndex", &trackIndex));
537 CHECK_LT(trackIndex, mTracks.size());
538
539 TrackInfo *info = &mTracks.editItemAt(trackIndex);
540 sp<AnotherPacketSource> source = info->mSource;
541 if (source != NULL) {
Chong Zhang632740c2014-06-26 13:03:47 -0700542 source->queueDiscontinuity(
Wei Jiafef808d2014-10-31 17:57:05 -0700543 ATSParser::DISCONTINUITY_TIME,
Chong Zhang632740c2014-06-26 13:03:47 -0700544 NULL,
545 true /* discard */);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700546 }
547
548 break;
549 }
550
551 case MyHandler::kWhatNormalPlayTimeMapping:
552 {
553 size_t trackIndex;
554 CHECK(msg->findSize("trackIndex", &trackIndex));
555 CHECK_LT(trackIndex, mTracks.size());
556
557 uint32_t rtpTime;
558 CHECK(msg->findInt32("rtpTime", (int32_t *)&rtpTime));
559
560 int64_t nptUs;
561 CHECK(msg->findInt64("nptUs", &nptUs));
562
563 TrackInfo *info = &mTracks.editItemAt(trackIndex);
564 info->mRTPTime = rtpTime;
565 info->mNormalPlaytimeUs = nptUs;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800566 info->mNPTMappingValid = true;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700567 break;
568 }
569
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100570 case SDPLoader::kWhatSDPLoaded:
571 {
572 onSDPLoaded(msg);
573 break;
574 }
575
Andreas Huber2bfdd422011-10-11 15:24:07 -0700576 default:
577 TRESPASS();
578 }
579}
580
581void NuPlayer::RTSPSource::onConnected() {
582 CHECK(mAudioTrack == NULL);
583 CHECK(mVideoTrack == NULL);
584
585 size_t numTracks = mHandler->countTracks();
586 for (size_t i = 0; i < numTracks; ++i) {
587 int32_t timeScale;
588 sp<MetaData> format = mHandler->getTrackFormat(i, &timeScale);
589
590 const char *mime;
591 CHECK(format->findCString(kKeyMIMEType, &mime));
592
Andreas Huber49694682012-08-31 10:27:46 -0700593 if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) {
594 // Very special case for MPEG2 Transport Streams.
595 CHECK_EQ(numTracks, 1u);
596
597 mTSParser = new ATSParser;
598 return;
599 }
600
Andreas Huber2bfdd422011-10-11 15:24:07 -0700601 bool isAudio = !strncasecmp(mime, "audio/", 6);
602 bool isVideo = !strncasecmp(mime, "video/", 6);
603
604 TrackInfo info;
605 info.mTimeScale = timeScale;
606 info.mRTPTime = 0;
607 info.mNormalPlaytimeUs = 0ll;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800608 info.mNPTMappingValid = false;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700609
610 if ((isAudio && mAudioTrack == NULL)
611 || (isVideo && mVideoTrack == NULL)) {
612 sp<AnotherPacketSource> source = new AnotherPacketSource(format);
613
614 if (isAudio) {
615 mAudioTrack = source;
616 } else {
617 mVideoTrack = source;
618 }
619
620 info.mSource = source;
621 }
622
623 mTracks.push(info);
624 }
625
626 mState = CONNECTED;
627}
628
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100629void NuPlayer::RTSPSource::onSDPLoaded(const sp<AMessage> &msg) {
630 status_t err;
631 CHECK(msg->findInt32("result", &err));
632
633 mSDPLoader.clear();
634
635 if (mDisconnectReplyID != 0) {
636 err = UNKNOWN_ERROR;
637 }
638
639 if (err == OK) {
640 sp<ASessionDescription> desc;
641 sp<RefBase> obj;
642 CHECK(msg->findObject("description", &obj));
643 desc = static_cast<ASessionDescription *>(obj.get());
644
645 AString rtspUri;
646 if (!desc->findAttribute(0, "a=control", &rtspUri)) {
647 ALOGE("Unable to find url in SDP");
648 err = UNKNOWN_ERROR;
649 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800650 sp<AMessage> notify = new AMessage(kWhatNotify, this);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100651
652 mHandler = new MyHandler(rtspUri.c_str(), notify, mUIDValid, mUID);
653 mLooper->registerHandler(mHandler);
654
655 mHandler->loadSDP(desc);
656 }
657 }
658
659 if (err != OK) {
Andreas Huber7f475c32013-02-05 14:47:13 -0800660 if (mState == CONNECTING) {
661 // We're still in the preparation phase, signal that it
662 // failed.
663 notifyPrepared(err);
664 }
665
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100666 mState = DISCONNECTED;
Chong Zhang180d1b92014-12-02 18:35:35 -0800667 setError(err);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100668
669 if (mDisconnectReplyID != 0) {
670 finishDisconnectIfPossible();
671 }
672 }
673}
674
Andreas Huber2bfdd422011-10-11 15:24:07 -0700675void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) {
Fredrik Rosin0ad03bc2013-03-06 13:42:53 +0100676 if (mState == DISCONNECTED) {
677 return;
678 }
679
Andreas Huber2bfdd422011-10-11 15:24:07 -0700680 status_t err;
681 CHECK(msg->findInt32("result", &err));
682 CHECK_NE(err, (status_t)OK);
683
684 mLooper->unregisterHandler(mHandler->id());
685 mHandler.clear();
686
Andreas Huber7f475c32013-02-05 14:47:13 -0800687 if (mState == CONNECTING) {
688 // We're still in the preparation phase, signal that it
689 // failed.
690 notifyPrepared(err);
691 }
692
Andreas Huber2bfdd422011-10-11 15:24:07 -0700693 mState = DISCONNECTED;
Chong Zhang180d1b92014-12-02 18:35:35 -0800694 setError(err);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700695
696 if (mDisconnectReplyID != 0) {
697 finishDisconnectIfPossible();
698 }
699}
700
701void NuPlayer::RTSPSource::finishDisconnectIfPossible() {
702 if (mState != DISCONNECTED) {
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100703 if (mHandler != NULL) {
704 mHandler->disconnect();
705 } else if (mSDPLoader != NULL) {
706 mSDPLoader->cancel();
707 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700708 return;
709 }
710
711 (new AMessage)->postReply(mDisconnectReplyID);
712 mDisconnectReplyID = 0;
713}
714
Chong Zhang180d1b92014-12-02 18:35:35 -0800715void NuPlayer::RTSPSource::setError(status_t err) {
716 Mutex::Autolock _l(mBufferingLock);
717 mFinalResult = err;
718}
719
720void NuPlayer::RTSPSource::startBufferingIfNecessary() {
721 Mutex::Autolock _l(mBufferingLock);
722
723 if (!mBuffering) {
724 mBuffering = true;
725
726 sp<AMessage> notify = dupNotify();
Wei Jiae67ba382016-02-03 01:41:49 +0000727 notify->setInt32("what", kWhatBufferingStart);
Chong Zhang180d1b92014-12-02 18:35:35 -0800728 notify->post();
729 }
730}
731
732bool NuPlayer::RTSPSource::stopBufferingIfNecessary() {
733 Mutex::Autolock _l(mBufferingLock);
734
735 if (mBuffering) {
736 if (!haveSufficientDataOnAllTracks()) {
737 return false;
738 }
739
740 mBuffering = false;
741
742 sp<AMessage> notify = dupNotify();
Wei Jiae67ba382016-02-03 01:41:49 +0000743 notify->setInt32("what", kWhatBufferingEnd);
Chong Zhang180d1b92014-12-02 18:35:35 -0800744 notify->post();
745 }
746
747 return true;
748}
749
Robert Shih8d237a52015-07-13 17:59:36 -0700750void NuPlayer::RTSPSource::finishSeek(status_t err) {
Wei Jia4ad74b22016-02-05 17:11:20 -0800751 if (mSeekReplyID == NULL) {
752 return;
753 }
Robert Shih8d237a52015-07-13 17:59:36 -0700754 sp<AMessage> seekReply = new AMessage;
755 seekReply->setInt32("err", err);
756 seekReply->postReply(mSeekReplyID);
757 mSeekReplyID = NULL;
758}
Chong Zhang180d1b92014-12-02 18:35:35 -0800759
Andreas Huber2bfdd422011-10-11 15:24:07 -0700760} // namespace android