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 |
| 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 | |
Dave Sparks | 59c1a93 | 2009-07-08 15:56:53 -0700 | [diff] [blame] | 21 | #include <utils/Timers.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
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 { |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 69 | CAMERA_MSG_ERROR = 0, |
Dave Sparks | d6289b1 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 70 | CAMERA_MSG_SHUTTER, |
| 71 | CAMERA_MSG_FOCUS, |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 72 | CAMERA_MSG_ZOOM, |
Dave Sparks | d6289b1 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 73 | CAMERA_MSG_PREVIEW_FRAME, |
| 74 | CAMERA_MSG_VIDEO_FRAME, |
| 75 | CAMERA_MSG_POSTVIEW_FRAME, |
| 76 | CAMERA_MSG_RAW_IMAGE, |
| 77 | CAMERA_MSG_COMPRESSED_IMAGE |
| 78 | }; |
| 79 | |
James Dong | a1b653d | 2009-07-02 10:04:20 -0700 | [diff] [blame] | 80 | // camera fatal errors |
| 81 | enum { |
| 82 | CAMERA_ERROR_UKNOWN = 1, |
| 83 | CAMERA_ERROR_SERVER_DIED = 100 |
| 84 | }; |
| 85 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | class ICameraService; |
| 87 | class ICamera; |
| 88 | class Surface; |
| 89 | class Mutex; |
| 90 | class String8; |
| 91 | |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 92 | // ref-counted object for callbacks |
| 93 | class CameraListener: virtual public RefBase |
| 94 | { |
| 95 | public: |
| 96 | virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2) = 0; |
| 97 | virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr) = 0; |
Dave Sparks | 59c1a93 | 2009-07-08 15:56:53 -0700 | [diff] [blame] | 98 | 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] | 99 | }; |
| 100 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | class Camera : public BnCameraClient, public IBinder::DeathRecipient |
| 102 | { |
| 103 | public: |
| 104 | // construct a camera client from an existing remote |
James Dong | 2adc2db | 2009-04-23 14:07:23 -0700 | [diff] [blame] | 105 | static sp<Camera> create(const sp<ICamera>& camera); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | static sp<Camera> connect(); |
| 107 | ~Camera(); |
| 108 | void init(); |
| 109 | |
| 110 | status_t reconnect(); |
| 111 | void disconnect(); |
| 112 | status_t lock(); |
| 113 | status_t unlock(); |
| 114 | |
| 115 | status_t getStatus() { return mStatus; } |
| 116 | |
| 117 | // pass the buffered ISurface to the camera service |
| 118 | status_t setPreviewDisplay(const sp<Surface>& surface); |
| 119 | status_t setPreviewDisplay(const sp<ISurface>& surface); |
| 120 | |
| 121 | // start preview mode, must call setPreviewDisplay first |
| 122 | status_t startPreview(); |
| 123 | |
| 124 | // stop preview mode |
| 125 | void stopPreview(); |
| 126 | |
| 127 | // get preview state |
| 128 | bool previewEnabled(); |
| 129 | |
| 130 | // start recording mode, must call setPreviewDisplay first |
| 131 | status_t startRecording(); |
| 132 | |
| 133 | // stop recording mode |
| 134 | void stopRecording(); |
| 135 | |
| 136 | // get recording state |
| 137 | bool recordingEnabled(); |
| 138 | |
| 139 | // release a recording frame |
| 140 | void releaseRecordingFrame(const sp<IMemory>& mem); |
| 141 | |
| 142 | // autoFocus - status returned from callback |
| 143 | status_t autoFocus(); |
| 144 | |
| 145 | // take a picture - picture returned from callback |
| 146 | status_t takePicture(); |
| 147 | |
| 148 | // set preview/capture parameters - key/value pairs |
| 149 | status_t setParameters(const String8& params); |
| 150 | |
| 151 | // get preview/capture parameters - key/value pairs |
| 152 | String8 getParameters() const; |
| 153 | |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 154 | void setListener(const sp<CameraListener>& listener); |
| 155 | void setPreviewCallbackFlags(int preview_callback_flag); |
| 156 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | // ICameraClient interface |
Dave Sparks | 2a04aef | 2009-05-07 12:25:25 -0700 | [diff] [blame] | 158 | virtual void notifyCallback(int32_t msgType, int32_t ext, int32_t ext2); |
Dave Sparks | d6289b1 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 159 | virtual void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr); |
Dave Sparks | 59c1a93 | 2009-07-08 15:56:53 -0700 | [diff] [blame] | 160 | 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] | 161 | |
| 162 | sp<ICamera> remote(); |
| 163 | |
| 164 | private: |
| 165 | Camera(); |
Dave Sparks | 6f0602e | 2009-06-24 10:42:53 -0700 | [diff] [blame] | 166 | Camera(const Camera&); |
| 167 | Camera& operator=(const Camera); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | virtual void binderDied(const wp<IBinder>& who); |
| 169 | |
| 170 | class DeathNotifier: public IBinder::DeathRecipient |
| 171 | { |
| 172 | public: |
| 173 | DeathNotifier() { |
| 174 | } |
| 175 | |
| 176 | virtual void binderDied(const wp<IBinder>& who); |
| 177 | }; |
| 178 | |
| 179 | static sp<DeathNotifier> mDeathNotifier; |
| 180 | |
| 181 | // helper function to obtain camera service handle |
| 182 | static const sp<ICameraService>& getCameraService(); |
| 183 | |
| 184 | sp<ICamera> mCamera; |
| 185 | status_t mStatus; |
| 186 | |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 187 | sp<CameraListener> mListener; |
| 188 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | friend class DeathNotifier; |
| 190 | |
| 191 | static Mutex mLock; |
| 192 | static sp<ICameraService> mCameraService; |
| 193 | |
| 194 | }; |
| 195 | |
| 196 | }; // namespace android |
| 197 | |
| 198 | #endif |
| 199 | |