blob: 9b04833b99914a4be2c7fee9390a14869964fb69 [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 Huber5ab368a2013-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
46void NuPlayer::StreamingSource::start() {
47 mStreamListener = new NuPlayerStreamListener(mSource, 0);
Andreas Huber52051522012-08-31 13:55:24 -070048
49 uint32_t sourceFlags = mSource->flags();
50
51 uint32_t parserFlags = ATSParser::TS_TIMESTAMPS_ARE_ABSOLUTE;
52 if (sourceFlags & IStreamSource::kFlagAlignedVideoData) {
53 parserFlags |= ATSParser::ALIGNED_VIDEO_DATA;
54 }
55
56 mTSParser = new ATSParser(parserFlags);
Andreas Huber5bc087c2010-12-23 10:27:40 -080057
58 mStreamListener->start();
59}
60
Andreas Hubereac68ba2011-09-27 12:12:25 -070061status_t NuPlayer::StreamingSource::feedMoreTSData() {
62 if (mFinalResult != OK) {
63 return mFinalResult;
Andreas Huber5bc087c2010-12-23 10:27:40 -080064 }
65
Andreas Huber078cfcf2011-09-15 12:25:04 -070066 for (int32_t i = 0; i < 50; ++i) {
Andreas Huber5bc087c2010-12-23 10:27:40 -080067 char buffer[188];
Andreas Huber32f3cef2011-03-02 15:34:46 -080068 sp<AMessage> extra;
69 ssize_t n = mStreamListener->read(buffer, sizeof(buffer), &extra);
Andreas Huber5bc087c2010-12-23 10:27:40 -080070
71 if (n == 0) {
Steve Blockdf64d152012-01-04 20:05:49 +000072 ALOGI("input data EOS reached.");
Andreas Huber5bc087c2010-12-23 10:27:40 -080073 mTSParser->signalEOS(ERROR_END_OF_STREAM);
Andreas Hubereac68ba2011-09-27 12:12:25 -070074 mFinalResult = ERROR_END_OF_STREAM;
Andreas Huber5bc087c2010-12-23 10:27:40 -080075 break;
76 } else if (n == INFO_DISCONTINUITY) {
Andreas Huberbfcc8d82011-11-29 11:57:35 -080077 int32_t type = ATSParser::DISCONTINUITY_SEEK;
Andreas Huber42e549e2011-07-13 09:36:11 -070078
Andreas Huberbfcc8d82011-11-29 11:57:35 -080079 int32_t mask;
Andreas Huber42e549e2011-07-13 09:36:11 -070080 if (extra != NULL
81 && extra->findInt32(
Andreas Huberbfcc8d82011-11-29 11:57:35 -080082 IStreamListener::kKeyDiscontinuityMask, &mask)) {
83 if (mask == 0) {
Steve Block29357bc2012-01-06 19:20:56 +000084 ALOGE("Client specified an illegal discontinuity type.");
Andreas Huberbfcc8d82011-11-29 11:57:35 -080085 return ERROR_UNSUPPORTED;
86 }
87
88 type = mask;
Andreas Huber42e549e2011-07-13 09:36:11 -070089 }
90
Andreas Huberbfcc8d82011-11-29 11:57:35 -080091 mTSParser->signalDiscontinuity(
92 (ATSParser::DiscontinuityType)type, extra);
Andreas Huber5bc087c2010-12-23 10:27:40 -080093 } else if (n < 0) {
94 CHECK_EQ(n, -EWOULDBLOCK);
95 break;
96 } else {
97 if (buffer[0] == 0x00) {
98 // XXX legacy
Andreas Hubera4c5bc02012-11-27 15:02:53 -080099
100 if (extra == NULL) {
101 extra = new AMessage;
102 }
103
104 uint8_t type = buffer[1];
105
106 if (type & 2) {
107 int64_t mediaTimeUs;
108 memcpy(&mediaTimeUs, &buffer[2], sizeof(mediaTimeUs));
109
110 extra->setInt64(IStreamListener::kKeyMediaTimeUs, mediaTimeUs);
111 }
112
Andreas Huber5bc087c2010-12-23 10:27:40 -0800113 mTSParser->signalDiscontinuity(
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800114 ((type & 1) == 0)
Andreas Huber5bc087c2010-12-23 10:27:40 -0800115 ? ATSParser::DISCONTINUITY_SEEK
Andreas Huber32f3cef2011-03-02 15:34:46 -0800116 : ATSParser::DISCONTINUITY_FORMATCHANGE,
117 extra);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800118 } else {
Andreas Huber06528d72011-08-31 16:29:05 -0700119 status_t err = mTSParser->feedTSPacket(buffer, sizeof(buffer));
120
121 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000122 ALOGE("TS Parser returned error %d", err);
Andreas Huber06528d72011-08-31 16:29:05 -0700123
124 mTSParser->signalEOS(err);
Andreas Hubereac68ba2011-09-27 12:12:25 -0700125 mFinalResult = err;
Andreas Huber06528d72011-08-31 16:29:05 -0700126 break;
127 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800128 }
129 }
130 }
131
Andreas Hubereac68ba2011-09-27 12:12:25 -0700132 return OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800133}
134
Andreas Huber84066782011-08-16 09:34:26 -0700135sp<MetaData> NuPlayer::StreamingSource::getFormatMeta(bool audio) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800136 ATSParser::SourceType type =
Andreas Huber386d6092011-05-19 08:37:39 -0700137 audio ? ATSParser::AUDIO : ATSParser::VIDEO;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800138
139 sp<AnotherPacketSource> source =
140 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
141
142 if (source == NULL) {
143 return NULL;
144 }
145
146 return source->getFormat();
147}
148
149status_t NuPlayer::StreamingSource::dequeueAccessUnit(
150 bool audio, sp<ABuffer> *accessUnit) {
151 ATSParser::SourceType type =
Andreas Huber386d6092011-05-19 08:37:39 -0700152 audio ? ATSParser::AUDIO : ATSParser::VIDEO;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800153
154 sp<AnotherPacketSource> source =
155 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
156
157 if (source == NULL) {
158 return -EWOULDBLOCK;
159 }
160
161 status_t finalResult;
162 if (!source->hasBufferAvailable(&finalResult)) {
163 return finalResult == OK ? -EWOULDBLOCK : finalResult;
164 }
165
Andreas Huber52051522012-08-31 13:55:24 -0700166 status_t err = source->dequeueAccessUnit(accessUnit);
167
168#if !defined(LOG_NDEBUG) || LOG_NDEBUG == 0
169 if (err == OK) {
170 int64_t timeUs;
171 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
172 ALOGV("dequeueAccessUnit timeUs=%lld us", timeUs);
173 }
174#endif
175
176 return err;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800177}
178
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800179uint32_t NuPlayer::StreamingSource::flags() const {
180 return 0;
181}
182
Andreas Huber5bc087c2010-12-23 10:27:40 -0800183} // namespace android
184