blob: 58ff113321c7dce67326b52009c27654f3742f5c [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() {
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
Lajos Molnar1d15ab52015-03-04 16:46:34 -080090 sp<AMessage> notify = new AMessage(kWhatNotify, this);
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
Chong Zhang180d1b92014-12-02 18:35:35 -0800109 startBufferingIfNecessary();
Andreas Huber57cea552013-02-05 13:59:56 -0800110}
111
112void NuPlayer::RTSPSource::start() {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700113}
114
115void NuPlayer::RTSPSource::stop() {
James Dong58341812012-11-16 14:31:15 -0800116 if (mLooper == NULL) {
117 return;
118 }
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800119 sp<AMessage> msg = new AMessage(kWhatDisconnect, this);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700120
121 sp<AMessage> dummy;
122 msg->postAndAwaitResponse(&dummy);
123}
124
Roger Jönsson46d13e32013-01-21 17:15:45 +0100125void NuPlayer::RTSPSource::pause() {
126 int64_t mediaDurationUs = 0;
127 getDuration(&mediaDurationUs);
128 for (size_t index = 0; index < mTracks.size(); index++) {
129 TrackInfo *info = &mTracks.editItemAt(index);
130 sp<AnotherPacketSource> source = info->mSource;
131
132 // Check if EOS or ERROR is received
133 if (source != NULL && source->isFinished(mediaDurationUs)) {
134 return;
135 }
136 }
137 mHandler->pause();
138}
139
140void NuPlayer::RTSPSource::resume() {
Robert Shih8d237a52015-07-13 17:59:36 -0700141 if (mHandler != NULL) {
142 mHandler->resume();
143 }
Roger Jönsson46d13e32013-01-21 17:15:45 +0100144}
145
Andreas Huber2bfdd422011-10-11 15:24:07 -0700146status_t NuPlayer::RTSPSource::feedMoreTSData() {
Chong Zhang180d1b92014-12-02 18:35:35 -0800147 Mutex::Autolock _l(mBufferingLock);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700148 return mFinalResult;
149}
150
Andreas Huber84066782011-08-16 09:34:26 -0700151sp<MetaData> NuPlayer::RTSPSource::getFormatMeta(bool audio) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700152 sp<AnotherPacketSource> source = getSource(audio);
153
154 if (source == NULL) {
155 return NULL;
156 }
157
158 return source->getFormat();
159}
160
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700161bool NuPlayer::RTSPSource::haveSufficientDataOnAllTracks() {
162 // We're going to buffer at least 2 secs worth data on all tracks before
163 // starting playback (both at startup and after a seek).
164
165 static const int64_t kMinDurationUs = 2000000ll;
166
Roger Jönssoncfc30832013-01-21 16:26:41 +0100167 int64_t mediaDurationUs = 0;
168 getDuration(&mediaDurationUs);
169 if ((mAudioTrack != NULL && mAudioTrack->isFinished(mediaDurationUs))
170 || (mVideoTrack != NULL && mVideoTrack->isFinished(mediaDurationUs))) {
171 return true;
172 }
173
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700174 status_t err;
175 int64_t durationUs;
176 if (mAudioTrack != NULL
177 && (durationUs = mAudioTrack->getBufferedDurationUs(&err))
178 < kMinDurationUs
179 && err == OK) {
180 ALOGV("audio track doesn't have enough data yet. (%.2f secs buffered)",
181 durationUs / 1E6);
182 return false;
183 }
184
185 if (mVideoTrack != NULL
186 && (durationUs = mVideoTrack->getBufferedDurationUs(&err))
187 < kMinDurationUs
188 && err == OK) {
189 ALOGV("video track doesn't have enough data yet. (%.2f secs buffered)",
190 durationUs / 1E6);
191 return false;
192 }
193
194 return true;
195}
196
Andreas Huber2bfdd422011-10-11 15:24:07 -0700197status_t NuPlayer::RTSPSource::dequeueAccessUnit(
198 bool audio, sp<ABuffer> *accessUnit) {
Chong Zhang180d1b92014-12-02 18:35:35 -0800199 if (!stopBufferingIfNecessary()) {
200 return -EWOULDBLOCK;
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700201 }
202
Andreas Huber2bfdd422011-10-11 15:24:07 -0700203 sp<AnotherPacketSource> source = getSource(audio);
204
205 if (source == NULL) {
206 return -EWOULDBLOCK;
207 }
208
209 status_t finalResult;
210 if (!source->hasBufferAvailable(&finalResult)) {
Roger Jönssoncfc30832013-01-21 16:26:41 +0100211 if (finalResult == OK) {
212 int64_t mediaDurationUs = 0;
213 getDuration(&mediaDurationUs);
214 sp<AnotherPacketSource> otherSource = getSource(!audio);
215 status_t otherFinalResult;
216
217 // If other source already signaled EOS, this source should also signal EOS
218 if (otherSource != NULL &&
219 !otherSource->hasBufferAvailable(&otherFinalResult) &&
220 otherFinalResult == ERROR_END_OF_STREAM) {
221 source->signalEOS(ERROR_END_OF_STREAM);
222 return ERROR_END_OF_STREAM;
223 }
224
225 // If this source has detected near end, give it some time to retrieve more
226 // data before signaling EOS
227 if (source->isFinished(mediaDurationUs)) {
228 int64_t eosTimeout = audio ? mEOSTimeoutAudio : mEOSTimeoutVideo;
229 if (eosTimeout == 0) {
230 setEOSTimeout(audio, ALooper::GetNowUs());
231 } else if ((ALooper::GetNowUs() - eosTimeout) > kNearEOSTimeoutUs) {
232 setEOSTimeout(audio, 0);
233 source->signalEOS(ERROR_END_OF_STREAM);
234 return ERROR_END_OF_STREAM;
235 }
236 return -EWOULDBLOCK;
237 }
238
239 if (!(otherSource != NULL && otherSource->isFinished(mediaDurationUs))) {
240 // We should not enter buffering mode
241 // if any of the sources already have detected EOS.
Chong Zhang180d1b92014-12-02 18:35:35 -0800242 startBufferingIfNecessary();
Roger Jönssoncfc30832013-01-21 16:26:41 +0100243 }
244
245 return -EWOULDBLOCK;
246 }
247 return finalResult;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700248 }
249
Roger Jönssoncfc30832013-01-21 16:26:41 +0100250 setEOSTimeout(audio, 0);
251
Andreas Huber2bfdd422011-10-11 15:24:07 -0700252 return source->dequeueAccessUnit(accessUnit);
253}
254
255sp<AnotherPacketSource> NuPlayer::RTSPSource::getSource(bool audio) {
Andreas Huber49694682012-08-31 10:27:46 -0700256 if (mTSParser != NULL) {
257 sp<MediaSource> source = mTSParser->getSource(
258 audio ? ATSParser::AUDIO : ATSParser::VIDEO);
259
260 return static_cast<AnotherPacketSource *>(source.get());
261 }
262
Andreas Huber2bfdd422011-10-11 15:24:07 -0700263 return audio ? mAudioTrack : mVideoTrack;
264}
265
Roger Jönssoncfc30832013-01-21 16:26:41 +0100266void NuPlayer::RTSPSource::setEOSTimeout(bool audio, int64_t timeout) {
267 if (audio) {
268 mEOSTimeoutAudio = timeout;
269 } else {
270 mEOSTimeoutVideo = timeout;
271 }
272}
273
Andreas Huber2bfdd422011-10-11 15:24:07 -0700274status_t NuPlayer::RTSPSource::getDuration(int64_t *durationUs) {
275 *durationUs = 0ll;
276
277 int64_t audioDurationUs;
278 if (mAudioTrack != NULL
279 && mAudioTrack->getFormat()->findInt64(
280 kKeyDuration, &audioDurationUs)
281 && audioDurationUs > *durationUs) {
282 *durationUs = audioDurationUs;
283 }
284
285 int64_t videoDurationUs;
286 if (mVideoTrack != NULL
287 && mVideoTrack->getFormat()->findInt64(
288 kKeyDuration, &videoDurationUs)
289 && videoDurationUs > *durationUs) {
290 *durationUs = videoDurationUs;
291 }
292
293 return OK;
294}
295
296status_t NuPlayer::RTSPSource::seekTo(int64_t seekTimeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800297 sp<AMessage> msg = new AMessage(kWhatPerformSeek, this);
Andreas Huberee736e92011-12-08 13:04:50 -0800298 msg->setInt32("generation", ++mSeekGeneration);
299 msg->setInt64("timeUs", seekTimeUs);
Andreas Huberee736e92011-12-08 13:04:50 -0800300
Robert Shih8d237a52015-07-13 17:59:36 -0700301 sp<AMessage> response;
302 status_t err = msg->postAndAwaitResponse(&response);
303 if (err == OK && response != NULL) {
304 CHECK(response->findInt32("err", &err));
305 }
306
307 return err;
Andreas Huberee736e92011-12-08 13:04:50 -0800308}
309
310void NuPlayer::RTSPSource::performSeek(int64_t seekTimeUs) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700311 if (mState != CONNECTED) {
Robert Shih8d237a52015-07-13 17:59:36 -0700312 finishSeek(INVALID_OPERATION);
Andreas Huberee736e92011-12-08 13:04:50 -0800313 return;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700314 }
315
316 mState = SEEKING;
317 mHandler->seek(seekTimeUs);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700318}
319
Andreas Huber2bfdd422011-10-11 15:24:07 -0700320void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) {
321 if (msg->what() == kWhatDisconnect) {
Lajos Molnar3f274362015-03-05 14:35:41 -0800322 sp<AReplyToken> replyID;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700323 CHECK(msg->senderAwaitsResponse(&replyID));
324
325 mDisconnectReplyID = replyID;
326 finishDisconnectIfPossible();
327 return;
Andreas Huberee736e92011-12-08 13:04:50 -0800328 } else if (msg->what() == kWhatPerformSeek) {
329 int32_t generation;
330 CHECK(msg->findInt32("generation", &generation));
Robert Shih8d237a52015-07-13 17:59:36 -0700331 CHECK(msg->senderAwaitsResponse(&mSeekReplyID));
Andreas Huberee736e92011-12-08 13:04:50 -0800332
333 if (generation != mSeekGeneration) {
334 // obsolete.
Robert Shih8d237a52015-07-13 17:59:36 -0700335 finishSeek(OK);
Andreas Huberee736e92011-12-08 13:04:50 -0800336 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
Chong Zhangced1c2f2014-08-08 15:22:35 -0700356 notifyVideoSizeChanged();
Andreas Huber7f475c32013-02-05 14:47:13 -0800357
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;
Robert Shih8d237a52015-07-13 17:59:36 -0700381 if (mSeekReplyID != NULL) {
382 // Unblock seekTo here in case we attempted to seek in a live stream
383 finishSeek(OK);
384 }
385 break;
386 }
387
388 case MyHandler::kWhatSeekPaused:
389 {
390 sp<AnotherPacketSource> source = getSource(true /* audio */);
391 if (source != NULL) {
392 source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
393 /* extra */ NULL,
394 /* discard */ true);
395 }
396 source = getSource(false /* video */);
397 if (source != NULL) {
398 source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
399 /* extra */ NULL,
400 /* discard */ true);
401 };
402
403 status_t err = OK;
404 msg->findInt32("err", &err);
405 finishSeek(err);
406
407 if (err == OK) {
408 int64_t timeUs;
409 CHECK(msg->findInt64("time", &timeUs));
410 mHandler->continueSeekAfterPause(timeUs);
411 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700412 break;
413 }
414
415 case MyHandler::kWhatAccessUnit:
416 {
417 size_t trackIndex;
418 CHECK(msg->findSize("trackIndex", &trackIndex));
Andreas Huber49694682012-08-31 10:27:46 -0700419
420 if (mTSParser == NULL) {
421 CHECK_LT(trackIndex, mTracks.size());
422 } else {
423 CHECK_EQ(trackIndex, 0u);
424 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700425
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800426 sp<ABuffer> accessUnit;
427 CHECK(msg->findBuffer("accessUnit", &accessUnit));
Andreas Huber2bfdd422011-10-11 15:24:07 -0700428
429 int32_t damaged;
430 if (accessUnit->meta()->findInt32("damaged", &damaged)
431 && damaged) {
Steve Blockdf64d152012-01-04 20:05:49 +0000432 ALOGI("dropping damaged access unit.");
Andreas Huber2bfdd422011-10-11 15:24:07 -0700433 break;
434 }
435
Andreas Huber49694682012-08-31 10:27:46 -0700436 if (mTSParser != NULL) {
437 size_t offset = 0;
438 status_t err = OK;
439 while (offset + 188 <= accessUnit->size()) {
440 err = mTSParser->feedTSPacket(
441 accessUnit->data() + offset, 188);
442 if (err != OK) {
443 break;
444 }
445
446 offset += 188;
447 }
448
449 if (offset < accessUnit->size()) {
450 err = ERROR_MALFORMED;
451 }
452
453 if (err != OK) {
454 sp<AnotherPacketSource> source = getSource(false /* audio */);
455 if (source != NULL) {
456 source->signalEOS(err);
457 }
458
459 source = getSource(true /* audio */);
460 if (source != NULL) {
461 source->signalEOS(err);
462 }
463 }
464 break;
465 }
466
Andreas Huber1906e5c2011-12-08 12:27:47 -0800467 TrackInfo *info = &mTracks.editItemAt(trackIndex);
468
469 sp<AnotherPacketSource> source = info->mSource;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700470 if (source != NULL) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700471 uint32_t rtpTime;
472 CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
473
Andreas Huber1906e5c2011-12-08 12:27:47 -0800474 if (!info->mNPTMappingValid) {
475 // This is a live stream, we didn't receive any normal
Andreas Huberc9d16962012-05-21 11:12:40 -0700476 // playtime mapping. We won't map to npt time.
477 source->queueAccessUnit(accessUnit);
478 break;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800479 }
480
Andreas Huber2bfdd422011-10-11 15:24:07 -0700481 int64_t nptUs =
Andreas Huber1906e5c2011-12-08 12:27:47 -0800482 ((double)rtpTime - (double)info->mRTPTime)
483 / info->mTimeScale
Andreas Huber2bfdd422011-10-11 15:24:07 -0700484 * 1000000ll
Andreas Huber1906e5c2011-12-08 12:27:47 -0800485 + info->mNormalPlaytimeUs;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700486
487 accessUnit->meta()->setInt64("timeUs", nptUs);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700488
489 source->queueAccessUnit(accessUnit);
490 }
491 break;
492 }
493
494 case MyHandler::kWhatEOS:
495 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700496 int32_t finalResult;
497 CHECK(msg->findInt32("finalResult", &finalResult));
498 CHECK_NE(finalResult, (status_t)OK);
499
Andreas Huber49694682012-08-31 10:27:46 -0700500 if (mTSParser != NULL) {
501 sp<AnotherPacketSource> source = getSource(false /* audio */);
502 if (source != NULL) {
503 source->signalEOS(finalResult);
504 }
505
506 source = getSource(true /* audio */);
507 if (source != NULL) {
508 source->signalEOS(finalResult);
509 }
510
511 return;
512 }
513
514 size_t trackIndex;
515 CHECK(msg->findSize("trackIndex", &trackIndex));
516 CHECK_LT(trackIndex, mTracks.size());
517
Andreas Huber2bfdd422011-10-11 15:24:07 -0700518 TrackInfo *info = &mTracks.editItemAt(trackIndex);
519 sp<AnotherPacketSource> source = info->mSource;
520 if (source != NULL) {
521 source->signalEOS(finalResult);
522 }
523
524 break;
525 }
526
527 case MyHandler::kWhatSeekDiscontinuity:
528 {
529 size_t trackIndex;
530 CHECK(msg->findSize("trackIndex", &trackIndex));
531 CHECK_LT(trackIndex, mTracks.size());
532
533 TrackInfo *info = &mTracks.editItemAt(trackIndex);
534 sp<AnotherPacketSource> source = info->mSource;
535 if (source != NULL) {
Chong Zhang632740c2014-06-26 13:03:47 -0700536 source->queueDiscontinuity(
Wei Jiafef808d2014-10-31 17:57:05 -0700537 ATSParser::DISCONTINUITY_TIME,
Chong Zhang632740c2014-06-26 13:03:47 -0700538 NULL,
539 true /* discard */);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700540 }
541
542 break;
543 }
544
545 case MyHandler::kWhatNormalPlayTimeMapping:
546 {
547 size_t trackIndex;
548 CHECK(msg->findSize("trackIndex", &trackIndex));
549 CHECK_LT(trackIndex, mTracks.size());
550
551 uint32_t rtpTime;
552 CHECK(msg->findInt32("rtpTime", (int32_t *)&rtpTime));
553
554 int64_t nptUs;
555 CHECK(msg->findInt64("nptUs", &nptUs));
556
557 TrackInfo *info = &mTracks.editItemAt(trackIndex);
558 info->mRTPTime = rtpTime;
559 info->mNormalPlaytimeUs = nptUs;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800560 info->mNPTMappingValid = true;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700561 break;
562 }
563
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100564 case SDPLoader::kWhatSDPLoaded:
565 {
566 onSDPLoaded(msg);
567 break;
568 }
569
Andreas Huber2bfdd422011-10-11 15:24:07 -0700570 default:
571 TRESPASS();
572 }
573}
574
575void NuPlayer::RTSPSource::onConnected() {
576 CHECK(mAudioTrack == NULL);
577 CHECK(mVideoTrack == NULL);
578
579 size_t numTracks = mHandler->countTracks();
580 for (size_t i = 0; i < numTracks; ++i) {
581 int32_t timeScale;
582 sp<MetaData> format = mHandler->getTrackFormat(i, &timeScale);
583
584 const char *mime;
585 CHECK(format->findCString(kKeyMIMEType, &mime));
586
Andreas Huber49694682012-08-31 10:27:46 -0700587 if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) {
588 // Very special case for MPEG2 Transport Streams.
589 CHECK_EQ(numTracks, 1u);
590
591 mTSParser = new ATSParser;
592 return;
593 }
594
Andreas Huber2bfdd422011-10-11 15:24:07 -0700595 bool isAudio = !strncasecmp(mime, "audio/", 6);
596 bool isVideo = !strncasecmp(mime, "video/", 6);
597
598 TrackInfo info;
599 info.mTimeScale = timeScale;
600 info.mRTPTime = 0;
601 info.mNormalPlaytimeUs = 0ll;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800602 info.mNPTMappingValid = false;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700603
604 if ((isAudio && mAudioTrack == NULL)
605 || (isVideo && mVideoTrack == NULL)) {
606 sp<AnotherPacketSource> source = new AnotherPacketSource(format);
607
608 if (isAudio) {
609 mAudioTrack = source;
610 } else {
611 mVideoTrack = source;
612 }
613
614 info.mSource = source;
615 }
616
617 mTracks.push(info);
618 }
619
620 mState = CONNECTED;
621}
622
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100623void NuPlayer::RTSPSource::onSDPLoaded(const sp<AMessage> &msg) {
624 status_t err;
625 CHECK(msg->findInt32("result", &err));
626
627 mSDPLoader.clear();
628
629 if (mDisconnectReplyID != 0) {
630 err = UNKNOWN_ERROR;
631 }
632
633 if (err == OK) {
634 sp<ASessionDescription> desc;
635 sp<RefBase> obj;
636 CHECK(msg->findObject("description", &obj));
637 desc = static_cast<ASessionDescription *>(obj.get());
638
639 AString rtspUri;
640 if (!desc->findAttribute(0, "a=control", &rtspUri)) {
641 ALOGE("Unable to find url in SDP");
642 err = UNKNOWN_ERROR;
643 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800644 sp<AMessage> notify = new AMessage(kWhatNotify, this);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100645
646 mHandler = new MyHandler(rtspUri.c_str(), notify, mUIDValid, mUID);
647 mLooper->registerHandler(mHandler);
648
649 mHandler->loadSDP(desc);
650 }
651 }
652
653 if (err != OK) {
Andreas Huber7f475c32013-02-05 14:47:13 -0800654 if (mState == CONNECTING) {
655 // We're still in the preparation phase, signal that it
656 // failed.
657 notifyPrepared(err);
658 }
659
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100660 mState = DISCONNECTED;
Chong Zhang180d1b92014-12-02 18:35:35 -0800661 setError(err);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100662
663 if (mDisconnectReplyID != 0) {
664 finishDisconnectIfPossible();
665 }
666 }
667}
668
Andreas Huber2bfdd422011-10-11 15:24:07 -0700669void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) {
Fredrik Rosin0ad03bc2013-03-06 13:42:53 +0100670 if (mState == DISCONNECTED) {
671 return;
672 }
673
Andreas Huber2bfdd422011-10-11 15:24:07 -0700674 status_t err;
675 CHECK(msg->findInt32("result", &err));
676 CHECK_NE(err, (status_t)OK);
677
678 mLooper->unregisterHandler(mHandler->id());
679 mHandler.clear();
680
Andreas Huber7f475c32013-02-05 14:47:13 -0800681 if (mState == CONNECTING) {
682 // We're still in the preparation phase, signal that it
683 // failed.
684 notifyPrepared(err);
685 }
686
Andreas Huber2bfdd422011-10-11 15:24:07 -0700687 mState = DISCONNECTED;
Chong Zhang180d1b92014-12-02 18:35:35 -0800688 setError(err);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700689
690 if (mDisconnectReplyID != 0) {
691 finishDisconnectIfPossible();
692 }
693}
694
695void NuPlayer::RTSPSource::finishDisconnectIfPossible() {
696 if (mState != DISCONNECTED) {
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100697 if (mHandler != NULL) {
698 mHandler->disconnect();
699 } else if (mSDPLoader != NULL) {
700 mSDPLoader->cancel();
701 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700702 return;
703 }
704
705 (new AMessage)->postReply(mDisconnectReplyID);
706 mDisconnectReplyID = 0;
707}
708
Chong Zhang180d1b92014-12-02 18:35:35 -0800709void NuPlayer::RTSPSource::setError(status_t err) {
710 Mutex::Autolock _l(mBufferingLock);
711 mFinalResult = err;
712}
713
714void NuPlayer::RTSPSource::startBufferingIfNecessary() {
715 Mutex::Autolock _l(mBufferingLock);
716
717 if (!mBuffering) {
718 mBuffering = true;
719
720 sp<AMessage> notify = dupNotify();
721 notify->setInt32("what", kWhatBufferingStart);
722 notify->post();
723 }
724}
725
726bool NuPlayer::RTSPSource::stopBufferingIfNecessary() {
727 Mutex::Autolock _l(mBufferingLock);
728
729 if (mBuffering) {
730 if (!haveSufficientDataOnAllTracks()) {
731 return false;
732 }
733
734 mBuffering = false;
735
736 sp<AMessage> notify = dupNotify();
737 notify->setInt32("what", kWhatBufferingEnd);
738 notify->post();
739 }
740
741 return true;
742}
743
Robert Shih8d237a52015-07-13 17:59:36 -0700744void NuPlayer::RTSPSource::finishSeek(status_t err) {
745 CHECK(mSeekReplyID != NULL);
746 sp<AMessage> seekReply = new AMessage;
747 seekReply->setInt32("err", err);
748 seekReply->postReply(mSeekReplyID);
749 mSeekReplyID = NULL;
750}
Chong Zhang180d1b92014-12-02 18:35:35 -0800751
Andreas Huber2bfdd422011-10-11 15:24:07 -0700752} // namespace android