The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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_HARDWARE_CAMERA_H |
| 18 | #define ANDROID_HARDWARE_CAMERA_H |
| 19 | |
Dave Sparks | 59c1a93 | 2009-07-08 15:56:53 -0700 | [diff] [blame] | 20 | #include <utils/Timers.h> |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 21 | #include <camera/ICameraClient.h> |
Jamie Gennis | ff2dc46 | 2010-12-20 11:51:31 -0800 | [diff] [blame] | 22 | #include <gui/ISurfaceTexture.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 67 | // msgType in notifyCallback and dataCallback functions |
Dave Sparks | d6289b1 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 68 | enum { |
James Dong | e00cab7 | 2011-02-17 16:38:06 -0800 | [diff] [blame] | 69 | CAMERA_MSG_ERROR = 0x0001, |
| 70 | CAMERA_MSG_SHUTTER = 0x0002, |
| 71 | CAMERA_MSG_FOCUS = 0x0004, |
| 72 | CAMERA_MSG_ZOOM = 0x0008, |
| 73 | CAMERA_MSG_PREVIEW_FRAME = 0x0010, |
| 74 | CAMERA_MSG_VIDEO_FRAME = 0x0020, |
| 75 | CAMERA_MSG_POSTVIEW_FRAME = 0x0040, |
| 76 | CAMERA_MSG_RAW_IMAGE = 0x0080, |
| 77 | CAMERA_MSG_COMPRESSED_IMAGE = 0x0100, |
| 78 | CAMERA_MSG_RAW_IMAGE_NOTIFY = 0x0200, |
| 79 | CAMERA_MSG_ALL_MSGS = 0xFFFF |
Dave Sparks | d6289b1 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
Wu-cheng Li | 36f68b8 | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 82 | // cmdType in sendCommand functions |
| 83 | enum { |
| 84 | CAMERA_CMD_START_SMOOTH_ZOOM = 1, |
| 85 | CAMERA_CMD_STOP_SMOOTH_ZOOM = 2, |
Wu-cheng Li | d55f7e5 | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 86 | // Set the clockwise rotation of preview display (setPreviewDisplay) in |
| 87 | // degrees. This affects the preview frames and the picture displayed after |
| 88 | // snapshot. This method is useful for portrait mode applications. Note that |
| 89 | // preview display of front-facing cameras is flipped horizontally before |
| 90 | // the rotation, that is, the image is reflected along the central vertical |
| 91 | // axis of the camera sensor. So the users can see themselves as looking |
| 92 | // into a mirror. |
| 93 | // |
| 94 | // This does not affect the order of byte array of CAMERA_MSG_PREVIEW_FRAME, |
| 95 | // CAMERA_MSG_VIDEO_FRAME, CAMERA_MSG_POSTVIEW_FRAME, CAMERA_MSG_RAW_IMAGE, |
| 96 | // or CAMERA_MSG_COMPRESSED_IMAGE. This is not allowed to be set during |
| 97 | // preview. |
Chih-Chung Chang | d1d7706 | 2010-01-22 17:49:48 -0800 | [diff] [blame] | 98 | CAMERA_CMD_SET_DISPLAY_ORIENTATION = 3, |
Nipun Kwatra | 088146a | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 99 | |
| 100 | // cmdType to disable/enable shutter sound. |
| 101 | // In sendCommand passing arg1 = 0 will disable, |
| 102 | // while passing arg1 = 1 will enable the shutter sound. |
| 103 | CAMERA_CMD_ENABLE_SHUTTER_SOUND = 4, |
Nipun Kwatra | 4e94c30 | 2010-09-14 16:49:08 -0700 | [diff] [blame] | 104 | |
| 105 | // cmdType to play recording sound. |
| 106 | CAMERA_CMD_PLAY_RECORDING_SOUND = 5, |
Wu-cheng Li | 36f68b8 | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
James Dong | a1b653d | 2009-07-02 10:04:20 -0700 | [diff] [blame] | 109 | // camera fatal errors |
| 110 | enum { |
James Dong | 853f6fd | 2011-01-04 18:32:16 -0800 | [diff] [blame] | 111 | CAMERA_ERROR_UNKNOWN = 1, |
James Dong | a1b653d | 2009-07-02 10:04:20 -0700 | [diff] [blame] | 112 | CAMERA_ERROR_SERVER_DIED = 100 |
| 113 | }; |
| 114 | |
Chih-Chung Chang | b8bb78f | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 115 | enum { |
Wu-cheng Li | 99a3f3e | 2010-11-19 15:56:16 +0800 | [diff] [blame] | 116 | CAMERA_FACING_BACK = 0, /* The facing of the camera is opposite to that of the screen. */ |
| 117 | CAMERA_FACING_FRONT = 1 /* The facing of the camera is the same as that of the screen. */ |
Chih-Chung Chang | b8bb78f | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | struct CameraInfo { |
| 121 | |
| 122 | /** |
| 123 | * The direction that the camera faces to. It should be |
| 124 | * CAMERA_FACING_BACK or CAMERA_FACING_FRONT. |
| 125 | */ |
| 126 | int facing; |
| 127 | |
| 128 | /** |
| 129 | * The orientation of the camera image. The value is the angle that the |
| 130 | * camera image needs to be rotated clockwise so it shows correctly on |
| 131 | * the display in its natural orientation. It should be 0, 90, 180, or 270. |
| 132 | * |
Wu-cheng Li | 99a3f3e | 2010-11-19 15:56:16 +0800 | [diff] [blame] | 133 | * For example, suppose a device has a naturally tall screen. The |
| 134 | * back-facing camera sensor is mounted in landscape. You are looking at |
| 135 | * the screen. If the top side of the camera sensor is aligned with the |
| 136 | * right edge of the screen in natural orientation, the value should be |
| 137 | * 90. If the top side of a front-facing camera sensor is aligned with |
| 138 | * the right of the screen, the value should be 270. |
Chih-Chung Chang | b8bb78f | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 139 | */ |
| 140 | int orientation; |
| 141 | }; |
| 142 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | class ICameraService; |
| 144 | class ICamera; |
| 145 | class Surface; |
| 146 | class Mutex; |
| 147 | class String8; |
| 148 | |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 149 | // ref-counted object for callbacks |
| 150 | class CameraListener: virtual public RefBase |
| 151 | { |
| 152 | public: |
| 153 | virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2) = 0; |
| 154 | virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr) = 0; |
Dave Sparks | 59c1a93 | 2009-07-08 15:56:53 -0700 | [diff] [blame] | 155 | virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) = 0; |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | class Camera : public BnCameraClient, public IBinder::DeathRecipient |
| 159 | { |
| 160 | public: |
| 161 | // construct a camera client from an existing remote |
James Dong | 2adc2db | 2009-04-23 14:07:23 -0700 | [diff] [blame] | 162 | static sp<Camera> create(const sp<ICamera>& camera); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 163 | static int32_t getNumberOfCameras(); |
Chih-Chung Chang | b8bb78f | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 164 | static status_t getCameraInfo(int cameraId, |
| 165 | struct CameraInfo* cameraInfo); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 166 | static sp<Camera> connect(int cameraId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | ~Camera(); |
| 168 | void init(); |
| 169 | |
| 170 | status_t reconnect(); |
| 171 | void disconnect(); |
| 172 | status_t lock(); |
| 173 | status_t unlock(); |
| 174 | |
| 175 | status_t getStatus() { return mStatus; } |
| 176 | |
Jamie Gennis | 85cfdd0 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 177 | // pass the buffered Surface to the camera service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | status_t setPreviewDisplay(const sp<Surface>& surface); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | |
Jamie Gennis | ff2dc46 | 2010-12-20 11:51:31 -0800 | [diff] [blame] | 180 | // pass the buffered ISurfaceTexture to the camera service |
| 181 | status_t setPreviewTexture(const sp<ISurfaceTexture>& surfaceTexture); |
| 182 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | // start preview mode, must call setPreviewDisplay first |
| 184 | status_t startPreview(); |
| 185 | |
| 186 | // stop preview mode |
| 187 | void stopPreview(); |
| 188 | |
| 189 | // get preview state |
| 190 | bool previewEnabled(); |
| 191 | |
| 192 | // start recording mode, must call setPreviewDisplay first |
| 193 | status_t startRecording(); |
| 194 | |
| 195 | // stop recording mode |
| 196 | void stopRecording(); |
| 197 | |
| 198 | // get recording state |
| 199 | bool recordingEnabled(); |
| 200 | |
| 201 | // release a recording frame |
| 202 | void releaseRecordingFrame(const sp<IMemory>& mem); |
| 203 | |
| 204 | // autoFocus - status returned from callback |
| 205 | status_t autoFocus(); |
| 206 | |
Chih-Chung Chang | 244f8c2 | 2009-09-15 14:51:56 +0800 | [diff] [blame] | 207 | // cancel auto focus |
| 208 | status_t cancelAutoFocus(); |
| 209 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | // take a picture - picture returned from callback |
James Dong | e00cab7 | 2011-02-17 16:38:06 -0800 | [diff] [blame] | 211 | status_t takePicture(int msgType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | |
| 213 | // set preview/capture parameters - key/value pairs |
| 214 | status_t setParameters(const String8& params); |
| 215 | |
| 216 | // get preview/capture parameters - key/value pairs |
| 217 | String8 getParameters() const; |
| 218 | |
Wu-cheng Li | 36f68b8 | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 219 | // send command to camera driver |
| 220 | status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2); |
| 221 | |
James Dong | 3831185 | 2010-10-18 20:42:51 -0700 | [diff] [blame] | 222 | // return the total number of available video buffers. |
| 223 | int32_t getNumberOfVideoBuffers() const; |
| 224 | |
| 225 | // return the individual video buffer corresponding to the given index. |
| 226 | sp<IMemory> getVideoBuffer(int32_t index) const; |
| 227 | |
| 228 | // tell camera hal to store meta data or real YUV in video buffers. |
| 229 | status_t storeMetaDataInBuffers(bool enabled); |
| 230 | |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 231 | void setListener(const sp<CameraListener>& listener); |
| 232 | void setPreviewCallbackFlags(int preview_callback_flag); |
| 233 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | // ICameraClient interface |
Dave Sparks | 2a04aef | 2009-05-07 12:25:25 -0700 | [diff] [blame] | 235 | virtual void notifyCallback(int32_t msgType, int32_t ext, int32_t ext2); |
Dave Sparks | d6289b1 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 236 | virtual void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr); |
Dave Sparks | 59c1a93 | 2009-07-08 15:56:53 -0700 | [diff] [blame] | 237 | virtual void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | |
| 239 | sp<ICamera> remote(); |
| 240 | |
| 241 | private: |
| 242 | Camera(); |
Dave Sparks | 6f0602e | 2009-06-24 10:42:53 -0700 | [diff] [blame] | 243 | Camera(const Camera&); |
| 244 | Camera& operator=(const Camera); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | virtual void binderDied(const wp<IBinder>& who); |
| 246 | |
| 247 | class DeathNotifier: public IBinder::DeathRecipient |
| 248 | { |
| 249 | public: |
| 250 | DeathNotifier() { |
| 251 | } |
| 252 | |
| 253 | virtual void binderDied(const wp<IBinder>& who); |
| 254 | }; |
| 255 | |
| 256 | static sp<DeathNotifier> mDeathNotifier; |
| 257 | |
| 258 | // helper function to obtain camera service handle |
| 259 | static const sp<ICameraService>& getCameraService(); |
| 260 | |
| 261 | sp<ICamera> mCamera; |
| 262 | status_t mStatus; |
| 263 | |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 264 | sp<CameraListener> mListener; |
| 265 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | friend class DeathNotifier; |
| 267 | |
| 268 | static Mutex mLock; |
| 269 | static sp<ICameraService> mCameraService; |
| 270 | |
| 271 | }; |
| 272 | |
| 273 | }; // namespace android |
| 274 | |
| 275 | #endif |