blob: 92309947b292abe59443edadb7d98bea1e69a1d5 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
Ruben Brunkd1176ef2014-02-21 10:51:38 -08002 * 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
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 */
Mathias Agopian65ab4712010-07-14 17:59:35 -070016
17#define LOG_TAG "CameraService"
Iliyan Malchev8951a972011-04-14 16:55:59 -070018//#define LOG_NDEBUG 0
Mathias Agopian65ab4712010-07-14 17:59:35 -070019
Ruben Brunkcc776712015-02-17 20:18:47 -080020#include <algorithm>
21#include <climits>
Mathias Agopian65ab4712010-07-14 17:59:35 -070022#include <stdio.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080023#include <cstring>
24#include <ctime>
25#include <string>
Mathias Agopian65ab4712010-07-14 17:59:35 -070026#include <sys/types.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080027#include <inttypes.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070028#include <pthread.h>
29
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080030#include <binder/AppOpsManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070031#include <binder/IPCThreadState.h>
32#include <binder/IServiceManager.h>
33#include <binder/MemoryBase.h>
34#include <binder/MemoryHeapBase.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080035#include <binder/ProcessInfoService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070036#include <cutils/atomic.h>
Nipun Kwatrab5ca4612010-09-11 19:31:10 -070037#include <cutils/properties.h>
Mathias Agopiandf712ea2012-02-25 18:48:35 -080038#include <gui/Surface.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070039#include <hardware/hardware.h>
40#include <media/AudioSystem.h>
Andreas Huber1b86fe02014-01-29 11:13:26 -080041#include <media/IMediaHTTPService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070042#include <media/mediaplayer.h>
Ruben Brunk99e69712015-05-26 17:25:07 -070043#include <mediautils/BatteryNotifier.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070044#include <utils/Errors.h>
45#include <utils/Log.h>
46#include <utils/String16.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080047#include <utils/Trace.h>
48#include <system/camera_vendor_tags.h>
Ruben Brunkb2119af2014-05-09 19:57:56 -070049#include <system/camera_metadata.h>
50#include <system/camera.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070051
52#include "CameraService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070053#include "api1/CameraClient.h"
54#include "api1/Camera2Client.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070055#include "api2/CameraDeviceClient.h"
Igor Murashkinff3e31d2013-10-23 16:40:06 -070056#include "utils/CameraTraces.h"
Igor Murashkin98e24722013-06-19 19:51:04 -070057#include "CameraDeviceFactory.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070058
59namespace android {
60
61// ----------------------------------------------------------------------------
62// Logging support -- this is for debugging only
63// Use "adb shell dumpsys media.camera -v 1" to change it.
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070064volatile int32_t gLogLevel = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -070065
Steve Blockb8a80522011-12-20 16:23:08 +000066#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
67#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
Mathias Agopian65ab4712010-07-14 17:59:35 -070068
69static void setLogLevel(int level) {
70 android_atomic_write(level, &gLogLevel);
71}
72
73// ----------------------------------------------------------------------------
74
Igor Murashkincba2c162013-03-20 15:56:31 -070075extern "C" {
76static void camera_device_status_change(
77 const struct camera_module_callbacks* callbacks,
78 int camera_id,
79 int new_status) {
80 sp<CameraService> cs = const_cast<CameraService*>(
Ruben Brunkcc776712015-02-17 20:18:47 -080081 static_cast<const CameraService*>(callbacks));
Igor Murashkincba2c162013-03-20 15:56:31 -070082
Ruben Brunkcc776712015-02-17 20:18:47 -080083 cs->onDeviceStatusChanged(static_cast<camera_device_status_t>(camera_id),
84 static_cast<camera_device_status_t>(new_status));
Igor Murashkincba2c162013-03-20 15:56:31 -070085}
Chien-Yu Chen3068d732015-02-09 13:29:57 -080086
87static void torch_mode_status_change(
88 const struct camera_module_callbacks* callbacks,
89 const char* camera_id,
90 int new_status) {
91 if (!callbacks || !camera_id) {
92 ALOGE("%s invalid parameters. callbacks %p, camera_id %p", __FUNCTION__,
93 callbacks, camera_id);
94 }
95 sp<CameraService> cs = const_cast<CameraService*>(
96 static_cast<const CameraService*>(callbacks));
97
98 ICameraServiceListener::TorchStatus status;
99 switch (new_status) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800100 case TORCH_MODE_STATUS_NOT_AVAILABLE:
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800101 status = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
102 break;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800103 case TORCH_MODE_STATUS_AVAILABLE_OFF:
104 status = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF;
105 break;
106 case TORCH_MODE_STATUS_AVAILABLE_ON:
107 status = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800108 break;
109 default:
110 ALOGE("Unknown torch status %d", new_status);
111 return;
112 }
113
114 cs->onTorchStatusChanged(
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800115 String8(camera_id),
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800116 status);
117}
Igor Murashkincba2c162013-03-20 15:56:31 -0700118} // extern "C"
119
Mathias Agopian65ab4712010-07-14 17:59:35 -0700120// ----------------------------------------------------------------------------
121
122// This is ugly and only safe if we never re-create the CameraService, but
123// should be ok for now.
124static CameraService *gCameraService;
125
Ruben Brunk6267b532015-04-30 17:44:07 -0700126CameraService::CameraService() : mEventLog(DEFAULT_EVENT_LOG_LENGTH), mAllowedUsers(),
127 mSoundRef(0), mModule(0), mFlashlight(0) {
Steve Blockdf64d152012-01-04 20:05:49 +0000128 ALOGI("CameraService started (pid=%d)", getpid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700129 gCameraService = this;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800130
Igor Murashkincba2c162013-03-20 15:56:31 -0700131 this->camera_device_status_change = android::camera_device_status_change;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800132 this->torch_mode_status_change = android::torch_mode_status_change;
133
Ruben Brunkcc776712015-02-17 20:18:47 -0800134 mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700135}
136
Iliyan Malchev8951a972011-04-14 16:55:59 -0700137void CameraService::onFirstRef()
138{
Ruben Brunkcc776712015-02-17 20:18:47 -0800139 ALOGI("CameraService process starting");
Igor Murashkin634a5152013-02-20 17:15:11 -0800140
Iliyan Malchev8951a972011-04-14 16:55:59 -0700141 BnCameraService::onFirstRef();
142
Ruben Brunk99e69712015-05-26 17:25:07 -0700143 // Update battery life tracking if service is restarting
144 BatteryNotifier& notifier(BatteryNotifier::getInstance());
145 notifier.noteResetCamera();
146 notifier.noteResetFlashlight();
147
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800148 camera_module_t *rawModule;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700149 int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
150 (const hw_module_t **)&rawModule);
151 if (err < 0) {
152 ALOGE("Could not load camera HAL module: %d (%s)", err, strerror(-err));
153 logServiceError("Could not load camera HAL module", err);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700154 mNumberOfCameras = 0;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700155 return;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700156 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800157
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700158 mModule = new CameraModule(rawModule);
159 ALOGI("Loaded \"%s\" camera module", mModule->getModuleName());
160 err = mModule->init();
161 if (err != OK) {
162 ALOGE("Could not initialize camera HAL module: %d (%s)", err,
163 strerror(-err));
164 logServiceError("Could not initialize camera HAL module", err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800165
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700166 mNumberOfCameras = 0;
167 delete mModule;
168 mModule = nullptr;
169 return;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700170 }
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700171
172 mNumberOfCameras = mModule->getNumberOfCameras();
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700173 mNumberOfNormalCameras = mNumberOfCameras;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700174
175 mFlashlight = new CameraFlashlight(*mModule, *this);
176 status_t res = mFlashlight->findFlashUnits();
177 if (res) {
178 // impossible because we haven't open any camera devices.
179 ALOGE("Failed to find flash units.");
180 }
181
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700182 int latestStrangeCameraId = INT_MAX;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700183 for (int i = 0; i < mNumberOfCameras; i++) {
184 String8 cameraId = String8::format("%d", i);
185
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700186 // Get camera info
187
188 struct camera_info info;
189 bool haveInfo = true;
190 status_t rc = mModule->getCameraInfo(i, &info);
191 if (rc != NO_ERROR) {
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 haveInfo = false;
196 }
197
198 // Check for backwards-compatibility support
199 if (haveInfo) {
200 if (checkCameraCapabilities(i, info, &latestStrangeCameraId) != OK) {
201 delete mModule;
202 mModule = nullptr;
203 return;
204 }
205 }
206
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700207 // Defaults to use for cost and conflicting devices
208 int cost = 100;
209 char** conflicting_devices = nullptr;
210 size_t conflicting_devices_length = 0;
211
212 // If using post-2.4 module version, query the cost + conflicting devices from the HAL
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700213 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 && haveInfo) {
214 cost = info.resource_cost;
215 conflicting_devices = info.conflicting_devices;
216 conflicting_devices_length = info.conflicting_devices_length;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700217 }
218
219 std::set<String8> conflicting;
220 for (size_t i = 0; i < conflicting_devices_length; i++) {
221 conflicting.emplace(String8(conflicting_devices[i]));
222 }
223
224 // Initialize state for each camera device
225 {
226 Mutex::Autolock lock(mCameraStatesLock);
227 mCameraStates.emplace(cameraId, std::make_shared<CameraState>(cameraId, cost,
228 conflicting));
229 }
230
231 if (mFlashlight->hasFlashUnit(cameraId)) {
232 mTorchStatusMap.add(cameraId,
233 ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF);
234 }
235 }
236
237 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_1) {
238 mModule->setCallbacks(this);
239 }
240
241 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
242
243 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_2) {
244 setUpVendorTags();
245 }
246
247 CameraDeviceFactory::registerService(this);
Ruben Brunk2823ce02015-05-19 17:25:13 -0700248
249 CameraService::pingCameraServiceProxy();
250}
251
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700252sp<ICameraServiceProxy> CameraService::getCameraServiceProxy() {
Ruben Brunk2823ce02015-05-19 17:25:13 -0700253 sp<IServiceManager> sm = defaultServiceManager();
254 sp<IBinder> binder = sm->getService(String16("media.camera.proxy"));
255 if (binder == nullptr) {
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700256 return nullptr;
Ruben Brunk2823ce02015-05-19 17:25:13 -0700257 }
258 sp<ICameraServiceProxy> proxyBinder = interface_cast<ICameraServiceProxy>(binder);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700259 return proxyBinder;
260}
261
262void CameraService::pingCameraServiceProxy() {
263 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
264 if (proxyBinder == nullptr) return;
Ruben Brunk2823ce02015-05-19 17:25:13 -0700265 proxyBinder->pingForUserUpdate();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700266}
267
Mathias Agopian65ab4712010-07-14 17:59:35 -0700268CameraService::~CameraService() {
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800269 if (mModule) {
270 delete mModule;
Ruben Brunkcc776712015-02-17 20:18:47 -0800271 mModule = nullptr;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800272 }
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800273 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
Ruben Brunkcc776712015-02-17 20:18:47 -0800274 gCameraService = nullptr;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700275}
276
Ruben Brunkcc776712015-02-17 20:18:47 -0800277void CameraService::onDeviceStatusChanged(camera_device_status_t cameraId,
278 camera_device_status_t newStatus) {
279 ALOGI("%s: Status changed for cameraId=%d, newStatus=%d", __FUNCTION__,
Igor Murashkincba2c162013-03-20 15:56:31 -0700280 cameraId, newStatus);
281
Ruben Brunkcc776712015-02-17 20:18:47 -0800282 String8 id = String8::format("%d", cameraId);
283 std::shared_ptr<CameraState> state = getCameraState(id);
284
285 if (state == nullptr) {
Igor Murashkincba2c162013-03-20 15:56:31 -0700286 ALOGE("%s: Bad camera ID %d", __FUNCTION__, cameraId);
287 return;
288 }
289
Ruben Brunkcc776712015-02-17 20:18:47 -0800290 ICameraServiceListener::Status oldStatus = state->getStatus();
291
292 if (oldStatus == static_cast<ICameraServiceListener::Status>(newStatus)) {
293 ALOGE("%s: State transition to the same status %#x not allowed", __FUNCTION__, newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700294 return;
295 }
296
Igor Murashkincba2c162013-03-20 15:56:31 -0700297 if (newStatus == CAMERA_DEVICE_STATUS_NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700298 logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus,
299 newStatus));
Ruben Brunkcc776712015-02-17 20:18:47 -0800300 sp<BasicClient> clientToDisconnect;
Igor Murashkincba2c162013-03-20 15:56:31 -0700301 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800302 // Don't do this in updateStatus to avoid deadlock over mServiceLock
303 Mutex::Autolock lock(mServiceLock);
Igor Murashkincba2c162013-03-20 15:56:31 -0700304
Ruben Brunkcc776712015-02-17 20:18:47 -0800305 // Set the device status to NOT_PRESENT, clients will no longer be able to connect
306 // to this device until the status changes
307 updateStatus(ICameraServiceListener::STATUS_NOT_PRESENT, id);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700308
Ruben Brunkcc776712015-02-17 20:18:47 -0800309 // Remove cached shim parameters
310 state->setShimParams(CameraParameters());
Igor Murashkincba2c162013-03-20 15:56:31 -0700311
Ruben Brunkcc776712015-02-17 20:18:47 -0800312 // Remove the client from the list of active clients
313 clientToDisconnect = removeClientLocked(id);
314
315 // Notify the client of disconnection
316 clientToDisconnect->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
317 CaptureResultExtras{});
Igor Murashkincba2c162013-03-20 15:56:31 -0700318 }
319
Ruben Brunkcc776712015-02-17 20:18:47 -0800320 ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL",
321 __FUNCTION__, id.string());
Igor Murashkincba2c162013-03-20 15:56:31 -0700322
Ruben Brunkcc776712015-02-17 20:18:47 -0800323 // Disconnect client
324 if (clientToDisconnect.get() != nullptr) {
325 // Ensure not in binder RPC so client disconnect PID checks work correctly
326 LOG_ALWAYS_FATAL_IF(getCallingPid() != getpid(),
327 "onDeviceStatusChanged must be called from the camera service process!");
328 clientToDisconnect->disconnect();
Igor Murashkincba2c162013-03-20 15:56:31 -0700329 }
330
Ruben Brunkcc776712015-02-17 20:18:47 -0800331 } else {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700332 if (oldStatus == ICameraServiceListener::Status::STATUS_NOT_PRESENT) {
333 logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus,
334 newStatus));
335 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800336 updateStatus(static_cast<ICameraServiceListener::Status>(newStatus), id);
Igor Murashkincba2c162013-03-20 15:56:31 -0700337 }
338
Igor Murashkincba2c162013-03-20 15:56:31 -0700339}
340
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800341void CameraService::onTorchStatusChanged(const String8& cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800342 ICameraServiceListener::TorchStatus newStatus) {
343 Mutex::Autolock al(mTorchStatusMutex);
344 onTorchStatusChangedLocked(cameraId, newStatus);
345}
346
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800347void CameraService::onTorchStatusChangedLocked(const String8& cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800348 ICameraServiceListener::TorchStatus newStatus) {
349 ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d",
350 __FUNCTION__, cameraId.string(), newStatus);
351
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800352 ICameraServiceListener::TorchStatus status;
353 status_t res = getTorchStatusLocked(cameraId, &status);
354 if (res) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -0700355 ALOGE("%s: cannot get torch status of camera %s: %s (%d)",
356 __FUNCTION__, cameraId.string(), strerror(-res), res);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800357 return;
358 }
359 if (status == newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800360 return;
361 }
362
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800363 res = setTorchStatusLocked(cameraId, newStatus);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800364 if (res) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700365 ALOGE("%s: Failed to set the torch status", __FUNCTION__, (uint32_t)newStatus);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800366 return;
367 }
368
Ruben Brunkcc776712015-02-17 20:18:47 -0800369 {
Ruben Brunk99e69712015-05-26 17:25:07 -0700370 // Update battery life logging for flashlight
Chien-Yu Chenfe751be2015-09-01 14:16:44 -0700371 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -0700372 auto iter = mTorchUidMap.find(cameraId);
373 if (iter != mTorchUidMap.end()) {
374 int oldUid = iter->second.second;
375 int newUid = iter->second.first;
376 BatteryNotifier& notifier(BatteryNotifier::getInstance());
377 if (oldUid != newUid) {
378 // If the UID has changed, log the status and update current UID in mTorchUidMap
379 if (status == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) {
380 notifier.noteFlashlightOff(cameraId, oldUid);
381 }
382 if (newStatus == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) {
383 notifier.noteFlashlightOn(cameraId, newUid);
384 }
385 iter->second.second = newUid;
386 } else {
387 // If the UID has not changed, log the status
388 if (newStatus == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) {
389 notifier.noteFlashlightOn(cameraId, oldUid);
390 } else {
391 notifier.noteFlashlightOff(cameraId, oldUid);
392 }
393 }
394 }
395 }
396
397 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800398 Mutex::Autolock lock(mStatusListenerLock);
399 for (auto& i : mListenerList) {
400 i->onTorchStatusChanged(newStatus, String16{cameraId});
401 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800402 }
403}
404
Mathias Agopian65ab4712010-07-14 17:59:35 -0700405int32_t CameraService::getNumberOfCameras() {
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700406 return getNumberOfCameras(CAMERA_TYPE_BACKWARD_COMPATIBLE);
407}
408
409int32_t CameraService::getNumberOfCameras(int type) {
410 switch (type) {
411 case CAMERA_TYPE_BACKWARD_COMPATIBLE:
412 return mNumberOfNormalCameras;
413 case CAMERA_TYPE_ALL:
414 return mNumberOfCameras;
415 default:
416 ALOGW("%s: Unknown camera type %d, returning 0",
417 __FUNCTION__, type);
418 return 0;
419 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700420}
421
422status_t CameraService::getCameraInfo(int cameraId,
423 struct CameraInfo* cameraInfo) {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700424 if (!mModule) {
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700425 return -ENODEV;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700426 }
427
Mathias Agopian65ab4712010-07-14 17:59:35 -0700428 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
429 return BAD_VALUE;
430 }
431
Iliyan Malchev8951a972011-04-14 16:55:59 -0700432 struct camera_info info;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700433 status_t rc = filterGetInfoErrorCode(
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800434 mModule->getCameraInfo(cameraId, &info));
Iliyan Malchev8951a972011-04-14 16:55:59 -0700435 cameraInfo->facing = info.facing;
436 cameraInfo->orientation = info.orientation;
437 return rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700438}
439
Ruben Brunkcc776712015-02-17 20:18:47 -0800440int CameraService::cameraIdToInt(const String8& cameraId) {
441 errno = 0;
442 size_t pos = 0;
443 int ret = stoi(std::string{cameraId.string()}, &pos);
444 if (errno != 0 || pos != cameraId.size()) {
445 return -1;
446 }
447 return ret;
448}
Ruben Brunkb2119af2014-05-09 19:57:56 -0700449
450status_t CameraService::generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo) {
451 status_t ret = OK;
452 struct CameraInfo info;
453 if ((ret = getCameraInfo(cameraId, &info)) != OK) {
454 return ret;
455 }
456
457 CameraMetadata shimInfo;
458 int32_t orientation = static_cast<int32_t>(info.orientation);
459 if ((ret = shimInfo.update(ANDROID_SENSOR_ORIENTATION, &orientation, 1)) != OK) {
460 return ret;
461 }
462
463 uint8_t facing = (info.facing == CAMERA_FACING_FRONT) ?
464 ANDROID_LENS_FACING_FRONT : ANDROID_LENS_FACING_BACK;
465 if ((ret = shimInfo.update(ANDROID_LENS_FACING, &facing, 1)) != OK) {
466 return ret;
467 }
468
Igor Murashkin65d14b92014-06-17 12:03:20 -0700469 CameraParameters shimParams;
470 if ((ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)) != OK) {
471 // Error logged by callee
472 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700473 }
474
475 Vector<Size> sizes;
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700476 Vector<Size> jpegSizes;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700477 Vector<int32_t> formats;
478 const char* supportedPreviewFormats;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700479 {
480 shimParams.getSupportedPreviewSizes(/*out*/sizes);
481 shimParams.getSupportedPreviewFormats(/*out*/formats);
482 shimParams.getSupportedPictureSizes(/*out*/jpegSizes);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700483 }
484
485 // Always include IMPLEMENTATION_DEFINED
486 formats.add(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED);
487
488 const size_t INTS_PER_CONFIG = 4;
489
490 // Build available stream configurations metadata
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700491 size_t streamConfigSize = (sizes.size() * formats.size() + jpegSizes.size()) * INTS_PER_CONFIG;
492
493 Vector<int32_t> streamConfigs;
494 streamConfigs.setCapacity(streamConfigSize);
495
Ruben Brunkb2119af2014-05-09 19:57:56 -0700496 for (size_t i = 0; i < formats.size(); ++i) {
497 for (size_t j = 0; j < sizes.size(); ++j) {
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700498 streamConfigs.add(formats[i]);
499 streamConfigs.add(sizes[j].width);
500 streamConfigs.add(sizes[j].height);
501 streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700502 }
503 }
504
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700505 for (size_t i = 0; i < jpegSizes.size(); ++i) {
506 streamConfigs.add(HAL_PIXEL_FORMAT_BLOB);
507 streamConfigs.add(jpegSizes[i].width);
508 streamConfigs.add(jpegSizes[i].height);
509 streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT);
510 }
511
Ruben Brunkb2119af2014-05-09 19:57:56 -0700512 if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700513 streamConfigs.array(), streamConfigSize)) != OK) {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700514 return ret;
515 }
516
517 int64_t fakeMinFrames[0];
518 // TODO: Fixme, don't fake min frame durations.
519 if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
520 fakeMinFrames, 0)) != OK) {
521 return ret;
522 }
523
524 int64_t fakeStalls[0];
525 // TODO: Fixme, don't fake stall durations.
526 if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
527 fakeStalls, 0)) != OK) {
528 return ret;
529 }
530
531 *cameraInfo = shimInfo;
532 return OK;
533}
534
Zhijun He2b59be82013-09-25 10:14:30 -0700535status_t CameraService::getCameraCharacteristics(int cameraId,
536 CameraMetadata* cameraInfo) {
537 if (!cameraInfo) {
538 ALOGE("%s: cameraInfo is NULL", __FUNCTION__);
539 return BAD_VALUE;
540 }
541
542 if (!mModule) {
543 ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__);
544 return -ENODEV;
545 }
546
Zhijun He2b59be82013-09-25 10:14:30 -0700547 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
548 ALOGE("%s: Invalid camera id: %d", __FUNCTION__, cameraId);
549 return BAD_VALUE;
550 }
551
552 int facing;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700553 status_t ret = OK;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800554 if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0 ||
Ruben Brunkb2119af2014-05-09 19:57:56 -0700555 getDeviceVersion(cameraId, &facing) <= CAMERA_DEVICE_API_VERSION_2_1 ) {
556 /**
557 * Backwards compatibility mode for old HALs:
558 * - Convert CameraInfo into static CameraMetadata properties.
559 * - Retrieve cached CameraParameters for this camera. If none exist,
560 * attempt to open CameraClient and retrieve the CameraParameters.
561 * - Convert cached CameraParameters into static CameraMetadata
562 * properties.
563 */
564 ALOGI("%s: Switching to HAL1 shim implementation...", __FUNCTION__);
Zhijun He2b59be82013-09-25 10:14:30 -0700565
Ruben Brunkb2119af2014-05-09 19:57:56 -0700566 if ((ret = generateShimMetadata(cameraId, cameraInfo)) != OK) {
567 return ret;
568 }
Zhijun Hef05e50e2013-10-01 11:05:33 -0700569
Ruben Brunkb2119af2014-05-09 19:57:56 -0700570 } else {
571 /**
572 * Normal HAL 2.1+ codepath.
573 */
574 struct camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800575 ret = filterGetInfoErrorCode(mModule->getCameraInfo(cameraId, &info));
Ruben Brunkb2119af2014-05-09 19:57:56 -0700576 *cameraInfo = info.static_camera_characteristics;
577 }
Zhijun He2b59be82013-09-25 10:14:30 -0700578
579 return ret;
580}
581
Ruben Brunkcc776712015-02-17 20:18:47 -0800582int CameraService::getCallingPid() {
583 return IPCThreadState::self()->getCallingPid();
584}
585
586int CameraService::getCallingUid() {
587 return IPCThreadState::self()->getCallingUid();
588}
589
590String8 CameraService::getFormattedCurrentTime() {
591 time_t now = time(nullptr);
592 char formattedTime[64];
593 strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now));
594 return String8(formattedTime);
595}
596
597int CameraService::getCameraPriorityFromProcState(int procState) {
598 // Find the priority for the camera usage based on the process state. Higher priority clients
599 // win for evictions.
Ruben Brunkbe0b6b42015-05-12 16:10:52 -0700600 if (procState < 0) {
601 ALOGE("%s: Received invalid process state %d from ActivityManagerService!", __FUNCTION__,
602 procState);
603 return -1;
Ruben Brunkcc776712015-02-17 20:18:47 -0800604 }
Eino-Ville Talvala52aad852015-09-03 12:24:24 -0700605 // Treat sleeping TOP processes the same as regular TOP processes, for
606 // access priority. This is important for lock-screen camera launch scenarios
607 if (procState == PROCESS_STATE_TOP_SLEEPING) {
608 procState = PROCESS_STATE_TOP;
609 }
Ruben Brunkbe0b6b42015-05-12 16:10:52 -0700610 return INT_MAX - procState;
Ruben Brunkcc776712015-02-17 20:18:47 -0800611}
612
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800613status_t CameraService::getCameraVendorTagDescriptor(/*out*/sp<VendorTagDescriptor>& desc) {
614 if (!mModule) {
615 ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__);
616 return -ENODEV;
617 }
618
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800619 desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
620 return OK;
621}
622
Igor Murashkin634a5152013-02-20 17:15:11 -0800623int CameraService::getDeviceVersion(int cameraId, int* facing) {
624 struct camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800625 if (mModule->getCameraInfo(cameraId, &info) != OK) {
Igor Murashkin634a5152013-02-20 17:15:11 -0800626 return -1;
627 }
628
629 int deviceVersion;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800630 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
Igor Murashkin634a5152013-02-20 17:15:11 -0800631 deviceVersion = info.device_version;
632 } else {
633 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
634 }
635
636 if (facing) {
637 *facing = info.facing;
638 }
639
640 return deviceVersion;
641}
642
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700643status_t CameraService::filterGetInfoErrorCode(status_t err) {
644 switch(err) {
645 case NO_ERROR:
646 case -EINVAL:
647 return err;
648 default:
649 break;
650 }
651 return -ENODEV;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800652}
653
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800654bool CameraService::setUpVendorTags() {
655 vendor_tag_ops_t vOps = vendor_tag_ops_t();
656
657 // Check if vendor operations have been implemented
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800658 if (!mModule->isVendorTagDefined()) {
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800659 ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__);
660 return false;
661 }
662
663 ATRACE_BEGIN("camera3->get_metadata_vendor_tag_ops");
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800664 mModule->getVendorTagOps(&vOps);
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800665 ATRACE_END();
666
667 // Ensure all vendor operations are present
668 if (vOps.get_tag_count == NULL || vOps.get_all_tags == NULL ||
669 vOps.get_section_name == NULL || vOps.get_tag_name == NULL ||
670 vOps.get_tag_type == NULL) {
671 ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions."
672 , __FUNCTION__);
673 return false;
674 }
675
676 // Read all vendor tag definitions into a descriptor
677 sp<VendorTagDescriptor> desc;
678 status_t res;
679 if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc))
680 != OK) {
681 ALOGE("%s: Could not generate descriptor from vendor tag operations,"
682 "received error %s (%d). Camera clients will not be able to use"
683 "vendor tags", __FUNCTION__, strerror(res), res);
684 return false;
685 }
686
687 // Set the global descriptor to use with camera metadata
688 VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc);
689 return true;
690}
691
Ruben Brunkcc776712015-02-17 20:18:47 -0800692status_t CameraService::makeClient(const sp<CameraService>& cameraService,
693 const sp<IInterface>& cameraCb, const String16& packageName, const String8& cameraId,
694 int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode,
695 int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
696 /*out*/sp<BasicClient>* client) {
697
698 // TODO: Update CameraClients + HAL interface to use strings for Camera IDs
699 int id = cameraIdToInt(cameraId);
700 if (id == -1) {
701 ALOGE("%s: Invalid camera ID %s, cannot convert to integer.", __FUNCTION__,
702 cameraId.string());
703 return BAD_VALUE;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700704 }
705
Ruben Brunkcc776712015-02-17 20:18:47 -0800706 if (halVersion < 0 || halVersion == deviceVersion) {
707 // Default path: HAL version is unspecified by caller, create CameraClient
708 // based on device version reported by the HAL.
709 switch(deviceVersion) {
710 case CAMERA_DEVICE_API_VERSION_1_0:
711 if (effectiveApiLevel == API_1) { // Camera1 API route
712 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
713 *client = new CameraClient(cameraService, tmp, packageName, id, facing,
714 clientPid, clientUid, getpid(), legacyMode);
715 } else { // Camera2 API route
716 ALOGW("Camera using old HAL version: %d", deviceVersion);
717 return -EOPNOTSUPP;
718 }
719 break;
720 case CAMERA_DEVICE_API_VERSION_2_0:
721 case CAMERA_DEVICE_API_VERSION_2_1:
722 case CAMERA_DEVICE_API_VERSION_3_0:
723 case CAMERA_DEVICE_API_VERSION_3_1:
724 case CAMERA_DEVICE_API_VERSION_3_2:
725 case CAMERA_DEVICE_API_VERSION_3_3:
726 if (effectiveApiLevel == API_1) { // Camera1 API route
727 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
728 *client = new Camera2Client(cameraService, tmp, packageName, id, facing,
729 clientPid, clientUid, servicePid, legacyMode);
730 } else { // Camera2 API route
731 sp<ICameraDeviceCallbacks> tmp =
732 static_cast<ICameraDeviceCallbacks*>(cameraCb.get());
733 *client = new CameraDeviceClient(cameraService, tmp, packageName, id,
734 facing, clientPid, clientUid, servicePid);
735 }
736 break;
737 default:
738 // Should not be reachable
739 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
740 return INVALID_OPERATION;
741 }
742 } else {
743 // A particular HAL version is requested by caller. Create CameraClient
744 // based on the requested HAL version.
745 if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 &&
746 halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
747 // Only support higher HAL version device opened as HAL1.0 device.
748 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
749 *client = new CameraClient(cameraService, tmp, packageName, id, facing,
750 clientPid, clientUid, servicePid, legacyMode);
751 } else {
752 // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
753 ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
754 " opened as HAL %x device", halVersion, deviceVersion,
755 CAMERA_DEVICE_API_VERSION_1_0);
756 return INVALID_OPERATION;
757 }
758 }
759 return NO_ERROR;
760}
761
Ruben Brunk6267b532015-04-30 17:44:07 -0700762String8 CameraService::toString(std::set<userid_t> intSet) {
763 String8 s("");
764 bool first = true;
765 for (userid_t i : intSet) {
766 if (first) {
767 s.appendFormat("%d", i);
768 first = false;
769 } else {
770 s.appendFormat(", %d", i);
771 }
772 }
773 return s;
774}
775
Ruben Brunkcc776712015-02-17 20:18:47 -0800776status_t CameraService::initializeShimMetadata(int cameraId) {
777 int uid = getCallingUid();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700778
779 String16 internalPackageName("media");
Ruben Brunkcc776712015-02-17 20:18:47 -0800780 String8 id = String8::format("%d", cameraId);
781 status_t ret = NO_ERROR;
782 sp<Client> tmp = nullptr;
783 if ((ret = connectHelper<ICameraClient,Client>(sp<ICameraClient>{nullptr}, id,
784 static_cast<int>(CAMERA_HAL_API_VERSION_UNSPECIFIED), internalPackageName, uid, API_1,
785 false, true, tmp)) != NO_ERROR) {
786 ALOGE("%s: Error %d (%s) initializing shim metadata.", __FUNCTION__, ret, strerror(ret));
787 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700788 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800789 return NO_ERROR;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700790}
791
Igor Murashkin65d14b92014-06-17 12:03:20 -0700792status_t CameraService::getLegacyParametersLazy(int cameraId,
793 /*out*/
794 CameraParameters* parameters) {
795
796 ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId);
797
798 status_t ret = 0;
799
800 if (parameters == NULL) {
801 ALOGE("%s: parameters must not be null", __FUNCTION__);
802 return BAD_VALUE;
803 }
804
Ruben Brunkcc776712015-02-17 20:18:47 -0800805 String8 id = String8::format("%d", cameraId);
806
807 // Check if we already have parameters
808 {
809 // Scope for service lock
Igor Murashkin65d14b92014-06-17 12:03:20 -0700810 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -0800811 auto cameraState = getCameraState(id);
812 if (cameraState == nullptr) {
813 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
814 return BAD_VALUE;
815 }
816 CameraParameters p = cameraState->getShimParams();
817 if (!p.isEmpty()) {
818 *parameters = p;
819 return NO_ERROR;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700820 }
821 }
822
Ruben Brunkcc776712015-02-17 20:18:47 -0800823 int64_t token = IPCThreadState::self()->clearCallingIdentity();
824 ret = initializeShimMetadata(cameraId);
825 IPCThreadState::self()->restoreCallingIdentity(token);
826 if (ret != NO_ERROR) {
827 // Error already logged by callee
828 return ret;
829 }
830
831 // Check for parameters again
832 {
833 // Scope for service lock
834 Mutex::Autolock lock(mServiceLock);
835 auto cameraState = getCameraState(id);
836 if (cameraState == nullptr) {
837 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
838 return BAD_VALUE;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700839 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800840 CameraParameters p = cameraState->getShimParams();
841 if (!p.isEmpty()) {
842 *parameters = p;
843 return NO_ERROR;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700844 }
845 }
846
Ruben Brunkcc776712015-02-17 20:18:47 -0800847 ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.",
848 __FUNCTION__);
849 return INVALID_OPERATION;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700850}
851
Ruben Brunk36597b22015-03-20 22:15:57 -0700852status_t CameraService::validateConnectLocked(const String8& cameraId, /*inout*/int& clientUid)
853 const {
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800854
Mathias Agopian65ab4712010-07-14 17:59:35 -0700855 int callingPid = getCallingPid();
Tyler Luu5861a9a2011-10-06 00:00:03 -0500856
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800857 if (clientUid == USE_CALLING_UID) {
858 clientUid = getCallingUid();
859 } else {
860 // We only trust our own process to forward client UIDs
861 if (callingPid != getpid()) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700862 ALOGE("CameraService::connect X (PID %d) rejected (don't trust clientUid %d)",
863 callingPid, clientUid);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700864 return PERMISSION_DENIED;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800865 }
866 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700867
Iliyan Malchev8951a972011-04-14 16:55:59 -0700868 if (!mModule) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800869 ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)",
870 callingPid);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700871 return -ENODEV;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700872 }
873
Ruben Brunkcc776712015-02-17 20:18:47 -0800874 if (getCameraState(cameraId) == nullptr) {
875 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
876 cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700877 return -ENODEV;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700878 }
879
Ruben Brunkcc776712015-02-17 20:18:47 -0800880 // Check device policy for this camera
Wu-cheng Lia3355432011-05-20 14:54:25 +0800881 char value[PROPERTY_VALUE_MAX];
Amith Yamasani228711d2015-02-13 13:25:39 -0800882 char key[PROPERTY_KEY_MAX];
Ruben Brunk6267b532015-04-30 17:44:07 -0700883 userid_t clientUserId = multiuser_get_user_id(clientUid);
Amith Yamasani228711d2015-02-13 13:25:39 -0800884 snprintf(key, PROPERTY_KEY_MAX, "sys.secpolicy.camera.off_%d", clientUserId);
885 property_get(key, value, "0");
Wu-cheng Lia3355432011-05-20 14:54:25 +0800886 if (strcmp(value, "1") == 0) {
887 // Camera is disabled by DevicePolicyManager.
Ruben Brunkcc776712015-02-17 20:18:47 -0800888 ALOGE("CameraService::connect X (PID %d) rejected (camera %s is disabled by device "
889 "policy)", callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700890 return -EACCES;
Wu-cheng Lia3355432011-05-20 14:54:25 +0800891 }
892
Ruben Brunka8ca9152015-04-07 14:23:40 -0700893 // Only allow clients who are being used by the current foreground device user, unless calling
894 // from our own process.
Ruben Brunk6267b532015-04-30 17:44:07 -0700895 if (callingPid != getpid() && (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) {
896 ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from "
897 "device user %d, currently allowed device users: %s)", callingPid, clientUserId,
898 toString(mAllowedUsers).string());
Ruben Brunk36597b22015-03-20 22:15:57 -0700899 return PERMISSION_DENIED;
900 }
901
Ruben Brunkcc776712015-02-17 20:18:47 -0800902 return checkIfDeviceIsUsable(cameraId);
903}
904
905status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const {
906 auto cameraState = getCameraState(cameraId);
907 int callingPid = getCallingPid();
908 if (cameraState == nullptr) {
909 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
910 cameraId.string());
911 return -ENODEV;
912 }
913
914 ICameraServiceListener::Status currentStatus = cameraState->getStatus();
Igor Murashkincba2c162013-03-20 15:56:31 -0700915 if (currentStatus == ICameraServiceListener::STATUS_NOT_PRESENT) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800916 ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)",
917 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700918 return -ENODEV;
Igor Murashkincba2c162013-03-20 15:56:31 -0700919 } else if (currentStatus == ICameraServiceListener::STATUS_ENUMERATING) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800920 ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)",
921 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700922 return -EBUSY;
Igor Murashkincba2c162013-03-20 15:56:31 -0700923 }
Igor Murashkincba2c162013-03-20 15:56:31 -0700924
Ruben Brunkcc776712015-02-17 20:18:47 -0800925 return NO_ERROR;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800926}
927
Ruben Brunkcc776712015-02-17 20:18:47 -0800928void CameraService::finishConnectLocked(const sp<BasicClient>& client,
929 const CameraService::DescriptorPtr& desc) {
Igor Murashkine6800ce2013-03-04 17:25:57 -0800930
Ruben Brunkcc776712015-02-17 20:18:47 -0800931 // Make a descriptor for the incoming client
932 auto clientDescriptor = CameraService::CameraClientManager::makeClientDescriptor(client, desc);
933 auto evicted = mActiveClientManager.addAndEvict(clientDescriptor);
934
935 logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()),
936 String8(client->getPackageName()));
937
938 if (evicted.size() > 0) {
939 // This should never happen - clients should already have been removed in disconnect
940 for (auto& i : evicted) {
941 ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect",
942 __FUNCTION__, i->getKey().string());
943 }
944
945 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly",
946 __FUNCTION__);
947 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -0700948
949 // And register a death notification for the client callback. Do
950 // this last to avoid Binder policy where a nested Binder
951 // transaction might be pre-empted to service the client death
952 // notification if the client process dies before linkToDeath is
953 // invoked.
954 sp<IBinder> remoteCallback = client->getRemote();
955 if (remoteCallback != nullptr) {
956 remoteCallback->linkToDeath(this);
957 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800958}
959
960status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid,
961 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
962 /*out*/
963 sp<BasicClient>* client,
964 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) {
965
966 status_t ret = NO_ERROR;
Ruben Brunk4f9576b2015-04-10 17:26:56 -0700967 std::vector<DescriptorPtr> evictedClients;
Ruben Brunkcc776712015-02-17 20:18:47 -0800968 DescriptorPtr clientDescriptor;
969 {
970 if (effectiveApiLevel == API_1) {
971 // If we are using API1, any existing client for this camera ID with the same remote
972 // should be returned rather than evicted to allow MediaRecorder to work properly.
973
974 auto current = mActiveClientManager.get(cameraId);
975 if (current != nullptr) {
976 auto clientSp = current->getValue();
977 if (clientSp.get() != nullptr) { // should never be needed
Ruben Brunk0bbf8b22015-04-30 14:35:42 -0700978 if (!clientSp->canCastToApiClient(effectiveApiLevel)) {
979 ALOGW("CameraService connect called from same client, but with a different"
980 " API level, evicting prior client...");
981 } else if (clientSp->getRemote() == remoteCallback) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800982 ALOGI("CameraService::connect X (PID %d) (second call from same"
Ruben Brunk0bbf8b22015-04-30 14:35:42 -0700983 " app binder, returning the same client)", clientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -0800984 *client = clientSp;
985 return NO_ERROR;
986 }
987 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800988 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800989 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800990
Ruben Brunkcc776712015-02-17 20:18:47 -0800991 // Return error if the device was unplugged or removed by the HAL for some reason
992 if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) {
993 return ret;
994 }
Igor Murashkin634a5152013-02-20 17:15:11 -0800995
Ruben Brunkcc776712015-02-17 20:18:47 -0800996 // Get current active client PIDs
997 std::vector<int> ownerPids(mActiveClientManager.getAllOwners());
998 ownerPids.push_back(clientPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800999
Chih-Hung Hsieh54b42462015-03-19 12:04:54 -07001000 // Use the value +PROCESS_STATE_NONEXISTENT, to avoid taking
1001 // address of PROCESS_STATE_NONEXISTENT as a reference argument
1002 // for the vector constructor. PROCESS_STATE_NONEXISTENT does
1003 // not have an out-of-class definition.
1004 std::vector<int> priorities(ownerPids.size(), +PROCESS_STATE_NONEXISTENT);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001005
Ruben Brunkcc776712015-02-17 20:18:47 -08001006 // Get priorites of all active PIDs
1007 ProcessInfoService::getProcessStatesFromPids(ownerPids.size(), &ownerPids[0],
1008 /*out*/&priorities[0]);
Ruben Brunkb2119af2014-05-09 19:57:56 -07001009
Ruben Brunkcc776712015-02-17 20:18:47 -08001010 // Update all active clients' priorities
1011 std::map<int,int> pidToPriorityMap;
1012 for (size_t i = 0; i < ownerPids.size() - 1; i++) {
1013 pidToPriorityMap.emplace(ownerPids[i], getCameraPriorityFromProcState(priorities[i]));
1014 }
1015 mActiveClientManager.updatePriorities(pidToPriorityMap);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001016
Ruben Brunkcc776712015-02-17 20:18:47 -08001017 // Get state for the given cameraId
1018 auto state = getCameraState(cameraId);
1019 if (state == nullptr) {
1020 ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)",
1021 clientPid, cameraId.string());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001022 return BAD_VALUE;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001023 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001024
1025 // Make descriptor for incoming client
1026 clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId,
1027 sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()),
1028 state->getConflicting(),
1029 getCameraPriorityFromProcState(priorities[priorities.size() - 1]), clientPid);
1030
1031 // Find clients that would be evicted
1032 auto evicted = mActiveClientManager.wouldEvict(clientDescriptor);
1033
1034 // If the incoming client was 'evicted,' higher priority clients have the camera in the
1035 // background, so we cannot do evictions
1036 if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) {
1037 ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher"
1038 " priority).", clientPid);
1039
1040 sp<BasicClient> clientSp = clientDescriptor->getValue();
1041 String8 curTime = getFormattedCurrentTime();
1042 auto incompatibleClients =
1043 mActiveClientManager.getIncompatibleClients(clientDescriptor);
1044
1045 String8 msg = String8::format("%s : DENIED connect device %s client for package %s "
Ruben Brunka8ca9152015-04-07 14:23:40 -07001046 "(PID %d, priority %d) due to eviction policy", curTime.string(),
Ruben Brunkcc776712015-02-17 20:18:47 -08001047 cameraId.string(), packageName.string(), clientPid,
1048 getCameraPriorityFromProcState(priorities[priorities.size() - 1]));
1049
1050 for (auto& i : incompatibleClients) {
1051 msg.appendFormat("\n - Blocked by existing device %s client for package %s"
1052 "(PID %" PRId32 ", priority %" PRId32 ")", i->getKey().string(),
1053 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
1054 i->getPriority());
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001055 ALOGE(" Conflicts with: Device %s, client package %s (PID %"
1056 PRId32 ", priority %" PRId32 ")", i->getKey().string(),
1057 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
1058 i->getPriority());
Ruben Brunkcc776712015-02-17 20:18:47 -08001059 }
1060
1061 // Log the client's attempt
Ruben Brunka8ca9152015-04-07 14:23:40 -07001062 Mutex::Autolock l(mLogLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001063 mEventLog.add(msg);
1064
1065 return -EBUSY;
1066 }
1067
1068 for (auto& i : evicted) {
1069 sp<BasicClient> clientSp = i->getValue();
1070 if (clientSp.get() == nullptr) {
1071 ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__);
1072
1073 // TODO: Remove this
1074 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list",
1075 __FUNCTION__);
1076 mActiveClientManager.remove(i);
1077 continue;
1078 }
1079
1080 ALOGE("CameraService::connect evicting conflicting client for camera ID %s",
1081 i->getKey().string());
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001082 evictedClients.push_back(i);
Ruben Brunkcc776712015-02-17 20:18:47 -08001083
Ruben Brunkcc776712015-02-17 20:18:47 -08001084 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001085 logEvent(String8::format("EVICT device %s client held by package %s (PID"
1086 " %" PRId32 ", priority %" PRId32 ")\n - Evicted by device %s client for"
1087 " package %s (PID %d, priority %" PRId32 ")",
Ruben Brunkcc776712015-02-17 20:18:47 -08001088 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
1089 i->getOwnerId(), i->getPriority(), cameraId.string(),
1090 packageName.string(), clientPid,
1091 getCameraPriorityFromProcState(priorities[priorities.size() - 1])));
1092
1093 // Notify the client of disconnection
1094 clientSp->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
1095 CaptureResultExtras());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001096 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001097 }
1098
Ruben Brunkcc776712015-02-17 20:18:47 -08001099 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1100 // other clients from connecting in mServiceLockWrapper if held
1101 mServiceLock.unlock();
1102
1103 // Clear caller identity temporarily so client disconnect PID checks work correctly
1104 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1105
1106 // Destroy evicted clients
1107 for (auto& i : evictedClients) {
1108 // Disconnect is blocking, and should only have returned when HAL has cleaned up
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001109 i->getValue()->disconnect(); // Clients will remove themselves from the active client list
Ruben Brunkcc776712015-02-17 20:18:47 -08001110 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001111
1112 IPCThreadState::self()->restoreCallingIdentity(token);
1113
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001114 for (const auto& i : evictedClients) {
1115 ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")",
1116 __FUNCTION__, i->getKey().string(), i->getOwnerId());
1117 ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS);
1118 if (ret == TIMED_OUT) {
1119 ALOGE("%s: Timed out waiting for client for device %s to disconnect, "
1120 "current clients:\n%s", __FUNCTION__, i->getKey().string(),
1121 mActiveClientManager.toString().string());
1122 return -EBUSY;
1123 }
1124 if (ret != NO_ERROR) {
1125 ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), "
1126 "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret),
1127 ret, mActiveClientManager.toString().string());
1128 return ret;
1129 }
1130 }
1131
1132 evictedClients.clear();
1133
Ruben Brunkcc776712015-02-17 20:18:47 -08001134 // Once clients have been disconnected, relock
1135 mServiceLock.lock();
1136
1137 // Check again if the device was unplugged or something while we weren't holding mServiceLock
1138 if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) {
1139 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001140 }
1141
Ruben Brunkcc776712015-02-17 20:18:47 -08001142 *partial = clientDescriptor;
1143 return NO_ERROR;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001144}
1145
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001146status_t CameraService::connect(
Igor Murashkine6800ce2013-03-04 17:25:57 -08001147 const sp<ICameraClient>& cameraClient,
1148 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001149 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001150 int clientUid,
1151 /*out*/
1152 sp<ICamera>& device) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001153
Ruben Brunkcc776712015-02-17 20:18:47 -08001154 status_t ret = NO_ERROR;
1155 String8 id = String8::format("%d", cameraId);
1156 sp<Client> client = nullptr;
1157 ret = connectHelper<ICameraClient,Client>(cameraClient, id, CAMERA_HAL_API_VERSION_UNSPECIFIED,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001158 clientPackageName, clientUid, API_1, false, false, /*out*/client);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001159
Ruben Brunkcc776712015-02-17 20:18:47 -08001160 if(ret != NO_ERROR) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001161 logRejected(id, getCallingPid(), String8(clientPackageName),
Ruben Brunka8ca9152015-04-07 14:23:40 -07001162 String8::format("%s (%d)", strerror(-ret), ret));
Ruben Brunkcc776712015-02-17 20:18:47 -08001163 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001164 }
1165
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001166 device = client;
Ruben Brunkcc776712015-02-17 20:18:47 -08001167 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001168}
1169
Zhijun Heb10cdad2014-06-16 16:38:35 -07001170status_t CameraService::connectLegacy(
1171 const sp<ICameraClient>& cameraClient,
1172 int cameraId, int halVersion,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001173 const String16& clientPackageName,
Zhijun Heb10cdad2014-06-16 16:38:35 -07001174 int clientUid,
1175 /*out*/
1176 sp<ICamera>& device) {
1177
Ruben Brunka8ca9152015-04-07 14:23:40 -07001178 String8 id = String8::format("%d", cameraId);
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08001179 int apiVersion = mModule->getModuleApiVersion();
Igor Murashkin3d07d1a2014-06-20 11:27:03 -07001180 if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED &&
Yin-Chia Yehe074a932015-01-30 10:29:02 -08001181 apiVersion < CAMERA_MODULE_API_VERSION_2_3) {
Igor Murashkin3d07d1a2014-06-20 11:27:03 -07001182 /*
1183 * Either the HAL version is unspecified in which case this just creates
1184 * a camera client selected by the latest device version, or
1185 * it's a particular version in which case the HAL must supported
1186 * the open_legacy call
1187 */
Zhijun Heb10cdad2014-06-16 16:38:35 -07001188 ALOGE("%s: camera HAL module version %x doesn't support connecting to legacy HAL devices!",
Yin-Chia Yehe074a932015-01-30 10:29:02 -08001189 __FUNCTION__, apiVersion);
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001190 logRejected(id, getCallingPid(), String8(clientPackageName),
Ruben Brunka8ca9152015-04-07 14:23:40 -07001191 String8("HAL module version doesn't support legacy HAL connections"));
Zhijun Heb10cdad2014-06-16 16:38:35 -07001192 return INVALID_OPERATION;
1193 }
1194
Ruben Brunkcc776712015-02-17 20:18:47 -08001195 status_t ret = NO_ERROR;
Ruben Brunkcc776712015-02-17 20:18:47 -08001196 sp<Client> client = nullptr;
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001197 ret = connectHelper<ICameraClient,Client>(cameraClient, id, halVersion, clientPackageName,
Ruben Brunkcc776712015-02-17 20:18:47 -08001198 clientUid, API_1, true, false, /*out*/client);
Zhijun Heb10cdad2014-06-16 16:38:35 -07001199
Ruben Brunkcc776712015-02-17 20:18:47 -08001200 if(ret != NO_ERROR) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001201 logRejected(id, getCallingPid(), String8(clientPackageName),
Ruben Brunka8ca9152015-04-07 14:23:40 -07001202 String8::format("%s (%d)", strerror(-ret), ret));
Ruben Brunkcc776712015-02-17 20:18:47 -08001203 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001204 }
1205
Zhijun Heb10cdad2014-06-16 16:38:35 -07001206 device = client;
Ruben Brunkcc776712015-02-17 20:18:47 -08001207 return NO_ERROR;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001208}
1209
Ruben Brunkcc776712015-02-17 20:18:47 -08001210status_t CameraService::connectDevice(
1211 const sp<ICameraDeviceCallbacks>& cameraCb,
1212 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001213 const String16& clientPackageName,
Ruben Brunkcc776712015-02-17 20:18:47 -08001214 int clientUid,
1215 /*out*/
1216 sp<ICameraDeviceUser>& device) {
1217
1218 status_t ret = NO_ERROR;
1219 String8 id = String8::format("%d", cameraId);
1220 sp<CameraDeviceClient> client = nullptr;
1221 ret = connectHelper<ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001222 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName, clientUid, API_2, false, false,
Ruben Brunkcc776712015-02-17 20:18:47 -08001223 /*out*/client);
1224
1225 if(ret != NO_ERROR) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001226 logRejected(id, getCallingPid(), String8(clientPackageName),
Ruben Brunka8ca9152015-04-07 14:23:40 -07001227 String8::format("%s (%d)", strerror(-ret), ret));
Ruben Brunkcc776712015-02-17 20:18:47 -08001228 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001229 }
1230
Ruben Brunkcc776712015-02-17 20:18:47 -08001231 device = client;
1232 return NO_ERROR;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001233}
1234
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001235status_t CameraService::setTorchMode(const String16& cameraId, bool enabled,
1236 const sp<IBinder>& clientBinder) {
Ruben Brunk99e69712015-05-26 17:25:07 -07001237 if (enabled && clientBinder == nullptr) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001238 ALOGE("%s: torch client binder is NULL", __FUNCTION__);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001239 return -EINVAL;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001240 }
1241
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001242 String8 id = String8(cameraId.string());
Ruben Brunk99e69712015-05-26 17:25:07 -07001243 int uid = getCallingUid();
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001244
1245 // verify id is valid.
Ruben Brunkcc776712015-02-17 20:18:47 -08001246 auto state = getCameraState(id);
1247 if (state == nullptr) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001248 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08001249 return -EINVAL;
1250 }
1251
1252 ICameraServiceListener::Status cameraStatus = state->getStatus();
1253 if (cameraStatus != ICameraServiceListener::STATUS_PRESENT &&
1254 cameraStatus != ICameraServiceListener::STATUS_NOT_AVAILABLE) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001255 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001256 return -EINVAL;
1257 }
1258
1259 {
1260 Mutex::Autolock al(mTorchStatusMutex);
1261 ICameraServiceListener::TorchStatus status;
1262 status_t res = getTorchStatusLocked(id, &status);
1263 if (res) {
1264 ALOGE("%s: getting current torch status failed for camera %s",
1265 __FUNCTION__, id.string());
1266 return -EINVAL;
1267 }
1268
1269 if (status == ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001270 if (cameraStatus == ICameraServiceListener::STATUS_NOT_AVAILABLE) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001271 ALOGE("%s: torch mode of camera %s is not available because "
1272 "camera is in use", __FUNCTION__, id.string());
1273 return -EBUSY;
1274 } else {
1275 ALOGE("%s: torch mode of camera %s is not available due to "
1276 "insufficient resources", __FUNCTION__, id.string());
1277 return -EUSERS;
1278 }
1279 }
1280 }
1281
Ruben Brunk99e69712015-05-26 17:25:07 -07001282 {
1283 // Update UID map - this is used in the torch status changed callbacks, so must be done
1284 // before setTorchMode
Chien-Yu Chenfe751be2015-09-01 14:16:44 -07001285 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -07001286 if (mTorchUidMap.find(id) == mTorchUidMap.end()) {
1287 mTorchUidMap[id].first = uid;
1288 mTorchUidMap[id].second = uid;
1289 } else {
1290 // Set the pending UID
1291 mTorchUidMap[id].first = uid;
1292 }
1293 }
1294
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001295 status_t res = mFlashlight->setTorchMode(id, enabled);
Ruben Brunk99e69712015-05-26 17:25:07 -07001296
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001297 if (res) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001298 ALOGE("%s: setting torch mode of camera %s to %d failed. %s (%d)",
1299 __FUNCTION__, id.string(), enabled, strerror(-res), res);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001300 return res;
1301 }
1302
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001303 {
1304 // update the link to client's death
1305 Mutex::Autolock al(mTorchClientMapMutex);
1306 ssize_t index = mTorchClientMap.indexOfKey(id);
Ruben Brunk99e69712015-05-26 17:25:07 -07001307 BatteryNotifier& notifier(BatteryNotifier::getInstance());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001308 if (enabled) {
1309 if (index == NAME_NOT_FOUND) {
1310 mTorchClientMap.add(id, clientBinder);
1311 } else {
Ruben Brunk99e69712015-05-26 17:25:07 -07001312 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001313 mTorchClientMap.replaceValueAt(index, clientBinder);
1314 }
1315 clientBinder->linkToDeath(this);
1316 } else if (index != NAME_NOT_FOUND) {
Ruben Brunk99e69712015-05-26 17:25:07 -07001317 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001318 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001319 }
1320
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001321 return OK;
1322}
1323
Ruben Brunk6267b532015-04-30 17:44:07 -07001324void CameraService::notifySystemEvent(int32_t eventId, const int32_t* args, size_t length) {
Ruben Brunk36597b22015-03-20 22:15:57 -07001325 switch(eventId) {
1326 case ICameraService::USER_SWITCHED: {
Ruben Brunk6267b532015-04-30 17:44:07 -07001327 doUserSwitch(/*newUserIds*/args, /*length*/length);
Ruben Brunk36597b22015-03-20 22:15:57 -07001328 break;
1329 }
1330 case ICameraService::NO_EVENT:
1331 default: {
1332 ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__,
1333 eventId);
1334 break;
1335 }
1336 }
1337}
1338
Ruben Brunk99e69712015-05-26 17:25:07 -07001339status_t CameraService::addListener(const sp<ICameraServiceListener>& listener) {
Igor Murashkinbfc99152013-02-27 12:55:20 -08001340 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -08001341
Ruben Brunk3450ba72015-06-16 11:00:37 -07001342 if (listener == nullptr) {
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001343 ALOGE("%s: Listener must not be null", __FUNCTION__);
1344 return BAD_VALUE;
1345 }
1346
Igor Murashkinbfc99152013-02-27 12:55:20 -08001347 Mutex::Autolock lock(mServiceLock);
1348
Ruben Brunkcc776712015-02-17 20:18:47 -08001349 {
1350 Mutex::Autolock lock(mStatusListenerLock);
1351 for (auto& it : mListenerList) {
1352 if (IInterface::asBinder(it) == IInterface::asBinder(listener)) {
1353 ALOGW("%s: Tried to add listener %p which was already subscribed",
1354 __FUNCTION__, listener.get());
1355 return ALREADY_EXISTS;
1356 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001357 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001358
1359 mListenerList.push_back(listener);
Igor Murashkinbfc99152013-02-27 12:55:20 -08001360 }
1361
Igor Murashkinbfc99152013-02-27 12:55:20 -08001362
Igor Murashkincba2c162013-03-20 15:56:31 -07001363 /* Immediately signal current status to this listener only */
1364 {
Ruben Brunkcc776712015-02-17 20:18:47 -08001365 Mutex::Autolock lock(mCameraStatesLock);
1366 for (auto& i : mCameraStates) {
1367 // TODO: Update binder to use String16 for camera IDs and remove;
1368 int id = cameraIdToInt(i.first);
1369 if (id == -1) continue;
1370
1371 listener->onStatusChanged(i.second->getStatus(), id);
Igor Murashkincba2c162013-03-20 15:56:31 -07001372 }
1373 }
1374
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001375 /* Immediately signal current torch status to this listener only */
1376 {
1377 Mutex::Autolock al(mTorchStatusMutex);
1378 for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001379 String16 id = String16(mTorchStatusMap.keyAt(i).string());
1380 listener->onTorchStatusChanged(mTorchStatusMap.valueAt(i), id);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001381 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001382 }
1383
Igor Murashkinbfc99152013-02-27 12:55:20 -08001384 return OK;
1385}
Ruben Brunkcc776712015-02-17 20:18:47 -08001386
1387status_t CameraService::removeListener(const sp<ICameraServiceListener>& listener) {
Igor Murashkinbfc99152013-02-27 12:55:20 -08001388 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
1389
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001390 if (listener == 0) {
1391 ALOGE("%s: Listener must not be null", __FUNCTION__);
1392 return BAD_VALUE;
1393 }
1394
Igor Murashkinbfc99152013-02-27 12:55:20 -08001395 Mutex::Autolock lock(mServiceLock);
1396
Ruben Brunkcc776712015-02-17 20:18:47 -08001397 {
1398 Mutex::Autolock lock(mStatusListenerLock);
1399 for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) {
1400 if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) {
1401 mListenerList.erase(it);
1402 return OK;
1403 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001404 }
1405 }
1406
1407 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
1408 __FUNCTION__, listener.get());
1409
1410 return BAD_VALUE;
Igor Murashkin634a5152013-02-20 17:15:11 -08001411}
1412
Ruben Brunkcc776712015-02-17 20:18:47 -08001413status_t CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) {
Igor Murashkin65d14b92014-06-17 12:03:20 -07001414 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1415
1416 if (parameters == NULL) {
1417 ALOGE("%s: parameters must not be null", __FUNCTION__);
1418 return BAD_VALUE;
1419 }
1420
1421 status_t ret = 0;
1422
1423 CameraParameters shimParams;
1424 if ((ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)) != OK) {
1425 // Error logged by caller
1426 return ret;
1427 }
1428
1429 String8 shimParamsString8 = shimParams.flatten();
1430 String16 shimParamsString16 = String16(shimParamsString8);
1431
1432 *parameters = shimParamsString16;
1433
1434 return OK;
1435}
1436
1437status_t CameraService::supportsCameraApi(int cameraId, int apiVersion) {
1438 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1439
1440 switch (apiVersion) {
1441 case API_VERSION_1:
1442 case API_VERSION_2:
1443 break;
1444 default:
1445 ALOGE("%s: Bad API version %d", __FUNCTION__, apiVersion);
1446 return BAD_VALUE;
1447 }
1448
1449 int facing = -1;
1450 int deviceVersion = getDeviceVersion(cameraId, &facing);
1451
1452 switch(deviceVersion) {
1453 case CAMERA_DEVICE_API_VERSION_1_0:
1454 case CAMERA_DEVICE_API_VERSION_2_0:
1455 case CAMERA_DEVICE_API_VERSION_2_1:
1456 case CAMERA_DEVICE_API_VERSION_3_0:
1457 case CAMERA_DEVICE_API_VERSION_3_1:
1458 if (apiVersion == API_VERSION_2) {
1459 ALOGV("%s: Camera id %d uses HAL prior to HAL3.2, doesn't support api2 without shim",
1460 __FUNCTION__, cameraId);
1461 return -EOPNOTSUPP;
1462 } else { // if (apiVersion == API_VERSION_1) {
1463 ALOGV("%s: Camera id %d uses older HAL before 3.2, but api1 is always supported",
1464 __FUNCTION__, cameraId);
1465 return OK;
1466 }
1467 case CAMERA_DEVICE_API_VERSION_3_2:
Ruben Brunkcc776712015-02-17 20:18:47 -08001468 case CAMERA_DEVICE_API_VERSION_3_3:
Igor Murashkin65d14b92014-06-17 12:03:20 -07001469 ALOGV("%s: Camera id %d uses HAL3.2 or newer, supports api1/api2 directly",
1470 __FUNCTION__, cameraId);
1471 return OK;
1472 case -1:
1473 ALOGE("%s: Invalid camera id %d", __FUNCTION__, cameraId);
1474 return BAD_VALUE;
1475 default:
1476 ALOGE("%s: Unknown camera device HAL version: %d", __FUNCTION__, deviceVersion);
1477 return INVALID_OPERATION;
1478 }
1479
1480 return OK;
1481}
1482
Ruben Brunkcc776712015-02-17 20:18:47 -08001483void CameraService::removeByClient(const BasicClient* client) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001484 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001485 for (auto& i : mActiveClientManager.getAll()) {
1486 auto clientSp = i->getValue();
1487 if (clientSp.get() == client) {
1488 mActiveClientManager.remove(i);
Igor Murashkin634a5152013-02-20 17:15:11 -08001489 }
Igor Murashkinecf17e82012-10-02 16:05:11 -07001490 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001491}
1492
Ruben Brunkcc776712015-02-17 20:18:47 -08001493bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) {
1494 const int callingPid = getCallingPid();
1495 const int servicePid = getpid();
1496 bool ret = false;
1497 {
1498 // Acquire mServiceLock and prevent other clients from connecting
1499 std::unique_ptr<AutoConditionLock> lock =
1500 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
Igor Murashkin634a5152013-02-20 17:15:11 -08001501
Igor Murashkin634a5152013-02-20 17:15:11 -08001502
Ruben Brunkcc776712015-02-17 20:18:47 -08001503 std::vector<sp<BasicClient>> evicted;
1504 for (auto& i : mActiveClientManager.getAll()) {
1505 auto clientSp = i->getValue();
1506 if (clientSp.get() == nullptr) {
1507 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1508 mActiveClientManager.remove(i);
1509 continue;
1510 }
1511 if (remote == clientSp->getRemote() && (callingPid == servicePid ||
1512 callingPid == clientSp->getClientPid())) {
1513 mActiveClientManager.remove(i);
1514 evicted.push_back(clientSp);
Igor Murashkin634a5152013-02-20 17:15:11 -08001515
Ruben Brunkcc776712015-02-17 20:18:47 -08001516 // Notify the client of disconnection
1517 clientSp->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
1518 CaptureResultExtras());
Igor Murashkin634a5152013-02-20 17:15:11 -08001519 }
1520 }
1521
Ruben Brunkcc776712015-02-17 20:18:47 -08001522 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1523 // other clients from connecting in mServiceLockWrapper if held
1524 mServiceLock.unlock();
1525
Ruben Brunk36597b22015-03-20 22:15:57 -07001526 // Do not clear caller identity, remote caller should be client proccess
1527
Ruben Brunkcc776712015-02-17 20:18:47 -08001528 for (auto& i : evicted) {
1529 if (i.get() != nullptr) {
1530 i->disconnect();
1531 ret = true;
1532 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001533 }
1534
Ruben Brunkcc776712015-02-17 20:18:47 -08001535 // Reacquire mServiceLock
1536 mServiceLock.lock();
Igor Murashkin634a5152013-02-20 17:15:11 -08001537
Ruben Brunkcc776712015-02-17 20:18:47 -08001538 } // lock is destroyed, allow further connect calls
1539
1540 return ret;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001541}
1542
Igor Murashkinecf17e82012-10-02 16:05:11 -07001543
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001544/**
1545 * Check camera capabilities, such as support for basic color operation
1546 */
1547int CameraService::checkCameraCapabilities(int id, camera_info info, int *latestStrangeCameraId) {
1548
1549 // Assume all devices pre-v3.3 are backward-compatible
1550 bool isBackwardCompatible = true;
1551 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0
1552 && info.device_version >= CAMERA_DEVICE_API_VERSION_3_3) {
1553 isBackwardCompatible = false;
1554 status_t res;
1555 camera_metadata_ro_entry_t caps;
1556 res = find_camera_metadata_ro_entry(
1557 info.static_camera_characteristics,
1558 ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
1559 &caps);
1560 if (res != 0) {
1561 ALOGW("%s: Unable to find camera capabilities for camera device %d",
1562 __FUNCTION__, id);
1563 caps.count = 0;
1564 }
1565 for (size_t i = 0; i < caps.count; i++) {
1566 if (caps.data.u8[i] ==
1567 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) {
1568 isBackwardCompatible = true;
1569 break;
1570 }
1571 }
1572 }
1573
1574 if (!isBackwardCompatible) {
1575 mNumberOfNormalCameras--;
1576 *latestStrangeCameraId = id;
1577 } else {
1578 if (id > *latestStrangeCameraId) {
1579 ALOGE("%s: Normal camera ID %d higher than strange camera ID %d. "
1580 "This is not allowed due backward-compatibility requirements",
1581 __FUNCTION__, id, *latestStrangeCameraId);
1582 logServiceError("Invalid order of camera devices", ENODEV);
1583 mNumberOfCameras = 0;
1584 mNumberOfNormalCameras = 0;
1585 return INVALID_OPERATION;
1586 }
1587 }
1588 return OK;
1589}
1590
Ruben Brunkcc776712015-02-17 20:18:47 -08001591std::shared_ptr<CameraService::CameraState> CameraService::getCameraState(
1592 const String8& cameraId) const {
1593 std::shared_ptr<CameraState> state;
1594 {
1595 Mutex::Autolock lock(mCameraStatesLock);
1596 auto iter = mCameraStates.find(cameraId);
1597 if (iter != mCameraStates.end()) {
1598 state = iter->second;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001599 }
1600 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001601 return state;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001602}
1603
Ruben Brunkcc776712015-02-17 20:18:47 -08001604sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) {
1605 // Remove from active clients list
1606 auto clientDescriptorPtr = mActiveClientManager.remove(cameraId);
1607 if (clientDescriptorPtr == nullptr) {
1608 ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__,
1609 cameraId.string());
1610 return sp<BasicClient>{nullptr};
1611 }
1612
1613 return clientDescriptorPtr->getValue();
Keun young Parkd8973a72012-03-28 14:13:09 -07001614}
1615
Ruben Brunk6267b532015-04-30 17:44:07 -07001616void CameraService::doUserSwitch(const int32_t* newUserId, size_t length) {
Ruben Brunk36597b22015-03-20 22:15:57 -07001617 // Acquire mServiceLock and prevent other clients from connecting
1618 std::unique_ptr<AutoConditionLock> lock =
1619 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
1620
Ruben Brunk6267b532015-04-30 17:44:07 -07001621 std::set<userid_t> newAllowedUsers;
1622 for (size_t i = 0; i < length; i++) {
1623 if (newUserId[i] < 0) {
1624 ALOGE("%s: Bad user ID %d given during user switch, ignoring.",
1625 __FUNCTION__, newUserId[i]);
1626 return;
1627 }
1628 newAllowedUsers.insert(static_cast<userid_t>(newUserId[i]));
Ruben Brunk36597b22015-03-20 22:15:57 -07001629 }
1630
Ruben Brunka8ca9152015-04-07 14:23:40 -07001631
Ruben Brunk6267b532015-04-30 17:44:07 -07001632 if (newAllowedUsers == mAllowedUsers) {
1633 ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__);
1634 return;
1635 }
1636
1637 logUserSwitch(mAllowedUsers, newAllowedUsers);
1638
1639 mAllowedUsers = std::move(newAllowedUsers);
Ruben Brunk36597b22015-03-20 22:15:57 -07001640
1641 // Current user has switched, evict all current clients.
1642 std::vector<sp<BasicClient>> evicted;
1643 for (auto& i : mActiveClientManager.getAll()) {
1644 auto clientSp = i->getValue();
1645
1646 if (clientSp.get() == nullptr) {
1647 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1648 continue;
1649 }
1650
Ruben Brunk6267b532015-04-30 17:44:07 -07001651 // Don't evict clients that are still allowed.
1652 uid_t clientUid = clientSp->getClientUid();
1653 userid_t clientUserId = multiuser_get_user_id(clientUid);
1654 if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) {
1655 continue;
1656 }
1657
Ruben Brunk36597b22015-03-20 22:15:57 -07001658 evicted.push_back(clientSp);
1659
1660 String8 curTime = getFormattedCurrentTime();
1661
1662 ALOGE("Evicting conflicting client for camera ID %s due to user change",
1663 i->getKey().string());
Ruben Brunka8ca9152015-04-07 14:23:40 -07001664
Ruben Brunk36597b22015-03-20 22:15:57 -07001665 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001666 logEvent(String8::format("EVICT device %s client held by package %s (PID %"
Ruben Brunk36597b22015-03-20 22:15:57 -07001667 PRId32 ", priority %" PRId32 ")\n - Evicted due to user switch.",
Ruben Brunka8ca9152015-04-07 14:23:40 -07001668 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
1669 i->getOwnerId(), i->getPriority()));
Ruben Brunk36597b22015-03-20 22:15:57 -07001670
1671 }
1672
1673 // Do not hold mServiceLock while disconnecting clients, but retain the condition
1674 // blocking other clients from connecting in mServiceLockWrapper if held.
1675 mServiceLock.unlock();
1676
1677 // Clear caller identity temporarily so client disconnect PID checks work correctly
1678 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1679
1680 for (auto& i : evicted) {
1681 i->disconnect();
1682 }
1683
1684 IPCThreadState::self()->restoreCallingIdentity(token);
1685
1686 // Reacquire mServiceLock
1687 mServiceLock.lock();
1688}
Ruben Brunkcc776712015-02-17 20:18:47 -08001689
Ruben Brunka8ca9152015-04-07 14:23:40 -07001690void CameraService::logEvent(const char* event) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001691 String8 curTime = getFormattedCurrentTime();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001692 Mutex::Autolock l(mLogLock);
1693 mEventLog.add(String8::format("%s : %s", curTime.string(), event));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001694}
1695
Ruben Brunka8ca9152015-04-07 14:23:40 -07001696void CameraService::logDisconnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001697 const char* clientPackage) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001698 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001699 logEvent(String8::format("DISCONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001700 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001701}
1702
1703void CameraService::logConnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001704 const char* clientPackage) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07001705 // Log the clients evicted
1706 logEvent(String8::format("CONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001707 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001708}
1709
1710void CameraService::logRejected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001711 const char* clientPackage, const char* reason) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07001712 // Log the client rejected
1713 logEvent(String8::format("REJECT device %s client for package %s (PID %d), reason: (%s)",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001714 cameraId, clientPackage, clientPid, reason));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001715}
1716
Ruben Brunk6267b532015-04-30 17:44:07 -07001717void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds,
1718 const std::set<userid_t>& newUserIds) {
1719 String8 newUsers = toString(newUserIds);
1720 String8 oldUsers = toString(oldUserIds);
Ruben Brunka8ca9152015-04-07 14:23:40 -07001721 // Log the new and old users
Ruben Brunk6267b532015-04-30 17:44:07 -07001722 logEvent(String8::format("USER_SWITCH previous allowed users: %s , current allowed users: %s",
1723 oldUsers.string(), newUsers.string()));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001724}
1725
1726void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) {
1727 // Log the device removal
1728 logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason));
1729}
1730
1731void CameraService::logDeviceAdded(const char* cameraId, const char* reason) {
1732 // Log the device removal
1733 logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason));
1734}
1735
1736void CameraService::logClientDied(int clientPid, const char* reason) {
1737 // Log the device removal
1738 logEvent(String8::format("DIED client(s) with PID %d, reason: (%s)", clientPid, reason));
Igor Murashkinecf17e82012-10-02 16:05:11 -07001739}
1740
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07001741void CameraService::logServiceError(const char* msg, int errorCode) {
1742 String8 curTime = getFormattedCurrentTime();
1743 logEvent(String8::format("SERVICE ERROR: %s : %d (%s)", msg, errorCode, strerror(errorCode)));
1744}
1745
Ruben Brunk36597b22015-03-20 22:15:57 -07001746status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
1747 uint32_t flags) {
1748
1749 const int pid = getCallingPid();
1750 const int selfPid = getpid();
1751
Mathias Agopian65ab4712010-07-14 17:59:35 -07001752 // Permission checks
1753 switch (code) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001754 case BnCameraService::CONNECT:
1755 case BnCameraService::CONNECT_DEVICE:
1756 case BnCameraService::CONNECT_LEGACY: {
1757 if (pid != selfPid) {
1758 // we're called from a different process, do the real check
1759 if (!checkCallingPermission(
1760 String16("android.permission.CAMERA"))) {
1761 const int uid = getCallingUid();
1762 ALOGE("Permission Denial: "
1763 "can't use the camera pid=%d, uid=%d", pid, uid);
1764 return PERMISSION_DENIED;
1765 }
1766 }
1767 break;
1768 }
Ruben Brunk36597b22015-03-20 22:15:57 -07001769 case BnCameraService::NOTIFY_SYSTEM_EVENT: {
1770 if (pid != selfPid) {
1771 // Ensure we're being called by system_server, or similar process with
1772 // permissions to notify the camera service about system events
1773 if (!checkCallingPermission(
1774 String16("android.permission.CAMERA_SEND_SYSTEM_EVENTS"))) {
1775 const int uid = getCallingUid();
1776 ALOGE("Permission Denial: cannot send updates to camera service about system"
1777 " events from pid=%d, uid=%d", pid, uid);
1778 return PERMISSION_DENIED;
1779 }
1780 }
1781 break;
1782 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001783 }
1784
1785 return BnCameraService::onTransact(code, data, reply, flags);
1786}
1787
Mathias Agopian65ab4712010-07-14 17:59:35 -07001788// We share the media players for shutter and recording sound for all clients.
1789// A reference count is kept to determine when we will actually release the
1790// media players.
1791
Chih-Chung Changff4f55c2011-10-17 19:03:12 +08001792MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001793 MediaPlayer* mp = new MediaPlayer();
Andreas Huber1b86fe02014-01-29 11:13:26 -08001794 if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -08001795 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001796 mp->prepare();
1797 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001798 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001799 return NULL;
1800 }
1801 return mp;
1802}
1803
1804void CameraService::loadSound() {
1805 Mutex::Autolock lock(mSoundLock);
1806 LOG1("CameraService::loadSound ref=%d", mSoundRef);
1807 if (mSoundRef++) return;
1808
1809 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
1810 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
1811}
1812
1813void CameraService::releaseSound() {
1814 Mutex::Autolock lock(mSoundLock);
1815 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
1816 if (--mSoundRef) return;
1817
1818 for (int i = 0; i < NUM_SOUNDS; i++) {
1819 if (mSoundPlayer[i] != 0) {
1820 mSoundPlayer[i]->disconnect();
1821 mSoundPlayer[i].clear();
1822 }
1823 }
1824}
1825
1826void CameraService::playSound(sound_kind kind) {
1827 LOG1("playSound(%d)", kind);
1828 Mutex::Autolock lock(mSoundLock);
1829 sp<MediaPlayer> player = mSoundPlayer[kind];
1830 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +08001831 player->seekTo(0);
1832 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001833 }
1834}
1835
1836// ----------------------------------------------------------------------------
1837
1838CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -07001839 const sp<ICameraClient>& cameraClient,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001840 const String16& clientPackageName,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001841 int cameraId, int cameraFacing,
1842 int clientPid, uid_t clientUid,
1843 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001844 CameraService::BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -08001845 IInterface::asBinder(cameraClient),
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001846 clientPackageName,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001847 cameraId, cameraFacing,
1848 clientPid, clientUid,
1849 servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -08001850{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001851 int callingPid = getCallingPid();
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001852 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001853
Igor Murashkin44cfcf02013-03-01 16:22:28 -08001854 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001855
Mathias Agopian65ab4712010-07-14 17:59:35 -07001856 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001857
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001858 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001859}
1860
Mathias Agopian65ab4712010-07-14 17:59:35 -07001861// tear down the client
1862CameraService::Client::~Client() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07001863 ALOGV("~Client");
Igor Murashkin634a5152013-02-20 17:15:11 -08001864 mDestructionStarted = true;
1865
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -07001866 mCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -07001867 // unconditionally disconnect. function is idempotent
1868 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001869}
1870
Igor Murashkin634a5152013-02-20 17:15:11 -08001871CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001872 const sp<IBinder>& remoteCallback,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001873 const String16& clientPackageName,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001874 int cameraId, int cameraFacing,
1875 int clientPid, uid_t clientUid,
1876 int servicePid):
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001877 mClientPackageName(clientPackageName), mDisconnected(false)
Igor Murashkin634a5152013-02-20 17:15:11 -08001878{
1879 mCameraService = cameraService;
Igor Murashkin44cfcf02013-03-01 16:22:28 -08001880 mRemoteBinder = remoteCallback;
Igor Murashkin634a5152013-02-20 17:15:11 -08001881 mCameraId = cameraId;
1882 mCameraFacing = cameraFacing;
1883 mClientPid = clientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001884 mClientUid = clientUid;
Igor Murashkin634a5152013-02-20 17:15:11 -08001885 mServicePid = servicePid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001886 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -08001887 mDestructionStarted = false;
1888}
1889
1890CameraService::BasicClient::~BasicClient() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07001891 ALOGV("~BasicClient");
Igor Murashkin634a5152013-02-20 17:15:11 -08001892 mDestructionStarted = true;
1893}
1894
1895void CameraService::BasicClient::disconnect() {
Ruben Brunk36597b22015-03-20 22:15:57 -07001896 if (mDisconnected) {
Ruben Brunk36597b22015-03-20 22:15:57 -07001897 return;
1898 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07001899 mDisconnected = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08001900
1901 mCameraService->removeByClient(this);
1902 mCameraService->logDisconnected(String8::format("%d", mCameraId), mClientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001903 String8(mClientPackageName));
Ruben Brunkcc776712015-02-17 20:18:47 -08001904
1905 sp<IBinder> remote = getRemote();
1906 if (remote != nullptr) {
1907 remote->unlinkToDeath(mCameraService);
1908 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07001909
1910 finishCameraOps();
Ruben Brunkcc776712015-02-17 20:18:47 -08001911 ALOGI("%s: Disconnected client for camera %d for PID %d", __FUNCTION__, mCameraId, mClientPid);
1912
Igor Murashkincba2c162013-03-20 15:56:31 -07001913 // client shouldn't be able to call into us anymore
1914 mClientPid = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -08001915}
1916
Ruben Brunkcc776712015-02-17 20:18:47 -08001917String16 CameraService::BasicClient::getPackageName() const {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001918 return mClientPackageName;
Ruben Brunkcc776712015-02-17 20:18:47 -08001919}
1920
1921
1922int CameraService::BasicClient::getClientPid() const {
1923 return mClientPid;
1924}
1925
Ruben Brunk6267b532015-04-30 17:44:07 -07001926uid_t CameraService::BasicClient::getClientUid() const {
1927 return mClientUid;
1928}
1929
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001930bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const {
1931 // Defaults to API2.
1932 return level == API_2;
1933}
1934
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001935status_t CameraService::BasicClient::startCameraOps() {
1936 int32_t res;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07001937 // Notify app ops that the camera is not available
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001938 mOpsCallback = new OpsCallback(this);
1939
Igor Murashkine6800ce2013-03-04 17:25:57 -08001940 {
1941 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001942 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001943 }
1944
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001945 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001946 mClientPackageName, mOpsCallback);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001947 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001948 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001949
Svetoslav28e8ef72015-05-11 19:21:31 -07001950 if (res == AppOpsManager::MODE_ERRORED) {
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001951 ALOGI("Camera %d: Access for \"%s\" has been revoked",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001952 mCameraId, String8(mClientPackageName).string());
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001953 return PERMISSION_DENIED;
1954 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07001955
Svetoslav28e8ef72015-05-11 19:21:31 -07001956 if (res == AppOpsManager::MODE_IGNORED) {
1957 ALOGI("Camera %d: Access for \"%s\" has been restricted",
1958 mCameraId, String8(mClientPackageName).string());
Eino-Ville Talvalae3afb2c2015-06-03 16:03:30 -07001959 // Return the same error as for device policy manager rejection
1960 return -EACCES;
Svetoslav28e8ef72015-05-11 19:21:31 -07001961 }
1962
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001963 mOpsActive = true;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07001964
1965 // Transition device availability listeners from PRESENT -> NOT_AVAILABLE
1966 mCameraService->updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE,
Ruben Brunkcc776712015-02-17 20:18:47 -08001967 String8::format("%d", mCameraId));
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07001968
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001969 // Transition device state to OPEN
1970 mCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_OPEN,
1971 String8::format("%d", mCameraId));
1972
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001973 return OK;
1974}
1975
1976status_t CameraService::BasicClient::finishCameraOps() {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07001977 // Check if startCameraOps succeeded, and if so, finish the camera op
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001978 if (mOpsActive) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07001979 // Notify app ops that the camera is available again
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001980 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001981 mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001982 mOpsActive = false;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07001983
Ruben Brunkcc776712015-02-17 20:18:47 -08001984 auto rejected = {ICameraServiceListener::STATUS_NOT_PRESENT,
1985 ICameraServiceListener::STATUS_ENUMERATING};
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07001986
Ruben Brunkcc776712015-02-17 20:18:47 -08001987 // Transition to PRESENT if the camera is not in either of the rejected states
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07001988 mCameraService->updateStatus(ICameraServiceListener::STATUS_PRESENT,
Ruben Brunkcc776712015-02-17 20:18:47 -08001989 String8::format("%d", mCameraId), rejected);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07001990
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001991 // Transition device state to CLOSED
1992 mCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_CLOSED,
1993 String8::format("%d", mCameraId));
1994
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001995 // Notify flashlight that a camera device is closed.
1996 mCameraService->mFlashlight->deviceClosed(
1997 String8::format("%d", mCameraId));
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001998 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07001999 // Always stop watching, even if no camera op is active
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002000 if (mOpsCallback != NULL) {
2001 mAppOpsManager.stopWatchingMode(mOpsCallback);
2002 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002003 mOpsCallback.clear();
2004
2005 return OK;
2006}
2007
2008void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
2009 String8 name(packageName);
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002010 String8 myName(mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002011
2012 if (op != AppOpsManager::OP_CAMERA) {
2013 ALOGW("Unexpected app ops notification received: %d", op);
2014 return;
2015 }
2016
2017 int32_t res;
2018 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002019 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002020 ALOGV("checkOp returns: %d, %s ", res,
2021 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
2022 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
2023 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
2024 "UNKNOWN");
2025
2026 if (res != AppOpsManager::MODE_ALLOWED) {
2027 ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId,
2028 myName.string());
2029 // Reset the client PID to allow server-initiated disconnect,
2030 // and to prevent further calls by client.
2031 mClientPid = getCallingPid();
Jianing Weicb0652e2014-03-12 18:29:36 -07002032 CaptureResultExtras resultExtras; // a dummy result (invalid)
2033 notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002034 disconnect();
2035 }
2036}
2037
Mathias Agopian65ab4712010-07-14 17:59:35 -07002038// ----------------------------------------------------------------------------
2039
Ruben Brunkcc776712015-02-17 20:18:47 -08002040// Provide client strong pointer for callbacks.
2041sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) {
2042 String8 cameraId = String8::format("%d", (int)(intptr_t) user);
2043 auto clientDescriptor = gCameraService->mActiveClientManager.get(cameraId);
2044 if (clientDescriptor != nullptr) {
2045 return sp<Client>{
2046 static_cast<Client*>(clientDescriptor->getValue().get())};
2047 }
2048 return sp<Client>{nullptr};
Mathias Agopian65ab4712010-07-14 17:59:35 -07002049}
2050
Jianing Weicb0652e2014-03-12 18:29:36 -07002051void CameraService::Client::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
2052 const CaptureResultExtras& resultExtras) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002053 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002054}
2055
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002056// NOTE: function is idempotent
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07002057void CameraService::Client::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002058 ALOGV("Client::disconnect");
Igor Murashkin634a5152013-02-20 17:15:11 -08002059 BasicClient::disconnect();
Wu-cheng Lie09591e2010-10-14 20:17:44 +08002060}
2061
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002062bool CameraService::Client::canCastToApiClient(apiLevel level) const {
2063 return level == API_1;
2064}
2065
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002066CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
2067 mClient(client) {
2068}
2069
2070void CameraService::Client::OpsCallback::opChanged(int32_t op,
2071 const String16& packageName) {
2072 sp<BasicClient> client = mClient.promote();
2073 if (client != NULL) {
2074 client->opChanged(op, packageName);
2075 }
2076}
2077
Mathias Agopian65ab4712010-07-14 17:59:35 -07002078// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002079// CameraState
2080// ----------------------------------------------------------------------------
2081
2082CameraService::CameraState::CameraState(const String8& id, int cost,
2083 const std::set<String8>& conflicting) : mId(id),
2084 mStatus(ICameraServiceListener::STATUS_PRESENT), mCost(cost), mConflicting(conflicting) {}
2085
2086CameraService::CameraState::~CameraState() {}
2087
2088ICameraServiceListener::Status CameraService::CameraState::getStatus() const {
2089 Mutex::Autolock lock(mStatusLock);
2090 return mStatus;
2091}
2092
2093CameraParameters CameraService::CameraState::getShimParams() const {
2094 return mShimParams;
2095}
2096
2097void CameraService::CameraState::setShimParams(const CameraParameters& params) {
2098 mShimParams = params;
2099}
2100
2101int CameraService::CameraState::getCost() const {
2102 return mCost;
2103}
2104
2105std::set<String8> CameraService::CameraState::getConflicting() const {
2106 return mConflicting;
2107}
2108
2109String8 CameraService::CameraState::getId() const {
2110 return mId;
2111}
2112
2113// ----------------------------------------------------------------------------
Ruben Brunk99e69712015-05-26 17:25:07 -07002114// ClientEventListener
2115// ----------------------------------------------------------------------------
2116
2117void CameraService::ClientEventListener::onClientAdded(
2118 const resource_policy::ClientDescriptor<String8,
2119 sp<CameraService::BasicClient>>& descriptor) {
2120 auto basicClient = descriptor.getValue();
2121 if (basicClient.get() != nullptr) {
2122 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2123 notifier.noteStartCamera(descriptor.getKey(),
2124 static_cast<int>(basicClient->getClientUid()));
2125 }
2126}
2127
2128void CameraService::ClientEventListener::onClientRemoved(
2129 const resource_policy::ClientDescriptor<String8,
2130 sp<CameraService::BasicClient>>& descriptor) {
2131 auto basicClient = descriptor.getValue();
2132 if (basicClient.get() != nullptr) {
2133 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2134 notifier.noteStopCamera(descriptor.getKey(),
2135 static_cast<int>(basicClient->getClientUid()));
2136 }
2137}
2138
2139
2140// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002141// CameraClientManager
2142// ----------------------------------------------------------------------------
2143
Ruben Brunk99e69712015-05-26 17:25:07 -07002144CameraService::CameraClientManager::CameraClientManager() {
2145 setListener(std::make_shared<ClientEventListener>());
2146}
2147
Ruben Brunkcc776712015-02-17 20:18:47 -08002148CameraService::CameraClientManager::~CameraClientManager() {}
2149
2150sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient(
2151 const String8& id) const {
2152 auto descriptor = get(id);
2153 if (descriptor == nullptr) {
2154 return sp<BasicClient>{nullptr};
2155 }
2156 return descriptor->getValue();
2157}
2158
2159String8 CameraService::CameraClientManager::toString() const {
2160 auto all = getAll();
2161 String8 ret("[");
2162 bool hasAny = false;
2163 for (auto& i : all) {
2164 hasAny = true;
2165 String8 key = i->getKey();
2166 int32_t cost = i->getCost();
2167 int32_t pid = i->getOwnerId();
2168 int32_t priority = i->getPriority();
2169 auto conflicting = i->getConflicting();
2170 auto clientSp = i->getValue();
2171 String8 packageName;
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07002172 userid_t clientUserId = 0;
Ruben Brunkcc776712015-02-17 20:18:47 -08002173 if (clientSp.get() != nullptr) {
2174 packageName = String8{clientSp->getPackageName()};
Ruben Brunk6267b532015-04-30 17:44:07 -07002175 uid_t clientUid = clientSp->getClientUid();
2176 clientUserId = multiuser_get_user_id(clientUid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002177 }
2178 ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Priority: %"
2179 PRId32 ", ", key.string(), cost, pid, priority);
2180
Ruben Brunk6267b532015-04-30 17:44:07 -07002181 if (clientSp.get() != nullptr) {
2182 ret.appendFormat("User Id: %d, ", clientUserId);
2183 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002184 if (packageName.size() != 0) {
2185 ret.appendFormat("Client Package Name: %s", packageName.string());
2186 }
2187
2188 ret.append(", Conflicting Client Devices: {");
2189 for (auto& j : conflicting) {
2190 ret.appendFormat("%s, ", j.string());
2191 }
2192 ret.append("})");
2193 }
2194 if (hasAny) ret.append("\n");
2195 ret.append("]\n");
2196 return ret;
2197}
2198
2199CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2200 const String8& key, const sp<BasicClient>& value, int32_t cost,
2201 const std::set<String8>& conflictingKeys, int32_t priority, int32_t ownerId) {
2202
2203 return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>(
2204 key, value, cost, conflictingKeys, priority, ownerId);
2205}
2206
2207CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2208 const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) {
2209 return makeClientDescriptor(partial->getKey(), value, partial->getCost(),
2210 partial->getConflicting(), partial->getPriority(), partial->getOwnerId());
2211}
2212
2213// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -07002214
2215static const int kDumpLockRetries = 50;
2216static const int kDumpLockSleep = 60000;
2217
2218static bool tryLock(Mutex& mutex)
2219{
2220 bool locked = false;
2221 for (int i = 0; i < kDumpLockRetries; ++i) {
2222 if (mutex.tryLock() == NO_ERROR) {
2223 locked = true;
2224 break;
2225 }
2226 usleep(kDumpLockSleep);
2227 }
2228 return locked;
2229}
2230
2231status_t CameraService::dump(int fd, const Vector<String16>& args) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07002232 String8 result("Dump of the Camera Service:\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002233 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07002234 result.appendFormat("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -07002235 "can't dump CameraService from pid=%d, uid=%d\n",
2236 getCallingPid(),
2237 getCallingUid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002238 write(fd, result.string(), result.size());
2239 } else {
2240 bool locked = tryLock(mServiceLock);
2241 // failed to lock - CameraService is probably deadlocked
2242 if (!locked) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07002243 result.append("CameraService may be deadlocked\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002244 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002245 }
2246
2247 bool hasClient = false;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002248 if (!mModule) {
2249 result = String8::format("No camera module available!\n");
2250 write(fd, result.string(), result.size());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002251
2252 // Dump event log for error information
2253 dumpEventLog(fd);
2254
Kalle Lampila6ec3a152013-04-30 15:27:19 +03002255 if (locked) mServiceLock.unlock();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002256 return NO_ERROR;
2257 }
2258
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08002259 result = String8::format("Camera module HAL API version: 0x%x\n", mModule->getHalApiVersion());
2260 result.appendFormat("Camera module API version: 0x%x\n", mModule->getModuleApiVersion());
2261 result.appendFormat("Camera module name: %s\n", mModule->getModuleName());
2262 result.appendFormat("Camera module author: %s\n", mModule->getModuleAuthor());
Ruben Brunkcc776712015-02-17 20:18:47 -08002263 result.appendFormat("Number of camera devices: %d\n", mNumberOfCameras);
2264 String8 activeClientString = mActiveClientManager.toString();
2265 result.appendFormat("Active Camera Clients:\n%s", activeClientString.string());
Ruben Brunk6267b532015-04-30 17:44:07 -07002266 result.appendFormat("Allowed users:\n%s\n", toString(mAllowedUsers).string());
Ruben Brunkcc776712015-02-17 20:18:47 -08002267
Ruben Brunkf81648e2014-04-17 16:14:57 -07002268 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
2269 if (desc == NULL) {
2270 result.appendFormat("Vendor tags left unimplemented.\n");
2271 } else {
2272 result.appendFormat("Vendor tag definitions:\n");
2273 }
2274
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002275 write(fd, result.string(), result.size());
Ruben Brunkf81648e2014-04-17 16:14:57 -07002276
2277 if (desc != NULL) {
2278 desc->dump(fd, /*verbosity*/2, /*indentation*/4);
2279 }
2280
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002281 dumpEventLog(fd);
Ruben Brunkcc776712015-02-17 20:18:47 -08002282
2283 bool stateLocked = tryLock(mCameraStatesLock);
2284 if (!stateLocked) {
2285 result = String8::format("CameraStates in use, may be deadlocked\n");
2286 write(fd, result.string(), result.size());
2287 }
2288
2289 for (auto& state : mCameraStates) {
2290 String8 cameraId = state.first;
2291 result = String8::format("Camera %s information:\n", cameraId.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002292 camera_info info;
2293
Ruben Brunkcc776712015-02-17 20:18:47 -08002294 // TODO: Change getCameraInfo + HAL to use String cameraIds
2295 status_t rc = mModule->getCameraInfo(cameraIdToInt(cameraId), &info);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002296 if (rc != OK) {
2297 result.appendFormat(" Error reading static information!\n");
2298 write(fd, result.string(), result.size());
2299 } else {
2300 result.appendFormat(" Facing: %s\n",
2301 info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT");
2302 result.appendFormat(" Orientation: %d\n", info.orientation);
2303 int deviceVersion;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08002304 if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002305 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
2306 } else {
2307 deviceVersion = info.device_version;
2308 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002309
2310 auto conflicting = state.second->getConflicting();
2311 result.appendFormat(" Resource Cost: %d\n", state.second->getCost());
2312 result.appendFormat(" Conflicting Devices:");
2313 for (auto& id : conflicting) {
2314 result.appendFormat(" %s", cameraId.string());
2315 }
2316 if (conflicting.size() == 0) {
2317 result.appendFormat(" NONE");
2318 }
2319 result.appendFormat("\n");
2320
2321 result.appendFormat(" Device version: %#x\n", deviceVersion);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002322 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) {
2323 result.appendFormat(" Device static metadata:\n");
2324 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -07002325 dump_indented_camera_metadata(info.static_camera_characteristics,
Ruben Brunkf81648e2014-04-17 16:14:57 -07002326 fd, /*verbosity*/2, /*indentation*/4);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002327 } else {
2328 write(fd, result.string(), result.size());
2329 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002330
2331 CameraParameters p = state.second->getShimParams();
2332 if (!p.isEmpty()) {
2333 result = String8::format(" Camera1 API shim is using parameters:\n ");
2334 write(fd, result.string(), result.size());
2335 p.dump(fd, args);
2336 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002337 }
2338
Ruben Brunkcc776712015-02-17 20:18:47 -08002339 auto clientDescriptor = mActiveClientManager.get(cameraId);
2340 if (clientDescriptor == nullptr) {
2341 result = String8::format(" Device %s is closed, no client instance\n",
2342 cameraId.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002343 write(fd, result.string(), result.size());
2344 continue;
2345 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002346 hasClient = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002347 result = String8::format(" Device %s is open. Client instance dump:\n\n",
2348 cameraId.string());
2349 result.appendFormat("Client priority level: %d\n", clientDescriptor->getPriority());
2350 result.appendFormat("Client PID: %d\n", clientDescriptor->getOwnerId());
2351
2352 auto client = clientDescriptor->getValue();
2353 result.appendFormat("Client package: %s\n",
2354 String8(client->getPackageName()).string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002355 write(fd, result.string(), result.size());
Ruben Brunkcc776712015-02-17 20:18:47 -08002356
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07002357 client->dump(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002358 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002359
2360 if (stateLocked) mCameraStatesLock.unlock();
2361
Mathias Agopian65ab4712010-07-14 17:59:35 -07002362 if (!hasClient) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002363 result = String8::format("\nNo active camera clients yet.\n");
2364 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002365 }
2366
2367 if (locked) mServiceLock.unlock();
2368
Igor Murashkinff3e31d2013-10-23 16:40:06 -07002369 // Dump camera traces if there were any
2370 write(fd, "\n", 1);
2371 camera3::CameraTraces::dump(fd, args);
2372
Mathias Agopian65ab4712010-07-14 17:59:35 -07002373 // change logging level
2374 int n = args.size();
2375 for (int i = 0; i + 1 < n; i++) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07002376 String16 verboseOption("-v");
2377 if (args[i] == verboseOption) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002378 String8 levelStr(args[i+1]);
2379 int level = atoi(levelStr.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002380 result = String8::format("\nSetting log level to %d.\n", level);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002381 setLogLevel(level);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002382 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002383 }
2384 }
2385 }
2386 return NO_ERROR;
2387}
2388
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002389void CameraService::dumpEventLog(int fd) {
2390 String8 result = String8("\nPrior client events (most recent at top):\n");
2391
2392 Mutex::Autolock l(mLogLock);
2393 for (const auto& msg : mEventLog) {
2394 result.appendFormat(" %s\n", msg.string());
2395 }
2396
2397 if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) {
2398 result.append(" ...\n");
2399 } else if (mEventLog.size() == 0) {
2400 result.append(" [no events yet]\n");
2401 }
2402 result.append("\n");
2403
2404 write(fd, result.string(), result.size());
2405}
2406
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002407void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002408 Mutex::Autolock al(mTorchClientMapMutex);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002409 for (size_t i = 0; i < mTorchClientMap.size(); i++) {
2410 if (mTorchClientMap[i] == who) {
2411 // turn off the torch mode that was turned on by dead client
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002412 String8 cameraId = mTorchClientMap.keyAt(i);
2413 status_t res = mFlashlight->setTorchMode(cameraId, false);
2414 if (res) {
2415 ALOGE("%s: torch client died but couldn't turn off torch: "
2416 "%s (%d)", __FUNCTION__, strerror(-res), res);
2417 return;
2418 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002419 mTorchClientMap.removeItemsAt(i);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002420 break;
2421 }
2422 }
2423}
2424
Ruben Brunkcc776712015-02-17 20:18:47 -08002425/*virtual*/void CameraService::binderDied(const wp<IBinder> &who) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07002426
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002427 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -07002428 * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the
2429 * binder driver
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002430 */
2431
Ruben Brunka8ca9152015-04-07 14:23:40 -07002432 logClientDied(getCallingPid(), String8("Binder died unexpectedly"));
2433
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002434 // check torch client
2435 handleTorchClientBinderDied(who);
2436
2437 // check camera device client
Ruben Brunkcc776712015-02-17 20:18:47 -08002438 if(!evictClientIdByRemote(who)) {
2439 ALOGV("%s: Java client's binder death already cleaned up (normal case)", __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002440 return;
2441 }
2442
Ruben Brunkcc776712015-02-17 20:18:47 -08002443 ALOGE("%s: Java client's binder died, removing it from the list of active clients",
2444 __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002445}
2446
Ruben Brunkcc776712015-02-17 20:18:47 -08002447void CameraService::updateStatus(ICameraServiceListener::Status status, const String8& cameraId) {
2448 updateStatus(status, cameraId, {});
Igor Murashkinbfc99152013-02-27 12:55:20 -08002449}
2450
Ruben Brunkcc776712015-02-17 20:18:47 -08002451void CameraService::updateStatus(ICameraServiceListener::Status status, const String8& cameraId,
2452 std::initializer_list<ICameraServiceListener::Status> rejectSourceStates) {
2453 // Do not lock mServiceLock here or can get into a deadlock from
2454 // connect() -> disconnect -> updateStatus
2455
2456 auto state = getCameraState(cameraId);
2457
2458 if (state == nullptr) {
2459 ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__,
2460 cameraId.string());
2461 return;
Igor Murashkincba2c162013-03-20 15:56:31 -07002462 }
2463
Ruben Brunkcc776712015-02-17 20:18:47 -08002464 // Update the status for this camera state, then send the onStatusChangedCallbacks to each
2465 // of the listeners with both the mStatusStatus and mStatusListenerLock held
2466 state->updateStatus(status, cameraId, rejectSourceStates, [this]
2467 (const String8& cameraId, ICameraServiceListener::Status status) {
2468
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002469 if (status != ICameraServiceListener::STATUS_ENUMERATING) {
2470 // Update torch status if it has a flash unit.
2471 Mutex::Autolock al(mTorchStatusMutex);
2472 ICameraServiceListener::TorchStatus torchStatus;
2473 if (getTorchStatusLocked(cameraId, &torchStatus) !=
2474 NAME_NOT_FOUND) {
2475 ICameraServiceListener::TorchStatus newTorchStatus =
2476 status == ICameraServiceListener::STATUS_PRESENT ?
2477 ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF :
2478 ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
2479 if (torchStatus != newTorchStatus) {
2480 onTorchStatusChangedLocked(cameraId, newTorchStatus);
2481 }
2482 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002483 }
2484
2485 Mutex::Autolock lock(mStatusListenerLock);
2486
2487 for (auto& listener : mListenerList) {
2488 // TODO: Refactor status listeners to use strings for Camera IDs and remove this.
2489 int id = cameraIdToInt(cameraId);
2490 if (id != -1) listener->onStatusChanged(status, id);
2491 }
2492 });
Igor Murashkincba2c162013-03-20 15:56:31 -07002493}
2494
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002495void CameraService::updateProxyDeviceState(ICameraServiceProxy::CameraState newState,
2496 const String8& cameraId) {
2497 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
2498 if (proxyBinder == nullptr) return;
2499 String16 id(cameraId);
2500 proxyBinder->notifyCameraState(id, newState);
2501}
2502
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002503status_t CameraService::getTorchStatusLocked(
2504 const String8& cameraId,
2505 ICameraServiceListener::TorchStatus *status) const {
2506 if (!status) {
2507 return BAD_VALUE;
2508 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002509 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
2510 if (index == NAME_NOT_FOUND) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002511 // invalid camera ID or the camera doesn't have a flash unit
2512 return NAME_NOT_FOUND;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002513 }
2514
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002515 *status = mTorchStatusMap.valueAt(index);
2516 return OK;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002517}
2518
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002519status_t CameraService::setTorchStatusLocked(const String8& cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002520 ICameraServiceListener::TorchStatus status) {
2521 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
2522 if (index == NAME_NOT_FOUND) {
2523 return BAD_VALUE;
2524 }
2525 ICameraServiceListener::TorchStatus& item =
2526 mTorchStatusMap.editValueAt(index);
2527 item = status;
2528
2529 return OK;
2530}
2531
Mathias Agopian65ab4712010-07-14 17:59:35 -07002532}; // namespace android