Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 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/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 | |
| 33 | namespace android { |
| 34 | |
| 35 | NuPlayer::StreamingSource::StreamingSource(const sp<IStreamSource> &source) |
| 36 | : mSource(source), |
Andreas Huber | eac68ba | 2011-09-27 12:12:25 -0700 | [diff] [blame] | 37 | mFinalResult(OK) { |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | NuPlayer::StreamingSource::~StreamingSource() { |
| 41 | } |
| 42 | |
| 43 | void NuPlayer::StreamingSource::start() { |
| 44 | mStreamListener = new NuPlayerStreamListener(mSource, 0); |
Andreas Huber | 87f2a55 | 2012-08-31 13:55:24 -0700 | [diff] [blame^] | 45 | |
| 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 Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 54 | |
| 55 | mStreamListener->start(); |
| 56 | } |
| 57 | |
Andreas Huber | eac68ba | 2011-09-27 12:12:25 -0700 | [diff] [blame] | 58 | status_t NuPlayer::StreamingSource::feedMoreTSData() { |
| 59 | if (mFinalResult != OK) { |
| 60 | return mFinalResult; |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Andreas Huber | 078cfcf | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 63 | for (int32_t i = 0; i < 50; ++i) { |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 64 | char buffer[188]; |
Andreas Huber | 32f3cef | 2011-03-02 15:34:46 -0800 | [diff] [blame] | 65 | sp<AMessage> extra; |
| 66 | ssize_t n = mStreamListener->read(buffer, sizeof(buffer), &extra); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 67 | |
| 68 | if (n == 0) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 69 | ALOGI("input data EOS reached."); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 70 | mTSParser->signalEOS(ERROR_END_OF_STREAM); |
Andreas Huber | eac68ba | 2011-09-27 12:12:25 -0700 | [diff] [blame] | 71 | mFinalResult = ERROR_END_OF_STREAM; |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 72 | break; |
| 73 | } else if (n == INFO_DISCONTINUITY) { |
Andreas Huber | bfcc8d8 | 2011-11-29 11:57:35 -0800 | [diff] [blame] | 74 | int32_t type = ATSParser::DISCONTINUITY_SEEK; |
Andreas Huber | 42e549e | 2011-07-13 09:36:11 -0700 | [diff] [blame] | 75 | |
Andreas Huber | bfcc8d8 | 2011-11-29 11:57:35 -0800 | [diff] [blame] | 76 | int32_t mask; |
Andreas Huber | 42e549e | 2011-07-13 09:36:11 -0700 | [diff] [blame] | 77 | if (extra != NULL |
| 78 | && extra->findInt32( |
Andreas Huber | bfcc8d8 | 2011-11-29 11:57:35 -0800 | [diff] [blame] | 79 | IStreamListener::kKeyDiscontinuityMask, &mask)) { |
| 80 | if (mask == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 81 | ALOGE("Client specified an illegal discontinuity type."); |
Andreas Huber | bfcc8d8 | 2011-11-29 11:57:35 -0800 | [diff] [blame] | 82 | return ERROR_UNSUPPORTED; |
| 83 | } |
| 84 | |
| 85 | type = mask; |
Andreas Huber | 42e549e | 2011-07-13 09:36:11 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Andreas Huber | bfcc8d8 | 2011-11-29 11:57:35 -0800 | [diff] [blame] | 88 | mTSParser->signalDiscontinuity( |
| 89 | (ATSParser::DiscontinuityType)type, extra); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 90 | } else if (n < 0) { |
| 91 | CHECK_EQ(n, -EWOULDBLOCK); |
| 92 | break; |
| 93 | } else { |
| 94 | if (buffer[0] == 0x00) { |
| 95 | // XXX legacy |
| 96 | mTSParser->signalDiscontinuity( |
| 97 | buffer[1] == 0x00 |
| 98 | ? ATSParser::DISCONTINUITY_SEEK |
Andreas Huber | 32f3cef | 2011-03-02 15:34:46 -0800 | [diff] [blame] | 99 | : ATSParser::DISCONTINUITY_FORMATCHANGE, |
| 100 | extra); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 101 | } else { |
Andreas Huber | 06528d7 | 2011-08-31 16:29:05 -0700 | [diff] [blame] | 102 | status_t err = mTSParser->feedTSPacket(buffer, sizeof(buffer)); |
| 103 | |
| 104 | if (err != OK) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 105 | ALOGE("TS Parser returned error %d", err); |
Andreas Huber | 06528d7 | 2011-08-31 16:29:05 -0700 | [diff] [blame] | 106 | |
| 107 | mTSParser->signalEOS(err); |
Andreas Huber | eac68ba | 2011-09-27 12:12:25 -0700 | [diff] [blame] | 108 | mFinalResult = err; |
Andreas Huber | 06528d7 | 2011-08-31 16:29:05 -0700 | [diff] [blame] | 109 | break; |
| 110 | } |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
Andreas Huber | eac68ba | 2011-09-27 12:12:25 -0700 | [diff] [blame] | 115 | return OK; |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Andreas Huber | 8406678 | 2011-08-16 09:34:26 -0700 | [diff] [blame] | 118 | sp<MetaData> NuPlayer::StreamingSource::getFormatMeta(bool audio) { |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 119 | ATSParser::SourceType type = |
Andreas Huber | 386d609 | 2011-05-19 08:37:39 -0700 | [diff] [blame] | 120 | audio ? ATSParser::AUDIO : ATSParser::VIDEO; |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 121 | |
| 122 | sp<AnotherPacketSource> source = |
| 123 | static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get()); |
| 124 | |
| 125 | if (source == NULL) { |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | return source->getFormat(); |
| 130 | } |
| 131 | |
| 132 | status_t NuPlayer::StreamingSource::dequeueAccessUnit( |
| 133 | bool audio, sp<ABuffer> *accessUnit) { |
| 134 | ATSParser::SourceType type = |
Andreas Huber | 386d609 | 2011-05-19 08:37:39 -0700 | [diff] [blame] | 135 | audio ? ATSParser::AUDIO : ATSParser::VIDEO; |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 136 | |
| 137 | sp<AnotherPacketSource> source = |
| 138 | static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get()); |
| 139 | |
| 140 | if (source == NULL) { |
| 141 | return -EWOULDBLOCK; |
| 142 | } |
| 143 | |
| 144 | status_t finalResult; |
| 145 | if (!source->hasBufferAvailable(&finalResult)) { |
| 146 | return finalResult == OK ? -EWOULDBLOCK : finalResult; |
| 147 | } |
| 148 | |
Andreas Huber | 87f2a55 | 2012-08-31 13:55:24 -0700 | [diff] [blame^] | 149 | status_t err = source->dequeueAccessUnit(accessUnit); |
| 150 | |
| 151 | #if !defined(LOG_NDEBUG) || LOG_NDEBUG == 0 |
| 152 | if (err == OK) { |
| 153 | int64_t timeUs; |
| 154 | CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs)); |
| 155 | ALOGV("dequeueAccessUnit timeUs=%lld us", timeUs); |
| 156 | } |
| 157 | #endif |
| 158 | |
| 159 | return err; |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | } // namespace android |
| 163 | |