blob: 648e82cdbdb71382d054ba96dd3e063262c8964e [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
20#include <stdio.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080021#include <string.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070022#include <sys/types.h>
23#include <pthread.h>
24
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080025#include <binder/AppOpsManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070026#include <binder/IPCThreadState.h>
27#include <binder/IServiceManager.h>
28#include <binder/MemoryBase.h>
29#include <binder/MemoryHeapBase.h>
30#include <cutils/atomic.h>
Nipun Kwatrab5ca4612010-09-11 19:31:10 -070031#include <cutils/properties.h>
Mathias Agopiandf712ea2012-02-25 18:48:35 -080032#include <gui/Surface.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070033#include <hardware/hardware.h>
34#include <media/AudioSystem.h>
Andreas Huber1b86fe02014-01-29 11:13:26 -080035#include <media/IMediaHTTPService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070036#include <media/mediaplayer.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070037#include <utils/Errors.h>
38#include <utils/Log.h>
39#include <utils/String16.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080040#include <utils/Trace.h>
41#include <system/camera_vendor_tags.h>
Ruben Brunkb2119af2014-05-09 19:57:56 -070042#include <system/camera_metadata.h>
43#include <system/camera.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070044
45#include "CameraService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070046#include "api1/CameraClient.h"
47#include "api1/Camera2Client.h"
48#include "api_pro/ProCamera2Client.h"
49#include "api2/CameraDeviceClient.h"
Igor Murashkinff3e31d2013-10-23 16:40:06 -070050#include "utils/CameraTraces.h"
Igor Murashkin98e24722013-06-19 19:51:04 -070051#include "CameraDeviceFactory.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
53namespace android {
54
55// ----------------------------------------------------------------------------
56// Logging support -- this is for debugging only
57// Use "adb shell dumpsys media.camera -v 1" to change it.
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070058volatile int32_t gLogLevel = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -070059
Steve Blockb8a80522011-12-20 16:23:08 +000060#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
61#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
Mathias Agopian65ab4712010-07-14 17:59:35 -070062
63static void setLogLevel(int level) {
64 android_atomic_write(level, &gLogLevel);
65}
66
67// ----------------------------------------------------------------------------
68
69static int getCallingPid() {
70 return IPCThreadState::self()->getCallingPid();
71}
72
73static int getCallingUid() {
74 return IPCThreadState::self()->getCallingUid();
75}
76
Igor Murashkincba2c162013-03-20 15:56:31 -070077extern "C" {
78static void camera_device_status_change(
79 const struct camera_module_callbacks* callbacks,
80 int camera_id,
81 int new_status) {
82 sp<CameraService> cs = const_cast<CameraService*>(
83 static_cast<const CameraService*>(callbacks));
84
85 cs->onDeviceStatusChanged(
86 camera_id,
87 new_status);
88}
89} // extern "C"
90
Mathias Agopian65ab4712010-07-14 17:59:35 -070091// ----------------------------------------------------------------------------
92
93// This is ugly and only safe if we never re-create the CameraService, but
94// should be ok for now.
95static CameraService *gCameraService;
96
97CameraService::CameraService()
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080098 :mSoundRef(0), mModule(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -070099{
Steve Blockdf64d152012-01-04 20:05:49 +0000100 ALOGI("CameraService started (pid=%d)", getpid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700101 gCameraService = this;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800102
103 for (size_t i = 0; i < MAX_CAMERAS; ++i) {
Igor Murashkincba2c162013-03-20 15:56:31 -0700104 mStatusList[i] = ICameraServiceListener::STATUS_PRESENT;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800105 }
Igor Murashkincba2c162013-03-20 15:56:31 -0700106
107 this->camera_device_status_change = android::camera_device_status_change;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700108}
109
Iliyan Malchev8951a972011-04-14 16:55:59 -0700110void CameraService::onFirstRef()
111{
Igor Murashkin634a5152013-02-20 17:15:11 -0800112 LOG1("CameraService::onFirstRef");
113
Iliyan Malchev8951a972011-04-14 16:55:59 -0700114 BnCameraService::onFirstRef();
115
116 if (hw_get_module(CAMERA_HARDWARE_MODULE_ID,
117 (const hw_module_t **)&mModule) < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000118 ALOGE("Could not load camera HAL module");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700119 mNumberOfCameras = 0;
120 }
121 else {
Alex Rayc0dd54f2013-02-20 13:39:37 -0800122 ALOGI("Loaded \"%s\" camera module", mModule->common.name);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700123 mNumberOfCameras = mModule->get_number_of_cameras();
124 if (mNumberOfCameras > MAX_CAMERAS) {
Steve Block29357bc2012-01-06 19:20:56 +0000125 ALOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
Iliyan Malchev8951a972011-04-14 16:55:59 -0700126 mNumberOfCameras, MAX_CAMERAS);
127 mNumberOfCameras = MAX_CAMERAS;
128 }
129 for (int i = 0; i < mNumberOfCameras; i++) {
130 setCameraFree(i);
131 }
Igor Murashkincba2c162013-03-20 15:56:31 -0700132
133 if (mModule->common.module_api_version >=
134 CAMERA_MODULE_API_VERSION_2_1) {
135 mModule->set_callbacks(this);
136 }
Igor Murashkin98e24722013-06-19 19:51:04 -0700137
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800138 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
139
140 if (mModule->common.module_api_version >= CAMERA_MODULE_API_VERSION_2_2) {
141 setUpVendorTags();
142 }
143
Igor Murashkin98e24722013-06-19 19:51:04 -0700144 CameraDeviceFactory::registerService(this);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700145 }
146}
147
Mathias Agopian65ab4712010-07-14 17:59:35 -0700148CameraService::~CameraService() {
149 for (int i = 0; i < mNumberOfCameras; i++) {
150 if (mBusy[i]) {
Steve Block29357bc2012-01-06 19:20:56 +0000151 ALOGE("camera %d is still in use in destructor!", i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700152 }
153 }
154
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800155 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700156 gCameraService = NULL;
157}
158
Igor Murashkincba2c162013-03-20 15:56:31 -0700159void CameraService::onDeviceStatusChanged(int cameraId,
160 int newStatus)
161{
162 ALOGI("%s: Status changed for cameraId=%d, newStatus=%d", __FUNCTION__,
163 cameraId, newStatus);
164
165 if (cameraId < 0 || cameraId >= MAX_CAMERAS) {
166 ALOGE("%s: Bad camera ID %d", __FUNCTION__, cameraId);
167 return;
168 }
169
170 if ((int)getStatus(cameraId) == newStatus) {
171 ALOGE("%s: State transition to the same status 0x%x not allowed",
172 __FUNCTION__, (uint32_t)newStatus);
173 return;
174 }
175
176 /* don't do this in updateStatus
177 since it is also called from connect and we could get into a deadlock */
178 if (newStatus == CAMERA_DEVICE_STATUS_NOT_PRESENT) {
179 Vector<sp<BasicClient> > clientsToDisconnect;
180 {
181 Mutex::Autolock al(mServiceLock);
182
Ruben Brunkb2119af2014-05-09 19:57:56 -0700183 /* Remove cached parameters from shim cache */
184 mShimParams.removeItem(cameraId);
185
Igor Murashkincba2c162013-03-20 15:56:31 -0700186 /* Find all clients that we need to disconnect */
Igor Murashkine7ee7632013-06-11 18:10:18 -0700187 sp<BasicClient> client = mClient[cameraId].promote();
Igor Murashkincba2c162013-03-20 15:56:31 -0700188 if (client.get() != NULL) {
189 clientsToDisconnect.push_back(client);
190 }
191
192 int i = cameraId;
193 for (size_t j = 0; j < mProClientList[i].size(); ++j) {
194 sp<ProClient> cl = mProClientList[i][j].promote();
195 if (cl != NULL) {
196 clientsToDisconnect.push_back(cl);
197 }
198 }
199 }
200
201 /* now disconnect them. don't hold the lock
202 or we can get into a deadlock */
203
204 for (size_t i = 0; i < clientsToDisconnect.size(); ++i) {
205 sp<BasicClient> client = clientsToDisconnect[i];
206
207 client->disconnect();
208 /**
209 * The remote app will no longer be able to call methods on the
210 * client since the client PID will be reset to 0
211 */
212 }
213
Colin Crosse5729fa2014-03-21 15:04:25 -0700214 ALOGV("%s: After unplug, disconnected %zu clients",
Igor Murashkincba2c162013-03-20 15:56:31 -0700215 __FUNCTION__, clientsToDisconnect.size());
216 }
217
218 updateStatus(
219 static_cast<ICameraServiceListener::Status>(newStatus), cameraId);
220
221}
222
Mathias Agopian65ab4712010-07-14 17:59:35 -0700223int32_t CameraService::getNumberOfCameras() {
224 return mNumberOfCameras;
225}
226
227status_t CameraService::getCameraInfo(int cameraId,
228 struct CameraInfo* cameraInfo) {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700229 if (!mModule) {
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700230 return -ENODEV;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700231 }
232
Mathias Agopian65ab4712010-07-14 17:59:35 -0700233 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
234 return BAD_VALUE;
235 }
236
Iliyan Malchev8951a972011-04-14 16:55:59 -0700237 struct camera_info info;
238 status_t rc = mModule->get_camera_info(cameraId, &info);
239 cameraInfo->facing = info.facing;
240 cameraInfo->orientation = info.orientation;
241 return rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242}
243
Ruben Brunkb2119af2014-05-09 19:57:56 -0700244
245status_t CameraService::generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo) {
246 status_t ret = OK;
247 struct CameraInfo info;
248 if ((ret = getCameraInfo(cameraId, &info)) != OK) {
249 return ret;
250 }
251
252 CameraMetadata shimInfo;
253 int32_t orientation = static_cast<int32_t>(info.orientation);
254 if ((ret = shimInfo.update(ANDROID_SENSOR_ORIENTATION, &orientation, 1)) != OK) {
255 return ret;
256 }
257
258 uint8_t facing = (info.facing == CAMERA_FACING_FRONT) ?
259 ANDROID_LENS_FACING_FRONT : ANDROID_LENS_FACING_BACK;
260 if ((ret = shimInfo.update(ANDROID_LENS_FACING, &facing, 1)) != OK) {
261 return ret;
262 }
263
Igor Murashkin65d14b92014-06-17 12:03:20 -0700264 CameraParameters shimParams;
265 if ((ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)) != OK) {
266 // Error logged by callee
267 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700268 }
269
270 Vector<Size> sizes;
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700271 Vector<Size> jpegSizes;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700272 Vector<int32_t> formats;
273 const char* supportedPreviewFormats;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700274 {
275 shimParams.getSupportedPreviewSizes(/*out*/sizes);
276 shimParams.getSupportedPreviewFormats(/*out*/formats);
277 shimParams.getSupportedPictureSizes(/*out*/jpegSizes);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700278 }
279
280 // Always include IMPLEMENTATION_DEFINED
281 formats.add(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED);
282
283 const size_t INTS_PER_CONFIG = 4;
284
285 // Build available stream configurations metadata
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700286 size_t streamConfigSize = (sizes.size() * formats.size() + jpegSizes.size()) * INTS_PER_CONFIG;
287
288 Vector<int32_t> streamConfigs;
289 streamConfigs.setCapacity(streamConfigSize);
290
Ruben Brunkb2119af2014-05-09 19:57:56 -0700291 for (size_t i = 0; i < formats.size(); ++i) {
292 for (size_t j = 0; j < sizes.size(); ++j) {
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700293 streamConfigs.add(formats[i]);
294 streamConfigs.add(sizes[j].width);
295 streamConfigs.add(sizes[j].height);
296 streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700297 }
298 }
299
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700300 for (size_t i = 0; i < jpegSizes.size(); ++i) {
301 streamConfigs.add(HAL_PIXEL_FORMAT_BLOB);
302 streamConfigs.add(jpegSizes[i].width);
303 streamConfigs.add(jpegSizes[i].height);
304 streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT);
305 }
306
Ruben Brunkb2119af2014-05-09 19:57:56 -0700307 if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700308 streamConfigs.array(), streamConfigSize)) != OK) {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700309 return ret;
310 }
311
312 int64_t fakeMinFrames[0];
313 // TODO: Fixme, don't fake min frame durations.
314 if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
315 fakeMinFrames, 0)) != OK) {
316 return ret;
317 }
318
319 int64_t fakeStalls[0];
320 // TODO: Fixme, don't fake stall durations.
321 if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
322 fakeStalls, 0)) != OK) {
323 return ret;
324 }
325
326 *cameraInfo = shimInfo;
327 return OK;
328}
329
Zhijun He2b59be82013-09-25 10:14:30 -0700330status_t CameraService::getCameraCharacteristics(int cameraId,
331 CameraMetadata* cameraInfo) {
332 if (!cameraInfo) {
333 ALOGE("%s: cameraInfo is NULL", __FUNCTION__);
334 return BAD_VALUE;
335 }
336
337 if (!mModule) {
338 ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__);
339 return -ENODEV;
340 }
341
Zhijun He2b59be82013-09-25 10:14:30 -0700342 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
343 ALOGE("%s: Invalid camera id: %d", __FUNCTION__, cameraId);
344 return BAD_VALUE;
345 }
346
347 int facing;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700348 status_t ret = OK;
349 if (mModule->common.module_api_version < CAMERA_MODULE_API_VERSION_2_0 ||
350 getDeviceVersion(cameraId, &facing) <= CAMERA_DEVICE_API_VERSION_2_1 ) {
351 /**
352 * Backwards compatibility mode for old HALs:
353 * - Convert CameraInfo into static CameraMetadata properties.
354 * - Retrieve cached CameraParameters for this camera. If none exist,
355 * attempt to open CameraClient and retrieve the CameraParameters.
356 * - Convert cached CameraParameters into static CameraMetadata
357 * properties.
358 */
359 ALOGI("%s: Switching to HAL1 shim implementation...", __FUNCTION__);
Zhijun He2b59be82013-09-25 10:14:30 -0700360
Ruben Brunkb2119af2014-05-09 19:57:56 -0700361 if ((ret = generateShimMetadata(cameraId, cameraInfo)) != OK) {
362 return ret;
363 }
Zhijun Hef05e50e2013-10-01 11:05:33 -0700364
Ruben Brunkb2119af2014-05-09 19:57:56 -0700365 } else {
366 /**
367 * Normal HAL 2.1+ codepath.
368 */
369 struct camera_info info;
370 ret = mModule->get_camera_info(cameraId, &info);
371 *cameraInfo = info.static_camera_characteristics;
372 }
Zhijun He2b59be82013-09-25 10:14:30 -0700373
374 return ret;
375}
376
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800377status_t CameraService::getCameraVendorTagDescriptor(/*out*/sp<VendorTagDescriptor>& desc) {
378 if (!mModule) {
379 ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__);
380 return -ENODEV;
381 }
382
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800383 desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
384 return OK;
385}
386
Igor Murashkin634a5152013-02-20 17:15:11 -0800387int CameraService::getDeviceVersion(int cameraId, int* facing) {
388 struct camera_info info;
389 if (mModule->get_camera_info(cameraId, &info) != OK) {
390 return -1;
391 }
392
393 int deviceVersion;
394 if (mModule->common.module_api_version >= CAMERA_MODULE_API_VERSION_2_0) {
395 deviceVersion = info.device_version;
396 } else {
397 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
398 }
399
400 if (facing) {
401 *facing = info.facing;
402 }
403
404 return deviceVersion;
405}
406
Igor Murashkinbfc99152013-02-27 12:55:20 -0800407bool CameraService::isValidCameraId(int cameraId) {
408 int facing;
409 int deviceVersion = getDeviceVersion(cameraId, &facing);
410
411 switch(deviceVersion) {
412 case CAMERA_DEVICE_API_VERSION_1_0:
413 case CAMERA_DEVICE_API_VERSION_2_0:
414 case CAMERA_DEVICE_API_VERSION_2_1:
415 case CAMERA_DEVICE_API_VERSION_3_0:
Zhijun He95dd5ba2014-03-26 18:18:00 -0700416 case CAMERA_DEVICE_API_VERSION_3_1:
417 case CAMERA_DEVICE_API_VERSION_3_2:
Igor Murashkinbfc99152013-02-27 12:55:20 -0800418 return true;
419 default:
420 return false;
421 }
422
423 return false;
424}
425
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800426bool CameraService::setUpVendorTags() {
427 vendor_tag_ops_t vOps = vendor_tag_ops_t();
428
429 // Check if vendor operations have been implemented
430 if (mModule->get_vendor_tag_ops == NULL) {
431 ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__);
432 return false;
433 }
434
435 ATRACE_BEGIN("camera3->get_metadata_vendor_tag_ops");
436 mModule->get_vendor_tag_ops(&vOps);
437 ATRACE_END();
438
439 // Ensure all vendor operations are present
440 if (vOps.get_tag_count == NULL || vOps.get_all_tags == NULL ||
441 vOps.get_section_name == NULL || vOps.get_tag_name == NULL ||
442 vOps.get_tag_type == NULL) {
443 ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions."
444 , __FUNCTION__);
445 return false;
446 }
447
448 // Read all vendor tag definitions into a descriptor
449 sp<VendorTagDescriptor> desc;
450 status_t res;
451 if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc))
452 != OK) {
453 ALOGE("%s: Could not generate descriptor from vendor tag operations,"
454 "received error %s (%d). Camera clients will not be able to use"
455 "vendor tags", __FUNCTION__, strerror(res), res);
456 return false;
457 }
458
459 // Set the global descriptor to use with camera metadata
460 VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc);
461 return true;
462}
463
Ruben Brunkb2119af2014-05-09 19:57:56 -0700464status_t CameraService::initializeShimMetadata(int cameraId) {
465 int pid = getCallingPid();
466 int uid = getCallingUid();
467 status_t ret = validateConnect(cameraId, uid);
468 if (ret != OK) {
Igor Murashkin65d14b92014-06-17 12:03:20 -0700469 // Error already logged by callee
Ruben Brunkb2119af2014-05-09 19:57:56 -0700470 return ret;
471 }
472
473 bool needsNewClient = false;
474 sp<Client> client;
475
476 String16 internalPackageName("media");
477 { // Scope for service lock
478 Mutex::Autolock lock(mServiceLock);
479 if (mClient[cameraId] != NULL) {
480 client = static_cast<Client*>(mClient[cameraId].promote().get());
481 }
482 if (client == NULL) {
483 needsNewClient = true;
484 ret = connectHelperLocked(/*cameraClient*/NULL, // Empty binder callbacks
485 cameraId,
486 internalPackageName,
487 uid,
488 pid,
489 client);
490
491 if (ret != OK) {
Igor Murashkin65d14b92014-06-17 12:03:20 -0700492 // Error already logged by callee
Ruben Brunkb2119af2014-05-09 19:57:56 -0700493 return ret;
494 }
495 }
496
497 if (client == NULL) {
498 ALOGE("%s: Could not connect to client camera device.", __FUNCTION__);
499 return BAD_VALUE;
500 }
501
502 String8 rawParams = client->getParameters();
503 CameraParameters params(rawParams);
504 mShimParams.add(cameraId, params);
505 }
506
507 // Close client if one was opened solely for this call
508 if (needsNewClient) {
509 client->disconnect();
510 }
511 return OK;
512}
513
Igor Murashkin65d14b92014-06-17 12:03:20 -0700514status_t CameraService::getLegacyParametersLazy(int cameraId,
515 /*out*/
516 CameraParameters* parameters) {
517
518 ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId);
519
520 status_t ret = 0;
521
522 if (parameters == NULL) {
523 ALOGE("%s: parameters must not be null", __FUNCTION__);
524 return BAD_VALUE;
525 }
526
527 ssize_t index = -1;
528 { // Scope for service lock
529 Mutex::Autolock lock(mServiceLock);
530 index = mShimParams.indexOfKey(cameraId);
531 // Release service lock so initializeShimMetadata can be called correctly.
532
533 if (index >= 0) {
534 *parameters = mShimParams[index];
535 }
536 }
537
538 if (index < 0) {
539 int64_t token = IPCThreadState::self()->clearCallingIdentity();
540 ret = initializeShimMetadata(cameraId);
541 IPCThreadState::self()->restoreCallingIdentity(token);
542 if (ret != OK) {
543 // Error already logged by callee
544 return ret;
545 }
546
547 { // Scope for service lock
548 Mutex::Autolock lock(mServiceLock);
549 index = mShimParams.indexOfKey(cameraId);
550
551 LOG_ALWAYS_FATAL_IF(index < 0, "index should have been initialized");
552
553 *parameters = mShimParams[index];
554 }
555 }
556
557 return OK;
558}
559
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700560status_t CameraService::validateConnect(int cameraId,
Igor Murashkine6800ce2013-03-04 17:25:57 -0800561 /*inout*/
562 int& clientUid) const {
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800563
Mathias Agopian65ab4712010-07-14 17:59:35 -0700564 int callingPid = getCallingPid();
Tyler Luu5861a9a2011-10-06 00:00:03 -0500565
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800566 if (clientUid == USE_CALLING_UID) {
567 clientUid = getCallingUid();
568 } else {
569 // We only trust our own process to forward client UIDs
570 if (callingPid != getpid()) {
571 ALOGE("CameraService::connect X (pid %d) rejected (don't trust clientUid)",
572 callingPid);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700573 return PERMISSION_DENIED;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800574 }
575 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700576
Iliyan Malchev8951a972011-04-14 16:55:59 -0700577 if (!mModule) {
Steve Block29357bc2012-01-06 19:20:56 +0000578 ALOGE("Camera HAL module not loaded");
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700579 return -ENODEV;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700580 }
581
Mathias Agopian65ab4712010-07-14 17:59:35 -0700582 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Steve Block29357bc2012-01-06 19:20:56 +0000583 ALOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700584 callingPid, cameraId);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700585 return -ENODEV;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700586 }
587
Wu-cheng Lia3355432011-05-20 14:54:25 +0800588 char value[PROPERTY_VALUE_MAX];
589 property_get("sys.secpolicy.camera.disabled", value, "0");
590 if (strcmp(value, "1") == 0) {
591 // Camera is disabled by DevicePolicyManager.
Steve Blockdf64d152012-01-04 20:05:49 +0000592 ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700593 return -EACCES;
Wu-cheng Lia3355432011-05-20 14:54:25 +0800594 }
595
Igor Murashkincba2c162013-03-20 15:56:31 -0700596 ICameraServiceListener::Status currentStatus = getStatus(cameraId);
597 if (currentStatus == ICameraServiceListener::STATUS_NOT_PRESENT) {
598 ALOGI("Camera is not plugged in,"
599 " connect X (pid %d) rejected", callingPid);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700600 return -ENODEV;
Igor Murashkincba2c162013-03-20 15:56:31 -0700601 } else if (currentStatus == ICameraServiceListener::STATUS_ENUMERATING) {
602 ALOGI("Camera is enumerating,"
603 " connect X (pid %d) rejected", callingPid);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700604 return -EBUSY;
Igor Murashkincba2c162013-03-20 15:56:31 -0700605 }
606 // Else don't check for STATUS_NOT_AVAILABLE.
607 // -- It's done implicitly in canConnectUnsafe /w the mBusy array
608
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700609 return OK;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800610}
611
612bool CameraService::canConnectUnsafe(int cameraId,
613 const String16& clientPackageName,
614 const sp<IBinder>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -0700615 sp<BasicClient> &client) {
Igor Murashkine6800ce2013-03-04 17:25:57 -0800616 String8 clientName8(clientPackageName);
617 int callingPid = getCallingPid();
618
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800619 if (mClient[cameraId] != 0) {
620 client = mClient[cameraId].promote();
621 if (client != 0) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700622 if (remoteCallback == client->getRemote()) {
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800623 LOG1("CameraService::connect X (pid %d) (the same client)",
624 callingPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800625 return true;
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800626 } else {
Igor Murashkine6800ce2013-03-04 17:25:57 -0800627 // TODOSC: need to support 1 regular client,
628 // multiple shared clients here
629 ALOGW("CameraService::connect X (pid %d) rejected"
630 " (existing client).", callingPid);
631 return false;
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800632 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800633 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800634 mClient[cameraId].clear();
635 }
636
Igor Murashkin634a5152013-02-20 17:15:11 -0800637 /*
638 mBusy is set to false as the last step of the Client destructor,
639 after which it is guaranteed that the Client destructor has finished (
640 including any inherited destructors)
641
642 We only need this for a Client subclasses since we don't allow
643 multiple Clents to be opened concurrently, but multiple BasicClient
644 would be fine
645 */
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800646 if (mBusy[cameraId]) {
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800647 ALOGW("CameraService::connect X (pid %d, \"%s\") rejected"
648 " (camera %d is still busy).", callingPid,
649 clientName8.string(), cameraId);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800650 return false;
651 }
652
653 return true;
654}
655
Ruben Brunkb2119af2014-05-09 19:57:56 -0700656status_t CameraService::connectHelperLocked(const sp<ICameraClient>& cameraClient,
657 int cameraId,
658 const String16& clientPackageName,
659 int clientUid,
660 int callingPid,
661 /*out*/
Zhijun Heb10cdad2014-06-16 16:38:35 -0700662 sp<Client>& client,
663 int halVersion) {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700664
665 int facing = -1;
666 int deviceVersion = getDeviceVersion(cameraId, &facing);
667
668 // If there are other non-exclusive users of the camera,
669 // this will tear them down before we can reuse the camera
670 if (isValidCameraId(cameraId)) {
671 // transition from PRESENT -> NOT_AVAILABLE
672 updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE,
673 cameraId);
674 }
675
Zhijun Heb10cdad2014-06-16 16:38:35 -0700676 if (halVersion < 0 || halVersion == deviceVersion) {
677 // Default path: HAL version is unspecified by caller, create CameraClient
678 // based on device version reported by the HAL.
679 switch(deviceVersion) {
680 case CAMERA_DEVICE_API_VERSION_1_0:
681 client = new CameraClient(this, cameraClient,
682 clientPackageName, cameraId,
683 facing, callingPid, clientUid, getpid());
684 break;
685 case CAMERA_DEVICE_API_VERSION_2_0:
686 case CAMERA_DEVICE_API_VERSION_2_1:
687 case CAMERA_DEVICE_API_VERSION_3_0:
688 case CAMERA_DEVICE_API_VERSION_3_1:
689 case CAMERA_DEVICE_API_VERSION_3_2:
690 client = new Camera2Client(this, cameraClient,
691 clientPackageName, cameraId,
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700692 facing, callingPid, clientUid, getpid());
Zhijun Heb10cdad2014-06-16 16:38:35 -0700693 break;
694 case -1:
695 ALOGE("Invalid camera id %d", cameraId);
696 return BAD_VALUE;
697 default:
698 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
699 return INVALID_OPERATION;
700 }
701 } else {
702 // A particular HAL version is requested by caller. Create CameraClient
703 // based on the requested HAL version.
704 if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 &&
705 halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
706 // Only support higher HAL version device opened as HAL1.0 device.
707 client = new CameraClient(this, cameraClient,
708 clientPackageName, cameraId,
709 facing, callingPid, clientUid, getpid());
710 } else {
711 // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
712 ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
713 " opened as HAL %x device", halVersion, deviceVersion,
714 CAMERA_DEVICE_API_VERSION_1_0);
715 return INVALID_OPERATION;
716 }
Ruben Brunkb2119af2014-05-09 19:57:56 -0700717 }
718
719 status_t status = connectFinishUnsafe(client, client->getRemote());
720 if (status != OK) {
721 // this is probably not recoverable.. maybe the client can try again
722 // OK: we can only get here if we were originally in PRESENT state
723 updateStatus(ICameraServiceListener::STATUS_PRESENT, cameraId);
724 return status;
725 }
726
727 mClient[cameraId] = client;
728 LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId,
729 getpid());
730
731 return OK;
732}
733
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700734status_t CameraService::connect(
Igor Murashkine6800ce2013-03-04 17:25:57 -0800735 const sp<ICameraClient>& cameraClient,
736 int cameraId,
737 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700738 int clientUid,
739 /*out*/
740 sp<ICamera>& device) {
Igor Murashkine6800ce2013-03-04 17:25:57 -0800741
742 String8 clientName8(clientPackageName);
743 int callingPid = getCallingPid();
744
745 LOG1("CameraService::connect E (pid %d \"%s\", id %d)", callingPid,
746 clientName8.string(), cameraId);
747
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700748 status_t status = validateConnect(cameraId, /*inout*/clientUid);
749 if (status != OK) {
750 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700751 }
752
Igor Murashkine6800ce2013-03-04 17:25:57 -0800753
Igor Murashkine7ee7632013-06-11 18:10:18 -0700754 sp<Client> client;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700755 {
756 Mutex::Autolock lock(mServiceLock);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700757 sp<BasicClient> clientTmp;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700758 if (!canConnectUnsafe(cameraId, clientPackageName,
759 cameraClient->asBinder(),
Igor Murashkine7ee7632013-06-11 18:10:18 -0700760 /*out*/clientTmp)) {
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700761 return -EBUSY;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700762 } else if (client.get() != NULL) {
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700763 device = static_cast<Client*>(clientTmp.get());
764 return OK;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700765 }
766
Ruben Brunkb2119af2014-05-09 19:57:56 -0700767 status = connectHelperLocked(cameraClient,
768 cameraId,
769 clientPackageName,
770 clientUid,
771 callingPid,
772 client);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700773 if (status != OK) {
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700774 return status;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700775 }
776
Igor Murashkine6800ce2013-03-04 17:25:57 -0800777 }
Igor Murashkinacd695c2013-03-13 17:23:00 -0700778 // important: release the mutex here so the client can call back
779 // into the service from its destructor (can be at the end of the call)
Igor Murashkinbfc99152013-02-27 12:55:20 -0800780
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700781 device = client;
782 return OK;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700783}
784
Zhijun Heb10cdad2014-06-16 16:38:35 -0700785status_t CameraService::connectLegacy(
786 const sp<ICameraClient>& cameraClient,
787 int cameraId, int halVersion,
788 const String16& clientPackageName,
789 int clientUid,
790 /*out*/
791 sp<ICamera>& device) {
792
Igor Murashkin3d07d1a2014-06-20 11:27:03 -0700793 if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED &&
794 mModule->common.module_api_version < CAMERA_MODULE_API_VERSION_2_3) {
795 /*
796 * Either the HAL version is unspecified in which case this just creates
797 * a camera client selected by the latest device version, or
798 * it's a particular version in which case the HAL must supported
799 * the open_legacy call
800 */
Zhijun Heb10cdad2014-06-16 16:38:35 -0700801 ALOGE("%s: camera HAL module version %x doesn't support connecting to legacy HAL devices!",
802 __FUNCTION__, mModule->common.module_api_version);
803 return INVALID_OPERATION;
804 }
805
806 String8 clientName8(clientPackageName);
807 int callingPid = getCallingPid();
808
809 LOG1("CameraService::connect legacy E (pid %d \"%s\", id %d)", callingPid,
810 clientName8.string(), cameraId);
811
812 status_t status = validateConnect(cameraId, /*inout*/clientUid);
813 if (status != OK) {
814 return status;
815 }
816
817 sp<Client> client;
818 {
819 Mutex::Autolock lock(mServiceLock);
820 sp<BasicClient> clientTmp;
821 if (!canConnectUnsafe(cameraId, clientPackageName,
822 cameraClient->asBinder(),
823 /*out*/clientTmp)) {
824 return -EBUSY;
825 } else if (client.get() != NULL) {
826 device = static_cast<Client*>(clientTmp.get());
827 return OK;
828 }
829
830 status = connectHelperLocked(cameraClient,
831 cameraId,
832 clientPackageName,
833 clientUid,
834 callingPid,
835 client,
836 halVersion);
837 if (status != OK) {
838 return status;
839 }
840
841 }
842 // important: release the mutex here so the client can call back
843 // into the service from its destructor (can be at the end of the call)
844
845 device = client;
846 return OK;
847}
848
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700849status_t CameraService::connectFinishUnsafe(const sp<BasicClient>& client,
850 const sp<IBinder>& remoteCallback) {
851 status_t status = client->initialize(mModule);
852 if (status != OK) {
853 return status;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800854 }
Ruben Brunkb2119af2014-05-09 19:57:56 -0700855 if (remoteCallback != NULL) {
856 remoteCallback->linkToDeath(this);
857 }
Igor Murashkine6800ce2013-03-04 17:25:57 -0800858
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700859 return OK;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800860}
861
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700862status_t CameraService::connectPro(
Igor Murashkin634a5152013-02-20 17:15:11 -0800863 const sp<IProCameraCallbacks>& cameraCb,
Igor Murashkinc073ba52013-02-26 14:32:34 -0800864 int cameraId,
865 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700866 int clientUid,
867 /*out*/
868 sp<IProCameraUser>& device)
Igor Murashkin634a5152013-02-20 17:15:11 -0800869{
Natalie Silvanoviche1b55da2014-05-01 14:44:52 -0700870 if (cameraCb == 0) {
871 ALOGE("%s: Callback must not be null", __FUNCTION__);
872 return BAD_VALUE;
873 }
874
Igor Murashkinbfc99152013-02-27 12:55:20 -0800875 String8 clientName8(clientPackageName);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700876 int callingPid = getCallingPid();
Igor Murashkin634a5152013-02-20 17:15:11 -0800877
Igor Murashkine6800ce2013-03-04 17:25:57 -0800878 LOG1("CameraService::connectPro E (pid %d \"%s\", id %d)", callingPid,
879 clientName8.string(), cameraId);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700880 status_t status = validateConnect(cameraId, /*inout*/clientUid);
881 if (status != OK) {
882 return status;
Igor Murashkin634a5152013-02-20 17:15:11 -0800883 }
884
Igor Murashkinacd695c2013-03-13 17:23:00 -0700885 sp<ProClient> client;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800886 {
Igor Murashkinacd695c2013-03-13 17:23:00 -0700887 Mutex::Autolock lock(mServiceLock);
888 {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700889 sp<BasicClient> client;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700890 if (!canConnectUnsafe(cameraId, clientPackageName,
891 cameraCb->asBinder(),
892 /*out*/client)) {
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700893 return -EBUSY;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700894 }
895 }
896
897 int facing = -1;
898 int deviceVersion = getDeviceVersion(cameraId, &facing);
899
900 switch(deviceVersion) {
901 case CAMERA_DEVICE_API_VERSION_1_0:
902 ALOGE("Camera id %d uses HALv1, doesn't support ProCamera",
903 cameraId);
Ruben Brunk17963d12013-08-19 15:21:19 -0700904 return -EOPNOTSUPP;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700905 break;
906 case CAMERA_DEVICE_API_VERSION_2_0:
907 case CAMERA_DEVICE_API_VERSION_2_1:
Zhijun He47110052013-07-22 17:34:34 -0700908 case CAMERA_DEVICE_API_VERSION_3_0:
Zhijun He95dd5ba2014-03-26 18:18:00 -0700909 case CAMERA_DEVICE_API_VERSION_3_1:
910 case CAMERA_DEVICE_API_VERSION_3_2:
Eino-Ville Talvala63d877f2014-06-16 19:21:12 -0700911 client = new ProCamera2Client(this, cameraCb, clientPackageName,
912 cameraId, facing, callingPid, clientUid, getpid());
Igor Murashkinacd695c2013-03-13 17:23:00 -0700913 break;
914 case -1:
915 ALOGE("Invalid camera id %d", cameraId);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700916 return BAD_VALUE;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700917 default:
918 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700919 return INVALID_OPERATION;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800920 }
Igor Murashkinacd695c2013-03-13 17:23:00 -0700921
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700922 status_t status = connectFinishUnsafe(client, client->getRemote());
923 if (status != OK) {
924 return status;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700925 }
926
927 mProClientList[cameraId].push(client);
928
929 LOG1("CameraService::connectPro X (id %d, this pid is %d)", cameraId,
930 getpid());
Igor Murashkine6800ce2013-03-04 17:25:57 -0800931 }
Igor Murashkinacd695c2013-03-13 17:23:00 -0700932 // important: release the mutex here so the client can call back
933 // into the service from its destructor (can be at the end of the call)
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700934 device = client;
935 return OK;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800936}
Igor Murashkin634a5152013-02-20 17:15:11 -0800937
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700938status_t CameraService::connectDevice(
Igor Murashkine7ee7632013-06-11 18:10:18 -0700939 const sp<ICameraDeviceCallbacks>& cameraCb,
940 int cameraId,
941 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700942 int clientUid,
943 /*out*/
944 sp<ICameraDeviceUser>& device)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700945{
Igor Murashkine7ee7632013-06-11 18:10:18 -0700946
947 String8 clientName8(clientPackageName);
948 int callingPid = getCallingPid();
949
950 LOG1("CameraService::connectDevice E (pid %d \"%s\", id %d)", callingPid,
951 clientName8.string(), cameraId);
952
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700953 status_t status = validateConnect(cameraId, /*inout*/clientUid);
954 if (status != OK) {
955 return status;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700956 }
957
958 sp<CameraDeviceClient> client;
959 {
960 Mutex::Autolock lock(mServiceLock);
961 {
962 sp<BasicClient> client;
963 if (!canConnectUnsafe(cameraId, clientPackageName,
964 cameraCb->asBinder(),
965 /*out*/client)) {
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700966 return -EBUSY;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700967 }
968 }
969
970 int facing = -1;
971 int deviceVersion = getDeviceVersion(cameraId, &facing);
972
973 // If there are other non-exclusive users of the camera,
974 // this will tear them down before we can reuse the camera
975 if (isValidCameraId(cameraId)) {
976 // transition from PRESENT -> NOT_AVAILABLE
977 updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE,
978 cameraId);
979 }
980
981 switch(deviceVersion) {
982 case CAMERA_DEVICE_API_VERSION_1_0:
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700983 ALOGW("Camera using old HAL version: %d", deviceVersion);
Ruben Brunk17963d12013-08-19 15:21:19 -0700984 return -EOPNOTSUPP;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700985 // TODO: don't allow 2.0 Only allow 2.1 and higher
986 case CAMERA_DEVICE_API_VERSION_2_0:
987 case CAMERA_DEVICE_API_VERSION_2_1:
988 case CAMERA_DEVICE_API_VERSION_3_0:
Zhijun He95dd5ba2014-03-26 18:18:00 -0700989 case CAMERA_DEVICE_API_VERSION_3_1:
990 case CAMERA_DEVICE_API_VERSION_3_2:
Eino-Ville Talvala63d877f2014-06-16 19:21:12 -0700991 client = new CameraDeviceClient(this, cameraCb, clientPackageName,
992 cameraId, facing, callingPid, clientUid, getpid());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700993 break;
994 case -1:
995 ALOGE("Invalid camera id %d", cameraId);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700996 return BAD_VALUE;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700997 default:
998 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700999 return INVALID_OPERATION;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001000 }
1001
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001002 status_t status = connectFinishUnsafe(client, client->getRemote());
1003 if (status != OK) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001004 // this is probably not recoverable.. maybe the client can try again
1005 // OK: we can only get here if we were originally in PRESENT state
1006 updateStatus(ICameraServiceListener::STATUS_PRESENT, cameraId);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001007 return status;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001008 }
1009
1010 LOG1("CameraService::connectDevice X (id %d, this pid is %d)", cameraId,
1011 getpid());
1012
1013 mClient[cameraId] = client;
1014 }
1015 // important: release the mutex here so the client can call back
1016 // into the service from its destructor (can be at the end of the call)
1017
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001018 device = client;
1019 return OK;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001020}
1021
1022
Igor Murashkinbfc99152013-02-27 12:55:20 -08001023status_t CameraService::addListener(
1024 const sp<ICameraServiceListener>& listener) {
1025 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -08001026
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001027 if (listener == 0) {
1028 ALOGE("%s: Listener must not be null", __FUNCTION__);
1029 return BAD_VALUE;
1030 }
1031
Igor Murashkinbfc99152013-02-27 12:55:20 -08001032 Mutex::Autolock lock(mServiceLock);
1033
1034 Vector<sp<ICameraServiceListener> >::iterator it, end;
1035 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
1036 if ((*it)->asBinder() == listener->asBinder()) {
1037 ALOGW("%s: Tried to add listener %p which was already subscribed",
1038 __FUNCTION__, listener.get());
1039 return ALREADY_EXISTS;
1040 }
1041 }
1042
1043 mListenerList.push_back(listener);
1044
Igor Murashkincba2c162013-03-20 15:56:31 -07001045 /* Immediately signal current status to this listener only */
1046 {
1047 Mutex::Autolock m(mStatusMutex) ;
1048 int numCams = getNumberOfCameras();
1049 for (int i = 0; i < numCams; ++i) {
1050 listener->onStatusChanged(mStatusList[i], i);
1051 }
1052 }
1053
Igor Murashkinbfc99152013-02-27 12:55:20 -08001054 return OK;
1055}
1056status_t CameraService::removeListener(
1057 const sp<ICameraServiceListener>& listener) {
1058 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
1059
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001060 if (listener == 0) {
1061 ALOGE("%s: Listener must not be null", __FUNCTION__);
1062 return BAD_VALUE;
1063 }
1064
Igor Murashkinbfc99152013-02-27 12:55:20 -08001065 Mutex::Autolock lock(mServiceLock);
1066
1067 Vector<sp<ICameraServiceListener> >::iterator it;
1068 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
1069 if ((*it)->asBinder() == listener->asBinder()) {
1070 mListenerList.erase(it);
1071 return OK;
1072 }
1073 }
1074
1075 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
1076 __FUNCTION__, listener.get());
1077
1078 return BAD_VALUE;
Igor Murashkin634a5152013-02-20 17:15:11 -08001079}
1080
Igor Murashkin65d14b92014-06-17 12:03:20 -07001081status_t CameraService::getLegacyParameters(
1082 int cameraId,
1083 /*out*/
1084 String16* parameters) {
1085 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1086
1087 if (parameters == NULL) {
1088 ALOGE("%s: parameters must not be null", __FUNCTION__);
1089 return BAD_VALUE;
1090 }
1091
1092 status_t ret = 0;
1093
1094 CameraParameters shimParams;
1095 if ((ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)) != OK) {
1096 // Error logged by caller
1097 return ret;
1098 }
1099
1100 String8 shimParamsString8 = shimParams.flatten();
1101 String16 shimParamsString16 = String16(shimParamsString8);
1102
1103 *parameters = shimParamsString16;
1104
1105 return OK;
1106}
1107
1108status_t CameraService::supportsCameraApi(int cameraId, int apiVersion) {
1109 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1110
1111 switch (apiVersion) {
1112 case API_VERSION_1:
1113 case API_VERSION_2:
1114 break;
1115 default:
1116 ALOGE("%s: Bad API version %d", __FUNCTION__, apiVersion);
1117 return BAD_VALUE;
1118 }
1119
1120 int facing = -1;
1121 int deviceVersion = getDeviceVersion(cameraId, &facing);
1122
1123 switch(deviceVersion) {
1124 case CAMERA_DEVICE_API_VERSION_1_0:
1125 case CAMERA_DEVICE_API_VERSION_2_0:
1126 case CAMERA_DEVICE_API_VERSION_2_1:
1127 case CAMERA_DEVICE_API_VERSION_3_0:
1128 case CAMERA_DEVICE_API_VERSION_3_1:
1129 if (apiVersion == API_VERSION_2) {
1130 ALOGV("%s: Camera id %d uses HAL prior to HAL3.2, doesn't support api2 without shim",
1131 __FUNCTION__, cameraId);
1132 return -EOPNOTSUPP;
1133 } else { // if (apiVersion == API_VERSION_1) {
1134 ALOGV("%s: Camera id %d uses older HAL before 3.2, but api1 is always supported",
1135 __FUNCTION__, cameraId);
1136 return OK;
1137 }
1138 case CAMERA_DEVICE_API_VERSION_3_2:
1139 ALOGV("%s: Camera id %d uses HAL3.2 or newer, supports api1/api2 directly",
1140 __FUNCTION__, cameraId);
1141 return OK;
1142 case -1:
1143 ALOGE("%s: Invalid camera id %d", __FUNCTION__, cameraId);
1144 return BAD_VALUE;
1145 default:
1146 ALOGE("%s: Unknown camera device HAL version: %d", __FUNCTION__, deviceVersion);
1147 return INVALID_OPERATION;
1148 }
1149
1150 return OK;
1151}
1152
Igor Murashkin634a5152013-02-20 17:15:11 -08001153void CameraService::removeClientByRemote(const wp<IBinder>& remoteBinder) {
1154 int callingPid = getCallingPid();
1155 LOG1("CameraService::removeClientByRemote E (pid %d)", callingPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001156
Igor Murashkinecf17e82012-10-02 16:05:11 -07001157 // Declare this before the lock to make absolutely sure the
1158 // destructor won't be called with the lock held.
1159 Mutex::Autolock lock(mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001160
Igor Murashkinecf17e82012-10-02 16:05:11 -07001161 int outIndex;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001162 sp<BasicClient> client = findClientUnsafe(remoteBinder, outIndex);
Igor Murashkinecf17e82012-10-02 16:05:11 -07001163
1164 if (client != 0) {
1165 // Found our camera, clear and leave.
1166 LOG1("removeClient: clear camera %d", outIndex);
Igor Murashkinecf17e82012-10-02 16:05:11 -07001167
Ruben Brunkb2119af2014-05-09 19:57:56 -07001168 sp<IBinder> remote = client->getRemote();
1169 if (remote != NULL) {
1170 remote->unlinkToDeath(this);
1171 }
1172
1173 mClient[outIndex].clear();
Igor Murashkin634a5152013-02-20 17:15:11 -08001174 } else {
1175
1176 sp<ProClient> clientPro = findProClientUnsafe(remoteBinder);
1177
1178 if (clientPro != NULL) {
1179 // Found our camera, clear and leave.
1180 LOG1("removeClient: clear pro %p", clientPro.get());
1181
1182 clientPro->getRemoteCallback()->asBinder()->unlinkToDeath(this);
1183 }
Igor Murashkinecf17e82012-10-02 16:05:11 -07001184 }
1185
Igor Murashkin634a5152013-02-20 17:15:11 -08001186 LOG1("CameraService::removeClientByRemote X (pid %d)", callingPid);
1187}
1188
1189sp<CameraService::ProClient> CameraService::findProClientUnsafe(
1190 const wp<IBinder>& cameraCallbacksRemote)
1191{
1192 sp<ProClient> clientPro;
1193
1194 for (int i = 0; i < mNumberOfCameras; ++i) {
1195 Vector<size_t> removeIdx;
1196
1197 for (size_t j = 0; j < mProClientList[i].size(); ++j) {
1198 wp<ProClient> cl = mProClientList[i][j];
1199
1200 sp<ProClient> clStrong = cl.promote();
1201 if (clStrong != NULL && clStrong->getRemote() == cameraCallbacksRemote) {
1202 clientPro = clStrong;
1203 break;
1204 } else if (clStrong == NULL) {
1205 // mark to clean up dead ptr
1206 removeIdx.push(j);
1207 }
1208 }
1209
1210 // remove stale ptrs (in reverse so the indices dont change)
1211 for (ssize_t j = (ssize_t)removeIdx.size() - 1; j >= 0; --j) {
1212 mProClientList[i].removeAt(removeIdx[j]);
1213 }
1214
1215 }
1216
1217 return clientPro;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001218}
1219
Igor Murashkine7ee7632013-06-11 18:10:18 -07001220sp<CameraService::BasicClient> CameraService::findClientUnsafe(
Igor Murashkin294d0ec2012-10-05 10:44:57 -07001221 const wp<IBinder>& cameraClient, int& outIndex) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001222 sp<BasicClient> client;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001223
1224 for (int i = 0; i < mNumberOfCameras; i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001225
1226 // This happens when we have already disconnected (or this is
1227 // just another unused camera).
1228 if (mClient[i] == 0) continue;
1229
1230 // Promote mClient. It can fail if we are called from this path:
Igor Murashkin634a5152013-02-20 17:15:11 -08001231 // Client::~Client() -> disconnect() -> removeClientByRemote().
Mathias Agopian65ab4712010-07-14 17:59:35 -07001232 client = mClient[i].promote();
1233
Igor Murashkinecf17e82012-10-02 16:05:11 -07001234 // Clean up stale client entry
1235 if (client == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001236 mClient[i].clear();
1237 continue;
1238 }
1239
Igor Murashkine7ee7632013-06-11 18:10:18 -07001240 if (cameraClient == client->getRemote()) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001241 // Found our camera
1242 outIndex = i;
1243 return client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001244 }
1245 }
1246
Igor Murashkinecf17e82012-10-02 16:05:11 -07001247 outIndex = -1;
1248 return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001249}
1250
Igor Murashkine7ee7632013-06-11 18:10:18 -07001251CameraService::BasicClient* CameraService::getClientByIdUnsafe(int cameraId) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001252 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
Keun young Parkd8973a72012-03-28 14:13:09 -07001253 return mClient[cameraId].unsafe_get();
1254}
1255
1256Mutex* CameraService::getClientLockById(int cameraId) {
1257 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
1258 return &mClientLock[cameraId];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001259}
1260
Igor Murashkin634a5152013-02-20 17:15:11 -08001261sp<CameraService::BasicClient> CameraService::getClientByRemote(
Igor Murashkin294d0ec2012-10-05 10:44:57 -07001262 const wp<IBinder>& cameraClient) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001263
1264 // Declare this before the lock to make absolutely sure the
1265 // destructor won't be called with the lock held.
Igor Murashkin634a5152013-02-20 17:15:11 -08001266 sp<BasicClient> client;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001267
1268 Mutex::Autolock lock(mServiceLock);
1269
1270 int outIndex;
1271 client = findClientUnsafe(cameraClient, outIndex);
1272
1273 return client;
1274}
1275
Mathias Agopian65ab4712010-07-14 17:59:35 -07001276status_t CameraService::onTransact(
1277 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
1278 // Permission checks
1279 switch (code) {
1280 case BnCameraService::CONNECT:
Igor Murashkin634a5152013-02-20 17:15:11 -08001281 case BnCameraService::CONNECT_PRO:
Eino-Ville Talvala63d877f2014-06-16 19:21:12 -07001282 case BnCameraService::CONNECT_DEVICE:
Zhijun Heb10cdad2014-06-16 16:38:35 -07001283 case BnCameraService::CONNECT_LEGACY:
Mathias Agopian65ab4712010-07-14 17:59:35 -07001284 const int pid = getCallingPid();
1285 const int self_pid = getpid();
1286 if (pid != self_pid) {
1287 // we're called from a different process, do the real check
1288 if (!checkCallingPermission(
1289 String16("android.permission.CAMERA"))) {
1290 const int uid = getCallingUid();
Steve Block29357bc2012-01-06 19:20:56 +00001291 ALOGE("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -07001292 "can't use the camera pid=%d, uid=%d", pid, uid);
1293 return PERMISSION_DENIED;
1294 }
1295 }
1296 break;
1297 }
1298
1299 return BnCameraService::onTransact(code, data, reply, flags);
1300}
1301
1302// The reason we need this busy bit is a new CameraService::connect() request
1303// may come in while the previous Client's destructor has not been run or is
1304// still running. If the last strong reference of the previous Client is gone
1305// but the destructor has not been finished, we should not allow the new Client
1306// to be created because we need to wait for the previous Client to tear down
1307// the hardware first.
1308void CameraService::setCameraBusy(int cameraId) {
1309 android_atomic_write(1, &mBusy[cameraId]);
Igor Murashkinecf17e82012-10-02 16:05:11 -07001310
1311 ALOGV("setCameraBusy cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001312}
1313
1314void CameraService::setCameraFree(int cameraId) {
1315 android_atomic_write(0, &mBusy[cameraId]);
Igor Murashkinecf17e82012-10-02 16:05:11 -07001316
1317 ALOGV("setCameraFree cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001318}
1319
1320// We share the media players for shutter and recording sound for all clients.
1321// A reference count is kept to determine when we will actually release the
1322// media players.
1323
Chih-Chung Changff4f55c2011-10-17 19:03:12 +08001324MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001325 MediaPlayer* mp = new MediaPlayer();
Andreas Huber1b86fe02014-01-29 11:13:26 -08001326 if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -08001327 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001328 mp->prepare();
1329 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001330 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001331 return NULL;
1332 }
1333 return mp;
1334}
1335
1336void CameraService::loadSound() {
1337 Mutex::Autolock lock(mSoundLock);
1338 LOG1("CameraService::loadSound ref=%d", mSoundRef);
1339 if (mSoundRef++) return;
1340
1341 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
1342 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
1343}
1344
1345void CameraService::releaseSound() {
1346 Mutex::Autolock lock(mSoundLock);
1347 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
1348 if (--mSoundRef) return;
1349
1350 for (int i = 0; i < NUM_SOUNDS; i++) {
1351 if (mSoundPlayer[i] != 0) {
1352 mSoundPlayer[i]->disconnect();
1353 mSoundPlayer[i].clear();
1354 }
1355 }
1356}
1357
1358void CameraService::playSound(sound_kind kind) {
1359 LOG1("playSound(%d)", kind);
1360 Mutex::Autolock lock(mSoundLock);
1361 sp<MediaPlayer> player = mSoundPlayer[kind];
1362 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +08001363 player->seekTo(0);
1364 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001365 }
1366}
1367
1368// ----------------------------------------------------------------------------
1369
1370CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -07001371 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001372 const String16& clientPackageName,
1373 int cameraId, int cameraFacing,
1374 int clientPid, uid_t clientUid,
1375 int servicePid) :
Igor Murashkin634a5152013-02-20 17:15:11 -08001376 CameraService::BasicClient(cameraService, cameraClient->asBinder(),
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001377 clientPackageName,
1378 cameraId, cameraFacing,
1379 clientPid, clientUid,
1380 servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -08001381{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001382 int callingPid = getCallingPid();
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001383 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001384
Igor Murashkin44cfcf02013-03-01 16:22:28 -08001385 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001386
Mathias Agopian65ab4712010-07-14 17:59:35 -07001387 cameraService->setCameraBusy(cameraId);
1388 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001389
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001390 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001391}
1392
Mathias Agopian65ab4712010-07-14 17:59:35 -07001393// tear down the client
1394CameraService::Client::~Client() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07001395 ALOGV("~Client");
Igor Murashkin634a5152013-02-20 17:15:11 -08001396 mDestructionStarted = true;
1397
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -07001398 mCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -07001399 // unconditionally disconnect. function is idempotent
1400 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001401}
1402
Igor Murashkin634a5152013-02-20 17:15:11 -08001403CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001404 const sp<IBinder>& remoteCallback,
1405 const String16& clientPackageName,
1406 int cameraId, int cameraFacing,
1407 int clientPid, uid_t clientUid,
1408 int servicePid):
1409 mClientPackageName(clientPackageName)
Igor Murashkin634a5152013-02-20 17:15:11 -08001410{
1411 mCameraService = cameraService;
Igor Murashkin44cfcf02013-03-01 16:22:28 -08001412 mRemoteBinder = remoteCallback;
Igor Murashkin634a5152013-02-20 17:15:11 -08001413 mCameraId = cameraId;
1414 mCameraFacing = cameraFacing;
1415 mClientPid = clientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001416 mClientUid = clientUid;
Igor Murashkin634a5152013-02-20 17:15:11 -08001417 mServicePid = servicePid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001418 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -08001419 mDestructionStarted = false;
1420}
1421
1422CameraService::BasicClient::~BasicClient() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07001423 ALOGV("~BasicClient");
Igor Murashkin634a5152013-02-20 17:15:11 -08001424 mDestructionStarted = true;
1425}
1426
1427void CameraService::BasicClient::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07001428 ALOGV("BasicClient::disconnect");
Igor Murashkin44cfcf02013-03-01 16:22:28 -08001429 mCameraService->removeClientByRemote(mRemoteBinder);
Igor Murashkincba2c162013-03-20 15:56:31 -07001430 // client shouldn't be able to call into us anymore
1431 mClientPid = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -08001432}
1433
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001434status_t CameraService::BasicClient::startCameraOps() {
1435 int32_t res;
1436
1437 mOpsCallback = new OpsCallback(this);
1438
Igor Murashkine6800ce2013-03-04 17:25:57 -08001439 {
1440 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
1441 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
1442 }
1443
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001444 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
1445 mClientPackageName, mOpsCallback);
1446 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
1447 mClientUid, mClientPackageName);
1448
1449 if (res != AppOpsManager::MODE_ALLOWED) {
1450 ALOGI("Camera %d: Access for \"%s\" has been revoked",
1451 mCameraId, String8(mClientPackageName).string());
1452 return PERMISSION_DENIED;
1453 }
1454 mOpsActive = true;
1455 return OK;
1456}
1457
1458status_t CameraService::BasicClient::finishCameraOps() {
1459 if (mOpsActive) {
1460 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
1461 mClientPackageName);
1462 mOpsActive = false;
1463 }
1464 mAppOpsManager.stopWatchingMode(mOpsCallback);
1465 mOpsCallback.clear();
1466
1467 return OK;
1468}
1469
1470void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
1471 String8 name(packageName);
1472 String8 myName(mClientPackageName);
1473
1474 if (op != AppOpsManager::OP_CAMERA) {
1475 ALOGW("Unexpected app ops notification received: %d", op);
1476 return;
1477 }
1478
1479 int32_t res;
1480 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
1481 mClientUid, mClientPackageName);
1482 ALOGV("checkOp returns: %d, %s ", res,
1483 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
1484 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
1485 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
1486 "UNKNOWN");
1487
1488 if (res != AppOpsManager::MODE_ALLOWED) {
1489 ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId,
1490 myName.string());
1491 // Reset the client PID to allow server-initiated disconnect,
1492 // and to prevent further calls by client.
1493 mClientPid = getCallingPid();
Jianing Weicb0652e2014-03-12 18:29:36 -07001494 CaptureResultExtras resultExtras; // a dummy result (invalid)
1495 notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001496 disconnect();
1497 }
1498}
1499
Mathias Agopian65ab4712010-07-14 17:59:35 -07001500// ----------------------------------------------------------------------------
1501
Keun young Parkd8973a72012-03-28 14:13:09 -07001502Mutex* CameraService::Client::getClientLockFromCookie(void* user) {
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001503 return gCameraService->getClientLockById((int)(intptr_t) user);
Keun young Parkd8973a72012-03-28 14:13:09 -07001504}
1505
1506// Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should
1507// be acquired for this to be safe
1508CameraService::Client* CameraService::Client::getClientFromCookie(void* user) {
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001509 BasicClient *basicClient = gCameraService->getClientByIdUnsafe((int)(intptr_t) user);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001510 // OK: only CameraClient calls this, and they already cast anyway.
1511 Client* client = static_cast<Client*>(basicClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001512
1513 // This could happen if the Client is in the process of shutting down (the
1514 // last strong reference is gone, but the destructor hasn't finished
1515 // stopping the hardware).
Keun young Parkd8973a72012-03-28 14:13:09 -07001516 if (client == NULL) return NULL;
1517
1518 // destruction already started, so should not be accessed
1519 if (client->mDestructionStarted) return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001520
Mathias Agopian65ab4712010-07-14 17:59:35 -07001521 return client;
1522}
1523
Jianing Weicb0652e2014-03-12 18:29:36 -07001524void CameraService::Client::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
1525 const CaptureResultExtras& resultExtras) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -08001526 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001527}
1528
Igor Murashkin036bc3e2012-10-08 15:09:46 -07001529// NOTE: function is idempotent
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001530void CameraService::Client::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07001531 ALOGV("Client::disconnect");
Igor Murashkin634a5152013-02-20 17:15:11 -08001532 BasicClient::disconnect();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001533 mCameraService->setCameraFree(mCameraId);
Igor Murashkin93747b92013-05-01 15:42:20 -07001534
1535 StatusVector rejectSourceStates;
1536 rejectSourceStates.push_back(ICameraServiceListener::STATUS_NOT_PRESENT);
1537 rejectSourceStates.push_back(ICameraServiceListener::STATUS_ENUMERATING);
1538
1539 // Transition to PRESENT if the camera is not in either of above 2 states
Igor Murashkincba2c162013-03-20 15:56:31 -07001540 mCameraService->updateStatus(ICameraServiceListener::STATUS_PRESENT,
Igor Murashkin93747b92013-05-01 15:42:20 -07001541 mCameraId,
1542 &rejectSourceStates);
Wu-cheng Lie09591e2010-10-14 20:17:44 +08001543}
1544
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001545CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
1546 mClient(client) {
1547}
1548
1549void CameraService::Client::OpsCallback::opChanged(int32_t op,
1550 const String16& packageName) {
1551 sp<BasicClient> client = mClient.promote();
1552 if (client != NULL) {
1553 client->opChanged(op, packageName);
1554 }
1555}
1556
Mathias Agopian65ab4712010-07-14 17:59:35 -07001557// ----------------------------------------------------------------------------
Igor Murashkin634a5152013-02-20 17:15:11 -08001558// IProCamera
1559// ----------------------------------------------------------------------------
1560
1561CameraService::ProClient::ProClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001562 const sp<IProCameraCallbacks>& remoteCallback,
1563 const String16& clientPackageName,
1564 int cameraId,
1565 int cameraFacing,
1566 int clientPid,
1567 uid_t clientUid,
1568 int servicePid)
1569 : CameraService::BasicClient(cameraService, remoteCallback->asBinder(),
1570 clientPackageName, cameraId, cameraFacing,
1571 clientPid, clientUid, servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -08001572{
1573 mRemoteCallback = remoteCallback;
1574}
1575
1576CameraService::ProClient::~ProClient() {
Igor Murashkin634a5152013-02-20 17:15:11 -08001577}
1578
Jianing Weicb0652e2014-03-12 18:29:36 -07001579void CameraService::ProClient::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
1580 const CaptureResultExtras& resultExtras) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001581 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001582}
1583
Igor Murashkin634a5152013-02-20 17:15:11 -08001584// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -07001585
1586static const int kDumpLockRetries = 50;
1587static const int kDumpLockSleep = 60000;
1588
1589static bool tryLock(Mutex& mutex)
1590{
1591 bool locked = false;
1592 for (int i = 0; i < kDumpLockRetries; ++i) {
1593 if (mutex.tryLock() == NO_ERROR) {
1594 locked = true;
1595 break;
1596 }
1597 usleep(kDumpLockSleep);
1598 }
1599 return locked;
1600}
1601
1602status_t CameraService::dump(int fd, const Vector<String16>& args) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001603 String8 result;
1604 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07001605 result.appendFormat("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -07001606 "can't dump CameraService from pid=%d, uid=%d\n",
1607 getCallingPid(),
1608 getCallingUid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001609 write(fd, result.string(), result.size());
1610 } else {
1611 bool locked = tryLock(mServiceLock);
1612 // failed to lock - CameraService is probably deadlocked
1613 if (!locked) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07001614 result.append("CameraService may be deadlocked\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001615 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001616 }
1617
1618 bool hasClient = false;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001619 if (!mModule) {
1620 result = String8::format("No camera module available!\n");
1621 write(fd, result.string(), result.size());
Kalle Lampila6ec3a152013-04-30 15:27:19 +03001622 if (locked) mServiceLock.unlock();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001623 return NO_ERROR;
1624 }
1625
1626 result = String8::format("Camera module HAL API version: 0x%x\n",
1627 mModule->common.hal_api_version);
1628 result.appendFormat("Camera module API version: 0x%x\n",
1629 mModule->common.module_api_version);
1630 result.appendFormat("Camera module name: %s\n",
1631 mModule->common.name);
1632 result.appendFormat("Camera module author: %s\n",
1633 mModule->common.author);
1634 result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras);
Ruben Brunkf81648e2014-04-17 16:14:57 -07001635
1636 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
1637 if (desc == NULL) {
1638 result.appendFormat("Vendor tags left unimplemented.\n");
1639 } else {
1640 result.appendFormat("Vendor tag definitions:\n");
1641 }
1642
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001643 write(fd, result.string(), result.size());
Ruben Brunkf81648e2014-04-17 16:14:57 -07001644
1645 if (desc != NULL) {
1646 desc->dump(fd, /*verbosity*/2, /*indentation*/4);
1647 }
1648
Mathias Agopian65ab4712010-07-14 17:59:35 -07001649 for (int i = 0; i < mNumberOfCameras; i++) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001650 result = String8::format("Camera %d static information:\n", i);
1651 camera_info info;
1652
1653 status_t rc = mModule->get_camera_info(i, &info);
1654 if (rc != OK) {
1655 result.appendFormat(" Error reading static information!\n");
1656 write(fd, result.string(), result.size());
1657 } else {
1658 result.appendFormat(" Facing: %s\n",
1659 info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT");
1660 result.appendFormat(" Orientation: %d\n", info.orientation);
1661 int deviceVersion;
1662 if (mModule->common.module_api_version <
1663 CAMERA_MODULE_API_VERSION_2_0) {
1664 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
1665 } else {
1666 deviceVersion = info.device_version;
1667 }
1668 result.appendFormat(" Device version: 0x%x\n", deviceVersion);
1669 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) {
1670 result.appendFormat(" Device static metadata:\n");
1671 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -07001672 dump_indented_camera_metadata(info.static_camera_characteristics,
Ruben Brunkf81648e2014-04-17 16:14:57 -07001673 fd, /*verbosity*/2, /*indentation*/4);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001674 } else {
1675 write(fd, result.string(), result.size());
1676 }
1677 }
1678
Igor Murashkine7ee7632013-06-11 18:10:18 -07001679 sp<BasicClient> client = mClient[i].promote();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001680 if (client == 0) {
1681 result = String8::format(" Device is closed, no client instance\n");
1682 write(fd, result.string(), result.size());
1683 continue;
1684 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001685 hasClient = true;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001686 result = String8::format(" Device is open. Client instance dump:\n");
1687 write(fd, result.string(), result.size());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001688 client->dump(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001689 }
1690 if (!hasClient) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001691 result = String8::format("\nNo active camera clients yet.\n");
1692 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001693 }
1694
1695 if (locked) mServiceLock.unlock();
1696
Igor Murashkinff3e31d2013-10-23 16:40:06 -07001697 // Dump camera traces if there were any
1698 write(fd, "\n", 1);
1699 camera3::CameraTraces::dump(fd, args);
1700
Mathias Agopian65ab4712010-07-14 17:59:35 -07001701 // change logging level
1702 int n = args.size();
1703 for (int i = 0; i + 1 < n; i++) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07001704 String16 verboseOption("-v");
1705 if (args[i] == verboseOption) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001706 String8 levelStr(args[i+1]);
1707 int level = atoi(levelStr.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001708 result = String8::format("\nSetting log level to %d.\n", level);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001709 setLogLevel(level);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001710 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001711 }
1712 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001713
Mathias Agopian65ab4712010-07-14 17:59:35 -07001714 }
1715 return NO_ERROR;
1716}
1717
Igor Murashkinecf17e82012-10-02 16:05:11 -07001718/*virtual*/void CameraService::binderDied(
1719 const wp<IBinder> &who) {
1720
Igor Murashkin294d0ec2012-10-05 10:44:57 -07001721 /**
1722 * While tempting to promote the wp<IBinder> into a sp,
1723 * it's actually not supported by the binder driver
1724 */
1725
Igor Murashkinecf17e82012-10-02 16:05:11 -07001726 ALOGV("java clients' binder died");
1727
Igor Murashkin634a5152013-02-20 17:15:11 -08001728 sp<BasicClient> cameraClient = getClientByRemote(who);
Igor Murashkinecf17e82012-10-02 16:05:11 -07001729
Igor Murashkin294d0ec2012-10-05 10:44:57 -07001730 if (cameraClient == 0) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001731 ALOGV("java clients' binder death already cleaned up (normal case)");
1732 return;
1733 }
1734
Igor Murashkinecf17e82012-10-02 16:05:11 -07001735 ALOGW("Disconnecting camera client %p since the binder for it "
1736 "died (this pid %d)", cameraClient.get(), getCallingPid());
1737
1738 cameraClient->disconnect();
1739
1740}
1741
Igor Murashkinbfc99152013-02-27 12:55:20 -08001742void CameraService::updateStatus(ICameraServiceListener::Status status,
Igor Murashkin93747b92013-05-01 15:42:20 -07001743 int32_t cameraId,
1744 const StatusVector *rejectSourceStates) {
Igor Murashkinbfc99152013-02-27 12:55:20 -08001745 // do not lock mServiceLock here or can get into a deadlock from
1746 // connect() -> ProClient::disconnect -> updateStatus
1747 Mutex::Autolock lock(mStatusMutex);
Igor Murashkinbfc99152013-02-27 12:55:20 -08001748
1749 ICameraServiceListener::Status oldStatus = mStatusList[cameraId];
1750
1751 mStatusList[cameraId] = status;
1752
1753 if (oldStatus != status) {
1754 ALOGV("%s: Status has changed for camera ID %d from 0x%x to 0x%x",
1755 __FUNCTION__, cameraId, (uint32_t)oldStatus, (uint32_t)status);
1756
Igor Murashkincba2c162013-03-20 15:56:31 -07001757 if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT &&
1758 (status != ICameraServiceListener::STATUS_PRESENT &&
1759 status != ICameraServiceListener::STATUS_ENUMERATING)) {
1760
1761 ALOGW("%s: From NOT_PRESENT can only transition into PRESENT"
1762 " or ENUMERATING", __FUNCTION__);
1763 mStatusList[cameraId] = oldStatus;
1764 return;
1765 }
1766
Igor Murashkin93747b92013-05-01 15:42:20 -07001767 if (rejectSourceStates != NULL) {
1768 const StatusVector &rejectList = *rejectSourceStates;
1769 StatusVector::const_iterator it = rejectList.begin();
1770
1771 /**
1772 * Sometimes we want to conditionally do a transition.
1773 * For example if a client disconnects, we want to go to PRESENT
1774 * only if we weren't already in NOT_PRESENT or ENUMERATING.
1775 */
1776 for (; it != rejectList.end(); ++it) {
1777 if (oldStatus == *it) {
1778 ALOGV("%s: Rejecting status transition for Camera ID %d, "
1779 " since the source state was was in one of the bad "
1780 " states.", __FUNCTION__, cameraId);
1781 mStatusList[cameraId] = oldStatus;
1782 return;
1783 }
1784 }
1785 }
1786
Igor Murashkinbfc99152013-02-27 12:55:20 -08001787 /**
1788 * ProClients lose their exclusive lock.
1789 * - Done before the CameraClient can initialize the HAL device,
1790 * since we want to be able to close it before they get to initialize
1791 */
1792 if (status == ICameraServiceListener::STATUS_NOT_AVAILABLE) {
1793 Vector<wp<ProClient> > proClients(mProClientList[cameraId]);
1794 Vector<wp<ProClient> >::const_iterator it;
1795
1796 for (it = proClients.begin(); it != proClients.end(); ++it) {
1797 sp<ProClient> proCl = it->promote();
1798 if (proCl.get() != NULL) {
1799 proCl->onExclusiveLockStolen();
1800 }
1801 }
1802 }
1803
1804 Vector<sp<ICameraServiceListener> >::const_iterator it;
1805 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
1806 (*it)->onStatusChanged(status, cameraId);
1807 }
1808 }
1809}
1810
Igor Murashkincba2c162013-03-20 15:56:31 -07001811ICameraServiceListener::Status CameraService::getStatus(int cameraId) const {
1812 if (cameraId < 0 || cameraId >= MAX_CAMERAS) {
1813 ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
1814 return ICameraServiceListener::STATUS_UNKNOWN;
1815 }
1816
1817 Mutex::Autolock al(mStatusMutex);
1818 return mStatusList[cameraId];
1819}
1820
Mathias Agopian65ab4712010-07-14 17:59:35 -07001821}; // namespace android