ProCamera.h revision 68c80668304fc92db43bbe2e7cbe9753b6d3865a
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    status_t cancelStream(int streamId);
118
119    sp<IProCameraUser>         remote();
120
121protected:
122    ////////////////////////////////////////////////////////
123    // IProCameraCallbacks implementation
124    ////////////////////////////////////////////////////////
125    virtual void        notifyCallback(int32_t msgType, int32_t ext,
126                                       int32_t ext2);
127    virtual void        dataCallback(int32_t msgType,
128                                     const sp<IMemory>& dataPtr,
129                                     camera_frame_metadata_t *metadata);
130    virtual void        dataCallbackTimestamp(nsecs_t timestamp,
131                                              int32_t msgType,
132                                              const sp<IMemory>& dataPtr);
133    virtual void        onLockStatusChanged(
134                                IProCameraCallbacks::LockStatus newLockStatus);
135
136    class DeathNotifier: public IBinder::DeathRecipient
137    {
138    public:
139        DeathNotifier() {
140        }
141
142        virtual void binderDied(const wp<IBinder>& who);
143    };
144
145private:
146    ProCamera();
147
148    virtual void binderDied(const wp<IBinder>& who);
149
150    // helper function to obtain camera service handle
151    static const sp<ICameraService>& getCameraService();
152
153    static sp<DeathNotifier> mDeathNotifier;
154
155    sp<IProCameraUser>  mCamera;
156    status_t            mStatus;
157
158    sp<ProCameraListener>  mListener;
159
160    friend class DeathNotifier;
161
162    static  Mutex               mLock;
163    static  sp<ICameraService>  mCameraService;
164
165
166};
167
168}; // namespace android
169
170#endif
171