blob: d9ee662a30edc92efed1c9acb729d4cbed9c65c9 [file] [log] [blame]
Igor Murashkin634a5152013-02-20 17:15:11 -08001/*
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 Murashkin5835cc42013-02-20 19:29:53 -080021#include <utils/KeyedVector.h>
Igor Murashkin634a5152013-02-20 17:15:11 -080022#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 Murashkina140a6e2013-02-21 14:45:03 -080027#include <camera/CameraMetadata.h>
Ruben Brunk0f61d8f2013-08-08 13:07:18 -070028#include <camera/ICameraService.h>
Igor Murashkin5835cc42013-02-20 19:29:53 -080029#include <gui/CpuConsumer.h>
Igor Murashkin634a5152013-02-20 17:15:11 -080030
Igor Murashkinc073ba52013-02-26 14:32:34 -080031#include <gui/Surface.h>
32
Igor Murashkina140a6e2013-02-21 14:45:03 -080033#include <utils/Condition.h>
34#include <utils/Mutex.h>
35
Igor Murashkinc073ba52013-02-26 14:32:34 -080036#include <camera/CameraBase.h>
37
Igor Murashkin634a5152013-02-20 17:15:11 -080038struct camera_metadata;
39
40namespace android {
41
Igor Murashkin5835cc42013-02-20 19:29:53 -080042// All callbacks on this class are concurrent
43// (they come from separate threads)
Igor Murashkinfa4cf9d2013-03-04 16:14:23 -080044class ProCameraListener : virtual public RefBase
Igor Murashkin634a5152013-02-20 17:15:11 -080045{
46public:
Igor Murashkinfa4cf9d2013-03-04 16:14:23 -080047 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2) = 0;
48
Igor Murashkin634a5152013-02-20 17:15:11 -080049 // Lock has been acquired. Write operations now available.
50 virtual void onLockAcquired() = 0;
Igor Murashkin53765732013-02-20 17:41:57 -080051 // Lock has been released with exclusiveUnlock.
Igor Murashkin634a5152013-02-20 17:15:11 -080052 virtual void onLockReleased() = 0;
Igor Murashkin53765732013-02-20 17:41:57 -080053 // Lock has been stolen by another client.
54 virtual void onLockStolen() = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -080055
56 // Lock free.
57 virtual void onTriggerNotify(int32_t msgType, int32_t ext1, int32_t ext2)
58 = 0;
Igor Murashkinfa4cf9d2013-03-04 16:14:23 -080059 // onFrameAvailable and OnResultReceived can come in with any order,
Igor Murashkin5835cc42013-02-20 19:29:53 -080060 // use android.sensor.timestamp and LockedBuffer.timestamp to correlate them
61
Igor Murashkina91537e2013-02-21 12:02:29 -080062 /**
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 Murashkina140a6e2013-02-21 14:45:03 -080068
Igor Murashkinc073ba52013-02-26 14:32:34 -080069 // TODO: make onFrameAvailable pure virtual
Igor Murashkina140a6e2013-02-21 14:45:03 -080070
71 // A new frame buffer has been received for this stream.
72 // -- This callback only fires for createStreamCpu streams
Igor Murashkinfa4cf9d2013-03-04 16:14:23 -080073 // -- A buffer may be obtained by calling cpuConsumer->lockNextBuffer
74 // -- Use buf.timestamp to correlate with result's android.sensor.timestamp
Igor Murashkina140a6e2013-02-21 14:45:03 -080075 // -- The buffer should be accessed with CpuConsumer::lockNextBuffer
76 // and CpuConsumer::unlockBuffer
Igor Murashkinc073ba52013-02-26 14:32:34 -080077 virtual void onFrameAvailable(int /*streamId*/,
78 const sp<CpuConsumer>& /*cpuConsumer*/) {
Igor Murashkina140a6e2013-02-21 14:45:03 -080079 }
80
Igor Murashkin634a5152013-02-20 17:15:11 -080081};
82
Igor Murashkinc073ba52013-02-26 14:32:34 -080083class ProCamera;
84
85template <>
86struct CameraTraits<ProCamera>
87{
88 typedef ProCameraListener TCamListener;
89 typedef IProCameraUser TCamUser;
90 typedef IProCameraCallbacks TCamCallbacks;
Ruben Brunk0f61d8f2013-08-08 13:07:18 -070091 typedef status_t (ICameraService::*TCamConnectService)(const sp<IProCameraCallbacks>&,
92 int, const String16&, int,
93 /*out*/
94 sp<IProCameraUser>&);
95 static TCamConnectService fnConnectService;
Igor Murashkinc073ba52013-02-26 14:32:34 -080096};
97
Ruben Brunk0f61d8f2013-08-08 13:07:18 -070098
Igor Murashkinc073ba52013-02-26 14:32:34 -080099class ProCamera :
100 public CameraBase<ProCamera>,
101 public BnProCameraCallbacks
Igor Murashkin634a5152013-02-20 17:15:11 -0800102{
103public:
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 Murashkin634a5152013-02-20 17:15:11 -0800110 virtual ~ProCamera();
111
Igor Murashkin634a5152013-02-20 17:15:11 -0800112 /**
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 Murashkin68506fd2013-02-20 17:57:31 -0800163// TODO: remove requestStream, its useless.
164
Igor Murashkin68506fd2013-02-20 17:57:31 -0800165 /**
Igor Murashkin5835cc42013-02-20 19:29:53 -0800166 * Delete a stream.
167 * Lock free.
Igor Murashkinbfc99152013-02-27 12:55:20 -0800168 *
169 * NOTE: As a side effect this cancels ALL streaming requests.
170 *
Igor Murashkin68506fd2013-02-20 17:57:31 -0800171 * Errors: BAD_VALUE if unknown stream ID.
Igor Murashkin5835cc42013-02-20 19:29:53 -0800172 * PERMISSION_DENIED if the stream wasn't yours
Igor Murashkin68506fd2013-02-20 17:57:31 -0800173 */
Igor Murashkin5835cc42013-02-20 19:29:53 -0800174 status_t deleteStream(int streamId);
Igor Murashkin634a5152013-02-20 17:15:11 -0800175
Igor Murashkin68506fd2013-02-20 17:57:31 -0800176 /**
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 Murashkin985fd302013-02-20 18:24:43 -0800182 const sp<Surface>& surface,
Igor Murashkin68506fd2013-02-20 17:57:31 -0800183 /*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 Murashkin5835cc42013-02-20 19:29:53 -0800195 status_t createStreamCpu(int width, int height, int format,
196 int heapCount,
197 /*out*/
Igor Murashkina140a6e2013-02-21 14:45:03 -0800198 sp<CpuConsumer>* cpuConsumer,
Igor Murashkin5835cc42013-02-20 19:29:53 -0800199 int* streamId);
Igor Murashkinba5ca4e2013-02-28 11:21:00 -0800200 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 Murashkin68506fd2013-02-20 17:57:31 -0800206
207 // Create a request object from a template.
208 status_t createDefaultRequest(int templateId,
209 /*out*/
210 camera_metadata** request) const;
211
Igor Murashkin68506fd2013-02-20 17:57:31 -0800212 // Get static camera metadata
Igor Murashkin7b33a742013-02-21 13:49:26 -0800213 camera_metadata* getCameraInfo(int cameraId);
Igor Murashkin68506fd2013-02-20 17:57:31 -0800214
Igor Murashkina140a6e2013-02-21 14:45:03 -0800215 // 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 Murashkin4bc4a382013-02-20 13:36:17 -0800218 // Return value:
219 // - >0 - number of frames available to be locked
220 // - <0 - error (refer to error codes)
Igor Murashkina140a6e2013-02-21 14:45:03 -0800221 // Error codes:
222 // -ETIMEDOUT if it took too long to get a frame
Igor Murashkin4bc4a382013-02-20 13:36:17 -0800223 int waitForFrameBuffer(int streamId);
Igor Murashkina140a6e2013-02-21 14:45:03 -0800224
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 Murashkin4bc4a382013-02-20 13:36:17 -0800236 // 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 Murashkin634a5152013-02-20 17:15:11 -0800244protected:
245 ////////////////////////////////////////////////////////
246 // IProCameraCallbacks implementation
247 ////////////////////////////////////////////////////////
Igor Murashkinfa4cf9d2013-03-04 16:14:23 -0800248 virtual void notifyCallback(int32_t msgType,
249 int32_t ext,
Igor Murashkin634a5152013-02-20 17:15:11 -0800250 int32_t ext2);
Igor Murashkinfa4cf9d2013-03-04 16:14:23 -0800251
Igor Murashkin53765732013-02-20 17:41:57 -0800252 virtual void onLockStatusChanged(
253 IProCameraCallbacks::LockStatus newLockStatus);
Igor Murashkin634a5152013-02-20 17:15:11 -0800254
Igor Murashkina91537e2013-02-21 12:02:29 -0800255 virtual void onResultReceived(int32_t frameId,
256 camera_metadata* result);
Igor Murashkin634a5152013-02-20 17:15:11 -0800257private:
Igor Murashkinc073ba52013-02-26 14:32:34 -0800258 ProCamera(int cameraId);
Igor Murashkin634a5152013-02-20 17:15:11 -0800259
Igor Murashkin5835cc42013-02-20 19:29:53 -0800260 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 Murashkin4bc4a382013-02-20 13:36:17 -0800286 frameReady = 0;
Igor Murashkin5835cc42013-02-20 19:29:53 -0800287 }
288
289 StreamInfo() {
290 streamID = -1;
291 cpuStream = false;
292 }
293
294 int streamID;
295 bool cpuStream;
296 sp<CpuConsumer> cpuConsumer;
Igor Murashkinba5ca4e2013-02-28 11:21:00 -0800297 bool synchronousMode;
Igor Murashkin5835cc42013-02-20 19:29:53 -0800298 sp<ProFrameListener> frameAvailableListener;
299 sp<Surface> stc;
Igor Murashkin4bc4a382013-02-20 13:36:17 -0800300 int frameReady;
Igor Murashkin5835cc42013-02-20 19:29:53 -0800301 };
302
Igor Murashkina140a6e2013-02-21 14:45:03 -0800303 Condition mWaitCondition;
304 Mutex mWaitMutex;
305 static const nsecs_t mWaitTimeout = 1000000000; // 1sec
Igor Murashkin5835cc42013-02-20 19:29:53 -0800306 KeyedVector<int, StreamInfo> mStreams;
Igor Murashkina140a6e2013-02-21 14:45:03 -0800307 bool mMetadataReady;
308 CameraMetadata mLatestMetadata;
Igor Murashkin5835cc42013-02-20 19:29:53 -0800309
310 void onFrameAvailable(int streamId);
311
312 StreamInfo& getStreamInfo(int streamId);
313
Igor Murashkinc073ba52013-02-26 14:32:34 -0800314 friend class CameraBase;
Igor Murashkin634a5152013-02-20 17:15:11 -0800315};
316
317}; // namespace android
318
319#endif