1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * Copyright (C) 2008 HTC Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #ifndef ANDROID_HARDWARE_CAMERA_H 19 #define ANDROID_HARDWARE_CAMERA_H 20 21 #include <utils/Timers.h> 22 #include <ui/ICameraClient.h> 23 24 namespace android { 25 26 /* 27 * A set of bit masks for specifying how the received preview frames are 28 * handled before the previewCallback() call. 29 * 30 * The least significant 3 bits of an "int" value are used for this purpose: 31 * 32 * ..... 0 0 0 33 * ^ ^ ^ 34 * | | |---------> determine whether the callback is enabled or not 35 * | |-----------> determine whether the callback is one-shot or not 36 * |-------------> determine whether the frame is copied out or not 37 * 38 * WARNING: 39 * When a frame is sent directly without copying, it is the frame receiver's 40 * responsiblity to make sure that the frame data won't get corrupted by 41 * subsequent preview frames filled by the camera. This flag is recommended 42 * only when copying out data brings significant performance price and the 43 * handling/processing of the received frame data is always faster than 44 * the preview frame rate so that data corruption won't occur. 45 * 46 * For instance, 47 * 1. 0x00 disables the callback. In this case, copy out and one shot bits 48 * are ignored. 49 * 2. 0x01 enables a callback without copying out the received frames. A 50 * typical use case is the Camcorder application to avoid making costly 51 * frame copies. 52 * 3. 0x05 is enabling a callback with frame copied out repeatedly. A typical 53 * use case is the Camera application. 54 * 4. 0x07 is enabling a callback with frame copied out only once. A typical use 55 * case is the Barcode scanner application. 56 */ 57 #define FRAME_CALLBACK_FLAG_ENABLE_MASK 0x01 58 #define FRAME_CALLBACK_FLAG_ONE_SHOT_MASK 0x02 59 #define FRAME_CALLBACK_FLAG_COPY_OUT_MASK 0x04 60 61 // Typical use cases 62 #define FRAME_CALLBACK_FLAG_NOOP 0x00 63 #define FRAME_CALLBACK_FLAG_CAMCORDER 0x01 64 #define FRAME_CALLBACK_FLAG_CAMERA 0x05 65 #define FRAME_CALLBACK_FLAG_BARCODE_SCANNER 0x07 66 67 // msgType in notifyCallback and dataCallback functions 68 enum { 69 CAMERA_MSG_ERROR = 0x001, 70 CAMERA_MSG_SHUTTER = 0x002, 71 CAMERA_MSG_FOCUS = 0x004, 72 CAMERA_MSG_ZOOM = 0x008, 73 CAMERA_MSG_PREVIEW_FRAME = 0x010, 74 CAMERA_MSG_VIDEO_FRAME = 0x020, 75 CAMERA_MSG_POSTVIEW_FRAME = 0x040, 76 CAMERA_MSG_RAW_IMAGE = 0x080, 77 CAMERA_MSG_COMPRESSED_IMAGE = 0x100, 78 CAMERA_MSG_ALL_MSGS = 0x1FF 79 }; 80 81 // cmdType in sendCommand functions 82 enum { 83 CAMERA_CMD_START_SMOOTH_ZOOM = 1, 84 CAMERA_CMD_STOP_SMOOTH_ZOOM = 2, 85 }; 86 87 // camera fatal errors 88 enum { 89 CAMERA_ERROR_UKNOWN = 1, 90 CAMERA_ERROR_SERVER_DIED = 100 91 }; 92 93 class ICameraService; 94 class ICamera; 95 class Surface; 96 class Mutex; 97 class String8; 98 99 // ref-counted object for callbacks 100 class CameraListener: virtual public RefBase 101 { 102 public: 103 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2) = 0; 104 virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr) = 0; 105 virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) = 0; 106 }; 107 108 class Camera : public BnCameraClient, public IBinder::DeathRecipient 109 { 110 public: 111 // construct a camera client from an existing remote 112 static sp<Camera> create(const sp<ICamera>& camera); 113 static sp<Camera> connect(); 114 ~Camera(); 115 void init(); 116 117 status_t reconnect(); 118 void disconnect(); 119 status_t lock(); 120 status_t unlock(); 121 getStatus()122 status_t getStatus() { return mStatus; } 123 124 // pass the buffered ISurface to the camera service 125 status_t setPreviewDisplay(const sp<Surface>& surface); 126 status_t setPreviewDisplay(const sp<ISurface>& surface); 127 128 // start preview mode, must call setPreviewDisplay first 129 status_t startPreview(); 130 131 // stop preview mode 132 void stopPreview(); 133 134 // get preview state 135 bool previewEnabled(); 136 137 // start recording mode, must call setPreviewDisplay first 138 status_t startRecording(); 139 140 // stop recording mode 141 void stopRecording(); 142 143 // get recording state 144 bool recordingEnabled(); 145 146 // release a recording frame 147 void releaseRecordingFrame(const sp<IMemory>& mem); 148 149 // autoFocus - status returned from callback 150 status_t autoFocus(); 151 152 // cancel auto focus 153 status_t cancelAutoFocus(); 154 155 // take a picture - picture returned from callback 156 status_t takePicture(); 157 158 // set preview/capture parameters - key/value pairs 159 status_t setParameters(const String8& params); 160 161 // get preview/capture parameters - key/value pairs 162 String8 getParameters() const; 163 164 // send command to camera driver 165 status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2); 166 167 void setListener(const sp<CameraListener>& listener); 168 void setPreviewCallbackFlags(int preview_callback_flag); 169 170 // ICameraClient interface 171 virtual void notifyCallback(int32_t msgType, int32_t ext, int32_t ext2); 172 virtual void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr); 173 virtual void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr); 174 175 sp<ICamera> remote(); 176 177 private: 178 Camera(); 179 Camera(const Camera&); 180 Camera& operator=(const Camera); 181 virtual void binderDied(const wp<IBinder>& who); 182 183 class DeathNotifier: public IBinder::DeathRecipient 184 { 185 public: DeathNotifier()186 DeathNotifier() { 187 } 188 189 virtual void binderDied(const wp<IBinder>& who); 190 }; 191 192 static sp<DeathNotifier> mDeathNotifier; 193 194 // helper function to obtain camera service handle 195 static const sp<ICameraService>& getCameraService(); 196 197 sp<ICamera> mCamera; 198 status_t mStatus; 199 200 sp<CameraListener> mListener; 201 202 friend class DeathNotifier; 203 204 static Mutex mLock; 205 static sp<ICameraService> mCameraService; 206 207 }; 208 209 }; // namespace android 210 211 #endif 212 213