Eino-Ville Talvala | 78822d7 | 2012-07-18 17:52:18 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2012 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 "MediaConsumer" |
| 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 20 | #include <utils/Log.h> |
| 21 | |
| 22 | #include "MediaConsumer.h" |
| 23 | |
| 24 | #define MC_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__) |
| 25 | #define MC_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__) |
| 26 | #define MC_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__) |
| 27 | #define MC_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__) |
| 28 | #define MC_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__) |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | // Get an ID that's unique within this process. |
| 33 | static int32_t createProcessUniqueId() { |
| 34 | static volatile int32_t globalCounter = 0; |
| 35 | return android_atomic_inc(&globalCounter); |
| 36 | } |
| 37 | |
| 38 | MediaConsumer::MediaConsumer(uint32_t maxLockedBuffers) : |
| 39 | mMaxLockedBuffers(maxLockedBuffers), |
| 40 | mCurrentLockedBuffers(0) |
| 41 | { |
| 42 | mName = String8::format("mc-unnamed-%d-%d", getpid(), |
| 43 | createProcessUniqueId()); |
| 44 | |
| 45 | mBufferQueue = new BufferQueue(true); |
| 46 | |
| 47 | wp<BufferQueue::ConsumerListener> listener; |
| 48 | sp<BufferQueue::ConsumerListener> proxy; |
| 49 | listener = static_cast<BufferQueue::ConsumerListener*>(this); |
| 50 | proxy = new BufferQueue::ProxyConsumerListener(listener); |
| 51 | |
| 52 | status_t err = mBufferQueue->consumerConnect(proxy); |
| 53 | if (err != NO_ERROR) { |
| 54 | ALOGE("MediaConsumer: error connecting to BufferQueue: %s (%d)", |
| 55 | strerror(-err), err); |
| 56 | } else { |
| 57 | mBufferQueue->setSynchronousMode(true); |
| 58 | mBufferQueue->setConsumerUsageBits(GRALLOC_USAGE_HW_VIDEO_ENCODER); |
| 59 | mBufferQueue->setConsumerName(mName); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | MediaConsumer::~MediaConsumer() |
| 64 | { |
| 65 | Mutex::Autolock _l(mMutex); |
| 66 | for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { |
| 67 | freeBufferLocked(i); |
| 68 | } |
| 69 | mBufferQueue->consumerDisconnect(); |
| 70 | mBufferQueue.clear(); |
| 71 | } |
| 72 | |
| 73 | void MediaConsumer::setName(const String8& name) { |
| 74 | Mutex::Autolock _l(mMutex); |
| 75 | mName = name; |
| 76 | mBufferQueue->setConsumerName(name); |
| 77 | } |
| 78 | |
| 79 | status_t MediaConsumer::getNextBuffer(buffer_handle_t *buffer, nsecs_t *timestamp) { |
| 80 | status_t err; |
| 81 | |
| 82 | if (!buffer) return BAD_VALUE; |
| 83 | if (mCurrentLockedBuffers == mMaxLockedBuffers) { |
| 84 | return INVALID_OPERATION; |
| 85 | } |
| 86 | |
| 87 | BufferQueue::BufferItem b; |
| 88 | |
| 89 | Mutex::Autolock _l(mMutex); |
| 90 | |
| 91 | err = mBufferQueue->acquireBuffer(&b); |
| 92 | if (err != OK) { |
| 93 | if (err == BufferQueue::NO_BUFFER_AVAILABLE) { |
| 94 | return BAD_VALUE; |
| 95 | } else { |
| 96 | MC_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err); |
| 97 | return err; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | int buf = b.mBuf; |
| 102 | |
| 103 | if (b.mGraphicBuffer != NULL) { |
| 104 | mBufferSlot[buf] = b.mGraphicBuffer; |
| 105 | } |
| 106 | |
| 107 | if (b.mFence.get()) { |
| 108 | err = b.mFence->wait(Fence::TIMEOUT_NEVER); |
| 109 | if (err != OK) { |
| 110 | MC_LOGE("Failed to wait for fence of acquired buffer: %s (%d)", |
| 111 | strerror(-err), err); |
| 112 | return err; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | *buffer = mBufferSlot[buf]->handle; |
| 117 | *timestamp = b.mTimestamp; |
| 118 | |
| 119 | mCurrentLockedBuffers++; |
| 120 | |
| 121 | return OK; |
| 122 | } |
| 123 | |
| 124 | status_t MediaConsumer::freeBuffer(buffer_handle_t buffer) { |
| 125 | Mutex::Autolock _l(mMutex); |
| 126 | int buf = 0; |
| 127 | status_t err; |
| 128 | |
| 129 | for (; buf < BufferQueue::NUM_BUFFER_SLOTS; buf++) { |
| 130 | if (buffer == mBufferSlot[buf]->handle) break; |
| 131 | } |
| 132 | if (buf == BufferQueue::NUM_BUFFER_SLOTS) { |
| 133 | MC_LOGE("%s: Can't find buffer to free", __FUNCTION__); |
| 134 | return BAD_VALUE; |
| 135 | } |
| 136 | |
| 137 | err = mBufferQueue->releaseBuffer(buf, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, |
| 138 | Fence::NO_FENCE); |
| 139 | if (err == BufferQueue::STALE_BUFFER_SLOT) { |
| 140 | freeBufferLocked(buf); |
| 141 | } else if (err != OK) { |
| 142 | MC_LOGE("%s: Unable to release graphic buffer %d to queue", __FUNCTION__, |
| 143 | buf); |
| 144 | return err; |
| 145 | } |
| 146 | |
| 147 | mCurrentLockedBuffers--; |
| 148 | |
| 149 | return OK; |
| 150 | } |
| 151 | |
| 152 | void MediaConsumer::setFrameAvailableListener( |
| 153 | const sp<FrameAvailableListener>& listener) { |
| 154 | MC_LOGV("setFrameAvailableListener"); |
| 155 | Mutex::Autolock lock(mMutex); |
| 156 | mFrameAvailableListener = listener; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | void MediaConsumer::onFrameAvailable() { |
| 161 | MC_LOGV("onFrameAvailable"); |
| 162 | sp<FrameAvailableListener> listener; |
| 163 | { // scope for the lock |
| 164 | Mutex::Autolock _l(mMutex); |
| 165 | listener = mFrameAvailableListener; |
| 166 | } |
| 167 | |
| 168 | if (listener != NULL) { |
| 169 | MC_LOGV("actually calling onFrameAvailable"); |
| 170 | listener->onFrameAvailable(); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void MediaConsumer::onBuffersReleased() { |
| 175 | MC_LOGV("onBuffersReleased"); |
| 176 | |
| 177 | Mutex::Autolock lock(mMutex); |
| 178 | |
| 179 | uint32_t mask = 0; |
| 180 | mBufferQueue->getReleasedBuffers(&mask); |
| 181 | for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { |
| 182 | if (mask & (1 << i)) { |
| 183 | freeBufferLocked(i); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | } |
| 188 | |
| 189 | status_t MediaConsumer::freeBufferLocked(int buf) { |
| 190 | status_t err = OK; |
| 191 | |
| 192 | mBufferSlot[buf] = NULL; |
| 193 | return err; |
| 194 | } |
| 195 | |
| 196 | } // namespace android |