CameraService.cpp (1527f07eb2b2b40f6b8f53a4644e6a400bddb460) CameraService.cpp (66296c4e99da5f2dd0a3dbf048be0d6ffc49a559)
1/*
2 * Copyright (C) 2008 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

--- 19 unchanged lines hidden (view full) ---

28#include <pthread.h>
29
30#include <binder/AppOpsManager.h>
31#include <binder/IPCThreadState.h>
32#include <binder/IServiceManager.h>
33#include <binder/MemoryBase.h>
34#include <binder/MemoryHeapBase.h>
35#include <binder/ProcessInfoService.h>
1/*
2 * Copyright (C) 2008 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

--- 19 unchanged lines hidden (view full) ---

28#include <pthread.h>
29
30#include <binder/AppOpsManager.h>
31#include <binder/IPCThreadState.h>
32#include <binder/IServiceManager.h>
33#include <binder/MemoryBase.h>
34#include <binder/MemoryHeapBase.h>
35#include <binder/ProcessInfoService.h>
36#include <camera/ICameraServiceProxy.h>
36#include <cutils/atomic.h>
37#include <cutils/properties.h>
38#include <gui/Surface.h>
39#include <hardware/hardware.h>
40#include <media/AudioSystem.h>
41#include <media/IMediaHTTPService.h>
42#include <media/mediaplayer.h>
37#include <cutils/atomic.h>
38#include <cutils/properties.h>
39#include <gui/Surface.h>
40#include <hardware/hardware.h>
41#include <media/AudioSystem.h>
42#include <media/IMediaHTTPService.h>
43#include <media/mediaplayer.h>
44#include <mediautils/BatteryNotifier.h>
43#include <utils/Errors.h>
44#include <utils/Log.h>
45#include <utils/String16.h>
46#include <utils/Trace.h>
47#include <system/camera_vendor_tags.h>
48#include <system/camera_metadata.h>
49#include <system/camera.h>
50

--- 66 unchanged lines hidden (view full) ---

117} // extern "C"
118
119// ----------------------------------------------------------------------------
120
121// This is ugly and only safe if we never re-create the CameraService, but
122// should be ok for now.
123static CameraService *gCameraService;
124
45#include <utils/Errors.h>
46#include <utils/Log.h>
47#include <utils/String16.h>
48#include <utils/Trace.h>
49#include <system/camera_vendor_tags.h>
50#include <system/camera_metadata.h>
51#include <system/camera.h>
52

--- 66 unchanged lines hidden (view full) ---

119} // extern "C"
120
121// ----------------------------------------------------------------------------
122
123// This is ugly and only safe if we never re-create the CameraService, but
124// should be ok for now.
125static CameraService *gCameraService;
126
125CameraService::CameraService() : mEventLog(DEFAULT_EVENT_LOG_LENGTH),
126 mLastUserId(DEFAULT_LAST_USER_ID), mSoundRef(0), mModule(0), mFlashlight(0) {
127CameraService::CameraService() : mEventLog(DEFAULT_EVENT_LOG_LENGTH), mAllowedUsers(),
128 mSoundRef(0), mModule(0), mFlashlight(0) {
127 ALOGI("CameraService started (pid=%d)", getpid());
128 gCameraService = this;
129
130 this->camera_device_status_change = android::camera_device_status_change;
131 this->torch_mode_status_change = android::torch_mode_status_change;
132
133 mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock);
134}
135
136void CameraService::onFirstRef()
137{
138 ALOGI("CameraService process starting");
139
140 BnCameraService::onFirstRef();
141
129 ALOGI("CameraService started (pid=%d)", getpid());
130 gCameraService = this;
131
132 this->camera_device_status_change = android::camera_device_status_change;
133 this->torch_mode_status_change = android::torch_mode_status_change;
134
135 mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock);
136}
137
138void CameraService::onFirstRef()
139{
140 ALOGI("CameraService process starting");
141
142 BnCameraService::onFirstRef();
143
144 // Update battery life tracking if service is restarting
145 BatteryNotifier& notifier(BatteryNotifier::getInstance());
146 notifier.noteResetCamera();
147 notifier.noteResetFlashlight();
148
142 camera_module_t *rawModule;
143 int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
144 (const hw_module_t **)&rawModule);
145 if (err < 0) {
146 ALOGE("Could not load camera HAL module: %d (%s)", err, strerror(-err));
147 logServiceError("Could not load camera HAL module", err);
148 mNumberOfCameras = 0;
149 return;

--- 9 unchanged lines hidden (view full) ---

159
160 mNumberOfCameras = 0;
161 delete mModule;
162 mModule = nullptr;
163 return;
164 }
165
166 mNumberOfCameras = mModule->getNumberOfCameras();
149 camera_module_t *rawModule;
150 int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
151 (const hw_module_t **)&rawModule);
152 if (err < 0) {
153 ALOGE("Could not load camera HAL module: %d (%s)", err, strerror(-err));
154 logServiceError("Could not load camera HAL module", err);
155 mNumberOfCameras = 0;
156 return;

--- 9 unchanged lines hidden (view full) ---

166
167 mNumberOfCameras = 0;
168 delete mModule;
169 mModule = nullptr;
170 return;
171 }
172
173 mNumberOfCameras = mModule->getNumberOfCameras();
174 mNumberOfNormalCameras = mNumberOfCameras;
167
168 mFlashlight = new CameraFlashlight(*mModule, *this);
169 status_t res = mFlashlight->findFlashUnits();
170 if (res) {
171 // impossible because we haven't open any camera devices.
172 ALOGE("Failed to find flash units.");
173 }
174
175
176 mFlashlight = new CameraFlashlight(*mModule, *this);
177 status_t res = mFlashlight->findFlashUnits();
178 if (res) {
179 // impossible because we haven't open any camera devices.
180 ALOGE("Failed to find flash units.");
181 }
182
183 int latestStrangeCameraId = INT_MAX;
175 for (int i = 0; i < mNumberOfCameras; i++) {
176 String8 cameraId = String8::format("%d", i);
177
184 for (int i = 0; i < mNumberOfCameras; i++) {
185 String8 cameraId = String8::format("%d", i);
186
187 // Get camera info
188
189 struct camera_info info;
190 bool haveInfo = true;
191 status_t rc = mModule->getCameraInfo(i, &info);
192 if (rc != NO_ERROR) {
193 ALOGE("%s: Received error loading camera info for device %d, cost and"
194 " conflicting devices fields set to defaults for this device.",
195 __FUNCTION__, i);
196 haveInfo = false;
197 }
198
199 // Check for backwards-compatibility support
200 if (haveInfo) {
201 if (checkCameraCapabilities(i, info, &latestStrangeCameraId) != OK) {
202 delete mModule;
203 mModule = nullptr;
204 return;
205 }
206 }
207
178 // Defaults to use for cost and conflicting devices
179 int cost = 100;
180 char** conflicting_devices = nullptr;
181 size_t conflicting_devices_length = 0;
182
183 // If using post-2.4 module version, query the cost + conflicting devices from the HAL
208 // Defaults to use for cost and conflicting devices
209 int cost = 100;
210 char** conflicting_devices = nullptr;
211 size_t conflicting_devices_length = 0;
212
213 // If using post-2.4 module version, query the cost + conflicting devices from the HAL
184 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4) {
185 struct camera_info info;
186 status_t rc = mModule->getCameraInfo(i, &info);
187 if (rc == NO_ERROR) {
188 cost = info.resource_cost;
189 conflicting_devices = info.conflicting_devices;
190 conflicting_devices_length = info.conflicting_devices_length;
191 } else {
192 ALOGE("%s: Received error loading camera info for device %d, cost and"
193 " conflicting devices fields set to defaults for this device.",
194 __FUNCTION__, i);
195 }
214 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 && haveInfo) {
215 cost = info.resource_cost;
216 conflicting_devices = info.conflicting_devices;
217 conflicting_devices_length = info.conflicting_devices_length;
196 }
197
198 std::set<String8> conflicting;
199 for (size_t i = 0; i < conflicting_devices_length; i++) {
200 conflicting.emplace(String8(conflicting_devices[i]));
201 }
202
203 // Initialize state for each camera device

--- 15 unchanged lines hidden (view full) ---

219
220 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
221
222 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_2) {
223 setUpVendorTags();
224 }
225
226 CameraDeviceFactory::registerService(this);
218 }
219
220 std::set<String8> conflicting;
221 for (size_t i = 0; i < conflicting_devices_length; i++) {
222 conflicting.emplace(String8(conflicting_devices[i]));
223 }
224
225 // Initialize state for each camera device

--- 15 unchanged lines hidden (view full) ---

241
242 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
243
244 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_2) {
245 setUpVendorTags();
246 }
247
248 CameraDeviceFactory::registerService(this);
249
250 CameraService::pingCameraServiceProxy();
227}
228
251}
252
253void CameraService::pingCameraServiceProxy() {
254 sp<IServiceManager> sm = defaultServiceManager();
255 sp<IBinder> binder = sm->getService(String16("media.camera.proxy"));
256 if (binder == nullptr) {
257 return;
258 }
259 sp<ICameraServiceProxy> proxyBinder = interface_cast<ICameraServiceProxy>(binder);
260 proxyBinder->pingForUserUpdate();
261}
262
229CameraService::~CameraService() {
230 if (mModule) {
231 delete mModule;
232 mModule = nullptr;
233 }
234 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
235 gCameraService = nullptr;
236}

--- 71 unchanged lines hidden (view full) ---

308void CameraService::onTorchStatusChangedLocked(const String8& cameraId,
309 ICameraServiceListener::TorchStatus newStatus) {
310 ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d",
311 __FUNCTION__, cameraId.string(), newStatus);
312
313 ICameraServiceListener::TorchStatus status;
314 status_t res = getTorchStatusLocked(cameraId, &status);
315 if (res) {
263CameraService::~CameraService() {
264 if (mModule) {
265 delete mModule;
266 mModule = nullptr;
267 }
268 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
269 gCameraService = nullptr;
270}

--- 71 unchanged lines hidden (view full) ---

342void CameraService::onTorchStatusChangedLocked(const String8& cameraId,
343 ICameraServiceListener::TorchStatus newStatus) {
344 ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d",
345 __FUNCTION__, cameraId.string(), newStatus);
346
347 ICameraServiceListener::TorchStatus status;
348 status_t res = getTorchStatusLocked(cameraId, &status);
349 if (res) {
316 ALOGE("%s: cannot get torch status of camera %s", cameraId.string());
350 ALOGE("%s: cannot get torch status of camera %s: %s (%d)",
351 __FUNCTION__, cameraId.string(), strerror(-res), res);
317 return;
318 }
319 if (status == newStatus) {
352 return;
353 }
354 if (status == newStatus) {
320 ALOGE("%s: Torch state transition to the same status 0x%x not allowed",
321 __FUNCTION__, (uint32_t)newStatus);
322 return;
323 }
324
325 res = setTorchStatusLocked(cameraId, newStatus);
326 if (res) {
355 return;
356 }
357
358 res = setTorchStatusLocked(cameraId, newStatus);
359 if (res) {
327 ALOGE("%s: Failed to set the torch status", __FUNCTION__,
328 (uint32_t)newStatus);
360 ALOGE("%s: Failed to set the torch status", __FUNCTION__, (uint32_t)newStatus);
329 return;
330 }
331
332 {
361 return;
362 }
363
364 {
365 // Update battery life logging for flashlight
366 Mutex::Autolock al(mTorchUidMapMutex);
367 auto iter = mTorchUidMap.find(cameraId);
368 if (iter != mTorchUidMap.end()) {
369 int oldUid = iter->second.second;
370 int newUid = iter->second.first;
371 BatteryNotifier& notifier(BatteryNotifier::getInstance());
372 if (oldUid != newUid) {
373 // If the UID has changed, log the status and update current UID in mTorchUidMap
374 if (status == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) {
375 notifier.noteFlashlightOff(cameraId, oldUid);
376 }
377 if (newStatus == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) {
378 notifier.noteFlashlightOn(cameraId, newUid);
379 }
380 iter->second.second = newUid;
381 } else {
382 // If the UID has not changed, log the status
383 if (newStatus == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) {
384 notifier.noteFlashlightOn(cameraId, oldUid);
385 } else {
386 notifier.noteFlashlightOff(cameraId, oldUid);
387 }
388 }
389 }
390 }
391
392 {
333 Mutex::Autolock lock(mStatusListenerLock);
334 for (auto& i : mListenerList) {
335 i->onTorchStatusChanged(newStatus, String16{cameraId});
336 }
337 }
338}
339
393 Mutex::Autolock lock(mStatusListenerLock);
394 for (auto& i : mListenerList) {
395 i->onTorchStatusChanged(newStatus, String16{cameraId});
396 }
397 }
398}
399
340
341int32_t CameraService::getNumberOfCameras() {
400int32_t CameraService::getNumberOfCameras() {
342 return mNumberOfCameras;
401 return getNumberOfCameras(CAMERA_TYPE_BACKWARD_COMPATIBLE);
343}
344
402}
403
404int32_t CameraService::getNumberOfCameras(int type) {
405 switch (type) {
406 case CAMERA_TYPE_BACKWARD_COMPATIBLE:
407 return mNumberOfNormalCameras;
408 case CAMERA_TYPE_ALL:
409 return mNumberOfCameras;
410 default:
411 ALOGW("%s: Unknown camera type %d, returning 0",
412 __FUNCTION__, type);
413 return 0;
414 }
415}
416
345status_t CameraService::getCameraInfo(int cameraId,
346 struct CameraInfo* cameraInfo) {
347 if (!mModule) {
348 return -ENODEV;
349 }
350
351 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
352 return BAD_VALUE;

--- 162 unchanged lines hidden (view full) ---

515 char formattedTime[64];
516 strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now));
517 return String8(formattedTime);
518}
519
520int CameraService::getCameraPriorityFromProcState(int procState) {
521 // Find the priority for the camera usage based on the process state. Higher priority clients
522 // win for evictions.
417status_t CameraService::getCameraInfo(int cameraId,
418 struct CameraInfo* cameraInfo) {
419 if (!mModule) {
420 return -ENODEV;
421 }
422
423 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
424 return BAD_VALUE;

--- 162 unchanged lines hidden (view full) ---

587 char formattedTime[64];
588 strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now));
589 return String8(formattedTime);
590}
591
592int CameraService::getCameraPriorityFromProcState(int procState) {
593 // Find the priority for the camera usage based on the process state. Higher priority clients
594 // win for evictions.
523 // Note: Unlike the ordering for ActivityManager, persistent system processes will always lose
524 // the camera to the top/foreground applications.
525 switch(procState) {
526 case PROCESS_STATE_TOP: // User visible
527 return 100;
528 case PROCESS_STATE_IMPORTANT_FOREGROUND: // Foreground
529 return 90;
530 case PROCESS_STATE_PERSISTENT: // Persistent system services
531 case PROCESS_STATE_PERSISTENT_UI:
532 return 80;
533 case PROCESS_STATE_IMPORTANT_BACKGROUND: // "Important" background processes
534 return 70;
535 case PROCESS_STATE_BACKUP: // Everything else
536 case PROCESS_STATE_HEAVY_WEIGHT:
537 case PROCESS_STATE_SERVICE:
538 case PROCESS_STATE_RECEIVER:
539 case PROCESS_STATE_HOME:
540 case PROCESS_STATE_LAST_ACTIVITY:
541 case PROCESS_STATE_CACHED_ACTIVITY:
542 case PROCESS_STATE_CACHED_ACTIVITY_CLIENT:
543 case PROCESS_STATE_CACHED_EMPTY:
544 return 1;
545 case PROCESS_STATE_NONEXISTENT:
546 return -1;
547 default:
548 ALOGE("%s: Received unknown process state from ActivityManagerService!", __FUNCTION__);
549 return -1;
595 if (procState < 0) {
596 ALOGE("%s: Received invalid process state %d from ActivityManagerService!", __FUNCTION__,
597 procState);
598 return -1;
550 }
599 }
600 return INT_MAX - procState;
551}
552
553status_t CameraService::getCameraVendorTagDescriptor(/*out*/sp<VendorTagDescriptor>& desc) {
554 if (!mModule) {
555 ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__);
556 return -ENODEV;
557 }
558

