blob: 7ce909d5b6370bd78b593afce66f66e0fb2db7e0 [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 Shihf1d261f2016-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 Shihf1d261f2016-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 Shihf1d261f2016-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 Shihf1d261f2016-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) {
Robert Shih7e074a82016-10-13 15:13:00 -0700261 *durationUs = -1ll;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700262
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 Shihf1d261f2016-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 Shihf1d261f2016-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 Shihf1d261f2016-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 Shihf1d261f2016-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 Shihf1d261f2016-07-29 16:44:39 -0700354 *finished = (finishedCount > 0);
Robert Shih641e0c72016-02-22 11:37:20 -0800355}
356
357void NuPlayer::RTSPSource::onPollBuffering() {
Robert Shihf1d261f2016-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
Robert Shih91ea5712016-08-01 16:26:58 -0700370 if (haveSufficientDataOnAllTracks()) {
Robert Shih641e0c72016-02-22 11:37:20 -0800371 stopBufferingIfNecessary();
Robert Shih91ea5712016-08-01 16:26:58 -0700372 }
373
374 if (overflow && mHandler != NULL) {
Robert Shih641e0c72016-02-22 11:37:20 -0800375 mHandler->pause();
376 }
377
378 if (startServer && mHandler != NULL) {
379 mHandler->resume();
380 }
381
Robert Shihf1d261f2016-07-29 16:44:39 -0700382 if (finished && mHandler != NULL) {
383 mHandler->cancelAccessUnitTimeoutCheck();
384 }
385
Robert Shih641e0c72016-02-22 11:37:20 -0800386 schedulePollBuffering();
387}
388
Robert Shihf1d261f2016-07-29 16:44:39 -0700389void NuPlayer::RTSPSource::signalSourceEOS(status_t result) {
390 const bool audio = true;
391 const bool video = false;
392
393 sp<AnotherPacketSource> source = getSource(audio);
394 if (source != NULL) {
395 source->signalEOS(result);
396 }
397
398 source = getSource(video);
399 if (source != NULL) {
400 source->signalEOS(result);
401 }
402}
403
404bool NuPlayer::RTSPSource::sourceReachedEOS(bool audio) {
405 sp<AnotherPacketSource> source = getSource(audio);
406 status_t finalResult;
407 return (source != NULL &&
408 !source->hasBufferAvailable(&finalResult) &&
409 finalResult == ERROR_END_OF_STREAM);
410}
411
412bool NuPlayer::RTSPSource::sourceNearEOS(bool audio) {
413 sp<AnotherPacketSource> source = getSource(audio);
414 int64_t mediaDurationUs = 0;
415 getDuration(&mediaDurationUs);
416 return (source != NULL && source->isFinished(mediaDurationUs));
417}
418
419void NuPlayer::RTSPSource::onSignalEOS(const sp<AMessage> &msg) {
420 int32_t generation;
421 CHECK(msg->findInt32("generation", &generation));
422
423 if (generation != mSeekGeneration) {
424 return;
425 }
426
427 if (mEOSPending) {
428 signalSourceEOS(ERROR_END_OF_STREAM);
429 mEOSPending = false;
430 }
431}
432
433void NuPlayer::RTSPSource::postSourceEOSIfNecessary() {
434 const bool audio = true;
435 const bool video = false;
436 // If a source has detected near end, give it some time to retrieve more
437 // data before signaling EOS
438 if (sourceNearEOS(audio) || sourceNearEOS(video)) {
439 if (!mEOSPending) {
440 sp<AMessage> msg = new AMessage(kWhatSignalEOS, this);
441 msg->setInt32("generation", mSeekGeneration);
442 msg->post(kNearEOSTimeoutUs);
443 mEOSPending = true;
444 }
445 }
446}
447
Andreas Huber2bfdd422011-10-11 15:24:07 -0700448void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) {
449 if (msg->what() == kWhatDisconnect) {
Lajos Molnar3f274362015-03-05 14:35:41 -0800450 sp<AReplyToken> replyID;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700451 CHECK(msg->senderAwaitsResponse(&replyID));
452
453 mDisconnectReplyID = replyID;
454 finishDisconnectIfPossible();
455 return;
Andreas Huberee736e92011-12-08 13:04:50 -0800456 } else if (msg->what() == kWhatPerformSeek) {
457 int32_t generation;
458 CHECK(msg->findInt32("generation", &generation));
Robert Shih8d237a52015-07-13 17:59:36 -0700459 CHECK(msg->senderAwaitsResponse(&mSeekReplyID));
Andreas Huberee736e92011-12-08 13:04:50 -0800460
461 if (generation != mSeekGeneration) {
462 // obsolete.
Robert Shih8d237a52015-07-13 17:59:36 -0700463 finishSeek(OK);
Andreas Huberee736e92011-12-08 13:04:50 -0800464 return;
465 }
466
467 int64_t seekTimeUs;
468 CHECK(msg->findInt64("timeUs", &seekTimeUs));
469
470 performSeek(seekTimeUs);
471 return;
Robert Shih641e0c72016-02-22 11:37:20 -0800472 } else if (msg->what() == kWhatPollBuffering) {
473 onPollBuffering();
474 return;
Robert Shihf1d261f2016-07-29 16:44:39 -0700475 } else if (msg->what() == kWhatSignalEOS) {
476 onSignalEOS(msg);
477 return;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700478 }
479
480 CHECK_EQ(msg->what(), (int)kWhatNotify);
481
482 int32_t what;
483 CHECK(msg->findInt32("what", &what));
484
485 switch (what) {
486 case MyHandler::kWhatConnected:
Andreas Huber7f475c32013-02-05 14:47:13 -0800487 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700488 onConnected();
Andreas Huber7f475c32013-02-05 14:47:13 -0800489
Chong Zhangced1c2f2014-08-08 15:22:35 -0700490 notifyVideoSizeChanged();
Andreas Huber7f475c32013-02-05 14:47:13 -0800491
492 uint32_t flags = 0;
493
494 if (mHandler->isSeekable()) {
Chong Zhang4b7069d2013-09-11 12:52:43 -0700495 flags = FLAG_CAN_PAUSE
496 | FLAG_CAN_SEEK
497 | FLAG_CAN_SEEK_BACKWARD
498 | FLAG_CAN_SEEK_FORWARD;
Andreas Huber7f475c32013-02-05 14:47:13 -0800499 }
500
501 notifyFlagsChanged(flags);
Robert Shih641e0c72016-02-22 11:37:20 -0800502 schedulePollBuffering();
Andreas Huber2bfdd422011-10-11 15:24:07 -0700503 break;
Andreas Huber7f475c32013-02-05 14:47:13 -0800504 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700505
506 case MyHandler::kWhatDisconnected:
Andreas Huber7f475c32013-02-05 14:47:13 -0800507 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700508 onDisconnected(msg);
509 break;
Andreas Huber7f475c32013-02-05 14:47:13 -0800510 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700511
512 case MyHandler::kWhatSeekDone:
513 {
514 mState = CONNECTED;
Wei Jia4ad74b22016-02-05 17:11:20 -0800515 // Unblock seekTo here in case we attempted to seek in a live stream
516 finishSeek(OK);
Robert Shih8d237a52015-07-13 17:59:36 -0700517 break;
518 }
519
520 case MyHandler::kWhatSeekPaused:
521 {
522 sp<AnotherPacketSource> source = getSource(true /* audio */);
523 if (source != NULL) {
524 source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
525 /* extra */ NULL,
526 /* discard */ true);
527 }
528 source = getSource(false /* video */);
529 if (source != NULL) {
530 source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
531 /* extra */ NULL,
532 /* discard */ true);
533 };
534
535 status_t err = OK;
536 msg->findInt32("err", &err);
Robert Shih8d237a52015-07-13 17:59:36 -0700537
538 if (err == OK) {
539 int64_t timeUs;
540 CHECK(msg->findInt64("time", &timeUs));
541 mHandler->continueSeekAfterPause(timeUs);
Wei Jia4ad74b22016-02-05 17:11:20 -0800542 } else {
543 finishSeek(err);
Robert Shih8d237a52015-07-13 17:59:36 -0700544 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700545 break;
546 }
547
548 case MyHandler::kWhatAccessUnit:
549 {
550 size_t trackIndex;
551 CHECK(msg->findSize("trackIndex", &trackIndex));
Andreas Huber49694682012-08-31 10:27:46 -0700552
553 if (mTSParser == NULL) {
554 CHECK_LT(trackIndex, mTracks.size());
555 } else {
556 CHECK_EQ(trackIndex, 0u);
557 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700558
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800559 sp<ABuffer> accessUnit;
560 CHECK(msg->findBuffer("accessUnit", &accessUnit));
Andreas Huber2bfdd422011-10-11 15:24:07 -0700561
562 int32_t damaged;
563 if (accessUnit->meta()->findInt32("damaged", &damaged)
564 && damaged) {
Steve Blockdf64d152012-01-04 20:05:49 +0000565 ALOGI("dropping damaged access unit.");
Andreas Huber2bfdd422011-10-11 15:24:07 -0700566 break;
567 }
568
Andreas Huber49694682012-08-31 10:27:46 -0700569 if (mTSParser != NULL) {
570 size_t offset = 0;
571 status_t err = OK;
572 while (offset + 188 <= accessUnit->size()) {
573 err = mTSParser->feedTSPacket(
574 accessUnit->data() + offset, 188);
575 if (err != OK) {
576 break;
577 }
578
579 offset += 188;
580 }
581
582 if (offset < accessUnit->size()) {
583 err = ERROR_MALFORMED;
584 }
585
586 if (err != OK) {
Robert Shihf1d261f2016-07-29 16:44:39 -0700587 signalSourceEOS(err);
Andreas Huber49694682012-08-31 10:27:46 -0700588 }
Robert Shihf1d261f2016-07-29 16:44:39 -0700589
590 postSourceEOSIfNecessary();
Andreas Huber49694682012-08-31 10:27:46 -0700591 break;
592 }
593
Andreas Huber1906e5c2011-12-08 12:27:47 -0800594 TrackInfo *info = &mTracks.editItemAt(trackIndex);
595
596 sp<AnotherPacketSource> source = info->mSource;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700597 if (source != NULL) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700598 uint32_t rtpTime;
599 CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
600
Andreas Huber1906e5c2011-12-08 12:27:47 -0800601 if (!info->mNPTMappingValid) {
602 // This is a live stream, we didn't receive any normal
Andreas Huberc9d16962012-05-21 11:12:40 -0700603 // playtime mapping. We won't map to npt time.
604 source->queueAccessUnit(accessUnit);
605 break;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800606 }
607
Andreas Huber2bfdd422011-10-11 15:24:07 -0700608 int64_t nptUs =
Andreas Huber1906e5c2011-12-08 12:27:47 -0800609 ((double)rtpTime - (double)info->mRTPTime)
610 / info->mTimeScale
Andreas Huber2bfdd422011-10-11 15:24:07 -0700611 * 1000000ll
Andreas Huber1906e5c2011-12-08 12:27:47 -0800612 + info->mNormalPlaytimeUs;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700613
614 accessUnit->meta()->setInt64("timeUs", nptUs);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700615
616 source->queueAccessUnit(accessUnit);
617 }
Robert Shihf1d261f2016-07-29 16:44:39 -0700618 postSourceEOSIfNecessary();
Andreas Huber2bfdd422011-10-11 15:24:07 -0700619 break;
620 }
621
622 case MyHandler::kWhatEOS:
623 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700624 int32_t finalResult;
625 CHECK(msg->findInt32("finalResult", &finalResult));
626 CHECK_NE(finalResult, (status_t)OK);
627
Andreas Huber49694682012-08-31 10:27:46 -0700628 if (mTSParser != NULL) {
Robert Shihf1d261f2016-07-29 16:44:39 -0700629 signalSourceEOS(finalResult);
Andreas Huber49694682012-08-31 10:27:46 -0700630 }
631
632 size_t trackIndex;
633 CHECK(msg->findSize("trackIndex", &trackIndex));
634 CHECK_LT(trackIndex, mTracks.size());
635
Andreas Huber2bfdd422011-10-11 15:24:07 -0700636 TrackInfo *info = &mTracks.editItemAt(trackIndex);
637 sp<AnotherPacketSource> source = info->mSource;
638 if (source != NULL) {
639 source->signalEOS(finalResult);
640 }
641
642 break;
643 }
644
645 case MyHandler::kWhatSeekDiscontinuity:
646 {
647 size_t trackIndex;
648 CHECK(msg->findSize("trackIndex", &trackIndex));
649 CHECK_LT(trackIndex, mTracks.size());
650
651 TrackInfo *info = &mTracks.editItemAt(trackIndex);
652 sp<AnotherPacketSource> source = info->mSource;
653 if (source != NULL) {
Chong Zhang632740c2014-06-26 13:03:47 -0700654 source->queueDiscontinuity(
Wei Jiafef808d2014-10-31 17:57:05 -0700655 ATSParser::DISCONTINUITY_TIME,
Chong Zhang632740c2014-06-26 13:03:47 -0700656 NULL,
657 true /* discard */);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700658 }
659
660 break;
661 }
662
663 case MyHandler::kWhatNormalPlayTimeMapping:
664 {
665 size_t trackIndex;
666 CHECK(msg->findSize("trackIndex", &trackIndex));
667 CHECK_LT(trackIndex, mTracks.size());
668
669 uint32_t rtpTime;
670 CHECK(msg->findInt32("rtpTime", (int32_t *)&rtpTime));
671
672 int64_t nptUs;
673 CHECK(msg->findInt64("nptUs", &nptUs));
674
675 TrackInfo *info = &mTracks.editItemAt(trackIndex);
676 info->mRTPTime = rtpTime;
677 info->mNormalPlaytimeUs = nptUs;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800678 info->mNPTMappingValid = true;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700679 break;
680 }
681
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100682 case SDPLoader::kWhatSDPLoaded:
683 {
684 onSDPLoaded(msg);
685 break;
686 }
687
Andreas Huber2bfdd422011-10-11 15:24:07 -0700688 default:
689 TRESPASS();
690 }
691}
692
693void NuPlayer::RTSPSource::onConnected() {
694 CHECK(mAudioTrack == NULL);
695 CHECK(mVideoTrack == NULL);
696
697 size_t numTracks = mHandler->countTracks();
698 for (size_t i = 0; i < numTracks; ++i) {
699 int32_t timeScale;
700 sp<MetaData> format = mHandler->getTrackFormat(i, &timeScale);
701
702 const char *mime;
703 CHECK(format->findCString(kKeyMIMEType, &mime));
704
Andreas Huber49694682012-08-31 10:27:46 -0700705 if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) {
706 // Very special case for MPEG2 Transport Streams.
707 CHECK_EQ(numTracks, 1u);
708
709 mTSParser = new ATSParser;
710 return;
711 }
712
Andreas Huber2bfdd422011-10-11 15:24:07 -0700713 bool isAudio = !strncasecmp(mime, "audio/", 6);
714 bool isVideo = !strncasecmp(mime, "video/", 6);
715
716 TrackInfo info;
717 info.mTimeScale = timeScale;
718 info.mRTPTime = 0;
719 info.mNormalPlaytimeUs = 0ll;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800720 info.mNPTMappingValid = false;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700721
722 if ((isAudio && mAudioTrack == NULL)
723 || (isVideo && mVideoTrack == NULL)) {
724 sp<AnotherPacketSource> source = new AnotherPacketSource(format);
725
726 if (isAudio) {
727 mAudioTrack = source;
728 } else {
729 mVideoTrack = source;
730 }
731
732 info.mSource = source;
733 }
734
735 mTracks.push(info);
736 }
737
738 mState = CONNECTED;
739}
740
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100741void NuPlayer::RTSPSource::onSDPLoaded(const sp<AMessage> &msg) {
742 status_t err;
743 CHECK(msg->findInt32("result", &err));
744
745 mSDPLoader.clear();
746
747 if (mDisconnectReplyID != 0) {
748 err = UNKNOWN_ERROR;
749 }
750
751 if (err == OK) {
752 sp<ASessionDescription> desc;
753 sp<RefBase> obj;
754 CHECK(msg->findObject("description", &obj));
755 desc = static_cast<ASessionDescription *>(obj.get());
756
757 AString rtspUri;
758 if (!desc->findAttribute(0, "a=control", &rtspUri)) {
759 ALOGE("Unable to find url in SDP");
760 err = UNKNOWN_ERROR;
761 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800762 sp<AMessage> notify = new AMessage(kWhatNotify, this);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100763
764 mHandler = new MyHandler(rtspUri.c_str(), notify, mUIDValid, mUID);
765 mLooper->registerHandler(mHandler);
766
767 mHandler->loadSDP(desc);
768 }
769 }
770
771 if (err != OK) {
Andreas Huber7f475c32013-02-05 14:47:13 -0800772 if (mState == CONNECTING) {
773 // We're still in the preparation phase, signal that it
774 // failed.
775 notifyPrepared(err);
776 }
777
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100778 mState = DISCONNECTED;
Chong Zhang180d1b92014-12-02 18:35:35 -0800779 setError(err);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100780
781 if (mDisconnectReplyID != 0) {
782 finishDisconnectIfPossible();
783 }
784 }
785}
786
Andreas Huber2bfdd422011-10-11 15:24:07 -0700787void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) {
Fredrik Rosin0ad03bc2013-03-06 13:42:53 +0100788 if (mState == DISCONNECTED) {
789 return;
790 }
791
Andreas Huber2bfdd422011-10-11 15:24:07 -0700792 status_t err;
793 CHECK(msg->findInt32("result", &err));
794 CHECK_NE(err, (status_t)OK);
795
796 mLooper->unregisterHandler(mHandler->id());
797 mHandler.clear();
798
Andreas Huber7f475c32013-02-05 14:47:13 -0800799 if (mState == CONNECTING) {
800 // We're still in the preparation phase, signal that it
801 // failed.
802 notifyPrepared(err);
803 }
804
Andreas Huber2bfdd422011-10-11 15:24:07 -0700805 mState = DISCONNECTED;
Chong Zhang180d1b92014-12-02 18:35:35 -0800806 setError(err);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700807
808 if (mDisconnectReplyID != 0) {
809 finishDisconnectIfPossible();
810 }
811}
812
813void NuPlayer::RTSPSource::finishDisconnectIfPossible() {
814 if (mState != DISCONNECTED) {
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100815 if (mHandler != NULL) {
816 mHandler->disconnect();
817 } else if (mSDPLoader != NULL) {
818 mSDPLoader->cancel();
819 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700820 return;
821 }
822
823 (new AMessage)->postReply(mDisconnectReplyID);
824 mDisconnectReplyID = 0;
825}
826
Chong Zhang180d1b92014-12-02 18:35:35 -0800827void NuPlayer::RTSPSource::setError(status_t err) {
828 Mutex::Autolock _l(mBufferingLock);
829 mFinalResult = err;
830}
831
832void NuPlayer::RTSPSource::startBufferingIfNecessary() {
833 Mutex::Autolock _l(mBufferingLock);
834
835 if (!mBuffering) {
836 mBuffering = true;
837
838 sp<AMessage> notify = dupNotify();
Wei Jiac9ff2002016-05-24 16:23:48 -0700839 notify->setInt32("what", kWhatPauseOnBufferingStart);
Chong Zhang180d1b92014-12-02 18:35:35 -0800840 notify->post();
841 }
842}
843
844bool NuPlayer::RTSPSource::stopBufferingIfNecessary() {
845 Mutex::Autolock _l(mBufferingLock);
846
847 if (mBuffering) {
848 if (!haveSufficientDataOnAllTracks()) {
849 return false;
850 }
851
852 mBuffering = false;
853
854 sp<AMessage> notify = dupNotify();
Wei Jiac9ff2002016-05-24 16:23:48 -0700855 notify->setInt32("what", kWhatResumeOnBufferingEnd);
Chong Zhang180d1b92014-12-02 18:35:35 -0800856 notify->post();
857 }
858
859 return true;
860}
861
Robert Shih8d237a52015-07-13 17:59:36 -0700862void NuPlayer::RTSPSource::finishSeek(status_t err) {
Wei Jia4ad74b22016-02-05 17:11:20 -0800863 if (mSeekReplyID == NULL) {
864 return;
865 }
Robert Shih8d237a52015-07-13 17:59:36 -0700866 sp<AMessage> seekReply = new AMessage;
867 seekReply->setInt32("err", err);
868 seekReply->postReply(mSeekReplyID);
869 mSeekReplyID = NULL;
870}
Chong Zhang180d1b92014-12-02 18:35:35 -0800871
Andreas Huber2bfdd422011-10-11 15:24:07 -0700872} // namespace android