AnotherPacketSource.cpp revision 22fc52f6f72f39e33c3970d0291de3569118aa5c
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#include "AnotherPacketSource.h"
18
19#include <media/stagefright/foundation/ABuffer.h>
20#include <media/stagefright/foundation/ADebug.h>
21#include <media/stagefright/foundation/AMessage.h>
22#include <media/stagefright/foundation/AString.h>
23#include <media/stagefright/foundation/hexdump.h>
24#include <media/stagefright/MediaBuffer.h>
25#include <media/stagefright/MediaDefs.h>
26#include <media/stagefright/MetaData.h>
27#include <utils/Vector.h>
28
29namespace android {
30
31AnotherPacketSource::AnotherPacketSource(const sp<MetaData> &meta)
32    : mFormat(meta),
33      mEOSResult(OK) {
34}
35
36void AnotherPacketSource::setFormat(const sp<MetaData> &meta) {
37    CHECK(mFormat == NULL);
38    mFormat = meta;
39}
40
41AnotherPacketSource::~AnotherPacketSource() {
42}
43
44status_t AnotherPacketSource::start(MetaData *params) {
45    return OK;
46}
47
48status_t AnotherPacketSource::stop() {
49    return OK;
50}
51
52sp<MetaData> AnotherPacketSource::getFormat() {
53    return mFormat;
54}
55
56status_t AnotherPacketSource::dequeueAccessUnit(sp<ABuffer> *buffer) {
57    buffer->clear();
58
59    Mutex::Autolock autoLock(mLock);
60    while (mEOSResult == OK && mBuffers.empty()) {
61        mCondition.wait(mLock);
62    }
63
64    if (!mBuffers.empty()) {
65        *buffer = *mBuffers.begin();
66        mBuffers.erase(mBuffers.begin());
67
68        int32_t discontinuity;
69        if ((*buffer)->meta()->findInt32("discontinuity", &discontinuity)) {
70
71            if (discontinuity == ATSParser::DISCONTINUITY_FORMATCHANGE) {
72                mFormat.clear();
73            }
74
75            return INFO_DISCONTINUITY;
76        }
77
78        return OK;
79    }
80
81    return mEOSResult;
82}
83
84status_t AnotherPacketSource::read(
85        MediaBuffer **out, const ReadOptions *) {
86    *out = NULL;
87
88    Mutex::Autolock autoLock(mLock);
89    while (mEOSResult == OK && mBuffers.empty()) {
90        mCondition.wait(mLock);
91    }
92
93    if (!mBuffers.empty()) {
94        const sp<ABuffer> buffer = *mBuffers.begin();
95        mBuffers.erase(mBuffers.begin());
96
97        int32_t discontinuity;
98        if (buffer->meta()->findInt32("discontinuity", &discontinuity)) {
99            if (discontinuity == ATSParser::DISCONTINUITY_FORMATCHANGE) {
100                mFormat.clear();
101            }
102
103            return INFO_DISCONTINUITY;
104        } else {
105            int64_t timeUs;
106            CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
107
108            MediaBuffer *mediaBuffer = new MediaBuffer(buffer->size());
109            mediaBuffer->meta_data()->setInt64(kKeyTime, timeUs);
110
111            // hexdump(buffer->data(), buffer->size());
112
113            memcpy(mediaBuffer->data(), buffer->data(), buffer->size());
114            *out = mediaBuffer;
115            return OK;
116        }
117    }
118
119    return mEOSResult;
120}
121
122void AnotherPacketSource::queueAccessUnit(const sp<ABuffer> &buffer) {
123    int32_t damaged;
124    if (buffer->meta()->findInt32("damaged", &damaged) && damaged) {
125        // LOG(VERBOSE) << "discarding damaged AU";
126        return;
127    }
128
129    int64_t timeUs;
130    CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
131    LOGV("queueAccessUnit timeUs=%lld us (%.2f secs)", timeUs, timeUs / 1E6);
132
133    Mutex::Autolock autoLock(mLock);
134    mBuffers.push_back(buffer);
135    mCondition.signal();
136}
137
138void AnotherPacketSource::queueDiscontinuity(
139        ATSParser::DiscontinuityType type) {
140    sp<ABuffer> buffer = new ABuffer(0);
141    buffer->meta()->setInt32("discontinuity", static_cast<int32_t>(type));
142
143    Mutex::Autolock autoLock(mLock);
144
145#if 0
146    if (type == ATSParser::DISCONTINUITY_SEEK
147            || type == ATSParser::DISCONTINUITY_FORMATCHANGE) {
148        // XXX Fix this: This will also clear any pending discontinuities,
149        // If there's a pending DISCONTINUITY_FORMATCHANGE and the new
150        // discontinuity is "just" a DISCONTINUITY_SEEK, this will effectively
151        // downgrade the type of discontinuity received by the client.
152
153        mBuffers.clear();
154        mEOSResult = OK;
155    }
156#endif
157
158    mBuffers.push_back(buffer);
159    mCondition.signal();
160}
161
162void AnotherPacketSource::signalEOS(status_t result) {
163    CHECK(result != OK);
164
165    Mutex::Autolock autoLock(mLock);
166    mEOSResult = result;
167    mCondition.signal();
168}
169
170bool AnotherPacketSource::hasBufferAvailable(status_t *finalResult) {
171    Mutex::Autolock autoLock(mLock);
172    if (!mBuffers.empty()) {
173        return true;
174    }
175
176    *finalResult = mEOSResult;
177    return false;
178}
179
180status_t AnotherPacketSource::nextBufferTime(int64_t *timeUs) {
181    *timeUs = 0;
182
183    Mutex::Autolock autoLock(mLock);
184
185    if (mBuffers.empty()) {
186        return mEOSResult != OK ? mEOSResult : -EWOULDBLOCK;
187    }
188
189    sp<ABuffer> buffer = *mBuffers.begin();
190    CHECK(buffer->meta()->findInt64("timeUs", timeUs));
191
192    return OK;
193}
194
195}  // namespace android
196