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 setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer); 41 virtual void setPreviewCallbackFlag(int flag); 42 virtual status_t setPreviewCallbackTarget( 43 const sp<IGraphicBufferProducer>& callbackProducer); 44 virtual status_t startPreview(); 45 virtual void stopPreview(); 46 virtual bool previewEnabled(); 47 virtual status_t storeMetaDataInBuffers(bool enabled); 48 virtual status_t startRecording(); 49 virtual void stopRecording(); 50 virtual bool recordingEnabled(); 51 virtual void releaseRecordingFrame(const sp<IMemory>& mem); 52 virtual status_t autoFocus(); 53 virtual status_t cancelAutoFocus(); 54 virtual status_t takePicture(int msgType); 55 virtual status_t setParameters(const String8& params); 56 virtual String8 getParameters() const; 57 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2); 58 59 // Interface used by CameraService 60 CameraClient(const sp<CameraService>& cameraService, 61 const sp<ICameraClient>& cameraClient, 62 const String16& clientPackageName, 63 int cameraId, 64 int cameraFacing, 65 int clientPid, 66 int clientUid, 67 int servicePid, 68 bool legacyMode = false); 69 ~CameraClient(); 70 71 status_t initialize(CameraModule *module); 72 73 virtual status_t dump(int fd, const Vector<String16>& args); 74 75 virtual status_t dumpClient(int fd, const Vector<String16>& args); 76 77 private: 78 79 // check whether the calling process matches mClientPid. 80 status_t checkPid() const; 81 status_t checkPidAndHardware() const; // also check mHardware != 0 82 83 // these are internal functions used to set up preview buffers 84 status_t registerPreviewBuffers(); 85 86 // camera operation mode 87 enum camera_mode { 88 CAMERA_PREVIEW_MODE = 0, // frame automatically released 89 CAMERA_RECORDING_MODE = 1, // frame has to be explicitly released by releaseRecordingFrame() 90 }; 91 // these are internal functions used for preview/recording 92 status_t startCameraMode(camera_mode mode); 93 status_t startPreviewMode(); 94 status_t startRecordingMode(); 95 96 // internal function used by sendCommand to enable/disable shutter sound. 97 status_t enableShutterSound(bool enable); 98 99 // these are static callback functions 100 static void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user); 101 static void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, 102 camera_frame_metadata_t *metadata, void* user); 103 static void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr, void* user); 104 // handlers for messages 105 void handleShutter(void); 106 void handlePreviewData(int32_t msgType, const sp<IMemory>& mem, 107 camera_frame_metadata_t *metadata); 108 void handlePostview(const sp<IMemory>& mem); 109 void handleRawPicture(const sp<IMemory>& mem); 110 void handleCompressedPicture(const sp<IMemory>& mem); 111 void handleGenericNotify(int32_t msgType, int32_t ext1, int32_t ext2); 112 void handleGenericData(int32_t msgType, const sp<IMemory>& dataPtr, 113 camera_frame_metadata_t *metadata); 114 void handleGenericDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr); 115 116 void copyFrameAndPostCopiedFrame( 117 int32_t msgType, 118 const sp<ICameraClient>& client, 119 const sp<IMemoryHeap>& heap, 120 size_t offset, size_t size, 121 camera_frame_metadata_t *metadata); 122 123 int getOrientation(int orientation, bool mirror); 124 125 status_t setPreviewWindow( 126 const sp<IBinder>& binder, 127 const sp<ANativeWindow>& window); 128 129 130 // these are initialized in the constructor. 131 sp<CameraHardwareInterface> mHardware; // cleared after disconnect() 132 int mPreviewCallbackFlag; 133 int mOrientation; // Current display orientation 134 bool mPlayShutterSound; 135 bool mLegacyMode; // camera2 api legacy mode? 136 137 // Ensures atomicity among the public methods 138 mutable Mutex mLock; 139 // This is a binder of Surface or Surface. 140 sp<IBinder> mSurface; 141 sp<ANativeWindow> mPreviewWindow; 142 143 // If the user want us to return a copy of the preview frame (instead 144 // of the original one), we allocate mPreviewBuffer and reuse it if possible. 145 sp<MemoryHeapBase> mPreviewBuffer; 146 147 // Debugging information 148 CameraParameters mLatestSetParameters; 149 150 // We need to avoid the deadlock when the incoming command thread and 151 // the CameraHardwareInterface callback thread both want to grab mLock. 152 // An extra flag is used to tell the callback thread that it should stop 153 // trying to deliver the callback messages if the client is not 154 // interested in it anymore. For example, if the client is calling 155 // stopPreview(), the preview frame messages do not need to be delivered 156 // anymore. 157 158 // This function takes the same parameter as the enableMsgType() and 159 // disableMsgType() functions in CameraHardwareInterface. 160 void enableMsgType(int32_t msgType); 161 void disableMsgType(int32_t msgType); 162 volatile int32_t mMsgEnabled; 163 164 // This function keeps trying to grab mLock, or give up if the message 165 // is found to be disabled. It returns true if mLock is grabbed. 166 bool lockIfMessageWanted(int32_t msgType); 167 }; 168 169 } 170 171 #endif 172