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_SERVERS_CAMERA_CAMERA2CLIENT_BASE_H
18#define ANDROID_SERVERS_CAMERA_CAMERA2CLIENT_BASE_H
19
20#include "common/CameraDeviceBase.h"
21#include "common/CameraModule.h"
22#include "camera/CaptureResult.h"
23
24namespace android {
25
26class IMemory;
27
28class CameraService;
29
30template <typename TClientBase>
31class Camera2ClientBase :
32        public TClientBase,
33        public CameraDeviceBase::NotificationListener
34{
35public:
36    typedef typename TClientBase::TCamCallbacks TCamCallbacks;
37
38    /**
39     * Base binder interface (see ICamera/ICameraDeviceUser for details)
40     */
41    virtual status_t       connect(const sp<TCamCallbacks>& callbacks);
42    virtual binder::Status disconnect();
43
44    /**
45     * Interface used by CameraService
46     */
47
48    // TODO: too many params, move into a ClientArgs<T>
49    Camera2ClientBase(const sp<CameraService>& cameraService,
50                      const sp<TCamCallbacks>& remoteCallback,
51                      const String16& clientPackageName,
52                      int cameraId,
53                      int cameraFacing,
54                      int clientPid,
55                      uid_t clientUid,
56                      int servicePid);
57    virtual ~Camera2ClientBase();
58
59    virtual status_t      initialize(CameraModule *module);
60    virtual status_t      dumpClient(int fd, const Vector<String16>& args);
61
62    /**
63     * CameraDeviceBase::NotificationListener implementation
64     */
65
66    virtual void          notifyError(int32_t errorCode,
67                                      const CaptureResultExtras& resultExtras);
68    virtual void          notifyIdle();
69    virtual void          notifyShutter(const CaptureResultExtras& resultExtras,
70                                        nsecs_t timestamp);
71    virtual void          notifyAutoFocus(uint8_t newState, int triggerId);
72    virtual void          notifyAutoExposure(uint8_t newState, int triggerId);
73    virtual void          notifyAutoWhitebalance(uint8_t newState,
74                                                 int triggerId);
75    virtual void          notifyPrepared(int streamId);
76    virtual void          notifyRepeatingRequestError(long lastFrameNumber);
77
78    int                   getCameraId() const;
79    const sp<CameraDeviceBase>&
80                          getCameraDevice();
81    int                   getCameraDeviceVersion() const;
82    const sp<CameraService>&
83                          getCameraService();
84
85    /**
86     * Interface used by independent components of CameraClient2Base.
87     */
88
89    // Simple class to ensure that access to TCamCallbacks is serialized
90    // by requiring mRemoteCallbackLock to be locked before access to
91    // mRemoteCallback is possible.
92    class SharedCameraCallbacks {
93      public:
94        class Lock {
95          public:
96            Lock(SharedCameraCallbacks &client);
97            ~Lock();
98            sp<TCamCallbacks> &mRemoteCallback;
99          private:
100            SharedCameraCallbacks &mSharedClient;
101        };
102        SharedCameraCallbacks(const sp<TCamCallbacks>& client);
103        SharedCameraCallbacks& operator=(const sp<TCamCallbacks>& client);
104        void clear();
105      private:
106        sp<TCamCallbacks> mRemoteCallback;
107        mutable Mutex mRemoteCallbackLock;
108    } mSharedCameraCallbacks;
109
110protected:
111
112    // The PID provided in the constructor call
113    pid_t mInitialClientPid;
114
115    virtual sp<IBinder> asBinderWrapper() {
116        return IInterface::asBinder(this);
117    }
118
119    virtual status_t      dumpDevice(int fd, const Vector<String16>& args);
120
121    /** Binder client interface-related private members */
122
123    // Mutex that must be locked by methods implementing the binder client
124    // interface. Ensures serialization between incoming client calls.
125    // All methods in this class hierarchy that append 'L' to the name assume
126    // that mBinderSerializationLock is locked when they're called
127    mutable Mutex         mBinderSerializationLock;
128
129    /** CameraDeviceBase instance wrapping HAL3+ entry */
130
131    const int mDeviceVersion;
132    sp<CameraDeviceBase>  mDevice;
133
134    /** Utility members */
135
136    // Verify that caller is the owner of the camera
137    status_t              checkPid(const char *checkLocation) const;
138
139    virtual void          detachDevice();
140
141    bool                  mDeviceActive;
142};
143
144}; // namespace android
145
146#endif
147