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