--- 135 unchanged lines hidden (view full) ---

694 " opened as HAL %x device", halVersion, deviceVersion,
695 CAMERA_DEVICE_API_VERSION_1_0);
696 return INVALID_OPERATION;
697 }
698 }
699 return NO_ERROR;
700}
701
601}
602
603status_t CameraService::getCameraVendorTagDescriptor(/*out*/sp<VendorTagDescriptor>& desc) {
604 if (!mModule) {
605 ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__);
606 return -ENODEV;
607 }
608

--- 135 unchanged lines hidden (view full) ---

744 " opened as HAL %x device", halVersion, deviceVersion,
745 CAMERA_DEVICE_API_VERSION_1_0);
746 return INVALID_OPERATION;
747 }
748 }
749 return NO_ERROR;
750}
751
752String8 CameraService::toString(std::set<userid_t> intSet) {
753 String8 s("");
754 bool first = true;
755 for (userid_t i : intSet) {
756 if (first) {
757 s.appendFormat("%d", i);
758 first = false;
759 } else {
760 s.appendFormat(", %d", i);
761 }
762 }
763 return s;
764}
765
702status_t CameraService::initializeShimMetadata(int cameraId) {
703 int uid = getCallingUid();
704
705 String16 internalPackageName("media");
706 String8 id = String8::format("%d", cameraId);
707 status_t ret = NO_ERROR;
708 sp<Client> tmp = nullptr;
709 if ((ret = connectHelper<ICameraClient,Client>(sp<ICameraClient>{nullptr}, id,

--- 91 unchanged lines hidden (view full) ---

801 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
802 cameraId.string());
803 return -ENODEV;
804 }
805
806 // Check device policy for this camera
807 char value[PROPERTY_VALUE_MAX];
808 char key[PROPERTY_KEY_MAX];
766status_t CameraService::initializeShimMetadata(int cameraId) {
767 int uid = getCallingUid();
768
769 String16 internalPackageName("media");
770 String8 id = String8::format("%d", cameraId);
771 status_t ret = NO_ERROR;
772 sp<Client> tmp = nullptr;
773 if ((ret = connectHelper<ICameraClient,Client>(sp<ICameraClient>{nullptr}, id,

--- 91 unchanged lines hidden (view full) ---

865 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
866 cameraId.string());
867 return -ENODEV;
868 }
869
870 // Check device policy for this camera
871 char value[PROPERTY_VALUE_MAX];
872 char key[PROPERTY_KEY_MAX];
809 int clientUserId = multiuser_get_user_id(clientUid);
873 userid_t clientUserId = multiuser_get_user_id(clientUid);
810 snprintf(key, PROPERTY_KEY_MAX, "sys.secpolicy.camera.off_%d", clientUserId);
811 property_get(key, value, "0");
812 if (strcmp(value, "1") == 0) {
813 // Camera is disabled by DevicePolicyManager.
814 ALOGE("CameraService::connect X (PID %d) rejected (camera %s is disabled by device "
815 "policy)", callingPid, cameraId.string());
816 return -EACCES;
817 }
818
819 // Only allow clients who are being used by the current foreground device user, unless calling
820 // from our own process.
874 snprintf(key, PROPERTY_KEY_MAX, "sys.secpolicy.camera.off_%d", clientUserId);
875 property_get(key, value, "0");
876 if (strcmp(value, "1") == 0) {
877 // Camera is disabled by DevicePolicyManager.
878 ALOGE("CameraService::connect X (PID %d) rejected (camera %s is disabled by device "
879 "policy)", callingPid, cameraId.string());
880 return -EACCES;
881 }
882
883 // Only allow clients who are being used by the current foreground device user, unless calling
884 // from our own process.
821 if (callingPid != getpid() &&
822 (mLastUserId != clientUserId && mLastUserId != DEFAULT_LAST_USER_ID)) {
823 ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from previous "
824 "device user %d, current device user %d)", callingPid, clientUserId, mLastUserId);
885 if (callingPid != getpid() && (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) {
886 ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from "
887 "device user %d, currently allowed device users: %s)", callingPid, clientUserId,
888 toString(mAllowedUsers).string());
825 return PERMISSION_DENIED;
826 }
827
828 return checkIfDeviceIsUsable(cameraId);
829}
830
831status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const {
832 auto cameraState = getCameraState(cameraId);

--- 53 unchanged lines hidden (view full) ---

886 if (effectiveApiLevel == API_1) {
887 // If we are using API1, any existing client for this camera ID with the same remote
888 // should be returned rather than evicted to allow MediaRecorder to work properly.
889
890 auto current = mActiveClientManager.get(cameraId);
891 if (current != nullptr) {
892 auto clientSp = current->getValue();
893 if (clientSp.get() != nullptr) { // should never be needed
889 return PERMISSION_DENIED;
890 }
891
892 return checkIfDeviceIsUsable(cameraId);
893}
894
895status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const {
896 auto cameraState = getCameraState(cameraId);

--- 53 unchanged lines hidden (view full) ---

950 if (effectiveApiLevel == API_1) {
951 // If we are using API1, any existing client for this camera ID with the same remote
952 // should be returned rather than evicted to allow MediaRecorder to work properly.
953
954 auto current = mActiveClientManager.get(cameraId);
955 if (current != nullptr) {
956 auto clientSp = current->getValue();
957 if (clientSp.get() != nullptr) { // should never be needed
894 if (clientSp->getRemote() == remoteCallback) {
958 if (!clientSp->canCastToApiClient(effectiveApiLevel)) {
959 ALOGW("CameraService connect called from same client, but with a different"
960 " API level, evicting prior client...");
961 } else if (clientSp->getRemote() == remoteCallback) {
895 ALOGI("CameraService::connect X (PID %d) (second call from same"
962 ALOGI("CameraService::connect X (PID %d) (second call from same"
896 "app binder, returning the same client)", clientPid);
963 " app binder, returning the same client)", clientPid);
897 *client = clientSp;
898 return NO_ERROR;
899 }
900 }
901 }
902 }
903
904 // Return error if the device was unplugged or removed by the HAL for some reason

--- 55 unchanged lines hidden (view full) ---

960 cameraId.string(), packageName.string(), clientPid,
961 getCameraPriorityFromProcState(priorities[priorities.size() - 1]));
962
963 for (auto& i : incompatibleClients) {
964 msg.appendFormat("\n - Blocked by existing device %s client for package %s"
965 "(PID %" PRId32 ", priority %" PRId32 ")", i->getKey().string(),
966 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
967 i->getPriority());
964 *client = clientSp;
965 return NO_ERROR;
966 }
967 }
968 }
969 }
970
971 // Return error if the device was unplugged or removed by the HAL for some reason

--- 55 unchanged lines hidden (view full) ---

1027 cameraId.string(), packageName.string(), clientPid,
1028 getCameraPriorityFromProcState(priorities[priorities.size() - 1]));
1029
1030 for (auto& i : incompatibleClients) {
1031 msg.appendFormat("\n - Blocked by existing device %s client for package %s"
1032 "(PID %" PRId32 ", priority %" PRId32 ")", i->getKey().string(),
1033 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
1034 i->getPriority());
1035 ALOGE(" Conflicts with: Device %s, client package %s (PID %"
1036 PRId32 ", priority %" PRId32 ")", i->getKey().string(),
1037 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
1038 i->getPriority());
968 }
969
970 // Log the client's attempt
971 Mutex::Autolock l(mLogLock);
972 mEventLog.add(msg);
973
974 return -EBUSY;
975 }

--- 162 unchanged lines hidden (view full) ---

1138 }
1139
1140 device = client;
1141 return NO_ERROR;
1142}
1143
1144status_t CameraService::setTorchMode(const String16& cameraId, bool enabled,
1145 const sp<IBinder>& clientBinder) {
1039 }
1040
1041 // Log the client's attempt
1042 Mutex::Autolock l(mLogLock);
1043 mEventLog.add(msg);
1044
1045 return -EBUSY;
1046 }

