blob: 42c02ca06ff60aecdf93a3dc95e95662b1f11c75 [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#ifndef ANDROID_SERVERS_CAMERA_CAMERASERVICE_H
18#define ANDROID_SERVERS_CAMERA_CAMERASERVICE_H
19
Ruben Brunk36597b22015-03-20 22:15:57 -070020#include <cutils/multiuser.h>
Igor Murashkin634a5152013-02-20 17:15:11 -080021#include <utils/Vector.h>
Ruben Brunkb2119af2014-05-09 19:57:56 -070022#include <utils/KeyedVector.h>
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080023#include <binder/AppOpsManager.h>
Mathias Agopian5462fc92010-07-14 18:41:18 -070024#include <binder/BinderService.h>
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080025#include <binder/IAppOpsCallback.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070026#include <camera/ICameraService.h>
Eino-Ville Talvala412fe562015-08-20 17:08:32 -070027#include <camera/ICameraServiceProxy.h>
Iliyan Malchev8951a972011-04-14 16:55:59 -070028#include <hardware/camera.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070029
Igor Murashkinc073ba52013-02-26 14:32:34 -080030#include <camera/ICamera.h>
31#include <camera/ICameraClient.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070032#include <camera/camera2/ICameraDeviceUser.h>
33#include <camera/camera2/ICameraDeviceCallbacks.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080034#include <camera/VendorTagDescriptor.h>
Jianing Weicb0652e2014-03-12 18:29:36 -070035#include <camera/CaptureResult.h>
Ruben Brunkb2119af2014-05-09 19:57:56 -070036#include <camera/CameraParameters.h>
Igor Murashkinc073ba52013-02-26 14:32:34 -080037
Igor Murashkinbfc99152013-02-27 12:55:20 -080038#include <camera/ICameraServiceListener.h>
Chien-Yu Chen3068d732015-02-09 13:29:57 -080039#include "CameraFlashlight.h"
40
Yin-Chia Yehe074a932015-01-30 10:29:02 -080041#include "common/CameraModule.h"
Ronghua Wu022ed722015-05-11 15:15:09 -070042#include "media/RingBuffer.h"
Ruben Brunkcc776712015-02-17 20:18:47 -080043#include "utils/AutoConditionLock.h"
44#include "utils/ClientManager.h"
Yin-Chia Yehe074a932015-01-30 10:29:02 -080045
Ruben Brunkcc776712015-02-17 20:18:47 -080046#include <set>
47#include <string>
48#include <map>
49#include <memory>
Ruben Brunk99e69712015-05-26 17:25:07 -070050#include <utility>
Mathias Agopian65ab4712010-07-14 17:59:35 -070051
52namespace android {
53
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070054extern volatile int32_t gLogLevel;
55
Mathias Agopian65ab4712010-07-14 17:59:35 -070056class MemoryHeapBase;
57class MediaPlayer;
58
Mathias Agopian5462fc92010-07-14 18:41:18 -070059class CameraService :
60 public BinderService<CameraService>,
Igor Murashkinecf17e82012-10-02 16:05:11 -070061 public BnCameraService,
Igor Murashkincba2c162013-03-20 15:56:31 -070062 public IBinder::DeathRecipient,
63 public camera_module_callbacks_t
Mathias Agopian65ab4712010-07-14 17:59:35 -070064{
Mathias Agopian5462fc92010-07-14 18:41:18 -070065 friend class BinderService<CameraService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070066public:
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070067 class Client;
Igor Murashkin634a5152013-02-20 17:15:11 -080068 class BasicClient;
69
Ruben Brunk0bbf8b22015-04-30 14:35:42 -070070 // The effective API level. The Camera2 API running in LEGACY mode counts as API_1.
Ruben Brunkcc776712015-02-17 20:18:47 -080071 enum apiLevel {
72 API_1 = 1,
73 API_2 = 2
74 };
75
Ruben Brunkbe0b6b42015-05-12 16:10:52 -070076 // Process state (mirrors frameworks/base/core/java/android/app/ActivityManager.java)
Ruben Brunkcc776712015-02-17 20:18:47 -080077 static const int PROCESS_STATE_NONEXISTENT = -1;
Eino-Ville Talvala52aad852015-09-03 12:24:24 -070078 static const int PROCESS_STATE_TOP = 2;
79 static const int PROCESS_STATE_TOP_SLEEPING = 5;
Ruben Brunkcc776712015-02-17 20:18:47 -080080
81 // 3 second busy timeout when other clients are connecting
82 static const nsecs_t DEFAULT_CONNECT_TIMEOUT_NS = 3000000000;
83
Ruben Brunk4f9576b2015-04-10 17:26:56 -070084 // 1 second busy timeout when other clients are disconnecting
85 static const nsecs_t DEFAULT_DISCONNECT_TIMEOUT_NS = 1000000000;
86
Ruben Brunkcc776712015-02-17 20:18:47 -080087 // Default number of messages to store in eviction log
Ruben Brunka8ca9152015-04-07 14:23:40 -070088 static const size_t DEFAULT_EVENT_LOG_LENGTH = 100;
Ruben Brunkcc776712015-02-17 20:18:47 -080089
Igor Murashkin634a5152013-02-20 17:15:11 -080090 // Implementation of BinderService<T>
Mathias Agopian5462fc92010-07-14 18:41:18 -070091 static char const* getServiceName() { return "media.camera"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070092
93 CameraService();
94 virtual ~CameraService();
95
Igor Murashkin634a5152013-02-20 17:15:11 -080096 /////////////////////////////////////////////////////////////////////
Igor Murashkincba2c162013-03-20 15:56:31 -070097 // HAL Callbacks
Bin Chene16d1162016-02-22 18:19:58 +110098 virtual void onDeviceStatusChanged(int cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -080099 camera_device_status_t newStatus);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800100 virtual void onTorchStatusChanged(const String8& cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800101 ICameraServiceListener::TorchStatus
102 newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700103
104 /////////////////////////////////////////////////////////////////////
Igor Murashkin634a5152013-02-20 17:15:11 -0800105 // ICameraService
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700106 virtual int32_t getNumberOfCameras(int type);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700107 virtual int32_t getNumberOfCameras();
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700108
Mathias Agopian65ab4712010-07-14 17:59:35 -0700109 virtual status_t getCameraInfo(int cameraId,
110 struct CameraInfo* cameraInfo);
Zhijun He2b59be82013-09-25 10:14:30 -0700111 virtual status_t getCameraCharacteristics(int cameraId,
112 CameraMetadata* cameraInfo);
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800113 virtual status_t getCameraVendorTagDescriptor(/*out*/ sp<VendorTagDescriptor>& desc);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800114
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700115 virtual status_t connect(const sp<ICameraClient>& cameraClient, int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000116 const String16& clientPackageName, int clientUid,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700117 /*out*/
118 sp<ICamera>& device);
119
Zhijun Heb10cdad2014-06-16 16:38:35 -0700120 virtual status_t connectLegacy(const sp<ICameraClient>& cameraClient, int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000121 int halVersion, const String16& clientPackageName, int clientUid,
Zhijun Heb10cdad2014-06-16 16:38:35 -0700122 /*out*/
123 sp<ICamera>& device);
124
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700125 virtual status_t connectDevice(
Igor Murashkine7ee7632013-06-11 18:10:18 -0700126 const sp<ICameraDeviceCallbacks>& cameraCb,
127 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000128 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700129 int clientUid,
130 /*out*/
131 sp<ICameraDeviceUser>& device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700132
Igor Murashkinbfc99152013-02-27 12:55:20 -0800133 virtual status_t addListener(const sp<ICameraServiceListener>& listener);
134 virtual status_t removeListener(
135 const sp<ICameraServiceListener>& listener);
136
Igor Murashkin65d14b92014-06-17 12:03:20 -0700137 virtual status_t getLegacyParameters(
138 int cameraId,
139 /*out*/
140 String16* parameters);
141
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800142 virtual status_t setTorchMode(const String16& cameraId, bool enabled,
143 const sp<IBinder>& clientBinder);
144
Ruben Brunk6267b532015-04-30 17:44:07 -0700145 virtual void notifySystemEvent(int32_t eventId, const int32_t* args, size_t length);
Ruben Brunk36597b22015-03-20 22:15:57 -0700146
Igor Murashkin65d14b92014-06-17 12:03:20 -0700147 // OK = supports api of that version, -EOPNOTSUPP = does not support
148 virtual status_t supportsCameraApi(
149 int cameraId, int apiVersion);
150
Igor Murashkin634a5152013-02-20 17:15:11 -0800151 // Extra permissions checks
Mathias Agopian65ab4712010-07-14 17:59:35 -0700152 virtual status_t onTransact(uint32_t code, const Parcel& data,
153 Parcel* reply, uint32_t flags);
Igor Murashkin634a5152013-02-20 17:15:11 -0800154
155 virtual status_t dump(int fd, const Vector<String16>& args);
156
157 /////////////////////////////////////////////////////////////////////
158 // Client functionality
Mathias Agopian65ab4712010-07-14 17:59:35 -0700159
160 enum sound_kind {
161 SOUND_SHUTTER = 0,
Chien-Yu Chen82104eb2015-10-14 11:29:31 -0700162 SOUND_RECORDING_START = 1,
163 SOUND_RECORDING_STOP = 2,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700164 NUM_SOUNDS
165 };
166
167 void loadSound();
168 void playSound(sound_kind kind);
169 void releaseSound();
170
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700171 /**
172 * Update the state of a given camera device (open/close/active/idle) with
173 * the camera proxy service in the system service
174 */
175 static void updateProxyDeviceState(
176 ICameraServiceProxy::CameraState newState,
177 const String8& cameraId);
178
Igor Murashkin98e24722013-06-19 19:51:04 -0700179 /////////////////////////////////////////////////////////////////////
180 // CameraDeviceFactory functionality
181 int getDeviceVersion(int cameraId, int* facing = NULL);
182
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700183 /////////////////////////////////////////////////////////////////////
184 // Shared utilities
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700185 static status_t filterGetInfoErrorCode(status_t err);
Igor Murashkin634a5152013-02-20 17:15:11 -0800186
187 /////////////////////////////////////////////////////////////////////
188 // CameraClient functionality
189
Igor Murashkin634a5152013-02-20 17:15:11 -0800190 class BasicClient : public virtual RefBase {
191 public:
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800192 virtual status_t initialize(CameraModule *module) = 0;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700193 virtual void disconnect();
Igor Murashkin634a5152013-02-20 17:15:11 -0800194
Igor Murashkine7ee7632013-06-11 18:10:18 -0700195 // because we can't virtually inherit IInterface, which breaks
196 // virtual inheritance
197 virtual sp<IBinder> asBinderWrapper() = 0;
198
Ruben Brunk9efdf952015-03-18 23:11:57 -0700199 // Return the remote callback binder object (e.g. ICameraDeviceCallbacks)
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700200 sp<IBinder> getRemote() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800201 return mRemoteBinder;
Igor Murashkin634a5152013-02-20 17:15:11 -0800202 }
203
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700204 virtual status_t dump(int fd, const Vector<String16>& args) = 0;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700205
Ruben Brunkcc776712015-02-17 20:18:47 -0800206 // Return the package name for this client
207 virtual String16 getPackageName() const;
208
209 // Notify client about a fatal error
210 virtual void notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
211 const CaptureResultExtras& resultExtras) = 0;
212
Ruben Brunk6267b532015-04-30 17:44:07 -0700213 // Get the UID of the application client using this
214 virtual uid_t getClientUid() const;
215
Ruben Brunkcc776712015-02-17 20:18:47 -0800216 // Get the PID of the application client using this
217 virtual int getClientPid() const;
Ruben Brunk0bbf8b22015-04-30 14:35:42 -0700218
219 // Check what API level is used for this client. This is used to determine which
220 // superclass this can be cast to.
221 virtual bool canCastToApiClient(apiLevel level) const;
Igor Murashkin634a5152013-02-20 17:15:11 -0800222 protected:
223 BasicClient(const sp<CameraService>& cameraService,
224 const sp<IBinder>& remoteCallback,
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000225 const String16& clientPackageName,
Igor Murashkin634a5152013-02-20 17:15:11 -0800226 int cameraId,
227 int cameraFacing,
228 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800229 uid_t clientUid,
Igor Murashkin634a5152013-02-20 17:15:11 -0800230 int servicePid);
231
232 virtual ~BasicClient();
233
234 // the instance is in the middle of destruction. When this is set,
235 // the instance should not be accessed from callback.
236 // CameraService's mClientLock should be acquired to access this.
237 // - subclasses should set this to true in their destructors.
238 bool mDestructionStarted;
239
240 // these are initialized in the constructor.
241 sp<CameraService> mCameraService; // immutable after constructor
242 int mCameraId; // immutable after constructor
243 int mCameraFacing; // immutable after constructor
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000244 const String16 mClientPackageName;
Igor Murashkin634a5152013-02-20 17:15:11 -0800245 pid_t mClientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800246 uid_t mClientUid; // immutable after constructor
Igor Murashkin634a5152013-02-20 17:15:11 -0800247 pid_t mServicePid; // immutable after constructor
Ruben Brunkcc776712015-02-17 20:18:47 -0800248 bool mDisconnected;
Igor Murashkin634a5152013-02-20 17:15:11 -0800249
250 // - The app-side Binder interface to receive callbacks from us
Igor Murashkine7ee7632013-06-11 18:10:18 -0700251 sp<IBinder> mRemoteBinder; // immutable after constructor
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800252
253 // permissions management
254 status_t startCameraOps();
255 status_t finishCameraOps();
256
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800257 private:
258 AppOpsManager mAppOpsManager;
259
260 class OpsCallback : public BnAppOpsCallback {
261 public:
262 OpsCallback(wp<BasicClient> client);
263 virtual void opChanged(int32_t op, const String16& packageName);
264
265 private:
266 wp<BasicClient> mClient;
267
268 }; // class OpsCallback
269
270 sp<OpsCallback> mOpsCallback;
271 // Track whether startCameraOps was called successfully, to avoid
272 // finishing what we didn't start.
273 bool mOpsActive;
274
275 // IAppOpsCallback interface, indirected through opListener
276 virtual void opChanged(int32_t op, const String16& packageName);
277 }; // class BasicClient
Igor Murashkin634a5152013-02-20 17:15:11 -0800278
279 class Client : public BnCamera, public BasicClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700280 {
281 public:
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800282 typedef ICameraClient TCamCallbacks;
283
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700284 // ICamera interface (see ICamera for details)
285 virtual void disconnect();
286 virtual status_t connect(const sp<ICameraClient>& client) = 0;
287 virtual status_t lock() = 0;
288 virtual status_t unlock() = 0;
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700289 virtual status_t setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)=0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700290 virtual void setPreviewCallbackFlag(int flag) = 0;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700291 virtual status_t setPreviewCallbackTarget(
292 const sp<IGraphicBufferProducer>& callbackProducer) = 0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700293 virtual status_t startPreview() = 0;
294 virtual void stopPreview() = 0;
295 virtual bool previewEnabled() = 0;
296 virtual status_t storeMetaDataInBuffers(bool enabled) = 0;
297 virtual status_t startRecording() = 0;
298 virtual void stopRecording() = 0;
299 virtual bool recordingEnabled() = 0;
300 virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0;
301 virtual status_t autoFocus() = 0;
302 virtual status_t cancelAutoFocus() = 0;
303 virtual status_t takePicture(int msgType) = 0;
304 virtual status_t setParameters(const String8& params) = 0;
305 virtual String8 getParameters() const = 0;
306 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0;
307
308 // Interface used by CameraService
309 Client(const sp<CameraService>& cameraService,
310 const sp<ICameraClient>& cameraClient,
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000311 const String16& clientPackageName,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700312 int cameraId,
313 int cameraFacing,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700314 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800315 uid_t clientUid,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700316 int servicePid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700317 ~Client();
318
319 // return our camera client
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800320 const sp<ICameraClient>& getRemoteCallback() {
321 return mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700322 }
323
Igor Murashkine7ee7632013-06-11 18:10:18 -0700324 virtual sp<IBinder> asBinderWrapper() {
Marco Nelissen06b46062014-11-14 07:58:25 -0800325 return asBinder(this);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700326 }
327
Jianing Weicb0652e2014-03-12 18:29:36 -0700328 virtual void notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
329 const CaptureResultExtras& resultExtras);
Ruben Brunk0bbf8b22015-04-30 14:35:42 -0700330
331 // Check what API level is used for this client. This is used to determine which
332 // superclass this can be cast to.
333 virtual bool canCastToApiClient(apiLevel level) const;
Ruben Brunkcc776712015-02-17 20:18:47 -0800334 protected:
335 // Convert client from cookie.
336 static sp<CameraService::Client> getClientFromCookie(void* user);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800337
Igor Murashkin634a5152013-02-20 17:15:11 -0800338 // Initialized in constructor
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700339
Igor Murashkin634a5152013-02-20 17:15:11 -0800340 // - The app-side Binder interface to receive callbacks from us
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800341 sp<ICameraClient> mRemoteCallback;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800342
343 }; // class Client
Igor Murashkin634a5152013-02-20 17:15:11 -0800344
Ruben Brunk99e69712015-05-26 17:25:07 -0700345 /**
346 * A listener class that implements the LISTENER interface for use with a ClientManager, and
347 * implements the following methods:
348 * void onClientRemoved(const ClientDescriptor<KEY, VALUE>& descriptor);
349 * void onClientAdded(const ClientDescriptor<KEY, VALUE>& descriptor);
350 */
351 class ClientEventListener {
352 public:
353 void onClientAdded(const resource_policy::ClientDescriptor<String8,
354 sp<CameraService::BasicClient>>& descriptor);
355 void onClientRemoved(const resource_policy::ClientDescriptor<String8,
356 sp<CameraService::BasicClient>>& descriptor);
357 }; // class ClientEventListener
358
Ruben Brunkcc776712015-02-17 20:18:47 -0800359 typedef std::shared_ptr<resource_policy::ClientDescriptor<String8,
360 sp<CameraService::BasicClient>>> DescriptorPtr;
361
362 /**
363 * A container class for managing active camera clients that are using HAL devices. Active
364 * clients are represented by ClientDescriptor objects that contain strong pointers to the
365 * actual BasicClient subclass binder interface implementation.
366 *
367 * This class manages the eviction behavior for the camera clients. See the parent class
368 * implementation in utils/ClientManager for the specifics of this behavior.
369 */
Ruben Brunk99e69712015-05-26 17:25:07 -0700370 class CameraClientManager : public resource_policy::ClientManager<String8,
371 sp<CameraService::BasicClient>, ClientEventListener> {
Ruben Brunkcc776712015-02-17 20:18:47 -0800372 public:
Ruben Brunk99e69712015-05-26 17:25:07 -0700373 CameraClientManager();
Ruben Brunkcc776712015-02-17 20:18:47 -0800374 virtual ~CameraClientManager();
375
376 /**
377 * Return a strong pointer to the active BasicClient for this camera ID, or an empty
378 * if none exists.
379 */
380 sp<CameraService::BasicClient> getCameraClient(const String8& id) const;
381
382 /**
383 * Return a string describing the current state.
384 */
385 String8 toString() const;
386
387 /**
388 * Make a ClientDescriptor object wrapping the given BasicClient strong pointer.
389 */
390 static DescriptorPtr makeClientDescriptor(const String8& key, const sp<BasicClient>& value,
391 int32_t cost, const std::set<String8>& conflictingKeys, int32_t priority,
392 int32_t ownerId);
393
394 /**
395 * Make a ClientDescriptor object wrapping the given BasicClient strong pointer with
396 * values intialized from a prior ClientDescriptor.
397 */
398 static DescriptorPtr makeClientDescriptor(const sp<BasicClient>& value,
399 const CameraService::DescriptorPtr& partial);
400
401 }; // class CameraClientManager
402
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403private:
Igor Murashkin634a5152013-02-20 17:15:11 -0800404
Ruben Brunkcc776712015-02-17 20:18:47 -0800405 /**
406 * Container class for the state of each logical camera device, including: ID, status, and
407 * dependencies on other devices. The mapping of camera ID -> state saved in mCameraStates
408 * represents the camera devices advertised by the HAL (and any USB devices, when we add
409 * those).
410 *
411 * This container does NOT represent an active camera client. These are represented using
412 * the ClientDescriptors stored in mActiveClientManager.
413 */
414 class CameraState {
415 public:
416 /**
417 * Make a new CameraState and set the ID, cost, and conflicting devices using the values
418 * returned in the HAL's camera_info struct for each device.
419 */
420 CameraState(const String8& id, int cost, const std::set<String8>& conflicting);
421 virtual ~CameraState();
422
423 /**
424 * Return the status for this device.
425 *
426 * This method acquires mStatusLock.
427 */
428 ICameraServiceListener::Status getStatus() const;
429
430 /**
431 * This function updates the status for this camera device, unless the given status
432 * is in the given list of rejected status states, and execute the function passed in
433 * with a signature onStatusUpdateLocked(const String8&, ICameraServiceListener::Status)
434 * if the status has changed.
435 *
436 * This method is idempotent, and will not result in the function passed to
437 * onStatusUpdateLocked being called more than once for the same arguments.
438 * This method aquires mStatusLock.
439 */
440 template<class Func>
441 void updateStatus(ICameraServiceListener::Status status, const String8& cameraId,
442 std::initializer_list<ICameraServiceListener::Status> rejectSourceStates,
443 Func onStatusUpdatedLocked);
444
445 /**
446 * Return the last set CameraParameters object generated from the information returned by
447 * the HAL for this device (or an empty CameraParameters object if none has been set).
448 */
449 CameraParameters getShimParams() const;
450
451 /**
452 * Set the CameraParameters for this device.
453 */
454 void setShimParams(const CameraParameters& params);
455
456 /**
457 * Return the resource_cost advertised by the HAL for this device.
458 */
459 int getCost() const;
460
461 /**
462 * Return a set of the IDs of conflicting devices advertised by the HAL for this device.
463 */
464 std::set<String8> getConflicting() const;
465
466 /**
467 * Return the ID of this camera device.
468 */
469 String8 getId() const;
470
471 private:
472 const String8 mId;
473 ICameraServiceListener::Status mStatus; // protected by mStatusLock
474 const int mCost;
475 std::set<String8> mConflicting;
476 mutable Mutex mStatusLock;
477 CameraParameters mShimParams;
478 }; // class CameraState
479
Igor Murashkin634a5152013-02-20 17:15:11 -0800480 // Delay-load the Camera HAL module
481 virtual void onFirstRef();
482
Ruben Brunkcc776712015-02-17 20:18:47 -0800483 // Check if we can connect, before we acquire the service lock.
Ruben Brunk36597b22015-03-20 22:15:57 -0700484 status_t validateConnectLocked(const String8& cameraId, /*inout*/int& clientUid) const;
Christopher Wiley0039bcf2016-02-05 10:29:50 -0800485 status_t validateClientPermissionsLocked(const String8& cameraId, /*inout*/int& clientUid) const;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800486
Ruben Brunkcc776712015-02-17 20:18:47 -0800487 // Handle active client evictions, and update service state.
488 // Only call with with mServiceLock held.
489 status_t handleEvictionsLocked(const String8& cameraId, int clientPid,
490 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
491 /*out*/
492 sp<BasicClient>* client,
493 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800494
Ruben Brunkcc776712015-02-17 20:18:47 -0800495 // Single implementation shared between the various connect calls
496 template<class CALLBACK, class CLIENT>
497 status_t connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId, int halVersion,
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000498 const String16& clientPackageName, int clientUid, apiLevel effectiveApiLevel,
Ruben Brunkcc776712015-02-17 20:18:47 -0800499 bool legacyMode, bool shimUpdateOnly, /*out*/sp<CLIENT>& device);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800500
Ruben Brunkcc776712015-02-17 20:18:47 -0800501 // Lock guarding camera service state
Mathias Agopian65ab4712010-07-14 17:59:35 -0700502 Mutex mServiceLock;
Ruben Brunkcc776712015-02-17 20:18:47 -0800503
504 // Condition to use with mServiceLock, used to handle simultaneous connect calls from clients
505 std::shared_ptr<WaitableMutexWrapper> mServiceLockWrapper;
506
507 // Return NO_ERROR if the device with a give ID can be connected to
508 status_t checkIfDeviceIsUsable(const String8& cameraId) const;
509
510 // Container for managing currently active application-layer clients
511 CameraClientManager mActiveClientManager;
512
513 // Mapping from camera ID -> state for each device, map is protected by mCameraStatesLock
514 std::map<String8, std::shared_ptr<CameraState>> mCameraStates;
515
516 // Mutex guarding mCameraStates map
517 mutable Mutex mCameraStatesLock;
518
519 // Circular buffer for storing event logging for dumps
520 RingBuffer<String8> mEventLog;
Ruben Brunka8ca9152015-04-07 14:23:40 -0700521 Mutex mLogLock;
Ruben Brunkcc776712015-02-17 20:18:47 -0800522
Ruben Brunk6267b532015-04-30 17:44:07 -0700523 // Currently allowed user IDs
524 std::set<userid_t> mAllowedUsers;
Ruben Brunk36597b22015-03-20 22:15:57 -0700525
Ruben Brunkcc776712015-02-17 20:18:47 -0800526 /**
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700527 * Check camera capabilities, such as support for basic color operation
528 */
529 int checkCameraCapabilities(int id, camera_info info, int *latestStrangeCameraId);
530
531 /**
Ruben Brunkcc776712015-02-17 20:18:47 -0800532 * Get the camera state for a given camera id.
533 *
534 * This acquires mCameraStatesLock.
535 */
536 std::shared_ptr<CameraService::CameraState> getCameraState(const String8& cameraId) const;
537
538 /**
539 * Evict client who's remote binder has died. Returns true if this client was in the active
540 * list and was disconnected.
541 *
542 * This method acquires mServiceLock.
543 */
544 bool evictClientIdByRemote(const wp<IBinder>& cameraClient);
545
546 /**
547 * Remove the given client from the active clients list; does not disconnect the client.
548 *
549 * This method acquires mServiceLock.
550 */
551 void removeByClient(const BasicClient* client);
552
553 /**
554 * Add new client to active clients list after conflicting clients have disconnected using the
555 * values set in the partial descriptor passed in to construct the actual client descriptor.
556 * This is typically called at the end of a connect call.
557 *
558 * This method must be called with mServiceLock held.
559 */
560 void finishConnectLocked(const sp<BasicClient>& client, const DescriptorPtr& desc);
561
562 /**
563 * Returns the integer corresponding to the given camera ID string, or -1 on failure.
564 */
565 static int cameraIdToInt(const String8& cameraId);
566
567 /**
568 * Remove a single client corresponding to the given camera id from the list of active clients.
569 * If none exists, return an empty strongpointer.
570 *
571 * This method must be called with mServiceLock held.
572 */
573 sp<CameraService::BasicClient> removeClientLocked(const String8& cameraId);
574
575 /**
Ruben Brunk36597b22015-03-20 22:15:57 -0700576 * Handle a notification that the current device user has changed.
577 */
Ruben Brunk6267b532015-04-30 17:44:07 -0700578 void doUserSwitch(const int32_t* newUserId, size_t length);
Ruben Brunk36597b22015-03-20 22:15:57 -0700579
580 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -0700581 * Add an event log message.
Ruben Brunkcc776712015-02-17 20:18:47 -0800582 */
Ruben Brunka8ca9152015-04-07 14:23:40 -0700583 void logEvent(const char* event);
Ruben Brunkcc776712015-02-17 20:18:47 -0800584
585 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -0700586 * Add an event log message that a client has been disconnected.
Ruben Brunkcc776712015-02-17 20:18:47 -0800587 */
Ruben Brunka8ca9152015-04-07 14:23:40 -0700588 void logDisconnected(const char* cameraId, int clientPid, const char* clientPackage);
589
590 /**
591 * Add an event log message that a client has been connected.
592 */
593 void logConnected(const char* cameraId, int clientPid, const char* clientPackage);
594
595 /**
596 * Add an event log message that a client's connect attempt has been rejected.
597 */
598 void logRejected(const char* cameraId, int clientPid, const char* clientPackage,
599 const char* reason);
600
601 /**
602 * Add an event log message that the current device user has been switched.
603 */
Ruben Brunk6267b532015-04-30 17:44:07 -0700604 void logUserSwitch(const std::set<userid_t>& oldUserIds,
605 const std::set<userid_t>& newUserIds);
Ruben Brunka8ca9152015-04-07 14:23:40 -0700606
607 /**
608 * Add an event log message that a device has been removed by the HAL
609 */
610 void logDeviceRemoved(const char* cameraId, const char* reason);
611
612 /**
613 * Add an event log message that a device has been added by the HAL
614 */
615 void logDeviceAdded(const char* cameraId, const char* reason);
616
617 /**
618 * Add an event log message that a client has unexpectedly died.
619 */
620 void logClientDied(int clientPid, const char* reason);
Ruben Brunkcc776712015-02-17 20:18:47 -0800621
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700622 /**
623 * Add a event log message that a serious service-level error has occured
624 */
625 void logServiceError(const char* msg, int errorCode);
626
627 /**
628 * Dump the event log to an FD
629 */
630 void dumpEventLog(int fd);
631
Mathias Agopian65ab4712010-07-14 17:59:35 -0700632 int mNumberOfCameras;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700633 int mNumberOfNormalCameras;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635 // sounds
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800636 MediaPlayer* newMediaPlayer(const char *file);
637
Mathias Agopian65ab4712010-07-14 17:59:35 -0700638 Mutex mSoundLock;
639 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
640 int mSoundRef; // reference count (release all MediaPlayer when 0)
641
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800642 CameraModule* mModule;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700643
Ruben Brunkcc776712015-02-17 20:18:47 -0800644 // Guarded by mStatusListenerMutex
645 std::vector<sp<ICameraServiceListener>> mListenerList;
646 Mutex mStatusListenerLock;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800647
Ruben Brunkcc776712015-02-17 20:18:47 -0800648 /**
649 * Update the status for the given camera id (if that device exists), and broadcast the
650 * status update to all current ICameraServiceListeners if the status has changed. Any
651 * statuses in rejectedSourceStates will be ignored.
652 *
653 * This method must be idempotent.
654 * This method acquires mStatusLock and mStatusListenerLock.
655 */
656 void updateStatus(ICameraServiceListener::Status status, const String8& cameraId,
657 std::initializer_list<ICameraServiceListener::Status> rejectedSourceStates);
658 void updateStatus(ICameraServiceListener::Status status, const String8& cameraId);
Igor Murashkinbfc99152013-02-27 12:55:20 -0800659
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800660 // flashlight control
661 sp<CameraFlashlight> mFlashlight;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800662 // guard mTorchStatusMap
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800663 Mutex mTorchStatusMutex;
Chien-Yu Chenfe751be2015-09-01 14:16:44 -0700664 // guard mTorchClientMap
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800665 Mutex mTorchClientMapMutex;
Chien-Yu Chenfe751be2015-09-01 14:16:44 -0700666 // guard mTorchUidMap
667 Mutex mTorchUidMapMutex;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800668 // camera id -> torch status
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800669 KeyedVector<String8, ICameraServiceListener::TorchStatus> mTorchStatusMap;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800670 // camera id -> torch client binder
671 // only store the last client that turns on each camera's torch mode
Ruben Brunk99e69712015-05-26 17:25:07 -0700672 KeyedVector<String8, sp<IBinder>> mTorchClientMap;
673 // camera id -> [incoming uid, current uid] pair
674 std::map<String8, std::pair<int, int>> mTorchUidMap;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800675
676 // check and handle if torch client's process has died
677 void handleTorchClientBinderDied(const wp<IBinder> &who);
678
679 // handle torch mode status change and invoke callbacks. mTorchStatusMutex
680 // should be locked.
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800681 void onTorchStatusChangedLocked(const String8& cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800682 ICameraServiceListener::TorchStatus newStatus);
683
684 // get a camera's torch status. mTorchStatusMutex should be locked.
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800685 status_t getTorchStatusLocked(const String8 &cameraId,
686 ICameraServiceListener::TorchStatus *status) const;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800687
688 // set a camera's torch status. mTorchStatusMutex should be locked.
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800689 status_t setTorchStatusLocked(const String8 &cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800690 ICameraServiceListener::TorchStatus status);
691
Igor Murashkinecf17e82012-10-02 16:05:11 -0700692 // IBinder::DeathRecipient implementation
Igor Murashkinbfc99152013-02-27 12:55:20 -0800693 virtual void binderDied(const wp<IBinder> &who);
Igor Murashkin634a5152013-02-20 17:15:11 -0800694
695 // Helpers
Igor Murashkinbfc99152013-02-27 12:55:20 -0800696
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800697 bool setUpVendorTags();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700698
699 /**
Ruben Brunkb2119af2014-05-09 19:57:56 -0700700 * Initialize and cache the metadata used by the HAL1 shim for a given cameraId.
701 *
702 * Returns OK on success, or a negative error code.
703 */
704 status_t initializeShimMetadata(int cameraId);
705
706 /**
Igor Murashkin65d14b92014-06-17 12:03:20 -0700707 * Get the cached CameraParameters for the camera. If they haven't been
708 * cached yet, then initialize them for the first time.
709 *
710 * Returns OK on success, or a negative error code.
711 */
712 status_t getLegacyParametersLazy(int cameraId, /*out*/CameraParameters* parameters);
713
714 /**
Ruben Brunkb2119af2014-05-09 19:57:56 -0700715 * Generate the CameraCharacteristics metadata required by the Camera2 API
716 * from the available HAL1 CameraParameters and CameraInfo.
717 *
718 * Returns OK on success, or a negative error code.
719 */
720 status_t generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo);
721
Ruben Brunkcc776712015-02-17 20:18:47 -0800722 static int getCallingPid();
723
724 static int getCallingUid();
725
Ruben Brunkb2119af2014-05-09 19:57:56 -0700726 /**
Ruben Brunkcc776712015-02-17 20:18:47 -0800727 * Get the current system time as a formatted string.
Ruben Brunkb2119af2014-05-09 19:57:56 -0700728 */
Ruben Brunkcc776712015-02-17 20:18:47 -0800729 static String8 getFormattedCurrentTime();
730
731 /**
732 * Get the camera eviction priority from the current process state given by ActivityManager.
733 */
734 static int getCameraPriorityFromProcState(int procState);
735
736 static status_t makeClient(const sp<CameraService>& cameraService,
737 const sp<IInterface>& cameraCb, const String16& packageName, const String8& cameraId,
738 int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode,
739 int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
740 /*out*/sp<BasicClient>* client);
Ruben Brunk6267b532015-04-30 17:44:07 -0700741
742 status_t checkCameraAccess(const String16& opPackageName);
743
744 static String8 toString(std::set<userid_t> intSet);
745
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700746 static sp<ICameraServiceProxy> getCameraServiceProxy();
Ruben Brunk2823ce02015-05-19 17:25:13 -0700747 static void pingCameraServiceProxy();
748
Mathias Agopian65ab4712010-07-14 17:59:35 -0700749};
750
Ruben Brunkcc776712015-02-17 20:18:47 -0800751template<class Func>
752void CameraService::CameraState::updateStatus(ICameraServiceListener::Status status,
753 const String8& cameraId,
754 std::initializer_list<ICameraServiceListener::Status> rejectSourceStates,
755 Func onStatusUpdatedLocked) {
756 Mutex::Autolock lock(mStatusLock);
757 ICameraServiceListener::Status oldStatus = mStatus;
758 mStatus = status;
759
760 if (oldStatus == status) {
761 return;
762 }
763
764 ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__,
765 cameraId.string(), oldStatus, status);
766
767 if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT &&
768 (status != ICameraServiceListener::STATUS_PRESENT &&
769 status != ICameraServiceListener::STATUS_ENUMERATING)) {
770
771 ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING",
772 __FUNCTION__);
773 mStatus = oldStatus;
774 return;
775 }
776
777 /**
778 * Sometimes we want to conditionally do a transition.
779 * For example if a client disconnects, we want to go to PRESENT
780 * only if we weren't already in NOT_PRESENT or ENUMERATING.
781 */
782 for (auto& rejectStatus : rejectSourceStates) {
783 if (oldStatus == rejectStatus) {
784 ALOGV("%s: Rejecting status transition for Camera ID %s, since the source "
785 "state was was in one of the bad states.", __FUNCTION__, cameraId.string());
786 mStatus = oldStatus;
787 return;
788 }
789 }
790
791 onStatusUpdatedLocked(cameraId, status);
792}
793
794
795template<class CALLBACK, class CLIENT>
796status_t CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000797 int halVersion, const String16& clientPackageName, int clientUid,
Ruben Brunkcc776712015-02-17 20:18:47 -0800798 apiLevel effectiveApiLevel, bool legacyMode, bool shimUpdateOnly,
799 /*out*/sp<CLIENT>& device) {
800 status_t ret = NO_ERROR;
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000801 String8 clientName8(clientPackageName);
Ruben Brunkcc776712015-02-17 20:18:47 -0800802 int clientPid = getCallingPid();
803
Ruben Brunka8ca9152015-04-07 14:23:40 -0700804 ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) for HAL version %s and "
Ruben Brunkcc776712015-02-17 20:18:47 -0800805 "Camera API version %d", clientPid, clientName8.string(), cameraId.string(),
Ruben Brunka8ca9152015-04-07 14:23:40 -0700806 (halVersion == -1) ? "default" : std::to_string(halVersion).c_str(),
807 static_cast<int>(effectiveApiLevel));
Ruben Brunkcc776712015-02-17 20:18:47 -0800808
Ruben Brunkcc776712015-02-17 20:18:47 -0800809 sp<CLIENT> client = nullptr;
810 {
811 // Acquire mServiceLock and prevent other clients from connecting
812 std::unique_ptr<AutoConditionLock> lock =
813 AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS);
Ruben Brunk36597b22015-03-20 22:15:57 -0700814
Ruben Brunkcc776712015-02-17 20:18:47 -0800815 if (lock == nullptr) {
816 ALOGE("CameraService::connect X (PID %d) rejected (too many other clients connecting)."
817 , clientPid);
818 return -EBUSY;
819 }
820
Ruben Brunk36597b22015-03-20 22:15:57 -0700821 // Enforce client permissions and do basic sanity checks
822 if((ret = validateConnectLocked(cameraId, /*inout*/clientUid)) != NO_ERROR) {
823 return ret;
824 }
Ruben Brunk36597b22015-03-20 22:15:57 -0700825
Ruben Brunkcc776712015-02-17 20:18:47 -0800826 // Check the shim parameters after acquiring lock, if they have already been updated and
827 // we were doing a shim update, return immediately
828 if (shimUpdateOnly) {
829 auto cameraState = getCameraState(cameraId);
830 if (cameraState != nullptr) {
831 if (!cameraState->getShimParams().isEmpty()) return NO_ERROR;
832 }
833 }
834
835 sp<BasicClient> clientTmp = nullptr;
836 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial;
837 if ((ret = handleEvictionsLocked(cameraId, clientPid, effectiveApiLevel,
838 IInterface::asBinder(cameraCb), clientName8, /*out*/&clientTmp,
839 /*out*/&partial)) != NO_ERROR) {
840 return ret;
841 }
842
843 if (clientTmp.get() != nullptr) {
844 // Handle special case for API1 MediaRecorder where the existing client is returned
845 device = static_cast<CLIENT*>(clientTmp.get());
846 return NO_ERROR;
847 }
848
849 // give flashlight a chance to close devices if necessary.
850 mFlashlight->prepareDeviceOpen(cameraId);
851
852 // TODO: Update getDeviceVersion + HAL interface to use strings for Camera IDs
853 int id = cameraIdToInt(cameraId);
854 if (id == -1) {
855 ALOGE("%s: Invalid camera ID %s, cannot get device version from HAL.", __FUNCTION__,
856 cameraId.string());
857 return BAD_VALUE;
858 }
859
860 int facing = -1;
861 int deviceVersion = getDeviceVersion(id, /*out*/&facing);
862 sp<BasicClient> tmp = nullptr;
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000863 if((ret = makeClient(this, cameraCb, clientPackageName, cameraId, facing, clientPid,
Ruben Brunkcc776712015-02-17 20:18:47 -0800864 clientUid, getpid(), legacyMode, halVersion, deviceVersion, effectiveApiLevel,
865 /*out*/&tmp)) != NO_ERROR) {
866 return ret;
867 }
868 client = static_cast<CLIENT*>(tmp.get());
869
870 LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state",
871 __FUNCTION__);
872
873 if ((ret = client->initialize(mModule)) != OK) {
874 ALOGE("%s: Could not initialize client from HAL module.", __FUNCTION__);
875 return ret;
876 }
877
Ruben Brunkcc776712015-02-17 20:18:47 -0800878 // Update shim paremeters for legacy clients
879 if (effectiveApiLevel == API_1) {
880 // Assume we have always received a Client subclass for API1
881 sp<Client> shimClient = reinterpret_cast<Client*>(client.get());
882 String8 rawParams = shimClient->getParameters();
883 CameraParameters params(rawParams);
884
885 auto cameraState = getCameraState(cameraId);
886 if (cameraState != nullptr) {
887 cameraState->setShimParams(params);
888 } else {
889 ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.",
890 __FUNCTION__, cameraId.string());
891 }
892 }
893
894 if (shimUpdateOnly) {
895 // If only updating legacy shim parameters, immediately disconnect client
896 mServiceLock.unlock();
897 client->disconnect();
898 mServiceLock.lock();
899 } else {
900 // Otherwise, add client to active clients list
901 finishConnectLocked(client, partial);
902 }
903 } // lock is destroyed, allow further connect calls
904
905 // Important: release the mutex here so the client can call back into the service from its
906 // destructor (can be at the end of the call)
907 device = client;
908 return NO_ERROR;
909}
910
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911} // namespace android
912
913#endif