1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ANDROID_SERVERS_CAMERA_CAMERACLIENT_H 18 #define ANDROID_SERVERS_CAMERA_CAMERACLIENT_H 19 20 #include "CameraService.h" 21 22 namespace android { 23 24 class MemoryHeapBase; 25 class CameraHardwareInterface; 26 27 /** 28 * Interface between android.hardware.Camera API and Camera HAL device for version 29 * CAMERA_DEVICE_API_VERSION_1_0. 30 */ 31 32 class CameraClient : public CameraService::Client 33 { 34 public: 35 // ICamera interface (see ICamera for details) 36 virtual void disconnect(); 37 virtual status_t connect(const sp<ICameraClient>& client); 38 virtual status_t lock(); 39 virtual status_t unlock(); 40 virtual status_t setPreviewDisplay(const sp<Surface>& surface); 41 virtual status_t setPreviewTexture(const sp<IGraphicBufferProducer>& bufferProducer); 42 virtual void setPreviewCallbackFlag(int flag); 43 virtual status_t startPreview(); 44 virtual void stopPreview(); 45 virtual bool previewEnabled(); 46 virtual status_t storeMetaDataInBuffers(bool enabled); 47 virtual status_t startRecording(); 48 virtual void stopRecording(); 49 virtual bool recordingEnabled(); 50 virtual void releaseRecordingFrame(const sp<IMemory>& mem); 51 virtual status_t autoFocus(); 52 virtual status_t cancelAutoFocus(); 53 virtual status_t takePicture(int msgType); 54 virtual status_t setParameters(const String8& params); 55 virtual String8 getParameters() const; 56 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2); 57 58 // Interface used by CameraService 59 CameraClient(const sp<CameraService>& cameraService, 60 const sp<ICameraClient>& cameraClient, 61 const String16& clientPackageName, 62 int cameraId, 63 int cameraFacing, 64 int clientPid, 65 int clientUid, 66 int servicePid); 67 ~CameraClient(); 68 69 status_t initialize(camera_module_t *module); 70 71 status_t dump(int fd, const Vector<String16>& args); 72 73 private: 74 75 // check whether the calling process matches mClientPid. 76 status_t checkPid() const; 77 status_t checkPidAndHardware() const; // also check mHardware != 0 78 79 // these are internal functions used to set up preview buffers 80 status_t registerPreviewBuffers(); 81 82 // camera operation mode 83 enum camera_mode { 84 CAMERA_PREVIEW_MODE = 0, // frame automatically released 85 CAMERA_RECORDING_MODE = 1, // frame has to be explicitly released by releaseRecordingFrame() 86 }; 87 // these are internal functions used for preview/recording 88 status_t startCameraMode(camera_mode mode); 89 status_t startPreviewMode(); 90 status_t startRecordingMode(); 91 92 // internal function used by sendCommand to enable/disable shutter sound. 93 status_t enableShutterSound(bool enable); 94 95 // these are static callback functions 96 static void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user); 97 static void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, 98 camera_frame_metadata_t *metadata, void* user); 99 static void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr, void* user); 100 // handlers for messages 101 void handleShutter(void); 102 void handlePreviewData(int32_t msgType, const sp<IMemory>& mem, 103 camera_frame_metadata_t *metadata); 104 void handlePostview(const sp<IMemory>& mem); 105 void handleRawPicture(const sp<IMemory>& mem); 106 void handleCompressedPicture(const sp<IMemory>& mem); 107 void handleGenericNotify(int32_t msgType, int32_t ext1, int32_t ext2); 108 void handleGenericData(int32_t msgType, const sp<IMemory>& dataPtr, 109 camera_frame_metadata_t *metadata); 110 void handleGenericDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr); 111 112 void copyFrameAndPostCopiedFrame( 113 int32_t msgType, 114 const sp<ICameraClient>& client, 115 const sp<IMemoryHeap>& heap, 116 size_t offset, size_t size, 117 camera_frame_metadata_t *metadata); 118 119 int getOrientation(int orientation, bool mirror); 120 121 status_t setPreviewWindow( 122 const sp<IBinder>& binder, 123 const sp<ANativeWindow>& window); 124 125 126 // these are initialized in the constructor. 127 sp<CameraHardwareInterface> mHardware; // cleared after disconnect() 128 int mPreviewCallbackFlag; 129 int mOrientation; // Current display orientation 130 bool mPlayShutterSound; 131 132 // Ensures atomicity among the public methods 133 mutable Mutex mLock; 134 // This is a binder of Surface or Surface. 135 sp<IBinder> mSurface; 136 sp<ANativeWindow> mPreviewWindow; 137 138 // If the user want us to return a copy of the preview frame (instead 139 // of the original one), we allocate mPreviewBuffer and reuse it if possible. 140 sp<MemoryHeapBase> mPreviewBuffer; 141 142 // We need to avoid the deadlock when the incoming command thread and 143 // the CameraHardwareInterface callback thread both want to grab mLock. 144 // An extra flag is used to tell the callback thread that it should stop 145 // trying to deliver the callback messages if the client is not 146 // interested in it anymore. For example, if the client is calling 147 // stopPreview(), the preview frame messages do not need to be delivered 148 // anymore. 149 150 // This function takes the same parameter as the enableMsgType() and 151 // disableMsgType() functions in CameraHardwareInterface. 152 void enableMsgType(int32_t msgType); 153 void disableMsgType(int32_t msgType); 154 volatile int32_t mMsgEnabled; 155 156 // This function keeps trying to grab mLock, or give up if the message 157 // is found to be disabled. It returns true if mLock is grabbed. 158 bool lockIfMessageWanted(int32_t msgType); 159 }; 160 161 } 162 163 #endif 164