AnotherPacketSource.cpp revision 2a4d22d79e927f2245537921e10fc5fda1c47a29
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
36AnotherPacketSource::~AnotherPacketSource() {
37}
38
39status_t AnotherPacketSource::start(MetaData *params) {
40    return OK;
41}
42
43status_t AnotherPacketSource::stop() {
44    return OK;
45}
46
47sp<MetaData> AnotherPacketSource::getFormat() {
48    return mFormat;
49}
50
51status_t AnotherPacketSource::read(
52        MediaBuffer **out, const ReadOptions *) {
53    *out = NULL;
54
55    Mutex::Autolock autoLock(mLock);
56    while (mEOSResult == OK && mBuffers.empty()) {
57        mCondition.wait(mLock);
58    }
59
60    if (!mBuffers.empty()) {
61        const sp<ABuffer> buffer = *mBuffers.begin();
62        mBuffers.erase(mBuffers.begin());
63
64        int32_t discontinuity;
65        if (buffer->meta()->findInt32("discontinuity", &discontinuity)
66                && discontinuity) {
67            return INFO_DISCONTINUITY;
68        } else {
69            uint64_t timeUs;
70            CHECK(buffer->meta()->findInt64(
71                        "time", (int64_t *)&timeUs));
72
73            MediaBuffer *mediaBuffer = new MediaBuffer(buffer->size());
74            mediaBuffer->meta_data()->setInt64(kKeyTime, timeUs);
75
76            // hexdump(buffer->data(), buffer->size());
77
78            memcpy(mediaBuffer->data(), buffer->data(), buffer->size());
79            *out = mediaBuffer;
80            return OK;
81        }
82    }
83
84    return mEOSResult;
85}
86
87void AnotherPacketSource::queueAccessUnit(const sp<ABuffer> &buffer) {
88    int32_t damaged;
89    if (buffer->meta()->findInt32("damaged", &damaged) && damaged) {
90        // LOG(VERBOSE) << "discarding damaged AU";
91        return;
92    }
93
94    Mutex::Autolock autoLock(mLock);
95    mBuffers.push_back(buffer);
96    mCondition.signal();
97}
98
99void AnotherPacketSource::queueDiscontinuity() {
100    sp<ABuffer> buffer = new ABuffer(0);
101    buffer->meta()->setInt32("discontinuity", true);
102
103    Mutex::Autolock autoLock(mLock);
104    mBuffers.push_back(buffer);
105    mCondition.signal();
106}
107
108void AnotherPacketSource::signalEOS(status_t result) {
109    CHECK(result != OK);
110
111    Mutex::Autolock autoLock(mLock);
112    mEOSResult = result;
113    mCondition.signal();
114}
115
116bool AnotherPacketSource::hasBufferAvailable(status_t *finalResult) {
117    Mutex::Autolock autoLock(mLock);
118    if (!mBuffers.empty()) {
119        return true;
120    }
121
122    *finalResult = mEOSResult;
123    return false;
124}
125
126}  // namespace android
127