blob: b35f35c97d2d00f783b2dd300c00380aaf5c0c43 [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
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080020#include <android/hardware/BnCameraService.h>
21#include <android/hardware/ICameraServiceListener.h>
22
Ruben Brunk36597b22015-03-20 22:15:57 -070023#include <cutils/multiuser.h>
Igor Murashkin634a5152013-02-20 17:15:11 -080024#include <utils/Vector.h>
Ruben Brunkb2119af2014-05-09 19:57:56 -070025#include <utils/KeyedVector.h>
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080026#include <binder/AppOpsManager.h>
Mathias Agopian5462fc92010-07-14 18:41:18 -070027#include <binder/BinderService.h>
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080028#include <binder/IAppOpsCallback.h>
Eino-Ville Talvala412fe562015-08-20 17:08:32 -070029#include <camera/ICameraServiceProxy.h>
Iliyan Malchev8951a972011-04-14 16:55:59 -070030#include <hardware/camera.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070031
Ruben Brunkd1176ef2014-02-21 10:51:38 -080032#include <camera/VendorTagDescriptor.h>
Jianing Weicb0652e2014-03-12 18:29:36 -070033#include <camera/CaptureResult.h>
Ruben Brunkb2119af2014-05-09 19:57:56 -070034#include <camera/CameraParameters.h>
Igor Murashkinc073ba52013-02-26 14:32:34 -080035
Chien-Yu Chen3068d732015-02-09 13:29:57 -080036#include "CameraFlashlight.h"
37
Yin-Chia Yehe074a932015-01-30 10:29:02 -080038#include "common/CameraModule.h"
Ronghua Wu022ed722015-05-11 15:15:09 -070039#include "media/RingBuffer.h"
Ruben Brunkcc776712015-02-17 20:18:47 -080040#include "utils/AutoConditionLock.h"
41#include "utils/ClientManager.h"
Yin-Chia Yehe074a932015-01-30 10:29:02 -080042
Ruben Brunkcc776712015-02-17 20:18:47 -080043#include <set>
44#include <string>
45#include <map>
46#include <memory>
Ruben Brunk99e69712015-05-26 17:25:07 -070047#include <utility>
Mathias Agopian65ab4712010-07-14 17:59:35 -070048
49namespace android {
50
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070051extern volatile int32_t gLogLevel;
52
Mathias Agopian65ab4712010-07-14 17:59:35 -070053class MemoryHeapBase;
54class MediaPlayer;
55
Mathias Agopian5462fc92010-07-14 18:41:18 -070056class CameraService :
57 public BinderService<CameraService>,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080058 public ::android::hardware::BnCameraService,
Igor Murashkincba2c162013-03-20 15:56:31 -070059 public IBinder::DeathRecipient,
60 public camera_module_callbacks_t
Mathias Agopian65ab4712010-07-14 17:59:35 -070061{
Mathias Agopian5462fc92010-07-14 18:41:18 -070062 friend class BinderService<CameraService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070063public:
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070064 class Client;
Igor Murashkin634a5152013-02-20 17:15:11 -080065 class BasicClient;
66
Ruben Brunk0bbf8b22015-04-30 14:35:42 -070067 // The effective API level. The Camera2 API running in LEGACY mode counts as API_1.
Ruben Brunkcc776712015-02-17 20:18:47 -080068 enum apiLevel {
69 API_1 = 1,
70 API_2 = 2
71 };
72
Ruben Brunkbe0b6b42015-05-12 16:10:52 -070073 // Process state (mirrors frameworks/base/core/java/android/app/ActivityManager.java)
Ruben Brunkcc776712015-02-17 20:18:47 -080074 static const int PROCESS_STATE_NONEXISTENT = -1;
Eino-Ville Talvala52aad852015-09-03 12:24:24 -070075 static const int PROCESS_STATE_TOP = 2;
76 static const int PROCESS_STATE_TOP_SLEEPING = 5;
Ruben Brunkcc776712015-02-17 20:18:47 -080077
78 // 3 second busy timeout when other clients are connecting
79 static const nsecs_t DEFAULT_CONNECT_TIMEOUT_NS = 3000000000;
80
Ruben Brunk4f9576b2015-04-10 17:26:56 -070081 // 1 second busy timeout when other clients are disconnecting
82 static const nsecs_t DEFAULT_DISCONNECT_TIMEOUT_NS = 1000000000;
83
Ruben Brunkcc776712015-02-17 20:18:47 -080084 // Default number of messages to store in eviction log
Ruben Brunka8ca9152015-04-07 14:23:40 -070085 static const size_t DEFAULT_EVENT_LOG_LENGTH = 100;
Ruben Brunkcc776712015-02-17 20:18:47 -080086
Eino-Ville Talvalac4003962016-01-13 10:07:04 -080087 // Event log ID
88 static const int SN_EVENT_LOG_ID = 0x534e4554;
89
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,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800101 int32_t newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700102
103 /////////////////////////////////////////////////////////////////////
Igor Murashkin634a5152013-02-20 17:15:11 -0800104 // ICameraService
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800105 virtual binder::Status getNumberOfCameras(int32_t type, int32_t* numCameras);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700106
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800107 virtual binder::Status getCameraInfo(int cameraId,
108 hardware::CameraInfo* cameraInfo);
109 virtual binder::Status getCameraCharacteristics(int cameraId,
110 CameraMetadata* cameraInfo);
111 virtual binder::Status getCameraVendorTagDescriptor(
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700112 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800113 hardware::camera2::params::VendorTagDescriptor* desc);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700114
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800115 virtual binder::Status connect(const sp<hardware::ICameraClient>& cameraClient,
116 int32_t cameraId, const String16& clientPackageName,
117 int32_t clientUid, int clientPid,
Zhijun Heb10cdad2014-06-16 16:38:35 -0700118 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800119 sp<hardware::ICamera>* device);
Zhijun Heb10cdad2014-06-16 16:38:35 -0700120
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800121 virtual binder::Status connectLegacy(const sp<hardware::ICameraClient>& cameraClient,
122 int32_t cameraId, int32_t halVersion,
123 const String16& clientPackageName, int32_t clientUid,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700124 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800125 sp<hardware::ICamera>* device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700126
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800127 virtual binder::Status connectDevice(
128 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb, int32_t cameraId,
129 const String16& clientPackageName, int32_t clientUid,
130 /*out*/
131 sp<hardware::camera2::ICameraDeviceUser>* device);
Igor Murashkinbfc99152013-02-27 12:55:20 -0800132
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800133 virtual binder::Status addListener(const sp<hardware::ICameraServiceListener>& listener);
134 virtual binder::Status removeListener(
135 const sp<hardware::ICameraServiceListener>& listener);
136
137 virtual binder::Status getLegacyParameters(
138 int32_t cameraId,
Igor Murashkin65d14b92014-06-17 12:03:20 -0700139 /*out*/
140 String16* parameters);
141
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800142 virtual binder::Status setTorchMode(const String16& cameraId, bool enabled,
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800143 const sp<IBinder>& clientBinder);
144
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800145 virtual binder::Status notifySystemEvent(int32_t eventId,
146 const std::vector<int32_t>& args);
Ruben Brunk36597b22015-03-20 22:15:57 -0700147
Igor Murashkin65d14b92014-06-17 12:03:20 -0700148 // OK = supports api of that version, -EOPNOTSUPP = does not support
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800149 virtual binder::Status supportsCameraApi(
150 int32_t cameraId, int32_t apiVersion,
151 /*out*/
152 bool *isSupported);
Igor Murashkin65d14b92014-06-17 12:03:20 -0700153
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 Talvalad56db1d2015-12-17 16:50:35 -0800188 static binder::Status 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:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800195 virtual status_t initialize(CameraModule *module) = 0;
196 virtual binder::Status 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
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800200 virtual sp<IBinder> asBinderWrapper() = 0;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700201
Ruben Brunk9efdf952015-03-18 23:11:57 -0700202 // Return the remote callback binder object (e.g. ICameraDeviceCallbacks)
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800203 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
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800208 virtual status_t dump(int fd, const Vector<String16>& args);
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800209 // Internal dump method to be called by CameraService
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800210 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
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800216 virtual void notifyError(int32_t errorCode,
Ruben Brunkcc776712015-02-17 20:18:47 -0800217 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.
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800247 sp<CameraService> mCameraService; // immutable after constructor
248 int mCameraId; // immutable after constructor
249 int mCameraFacing; // immutable after constructor
250 String16 mClientPackageName; // immutable after constructor
Igor Murashkin634a5152013-02-20 17:15:11 -0800251 pid_t mClientPid;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800252 uid_t mClientUid; // immutable after constructor
253 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
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800285 class Client : public hardware::BnCamera, public BasicClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700286 {
287 public:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800288 typedef hardware::ICameraClient TCamCallbacks;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800289
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700290 // ICamera interface (see ICamera for details)
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800291 virtual binder::Status disconnect();
292 virtual status_t connect(const sp<hardware::ICameraClient>& client) = 0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700293 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;
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800302 virtual status_t setVideoBufferMode(int32_t videoBufferMode) = 0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700303 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;
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800313 virtual status_t setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer) = 0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700314
315 // Interface used by CameraService
316 Client(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800317 const sp<hardware::ICameraClient>& cameraClient,
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000318 const String16& clientPackageName,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700319 int cameraId,
320 int cameraFacing,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700321 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800322 uid_t clientUid,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700323 int servicePid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700324 ~Client();
325
326 // return our camera client
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800327 const sp<hardware::ICameraClient>& getRemoteCallback() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800328 return mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700329 }
330
Igor Murashkine7ee7632013-06-11 18:10:18 -0700331 virtual sp<IBinder> asBinderWrapper() {
Marco Nelissen06b46062014-11-14 07:58:25 -0800332 return asBinder(this);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700333 }
334
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800335 virtual void notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -0700336 const CaptureResultExtras& resultExtras);
Ruben Brunk0bbf8b22015-04-30 14:35:42 -0700337
338 // Check what API level is used for this client. This is used to determine which
339 // superclass this can be cast to.
340 virtual bool canCastToApiClient(apiLevel level) const;
Ruben Brunkcc776712015-02-17 20:18:47 -0800341 protected:
342 // Convert client from cookie.
343 static sp<CameraService::Client> getClientFromCookie(void* user);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800344
Igor Murashkin634a5152013-02-20 17:15:11 -0800345 // Initialized in constructor
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700346
Igor Murashkin634a5152013-02-20 17:15:11 -0800347 // - The app-side Binder interface to receive callbacks from us
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800348 sp<hardware::ICameraClient> mRemoteCallback;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800349
350 }; // class Client
Igor Murashkin634a5152013-02-20 17:15:11 -0800351
Ruben Brunk99e69712015-05-26 17:25:07 -0700352 /**
353 * A listener class that implements the LISTENER interface for use with a ClientManager, and
354 * implements the following methods:
355 * void onClientRemoved(const ClientDescriptor<KEY, VALUE>& descriptor);
356 * void onClientAdded(const ClientDescriptor<KEY, VALUE>& descriptor);
357 */
358 class ClientEventListener {
359 public:
360 void onClientAdded(const resource_policy::ClientDescriptor<String8,
361 sp<CameraService::BasicClient>>& descriptor);
362 void onClientRemoved(const resource_policy::ClientDescriptor<String8,
363 sp<CameraService::BasicClient>>& descriptor);
364 }; // class ClientEventListener
365
Ruben Brunkcc776712015-02-17 20:18:47 -0800366 typedef std::shared_ptr<resource_policy::ClientDescriptor<String8,
367 sp<CameraService::BasicClient>>> DescriptorPtr;
368
369 /**
370 * A container class for managing active camera clients that are using HAL devices. Active
371 * clients are represented by ClientDescriptor objects that contain strong pointers to the
372 * actual BasicClient subclass binder interface implementation.
373 *
374 * This class manages the eviction behavior for the camera clients. See the parent class
375 * implementation in utils/ClientManager for the specifics of this behavior.
376 */
Ruben Brunk99e69712015-05-26 17:25:07 -0700377 class CameraClientManager : public resource_policy::ClientManager<String8,
378 sp<CameraService::BasicClient>, ClientEventListener> {
Ruben Brunkcc776712015-02-17 20:18:47 -0800379 public:
Ruben Brunk99e69712015-05-26 17:25:07 -0700380 CameraClientManager();
Ruben Brunkcc776712015-02-17 20:18:47 -0800381 virtual ~CameraClientManager();
382
383 /**
384 * Return a strong pointer to the active BasicClient for this camera ID, or an empty
385 * if none exists.
386 */
387 sp<CameraService::BasicClient> getCameraClient(const String8& id) const;
388
389 /**
390 * Return a string describing the current state.
391 */
392 String8 toString() const;
393
394 /**
395 * Make a ClientDescriptor object wrapping the given BasicClient strong pointer.
396 */
397 static DescriptorPtr makeClientDescriptor(const String8& key, const sp<BasicClient>& value,
398 int32_t cost, const std::set<String8>& conflictingKeys, int32_t priority,
399 int32_t ownerId);
400
401 /**
402 * Make a ClientDescriptor object wrapping the given BasicClient strong pointer with
403 * values intialized from a prior ClientDescriptor.
404 */
405 static DescriptorPtr makeClientDescriptor(const sp<BasicClient>& value,
406 const CameraService::DescriptorPtr& partial);
407
408 }; // class CameraClientManager
409
Mathias Agopian65ab4712010-07-14 17:59:35 -0700410private:
Igor Murashkin634a5152013-02-20 17:15:11 -0800411
Ruben Brunkcc776712015-02-17 20:18:47 -0800412 /**
413 * Container class for the state of each logical camera device, including: ID, status, and
414 * dependencies on other devices. The mapping of camera ID -> state saved in mCameraStates
415 * represents the camera devices advertised by the HAL (and any USB devices, when we add
416 * those).
417 *
418 * This container does NOT represent an active camera client. These are represented using
419 * the ClientDescriptors stored in mActiveClientManager.
420 */
421 class CameraState {
422 public:
423 /**
424 * Make a new CameraState and set the ID, cost, and conflicting devices using the values
425 * returned in the HAL's camera_info struct for each device.
426 */
427 CameraState(const String8& id, int cost, const std::set<String8>& conflicting);
428 virtual ~CameraState();
429
430 /**
431 * Return the status for this device.
432 *
433 * This method acquires mStatusLock.
434 */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800435 int32_t getStatus() const;
Ruben Brunkcc776712015-02-17 20:18:47 -0800436
437 /**
438 * This function updates the status for this camera device, unless the given status
439 * is in the given list of rejected status states, and execute the function passed in
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800440 * with a signature onStatusUpdateLocked(const String8&, int32_t)
Ruben Brunkcc776712015-02-17 20:18:47 -0800441 * if the status has changed.
442 *
443 * This method is idempotent, and will not result in the function passed to
444 * onStatusUpdateLocked being called more than once for the same arguments.
445 * This method aquires mStatusLock.
446 */
447 template<class Func>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800448 void updateStatus(int32_t status, const String8& cameraId,
449 std::initializer_list<int32_t> rejectSourceStates,
Ruben Brunkcc776712015-02-17 20:18:47 -0800450 Func onStatusUpdatedLocked);
451
452 /**
453 * Return the last set CameraParameters object generated from the information returned by
454 * the HAL for this device (or an empty CameraParameters object if none has been set).
455 */
456 CameraParameters getShimParams() const;
457
458 /**
459 * Set the CameraParameters for this device.
460 */
461 void setShimParams(const CameraParameters& params);
462
463 /**
464 * Return the resource_cost advertised by the HAL for this device.
465 */
466 int getCost() const;
467
468 /**
469 * Return a set of the IDs of conflicting devices advertised by the HAL for this device.
470 */
471 std::set<String8> getConflicting() const;
472
473 /**
474 * Return the ID of this camera device.
475 */
476 String8 getId() const;
477
478 private:
479 const String8 mId;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800480 int32_t mStatus; // protected by mStatusLock
Ruben Brunkcc776712015-02-17 20:18:47 -0800481 const int mCost;
482 std::set<String8> mConflicting;
483 mutable Mutex mStatusLock;
484 CameraParameters mShimParams;
485 }; // class CameraState
486
Igor Murashkin634a5152013-02-20 17:15:11 -0800487 // Delay-load the Camera HAL module
488 virtual void onFirstRef();
489
Ruben Brunkcc776712015-02-17 20:18:47 -0800490 // Check if we can connect, before we acquire the service lock.
Chien-Yu Chen18df60e2016-03-18 18:18:09 -0700491 // The returned originalClientPid is the PID of the original process that wants to connect to
492 // camera.
493 // The returned clientPid is the PID of the client that directly connects to camera.
494 // originalClientPid and clientPid are usually the same except when the application uses
495 // mediaserver to connect to camera (using MediaRecorder to connect to camera). In that case,
496 // clientPid is the PID of mediaserver and originalClientPid is the PID of the application.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800497 binder::Status validateConnectLocked(const String8& cameraId, const String8& clientName8,
Chien-Yu Chen7939aee2016-03-21 18:19:33 -0700498 /*inout*/int& clientUid, /*inout*/int& clientPid, /*out*/int& originalClientPid) const;
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800499 binder::Status validateClientPermissionsLocked(const String8& cameraId, const String8& clientName8,
Chien-Yu Chen7939aee2016-03-21 18:19:33 -0700500 /*inout*/int& clientUid, /*inout*/int& clientPid, /*out*/int& originalClientPid) const;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800501
Ruben Brunkcc776712015-02-17 20:18:47 -0800502 // Handle active client evictions, and update service state.
503 // Only call with with mServiceLock held.
504 status_t handleEvictionsLocked(const String8& cameraId, int clientPid,
505 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
506 /*out*/
507 sp<BasicClient>* client,
508 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800509
Ruben Brunkcc776712015-02-17 20:18:47 -0800510 // Single implementation shared between the various connect calls
511 template<class CALLBACK, class CLIENT>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800512 binder::Status connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId,
513 int halVersion, const String16& clientPackageName,
514 int clientUid, int clientPid,
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800515 apiLevel effectiveApiLevel, bool legacyMode, bool shimUpdateOnly,
516 /*out*/sp<CLIENT>& device);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800517
Ruben Brunkcc776712015-02-17 20:18:47 -0800518 // Lock guarding camera service state
Mathias Agopian65ab4712010-07-14 17:59:35 -0700519 Mutex mServiceLock;
Ruben Brunkcc776712015-02-17 20:18:47 -0800520
521 // Condition to use with mServiceLock, used to handle simultaneous connect calls from clients
522 std::shared_ptr<WaitableMutexWrapper> mServiceLockWrapper;
523
524 // Return NO_ERROR if the device with a give ID can be connected to
525 status_t checkIfDeviceIsUsable(const String8& cameraId) const;
526
527 // Container for managing currently active application-layer clients
528 CameraClientManager mActiveClientManager;
529
530 // Mapping from camera ID -> state for each device, map is protected by mCameraStatesLock
531 std::map<String8, std::shared_ptr<CameraState>> mCameraStates;
532
533 // Mutex guarding mCameraStates map
534 mutable Mutex mCameraStatesLock;
535
536 // Circular buffer for storing event logging for dumps
537 RingBuffer<String8> mEventLog;
Ruben Brunka8ca9152015-04-07 14:23:40 -0700538 Mutex mLogLock;
Ruben Brunkcc776712015-02-17 20:18:47 -0800539
Ruben Brunk6267b532015-04-30 17:44:07 -0700540 // Currently allowed user IDs
541 std::set<userid_t> mAllowedUsers;
Ruben Brunk36597b22015-03-20 22:15:57 -0700542
Ruben Brunkcc776712015-02-17 20:18:47 -0800543 /**
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700544 * Check camera capabilities, such as support for basic color operation
545 */
546 int checkCameraCapabilities(int id, camera_info info, int *latestStrangeCameraId);
547
548 /**
Ruben Brunkcc776712015-02-17 20:18:47 -0800549 * Get the camera state for a given camera id.
550 *
551 * This acquires mCameraStatesLock.
552 */
553 std::shared_ptr<CameraService::CameraState> getCameraState(const String8& cameraId) const;
554
555 /**
556 * Evict client who's remote binder has died. Returns true if this client was in the active
557 * list and was disconnected.
558 *
559 * This method acquires mServiceLock.
560 */
561 bool evictClientIdByRemote(const wp<IBinder>& cameraClient);
562
563 /**
564 * Remove the given client from the active clients list; does not disconnect the client.
565 *
566 * This method acquires mServiceLock.
567 */
568 void removeByClient(const BasicClient* client);
569
570 /**
571 * Add new client to active clients list after conflicting clients have disconnected using the
572 * values set in the partial descriptor passed in to construct the actual client descriptor.
573 * This is typically called at the end of a connect call.
574 *
575 * This method must be called with mServiceLock held.
576 */
577 void finishConnectLocked(const sp<BasicClient>& client, const DescriptorPtr& desc);
578
579 /**
580 * Returns the integer corresponding to the given camera ID string, or -1 on failure.
581 */
582 static int cameraIdToInt(const String8& cameraId);
583
584 /**
585 * Remove a single client corresponding to the given camera id from the list of active clients.
586 * If none exists, return an empty strongpointer.
587 *
588 * This method must be called with mServiceLock held.
589 */
590 sp<CameraService::BasicClient> removeClientLocked(const String8& cameraId);
591
592 /**
Ruben Brunk36597b22015-03-20 22:15:57 -0700593 * Handle a notification that the current device user has changed.
594 */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800595 void doUserSwitch(const std::vector<int32_t>& newUserIds);
Ruben Brunk36597b22015-03-20 22:15:57 -0700596
597 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -0700598 * Add an event log message.
Ruben Brunkcc776712015-02-17 20:18:47 -0800599 */
Ruben Brunka8ca9152015-04-07 14:23:40 -0700600 void logEvent(const char* event);
Ruben Brunkcc776712015-02-17 20:18:47 -0800601
602 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -0700603 * Add an event log message that a client has been disconnected.
Ruben Brunkcc776712015-02-17 20:18:47 -0800604 */
Ruben Brunka8ca9152015-04-07 14:23:40 -0700605 void logDisconnected(const char* cameraId, int clientPid, const char* clientPackage);
606
607 /**
608 * Add an event log message that a client has been connected.
609 */
610 void logConnected(const char* cameraId, int clientPid, const char* clientPackage);
611
612 /**
613 * Add an event log message that a client's connect attempt has been rejected.
614 */
615 void logRejected(const char* cameraId, int clientPid, const char* clientPackage,
616 const char* reason);
617
618 /**
619 * Add an event log message that the current device user has been switched.
620 */
Ruben Brunk6267b532015-04-30 17:44:07 -0700621 void logUserSwitch(const std::set<userid_t>& oldUserIds,
622 const std::set<userid_t>& newUserIds);
Ruben Brunka8ca9152015-04-07 14:23:40 -0700623
624 /**
625 * Add an event log message that a device has been removed by the HAL
626 */
627 void logDeviceRemoved(const char* cameraId, const char* reason);
628
629 /**
630 * Add an event log message that a device has been added by the HAL
631 */
632 void logDeviceAdded(const char* cameraId, const char* reason);
633
634 /**
635 * Add an event log message that a client has unexpectedly died.
636 */
637 void logClientDied(int clientPid, const char* reason);
Ruben Brunkcc776712015-02-17 20:18:47 -0800638
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700639 /**
640 * Add a event log message that a serious service-level error has occured
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800641 * The errorCode should be one of the Android Errors
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700642 */
643 void logServiceError(const char* msg, int errorCode);
644
645 /**
646 * Dump the event log to an FD
647 */
648 void dumpEventLog(int fd);
649
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650 int mNumberOfCameras;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700651 int mNumberOfNormalCameras;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700652
Mathias Agopian65ab4712010-07-14 17:59:35 -0700653 // sounds
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800654 MediaPlayer* newMediaPlayer(const char *file);
655
Mathias Agopian65ab4712010-07-14 17:59:35 -0700656 Mutex mSoundLock;
657 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
658 int mSoundRef; // reference count (release all MediaPlayer when 0)
659
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800660 CameraModule* mModule;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700661
Ruben Brunkcc776712015-02-17 20:18:47 -0800662 // Guarded by mStatusListenerMutex
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800663 std::vector<sp<hardware::ICameraServiceListener>> mListenerList;
Ruben Brunkcc776712015-02-17 20:18:47 -0800664 Mutex mStatusListenerLock;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800665
Ruben Brunkcc776712015-02-17 20:18:47 -0800666 /**
667 * Update the status for the given camera id (if that device exists), and broadcast the
668 * status update to all current ICameraServiceListeners if the status has changed. Any
669 * statuses in rejectedSourceStates will be ignored.
670 *
671 * This method must be idempotent.
672 * This method acquires mStatusLock and mStatusListenerLock.
673 */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800674 void updateStatus(int32_t status, const String8& cameraId,
675 std::initializer_list<int32_t> rejectedSourceStates);
676 void updateStatus(int32_t status, const String8& cameraId);
Igor Murashkinbfc99152013-02-27 12:55:20 -0800677
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800678 // flashlight control
679 sp<CameraFlashlight> mFlashlight;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800680 // guard mTorchStatusMap
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800681 Mutex mTorchStatusMutex;
Chien-Yu Chenfe751be2015-09-01 14:16:44 -0700682 // guard mTorchClientMap
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800683 Mutex mTorchClientMapMutex;
Chien-Yu Chenfe751be2015-09-01 14:16:44 -0700684 // guard mTorchUidMap
685 Mutex mTorchUidMapMutex;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800686 // camera id -> torch status
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800687 KeyedVector<String8, int32_t> mTorchStatusMap;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800688 // camera id -> torch client binder
689 // only store the last client that turns on each camera's torch mode
Ruben Brunk99e69712015-05-26 17:25:07 -0700690 KeyedVector<String8, sp<IBinder>> mTorchClientMap;
691 // camera id -> [incoming uid, current uid] pair
692 std::map<String8, std::pair<int, int>> mTorchUidMap;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800693
694 // check and handle if torch client's process has died
695 void handleTorchClientBinderDied(const wp<IBinder> &who);
696
697 // handle torch mode status change and invoke callbacks. mTorchStatusMutex
698 // should be locked.
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800699 void onTorchStatusChangedLocked(const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800700 int32_t newStatus);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800701
702 // get a camera's torch status. mTorchStatusMutex should be locked.
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800703 status_t getTorchStatusLocked(const String8 &cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800704 int32_t *status) const;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800705
706 // set a camera's torch status. mTorchStatusMutex should be locked.
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800707 status_t setTorchStatusLocked(const String8 &cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800708 int32_t status);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800709
Igor Murashkinecf17e82012-10-02 16:05:11 -0700710 // IBinder::DeathRecipient implementation
Igor Murashkinbfc99152013-02-27 12:55:20 -0800711 virtual void binderDied(const wp<IBinder> &who);
Igor Murashkin634a5152013-02-20 17:15:11 -0800712
713 // Helpers
Igor Murashkinbfc99152013-02-27 12:55:20 -0800714
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800715 bool setUpVendorTags();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700716
717 /**
Ruben Brunkb2119af2014-05-09 19:57:56 -0700718 * Initialize and cache the metadata used by the HAL1 shim for a given cameraId.
719 *
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800720 * Sets Status to a service-specific error on failure
Ruben Brunkb2119af2014-05-09 19:57:56 -0700721 */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800722 binder::Status initializeShimMetadata(int cameraId);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700723
724 /**
Igor Murashkin65d14b92014-06-17 12:03:20 -0700725 * Get the cached CameraParameters for the camera. If they haven't been
726 * cached yet, then initialize them for the first time.
727 *
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800728 * Sets Status to a service-specific error on failure
Igor Murashkin65d14b92014-06-17 12:03:20 -0700729 */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800730 binder::Status getLegacyParametersLazy(int cameraId, /*out*/CameraParameters* parameters);
Igor Murashkin65d14b92014-06-17 12:03:20 -0700731
732 /**
Ruben Brunkb2119af2014-05-09 19:57:56 -0700733 * Generate the CameraCharacteristics metadata required by the Camera2 API
734 * from the available HAL1 CameraParameters and CameraInfo.
735 *
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800736 * Sets Status to a service-specific error on failure
Ruben Brunkb2119af2014-05-09 19:57:56 -0700737 */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800738 binder::Status generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700739
Ruben Brunkcc776712015-02-17 20:18:47 -0800740 static int getCallingPid();
741
742 static int getCallingUid();
743
Ruben Brunkb2119af2014-05-09 19:57:56 -0700744 /**
Ruben Brunkcc776712015-02-17 20:18:47 -0800745 * Get the current system time as a formatted string.
Ruben Brunkb2119af2014-05-09 19:57:56 -0700746 */
Ruben Brunkcc776712015-02-17 20:18:47 -0800747 static String8 getFormattedCurrentTime();
748
749 /**
750 * Get the camera eviction priority from the current process state given by ActivityManager.
751 */
752 static int getCameraPriorityFromProcState(int procState);
753
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800754 static binder::Status makeClient(const sp<CameraService>& cameraService,
755 const sp<IInterface>& cameraCb, const String16& packageName, int cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800756 int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode,
757 int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
758 /*out*/sp<BasicClient>* client);
Ruben Brunk6267b532015-04-30 17:44:07 -0700759
760 status_t checkCameraAccess(const String16& opPackageName);
761
762 static String8 toString(std::set<userid_t> intSet);
763
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700764 static sp<ICameraServiceProxy> getCameraServiceProxy();
Ruben Brunk2823ce02015-05-19 17:25:13 -0700765 static void pingCameraServiceProxy();
766
Mathias Agopian65ab4712010-07-14 17:59:35 -0700767};
768
Ruben Brunkcc776712015-02-17 20:18:47 -0800769template<class Func>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800770void CameraService::CameraState::updateStatus(int32_t status,
Ruben Brunkcc776712015-02-17 20:18:47 -0800771 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800772 std::initializer_list<int32_t> rejectSourceStates,
Ruben Brunkcc776712015-02-17 20:18:47 -0800773 Func onStatusUpdatedLocked) {
774 Mutex::Autolock lock(mStatusLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800775 int32_t oldStatus = mStatus;
Ruben Brunkcc776712015-02-17 20:18:47 -0800776 mStatus = status;
777
778 if (oldStatus == status) {
779 return;
780 }
781
782 ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__,
783 cameraId.string(), oldStatus, status);
784
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800785 if (oldStatus == hardware::ICameraServiceListener::STATUS_NOT_PRESENT &&
786 (status != hardware::ICameraServiceListener::STATUS_PRESENT &&
787 status != hardware::ICameraServiceListener::STATUS_ENUMERATING)) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800788
789 ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING",
790 __FUNCTION__);
791 mStatus = oldStatus;
792 return;
793 }
794
795 /**
796 * Sometimes we want to conditionally do a transition.
797 * For example if a client disconnects, we want to go to PRESENT
798 * only if we weren't already in NOT_PRESENT or ENUMERATING.
799 */
800 for (auto& rejectStatus : rejectSourceStates) {
801 if (oldStatus == rejectStatus) {
802 ALOGV("%s: Rejecting status transition for Camera ID %s, since the source "
803 "state was was in one of the bad states.", __FUNCTION__, cameraId.string());
804 mStatus = oldStatus;
805 return;
806 }
807 }
808
809 onStatusUpdatedLocked(cameraId, status);
810}
811
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800812#define STATUS_ERROR(errorCode, errorString) \
813 binder::Status::fromServiceSpecificError(errorCode, \
814 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
815
816#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
817 binder::Status::fromServiceSpecificError(errorCode, \
818 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, __VA_ARGS__))
819
Ruben Brunkcc776712015-02-17 20:18:47 -0800820
821template<class CALLBACK, class CLIENT>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800822binder::Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId,
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800823 int halVersion, const String16& clientPackageName, int clientUid, int clientPid,
Ruben Brunkcc776712015-02-17 20:18:47 -0800824 apiLevel effectiveApiLevel, bool legacyMode, bool shimUpdateOnly,
825 /*out*/sp<CLIENT>& device) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800826 binder::Status ret = binder::Status::ok();
827
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000828 String8 clientName8(clientPackageName);
Ruben Brunkcc776712015-02-17 20:18:47 -0800829
Chien-Yu Chen18df60e2016-03-18 18:18:09 -0700830 int originalClientPid = 0;
831
Ruben Brunka8ca9152015-04-07 14:23:40 -0700832 ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) for HAL version %s and "
Ruben Brunkcc776712015-02-17 20:18:47 -0800833 "Camera API version %d", clientPid, clientName8.string(), cameraId.string(),
Ruben Brunka8ca9152015-04-07 14:23:40 -0700834 (halVersion == -1) ? "default" : std::to_string(halVersion).c_str(),
835 static_cast<int>(effectiveApiLevel));
Ruben Brunkcc776712015-02-17 20:18:47 -0800836
Ruben Brunkcc776712015-02-17 20:18:47 -0800837 sp<CLIENT> client = nullptr;
838 {
839 // Acquire mServiceLock and prevent other clients from connecting
840 std::unique_ptr<AutoConditionLock> lock =
841 AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS);
Ruben Brunk36597b22015-03-20 22:15:57 -0700842
Ruben Brunkcc776712015-02-17 20:18:47 -0800843 if (lock == nullptr) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800844 ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)."
Ruben Brunkcc776712015-02-17 20:18:47 -0800845 , clientPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800846 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
847 "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting",
848 cameraId.string(), clientName8.string(), clientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -0800849 }
850
Ruben Brunk36597b22015-03-20 22:15:57 -0700851 // Enforce client permissions and do basic sanity checks
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800852 if(!(ret = validateConnectLocked(cameraId, clientName8,
Chien-Yu Chen18df60e2016-03-18 18:18:09 -0700853 /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) {
Ruben Brunk36597b22015-03-20 22:15:57 -0700854 return ret;
855 }
Ruben Brunk36597b22015-03-20 22:15:57 -0700856
Ruben Brunkcc776712015-02-17 20:18:47 -0800857 // Check the shim parameters after acquiring lock, if they have already been updated and
858 // we were doing a shim update, return immediately
859 if (shimUpdateOnly) {
860 auto cameraState = getCameraState(cameraId);
861 if (cameraState != nullptr) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800862 if (!cameraState->getShimParams().isEmpty()) return ret;
Ruben Brunkcc776712015-02-17 20:18:47 -0800863 }
864 }
865
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800866 status_t err;
867
Ruben Brunkcc776712015-02-17 20:18:47 -0800868 sp<BasicClient> clientTmp = nullptr;
869 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial;
Chien-Yu Chen18df60e2016-03-18 18:18:09 -0700870 if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel,
Ruben Brunkcc776712015-02-17 20:18:47 -0800871 IInterface::asBinder(cameraCb), clientName8, /*out*/&clientTmp,
872 /*out*/&partial)) != NO_ERROR) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800873 switch (err) {
874 case -ENODEV:
875 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
876 "No camera device with ID \"%s\" currently available",
877 cameraId.string());
878 case -EBUSY:
879 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
880 "Higher-priority client using camera, ID \"%s\" currently unavailable",
881 cameraId.string());
882 default:
883 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
884 "Unexpected error %s (%d) opening camera \"%s\"",
885 strerror(-err), err, cameraId.string());
886 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800887 }
888
889 if (clientTmp.get() != nullptr) {
890 // Handle special case for API1 MediaRecorder where the existing client is returned
891 device = static_cast<CLIENT*>(clientTmp.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800892 return ret;
Ruben Brunkcc776712015-02-17 20:18:47 -0800893 }
894
895 // give flashlight a chance to close devices if necessary.
896 mFlashlight->prepareDeviceOpen(cameraId);
897
898 // TODO: Update getDeviceVersion + HAL interface to use strings for Camera IDs
899 int id = cameraIdToInt(cameraId);
900 if (id == -1) {
901 ALOGE("%s: Invalid camera ID %s, cannot get device version from HAL.", __FUNCTION__,
902 cameraId.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800903 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
904 "Bad camera ID \"%s\" passed to camera open", cameraId.string());
Ruben Brunkcc776712015-02-17 20:18:47 -0800905 }
906
907 int facing = -1;
908 int deviceVersion = getDeviceVersion(id, /*out*/&facing);
909 sp<BasicClient> tmp = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800910 if(!(ret = makeClient(this, cameraCb, clientPackageName, id, facing, clientPid,
Ruben Brunkcc776712015-02-17 20:18:47 -0800911 clientUid, getpid(), legacyMode, halVersion, deviceVersion, effectiveApiLevel,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800912 /*out*/&tmp)).isOk()) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800913 return ret;
914 }
915 client = static_cast<CLIENT*>(tmp.get());
916
917 LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state",
918 __FUNCTION__);
919
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800920 if ((err = client->initialize(mModule)) != OK) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800921 ALOGE("%s: Could not initialize client from HAL module.", __FUNCTION__);
Eino-Ville Talvala3525dd92016-03-20 18:04:09 -0700922 // Errors could be from the HAL module open call or from AppOpsManager
923 switch(err) {
924 case BAD_VALUE:
925 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
926 "Illegal argument to HAL module for camera \"%s\"", cameraId.string());
927 case -EBUSY:
928 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
929 "Camera \"%s\" is already open", cameraId.string());
930 case -EUSERS:
931 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
932 "Too many cameras already open, cannot open camera \"%s\"",
933 cameraId.string());
934 case PERMISSION_DENIED:
935 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
936 "No permission to open camera \"%s\"", cameraId.string());
937 case -EACCES:
938 return STATUS_ERROR_FMT(ERROR_DISABLED,
939 "Camera \"%s\" disabled by policy", cameraId.string());
940 case -ENODEV:
941 default:
942 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
943 "Failed to initialize camera \"%s\": %s (%d)", cameraId.string(),
944 strerror(-err), err);
945 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800946 }
947
Ruben Brunkcc776712015-02-17 20:18:47 -0800948 // Update shim paremeters for legacy clients
949 if (effectiveApiLevel == API_1) {
950 // Assume we have always received a Client subclass for API1
951 sp<Client> shimClient = reinterpret_cast<Client*>(client.get());
952 String8 rawParams = shimClient->getParameters();
953 CameraParameters params(rawParams);
954
955 auto cameraState = getCameraState(cameraId);
956 if (cameraState != nullptr) {
957 cameraState->setShimParams(params);
958 } else {
959 ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.",
960 __FUNCTION__, cameraId.string());
961 }
962 }
963
964 if (shimUpdateOnly) {
965 // If only updating legacy shim parameters, immediately disconnect client
966 mServiceLock.unlock();
967 client->disconnect();
968 mServiceLock.lock();
969 } else {
970 // Otherwise, add client to active clients list
971 finishConnectLocked(client, partial);
972 }
973 } // lock is destroyed, allow further connect calls
974
975 // Important: release the mutex here so the client can call back into the service from its
976 // destructor (can be at the end of the call)
977 device = client;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800978 return ret;
Ruben Brunkcc776712015-02-17 20:18:47 -0800979}
980
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800981#undef STATUS_ERROR_FMT
982#undef STATUS_ERROR
983
Mathias Agopian65ab4712010-07-14 17:59:35 -0700984} // namespace android
985
986#endif