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