blob: 7159404175e88664086a5fb19a56d226d14fd913 [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
35NuPlayer::StreamingSource::StreamingSource(const sp<IStreamSource> &source)
36 : mSource(source),
Andreas Hubereac68ba2011-09-27 12:12:25 -070037 mFinalResult(OK) {
Andreas Huber5bc087c2010-12-23 10:27:40 -080038}
39
40NuPlayer::StreamingSource::~StreamingSource() {
41}
42
43void NuPlayer::StreamingSource::start() {
44 mStreamListener = new NuPlayerStreamListener(mSource, 0);
Andreas Huber87f2a552012-08-31 13:55:24 -070045
46 uint32_t sourceFlags = mSource->flags();
47
48 uint32_t parserFlags = ATSParser::TS_TIMESTAMPS_ARE_ABSOLUTE;
49 if (sourceFlags & IStreamSource::kFlagAlignedVideoData) {
50 parserFlags |= ATSParser::ALIGNED_VIDEO_DATA;
51 }
52
53 mTSParser = new ATSParser(parserFlags);
Andreas Huber5bc087c2010-12-23 10:27:40 -080054
55 mStreamListener->start();
56}
57
Andreas Hubereac68ba2011-09-27 12:12:25 -070058status_t NuPlayer::StreamingSource::feedMoreTSData() {
59 if (mFinalResult != OK) {
60 return mFinalResult;
Andreas Huber5bc087c2010-12-23 10:27:40 -080061 }
62
Andreas Huber078cfcf2011-09-15 12:25:04 -070063 for (int32_t i = 0; i < 50; ++i) {
Andreas Huber5bc087c2010-12-23 10:27:40 -080064 char buffer[188];
Andreas Huber32f3cef2011-03-02 15:34:46 -080065 sp<AMessage> extra;
66 ssize_t n = mStreamListener->read(buffer, sizeof(buffer), &extra);
Andreas Huber5bc087c2010-12-23 10:27:40 -080067
68 if (n == 0) {
Steve Blockdf64d152012-01-04 20:05:49 +000069 ALOGI("input data EOS reached.");
Andreas Huber5bc087c2010-12-23 10:27:40 -080070 mTSParser->signalEOS(ERROR_END_OF_STREAM);
Andreas Hubereac68ba2011-09-27 12:12:25 -070071 mFinalResult = ERROR_END_OF_STREAM;
Andreas Huber5bc087c2010-12-23 10:27:40 -080072 break;
73 } else if (n == INFO_DISCONTINUITY) {
Andreas Huberbfcc8d82011-11-29 11:57:35 -080074 int32_t type = ATSParser::DISCONTINUITY_SEEK;
Andreas Huber42e549e2011-07-13 09:36:11 -070075
Andreas Huberbfcc8d82011-11-29 11:57:35 -080076 int32_t mask;
Andreas Huber42e549e2011-07-13 09:36:11 -070077 if (extra != NULL
78 && extra->findInt32(
Andreas Huberbfcc8d82011-11-29 11:57:35 -080079 IStreamListener::kKeyDiscontinuityMask, &mask)) {
80 if (mask == 0) {
Steve Block29357bc2012-01-06 19:20:56 +000081 ALOGE("Client specified an illegal discontinuity type.");
Andreas Huberbfcc8d82011-11-29 11:57:35 -080082 return ERROR_UNSUPPORTED;
83 }
84
85 type = mask;
Andreas Huber42e549e2011-07-13 09:36:11 -070086 }
87
Andreas Huberbfcc8d82011-11-29 11:57:35 -080088 mTSParser->signalDiscontinuity(
89 (ATSParser::DiscontinuityType)type, extra);
Andreas Huber5bc087c2010-12-23 10:27:40 -080090 } else if (n < 0) {
91 CHECK_EQ(n, -EWOULDBLOCK);
92 break;
93 } else {
94 if (buffer[0] == 0x00) {
95 // XXX legacy
Andreas Huberb7c8e912012-11-27 15:02:53 -080096
97 if (extra == NULL) {
98 extra = new AMessage;
99 }
100
101 uint8_t type = buffer[1];
102
103 if (type & 2) {
104 int64_t mediaTimeUs;
105 memcpy(&mediaTimeUs, &buffer[2], sizeof(mediaTimeUs));
106
107 extra->setInt64(IStreamListener::kKeyMediaTimeUs, mediaTimeUs);
108 }
109
Andreas Huber5bc087c2010-12-23 10:27:40 -0800110 mTSParser->signalDiscontinuity(
Andreas Huberb7c8e912012-11-27 15:02:53 -0800111 ((type & 1) == 0)
Andreas Huber5bc087c2010-12-23 10:27:40 -0800112 ? ATSParser::DISCONTINUITY_SEEK
Andreas Huber32f3cef2011-03-02 15:34:46 -0800113 : ATSParser::DISCONTINUITY_FORMATCHANGE,
114 extra);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800115 } else {
Andreas Huber06528d72011-08-31 16:29:05 -0700116 status_t err = mTSParser->feedTSPacket(buffer, sizeof(buffer));
117
118 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000119 ALOGE("TS Parser returned error %d", err);
Andreas Huber06528d72011-08-31 16:29:05 -0700120
121 mTSParser->signalEOS(err);
Andreas Hubereac68ba2011-09-27 12:12:25 -0700122 mFinalResult = err;
Andreas Huber06528d72011-08-31 16:29:05 -0700123 break;
124 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800125 }
126 }
127 }
128
Andreas Hubereac68ba2011-09-27 12:12:25 -0700129 return OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800130}
131
Andreas Huber84066782011-08-16 09:34:26 -0700132sp<MetaData> NuPlayer::StreamingSource::getFormatMeta(bool audio) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800133 ATSParser::SourceType type =
Andreas Huber386d6092011-05-19 08:37:39 -0700134 audio ? ATSParser::AUDIO : ATSParser::VIDEO;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800135
136 sp<AnotherPacketSource> source =
137 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
138
139 if (source == NULL) {
140 return NULL;
141 }
142
143 return source->getFormat();
144}
145
146status_t NuPlayer::StreamingSource::dequeueAccessUnit(
147 bool audio, sp<ABuffer> *accessUnit) {
148 ATSParser::SourceType type =
Andreas Huber386d6092011-05-19 08:37:39 -0700149 audio ? ATSParser::AUDIO : ATSParser::VIDEO;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800150
151 sp<AnotherPacketSource> source =
152 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
153
154 if (source == NULL) {
155 return -EWOULDBLOCK;
156 }
157
158 status_t finalResult;
159 if (!source->hasBufferAvailable(&finalResult)) {
160 return finalResult == OK ? -EWOULDBLOCK : finalResult;
161 }
162
Andreas Huber87f2a552012-08-31 13:55:24 -0700163 status_t err = source->dequeueAccessUnit(accessUnit);
164
165#if !defined(LOG_NDEBUG) || LOG_NDEBUG == 0
166 if (err == OK) {
167 int64_t timeUs;
168 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
169 ALOGV("dequeueAccessUnit timeUs=%lld us", timeUs);
170 }
171#endif
172
173 return err;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800174}
175
Andreas Huberb7c8e912012-11-27 15:02:53 -0800176uint32_t NuPlayer::StreamingSource::flags() const {
177 return 0;
178}
179
Andreas Huber5bc087c2010-12-23 10:27:40 -0800180} // namespace android
181