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/Surface.h>
225e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
237b82efe7a376c882f8f938e1c41b8311a8cdda4aEino-Ville Talvala#include "api1/CameraClient.h"
247b82efe7a376c882f8f938e1c41b8311a8cdda4aEino-Ville Talvala#include "device1/CameraHardwareInterface.h"
255e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala#include "CameraService.h"
265e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
275e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalanamespace android {
285e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
295e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
305e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
315e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
325e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatic int getCallingPid() {
335e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return IPCThreadState::self()->getCallingPid();
345e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
355e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
365e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville TalvalaCameraClient::CameraClient(const sp<CameraService>& cameraService,
375e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        const sp<ICameraClient>& cameraClient,
38ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala        const String16& clientPackageName,
39ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala        int cameraId, int cameraFacing,
40ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala        int clientPid, int clientUid,
41a858ea0495c887621a2fd9c0afc13780deccb597Igor Murashkin        int servicePid, bool legacyMode):
42ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala        Client(cameraService, cameraClient, clientPackageName,
43ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala                cameraId, cameraFacing, clientPid, clientUid, servicePid)
445e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala{
455e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int callingPid = getCallingPid();
465e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("CameraClient::CameraClient E (pid %d, id %d)", callingPid, cameraId);
475e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
48f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    mHardware = NULL;
495e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mMsgEnabled = 0;
505e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mSurface = 0;
515e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mPreviewWindow = 0;
525e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mDestructionStarted = false;
535e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
545e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // Callback is disabled by default
555e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
565e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
57a858ea0495c887621a2fd9c0afc13780deccb597Igor Murashkin    mLegacyMode = legacyMode;
585e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mPlayShutterSound = true;
595e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("CameraClient::CameraClient X (pid %d, id %d)", callingPid, cameraId);
605e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
615e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
62f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvalastatus_t CameraClient::initialize(camera_module_t *module) {
63f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    int callingPid = getCallingPid();
64ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala    status_t res;
65ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala
66f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    LOG1("CameraClient::initialize E (pid %d, id %d)", callingPid, mCameraId);
67f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala
68ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala    // Verify ops permissions
69ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala    res = startCameraOps();
70ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala    if (res != OK) {
71ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala        return res;
72ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala    }
73ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala
74f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    char camera_device_name[10];
75f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    snprintf(camera_device_name, sizeof(camera_device_name), "%d", mCameraId);
76f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala
77f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    mHardware = new CameraHardwareInterface(camera_device_name);
78f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    res = mHardware->initialize(&module->common);
79f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    if (res != OK) {
80f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala        ALOGE("%s: Camera %d: unable to initialize device: %s (%d)",
81f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala                __FUNCTION__, mCameraId, strerror(-res), res);
8244f120fa169cb3131ec853c638ea9c88a9b33b97Igor Murashkin        mHardware.clear();
83b10cdadf0fb945e23ca77008d4af76584bd0e39aZhijun He        return res;
84f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    }
85f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala
86f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    mHardware->setCallbacks(notifyCallback,
87f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala            dataCallback,
88f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala            dataCallbackTimestamp,
89377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT            (void *)(uintptr_t)mCameraId);
90f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala
91f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    // Enable zoom, error, focus, and metadata messages by default
92f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS |
93f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala                  CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE);
94f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala
95f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    LOG1("CameraClient::initialize X (pid %d, id %d)", callingPid, mCameraId);
96f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    return OK;
97f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala}
98f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala
99f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala
1005e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// tear down the client
1015e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville TalvalaCameraClient::~CameraClient() {
1025e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // this lock should never be NULL
1035e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex* lock = mCameraService->getClientLockById(mCameraId);
1045e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    lock->lock();
1055e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mDestructionStarted = true;
1065e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // client will not be accessed from callback. should unlock to prevent dead-lock in disconnect
1075e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    lock->unlock();
1085e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int callingPid = getCallingPid();
1095e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("CameraClient::~CameraClient E (pid %d, this %p)", callingPid, this);
1105e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1115e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    disconnect();
1125e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("CameraClient::~CameraClient X (pid %d, this %p)", callingPid, this);
1135e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
1145e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1155e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::dump(int fd, const Vector<String16>& args) {
1165e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    const size_t SIZE = 256;
1175e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    char buffer[SIZE];
1185e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1195e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    size_t len = snprintf(buffer, SIZE, "Client[%d] (%p) PID: %d\n",
1205e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            mCameraId,
12144cfcf00b9008c1c04f4c8277c6c06af039fd976Igor Murashkin            getRemoteCallback()->asBinder().get(),
1225e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            mClientPid);
1235e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    len = (len > SIZE - 1) ? SIZE - 1 : len;
1245e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    write(fd, buffer, len);
125fcf5fea2ac99e6359998078d09fd7e21269dca4aIgor Murashkin
126fcf5fea2ac99e6359998078d09fd7e21269dca4aIgor Murashkin    len = snprintf(buffer, SIZE, "Latest set parameters:\n");
127fcf5fea2ac99e6359998078d09fd7e21269dca4aIgor Murashkin    len = (len > SIZE - 1) ? SIZE - 1 : len;
128fcf5fea2ac99e6359998078d09fd7e21269dca4aIgor Murashkin    write(fd, buffer, len);
129fcf5fea2ac99e6359998078d09fd7e21269dca4aIgor Murashkin
130fcf5fea2ac99e6359998078d09fd7e21269dca4aIgor Murashkin    mLatestSetParameters.dump(fd, args);
131fcf5fea2ac99e6359998078d09fd7e21269dca4aIgor Murashkin
132fcf5fea2ac99e6359998078d09fd7e21269dca4aIgor Murashkin    const char *enddump = "\n\n";
133fcf5fea2ac99e6359998078d09fd7e21269dca4aIgor Murashkin    write(fd, enddump, strlen(enddump));
134fcf5fea2ac99e6359998078d09fd7e21269dca4aIgor Murashkin
1355e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return mHardware->dump(fd, args);
1365e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
1375e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1385e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// ----------------------------------------------------------------------------
1395e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1405e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::checkPid() const {
1415e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int callingPid = getCallingPid();
142c03792041b9dd6f7f54abd6c82bd6c755a336cd8Eino-Ville Talvala    if (callingPid == mClientPid) return NO_ERROR;
1435e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1445e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    ALOGW("attempt to use a locked camera from a different process"
1455e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala         " (old pid %d, new pid %d)", mClientPid, callingPid);
1465e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return EBUSY;
1475e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
1485e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1495e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::checkPidAndHardware() const {
1505e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = checkPid();
1515e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result != NO_ERROR) return result;
1525e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mHardware == 0) {
1535e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        ALOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
1545e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return INVALID_OPERATION;
1555e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
1565e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return NO_ERROR;
1575e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
1585e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1595e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::lock() {
1605e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int callingPid = getCallingPid();
1615e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("lock (pid %d)", callingPid);
1625e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
1635e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1645e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // lock camera to this client if the the camera is unlocked
1655e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mClientPid == 0) {
1665e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mClientPid = callingPid;
1675e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return NO_ERROR;
1685e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
1695e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1705e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
1715e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return checkPid();
1725e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
1735e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1745e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::unlock() {
1755e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int callingPid = getCallingPid();
1765e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("unlock (pid %d)", callingPid);
1775e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
1785e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1795e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // allow anyone to use camera (after they lock the camera)
1805e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = checkPid();
1815e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result == NO_ERROR) {
1825e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        if (mHardware->recordingEnabled()) {
1835e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            ALOGE("Not allowed to unlock camera during recording.");
1845e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            return INVALID_OPERATION;
1855e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        }
1865e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mClientPid = 0;
18744cfcf00b9008c1c04f4c8277c6c06af039fd976Igor Murashkin        LOG1("clear mRemoteCallback (pid %d)", callingPid);
1885e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        // we need to remove the reference to ICameraClient so that when the app
1895e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        // goes away, the reference count goes to 0.
19044cfcf00b9008c1c04f4c8277c6c06af039fd976Igor Murashkin        mRemoteCallback.clear();
1915e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
1925e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return result;
1935e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
1945e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1955e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// connect a new client to the camera
1965e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::connect(const sp<ICameraClient>& client) {
1975e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int callingPid = getCallingPid();
1985e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("connect E (pid %d)", callingPid);
1995e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
2005e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2015e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mClientPid != 0 && checkPid() != NO_ERROR) {
2025e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
2035e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                mClientPid, callingPid);
2045e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return EBUSY;
2055e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
2065e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
20744cfcf00b9008c1c04f4c8277c6c06af039fd976Igor Murashkin    if (mRemoteCallback != 0 &&
20844cfcf00b9008c1c04f4c8277c6c06af039fd976Igor Murashkin        (client->asBinder() == mRemoteCallback->asBinder())) {
2095e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        LOG1("Connect to the same client");
2105e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return NO_ERROR;
2115e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
2125e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2135e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
2145e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mClientPid = callingPid;
21544cfcf00b9008c1c04f4c8277c6c06af039fd976Igor Murashkin    mRemoteCallback = client;
2165e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2175e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("connect X (pid %d)", callingPid);
2185e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return NO_ERROR;
2195e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
2205e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2215e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatic void disconnectWindow(const sp<ANativeWindow>& window) {
2225e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (window != 0) {
2235e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        status_t result = native_window_api_disconnect(window.get(),
2245e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                NATIVE_WINDOW_API_CAMERA);
2255e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        if (result != NO_ERROR) {
2265e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
2275e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                    result);
2285e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        }
2295e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
2305e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
2315e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2325e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::disconnect() {
2335e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int callingPid = getCallingPid();
2345e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("disconnect E (pid %d)", callingPid);
2355e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
2365e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
237c03792041b9dd6f7f54abd6c82bd6c755a336cd8Eino-Ville Talvala    // Allow both client and the media server to disconnect at all times
238c03792041b9dd6f7f54abd6c82bd6c755a336cd8Eino-Ville Talvala    if (callingPid != mClientPid && callingPid != mServicePid) {
2395e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        ALOGW("different client - don't disconnect");
2405e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return;
2415e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
2425e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2435e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mClientPid <= 0) {
2445e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
2455e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return;
2465e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
2475e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2485e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // Make sure disconnect() is done once and once only, whether it is called
2495e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // from the user directly, or called by the destructor.
2505e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mHardware == 0) return;
2515e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2525e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("hardware teardown");
2535e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // Before destroying mHardware, we must make sure it's in the
2545e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // idle state.
2555e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // Turn off all messages.
2565e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    disableMsgType(CAMERA_MSG_ALL_MSGS);
2575e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mHardware->stopPreview();
2585e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mHardware->cancelPicture();
2595e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // Release the hardware resources.
2605e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mHardware->release();
2615e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2625e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // Release the held ANativeWindow resources.
2635e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mPreviewWindow != 0) {
2645e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        disconnectWindow(mPreviewWindow);
2655e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mPreviewWindow = 0;
2665e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mHardware->setPreviewWindow(mPreviewWindow);
2675e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
2685e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mHardware.clear();
2695e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2705e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    CameraService::Client::disconnect();
2715e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2725e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("disconnect X (pid %d)", callingPid);
2735e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
2745e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2755e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// ----------------------------------------------------------------------------
2765e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2775e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::setPreviewWindow(const sp<IBinder>& binder,
2785e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        const sp<ANativeWindow>& window) {
2795e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
2805e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = checkPidAndHardware();
2815e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result != NO_ERROR) return result;
2825e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2835e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // return if no change in surface.
2845e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (binder == mSurface) {
2855e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return NO_ERROR;
2865e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
2875e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2885e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (window != 0) {
2895e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
2905e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        if (result != NO_ERROR) {
2915e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
2925e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                    result);
2935e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            return result;
2945e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        }
2955e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
2965e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
2975e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // If preview has been already started, register preview buffers now.
2985e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mHardware->previewEnabled()) {
2995e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        if (window != 0) {
3005e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            native_window_set_scaling_mode(window.get(),
3015e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                    NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
3025e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            native_window_set_buffers_transform(window.get(), mOrientation);
3035e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            result = mHardware->setPreviewWindow(window);
3045e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        }
3055e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
3065e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3075e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result == NO_ERROR) {
3085e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        // Everything has succeeded.  Disconnect the old window and remember the
3095e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        // new window.
3105e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        disconnectWindow(mPreviewWindow);
3115e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mSurface = binder;
3125e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mPreviewWindow = window;
3135e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    } else {
3145e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        // Something went wrong after we connected to the new window, so
3155e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        // disconnect here.
3165e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        disconnectWindow(window);
3175e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
3185e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3195e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return result;
3205e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
3215e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3221ce7c34e67c2cf58dd88c31f36f4bd62e375f7f0Eino-Ville Talvala// set the buffer consumer that the preview will use
3231ce7c34e67c2cf58dd88c31f36f4bd62e375f7f0Eino-Ville Talvalastatus_t CameraClient::setPreviewTarget(
3248ba01021b573889802e67e029225a96f0dfa471aAndy McFadden        const sp<IGraphicBufferProducer>& bufferProducer) {
3251ce7c34e67c2cf58dd88c31f36f4bd62e375f7f0Eino-Ville Talvala    LOG1("setPreviewTarget(%p) (pid %d)", bufferProducer.get(),
3265e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            getCallingPid());
3275e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3285e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    sp<IBinder> binder;
3295e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    sp<ANativeWindow> window;
3308ba01021b573889802e67e029225a96f0dfa471aAndy McFadden    if (bufferProducer != 0) {
3318ba01021b573889802e67e029225a96f0dfa471aAndy McFadden        binder = bufferProducer->asBinder();
3321ce7c34e67c2cf58dd88c31f36f4bd62e375f7f0Eino-Ville Talvala        // Using controlledByApp flag to ensure that the buffer queue remains in
3331ce7c34e67c2cf58dd88c31f36f4bd62e375f7f0Eino-Ville Talvala        // async mode for the old camera API, where many applications depend
3341ce7c34e67c2cf58dd88c31f36f4bd62e375f7f0Eino-Ville Talvala        // on that behavior.
3351ce7c34e67c2cf58dd88c31f36f4bd62e375f7f0Eino-Ville Talvala        window = new Surface(bufferProducer, /*controlledByApp*/ true);
3365e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
3375e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return setPreviewWindow(binder, window);
3385e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
3395e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3405e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// set the preview callback flag to affect how the received frames from
3415e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// preview are handled.
3425e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::setPreviewCallbackFlag(int callback_flag) {
3435e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
3445e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
3455e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (checkPidAndHardware() != NO_ERROR) return;
3465e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3475e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mPreviewCallbackFlag = callback_flag;
3485e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
3495e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
3505e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    } else {
3515e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
3525e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
3535e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
3545e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3553ee3550a2f529cbf56d87d8503f59a8f45dccf32Eino-Ville Talvalastatus_t CameraClient::setPreviewCallbackTarget(
3563ee3550a2f529cbf56d87d8503f59a8f45dccf32Eino-Ville Talvala        const sp<IGraphicBufferProducer>& callbackProducer) {
3577b82efe7a376c882f8f938e1c41b8311a8cdda4aEino-Ville Talvala    (void)callbackProducer;
3583ee3550a2f529cbf56d87d8503f59a8f45dccf32Eino-Ville Talvala    ALOGE("%s: Unimplemented!", __FUNCTION__);
3593ee3550a2f529cbf56d87d8503f59a8f45dccf32Eino-Ville Talvala    return INVALID_OPERATION;
3603ee3550a2f529cbf56d87d8503f59a8f45dccf32Eino-Ville Talvala}
3613ee3550a2f529cbf56d87d8503f59a8f45dccf32Eino-Ville Talvala
3625e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// start preview mode
3635e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::startPreview() {
3645e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("startPreview (pid %d)", getCallingPid());
3655e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return startCameraMode(CAMERA_PREVIEW_MODE);
3665e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
3675e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3685e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// start recording mode
3695e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::startRecording() {
3705e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("startRecording (pid %d)", getCallingPid());
3715e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return startCameraMode(CAMERA_RECORDING_MODE);
3725e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
3735e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3745e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// start preview or recording
3755e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::startCameraMode(camera_mode mode) {
3765e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("startCameraMode(%d)", mode);
3775e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
3785e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = checkPidAndHardware();
3795e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result != NO_ERROR) return result;
3805e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3815e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    switch(mode) {
3825e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        case CAMERA_PREVIEW_MODE:
3835e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            if (mSurface == 0 && mPreviewWindow == 0) {
3845e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                LOG1("mSurface is not set yet.");
3855e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                // still able to start preview in this case.
3865e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            }
3875e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            return startPreviewMode();
3885e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        case CAMERA_RECORDING_MODE:
3895e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            if (mSurface == 0 && mPreviewWindow == 0) {
3905e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
3915e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                return INVALID_OPERATION;
3925e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            }
3935e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            return startRecordingMode();
3945e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        default:
3955e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            return UNKNOWN_ERROR;
3965e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
3975e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
3985e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
3995e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::startPreviewMode() {
4005e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("startPreviewMode");
4015e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = NO_ERROR;
4025e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4035e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // if preview has been enabled, nothing needs to be done
4045e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mHardware->previewEnabled()) {
4055e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return NO_ERROR;
4065e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
4075e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4085e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mPreviewWindow != 0) {
4095e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        native_window_set_scaling_mode(mPreviewWindow.get(),
4105e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4115e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        native_window_set_buffers_transform(mPreviewWindow.get(),
4125e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                mOrientation);
4135e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
4145e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mHardware->setPreviewWindow(mPreviewWindow);
4155e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    result = mHardware->startPreview();
4165e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4175e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return result;
4185e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
4195e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4205e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::startRecordingMode() {
4215e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("startRecordingMode");
4225e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = NO_ERROR;
4235e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4245e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // if recording has been enabled, nothing needs to be done
4255e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mHardware->recordingEnabled()) {
4265e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return NO_ERROR;
4275e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
4285e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4295e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // if preview has not been started, start preview first
4305e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (!mHardware->previewEnabled()) {
4315e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        result = startPreviewMode();
4325e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        if (result != NO_ERROR) {
4335e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            return result;
4345e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        }
4355e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
4365e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4375e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // start recording mode
4385e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    enableMsgType(CAMERA_MSG_VIDEO_FRAME);
4395e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mCameraService->playSound(CameraService::SOUND_RECORDING);
4405e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    result = mHardware->startRecording();
4415e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result != NO_ERROR) {
4425e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        ALOGE("mHardware->startRecording() failed with status %d", result);
4435e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
4445e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return result;
4455e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
4465e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4475e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// stop preview mode
4485e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::stopPreview() {
4495e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("stopPreview (pid %d)", getCallingPid());
4505e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
4515e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (checkPidAndHardware() != NO_ERROR) return;
4525e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4535e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4545e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
4555e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mHardware->stopPreview();
4565e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4575e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mPreviewBuffer.clear();
4585e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
4595e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4605e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// stop recording mode
4615e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::stopRecording() {
4625e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("stopRecording (pid %d)", getCallingPid());
4635e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
4645e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (checkPidAndHardware() != NO_ERROR) return;
4655e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4665e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    disableMsgType(CAMERA_MSG_VIDEO_FRAME);
4675e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mHardware->stopRecording();
46835f859b338c0922094ecfc37fb8157530df6b305Patric Frederiksen    mCameraService->playSound(CameraService::SOUND_RECORDING);
4695e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4705e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mPreviewBuffer.clear();
4715e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
4725e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4735e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// release a recording frame
4745e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::releaseRecordingFrame(const sp<IMemory>& mem) {
4755e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
4765e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (checkPidAndHardware() != NO_ERROR) return;
4775e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mHardware->releaseRecordingFrame(mem);
4785e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
4795e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4805e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::storeMetaDataInBuffers(bool enabled)
4815e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala{
4825e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
4835e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
4845e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (checkPidAndHardware() != NO_ERROR) {
4855e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return UNKNOWN_ERROR;
4865e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
4875e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return mHardware->storeMetaDataInBuffers(enabled);
4885e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
4895e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4905e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalabool CameraClient::previewEnabled() {
4915e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("previewEnabled (pid %d)", getCallingPid());
4925e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4935e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
4945e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (checkPidAndHardware() != NO_ERROR) return false;
4955e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return mHardware->previewEnabled();
4965e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
4975e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
4985e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalabool CameraClient::recordingEnabled() {
4995e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("recordingEnabled (pid %d)", getCallingPid());
5005e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5015e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
5025e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (checkPidAndHardware() != NO_ERROR) return false;
5035e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return mHardware->recordingEnabled();
5045e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
5055e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5065e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::autoFocus() {
5075e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("autoFocus (pid %d)", getCallingPid());
5085e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5095e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
5105e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = checkPidAndHardware();
5115e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result != NO_ERROR) return result;
5125e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5135e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return mHardware->autoFocus();
5145e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
5155e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5165e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::cancelAutoFocus() {
5175e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("cancelAutoFocus (pid %d)", getCallingPid());
5185e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5195e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
5205e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = checkPidAndHardware();
5215e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result != NO_ERROR) return result;
5225e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5235e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return mHardware->cancelAutoFocus();
5245e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
5255e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5265e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// take a picture - image is returned in callback
5275e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::takePicture(int msgType) {
5285e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
5295e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5305e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
5315e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = checkPidAndHardware();
5325e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result != NO_ERROR) return result;
5335e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5345e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
5355e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
5365e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
5375e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                " cannot be both enabled");
5385e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return BAD_VALUE;
5395e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
5405e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5415e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // We only accept picture related message types
5425e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // and ignore other types of messages for takePicture().
5435e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int picMsgType = msgType
5445e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                        & (CAMERA_MSG_SHUTTER |
5455e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                           CAMERA_MSG_POSTVIEW_FRAME |
5465e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                           CAMERA_MSG_RAW_IMAGE |
5475e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                           CAMERA_MSG_RAW_IMAGE_NOTIFY |
5485e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                           CAMERA_MSG_COMPRESSED_IMAGE);
5495e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5505e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    enableMsgType(picMsgType);
5515e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5525e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return mHardware->takePicture();
5535e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
5545e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5555e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// set preview/capture parameters - key/value pairs
5565e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::setParameters(const String8& params) {
5575e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
5585e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5595e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
5605e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = checkPidAndHardware();
5615e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result != NO_ERROR) return result;
5625e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
563fcf5fea2ac99e6359998078d09fd7e21269dca4aIgor Murashkin    mLatestSetParameters = CameraParameters(params);
5645e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    CameraParameters p(params);
5655e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return mHardware->setParameters(p);
5665e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
5675e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5685e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// get preview/capture parameters - key/value pairs
5695e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville TalvalaString8 CameraClient::getParameters() const {
5705e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
571ebe865b175b3f1b2a9212cea7b008937c919d8f3Igor Murashkin    // The camera service can unconditionally get the parameters at all times
572ebe865b175b3f1b2a9212cea7b008937c919d8f3Igor Murashkin    if (getCallingPid() != mServicePid && checkPidAndHardware() != NO_ERROR) return String8();
5735e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5745e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    String8 params(mHardware->getParameters().flatten());
5755e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
5765e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return params;
5775e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
5785e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5795e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// enable shutter sound
5805e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::enableShutterSound(bool enable) {
5815e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("enableShutterSound (pid %d)", getCallingPid());
5825e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5835e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = checkPidAndHardware();
5845e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result != NO_ERROR) return result;
5855e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5865e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (enable) {
5875e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mPlayShutterSound = true;
5885e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return OK;
589a858ea0495c887621a2fd9c0afc13780deccb597Igor Murashkin    }
590a858ea0495c887621a2fd9c0afc13780deccb597Igor Murashkin
591a858ea0495c887621a2fd9c0afc13780deccb597Igor Murashkin    // the camera2 api legacy mode can unconditionally disable the shutter sound
592a858ea0495c887621a2fd9c0afc13780deccb597Igor Murashkin    if (mLegacyMode) {
593a858ea0495c887621a2fd9c0afc13780deccb597Igor Murashkin        ALOGV("%s: Disable shutter sound in legacy mode", __FUNCTION__);
594a858ea0495c887621a2fd9c0afc13780deccb597Igor Murashkin        mPlayShutterSound = false;
595a858ea0495c887621a2fd9c0afc13780deccb597Igor Murashkin        return OK;
5965e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
5975e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
5985e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // Disabling shutter sound may not be allowed. In that case only
5995e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // allow the mediaserver process to disable the sound.
6005e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    char value[PROPERTY_VALUE_MAX];
6015e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    property_get("ro.camera.sound.forced", value, "0");
6025e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (strcmp(value, "0") != 0) {
6035e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        // Disabling shutter sound is not allowed. Deny if the current
6045e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        // process is not mediaserver.
6055e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        if (getCallingPid() != getpid()) {
6065e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
6075e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            return PERMISSION_DENIED;
6085e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        }
6095e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
6105e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
6115e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mPlayShutterSound = false;
6125e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return OK;
6135e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
6145e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
6155e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalastatus_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
6165e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG1("sendCommand (pid %d)", getCallingPid());
6175e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int orientation;
6185e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock lock(mLock);
6195e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t result = checkPidAndHardware();
6205e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (result != NO_ERROR) return result;
6215e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
6225e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
6235e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        // Mirror the preview if the camera is front-facing.
6245e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
6255e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        if (orientation == -1) return BAD_VALUE;
6265e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
6275e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        if (mOrientation != orientation) {
6285e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            mOrientation = orientation;
6295e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            if (mPreviewWindow != 0) {
6305e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                native_window_set_buffers_transform(mPreviewWindow.get(),
6315e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                        mOrientation);
6325e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            }
6335e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        }
6345e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return OK;
6355e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
6365e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        switch (arg1) {
6375e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            case 0:
638c5268e825d4a513ad857b7944a9f85dc478c1dc6Eino-Ville Talvala                return enableShutterSound(false);
6395e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            case 1:
640c5268e825d4a513ad857b7944a9f85dc478c1dc6Eino-Ville Talvala                return enableShutterSound(true);
6415e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            default:
6425e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                return BAD_VALUE;
6435e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        }
6445e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return OK;
6455e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
6465e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mCameraService->playSound(CameraService::SOUND_RECORDING);
647983cf231ab2d176a14595cdae46ff1b0c239af47James Dong    } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) {
648983cf231ab2d176a14595cdae46ff1b0c239af47James Dong        // Silently ignore this command
649983cf231ab2d176a14595cdae46ff1b0c239af47James Dong        return INVALID_OPERATION;
6505e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    } else if (cmd == CAMERA_CMD_PING) {
6515e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        // If mHardware is 0, checkPidAndHardware will return error.
6525e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return OK;
6535e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
6545e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
6555e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return mHardware->sendCommand(cmd, arg1, arg2);
6565e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
6575e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
6585e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// ----------------------------------------------------------------------------
6595e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
6605e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::enableMsgType(int32_t msgType) {
6615e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    android_atomic_or(msgType, &mMsgEnabled);
6625e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mHardware->enableMsgType(msgType);
6635e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
6645e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
6655e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::disableMsgType(int32_t msgType) {
6665e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    android_atomic_and(~msgType, &mMsgEnabled);
6675e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mHardware->disableMsgType(msgType);
6685e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
6695e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
6705e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala#define CHECK_MESSAGE_INTERVAL 10 // 10ms
6715e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalabool CameraClient::lockIfMessageWanted(int32_t msgType) {
6725e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int sleepCount = 0;
6735e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    while (mMsgEnabled & msgType) {
6745e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        if (mLock.tryLock() == NO_ERROR) {
6755e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            if (sleepCount > 0) {
6765e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                LOG1("lockIfMessageWanted(%d): waited for %d ms",
6775e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                    msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
6785e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            }
6795e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            return true;
6805e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        }
6815e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        if (sleepCount++ == 0) {
6825e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
6835e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        }
6845e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        usleep(CHECK_MESSAGE_INTERVAL * 1000);
6855e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
6865e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
6875e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return false;
6885e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
6895e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
6905e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// Callback messages can be dispatched to internal handlers or pass to our
6915e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// client's callback functions, depending on the message type.
6925e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala//
6935e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// notifyCallback:
6945e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala//      CAMERA_MSG_SHUTTER              handleShutter
6955e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala//      (others)                        c->notifyCallback
6965e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// dataCallback:
6975e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala//      CAMERA_MSG_PREVIEW_FRAME        handlePreviewData
6985e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala//      CAMERA_MSG_POSTVIEW_FRAME       handlePostview
6995e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala//      CAMERA_MSG_RAW_IMAGE            handleRawPicture
7005e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala//      CAMERA_MSG_COMPRESSED_IMAGE     handleCompressedPicture
7015e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala//      (others)                        c->dataCallback
7025e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// dataCallbackTimestamp
7035e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala//      (others)                        c->dataCallbackTimestamp
7045e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala//
7055e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// NOTE: the *Callback functions grab mLock of the client before passing
7065e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// control to handle* functions. So the handle* functions must release the
7075e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// lock before calling the ICameraClient's callbacks, so those callbacks can
7085e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// invoke methods in the Client class again (For example, the preview frame
7095e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// callback may want to releaseRecordingFrame). The handle* functions must
7105e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// release the lock after all accesses to member variables, so it must be
7115e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// handled very carefully.
7125e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
7135e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::notifyCallback(int32_t msgType, int32_t ext1,
7145e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        int32_t ext2, void* user) {
7155e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG2("notifyCallback(%d)", msgType);
7165e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
7175e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex* lock = getClientLockFromCookie(user);
7185e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (lock == NULL) return;
7195e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock alock(*lock);
7205e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
7215e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    CameraClient* client =
7225e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            static_cast<CameraClient*>(getClientFromCookie(user));
7235e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (client == NULL) return;
7245e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
7255e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (!client->lockIfMessageWanted(msgType)) return;
7265e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
7275e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    switch (msgType) {
7285e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        case CAMERA_MSG_SHUTTER:
7295e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            // ext1 is the dimension of the yuv picture.
7305e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            client->handleShutter();
7315e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            break;
7325e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        default:
7335e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            client->handleGenericNotify(msgType, ext1, ext2);
7345e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            break;
7355e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
7365e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
7375e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
7385e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::dataCallback(int32_t msgType,
7395e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
7405e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG2("dataCallback(%d)", msgType);
7415e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
7425e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex* lock = getClientLockFromCookie(user);
7435e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (lock == NULL) return;
7445e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock alock(*lock);
7455e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
7465e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    CameraClient* client =
7475e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            static_cast<CameraClient*>(getClientFromCookie(user));
7485e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (client == NULL) return;
7495e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
7505e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (!client->lockIfMessageWanted(msgType)) return;
7515e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (dataPtr == 0 && metadata == NULL) {
7525e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        ALOGE("Null data returned in data callback");
7535e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
7545e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return;
7555e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
7565e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
7575e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
7585e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        case CAMERA_MSG_PREVIEW_FRAME:
7595e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            client->handlePreviewData(msgType, dataPtr, metadata);
7605e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            break;
7615e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        case CAMERA_MSG_POSTVIEW_FRAME:
7625e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            client->handlePostview(dataPtr);
7635e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            break;
7645e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        case CAMERA_MSG_RAW_IMAGE:
7655e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            client->handleRawPicture(dataPtr);
7665e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            break;
7675e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        case CAMERA_MSG_COMPRESSED_IMAGE:
7685e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            client->handleCompressedPicture(dataPtr);
7695e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            break;
7705e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        default:
7715e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            client->handleGenericData(msgType, dataPtr, metadata);
7725e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            break;
7735e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
7745e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
7755e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
7765e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::dataCallbackTimestamp(nsecs_t timestamp,
7775e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
7785e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG2("dataCallbackTimestamp(%d)", msgType);
7795e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
7805e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex* lock = getClientLockFromCookie(user);
7815e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (lock == NULL) return;
7825e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    Mutex::Autolock alock(*lock);
7835e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
7845e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    CameraClient* client =
7855e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            static_cast<CameraClient*>(getClientFromCookie(user));
7865e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (client == NULL) return;
7875e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
7885e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (!client->lockIfMessageWanted(msgType)) return;
7895e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
7905e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (dataPtr == 0) {
7915e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        ALOGE("Null data returned in data with timestamp callback");
7925e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
7935e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return;
7945e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
7955e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
7965e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
7975e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
7985e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
7995e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// snapshot taken callback
8005e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::handleShutter(void) {
8015e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mPlayShutterSound) {
8025e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mCameraService->playSound(CameraService::SOUND_SHUTTER);
8035e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
8045e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
80544cfcf00b9008c1c04f4c8277c6c06af039fd976Igor Murashkin    sp<ICameraClient> c = mRemoteCallback;
8065e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (c != 0) {
8075e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mLock.unlock();
8085e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
8095e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
8105e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
8115e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    disableMsgType(CAMERA_MSG_SHUTTER);
8125e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
8135e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mLock.unlock();
8145e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
8155e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
8165e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// preview callback - frame buffer update
8175e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::handlePreviewData(int32_t msgType,
8185e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                                              const sp<IMemory>& mem,
8195e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                                              camera_frame_metadata_t *metadata) {
8205e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    ssize_t offset;
8215e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    size_t size;
8225e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
8235e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
8245e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // local copy of the callback flags
8255e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int flags = mPreviewCallbackFlag;
8265e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
8275e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // is callback enabled?
8285e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
8295e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        // If the enable bit is off, the copy-out and one-shot bits are ignored
8305e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        LOG2("frame callback is disabled");
8315e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mLock.unlock();
8325e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return;
8335e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
8345e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
8355e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // hold a strong pointer to the client
83644cfcf00b9008c1c04f4c8277c6c06af039fd976Igor Murashkin    sp<ICameraClient> c = mRemoteCallback;
8375e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
8385e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // clear callback flags if no client or one-shot mode
8395e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
8405e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        LOG2("Disable preview callback");
8415e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
8425e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                                  CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
8435e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala                                  CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
8445e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
8455e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
8465e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
8475e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (c != 0) {
8485e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        // Is the received frame copied out or not?
8495e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
8505e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            LOG2("frame is copied");
8515e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
8525e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        } else {
8535e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            LOG2("frame is forwarded");
8545e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            mLock.unlock();
8555e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            c->dataCallback(msgType, mem, metadata);
8565e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        }
8575e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    } else {
8585e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mLock.unlock();
8595e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
8605e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
8615e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
8625e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// picture callback - postview image ready
8635e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::handlePostview(const sp<IMemory>& mem) {
8645e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
8655e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
86644cfcf00b9008c1c04f4c8277c6c06af039fd976Igor Murashkin    sp<ICameraClient> c = mRemoteCallback;
8675e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mLock.unlock();
8685e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (c != 0) {
8695e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
8705e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
8715e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
8725e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
8735e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// picture callback - raw image ready
8745e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::handleRawPicture(const sp<IMemory>& mem) {
8755e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    disableMsgType(CAMERA_MSG_RAW_IMAGE);
8765e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
8775e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    ssize_t offset;
8785e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    size_t size;
8795e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
8805e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
88144cfcf00b9008c1c04f4c8277c6c06af039fd976Igor Murashkin    sp<ICameraClient> c = mRemoteCallback;
8825e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mLock.unlock();
8835e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (c != 0) {
8845e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
8855e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
8865e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
8875e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
8885e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala// picture callback - compressed picture ready
8895e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::handleCompressedPicture(const sp<IMemory>& mem) {
8905e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
8915e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
89244cfcf00b9008c1c04f4c8277c6c06af039fd976Igor Murashkin    sp<ICameraClient> c = mRemoteCallback;
8935e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mLock.unlock();
8945e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (c != 0) {
8955e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
8965e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
8975e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
8985e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
8995e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
9005e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::handleGenericNotify(int32_t msgType,
9015e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int32_t ext1, int32_t ext2) {
90244cfcf00b9008c1c04f4c8277c6c06af039fd976Igor Murashkin    sp<ICameraClient> c = mRemoteCallback;
9035e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mLock.unlock();
9045e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (c != 0) {
9055e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        c->notifyCallback(msgType, ext1, ext2);
9065e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
9075e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
9085e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
9095e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::handleGenericData(int32_t msgType,
9105e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
91144cfcf00b9008c1c04f4c8277c6c06af039fd976Igor Murashkin    sp<ICameraClient> c = mRemoteCallback;
9125e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mLock.unlock();
9135e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (c != 0) {
9145e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        c->dataCallback(msgType, dataPtr, metadata);
9155e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
9165e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
9175e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
9185e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::handleGenericDataTimestamp(nsecs_t timestamp,
9195e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int32_t msgType, const sp<IMemory>& dataPtr) {
92044cfcf00b9008c1c04f4c8277c6c06af039fd976Igor Murashkin    sp<ICameraClient> c = mRemoteCallback;
9215e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mLock.unlock();
9225e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (c != 0) {
9235e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
9245e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
9255e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
9265e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
9275e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalavoid CameraClient::copyFrameAndPostCopiedFrame(
9285e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        int32_t msgType, const sp<ICameraClient>& client,
9295e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        const sp<IMemoryHeap>& heap, size_t offset, size_t size,
9305e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        camera_frame_metadata_t *metadata) {
9315e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    LOG2("copyFrameAndPostCopiedFrame");
9325e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // It is necessary to copy out of pmem before sending this to
9335e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // the callback. For efficiency, reuse the same MemoryHeapBase
9345e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // provided it's big enough. Don't allocate the memory or
9355e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // perform the copy if there's no callback.
9365e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // hold the preview lock while we grab a reference to the preview buffer
9375e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    sp<MemoryHeapBase> previewBuffer;
9385e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
9395e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mPreviewBuffer == 0) {
9405e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
9415e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    } else if (size > mPreviewBuffer->virtualSize()) {
9425e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mPreviewBuffer.clear();
9435e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
9445e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
9455e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (mPreviewBuffer == 0) {
9465e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        ALOGE("failed to allocate space for preview buffer");
9475e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mLock.unlock();
9485e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return;
9495e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
9505e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    previewBuffer = mPreviewBuffer;
9515e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
95265e01f763571b779fbc759e6f96d0a195b94c6daRuben Brunk    void* previewBufferBase = previewBuffer->base();
95365e01f763571b779fbc759e6f96d0a195b94c6daRuben Brunk    void* heapBase = heap->base();
95465e01f763571b779fbc759e6f96d0a195b94c6daRuben Brunk
95565e01f763571b779fbc759e6f96d0a195b94c6daRuben Brunk    if (heapBase == MAP_FAILED) {
95665e01f763571b779fbc759e6f96d0a195b94c6daRuben Brunk        ALOGE("%s: Failed to mmap heap for preview frame.", __FUNCTION__);
95765e01f763571b779fbc759e6f96d0a195b94c6daRuben Brunk        mLock.unlock();
95865e01f763571b779fbc759e6f96d0a195b94c6daRuben Brunk        return;
95965e01f763571b779fbc759e6f96d0a195b94c6daRuben Brunk    } else if (previewBufferBase == MAP_FAILED) {
96065e01f763571b779fbc759e6f96d0a195b94c6daRuben Brunk        ALOGE("%s: Failed to mmap preview buffer for preview frame.", __FUNCTION__);
96165e01f763571b779fbc759e6f96d0a195b94c6daRuben Brunk        mLock.unlock();
96265e01f763571b779fbc759e6f96d0a195b94c6daRuben Brunk        return;
96365e01f763571b779fbc759e6f96d0a195b94c6daRuben Brunk    }
96465e01f763571b779fbc759e6f96d0a195b94c6daRuben Brunk
96565e01f763571b779fbc759e6f96d0a195b94c6daRuben Brunk    memcpy(previewBufferBase, (uint8_t *) heapBase + offset, size);
9665e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
9675e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
9685e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (frame == 0) {
9695e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        ALOGE("failed to allocate space for frame callback");
9705e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        mLock.unlock();
9715e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        return;
9725e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
9735e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
9745e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mLock.unlock();
9755e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    client->dataCallback(msgType, frame, metadata);
9765e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
9775e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
9785e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalaint CameraClient::getOrientation(int degrees, bool mirror) {
9795e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    if (!mirror) {
9805e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        if (degrees == 0) return 0;
9815e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
9825e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
9835e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
9845e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    } else {  // Do mirror (horizontal flip)
9855e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        if (degrees == 0) {           // FLIP_H and ROT_0
9865e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            return HAL_TRANSFORM_FLIP_H;
9875e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        } else if (degrees == 90) {   // FLIP_H and ROT_90
9885e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
9895e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        } else if (degrees == 180) {  // FLIP_H and ROT_180
9905e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            return HAL_TRANSFORM_FLIP_V;
9915e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        } else if (degrees == 270) {  // FLIP_H and ROT_270
9925e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
9935e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        }
9945e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    }
9955e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
9965e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    return -1;
9975e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
9985e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
9995e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}; // namespace android
1000