AnotherPacketSource.cpp revision cda17c606b0fe3ccda4dc68a6d43882410ea2462
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
63        uint64_t timeUs;
64        CHECK(buffer->meta()->findInt64(
65                    "time", (int64_t *)&timeUs));
66
67        MediaBuffer *mediaBuffer = new MediaBuffer(buffer->size());
68        mediaBuffer->meta_data()->setInt64(kKeyTime, timeUs);
69
70        // hexdump(buffer->data(), buffer->size());
71
72        memcpy(mediaBuffer->data(), buffer->data(), buffer->size());
73        *out = mediaBuffer;
74
75        mBuffers.erase(mBuffers.begin());
76        return OK;
77    }
78
79    return mEOSResult;
80}
81
82void AnotherPacketSource::queueAccessUnit(const sp<ABuffer> &buffer) {
83    int32_t damaged;
84    if (buffer->meta()->findInt32("damaged", &damaged) && damaged) {
85        // LOG(VERBOSE) << "discarding damaged AU";
86        return;
87    }
88
89    Mutex::Autolock autoLock(mLock);
90    mBuffers.push_back(buffer);
91    mCondition.signal();
92}
93
94void AnotherPacketSource::signalEOS(status_t result) {
95    CHECK(result != OK);
96
97    Mutex::Autolock autoLock(mLock);
98    mEOSResult = result;
99    mCondition.signal();
100}
101
102bool AnotherPacketSource::hasBufferAvailable(status_t *finalResult) {
103    Mutex::Autolock autoLock(mLock);
104    if (!mBuffers.empty()) {
105        return true;
106    }
107
108    *finalResult = mEOSResult;
109    return false;
110}
111
112}  // namespace android
113