The Android Open Source Project | edbf3b6 | 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 | |
| 21 | #include <ui/ICameraClient.h> |
| 22 | |
| 23 | namespace 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 | class ICameraService; |
| 67 | class ICamera; |
| 68 | class Surface; |
| 69 | class Mutex; |
| 70 | class String8; |
| 71 | |
| 72 | typedef void (*shutter_callback)(void *cookie); |
| 73 | typedef void (*frame_callback)(const sp<IMemory>& mem, void *cookie); |
| 74 | typedef void (*autofocus_callback)(bool focused, void *cookie); |
| 75 | typedef void (*error_callback)(status_t err, void *cookie); |
| 76 | |
| 77 | class Camera : public BnCameraClient, public IBinder::DeathRecipient |
| 78 | { |
| 79 | public: |
| 80 | // construct a camera client from an existing remote |
| 81 | Camera(const sp<ICamera>& camera); |
| 82 | |
| 83 | static sp<Camera> connect(); |
| 84 | ~Camera(); |
| 85 | void init(); |
| 86 | |
| 87 | status_t reconnect(); |
| 88 | void disconnect(); |
| 89 | status_t lock(); |
| 90 | status_t unlock(); |
| 91 | |
| 92 | status_t getStatus() { return mStatus; } |
| 93 | |
| 94 | // pass the buffered ISurface to the camera service |
| 95 | status_t setPreviewDisplay(const sp<Surface>& surface); |
| 96 | status_t setPreviewDisplay(const sp<ISurface>& surface); |
| 97 | |
| 98 | // start preview mode, must call setPreviewDisplay first |
| 99 | status_t startPreview(); |
| 100 | |
| 101 | // stop preview mode |
| 102 | void stopPreview(); |
| 103 | |
| 104 | // get preview state |
| 105 | bool previewEnabled(); |
| 106 | |
| 107 | // start recording mode, must call setPreviewDisplay first |
| 108 | status_t startRecording(); |
| 109 | |
| 110 | // stop recording mode |
| 111 | void stopRecording(); |
| 112 | |
| 113 | // get recording state |
| 114 | bool recordingEnabled(); |
| 115 | |
| 116 | // release a recording frame |
| 117 | void releaseRecordingFrame(const sp<IMemory>& mem); |
| 118 | |
| 119 | // autoFocus - status returned from callback |
| 120 | status_t autoFocus(); |
| 121 | |
| 122 | // take a picture - picture returned from callback |
| 123 | status_t takePicture(); |
| 124 | |
| 125 | // set preview/capture parameters - key/value pairs |
| 126 | status_t setParameters(const String8& params); |
| 127 | |
| 128 | // get preview/capture parameters - key/value pairs |
| 129 | String8 getParameters() const; |
| 130 | |
| 131 | void setShutterCallback(shutter_callback cb, void *cookie); |
| 132 | void setRawCallback(frame_callback cb, void *cookie); |
| 133 | void setJpegCallback(frame_callback cb, void *cookie); |
| 134 | void setRecordingCallback(frame_callback cb, void *cookie); |
| 135 | void setPreviewCallback(frame_callback cb, void *cookie, int preview_callback_flag = FRAME_CALLBACK_FLAG_NOOP); |
| 136 | void setErrorCallback(error_callback cb, void *cookie); |
| 137 | void setAutoFocusCallback(autofocus_callback cb, void *cookie); |
| 138 | |
| 139 | // ICameraClient interface |
| 140 | virtual void shutterCallback(); |
| 141 | virtual void rawCallback(const sp<IMemory>& picture); |
| 142 | virtual void jpegCallback(const sp<IMemory>& picture); |
| 143 | virtual void previewCallback(const sp<IMemory>& frame); |
| 144 | virtual void errorCallback(status_t error); |
| 145 | virtual void autoFocusCallback(bool focused); |
| 146 | virtual void recordingCallback(const sp<IMemory>& frame); |
| 147 | |
| 148 | sp<ICamera> remote(); |
| 149 | |
| 150 | private: |
| 151 | Camera(); |
| 152 | virtual void binderDied(const wp<IBinder>& who); |
| 153 | |
| 154 | class DeathNotifier: public IBinder::DeathRecipient |
| 155 | { |
| 156 | public: |
| 157 | DeathNotifier() { |
| 158 | } |
| 159 | |
| 160 | virtual void binderDied(const wp<IBinder>& who); |
| 161 | }; |
| 162 | |
| 163 | static sp<DeathNotifier> mDeathNotifier; |
| 164 | |
| 165 | // helper function to obtain camera service handle |
| 166 | static const sp<ICameraService>& getCameraService(); |
| 167 | |
| 168 | sp<ICamera> mCamera; |
| 169 | status_t mStatus; |
| 170 | |
| 171 | shutter_callback mShutterCallback; |
| 172 | void *mShutterCallbackCookie; |
| 173 | frame_callback mRawCallback; |
| 174 | void *mRawCallbackCookie; |
| 175 | frame_callback mJpegCallback; |
| 176 | void *mJpegCallbackCookie; |
| 177 | frame_callback mPreviewCallback; |
| 178 | void *mPreviewCallbackCookie; |
| 179 | frame_callback mRecordingCallback; |
| 180 | void *mRecordingCallbackCookie; |
| 181 | error_callback mErrorCallback; |
| 182 | void *mErrorCallbackCookie; |
| 183 | autofocus_callback mAutoFocusCallback; |
| 184 | void *mAutoFocusCallbackCookie; |
| 185 | |
| 186 | friend class DeathNotifier; |
| 187 | |
| 188 | static Mutex mLock; |
| 189 | static sp<ICameraService> mCameraService; |
| 190 | |
| 191 | }; |
| 192 | |
| 193 | }; // namespace android |
| 194 | |
| 195 | #endif |
| 196 | |