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 "camera/CaptureResult.h"
22 
23 namespace android {
24 
25 class IMemory;
26 
27 class CameraService;
28 
29 template <typename TClientBase>
30 class Camera2ClientBase :
31         public TClientBase,
32         public CameraDeviceBase::NotificationListener
33 {
34 public:
35     typedef typename TClientBase::TCamCallbacks TCamCallbacks;
36 
37     /**
38      * Base binder interface (see ICamera/ICameraDeviceUser for details)
39      */
40     virtual status_t       connect(const sp<TCamCallbacks>& callbacks);
41     virtual binder::Status disconnect();
42 
43     /**
44      * Interface used by CameraService
45      */
46 
47     // TODO: too many params, move into a ClientArgs<T>
48     Camera2ClientBase(const sp<CameraService>& cameraService,
49                       const sp<TCamCallbacks>& remoteCallback,
50                       const String16& clientPackageName,
51                       const String8& cameraId,
52                       int api1CameraId,
53                       int cameraFacing,
54                       int clientPid,
55                       uid_t clientUid,
56                       int servicePid);
57     virtual ~Camera2ClientBase();
58 
59     virtual status_t      initialize(sp<CameraProviderManager> manager, const String8& monitorTags);
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          notifyRequestQueueEmpty();
77     virtual void          notifyRepeatingRequestError(long lastFrameNumber);
78 
79     int                   getCameraId() const;
80     const sp<CameraDeviceBase>&
81                           getCameraDevice();
82     int                   getCameraDeviceVersion() const;
83     const sp<CameraService>&
84                           getCameraService();
85 
86     /**
87      * Interface used by independent components of CameraClient2Base.
88      */
89 
90     // Simple class to ensure that access to TCamCallbacks is serialized
91     // by requiring mRemoteCallbackLock to be locked before access to
92     // mRemoteCallback is possible.
93     class SharedCameraCallbacks {
94       public:
95         class Lock {
96           public:
97             explicit Lock(SharedCameraCallbacks &client);
98             ~Lock();
99             sp<TCamCallbacks> &mRemoteCallback;
100           private:
101             SharedCameraCallbacks &mSharedClient;
102         };
103         explicit SharedCameraCallbacks(const sp<TCamCallbacks>& client);
104         SharedCameraCallbacks& operator=(const sp<TCamCallbacks>& client);
105         void clear();
106       private:
107         sp<TCamCallbacks> mRemoteCallback;
108         mutable Mutex mRemoteCallbackLock;
109     } mSharedCameraCallbacks;
110 
111 protected:
112 
113     // The PID provided in the constructor call
114     pid_t mInitialClientPid;
115 
asBinderWrapper()116     virtual sp<IBinder> asBinderWrapper() {
117         return IInterface::asBinder(this);
118     }
119 
120     virtual status_t      dumpDevice(int fd, const Vector<String16>& args);
121 
122     /** Binder client interface-related private members */
123 
124     // Mutex that must be locked by methods implementing the binder client
125     // interface. Ensures serialization between incoming client calls.
126     // All methods in this class hierarchy that append 'L' to the name assume
127     // that mBinderSerializationLock is locked when they're called
128     mutable Mutex         mBinderSerializationLock;
129 
130     /** CameraDeviceBase instance wrapping HAL3+ entry */
131 
132     const int mDeviceVersion;
133 
134     // Set to const to avoid mDevice being updated (update of sp<> is racy) during
135     // dumpDevice (which is important to be lock free for debugging purpose)
136     const sp<CameraDeviceBase>  mDevice;
137 
138     /** Utility members */
139 
140     // Verify that caller is the owner of the camera
141     status_t              checkPid(const char *checkLocation) const;
142 
143     virtual void          detachDevice();
144 
145     bool                  mDeviceActive;
146 
147     const int             mApi1CameraId; // -1 if client is API2
148 
149 private:
150     template<typename TProviderPtr>
151     status_t              initializeImpl(TProviderPtr providerPtr, const String8& monitorTags);
152 };
153 
154 }; // namespace android
155 
156 #endif
157