blob: af0351e100b8058ac58e33ee99d1253f839262ef [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 }
Robert Shih6d3cd2e2015-08-13 12:09:52 -0700137 if (mHandler != NULL) {
138 mHandler->pause();
139 }
Roger Jönsson46d13e32013-01-21 17:15:45 +0100140}
141
142void NuPlayer::RTSPSource::resume() {
Robert Shih8d237a52015-07-13 17:59:36 -0700143 if (mHandler != NULL) {
144 mHandler->resume();
145 }
Roger Jönsson46d13e32013-01-21 17:15:45 +0100146}
147
Andreas Huber2bfdd422011-10-11 15:24:07 -0700148status_t NuPlayer::RTSPSource::feedMoreTSData() {
Chong Zhang180d1b92014-12-02 18:35:35 -0800149 Mutex::Autolock _l(mBufferingLock);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700150 return mFinalResult;
151}
152
Andreas Huber84066782011-08-16 09:34:26 -0700153sp<MetaData> NuPlayer::RTSPSource::getFormatMeta(bool audio) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700154 sp<AnotherPacketSource> source = getSource(audio);
155
156 if (source == NULL) {
157 return NULL;
158 }
159
160 return source->getFormat();
161}
162
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700163bool NuPlayer::RTSPSource::haveSufficientDataOnAllTracks() {
164 // We're going to buffer at least 2 secs worth data on all tracks before
165 // starting playback (both at startup and after a seek).
166
167 static const int64_t kMinDurationUs = 2000000ll;
168
Roger Jönssoncfc30832013-01-21 16:26:41 +0100169 int64_t mediaDurationUs = 0;
170 getDuration(&mediaDurationUs);
171 if ((mAudioTrack != NULL && mAudioTrack->isFinished(mediaDurationUs))
172 || (mVideoTrack != NULL && mVideoTrack->isFinished(mediaDurationUs))) {
173 return true;
174 }
175
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700176 status_t err;
177 int64_t durationUs;
178 if (mAudioTrack != NULL
179 && (durationUs = mAudioTrack->getBufferedDurationUs(&err))
180 < kMinDurationUs
181 && err == OK) {
182 ALOGV("audio track doesn't have enough data yet. (%.2f secs buffered)",
183 durationUs / 1E6);
184 return false;
185 }
186
187 if (mVideoTrack != NULL
188 && (durationUs = mVideoTrack->getBufferedDurationUs(&err))
189 < kMinDurationUs
190 && err == OK) {
191 ALOGV("video track doesn't have enough data yet. (%.2f secs buffered)",
192 durationUs / 1E6);
193 return false;
194 }
195
196 return true;
197}
198
Andreas Huber2bfdd422011-10-11 15:24:07 -0700199status_t NuPlayer::RTSPSource::dequeueAccessUnit(
200 bool audio, sp<ABuffer> *accessUnit) {
Chong Zhang180d1b92014-12-02 18:35:35 -0800201 if (!stopBufferingIfNecessary()) {
202 return -EWOULDBLOCK;
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700203 }
204
Andreas Huber2bfdd422011-10-11 15:24:07 -0700205 sp<AnotherPacketSource> source = getSource(audio);
206
207 if (source == NULL) {
208 return -EWOULDBLOCK;
209 }
210
211 status_t finalResult;
212 if (!source->hasBufferAvailable(&finalResult)) {
Roger Jönssoncfc30832013-01-21 16:26:41 +0100213 if (finalResult == OK) {
214 int64_t mediaDurationUs = 0;
215 getDuration(&mediaDurationUs);
216 sp<AnotherPacketSource> otherSource = getSource(!audio);
217 status_t otherFinalResult;
218
219 // If other source already signaled EOS, this source should also signal EOS
220 if (otherSource != NULL &&
221 !otherSource->hasBufferAvailable(&otherFinalResult) &&
222 otherFinalResult == ERROR_END_OF_STREAM) {
223 source->signalEOS(ERROR_END_OF_STREAM);
224 return ERROR_END_OF_STREAM;
225 }
226
227 // If this source has detected near end, give it some time to retrieve more
228 // data before signaling EOS
229 if (source->isFinished(mediaDurationUs)) {
230 int64_t eosTimeout = audio ? mEOSTimeoutAudio : mEOSTimeoutVideo;
231 if (eosTimeout == 0) {
232 setEOSTimeout(audio, ALooper::GetNowUs());
233 } else if ((ALooper::GetNowUs() - eosTimeout) > kNearEOSTimeoutUs) {
234 setEOSTimeout(audio, 0);
235 source->signalEOS(ERROR_END_OF_STREAM);
236 return ERROR_END_OF_STREAM;
237 }
238 return -EWOULDBLOCK;
239 }
240
241 if (!(otherSource != NULL && otherSource->isFinished(mediaDurationUs))) {
242 // We should not enter buffering mode
243 // if any of the sources already have detected EOS.
Chong Zhang180d1b92014-12-02 18:35:35 -0800244 startBufferingIfNecessary();
Roger Jönssoncfc30832013-01-21 16:26:41 +0100245 }
246
247 return -EWOULDBLOCK;
248 }
249 return finalResult;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700250 }
251
Roger Jönssoncfc30832013-01-21 16:26:41 +0100252 setEOSTimeout(audio, 0);
253
Andreas Huber2bfdd422011-10-11 15:24:07 -0700254 return source->dequeueAccessUnit(accessUnit);
255}
256
257sp<AnotherPacketSource> NuPlayer::RTSPSource::getSource(bool audio) {
Andreas Huber49694682012-08-31 10:27:46 -0700258 if (mTSParser != NULL) {
259 sp<MediaSource> source = mTSParser->getSource(
260 audio ? ATSParser::AUDIO : ATSParser::VIDEO);
261
262 return static_cast<AnotherPacketSource *>(source.get());
263 }
264
Andreas Huber2bfdd422011-10-11 15:24:07 -0700265 return audio ? mAudioTrack : mVideoTrack;
266}
267
Roger Jönssoncfc30832013-01-21 16:26:41 +0100268void NuPlayer::RTSPSource::setEOSTimeout(bool audio, int64_t timeout) {
269 if (audio) {
270 mEOSTimeoutAudio = timeout;
271 } else {
272 mEOSTimeoutVideo = timeout;
273 }
274}
275
Andreas Huber2bfdd422011-10-11 15:24:07 -0700276status_t NuPlayer::RTSPSource::getDuration(int64_t *durationUs) {
277 *durationUs = 0ll;
278
279 int64_t audioDurationUs;
280 if (mAudioTrack != NULL
281 && mAudioTrack->getFormat()->findInt64(
282 kKeyDuration, &audioDurationUs)
283 && audioDurationUs > *durationUs) {
284 *durationUs = audioDurationUs;
285 }
286
287 int64_t videoDurationUs;
288 if (mVideoTrack != NULL
289 && mVideoTrack->getFormat()->findInt64(
290 kKeyDuration, &videoDurationUs)
291 && videoDurationUs > *durationUs) {
292 *durationUs = videoDurationUs;
293 }
294
295 return OK;
296}
297
298status_t NuPlayer::RTSPSource::seekTo(int64_t seekTimeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800299 sp<AMessage> msg = new AMessage(kWhatPerformSeek, this);
Andreas Huberee736e92011-12-08 13:04:50 -0800300 msg->setInt32("generation", ++mSeekGeneration);
301 msg->setInt64("timeUs", seekTimeUs);
Andreas Huberee736e92011-12-08 13:04:50 -0800302
Robert Shih8d237a52015-07-13 17:59:36 -0700303 sp<AMessage> response;
304 status_t err = msg->postAndAwaitResponse(&response);
305 if (err == OK && response != NULL) {
306 CHECK(response->findInt32("err", &err));
307 }
308
309 return err;
Andreas Huberee736e92011-12-08 13:04:50 -0800310}
311
312void NuPlayer::RTSPSource::performSeek(int64_t seekTimeUs) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700313 if (mState != CONNECTED) {
Robert Shih8d237a52015-07-13 17:59:36 -0700314 finishSeek(INVALID_OPERATION);
Andreas Huberee736e92011-12-08 13:04:50 -0800315 return;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700316 }
317
318 mState = SEEKING;
319 mHandler->seek(seekTimeUs);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700320}
321
Andreas Huber2bfdd422011-10-11 15:24:07 -0700322void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) {
323 if (msg->what() == kWhatDisconnect) {
Lajos Molnar3f274362015-03-05 14:35:41 -0800324 sp<AReplyToken> replyID;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700325 CHECK(msg->senderAwaitsResponse(&replyID));
326
327 mDisconnectReplyID = replyID;
328 finishDisconnectIfPossible();
329 return;
Andreas Huberee736e92011-12-08 13:04:50 -0800330 } else if (msg->what() == kWhatPerformSeek) {
331 int32_t generation;
332 CHECK(msg->findInt32("generation", &generation));
Robert Shih8d237a52015-07-13 17:59:36 -0700333 CHECK(msg->senderAwaitsResponse(&mSeekReplyID));
Andreas Huberee736e92011-12-08 13:04:50 -0800334
335 if (generation != mSeekGeneration) {
336 // obsolete.
Robert Shih8d237a52015-07-13 17:59:36 -0700337 finishSeek(OK);
Andreas Huberee736e92011-12-08 13:04:50 -0800338 return;
339 }
340
341 int64_t seekTimeUs;
342 CHECK(msg->findInt64("timeUs", &seekTimeUs));
343
344 performSeek(seekTimeUs);
345 return;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700346 }
347
348 CHECK_EQ(msg->what(), (int)kWhatNotify);
349
350 int32_t what;
351 CHECK(msg->findInt32("what", &what));
352
353 switch (what) {
354 case MyHandler::kWhatConnected:
Andreas Huber7f475c32013-02-05 14:47:13 -0800355 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700356 onConnected();
Andreas Huber7f475c32013-02-05 14:47:13 -0800357
Chong Zhangced1c2f2014-08-08 15:22:35 -0700358 notifyVideoSizeChanged();
Andreas Huber7f475c32013-02-05 14:47:13 -0800359
360 uint32_t flags = 0;
361
362 if (mHandler->isSeekable()) {
Chong Zhang4b7069d2013-09-11 12:52:43 -0700363 flags = FLAG_CAN_PAUSE
364 | FLAG_CAN_SEEK
365 | FLAG_CAN_SEEK_BACKWARD
366 | FLAG_CAN_SEEK_FORWARD;
Andreas Huber7f475c32013-02-05 14:47:13 -0800367 }
368
369 notifyFlagsChanged(flags);
370 notifyPrepared();
Andreas Huber2bfdd422011-10-11 15:24:07 -0700371 break;
Andreas Huber7f475c32013-02-05 14:47:13 -0800372 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700373
374 case MyHandler::kWhatDisconnected:
Andreas Huber7f475c32013-02-05 14:47:13 -0800375 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700376 onDisconnected(msg);
377 break;
Andreas Huber7f475c32013-02-05 14:47:13 -0800378 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700379
380 case MyHandler::kWhatSeekDone:
381 {
382 mState = CONNECTED;
Robert Shih8d237a52015-07-13 17:59:36 -0700383 if (mSeekReplyID != NULL) {
384 // Unblock seekTo here in case we attempted to seek in a live stream
385 finishSeek(OK);
386 }
387 break;
388 }
389
390 case MyHandler::kWhatSeekPaused:
391 {
392 sp<AnotherPacketSource> source = getSource(true /* audio */);
393 if (source != NULL) {
394 source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
395 /* extra */ NULL,
396 /* discard */ true);
397 }
398 source = getSource(false /* video */);
399 if (source != NULL) {
400 source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
401 /* extra */ NULL,
402 /* discard */ true);
403 };
404
405 status_t err = OK;
406 msg->findInt32("err", &err);
407 finishSeek(err);
408
409 if (err == OK) {
410 int64_t timeUs;
411 CHECK(msg->findInt64("time", &timeUs));
412 mHandler->continueSeekAfterPause(timeUs);
413 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700414 break;
415 }
416
417 case MyHandler::kWhatAccessUnit:
418 {
419 size_t trackIndex;
420 CHECK(msg->findSize("trackIndex", &trackIndex));
Andreas Huber49694682012-08-31 10:27:46 -0700421
422 if (mTSParser == NULL) {
423 CHECK_LT(trackIndex, mTracks.size());
424 } else {
425 CHECK_EQ(trackIndex, 0u);
426 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700427
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800428 sp<ABuffer> accessUnit;
429 CHECK(msg->findBuffer("accessUnit", &accessUnit));
Andreas Huber2bfdd422011-10-11 15:24:07 -0700430
431 int32_t damaged;
432 if (accessUnit->meta()->findInt32("damaged", &damaged)
433 && damaged) {
Steve Blockdf64d152012-01-04 20:05:49 +0000434 ALOGI("dropping damaged access unit.");
Andreas Huber2bfdd422011-10-11 15:24:07 -0700435 break;
436 }
437
Andreas Huber49694682012-08-31 10:27:46 -0700438 if (mTSParser != NULL) {
439 size_t offset = 0;
440 status_t err = OK;
441 while (offset + 188 <= accessUnit->size()) {
442 err = mTSParser->feedTSPacket(
443 accessUnit->data() + offset, 188);
444 if (err != OK) {
445 break;
446 }
447
448 offset += 188;
449 }
450
451 if (offset < accessUnit->size()) {
452 err = ERROR_MALFORMED;
453 }
454
455 if (err != OK) {
456 sp<AnotherPacketSource> source = getSource(false /* audio */);
457 if (source != NULL) {
458 source->signalEOS(err);
459 }
460
461 source = getSource(true /* audio */);
462 if (source != NULL) {
463 source->signalEOS(err);
464 }
465 }
466 break;
467 }
468
Andreas Huber1906e5c2011-12-08 12:27:47 -0800469 TrackInfo *info = &mTracks.editItemAt(trackIndex);
470
471 sp<AnotherPacketSource> source = info->mSource;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700472 if (source != NULL) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700473 uint32_t rtpTime;
474 CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
475
Andreas Huber1906e5c2011-12-08 12:27:47 -0800476 if (!info->mNPTMappingValid) {
477 // This is a live stream, we didn't receive any normal
Andreas Huberc9d16962012-05-21 11:12:40 -0700478 // playtime mapping. We won't map to npt time.
479 source->queueAccessUnit(accessUnit);
480 break;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800481 }
482
Andreas Huber2bfdd422011-10-11 15:24:07 -0700483 int64_t nptUs =
Andreas Huber1906e5c2011-12-08 12:27:47 -0800484 ((double)rtpTime - (double)info->mRTPTime)
485 / info->mTimeScale
Andreas Huber2bfdd422011-10-11 15:24:07 -0700486 * 1000000ll
Andreas Huber1906e5c2011-12-08 12:27:47 -0800487 + info->mNormalPlaytimeUs;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700488
489 accessUnit->meta()->setInt64("timeUs", nptUs);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700490
491 source->queueAccessUnit(accessUnit);
492 }
493 break;
494 }
495
496 case MyHandler::kWhatEOS:
497 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700498 int32_t finalResult;
499 CHECK(msg->findInt32("finalResult", &finalResult));
500 CHECK_NE(finalResult, (status_t)OK);
501
Andreas Huber49694682012-08-31 10:27:46 -0700502 if (mTSParser != NULL) {
503 sp<AnotherPacketSource> source = getSource(false /* audio */);
504 if (source != NULL) {
505 source->signalEOS(finalResult);
506 }
507
508 source = getSource(true /* audio */);
509 if (source != NULL) {
510 source->signalEOS(finalResult);
511 }
512
513 return;
514 }
515
516 size_t trackIndex;
517 CHECK(msg->findSize("trackIndex", &trackIndex));
518 CHECK_LT(trackIndex, mTracks.size());
519
Andreas Huber2bfdd422011-10-11 15:24:07 -0700520 TrackInfo *info = &mTracks.editItemAt(trackIndex);
521 sp<AnotherPacketSource> source = info->mSource;
522 if (source != NULL) {
523 source->signalEOS(finalResult);
524 }
525
526 break;
527 }
528
529 case MyHandler::kWhatSeekDiscontinuity:
530 {
531 size_t trackIndex;
532 CHECK(msg->findSize("trackIndex", &trackIndex));
533 CHECK_LT(trackIndex, mTracks.size());
534
535 TrackInfo *info = &mTracks.editItemAt(trackIndex);
536 sp<AnotherPacketSource> source = info->mSource;
537 if (source != NULL) {
Chong Zhang632740c2014-06-26 13:03:47 -0700538 source->queueDiscontinuity(
Wei Jiafef808d2014-10-31 17:57:05 -0700539 ATSParser::DISCONTINUITY_TIME,
Chong Zhang632740c2014-06-26 13:03:47 -0700540 NULL,
541 true /* discard */);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700542 }
543
544 break;
545 }
546
547 case MyHandler::kWhatNormalPlayTimeMapping:
548 {
549 size_t trackIndex;
550 CHECK(msg->findSize("trackIndex", &trackIndex));
551 CHECK_LT(trackIndex, mTracks.size());
552
553 uint32_t rtpTime;
554 CHECK(msg->findInt32("rtpTime", (int32_t *)&rtpTime));
555
556 int64_t nptUs;
557 CHECK(msg->findInt64("nptUs", &nptUs));
558
559 TrackInfo *info = &mTracks.editItemAt(trackIndex);
560 info->mRTPTime = rtpTime;
561 info->mNormalPlaytimeUs = nptUs;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800562 info->mNPTMappingValid = true;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700563 break;
564 }
565
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100566 case SDPLoader::kWhatSDPLoaded:
567 {
568 onSDPLoaded(msg);
569 break;
570 }
571
Andreas Huber2bfdd422011-10-11 15:24:07 -0700572 default:
573 TRESPASS();
574 }
575}
576
577void NuPlayer::RTSPSource::onConnected() {
578 CHECK(mAudioTrack == NULL);
579 CHECK(mVideoTrack == NULL);
580
581 size_t numTracks = mHandler->countTracks();
582 for (size_t i = 0; i < numTracks; ++i) {
583 int32_t timeScale;
584 sp<MetaData> format = mHandler->getTrackFormat(i, &timeScale);
585
586 const char *mime;
587 CHECK(format->findCString(kKeyMIMEType, &mime));
588
Andreas Huber49694682012-08-31 10:27:46 -0700589 if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) {
590 // Very special case for MPEG2 Transport Streams.
591 CHECK_EQ(numTracks, 1u);
592
593 mTSParser = new ATSParser;
594 return;
595 }
596
Andreas Huber2bfdd422011-10-11 15:24:07 -0700597 bool isAudio = !strncasecmp(mime, "audio/", 6);
598 bool isVideo = !strncasecmp(mime, "video/", 6);
599
600 TrackInfo info;
601 info.mTimeScale = timeScale;
602 info.mRTPTime = 0;
603 info.mNormalPlaytimeUs = 0ll;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800604 info.mNPTMappingValid = false;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700605
606 if ((isAudio && mAudioTrack == NULL)
607 || (isVideo && mVideoTrack == NULL)) {
608 sp<AnotherPacketSource> source = new AnotherPacketSource(format);
609
610 if (isAudio) {
611 mAudioTrack = source;
612 } else {
613 mVideoTrack = source;
614 }
615
616 info.mSource = source;
617 }
618
619 mTracks.push(info);
620 }
621
622 mState = CONNECTED;
623}
624
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100625void NuPlayer::RTSPSource::onSDPLoaded(const sp<AMessage> &msg) {
626 status_t err;
627 CHECK(msg->findInt32("result", &err));
628
629 mSDPLoader.clear();
630
631 if (mDisconnectReplyID != 0) {
632 err = UNKNOWN_ERROR;
633 }
634
635 if (err == OK) {
636 sp<ASessionDescription> desc;
637 sp<RefBase> obj;
638 CHECK(msg->findObject("description", &obj));
639 desc = static_cast<ASessionDescription *>(obj.get());
640
641 AString rtspUri;
642 if (!desc->findAttribute(0, "a=control", &rtspUri)) {
643 ALOGE("Unable to find url in SDP");
644 err = UNKNOWN_ERROR;
645 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800646 sp<AMessage> notify = new AMessage(kWhatNotify, this);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100647
648 mHandler = new MyHandler(rtspUri.c_str(), notify, mUIDValid, mUID);
649 mLooper->registerHandler(mHandler);
650
651 mHandler->loadSDP(desc);
652 }
653 }
654
655 if (err != OK) {
Andreas Huber7f475c32013-02-05 14:47:13 -0800656 if (mState == CONNECTING) {
657 // We're still in the preparation phase, signal that it
658 // failed.
659 notifyPrepared(err);
660 }
661
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100662 mState = DISCONNECTED;
Chong Zhang180d1b92014-12-02 18:35:35 -0800663 setError(err);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100664
665 if (mDisconnectReplyID != 0) {
666 finishDisconnectIfPossible();
667 }
668 }
669}
670
Andreas Huber2bfdd422011-10-11 15:24:07 -0700671void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) {
Fredrik Rosin0ad03bc2013-03-06 13:42:53 +0100672 if (mState == DISCONNECTED) {
673 return;
674 }
675
Andreas Huber2bfdd422011-10-11 15:24:07 -0700676 status_t err;
677 CHECK(msg->findInt32("result", &err));
678 CHECK_NE(err, (status_t)OK);
679
680 mLooper->unregisterHandler(mHandler->id());
681 mHandler.clear();
682
Andreas Huber7f475c32013-02-05 14:47:13 -0800683 if (mState == CONNECTING) {
684 // We're still in the preparation phase, signal that it
685 // failed.
686 notifyPrepared(err);
687 }
688
Andreas Huber2bfdd422011-10-11 15:24:07 -0700689 mState = DISCONNECTED;
Chong Zhang180d1b92014-12-02 18:35:35 -0800690 setError(err);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700691
692 if (mDisconnectReplyID != 0) {
693 finishDisconnectIfPossible();
694 }
695}
696
697void NuPlayer::RTSPSource::finishDisconnectIfPossible() {
698 if (mState != DISCONNECTED) {
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100699 if (mHandler != NULL) {
700 mHandler->disconnect();
701 } else if (mSDPLoader != NULL) {
702 mSDPLoader->cancel();
703 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700704 return;
705 }
706
707 (new AMessage)->postReply(mDisconnectReplyID);
708 mDisconnectReplyID = 0;
709}
710
Chong Zhang180d1b92014-12-02 18:35:35 -0800711void NuPlayer::RTSPSource::setError(status_t err) {
712 Mutex::Autolock _l(mBufferingLock);
713 mFinalResult = err;
714}
715
716void NuPlayer::RTSPSource::startBufferingIfNecessary() {
717 Mutex::Autolock _l(mBufferingLock);
718
719 if (!mBuffering) {
720 mBuffering = true;
721
722 sp<AMessage> notify = dupNotify();
723 notify->setInt32("what", kWhatBufferingStart);
724 notify->post();
725 }
726}
727
728bool NuPlayer::RTSPSource::stopBufferingIfNecessary() {
729 Mutex::Autolock _l(mBufferingLock);
730
731 if (mBuffering) {
732 if (!haveSufficientDataOnAllTracks()) {
733 return false;
734 }
735
736 mBuffering = false;
737
738 sp<AMessage> notify = dupNotify();
739 notify->setInt32("what", kWhatBufferingEnd);
740 notify->post();
741 }
742
743 return true;
744}
745
Robert Shih8d237a52015-07-13 17:59:36 -0700746void NuPlayer::RTSPSource::finishSeek(status_t err) {
747 CHECK(mSeekReplyID != NULL);
748 sp<AMessage> seekReply = new AMessage;
749 seekReply->setInt32("err", err);
750 seekReply->postReply(mSeekReplyID);
751 mSeekReplyID = NULL;
752}
Chong Zhang180d1b92014-12-02 18:35:35 -0800753
Andreas Huber2bfdd422011-10-11 15:24:07 -0700754} // namespace android