AnotherPacketSource.cpp revision 5bc087c573c70c84c6a39946457590b42d392a33
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    mBuffers.push_back(buffer);
146    mCondition.signal();
147}
148
149void AnotherPacketSource::clear() {
150    Mutex::Autolock autoLock(mLock);
151    mBuffers.clear();
152    mEOSResult = OK;
153}
154
155void AnotherPacketSource::signalEOS(status_t result) {
156    CHECK(result != OK);
157
158    Mutex::Autolock autoLock(mLock);
159    mEOSResult = result;
160    mCondition.signal();
161}
162
163bool AnotherPacketSource::hasBufferAvailable(status_t *finalResult) {
164    Mutex::Autolock autoLock(mLock);
165    if (!mBuffers.empty()) {
166        return true;
167    }
168
169    *finalResult = mEOSResult;
170    return false;
171}
172
173status_t AnotherPacketSource::nextBufferTime(int64_t *timeUs) {
174    *timeUs = 0;
175
176    Mutex::Autolock autoLock(mLock);
177
178    if (mBuffers.empty()) {
179        return mEOSResult != OK ? mEOSResult : -EWOULDBLOCK;
180    }
181
182    sp<ABuffer> buffer = *mBuffers.begin();
183    CHECK(buffer->meta()->findInt64("timeUs", timeUs));
184
185    return OK;
186}
187
188}  // namespace android
189