--- 162 unchanged lines hidden (view full) ---

1209 }
1210
1211 device = client;
1212 return NO_ERROR;
1213}
1214
1215status_t CameraService::setTorchMode(const String16& cameraId, bool enabled,
1216 const sp<IBinder>& clientBinder) {
1146 if (enabled && clientBinder == NULL) {
1217 if (enabled && clientBinder == nullptr) {
1147 ALOGE("%s: torch client binder is NULL", __FUNCTION__);
1148 return -EINVAL;
1149 }
1150
1151 String8 id = String8(cameraId.string());
1218 ALOGE("%s: torch client binder is NULL", __FUNCTION__);
1219 return -EINVAL;
1220 }
1221
1222 String8 id = String8(cameraId.string());
1223 int uid = getCallingUid();
1152
1153 // verify id is valid.
1154 auto state = getCameraState(id);
1155 if (state == nullptr) {
1224
1225 // verify id is valid.
1226 auto state = getCameraState(id);
1227 if (state == nullptr) {
1156 ALOGE("%s: camera id is invalid %s", id.string());
1228 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
1157 return -EINVAL;
1158 }
1159
1160 ICameraServiceListener::Status cameraStatus = state->getStatus();
1161 if (cameraStatus != ICameraServiceListener::STATUS_PRESENT &&
1162 cameraStatus != ICameraServiceListener::STATUS_NOT_AVAILABLE) {
1229 return -EINVAL;
1230 }
1231
1232 ICameraServiceListener::Status cameraStatus = state->getStatus();
1233 if (cameraStatus != ICameraServiceListener::STATUS_PRESENT &&
1234 cameraStatus != ICameraServiceListener::STATUS_NOT_AVAILABLE) {
1163 ALOGE("%s: camera id is invalid %s", id.string());
1235 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
1164 return -EINVAL;
1165 }
1166
1167 {
1168 Mutex::Autolock al(mTorchStatusMutex);
1169 ICameraServiceListener::TorchStatus status;
1170 status_t res = getTorchStatusLocked(id, &status);
1171 if (res) {

--- 10 unchanged lines hidden (view full) ---

1182 } else {
1183 ALOGE("%s: torch mode of camera %s is not available due to "
1184 "insufficient resources", __FUNCTION__, id.string());
1185 return -EUSERS;
1186 }
1187 }
1188 }
1189
1236 return -EINVAL;
1237 }
1238
1239 {
1240 Mutex::Autolock al(mTorchStatusMutex);
1241 ICameraServiceListener::TorchStatus status;
1242 status_t res = getTorchStatusLocked(id, &status);
1243 if (res) {

--- 10 unchanged lines hidden (view full) ---

1254 } else {
1255 ALOGE("%s: torch mode of camera %s is not available due to "
1256 "insufficient resources", __FUNCTION__, id.string());
1257 return -EUSERS;
1258 }
1259 }
1260 }
1261
1262 {
1263 // Update UID map - this is used in the torch status changed callbacks, so must be done
1264 // before setTorchMode
1265 Mutex::Autolock al(mTorchUidMapMutex);
1266 if (mTorchUidMap.find(id) == mTorchUidMap.end()) {
1267 mTorchUidMap[id].first = uid;
1268 mTorchUidMap[id].second = uid;
1269 } else {
1270 // Set the pending UID
1271 mTorchUidMap[id].first = uid;
1272 }
1273 }
1274
1190 status_t res = mFlashlight->setTorchMode(id, enabled);
1275 status_t res = mFlashlight->setTorchMode(id, enabled);
1276
1191 if (res) {
1192 ALOGE("%s: setting torch mode of camera %s to %d failed. %s (%d)",
1193 __FUNCTION__, id.string(), enabled, strerror(-res), res);
1194 return res;
1195 }
1196
1197 {
1198 // update the link to client's death
1199 Mutex::Autolock al(mTorchClientMapMutex);
1200 ssize_t index = mTorchClientMap.indexOfKey(id);
1277 if (res) {
1278 ALOGE("%s: setting torch mode of camera %s to %d failed. %s (%d)",
1279 __FUNCTION__, id.string(), enabled, strerror(-res), res);
1280 return res;
1281 }
1282
1283 {
1284 // update the link to client's death
1285 Mutex::Autolock al(mTorchClientMapMutex);
1286 ssize_t index = mTorchClientMap.indexOfKey(id);
1287 BatteryNotifier& notifier(BatteryNotifier::getInstance());
1201 if (enabled) {
1202 if (index == NAME_NOT_FOUND) {
1203 mTorchClientMap.add(id, clientBinder);
1204 } else {
1288 if (enabled) {
1289 if (index == NAME_NOT_FOUND) {
1290 mTorchClientMap.add(id, clientBinder);
1291 } else {
1205 const sp<IBinder> oldBinder = mTorchClientMap.valueAt(index);
1206 oldBinder->unlinkToDeath(this);
1207
1292 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
1208 mTorchClientMap.replaceValueAt(index, clientBinder);
1209 }
1210 clientBinder->linkToDeath(this);
1211 } else if (index != NAME_NOT_FOUND) {
1293 mTorchClientMap.replaceValueAt(index, clientBinder);
1294 }
1295 clientBinder->linkToDeath(this);
1296 } else if (index != NAME_NOT_FOUND) {
1212 sp<IBinder> oldBinder = mTorchClientMap.valueAt(index);
1213 oldBinder->unlinkToDeath(this);
1297 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
1214 }
1215 }
1216
1217 return OK;
1218}
1219
1298 }
1299 }
1300
1301 return OK;
1302}
1303
1220void CameraService::notifySystemEvent(int eventId, int arg0) {
1304void CameraService::notifySystemEvent(int32_t eventId, const int32_t* args, size_t length) {
1221 switch(eventId) {
1222 case ICameraService::USER_SWITCHED: {
1305 switch(eventId) {
1306 case ICameraService::USER_SWITCHED: {
1223 doUserSwitch(/*newUserId*/arg0);
1307 doUserSwitch(/*newUserIds*/args, /*length*/length);
1224 break;
1225 }
1226 case ICameraService::NO_EVENT:
1227 default: {
1228 ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__,
1229 eventId);
1230 break;
1231 }
1232 }
1233}
1234
1308 break;
1309 }
1310 case ICameraService::NO_EVENT:
1311 default: {
1312 ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__,
1313 eventId);
1314 break;
1315 }
1316 }
1317}
1318
1235status_t CameraService::addListener(
1236 const sp<ICameraServiceListener>& listener) {
1319status_t CameraService::addListener(const sp<ICameraServiceListener>& listener) {
1237 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
1238
1320 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
1321
1239 if (listener == 0) {
1322 if (listener == nullptr) {
1240 ALOGE("%s: Listener must not be null", __FUNCTION__);
1241 return BAD_VALUE;
1242 }
1243
1244 Mutex::Autolock lock(mServiceLock);
1245
1246 {
1247 Mutex::Autolock lock(mStatusListenerLock);

--- 185 unchanged lines hidden (view full) ---

1433 mServiceLock.lock();
1434
1435 } // lock is destroyed, allow further connect calls
1436
1437 return ret;
1438}
1439
1440
1323 ALOGE("%s: Listener must not be null", __FUNCTION__);
1324 return BAD_VALUE;
1325 }
1326
1327 Mutex::Autolock lock(mServiceLock);
1328
1329 {
1330 Mutex::Autolock lock(mStatusListenerLock);

--- 185 unchanged lines hidden (view full) ---

1516 mServiceLock.lock();
1517
1518 } // lock is destroyed, allow further connect calls
1519
1520 return ret;
1521}
1522
1523
1524/**
1525 * Check camera capabilities, such as support for basic color operation
1526 */
1527int CameraService::checkCameraCapabilities(int id, camera_info info, int *latestStrangeCameraId) {
1528
1529 // Assume all devices pre-v3.3 are backward-compatible
1530 bool isBackwardCompatible = true;
1531 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0
1532 && info.device_version >= CAMERA_DEVICE_API_VERSION_3_3) {
1533 isBackwardCompatible = false;
1534 status_t res;
1535 camera_metadata_ro_entry_t caps;
1536 res = find_camera_metadata_ro_entry(
1537 info.static_camera_characteristics,
1538 ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
1539 &caps);
1540 if (res != 0) {
1541 ALOGW("%s: Unable to find camera capabilities for camera device %d",
1542 __FUNCTION__, id);
1543 caps.count = 0;
1544 }
1545 for (size_t i = 0; i < caps.count; i++) {
1546 if (caps.data.u8[i] ==
1547 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) {
1548 isBackwardCompatible = true;
1549 break;
1550 }
1551 }
1552 }
1553
1554 if (!isBackwardCompatible) {
1555 mNumberOfNormalCameras--;
1556 *latestStrangeCameraId = id;
1557 } else {
1558 if (id > *latestStrangeCameraId) {
1559 ALOGE("%s: Normal camera ID %d higher than strange camera ID %d. "
1560 "This is not allowed due backward-compatibility requirements",
1561 __FUNCTION__, id, *latestStrangeCameraId);
1562 logServiceError("Invalid order of camera devices", ENODEV);
1563 mNumberOfCameras = 0;
1564 mNumberOfNormalCameras = 0;
1565 return INVALID_OPERATION;
1566 }
1567 }
1568 return OK;
1569}
1570
1441std::shared_ptr<CameraService::CameraState> CameraService::getCameraState(
1442 const String8& cameraId) const {
1443 std::shared_ptr<CameraState> state;
1444 {
1445 Mutex::Autolock lock(mCameraStatesLock);
1446 auto iter = mCameraStates.find(cameraId);
1447 if (iter != mCameraStates.end()) {
1448 state = iter->second;

--- 9 unchanged lines hidden (view full) ---

1458 ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__,
1459 cameraId.string());
1460 return sp<BasicClient>{nullptr};
1461 }
1462
1463 return clientDescriptorPtr->getValue();
1464}
1465
1571std::shared_ptr<CameraService::CameraState> CameraService::getCameraState(
1572 const String8& cameraId) const {
1573 std::shared_ptr<CameraState> state;
1574 {
1575 Mutex::Autolock lock(mCameraStatesLock);
1576 auto iter = mCameraStates.find(cameraId);
1577 if (iter != mCameraStates.end()) {
1578 state = iter->second;

--- 9 unchanged lines hidden (view full) ---

1588 ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__,
1589 cameraId.string());
1590 return sp<BasicClient>{nullptr};
1591 }
1592
1593 return clientDescriptorPtr->getValue();
1594}
1595
1466void CameraService::doUserSwitch(int newUserId) {
1596void CameraService::doUserSwitch(const int32_t* newUserId, size_t length) {
1467 // Acquire mServiceLock and prevent other clients from connecting
1468 std::unique_ptr<AutoConditionLock> lock =
1469 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
1470
1597 // Acquire mServiceLock and prevent other clients from connecting
1598 std::unique_ptr<AutoConditionLock> lock =
1599 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
1600
1471 if (newUserId <= 0) {
1472 ALOGW("%s: Bad user ID %d given during user switch, resetting to default.", __FUNCTION__,
1473 newUserId);
1474 newUserId = DEFAULT_LAST_USER_ID;
1601 std::set<userid_t> newAllowedUsers;
1602 for (size_t i = 0; i < length; i++) {
1603 if (newUserId[i] < 0) {
1604 ALOGE("%s: Bad user ID %d given during user switch, ignoring.",
1605 __FUNCTION__, newUserId[i]);
1606 return;
1607 }
1608 newAllowedUsers.insert(static_cast<userid_t>(newUserId[i]));
1475 }
1476
1609 }
1610
1477 logUserSwitch(mLastUserId, newUserId);
1478
1611
1479 mLastUserId = newUserId;
1612 if (newAllowedUsers == mAllowedUsers) {
1613 ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__);
1614 return;
1615 }
1480
1616
1617 logUserSwitch(mAllowedUsers, newAllowedUsers);
1618
1619 mAllowedUsers = std::move(newAllowedUsers);
1620
1481 // Current user has switched, evict all current clients.
1482 std::vector<sp<BasicClient>> evicted;
1483 for (auto& i : mActiveClientManager.getAll()) {
1484 auto clientSp = i->getValue();
1485
1486 if (clientSp.get() == nullptr) {
1487 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1488 continue;
1489 }
1490
1621 // Current user has switched, evict all current clients.
1622 std::vector<sp<BasicClient>> evicted;
1623 for (auto& i : mActiveClientManager.getAll()) {
1624 auto clientSp = i->getValue();
1625
1626 if (clientSp.get() == nullptr) {
1627 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1628 continue;
1629 }
1630
1631 // Don't evict clients that are still allowed.
1632 uid_t clientUid = clientSp->getClientUid();
1633 userid_t clientUserId = multiuser_get_user_id(clientUid);
1634 if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) {
1635 continue;
1636 }
1637
1491 evicted.push_back(clientSp);
1492
1493 String8 curTime = getFormattedCurrentTime();
1494
1495 ALOGE("Evicting conflicting client for camera ID %s due to user change",
1496 i->getKey().string());
1497
1498 // Log the clients evicted

--- 43 unchanged lines hidden (view full) ---

1542
1543void CameraService::logRejected(const char* cameraId, int clientPid,
1544 const char* clientPackage, const char* reason) {
1545 // Log the client rejected
1546 logEvent(String8::format("REJECT device %s client for package %s (PID %d), reason: (%s)",
1547 cameraId, clientPackage, clientPid, reason));
1548}
1549
1638 evicted.push_back(clientSp);
1639
1640 String8 curTime = getFormattedCurrentTime();
1641
1642 ALOGE("Evicting conflicting client for camera ID %s due to user change",
1643 i->getKey().string());
1644
1645 // Log the clients evicted

--- 43 unchanged lines hidden (view full) ---

1689
1690void CameraService::logRejected(const char* cameraId, int clientPid,
1691 const char* clientPackage, const char* reason) {
1692 // Log the client rejected
1693 logEvent(String8::format("REJECT device %s client for package %s (PID %d), reason: (%s)",
1694 cameraId, clientPackage, clientPid, reason));
1695}
1696
1550void CameraService::logUserSwitch(int oldUserId, int newUserId) {
1697void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds,
1698 const std::set<userid_t>& newUserIds) {
1699 String8 newUsers = toString(newUserIds);
1700 String8 oldUsers = toString(oldUserIds);
1551 // Log the new and old users
1701 // Log the new and old users
1552 logEvent(String8::format("USER_SWITCH from old user: %d , to new user: %d", oldUserId,
1553 newUserId));
1702 logEvent(String8::format("USER_SWITCH previous allowed users: %s , current allowed users: %s",
1703 oldUsers.string(), newUsers.string()));
1554}
1555
1556void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) {
1557 // Log the device removal
1558 logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason));
1559}
1560
1561void CameraService::logDeviceAdded(const char* cameraId, const char* reason) {

--- 188 unchanged lines hidden (view full) ---

1750 return mClientPackageName;
1751}
1752
1753
1754int CameraService::BasicClient::getClientPid() const {
1755 return mClientPid;
1756}
1757
1704}
1705
1706void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) {
1707 // Log the device removal
1708 logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason));
1709}
1710
1711void CameraService::logDeviceAdded(const char* cameraId, const char* reason) {

--- 188 unchanged lines hidden (view full) ---

1900 return mClientPackageName;
1901}
1902
1903
1904int CameraService::BasicClient::getClientPid() const {
1905 return mClientPid;
1906}
1907
1908uid_t CameraService::BasicClient::getClientUid() const {
1909 return mClientUid;
1910}
1911
1912bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const {
1913 // Defaults to API2.
1914 return level == API_2;
1915}
1916
1758status_t CameraService::BasicClient::startCameraOps() {
1759 int32_t res;
1760 // Notify app ops that the camera is not available
1761 mOpsCallback = new OpsCallback(this);
1762
1763 {
1764 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
1765 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
1766 }
1767
1768 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
1769 mClientPackageName, mOpsCallback);
1770 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
1771 mClientUid, mClientPackageName);
1772
1917status_t CameraService::BasicClient::startCameraOps() {
1918 int32_t res;
1919 // Notify app ops that the camera is not available
1920 mOpsCallback = new OpsCallback(this);
1921
1922 {
1923 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
1924 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
1925 }
1926
1927 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
1928 mClientPackageName, mOpsCallback);
1929 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
1930 mClientUid, mClientPackageName);
1931
1773 if (res != AppOpsManager::MODE_ALLOWED) {
1932 if (res == AppOpsManager::MODE_ERRORED) {
1774 ALOGI("Camera %d: Access for \"%s\" has been revoked",
1775 mCameraId, String8(mClientPackageName).string());
1776 return PERMISSION_DENIED;
1777 }
1778
1933 ALOGI("Camera %d: Access for \"%s\" has been revoked",
1934 mCameraId, String8(mClientPackageName).string());
1935 return PERMISSION_DENIED;
1936 }
1937
1938 if (res == AppOpsManager::MODE_IGNORED) {
1939 ALOGI("Camera %d: Access for \"%s\" has been restricted",
1940 mCameraId, String8(mClientPackageName).string());
1941 // Return the same error as for device policy manager rejection
1942 return -EACCES;
1943 }
1944
1779 mOpsActive = true;
1780
1781 // Transition device availability listeners from PRESENT -> NOT_AVAILABLE
1782 mCameraService->updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE,
1783 String8::format("%d", mCameraId));
1784
1785 return OK;
1786}

