2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Contains implementation of an abstract class EmulatedCameraDevice that defines
19 * functionality expected from an emulated physical camera device:
20 * - Obtaining and setting camera parameters
27 #define LOG_TAG "EmulatedCamera_Device"
28 #include <cutils/log.h>
29 #include <sys/select.h>
31 #include "EmulatedCameraDevice.h"
35 const float GAMMA_CORRECTION = 2.2f;
36 EmulatedCameraDevice::EmulatedCameraDevice(EmulatedCamera* camera_hal)
38 mCurFrameTimestamp(0),
39 mCameraHAL(camera_hal),
41 mExposureCompensation(1.0f),
42 mWhiteBalanceScale(NULL),
43 mSupportedWhiteBalanceScale(),
44 mState(ECDS_CONSTRUCTED)
48 EmulatedCameraDevice::~EmulatedCameraDevice()
50 ALOGV("EmulatedCameraDevice destructor");
51 if (mCurrentFrame != NULL) {
52 delete[] mCurrentFrame;
54 for (size_t i = 0; i < mSupportedWhiteBalanceScale.size(); ++i) {
55 if (mSupportedWhiteBalanceScale.valueAt(i) != NULL) {
56 delete[] mSupportedWhiteBalanceScale.valueAt(i);
61 /****************************************************************************
62 * Emulated camera device public API
63 ***************************************************************************/
65 status_t EmulatedCameraDevice::Initialize()
67 if (isInitialized()) {
68 ALOGW("%s: Emulated camera device is already initialized: mState = %d",
69 __FUNCTION__, mState);
73 /* Instantiate worker thread object. */
74 mWorkerThread = new WorkerThread(this);
75 if (getWorkerThread() == NULL) {
76 ALOGE("%s: Unable to instantiate worker thread object", __FUNCTION__);
80 mState = ECDS_INITIALIZED;
85 status_t EmulatedCameraDevice::startDeliveringFrames(bool one_burst)
87 ALOGV("%s", __FUNCTION__);
90 ALOGE("%s: Device is not started", __FUNCTION__);
94 /* Frames will be delivered from the thread routine. */
95 const status_t res = startWorkerThread(one_burst);
96 ALOGE_IF(res != NO_ERROR, "%s: startWorkerThread failed", __FUNCTION__);
100 status_t EmulatedCameraDevice::stopDeliveringFrames()
102 ALOGV("%s", __FUNCTION__);
105 ALOGW("%s: Device is not started", __FUNCTION__);
109 const status_t res = stopWorkerThread();
110 ALOGE_IF(res != NO_ERROR, "%s: startWorkerThread failed", __FUNCTION__);
114 void EmulatedCameraDevice::setExposureCompensation(const float ev) {
115 ALOGV("%s", __FUNCTION__);
118 ALOGW("%s: Fake camera device is not started.", __FUNCTION__);
121 mExposureCompensation = std::pow(2.0f, ev / GAMMA_CORRECTION);
122 ALOGV("New exposure compensation is %f", mExposureCompensation);
125 void EmulatedCameraDevice::initializeWhiteBalanceModes(const char* mode,
127 const float b_scale) {
128 ALOGV("%s with %s, %f, %f", __FUNCTION__, mode, r_scale, b_scale);
129 float* value = new float[3];
130 value[0] = r_scale; value[1] = 1.0f; value[2] = b_scale;
131 mSupportedWhiteBalanceScale.add(String8(mode), value);
134 void EmulatedCameraDevice::setWhiteBalanceMode(const char* mode) {
135 ALOGV("%s with white balance %s", __FUNCTION__, mode);
137 mSupportedWhiteBalanceScale.valueFor(String8(mode));
140 /* Computes the pixel value after adjusting the white balance to the current
141 * one. The input the y, u, v channel of the pixel and the adjusted value will
142 * be stored in place. The adjustment is done in RGB space.
144 void EmulatedCameraDevice::changeWhiteBalance(uint8_t& y,
147 float r_scale = mWhiteBalanceScale[0];
148 float b_scale = mWhiteBalanceScale[2];
149 int r = static_cast<float>(YUV2R(y, u, v)) / r_scale;
150 int g = YUV2G(y, u, v);
151 int b = static_cast<float>(YUV2B(y, u, v)) / b_scale;
158 status_t EmulatedCameraDevice::getCurrentPreviewFrame(void* buffer)
161 ALOGE("%s: Device is not started", __FUNCTION__);
164 if (mCurrentFrame == NULL || buffer == NULL) {
165 ALOGE("%s: No framebuffer", __FUNCTION__);
169 /* In emulation the framebuffer is never RGB. */
170 switch (mPixelFormat) {
171 case V4L2_PIX_FMT_YVU420:
172 YV12ToRGB32(mCurrentFrame, buffer, mFrameWidth, mFrameHeight);
174 case V4L2_PIX_FMT_YUV420:
175 YU12ToRGB32(mCurrentFrame, buffer, mFrameWidth, mFrameHeight);
177 case V4L2_PIX_FMT_NV21:
178 NV21ToRGB32(mCurrentFrame, buffer, mFrameWidth, mFrameHeight);
180 case V4L2_PIX_FMT_NV12:
181 NV12ToRGB32(mCurrentFrame, buffer, mFrameWidth, mFrameHeight);
185 ALOGE("%s: Unknown pixel format %.4s",
186 __FUNCTION__, reinterpret_cast<const char*>(&mPixelFormat));
191 /****************************************************************************
192 * Emulated camera device private API
193 ***************************************************************************/
195 status_t EmulatedCameraDevice::commonStartDevice(int width,
199 /* Validate pixel format, and calculate framebuffer size at the same time. */
201 case V4L2_PIX_FMT_YVU420:
202 case V4L2_PIX_FMT_YUV420:
203 case V4L2_PIX_FMT_NV21:
204 case V4L2_PIX_FMT_NV12:
205 mFrameBufferSize = (width * height * 12) / 8;
209 ALOGE("%s: Unknown pixel format %.4s",
210 __FUNCTION__, reinterpret_cast<const char*>(&pix_fmt));
214 /* Cache framebuffer info. */
216 mFrameHeight = height;
217 mPixelFormat = pix_fmt;
218 mTotalPixels = width * height;
220 /* Allocate framebuffer. */
221 mCurrentFrame = new uint8_t[mFrameBufferSize];
222 if (mCurrentFrame == NULL) {
223 ALOGE("%s: Unable to allocate framebuffer", __FUNCTION__);
226 ALOGV("%s: Allocated %p %zu bytes for %d pixels in %.4s[%dx%d] frame",
227 __FUNCTION__, mCurrentFrame, mFrameBufferSize, mTotalPixels,
228 reinterpret_cast<const char*>(&mPixelFormat), mFrameWidth, mFrameHeight);
232 void EmulatedCameraDevice::commonStopDevice()
234 mFrameWidth = mFrameHeight = mTotalPixels = 0;
237 if (mCurrentFrame != NULL) {
238 delete[] mCurrentFrame;
239 mCurrentFrame = NULL;
243 /****************************************************************************
244 * Worker thread management.
245 ***************************************************************************/
247 status_t EmulatedCameraDevice::startWorkerThread(bool one_burst)
249 ALOGV("%s", __FUNCTION__);
251 if (!isInitialized()) {
252 ALOGE("%s: Emulated camera device is not initialized", __FUNCTION__);
256 const status_t res = getWorkerThread()->startThread(one_burst);
257 ALOGE_IF(res != NO_ERROR, "%s: Unable to start worker thread", __FUNCTION__);
261 status_t EmulatedCameraDevice::stopWorkerThread()
263 ALOGV("%s", __FUNCTION__);
265 if (!isInitialized()) {
266 ALOGE("%s: Emulated camera device is not initialized", __FUNCTION__);
270 const status_t res = getWorkerThread()->stopThread();
271 ALOGE_IF(res != NO_ERROR, "%s: Unable to stop worker thread", __FUNCTION__);
275 bool EmulatedCameraDevice::inWorkerThread()
277 /* This will end the thread loop, and will terminate the thread. Derived
278 * classes must override this method. */
282 /****************************************************************************
283 * Worker thread implementation.
284 ***************************************************************************/
286 status_t EmulatedCameraDevice::WorkerThread::readyToRun()
288 ALOGV("Starting emulated camera device worker thread...");
290 ALOGW_IF(mThreadControl >= 0 || mControlFD >= 0,
291 "%s: Thread control FDs are opened", __FUNCTION__);
292 /* Create a pair of FDs that would be used to control the thread. */
295 Mutex::Autolock lock(mCameraDevice->mObjectLock);
296 if (pipe(thread_fds) == 0) {
297 mThreadControl = thread_fds[1];
298 mControlFD = thread_fds[0];
299 ALOGV("Emulated device's worker thread has been started.");
302 ALOGE("%s: Unable to create thread control FDs: %d -> %s",
303 __FUNCTION__, errno, strerror(errno));
311 status_t EmulatedCameraDevice::WorkerThread::stopThread()
313 ALOGV("Stopping emulated camera device's worker thread...");
315 status_t res = EINVAL;
317 // Limit the scope of the Autolock
319 // If thread is running and readyToRun() has not finished running,
320 // then wait until it is done.
321 Mutex::Autolock lock(mCameraDevice->mObjectLock);
322 if (isRunning() && (mThreadControl < 0 || mControlFD < 0)) {
323 mSetup.wait(mCameraDevice->mObjectLock);
327 if (mThreadControl >= 0) {
328 /* Send "stop" message to the thread loop. */
329 const ControlMessage msg = THREAD_STOP;
331 TEMP_FAILURE_RETRY(write(mThreadControl, &msg, sizeof(msg)));
332 if (wres == sizeof(msg)) {
333 /* Stop the thread, and wait till it's terminated. */
334 res = requestExitAndWait();
335 if (res == NO_ERROR) {
336 /* Close control FDs. */
337 if (mThreadControl >= 0) {
338 close(mThreadControl);
341 if (mControlFD >= 0) {
345 ALOGV("Emulated camera device's worker thread has been stopped.");
347 ALOGE("%s: requestExitAndWait failed: %d -> %s",
348 __FUNCTION__, res, strerror(-res));
351 ALOGE("%s: Unable to send THREAD_STOP message: %d -> %s",
352 __FUNCTION__, errno, strerror(errno));
353 res = errno ? errno : EINVAL;
356 ALOGE("%s: Thread control FDs are not opened", __FUNCTION__);
362 EmulatedCameraDevice::WorkerThread::SelectRes
363 EmulatedCameraDevice::WorkerThread::Select(int fd, int timeout)
366 struct timeval tv, *tvp = NULL;
368 const int fd_num = (fd >= 0) ? max(fd, mControlFD) + 1 :
371 FD_SET(mControlFD, fds);
376 tv.tv_sec = timeout / 1000000;
377 tv.tv_usec = timeout % 1000000;
380 int res = TEMP_FAILURE_RETRY(select(fd_num, fds, NULL, NULL, tvp));
382 ALOGE("%s: select returned %d and failed: %d -> %s",
383 __FUNCTION__, res, errno, strerror(errno));
385 } else if (res == 0) {
388 } else if (FD_ISSET(mControlFD, fds)) {
389 /* A control event. Lets read the message. */
391 res = TEMP_FAILURE_RETRY(read(mControlFD, &msg, sizeof(msg)));
392 if (res != sizeof(msg)) {
393 ALOGE("%s: Unexpected message size %d, or an error %d -> %s",
394 __FUNCTION__, res, errno, strerror(errno));
397 /* THREAD_STOP is the only message expected here. */
398 if (msg == THREAD_STOP) {
399 ALOGV("%s: THREAD_STOP message is received", __FUNCTION__);
402 ALOGE("Unknown worker thread message %d", msg);
407 ALOGW_IF(fd < 0 || !FD_ISSET(fd, fds), "%s: Undefined 'select' result",
413 }; /* namespace android */