Camera2Client.h revision 2e19c3c02957208371cdd491e6342ea7ddb440d9
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 "camera2/Parameters.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 /** ICamera interface-related private members */ 87 88 // Mutex that must be locked by methods implementing the ICamera interface. 89 // Ensures serialization between incoming ICamera calls. All methods below 90 // that append 'L' to the name assume that mICameraLock is locked when 91 // they're called 92 mutable Mutex mICameraLock; 93 94 // Mutex that must be locked by methods accessing the base Client's 95 // mCameraClient ICameraClient interface member, for sending notifications 96 // up to the camera user 97 mutable Mutex mICameraClientLock; 98 99 typedef camera2::Parameters Parameters; 100 typedef camera2::CameraMetadata CameraMetadata; 101 102 status_t setPreviewWindowL(const sp<IBinder>& binder, 103 sp<ANativeWindow> window); 104 status_t startPreviewL(Parameters ¶ms, bool restart); 105 void stopPreviewL(); 106 status_t startRecordingL(Parameters ¶ms, bool restart); 107 bool recordingEnabledL(); 108 109 // Individual commands for sendCommand() 110 status_t commandStartSmoothZoomL(); 111 status_t commandStopSmoothZoomL(); 112 status_t commandSetDisplayOrientationL(int degrees); 113 status_t commandEnableShutterSoundL(bool enable); 114 status_t commandPlayRecordingSoundL(); 115 status_t commandStartFaceDetectionL(int type); 116 status_t commandStopFaceDetectionL(Parameters ¶ms); 117 status_t commandEnableFocusMoveMsgL(bool enable); 118 status_t commandPingL(); 119 status_t commandSetVideoBufferCountL(size_t count); 120 121 // Current camera device configuration 122 camera2::SharedParameters mParameters; 123 124 /** Camera device-related private members */ 125 126 class Camera2Heap; 127 128 void setPreviewCallbackFlagL(Parameters ¶ms, int flag); 129 status_t updateRequests(const Parameters ¶ms); 130 131 // Used with stream IDs 132 static const int NO_STREAM = -1; 133 134 /* Output frame metadata processing thread. This thread waits for new 135 * frames from the device, and analyzes them as necessary. 136 */ 137 class FrameProcessor: public Thread { 138 public: 139 FrameProcessor(wp<Camera2Client> client); 140 ~FrameProcessor(); 141 142 void dump(int fd, const Vector<String16>& args); 143 private: 144 static const nsecs_t kWaitDuration = 10000000; // 10 ms 145 wp<Camera2Client> mClient; 146 147 virtual bool threadLoop(); 148 149 void processNewFrames(sp<Camera2Client> &client); 150 status_t processFaceDetect(const CameraMetadata &frame, 151 sp<Camera2Client> &client); 152 153 CameraMetadata mLastFrame; 154 }; 155 156 sp<FrameProcessor> mFrameProcessor; 157 158 /* Preview related members */ 159 160 int mPreviewStreamId; 161 CameraMetadata mPreviewRequest; 162 sp<IBinder> mPreviewSurface; 163 sp<ANativeWindow> mPreviewWindow; 164 165 status_t updatePreviewRequest(const Parameters ¶ms); 166 status_t updatePreviewStream(const Parameters ¶ms); 167 168 /** Preview callback related members */ 169 170 int mCallbackStreamId; 171 static const size_t kCallbackHeapCount = 6; 172 sp<CpuConsumer> mCallbackConsumer; 173 sp<ANativeWindow> mCallbackWindow; 174 // Simple listener that forwards frame available notifications from 175 // a CPU consumer to the callback notification 176 class CallbackWaiter: public CpuConsumer::FrameAvailableListener { 177 public: 178 CallbackWaiter(Camera2Client *parent) : mParent(parent) {} 179 void onFrameAvailable() { mParent->onCallbackAvailable(); } 180 private: 181 Camera2Client *mParent; 182 }; 183 sp<CallbackWaiter> mCallbackWaiter; 184 sp<Camera2Heap> mCallbackHeap; 185 int mCallbackHeapId; 186 size_t mCallbackHeapHead, mCallbackHeapFree; 187 // Handle callback image buffers 188 void onCallbackAvailable(); 189 190 status_t updateCallbackStream(const Parameters ¶ms); 191 192 /* Still image capture related members */ 193 194 int mCaptureStreamId; 195 sp<CpuConsumer> mCaptureConsumer; 196 sp<ANativeWindow> mCaptureWindow; 197 // Simple listener that forwards frame available notifications from 198 // a CPU consumer to the capture notification 199 class CaptureWaiter: public CpuConsumer::FrameAvailableListener { 200 public: 201 CaptureWaiter(Camera2Client *parent) : mParent(parent) {} 202 void onFrameAvailable() { mParent->onCaptureAvailable(); } 203 private: 204 Camera2Client *mParent; 205 }; 206 sp<CaptureWaiter> mCaptureWaiter; 207 CameraMetadata mCaptureRequest; 208 sp<Camera2Heap> mCaptureHeap; 209 // Handle captured image buffers 210 void onCaptureAvailable(); 211 212 status_t updateCaptureRequest(const Parameters ¶ms); 213 status_t updateCaptureStream(const Parameters ¶ms); 214 215 /* Recording related members */ 216 217 int mRecordingStreamId; 218 int mRecordingFrameCount; 219 sp<BufferItemConsumer> mRecordingConsumer; 220 sp<ANativeWindow> mRecordingWindow; 221 // Simple listener that forwards frame available notifications from 222 // a CPU consumer to the recording notification 223 class RecordingWaiter: public BufferItemConsumer::FrameAvailableListener { 224 public: 225 RecordingWaiter(Camera2Client *parent) : mParent(parent) {} 226 void onFrameAvailable() { mParent->onRecordingFrameAvailable(); } 227 private: 228 Camera2Client *mParent; 229 }; 230 sp<RecordingWaiter> mRecordingWaiter; 231 CameraMetadata mRecordingRequest; 232 sp<Camera2Heap> mRecordingHeap; 233 234 static const size_t kDefaultRecordingHeapCount = 8; 235 size_t mRecordingHeapCount; 236 Vector<BufferItemConsumer::BufferItem> mRecordingBuffers; 237 size_t mRecordingHeapHead, mRecordingHeapFree; 238 // Handle new recording image buffers 239 void onRecordingFrameAvailable(); 240 241 status_t updateRecordingRequest(const Parameters ¶ms); 242 status_t updateRecordingStream(const Parameters ¶ms); 243 244 /** Notification-related members */ 245 246 bool mAfInMotion; 247 248 /** Camera2Device instance wrapping HAL2 entry */ 249 250 sp<Camera2Device> mDevice; 251 252 /** Utility members */ 253 254 // Verify that caller is the owner of the camera 255 status_t checkPid(const char *checkLocation) const; 256 257 // Utility class for managing a set of IMemory blocks 258 class Camera2Heap : public RefBase { 259 public: 260 Camera2Heap(size_t buf_size, uint_t num_buffers = 1, 261 const char *name = NULL) : 262 mBufSize(buf_size), 263 mNumBufs(num_buffers) { 264 mHeap = new MemoryHeapBase(buf_size * num_buffers, 0, name); 265 mBuffers = new sp<MemoryBase>[mNumBufs]; 266 for (uint_t i = 0; i < mNumBufs; i++) 267 mBuffers[i] = new MemoryBase(mHeap, 268 i * mBufSize, 269 mBufSize); 270 } 271 272 virtual ~Camera2Heap() 273 { 274 delete [] mBuffers; 275 } 276 277 size_t mBufSize; 278 uint_t mNumBufs; 279 sp<MemoryHeapBase> mHeap; 280 sp<MemoryBase> *mBuffers; 281 }; 282 283 // Update parameters all requests use, based on mParameters 284 status_t updateRequestCommon(CameraMetadata *request, const Parameters ¶ms) const; 285 286 // Map from sensor active array pixel coordinates to normalized camera 287 // parameter coordinates. The former are (0,0)-(array width - 1, array height 288 // - 1), the latter from (-1000,-1000)-(1000,1000) 289 int normalizedXToArray(int x) const; 290 int normalizedYToArray(int y) const; 291 int arrayXToNormalized(int width) const; 292 int arrayYToNormalized(int height) const; 293 294 295 static size_t calculateBufferSize(int width, int height, 296 int format, int stride); 297}; 298 299}; // namespace android 300 301#endif 302