Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 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> |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 21 | #include <utils/KeyedVector.h> |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 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> |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 27 | #include <camera/CameraMetadata.h> |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame^] | 28 | #include <camera/ICameraService.h> |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 29 | #include <gui/CpuConsumer.h> |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 30 | |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 31 | #include <gui/Surface.h> |
| 32 | |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 33 | #include <utils/Condition.h> |
| 34 | #include <utils/Mutex.h> |
| 35 | |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 36 | #include <camera/CameraBase.h> |
| 37 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 38 | struct camera_metadata; |
| 39 | |
| 40 | namespace android { |
| 41 | |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 42 | // All callbacks on this class are concurrent |
| 43 | // (they come from separate threads) |
Igor Murashkin | fa4cf9d | 2013-03-04 16:14:23 -0800 | [diff] [blame] | 44 | class ProCameraListener : virtual public RefBase |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 45 | { |
| 46 | public: |
Igor Murashkin | fa4cf9d | 2013-03-04 16:14:23 -0800 | [diff] [blame] | 47 | virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2) = 0; |
| 48 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 49 | // Lock has been acquired. Write operations now available. |
| 50 | virtual void onLockAcquired() = 0; |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 51 | // Lock has been released with exclusiveUnlock. |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 52 | virtual void onLockReleased() = 0; |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 53 | // Lock has been stolen by another client. |
| 54 | virtual void onLockStolen() = 0; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 55 | |
| 56 | // Lock free. |
| 57 | virtual void onTriggerNotify(int32_t msgType, int32_t ext1, int32_t ext2) |
| 58 | = 0; |
Igor Murashkin | fa4cf9d | 2013-03-04 16:14:23 -0800 | [diff] [blame] | 59 | // onFrameAvailable and OnResultReceived can come in with any order, |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 60 | // use android.sensor.timestamp and LockedBuffer.timestamp to correlate them |
| 61 | |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 62 | /** |
| 63 | * A new metadata buffer has been received. |
| 64 | * -- Ownership of request passes on to the callee, free with |
| 65 | * free_camera_metadata. |
| 66 | */ |
| 67 | virtual void onResultReceived(int32_t frameId, camera_metadata* result) = 0; |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 68 | |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 69 | // TODO: make onFrameAvailable pure virtual |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 70 | |
| 71 | // A new frame buffer has been received for this stream. |
| 72 | // -- This callback only fires for createStreamCpu streams |
Igor Murashkin | fa4cf9d | 2013-03-04 16:14:23 -0800 | [diff] [blame] | 73 | // -- A buffer may be obtained by calling cpuConsumer->lockNextBuffer |
| 74 | // -- Use buf.timestamp to correlate with result's android.sensor.timestamp |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 75 | // -- The buffer should be accessed with CpuConsumer::lockNextBuffer |
| 76 | // and CpuConsumer::unlockBuffer |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 77 | virtual void onFrameAvailable(int /*streamId*/, |
| 78 | const sp<CpuConsumer>& /*cpuConsumer*/) { |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 83 | class ProCamera; |
| 84 | |
| 85 | template <> |
| 86 | struct CameraTraits<ProCamera> |
| 87 | { |
| 88 | typedef ProCameraListener TCamListener; |
| 89 | typedef IProCameraUser TCamUser; |
| 90 | typedef IProCameraCallbacks TCamCallbacks; |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame^] | 91 | typedef status_t (ICameraService::*TCamConnectService)(const sp<IProCameraCallbacks>&, |
| 92 | int, const String16&, int, |
| 93 | /*out*/ |
| 94 | sp<IProCameraUser>&); |
| 95 | static TCamConnectService fnConnectService; |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 96 | }; |
| 97 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame^] | 98 | |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 99 | class ProCamera : |
| 100 | public CameraBase<ProCamera>, |
| 101 | public BnProCameraCallbacks |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 102 | { |
| 103 | public: |
| 104 | /** |
| 105 | * Connect a shared camera. By default access is restricted to read only |
| 106 | * (Lock free) operations. To be able to submit custom requests a lock needs |
| 107 | * to be acquired with exclusive[Try]Lock. |
| 108 | */ |
| 109 | static sp<ProCamera> connect(int cameraId); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 110 | virtual ~ProCamera(); |
| 111 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 112 | /** |
| 113 | * Exclusive Locks: |
| 114 | * - We may request exclusive access to a camera if no other |
| 115 | * clients are using the camera. This works as a traditional |
| 116 | * client, writing/reading any camera state. |
| 117 | * - An application opening the camera (a regular 'Camera') will |
| 118 | * always steal away the exclusive lock from a ProCamera, |
| 119 | * this will call onLockReleased. |
| 120 | * - onLockAcquired will be called again once it is possible |
| 121 | * to again exclusively lock the camera. |
| 122 | * |
| 123 | */ |
| 124 | |
| 125 | /** |
| 126 | * All exclusiveLock/unlock functions are asynchronous. The remote endpoint |
| 127 | * shall not block while waiting to acquire the lock. Instead the lock |
| 128 | * notifications will come in asynchronously on the listener. |
| 129 | */ |
| 130 | |
| 131 | /** |
| 132 | * Attempt to acquire the lock instantly (non-blocking) |
| 133 | * - If this succeeds, you do not need to wait for onLockAcquired |
| 134 | * but the event will still be fired |
| 135 | * |
| 136 | * Returns -EBUSY if already locked. 0 on success. |
| 137 | */ |
| 138 | status_t exclusiveTryLock(); |
| 139 | // always returns 0. wait for onLockAcquired before lock is acquired. |
| 140 | status_t exclusiveLock(); |
| 141 | // release a lock if we have one, or cancel the lock request. |
| 142 | status_t exclusiveUnlock(); |
| 143 | |
| 144 | // exclusive lock = do whatever we want. no lock = read only. |
| 145 | bool hasExclusiveLock(); |
| 146 | |
| 147 | /** |
| 148 | * < 0 error, >= 0 the request ID. streaming to have the request repeat |
| 149 | * until cancelled. |
| 150 | * The request queue is flushed when a lock is released or stolen |
| 151 | * if not locked will return PERMISSION_DENIED |
| 152 | */ |
| 153 | int submitRequest(const struct camera_metadata* metadata, |
| 154 | bool streaming = false); |
| 155 | // if not locked will return PERMISSION_DENIED, BAD_VALUE if requestId bad |
| 156 | status_t cancelRequest(int requestId); |
| 157 | |
| 158 | /** |
| 159 | * Ask for a stream to be enabled. |
| 160 | * Lock free. Service maintains counter of streams. |
| 161 | */ |
| 162 | status_t requestStream(int streamId); |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 163 | // TODO: remove requestStream, its useless. |
| 164 | |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 165 | /** |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 166 | * Delete a stream. |
| 167 | * Lock free. |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 168 | * |
| 169 | * NOTE: As a side effect this cancels ALL streaming requests. |
| 170 | * |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 171 | * Errors: BAD_VALUE if unknown stream ID. |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 172 | * PERMISSION_DENIED if the stream wasn't yours |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 173 | */ |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 174 | status_t deleteStream(int streamId); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 175 | |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 176 | /** |
| 177 | * Create a new HW stream, whose sink will be the window. |
| 178 | * Lock free. Service maintains counter of streams. |
| 179 | * Errors: -EBUSY if too many streams created |
| 180 | */ |
| 181 | status_t createStream(int width, int height, int format, |
Igor Murashkin | 985fd30 | 2013-02-20 18:24:43 -0800 | [diff] [blame] | 182 | const sp<Surface>& surface, |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 183 | /*out*/ |
| 184 | int* streamId); |
| 185 | |
| 186 | /** |
| 187 | * Create a new HW stream, whose sink will be the SurfaceTexture. |
| 188 | * Lock free. Service maintains counter of streams. |
| 189 | * Errors: -EBUSY if too many streams created |
| 190 | */ |
| 191 | status_t createStream(int width, int height, int format, |
| 192 | const sp<IGraphicBufferProducer>& bufferProducer, |
| 193 | /*out*/ |
| 194 | int* streamId); |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 195 | status_t createStreamCpu(int width, int height, int format, |
| 196 | int heapCount, |
| 197 | /*out*/ |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 198 | sp<CpuConsumer>* cpuConsumer, |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 199 | int* streamId); |
Igor Murashkin | ba5ca4e | 2013-02-28 11:21:00 -0800 | [diff] [blame] | 200 | status_t createStreamCpu(int width, int height, int format, |
| 201 | int heapCount, |
| 202 | bool synchronousMode, |
| 203 | /*out*/ |
| 204 | sp<CpuConsumer>* cpuConsumer, |
| 205 | int* streamId); |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 206 | |
| 207 | // Create a request object from a template. |
| 208 | status_t createDefaultRequest(int templateId, |
| 209 | /*out*/ |
| 210 | camera_metadata** request) const; |
| 211 | |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 212 | // Get static camera metadata |
Igor Murashkin | 7b33a74 | 2013-02-21 13:49:26 -0800 | [diff] [blame] | 213 | camera_metadata* getCameraInfo(int cameraId); |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 214 | |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 215 | // Blocks until a frame is available (CPU streams only) |
| 216 | // - Obtain the frame data by calling CpuConsumer::lockNextBuffer |
| 217 | // - Release the frame data after use with CpuConsumer::unlockBuffer |
Igor Murashkin | 4bc4a38 | 2013-02-20 13:36:17 -0800 | [diff] [blame] | 218 | // Return value: |
| 219 | // - >0 - number of frames available to be locked |
| 220 | // - <0 - error (refer to error codes) |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 221 | // Error codes: |
| 222 | // -ETIMEDOUT if it took too long to get a frame |
Igor Murashkin | 4bc4a38 | 2013-02-20 13:36:17 -0800 | [diff] [blame] | 223 | int waitForFrameBuffer(int streamId); |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 224 | |
| 225 | // Blocks until a metadata result is available |
| 226 | // - Obtain the metadata by calling consumeFrameMetadata() |
| 227 | // Error codes: |
| 228 | // -ETIMEDOUT if it took too long to get a frame |
| 229 | status_t waitForFrameMetadata(); |
| 230 | |
| 231 | // Get the latest metadata. This is destructive. |
| 232 | // - Calling this repeatedly will produce empty metadata objects. |
| 233 | // - Use waitForFrameMetadata to sync until new data is available. |
| 234 | CameraMetadata consumeFrameMetadata(); |
| 235 | |
Igor Murashkin | 4bc4a38 | 2013-02-20 13:36:17 -0800 | [diff] [blame] | 236 | // Convenience method to drop frame buffers (CPU streams only) |
| 237 | // Return values: |
| 238 | // >=0 - number of frames dropped (up to count) |
| 239 | // <0 - error code |
| 240 | // Error codes: |
| 241 | // BAD_VALUE - invalid streamId or count passed |
| 242 | int dropFrameBuffer(int streamId, int count); |
| 243 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 244 | protected: |
| 245 | //////////////////////////////////////////////////////// |
| 246 | // IProCameraCallbacks implementation |
| 247 | //////////////////////////////////////////////////////// |
Igor Murashkin | fa4cf9d | 2013-03-04 16:14:23 -0800 | [diff] [blame] | 248 | virtual void notifyCallback(int32_t msgType, |
| 249 | int32_t ext, |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 250 | int32_t ext2); |
Igor Murashkin | fa4cf9d | 2013-03-04 16:14:23 -0800 | [diff] [blame] | 251 | |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 252 | virtual void onLockStatusChanged( |
| 253 | IProCameraCallbacks::LockStatus newLockStatus); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 254 | |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 255 | virtual void onResultReceived(int32_t frameId, |
| 256 | camera_metadata* result); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 257 | private: |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 258 | ProCamera(int cameraId); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 259 | |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 260 | class ProFrameListener : public CpuConsumer::FrameAvailableListener { |
| 261 | public: |
| 262 | ProFrameListener(wp<ProCamera> camera, int streamID) { |
| 263 | mCamera = camera; |
| 264 | mStreamId = streamID; |
| 265 | } |
| 266 | |
| 267 | protected: |
| 268 | virtual void onFrameAvailable() { |
| 269 | sp<ProCamera> c = mCamera.promote(); |
| 270 | if (c.get() != NULL) { |
| 271 | c->onFrameAvailable(mStreamId); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | private: |
| 276 | wp<ProCamera> mCamera; |
| 277 | int mStreamId; |
| 278 | }; |
| 279 | friend class ProFrameListener; |
| 280 | |
| 281 | struct StreamInfo |
| 282 | { |
| 283 | StreamInfo(int streamId) { |
| 284 | this->streamID = streamId; |
| 285 | cpuStream = false; |
Igor Murashkin | 4bc4a38 | 2013-02-20 13:36:17 -0800 | [diff] [blame] | 286 | frameReady = 0; |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | StreamInfo() { |
| 290 | streamID = -1; |
| 291 | cpuStream = false; |
| 292 | } |
| 293 | |
| 294 | int streamID; |
| 295 | bool cpuStream; |
| 296 | sp<CpuConsumer> cpuConsumer; |
Igor Murashkin | ba5ca4e | 2013-02-28 11:21:00 -0800 | [diff] [blame] | 297 | bool synchronousMode; |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 298 | sp<ProFrameListener> frameAvailableListener; |
| 299 | sp<Surface> stc; |
Igor Murashkin | 4bc4a38 | 2013-02-20 13:36:17 -0800 | [diff] [blame] | 300 | int frameReady; |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 301 | }; |
| 302 | |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 303 | Condition mWaitCondition; |
| 304 | Mutex mWaitMutex; |
| 305 | static const nsecs_t mWaitTimeout = 1000000000; // 1sec |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 306 | KeyedVector<int, StreamInfo> mStreams; |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 307 | bool mMetadataReady; |
| 308 | CameraMetadata mLatestMetadata; |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 309 | |
| 310 | void onFrameAvailable(int streamId); |
| 311 | |
| 312 | StreamInfo& getStreamInfo(int streamId); |
| 313 | |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 314 | friend class CameraBase; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 315 | }; |
| 316 | |
| 317 | }; // namespace android |
| 318 | |
| 319 | #endif |