ProCamera.h revision 68506fd58d26748617babe94d5648503cb3690bb
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 <gui/IGraphicBufferProducer.h>
22#include <system/camera.h>
23#include <camera/IProCameraCallbacks.h>
24#include <camera/IProCameraUser.h>
25#include <camera/Camera.h>
26
27struct camera_metadata;
28
29namespace android {
30
31// ref-counted object for callbacks
32class ProCameraListener : public CameraListener
33{
34public:
35    // Lock has been acquired. Write operations now available.
36    virtual void onLockAcquired() = 0;
37    // Lock has been released with exclusiveUnlock.
38    virtual void onLockReleased() = 0;
39    // Lock has been stolen by another client.
40    virtual void onLockStolen() = 0;
41
42    // Lock free.
43    virtual void onTriggerNotify(int32_t msgType, int32_t ext1, int32_t ext2)
44                                                                            = 0;
45};
46
47class ProCamera : public BnProCameraCallbacks, public IBinder::DeathRecipient
48{
49public:
50    /**
51     * Connect a shared camera. By default access is restricted to read only
52     * (Lock free) operations. To be able to submit custom requests a lock needs
53     * to be acquired with exclusive[Try]Lock.
54     */
55    static sp<ProCamera> connect(int cameraId);
56    virtual void disconnect();
57    virtual ~ProCamera();
58
59    void setListener(const sp<ProCameraListener>& listener);
60
61    /**
62     * Exclusive Locks:
63     * - We may request exclusive access to a camera if no other
64     *   clients are using the camera. This works as a traditional
65     *   client, writing/reading any camera state.
66     * - An application opening the camera (a regular 'Camera') will
67     *   always steal away the exclusive lock from a ProCamera,
68     *   this will call onLockReleased.
69     * - onLockAcquired will be called again once it is possible
70     *   to again exclusively lock the camera.
71     *
72     */
73
74    /**
75     * All exclusiveLock/unlock functions are asynchronous. The remote endpoint
76     * shall not block while waiting to acquire the lock. Instead the lock
77     * notifications will come in asynchronously on the listener.
78     */
79
80    /**
81      * Attempt to acquire the lock instantly (non-blocking)
82      * - If this succeeds, you do not need to wait for onLockAcquired
83      *   but the event will still be fired
84      *
85      * Returns -EBUSY if already locked. 0 on success.
86      */
87    status_t exclusiveTryLock();
88    // always returns 0. wait for onLockAcquired before lock is acquired.
89    status_t exclusiveLock();
90    // release a lock if we have one, or cancel the lock request.
91    status_t exclusiveUnlock();
92
93    // exclusive lock = do whatever we want. no lock = read only.
94    bool hasExclusiveLock();
95
96    /**
97     * < 0 error, >= 0 the request ID. streaming to have the request repeat
98     *    until cancelled.
99     * The request queue is flushed when a lock is released or stolen
100     *    if not locked will return PERMISSION_DENIED
101     */
102    int submitRequest(const struct camera_metadata* metadata,
103                                                        bool streaming = false);
104    // if not locked will return PERMISSION_DENIED, BAD_VALUE if requestId bad
105    status_t cancelRequest(int requestId);
106
107    /**
108     * Ask for a stream to be enabled.
109     * Lock free. Service maintains counter of streams.
110     */
111    status_t requestStream(int streamId);
112    /**
113     * Ask for a stream to be disabled.
114     * Lock free. Service maintains counter of streams.
115     * Errors: BAD_VALUE if unknown stream ID.
116     */
117// TODO: remove requestStream, its useless.
118
119// TODO: rename cancelStream to deleteStream
120// can probably do it with a grep/sed
121
122    /**
123      * Ask for a stream to be disabled.
124      * Lock free. Service maintains counter of streams.
125      * Errors: BAD_VALUE if unknown stream ID.
126      */
127    status_t cancelStream(int streamId);
128
129    /**
130      * Create a new HW stream, whose sink will be the window.
131      * Lock free. Service maintains counter of streams.
132      * Errors: -EBUSY if too many streams created
133      */
134    status_t createStream(int width, int height, int format,
135                          const sp<ANativeWindow>& window,
136                          /*out*/
137                          int* streamId);
138
139    /**
140      * Create a new HW stream, whose sink will be the SurfaceTexture.
141      * Lock free. Service maintains counter of streams.
142      * Errors: -EBUSY if too many streams created
143      */
144    status_t createStream(int width, int height, int format,
145                          const sp<IGraphicBufferProducer>& bufferProducer,
146                          /*out*/
147                          int* streamId);
148
149    // Create a request object from a template.
150    status_t createDefaultRequest(int templateId,
151                                 /*out*/
152                                  camera_metadata** request) const;
153
154    // Get number of cameras
155    static int getNumberOfCameras();
156
157    // Get static camera metadata
158    static camera_metadata* getCameraInfo(int cameraId);
159
160    sp<IProCameraUser>         remote();
161
162protected:
163    ////////////////////////////////////////////////////////
164    // IProCameraCallbacks implementation
165    ////////////////////////////////////////////////////////
166    virtual void        notifyCallback(int32_t msgType, int32_t ext,
167                                       int32_t ext2);
168    virtual void        dataCallback(int32_t msgType,
169                                     const sp<IMemory>& dataPtr,
170                                     camera_frame_metadata_t *metadata);
171    virtual void        dataCallbackTimestamp(nsecs_t timestamp,
172                                              int32_t msgType,
173                                              const sp<IMemory>& dataPtr);
174    virtual void        onLockStatusChanged(
175                                IProCameraCallbacks::LockStatus newLockStatus);
176
177    class DeathNotifier: public IBinder::DeathRecipient
178    {
179    public:
180        DeathNotifier() {
181        }
182
183        virtual void binderDied(const wp<IBinder>& who);
184    };
185
186private:
187    ProCamera();
188
189    virtual void binderDied(const wp<IBinder>& who);
190
191    // helper function to obtain camera service handle
192    static const sp<ICameraService>& getCameraService();
193
194    static sp<DeathNotifier> mDeathNotifier;
195
196    sp<IProCameraUser>  mCamera;
197    status_t            mStatus;
198
199    sp<ProCameraListener>  mListener;
200
201    friend class DeathNotifier;
202
203    static  Mutex               mLock;
204    static  sp<ICameraService>  mCameraService;
205
206
207};
208
209}; // namespace android
210
211#endif
212