blob: 7319e4ceb5854c41d777ec4a71c303a2d498e34f [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),
37 mEOS(false) {
38}
39
40NuPlayer::StreamingSource::~StreamingSource() {
41}
42
43void NuPlayer::StreamingSource::start() {
44 mStreamListener = new NuPlayerStreamListener(mSource, 0);
Andreas Huberc4c17d42011-08-30 16:06:28 -070045 mTSParser = new ATSParser(ATSParser::TS_TIMESTAMPS_ARE_ABSOLUTE);
Andreas Huber5bc087c2010-12-23 10:27:40 -080046
47 mStreamListener->start();
48}
49
50bool NuPlayer::StreamingSource::feedMoreTSData() {
51 if (mEOS) {
52 return false;
53 }
54
Andreas Huber078cfcf2011-09-15 12:25:04 -070055 for (int32_t i = 0; i < 50; ++i) {
Andreas Huber5bc087c2010-12-23 10:27:40 -080056 char buffer[188];
Andreas Huber32f3cef2011-03-02 15:34:46 -080057 sp<AMessage> extra;
58 ssize_t n = mStreamListener->read(buffer, sizeof(buffer), &extra);
Andreas Huber5bc087c2010-12-23 10:27:40 -080059
60 if (n == 0) {
61 LOGI("input data EOS reached.");
62 mTSParser->signalEOS(ERROR_END_OF_STREAM);
63 mEOS = true;
64 break;
65 } else if (n == INFO_DISCONTINUITY) {
Andreas Huber42e549e2011-07-13 09:36:11 -070066 ATSParser::DiscontinuityType type = ATSParser::DISCONTINUITY_SEEK;
67
68 int32_t formatChange;
69 if (extra != NULL
70 && extra->findInt32(
71 IStreamListener::kKeyFormatChange, &formatChange)
72 && formatChange != 0) {
73 type = ATSParser::DISCONTINUITY_FORMATCHANGE;
74 }
75
76 mTSParser->signalDiscontinuity(type, extra);
Andreas Huber5bc087c2010-12-23 10:27:40 -080077 } else if (n < 0) {
78 CHECK_EQ(n, -EWOULDBLOCK);
79 break;
80 } else {
81 if (buffer[0] == 0x00) {
82 // XXX legacy
83 mTSParser->signalDiscontinuity(
84 buffer[1] == 0x00
85 ? ATSParser::DISCONTINUITY_SEEK
Andreas Huber32f3cef2011-03-02 15:34:46 -080086 : ATSParser::DISCONTINUITY_FORMATCHANGE,
87 extra);
Andreas Huber5bc087c2010-12-23 10:27:40 -080088 } else {
Andreas Huber06528d72011-08-31 16:29:05 -070089 status_t err = mTSParser->feedTSPacket(buffer, sizeof(buffer));
90
91 if (err != OK) {
92 LOGE("TS Parser returned error %d", err);
93
94 mTSParser->signalEOS(err);
95 mEOS = true;
96 break;
97 }
Andreas Huber5bc087c2010-12-23 10:27:40 -080098 }
99 }
100 }
101
102 return true;
103}
104
105sp<MetaData> NuPlayer::StreamingSource::getFormat(bool audio) {
106 ATSParser::SourceType type =
Andreas Huber386d6092011-05-19 08:37:39 -0700107 audio ? ATSParser::AUDIO : ATSParser::VIDEO;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800108
109 sp<AnotherPacketSource> source =
110 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
111
112 if (source == NULL) {
113 return NULL;
114 }
115
116 return source->getFormat();
117}
118
119status_t NuPlayer::StreamingSource::dequeueAccessUnit(
120 bool audio, sp<ABuffer> *accessUnit) {
121 ATSParser::SourceType type =
Andreas Huber386d6092011-05-19 08:37:39 -0700122 audio ? ATSParser::AUDIO : ATSParser::VIDEO;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800123
124 sp<AnotherPacketSource> source =
125 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
126
127 if (source == NULL) {
128 return -EWOULDBLOCK;
129 }
130
131 status_t finalResult;
132 if (!source->hasBufferAvailable(&finalResult)) {
133 return finalResult == OK ? -EWOULDBLOCK : finalResult;
134 }
135
136 return source->dequeueAccessUnit(accessUnit);
137}
138
139} // namespace android
140