ProCamera.h revision d127c2c8a8b68dae2f8743c310c2547e8f46d967
1/* 2 * Copyright (C) 2013 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_HARDWARE_PRO_CAMERA_H 18#define ANDROID_HARDWARE_PRO_CAMERA_H 19 20#include <utils/Timers.h> 21#include <utils/KeyedVector.h> 22#include <gui/IGraphicBufferProducer.h> 23#include <system/camera.h> 24#include <camera/IProCameraCallbacks.h> 25#include <camera/IProCameraUser.h> 26#include <camera/Camera.h> 27#include <gui/CpuConsumer.h> 28 29struct camera_metadata; 30 31namespace android { 32 33// All callbacks on this class are concurrent 34// (they come from separate threads) 35class ProCameraListener : public CameraListener 36{ 37public: 38 // Lock has been acquired. Write operations now available. 39 virtual void onLockAcquired() = 0; 40 // Lock has been released with exclusiveUnlock. 41 virtual void onLockReleased() = 0; 42 // Lock has been stolen by another client. 43 virtual void onLockStolen() = 0; 44 45 // Lock free. 46 virtual void onTriggerNotify(int32_t msgType, int32_t ext1, int32_t ext2) 47 = 0; 48 49 // OnBufferReceived and OnRequestReceived can come in with any order, 50 // use android.sensor.timestamp and LockedBuffer.timestamp to correlate them 51 52 // A new frame buffer has been received for this stream. 53 // -- This callback only fires for createStreamCpu streams 54 // -- Use buf.timestamp to correlate with metadata's 55 // android.sensor.timestamp 56 // -- The buffer must not be accessed after this function call completes 57 virtual void onBufferReceived(int streamId, 58 const CpuConsumer::LockedBuffer& buf) = 0; 59 /** 60 * A new metadata buffer has been received. 61 * -- Ownership of request passes on to the callee, free with 62 * free_camera_metadata. 63 */ 64 virtual void onResultReceived(int32_t frameId, camera_metadata* result) = 0; 65}; 66 67class ProCamera : public BnProCameraCallbacks, public IBinder::DeathRecipient 68{ 69public: 70 /** 71 * Connect a shared camera. By default access is restricted to read only 72 * (Lock free) operations. To be able to submit custom requests a lock needs 73 * to be acquired with exclusive[Try]Lock. 74 */ 75 static sp<ProCamera> connect(int cameraId); 76 virtual void disconnect(); 77 virtual ~ProCamera(); 78 79 void setListener(const sp<ProCameraListener>& listener); 80 81 /** 82 * Exclusive Locks: 83 * - We may request exclusive access to a camera if no other 84 * clients are using the camera. This works as a traditional 85 * client, writing/reading any camera state. 86 * - An application opening the camera (a regular 'Camera') will 87 * always steal away the exclusive lock from a ProCamera, 88 * this will call onLockReleased. 89 * - onLockAcquired will be called again once it is possible 90 * to again exclusively lock the camera. 91 * 92 */ 93 94 /** 95 * All exclusiveLock/unlock functions are asynchronous. The remote endpoint 96 * shall not block while waiting to acquire the lock. Instead the lock 97 * notifications will come in asynchronously on the listener. 98 */ 99 100 /** 101 * Attempt to acquire the lock instantly (non-blocking) 102 * - If this succeeds, you do not need to wait for onLockAcquired 103 * but the event will still be fired 104 * 105 * Returns -EBUSY if already locked. 0 on success. 106 */ 107 status_t exclusiveTryLock(); 108 // always returns 0. wait for onLockAcquired before lock is acquired. 109 status_t exclusiveLock(); 110 // release a lock if we have one, or cancel the lock request. 111 status_t exclusiveUnlock(); 112 113 // exclusive lock = do whatever we want. no lock = read only. 114 bool hasExclusiveLock(); 115 116 /** 117 * < 0 error, >= 0 the request ID. streaming to have the request repeat 118 * until cancelled. 119 * The request queue is flushed when a lock is released or stolen 120 * if not locked will return PERMISSION_DENIED 121 */ 122 int submitRequest(const struct camera_metadata* metadata, 123 bool streaming = false); 124 // if not locked will return PERMISSION_DENIED, BAD_VALUE if requestId bad 125 status_t cancelRequest(int requestId); 126 127 /** 128 * Ask for a stream to be enabled. 129 * Lock free. Service maintains counter of streams. 130 */ 131 status_t requestStream(int streamId); 132// TODO: remove requestStream, its useless. 133 134 /** 135 * Delete a stream. 136 * Lock free. 137 * Errors: BAD_VALUE if unknown stream ID. 138 * PERMISSION_DENIED if the stream wasn't yours 139 */ 140 status_t deleteStream(int streamId); 141 142 /** 143 * Create a new HW stream, whose sink will be the window. 144 * Lock free. Service maintains counter of streams. 145 * Errors: -EBUSY if too many streams created 146 */ 147 status_t createStream(int width, int height, int format, 148 const sp<Surface>& surface, 149 /*out*/ 150 int* streamId); 151 152 /** 153 * Create a new HW stream, whose sink will be the SurfaceTexture. 154 * Lock free. Service maintains counter of streams. 155 * Errors: -EBUSY if too many streams created 156 */ 157 status_t createStream(int width, int height, int format, 158 const sp<IGraphicBufferProducer>& bufferProducer, 159 /*out*/ 160 int* streamId); 161 status_t createStreamCpu(int width, int height, int format, 162 int heapCount, 163 /*out*/ 164 int* streamId); 165 166 // Create a request object from a template. 167 status_t createDefaultRequest(int templateId, 168 /*out*/ 169 camera_metadata** request) const; 170 171 // Get number of cameras 172 static int getNumberOfCameras(); 173 174 // Get static camera metadata 175 camera_metadata* getCameraInfo(int cameraId); 176 177 sp<IProCameraUser> remote(); 178 179protected: 180 //////////////////////////////////////////////////////// 181 // IProCameraCallbacks implementation 182 //////////////////////////////////////////////////////// 183 virtual void notifyCallback(int32_t msgType, int32_t ext, 184 int32_t ext2); 185 virtual void dataCallback(int32_t msgType, 186 const sp<IMemory>& dataPtr, 187 camera_frame_metadata_t *metadata); 188 virtual void dataCallbackTimestamp(nsecs_t timestamp, 189 int32_t msgType, 190 const sp<IMemory>& dataPtr); 191 virtual void onLockStatusChanged( 192 IProCameraCallbacks::LockStatus newLockStatus); 193 194 virtual void onResultReceived(int32_t frameId, 195 camera_metadata* result); 196 197 class DeathNotifier: public IBinder::DeathRecipient 198 { 199 public: 200 DeathNotifier() { 201 } 202 203 virtual void binderDied(const wp<IBinder>& who); 204 }; 205 206private: 207 ProCamera(); 208 209 virtual void binderDied(const wp<IBinder>& who); 210 211 // helper function to obtain camera service handle 212 static const sp<ICameraService>& getCameraService(); 213 214 static sp<DeathNotifier> mDeathNotifier; 215 216 sp<IProCameraUser> mCamera; 217 status_t mStatus; 218 219 sp<ProCameraListener> mListener; 220 221 friend class DeathNotifier; 222 223 static Mutex mLock; 224 static sp<ICameraService> mCameraService; 225 226 class ProFrameListener : public CpuConsumer::FrameAvailableListener { 227 public: 228 ProFrameListener(wp<ProCamera> camera, int streamID) { 229 mCamera = camera; 230 mStreamId = streamID; 231 } 232 233 protected: 234 virtual void onFrameAvailable() { 235 sp<ProCamera> c = mCamera.promote(); 236 if (c.get() != NULL) { 237 c->onFrameAvailable(mStreamId); 238 } 239 } 240 241 private: 242 wp<ProCamera> mCamera; 243 int mStreamId; 244 }; 245 friend class ProFrameListener; 246 247 struct StreamInfo 248 { 249 StreamInfo(int streamId) { 250 this->streamID = streamId; 251 cpuStream = false; 252 } 253 254 StreamInfo() { 255 streamID = -1; 256 cpuStream = false; 257 } 258 259 int streamID; 260 bool cpuStream; 261 sp<CpuConsumer> cpuConsumer; 262 sp<ProFrameListener> frameAvailableListener; 263 sp<Surface> stc; 264 }; 265 266 KeyedVector<int, StreamInfo> mStreams; 267 268 269 void onFrameAvailable(int streamId); 270 271 StreamInfo& getStreamInfo(int streamId); 272 273 274}; 275 276}; // namespace android 277 278#endif 279