--- 75 unchanged lines hidden (view full) ---

1862}
1863
1864// NOTE: function is idempotent
1865void CameraService::Client::disconnect() {
1866 ALOGV("Client::disconnect");
1867 BasicClient::disconnect();
1868}
1869
1945 mOpsActive = true;
1946
1947 // Transition device availability listeners from PRESENT -> NOT_AVAILABLE
1948 mCameraService->updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE,
1949 String8::format("%d", mCameraId));
1950
1951 return OK;
1952}

--- 75 unchanged lines hidden (view full) ---

2028}
2029
2030// NOTE: function is idempotent
2031void CameraService::Client::disconnect() {
2032 ALOGV("Client::disconnect");
2033 BasicClient::disconnect();
2034}
2035
2036bool CameraService::Client::canCastToApiClient(apiLevel level) const {
2037 return level == API_1;
2038}
2039
1870CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
1871 mClient(client) {
1872}
1873
1874void CameraService::Client::OpsCallback::opChanged(int32_t op,
1875 const String16& packageName) {
1876 sp<BasicClient> client = mClient.promote();
1877 if (client != NULL) {

--- 32 unchanged lines hidden (view full) ---

1910 return mConflicting;
1911}
1912
1913String8 CameraService::CameraState::getId() const {
1914 return mId;
1915}
1916
1917// ----------------------------------------------------------------------------
2040CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
2041 mClient(client) {
2042}
2043
2044void CameraService::Client::OpsCallback::opChanged(int32_t op,
2045 const String16& packageName) {
2046 sp<BasicClient> client = mClient.promote();
2047 if (client != NULL) {

--- 32 unchanged lines hidden (view full) ---

2080 return mConflicting;
2081}
2082
2083String8 CameraService::CameraState::getId() const {
2084 return mId;
2085}
2086
2087// ----------------------------------------------------------------------------
2088// ClientEventListener
2089// ----------------------------------------------------------------------------
2090
2091void CameraService::ClientEventListener::onClientAdded(
2092 const resource_policy::ClientDescriptor<String8,
2093 sp<CameraService::BasicClient>>& descriptor) {
2094 auto basicClient = descriptor.getValue();
2095 if (basicClient.get() != nullptr) {
2096 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2097 notifier.noteStartCamera(descriptor.getKey(),
2098 static_cast<int>(basicClient->getClientUid()));
2099 }
2100}
2101
2102void CameraService::ClientEventListener::onClientRemoved(
2103 const resource_policy::ClientDescriptor<String8,
2104 sp<CameraService::BasicClient>>& descriptor) {
2105 auto basicClient = descriptor.getValue();
2106 if (basicClient.get() != nullptr) {
2107 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2108 notifier.noteStopCamera(descriptor.getKey(),
2109 static_cast<int>(basicClient->getClientUid()));
2110 }
2111}
2112
2113
2114// ----------------------------------------------------------------------------
1918// CameraClientManager
1919// ----------------------------------------------------------------------------
1920
2115// CameraClientManager
2116// ----------------------------------------------------------------------------
2117
2118CameraService::CameraClientManager::CameraClientManager() {
2119 setListener(std::make_shared<ClientEventListener>());
2120}
2121
1921CameraService::CameraClientManager::~CameraClientManager() {}
1922
1923sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient(
1924 const String8& id) const {
1925 auto descriptor = get(id);
1926 if (descriptor == nullptr) {
1927 return sp<BasicClient>{nullptr};
1928 }

--- 8 unchanged lines hidden (view full) ---

1937 hasAny = true;
1938 String8 key = i->getKey();
1939 int32_t cost = i->getCost();
1940 int32_t pid = i->getOwnerId();
1941 int32_t priority = i->getPriority();
1942 auto conflicting = i->getConflicting();
1943 auto clientSp = i->getValue();
1944 String8 packageName;
2122CameraService::CameraClientManager::~CameraClientManager() {}
2123
2124sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient(
2125 const String8& id) const {
2126 auto descriptor = get(id);
2127 if (descriptor == nullptr) {
2128 return sp<BasicClient>{nullptr};
2129 }

--- 8 unchanged lines hidden (view full) ---

2138 hasAny = true;
2139 String8 key = i->getKey();
2140 int32_t cost = i->getCost();
2141 int32_t pid = i->getOwnerId();
2142 int32_t priority = i->getPriority();
2143 auto conflicting = i->getConflicting();
2144 auto clientSp = i->getValue();
2145 String8 packageName;
2146 userid_t clientUserId = 0;
1945 if (clientSp.get() != nullptr) {
1946 packageName = String8{clientSp->getPackageName()};
2147 if (clientSp.get() != nullptr) {
2148 packageName = String8{clientSp->getPackageName()};
2149 uid_t clientUid = clientSp->getClientUid();
2150 clientUserId = multiuser_get_user_id(clientUid);
1947 }
1948 ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Priority: %"
1949 PRId32 ", ", key.string(), cost, pid, priority);
1950
2151 }
2152 ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Priority: %"
2153 PRId32 ", ", key.string(), cost, pid, priority);
2154
2155 if (clientSp.get() != nullptr) {
2156 ret.appendFormat("User Id: %d, ", clientUserId);
2157 }
1951 if (packageName.size() != 0) {
1952 ret.appendFormat("Client Package Name: %s", packageName.string());
1953 }
1954
1955 ret.append(", Conflicting Client Devices: {");
1956 for (auto& j : conflicting) {
1957 ret.appendFormat("%s, ", j.string());
1958 }

--- 66 unchanged lines hidden (view full) ---

2025
2026 result = String8::format("Camera module HAL API version: 0x%x\n", mModule->getHalApiVersion());
2027 result.appendFormat("Camera module API version: 0x%x\n", mModule->getModuleApiVersion());
2028 result.appendFormat("Camera module name: %s\n", mModule->getModuleName());
2029 result.appendFormat("Camera module author: %s\n", mModule->getModuleAuthor());
2030 result.appendFormat("Number of camera devices: %d\n", mNumberOfCameras);
2031 String8 activeClientString = mActiveClientManager.toString();
2032 result.appendFormat("Active Camera Clients:\n%s", activeClientString.string());
2158 if (packageName.size() != 0) {
2159 ret.appendFormat("Client Package Name: %s", packageName.string());
2160 }
2161
2162 ret.append(", Conflicting Client Devices: {");
2163 for (auto& j : conflicting) {
2164 ret.appendFormat("%s, ", j.string());
2165 }

--- 66 unchanged lines hidden (view full) ---

2232
2233 result = String8::format("Camera module HAL API version: 0x%x\n", mModule->getHalApiVersion());
2234 result.appendFormat("Camera module API version: 0x%x\n", mModule->getModuleApiVersion());
2235 result.appendFormat("Camera module name: %s\n", mModule->getModuleName());
2236 result.appendFormat("Camera module author: %s\n", mModule->getModuleAuthor());
2237 result.appendFormat("Number of camera devices: %d\n", mNumberOfCameras);
2238 String8 activeClientString = mActiveClientManager.toString();
2239 result.appendFormat("Active Camera Clients:\n%s", activeClientString.string());
2240 result.appendFormat("Allowed users:\n%s\n", toString(mAllowedUsers).string());
2033
2034 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
2035 if (desc == NULL) {
2036 result.appendFormat("Vendor tags left unimplemented.\n");
2037 } else {
2038 result.appendFormat("Vendor tag definitions:\n");
2039 }
2040

--- 186 unchanged lines hidden (view full) ---

2227 return;
2228 }
2229
2230 // Update the status for this camera state, then send the onStatusChangedCallbacks to each
2231 // of the listeners with both the mStatusStatus and mStatusListenerLock held
2232 state->updateStatus(status, cameraId, rejectSourceStates, [this]
2233 (const String8& cameraId, ICameraServiceListener::Status status) {
2234
2241
2242 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
2243 if (desc == NULL) {
2244 result.appendFormat("Vendor tags left unimplemented.\n");
2245 } else {
2246 result.appendFormat("Vendor tag definitions:\n");
2247 }
2248

--- 186 unchanged lines hidden (view full) ---

2435 return;
2436 }
2437
2438 // Update the status for this camera state, then send the onStatusChangedCallbacks to each
2439 // of the listeners with both the mStatusStatus and mStatusListenerLock held
2440 state->updateStatus(status, cameraId, rejectSourceStates, [this]
2441 (const String8& cameraId, ICameraServiceListener::Status status) {
2442
2235 // Update torch status
2236 if (status == ICameraServiceListener::STATUS_NOT_PRESENT ||
2237 status == ICameraServiceListener::STATUS_NOT_AVAILABLE) {
2238 // Update torch status to not available when the camera device becomes not present
2239 // or not available.
2240 onTorchStatusChanged(cameraId, ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE);
2241 } else if (status == ICameraServiceListener::STATUS_PRESENT) {
2242 // Update torch status to available when the camera device becomes present or
2243 // available
2244 onTorchStatusChanged(cameraId, ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF);
2443 if (status != ICameraServiceListener::STATUS_ENUMERATING) {
2444 // Update torch status if it has a flash unit.
2445 Mutex::Autolock al(mTorchStatusMutex);
2446 ICameraServiceListener::TorchStatus torchStatus;
2447 if (getTorchStatusLocked(cameraId, &torchStatus) !=
2448 NAME_NOT_FOUND) {
2449 ICameraServiceListener::TorchStatus newTorchStatus =
2450 status == ICameraServiceListener::STATUS_PRESENT ?
2451 ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF :
2452 ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
2453 if (torchStatus != newTorchStatus) {
2454 onTorchStatusChangedLocked(cameraId, newTorchStatus);
2455 }
2456 }
2245 }
2246
2247 Mutex::Autolock lock(mStatusListenerLock);
2248
2249 for (auto& listener : mListenerList) {
2250 // TODO: Refactor status listeners to use strings for Camera IDs and remove this.
2251 int id = cameraIdToInt(cameraId);
2252 if (id != -1) listener->onStatusChanged(status, id);

--- 34 unchanged lines hidden ---
2457 }
2458
2459 Mutex::Autolock lock(mStatusListenerLock);
2460
2461 for (auto& listener : mListenerList) {
2462 // TODO: Refactor status listeners to use strings for Camera IDs and remove this.
2463 int id = cameraIdToInt(cameraId);
2464 if (id != -1) listener->onStatusChanged(status, id);

--- 34 unchanged lines hidden ---