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