Camera2Client.h revision 160d4af9d933fe9461bb213e5876fc5c8c2178c2
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 : public CameraService::Client, 35 public Camera2Device::NotificationListener 36{ 37public: 38 // ICamera interface (see ICamera for details) 39 40 virtual void disconnect(); 41 virtual status_t connect(const sp<ICameraClient>& client); 42 virtual status_t lock(); 43 virtual status_t unlock(); 44 virtual status_t setPreviewDisplay(const sp<Surface>& surface); 45 virtual status_t setPreviewTexture( 46 const sp<ISurfaceTexture>& surfaceTexture); 47 virtual void setPreviewCallbackFlag(int flag); 48 virtual status_t startPreview(); 49 virtual void stopPreview(); 50 virtual bool previewEnabled(); 51 virtual status_t storeMetaDataInBuffers(bool enabled); 52 virtual status_t startRecording(); 53 virtual void stopRecording(); 54 virtual bool recordingEnabled(); 55 virtual void releaseRecordingFrame(const sp<IMemory>& mem); 56 virtual status_t autoFocus(); 57 virtual status_t cancelAutoFocus(); 58 virtual status_t takePicture(int msgType); 59 virtual status_t setParameters(const String8& params); 60 virtual String8 getParameters() const; 61 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2); 62 63 // Interface used by CameraService 64 65 Camera2Client(const sp<CameraService>& cameraService, 66 const sp<ICameraClient>& cameraClient, 67 int cameraId, 68 int cameraFacing, 69 int clientPid); 70 virtual ~Camera2Client(); 71 72 status_t initialize(camera_module_t *module); 73 74 virtual status_t dump(int fd, const Vector<String16>& args); 75 76 // Interface used by CameraDevice 77 78 virtual void notifyError(int errorCode, int arg1, int arg2); 79 virtual void notifyShutter(int frameNumber, nsecs_t timestamp); 80 virtual void notifyAutoFocus(uint8_t newState, int triggerId); 81 virtual void notifyAutoExposure(uint8_t newState, int triggerId); 82 virtual void notifyAutoWhitebalance(uint8_t newState, int triggerId); 83 84private: 85 enum State { 86 DISCONNECTED, 87 STOPPED, 88 WAITING_FOR_PREVIEW_WINDOW, 89 PREVIEW, 90 RECORD, 91 STILL_CAPTURE, 92 VIDEO_SNAPSHOT 93 } mState; 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 status_t setPreviewWindowL(const sp<IBinder>& binder, 106 sp<ANativeWindow> window); 107 108 void stopPreviewL(); 109 status_t startPreviewL(); 110 111 // Individual commands for sendCommand() 112 status_t commandStartSmoothZoomL(); 113 status_t commandStopSmoothZoomL(); 114 status_t commandSetDisplayOrientationL(int degrees); 115 status_t commandEnableShutterSoundL(bool enable); 116 status_t commandPlayRecordingSoundL(); 117 status_t commandStartFaceDetectionL(int type); 118 status_t commandStopFaceDetectionL(); 119 status_t commandEnableFocusMoveMsgL(bool enable); 120 status_t commandPingL(); 121 status_t commandSetVideoBufferCountL(size_t count); 122 123 // Current camera state; this is the contents of the CameraParameters object 124 // in a more-efficient format. The enum values are mostly based off the 125 // corresponding camera2 enums, not the camera1 strings. A few are defined 126 // here if they don't cleanly map to camera2 values. 127 struct Parameters { 128 int previewWidth, previewHeight; 129 int32_t previewFpsRange[2]; 130 int previewFps; // deprecated, here only for tracking changes 131 int previewFormat; 132 133 int previewTransform; // set by CAMERA_CMD_SET_DISPLAY_ORIENTATION 134 135 int pictureWidth, pictureHeight; 136 137 int32_t jpegThumbSize[2]; 138 int32_t jpegQuality, jpegThumbQuality; 139 int32_t jpegRotation; 140 141 bool gpsEnabled; 142 double gpsCoordinates[3]; 143 int64_t gpsTimestamp; 144 String8 gpsProcessingMethod; 145 146 int wbMode; 147 int effectMode; 148 int antibandingMode; 149 int sceneMode; 150 151 enum flashMode_t { 152 FLASH_MODE_OFF = 0, 153 FLASH_MODE_AUTO, 154 FLASH_MODE_ON, 155 FLASH_MODE_TORCH, 156 FLASH_MODE_RED_EYE = ANDROID_CONTROL_AE_ON_AUTO_FLASH_REDEYE, 157 FLASH_MODE_INVALID = -1 158 } flashMode; 159 160 enum focusMode_t { 161 FOCUS_MODE_AUTO = ANDROID_CONTROL_AF_AUTO, 162 FOCUS_MODE_MACRO = ANDROID_CONTROL_AF_MACRO, 163 FOCUS_MODE_CONTINUOUS_VIDEO = ANDROID_CONTROL_AF_CONTINUOUS_VIDEO, 164 FOCUS_MODE_CONTINUOUS_PICTURE = 165 ANDROID_CONTROL_AF_CONTINUOUS_PICTURE, 166 FOCUS_MODE_EDOF = ANDROID_CONTROL_AF_EDOF, 167 FOCUS_MODE_INFINITY, 168 FOCUS_MODE_FIXED, 169 FOCUS_MODE_INVALID = -1 170 } focusMode; 171 172 struct Area { 173 int left, top, right, bottom; 174 int weight; 175 Area() {} 176 Area(int left, int top, int right, int bottom, int weight): 177 left(left), top(top), right(right), bottom(bottom), 178 weight(weight) {} 179 }; 180 Vector<Area> focusingAreas; 181 182 int32_t exposureCompensation; 183 bool autoExposureLock; 184 bool autoWhiteBalanceLock; 185 186 Vector<Area> meteringAreas; 187 188 int zoom; 189 190 int videoWidth, videoHeight; 191 192 bool recordingHint; 193 bool videoStabilization; 194 195 String8 paramsFlattened; 196 197 // These parameters are also part of the camera API-visible state, but not directly 198 // listed in Camera.Parameters 199 bool storeMetadataInBuffers; 200 bool playShutterSound; 201 }; 202 203 class LockedParameters { 204 public: 205 class Key { 206 public: 207 Key(LockedParameters &p): 208 mParameters(p.mParameters), 209 mLockedParameters(p) { 210 mLockedParameters.mLock.lock(); 211 } 212 213 ~Key() { 214 mLockedParameters.mLock.unlock(); 215 } 216 Parameters &mParameters; 217 private: 218 // Disallow copying, default construction 219 Key(); 220 Key(const Key &); 221 Key &operator=(const Key &); 222 LockedParameters &mLockedParameters; 223 }; 224 class ReadKey { 225 public: 226 ReadKey(const LockedParameters &p): 227 mParameters(p.mParameters), 228 mLockedParameters(p) { 229 mLockedParameters.mLock.lock(); 230 } 231 232 ~ReadKey() { 233 mLockedParameters.mLock.unlock(); 234 } 235 const Parameters &mParameters; 236 private: 237 // Disallow copying, default construction 238 ReadKey(); 239 ReadKey(const ReadKey &); 240 ReadKey &operator=(const ReadKey &); 241 const LockedParameters &mLockedParameters; 242 }; 243 244 // Only use for dumping or other debugging 245 const Parameters &unsafeUnlock() { 246 return mParameters; 247 } 248 private: 249 Parameters mParameters; 250 mutable Mutex mLock; 251 252 } mParameters; 253 254 /** Camera device-related private members */ 255 256 class Camera2Heap; 257 258 // Number of zoom steps to simulate 259 static const unsigned int NUM_ZOOM_STEPS = 10; 260 // Used with stream IDs 261 static const int NO_STREAM = -1; 262 263 /* Preview related members */ 264 265 int mPreviewStreamId; 266 camera_metadata_t *mPreviewRequest; 267 sp<IBinder> mPreviewSurface; 268 sp<ANativeWindow> mPreviewWindow; 269 270 status_t updatePreviewRequest(const Parameters ¶ms); 271 status_t updatePreviewStream(const Parameters ¶ms); 272 273 /* Still image capture related members */ 274 275 int mCaptureStreamId; 276 sp<CpuConsumer> mCaptureConsumer; 277 sp<ANativeWindow> mCaptureWindow; 278 // Simple listener that forwards frame available notifications from 279 // a CPU consumer to the capture notification 280 class CaptureWaiter: public CpuConsumer::FrameAvailableListener { 281 public: 282 CaptureWaiter(Camera2Client *parent) : mParent(parent) {} 283 void onFrameAvailable() { mParent->onCaptureAvailable(); } 284 private: 285 Camera2Client *mParent; 286 }; 287 sp<CaptureWaiter> mCaptureWaiter; 288 camera_metadata_t *mCaptureRequest; 289 sp<Camera2Heap> mCaptureHeap; 290 // Handle captured image buffers 291 void onCaptureAvailable(); 292 293 status_t updateCaptureRequest(const Parameters ¶ms); 294 status_t updateCaptureStream(const Parameters ¶ms); 295 296 /* Recording related members */ 297 298 int mRecordingStreamId; 299 sp<MediaConsumer> mRecordingConsumer; 300 sp<ANativeWindow> mRecordingWindow; 301 // Simple listener that forwards frame available notifications from 302 // a CPU consumer to the recording notification 303 class RecordingWaiter: public MediaConsumer::FrameAvailableListener { 304 public: 305 RecordingWaiter(Camera2Client *parent) : mParent(parent) {} 306 void onFrameAvailable() { mParent->onRecordingFrameAvailable(); } 307 private: 308 Camera2Client *mParent; 309 }; 310 sp<RecordingWaiter> mRecordingWaiter; 311 camera_metadata_t *mRecordingRequest; 312 sp<Camera2Heap> mRecordingHeap; 313 314 // TODO: This needs to be queried from somewhere, or the BufferQueue needs 315 // to be passed all the way to stagefright. Right now, set to a large number 316 // to avoid starvation of the video encoders. 317 static const size_t kDefaultRecordingHeapCount = 8; 318 size_t mRecordingHeapCount; 319 size_t mRecordingHeapHead, mRecordingHeapFree; 320 // Handle new recording image buffers 321 void onRecordingFrameAvailable(); 322 323 status_t updateRecordingRequest(const Parameters ¶ms); 324 status_t updateRecordingStream(const Parameters ¶ms); 325 326 /** Camera2Device instance wrapping HAL2 entry */ 327 328 sp<Camera2Device> mDevice; 329 330 /** Utility members */ 331 332 // Verify that caller is the owner of the camera 333 status_t checkPid(const char *checkLocation) const; 334 335 // Utility class for managing a set of IMemory blocks 336 class Camera2Heap : public RefBase { 337 public: 338 Camera2Heap(size_t buf_size, uint_t num_buffers = 1, 339 const char *name = NULL) : 340 mBufSize(buf_size), 341 mNumBufs(num_buffers) { 342 mHeap = new MemoryHeapBase(buf_size * num_buffers, 0, name); 343 mBuffers = new sp<MemoryBase>[mNumBufs]; 344 for (uint_t i = 0; i < mNumBufs; i++) 345 mBuffers[i] = new MemoryBase(mHeap, 346 i * mBufSize, 347 mBufSize); 348 } 349 350 virtual ~Camera2Heap() 351 { 352 delete [] mBuffers; 353 } 354 355 size_t mBufSize; 356 uint_t mNumBufs; 357 sp<MemoryHeapBase> mHeap; 358 sp<MemoryBase> *mBuffers; 359 }; 360 361 // Get values for static camera info entry. min/maxCount are used for error 362 // checking the number of values in the entry. 0 for max/minCount means to 363 // do no bounds check in that direction. In case of error, the entry data 364 // pointer is null and the count is 0. 365 camera_metadata_entry_t staticInfo(uint32_t tag, 366 size_t minCount=0, size_t maxCount=0); 367 368 // Convert static camera info from a camera2 device to the 369 // old API parameter map. 370 status_t buildDefaultParameters(); 371 372 // Update parameters all requests use, based on mParameters 373 status_t updateRequestCommon(camera_metadata_t *request, const Parameters ¶ms); 374 375 // Update specific metadata entry with new values. Adds entry if it does not 376 // exist, which will invalidate sorting 377 static status_t updateEntry(camera_metadata_t *buffer, 378 uint32_t tag, const void *data, size_t data_count); 379 380 // Remove metadata entry. Will invalidate sorting. If entry does not exist, 381 // does nothing. 382 static status_t deleteEntry(camera_metadata_t *buffer, 383 uint32_t tag); 384 385 // Convert camera1 preview format string to camera2 enum 386 static int formatStringToEnum(const char *format); 387 static const char *formatEnumToString(int format); 388 389 static int wbModeStringToEnum(const char *wbMode); 390 static int effectModeStringToEnum(const char *effectMode); 391 static int abModeStringToEnum(const char *abMode); 392 static int sceneModeStringToEnum(const char *sceneMode); 393 static Parameters::flashMode_t flashModeStringToEnum(const char *flashMode); 394 static Parameters::focusMode_t focusModeStringToEnum(const char *focusMode); 395 static status_t parseAreas(const char *areasCStr, 396 Vector<Parameters::Area> *areas); 397 static status_t validateAreas(const Vector<Parameters::Area> &areas, 398 size_t maxRegions); 399 static bool boolFromString(const char *boolStr); 400 401 // Map from camera orientation + facing to gralloc transform enum 402 static int degToTransform(int degrees, bool mirror); 403 404}; 405 406}; // namespace android 407 408#endif 409