blob: f813c1c746a6b051e1e3acd158e7afe3a88bc1a6 [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 Murashkin687f26c2013-02-21 14:45:03 -080030#include <utils/Condition.h>
31#include <utils/Mutex.h>
32
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080033struct camera_metadata;
34
35namespace android {
36
Igor Murashkinc0767f12013-02-20 19:29:53 -080037// All callbacks on this class are concurrent
38// (they come from separate threads)
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080039class ProCameraListener : public CameraListener
40{
41public:
42 // Lock has been acquired. Write operations now available.
43 virtual void onLockAcquired() = 0;
Igor Murashkin68c80662013-02-20 17:41:57 -080044 // Lock has been released with exclusiveUnlock.
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080045 virtual void onLockReleased() = 0;
Igor Murashkin68c80662013-02-20 17:41:57 -080046 // Lock has been stolen by another client.
47 virtual void onLockStolen() = 0;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080048
49 // Lock free.
50 virtual void onTriggerNotify(int32_t msgType, int32_t ext1, int32_t ext2)
51 = 0;
Igor Murashkinc0767f12013-02-20 19:29:53 -080052
53 // OnBufferReceived and OnRequestReceived can come in with any order,
54 // use android.sensor.timestamp and LockedBuffer.timestamp to correlate them
55
Igor Murashkinc0767f12013-02-20 19:29:53 -080056 // A new frame buffer has been received for this stream.
57 // -- This callback only fires for createStreamCpu streams
Igor Murashkin418e4932013-02-21 12:02:29 -080058 // -- Use buf.timestamp to correlate with metadata's
59 // android.sensor.timestamp
Igor Murashkinc0767f12013-02-20 19:29:53 -080060 // -- The buffer must not be accessed after this function call completes
61 virtual void onBufferReceived(int streamId,
62 const CpuConsumer::LockedBuffer& buf) = 0;
Igor Murashkin418e4932013-02-21 12:02:29 -080063 /**
64 * A new metadata buffer has been received.
65 * -- Ownership of request passes on to the callee, free with
66 * free_camera_metadata.
67 */
68 virtual void onResultReceived(int32_t frameId, camera_metadata* result) = 0;
Igor Murashkin687f26c2013-02-21 14:45:03 -080069
70
71 // A new frame buffer has been received for this stream.
72 // -- This callback only fires for createStreamCpu streams
73 // -- Use buf.timestamp to correlate with metadata's android.sensor.timestamp
74 // -- The buffer should be accessed with CpuConsumer::lockNextBuffer
75 // and CpuConsumer::unlockBuffer
76 virtual void onFrameAvailable(int streamId,
77 const sp<CpuConsumer>& cpuConsumer) {
78 }
79
80 virtual bool useOnFrameAvailable() {
81 return false;
82 }
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080083};
84
85class ProCamera : public BnProCameraCallbacks, public IBinder::DeathRecipient
86{
87public:
88 /**
89 * Connect a shared camera. By default access is restricted to read only
90 * (Lock free) operations. To be able to submit custom requests a lock needs
91 * to be acquired with exclusive[Try]Lock.
92 */
93 static sp<ProCamera> connect(int cameraId);
94 virtual void disconnect();
95 virtual ~ProCamera();
96
97 void setListener(const sp<ProCameraListener>& listener);
98
99 /**
100 * Exclusive Locks:
101 * - We may request exclusive access to a camera if no other
102 * clients are using the camera. This works as a traditional
103 * client, writing/reading any camera state.
104 * - An application opening the camera (a regular 'Camera') will
105 * always steal away the exclusive lock from a ProCamera,
106 * this will call onLockReleased.
107 * - onLockAcquired will be called again once it is possible
108 * to again exclusively lock the camera.
109 *
110 */
111
112 /**
113 * All exclusiveLock/unlock functions are asynchronous. The remote endpoint
114 * shall not block while waiting to acquire the lock. Instead the lock
115 * notifications will come in asynchronously on the listener.
116 */
117
118 /**
119 * Attempt to acquire the lock instantly (non-blocking)
120 * - If this succeeds, you do not need to wait for onLockAcquired
121 * but the event will still be fired
122 *
123 * Returns -EBUSY if already locked. 0 on success.
124 */
125 status_t exclusiveTryLock();
126 // always returns 0. wait for onLockAcquired before lock is acquired.
127 status_t exclusiveLock();
128 // release a lock if we have one, or cancel the lock request.
129 status_t exclusiveUnlock();
130
131 // exclusive lock = do whatever we want. no lock = read only.
132 bool hasExclusiveLock();
133
134 /**
135 * < 0 error, >= 0 the request ID. streaming to have the request repeat
136 * until cancelled.
137 * The request queue is flushed when a lock is released or stolen
138 * if not locked will return PERMISSION_DENIED
139 */
140 int submitRequest(const struct camera_metadata* metadata,
141 bool streaming = false);
142 // if not locked will return PERMISSION_DENIED, BAD_VALUE if requestId bad
143 status_t cancelRequest(int requestId);
144
145 /**
146 * Ask for a stream to be enabled.
147 * Lock free. Service maintains counter of streams.
148 */
149 status_t requestStream(int streamId);
Igor Murashkin94769262013-02-20 17:57:31 -0800150// TODO: remove requestStream, its useless.
151
Igor Murashkin94769262013-02-20 17:57:31 -0800152 /**
Igor Murashkinc0767f12013-02-20 19:29:53 -0800153 * Delete a stream.
154 * Lock free.
Igor Murashkin94769262013-02-20 17:57:31 -0800155 * Errors: BAD_VALUE if unknown stream ID.
Igor Murashkinc0767f12013-02-20 19:29:53 -0800156 * PERMISSION_DENIED if the stream wasn't yours
Igor Murashkin94769262013-02-20 17:57:31 -0800157 */
Igor Murashkinc0767f12013-02-20 19:29:53 -0800158 status_t deleteStream(int streamId);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800159
Igor Murashkin94769262013-02-20 17:57:31 -0800160 /**
161 * Create a new HW stream, whose sink will be the window.
162 * Lock free. Service maintains counter of streams.
163 * Errors: -EBUSY if too many streams created
164 */
165 status_t createStream(int width, int height, int format,
Igor Murashkin69e22432013-02-20 18:24:43 -0800166 const sp<Surface>& surface,
Igor Murashkin94769262013-02-20 17:57:31 -0800167 /*out*/
168 int* streamId);
169
170 /**
171 * Create a new HW stream, whose sink will be the SurfaceTexture.
172 * Lock free. Service maintains counter of streams.
173 * Errors: -EBUSY if too many streams created
174 */
175 status_t createStream(int width, int height, int format,
176 const sp<IGraphicBufferProducer>& bufferProducer,
177 /*out*/
178 int* streamId);
Igor Murashkinc0767f12013-02-20 19:29:53 -0800179 status_t createStreamCpu(int width, int height, int format,
180 int heapCount,
181 /*out*/
Igor Murashkin687f26c2013-02-21 14:45:03 -0800182 sp<CpuConsumer>* cpuConsumer,
Igor Murashkinc0767f12013-02-20 19:29:53 -0800183 int* streamId);
Igor Murashkin94769262013-02-20 17:57:31 -0800184
185 // Create a request object from a template.
186 status_t createDefaultRequest(int templateId,
187 /*out*/
188 camera_metadata** request) const;
189
190 // Get number of cameras
191 static int getNumberOfCameras();
192
193 // Get static camera metadata
Igor Murashkind127c2c2013-02-21 13:49:26 -0800194 camera_metadata* getCameraInfo(int cameraId);
Igor Murashkin94769262013-02-20 17:57:31 -0800195
Igor Murashkin687f26c2013-02-21 14:45:03 -0800196 // Blocks until a frame is available (CPU streams only)
197 // - Obtain the frame data by calling CpuConsumer::lockNextBuffer
198 // - Release the frame data after use with CpuConsumer::unlockBuffer
199 // Error codes:
200 // -ETIMEDOUT if it took too long to get a frame
201 status_t waitForFrameBuffer(int streamId);
202
203 // Blocks until a metadata result is available
204 // - Obtain the metadata by calling consumeFrameMetadata()
205 // Error codes:
206 // -ETIMEDOUT if it took too long to get a frame
207 status_t waitForFrameMetadata();
208
209 // Get the latest metadata. This is destructive.
210 // - Calling this repeatedly will produce empty metadata objects.
211 // - Use waitForFrameMetadata to sync until new data is available.
212 CameraMetadata consumeFrameMetadata();
213
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800214 sp<IProCameraUser> remote();
215
216protected:
217 ////////////////////////////////////////////////////////
218 // IProCameraCallbacks implementation
219 ////////////////////////////////////////////////////////
220 virtual void notifyCallback(int32_t msgType, int32_t ext,
221 int32_t ext2);
222 virtual void dataCallback(int32_t msgType,
223 const sp<IMemory>& dataPtr,
224 camera_frame_metadata_t *metadata);
225 virtual void dataCallbackTimestamp(nsecs_t timestamp,
226 int32_t msgType,
227 const sp<IMemory>& dataPtr);
Igor Murashkin68c80662013-02-20 17:41:57 -0800228 virtual void onLockStatusChanged(
229 IProCameraCallbacks::LockStatus newLockStatus);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800230
Igor Murashkin418e4932013-02-21 12:02:29 -0800231 virtual void onResultReceived(int32_t frameId,
232 camera_metadata* result);
233
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800234 class DeathNotifier: public IBinder::DeathRecipient
235 {
236 public:
237 DeathNotifier() {
238 }
239
240 virtual void binderDied(const wp<IBinder>& who);
241 };
242
243private:
244 ProCamera();
245
246 virtual void binderDied(const wp<IBinder>& who);
247
248 // helper function to obtain camera service handle
249 static const sp<ICameraService>& getCameraService();
250
251 static sp<DeathNotifier> mDeathNotifier;
252
253 sp<IProCameraUser> mCamera;
254 status_t mStatus;
255
256 sp<ProCameraListener> mListener;
257
258 friend class DeathNotifier;
259
260 static Mutex mLock;
261 static sp<ICameraService> mCameraService;
262
Igor Murashkinc0767f12013-02-20 19:29:53 -0800263 class ProFrameListener : public CpuConsumer::FrameAvailableListener {
264 public:
265 ProFrameListener(wp<ProCamera> camera, int streamID) {
266 mCamera = camera;
267 mStreamId = streamID;
268 }
269
270 protected:
271 virtual void onFrameAvailable() {
272 sp<ProCamera> c = mCamera.promote();
273 if (c.get() != NULL) {
274 c->onFrameAvailable(mStreamId);
275 }
276 }
277
278 private:
279 wp<ProCamera> mCamera;
280 int mStreamId;
281 };
282 friend class ProFrameListener;
283
284 struct StreamInfo
285 {
286 StreamInfo(int streamId) {
287 this->streamID = streamId;
288 cpuStream = false;
Igor Murashkin687f26c2013-02-21 14:45:03 -0800289 frameReady = false;
Igor Murashkinc0767f12013-02-20 19:29:53 -0800290 }
291
292 StreamInfo() {
293 streamID = -1;
294 cpuStream = false;
295 }
296
297 int streamID;
298 bool cpuStream;
299 sp<CpuConsumer> cpuConsumer;
300 sp<ProFrameListener> frameAvailableListener;
301 sp<Surface> stc;
Igor Murashkin687f26c2013-02-21 14:45:03 -0800302 bool frameReady;
Igor Murashkinc0767f12013-02-20 19:29:53 -0800303 };
304
Igor Murashkin687f26c2013-02-21 14:45:03 -0800305 Condition mWaitCondition;
306 Mutex mWaitMutex;
307 static const nsecs_t mWaitTimeout = 1000000000; // 1sec
Igor Murashkinc0767f12013-02-20 19:29:53 -0800308 KeyedVector<int, StreamInfo> mStreams;
Igor Murashkin687f26c2013-02-21 14:45:03 -0800309 bool mMetadataReady;
310 CameraMetadata mLatestMetadata;
Igor Murashkinc0767f12013-02-20 19:29:53 -0800311
312 void onFrameAvailable(int streamId);
313
314 StreamInfo& getStreamInfo(int streamId);
315
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800316
317};
318
319}; // namespace android
320
321#endif