blob: 269507e9ae149569807bb144d46c795481747047 [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
Eino-Ville Talvalac4003962016-01-13 10:07:04 -080090 // Event log ID
91 static const int SN_EVENT_LOG_ID = 0x534e4554;
92
Igor Murashkin634a5152013-02-20 17:15:11 -080093 // Implementation of BinderService<T>
Mathias Agopian5462fc92010-07-14 18:41:18 -070094 static char const* getServiceName() { return "media.camera"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070095
96 CameraService();
97 virtual ~CameraService();
98
Igor Murashkin634a5152013-02-20 17:15:11 -080099 /////////////////////////////////////////////////////////////////////
Igor Murashkincba2c162013-03-20 15:56:31 -0700100 // HAL Callbacks
Bin Chene16d1162016-02-22 18:19:58 +1100101 virtual void onDeviceStatusChanged(int cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800102 camera_device_status_t newStatus);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800103 virtual void onTorchStatusChanged(const String8& cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800104 ICameraServiceListener::TorchStatus
105 newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700106
107 /////////////////////////////////////////////////////////////////////
Igor Murashkin634a5152013-02-20 17:15:11 -0800108 // ICameraService
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700109 virtual int32_t getNumberOfCameras(int type);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700110 virtual int32_t getNumberOfCameras();
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700111
Mathias Agopian65ab4712010-07-14 17:59:35 -0700112 virtual status_t getCameraInfo(int cameraId,
113 struct CameraInfo* cameraInfo);
Zhijun He2b59be82013-09-25 10:14:30 -0700114 virtual status_t getCameraCharacteristics(int cameraId,
115 CameraMetadata* cameraInfo);
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800116 virtual status_t getCameraVendorTagDescriptor(/*out*/ sp<VendorTagDescriptor>& desc);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800117
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700118 virtual status_t connect(const sp<ICameraClient>& cameraClient, int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000119 const String16& clientPackageName, int clientUid,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700120 /*out*/
121 sp<ICamera>& device);
122
Zhijun Heb10cdad2014-06-16 16:38:35 -0700123 virtual status_t connectLegacy(const sp<ICameraClient>& cameraClient, int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000124 int halVersion, const String16& clientPackageName, int clientUid,
Zhijun Heb10cdad2014-06-16 16:38:35 -0700125 /*out*/
126 sp<ICamera>& device);
127
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700128 virtual status_t connectDevice(
Igor Murashkine7ee7632013-06-11 18:10:18 -0700129 const sp<ICameraDeviceCallbacks>& cameraCb,
130 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000131 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700132 int clientUid,
133 /*out*/
134 sp<ICameraDeviceUser>& device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700135
Igor Murashkinbfc99152013-02-27 12:55:20 -0800136 virtual status_t addListener(const sp<ICameraServiceListener>& listener);
137 virtual status_t removeListener(
138 const sp<ICameraServiceListener>& listener);
139
Igor Murashkin65d14b92014-06-17 12:03:20 -0700140 virtual status_t getLegacyParameters(
141 int cameraId,
142 /*out*/
143 String16* parameters);
144
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800145 virtual status_t setTorchMode(const String16& cameraId, bool enabled,
146 const sp<IBinder>& clientBinder);
147
Ruben Brunk6267b532015-04-30 17:44:07 -0700148 virtual void notifySystemEvent(int32_t eventId, const int32_t* args, size_t length);
Ruben Brunk36597b22015-03-20 22:15:57 -0700149
Igor Murashkin65d14b92014-06-17 12:03:20 -0700150 // OK = supports api of that version, -EOPNOTSUPP = does not support
151 virtual status_t supportsCameraApi(
152 int cameraId, int apiVersion);
153
Igor Murashkin634a5152013-02-20 17:15:11 -0800154 // Extra permissions checks
Mathias Agopian65ab4712010-07-14 17:59:35 -0700155 virtual status_t onTransact(uint32_t code, const Parcel& data,
156 Parcel* reply, uint32_t flags);
Igor Murashkin634a5152013-02-20 17:15:11 -0800157
158 virtual status_t dump(int fd, const Vector<String16>& args);
159
160 /////////////////////////////////////////////////////////////////////
161 // Client functionality
Mathias Agopian65ab4712010-07-14 17:59:35 -0700162
163 enum sound_kind {
164 SOUND_SHUTTER = 0,
Chien-Yu Chen82104eb2015-10-14 11:29:31 -0700165 SOUND_RECORDING_START = 1,
166 SOUND_RECORDING_STOP = 2,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700167 NUM_SOUNDS
168 };
169
170 void loadSound();
171 void playSound(sound_kind kind);
172 void releaseSound();
173
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700174 /**
175 * Update the state of a given camera device (open/close/active/idle) with
176 * the camera proxy service in the system service
177 */
178 static void updateProxyDeviceState(
179 ICameraServiceProxy::CameraState newState,
180 const String8& cameraId);
181
Igor Murashkin98e24722013-06-19 19:51:04 -0700182 /////////////////////////////////////////////////////////////////////
183 // CameraDeviceFactory functionality
184 int getDeviceVersion(int cameraId, int* facing = NULL);
185
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700186 /////////////////////////////////////////////////////////////////////
187 // Shared utilities
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700188 static status_t filterGetInfoErrorCode(status_t err);
Igor Murashkin634a5152013-02-20 17:15:11 -0800189
190 /////////////////////////////////////////////////////////////////////
191 // CameraClient functionality
192
Igor Murashkin634a5152013-02-20 17:15:11 -0800193 class BasicClient : public virtual RefBase {
194 public:
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800195 virtual status_t initialize(CameraModule *module) = 0;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700196 virtual void disconnect();
Igor Murashkin634a5152013-02-20 17:15:11 -0800197
Igor Murashkine7ee7632013-06-11 18:10:18 -0700198 // because we can't virtually inherit IInterface, which breaks
199 // virtual inheritance
200 virtual sp<IBinder> asBinderWrapper() = 0;
201
Ruben Brunk9efdf952015-03-18 23:11:57 -0700202 // Return the remote callback binder object (e.g. ICameraDeviceCallbacks)
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700203 sp<IBinder> getRemote() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800204 return mRemoteBinder;
Igor Murashkin634a5152013-02-20 17:15:11 -0800205 }
206
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800207 // Disallows dumping over binder interface
208 virtual status_t dump(int fd, const Vector<String16>& args);
209 // Internal dump method to be called by CameraService
210 virtual status_t dumpClient(int fd, const Vector<String16>& args) = 0;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700211
Ruben Brunkcc776712015-02-17 20:18:47 -0800212 // Return the package name for this client
213 virtual String16 getPackageName() const;
214
215 // Notify client about a fatal error
216 virtual void notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
217 const CaptureResultExtras& resultExtras) = 0;
218
Ruben Brunk6267b532015-04-30 17:44:07 -0700219 // Get the UID of the application client using this
220 virtual uid_t getClientUid() const;
221
Ruben Brunkcc776712015-02-17 20:18:47 -0800222 // Get the PID of the application client using this
223 virtual int getClientPid() const;
Ruben Brunk0bbf8b22015-04-30 14:35:42 -0700224
225 // Check what API level is used for this client. This is used to determine which
226 // superclass this can be cast to.
227 virtual bool canCastToApiClient(apiLevel level) const;
Igor Murashkin634a5152013-02-20 17:15:11 -0800228 protected:
229 BasicClient(const sp<CameraService>& cameraService,
230 const sp<IBinder>& remoteCallback,
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000231 const String16& clientPackageName,
Igor Murashkin634a5152013-02-20 17:15:11 -0800232 int cameraId,
233 int cameraFacing,
234 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800235 uid_t clientUid,
Igor Murashkin634a5152013-02-20 17:15:11 -0800236 int servicePid);
237
238 virtual ~BasicClient();
239
240 // the instance is in the middle of destruction. When this is set,
241 // the instance should not be accessed from callback.
242 // CameraService's mClientLock should be acquired to access this.
243 // - subclasses should set this to true in their destructors.
244 bool mDestructionStarted;
245
246 // these are initialized in the constructor.
247 sp<CameraService> mCameraService; // immutable after constructor
248 int mCameraId; // immutable after constructor
249 int mCameraFacing; // immutable after constructor
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000250 const String16 mClientPackageName;
Igor Murashkin634a5152013-02-20 17:15:11 -0800251 pid_t mClientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800252 uid_t mClientUid; // immutable after constructor
Igor Murashkin634a5152013-02-20 17:15:11 -0800253 pid_t mServicePid; // immutable after constructor
Ruben Brunkcc776712015-02-17 20:18:47 -0800254 bool mDisconnected;
Igor Murashkin634a5152013-02-20 17:15:11 -0800255
256 // - The app-side Binder interface to receive callbacks from us
Igor Murashkine7ee7632013-06-11 18:10:18 -0700257 sp<IBinder> mRemoteBinder; // immutable after constructor
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800258
259 // permissions management
260 status_t startCameraOps();
261 status_t finishCameraOps();
262
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800263 private:
264 AppOpsManager mAppOpsManager;
265
266 class OpsCallback : public BnAppOpsCallback {
267 public:
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -0700268 explicit OpsCallback(wp<BasicClient> client);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800269 virtual void opChanged(int32_t op, const String16& packageName);
270
271 private:
272 wp<BasicClient> mClient;
273
274 }; // class OpsCallback
275
276 sp<OpsCallback> mOpsCallback;
277 // Track whether startCameraOps was called successfully, to avoid
278 // finishing what we didn't start.
279 bool mOpsActive;
280
281 // IAppOpsCallback interface, indirected through opListener
282 virtual void opChanged(int32_t op, const String16& packageName);
283 }; // class BasicClient
Igor Murashkin634a5152013-02-20 17:15:11 -0800284
285 class Client : public BnCamera, public BasicClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700286 {
287 public:
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800288 typedef ICameraClient TCamCallbacks;
289
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700290 // ICamera interface (see ICamera for details)
291 virtual void disconnect();
292 virtual status_t connect(const sp<ICameraClient>& client) = 0;
293 virtual status_t lock() = 0;
294 virtual status_t unlock() = 0;
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700295 virtual status_t setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)=0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700296 virtual void setPreviewCallbackFlag(int flag) = 0;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700297 virtual status_t setPreviewCallbackTarget(
298 const sp<IGraphicBufferProducer>& callbackProducer) = 0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700299 virtual status_t startPreview() = 0;
300 virtual void stopPreview() = 0;
301 virtual bool previewEnabled() = 0;
302 virtual status_t storeMetaDataInBuffers(bool enabled) = 0;
303 virtual status_t startRecording() = 0;
304 virtual void stopRecording() = 0;
305 virtual bool recordingEnabled() = 0;
306 virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0;
307 virtual status_t autoFocus() = 0;
308 virtual status_t cancelAutoFocus() = 0;
309 virtual status_t takePicture(int msgType) = 0;
310 virtual status_t setParameters(const String8& params) = 0;
311 virtual String8 getParameters() const = 0;
312 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0;
313
314 // Interface used by CameraService
315 Client(const sp<CameraService>& cameraService,
316 const sp<ICameraClient>& cameraClient,
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000317 const String16& clientPackageName,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700318 int cameraId,
319 int cameraFacing,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700320 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800321 uid_t clientUid,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700322 int servicePid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700323 ~Client();
324
325 // return our camera client
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800326 const sp<ICameraClient>& getRemoteCallback() {
327 return mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700328 }
329
Igor Murashkine7ee7632013-06-11 18:10:18 -0700330 virtual sp<IBinder> asBinderWrapper() {
Marco Nelissen06b46062014-11-14 07:58:25 -0800331 return asBinder(this);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700332 }
333
Jianing Weicb0652e2014-03-12 18:29:36 -0700334 virtual void notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
335 const CaptureResultExtras& resultExtras);
Ruben Brunk0bbf8b22015-04-30 14:35:42 -0700336
337 // Check what API level is used for this client. This is used to determine which
338 // superclass this can be cast to.
339 virtual bool canCastToApiClient(apiLevel level) const;
Ruben Brunkcc776712015-02-17 20:18:47 -0800340 protected:
341 // Convert client from cookie.
342 static sp<CameraService::Client> getClientFromCookie(void* user);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800343
Igor Murashkin634a5152013-02-20 17:15:11 -0800344 // Initialized in constructor
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700345
Igor Murashkin634a5152013-02-20 17:15:11 -0800346 // - The app-side Binder interface to receive callbacks from us
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800347 sp<ICameraClient> mRemoteCallback;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800348
349 }; // class Client
Igor Murashkin634a5152013-02-20 17:15:11 -0800350
Ruben Brunk99e69712015-05-26 17:25:07 -0700351 /**
352 * A listener class that implements the LISTENER interface for use with a ClientManager, and
353 * implements the following methods:
354 * void onClientRemoved(const ClientDescriptor<KEY, VALUE>& descriptor);
355 * void onClientAdded(const ClientDescriptor<KEY, VALUE>& descriptor);
356 */
357 class ClientEventListener {
358 public:
359 void onClientAdded(const resource_policy::ClientDescriptor<String8,
360 sp<CameraService::BasicClient>>& descriptor);
361 void onClientRemoved(const resource_policy::ClientDescriptor<String8,
362 sp<CameraService::BasicClient>>& descriptor);
363 }; // class ClientEventListener
364
Ruben Brunkcc776712015-02-17 20:18:47 -0800365 typedef std::shared_ptr<resource_policy::ClientDescriptor<String8,
366 sp<CameraService::BasicClient>>> DescriptorPtr;
367
368 /**
369 * A container class for managing active camera clients that are using HAL devices. Active
370 * clients are represented by ClientDescriptor objects that contain strong pointers to the
371 * actual BasicClient subclass binder interface implementation.
372 *
373 * This class manages the eviction behavior for the camera clients. See the parent class
374 * implementation in utils/ClientManager for the specifics of this behavior.
375 */
Ruben Brunk99e69712015-05-26 17:25:07 -0700376 class CameraClientManager : public resource_policy::ClientManager<String8,
377 sp<CameraService::BasicClient>, ClientEventListener> {
Ruben Brunkcc776712015-02-17 20:18:47 -0800378 public:
Ruben Brunk99e69712015-05-26 17:25:07 -0700379 CameraClientManager();
Ruben Brunkcc776712015-02-17 20:18:47 -0800380 virtual ~CameraClientManager();
381
382 /**
383 * Return a strong pointer to the active BasicClient for this camera ID, or an empty
384 * if none exists.
385 */
386 sp<CameraService::BasicClient> getCameraClient(const String8& id) const;
387
388 /**
389 * Return a string describing the current state.
390 */
391 String8 toString() const;
392
393 /**
394 * Make a ClientDescriptor object wrapping the given BasicClient strong pointer.
395 */
396 static DescriptorPtr makeClientDescriptor(const String8& key, const sp<BasicClient>& value,
397 int32_t cost, const std::set<String8>& conflictingKeys, int32_t priority,
398 int32_t ownerId);
399
400 /**
401 * Make a ClientDescriptor object wrapping the given BasicClient strong pointer with
402 * values intialized from a prior ClientDescriptor.
403 */
404 static DescriptorPtr makeClientDescriptor(const sp<BasicClient>& value,
405 const CameraService::DescriptorPtr& partial);
406
407 }; // class CameraClientManager
408
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409private:
Igor Murashkin634a5152013-02-20 17:15:11 -0800410
Ruben Brunkcc776712015-02-17 20:18:47 -0800411 /**
412 * Container class for the state of each logical camera device, including: ID, status, and
413 * dependencies on other devices. The mapping of camera ID -> state saved in mCameraStates
414 * represents the camera devices advertised by the HAL (and any USB devices, when we add
415 * those).
416 *
417 * This container does NOT represent an active camera client. These are represented using
418 * the ClientDescriptors stored in mActiveClientManager.
419 */
420 class CameraState {
421 public:
422 /**
423 * Make a new CameraState and set the ID, cost, and conflicting devices using the values
424 * returned in the HAL's camera_info struct for each device.
425 */
426 CameraState(const String8& id, int cost, const std::set<String8>& conflicting);
427 virtual ~CameraState();
428
429 /**
430 * Return the status for this device.
431 *
432 * This method acquires mStatusLock.
433 */
434 ICameraServiceListener::Status getStatus() const;
435
436 /**
437 * This function updates the status for this camera device, unless the given status
438 * is in the given list of rejected status states, and execute the function passed in
439 * with a signature onStatusUpdateLocked(const String8&, ICameraServiceListener::Status)
440 * if the status has changed.
441 *
442 * This method is idempotent, and will not result in the function passed to
443 * onStatusUpdateLocked being called more than once for the same arguments.
444 * This method aquires mStatusLock.
445 */
446 template<class Func>
447 void updateStatus(ICameraServiceListener::Status status, const String8& cameraId,
448 std::initializer_list<ICameraServiceListener::Status> rejectSourceStates,
449 Func onStatusUpdatedLocked);
450
451 /**
452 * Return the last set CameraParameters object generated from the information returned by
453 * the HAL for this device (or an empty CameraParameters object if none has been set).
454 */
455 CameraParameters getShimParams() const;
456
457 /**
458 * Set the CameraParameters for this device.
459 */
460 void setShimParams(const CameraParameters& params);
461
462 /**
463 * Return the resource_cost advertised by the HAL for this device.
464 */
465 int getCost() const;
466
467 /**
468 * Return a set of the IDs of conflicting devices advertised by the HAL for this device.
469 */
470 std::set<String8> getConflicting() const;
471
472 /**
473 * Return the ID of this camera device.
474 */
475 String8 getId() const;
476
477 private:
478 const String8 mId;
479 ICameraServiceListener::Status mStatus; // protected by mStatusLock
480 const int mCost;
481 std::set<String8> mConflicting;
482 mutable Mutex mStatusLock;
483 CameraParameters mShimParams;
484 }; // class CameraState
485
Igor Murashkin634a5152013-02-20 17:15:11 -0800486 // Delay-load the Camera HAL module
487 virtual void onFirstRef();
488
Ruben Brunkcc776712015-02-17 20:18:47 -0800489 // Check if we can connect, before we acquire the service lock.
Ruben Brunk36597b22015-03-20 22:15:57 -0700490 status_t validateConnectLocked(const String8& cameraId, /*inout*/int& clientUid) const;
Christopher Wiley0039bcf2016-02-05 10:29:50 -0800491 status_t validateClientPermissionsLocked(const String8& cameraId, /*inout*/int& clientUid) const;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800492
Ruben Brunkcc776712015-02-17 20:18:47 -0800493 // Handle active client evictions, and update service state.
494 // Only call with with mServiceLock held.
495 status_t handleEvictionsLocked(const String8& cameraId, int clientPid,
496 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
497 /*out*/
498 sp<BasicClient>* client,
499 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800500
Ruben Brunkcc776712015-02-17 20:18:47 -0800501 // Single implementation shared between the various connect calls
502 template<class CALLBACK, class CLIENT>
503 status_t connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId, int halVersion,
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000504 const String16& clientPackageName, int clientUid, apiLevel effectiveApiLevel,
Ruben Brunkcc776712015-02-17 20:18:47 -0800505 bool legacyMode, bool shimUpdateOnly, /*out*/sp<CLIENT>& device);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800506
Ruben Brunkcc776712015-02-17 20:18:47 -0800507 // Lock guarding camera service state
Mathias Agopian65ab4712010-07-14 17:59:35 -0700508 Mutex mServiceLock;
Ruben Brunkcc776712015-02-17 20:18:47 -0800509
510 // Condition to use with mServiceLock, used to handle simultaneous connect calls from clients
511 std::shared_ptr<WaitableMutexWrapper> mServiceLockWrapper;
512
513 // Return NO_ERROR if the device with a give ID can be connected to
514 status_t checkIfDeviceIsUsable(const String8& cameraId) const;
515
516 // Container for managing currently active application-layer clients
517 CameraClientManager mActiveClientManager;
518
519 // Mapping from camera ID -> state for each device, map is protected by mCameraStatesLock
520 std::map<String8, std::shared_ptr<CameraState>> mCameraStates;
521
522 // Mutex guarding mCameraStates map
523 mutable Mutex mCameraStatesLock;
524
525 // Circular buffer for storing event logging for dumps
526 RingBuffer<String8> mEventLog;
Ruben Brunka8ca9152015-04-07 14:23:40 -0700527 Mutex mLogLock;
Ruben Brunkcc776712015-02-17 20:18:47 -0800528
Ruben Brunk6267b532015-04-30 17:44:07 -0700529 // Currently allowed user IDs
530 std::set<userid_t> mAllowedUsers;
Ruben Brunk36597b22015-03-20 22:15:57 -0700531
Ruben Brunkcc776712015-02-17 20:18:47 -0800532 /**
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700533 * Check camera capabilities, such as support for basic color operation
534 */
535 int checkCameraCapabilities(int id, camera_info info, int *latestStrangeCameraId);
536
537 /**
Ruben Brunkcc776712015-02-17 20:18:47 -0800538 * Get the camera state for a given camera id.
539 *
540 * This acquires mCameraStatesLock.
541 */
542 std::shared_ptr<CameraService::CameraState> getCameraState(const String8& cameraId) const;
543
544 /**
545 * Evict client who's remote binder has died. Returns true if this client was in the active
546 * list and was disconnected.
547 *
548 * This method acquires mServiceLock.
549 */
550 bool evictClientIdByRemote(const wp<IBinder>& cameraClient);
551
552 /**
553 * Remove the given client from the active clients list; does not disconnect the client.
554 *
555 * This method acquires mServiceLock.
556 */
557 void removeByClient(const BasicClient* client);
558
559 /**
560 * Add new client to active clients list after conflicting clients have disconnected using the
561 * values set in the partial descriptor passed in to construct the actual client descriptor.
562 * This is typically called at the end of a connect call.
563 *
564 * This method must be called with mServiceLock held.
565 */
566 void finishConnectLocked(const sp<BasicClient>& client, const DescriptorPtr& desc);
567
568 /**
569 * Returns the integer corresponding to the given camera ID string, or -1 on failure.
570 */
571 static int cameraIdToInt(const String8& cameraId);
572
573 /**
574 * Remove a single client corresponding to the given camera id from the list of active clients.
575 * If none exists, return an empty strongpointer.
576 *
577 * This method must be called with mServiceLock held.
578 */
579 sp<CameraService::BasicClient> removeClientLocked(const String8& cameraId);
580
581 /**
Ruben Brunk36597b22015-03-20 22:15:57 -0700582 * Handle a notification that the current device user has changed.
583 */
Ruben Brunk6267b532015-04-30 17:44:07 -0700584 void doUserSwitch(const int32_t* newUserId, size_t length);
Ruben Brunk36597b22015-03-20 22:15:57 -0700585
586 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -0700587 * Add an event log message.
Ruben Brunkcc776712015-02-17 20:18:47 -0800588 */
Ruben Brunka8ca9152015-04-07 14:23:40 -0700589 void logEvent(const char* event);
Ruben Brunkcc776712015-02-17 20:18:47 -0800590
591 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -0700592 * Add an event log message that a client has been disconnected.
Ruben Brunkcc776712015-02-17 20:18:47 -0800593 */
Ruben Brunka8ca9152015-04-07 14:23:40 -0700594 void logDisconnected(const char* cameraId, int clientPid, const char* clientPackage);
595
596 /**
597 * Add an event log message that a client has been connected.
598 */
599 void logConnected(const char* cameraId, int clientPid, const char* clientPackage);
600
601 /**
602 * Add an event log message that a client's connect attempt has been rejected.
603 */
604 void logRejected(const char* cameraId, int clientPid, const char* clientPackage,
605 const char* reason);
606
607 /**
608 * Add an event log message that the current device user has been switched.
609 */
Ruben Brunk6267b532015-04-30 17:44:07 -0700610 void logUserSwitch(const std::set<userid_t>& oldUserIds,
611 const std::set<userid_t>& newUserIds);
Ruben Brunka8ca9152015-04-07 14:23:40 -0700612
613 /**
614 * Add an event log message that a device has been removed by the HAL
615 */
616 void logDeviceRemoved(const char* cameraId, const char* reason);
617
618 /**
619 * Add an event log message that a device has been added by the HAL
620 */
621 void logDeviceAdded(const char* cameraId, const char* reason);
622
623 /**
624 * Add an event log message that a client has unexpectedly died.
625 */
626 void logClientDied(int clientPid, const char* reason);
Ruben Brunkcc776712015-02-17 20:18:47 -0800627
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700628 /**
629 * Add a event log message that a serious service-level error has occured
630 */
631 void logServiceError(const char* msg, int errorCode);
632
633 /**
634 * Dump the event log to an FD
635 */
636 void dumpEventLog(int fd);
637
Mathias Agopian65ab4712010-07-14 17:59:35 -0700638 int mNumberOfCameras;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700639 int mNumberOfNormalCameras;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700640
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641 // sounds
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800642 MediaPlayer* newMediaPlayer(const char *file);
643
Mathias Agopian65ab4712010-07-14 17:59:35 -0700644 Mutex mSoundLock;
645 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
646 int mSoundRef; // reference count (release all MediaPlayer when 0)
647
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800648 CameraModule* mModule;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700649
Ruben Brunkcc776712015-02-17 20:18:47 -0800650 // Guarded by mStatusListenerMutex
651 std::vector<sp<ICameraServiceListener>> mListenerList;
652 Mutex mStatusListenerLock;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800653
Ruben Brunkcc776712015-02-17 20:18:47 -0800654 /**
655 * Update the status for the given camera id (if that device exists), and broadcast the
656 * status update to all current ICameraServiceListeners if the status has changed. Any
657 * statuses in rejectedSourceStates will be ignored.
658 *
659 * This method must be idempotent.
660 * This method acquires mStatusLock and mStatusListenerLock.
661 */
662 void updateStatus(ICameraServiceListener::Status status, const String8& cameraId,
663 std::initializer_list<ICameraServiceListener::Status> rejectedSourceStates);
664 void updateStatus(ICameraServiceListener::Status status, const String8& cameraId);
Igor Murashkinbfc99152013-02-27 12:55:20 -0800665
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800666 // flashlight control
667 sp<CameraFlashlight> mFlashlight;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800668 // guard mTorchStatusMap
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800669 Mutex mTorchStatusMutex;
Chien-Yu Chenfe751be2015-09-01 14:16:44 -0700670 // guard mTorchClientMap
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800671 Mutex mTorchClientMapMutex;
Chien-Yu Chenfe751be2015-09-01 14:16:44 -0700672 // guard mTorchUidMap
673 Mutex mTorchUidMapMutex;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800674 // camera id -> torch status
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800675 KeyedVector<String8, ICameraServiceListener::TorchStatus> mTorchStatusMap;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800676 // camera id -> torch client binder
677 // only store the last client that turns on each camera's torch mode
Ruben Brunk99e69712015-05-26 17:25:07 -0700678 KeyedVector<String8, sp<IBinder>> mTorchClientMap;
679 // camera id -> [incoming uid, current uid] pair
680 std::map<String8, std::pair<int, int>> mTorchUidMap;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800681
682 // check and handle if torch client's process has died
683 void handleTorchClientBinderDied(const wp<IBinder> &who);
684
685 // handle torch mode status change and invoke callbacks. mTorchStatusMutex
686 // should be locked.
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800687 void onTorchStatusChangedLocked(const String8& cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800688 ICameraServiceListener::TorchStatus newStatus);
689
690 // get a camera's torch status. mTorchStatusMutex should be locked.
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800691 status_t getTorchStatusLocked(const String8 &cameraId,
692 ICameraServiceListener::TorchStatus *status) const;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800693
694 // set a camera's torch status. mTorchStatusMutex should be locked.
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800695 status_t setTorchStatusLocked(const String8 &cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800696 ICameraServiceListener::TorchStatus status);
697
Igor Murashkinecf17e82012-10-02 16:05:11 -0700698 // IBinder::DeathRecipient implementation
Igor Murashkinbfc99152013-02-27 12:55:20 -0800699 virtual void binderDied(const wp<IBinder> &who);
Igor Murashkin634a5152013-02-20 17:15:11 -0800700
701 // Helpers
Igor Murashkinbfc99152013-02-27 12:55:20 -0800702
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800703 bool setUpVendorTags();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700704
705 /**
Ruben Brunkb2119af2014-05-09 19:57:56 -0700706 * Initialize and cache the metadata used by the HAL1 shim for a given cameraId.
707 *
708 * Returns OK on success, or a negative error code.
709 */
710 status_t initializeShimMetadata(int cameraId);
711
712 /**
Igor Murashkin65d14b92014-06-17 12:03:20 -0700713 * Get the cached CameraParameters for the camera. If they haven't been
714 * cached yet, then initialize them for the first time.
715 *
716 * Returns OK on success, or a negative error code.
717 */
718 status_t getLegacyParametersLazy(int cameraId, /*out*/CameraParameters* parameters);
719
720 /**
Ruben Brunkb2119af2014-05-09 19:57:56 -0700721 * Generate the CameraCharacteristics metadata required by the Camera2 API
722 * from the available HAL1 CameraParameters and CameraInfo.
723 *
724 * Returns OK on success, or a negative error code.
725 */
726 status_t generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo);
727
Ruben Brunkcc776712015-02-17 20:18:47 -0800728 static int getCallingPid();
729
730 static int getCallingUid();
731
Ruben Brunkb2119af2014-05-09 19:57:56 -0700732 /**
Ruben Brunkcc776712015-02-17 20:18:47 -0800733 * Get the current system time as a formatted string.
Ruben Brunkb2119af2014-05-09 19:57:56 -0700734 */
Ruben Brunkcc776712015-02-17 20:18:47 -0800735 static String8 getFormattedCurrentTime();
736
737 /**
738 * Get the camera eviction priority from the current process state given by ActivityManager.
739 */
740 static int getCameraPriorityFromProcState(int procState);
741
742 static status_t makeClient(const sp<CameraService>& cameraService,
743 const sp<IInterface>& cameraCb, const String16& packageName, const String8& cameraId,
744 int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode,
745 int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
746 /*out*/sp<BasicClient>* client);
Ruben Brunk6267b532015-04-30 17:44:07 -0700747
748 status_t checkCameraAccess(const String16& opPackageName);
749
750 static String8 toString(std::set<userid_t> intSet);
751
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700752 static sp<ICameraServiceProxy> getCameraServiceProxy();
Ruben Brunk2823ce02015-05-19 17:25:13 -0700753 static void pingCameraServiceProxy();
754
Mathias Agopian65ab4712010-07-14 17:59:35 -0700755};
756
Ruben Brunkcc776712015-02-17 20:18:47 -0800757template<class Func>
758void CameraService::CameraState::updateStatus(ICameraServiceListener::Status status,
759 const String8& cameraId,
760 std::initializer_list<ICameraServiceListener::Status> rejectSourceStates,
761 Func onStatusUpdatedLocked) {
762 Mutex::Autolock lock(mStatusLock);
763 ICameraServiceListener::Status oldStatus = mStatus;
764 mStatus = status;
765
766 if (oldStatus == status) {
767 return;
768 }
769
770 ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__,
771 cameraId.string(), oldStatus, status);
772
773 if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT &&
774 (status != ICameraServiceListener::STATUS_PRESENT &&
775 status != ICameraServiceListener::STATUS_ENUMERATING)) {
776
777 ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING",
778 __FUNCTION__);
779 mStatus = oldStatus;
780 return;
781 }
782
783 /**
784 * Sometimes we want to conditionally do a transition.
785 * For example if a client disconnects, we want to go to PRESENT
786 * only if we weren't already in NOT_PRESENT or ENUMERATING.
787 */
788 for (auto& rejectStatus : rejectSourceStates) {
789 if (oldStatus == rejectStatus) {
790 ALOGV("%s: Rejecting status transition for Camera ID %s, since the source "
791 "state was was in one of the bad states.", __FUNCTION__, cameraId.string());
792 mStatus = oldStatus;
793 return;
794 }
795 }
796
797 onStatusUpdatedLocked(cameraId, status);
798}
799
800
801template<class CALLBACK, class CLIENT>
802status_t CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000803 int halVersion, const String16& clientPackageName, int clientUid,
Ruben Brunkcc776712015-02-17 20:18:47 -0800804 apiLevel effectiveApiLevel, bool legacyMode, bool shimUpdateOnly,
805 /*out*/sp<CLIENT>& device) {
806 status_t ret = NO_ERROR;
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000807 String8 clientName8(clientPackageName);
Ruben Brunkcc776712015-02-17 20:18:47 -0800808 int clientPid = getCallingPid();
809
Ruben Brunka8ca9152015-04-07 14:23:40 -0700810 ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) for HAL version %s and "
Ruben Brunkcc776712015-02-17 20:18:47 -0800811 "Camera API version %d", clientPid, clientName8.string(), cameraId.string(),
Ruben Brunka8ca9152015-04-07 14:23:40 -0700812 (halVersion == -1) ? "default" : std::to_string(halVersion).c_str(),
813 static_cast<int>(effectiveApiLevel));
Ruben Brunkcc776712015-02-17 20:18:47 -0800814
Ruben Brunkcc776712015-02-17 20:18:47 -0800815 sp<CLIENT> client = nullptr;
816 {
817 // Acquire mServiceLock and prevent other clients from connecting
818 std::unique_ptr<AutoConditionLock> lock =
819 AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS);
Ruben Brunk36597b22015-03-20 22:15:57 -0700820
Ruben Brunkcc776712015-02-17 20:18:47 -0800821 if (lock == nullptr) {
822 ALOGE("CameraService::connect X (PID %d) rejected (too many other clients connecting)."
823 , clientPid);
824 return -EBUSY;
825 }
826
Ruben Brunk36597b22015-03-20 22:15:57 -0700827 // Enforce client permissions and do basic sanity checks
828 if((ret = validateConnectLocked(cameraId, /*inout*/clientUid)) != NO_ERROR) {
829 return ret;
830 }
Ruben Brunk36597b22015-03-20 22:15:57 -0700831
Ruben Brunkcc776712015-02-17 20:18:47 -0800832 // Check the shim parameters after acquiring lock, if they have already been updated and
833 // we were doing a shim update, return immediately
834 if (shimUpdateOnly) {
835 auto cameraState = getCameraState(cameraId);
836 if (cameraState != nullptr) {
837 if (!cameraState->getShimParams().isEmpty()) return NO_ERROR;
838 }
839 }
840
841 sp<BasicClient> clientTmp = nullptr;
842 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial;
843 if ((ret = handleEvictionsLocked(cameraId, clientPid, effectiveApiLevel,
844 IInterface::asBinder(cameraCb), clientName8, /*out*/&clientTmp,
845 /*out*/&partial)) != NO_ERROR) {
846 return ret;
847 }
848
849 if (clientTmp.get() != nullptr) {
850 // Handle special case for API1 MediaRecorder where the existing client is returned
851 device = static_cast<CLIENT*>(clientTmp.get());
852 return NO_ERROR;
853 }
854
855 // give flashlight a chance to close devices if necessary.
856 mFlashlight->prepareDeviceOpen(cameraId);
857
858 // TODO: Update getDeviceVersion + HAL interface to use strings for Camera IDs
859 int id = cameraIdToInt(cameraId);
860 if (id == -1) {
861 ALOGE("%s: Invalid camera ID %s, cannot get device version from HAL.", __FUNCTION__,
862 cameraId.string());
863 return BAD_VALUE;
864 }
865
866 int facing = -1;
867 int deviceVersion = getDeviceVersion(id, /*out*/&facing);
868 sp<BasicClient> tmp = nullptr;
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000869 if((ret = makeClient(this, cameraCb, clientPackageName, cameraId, facing, clientPid,
Ruben Brunkcc776712015-02-17 20:18:47 -0800870 clientUid, getpid(), legacyMode, halVersion, deviceVersion, effectiveApiLevel,
871 /*out*/&tmp)) != NO_ERROR) {
872 return ret;
873 }
874 client = static_cast<CLIENT*>(tmp.get());
875
876 LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state",
877 __FUNCTION__);
878
879 if ((ret = client->initialize(mModule)) != OK) {
880 ALOGE("%s: Could not initialize client from HAL module.", __FUNCTION__);
881 return ret;
882 }
883
Ruben Brunkcc776712015-02-17 20:18:47 -0800884 // Update shim paremeters for legacy clients
885 if (effectiveApiLevel == API_1) {
886 // Assume we have always received a Client subclass for API1
887 sp<Client> shimClient = reinterpret_cast<Client*>(client.get());
888 String8 rawParams = shimClient->getParameters();
889 CameraParameters params(rawParams);
890
891 auto cameraState = getCameraState(cameraId);
892 if (cameraState != nullptr) {
893 cameraState->setShimParams(params);
894 } else {
895 ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.",
896 __FUNCTION__, cameraId.string());
897 }
898 }
899
900 if (shimUpdateOnly) {
901 // If only updating legacy shim parameters, immediately disconnect client
902 mServiceLock.unlock();
903 client->disconnect();
904 mServiceLock.lock();
905 } else {
906 // Otherwise, add client to active clients list
907 finishConnectLocked(client, partial);
908 }
909 } // lock is destroyed, allow further connect calls
910
911 // Important: release the mutex here so the client can call back into the service from its
912 // destructor (can be at the end of the call)
913 device = client;
914 return NO_ERROR;
915}
916
Mathias Agopian65ab4712010-07-14 17:59:35 -0700917} // namespace android
918
919#endif