Camera.h revision e09591eff55fdff1868b32c3e046c62f800330fc
1/* 2 * Copyright (C) 2008 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_HARDWARE_CAMERA_H 18#define ANDROID_HARDWARE_CAMERA_H 19 20#include <utils/Timers.h> 21#include <camera/ICameraClient.h> 22 23namespace android { 24 25/* 26 * A set of bit masks for specifying how the received preview frames are 27 * handled before the previewCallback() call. 28 * 29 * The least significant 3 bits of an "int" value are used for this purpose: 30 * 31 * ..... 0 0 0 32 * ^ ^ ^ 33 * | | |---------> determine whether the callback is enabled or not 34 * | |-----------> determine whether the callback is one-shot or not 35 * |-------------> determine whether the frame is copied out or not 36 * 37 * WARNING: 38 * When a frame is sent directly without copying, it is the frame receiver's 39 * responsiblity to make sure that the frame data won't get corrupted by 40 * subsequent preview frames filled by the camera. This flag is recommended 41 * only when copying out data brings significant performance price and the 42 * handling/processing of the received frame data is always faster than 43 * the preview frame rate so that data corruption won't occur. 44 * 45 * For instance, 46 * 1. 0x00 disables the callback. In this case, copy out and one shot bits 47 * are ignored. 48 * 2. 0x01 enables a callback without copying out the received frames. A 49 * typical use case is the Camcorder application to avoid making costly 50 * frame copies. 51 * 3. 0x05 is enabling a callback with frame copied out repeatedly. A typical 52 * use case is the Camera application. 53 * 4. 0x07 is enabling a callback with frame copied out only once. A typical use 54 * case is the Barcode scanner application. 55 */ 56#define FRAME_CALLBACK_FLAG_ENABLE_MASK 0x01 57#define FRAME_CALLBACK_FLAG_ONE_SHOT_MASK 0x02 58#define FRAME_CALLBACK_FLAG_COPY_OUT_MASK 0x04 59 60// Typical use cases 61#define FRAME_CALLBACK_FLAG_NOOP 0x00 62#define FRAME_CALLBACK_FLAG_CAMCORDER 0x01 63#define FRAME_CALLBACK_FLAG_CAMERA 0x05 64#define FRAME_CALLBACK_FLAG_BARCODE_SCANNER 0x07 65 66// msgType in notifyCallback and dataCallback functions 67enum { 68 CAMERA_MSG_ERROR = 0x001, 69 CAMERA_MSG_SHUTTER = 0x002, 70 CAMERA_MSG_FOCUS = 0x004, 71 CAMERA_MSG_ZOOM = 0x008, 72 CAMERA_MSG_PREVIEW_FRAME = 0x010, 73 CAMERA_MSG_VIDEO_FRAME = 0x020, 74 CAMERA_MSG_POSTVIEW_FRAME = 0x040, 75 CAMERA_MSG_RAW_IMAGE = 0x080, 76 CAMERA_MSG_COMPRESSED_IMAGE = 0x100, 77 CAMERA_MSG_ALL_MSGS = 0x1FF 78}; 79 80// cmdType in sendCommand functions 81enum { 82 CAMERA_CMD_START_SMOOTH_ZOOM = 1, 83 CAMERA_CMD_STOP_SMOOTH_ZOOM = 2, 84 // Set the clockwise rotation of preview display (setPreviewDisplay) in 85 // degrees. This affects the preview frames and the picture displayed after 86 // snapshot. This method is useful for portrait mode applications. Note that 87 // preview display of front-facing cameras is flipped horizontally before 88 // the rotation, that is, the image is reflected along the central vertical 89 // axis of the camera sensor. So the users can see themselves as looking 90 // into a mirror. 91 // 92 // This does not affect the order of byte array of CAMERA_MSG_PREVIEW_FRAME, 93 // CAMERA_MSG_VIDEO_FRAME, CAMERA_MSG_POSTVIEW_FRAME, CAMERA_MSG_RAW_IMAGE, 94 // or CAMERA_MSG_COMPRESSED_IMAGE. This is not allowed to be set during 95 // preview. 96 CAMERA_CMD_SET_DISPLAY_ORIENTATION = 3, 97 98 // cmdType to disable/enable shutter sound. 99 // In sendCommand passing arg1 = 0 will disable, 100 // while passing arg1 = 1 will enable the shutter sound. 101 CAMERA_CMD_ENABLE_SHUTTER_SOUND = 4, 102 103 // cmdType to play recording sound. 104 CAMERA_CMD_PLAY_RECORDING_SOUND = 5, 105}; 106 107// camera fatal errors 108enum { 109 CAMERA_ERROR_UKNOWN = 1, 110 CAMERA_ERROR_SERVER_DIED = 100 111}; 112 113enum { 114 CAMERA_FACING_BACK = 0, 115 CAMERA_FACING_FRONT = 1 /* The camera faces to the user */ 116}; 117 118struct CameraInfo { 119 120 /** 121 * The direction that the camera faces to. It should be 122 * CAMERA_FACING_BACK or CAMERA_FACING_FRONT. 123 */ 124 int facing; 125 126 /** 127 * The orientation of the camera image. The value is the angle that the 128 * camera image needs to be rotated clockwise so it shows correctly on 129 * the display in its natural orientation. It should be 0, 90, 180, or 270. 130 * 131 * For example, suppose a device has a naturally tall screen, but the camera 132 * sensor is mounted in landscape. If the top side of the camera sensor is 133 * aligned with the right edge of the display in natural orientation, the 134 * value should be 90. 135 */ 136 int orientation; 137}; 138 139class ICameraService; 140class ICamera; 141class Surface; 142class Mutex; 143class String8; 144 145// ref-counted object for callbacks 146class CameraListener: virtual public RefBase 147{ 148public: 149 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2) = 0; 150 virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr) = 0; 151 virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) = 0; 152}; 153 154class Camera : public BnCameraClient, public IBinder::DeathRecipient 155{ 156public: 157 // construct a camera client from an existing remote 158 static sp<Camera> create(const sp<ICamera>& camera); 159 static int32_t getNumberOfCameras(); 160 static status_t getCameraInfo(int cameraId, 161 struct CameraInfo* cameraInfo); 162 static sp<Camera> connect(int cameraId); 163 ~Camera(); 164 void init(); 165 166 status_t reconnect(); 167 void disconnect(); 168 status_t lock(); 169 status_t unlock(); 170 171 status_t getStatus() { return mStatus; } 172 173 // pass the buffered Surface to the camera service 174 status_t setPreviewDisplay(const sp<Surface>& surface); 175 176 // start preview mode, must call setPreviewDisplay first 177 status_t startPreview(); 178 179 // stop preview mode 180 void stopPreview(); 181 182 // get preview state 183 bool previewEnabled(); 184 185 // start recording mode, must call setPreviewDisplay first 186 status_t startRecording(); 187 188 // stop recording mode 189 void stopRecording(); 190 191 // get recording state 192 bool recordingEnabled(); 193 194 // release a recording frame 195 void releaseRecordingFrame(const sp<IMemory>& mem); 196 197 // autoFocus - status returned from callback 198 status_t autoFocus(); 199 200 // cancel auto focus 201 status_t cancelAutoFocus(); 202 203 // take a picture - picture returned from callback 204 status_t takePicture(); 205 206 // set preview/capture parameters - key/value pairs 207 status_t setParameters(const String8& params); 208 209 // get preview/capture parameters - key/value pairs 210 String8 getParameters() const; 211 212 // send command to camera driver 213 status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2); 214 215 // return the total number of available video buffers. 216 int32_t getNumberOfVideoBuffers() const; 217 218 // return the individual video buffer corresponding to the given index. 219 sp<IMemory> getVideoBuffer(int32_t index) const; 220 221 // tell camera hal to store meta data or real YUV in video buffers. 222 status_t storeMetaDataInBuffers(bool enabled); 223 224 void setListener(const sp<CameraListener>& listener); 225 void setPreviewCallbackFlags(int preview_callback_flag); 226 227 // ICameraClient interface 228 virtual void notifyCallback(int32_t msgType, int32_t ext, int32_t ext2); 229 virtual void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr); 230 virtual void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr); 231 232 sp<ICamera> remote(); 233 234private: 235 Camera(); 236 Camera(const Camera&); 237 Camera& operator=(const Camera); 238 virtual void binderDied(const wp<IBinder>& who); 239 240 class DeathNotifier: public IBinder::DeathRecipient 241 { 242 public: 243 DeathNotifier() { 244 } 245 246 virtual void binderDied(const wp<IBinder>& who); 247 }; 248 249 static sp<DeathNotifier> mDeathNotifier; 250 251 // helper function to obtain camera service handle 252 static const sp<ICameraService>& getCameraService(); 253 254 sp<ICamera> mCamera; 255 status_t mStatus; 256 257 sp<CameraListener> mListener; 258 259 friend class DeathNotifier; 260 261 static Mutex mLock; 262 static sp<ICameraService> mCameraService; 263 264}; 265 266}; // namespace android 267 268#endif 269