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