15e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala/*
25e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala * Copyright (C) 2012 The Android Open Source Project
35e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala *
45e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala * Licensed under the Apache License, Version 2.0 (the "License");
55e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala * you may not use this file except in compliance with the License.
65e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala * You may obtain a copy of the License at
75e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala *
85e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala *      http://www.apache.org/licenses/LICENSE-2.0
95e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala *
105e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala * Unless required by applicable law or agreed to in writing, software
115e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala * distributed under the License is distributed on an "AS IS" BASIS,
125e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala * See the License for the specific language governing permissions and
145e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala * limitations under the License.
155e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala */
165e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
175e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala#define LOG_TAG "CameraClient"
185e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala//#define LOG_NDEBUG 0
195e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
205e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala#include <cutils/properties.h>
215e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala#include <gui/SurfaceTextureClient.h>
225e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala#include <gui/Surface.h>
235e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
245e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala#include "CameraClient.h"
255e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala#include "CameraHardwareInterface.h"
265e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala#include "CameraService.h"
275e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
285e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalanamespace android {
295e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
305e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
315e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
325e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
335e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatic int getCallingPid() {
345e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return IPCThreadState::self()->getCallingPid();
355e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
365e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
375e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatic int getCallingUid() {
385e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return IPCThreadState::self()->getCallingUid();
395e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
405e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
415e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville TalvalaCameraClient::CameraClient(const sp<CameraService>& cameraService,
425e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        const sp<ICameraClient>& cameraClient,
43ecf17e82505fdb60d59e00b6dd59036df93de655Igor Murashkin        int cameraId, int cameraFacing, int clientPid, int servicePid):
445e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        Client(cameraService, cameraClient,
45ecf17e82505fdb60d59e00b6dd59036df93de655Igor Murashkin                cameraId, cameraFacing, clientPid, servicePid)
465e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala{
475e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int callingPid = getCallingPid();
485e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("CameraClient::CameraClient E (pid %d, id %d)", callingPid, cameraId);
495e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
50f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    mHardware = NULL;
515e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mMsgEnabled = 0;
525e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mSurface = 0;
535e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mPreviewWindow = 0;
545e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mDestructionStarted = false;
555e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
565e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // Callback is disabled by default
575e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
585e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
595e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mPlayShutterSound = true;
605e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("CameraClient::CameraClient X (pid %d, id %d)", callingPid, cameraId);
615e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
625e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
63f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvalastatus_t CameraClient::initialize(camera_module_t *module) {
64f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    int callingPid = getCallingPid();
65f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    LOG1("CameraClient::initialize E (pid %d, id %d)", callingPid, mCameraId);
66f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala
67f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    char camera_device_name[10];
68f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    status_t res;
69f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    snprintf(camera_device_name, sizeof(camera_device_name), "%d", mCameraId);
70f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala
71f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    mHardware = new CameraHardwareInterface(camera_device_name);
72f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    res = mHardware->initialize(&module->common);
73f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    if (res != OK) {
74f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala        ALOGE("%s: Camera %d: unable to initialize device: %s (%d)",
75f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala                __FUNCTION__, mCameraId, strerror(-res), res);
7644f120fa169cb3131ec853c638ea9c88a9b33b97Igor Murashkin        mHardware.clear();
77f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala        return NO_INIT;
78f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    }
79f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala
80f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    mHardware->setCallbacks(notifyCallback,
81f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala            dataCallback,
82f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala            dataCallbackTimestamp,
83f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala            (void *)mCameraId);
84f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala
85f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    // Enable zoom, error, focus, and metadata messages by default
86f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS |
87f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala                  CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE);
88f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala
89f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    LOG1("CameraClient::initialize X (pid %d, id %d)", callingPid, mCameraId);
90f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    return OK;
91f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala}
92f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala
93f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala
945e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// tear down the client
955e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville TalvalaCameraClient::~CameraClient() {
965e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // this lock should never be NULL
975e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex* lock = mCameraService->getClientLockById(mCameraId);
985e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    lock->lock();
995e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mDestructionStarted = true;
1005e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // client will not be accessed from callback. should unlock to prevent dead-lock in disconnect
1015e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    lock->unlock();
1025e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int callingPid = getCallingPid();
1035e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("CameraClient::~CameraClient E (pid %d, this %p)", callingPid, this);
1045e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1055e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    disconnect();
1065e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("CameraClient::~CameraClient X (pid %d, this %p)", callingPid, this);
1075e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
1085e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1095e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::dump(int fd, const Vector<String16>& args) {
1105e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    const size_t SIZE = 256;
1115e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    char buffer[SIZE];
1125e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1135e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    size_t len = snprintf(buffer, SIZE, "Client[%d] (%p) PID: %d\n",
1145e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            mCameraId,
1155e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            getCameraClient()->asBinder().get(),
1165e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            mClientPid);
1175e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    len = (len > SIZE - 1) ? SIZE - 1 : len;
1185e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    write(fd, buffer, len);
1195e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return mHardware->dump(fd, args);
1205e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
1215e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1225e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// ----------------------------------------------------------------------------
1235e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1245e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::checkPid() const {
1255e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int callingPid = getCallingPid();
126c03792041b9dd6f7f54abd6c82bd6c755a336cd8Eino-Ville Talvala    if (callingPid == mClientPid) return NO_ERROR;
1275e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1285e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    ALOGW("attempt to use a locked camera from a different process"
1295e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala         " (old pid %d, new pid %d)", mClientPid, callingPid);
1305e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return EBUSY;
1315e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
1325e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1335e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::checkPidAndHardware() const {
1345e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = checkPid();
1355e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result != NO_ERROR) return result;
1365e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mHardware == 0) {
1375e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        ALOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
1385e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return INVALID_OPERATION;
1395e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
1405e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return NO_ERROR;
1415e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
1425e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1435e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::lock() {
1445e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int callingPid = getCallingPid();
1455e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("lock (pid %d)", callingPid);
1465e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
1475e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1485e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // lock camera to this client if the the camera is unlocked
1495e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mClientPid == 0) {
1505e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mClientPid = callingPid;
1515e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return NO_ERROR;
1525e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
1535e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1545e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
1555e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return checkPid();
1565e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
1575e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1585e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::unlock() {
1595e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int callingPid = getCallingPid();
1605e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("unlock (pid %d)", callingPid);
1615e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
1625e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1635e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // allow anyone to use camera (after they lock the camera)
1645e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = checkPid();
1655e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result == NO_ERROR) {
1665e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        if (mHardware->recordingEnabled()) {
1675e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            ALOGE("Not allowed to unlock camera during recording.");
1685e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            return INVALID_OPERATION;
1695e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        }
1705e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mClientPid = 0;
1715e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        LOG1("clear mCameraClient (pid %d)", callingPid);
1725e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        // we need to remove the reference to ICameraClient so that when the app
1735e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        // goes away, the reference count goes to 0.
1745e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mCameraClient.clear();
1755e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
1765e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return result;
1775e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
1785e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1795e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// connect a new client to the camera
1805e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::connect(const sp<ICameraClient>& client) {
1815e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int callingPid = getCallingPid();
1825e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("connect E (pid %d)", callingPid);
1835e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
1845e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1855e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mClientPid != 0 && checkPid() != NO_ERROR) {
1865e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
1875e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                mClientPid, callingPid);
1885e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return EBUSY;
1895e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
1905e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1915e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mCameraClient != 0 && (client->asBinder() == mCameraClient->asBinder())) {
1925e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        LOG1("Connect to the same client");
1935e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return NO_ERROR;
1945e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
1955e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1965e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
1975e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mClientPid = callingPid;
1985e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mCameraClient = client;
1995e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2005e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("connect X (pid %d)", callingPid);
2015e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return NO_ERROR;
2025e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
2035e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2045e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatic void disconnectWindow(const sp<ANativeWindow>& window) {
2055e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (window != 0) {
2065e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        status_t result = native_window_api_disconnect(window.get(),
2075e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                NATIVE_WINDOW_API_CAMERA);
2085e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        if (result != NO_ERROR) {
2095e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
2105e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                    result);
2115e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        }
2125e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
2135e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
2145e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2155e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::disconnect() {
2165e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int callingPid = getCallingPid();
2175e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("disconnect E (pid %d)", callingPid);
2185e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
2195e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
220c03792041b9dd6f7f54abd6c82bd6c755a336cd8Eino-Ville Talvala    // Allow both client and the media server to disconnect at all times
221c03792041b9dd6f7f54abd6c82bd6c755a336cd8Eino-Ville Talvala    if (callingPid != mClientPid && callingPid != mServicePid) {
2225e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        ALOGW("different client - don't disconnect");
2235e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return;
2245e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
2255e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2265e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mClientPid <= 0) {
2275e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
2285e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return;
2295e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
2305e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2315e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // Make sure disconnect() is done once and once only, whether it is called
2325e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // from the user directly, or called by the destructor.
2335e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mHardware == 0) return;
2345e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2355e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("hardware teardown");
2365e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // Before destroying mHardware, we must make sure it's in the
2375e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // idle state.
2385e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // Turn off all messages.
2395e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    disableMsgType(CAMERA_MSG_ALL_MSGS);
2405e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mHardware->stopPreview();
2415e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mHardware->cancelPicture();
2425e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // Release the hardware resources.
2435e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mHardware->release();
2445e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2455e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // Release the held ANativeWindow resources.
2465e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mPreviewWindow != 0) {
2475e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        disconnectWindow(mPreviewWindow);
2485e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mPreviewWindow = 0;
2495e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mHardware->setPreviewWindow(mPreviewWindow);
2505e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
2515e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mHardware.clear();
2525e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2535e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    CameraService::Client::disconnect();
2545e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2555e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("disconnect X (pid %d)", callingPid);
2565e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
2575e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2585e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// ----------------------------------------------------------------------------
2595e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2605e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::setPreviewWindow(const sp<IBinder>& binder,
2615e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        const sp<ANativeWindow>& window) {
2625e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
2635e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = checkPidAndHardware();
2645e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result != NO_ERROR) return result;
2655e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2665e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // return if no change in surface.
2675e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (binder == mSurface) {
2685e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return NO_ERROR;
2695e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
2705e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2715e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (window != 0) {
2725e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
2735e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        if (result != NO_ERROR) {
2745e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
2755e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                    result);
2765e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            return result;
2775e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        }
2785e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
2795e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2805e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // If preview has been already started, register preview buffers now.
2815e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mHardware->previewEnabled()) {
2825e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        if (window != 0) {
2835e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            native_window_set_scaling_mode(window.get(),
2845e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                    NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2855e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            native_window_set_buffers_transform(window.get(), mOrientation);
2865e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            result = mHardware->setPreviewWindow(window);
2875e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        }
2885e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
2895e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2905e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result == NO_ERROR) {
2915e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        // Everything has succeeded.  Disconnect the old window and remember the
2925e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        // new window.
2935e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        disconnectWindow(mPreviewWindow);
2945e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mSurface = binder;
2955e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mPreviewWindow = window;
2965e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    } else {
2975e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        // Something went wrong after we connected to the new window, so
2985e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        // disconnect here.
2995e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        disconnectWindow(window);
3005e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
3015e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3025e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return result;
3035e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
3045e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3055e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// set the Surface that the preview will use
3065e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::setPreviewDisplay(const sp<Surface>& surface) {
3075e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid());
3085e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3095e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    sp<IBinder> binder(surface != 0 ? surface->asBinder() : 0);
3105e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    sp<ANativeWindow> window(surface);
3115e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return setPreviewWindow(binder, window);
3125e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
3135e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3145e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// set the SurfaceTexture that the preview will use
3155e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::setPreviewTexture(
3165e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        const sp<ISurfaceTexture>& surfaceTexture) {
3175e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("setPreviewTexture(%p) (pid %d)", surfaceTexture.get(),
3185e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            getCallingPid());
3195e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3205e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    sp<IBinder> binder;
3215e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    sp<ANativeWindow> window;
3225e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (surfaceTexture != 0) {
3235e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        binder = surfaceTexture->asBinder();
3245e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        window = new SurfaceTextureClient(surfaceTexture);
3255e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
3265e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return setPreviewWindow(binder, window);
3275e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
3285e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3295e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// set the preview callback flag to affect how the received frames from
3305e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// preview are handled.
3315e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::setPreviewCallbackFlag(int callback_flag) {
3325e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
3335e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
3345e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (checkPidAndHardware() != NO_ERROR) return;
3355e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3365e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mPreviewCallbackFlag = callback_flag;
3375e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
3385e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
3395e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    } else {
3405e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
3415e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
3425e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
3435e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3445e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// start preview mode
3455e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::startPreview() {
3465e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("startPreview (pid %d)", getCallingPid());
3475e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return startCameraMode(CAMERA_PREVIEW_MODE);
3485e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
3495e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3505e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// start recording mode
3515e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::startRecording() {
3525e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("startRecording (pid %d)", getCallingPid());
3535e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return startCameraMode(CAMERA_RECORDING_MODE);
3545e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
3555e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3565e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// start preview or recording
3575e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::startCameraMode(camera_mode mode) {
3585e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("startCameraMode(%d)", mode);
3595e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
3605e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = checkPidAndHardware();
3615e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result != NO_ERROR) return result;
3625e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3635e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    switch(mode) {
3645e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        case CAMERA_PREVIEW_MODE:
3655e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            if (mSurface == 0 && mPreviewWindow == 0) {
3665e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                LOG1("mSurface is not set yet.");
3675e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                // still able to start preview in this case.
3685e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            }
3695e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            return startPreviewMode();
3705e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        case CAMERA_RECORDING_MODE:
3715e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            if (mSurface == 0 && mPreviewWindow == 0) {
3725e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
3735e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                return INVALID_OPERATION;
3745e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            }
3755e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            return startRecordingMode();
3765e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        default:
3775e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            return UNKNOWN_ERROR;
3785e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
3795e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
3805e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3815e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::startPreviewMode() {
3825e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("startPreviewMode");
3835e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = NO_ERROR;
3845e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3855e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // if preview has been enabled, nothing needs to be done
3865e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mHardware->previewEnabled()) {
3875e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return NO_ERROR;
3885e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
3895e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3905e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mPreviewWindow != 0) {
3915e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        native_window_set_scaling_mode(mPreviewWindow.get(),
3925e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
3935e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        native_window_set_buffers_transform(mPreviewWindow.get(),
3945e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                mOrientation);
3955e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
3965e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mHardware->setPreviewWindow(mPreviewWindow);
3975e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    result = mHardware->startPreview();
3985e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3995e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return result;
4005e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
4015e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4025e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::startRecordingMode() {
4035e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("startRecordingMode");
4045e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = NO_ERROR;
4055e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4065e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // if recording has been enabled, nothing needs to be done
4075e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mHardware->recordingEnabled()) {
4085e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return NO_ERROR;
4095e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
4105e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4115e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // if preview has not been started, start preview first
4125e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (!mHardware->previewEnabled()) {
4135e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        result = startPreviewMode();
4145e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        if (result != NO_ERROR) {
4155e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            return result;
4165e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        }
4175e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
4185e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4195e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // start recording mode
4205e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    enableMsgType(CAMERA_MSG_VIDEO_FRAME);
4215e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mCameraService->playSound(CameraService::SOUND_RECORDING);
4225e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    result = mHardware->startRecording();
4235e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result != NO_ERROR) {
4245e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        ALOGE("mHardware->startRecording() failed with status %d", result);
4255e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
4265e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return result;
4275e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
4285e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4295e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// stop preview mode
4305e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::stopPreview() {
4315e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("stopPreview (pid %d)", getCallingPid());
4325e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
4335e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (checkPidAndHardware() != NO_ERROR) return;
4345e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4355e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4365e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
4375e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mHardware->stopPreview();
4385e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4395e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mPreviewBuffer.clear();
4405e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
4415e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4425e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// stop recording mode
4435e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::stopRecording() {
4445e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("stopRecording (pid %d)", getCallingPid());
4455e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
4465e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (checkPidAndHardware() != NO_ERROR) return;
4475e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4485e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    disableMsgType(CAMERA_MSG_VIDEO_FRAME);
4495e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mHardware->stopRecording();
45035f859b338c0922094ecfc37fb8157530df6b305Patric Frederiksen    mCameraService->playSound(CameraService::SOUND_RECORDING);
4515e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4525e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mPreviewBuffer.clear();
4535e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
4545e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4555e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// release a recording frame
4565e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::releaseRecordingFrame(const sp<IMemory>& mem) {
4575e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
4585e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (checkPidAndHardware() != NO_ERROR) return;
4595e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mHardware->releaseRecordingFrame(mem);
4605e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
4615e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4625e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::storeMetaDataInBuffers(bool enabled)
4635e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala{
4645e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
4655e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
4665e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (checkPidAndHardware() != NO_ERROR) {
4675e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return UNKNOWN_ERROR;
4685e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
4695e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return mHardware->storeMetaDataInBuffers(enabled);
4705e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
4715e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4725e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalabool CameraClient::previewEnabled() {
4735e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("previewEnabled (pid %d)", getCallingPid());
4745e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4755e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
4765e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (checkPidAndHardware() != NO_ERROR) return false;
4775e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return mHardware->previewEnabled();
4785e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
4795e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4805e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalabool CameraClient::recordingEnabled() {
4815e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("recordingEnabled (pid %d)", getCallingPid());
4825e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4835e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
4845e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (checkPidAndHardware() != NO_ERROR) return false;
4855e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return mHardware->recordingEnabled();
4865e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
4875e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4885e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::autoFocus() {
4895e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("autoFocus (pid %d)", getCallingPid());
4905e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4915e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
4925e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = checkPidAndHardware();
4935e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result != NO_ERROR) return result;
4945e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4955e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return mHardware->autoFocus();
4965e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
4975e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4985e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::cancelAutoFocus() {
4995e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("cancelAutoFocus (pid %d)", getCallingPid());
5005e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5015e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
5025e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = checkPidAndHardware();
5035e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result != NO_ERROR) return result;
5045e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5055e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return mHardware->cancelAutoFocus();
5065e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
5075e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5085e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// take a picture - image is returned in callback
5095e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::takePicture(int msgType) {
5105e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
5115e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5125e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
5135e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = checkPidAndHardware();
5145e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result != NO_ERROR) return result;
5155e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5165e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
5175e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
5185e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
5195e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                " cannot be both enabled");
5205e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return BAD_VALUE;
5215e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
5225e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5235e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // We only accept picture related message types
5245e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // and ignore other types of messages for takePicture().
5255e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int picMsgType = msgType
5265e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                        & (CAMERA_MSG_SHUTTER |
5275e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                           CAMERA_MSG_POSTVIEW_FRAME |
5285e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                           CAMERA_MSG_RAW_IMAGE |
5295e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                           CAMERA_MSG_RAW_IMAGE_NOTIFY |
5305e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                           CAMERA_MSG_COMPRESSED_IMAGE);
5315e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5325e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    enableMsgType(picMsgType);
5335e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5345e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return mHardware->takePicture();
5355e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
5365e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5375e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// set preview/capture parameters - key/value pairs
5385e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::setParameters(const String8& params) {
5395e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
5405e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5415e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
5425e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = checkPidAndHardware();
5435e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result != NO_ERROR) return result;
5445e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5455e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    CameraParameters p(params);
5465e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return mHardware->setParameters(p);
5475e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
5485e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5495e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// get preview/capture parameters - key/value pairs
5505e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville TalvalaString8 CameraClient::getParameters() const {
5515e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
5525e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (checkPidAndHardware() != NO_ERROR) return String8();
5535e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5545e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    String8 params(mHardware->getParameters().flatten());
555