blob: 4c65b6564df514865f45e8777c0bc1ec47969381 [file] [log] [blame]
Andreas Hubera6be6dc2011-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"
25
26#include <media/stagefright/MetaData.h>
27
28namespace android {
29
30NuPlayer::RTSPSource::RTSPSource(
31 const char *url,
32 const KeyedVector<String8, String8> *headers,
33 bool uidValid,
34 uid_t uid)
35 : mURL(url),
36 mUIDValid(uidValid),
37 mUID(uid),
38 mFlags(0),
39 mState(DISCONNECTED),
40 mFinalResult(OK),
Andreas Huber21902a82011-12-08 13:04:50 -080041 mDisconnectReplyID(0),
42 mSeekGeneration(0) {
Andreas Hubera6be6dc2011-10-11 15:24:07 -070043 if (headers) {
44 mExtraHeaders = *headers;
45
46 ssize_t index =
47 mExtraHeaders.indexOfKey(String8("x-hide-urls-from-log"));
48
49 if (index >= 0) {
50 mFlags |= kFlagIncognito;
51
52 mExtraHeaders.removeItemsAt(index);
53 }
54 }
55}
56
57NuPlayer::RTSPSource::~RTSPSource() {
58 if (mLooper != NULL) {
59 mLooper->stop();
60 }
61}
62
63void NuPlayer::RTSPSource::start() {
64 if (mLooper == NULL) {
65 mLooper = new ALooper;
66 mLooper->setName("rtsp");
67 mLooper->start();
68
69 mReflector = new AHandlerReflector<RTSPSource>(this);
70 mLooper->registerHandler(mReflector);
71 }
72
73 CHECK(mHandler == NULL);
74
75 sp<AMessage> notify = new AMessage(kWhatNotify, mReflector->id());
76
77 mHandler = new MyHandler(mURL.c_str(), notify, mUIDValid, mUID);
78 mLooper->registerHandler(mHandler);
79
80 CHECK_EQ(mState, (int)DISCONNECTED);
81 mState = CONNECTING;
82
83 mHandler->connect();
84}
85
86void NuPlayer::RTSPSource::stop() {
87 sp<AMessage> msg = new AMessage(kWhatDisconnect, mReflector->id());
88
89 sp<AMessage> dummy;
90 msg->postAndAwaitResponse(&dummy);
91}
92
93status_t NuPlayer::RTSPSource::feedMoreTSData() {
94 return mFinalResult;
95}
96
97sp<MetaData> NuPlayer::RTSPSource::getFormat(bool audio) {
98 sp<AnotherPacketSource> source = getSource(audio);
99
100 if (source == NULL) {
101 return NULL;
102 }
103
104 return source->getFormat();
105}
106
107status_t NuPlayer::RTSPSource::dequeueAccessUnit(
108 bool audio, sp<ABuffer> *accessUnit) {
109 sp<AnotherPacketSource> source = getSource(audio);
110
111 if (source == NULL) {
112 return -EWOULDBLOCK;
113 }
114
115 status_t finalResult;
116 if (!source->hasBufferAvailable(&finalResult)) {
117 return finalResult == OK ? -EWOULDBLOCK : finalResult;
118 }
119
120 return source->dequeueAccessUnit(accessUnit);
121}
122
123sp<AnotherPacketSource> NuPlayer::RTSPSource::getSource(bool audio) {
124 return audio ? mAudioTrack : mVideoTrack;
125}
126
127status_t NuPlayer::RTSPSource::getDuration(int64_t *durationUs) {
128 *durationUs = 0ll;
129
130 int64_t audioDurationUs;
131 if (mAudioTrack != NULL
132 && mAudioTrack->getFormat()->findInt64(
133 kKeyDuration, &audioDurationUs)
134 && audioDurationUs > *durationUs) {
135 *durationUs = audioDurationUs;
136 }
137
138 int64_t videoDurationUs;
139 if (mVideoTrack != NULL
140 && mVideoTrack->getFormat()->findInt64(
141 kKeyDuration, &videoDurationUs)
142 && videoDurationUs > *durationUs) {
143 *durationUs = videoDurationUs;
144 }
145
146 return OK;
147}
148
149status_t NuPlayer::RTSPSource::seekTo(int64_t seekTimeUs) {
Andreas Huber21902a82011-12-08 13:04:50 -0800150 sp<AMessage> msg = new AMessage(kWhatPerformSeek, mReflector->id());
151 msg->setInt32("generation", ++mSeekGeneration);
152 msg->setInt64("timeUs", seekTimeUs);
153 msg->post(200000ll);
154
155 return OK;
156}
157
158void NuPlayer::RTSPSource::performSeek(int64_t seekTimeUs) {
Andreas Hubera6be6dc2011-10-11 15:24:07 -0700159 if (mState != CONNECTED) {
Andreas Huber21902a82011-12-08 13:04:50 -0800160 return;
Andreas Hubera6be6dc2011-10-11 15:24:07 -0700161 }
162
163 mState = SEEKING;
164 mHandler->seek(seekTimeUs);
Andreas Hubera6be6dc2011-10-11 15:24:07 -0700165}
166
167bool NuPlayer::RTSPSource::isSeekable() {
168 return true;
169}
170
171void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) {
172 if (msg->what() == kWhatDisconnect) {
173 uint32_t replyID;
174 CHECK(msg->senderAwaitsResponse(&replyID));
175
176 mDisconnectReplyID = replyID;
177 finishDisconnectIfPossible();
178 return;
Andreas Huber21902a82011-12-08 13:04:50 -0800179 } else if (msg->what() == kWhatPerformSeek) {
180 int32_t generation;
181 CHECK(msg->findInt32("generation", &generation));
182
183 if (generation != mSeekGeneration) {
184 // obsolete.
185 return;
186 }
187
188 int64_t seekTimeUs;
189 CHECK(msg->findInt64("timeUs", &seekTimeUs));
190
191 performSeek(seekTimeUs);
192 return;
Andreas Hubera6be6dc2011-10-11 15:24:07 -0700193 }
194
195 CHECK_EQ(msg->what(), (int)kWhatNotify);
196
197 int32_t what;
198 CHECK(msg->findInt32("what", &what));
199
200 switch (what) {
201 case MyHandler::kWhatConnected:
202 onConnected();
203 break;
204
205 case MyHandler::kWhatDisconnected:
206 onDisconnected(msg);
207 break;
208
209 case MyHandler::kWhatSeekDone:
210 {
211 mState = CONNECTED;
212 break;
213 }
214
215 case MyHandler::kWhatAccessUnit:
216 {
217 size_t trackIndex;
218 CHECK(msg->findSize("trackIndex", &trackIndex));
219 CHECK_LT(trackIndex, mTracks.size());
220
Andreas Huber5c850392012-02-21 14:38:23 -0800221 sp<ABuffer> accessUnit;
222 CHECK(msg->findBuffer("accessUnit", &accessUnit));
Andreas Hubera6be6dc2011-10-11 15:24:07 -0700223
224 int32_t damaged;
225 if (accessUnit->meta()->findInt32("damaged", &damaged)
226 && damaged) {
Steve Block6215d3f2012-01-04 20:05:49 +0000227 ALOGI("dropping damaged access unit.");
Andreas Hubera6be6dc2011-10-11 15:24:07 -0700228 break;
229 }
230
Andreas Huberf6ae7112011-12-08 12:27:47 -0800231 TrackInfo *info = &mTracks.editItemAt(trackIndex);
232
233 sp<AnotherPacketSource> source = info->mSource;
Andreas Hubera6be6dc2011-10-11 15:24:07 -0700234 if (source != NULL) {
Andreas Hubera6be6dc2011-10-11 15:24:07 -0700235 uint32_t rtpTime;
236 CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
237
Andreas Huberf6ae7112011-12-08 12:27:47 -0800238 if (!info->mNPTMappingValid) {
239 // This is a live stream, we didn't receive any normal
240 // playtime mapping. Assume the first packets correspond
241 // to time 0.
242
Owen Lin130f62a2011-12-12 16:17:34 +0800243 ALOGV("This is a live stream, assuming time = 0");
Andreas Huberf6ae7112011-12-08 12:27:47 -0800244
245 info->mRTPTime = rtpTime;
246 info->mNormalPlaytimeUs = 0ll;
247 info->mNPTMappingValid = true;
248 }
249
Andreas Hubera6be6dc2011-10-11 15:24:07 -0700250 int64_t nptUs =
Andreas Huberf6ae7112011-12-08 12:27:47 -0800251 ((double)rtpTime - (double)info->mRTPTime)
252 / info->mTimeScale
Andreas Hubera6be6dc2011-10-11 15:24:07 -0700253 * 1000000ll
Andreas Huberf6ae7112011-12-08 12:27:47 -0800254 + info->mNormalPlaytimeUs;
Andreas Hubera6be6dc2011-10-11 15:24:07 -0700255
256 accessUnit->meta()->setInt64("timeUs", nptUs);
Andreas Hubera6be6dc2011-10-11 15:24:07 -0700257
258 source->queueAccessUnit(accessUnit);
259 }
260 break;
261 }
262
263 case MyHandler::kWhatEOS:
264 {
265 size_t trackIndex;
266 CHECK(msg->findSize("trackIndex", &trackIndex));
267 CHECK_LT(trackIndex, mTracks.size());
268
269 int32_t finalResult;
270 CHECK(msg->findInt32("finalResult", &finalResult));
271 CHECK_NE(finalResult, (status_t)OK);
272
273 TrackInfo *info = &mTracks.editItemAt(trackIndex);
274 sp<AnotherPacketSource> source = info->mSource;
275 if (source != NULL) {
276 source->signalEOS(finalResult);
277 }
278
279 break;
280 }
281
282 case MyHandler::kWhatSeekDiscontinuity:
283 {
284 size_t trackIndex;
285 CHECK(msg->findSize("trackIndex", &trackIndex));
286 CHECK_LT(trackIndex, mTracks.size());
287
288 TrackInfo *info = &mTracks.editItemAt(trackIndex);
289 sp<AnotherPacketSource> source = info->mSource;
290 if (source != NULL) {
291 source->queueDiscontinuity(ATSParser::DISCONTINUITY_SEEK, NULL);
292 }
293
294 break;
295 }
296
297 case MyHandler::kWhatNormalPlayTimeMapping:
298 {
299 size_t trackIndex;
300 CHECK(msg->findSize("trackIndex", &trackIndex));
301 CHECK_LT(trackIndex, mTracks.size());
302
303 uint32_t rtpTime;
304 CHECK(msg->findInt32("rtpTime", (int32_t *)&rtpTime));
305
306 int64_t nptUs;
307 CHECK(msg->findInt64("nptUs", &nptUs));
308
309 TrackInfo *info = &mTracks.editItemAt(trackIndex);
310 info->mRTPTime = rtpTime;
311 info->mNormalPlaytimeUs = nptUs;
Andreas Huberf6ae7112011-12-08 12:27:47 -0800312 info->mNPTMappingValid = true;
Andreas Hubera6be6dc2011-10-11 15:24:07 -0700313 break;
314 }
315
316 default:
317 TRESPASS();
318 }
319}
320
321void NuPlayer::RTSPSource::onConnected() {
322 CHECK(mAudioTrack == NULL);
323 CHECK(mVideoTrack == NULL);
324
325 size_t numTracks = mHandler->countTracks();
326 for (size_t i = 0; i < numTracks; ++i) {
327 int32_t timeScale;
328 sp<MetaData> format = mHandler->getTrackFormat(i, &timeScale);
329
330 const char *mime;
331 CHECK(format->findCString(kKeyMIMEType, &mime));
332
333 bool isAudio = !strncasecmp(mime, "audio/", 6);
334 bool isVideo = !strncasecmp(mime, "video/", 6);
335
336 TrackInfo info;
337 info.mTimeScale = timeScale;
338 info.mRTPTime = 0;
339 info.mNormalPlaytimeUs = 0ll;
Andreas Huberf6ae7112011-12-08 12:27:47 -0800340 info.mNPTMappingValid = false;
Andreas Hubera6be6dc2011-10-11 15:24:07 -0700341
342 if ((isAudio && mAudioTrack == NULL)
343 || (isVideo && mVideoTrack == NULL)) {
344 sp<AnotherPacketSource> source = new AnotherPacketSource(format);
345
346 if (isAudio) {
347 mAudioTrack = source;
348 } else {
349 mVideoTrack = source;
350 }
351
352 info.mSource = source;
353 }
354
355 mTracks.push(info);
356 }
357
358 mState = CONNECTED;
359}
360
361void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) {
362 status_t err;
363 CHECK(msg->findInt32("result", &err));
364 CHECK_NE(err, (status_t)OK);
365
366 mLooper->unregisterHandler(mHandler->id());
367 mHandler.clear();
368
369 mState = DISCONNECTED;
370 mFinalResult = err;
371
372 if (mDisconnectReplyID != 0) {
373 finishDisconnectIfPossible();
374 }
375}
376
377void NuPlayer::RTSPSource::finishDisconnectIfPossible() {
378 if (mState != DISCONNECTED) {
379 mHandler->disconnect();
380 return;
381 }
382
383 (new AMessage)->postReply(mDisconnectReplyID);
384 mDisconnectReplyID = 0;
385}
386
387} // namespace android