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 | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 22 | #include "camera/CameraParameters.h" |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 23 | #include <binder/MemoryBase.h> |
| 24 | #include <binder/MemoryHeapBase.h> |
| 25 | #include <gui/CpuConsumer.h> |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | /** |
| 30 | * Implements the android.hardware.camera API on top of |
| 31 | * camera device HAL version 2. |
| 32 | */ |
| 33 | class Camera2Client : public CameraService::Client |
| 34 | { |
| 35 | public: |
| 36 | // ICamera interface (see ICamera for details) |
| 37 | virtual void disconnect(); |
| 38 | virtual status_t connect(const sp<ICameraClient>& client); |
| 39 | virtual status_t lock(); |
| 40 | virtual status_t unlock(); |
| 41 | virtual status_t setPreviewDisplay(const sp<Surface>& surface); |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 42 | virtual status_t setPreviewTexture( |
| 43 | const sp<ISurfaceTexture>& surfaceTexture); |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 44 | virtual void setPreviewCallbackFlag(int flag); |
| 45 | virtual status_t startPreview(); |
| 46 | virtual void stopPreview(); |
| 47 | virtual bool previewEnabled(); |
| 48 | virtual status_t storeMetaDataInBuffers(bool enabled); |
| 49 | virtual status_t startRecording(); |
| 50 | virtual void stopRecording(); |
| 51 | virtual bool recordingEnabled(); |
| 52 | virtual void releaseRecordingFrame(const sp<IMemory>& mem); |
| 53 | virtual status_t autoFocus(); |
| 54 | virtual status_t cancelAutoFocus(); |
| 55 | virtual status_t takePicture(int msgType); |
| 56 | virtual status_t setParameters(const String8& params); |
| 57 | virtual String8 getParameters() const; |
| 58 | virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2); |
| 59 | |
| 60 | // Interface used by CameraService |
| 61 | Camera2Client(const sp<CameraService>& cameraService, |
| 62 | const sp<ICameraClient>& cameraClient, |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 63 | int cameraId, |
| 64 | int cameraFacing, |
| 65 | int clientPid); |
| 66 | ~Camera2Client(); |
| 67 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 68 | status_t initialize(camera_module_t *module); |
| 69 | |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 70 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 71 | |
| 72 | private: |
Eino-Ville Talvala | 4ecfec3 | 2012-06-12 17:13:48 -0700 | [diff] [blame^] | 73 | enum State { |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 74 | NOT_INITIALIZED, |
| 75 | STOPPED, |
| 76 | WAITING_FOR_PREVIEW_WINDOW, |
Eino-Ville Talvala | 7f61084 | 2012-06-07 10:20:51 -0700 | [diff] [blame] | 77 | PREVIEW, |
| 78 | RECORD, |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 79 | STILL_CAPTURE, |
| 80 | VIDEO_SNAPSHOT |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 81 | } mState; |
| 82 | |
Eino-Ville Talvala | 4ecfec3 | 2012-06-12 17:13:48 -0700 | [diff] [blame^] | 83 | static const char *getStateName(State state); |
| 84 | |
Eino-Ville Talvala | 3cca136 | 2012-06-07 10:07:18 -0700 | [diff] [blame] | 85 | /** ICamera interface-related private members */ |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 86 | |
Eino-Ville Talvala | ac45eb3 | 2012-06-07 10:24:51 -0700 | [diff] [blame] | 87 | // Mutex that must be locked by methods implementing the ICamera interface. |
| 88 | // Ensures serialization between incoming ICamera calls |
| 89 | mutable Mutex mICameraLock; |
| 90 | |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 91 | // The following must be called with mICamaeraLock already locked |
Eino-Ville Talvala | ac45eb3 | 2012-06-07 10:24:51 -0700 | [diff] [blame] | 92 | |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 93 | status_t setPreviewWindowLocked(const sp<IBinder>& binder, |
| 94 | const sp<ANativeWindow>& window); |
| 95 | |
| 96 | void stopPreviewLocked(); |
| 97 | status_t startPreviewLocked(); |
| 98 | |
| 99 | // Mutex that must be locked before accessing mParameters, mParamsFlattened |
Eino-Ville Talvala | ac45eb3 | 2012-06-07 10:24:51 -0700 | [diff] [blame] | 100 | mutable Mutex mParamsLock; |
Eino-Ville Talvala | 3cca136 | 2012-06-07 10:07:18 -0700 | [diff] [blame] | 101 | String8 mParamsFlattened; |
| 102 | // Current camera state; this is the contents of the CameraParameters object |
| 103 | // in a more-efficient format. The enum values are mostly based off the |
| 104 | // corresponding camera2 enums, not the camera1 strings. A few are defined |
| 105 | // here if they don't cleanly map to camera2 values. |
| 106 | struct Parameters { |
| 107 | int previewWidth, previewHeight; |
| 108 | int previewFpsRangeMin, previewFpsRangeMax; |
| 109 | int previewFps; // deprecated, here only for tracking changes |
| 110 | int previewFormat; |
| 111 | |
| 112 | int pictureWidth, pictureHeight; |
| 113 | |
| 114 | int jpegThumbWidth, jpegThumbHeight; |
| 115 | int jpegQuality, jpegThumbQuality; |
| 116 | int jpegRotation; |
| 117 | |
| 118 | bool gpsEnabled; |
| 119 | double gpsLatitude; |
| 120 | double gpsLongitude; |
| 121 | double gpsAltitude; |
| 122 | int64_t gpsTimestamp; |
| 123 | String8 gpsProcessingMethod; |
| 124 | |
| 125 | int wbMode; |
| 126 | int effectMode; |
| 127 | int antibandingMode; |
| 128 | int sceneMode; |
| 129 | |
| 130 | enum flashMode_t { |
| 131 | FLASH_MODE_OFF = 0, |
| 132 | FLASH_MODE_AUTO, |
| 133 | FLASH_MODE_ON, |
| 134 | FLASH_MODE_TORCH, |
| 135 | FLASH_MODE_RED_EYE = ANDROID_CONTROL_AE_ON_AUTO_FLASH_REDEYE, |
| 136 | FLASH_MODE_INVALID = -1 |
| 137 | } flashMode; |
| 138 | |
| 139 | enum focusMode_t { |
| 140 | FOCUS_MODE_AUTO = ANDROID_CONTROL_AF_AUTO, |
| 141 | FOCUS_MODE_MACRO = ANDROID_CONTROL_AF_MACRO, |
| 142 | FOCUS_MODE_CONTINUOUS_VIDEO = ANDROID_CONTROL_AF_CONTINUOUS_VIDEO, |
| 143 | FOCUS_MODE_CONTINUOUS_PICTURE = |
| 144 | ANDROID_CONTROL_AF_CONTINUOUS_PICTURE, |
| 145 | FOCUS_MODE_EDOF = ANDROID_CONTROL_AF_EDOF, |
| 146 | FOCUS_MODE_INFINITY, |
| 147 | FOCUS_MODE_FIXED, |
| 148 | FOCUS_MODE_INVALID = -1 |
| 149 | } focusMode; |
| 150 | |
| 151 | struct Area { |
| 152 | int left, top, right, bottom; |
| 153 | int weight; |
| 154 | Area() {} |
| 155 | Area(int left, int top, int right, int bottom, int weight): |
| 156 | left(left), top(top), right(right), bottom(bottom), |
| 157 | weight(weight) {} |
| 158 | }; |
| 159 | Vector<Area> focusingAreas; |
| 160 | |
| 161 | int exposureCompensation; |
| 162 | bool autoExposureLock; |
| 163 | bool autoWhiteBalanceLock; |
| 164 | |
| 165 | Vector<Area> meteringAreas; |
| 166 | |
| 167 | int zoom; |
| 168 | |
| 169 | int videoWidth, videoHeight; |
| 170 | |
| 171 | bool recordingHint; |
| 172 | bool videoStabilization; |
| 173 | } mParameters; |
| 174 | |
| 175 | /** Camera device-related private members */ |
| 176 | |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 177 | // Simple listener that forwards frame available notifications from |
| 178 | // a CPU consumer to the capture notification |
| 179 | class CaptureWaiter: public CpuConsumer::FrameAvailableListener { |
| 180 | public: |
| 181 | CaptureWaiter(Camera2Client *parent) : mParent(parent) {} |
| 182 | void onFrameAvailable() { mParent->onCaptureAvailable(); } |
| 183 | private: |
| 184 | Camera2Client *mParent; |
| 185 | }; |
| 186 | |
| 187 | void onCaptureAvailable(); |
| 188 | |
Eino-Ville Talvala | 3cca136 | 2012-06-07 10:07:18 -0700 | [diff] [blame] | 189 | // Number of zoom steps to simulate |
| 190 | static const unsigned int NUM_ZOOM_STEPS = 10; |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 191 | // Used with mPreviewStreamId, mCaptureStreamId |
| 192 | static const int NO_STREAM = -1; |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 193 | |
| 194 | sp<IBinder> mPreviewSurface; |
| 195 | int mPreviewStreamId; |
| 196 | camera_metadata_t *mPreviewRequest; |
| 197 | |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 198 | int mCaptureStreamId; |
| 199 | sp<CpuConsumer> mCaptureConsumer; |
| 200 | sp<ANativeWindow> mCaptureWindow; |
| 201 | sp<CaptureWaiter> mCaptureWaiter; |
Eino-Ville Talvala | 3cca136 | 2012-06-07 10:07:18 -0700 | [diff] [blame] | 202 | camera_metadata_t *mCaptureRequest; |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 203 | sp<MemoryHeapBase> mCaptureHeap; |
| 204 | sp<MemoryBase> mCaptureMemory; |
Eino-Ville Talvala | 3cca136 | 2012-06-07 10:07:18 -0700 | [diff] [blame] | 205 | |
| 206 | sp<Camera2Device> mDevice; |
| 207 | |
| 208 | |
| 209 | // Get values for static camera info entry. min/maxCount are used for error |
| 210 | // checking the number of values in the entry. 0 for max/minCount means to |
| 211 | // do no bounds check in that direction. In case of error, the entry data |
| 212 | // pointer is null and the count is 0. |
| 213 | camera_metadata_entry_t staticInfo(uint32_t tag, |
| 214 | size_t minCount=0, size_t maxCount=0); |
| 215 | |
| 216 | /** Utility methods */ |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 217 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 218 | // Convert static camera info from a camera2 device to the |
| 219 | // old API parameter map. |
| 220 | status_t buildDefaultParameters(); |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 221 | |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 222 | // Update preview request based on mParams |
| 223 | status_t updatePreviewRequest(); |
Eino-Ville Talvala | 6861a4e | 2012-06-07 10:32:12 -0700 | [diff] [blame] | 224 | |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 225 | // Update capture request based on mParams |
| 226 | status_t updateCaptureRequest(); |
| 227 | // Update capture stream based on mParams |
| 228 | status_t updateCaptureStream(); |
| 229 | |
Eino-Ville Talvala | 6861a4e | 2012-06-07 10:32:12 -0700 | [diff] [blame] | 230 | // Convert camera1 preview format string to camera2 enum |
| 231 | static int formatStringToEnum(const char *format); |
Eino-Ville Talvala | 3cca136 | 2012-06-07 10:07:18 -0700 | [diff] [blame] | 232 | static const char *formatEnumToString(int format); |
Eino-Ville Talvala | 6861a4e | 2012-06-07 10:32:12 -0700 | [diff] [blame] | 233 | |
| 234 | static int wbModeStringToEnum(const char *wbMode); |
| 235 | static int effectModeStringToEnum(const char *effectMode); |
| 236 | static int abModeStringToEnum(const char *abMode); |
| 237 | static int sceneModeStringToEnum(const char *sceneMode); |
| 238 | static Parameters::flashMode_t flashModeStringToEnum(const char *flashMode); |
| 239 | static Parameters::focusMode_t focusModeStringToEnum(const char *focusMode); |
| 240 | static status_t parseAreas(const char *areasCStr, |
| 241 | Vector<Parameters::Area> *areas); |
| 242 | static status_t validateAreas(const Vector<Parameters::Area> &areas, |
| 243 | size_t maxRegions); |
| 244 | static bool boolFromString(const char *boolStr); |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | }; // namespace android |
| 248 | |
| 249 | #endif |