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 #define LOG_TAG "Camera2ClientBase"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 //#define LOG_NDEBUG 0
20 
21 #include <inttypes.h>
22 
23 #include <utils/Log.h>
24 #include <utils/Trace.h>
25 
26 #include <cutils/properties.h>
27 #include <gui/Surface.h>
28 #include <gui/Surface.h>
29 
30 #include <camera/CameraSessionStats.h>
31 
32 #include "common/Camera2ClientBase.h"
33 
34 #include "api2/CameraDeviceClient.h"
35 
36 #include "device3/Camera3Device.h"
37 #include "utils/CameraThreadState.h"
38 #include "utils/CameraServiceProxyWrapper.h"
39 
40 namespace android {
41 using namespace camera2;
42 
43 // Interface used by CameraService
44 
45 template <typename TClientBase>
Camera2ClientBase(const sp<CameraService> & cameraService,const sp<TCamCallbacks> & remoteCallback,const String16 & clientPackageName,const std::optional<String16> & clientFeatureId,const String8 & cameraId,int api1CameraId,int cameraFacing,int sensorOrientation,int clientPid,uid_t clientUid,int servicePid,bool overrideForPerfClass,bool legacyClient)46 Camera2ClientBase<TClientBase>::Camera2ClientBase(
47         const sp<CameraService>& cameraService,
48         const sp<TCamCallbacks>& remoteCallback,
49         const String16& clientPackageName,
50         const std::optional<String16>& clientFeatureId,
51         const String8& cameraId,
52         int api1CameraId,
53         int cameraFacing,
54         int sensorOrientation,
55         int clientPid,
56         uid_t clientUid,
57         int servicePid,
58         bool overrideForPerfClass,
59         bool legacyClient):
60         TClientBase(cameraService, remoteCallback, clientPackageName, clientFeatureId,
61                 cameraId, api1CameraId, cameraFacing, sensorOrientation, clientPid, clientUid,
62                 servicePid),
63         mSharedCameraCallbacks(remoteCallback),
64         mDeviceVersion(cameraService->getDeviceVersion(TClientBase::mCameraIdStr)),
65         mDevice(new Camera3Device(cameraId, overrideForPerfClass, legacyClient)),
66         mDeviceActive(false), mApi1CameraId(api1CameraId)
67 {
68     ALOGI("Camera %s: Opened. Client: %s (PID %d, UID %d)", cameraId.string(),
69             String8(clientPackageName).string(), clientPid, clientUid);
70 
71     mInitialClientPid = clientPid;
72     LOG_ALWAYS_FATAL_IF(mDevice == 0, "Device should never be NULL here.");
73 }
74 
75 template <typename TClientBase>
checkPid(const char * checkLocation) const76 status_t Camera2ClientBase<TClientBase>::checkPid(const char* checkLocation)
77         const {
78 
79     int callingPid = CameraThreadState::getCallingPid();
80     if (callingPid == TClientBase::mClientPid) return NO_ERROR;
81 
82     ALOGE("%s: attempt to use a locked camera from a different process"
83             " (old pid %d, new pid %d)", checkLocation, TClientBase::mClientPid, callingPid);
84     return PERMISSION_DENIED;
85 }
86 
87 template <typename TClientBase>
initialize(sp<CameraProviderManager> manager,const String8 & monitorTags)88 status_t Camera2ClientBase<TClientBase>::initialize(sp<CameraProviderManager> manager,
89         const String8& monitorTags) {
90     return initializeImpl(manager, monitorTags);
91 }
92 
93 template <typename TClientBase>
94 template <typename TProviderPtr>
initializeImpl(TProviderPtr providerPtr,const String8 & monitorTags)95 status_t Camera2ClientBase<TClientBase>::initializeImpl(TProviderPtr providerPtr,
96         const String8& monitorTags) {
97     ATRACE_CALL();
98     ALOGV("%s: Initializing client for camera %s", __FUNCTION__,
99           TClientBase::mCameraIdStr.string());
100     status_t res;
101 
102     // Verify ops permissions
103     res = TClientBase::startCameraOps();
104     if (res != OK) {
105         return res;
106     }
107 
108     if (mDevice == NULL) {
109         ALOGE("%s: Camera %s: No device connected",
110                 __FUNCTION__, TClientBase::mCameraIdStr.string());
111         return NO_INIT;
112     }
113 
114     res = mDevice->initialize(providerPtr, monitorTags);
115     if (res != OK) {
116         ALOGE("%s: Camera %s: unable to initialize device: %s (%d)",
117                 __FUNCTION__, TClientBase::mCameraIdStr.string(), strerror(-res), res);
118         return res;
119     }
120 
121     wp<NotificationListener> weakThis(this);
122     res = mDevice->setNotifyCallback(weakThis);
123 
124     return OK;
125 }
126 
127 template <typename TClientBase>
~Camera2ClientBase()128 Camera2ClientBase<TClientBase>::~Camera2ClientBase() {
129     ATRACE_CALL();
130 
131     TClientBase::mDestructionStarted = true;
132 
133     disconnect();
134 
135     ALOGI("Closed Camera %s. Client was: %s (PID %d, UID %u)",
136             TClientBase::mCameraIdStr.string(),
137             String8(TClientBase::mClientPackageName).string(),
138             mInitialClientPid, TClientBase::mClientUid);
139 }
140 
141 template <typename TClientBase>
dumpClient(int fd,const Vector<String16> & args)142 status_t Camera2ClientBase<TClientBase>::dumpClient(int fd,
143                                               const Vector<String16>& args) {
144     String8 result;
145     result.appendFormat("Camera2ClientBase[%s] (%p) PID: %d, dump:\n",
146             TClientBase::mCameraIdStr.string(),
147             (TClientBase::getRemoteCallback() != NULL ?
148                     IInterface::asBinder(TClientBase::getRemoteCallback()).get() : NULL),
149             TClientBase::mClientPid);
150     result.append("  State: ");
151 
152     write(fd, result.string(), result.size());
153     // TODO: print dynamic/request section from most recent requests
154 
155     return dumpDevice(fd, args);
156 }
157 
158 template <typename TClientBase>
dumpDevice(int fd,const Vector<String16> & args)159 status_t Camera2ClientBase<TClientBase>::dumpDevice(
160                                                 int fd,
161                                                 const Vector<String16>& args) {
162     String8 result;
163 
164     result = "  Device dump:\n";
165     write(fd, result.string(), result.size());
166 
167     sp<CameraDeviceBase> device = mDevice;
168     if (!device.get()) {
169         result = "  *** Device is detached\n";
170         write(fd, result.string(), result.size());
171         return NO_ERROR;
172     }
173 
174     status_t res = device->dump(fd, args);
175     if (res != OK) {
176         result = String8::format("   Error dumping device: %s (%d)",
177                 strerror(-res), res);
178         write(fd, result.string(), result.size());
179     }
180 
181     return NO_ERROR;
182 }
183 
184 // ICameraClient2BaseUser interface
185 
186 
187 template <typename TClientBase>
disconnect()188 binder::Status Camera2ClientBase<TClientBase>::disconnect() {
189     ATRACE_CALL();
190     Mutex::Autolock icl(mBinderSerializationLock);
191 
192     binder::Status res = binder::Status::ok();
193     // Allow both client and the media server to disconnect at all times
194     int callingPid = CameraThreadState::getCallingPid();
195     if (callingPid != TClientBase::mClientPid &&
196         callingPid != TClientBase::mServicePid) return res;
197 
198     ALOGV("Camera %s: Shutting down", TClientBase::mCameraIdStr.string());
199 
200     // Before detaching the device, cache the info from current open session.
201     // The disconnected check avoids duplication of info and also prevents
202     // deadlock while acquiring service lock in cacheDump.
203     if (!TClientBase::mDisconnected) {
204         Camera2ClientBase::getCameraService()->cacheDump();
205     }
206 
207     detachDevice();
208 
209     CameraService::BasicClient::disconnect();
210 
211     ALOGV("Camera %s: Shut down complete", TClientBase::mCameraIdStr.string());
212 
213     return res;
214 }
215 
216 template <typename TClientBase>
detachDevice()217 void Camera2ClientBase<TClientBase>::detachDevice() {
218     if (mDevice == 0) return;
219     mDevice->disconnect();
220 
221     ALOGV("Camera %s: Detach complete", TClientBase::mCameraIdStr.string());
222 }
223 
224 template <typename TClientBase>
connect(const sp<TCamCallbacks> & client)225 status_t Camera2ClientBase<TClientBase>::connect(
226         const sp<TCamCallbacks>& client) {
227     ATRACE_CALL();
228     ALOGV("%s: E", __FUNCTION__);
229     Mutex::Autolock icl(mBinderSerializationLock);
230 
231     if (TClientBase::mClientPid != 0 &&
232         CameraThreadState::getCallingPid() != TClientBase::mClientPid) {
233 
234         ALOGE("%s: Camera %s: Connection attempt from pid %d; "
235                 "current locked to pid %d",
236                 __FUNCTION__,
237                 TClientBase::mCameraIdStr.string(),
238                 CameraThreadState::getCallingPid(),
239                 TClientBase::mClientPid);
240         return BAD_VALUE;
241     }
242 
243     TClientBase::mClientPid = CameraThreadState::getCallingPid();
244 
245     TClientBase::mRemoteCallback = client;
246     mSharedCameraCallbacks = client;
247 
248     return OK;
249 }
250 
251 /** Device-related methods */
252 
253 template <typename TClientBase>
notifyError(int32_t errorCode,const CaptureResultExtras & resultExtras)254 void Camera2ClientBase<TClientBase>::notifyError(
255         int32_t errorCode,
256         const CaptureResultExtras& resultExtras) {
257     ALOGE("Error condition %d reported by HAL, requestId %" PRId32, errorCode,
258           resultExtras.requestId);
259 }
260 
261 template <typename TClientBase>
notifyActive()262 status_t Camera2ClientBase<TClientBase>::notifyActive() {
263     if (!mDeviceActive) {
264         status_t res = TClientBase::startCameraStreamingOps();
265         if (res != OK) {
266             ALOGE("%s: Camera %s: Error starting camera streaming ops: %d", __FUNCTION__,
267                     TClientBase::mCameraIdStr.string(), res);
268             return res;
269         }
270         CameraServiceProxyWrapper::logActive(TClientBase::mCameraIdStr);
271     }
272     mDeviceActive = true;
273 
274     ALOGV("Camera device is now active");
275     return OK;
276 }
277 
278 template <typename TClientBase>
notifyIdle(int64_t requestCount,int64_t resultErrorCount,bool deviceError,const std::vector<hardware::CameraStreamStats> & streamStats)279 void Camera2ClientBase<TClientBase>::notifyIdle(
280         int64_t requestCount, int64_t resultErrorCount, bool deviceError,
281         const std::vector<hardware::CameraStreamStats>& streamStats) {
282     if (mDeviceActive) {
283         status_t res = TClientBase::finishCameraStreamingOps();
284         if (res != OK) {
285             ALOGE("%s: Camera %s: Error finishing streaming ops: %d", __FUNCTION__,
286                     TClientBase::mCameraIdStr.string(), res);
287         }
288         CameraServiceProxyWrapper::logIdle(TClientBase::mCameraIdStr,
289                 requestCount, resultErrorCount, deviceError, streamStats);
290     }
291     mDeviceActive = false;
292 
293     ALOGV("Camera device is now idle");
294 }
295 
296 template <typename TClientBase>
notifyShutter(const CaptureResultExtras & resultExtras,nsecs_t timestamp)297 void Camera2ClientBase<TClientBase>::notifyShutter(const CaptureResultExtras& resultExtras,
298                                                    nsecs_t timestamp) {
299     (void)resultExtras;
300     (void)timestamp;
301 
302     ALOGV("%s: Shutter notification for request id %" PRId32 " at time %" PRId64,
303             __FUNCTION__, resultExtras.requestId, timestamp);
304 }
305 
306 template <typename TClientBase>
notifyAutoFocus(uint8_t newState,int triggerId)307 void Camera2ClientBase<TClientBase>::notifyAutoFocus(uint8_t newState,
308                                                      int triggerId) {
309     (void)newState;
310     (void)triggerId;
311 
312     ALOGV("%s: Autofocus state now %d, last trigger %d",
313           __FUNCTION__, newState, triggerId);
314 
315 }
316 
317 template <typename TClientBase>
notifyAutoExposure(uint8_t newState,int triggerId)318 void Camera2ClientBase<TClientBase>::notifyAutoExposure(uint8_t newState,
319                                                         int triggerId) {
320     (void)newState;
321     (void)triggerId;
322 
323     ALOGV("%s: Autoexposure state now %d, last trigger %d",
324             __FUNCTION__, newState, triggerId);
325 }
326 
327 template <typename TClientBase>
notifyAutoWhitebalance(uint8_t newState,int triggerId)328 void Camera2ClientBase<TClientBase>::notifyAutoWhitebalance(uint8_t newState,
329                                                             int triggerId) {
330     (void)newState;
331     (void)triggerId;
332 
333     ALOGV("%s: Auto-whitebalance state now %d, last trigger %d",
334             __FUNCTION__, newState, triggerId);
335 }
336 
337 template <typename TClientBase>
notifyPrepared(int streamId)338 void Camera2ClientBase<TClientBase>::notifyPrepared(int streamId) {
339     (void)streamId;
340 
341     ALOGV("%s: Stream %d now prepared",
342             __FUNCTION__, streamId);
343 }
344 
345 template <typename TClientBase>
notifyRequestQueueEmpty()346 void Camera2ClientBase<TClientBase>::notifyRequestQueueEmpty() {
347 
348     ALOGV("%s: Request queue now empty", __FUNCTION__);
349 }
350 
351 template <typename TClientBase>
notifyRepeatingRequestError(long lastFrameNumber)352 void Camera2ClientBase<TClientBase>::notifyRepeatingRequestError(long lastFrameNumber) {
353     (void)lastFrameNumber;
354 
355     ALOGV("%s: Repeating request was stopped. Last frame number is %ld",
356             __FUNCTION__, lastFrameNumber);
357 }
358 
359 template <typename TClientBase>
getCameraId() const360 int Camera2ClientBase<TClientBase>::getCameraId() const {
361     return mApi1CameraId;
362 }
363 
364 template <typename TClientBase>
getCameraDeviceVersion() const365 int Camera2ClientBase<TClientBase>::getCameraDeviceVersion() const {
366     return mDeviceVersion;
367 }
368 
369 template <typename TClientBase>
getCameraDevice()370 const sp<CameraDeviceBase>& Camera2ClientBase<TClientBase>::getCameraDevice() {
371     return mDevice;
372 }
373 
374 template <typename TClientBase>
getCameraService()375 const sp<CameraService>& Camera2ClientBase<TClientBase>::getCameraService() {
376     return TClientBase::sCameraService;
377 }
378 
379 template <typename TClientBase>
Lock(SharedCameraCallbacks & client)380 Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::Lock(
381         SharedCameraCallbacks &client) :
382 
383         mRemoteCallback(client.mRemoteCallback),
384         mSharedClient(client) {
385 
386     mSharedClient.mRemoteCallbackLock.lock();
387 }
388 
389 template <typename TClientBase>
~Lock()390 Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::~Lock() {
391     mSharedClient.mRemoteCallbackLock.unlock();
392 }
393 
394 template <typename TClientBase>
SharedCameraCallbacks(const sp<TCamCallbacks> & client)395 Camera2ClientBase<TClientBase>::SharedCameraCallbacks::SharedCameraCallbacks(
396         const sp<TCamCallbacks>&client) :
397 
398         mRemoteCallback(client) {
399 }
400 
401 template <typename TClientBase>
402 typename Camera2ClientBase<TClientBase>::SharedCameraCallbacks&
operator =(const sp<TCamCallbacks> & client)403 Camera2ClientBase<TClientBase>::SharedCameraCallbacks::operator=(
404         const sp<TCamCallbacks>&client) {
405 
406     Mutex::Autolock l(mRemoteCallbackLock);
407     mRemoteCallback = client;
408     return *this;
409 }
410 
411 template <typename TClientBase>
clear()412 void Camera2ClientBase<TClientBase>::SharedCameraCallbacks::clear() {
413     Mutex::Autolock l(mRemoteCallbackLock);
414     mRemoteCallback.clear();
415 }
416 
417 template class Camera2ClientBase<CameraService::Client>;
418 template class Camera2ClientBase<CameraDeviceClientBase>;
419 
420 } // namespace android
421