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#ifndef ANDROID_SERVERS_CAMERA_CAMERACLIENT_H
185e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala#define ANDROID_SERVERS_CAMERA_CAMERACLIENT_H
195e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
205e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala#include "CameraService.h"
215e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
225e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalanamespace android {
235e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
245e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalaclass MemoryHeapBase;
255e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalaclass CameraHardwareInterface;
265e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
275e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalaclass CameraClient : public CameraService::Client
285e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala{
295e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalapublic:
305e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // ICamera interface (see ICamera for details)
315e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual void            disconnect();
325e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual status_t        connect(const sp<ICameraClient>& client);
335e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual status_t        lock();
345e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual status_t        unlock();
355e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual status_t        setPreviewDisplay(const sp<Surface>& surface);
365e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual status_t        setPreviewTexture(const sp<ISurfaceTexture>& surfaceTexture);
375e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual void            setPreviewCallbackFlag(int flag);
385e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual status_t        startPreview();
395e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual void            stopPreview();
405e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual bool            previewEnabled();
415e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual status_t        storeMetaDataInBuffers(bool enabled);
425e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual status_t        startRecording();
435e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual void            stopRecording();
445e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual bool            recordingEnabled();
455e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual void            releaseRecordingFrame(const sp<IMemory>& mem);
465e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual status_t        autoFocus();
475e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual status_t        cancelAutoFocus();
485e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual status_t        takePicture(int msgType);
495e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual status_t        setParameters(const String8& params);
505e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual String8         getParameters() const;
515e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    virtual status_t        sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
525e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
535e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // Interface used by CameraService
545e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    CameraClient(const sp<CameraService>& cameraService,
555e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            const sp<ICameraClient>& cameraClient,
565e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            int cameraId,
575e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            int cameraFacing,
58ecf17e82505fdb60d59e00b6dd59036df93de655Igor Murashkin            int clientPid,
59ecf17e82505fdb60d59e00b6dd59036df93de655Igor Murashkin            int servicePid);
605e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    ~CameraClient();
615e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
62f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    status_t initialize(camera_module_t *module);
63f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala
64f69c70ded4316ea3ee504ac779bd024433ed4ef7Eino-Ville Talvala    status_t dump(int fd, const Vector<String16>& args);
655e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
665e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvalaprivate:
675e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
685e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // check whether the calling process matches mClientPid.
695e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t                checkPid() const;
705e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t                checkPidAndHardware() const;  // also check mHardware != 0
715e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
725e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // these are internal functions used to set up preview buffers
735e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t                registerPreviewBuffers();
745e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
755e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // camera operation mode
765e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    enum camera_mode {
775e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        CAMERA_PREVIEW_MODE   = 0,  // frame automatically released
785e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        CAMERA_RECORDING_MODE = 1,  // frame has to be explicitly released by releaseRecordingFrame()
795e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    };
805e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // these are internal functions used for preview/recording
815e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t                startCameraMode(camera_mode mode);
825e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t                startPreviewMode();
835e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t                startRecordingMode();
845e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
855e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // internal function used by sendCommand to enable/disable shutter sound.
865e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t                enableShutterSound(bool enable);
875e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
885e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // these are static callback functions
895e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    static void             notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user);
905e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    static void             dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
915e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            camera_frame_metadata_t *metadata, void* user);
925e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    static void             dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr, void* user);
935e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // handlers for messages
945e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    void                    handleShutter(void);
955e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    void                    handlePreviewData(int32_t msgType, const sp<IMemory>& mem,
965e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            camera_frame_metadata_t *metadata);
975e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    void                    handlePostview(const sp<IMemory>& mem);
985e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    void                    handleRawPicture(const sp<IMemory>& mem);
995e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    void                    handleCompressedPicture(const sp<IMemory>& mem);
1005e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    void                    handleGenericNotify(int32_t msgType, int32_t ext1, int32_t ext2);
1015e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    void                    handleGenericData(int32_t msgType, const sp<IMemory>& dataPtr,
1025e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala            camera_frame_metadata_t *metadata);
1035e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    void                    handleGenericDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
1045e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1055e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    void                    copyFrameAndPostCopiedFrame(
1065e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        int32_t msgType,
1075e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        const sp<ICameraClient>& client,
1085e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        const sp<IMemoryHeap>& heap,
1095e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        size_t offset, size_t size,
1105e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        camera_frame_metadata_t *metadata);
1115e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1125e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int                     getOrientation(int orientation, bool mirror);
1135e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1145e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    status_t                setPreviewWindow(
1155e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        const sp<IBinder>& binder,
1165e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala        const sp<ANativeWindow>& window);
1175e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1185e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1195e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // these are initialized in the constructor.
1205e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    sp<CameraHardwareInterface>     mHardware;       // cleared after disconnect()
1215e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int                             mPreviewCallbackFlag;
1225e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    int                             mOrientation;     // Current display orientation
1235e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    bool                            mPlayShutterSound;
1245e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1255e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // Ensures atomicity among the public methods
1265e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    mutable Mutex                   mLock;
1275e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // This is a binder of Surface or SurfaceTexture.
1285e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    sp<IBinder>                     mSurface;
1295e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    sp<ANativeWindow>               mPreviewWindow;
1305e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1315e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // If the user want us to return a copy of the preview frame (instead
1325e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // of the original one), we allocate mPreviewBuffer and reuse it if possible.
1335e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    sp<MemoryHeapBase>              mPreviewBuffer;
1345e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1355e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // We need to avoid the deadlock when the incoming command thread and
1365e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // the CameraHardwareInterface callback thread both want to grab mLock.
1375e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // An extra flag is used to tell the callback thread that it should stop
1385e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // trying to deliver the callback messages if the client is not
1395e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // interested in it anymore. For example, if the client is calling
1405e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // stopPreview(), the preview frame messages do not need to be delivered
1415e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // anymore.
1425e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1435e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // This function takes the same parameter as the enableMsgType() and
1445e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // disableMsgType() functions in CameraHardwareInterface.
1455e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    void                    enableMsgType(int32_t msgType);
1465e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    void                    disableMsgType(int32_t msgType);
1475e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    volatile int32_t        mMsgEnabled;
1485e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1495e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // This function keeps trying to grab mLock, or give up if the message
1505e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    // is found to be disabled. It returns true if mLock is grabbed.
1515e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala    bool                    lockIfMessageWanted(int32_t msgType);
1525e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala};
1535e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1545e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala}
1555e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala
1565e08d60617fc63c2e41f9069ff89f5c00db2617dEino-Ville Talvala#endif
157