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