blob: 550057d10dfff4545667b1c4a86a1e965a457216 [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
Robert Shih641e0c72016-02-22 11:37:20 -080035// Buffer Underflow/Prepare/StartServer/Overflow Marks
36const int64_t NuPlayer::RTSPSource::kUnderflowMarkUs = 1000000ll;
37const int64_t NuPlayer::RTSPSource::kPrepareMarkUs = 3000000ll;
38const int64_t NuPlayer::RTSPSource::kStartServerMarkUs = 5000000ll;
39const int64_t NuPlayer::RTSPSource::kOverflowMarkUs = 10000000ll;
40
Andreas Huber2bfdd422011-10-11 15:24:07 -070041NuPlayer::RTSPSource::RTSPSource(
Andreas Huber5ab368a2013-02-05 10:14:26 -080042 const sp<AMessage> &notify,
Andreas Huber1b86fe02014-01-29 11:13:26 -080043 const sp<IMediaHTTPService> &httpService,
Andreas Huber2bfdd422011-10-11 15:24:07 -070044 const char *url,
45 const KeyedVector<String8, String8> *headers,
46 bool uidValid,
Oscar Rydhé81dd60e2012-02-20 10:15:48 +010047 uid_t uid,
48 bool isSDP)
Andreas Huber5ab368a2013-02-05 10:14:26 -080049 : Source(notify),
Andreas Huber1b86fe02014-01-29 11:13:26 -080050 mHTTPService(httpService),
Andreas Huber5ab368a2013-02-05 10:14:26 -080051 mURL(url),
Andreas Huber2bfdd422011-10-11 15:24:07 -070052 mUIDValid(uidValid),
53 mUID(uid),
54 mFlags(0),
Oscar Rydhé81dd60e2012-02-20 10:15:48 +010055 mIsSDP(isSDP),
Andreas Huber2bfdd422011-10-11 15:24:07 -070056 mState(DISCONNECTED),
57 mFinalResult(OK),
Andreas Huberee736e92011-12-08 13:04:50 -080058 mDisconnectReplyID(0),
Chong Zhang180d1b92014-12-02 18:35:35 -080059 mBuffering(false),
Robert Shih641e0c72016-02-22 11:37:20 -080060 mInPreparationPhase(true),
Robert Shihae481672016-07-29 16:44:39 -070061 mEOSPending(false),
Roger Jönssoncfc30832013-01-21 16:26:41 +010062 mSeekGeneration(0),
63 mEOSTimeoutAudio(0),
64 mEOSTimeoutVideo(0) {
Andreas Huber2bfdd422011-10-11 15:24:07 -070065 if (headers) {
66 mExtraHeaders = *headers;
67
68 ssize_t index =
69 mExtraHeaders.indexOfKey(String8("x-hide-urls-from-log"));
70
71 if (index >= 0) {
72 mFlags |= kFlagIncognito;
73
74 mExtraHeaders.removeItemsAt(index);
75 }
76 }
77}
78
79NuPlayer::RTSPSource::~RTSPSource() {
Andreas Huber602f5bb2013-04-15 16:18:56 -070080 if (mLooper != NULL) {
Chong Zhang1228d6b2014-08-12 21:25:48 -070081 mLooper->unregisterHandler(id());
Andreas Huber602f5bb2013-04-15 16:18:56 -070082 mLooper->stop();
83 }
Andreas Huber2bfdd422011-10-11 15:24:07 -070084}
85
Andreas Huber57cea552013-02-05 13:59:56 -080086void NuPlayer::RTSPSource::prepareAsync() {
Wei Jiace84b222016-01-14 14:39:42 -080087 if (mIsSDP && mHTTPService == NULL) {
88 notifyPrepared(BAD_VALUE);
89 return;
90 }
91
Andreas Huber2bfdd422011-10-11 15:24:07 -070092 if (mLooper == NULL) {
93 mLooper = new ALooper;
94 mLooper->setName("rtsp");
95 mLooper->start();
96
Chong Zhang1228d6b2014-08-12 21:25:48 -070097 mLooper->registerHandler(this);
Andreas Huber2bfdd422011-10-11 15:24:07 -070098 }
99
100 CHECK(mHandler == NULL);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100101 CHECK(mSDPLoader == NULL);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700102
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800103 sp<AMessage> notify = new AMessage(kWhatNotify, this);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700104
Andreas Huber2bfdd422011-10-11 15:24:07 -0700105 CHECK_EQ(mState, (int)DISCONNECTED);
106 mState = CONNECTING;
107
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100108 if (mIsSDP) {
109 mSDPLoader = new SDPLoader(notify,
110 (mFlags & kFlagIncognito) ? SDPLoader::kFlagIncognito : 0,
Andreas Huber81e68442014-02-05 11:52:33 -0800111 mHTTPService);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100112
Andreas Huber57cea552013-02-05 13:59:56 -0800113 mSDPLoader->load(
114 mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100115 } else {
116 mHandler = new MyHandler(mURL.c_str(), notify, mUIDValid, mUID);
117 mLooper->registerHandler(mHandler);
118
119 mHandler->connect();
120 }
Roger Jönssoncfc30832013-01-21 16:26:41 +0100121
Chong Zhang180d1b92014-12-02 18:35:35 -0800122 startBufferingIfNecessary();
Andreas Huber57cea552013-02-05 13:59:56 -0800123}
124
125void NuPlayer::RTSPSource::start() {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700126}
127
128void NuPlayer::RTSPSource::stop() {
James Dong58341812012-11-16 14:31:15 -0800129 if (mLooper == NULL) {
130 return;
131 }
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800132 sp<AMessage> msg = new AMessage(kWhatDisconnect, this);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700133
134 sp<AMessage> dummy;
135 msg->postAndAwaitResponse(&dummy);
136}
137
138status_t NuPlayer::RTSPSource::feedMoreTSData() {
Chong Zhang180d1b92014-12-02 18:35:35 -0800139 Mutex::Autolock _l(mBufferingLock);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700140 return mFinalResult;
141}
142
Andreas Huber84066782011-08-16 09:34:26 -0700143sp<MetaData> NuPlayer::RTSPSource::getFormatMeta(bool audio) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700144 sp<AnotherPacketSource> source = getSource(audio);
145
146 if (source == NULL) {
147 return NULL;
148 }
149
150 return source->getFormat();
151}
152
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700153bool NuPlayer::RTSPSource::haveSufficientDataOnAllTracks() {
154 // We're going to buffer at least 2 secs worth data on all tracks before
155 // starting playback (both at startup and after a seek).
156
157 static const int64_t kMinDurationUs = 2000000ll;
158
Roger Jönssoncfc30832013-01-21 16:26:41 +0100159 int64_t mediaDurationUs = 0;
160 getDuration(&mediaDurationUs);
161 if ((mAudioTrack != NULL && mAudioTrack->isFinished(mediaDurationUs))
162 || (mVideoTrack != NULL && mVideoTrack->isFinished(mediaDurationUs))) {
163 return true;
164 }
165
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700166 status_t err;
167 int64_t durationUs;
168 if (mAudioTrack != NULL
169 && (durationUs = mAudioTrack->getBufferedDurationUs(&err))
170 < kMinDurationUs
171 && err == OK) {
172 ALOGV("audio track doesn't have enough data yet. (%.2f secs buffered)",
173 durationUs / 1E6);
174 return false;
175 }
176
177 if (mVideoTrack != NULL
178 && (durationUs = mVideoTrack->getBufferedDurationUs(&err))
179 < kMinDurationUs
180 && err == OK) {
181 ALOGV("video track doesn't have enough data yet. (%.2f secs buffered)",
182 durationUs / 1E6);
183 return false;
184 }
185
186 return true;
187}
188
Andreas Huber2bfdd422011-10-11 15:24:07 -0700189status_t NuPlayer::RTSPSource::dequeueAccessUnit(
190 bool audio, sp<ABuffer> *accessUnit) {
Chong Zhang180d1b92014-12-02 18:35:35 -0800191 if (!stopBufferingIfNecessary()) {
192 return -EWOULDBLOCK;
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700193 }
194
Andreas Huber2bfdd422011-10-11 15:24:07 -0700195 sp<AnotherPacketSource> source = getSource(audio);
196
197 if (source == NULL) {
198 return -EWOULDBLOCK;
199 }
200
201 status_t finalResult;
202 if (!source->hasBufferAvailable(&finalResult)) {
Roger Jönssoncfc30832013-01-21 16:26:41 +0100203 if (finalResult == OK) {
Roger Jönssoncfc30832013-01-21 16:26:41 +0100204
Robert Shihae481672016-07-29 16:44:39 -0700205 // If other source already signaled EOS, this source should also return EOS
206 if (sourceReachedEOS(!audio)) {
Roger Jönssoncfc30832013-01-21 16:26:41 +0100207 return ERROR_END_OF_STREAM;
208 }
209
210 // If this source has detected near end, give it some time to retrieve more
Robert Shihae481672016-07-29 16:44:39 -0700211 // data before returning EOS
212 int64_t mediaDurationUs = 0;
213 getDuration(&mediaDurationUs);
Roger Jönssoncfc30832013-01-21 16:26:41 +0100214 if (source->isFinished(mediaDurationUs)) {
215 int64_t eosTimeout = audio ? mEOSTimeoutAudio : mEOSTimeoutVideo;
216 if (eosTimeout == 0) {
217 setEOSTimeout(audio, ALooper::GetNowUs());
218 } else if ((ALooper::GetNowUs() - eosTimeout) > kNearEOSTimeoutUs) {
219 setEOSTimeout(audio, 0);
Roger Jönssoncfc30832013-01-21 16:26:41 +0100220 return ERROR_END_OF_STREAM;
221 }
222 return -EWOULDBLOCK;
223 }
224
Robert Shihae481672016-07-29 16:44:39 -0700225 if (!sourceNearEOS(!audio)) {
Roger Jönssoncfc30832013-01-21 16:26:41 +0100226 // We should not enter buffering mode
227 // if any of the sources already have detected EOS.
Chong Zhang180d1b92014-12-02 18:35:35 -0800228 startBufferingIfNecessary();
Roger Jönssoncfc30832013-01-21 16:26:41 +0100229 }
230
231 return -EWOULDBLOCK;
232 }
233 return finalResult;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700234 }
235
Roger Jönssoncfc30832013-01-21 16:26:41 +0100236 setEOSTimeout(audio, 0);
237
Andreas Huber2bfdd422011-10-11 15:24:07 -0700238 return source->dequeueAccessUnit(accessUnit);
239}
240
241sp<AnotherPacketSource> NuPlayer::RTSPSource::getSource(bool audio) {
Andreas Huber49694682012-08-31 10:27:46 -0700242 if (mTSParser != NULL) {
243 sp<MediaSource> source = mTSParser->getSource(
244 audio ? ATSParser::AUDIO : ATSParser::VIDEO);
245
246 return static_cast<AnotherPacketSource *>(source.get());
247 }
248
Andreas Huber2bfdd422011-10-11 15:24:07 -0700249 return audio ? mAudioTrack : mVideoTrack;
250}
251
Roger Jönssoncfc30832013-01-21 16:26:41 +0100252void NuPlayer::RTSPSource::setEOSTimeout(bool audio, int64_t timeout) {
253 if (audio) {
254 mEOSTimeoutAudio = timeout;
255 } else {
256 mEOSTimeoutVideo = timeout;
257 }
258}
259
Andreas Huber2bfdd422011-10-11 15:24:07 -0700260status_t NuPlayer::RTSPSource::getDuration(int64_t *durationUs) {
261 *durationUs = 0ll;
262
263 int64_t audioDurationUs;
264 if (mAudioTrack != NULL
265 && mAudioTrack->getFormat()->findInt64(
266 kKeyDuration, &audioDurationUs)
267 && audioDurationUs > *durationUs) {
268 *durationUs = audioDurationUs;
269 }
270
271 int64_t videoDurationUs;
272 if (mVideoTrack != NULL
273 && mVideoTrack->getFormat()->findInt64(
274 kKeyDuration, &videoDurationUs)
275 && videoDurationUs > *durationUs) {
276 *durationUs = videoDurationUs;
277 }
278
279 return OK;
280}
281
282status_t NuPlayer::RTSPSource::seekTo(int64_t seekTimeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800283 sp<AMessage> msg = new AMessage(kWhatPerformSeek, this);
Andreas Huberee736e92011-12-08 13:04:50 -0800284 msg->setInt32("generation", ++mSeekGeneration);
285 msg->setInt64("timeUs", seekTimeUs);
Andreas Huberee736e92011-12-08 13:04:50 -0800286
Robert Shih8d237a52015-07-13 17:59:36 -0700287 sp<AMessage> response;
288 status_t err = msg->postAndAwaitResponse(&response);
289 if (err == OK && response != NULL) {
290 CHECK(response->findInt32("err", &err));
291 }
292
293 return err;
Andreas Huberee736e92011-12-08 13:04:50 -0800294}
295
296void NuPlayer::RTSPSource::performSeek(int64_t seekTimeUs) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700297 if (mState != CONNECTED) {
Robert Shih8d237a52015-07-13 17:59:36 -0700298 finishSeek(INVALID_OPERATION);
Andreas Huberee736e92011-12-08 13:04:50 -0800299 return;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700300 }
301
302 mState = SEEKING;
303 mHandler->seek(seekTimeUs);
Robert Shihae481672016-07-29 16:44:39 -0700304 mEOSPending = false;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700305}
306
Robert Shih641e0c72016-02-22 11:37:20 -0800307void NuPlayer::RTSPSource::schedulePollBuffering() {
308 sp<AMessage> msg = new AMessage(kWhatPollBuffering, this);
309 msg->post(1000000ll); // 1 second intervals
310}
311
312void NuPlayer::RTSPSource::checkBuffering(
Robert Shihae481672016-07-29 16:44:39 -0700313 bool *prepared, bool *underflow, bool *overflow, bool *startServer, bool *finished) {
Robert Shih641e0c72016-02-22 11:37:20 -0800314 size_t numTracks = mTracks.size();
Robert Shihae481672016-07-29 16:44:39 -0700315 size_t preparedCount, underflowCount, overflowCount, startCount, finishedCount;
316 preparedCount = underflowCount = overflowCount = startCount = finishedCount = 0;
Takahiro Aizawac622c582016-06-20 09:53:47 +0900317
318 size_t count = numTracks;
319 for (size_t i = 0; i < count; ++i) {
Robert Shih641e0c72016-02-22 11:37:20 -0800320 status_t finalResult;
321 TrackInfo *info = &mTracks.editItemAt(i);
322 sp<AnotherPacketSource> src = info->mSource;
Takahiro Aizawac622c582016-06-20 09:53:47 +0900323 if (src == NULL) {
324 --numTracks;
325 continue;
326 }
Robert Shih641e0c72016-02-22 11:37:20 -0800327 int64_t bufferedDurationUs = src->getBufferedDurationUs(&finalResult);
328
329 // isFinished when duration is 0 checks for EOS result only
330 if (bufferedDurationUs > kPrepareMarkUs || src->isFinished(/* duration */ 0)) {
331 ++preparedCount;
332 }
333
334 if (src->isFinished(/* duration */ 0)) {
335 ++overflowCount;
Robert Shihae481672016-07-29 16:44:39 -0700336 ++finishedCount;
Robert Shih641e0c72016-02-22 11:37:20 -0800337 } else {
338 if (bufferedDurationUs < kUnderflowMarkUs) {
339 ++underflowCount;
340 }
341 if (bufferedDurationUs > kOverflowMarkUs) {
342 ++overflowCount;
343 }
344 if (bufferedDurationUs < kStartServerMarkUs) {
345 ++startCount;
346 }
347 }
348 }
349
350 *prepared = (preparedCount == numTracks);
351 *underflow = (underflowCount > 0);
352 *overflow = (overflowCount == numTracks);
353 *startServer = (startCount > 0);
Robert Shihae481672016-07-29 16:44:39 -0700354 *finished = (finishedCount > 0);
Robert Shih641e0c72016-02-22 11:37:20 -0800355}
356
357void NuPlayer::RTSPSource::onPollBuffering() {
Robert Shihae481672016-07-29 16:44:39 -0700358 bool prepared, underflow, overflow, startServer, finished;
359 checkBuffering(&prepared, &underflow, &overflow, &startServer, &finished);
Robert Shih641e0c72016-02-22 11:37:20 -0800360
361 if (prepared && mInPreparationPhase) {
362 mInPreparationPhase = false;
363 notifyPrepared();
364 }
365
366 if (!mInPreparationPhase && underflow) {
367 startBufferingIfNecessary();
368 }
369
370 if (overflow && mHandler != NULL) {
371 stopBufferingIfNecessary();
372 mHandler->pause();
373 }
374
375 if (startServer && mHandler != NULL) {
376 mHandler->resume();
377 }
378
Robert Shihae481672016-07-29 16:44:39 -0700379 if (finished && mHandler != NULL) {
380 mHandler->cancelAccessUnitTimeoutCheck();
381 }
382
Robert Shih641e0c72016-02-22 11:37:20 -0800383 schedulePollBuffering();
384}
385
Robert Shihae481672016-07-29 16:44:39 -0700386void NuPlayer::RTSPSource::signalSourceEOS(status_t result) {
387 const bool audio = true;
388 const bool video = false;
389
390 sp<AnotherPacketSource> source = getSource(audio);
391 if (source != NULL) {
392 source->signalEOS(result);
393 }
394
395 source = getSource(video);
396 if (source != NULL) {
397 source->signalEOS(result);
398 }
399}
400
401bool NuPlayer::RTSPSource::sourceReachedEOS(bool audio) {
402 sp<AnotherPacketSource> source = getSource(audio);
403 status_t finalResult;
404 return (source != NULL &&
405 !source->hasBufferAvailable(&finalResult) &&
406 finalResult == ERROR_END_OF_STREAM);
407}
408
409bool NuPlayer::RTSPSource::sourceNearEOS(bool audio) {
410 sp<AnotherPacketSource> source = getSource(audio);
411 int64_t mediaDurationUs = 0;
412 getDuration(&mediaDurationUs);
413 return (source != NULL && source->isFinished(mediaDurationUs));
414}
415
416void NuPlayer::RTSPSource::onSignalEOS(const sp<AMessage> &msg) {
417 int32_t generation;
418 CHECK(msg->findInt32("generation", &generation));
419
420 if (generation != mSeekGeneration) {
421 return;
422 }
423
424 if (mEOSPending) {
425 signalSourceEOS(ERROR_END_OF_STREAM);
426 mEOSPending = false;
427 }
428}
429
430void NuPlayer::RTSPSource::postSourceEOSIfNecessary() {
431 const bool audio = true;
432 const bool video = false;
433 // If a source has detected near end, give it some time to retrieve more
434 // data before signaling EOS
435 if (sourceNearEOS(audio) || sourceNearEOS(video)) {
436 if (!mEOSPending) {
437 sp<AMessage> msg = new AMessage(kWhatSignalEOS, this);
438 msg->setInt32("generation", mSeekGeneration);
439 msg->post(kNearEOSTimeoutUs);
440 mEOSPending = true;
441 }
442 }
443}
444
Andreas Huber2bfdd422011-10-11 15:24:07 -0700445void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) {
446 if (msg->what() == kWhatDisconnect) {
Lajos Molnar3f274362015-03-05 14:35:41 -0800447 sp<AReplyToken> replyID;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700448 CHECK(msg->senderAwaitsResponse(&replyID));
449
450 mDisconnectReplyID = replyID;
451 finishDisconnectIfPossible();
452 return;
Andreas Huberee736e92011-12-08 13:04:50 -0800453 } else if (msg->what() == kWhatPerformSeek) {
454 int32_t generation;
455 CHECK(msg->findInt32("generation", &generation));
Robert Shih8d237a52015-07-13 17:59:36 -0700456 CHECK(msg->senderAwaitsResponse(&mSeekReplyID));
Andreas Huberee736e92011-12-08 13:04:50 -0800457
458 if (generation != mSeekGeneration) {
459 // obsolete.
Robert Shih8d237a52015-07-13 17:59:36 -0700460 finishSeek(OK);
Andreas Huberee736e92011-12-08 13:04:50 -0800461 return;
462 }
463
464 int64_t seekTimeUs;
465 CHECK(msg->findInt64("timeUs", &seekTimeUs));
466
467 performSeek(seekTimeUs);
468 return;
Robert Shih641e0c72016-02-22 11:37:20 -0800469 } else if (msg->what() == kWhatPollBuffering) {
470 onPollBuffering();
471 return;
Robert Shihae481672016-07-29 16:44:39 -0700472 } else if (msg->what() == kWhatSignalEOS) {
473 onSignalEOS(msg);
474 return;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700475 }
476
477 CHECK_EQ(msg->what(), (int)kWhatNotify);
478
479 int32_t what;
480 CHECK(msg->findInt32("what", &what));
481
482 switch (what) {
483 case MyHandler::kWhatConnected:
Andreas Huber7f475c32013-02-05 14:47:13 -0800484 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700485 onConnected();
Andreas Huber7f475c32013-02-05 14:47:13 -0800486
Chong Zhangced1c2f2014-08-08 15:22:35 -0700487 notifyVideoSizeChanged();
Andreas Huber7f475c32013-02-05 14:47:13 -0800488
489 uint32_t flags = 0;
490
491 if (mHandler->isSeekable()) {
Chong Zhang4b7069d2013-09-11 12:52:43 -0700492 flags = FLAG_CAN_PAUSE
493 | FLAG_CAN_SEEK
494 | FLAG_CAN_SEEK_BACKWARD
495 | FLAG_CAN_SEEK_FORWARD;
Andreas Huber7f475c32013-02-05 14:47:13 -0800496 }
497
498 notifyFlagsChanged(flags);
Robert Shih641e0c72016-02-22 11:37:20 -0800499 schedulePollBuffering();
Andreas Huber2bfdd422011-10-11 15:24:07 -0700500 break;
Andreas Huber7f475c32013-02-05 14:47:13 -0800501 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700502
503 case MyHandler::kWhatDisconnected:
Andreas Huber7f475c32013-02-05 14:47:13 -0800504 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700505 onDisconnected(msg);
506 break;
Andreas Huber7f475c32013-02-05 14:47:13 -0800507 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700508
509 case MyHandler::kWhatSeekDone:
510 {
511 mState = CONNECTED;
Wei Jia4ad74b22016-02-05 17:11:20 -0800512 // Unblock seekTo here in case we attempted to seek in a live stream
513 finishSeek(OK);
Robert Shih8d237a52015-07-13 17:59:36 -0700514 break;
515 }
516
517 case MyHandler::kWhatSeekPaused:
518 {
519 sp<AnotherPacketSource> source = getSource(true /* audio */);
520 if (source != NULL) {
521 source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
522 /* extra */ NULL,
523 /* discard */ true);
524 }
525 source = getSource(false /* video */);
526 if (source != NULL) {
527 source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
528 /* extra */ NULL,
529 /* discard */ true);
530 };
531
532 status_t err = OK;
533 msg->findInt32("err", &err);
Robert Shih8d237a52015-07-13 17:59:36 -0700534
535 if (err == OK) {
536 int64_t timeUs;
537 CHECK(msg->findInt64("time", &timeUs));
538 mHandler->continueSeekAfterPause(timeUs);
Wei Jia4ad74b22016-02-05 17:11:20 -0800539 } else {
540 finishSeek(err);
Robert Shih8d237a52015-07-13 17:59:36 -0700541 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700542 break;
543 }
544
545 case MyHandler::kWhatAccessUnit:
546 {
547 size_t trackIndex;
548 CHECK(msg->findSize("trackIndex", &trackIndex));
Andreas Huber49694682012-08-31 10:27:46 -0700549
550 if (mTSParser == NULL) {
551 CHECK_LT(trackIndex, mTracks.size());
552 } else {
553 CHECK_EQ(trackIndex, 0u);
554 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700555
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800556 sp<ABuffer> accessUnit;
557 CHECK(msg->findBuffer("accessUnit", &accessUnit));
Andreas Huber2bfdd422011-10-11 15:24:07 -0700558
559 int32_t damaged;
560 if (accessUnit->meta()->findInt32("damaged", &damaged)
561 && damaged) {
Steve Blockdf64d152012-01-04 20:05:49 +0000562 ALOGI("dropping damaged access unit.");
Andreas Huber2bfdd422011-10-11 15:24:07 -0700563 break;
564 }
565
Andreas Huber49694682012-08-31 10:27:46 -0700566 if (mTSParser != NULL) {
567 size_t offset = 0;
568 status_t err = OK;
569 while (offset + 188 <= accessUnit->size()) {
570 err = mTSParser->feedTSPacket(
571 accessUnit->data() + offset, 188);
572 if (err != OK) {
573 break;
574 }
575
576 offset += 188;
577 }
578
579 if (offset < accessUnit->size()) {
580 err = ERROR_MALFORMED;
581 }
582
583 if (err != OK) {
Robert Shihae481672016-07-29 16:44:39 -0700584 signalSourceEOS(err);
Andreas Huber49694682012-08-31 10:27:46 -0700585 }
Robert Shihae481672016-07-29 16:44:39 -0700586
587 postSourceEOSIfNecessary();
Andreas Huber49694682012-08-31 10:27:46 -0700588 break;
589 }
590
Andreas Huber1906e5c2011-12-08 12:27:47 -0800591 TrackInfo *info = &mTracks.editItemAt(trackIndex);
592
593 sp<AnotherPacketSource> source = info->mSource;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700594 if (source != NULL) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700595 uint32_t rtpTime;
596 CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
597
Andreas Huber1906e5c2011-12-08 12:27:47 -0800598 if (!info->mNPTMappingValid) {
599 // This is a live stream, we didn't receive any normal
Andreas Huberc9d16962012-05-21 11:12:40 -0700600 // playtime mapping. We won't map to npt time.
601 source->queueAccessUnit(accessUnit);
602 break;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800603 }
604
Andreas Huber2bfdd422011-10-11 15:24:07 -0700605 int64_t nptUs =
Andreas Huber1906e5c2011-12-08 12:27:47 -0800606 ((double)rtpTime - (double)info->mRTPTime)
607 / info->mTimeScale
Andreas Huber2bfdd422011-10-11 15:24:07 -0700608 * 1000000ll
Andreas Huber1906e5c2011-12-08 12:27:47 -0800609 + info->mNormalPlaytimeUs;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700610
611 accessUnit->meta()->setInt64("timeUs", nptUs);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700612
613 source->queueAccessUnit(accessUnit);
614 }
Robert Shihae481672016-07-29 16:44:39 -0700615 postSourceEOSIfNecessary();
Andreas Huber2bfdd422011-10-11 15:24:07 -0700616 break;
617 }
618
619 case MyHandler::kWhatEOS:
620 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700621 int32_t finalResult;
622 CHECK(msg->findInt32("finalResult", &finalResult));
623 CHECK_NE(finalResult, (status_t)OK);
624
Andreas Huber49694682012-08-31 10:27:46 -0700625 if (mTSParser != NULL) {
Robert Shihae481672016-07-29 16:44:39 -0700626 signalSourceEOS(finalResult);
Andreas Huber49694682012-08-31 10:27:46 -0700627 }
628
629 size_t trackIndex;
630 CHECK(msg->findSize("trackIndex", &trackIndex));
631 CHECK_LT(trackIndex, mTracks.size());
632
Andreas Huber2bfdd422011-10-11 15:24:07 -0700633 TrackInfo *info = &mTracks.editItemAt(trackIndex);
634 sp<AnotherPacketSource> source = info->mSource;
635 if (source != NULL) {
636 source->signalEOS(finalResult);
637 }
638
639 break;
640 }
641
642 case MyHandler::kWhatSeekDiscontinuity:
643 {
644 size_t trackIndex;
645 CHECK(msg->findSize("trackIndex", &trackIndex));
646 CHECK_LT(trackIndex, mTracks.size());
647
648 TrackInfo *info = &mTracks.editItemAt(trackIndex);
649 sp<AnotherPacketSource> source = info->mSource;
650 if (source != NULL) {
Chong Zhang632740c2014-06-26 13:03:47 -0700651 source->queueDiscontinuity(
Wei Jiafef808d2014-10-31 17:57:05 -0700652 ATSParser::DISCONTINUITY_TIME,
Chong Zhang632740c2014-06-26 13:03:47 -0700653 NULL,
654 true /* discard */);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700655 }
656
657 break;
658 }
659
660 case MyHandler::kWhatNormalPlayTimeMapping:
661 {
662 size_t trackIndex;
663 CHECK(msg->findSize("trackIndex", &trackIndex));
664 CHECK_LT(trackIndex, mTracks.size());
665
666 uint32_t rtpTime;
667 CHECK(msg->findInt32("rtpTime", (int32_t *)&rtpTime));
668
669 int64_t nptUs;
670 CHECK(msg->findInt64("nptUs", &nptUs));
671
672 TrackInfo *info = &mTracks.editItemAt(trackIndex);
673 info->mRTPTime = rtpTime;
674 info->mNormalPlaytimeUs = nptUs;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800675 info->mNPTMappingValid = true;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700676 break;
677 }
678
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100679 case SDPLoader::kWhatSDPLoaded:
680 {
681 onSDPLoaded(msg);
682 break;
683 }
684
Andreas Huber2bfdd422011-10-11 15:24:07 -0700685 default:
686 TRESPASS();
687 }
688}
689
690void NuPlayer::RTSPSource::onConnected() {
691 CHECK(mAudioTrack == NULL);
692 CHECK(mVideoTrack == NULL);
693
694 size_t numTracks = mHandler->countTracks();
695 for (size_t i = 0; i < numTracks; ++i) {
696 int32_t timeScale;
697 sp<MetaData> format = mHandler->getTrackFormat(i, &timeScale);
698
699 const char *mime;
700 CHECK(format->findCString(kKeyMIMEType, &mime));
701
Andreas Huber49694682012-08-31 10:27:46 -0700702 if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) {
703 // Very special case for MPEG2 Transport Streams.
704 CHECK_EQ(numTracks, 1u);
705
706 mTSParser = new ATSParser;
707 return;
708 }
709
Andreas Huber2bfdd422011-10-11 15:24:07 -0700710 bool isAudio = !strncasecmp(mime, "audio/", 6);
711 bool isVideo = !strncasecmp(mime, "video/", 6);
712
713 TrackInfo info;
714 info.mTimeScale = timeScale;
715 info.mRTPTime = 0;
716 info.mNormalPlaytimeUs = 0ll;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800717 info.mNPTMappingValid = false;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700718
719 if ((isAudio && mAudioTrack == NULL)
720 || (isVideo && mVideoTrack == NULL)) {
721 sp<AnotherPacketSource> source = new AnotherPacketSource(format);
722
723 if (isAudio) {
724 mAudioTrack = source;
725 } else {
726 mVideoTrack = source;
727 }
728
729 info.mSource = source;
730 }
731
732 mTracks.push(info);
733 }
734
735 mState = CONNECTED;
736}
737
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100738void NuPlayer::RTSPSource::onSDPLoaded(const sp<AMessage> &msg) {
739 status_t err;
740 CHECK(msg->findInt32("result", &err));
741
742 mSDPLoader.clear();
743
744 if (mDisconnectReplyID != 0) {
745 err = UNKNOWN_ERROR;
746 }
747
748 if (err == OK) {
749 sp<ASessionDescription> desc;
750 sp<RefBase> obj;
751 CHECK(msg->findObject("description", &obj));
752 desc = static_cast<ASessionDescription *>(obj.get());
753
754 AString rtspUri;
755 if (!desc->findAttribute(0, "a=control", &rtspUri)) {
756 ALOGE("Unable to find url in SDP");
757 err = UNKNOWN_ERROR;
758 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800759 sp<AMessage> notify = new AMessage(kWhatNotify, this);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100760
761 mHandler = new MyHandler(rtspUri.c_str(), notify, mUIDValid, mUID);
762 mLooper->registerHandler(mHandler);
763
764 mHandler->loadSDP(desc);
765 }
766 }
767
768 if (err != OK) {
Andreas Huber7f475c32013-02-05 14:47:13 -0800769 if (mState == CONNECTING) {
770 // We're still in the preparation phase, signal that it
771 // failed.
772 notifyPrepared(err);
773 }
774
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100775 mState = DISCONNECTED;
Chong Zhang180d1b92014-12-02 18:35:35 -0800776 setError(err);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100777
778 if (mDisconnectReplyID != 0) {
779 finishDisconnectIfPossible();
780 }
781 }
782}
783
Andreas Huber2bfdd422011-10-11 15:24:07 -0700784void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) {
Fredrik Rosin0ad03bc2013-03-06 13:42:53 +0100785 if (mState == DISCONNECTED) {
786 return;
787 }
788
Andreas Huber2bfdd422011-10-11 15:24:07 -0700789 status_t err;
790 CHECK(msg->findInt32("result", &err));
791 CHECK_NE(err, (status_t)OK);
792
793 mLooper->unregisterHandler(mHandler->id());
794 mHandler.clear();
795
Andreas Huber7f475c32013-02-05 14:47:13 -0800796 if (mState == CONNECTING) {
797 // We're still in the preparation phase, signal that it
798 // failed.
799 notifyPrepared(err);
800 }
801
Andreas Huber2bfdd422011-10-11 15:24:07 -0700802 mState = DISCONNECTED;
Chong Zhang180d1b92014-12-02 18:35:35 -0800803 setError(err);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700804
805 if (mDisconnectReplyID != 0) {
806 finishDisconnectIfPossible();
807 }
808}
809
810void NuPlayer::RTSPSource::finishDisconnectIfPossible() {
811 if (mState != DISCONNECTED) {
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100812 if (mHandler != NULL) {
813 mHandler->disconnect();
814 } else if (mSDPLoader != NULL) {
815 mSDPLoader->cancel();
816 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700817 return;
818 }
819
820 (new AMessage)->postReply(mDisconnectReplyID);
821 mDisconnectReplyID = 0;
822}
823
Chong Zhang180d1b92014-12-02 18:35:35 -0800824void NuPlayer::RTSPSource::setError(status_t err) {
825 Mutex::Autolock _l(mBufferingLock);
826 mFinalResult = err;
827}
828
829void NuPlayer::RTSPSource::startBufferingIfNecessary() {
830 Mutex::Autolock _l(mBufferingLock);
831
832 if (!mBuffering) {
833 mBuffering = true;
834
835 sp<AMessage> notify = dupNotify();
Wei Jiac9ff2002016-05-24 16:23:48 -0700836 notify->setInt32("what", kWhatPauseOnBufferingStart);
Chong Zhang180d1b92014-12-02 18:35:35 -0800837 notify->post();
838 }
839}
840
841bool NuPlayer::RTSPSource::stopBufferingIfNecessary() {
842 Mutex::Autolock _l(mBufferingLock);
843
844 if (mBuffering) {
845 if (!haveSufficientDataOnAllTracks()) {
846 return false;
847 }
848
849 mBuffering = false;
850
851 sp<AMessage> notify = dupNotify();
Wei Jiac9ff2002016-05-24 16:23:48 -0700852 notify->setInt32("what", kWhatResumeOnBufferingEnd);
Chong Zhang180d1b92014-12-02 18:35:35 -0800853 notify->post();
854 }
855
856 return true;
857}
858
Robert Shih8d237a52015-07-13 17:59:36 -0700859void NuPlayer::RTSPSource::finishSeek(status_t err) {
Wei Jia4ad74b22016-02-05 17:11:20 -0800860 if (mSeekReplyID == NULL) {
861 return;
862 }
Robert Shih8d237a52015-07-13 17:59:36 -0700863 sp<AMessage> seekReply = new AMessage;
864 seekReply->setInt32("err", err);
865 seekReply->postReply(mSeekReplyID);
866 mSeekReplyID = NULL;
867}
Chong Zhang180d1b92014-12-02 18:35:35 -0800868
Andreas Huber2bfdd422011-10-11 15:24:07 -0700869} // namespace android