ProCamera.h revision a140a6efea1db7837984b3578755cfa4eaa8d92d
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>
21#include <utils/KeyedVector.h>
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>
27#include <camera/CameraMetadata.h>
28#include <gui/CpuConsumer.h>
29
30#include <utils/Condition.h>
31#include <utils/Mutex.h>
32
33struct camera_metadata;
34
35namespace android {
36
37// All callbacks on this class are concurrent
38// (they come from separate threads)
39class ProCameraListener : public CameraListener
40{
41public:
42    // Lock has been acquired. Write operations now available.
43    virtual void onLockAcquired() = 0;
44    // Lock has been released with exclusiveUnlock.
45    virtual void onLockReleased() = 0;
46    // Lock has been stolen by another client.
47    virtual void onLockStolen() = 0;
48
49    // Lock free.
50    virtual void onTriggerNotify(int32_t msgType, int32_t ext1, int32_t ext2)
51                                                                            = 0;
52
53    // OnBufferReceived and OnRequestReceived can come in with any order,
54    // use android.sensor.timestamp and LockedBuffer.timestamp to correlate them
55
56    // A new frame buffer has been received for this stream.
57    // -- This callback only fires for createStreamCpu streams
58    // -- Use buf.timestamp to correlate with metadata's
59    //    android.sensor.timestamp
60    // -- The buffer must not be accessed after this function call completes
61    virtual void onBufferReceived(int streamId,
62                                  const CpuConsumer::LockedBuffer& buf) = 0;
63    /**
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;
69
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    }
83};
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);
150// TODO: remove requestStream, its useless.
151
152    /**
153      * Delete a stream.
154      * Lock free.
155      * Errors: BAD_VALUE if unknown stream ID.
156      *         PERMISSION_DENIED if the stream wasn't yours
157      */
158    status_t deleteStream(int streamId);
159
160    /**
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,
166                          const sp<Surface>& surface,
167                          /*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);
179    status_t createStreamCpu(int width, int height, int format,
180                          int heapCount,
181                          /*out*/
182                          sp<CpuConsumer>* cpuConsumer,
183                          int* streamId);
184
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
194    camera_metadata* getCameraInfo(int cameraId);
195
196    // 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
214    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);
228    virtual void        onLockStatusChanged(
229                                IProCameraCallbacks::LockStatus newLockStatus);
230
231    virtual void        onResultReceived(int32_t frameId,
232                                         camera_metadata* result);
233
234    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
263    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;
289            frameReady = false;
290        }
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;
302        bool frameReady;
303    };
304
305    Condition mWaitCondition;
306    Mutex     mWaitMutex;
307    static const nsecs_t mWaitTimeout = 1000000000; // 1sec
308    KeyedVector<int, StreamInfo> mStreams;
309    bool mMetadataReady;
310    CameraMetadata mLatestMetadata;
311
312    void onFrameAvailable(int streamId);
313
314    StreamInfo& getStreamInfo(int streamId);
315
316
317};
318
319}; // namespace android
320
321#endif
322