Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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_SERVERS_CAMERA_CAMERA2CLIENT_H |
| 18 | #define ANDROID_SERVERS_CAMERA_CAMERA2CLIENT_H |
| 19 | |
| 20 | #include "Camera2Device.h" |
| 21 | #include "CameraService.h" |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 22 | #include "camera2/Parameters.h" |
Eino-Ville Talvala | a16733e | 2012-08-27 23:41:56 -0700 | [diff] [blame^] | 23 | #include "camera2/FrameProcessor.h" |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 24 | #include <binder/MemoryBase.h> |
| 25 | #include <binder/MemoryHeapBase.h> |
| 26 | #include <gui/CpuConsumer.h> |
Eino-Ville Talvala | 30e65e7 | 2012-08-21 13:30:45 -0700 | [diff] [blame] | 27 | #include <gui/BufferItemConsumer.h> |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | /** |
| 32 | * Implements the android.hardware.camera API on top of |
| 33 | * camera device HAL version 2. |
| 34 | */ |
Eino-Ville Talvala | 8ce89d9 | 2012-08-10 08:40:26 -0700 | [diff] [blame] | 35 | class Camera2Client : |
| 36 | public CameraService::Client, |
Eino-Ville Talvala | c8474b6 | 2012-08-24 16:30:44 -0700 | [diff] [blame] | 37 | public Camera2Device::NotificationListener |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 38 | { |
| 39 | public: |
| 40 | // ICamera interface (see ICamera for details) |
Eino-Ville Talvala | 160d4af | 2012-08-03 09:40:16 -0700 | [diff] [blame] | 41 | |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 42 | virtual void disconnect(); |
| 43 | virtual status_t connect(const sp<ICameraClient>& client); |
| 44 | virtual status_t lock(); |
| 45 | virtual status_t unlock(); |
| 46 | virtual status_t setPreviewDisplay(const sp<Surface>& surface); |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 47 | virtual status_t setPreviewTexture( |
| 48 | const sp<ISurfaceTexture>& surfaceTexture); |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 49 | virtual void setPreviewCallbackFlag(int flag); |
| 50 | virtual status_t startPreview(); |
| 51 | virtual void stopPreview(); |
| 52 | virtual bool previewEnabled(); |
| 53 | virtual status_t storeMetaDataInBuffers(bool enabled); |
| 54 | virtual status_t startRecording(); |
| 55 | virtual void stopRecording(); |
| 56 | virtual bool recordingEnabled(); |
| 57 | virtual void releaseRecordingFrame(const sp<IMemory>& mem); |
| 58 | virtual status_t autoFocus(); |
| 59 | virtual status_t cancelAutoFocus(); |
| 60 | virtual status_t takePicture(int msgType); |
| 61 | virtual status_t setParameters(const String8& params); |
| 62 | virtual String8 getParameters() const; |
| 63 | virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2); |
| 64 | |
| 65 | // Interface used by CameraService |
Eino-Ville Talvala | 160d4af | 2012-08-03 09:40:16 -0700 | [diff] [blame] | 66 | |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 67 | Camera2Client(const sp<CameraService>& cameraService, |
| 68 | const sp<ICameraClient>& cameraClient, |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 69 | int cameraId, |
| 70 | int cameraFacing, |
| 71 | int clientPid); |
Eino-Ville Talvala | 160d4af | 2012-08-03 09:40:16 -0700 | [diff] [blame] | 72 | virtual ~Camera2Client(); |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 73 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 74 | status_t initialize(camera_module_t *module); |
| 75 | |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 76 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 77 | |
Eino-Ville Talvala | 160d4af | 2012-08-03 09:40:16 -0700 | [diff] [blame] | 78 | // Interface used by CameraDevice |
| 79 | |
| 80 | virtual void notifyError(int errorCode, int arg1, int arg2); |
| 81 | virtual void notifyShutter(int frameNumber, nsecs_t timestamp); |
| 82 | virtual void notifyAutoFocus(uint8_t newState, int triggerId); |
| 83 | virtual void notifyAutoExposure(uint8_t newState, int triggerId); |
| 84 | virtual void notifyAutoWhitebalance(uint8_t newState, int triggerId); |
| 85 | |
Eino-Ville Talvala | a16733e | 2012-08-27 23:41:56 -0700 | [diff] [blame^] | 86 | // Interface used by independent components of Camera2Client. |
| 87 | |
| 88 | int getCameraId(); |
| 89 | const sp<Camera2Device>& getCameraDevice(); |
| 90 | camera2::SharedParameters& getParameters(); |
| 91 | |
| 92 | // Simple class to ensure that access to ICameraClient is serialized by |
| 93 | // requiring mCameraClientLock to be locked before access to mCameraClient |
| 94 | // is possible. |
| 95 | class SharedCameraClient { |
| 96 | public: |
| 97 | class Lock { |
| 98 | public: |
| 99 | Lock(SharedCameraClient &client); |
| 100 | ~Lock(); |
| 101 | sp<ICameraClient> &mCameraClient; |
| 102 | private: |
| 103 | SharedCameraClient &mSharedClient; |
| 104 | }; |
| 105 | SharedCameraClient& operator=(const sp<ICameraClient>& client); |
| 106 | void clear(); |
| 107 | private: |
| 108 | sp<ICameraClient> mCameraClient; |
| 109 | mutable Mutex mCameraClientLock; |
| 110 | } mSharedCameraClient; |
| 111 | |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 112 | private: |
Eino-Ville Talvala | 3cca136 | 2012-06-07 10:07:18 -0700 | [diff] [blame] | 113 | /** ICamera interface-related private members */ |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 114 | |
Eino-Ville Talvala | ac45eb3 | 2012-06-07 10:24:51 -0700 | [diff] [blame] | 115 | // Mutex that must be locked by methods implementing the ICamera interface. |
Eino-Ville Talvala | 36cdfb1 | 2012-08-02 17:34:15 -0700 | [diff] [blame] | 116 | // Ensures serialization between incoming ICamera calls. All methods below |
| 117 | // that append 'L' to the name assume that mICameraLock is locked when |
| 118 | // they're called |
Eino-Ville Talvala | ac45eb3 | 2012-06-07 10:24:51 -0700 | [diff] [blame] | 119 | mutable Mutex mICameraLock; |
| 120 | |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 121 | typedef camera2::Parameters Parameters; |
| 122 | typedef camera2::CameraMetadata CameraMetadata; |
Eino-Ville Talvala | 228a538 | 2012-08-13 12:16:06 -0700 | [diff] [blame] | 123 | |
Eino-Ville Talvala | 36cdfb1 | 2012-08-02 17:34:15 -0700 | [diff] [blame] | 124 | status_t setPreviewWindowL(const sp<IBinder>& binder, |
Eino-Ville Talvala | be0573b | 2012-06-15 12:42:30 -0700 | [diff] [blame] | 125 | sp<ANativeWindow> window); |
Eino-Ville Talvala | 228a538 | 2012-08-13 12:16:06 -0700 | [diff] [blame] | 126 | status_t startPreviewL(Parameters ¶ms, bool restart); |
| 127 | void stopPreviewL(); |
| 128 | status_t startRecordingL(Parameters ¶ms, bool restart); |
| 129 | bool recordingEnabledL(); |
James Dong | 8da4cd7 | 2012-08-04 19:58:07 -0700 | [diff] [blame] | 130 | |
Eino-Ville Talvala | 36cdfb1 | 2012-08-02 17:34:15 -0700 | [diff] [blame] | 131 | // Individual commands for sendCommand() |
| 132 | status_t commandStartSmoothZoomL(); |
| 133 | status_t commandStopSmoothZoomL(); |
| 134 | status_t commandSetDisplayOrientationL(int degrees); |
| 135 | status_t commandEnableShutterSoundL(bool enable); |
| 136 | status_t commandPlayRecordingSoundL(); |
| 137 | status_t commandStartFaceDetectionL(int type); |
Eino-Ville Talvala | 228a538 | 2012-08-13 12:16:06 -0700 | [diff] [blame] | 138 | status_t commandStopFaceDetectionL(Parameters ¶ms); |
Eino-Ville Talvala | 36cdfb1 | 2012-08-02 17:34:15 -0700 | [diff] [blame] | 139 | status_t commandEnableFocusMoveMsgL(bool enable); |
| 140 | status_t commandPingL(); |
| 141 | status_t commandSetVideoBufferCountL(size_t count); |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 142 | |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 143 | // Current camera device configuration |
| 144 | camera2::SharedParameters mParameters; |
Eino-Ville Talvala | 8ce89d9 | 2012-08-10 08:40:26 -0700 | [diff] [blame] | 145 | |
Eino-Ville Talvala | 3cca136 | 2012-06-07 10:07:18 -0700 | [diff] [blame] | 146 | /** Camera device-related private members */ |
| 147 | |
Eino-Ville Talvala | 9cca4c6 | 2012-06-15 15:41:44 -0700 | [diff] [blame] | 148 | class Camera2Heap; |
| 149 | |
Eino-Ville Talvala | 228a538 | 2012-08-13 12:16:06 -0700 | [diff] [blame] | 150 | void setPreviewCallbackFlagL(Parameters ¶ms, int flag); |
Eino-Ville Talvala | 8ce89d9 | 2012-08-10 08:40:26 -0700 | [diff] [blame] | 151 | status_t updateRequests(const Parameters ¶ms); |
| 152 | |
Eino-Ville Talvala | 9cca4c6 | 2012-06-15 15:41:44 -0700 | [diff] [blame] | 153 | // Used with stream IDs |
| 154 | static const int NO_STREAM = -1; |
| 155 | |
Eino-Ville Talvala | a16733e | 2012-08-27 23:41:56 -0700 | [diff] [blame^] | 156 | sp<camera2::FrameProcessor> mFrameProcessor; |
Eino-Ville Talvala | 8ce89d9 | 2012-08-10 08:40:26 -0700 | [diff] [blame] | 157 | |
Eino-Ville Talvala | 9cca4c6 | 2012-06-15 15:41:44 -0700 | [diff] [blame] | 158 | /* Preview related members */ |
| 159 | |
| 160 | int mPreviewStreamId; |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 161 | CameraMetadata mPreviewRequest; |
Eino-Ville Talvala | 9cca4c6 | 2012-06-15 15:41:44 -0700 | [diff] [blame] | 162 | sp<IBinder> mPreviewSurface; |
| 163 | sp<ANativeWindow> mPreviewWindow; |
Eino-Ville Talvala | 836b81f | 2012-07-27 11:35:21 -0700 | [diff] [blame] | 164 | |
| 165 | status_t updatePreviewRequest(const Parameters ¶ms); |
| 166 | status_t updatePreviewStream(const Parameters ¶ms); |
Eino-Ville Talvala | 9cca4c6 | 2012-06-15 15:41:44 -0700 | [diff] [blame] | 167 | |
Eino-Ville Talvala | 228a538 | 2012-08-13 12:16:06 -0700 | [diff] [blame] | 168 | /** Preview callback related members */ |
| 169 | |
| 170 | int mCallbackStreamId; |
| 171 | static const size_t kCallbackHeapCount = 6; |
| 172 | sp<CpuConsumer> mCallbackConsumer; |
| 173 | sp<ANativeWindow> mCallbackWindow; |
| 174 | // Simple listener that forwards frame available notifications from |
| 175 | // a CPU consumer to the callback notification |
| 176 | class CallbackWaiter: public CpuConsumer::FrameAvailableListener { |
| 177 | public: |
| 178 | CallbackWaiter(Camera2Client *parent) : mParent(parent) {} |
| 179 | void onFrameAvailable() { mParent->onCallbackAvailable(); } |
| 180 | private: |
| 181 | Camera2Client *mParent; |
| 182 | }; |
| 183 | sp<CallbackWaiter> mCallbackWaiter; |
| 184 | sp<Camera2Heap> mCallbackHeap; |
| 185 | int mCallbackHeapId; |
| 186 | size_t mCallbackHeapHead, mCallbackHeapFree; |
| 187 | // Handle callback image buffers |
| 188 | void onCallbackAvailable(); |
| 189 | |
| 190 | status_t updateCallbackStream(const Parameters ¶ms); |
| 191 | |
Eino-Ville Talvala | 9cca4c6 | 2012-06-15 15:41:44 -0700 | [diff] [blame] | 192 | /* Still image capture related members */ |
| 193 | |
| 194 | int mCaptureStreamId; |
| 195 | sp<CpuConsumer> mCaptureConsumer; |
| 196 | sp<ANativeWindow> mCaptureWindow; |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 197 | // Simple listener that forwards frame available notifications from |
| 198 | // a CPU consumer to the capture notification |
| 199 | class CaptureWaiter: public CpuConsumer::FrameAvailableListener { |
| 200 | public: |
| 201 | CaptureWaiter(Camera2Client *parent) : mParent(parent) {} |
| 202 | void onFrameAvailable() { mParent->onCaptureAvailable(); } |
| 203 | private: |
| 204 | Camera2Client *mParent; |
| 205 | }; |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 206 | sp<CaptureWaiter> mCaptureWaiter; |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 207 | CameraMetadata mCaptureRequest; |
Eino-Ville Talvala | 9cca4c6 | 2012-06-15 15:41:44 -0700 | [diff] [blame] | 208 | sp<Camera2Heap> mCaptureHeap; |
| 209 | // Handle captured image buffers |
| 210 | void onCaptureAvailable(); |
Eino-Ville Talvala | 836b81f | 2012-07-27 11:35:21 -0700 | [diff] [blame] | 211 | |
| 212 | status_t updateCaptureRequest(const Parameters ¶ms); |
| 213 | status_t updateCaptureStream(const Parameters ¶ms); |
Eino-Ville Talvala | 9cca4c6 | 2012-06-15 15:41:44 -0700 | [diff] [blame] | 214 | |
| 215 | /* Recording related members */ |
| 216 | |
| 217 | int mRecordingStreamId; |
Eino-Ville Talvala | 228a538 | 2012-08-13 12:16:06 -0700 | [diff] [blame] | 218 | int mRecordingFrameCount; |
Eino-Ville Talvala | 30e65e7 | 2012-08-21 13:30:45 -0700 | [diff] [blame] | 219 | sp<BufferItemConsumer> mRecordingConsumer; |
Eino-Ville Talvala | 9cca4c6 | 2012-06-15 15:41:44 -0700 | [diff] [blame] | 220 | sp<ANativeWindow> mRecordingWindow; |
| 221 | // Simple listener that forwards frame available notifications from |
| 222 | // a CPU consumer to the recording notification |
Eino-Ville Talvala | 30e65e7 | 2012-08-21 13:30:45 -0700 | [diff] [blame] | 223 | class RecordingWaiter: public BufferItemConsumer::FrameAvailableListener { |
Eino-Ville Talvala | 9cca4c6 | 2012-06-15 15:41:44 -0700 | [diff] [blame] | 224 | public: |
| 225 | RecordingWaiter(Camera2Client *parent) : mParent(parent) {} |
| 226 | void onFrameAvailable() { mParent->onRecordingFrameAvailable(); } |
| 227 | private: |
| 228 | Camera2Client *mParent; |
| 229 | }; |
| 230 | sp<RecordingWaiter> mRecordingWaiter; |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 231 | CameraMetadata mRecordingRequest; |
Eino-Ville Talvala | 9cca4c6 | 2012-06-15 15:41:44 -0700 | [diff] [blame] | 232 | sp<Camera2Heap> mRecordingHeap; |
| 233 | |
James Dong | 983cf23 | 2012-08-01 16:39:55 -0700 | [diff] [blame] | 234 | static const size_t kDefaultRecordingHeapCount = 8; |
| 235 | size_t mRecordingHeapCount; |
Eino-Ville Talvala | 30e65e7 | 2012-08-21 13:30:45 -0700 | [diff] [blame] | 236 | Vector<BufferItemConsumer::BufferItem> mRecordingBuffers; |
Eino-Ville Talvala | 9cca4c6 | 2012-06-15 15:41:44 -0700 | [diff] [blame] | 237 | size_t mRecordingHeapHead, mRecordingHeapFree; |
| 238 | // Handle new recording image buffers |
| 239 | void onRecordingFrameAvailable(); |
Eino-Ville Talvala | 836b81f | 2012-07-27 11:35:21 -0700 | [diff] [blame] | 240 | |
| 241 | status_t updateRecordingRequest(const Parameters ¶ms); |
| 242 | status_t updateRecordingStream(const Parameters ¶ms); |
Eino-Ville Talvala | 9cca4c6 | 2012-06-15 15:41:44 -0700 | [diff] [blame] | 243 | |
Eino-Ville Talvala | 174181e | 2012-08-03 13:53:39 -0700 | [diff] [blame] | 244 | /** Notification-related members */ |
| 245 | |
| 246 | bool mAfInMotion; |
| 247 | |
Eino-Ville Talvala | 9cca4c6 | 2012-06-15 15:41:44 -0700 | [diff] [blame] | 248 | /** Camera2Device instance wrapping HAL2 entry */ |
Eino-Ville Talvala | 3cca136 | 2012-06-07 10:07:18 -0700 | [diff] [blame] | 249 | |
| 250 | sp<Camera2Device> mDevice; |
| 251 | |
Eino-Ville Talvala | 9cca4c6 | 2012-06-15 15:41:44 -0700 | [diff] [blame] | 252 | /** Utility members */ |
| 253 | |
Eino-Ville Talvala | 3a60914 | 2012-07-31 14:36:26 -0700 | [diff] [blame] | 254 | // Verify that caller is the owner of the camera |
| 255 | status_t checkPid(const char *checkLocation) const; |
| 256 | |
Eino-Ville Talvala | 9cca4c6 | 2012-06-15 15:41:44 -0700 | [diff] [blame] | 257 | // Utility class for managing a set of IMemory blocks |
| 258 | class Camera2Heap : public RefBase { |
| 259 | public: |
| 260 | Camera2Heap(size_t buf_size, uint_t num_buffers = 1, |
| 261 | const char *name = NULL) : |
| 262 | mBufSize(buf_size), |
| 263 | mNumBufs(num_buffers) { |
| 264 | mHeap = new MemoryHeapBase(buf_size * num_buffers, 0, name); |
| 265 | mBuffers = new sp<MemoryBase>[mNumBufs]; |
| 266 | for (uint_t i = 0; i < mNumBufs; i++) |
| 267 | mBuffers[i] = new MemoryBase(mHeap, |
| 268 | i * mBufSize, |
| 269 | mBufSize); |
| 270 | } |
| 271 | |
| 272 | virtual ~Camera2Heap() |
| 273 | { |
| 274 | delete [] mBuffers; |
| 275 | } |
| 276 | |
| 277 | size_t mBufSize; |
| 278 | uint_t mNumBufs; |
| 279 | sp<MemoryHeapBase> mHeap; |
| 280 | sp<MemoryBase> *mBuffers; |
| 281 | }; |
Eino-Ville Talvala | 3cca136 | 2012-06-07 10:07:18 -0700 | [diff] [blame] | 282 | |
Eino-Ville Talvala | be0573b | 2012-06-15 12:42:30 -0700 | [diff] [blame] | 283 | // Update parameters all requests use, based on mParameters |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 284 | status_t updateRequestCommon(CameraMetadata *request, const Parameters ¶ms) const; |
Eino-Ville Talvala | be0573b | 2012-06-15 12:42:30 -0700 | [diff] [blame] | 285 | |
Eino-Ville Talvala | 30e65e7 | 2012-08-21 13:30:45 -0700 | [diff] [blame] | 286 | // Map from sensor active array pixel coordinates to normalized camera |
| 287 | // parameter coordinates. The former are (0,0)-(array width - 1, array height |
| 288 | // - 1), the latter from (-1000,-1000)-(1000,1000) |
Eino-Ville Talvala | f17b6cf | 2012-08-25 17:12:18 -0700 | [diff] [blame] | 289 | int normalizedXToArray(int x) const; |
| 290 | int normalizedYToArray(int y) const; |
Eino-Ville Talvala | 8ce89d9 | 2012-08-10 08:40:26 -0700 | [diff] [blame] | 291 | int arrayXToNormalized(int width) const; |
| 292 | int arrayYToNormalized(int height) const; |
| 293 | |
Eino-Ville Talvala | 160d4af | 2012-08-03 09:40:16 -0700 | [diff] [blame] | 294 | |
Eino-Ville Talvala | 228a538 | 2012-08-13 12:16:06 -0700 | [diff] [blame] | 295 | static size_t calculateBufferSize(int width, int height, |
| 296 | int format, int stride); |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | }; // namespace android |
| 300 | |
| 301 | #endif |