ProCamera.h revision bfb5d5ef5bae01efac171397260a7152782d92c7
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, or has been stolen by
38    // another client.
39    virtual void onLockReleased() = 0;
40
41    // Lock free.
42    virtual void onTriggerNotify(int32_t msgType, int32_t ext1, int32_t ext2)
43                                                                            = 0;
44};
45
46class ProCamera : public BnProCameraCallbacks, public IBinder::DeathRecipient
47{
48public:
49    /**
50     * Connect a shared camera. By default access is restricted to read only
51     * (Lock free) operations. To be able to submit custom requests a lock needs
52     * to be acquired with exclusive[Try]Lock.
53     */
54    static sp<ProCamera> connect(int cameraId);
55    virtual void disconnect();
56    virtual ~ProCamera();
57
58    void setListener(const sp<ProCameraListener>& listener);
59
60    /**
61     * Exclusive Locks:
62     * - We may request exclusive access to a camera if no other
63     *   clients are using the camera. This works as a traditional
64     *   client, writing/reading any camera state.
65     * - An application opening the camera (a regular 'Camera') will
66     *   always steal away the exclusive lock from a ProCamera,
67     *   this will call onLockReleased.
68     * - onLockAcquired will be called again once it is possible
69     *   to again exclusively lock the camera.
70     *
71     */
72
73    /**
74     * All exclusiveLock/unlock functions are asynchronous. The remote endpoint
75     * shall not block while waiting to acquire the lock. Instead the lock
76     * notifications will come in asynchronously on the listener.
77     */
78
79    /**
80      * Attempt to acquire the lock instantly (non-blocking)
81      * - If this succeeds, you do not need to wait for onLockAcquired
82      *   but the event will still be fired
83      *
84      * Returns -EBUSY if already locked. 0 on success.
85      */
86    status_t exclusiveTryLock();
87    // always returns 0. wait for onLockAcquired before lock is acquired.
88    status_t exclusiveLock();
89    // release a lock if we have one, or cancel the lock request.
90    status_t exclusiveUnlock();
91
92    // exclusive lock = do whatever we want. no lock = read only.
93    bool hasExclusiveLock();
94
95    /**
96     * < 0 error, >= 0 the request ID. streaming to have the request repeat
97     *    until cancelled.
98     * The request queue is flushed when a lock is released or stolen
99     *    if not locked will return PERMISSION_DENIED
100     */
101    int submitRequest(const struct camera_metadata* metadata,
102                                                        bool streaming = false);
103    // if not locked will return PERMISSION_DENIED, BAD_VALUE if requestId bad
104    status_t cancelRequest(int requestId);
105
106    /**
107     * Ask for a stream to be enabled.
108     * Lock free. Service maintains counter of streams.
109     */
110    status_t requestStream(int streamId);
111    /**
112     * Ask for a stream to be disabled.
113     * Lock free. Service maintains counter of streams.
114     * Errors: BAD_VALUE if unknown stream ID.
115     */
116    status_t cancelStream(int streamId);
117
118    sp<IProCameraUser>         remote();
119
120protected:
121    ////////////////////////////////////////////////////////
122    // IProCameraCallbacks implementation
123    ////////////////////////////////////////////////////////
124    virtual void        notifyCallback(int32_t msgType, int32_t ext,
125                                       int32_t ext2);
126    virtual void        dataCallback(int32_t msgType,
127                                     const sp<IMemory>& dataPtr,
128                                     camera_frame_metadata_t *metadata);
129    virtual void        dataCallbackTimestamp(nsecs_t timestamp,
130                                              int32_t msgType,
131                                              const sp<IMemory>& dataPtr);
132
133    class DeathNotifier: public IBinder::DeathRecipient
134    {
135    public:
136        DeathNotifier() {
137        }
138
139        virtual void binderDied(const wp<IBinder>& who);
140    };
141
142private:
143    ProCamera();
144
145    virtual void binderDied(const wp<IBinder>& who);
146
147    // helper function to obtain camera service handle
148    static const sp<ICameraService>& getCameraService();
149
150    static sp<DeathNotifier> mDeathNotifier;
151
152    sp<IProCameraUser>  mCamera;
153    status_t            mStatus;
154
155    sp<ProCameraListener>  mListener;
156
157    friend class DeathNotifier;
158
159    static  Mutex               mLock;
160    static  sp<ICameraService>  mCameraService;
161
162
163};
164
165}; // namespace android
166
167#endif
168