Camera2Client.h revision 8ce89d9e2b132bf58a030acec88acf0a998926a1
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" 22#include "camera/CameraParameters.h" 23#include <binder/MemoryBase.h> 24#include <binder/MemoryHeapBase.h> 25#include <gui/CpuConsumer.h> 26#include "MediaConsumer.h" 27 28namespace android { 29 30/** 31 * Implements the android.hardware.camera API on top of 32 * camera device HAL version 2. 33 */ 34class Camera2Client : 35 public CameraService::Client, 36 public Camera2Device::NotificationListener, 37 public Camera2Device::FrameListener 38{ 39public: 40 // ICamera interface (see ICamera for details) 41 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); 47 virtual status_t setPreviewTexture( 48 const sp<ISurfaceTexture>& surfaceTexture); 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 66 67 Camera2Client(const sp<CameraService>& cameraService, 68 const sp<ICameraClient>& cameraClient, 69 int cameraId, 70 int cameraFacing, 71 int clientPid); 72 virtual ~Camera2Client(); 73 74 status_t initialize(camera_module_t *module); 75 76 virtual status_t dump(int fd, const Vector<String16>& args); 77 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 86 virtual void onNewFrameAvailable(); 87private: 88 enum State { 89 DISCONNECTED, 90 STOPPED, 91 WAITING_FOR_PREVIEW_WINDOW, 92 PREVIEW, 93 RECORD, 94 STILL_CAPTURE, 95 VIDEO_SNAPSHOT 96 } mState; 97 98 static const char *getStateName(State state); 99 100 /** ICamera interface-related private members */ 101 102 // Mutex that must be locked by methods implementing the ICamera interface. 103 // Ensures serialization between incoming ICamera calls. All methods below 104 // that append 'L' to the name assume that mICameraLock is locked when 105 // they're called 106 mutable Mutex mICameraLock; 107 108 // Mutex that must be locked by methods accessing the base Client's 109 // mCameraClient ICameraClient interface member, for sending notifications 110 // up to the camera user 111 mutable Mutex mICameraClientLock; 112 113 status_t setPreviewWindowL(const sp<IBinder>& binder, 114 sp<ANativeWindow> window); 115 116 void stopPreviewL(); 117 status_t startPreviewL(); 118 119 bool recordingEnabledL(); 120 121 // Individual commands for sendCommand() 122 status_t commandStartSmoothZoomL(); 123 status_t commandStopSmoothZoomL(); 124 status_t commandSetDisplayOrientationL(int degrees); 125 status_t commandEnableShutterSoundL(bool enable); 126 status_t commandPlayRecordingSoundL(); 127 status_t commandStartFaceDetectionL(int type); 128 status_t commandStopFaceDetectionL(); 129 status_t commandEnableFocusMoveMsgL(bool enable); 130 status_t commandPingL(); 131 status_t commandSetVideoBufferCountL(size_t count); 132 133 // Current camera state; this is the contents of the CameraParameters object 134 // in a more-efficient format. The enum values are mostly based off the 135 // corresponding camera2 enums, not the camera1 strings. A few are defined 136 // here if they don't cleanly map to camera2 values. 137 struct Parameters { 138 int previewWidth, previewHeight; 139 int32_t previewFpsRange[2]; 140 int previewFps; // deprecated, here only for tracking changes 141 int previewFormat; 142 143 int previewTransform; // set by CAMERA_CMD_SET_DISPLAY_ORIENTATION 144 145 int pictureWidth, pictureHeight; 146 147 int32_t jpegThumbSize[2]; 148 int32_t jpegQuality, jpegThumbQuality; 149 int32_t jpegRotation; 150 151 bool gpsEnabled; 152 double gpsCoordinates[3]; 153 int64_t gpsTimestamp; 154 String8 gpsProcessingMethod; 155 156 int wbMode; 157 int effectMode; 158 int antibandingMode; 159 int sceneMode; 160 161 enum flashMode_t { 162 FLASH_MODE_OFF = 0, 163 FLASH_MODE_AUTO, 164 FLASH_MODE_ON, 165 FLASH_MODE_TORCH, 166 FLASH_MODE_RED_EYE = ANDROID_CONTROL_AE_ON_AUTO_FLASH_REDEYE, 167 FLASH_MODE_INVALID = -1 168 } flashMode; 169 170 enum focusMode_t { 171 FOCUS_MODE_AUTO = ANDROID_CONTROL_AF_AUTO, 172 FOCUS_MODE_MACRO = ANDROID_CONTROL_AF_MACRO, 173 FOCUS_MODE_CONTINUOUS_VIDEO = ANDROID_CONTROL_AF_CONTINUOUS_VIDEO, 174 FOCUS_MODE_CONTINUOUS_PICTURE = 175 ANDROID_CONTROL_AF_CONTINUOUS_PICTURE, 176 FOCUS_MODE_EDOF = ANDROID_CONTROL_AF_EDOF, 177 FOCUS_MODE_INFINITY, 178 FOCUS_MODE_FIXED, 179 FOCUS_MODE_INVALID = -1 180 } focusMode; 181 182 struct Area { 183 int left, top, right, bottom; 184 int weight; 185 Area() {} 186 Area(int left, int top, int right, int bottom, int weight): 187 left(left), top(top), right(right), bottom(bottom), 188 weight(weight) {} 189 }; 190 Vector<Area> focusingAreas; 191 192 int32_t exposureCompensation; 193 bool autoExposureLock; 194 bool autoWhiteBalanceLock; 195 196 Vector<Area> meteringAreas; 197 198 int zoom; 199 200 int videoWidth, videoHeight; 201 202 bool recordingHint; 203 bool videoStabilization; 204 205 String8 paramsFlattened; 206 207 // These parameters are also part of the camera API-visible state, but not directly 208 // listed in Camera.Parameters 209 bool storeMetadataInBuffers; 210 bool playShutterSound; 211 bool enableFaceDetect; 212 213 bool enableFocusMoveMessages; 214 int afTriggerCounter; 215 int currentAfTriggerId; 216 bool afInMotion; 217 }; 218 219 // This class encapsulates the Parameters class so that it can only be accessed 220 // by constructing a Key object, which locks the LockedParameter's mutex. 221 class LockedParameters { 222 public: 223 class Key { 224 public: 225 Key(LockedParameters &p): 226 mParameters(p.mParameters), 227 mLockedParameters(p) { 228 mLockedParameters.mLock.lock(); 229 } 230 231 ~Key() { 232 mLockedParameters.mLock.unlock(); 233 } 234 Parameters &mParameters; 235 private: 236 // Disallow copying, default construction 237 Key(); 238 Key(const Key &); 239 Key &operator=(const Key &); 240 LockedParameters &mLockedParameters; 241 }; 242 class ReadKey { 243 public: 244 ReadKey(const LockedParameters &p): 245 mParameters(p.mParameters), 246 mLockedParameters(p) { 247 mLockedParameters.mLock.lock(); 248 } 249 250 ~ReadKey() { 251 mLockedParameters.mLock.unlock(); 252 } 253 const Parameters &mParameters; 254 private: 255 // Disallow copying, default construction 256 ReadKey(); 257 ReadKey(const ReadKey &); 258 ReadKey &operator=(const ReadKey &); 259 const LockedParameters &mLockedParameters; 260 }; 261 262 // Only use for dumping or other debugging 263 const Parameters &unsafeUnlock() { 264 return mParameters; 265 } 266 private: 267 Parameters mParameters; 268 mutable Mutex mLock; 269 270 } mParameters; 271 272 // Static device information; this is a subset of the information 273 // available through the staticInfo() method, used for frequently-accessed 274 // values or values that have to be calculated from the static information. 275 struct DeviceInfo { 276 int32_t arrayWidth; 277 int32_t arrayHeight; 278 uint8_t bestFaceDetectMode; 279 int32_t maxFaces; 280 }; 281 const DeviceInfo *mDeviceInfo; 282 283 /** Camera device-related private members */ 284 285 class Camera2Heap; 286 287 status_t updateRequests(const Parameters ¶ms); 288 289 // Number of zoom steps to simulate 290 static const unsigned int NUM_ZOOM_STEPS = 10; 291 // Used with stream IDs 292 static const int NO_STREAM = -1; 293 294 /* Output frame metadata processing methods */ 295 296 status_t processFrameFaceDetect(camera_metadata_t *frame); 297 298 /* Preview related members */ 299 300 int mPreviewStreamId; 301 camera_metadata_t *mPreviewRequest; 302 sp<IBinder> mPreviewSurface; 303 sp<ANativeWindow> mPreviewWindow; 304 305 status_t updatePreviewRequest(const Parameters ¶ms); 306 status_t updatePreviewStream(const Parameters ¶ms); 307 308 /* Still image capture related members */ 309 310 int mCaptureStreamId; 311 sp<CpuConsumer> mCaptureConsumer; 312 sp<ANativeWindow> mCaptureWindow; 313 // Simple listener that forwards frame available notifications from 314 // a CPU consumer to the capture notification 315 class CaptureWaiter: public CpuConsumer::FrameAvailableListener { 316 public: 317 CaptureWaiter(Camera2Client *parent) : mParent(parent) {} 318 void onFrameAvailable() { mParent->onCaptureAvailable(); } 319 private: 320 Camera2Client *mParent; 321 }; 322 sp<CaptureWaiter> mCaptureWaiter; 323 camera_metadata_t *mCaptureRequest; 324 sp<Camera2Heap> mCaptureHeap; 325 // Handle captured image buffers 326 void onCaptureAvailable(); 327 328 status_t updateCaptureRequest(const Parameters ¶ms); 329 status_t updateCaptureStream(const Parameters ¶ms); 330 331 /* Recording related members */ 332 333 int mRecordingStreamId; 334 sp<MediaConsumer> mRecordingConsumer; 335 sp<ANativeWindow> mRecordingWindow; 336 // Simple listener that forwards frame available notifications from 337 // a CPU consumer to the recording notification 338 class RecordingWaiter: public MediaConsumer::FrameAvailableListener { 339 public: 340 RecordingWaiter(Camera2Client *parent) : mParent(parent) {} 341 void onFrameAvailable() { mParent->onRecordingFrameAvailable(); } 342 private: 343 Camera2Client *mParent; 344 }; 345 sp<RecordingWaiter> mRecordingWaiter; 346 camera_metadata_t *mRecordingRequest; 347 sp<Camera2Heap> mRecordingHeap; 348 349 static const size_t kDefaultRecordingHeapCount = 8; 350 size_t mRecordingHeapCount; 351 size_t mRecordingHeapHead, mRecordingHeapFree; 352 // Handle new recording image buffers 353 void onRecordingFrameAvailable(); 354 355 status_t updateRecordingRequest(const Parameters ¶ms); 356 status_t updateRecordingStream(const Parameters ¶ms); 357 358 /** Notification-related members */ 359 360 bool mAfInMotion; 361 362 /** Camera2Device instance wrapping HAL2 entry */ 363 364 sp<Camera2Device> mDevice; 365 366 /** Utility members */ 367 368 // Verify that caller is the owner of the camera 369 status_t checkPid(const char *checkLocation) const; 370 371 // Utility class for managing a set of IMemory blocks 372 class Camera2Heap : public RefBase { 373 public: 374 Camera2Heap(size_t buf_size, uint_t num_buffers = 1, 375 const char *name = NULL) : 376 mBufSize(buf_size), 377 mNumBufs(num_buffers) { 378 mHeap = new MemoryHeapBase(buf_size * num_buffers, 0, name); 379 mBuffers = new sp<MemoryBase>[mNumBufs]; 380 for (uint_t i = 0; i < mNumBufs; i++) 381 mBuffers[i] = new MemoryBase(mHeap, 382 i * mBufSize, 383 mBufSize); 384 } 385 386 virtual ~Camera2Heap() 387 { 388 delete [] mBuffers; 389 } 390 391 size_t mBufSize; 392 uint_t mNumBufs; 393 sp<MemoryHeapBase> mHeap; 394 sp<MemoryBase> *mBuffers; 395 }; 396 397 // Get values for static camera info entry. min/maxCount are used for error 398 // checking the number of values in the entry. 0 for max/minCount means to 399 // do no bounds check in that direction. In case of error, the entry data 400 // pointer is null and the count is 0. 401 camera_metadata_entry_t staticInfo(uint32_t tag, 402 size_t minCount=0, size_t maxCount=0); 403 404 // Extract frequently-used camera static information into mDeviceInfo 405 status_t buildDeviceInfo(); 406 // Convert static camera info from a camera2 device to the 407 // old API parameter map. 408 status_t buildDefaultParameters(); 409 410 // Update parameters all requests use, based on mParameters 411 status_t updateRequestCommon(camera_metadata_t *request, const Parameters ¶ms); 412 413 // Map from sensor active array pixel coordinates to normalized camera parameter coordinates 414 // The former are (0,0)-(array width - 1, array height - 1), the latter from 415 // (-1000,-1000)-(1000,1000) 416 int arrayXToNormalized(int width) const; 417 int arrayYToNormalized(int height) const; 418 419 // Update specific metadata entry with new values. Adds entry if it does not 420 // exist, which will invalidate sorting 421 static status_t updateEntry(camera_metadata_t *buffer, 422 uint32_t tag, const void *data, size_t data_count); 423 424 // Remove metadata entry. Will invalidate sorting. If entry does not exist, 425 // does nothing. 426 static status_t deleteEntry(camera_metadata_t *buffer, 427 uint32_t tag); 428 429 // Convert camera1 preview format string to camera2 enum 430 static int formatStringToEnum(const char *format); 431 static const char *formatEnumToString(int format); 432 433 static int wbModeStringToEnum(const char *wbMode); 434 static int effectModeStringToEnum(const char *effectMode); 435 static int abModeStringToEnum(const char *abMode); 436 static int sceneModeStringToEnum(const char *sceneMode); 437 static Parameters::flashMode_t flashModeStringToEnum(const char *flashMode); 438 static Parameters::focusMode_t focusModeStringToEnum(const char *focusMode); 439 static status_t parseAreas(const char *areasCStr, 440 Vector<Parameters::Area> *areas); 441 static status_t validateAreas(const Vector<Parameters::Area> &areas, 442 size_t maxRegions); 443 static bool boolFromString(const char *boolStr); 444 445 // Map from camera orientation + facing to gralloc transform enum 446 static int degToTransform(int degrees, bool mirror); 447 448}; 449 450}; // namespace android 451 452#endif 453