1 /*
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 "StreamingSource"
19 #include <utils/Log.h>
20
21 #include "StreamingSource.h"
22
23 #include "ATSParser.h"
24 #include "AnotherPacketSource.h"
25 #include "NuPlayerStreamListener.h"
26
27 #include <media/MediaSource.h>
28 #include <media/stagefright/foundation/ABuffer.h>
29 #include <media/stagefright/foundation/ADebug.h>
30 #include <media/stagefright/foundation/AMessage.h>
31 #include <media/stagefright/foundation/MediaKeys.h>
32 #include <media/stagefright/MetaData.h>
33 #include <media/stagefright/Utils.h>
34
35 namespace android {
36
37 const int32_t kNumListenerQueuePackets = 80;
38
StreamingSource(const sp<AMessage> & notify,const sp<IStreamSource> & source)39 NuPlayer::StreamingSource::StreamingSource(
40 const sp<AMessage> ¬ify,
41 const sp<IStreamSource> &source)
42 : Source(notify),
43 mSource(source),
44 mFinalResult(OK),
45 mBuffering(false) {
46 }
47
~StreamingSource()48 NuPlayer::StreamingSource::~StreamingSource() {
49 if (mLooper != NULL) {
50 mLooper->unregisterHandler(id());
51 mLooper->stop();
52 }
53 }
54
getBufferingSettings(BufferingSettings * buffering)55 status_t NuPlayer::StreamingSource::getBufferingSettings(
56 BufferingSettings *buffering /* nonnull */) {
57 *buffering = BufferingSettings();
58 return OK;
59 }
60
setBufferingSettings(const BufferingSettings &)61 status_t NuPlayer::StreamingSource::setBufferingSettings(
62 const BufferingSettings & /* buffering */) {
63 return OK;
64 }
65
prepareAsync()66 void NuPlayer::StreamingSource::prepareAsync() {
67 if (mLooper == NULL) {
68 mLooper = new ALooper;
69 mLooper->setName("streaming");
70 mLooper->start();
71
72 mLooper->registerHandler(this);
73 }
74
75 notifyVideoSizeChanged();
76 notifyFlagsChanged(0);
77 notifyPrepared();
78 }
79
start()80 void NuPlayer::StreamingSource::start() {
81 mStreamListener = new NuPlayerStreamListener(mSource, NULL);
82
83 uint32_t sourceFlags = mSource->flags();
84
85 uint32_t parserFlags = ATSParser::TS_TIMESTAMPS_ARE_ABSOLUTE;
86 if (sourceFlags & IStreamSource::kFlagAlignedVideoData) {
87 parserFlags |= ATSParser::ALIGNED_VIDEO_DATA;
88 }
89
90 mTSParser = new ATSParser(parserFlags);
91
92 mStreamListener->start();
93
94 postReadBuffer();
95 }
96
feedMoreTSData()97 status_t NuPlayer::StreamingSource::feedMoreTSData() {
98 return postReadBuffer();
99 }
100
onReadBuffer()101 void NuPlayer::StreamingSource::onReadBuffer() {
102 for (int32_t i = 0; i < kNumListenerQueuePackets; ++i) {
103 char buffer[188];
104 sp<AMessage> extra;
105 ssize_t n = mStreamListener->read(buffer, sizeof(buffer), &extra);
106
107 if (n == 0) {
108 ALOGI("input data EOS reached.");
109 mTSParser->signalEOS(ERROR_END_OF_STREAM);
110 setError(ERROR_END_OF_STREAM);
111 break;
112 } else if (n == INFO_DISCONTINUITY) {
113 int32_t type = ATSParser::DISCONTINUITY_TIME;
114
115 int32_t mask;
116 if (extra != NULL
117 && extra->findInt32(
118 kIStreamListenerKeyDiscontinuityMask, &mask)) {
119 if (mask == 0) {
120 ALOGE("Client specified an illegal discontinuity type.");
121 setError(ERROR_UNSUPPORTED);
122 break;
123 }
124
125 type = mask;
126 }
127
128 mTSParser->signalDiscontinuity(
129 (ATSParser::DiscontinuityType)type, extra);
130 } else if (n < 0) {
131 break;
132 } else {
133 if (buffer[0] == 0x00) { // OK to access buffer[0] since n must be > 0 here
134 // XXX legacy
135
136 if (extra == NULL) {
137 extra = new AMessage;
138 }
139
140 uint8_t type = 0;
141 if (n > 1) {
142 type = buffer[1];
143
144 if ((type & 2) && (n >= 2 + sizeof(int64_t))) {
145 int64_t mediaTimeUs;
146 memcpy(&mediaTimeUs, &buffer[2], sizeof(mediaTimeUs));
147
148 extra->setInt64(kATSParserKeyMediaTimeUs, mediaTimeUs);
149 }
150 }
151
152 mTSParser->signalDiscontinuity(
153 ((type & 1) == 0)
154 ? ATSParser::DISCONTINUITY_TIME
155 : ATSParser::DISCONTINUITY_FORMATCHANGE,
156 extra);
157 } else {
158 status_t err = mTSParser->feedTSPacket(buffer, n);
159
160 if (err != OK) {
161 ALOGE("TS Parser returned error %d", err);
162
163 mTSParser->signalEOS(err);
164 setError(err);
165 break;
166 }
167 }
168 }
169 }
170 }
171
postReadBuffer()172 status_t NuPlayer::StreamingSource::postReadBuffer() {
173 {
174 Mutex::Autolock _l(mBufferingLock);
175 if (mFinalResult != OK) {
176 return mFinalResult;
177 }
178 if (mBuffering) {
179 return OK;
180 }
181 mBuffering = true;
182 }
183
184 (new AMessage(kWhatReadBuffer, this))->post();
185 return OK;
186 }
187
haveSufficientDataOnAllTracks()188 bool NuPlayer::StreamingSource::haveSufficientDataOnAllTracks() {
189 // We're going to buffer at least 2 secs worth data on all tracks before
190 // starting playback (both at startup and after a seek).
191
192 static const int64_t kMinDurationUs = 2000000LL;
193
194 sp<AnotherPacketSource> audioTrack = getSource(true /*audio*/);
195 sp<AnotherPacketSource> videoTrack = getSource(false /*audio*/);
196
197 status_t err;
198 int64_t durationUs;
199 if (audioTrack != NULL
200 && (durationUs = audioTrack->getBufferedDurationUs(&err))
201 < kMinDurationUs
202 && err == OK) {
203 ALOGV("audio track doesn't have enough data yet. (%.2f secs buffered)",
204 durationUs / 1E6);
205 return false;
206 }
207
208 if (videoTrack != NULL
209 && (durationUs = videoTrack->getBufferedDurationUs(&err))
210 < kMinDurationUs
211 && err == OK) {
212 ALOGV("video track doesn't have enough data yet. (%.2f secs buffered)",
213 durationUs / 1E6);
214 return false;
215 }
216
217 return true;
218 }
219
setError(status_t err)220 void NuPlayer::StreamingSource::setError(status_t err) {
221 Mutex::Autolock _l(mBufferingLock);
222 mFinalResult = err;
223 }
224
getSource(bool audio)225 sp<AnotherPacketSource> NuPlayer::StreamingSource::getSource(bool audio) {
226 if (mTSParser == NULL) {
227 return NULL;
228 }
229
230 sp<MediaSource> source = mTSParser->getSource(
231 audio ? ATSParser::AUDIO : ATSParser::VIDEO);
232
233 return static_cast<AnotherPacketSource *>(source.get());
234 }
235
getFormat(bool audio)236 sp<AMessage> NuPlayer::StreamingSource::getFormat(bool audio) {
237 sp<AnotherPacketSource> source = getSource(audio);
238
239 sp<AMessage> format = new AMessage;
240 if (source == NULL) {
241 format->setInt32("err", -EWOULDBLOCK);
242 return format;
243 }
244
245 sp<MetaData> meta = source->getFormat();
246 if (meta == NULL) {
247 format->setInt32("err", -EWOULDBLOCK);
248 return format;
249 }
250 status_t err = convertMetaDataToMessage(meta, &format);
251 if (err != OK) { // format may have been cleared on error
252 return NULL;
253 }
254 return format;
255 }
256
dequeueAccessUnit(bool audio,sp<ABuffer> * accessUnit)257 status_t NuPlayer::StreamingSource::dequeueAccessUnit(
258 bool audio, sp<ABuffer> *accessUnit) {
259 sp<AnotherPacketSource> source = getSource(audio);
260
261 if (source == NULL) {
262 return -EWOULDBLOCK;
263 }
264
265 if (!haveSufficientDataOnAllTracks()) {
266 postReadBuffer();
267 }
268
269 status_t finalResult;
270 if (!source->hasBufferAvailable(&finalResult)) {
271 return finalResult == OK ? -EWOULDBLOCK : finalResult;
272 }
273
274 status_t err = source->dequeueAccessUnit(accessUnit);
275
276 #if !defined(LOG_NDEBUG) || LOG_NDEBUG == 0
277 if (err == OK) {
278 int64_t timeUs;
279 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
280 ALOGV("dequeueAccessUnit timeUs=%lld us", timeUs);
281 }
282 #endif
283
284 return err;
285 }
286
isRealTime() const287 bool NuPlayer::StreamingSource::isRealTime() const {
288 return mSource->flags() & IStreamSource::kFlagIsRealTimeData;
289 }
290
onMessageReceived(const sp<AMessage> & msg)291 void NuPlayer::StreamingSource::onMessageReceived(
292 const sp<AMessage> &msg) {
293 switch (msg->what()) {
294 case kWhatReadBuffer:
295 {
296 onReadBuffer();
297
298 {
299 Mutex::Autolock _l(mBufferingLock);
300 mBuffering = false;
301 }
302 break;
303 }
304 default:
305 {
306 TRESPASS();
307 }
308 }
309 }
310
311
312 } // namespace android
313
314