blob: b228145657b22d3c6a1162d744dd1650f6206ab9 [file] [log] [blame]
Igor Murashkinbfb5d5e2013-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 Murashkinc0767f12013-02-20 19:29:53 -080021#include <utils/KeyedVector.h>
Igor Murashkinbfb5d5e2013-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 Murashkin687f26c2013-02-21 14:45:03 -080027#include <camera/CameraMetadata.h>
Igor Murashkinc0767f12013-02-20 19:29:53 -080028#include <gui/CpuConsumer.h>
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080029
Igor Murashkinb84d9352013-02-26 14:32:34 -080030#include <gui/Surface.h>
31
Igor Murashkin687f26c2013-02-21 14:45:03 -080032#include <utils/Condition.h>
33#include <utils/Mutex.h>
34
Igor Murashkinb84d9352013-02-26 14:32:34 -080035#include <camera/CameraBase.h>
36
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080037struct camera_metadata;
38
39namespace android {
40
Igor Murashkinc0767f12013-02-20 19:29:53 -080041// All callbacks on this class are concurrent
42// (they come from separate threads)
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080043class ProCameraListener : public CameraListener
44{
45public:
46 // Lock has been acquired. Write operations now available.
47 virtual void onLockAcquired() = 0;
Igor Murashkin68c80662013-02-20 17:41:57 -080048 // Lock has been released with exclusiveUnlock.
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080049 virtual void onLockReleased() = 0;
Igor Murashkin68c80662013-02-20 17:41:57 -080050 // Lock has been stolen by another client.
51 virtual void onLockStolen() = 0;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080052
53 // Lock free.
54 virtual void onTriggerNotify(int32_t msgType, int32_t ext1, int32_t ext2)
55 = 0;
Igor Murashkinc0767f12013-02-20 19:29:53 -080056
57 // OnBufferReceived and OnRequestReceived can come in with any order,
58 // use android.sensor.timestamp and LockedBuffer.timestamp to correlate them
59
Igor Murashkinb84d9352013-02-26 14:32:34 -080060 // TODO: remove onBufferReceived
61
Igor Murashkinc0767f12013-02-20 19:29:53 -080062 // A new frame buffer has been received for this stream.
63 // -- This callback only fires for createStreamCpu streams
Igor Murashkin418e4932013-02-21 12:02:29 -080064 // -- Use buf.timestamp to correlate with metadata's
65 // android.sensor.timestamp
Igor Murashkinc0767f12013-02-20 19:29:53 -080066 // -- The buffer must not be accessed after this function call completes
67 virtual void onBufferReceived(int streamId,
68 const CpuConsumer::LockedBuffer& buf) = 0;
Igor Murashkin418e4932013-02-21 12:02:29 -080069 /**
70 * A new metadata buffer has been received.
71 * -- Ownership of request passes on to the callee, free with
72 * free_camera_metadata.
73 */
74 virtual void onResultReceived(int32_t frameId, camera_metadata* result) = 0;
Igor Murashkin687f26c2013-02-21 14:45:03 -080075
Igor Murashkinb84d9352013-02-26 14:32:34 -080076 // TODO: make onFrameAvailable pure virtual
Igor Murashkin687f26c2013-02-21 14:45:03 -080077
78 // A new frame buffer has been received for this stream.
79 // -- This callback only fires for createStreamCpu streams
80 // -- Use buf.timestamp to correlate with metadata's android.sensor.timestamp
81 // -- The buffer should be accessed with CpuConsumer::lockNextBuffer
82 // and CpuConsumer::unlockBuffer
Igor Murashkinb84d9352013-02-26 14:32:34 -080083 virtual void onFrameAvailable(int /*streamId*/,
84 const sp<CpuConsumer>& /*cpuConsumer*/) {
Igor Murashkin687f26c2013-02-21 14:45:03 -080085 }
86
Igor Murashkinb84d9352013-02-26 14:32:34 -080087 // TODO: Remove useOnFrameAvailable
Igor Murashkin687f26c2013-02-21 14:45:03 -080088 virtual bool useOnFrameAvailable() {
89 return false;
90 }
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080091};
92
Igor Murashkinb84d9352013-02-26 14:32:34 -080093class ProCamera;
94
95template <>
96struct CameraTraits<ProCamera>
97{
98 typedef ProCameraListener TCamListener;
99 typedef IProCameraUser TCamUser;
100 typedef IProCameraCallbacks TCamCallbacks;
101};
102
103class ProCamera :
104 public CameraBase<ProCamera>,
105 public BnProCameraCallbacks
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800106{
107public:
108 /**
109 * Connect a shared camera. By default access is restricted to read only
110 * (Lock free) operations. To be able to submit custom requests a lock needs
111 * to be acquired with exclusive[Try]Lock.
112 */
113 static sp<ProCamera> connect(int cameraId);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800114 virtual ~ProCamera();
115
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800116 /**
117 * Exclusive Locks:
118 * - We may request exclusive access to a camera if no other
119 * clients are using the camera. This works as a traditional
120 * client, writing/reading any camera state.
121 * - An application opening the camera (a regular 'Camera') will
122 * always steal away the exclusive lock from a ProCamera,
123 * this will call onLockReleased.
124 * - onLockAcquired will be called again once it is possible
125 * to again exclusively lock the camera.
126 *
127 */
128
129 /**
130 * All exclusiveLock/unlock functions are asynchronous. The remote endpoint
131 * shall not block while waiting to acquire the lock. Instead the lock
132 * notifications will come in asynchronously on the listener.
133 */
134
135 /**
136 * Attempt to acquire the lock instantly (non-blocking)
137 * - If this succeeds, you do not need to wait for onLockAcquired
138 * but the event will still be fired
139 *
140 * Returns -EBUSY if already locked. 0 on success.
141 */
142 status_t exclusiveTryLock();
143 // always returns 0. wait for onLockAcquired before lock is acquired.
144 status_t exclusiveLock();
145 // release a lock if we have one, or cancel the lock request.
146 status_t exclusiveUnlock();
147
148 // exclusive lock = do whatever we want. no lock = read only.
149 bool hasExclusiveLock();
150
151 /**
152 * < 0 error, >= 0 the request ID. streaming to have the request repeat
153 * until cancelled.
154 * The request queue is flushed when a lock is released or stolen
155 * if not locked will return PERMISSION_DENIED
156 */
157 int submitRequest(const struct camera_metadata* metadata,
158 bool streaming = false);
159 // if not locked will return PERMISSION_DENIED, BAD_VALUE if requestId bad
160 status_t cancelRequest(int requestId);
161
162 /**
163 * Ask for a stream to be enabled.
164 * Lock free. Service maintains counter of streams.
165 */
166 status_t requestStream(int streamId);
Igor Murashkin94769262013-02-20 17:57:31 -0800167// TODO: remove requestStream, its useless.
168
Igor Murashkin94769262013-02-20 17:57:31 -0800169 /**
Igor Murashkinc0767f12013-02-20 19:29:53 -0800170 * Delete a stream.
171 * Lock free.
Igor Murashkin94769262013-02-20 17:57:31 -0800172 * Errors: BAD_VALUE if unknown stream ID.
Igor Murashkinc0767f12013-02-20 19:29:53 -0800173 * PERMISSION_DENIED if the stream wasn't yours
Igor Murashkin94769262013-02-20 17:57:31 -0800174 */
Igor Murashkinc0767f12013-02-20 19:29:53 -0800175 status_t deleteStream(int streamId);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800176
Igor Murashkin94769262013-02-20 17:57:31 -0800177 /**
178 * Create a new HW stream, whose sink will be the window.
179 * Lock free. Service maintains counter of streams.
180 * Errors: -EBUSY if too many streams created
181 */
182 status_t createStream(int width, int height, int format,
Igor Murashkin69e22432013-02-20 18:24:43 -0800183 const sp<Surface>& surface,
Igor Murashkin94769262013-02-20 17:57:31 -0800184 /*out*/
185 int* streamId);
186
187 /**
188 * Create a new HW stream, whose sink will be the SurfaceTexture.
189 * Lock free. Service maintains counter of streams.
190 * Errors: -EBUSY if too many streams created
191 */
192 status_t createStream(int width, int height, int format,
193 const sp<IGraphicBufferProducer>& bufferProducer,
194 /*out*/
195 int* streamId);
Igor Murashkinc0767f12013-02-20 19:29:53 -0800196 status_t createStreamCpu(int width, int height, int format,
197 int heapCount,
198 /*out*/
Igor Murashkin687f26c2013-02-21 14:45:03 -0800199 sp<CpuConsumer>* cpuConsumer,
Igor Murashkinc0767f12013-02-20 19:29:53 -0800200 int* streamId);
Igor Murashkin94769262013-02-20 17:57:31 -0800201
202 // Create a request object from a template.
203 status_t createDefaultRequest(int templateId,
204 /*out*/
205 camera_metadata** request) const;
206
Igor Murashkin94769262013-02-20 17:57:31 -0800207 // Get static camera metadata
Igor Murashkind127c2c2013-02-21 13:49:26 -0800208 camera_metadata* getCameraInfo(int cameraId);
Igor Murashkin94769262013-02-20 17:57:31 -0800209
Igor Murashkin687f26c2013-02-21 14:45:03 -0800210 // Blocks until a frame is available (CPU streams only)
211 // - Obtain the frame data by calling CpuConsumer::lockNextBuffer
212 // - Release the frame data after use with CpuConsumer::unlockBuffer
Igor Murashkin94a90a42013-02-20 13:36:17 -0800213 // Return value:
214 // - >0 - number of frames available to be locked
215 // - <0 - error (refer to error codes)
Igor Murashkin687f26c2013-02-21 14:45:03 -0800216 // Error codes:
217 // -ETIMEDOUT if it took too long to get a frame
Igor Murashkin94a90a42013-02-20 13:36:17 -0800218 int waitForFrameBuffer(int streamId);
Igor Murashkin687f26c2013-02-21 14:45:03 -0800219
220 // Blocks until a metadata result is available
221 // - Obtain the metadata by calling consumeFrameMetadata()
222 // Error codes:
223 // -ETIMEDOUT if it took too long to get a frame
224 status_t waitForFrameMetadata();
225
226 // Get the latest metadata. This is destructive.
227 // - Calling this repeatedly will produce empty metadata objects.
228 // - Use waitForFrameMetadata to sync until new data is available.
229 CameraMetadata consumeFrameMetadata();
230
Igor Murashkin94a90a42013-02-20 13:36:17 -0800231 // Convenience method to drop frame buffers (CPU streams only)
232 // Return values:
233 // >=0 - number of frames dropped (up to count)
234 // <0 - error code
235 // Error codes:
236 // BAD_VALUE - invalid streamId or count passed
237 int dropFrameBuffer(int streamId, int count);
238
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800239protected:
240 ////////////////////////////////////////////////////////
241 // IProCameraCallbacks implementation
242 ////////////////////////////////////////////////////////
243 virtual void notifyCallback(int32_t msgType, int32_t ext,
244 int32_t ext2);
245 virtual void dataCallback(int32_t msgType,
246 const sp<IMemory>& dataPtr,
247 camera_frame_metadata_t *metadata);
248 virtual void dataCallbackTimestamp(nsecs_t timestamp,
249 int32_t msgType,
250 const sp<IMemory>& dataPtr);
Igor Murashkin68c80662013-02-20 17:41:57 -0800251 virtual void onLockStatusChanged(
252 IProCameraCallbacks::LockStatus newLockStatus);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800253
Igor Murashkin418e4932013-02-21 12:02:29 -0800254 virtual void onResultReceived(int32_t frameId,
255 camera_metadata* result);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800256private:
Igor Murashkinb84d9352013-02-26 14:32:34 -0800257 ProCamera(int cameraId);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800258
Igor Murashkinc0767f12013-02-20 19:29:53 -0800259 class ProFrameListener : public CpuConsumer::FrameAvailableListener {
260 public:
261 ProFrameListener(wp<ProCamera> camera, int streamID) {
262 mCamera = camera;
263 mStreamId = streamID;
264 }
265
266 protected:
267 virtual void onFrameAvailable() {
268 sp<ProCamera> c = mCamera.promote();
269 if (c.get() != NULL) {
270 c->onFrameAvailable(mStreamId);
271 }
272 }
273
274 private:
275 wp<ProCamera> mCamera;
276 int mStreamId;
277 };
278 friend class ProFrameListener;
279
280 struct StreamInfo
281 {
282 StreamInfo(int streamId) {
283 this->streamID = streamId;
284 cpuStream = false;
Igor Murashkin94a90a42013-02-20 13:36:17 -0800285 frameReady = 0;
Igor Murashkinc0767f12013-02-20 19:29:53 -0800286 }
287
288 StreamInfo() {
289 streamID = -1;
290 cpuStream = false;
291 }
292
293 int streamID;
294 bool cpuStream;
295 sp<CpuConsumer> cpuConsumer;
296 sp<ProFrameListener> frameAvailableListener;
297 sp<Surface> stc;
Igor Murashkin94a90a42013-02-20 13:36:17 -0800298 int frameReady;
Igor Murashkinc0767f12013-02-20 19:29:53 -0800299 };
300
Igor Murashkin687f26c2013-02-21 14:45:03 -0800301 Condition mWaitCondition;
302 Mutex mWaitMutex;
303 static const nsecs_t mWaitTimeout = 1000000000; // 1sec
Igor Murashkinc0767f12013-02-20 19:29:53 -0800304 KeyedVector<int, StreamInfo> mStreams;
Igor Murashkin687f26c2013-02-21 14:45:03 -0800305 bool mMetadataReady;
306 CameraMetadata mLatestMetadata;
Igor Murashkinc0767f12013-02-20 19:29:53 -0800307
308 void onFrameAvailable(int streamId);
309
310 StreamInfo& getStreamInfo(int streamId);
311
Igor Murashkinb84d9352013-02-26 14:32:34 -0800312 friend class CameraBase;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800313};
314
315}; // namespace android
316
317#endif