blob: a5fe302d4383314e7d4ef70655fe5ff6f4a325fe [file] [log] [blame]
Eino-Ville Talvala78822d72012-07-18 17:52:18 -07001/*
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
30namespace android {
31
32// Get an ID that's unique within this process.
33static int32_t createProcessUniqueId() {
34 static volatile int32_t globalCounter = 0;
35 return android_atomic_inc(&globalCounter);
36}
37
38MediaConsumer::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
63MediaConsumer::~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
73void MediaConsumer::setName(const String8& name) {
74 Mutex::Autolock _l(mMutex);
75 mName = name;
76 mBufferQueue->setConsumerName(name);
77}
78
79status_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) {
Eino-Ville Talvala228a5382012-08-13 12:16:06 -070084 MC_LOGV("Too many buffers (max %d)", mCurrentLockedBuffers);
Eino-Ville Talvala78822d72012-07-18 17:52:18 -070085 return INVALID_OPERATION;
86 }
87
88 BufferQueue::BufferItem b;
89
90 Mutex::Autolock _l(mMutex);
91
92 err = mBufferQueue->acquireBuffer(&b);
93 if (err != OK) {
94 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Eino-Ville Talvala228a5382012-08-13 12:16:06 -070095 MC_LOGV("No buffer available");
Eino-Ville Talvala78822d72012-07-18 17:52:18 -070096 return BAD_VALUE;
97 } else {
98 MC_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
99 return err;
100 }
101 }
102
103 int buf = b.mBuf;
104
105 if (b.mGraphicBuffer != NULL) {
106 mBufferSlot[buf] = b.mGraphicBuffer;
107 }
108
109 if (b.mFence.get()) {
110 err = b.mFence->wait(Fence::TIMEOUT_NEVER);
111 if (err != OK) {
112 MC_LOGE("Failed to wait for fence of acquired buffer: %s (%d)",
113 strerror(-err), err);
114 return err;
115 }
116 }
117
118 *buffer = mBufferSlot[buf]->handle;
119 *timestamp = b.mTimestamp;
120
121 mCurrentLockedBuffers++;
Eino-Ville Talvala228a5382012-08-13 12:16:06 -0700122 MC_LOGV("getNextBuffer: %d buffers in use", mCurrentLockedBuffers);
Eino-Ville Talvala78822d72012-07-18 17:52:18 -0700123 return OK;
124}
125
126status_t MediaConsumer::freeBuffer(buffer_handle_t buffer) {
127 Mutex::Autolock _l(mMutex);
128 int buf = 0;
129 status_t err;
130
131 for (; buf < BufferQueue::NUM_BUFFER_SLOTS; buf++) {
132 if (buffer == mBufferSlot[buf]->handle) break;
133 }
134 if (buf == BufferQueue::NUM_BUFFER_SLOTS) {
135 MC_LOGE("%s: Can't find buffer to free", __FUNCTION__);
136 return BAD_VALUE;
137 }
138
139 err = mBufferQueue->releaseBuffer(buf, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR,
140 Fence::NO_FENCE);
141 if (err == BufferQueue::STALE_BUFFER_SLOT) {
142 freeBufferLocked(buf);
143 } else if (err != OK) {
144 MC_LOGE("%s: Unable to release graphic buffer %d to queue", __FUNCTION__,
145 buf);
146 return err;
147 }
Eino-Ville Talvala78822d72012-07-18 17:52:18 -0700148 mCurrentLockedBuffers--;
Eino-Ville Talvala228a5382012-08-13 12:16:06 -0700149 MC_LOGV("freeBuffer: %d buffers in use", mCurrentLockedBuffers);
Eino-Ville Talvala78822d72012-07-18 17:52:18 -0700150
151 return OK;
152}
153
154void MediaConsumer::setFrameAvailableListener(
155 const sp<FrameAvailableListener>& listener) {
156 MC_LOGV("setFrameAvailableListener");
157 Mutex::Autolock lock(mMutex);
158 mFrameAvailableListener = listener;
159}
160
161
162void MediaConsumer::onFrameAvailable() {
163 MC_LOGV("onFrameAvailable");
164 sp<FrameAvailableListener> listener;
165 { // scope for the lock
166 Mutex::Autolock _l(mMutex);
167 listener = mFrameAvailableListener;
168 }
169
170 if (listener != NULL) {
171 MC_LOGV("actually calling onFrameAvailable");
172 listener->onFrameAvailable();
173 }
174}
175
176void MediaConsumer::onBuffersReleased() {
177 MC_LOGV("onBuffersReleased");
178
179 Mutex::Autolock lock(mMutex);
180
181 uint32_t mask = 0;
182 mBufferQueue->getReleasedBuffers(&mask);
183 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
184 if (mask & (1 << i)) {
185 freeBufferLocked(i);
186 }
187 }
188
189}
190
191status_t MediaConsumer::freeBufferLocked(int buf) {
192 status_t err = OK;
193
194 mBufferSlot[buf] = NULL;
195 return err;
196}
197
198} // namespace android