blob: fb1f31a3d47fbe2b27d489793eaeaf80e9b70032 [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
Wei Jiac5de0912016-11-18 10:22:14 -0800282status_t NuPlayer::RTSPSource::seekTo(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
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);
Wei Jiac5de0912016-11-18 10:22:14 -0800286 msg->setInt32("mode", mode);
Andreas Huberee736e92011-12-08 13:04:50 -0800287
Robert Shih8d237a52015-07-13 17:59:36 -0700288 sp<AMessage> response;
289 status_t err = msg->postAndAwaitResponse(&response);
290 if (err == OK && response != NULL) {
291 CHECK(response->findInt32("err", &err));
292 }
293
294 return err;
Andreas Huberee736e92011-12-08 13:04:50 -0800295}
296
297void NuPlayer::RTSPSource::performSeek(int64_t seekTimeUs) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700298 if (mState != CONNECTED) {
Robert Shih8d237a52015-07-13 17:59:36 -0700299 finishSeek(INVALID_OPERATION);
Andreas Huberee736e92011-12-08 13:04:50 -0800300 return;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700301 }
302
303 mState = SEEKING;
304 mHandler->seek(seekTimeUs);
Robert Shihf1d261f2016-07-29 16:44:39 -0700305 mEOSPending = false;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700306}
307
Robert Shih641e0c72016-02-22 11:37:20 -0800308void NuPlayer::RTSPSource::schedulePollBuffering() {
309 sp<AMessage> msg = new AMessage(kWhatPollBuffering, this);
310 msg->post(1000000ll); // 1 second intervals
311}
312
313void NuPlayer::RTSPSource::checkBuffering(
Robert Shihf1d261f2016-07-29 16:44:39 -0700314 bool *prepared, bool *underflow, bool *overflow, bool *startServer, bool *finished) {
Robert Shih641e0c72016-02-22 11:37:20 -0800315 size_t numTracks = mTracks.size();
Robert Shihf1d261f2016-07-29 16:44:39 -0700316 size_t preparedCount, underflowCount, overflowCount, startCount, finishedCount;
317 preparedCount = underflowCount = overflowCount = startCount = finishedCount = 0;
Takahiro Aizawac622c582016-06-20 09:53:47 +0900318
319 size_t count = numTracks;
320 for (size_t i = 0; i < count; ++i) {
Robert Shih641e0c72016-02-22 11:37:20 -0800321 status_t finalResult;
322 TrackInfo *info = &mTracks.editItemAt(i);
323 sp<AnotherPacketSource> src = info->mSource;
Takahiro Aizawac622c582016-06-20 09:53:47 +0900324 if (src == NULL) {
325 --numTracks;
326 continue;
327 }
Robert Shih641e0c72016-02-22 11:37:20 -0800328 int64_t bufferedDurationUs = src->getBufferedDurationUs(&finalResult);
329
330 // isFinished when duration is 0 checks for EOS result only
331 if (bufferedDurationUs > kPrepareMarkUs || src->isFinished(/* duration */ 0)) {
332 ++preparedCount;
333 }
334
335 if (src->isFinished(/* duration */ 0)) {
336 ++overflowCount;
Robert Shihf1d261f2016-07-29 16:44:39 -0700337 ++finishedCount;
Robert Shih641e0c72016-02-22 11:37:20 -0800338 } else {
339 if (bufferedDurationUs < kUnderflowMarkUs) {
340 ++underflowCount;
341 }
342 if (bufferedDurationUs > kOverflowMarkUs) {
343 ++overflowCount;
344 }
345 if (bufferedDurationUs < kStartServerMarkUs) {
346 ++startCount;
347 }
348 }
349 }
350
351 *prepared = (preparedCount == numTracks);
352 *underflow = (underflowCount > 0);
353 *overflow = (overflowCount == numTracks);
354 *startServer = (startCount > 0);
Robert Shihf1d261f2016-07-29 16:44:39 -0700355 *finished = (finishedCount > 0);
Robert Shih641e0c72016-02-22 11:37:20 -0800356}
357
358void NuPlayer::RTSPSource::onPollBuffering() {
Robert Shihf1d261f2016-07-29 16:44:39 -0700359 bool prepared, underflow, overflow, startServer, finished;
360 checkBuffering(&prepared, &underflow, &overflow, &startServer, &finished);
Robert Shih641e0c72016-02-22 11:37:20 -0800361
362 if (prepared && mInPreparationPhase) {
363 mInPreparationPhase = false;
364 notifyPrepared();
365 }
366
367 if (!mInPreparationPhase && underflow) {
368 startBufferingIfNecessary();
369 }
370
Robert Shih91ea5712016-08-01 16:26:58 -0700371 if (haveSufficientDataOnAllTracks()) {
Robert Shih641e0c72016-02-22 11:37:20 -0800372 stopBufferingIfNecessary();
Robert Shih91ea5712016-08-01 16:26:58 -0700373 }
374
375 if (overflow && mHandler != NULL) {
Robert Shih641e0c72016-02-22 11:37:20 -0800376 mHandler->pause();
377 }
378
379 if (startServer && mHandler != NULL) {
380 mHandler->resume();
381 }
382
Robert Shihf1d261f2016-07-29 16:44:39 -0700383 if (finished && mHandler != NULL) {
384 mHandler->cancelAccessUnitTimeoutCheck();
385 }
386
Robert Shih641e0c72016-02-22 11:37:20 -0800387 schedulePollBuffering();
388}
389
Robert Shihf1d261f2016-07-29 16:44:39 -0700390void NuPlayer::RTSPSource::signalSourceEOS(status_t result) {
391 const bool audio = true;
392 const bool video = false;
393
394 sp<AnotherPacketSource> source = getSource(audio);
395 if (source != NULL) {
396 source->signalEOS(result);
397 }
398
399 source = getSource(video);
400 if (source != NULL) {
401 source->signalEOS(result);
402 }
403}
404
405bool NuPlayer::RTSPSource::sourceReachedEOS(bool audio) {
406 sp<AnotherPacketSource> source = getSource(audio);
407 status_t finalResult;
408 return (source != NULL &&
409 !source->hasBufferAvailable(&finalResult) &&
410 finalResult == ERROR_END_OF_STREAM);
411}
412
413bool NuPlayer::RTSPSource::sourceNearEOS(bool audio) {
414 sp<AnotherPacketSource> source = getSource(audio);
415 int64_t mediaDurationUs = 0;
416 getDuration(&mediaDurationUs);
417 return (source != NULL && source->isFinished(mediaDurationUs));
418}
419
420void NuPlayer::RTSPSource::onSignalEOS(const sp<AMessage> &msg) {
421 int32_t generation;
422 CHECK(msg->findInt32("generation", &generation));
423
424 if (generation != mSeekGeneration) {
425 return;
426 }
427
428 if (mEOSPending) {
429 signalSourceEOS(ERROR_END_OF_STREAM);
430 mEOSPending = false;
431 }
432}
433
434void NuPlayer::RTSPSource::postSourceEOSIfNecessary() {
435 const bool audio = true;
436 const bool video = false;
437 // If a source has detected near end, give it some time to retrieve more
438 // data before signaling EOS
439 if (sourceNearEOS(audio) || sourceNearEOS(video)) {
440 if (!mEOSPending) {
441 sp<AMessage> msg = new AMessage(kWhatSignalEOS, this);
442 msg->setInt32("generation", mSeekGeneration);
443 msg->post(kNearEOSTimeoutUs);
444 mEOSPending = true;
445 }
446 }
447}
448
Andreas Huber2bfdd422011-10-11 15:24:07 -0700449void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) {
450 if (msg->what() == kWhatDisconnect) {
Lajos Molnar3f274362015-03-05 14:35:41 -0800451 sp<AReplyToken> replyID;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700452 CHECK(msg->senderAwaitsResponse(&replyID));
453
454 mDisconnectReplyID = replyID;
455 finishDisconnectIfPossible();
456 return;
Andreas Huberee736e92011-12-08 13:04:50 -0800457 } else if (msg->what() == kWhatPerformSeek) {
458 int32_t generation;
459 CHECK(msg->findInt32("generation", &generation));
Robert Shih8d237a52015-07-13 17:59:36 -0700460 CHECK(msg->senderAwaitsResponse(&mSeekReplyID));
Andreas Huberee736e92011-12-08 13:04:50 -0800461
462 if (generation != mSeekGeneration) {
463 // obsolete.
Robert Shih8d237a52015-07-13 17:59:36 -0700464 finishSeek(OK);
Andreas Huberee736e92011-12-08 13:04:50 -0800465 return;
466 }
467
468 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -0800469 int32_t mode;
Andreas Huberee736e92011-12-08 13:04:50 -0800470 CHECK(msg->findInt64("timeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -0800471 CHECK(msg->findInt32("mode", &mode));
Andreas Huberee736e92011-12-08 13:04:50 -0800472
Wei Jiac5de0912016-11-18 10:22:14 -0800473 // TODO: add "mode" to performSeek.
474 performSeek(seekTimeUs/*, (MediaPlayerSeekMode)mode */);
Andreas Huberee736e92011-12-08 13:04:50 -0800475 return;
Robert Shih641e0c72016-02-22 11:37:20 -0800476 } else if (msg->what() == kWhatPollBuffering) {
477 onPollBuffering();
478 return;
Robert Shihf1d261f2016-07-29 16:44:39 -0700479 } else if (msg->what() == kWhatSignalEOS) {
480 onSignalEOS(msg);
481 return;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700482 }
483
484 CHECK_EQ(msg->what(), (int)kWhatNotify);
485
486 int32_t what;
487 CHECK(msg->findInt32("what", &what));
488
489 switch (what) {
490 case MyHandler::kWhatConnected:
Andreas Huber7f475c32013-02-05 14:47:13 -0800491 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700492 onConnected();
Andreas Huber7f475c32013-02-05 14:47:13 -0800493
Chong Zhangced1c2f2014-08-08 15:22:35 -0700494 notifyVideoSizeChanged();
Andreas Huber7f475c32013-02-05 14:47:13 -0800495
496 uint32_t flags = 0;
497
498 if (mHandler->isSeekable()) {
Chong Zhang4b7069d2013-09-11 12:52:43 -0700499 flags = FLAG_CAN_PAUSE
500 | FLAG_CAN_SEEK
501 | FLAG_CAN_SEEK_BACKWARD
502 | FLAG_CAN_SEEK_FORWARD;
Andreas Huber7f475c32013-02-05 14:47:13 -0800503 }
504
505 notifyFlagsChanged(flags);
Robert Shih641e0c72016-02-22 11:37:20 -0800506 schedulePollBuffering();
Andreas Huber2bfdd422011-10-11 15:24:07 -0700507 break;
Andreas Huber7f475c32013-02-05 14:47:13 -0800508 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700509
510 case MyHandler::kWhatDisconnected:
Andreas Huber7f475c32013-02-05 14:47:13 -0800511 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700512 onDisconnected(msg);
513 break;
Andreas Huber7f475c32013-02-05 14:47:13 -0800514 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700515
516 case MyHandler::kWhatSeekDone:
517 {
518 mState = CONNECTED;
Wei Jia4ad74b22016-02-05 17:11:20 -0800519 // Unblock seekTo here in case we attempted to seek in a live stream
520 finishSeek(OK);
Robert Shih8d237a52015-07-13 17:59:36 -0700521 break;
522 }
523
524 case MyHandler::kWhatSeekPaused:
525 {
526 sp<AnotherPacketSource> source = getSource(true /* audio */);
527 if (source != NULL) {
528 source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
529 /* extra */ NULL,
530 /* discard */ true);
531 }
532 source = getSource(false /* video */);
533 if (source != NULL) {
534 source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
535 /* extra */ NULL,
536 /* discard */ true);
537 };
538
539 status_t err = OK;
540 msg->findInt32("err", &err);
Robert Shih8d237a52015-07-13 17:59:36 -0700541
542 if (err == OK) {
543 int64_t timeUs;
544 CHECK(msg->findInt64("time", &timeUs));
545 mHandler->continueSeekAfterPause(timeUs);
Wei Jia4ad74b22016-02-05 17:11:20 -0800546 } else {
547 finishSeek(err);
Robert Shih8d237a52015-07-13 17:59:36 -0700548 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700549 break;
550 }
551
552 case MyHandler::kWhatAccessUnit:
553 {
554 size_t trackIndex;
555 CHECK(msg->findSize("trackIndex", &trackIndex));
Andreas Huber49694682012-08-31 10:27:46 -0700556
557 if (mTSParser == NULL) {
558 CHECK_LT(trackIndex, mTracks.size());
559 } else {
560 CHECK_EQ(trackIndex, 0u);
561 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700562
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800563 sp<ABuffer> accessUnit;
564 CHECK(msg->findBuffer("accessUnit", &accessUnit));
Andreas Huber2bfdd422011-10-11 15:24:07 -0700565
566 int32_t damaged;
567 if (accessUnit->meta()->findInt32("damaged", &damaged)
568 && damaged) {
Steve Blockdf64d152012-01-04 20:05:49 +0000569 ALOGI("dropping damaged access unit.");
Andreas Huber2bfdd422011-10-11 15:24:07 -0700570 break;
571 }
572
Andreas Huber49694682012-08-31 10:27:46 -0700573 if (mTSParser != NULL) {
574 size_t offset = 0;
575 status_t err = OK;
576 while (offset + 188 <= accessUnit->size()) {
577 err = mTSParser->feedTSPacket(
578 accessUnit->data() + offset, 188);
579 if (err != OK) {
580 break;
581 }
582
583 offset += 188;
584 }
585
586 if (offset < accessUnit->size()) {
587 err = ERROR_MALFORMED;
588 }
589
590 if (err != OK) {
Robert Shihf1d261f2016-07-29 16:44:39 -0700591 signalSourceEOS(err);
Andreas Huber49694682012-08-31 10:27:46 -0700592 }
Robert Shihf1d261f2016-07-29 16:44:39 -0700593
594 postSourceEOSIfNecessary();
Andreas Huber49694682012-08-31 10:27:46 -0700595 break;
596 }
597
Andreas Huber1906e5c2011-12-08 12:27:47 -0800598 TrackInfo *info = &mTracks.editItemAt(trackIndex);
599
600 sp<AnotherPacketSource> source = info->mSource;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700601 if (source != NULL) {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700602 uint32_t rtpTime;
603 CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
604
Andreas Huber1906e5c2011-12-08 12:27:47 -0800605 if (!info->mNPTMappingValid) {
606 // This is a live stream, we didn't receive any normal
Andreas Huberc9d16962012-05-21 11:12:40 -0700607 // playtime mapping. We won't map to npt time.
608 source->queueAccessUnit(accessUnit);
609 break;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800610 }
611
Andreas Huber2bfdd422011-10-11 15:24:07 -0700612 int64_t nptUs =
Andreas Huber1906e5c2011-12-08 12:27:47 -0800613 ((double)rtpTime - (double)info->mRTPTime)
614 / info->mTimeScale
Andreas Huber2bfdd422011-10-11 15:24:07 -0700615 * 1000000ll
Andreas Huber1906e5c2011-12-08 12:27:47 -0800616 + info->mNormalPlaytimeUs;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700617
618 accessUnit->meta()->setInt64("timeUs", nptUs);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700619
620 source->queueAccessUnit(accessUnit);
621 }
Robert Shihf1d261f2016-07-29 16:44:39 -0700622 postSourceEOSIfNecessary();
Andreas Huber2bfdd422011-10-11 15:24:07 -0700623 break;
624 }
625
626 case MyHandler::kWhatEOS:
627 {
Andreas Huber2bfdd422011-10-11 15:24:07 -0700628 int32_t finalResult;
629 CHECK(msg->findInt32("finalResult", &finalResult));
630 CHECK_NE(finalResult, (status_t)OK);
631
Andreas Huber49694682012-08-31 10:27:46 -0700632 if (mTSParser != NULL) {
Robert Shihf1d261f2016-07-29 16:44:39 -0700633 signalSourceEOS(finalResult);
Andreas Huber49694682012-08-31 10:27:46 -0700634 }
635
636 size_t trackIndex;
637 CHECK(msg->findSize("trackIndex", &trackIndex));
638 CHECK_LT(trackIndex, mTracks.size());
639
Andreas Huber2bfdd422011-10-11 15:24:07 -0700640 TrackInfo *info = &mTracks.editItemAt(trackIndex);
641 sp<AnotherPacketSource> source = info->mSource;
642 if (source != NULL) {
643 source->signalEOS(finalResult);
644 }
645
646 break;
647 }
648
649 case MyHandler::kWhatSeekDiscontinuity:
650 {
651 size_t trackIndex;
652 CHECK(msg->findSize("trackIndex", &trackIndex));
653 CHECK_LT(trackIndex, mTracks.size());
654
655 TrackInfo *info = &mTracks.editItemAt(trackIndex);
656 sp<AnotherPacketSource> source = info->mSource;
657 if (source != NULL) {
Chong Zhang632740c2014-06-26 13:03:47 -0700658 source->queueDiscontinuity(
Wei Jiafef808d2014-10-31 17:57:05 -0700659 ATSParser::DISCONTINUITY_TIME,
Chong Zhang632740c2014-06-26 13:03:47 -0700660 NULL,
661 true /* discard */);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700662 }
663
664 break;
665 }
666
667 case MyHandler::kWhatNormalPlayTimeMapping:
668 {
669 size_t trackIndex;
670 CHECK(msg->findSize("trackIndex", &trackIndex));
671 CHECK_LT(trackIndex, mTracks.size());
672
673 uint32_t rtpTime;
674 CHECK(msg->findInt32("rtpTime", (int32_t *)&rtpTime));
675
676 int64_t nptUs;
677 CHECK(msg->findInt64("nptUs", &nptUs));
678
679 TrackInfo *info = &mTracks.editItemAt(trackIndex);
680 info->mRTPTime = rtpTime;
681 info->mNormalPlaytimeUs = nptUs;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800682 info->mNPTMappingValid = true;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700683 break;
684 }
685
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100686 case SDPLoader::kWhatSDPLoaded:
687 {
688 onSDPLoaded(msg);
689 break;
690 }
691
Andreas Huber2bfdd422011-10-11 15:24:07 -0700692 default:
693 TRESPASS();
694 }
695}
696
697void NuPlayer::RTSPSource::onConnected() {
698 CHECK(mAudioTrack == NULL);
699 CHECK(mVideoTrack == NULL);
700
701 size_t numTracks = mHandler->countTracks();
702 for (size_t i = 0; i < numTracks; ++i) {
703 int32_t timeScale;
704 sp<MetaData> format = mHandler->getTrackFormat(i, &timeScale);
705
706 const char *mime;
707 CHECK(format->findCString(kKeyMIMEType, &mime));
708
Andreas Huber49694682012-08-31 10:27:46 -0700709 if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) {
710 // Very special case for MPEG2 Transport Streams.
711 CHECK_EQ(numTracks, 1u);
712
713 mTSParser = new ATSParser;
714 return;
715 }
716
Andreas Huber2bfdd422011-10-11 15:24:07 -0700717 bool isAudio = !strncasecmp(mime, "audio/", 6);
718 bool isVideo = !strncasecmp(mime, "video/", 6);
719
720 TrackInfo info;
721 info.mTimeScale = timeScale;
722 info.mRTPTime = 0;
723 info.mNormalPlaytimeUs = 0ll;
Andreas Huber1906e5c2011-12-08 12:27:47 -0800724 info.mNPTMappingValid = false;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700725
726 if ((isAudio && mAudioTrack == NULL)
727 || (isVideo && mVideoTrack == NULL)) {
728 sp<AnotherPacketSource> source = new AnotherPacketSource(format);
729
730 if (isAudio) {
731 mAudioTrack = source;
732 } else {
733 mVideoTrack = source;
734 }
735
736 info.mSource = source;
737 }
738
739 mTracks.push(info);
740 }
741
742 mState = CONNECTED;
743}
744
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100745void NuPlayer::RTSPSource::onSDPLoaded(const sp<AMessage> &msg) {
746 status_t err;
747 CHECK(msg->findInt32("result", &err));
748
749 mSDPLoader.clear();
750
751 if (mDisconnectReplyID != 0) {
752 err = UNKNOWN_ERROR;
753 }
754
755 if (err == OK) {
756 sp<ASessionDescription> desc;
757 sp<RefBase> obj;
758 CHECK(msg->findObject("description", &obj));
759 desc = static_cast<ASessionDescription *>(obj.get());
760
761 AString rtspUri;
762 if (!desc->findAttribute(0, "a=control", &rtspUri)) {
763 ALOGE("Unable to find url in SDP");
764 err = UNKNOWN_ERROR;
765 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800766 sp<AMessage> notify = new AMessage(kWhatNotify, this);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100767
768 mHandler = new MyHandler(rtspUri.c_str(), notify, mUIDValid, mUID);
769 mLooper->registerHandler(mHandler);
770
771 mHandler->loadSDP(desc);
772 }
773 }
774
775 if (err != OK) {
Andreas Huber7f475c32013-02-05 14:47:13 -0800776 if (mState == CONNECTING) {
777 // We're still in the preparation phase, signal that it
778 // failed.
779 notifyPrepared(err);
780 }
781
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100782 mState = DISCONNECTED;
Chong Zhang180d1b92014-12-02 18:35:35 -0800783 setError(err);
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100784
785 if (mDisconnectReplyID != 0) {
786 finishDisconnectIfPossible();
787 }
788 }
789}
790
Andreas Huber2bfdd422011-10-11 15:24:07 -0700791void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) {
Fredrik Rosin0ad03bc2013-03-06 13:42:53 +0100792 if (mState == DISCONNECTED) {
793 return;
794 }
795
Andreas Huber2bfdd422011-10-11 15:24:07 -0700796 status_t err;
797 CHECK(msg->findInt32("result", &err));
798 CHECK_NE(err, (status_t)OK);
799
800 mLooper->unregisterHandler(mHandler->id());
801 mHandler.clear();
802
Andreas Huber7f475c32013-02-05 14:47:13 -0800803 if (mState == CONNECTING) {
804 // We're still in the preparation phase, signal that it
805 // failed.
806 notifyPrepared(err);
807 }
808
Andreas Huber2bfdd422011-10-11 15:24:07 -0700809 mState = DISCONNECTED;
Chong Zhang180d1b92014-12-02 18:35:35 -0800810 setError(err);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700811
812 if (mDisconnectReplyID != 0) {
813 finishDisconnectIfPossible();
814 }
815}
816
817void NuPlayer::RTSPSource::finishDisconnectIfPossible() {
818 if (mState != DISCONNECTED) {
Oscar Rydhé81dd60e2012-02-20 10:15:48 +0100819 if (mHandler != NULL) {
820 mHandler->disconnect();
821 } else if (mSDPLoader != NULL) {
822 mSDPLoader->cancel();
823 }
Andreas Huber2bfdd422011-10-11 15:24:07 -0700824 return;
825 }
826
827 (new AMessage)->postReply(mDisconnectReplyID);
828 mDisconnectReplyID = 0;
829}
830
Chong Zhang180d1b92014-12-02 18:35:35 -0800831void NuPlayer::RTSPSource::setError(status_t err) {
832 Mutex::Autolock _l(mBufferingLock);
833 mFinalResult = err;
834}
835
836void NuPlayer::RTSPSource::startBufferingIfNecessary() {
837 Mutex::Autolock _l(mBufferingLock);
838
839 if (!mBuffering) {
840 mBuffering = true;
841
842 sp<AMessage> notify = dupNotify();
Wei Jiac9ff2002016-05-24 16:23:48 -0700843 notify->setInt32("what", kWhatPauseOnBufferingStart);
Chong Zhang180d1b92014-12-02 18:35:35 -0800844 notify->post();
845 }
846}
847
848bool NuPlayer::RTSPSource::stopBufferingIfNecessary() {
849 Mutex::Autolock _l(mBufferingLock);
850
851 if (mBuffering) {
852 if (!haveSufficientDataOnAllTracks()) {
853 return false;
854 }
855
856 mBuffering = false;
857
858 sp<AMessage> notify = dupNotify();
Wei Jiac9ff2002016-05-24 16:23:48 -0700859 notify->setInt32("what", kWhatResumeOnBufferingEnd);
Chong Zhang180d1b92014-12-02 18:35:35 -0800860 notify->post();
861 }
862
863 return true;
864}
865
Robert Shih8d237a52015-07-13 17:59:36 -0700866void NuPlayer::RTSPSource::finishSeek(status_t err) {
Wei Jia4ad74b22016-02-05 17:11:20 -0800867 if (mSeekReplyID == NULL) {
868 return;
869 }
Robert Shih8d237a52015-07-13 17:59:36 -0700870 sp<AMessage> seekReply = new AMessage;
871 seekReply->setInt32("err", err);
872 seekReply->postReply(mSeekReplyID);
873 mSeekReplyID = NULL;
874}
Chong Zhang180d1b92014-12-02 18:35:35 -0800875
Andreas Huber2bfdd422011-10-11 15:24:07 -0700876} // namespace android