blob: 7766b904c0614d16650013acde7375e09187bb0d [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;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700238 status_t rc = filterGetInfoErrorCode(
239 mModule->get_camera_info(cameraId, &info));
Iliyan Malchev8951a972011-04-14 16:55:59 -0700240 cameraInfo->facing = info.facing;
241 cameraInfo->orientation = info.orientation;
242 return rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700243}
244
Ruben Brunkb2119af2014-05-09 19:57:56 -0700245
246status_t CameraService::generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo) {
247 status_t ret = OK;
248 struct CameraInfo info;
249 if ((ret = getCameraInfo(cameraId, &info)) != OK) {
250 return ret;
251 }
252
253 CameraMetadata shimInfo;
254 int32_t orientation = static_cast<int32_t>(info.orientation);
255 if ((ret = shimInfo.update(ANDROID_SENSOR_ORIENTATION, &orientation, 1)) != OK) {
256 return ret;
257 }
258
259 uint8_t facing = (info.facing == CAMERA_FACING_FRONT) ?
260 ANDROID_LENS_FACING_FRONT : ANDROID_LENS_FACING_BACK;
261 if ((ret = shimInfo.update(ANDROID_LENS_FACING, &facing, 1)) != OK) {
262 return ret;
263 }
264
Igor Murashkin65d14b92014-06-17 12:03:20 -0700265 CameraParameters shimParams;
266 if ((ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)) != OK) {
267 // Error logged by callee
268 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700269 }
270
271 Vector<Size> sizes;
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700272 Vector<Size> jpegSizes;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700273 Vector<int32_t> formats;
274 const char* supportedPreviewFormats;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700275 {
276 shimParams.getSupportedPreviewSizes(/*out*/sizes);
277 shimParams.getSupportedPreviewFormats(/*out*/formats);
278 shimParams.getSupportedPictureSizes(/*out*/jpegSizes);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700279 }
280
281 // Always include IMPLEMENTATION_DEFINED
282 formats.add(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED);
283
284 const size_t INTS_PER_CONFIG = 4;
285
286 // Build available stream configurations metadata
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700287 size_t streamConfigSize = (sizes.size() * formats.size() + jpegSizes.size()) * INTS_PER_CONFIG;
288
289 Vector<int32_t> streamConfigs;
290 streamConfigs.setCapacity(streamConfigSize);
291
Ruben Brunkb2119af2014-05-09 19:57:56 -0700292 for (size_t i = 0; i < formats.size(); ++i) {
293 for (size_t j = 0; j < sizes.size(); ++j) {
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700294 streamConfigs.add(formats[i]);
295 streamConfigs.add(sizes[j].width);
296 streamConfigs.add(sizes[j].height);
297 streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700298 }
299 }
300
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700301 for (size_t i = 0; i < jpegSizes.size(); ++i) {
302 streamConfigs.add(HAL_PIXEL_FORMAT_BLOB);
303 streamConfigs.add(jpegSizes[i].width);
304 streamConfigs.add(jpegSizes[i].height);
305 streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT);
306 }
307
Ruben Brunkb2119af2014-05-09 19:57:56 -0700308 if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700309 streamConfigs.array(), streamConfigSize)) != OK) {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700310 return ret;
311 }
312
313 int64_t fakeMinFrames[0];
314 // TODO: Fixme, don't fake min frame durations.
315 if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
316 fakeMinFrames, 0)) != OK) {
317 return ret;
318 }
319
320 int64_t fakeStalls[0];
321 // TODO: Fixme, don't fake stall durations.
322 if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
323 fakeStalls, 0)) != OK) {
324 return ret;
325 }
326
327 *cameraInfo = shimInfo;
328 return OK;
329}
330
Zhijun He2b59be82013-09-25 10:14:30 -0700331status_t CameraService::getCameraCharacteristics(int cameraId,
332 CameraMetadata* cameraInfo) {
333 if (!cameraInfo) {
334 ALOGE("%s: cameraInfo is NULL", __FUNCTION__);
335 return BAD_VALUE;
336 }
337
338 if (!mModule) {
339 ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__);
340 return -ENODEV;
341 }
342
Zhijun He2b59be82013-09-25 10:14:30 -0700343 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
344 ALOGE("%s: Invalid camera id: %d", __FUNCTION__, cameraId);
345 return BAD_VALUE;
346 }
347
348 int facing;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700349 status_t ret = OK;
350 if (mModule->common.module_api_version < CAMERA_MODULE_API_VERSION_2_0 ||
351 getDeviceVersion(cameraId, &facing) <= CAMERA_DEVICE_API_VERSION_2_1 ) {
352 /**
353 * Backwards compatibility mode for old HALs:
354 * - Convert CameraInfo into static CameraMetadata properties.
355 * - Retrieve cached CameraParameters for this camera. If none exist,
356 * attempt to open CameraClient and retrieve the CameraParameters.
357 * - Convert cached CameraParameters into static CameraMetadata
358 * properties.
359 */
360 ALOGI("%s: Switching to HAL1 shim implementation...", __FUNCTION__);
Zhijun He2b59be82013-09-25 10:14:30 -0700361
Ruben Brunkb2119af2014-05-09 19:57:56 -0700362 if ((ret = generateShimMetadata(cameraId, cameraInfo)) != OK) {
363 return ret;
364 }
Zhijun Hef05e50e2013-10-01 11:05:33 -0700365
Ruben Brunkb2119af2014-05-09 19:57:56 -0700366 } else {
367 /**
368 * Normal HAL 2.1+ codepath.
369 */
370 struct camera_info info;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700371 ret = filterGetInfoErrorCode(mModule->get_camera_info(cameraId, &info));
Ruben Brunkb2119af2014-05-09 19:57:56 -0700372 *cameraInfo = info.static_camera_characteristics;
373 }
Zhijun He2b59be82013-09-25 10:14:30 -0700374
375 return ret;
376}
377
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800378status_t CameraService::getCameraVendorTagDescriptor(/*out*/sp<VendorTagDescriptor>& desc) {
379 if (!mModule) {
380 ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__);
381 return -ENODEV;
382 }
383
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800384 desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
385 return OK;
386}
387
Igor Murashkin634a5152013-02-20 17:15:11 -0800388int CameraService::getDeviceVersion(int cameraId, int* facing) {
389 struct camera_info info;
390 if (mModule->get_camera_info(cameraId, &info) != OK) {
391 return -1;
392 }
393
394 int deviceVersion;
395 if (mModule->common.module_api_version >= CAMERA_MODULE_API_VERSION_2_0) {
396 deviceVersion = info.device_version;
397 } else {
398 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
399 }
400
401 if (facing) {
402 *facing = info.facing;
403 }
404
405 return deviceVersion;
406}
407
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700408status_t CameraService::filterOpenErrorCode(status_t err) {
409 switch(err) {
410 case NO_ERROR:
411 case -EBUSY:
412 case -EINVAL:
413 case -EUSERS:
414 return err;
415 default:
416 break;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800417 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700418 return -ENODEV;
419}
Igor Murashkinbfc99152013-02-27 12:55:20 -0800420
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700421status_t CameraService::filterGetInfoErrorCode(status_t err) {
422 switch(err) {
423 case NO_ERROR:
424 case -EINVAL:
425 return err;
426 default:
427 break;
428 }
429 return -ENODEV;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800430}
431
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800432bool CameraService::setUpVendorTags() {
433 vendor_tag_ops_t vOps = vendor_tag_ops_t();
434
435 // Check if vendor operations have been implemented
436 if (mModule->get_vendor_tag_ops == NULL) {
437 ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__);
438 return false;
439 }
440
441 ATRACE_BEGIN("camera3->get_metadata_vendor_tag_ops");
442 mModule->get_vendor_tag_ops(&vOps);
443 ATRACE_END();
444
445 // Ensure all vendor operations are present
446 if (vOps.get_tag_count == NULL || vOps.get_all_tags == NULL ||
447 vOps.get_section_name == NULL || vOps.get_tag_name == NULL ||
448 vOps.get_tag_type == NULL) {
449 ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions."
450 , __FUNCTION__);
451 return false;
452 }
453
454 // Read all vendor tag definitions into a descriptor
455 sp<VendorTagDescriptor> desc;
456 status_t res;
457 if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc))
458 != OK) {
459 ALOGE("%s: Could not generate descriptor from vendor tag operations,"
460 "received error %s (%d). Camera clients will not be able to use"
461 "vendor tags", __FUNCTION__, strerror(res), res);
462 return false;
463 }
464
465 // Set the global descriptor to use with camera metadata
466 VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc);
467 return true;
468}
469
Ruben Brunkb2119af2014-05-09 19:57:56 -0700470status_t CameraService::initializeShimMetadata(int cameraId) {
471 int pid = getCallingPid();
472 int uid = getCallingUid();
473 status_t ret = validateConnect(cameraId, uid);
474 if (ret != OK) {
Igor Murashkin65d14b92014-06-17 12:03:20 -0700475 // Error already logged by callee
Ruben Brunkb2119af2014-05-09 19:57:56 -0700476 return ret;
477 }
478
479 bool needsNewClient = false;
480 sp<Client> client;
481
482 String16 internalPackageName("media");
483 { // Scope for service lock
484 Mutex::Autolock lock(mServiceLock);
485 if (mClient[cameraId] != NULL) {
486 client = static_cast<Client*>(mClient[cameraId].promote().get());
487 }
488 if (client == NULL) {
489 needsNewClient = true;
490 ret = connectHelperLocked(/*cameraClient*/NULL, // Empty binder callbacks
491 cameraId,
492 internalPackageName,
493 uid,
494 pid,
495 client);
496
497 if (ret != OK) {
Igor Murashkin65d14b92014-06-17 12:03:20 -0700498 // Error already logged by callee
Ruben Brunkb2119af2014-05-09 19:57:56 -0700499 return ret;
500 }
501 }
502
503 if (client == NULL) {
504 ALOGE("%s: Could not connect to client camera device.", __FUNCTION__);
505 return BAD_VALUE;
506 }
507
508 String8 rawParams = client->getParameters();
509 CameraParameters params(rawParams);
510 mShimParams.add(cameraId, params);
511 }
512
513 // Close client if one was opened solely for this call
514 if (needsNewClient) {
515 client->disconnect();
516 }
517 return OK;
518}
519
Igor Murashkin65d14b92014-06-17 12:03:20 -0700520status_t CameraService::getLegacyParametersLazy(int cameraId,
521 /*out*/
522 CameraParameters* parameters) {
523
524 ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId);
525
526 status_t ret = 0;
527
528 if (parameters == NULL) {
529 ALOGE("%s: parameters must not be null", __FUNCTION__);
530 return BAD_VALUE;
531 }
532
533 ssize_t index = -1;
534 { // Scope for service lock
535 Mutex::Autolock lock(mServiceLock);
536 index = mShimParams.indexOfKey(cameraId);
537 // Release service lock so initializeShimMetadata can be called correctly.
538
539 if (index >= 0) {
540 *parameters = mShimParams[index];
541 }
542 }
543
544 if (index < 0) {
545 int64_t token = IPCThreadState::self()->clearCallingIdentity();
546 ret = initializeShimMetadata(cameraId);
547 IPCThreadState::self()->restoreCallingIdentity(token);
548 if (ret != OK) {
549 // Error already logged by callee
550 return ret;
551 }
552
553 { // Scope for service lock
554 Mutex::Autolock lock(mServiceLock);
555 index = mShimParams.indexOfKey(cameraId);
556
557 LOG_ALWAYS_FATAL_IF(index < 0, "index should have been initialized");
558
559 *parameters = mShimParams[index];
560 }
561 }
562
563 return OK;
564}
565
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700566status_t CameraService::validateConnect(int cameraId,
Igor Murashkine6800ce2013-03-04 17:25:57 -0800567 /*inout*/
568 int& clientUid) const {
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800569
Mathias Agopian65ab4712010-07-14 17:59:35 -0700570 int callingPid = getCallingPid();
Tyler Luu5861a9a2011-10-06 00:00:03 -0500571
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800572 if (clientUid == USE_CALLING_UID) {
573 clientUid = getCallingUid();
574 } else {
575 // We only trust our own process to forward client UIDs
576 if (callingPid != getpid()) {
577 ALOGE("CameraService::connect X (pid %d) rejected (don't trust clientUid)",
578 callingPid);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700579 return PERMISSION_DENIED;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800580 }
581 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700582
Iliyan Malchev8951a972011-04-14 16:55:59 -0700583 if (!mModule) {
Steve Block29357bc2012-01-06 19:20:56 +0000584 ALOGE("Camera HAL module not loaded");
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700585 return -ENODEV;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700586 }
587
Mathias Agopian65ab4712010-07-14 17:59:35 -0700588 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Steve Block29357bc2012-01-06 19:20:56 +0000589 ALOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700590 callingPid, cameraId);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700591 return -ENODEV;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700592 }
593
Wu-cheng Lia3355432011-05-20 14:54:25 +0800594 char value[PROPERTY_VALUE_MAX];
595 property_get("sys.secpolicy.camera.disabled", value, "0");
596 if (strcmp(value, "1") == 0) {
597 // Camera is disabled by DevicePolicyManager.
Steve Blockdf64d152012-01-04 20:05:49 +0000598 ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700599 return -EACCES;
Wu-cheng Lia3355432011-05-20 14:54:25 +0800600 }
601
Igor Murashkincba2c162013-03-20 15:56:31 -0700602 ICameraServiceListener::Status currentStatus = getStatus(cameraId);
603 if (currentStatus == ICameraServiceListener::STATUS_NOT_PRESENT) {
604 ALOGI("Camera is not plugged in,"
605 " connect X (pid %d) rejected", callingPid);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700606 return -ENODEV;
Igor Murashkincba2c162013-03-20 15:56:31 -0700607 } else if (currentStatus == ICameraServiceListener::STATUS_ENUMERATING) {
608 ALOGI("Camera is enumerating,"
609 " connect X (pid %d) rejected", callingPid);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700610 return -EBUSY;
Igor Murashkincba2c162013-03-20 15:56:31 -0700611 }
612 // Else don't check for STATUS_NOT_AVAILABLE.
613 // -- It's done implicitly in canConnectUnsafe /w the mBusy array
614
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700615 return OK;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800616}
617
618bool CameraService::canConnectUnsafe(int cameraId,
619 const String16& clientPackageName,
620 const sp<IBinder>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -0700621 sp<BasicClient> &client) {
Igor Murashkine6800ce2013-03-04 17:25:57 -0800622 String8 clientName8(clientPackageName);
623 int callingPid = getCallingPid();
624
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800625 if (mClient[cameraId] != 0) {
626 client = mClient[cameraId].promote();
627 if (client != 0) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700628 if (remoteCallback == client->getRemote()) {
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800629 LOG1("CameraService::connect X (pid %d) (the same client)",
630 callingPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800631 return true;
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800632 } else {
Igor Murashkine6800ce2013-03-04 17:25:57 -0800633 // TODOSC: need to support 1 regular client,
634 // multiple shared clients here
635 ALOGW("CameraService::connect X (pid %d) rejected"
636 " (existing client).", callingPid);
637 return false;
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800638 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800639 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800640 mClient[cameraId].clear();
641 }
642
Igor Murashkin634a5152013-02-20 17:15:11 -0800643 /*
644 mBusy is set to false as the last step of the Client destructor,
645 after which it is guaranteed that the Client destructor has finished (
646 including any inherited destructors)
647
648 We only need this for a Client subclasses since we don't allow
649 multiple Clents to be opened concurrently, but multiple BasicClient
650 would be fine
651 */
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800652 if (mBusy[cameraId]) {
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800653 ALOGW("CameraService::connect X (pid %d, \"%s\") rejected"
654 " (camera %d is still busy).", callingPid,
655 clientName8.string(), cameraId);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800656 return false;
657 }
658
659 return true;
660}
661
Ruben Brunkb2119af2014-05-09 19:57:56 -0700662status_t CameraService::connectHelperLocked(const sp<ICameraClient>& cameraClient,
663 int cameraId,
664 const String16& clientPackageName,
665 int clientUid,
666 int callingPid,
667 /*out*/
Zhijun Heb10cdad2014-06-16 16:38:35 -0700668 sp<Client>& client,
669 int halVersion) {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700670
671 int facing = -1;
672 int deviceVersion = getDeviceVersion(cameraId, &facing);
673
Zhijun Heb10cdad2014-06-16 16:38:35 -0700674 if (halVersion < 0 || halVersion == deviceVersion) {
675 // Default path: HAL version is unspecified by caller, create CameraClient
676 // based on device version reported by the HAL.
677 switch(deviceVersion) {
678 case CAMERA_DEVICE_API_VERSION_1_0:
679 client = new CameraClient(this, cameraClient,
680 clientPackageName, cameraId,
681 facing, callingPid, clientUid, getpid());
682 break;
683 case CAMERA_DEVICE_API_VERSION_2_0:
684 case CAMERA_DEVICE_API_VERSION_2_1:
685 case CAMERA_DEVICE_API_VERSION_3_0:
686 case CAMERA_DEVICE_API_VERSION_3_1:
687 case CAMERA_DEVICE_API_VERSION_3_2:
688 client = new Camera2Client(this, cameraClient,
689 clientPackageName, cameraId,
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700690 facing, callingPid, clientUid, getpid());
Zhijun Heb10cdad2014-06-16 16:38:35 -0700691 break;
692 case -1:
693 ALOGE("Invalid camera id %d", cameraId);
694 return BAD_VALUE;
695 default:
696 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
697 return INVALID_OPERATION;
698 }
699 } else {
700 // A particular HAL version is requested by caller. Create CameraClient
701 // based on the requested HAL version.
702 if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 &&
703 halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
704 // Only support higher HAL version device opened as HAL1.0 device.
705 client = new CameraClient(this, cameraClient,
706 clientPackageName, cameraId,
707 facing, callingPid, clientUid, getpid());
708 } else {
709 // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
710 ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
711 " opened as HAL %x device", halVersion, deviceVersion,
712 CAMERA_DEVICE_API_VERSION_1_0);
713 return INVALID_OPERATION;
714 }
Ruben Brunkb2119af2014-05-09 19:57:56 -0700715 }
716
717 status_t status = connectFinishUnsafe(client, client->getRemote());
718 if (status != OK) {
719 // this is probably not recoverable.. maybe the client can try again
Ruben Brunkb2119af2014-05-09 19:57:56 -0700720 return status;
721 }
722
723 mClient[cameraId] = client;
724 LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId,
725 getpid());
726
727 return OK;
728}
729
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700730status_t CameraService::connect(
Igor Murashkine6800ce2013-03-04 17:25:57 -0800731 const sp<ICameraClient>& cameraClient,
732 int cameraId,
733 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700734 int clientUid,
735 /*out*/
736 sp<ICamera>& device) {
Igor Murashkine6800ce2013-03-04 17:25:57 -0800737
738 String8 clientName8(clientPackageName);
739 int callingPid = getCallingPid();
740
741 LOG1("CameraService::connect E (pid %d \"%s\", id %d)", callingPid,
742 clientName8.string(), cameraId);
743
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700744 status_t status = validateConnect(cameraId, /*inout*/clientUid);
745 if (status != OK) {
746 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700747 }
748
Igor Murashkine6800ce2013-03-04 17:25:57 -0800749
Igor Murashkine7ee7632013-06-11 18:10:18 -0700750 sp<Client> client;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700751 {
752 Mutex::Autolock lock(mServiceLock);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700753 sp<BasicClient> clientTmp;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700754 if (!canConnectUnsafe(cameraId, clientPackageName,
755 cameraClient->asBinder(),
Igor Murashkine7ee7632013-06-11 18:10:18 -0700756 /*out*/clientTmp)) {
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700757 return -EBUSY;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700758 } else if (client.get() != NULL) {
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700759 device = static_cast<Client*>(clientTmp.get());
760 return OK;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700761 }
762
Ruben Brunkb2119af2014-05-09 19:57:56 -0700763 status = connectHelperLocked(cameraClient,
764 cameraId,
765 clientPackageName,
766 clientUid,
767 callingPid,
768 client);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700769 if (status != OK) {
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700770 return status;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700771 }
772
Igor Murashkine6800ce2013-03-04 17:25:57 -0800773 }
Igor Murashkinacd695c2013-03-13 17:23:00 -0700774 // important: release the mutex here so the client can call back
775 // into the service from its destructor (can be at the end of the call)
Igor Murashkinbfc99152013-02-27 12:55:20 -0800776
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700777 device = client;
778 return OK;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700779}
780
Zhijun Heb10cdad2014-06-16 16:38:35 -0700781status_t CameraService::connectLegacy(
782 const sp<ICameraClient>& cameraClient,
783 int cameraId, int halVersion,
784 const String16& clientPackageName,
785 int clientUid,
786 /*out*/
787 sp<ICamera>& device) {
788
Igor Murashkin3d07d1a2014-06-20 11:27:03 -0700789 if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED &&
790 mModule->common.module_api_version < CAMERA_MODULE_API_VERSION_2_3) {
791 /*
792 * Either the HAL version is unspecified in which case this just creates
793 * a camera client selected by the latest device version, or
794 * it's a particular version in which case the HAL must supported
795 * the open_legacy call
796 */
Zhijun Heb10cdad2014-06-16 16:38:35 -0700797 ALOGE("%s: camera HAL module version %x doesn't support connecting to legacy HAL devices!",
798 __FUNCTION__, mModule->common.module_api_version);
799 return INVALID_OPERATION;
800 }
801
802 String8 clientName8(clientPackageName);
803 int callingPid = getCallingPid();
804
805 LOG1("CameraService::connect legacy E (pid %d \"%s\", id %d)", callingPid,
806 clientName8.string(), cameraId);
807
808 status_t status = validateConnect(cameraId, /*inout*/clientUid);
809 if (status != OK) {
810 return status;
811 }
812
813 sp<Client> client;
814 {
815 Mutex::Autolock lock(mServiceLock);
816 sp<BasicClient> clientTmp;
817 if (!canConnectUnsafe(cameraId, clientPackageName,
818 cameraClient->asBinder(),
819 /*out*/clientTmp)) {
820 return -EBUSY;
821 } else if (client.get() != NULL) {
822 device = static_cast<Client*>(clientTmp.get());
823 return OK;
824 }
825
826 status = connectHelperLocked(cameraClient,
827 cameraId,
828 clientPackageName,
829 clientUid,
830 callingPid,
831 client,
832 halVersion);
833 if (status != OK) {
834 return status;
835 }
836
837 }
838 // important: release the mutex here so the client can call back
839 // into the service from its destructor (can be at the end of the call)
840
841 device = client;
842 return OK;
843}
844
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700845status_t CameraService::connectFinishUnsafe(const sp<BasicClient>& client,
846 const sp<IBinder>& remoteCallback) {
847 status_t status = client->initialize(mModule);
848 if (status != OK) {
849 return status;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800850 }
Ruben Brunkb2119af2014-05-09 19:57:56 -0700851 if (remoteCallback != NULL) {
852 remoteCallback->linkToDeath(this);
853 }
Igor Murashkine6800ce2013-03-04 17:25:57 -0800854
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700855 return OK;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800856}
857
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700858status_t CameraService::connectPro(
Igor Murashkin634a5152013-02-20 17:15:11 -0800859 const sp<IProCameraCallbacks>& cameraCb,
Igor Murashkinc073ba52013-02-26 14:32:34 -0800860 int cameraId,
861 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700862 int clientUid,
863 /*out*/
864 sp<IProCameraUser>& device)
Igor Murashkin634a5152013-02-20 17:15:11 -0800865{
Natalie Silvanoviche1b55da2014-05-01 14:44:52 -0700866 if (cameraCb == 0) {
867 ALOGE("%s: Callback must not be null", __FUNCTION__);
868 return BAD_VALUE;
869 }
870
Igor Murashkinbfc99152013-02-27 12:55:20 -0800871 String8 clientName8(clientPackageName);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700872 int callingPid = getCallingPid();
Igor Murashkin634a5152013-02-20 17:15:11 -0800873
Igor Murashkine6800ce2013-03-04 17:25:57 -0800874 LOG1("CameraService::connectPro E (pid %d \"%s\", id %d)", callingPid,
875 clientName8.string(), cameraId);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700876 status_t status = validateConnect(cameraId, /*inout*/clientUid);
877 if (status != OK) {
878 return status;
Igor Murashkin634a5152013-02-20 17:15:11 -0800879 }
880
Igor Murashkinacd695c2013-03-13 17:23:00 -0700881 sp<ProClient> client;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800882 {
Igor Murashkinacd695c2013-03-13 17:23:00 -0700883 Mutex::Autolock lock(mServiceLock);
884 {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700885 sp<BasicClient> client;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700886 if (!canConnectUnsafe(cameraId, clientPackageName,
887 cameraCb->asBinder(),
888 /*out*/client)) {
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700889 return -EBUSY;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700890 }
891 }
892
893 int facing = -1;
894 int deviceVersion = getDeviceVersion(cameraId, &facing);
895
896 switch(deviceVersion) {
897 case CAMERA_DEVICE_API_VERSION_1_0:
898 ALOGE("Camera id %d uses HALv1, doesn't support ProCamera",
899 cameraId);
Ruben Brunk17963d12013-08-19 15:21:19 -0700900 return -EOPNOTSUPP;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700901 break;
902 case CAMERA_DEVICE_API_VERSION_2_0:
903 case CAMERA_DEVICE_API_VERSION_2_1:
Zhijun He47110052013-07-22 17:34:34 -0700904 case CAMERA_DEVICE_API_VERSION_3_0:
Zhijun He95dd5ba2014-03-26 18:18:00 -0700905 case CAMERA_DEVICE_API_VERSION_3_1:
906 case CAMERA_DEVICE_API_VERSION_3_2:
Eino-Ville Talvala63d877f2014-06-16 19:21:12 -0700907 client = new ProCamera2Client(this, cameraCb, clientPackageName,
908 cameraId, facing, callingPid, clientUid, getpid());
Igor Murashkinacd695c2013-03-13 17:23:00 -0700909 break;
910 case -1:
911 ALOGE("Invalid camera id %d", cameraId);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700912 return BAD_VALUE;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700913 default:
914 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700915 return INVALID_OPERATION;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800916 }
Igor Murashkinacd695c2013-03-13 17:23:00 -0700917
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700918 status_t status = connectFinishUnsafe(client, client->getRemote());
919 if (status != OK) {
920 return status;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700921 }
922
923 mProClientList[cameraId].push(client);
924
925 LOG1("CameraService::connectPro X (id %d, this pid is %d)", cameraId,
926 getpid());
Igor Murashkine6800ce2013-03-04 17:25:57 -0800927 }
Igor Murashkinacd695c2013-03-13 17:23:00 -0700928 // important: release the mutex here so the client can call back
929 // into the service from its destructor (can be at the end of the call)
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700930 device = client;
931 return OK;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800932}
Igor Murashkin634a5152013-02-20 17:15:11 -0800933
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700934status_t CameraService::connectDevice(
Igor Murashkine7ee7632013-06-11 18:10:18 -0700935 const sp<ICameraDeviceCallbacks>& cameraCb,
936 int cameraId,
937 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700938 int clientUid,
939 /*out*/
940 sp<ICameraDeviceUser>& device)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700941{
Igor Murashkine7ee7632013-06-11 18:10:18 -0700942
943 String8 clientName8(clientPackageName);
944 int callingPid = getCallingPid();
945
946 LOG1("CameraService::connectDevice E (pid %d \"%s\", id %d)", callingPid,
947 clientName8.string(), cameraId);
948
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700949 status_t status = validateConnect(cameraId, /*inout*/clientUid);
950 if (status != OK) {
951 return status;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700952 }
953
954 sp<CameraDeviceClient> client;
955 {
956 Mutex::Autolock lock(mServiceLock);
957 {
958 sp<BasicClient> client;
959 if (!canConnectUnsafe(cameraId, clientPackageName,
960 cameraCb->asBinder(),
961 /*out*/client)) {
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700962 return -EBUSY;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700963 }
964 }
965
966 int facing = -1;
967 int deviceVersion = getDeviceVersion(cameraId, &facing);
968
Igor Murashkine7ee7632013-06-11 18:10:18 -0700969 switch(deviceVersion) {
970 case CAMERA_DEVICE_API_VERSION_1_0:
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700971 ALOGW("Camera using old HAL version: %d", deviceVersion);
Ruben Brunk17963d12013-08-19 15:21:19 -0700972 return -EOPNOTSUPP;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700973 // TODO: don't allow 2.0 Only allow 2.1 and higher
974 case CAMERA_DEVICE_API_VERSION_2_0:
975 case CAMERA_DEVICE_API_VERSION_2_1:
976 case CAMERA_DEVICE_API_VERSION_3_0:
Zhijun He95dd5ba2014-03-26 18:18:00 -0700977 case CAMERA_DEVICE_API_VERSION_3_1:
978 case CAMERA_DEVICE_API_VERSION_3_2:
Eino-Ville Talvala63d877f2014-06-16 19:21:12 -0700979 client = new CameraDeviceClient(this, cameraCb, clientPackageName,
980 cameraId, facing, callingPid, clientUid, getpid());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700981 break;
982 case -1:
983 ALOGE("Invalid camera id %d", cameraId);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700984 return BAD_VALUE;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700985 default:
986 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700987 return INVALID_OPERATION;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700988 }
989
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700990 status_t status = connectFinishUnsafe(client, client->getRemote());
991 if (status != OK) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700992 // this is probably not recoverable.. maybe the client can try again
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700993 return status;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700994 }
995
996 LOG1("CameraService::connectDevice X (id %d, this pid is %d)", cameraId,
997 getpid());
998
999 mClient[cameraId] = client;
1000 }
1001 // important: release the mutex here so the client can call back
1002 // into the service from its destructor (can be at the end of the call)
1003
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001004 device = client;
1005 return OK;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001006}
1007
1008
Igor Murashkinbfc99152013-02-27 12:55:20 -08001009status_t CameraService::addListener(
1010 const sp<ICameraServiceListener>& listener) {
1011 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -08001012
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001013 if (listener == 0) {
1014 ALOGE("%s: Listener must not be null", __FUNCTION__);
1015 return BAD_VALUE;
1016 }
1017
Igor Murashkinbfc99152013-02-27 12:55:20 -08001018 Mutex::Autolock lock(mServiceLock);
1019
1020 Vector<sp<ICameraServiceListener> >::iterator it, end;
1021 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
1022 if ((*it)->asBinder() == listener->asBinder()) {
1023 ALOGW("%s: Tried to add listener %p which was already subscribed",
1024 __FUNCTION__, listener.get());
1025 return ALREADY_EXISTS;
1026 }
1027 }
1028
1029 mListenerList.push_back(listener);
1030
Igor Murashkincba2c162013-03-20 15:56:31 -07001031 /* Immediately signal current status to this listener only */
1032 {
1033 Mutex::Autolock m(mStatusMutex) ;
1034 int numCams = getNumberOfCameras();
1035 for (int i = 0; i < numCams; ++i) {
1036 listener->onStatusChanged(mStatusList[i], i);
1037 }
1038 }
1039
Igor Murashkinbfc99152013-02-27 12:55:20 -08001040 return OK;
1041}
1042status_t CameraService::removeListener(
1043 const sp<ICameraServiceListener>& listener) {
1044 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
1045
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001046 if (listener == 0) {
1047 ALOGE("%s: Listener must not be null", __FUNCTION__);
1048 return BAD_VALUE;
1049 }
1050
Igor Murashkinbfc99152013-02-27 12:55:20 -08001051 Mutex::Autolock lock(mServiceLock);
1052
1053 Vector<sp<ICameraServiceListener> >::iterator it;
1054 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
1055 if ((*it)->asBinder() == listener->asBinder()) {
1056 mListenerList.erase(it);
1057 return OK;
1058 }
1059 }
1060
1061 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
1062 __FUNCTION__, listener.get());
1063
1064 return BAD_VALUE;
Igor Murashkin634a5152013-02-20 17:15:11 -08001065}
1066
Igor Murashkin65d14b92014-06-17 12:03:20 -07001067status_t CameraService::getLegacyParameters(
1068 int cameraId,
1069 /*out*/
1070 String16* parameters) {
1071 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1072
1073 if (parameters == NULL) {
1074 ALOGE("%s: parameters must not be null", __FUNCTION__);
1075 return BAD_VALUE;
1076 }
1077
1078 status_t ret = 0;
1079
1080 CameraParameters shimParams;
1081 if ((ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)) != OK) {
1082 // Error logged by caller
1083 return ret;
1084 }
1085
1086 String8 shimParamsString8 = shimParams.flatten();
1087 String16 shimParamsString16 = String16(shimParamsString8);
1088
1089 *parameters = shimParamsString16;
1090
1091 return OK;
1092}
1093
1094status_t CameraService::supportsCameraApi(int cameraId, int apiVersion) {
1095 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1096
1097 switch (apiVersion) {
1098 case API_VERSION_1:
1099 case API_VERSION_2:
1100 break;
1101 default:
1102 ALOGE("%s: Bad API version %d", __FUNCTION__, apiVersion);
1103 return BAD_VALUE;
1104 }
1105
1106 int facing = -1;
1107 int deviceVersion = getDeviceVersion(cameraId, &facing);
1108
1109 switch(deviceVersion) {
1110 case CAMERA_DEVICE_API_VERSION_1_0:
1111 case CAMERA_DEVICE_API_VERSION_2_0:
1112 case CAMERA_DEVICE_API_VERSION_2_1:
1113 case CAMERA_DEVICE_API_VERSION_3_0:
1114 case CAMERA_DEVICE_API_VERSION_3_1:
1115 if (apiVersion == API_VERSION_2) {
1116 ALOGV("%s: Camera id %d uses HAL prior to HAL3.2, doesn't support api2 without shim",
1117 __FUNCTION__, cameraId);
1118 return -EOPNOTSUPP;
1119 } else { // if (apiVersion == API_VERSION_1) {
1120 ALOGV("%s: Camera id %d uses older HAL before 3.2, but api1 is always supported",
1121 __FUNCTION__, cameraId);
1122 return OK;
1123 }
1124 case CAMERA_DEVICE_API_VERSION_3_2:
1125 ALOGV("%s: Camera id %d uses HAL3.2 or newer, supports api1/api2 directly",
1126 __FUNCTION__, cameraId);
1127 return OK;
1128 case -1:
1129 ALOGE("%s: Invalid camera id %d", __FUNCTION__, cameraId);
1130 return BAD_VALUE;
1131 default:
1132 ALOGE("%s: Unknown camera device HAL version: %d", __FUNCTION__, deviceVersion);
1133 return INVALID_OPERATION;
1134 }
1135
1136 return OK;
1137}
1138
Igor Murashkin634a5152013-02-20 17:15:11 -08001139void CameraService::removeClientByRemote(const wp<IBinder>& remoteBinder) {
1140 int callingPid = getCallingPid();
1141 LOG1("CameraService::removeClientByRemote E (pid %d)", callingPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001142
Igor Murashkinecf17e82012-10-02 16:05:11 -07001143 // Declare this before the lock to make absolutely sure the
1144 // destructor won't be called with the lock held.
1145 Mutex::Autolock lock(mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001146
Igor Murashkinecf17e82012-10-02 16:05:11 -07001147 int outIndex;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001148 sp<BasicClient> client = findClientUnsafe(remoteBinder, outIndex);
Igor Murashkinecf17e82012-10-02 16:05:11 -07001149
1150 if (client != 0) {
1151 // Found our camera, clear and leave.
1152 LOG1("removeClient: clear camera %d", outIndex);
Igor Murashkinecf17e82012-10-02 16:05:11 -07001153
Ruben Brunkb2119af2014-05-09 19:57:56 -07001154 sp<IBinder> remote = client->getRemote();
1155 if (remote != NULL) {
1156 remote->unlinkToDeath(this);
1157 }
1158
1159 mClient[outIndex].clear();
Igor Murashkin634a5152013-02-20 17:15:11 -08001160 } else {
1161
1162 sp<ProClient> clientPro = findProClientUnsafe(remoteBinder);
1163
1164 if (clientPro != NULL) {
1165 // Found our camera, clear and leave.
1166 LOG1("removeClient: clear pro %p", clientPro.get());
1167
1168 clientPro->getRemoteCallback()->asBinder()->unlinkToDeath(this);
1169 }
Igor Murashkinecf17e82012-10-02 16:05:11 -07001170 }
1171
Igor Murashkin634a5152013-02-20 17:15:11 -08001172 LOG1("CameraService::removeClientByRemote X (pid %d)", callingPid);
1173}
1174
1175sp<CameraService::ProClient> CameraService::findProClientUnsafe(
1176 const wp<IBinder>& cameraCallbacksRemote)
1177{
1178 sp<ProClient> clientPro;
1179
1180 for (int i = 0; i < mNumberOfCameras; ++i) {
1181 Vector<size_t> removeIdx;
1182
1183 for (size_t j = 0; j < mProClientList[i].size(); ++j) {
1184 wp<ProClient> cl = mProClientList[i][j];
1185
1186 sp<ProClient> clStrong = cl.promote();
1187 if (clStrong != NULL && clStrong->getRemote() == cameraCallbacksRemote) {
1188 clientPro = clStrong;
1189 break;
1190 } else if (clStrong == NULL) {
1191 // mark to clean up dead ptr
1192 removeIdx.push(j);
1193 }
1194 }
1195
1196 // remove stale ptrs (in reverse so the indices dont change)
1197 for (ssize_t j = (ssize_t)removeIdx.size() - 1; j >= 0; --j) {
1198 mProClientList[i].removeAt(removeIdx[j]);
1199 }
1200
1201 }
1202
1203 return clientPro;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001204}
1205
Igor Murashkine7ee7632013-06-11 18:10:18 -07001206sp<CameraService::BasicClient> CameraService::findClientUnsafe(
Igor Murashkin294d0ec2012-10-05 10:44:57 -07001207 const wp<IBinder>& cameraClient, int& outIndex) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001208 sp<BasicClient> client;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001209
1210 for (int i = 0; i < mNumberOfCameras; i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001211
1212 // This happens when we have already disconnected (or this is
1213 // just another unused camera).
1214 if (mClient[i] == 0) continue;
1215
1216 // Promote mClient. It can fail if we are called from this path:
Igor Murashkin634a5152013-02-20 17:15:11 -08001217 // Client::~Client() -> disconnect() -> removeClientByRemote().
Mathias Agopian65ab4712010-07-14 17:59:35 -07001218 client = mClient[i].promote();
1219
Igor Murashkinecf17e82012-10-02 16:05:11 -07001220 // Clean up stale client entry
1221 if (client == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001222 mClient[i].clear();
1223 continue;
1224 }
1225
Igor Murashkine7ee7632013-06-11 18:10:18 -07001226 if (cameraClient == client->getRemote()) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001227 // Found our camera
1228 outIndex = i;
1229 return client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001230 }
1231 }
1232
Igor Murashkinecf17e82012-10-02 16:05:11 -07001233 outIndex = -1;
1234 return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001235}
1236
Igor Murashkine7ee7632013-06-11 18:10:18 -07001237CameraService::BasicClient* CameraService::getClientByIdUnsafe(int cameraId) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001238 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
Keun young Parkd8973a72012-03-28 14:13:09 -07001239 return mClient[cameraId].unsafe_get();
1240}
1241
1242Mutex* CameraService::getClientLockById(int cameraId) {
1243 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
1244 return &mClientLock[cameraId];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001245}
1246
Igor Murashkin634a5152013-02-20 17:15:11 -08001247sp<CameraService::BasicClient> CameraService::getClientByRemote(
Igor Murashkin294d0ec2012-10-05 10:44:57 -07001248 const wp<IBinder>& cameraClient) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001249
1250 // Declare this before the lock to make absolutely sure the
1251 // destructor won't be called with the lock held.
Igor Murashkin634a5152013-02-20 17:15:11 -08001252 sp<BasicClient> client;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001253
1254 Mutex::Autolock lock(mServiceLock);
1255
1256 int outIndex;
1257 client = findClientUnsafe(cameraClient, outIndex);
1258
1259 return client;
1260}
1261
Mathias Agopian65ab4712010-07-14 17:59:35 -07001262status_t CameraService::onTransact(
1263 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
1264 // Permission checks
1265 switch (code) {
1266 case BnCameraService::CONNECT:
Igor Murashkin634a5152013-02-20 17:15:11 -08001267 case BnCameraService::CONNECT_PRO:
Eino-Ville Talvala63d877f2014-06-16 19:21:12 -07001268 case BnCameraService::CONNECT_DEVICE:
Zhijun Heb10cdad2014-06-16 16:38:35 -07001269 case BnCameraService::CONNECT_LEGACY:
Mathias Agopian65ab4712010-07-14 17:59:35 -07001270 const int pid = getCallingPid();
1271 const int self_pid = getpid();
1272 if (pid != self_pid) {
1273 // we're called from a different process, do the real check
1274 if (!checkCallingPermission(
1275 String16("android.permission.CAMERA"))) {
1276 const int uid = getCallingUid();
Steve Block29357bc2012-01-06 19:20:56 +00001277 ALOGE("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -07001278 "can't use the camera pid=%d, uid=%d", pid, uid);
1279 return PERMISSION_DENIED;
1280 }
1281 }
1282 break;
1283 }
1284
1285 return BnCameraService::onTransact(code, data, reply, flags);
1286}
1287
1288// The reason we need this busy bit is a new CameraService::connect() request
1289// may come in while the previous Client's destructor has not been run or is
1290// still running. If the last strong reference of the previous Client is gone
1291// but the destructor has not been finished, we should not allow the new Client
1292// to be created because we need to wait for the previous Client to tear down
1293// the hardware first.
1294void CameraService::setCameraBusy(int cameraId) {
1295 android_atomic_write(1, &mBusy[cameraId]);
Igor Murashkinecf17e82012-10-02 16:05:11 -07001296
1297 ALOGV("setCameraBusy cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001298}
1299
1300void CameraService::setCameraFree(int cameraId) {
1301 android_atomic_write(0, &mBusy[cameraId]);
Igor Murashkinecf17e82012-10-02 16:05:11 -07001302
1303 ALOGV("setCameraFree cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001304}
1305
1306// We share the media players for shutter and recording sound for all clients.
1307// A reference count is kept to determine when we will actually release the
1308// media players.
1309
Chih-Chung Changff4f55c2011-10-17 19:03:12 +08001310MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001311 MediaPlayer* mp = new MediaPlayer();
Andreas Huber1b86fe02014-01-29 11:13:26 -08001312 if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -08001313 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001314 mp->prepare();
1315 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001316 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001317 return NULL;
1318 }
1319 return mp;
1320}
1321
1322void CameraService::loadSound() {
1323 Mutex::Autolock lock(mSoundLock);
1324 LOG1("CameraService::loadSound ref=%d", mSoundRef);
1325 if (mSoundRef++) return;
1326
1327 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
1328 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
1329}
1330
1331void CameraService::releaseSound() {
1332 Mutex::Autolock lock(mSoundLock);
1333 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
1334 if (--mSoundRef) return;
1335
1336 for (int i = 0; i < NUM_SOUNDS; i++) {
1337 if (mSoundPlayer[i] != 0) {
1338 mSoundPlayer[i]->disconnect();
1339 mSoundPlayer[i].clear();
1340 }
1341 }
1342}
1343
1344void CameraService::playSound(sound_kind kind) {
1345 LOG1("playSound(%d)", kind);
1346 Mutex::Autolock lock(mSoundLock);
1347 sp<MediaPlayer> player = mSoundPlayer[kind];
1348 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +08001349 player->seekTo(0);
1350 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001351 }
1352}
1353
1354// ----------------------------------------------------------------------------
1355
1356CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -07001357 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001358 const String16& clientPackageName,
1359 int cameraId, int cameraFacing,
1360 int clientPid, uid_t clientUid,
1361 int servicePid) :
Igor Murashkin634a5152013-02-20 17:15:11 -08001362 CameraService::BasicClient(cameraService, cameraClient->asBinder(),
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001363 clientPackageName,
1364 cameraId, cameraFacing,
1365 clientPid, clientUid,
1366 servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -08001367{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001368 int callingPid = getCallingPid();
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001369 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001370
Igor Murashkin44cfcf02013-03-01 16:22:28 -08001371 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001372
Mathias Agopian65ab4712010-07-14 17:59:35 -07001373 cameraService->setCameraBusy(cameraId);
1374 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001375
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001376 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001377}
1378
Mathias Agopian65ab4712010-07-14 17:59:35 -07001379// tear down the client
1380CameraService::Client::~Client() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07001381 ALOGV("~Client");
Igor Murashkin634a5152013-02-20 17:15:11 -08001382 mDestructionStarted = true;
1383
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -07001384 mCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -07001385 // unconditionally disconnect. function is idempotent
1386 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001387}
1388
Igor Murashkin634a5152013-02-20 17:15:11 -08001389CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001390 const sp<IBinder>& remoteCallback,
1391 const String16& clientPackageName,
1392 int cameraId, int cameraFacing,
1393 int clientPid, uid_t clientUid,
1394 int servicePid):
1395 mClientPackageName(clientPackageName)
Igor Murashkin634a5152013-02-20 17:15:11 -08001396{
1397 mCameraService = cameraService;
Igor Murashkin44cfcf02013-03-01 16:22:28 -08001398 mRemoteBinder = remoteCallback;
Igor Murashkin634a5152013-02-20 17:15:11 -08001399 mCameraId = cameraId;
1400 mCameraFacing = cameraFacing;
1401 mClientPid = clientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001402 mClientUid = clientUid;
Igor Murashkin634a5152013-02-20 17:15:11 -08001403 mServicePid = servicePid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001404 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -08001405 mDestructionStarted = false;
1406}
1407
1408CameraService::BasicClient::~BasicClient() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07001409 ALOGV("~BasicClient");
Igor Murashkin634a5152013-02-20 17:15:11 -08001410 mDestructionStarted = true;
1411}
1412
1413void CameraService::BasicClient::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07001414 ALOGV("BasicClient::disconnect");
Igor Murashkin44cfcf02013-03-01 16:22:28 -08001415 mCameraService->removeClientByRemote(mRemoteBinder);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07001416
1417 finishCameraOps();
Igor Murashkincba2c162013-03-20 15:56:31 -07001418 // client shouldn't be able to call into us anymore
1419 mClientPid = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -08001420}
1421
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001422status_t CameraService::BasicClient::startCameraOps() {
1423 int32_t res;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07001424 // Notify app ops that the camera is not available
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001425 mOpsCallback = new OpsCallback(this);
1426
Igor Murashkine6800ce2013-03-04 17:25:57 -08001427 {
1428 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
1429 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
1430 }
1431
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001432 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
1433 mClientPackageName, mOpsCallback);
1434 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
1435 mClientUid, mClientPackageName);
1436
1437 if (res != AppOpsManager::MODE_ALLOWED) {
1438 ALOGI("Camera %d: Access for \"%s\" has been revoked",
1439 mCameraId, String8(mClientPackageName).string());
1440 return PERMISSION_DENIED;
1441 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07001442
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001443 mOpsActive = true;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07001444
1445 // Transition device availability listeners from PRESENT -> NOT_AVAILABLE
1446 mCameraService->updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE,
1447 mCameraId);
1448
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001449 return OK;
1450}
1451
1452status_t CameraService::BasicClient::finishCameraOps() {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07001453 // Check if startCameraOps succeeded, and if so, finish the camera op
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001454 if (mOpsActive) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07001455 // Notify app ops that the camera is available again
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001456 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
1457 mClientPackageName);
1458 mOpsActive = false;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07001459
1460 // Notify device availability listeners that this camera is available
1461 // again
1462
1463 StatusVector rejectSourceStates;
1464 rejectSourceStates.push_back(ICameraServiceListener::STATUS_NOT_PRESENT);
1465 rejectSourceStates.push_back(ICameraServiceListener::STATUS_ENUMERATING);
1466
1467 // Transition to PRESENT if the camera is not in either of above 2
1468 // states
1469 mCameraService->updateStatus(ICameraServiceListener::STATUS_PRESENT,
1470 mCameraId,
1471 &rejectSourceStates);
1472
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001473 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07001474 // Always stop watching, even if no camera op is active
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001475 mAppOpsManager.stopWatchingMode(mOpsCallback);
1476 mOpsCallback.clear();
1477
1478 return OK;
1479}
1480
1481void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
1482 String8 name(packageName);
1483 String8 myName(mClientPackageName);
1484
1485 if (op != AppOpsManager::OP_CAMERA) {
1486 ALOGW("Unexpected app ops notification received: %d", op);
1487 return;
1488 }
1489
1490 int32_t res;
1491 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
1492 mClientUid, mClientPackageName);
1493 ALOGV("checkOp returns: %d, %s ", res,
1494 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
1495 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
1496 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
1497 "UNKNOWN");
1498
1499 if (res != AppOpsManager::MODE_ALLOWED) {
1500 ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId,
1501 myName.string());
1502 // Reset the client PID to allow server-initiated disconnect,
1503 // and to prevent further calls by client.
1504 mClientPid = getCallingPid();
Jianing Weicb0652e2014-03-12 18:29:36 -07001505 CaptureResultExtras resultExtras; // a dummy result (invalid)
1506 notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001507 disconnect();
1508 }
1509}
1510
Mathias Agopian65ab4712010-07-14 17:59:35 -07001511// ----------------------------------------------------------------------------
1512
Keun young Parkd8973a72012-03-28 14:13:09 -07001513Mutex* CameraService::Client::getClientLockFromCookie(void* user) {
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001514 return gCameraService->getClientLockById((int)(intptr_t) user);
Keun young Parkd8973a72012-03-28 14:13:09 -07001515}
1516
1517// Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should
1518// be acquired for this to be safe
1519CameraService::Client* CameraService::Client::getClientFromCookie(void* user) {
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001520 BasicClient *basicClient = gCameraService->getClientByIdUnsafe((int)(intptr_t) user);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001521 // OK: only CameraClient calls this, and they already cast anyway.
1522 Client* client = static_cast<Client*>(basicClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001523
1524 // This could happen if the Client is in the process of shutting down (the
1525 // last strong reference is gone, but the destructor hasn't finished
1526 // stopping the hardware).
Keun young Parkd8973a72012-03-28 14:13:09 -07001527 if (client == NULL) return NULL;
1528
1529 // destruction already started, so should not be accessed
1530 if (client->mDestructionStarted) return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001531
Mathias Agopian65ab4712010-07-14 17:59:35 -07001532 return client;
1533}
1534
Jianing Weicb0652e2014-03-12 18:29:36 -07001535void CameraService::Client::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
1536 const CaptureResultExtras& resultExtras) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -08001537 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001538}
1539
Igor Murashkin036bc3e2012-10-08 15:09:46 -07001540// NOTE: function is idempotent
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001541void CameraService::Client::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07001542 ALOGV("Client::disconnect");
Igor Murashkin634a5152013-02-20 17:15:11 -08001543 BasicClient::disconnect();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001544 mCameraService->setCameraFree(mCameraId);
Wu-cheng Lie09591e2010-10-14 20:17:44 +08001545}
1546
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001547CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
1548 mClient(client) {
1549}
1550
1551void CameraService::Client::OpsCallback::opChanged(int32_t op,
1552 const String16& packageName) {
1553 sp<BasicClient> client = mClient.promote();
1554 if (client != NULL) {
1555 client->opChanged(op, packageName);
1556 }
1557}
1558
Mathias Agopian65ab4712010-07-14 17:59:35 -07001559// ----------------------------------------------------------------------------
Igor Murashkin634a5152013-02-20 17:15:11 -08001560// IProCamera
1561// ----------------------------------------------------------------------------
1562
1563CameraService::ProClient::ProClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001564 const sp<IProCameraCallbacks>& remoteCallback,
1565 const String16& clientPackageName,
1566 int cameraId,
1567 int cameraFacing,
1568 int clientPid,
1569 uid_t clientUid,
1570 int servicePid)
1571 : CameraService::BasicClient(cameraService, remoteCallback->asBinder(),
1572 clientPackageName, cameraId, cameraFacing,
1573 clientPid, clientUid, servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -08001574{
1575 mRemoteCallback = remoteCallback;
1576}
1577
1578CameraService::ProClient::~ProClient() {
Igor Murashkin634a5152013-02-20 17:15:11 -08001579}
1580
Jianing Weicb0652e2014-03-12 18:29:36 -07001581void CameraService::ProClient::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
1582 const CaptureResultExtras& resultExtras) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001583 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001584}
1585
Igor Murashkin634a5152013-02-20 17:15:11 -08001586// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -07001587
1588static const int kDumpLockRetries = 50;
1589static const int kDumpLockSleep = 60000;
1590
1591static bool tryLock(Mutex& mutex)
1592{
1593 bool locked = false;
1594 for (int i = 0; i < kDumpLockRetries; ++i) {
1595 if (mutex.tryLock() == NO_ERROR) {
1596 locked = true;
1597 break;
1598 }
1599 usleep(kDumpLockSleep);
1600 }
1601 return locked;
1602}
1603
1604status_t CameraService::dump(int fd, const Vector<String16>& args) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001605 String8 result;
1606 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07001607 result.appendFormat("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -07001608 "can't dump CameraService from pid=%d, uid=%d\n",
1609 getCallingPid(),
1610 getCallingUid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001611 write(fd, result.string(), result.size());
1612 } else {
1613 bool locked = tryLock(mServiceLock);
1614 // failed to lock - CameraService is probably deadlocked
1615 if (!locked) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07001616 result.append("CameraService may be deadlocked\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001617 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001618 }
1619
1620 bool hasClient = false;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001621 if (!mModule) {
1622 result = String8::format("No camera module available!\n");
1623 write(fd, result.string(), result.size());
Kalle Lampila6ec3a152013-04-30 15:27:19 +03001624 if (locked) mServiceLock.unlock();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001625 return NO_ERROR;
1626 }
1627
1628 result = String8::format("Camera module HAL API version: 0x%x\n",
1629 mModule->common.hal_api_version);
1630 result.appendFormat("Camera module API version: 0x%x\n",
1631 mModule->common.module_api_version);
1632 result.appendFormat("Camera module name: %s\n",
1633 mModule->common.name);
1634 result.appendFormat("Camera module author: %s\n",
1635 mModule->common.author);
1636 result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras);
Ruben Brunkf81648e2014-04-17 16:14:57 -07001637
1638 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
1639 if (desc == NULL) {
1640 result.appendFormat("Vendor tags left unimplemented.\n");
1641 } else {
1642 result.appendFormat("Vendor tag definitions:\n");
1643 }
1644
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001645 write(fd, result.string(), result.size());
Ruben Brunkf81648e2014-04-17 16:14:57 -07001646
1647 if (desc != NULL) {
1648 desc->dump(fd, /*verbosity*/2, /*indentation*/4);
1649 }
1650
Mathias Agopian65ab4712010-07-14 17:59:35 -07001651 for (int i = 0; i < mNumberOfCameras; i++) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001652 result = String8::format("Camera %d static information:\n", i);
1653 camera_info info;
1654
1655 status_t rc = mModule->get_camera_info(i, &info);
1656 if (rc != OK) {
1657 result.appendFormat(" Error reading static information!\n");
1658 write(fd, result.string(), result.size());
1659 } else {
1660 result.appendFormat(" Facing: %s\n",
1661 info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT");
1662 result.appendFormat(" Orientation: %d\n", info.orientation);
1663 int deviceVersion;
1664 if (mModule->common.module_api_version <
1665 CAMERA_MODULE_API_VERSION_2_0) {
1666 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
1667 } else {
1668 deviceVersion = info.device_version;
1669 }
1670 result.appendFormat(" Device version: 0x%x\n", deviceVersion);
1671 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) {
1672 result.appendFormat(" Device static metadata:\n");
1673 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -07001674 dump_indented_camera_metadata(info.static_camera_characteristics,
Ruben Brunkf81648e2014-04-17 16:14:57 -07001675 fd, /*verbosity*/2, /*indentation*/4);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001676 } else {
1677 write(fd, result.string(), result.size());
1678 }
1679 }
1680
Igor Murashkine7ee7632013-06-11 18:10:18 -07001681 sp<BasicClient> client = mClient[i].promote();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001682 if (client == 0) {
1683 result = String8::format(" Device is closed, no client instance\n");
1684 write(fd, result.string(), result.size());
1685 continue;
1686 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001687 hasClient = true;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001688 result = String8::format(" Device is open. Client instance dump:\n");
1689 write(fd, result.string(), result.size());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001690 client->dump(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001691 }
1692 if (!hasClient) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001693 result = String8::format("\nNo active camera clients yet.\n");
1694 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001695 }
1696
1697 if (locked) mServiceLock.unlock();
1698
Igor Murashkinff3e31d2013-10-23 16:40:06 -07001699 // Dump camera traces if there were any
1700 write(fd, "\n", 1);
1701 camera3::CameraTraces::dump(fd, args);
1702
Mathias Agopian65ab4712010-07-14 17:59:35 -07001703 // change logging level
1704 int n = args.size();
1705 for (int i = 0; i + 1 < n; i++) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07001706 String16 verboseOption("-v");
1707 if (args[i] == verboseOption) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001708 String8 levelStr(args[i+1]);
1709 int level = atoi(levelStr.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001710 result = String8::format("\nSetting log level to %d.\n", level);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001711 setLogLevel(level);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001712 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001713 }
1714 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001715
Mathias Agopian65ab4712010-07-14 17:59:35 -07001716 }
1717 return NO_ERROR;
1718}
1719
Igor Murashkinecf17e82012-10-02 16:05:11 -07001720/*virtual*/void CameraService::binderDied(
1721 const wp<IBinder> &who) {
1722
Igor Murashkin294d0ec2012-10-05 10:44:57 -07001723 /**
1724 * While tempting to promote the wp<IBinder> into a sp,
1725 * it's actually not supported by the binder driver
1726 */
1727
Igor Murashkinecf17e82012-10-02 16:05:11 -07001728 ALOGV("java clients' binder died");
1729
Igor Murashkin634a5152013-02-20 17:15:11 -08001730 sp<BasicClient> cameraClient = getClientByRemote(who);
Igor Murashkinecf17e82012-10-02 16:05:11 -07001731
Igor Murashkin294d0ec2012-10-05 10:44:57 -07001732 if (cameraClient == 0) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001733 ALOGV("java clients' binder death already cleaned up (normal case)");
1734 return;
1735 }
1736
Igor Murashkinecf17e82012-10-02 16:05:11 -07001737 ALOGW("Disconnecting camera client %p since the binder for it "
1738 "died (this pid %d)", cameraClient.get(), getCallingPid());
1739
1740 cameraClient->disconnect();
1741
1742}
1743
Igor Murashkinbfc99152013-02-27 12:55:20 -08001744void CameraService::updateStatus(ICameraServiceListener::Status status,
Igor Murashkin93747b92013-05-01 15:42:20 -07001745 int32_t cameraId,
1746 const StatusVector *rejectSourceStates) {
Igor Murashkinbfc99152013-02-27 12:55:20 -08001747 // do not lock mServiceLock here or can get into a deadlock from
1748 // connect() -> ProClient::disconnect -> updateStatus
1749 Mutex::Autolock lock(mStatusMutex);
Igor Murashkinbfc99152013-02-27 12:55:20 -08001750
1751 ICameraServiceListener::Status oldStatus = mStatusList[cameraId];
1752
1753 mStatusList[cameraId] = status;
1754
1755 if (oldStatus != status) {
1756 ALOGV("%s: Status has changed for camera ID %d from 0x%x to 0x%x",
1757 __FUNCTION__, cameraId, (uint32_t)oldStatus, (uint32_t)status);
1758
Igor Murashkincba2c162013-03-20 15:56:31 -07001759 if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT &&
1760 (status != ICameraServiceListener::STATUS_PRESENT &&
1761 status != ICameraServiceListener::STATUS_ENUMERATING)) {
1762
1763 ALOGW("%s: From NOT_PRESENT can only transition into PRESENT"
1764 " or ENUMERATING", __FUNCTION__);
1765 mStatusList[cameraId] = oldStatus;
1766 return;
1767 }
1768
Igor Murashkin93747b92013-05-01 15:42:20 -07001769 if (rejectSourceStates != NULL) {
1770 const StatusVector &rejectList = *rejectSourceStates;
1771 StatusVector::const_iterator it = rejectList.begin();
1772
1773 /**
1774 * Sometimes we want to conditionally do a transition.
1775 * For example if a client disconnects, we want to go to PRESENT
1776 * only if we weren't already in NOT_PRESENT or ENUMERATING.
1777 */
1778 for (; it != rejectList.end(); ++it) {
1779 if (oldStatus == *it) {
1780 ALOGV("%s: Rejecting status transition for Camera ID %d, "
1781 " since the source state was was in one of the bad "
1782 " states.", __FUNCTION__, cameraId);
1783 mStatusList[cameraId] = oldStatus;
1784 return;
1785 }
1786 }
1787 }
1788
Igor Murashkinbfc99152013-02-27 12:55:20 -08001789 /**
1790 * ProClients lose their exclusive lock.
1791 * - Done before the CameraClient can initialize the HAL device,
1792 * since we want to be able to close it before they get to initialize
1793 */
1794 if (status == ICameraServiceListener::STATUS_NOT_AVAILABLE) {
1795 Vector<wp<ProClient> > proClients(mProClientList[cameraId]);
1796 Vector<wp<ProClient> >::const_iterator it;
1797
1798 for (it = proClients.begin(); it != proClients.end(); ++it) {
1799 sp<ProClient> proCl = it->promote();
1800 if (proCl.get() != NULL) {
1801 proCl->onExclusiveLockStolen();
1802 }
1803 }
1804 }
1805
1806 Vector<sp<ICameraServiceListener> >::const_iterator it;
1807 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
1808 (*it)->onStatusChanged(status, cameraId);
1809 }
1810 }
1811}
1812
Igor Murashkincba2c162013-03-20 15:56:31 -07001813ICameraServiceListener::Status CameraService::getStatus(int cameraId) const {
1814 if (cameraId < 0 || cameraId >= MAX_CAMERAS) {
1815 ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
1816 return ICameraServiceListener::STATUS_UNKNOWN;
1817 }
1818
1819 Mutex::Autolock al(mStatusMutex);
1820 return mStatusList[cameraId];
1821}
1822
Mathias Agopian65ab4712010-07-14 17:59:35 -07001823}; // namespace android