blob: 28f0d50f05d9f485f782f5ce4ccdc24b518ce8f4 [file] [log] [blame]
Andreas Huber5bc087c2010-12-23 10:27:40 -08001/*
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/stagefright/foundation/ABuffer.h>
28#include <media/stagefright/foundation/ADebug.h>
29#include <media/stagefright/foundation/AMessage.h>
30#include <media/stagefright/MediaSource.h>
31#include <media/stagefright/MetaData.h>
32
33namespace android {
34
Andreas Huberb5f25f02013-02-05 10:14:26 -080035NuPlayer::StreamingSource::StreamingSource(
36 const sp<AMessage> &notify,
37 const sp<IStreamSource> &source)
38 : Source(notify),
39 mSource(source),
Andreas Hubereac68ba2011-09-27 12:12:25 -070040 mFinalResult(OK) {
Andreas Huber5bc087c2010-12-23 10:27:40 -080041}
42
43NuPlayer::StreamingSource::~StreamingSource() {
44}
45
Andreas Huber9575c962013-02-05 13:59:56 -080046void NuPlayer::StreamingSource::prepareAsync() {
47 notifyVideoSizeChanged(0, 0);
48 notifyFlagsChanged(0);
49 notifyPrepared();
50}
51
Andreas Huber5bc087c2010-12-23 10:27:40 -080052void NuPlayer::StreamingSource::start() {
53 mStreamListener = new NuPlayerStreamListener(mSource, 0);
Andreas Huber87f2a552012-08-31 13:55:24 -070054
55 uint32_t sourceFlags = mSource->flags();
56
57 uint32_t parserFlags = ATSParser::TS_TIMESTAMPS_ARE_ABSOLUTE;
58 if (sourceFlags & IStreamSource::kFlagAlignedVideoData) {
59 parserFlags |= ATSParser::ALIGNED_VIDEO_DATA;
60 }
61
62 mTSParser = new ATSParser(parserFlags);
Andreas Huber5bc087c2010-12-23 10:27:40 -080063
64 mStreamListener->start();
65}
66
Andreas Hubereac68ba2011-09-27 12:12:25 -070067status_t NuPlayer::StreamingSource::feedMoreTSData() {
68 if (mFinalResult != OK) {
69 return mFinalResult;
Andreas Huber5bc087c2010-12-23 10:27:40 -080070 }
71
Andreas Huber078cfcf2011-09-15 12:25:04 -070072 for (int32_t i = 0; i < 50; ++i) {
Andreas Huber5bc087c2010-12-23 10:27:40 -080073 char buffer[188];
Andreas Huber32f3cef2011-03-02 15:34:46 -080074 sp<AMessage> extra;
75 ssize_t n = mStreamListener->read(buffer, sizeof(buffer), &extra);
Andreas Huber5bc087c2010-12-23 10:27:40 -080076
77 if (n == 0) {
Steve Blockdf64d152012-01-04 20:05:49 +000078 ALOGI("input data EOS reached.");
Andreas Huber5bc087c2010-12-23 10:27:40 -080079 mTSParser->signalEOS(ERROR_END_OF_STREAM);
Andreas Hubereac68ba2011-09-27 12:12:25 -070080 mFinalResult = ERROR_END_OF_STREAM;
Andreas Huber5bc087c2010-12-23 10:27:40 -080081 break;
82 } else if (n == INFO_DISCONTINUITY) {
Andreas Huberbfcc8d82011-11-29 11:57:35 -080083 int32_t type = ATSParser::DISCONTINUITY_SEEK;
Andreas Huber42e549e2011-07-13 09:36:11 -070084
Andreas Huberbfcc8d82011-11-29 11:57:35 -080085 int32_t mask;
Andreas Huber42e549e2011-07-13 09:36:11 -070086 if (extra != NULL
87 && extra->findInt32(
Andreas Huberbfcc8d82011-11-29 11:57:35 -080088 IStreamListener::kKeyDiscontinuityMask, &mask)) {
89 if (mask == 0) {
Steve Block29357bc2012-01-06 19:20:56 +000090 ALOGE("Client specified an illegal discontinuity type.");
Andreas Huberbfcc8d82011-11-29 11:57:35 -080091 return ERROR_UNSUPPORTED;
92 }
93
94 type = mask;
Andreas Huber42e549e2011-07-13 09:36:11 -070095 }
96
Andreas Huberbfcc8d82011-11-29 11:57:35 -080097 mTSParser->signalDiscontinuity(
98 (ATSParser::DiscontinuityType)type, extra);
Andreas Huber5bc087c2010-12-23 10:27:40 -080099 } else if (n < 0) {
100 CHECK_EQ(n, -EWOULDBLOCK);
101 break;
102 } else {
103 if (buffer[0] == 0x00) {
104 // XXX legacy
Andreas Huberb7c8e912012-11-27 15:02:53 -0800105
106 if (extra == NULL) {
107 extra = new AMessage;
108 }
109
110 uint8_t type = buffer[1];
111
112 if (type & 2) {
113 int64_t mediaTimeUs;
114 memcpy(&mediaTimeUs, &buffer[2], sizeof(mediaTimeUs));
115
116 extra->setInt64(IStreamListener::kKeyMediaTimeUs, mediaTimeUs);
117 }
118
Andreas Huber5bc087c2010-12-23 10:27:40 -0800119 mTSParser->signalDiscontinuity(
Andreas Huberb7c8e912012-11-27 15:02:53 -0800120 ((type & 1) == 0)
Andreas Huber5bc087c2010-12-23 10:27:40 -0800121 ? ATSParser::DISCONTINUITY_SEEK
Andreas Huber32f3cef2011-03-02 15:34:46 -0800122 : ATSParser::DISCONTINUITY_FORMATCHANGE,
123 extra);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800124 } else {
Andreas Huber06528d72011-08-31 16:29:05 -0700125 status_t err = mTSParser->feedTSPacket(buffer, sizeof(buffer));
126
127 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000128 ALOGE("TS Parser returned error %d", err);
Andreas Huber06528d72011-08-31 16:29:05 -0700129
130 mTSParser->signalEOS(err);
Andreas Hubereac68ba2011-09-27 12:12:25 -0700131 mFinalResult = err;
Andreas Huber06528d72011-08-31 16:29:05 -0700132 break;
133 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800134 }
135 }
136 }
137
Andreas Hubereac68ba2011-09-27 12:12:25 -0700138 return OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800139}
140
Andreas Huber84066782011-08-16 09:34:26 -0700141sp<MetaData> NuPlayer::StreamingSource::getFormatMeta(bool audio) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800142 ATSParser::SourceType type =
Andreas Huber386d6092011-05-19 08:37:39 -0700143 audio ? ATSParser::AUDIO : ATSParser::VIDEO;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800144
145 sp<AnotherPacketSource> source =
146 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
147
148 if (source == NULL) {
149 return NULL;
150 }
151
152 return source->getFormat();
153}
154
155status_t NuPlayer::StreamingSource::dequeueAccessUnit(
156 bool audio, sp<ABuffer> *accessUnit) {
157 ATSParser::SourceType type =
Andreas Huber386d6092011-05-19 08:37:39 -0700158 audio ? ATSParser::AUDIO : ATSParser::VIDEO;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800159
160 sp<AnotherPacketSource> source =
161 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
162
163 if (source == NULL) {
164 return -EWOULDBLOCK;
165 }
166
167 status_t finalResult;
168 if (!source->hasBufferAvailable(&finalResult)) {
169 return finalResult == OK ? -EWOULDBLOCK : finalResult;
170 }
171
Andreas Huber87f2a552012-08-31 13:55:24 -0700172 status_t err = source->dequeueAccessUnit(accessUnit);
173
174#if !defined(LOG_NDEBUG) || LOG_NDEBUG == 0
175 if (err == OK) {
176 int64_t timeUs;
177 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
178 ALOGV("dequeueAccessUnit timeUs=%lld us", timeUs);
179 }
180#endif
181
182 return err;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800183}
184
Andreas Huberd5e56232013-03-12 11:01:43 -0700185bool NuPlayer::StreamingSource::isRealTime() const {
186 return mSource->flags() & IStreamSource::kFlagIsRealTimeData;
187}
188
Andreas Huber5bc087c2010-12-23 10:27:40 -0800189} // namespace android
190