Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 2 | * 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 Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 16 | |
| 17 | #ifndef ANDROID_SERVERS_CAMERA_CAMERASERVICE_H |
| 18 | #define ANDROID_SERVERS_CAMERA_CAMERASERVICE_H |
| 19 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 20 | #include <cutils/multiuser.h> |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 21 | #include <utils/Vector.h> |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 22 | #include <utils/KeyedVector.h> |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 23 | #include <binder/AppOpsManager.h> |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 24 | #include <binder/BinderService.h> |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 25 | #include <binder/IAppOpsCallback.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 26 | #include <camera/ICameraService.h> |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 27 | #include <hardware/camera.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 28 | |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 29 | #include <camera/ICamera.h> |
| 30 | #include <camera/ICameraClient.h> |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 31 | #include <camera/camera2/ICameraDeviceUser.h> |
| 32 | #include <camera/camera2/ICameraDeviceCallbacks.h> |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 33 | #include <camera/VendorTagDescriptor.h> |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 34 | #include <camera/CaptureResult.h> |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 35 | #include <camera/CameraParameters.h> |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 36 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 37 | #include <camera/ICameraServiceListener.h> |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 38 | #include "CameraFlashlight.h" |
| 39 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 40 | #include "common/CameraModule.h" |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 41 | #include "utils/AutoConditionLock.h" |
| 42 | #include "utils/ClientManager.h" |
| 43 | #include "utils/RingBuffer.h" |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 44 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 45 | #include <set> |
| 46 | #include <string> |
| 47 | #include <map> |
| 48 | #include <memory> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 49 | |
| 50 | namespace android { |
| 51 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 52 | extern volatile int32_t gLogLevel; |
| 53 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 54 | class MemoryHeapBase; |
| 55 | class MediaPlayer; |
| 56 | |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 57 | class CameraService : |
| 58 | public BinderService<CameraService>, |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 59 | public BnCameraService, |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 60 | public IBinder::DeathRecipient, |
| 61 | public camera_module_callbacks_t |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 62 | { |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 63 | friend class BinderService<CameraService>; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 64 | public: |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 65 | class Client; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 66 | class BasicClient; |
| 67 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 68 | // The effective API level. The Camera2 API running in LEGACY mode counts as API_1. |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 69 | enum apiLevel { |
| 70 | API_1 = 1, |
| 71 | API_2 = 2 |
| 72 | }; |
| 73 | |
| 74 | // Process States (mirrors frameworks/base/core/java/android/app/ActivityManager.java) |
| 75 | static const int PROCESS_STATE_NONEXISTENT = -1; |
| 76 | static const int PROCESS_STATE_PERSISTENT = 0; |
| 77 | static const int PROCESS_STATE_PERSISTENT_UI = 1; |
| 78 | static const int PROCESS_STATE_TOP = 2; |
| 79 | static const int PROCESS_STATE_IMPORTANT_FOREGROUND = 3; |
| 80 | static const int PROCESS_STATE_IMPORTANT_BACKGROUND = 4; |
| 81 | static const int PROCESS_STATE_BACKUP = 5; |
| 82 | static const int PROCESS_STATE_HEAVY_WEIGHT = 6; |
| 83 | static const int PROCESS_STATE_SERVICE = 7; |
| 84 | static const int PROCESS_STATE_RECEIVER = 8; |
| 85 | static const int PROCESS_STATE_HOME = 9; |
| 86 | static const int PROCESS_STATE_LAST_ACTIVITY = 10; |
| 87 | static const int PROCESS_STATE_CACHED_ACTIVITY = 11; |
| 88 | static const int PROCESS_STATE_CACHED_ACTIVITY_CLIENT = 12; |
| 89 | static const int PROCESS_STATE_CACHED_EMPTY = 13; |
| 90 | |
| 91 | // 3 second busy timeout when other clients are connecting |
| 92 | static const nsecs_t DEFAULT_CONNECT_TIMEOUT_NS = 3000000000; |
| 93 | |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 94 | // 1 second busy timeout when other clients are disconnecting |
| 95 | static const nsecs_t DEFAULT_DISCONNECT_TIMEOUT_NS = 1000000000; |
| 96 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 97 | // Default number of messages to store in eviction log |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 98 | static const size_t DEFAULT_EVENT_LOG_LENGTH = 100; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 99 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 100 | enum { |
| 101 | // Default last user id |
| 102 | DEFAULT_LAST_USER_ID = 0, |
| 103 | }; |
| 104 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 105 | // Implementation of BinderService<T> |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 106 | static char const* getServiceName() { return "media.camera"; } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 107 | |
| 108 | CameraService(); |
| 109 | virtual ~CameraService(); |
| 110 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 111 | ///////////////////////////////////////////////////////////////////// |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 112 | // HAL Callbacks |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 113 | virtual void onDeviceStatusChanged(camera_device_status_t cameraId, |
| 114 | camera_device_status_t newStatus); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 115 | virtual void onTorchStatusChanged(const String8& cameraId, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 116 | ICameraServiceListener::TorchStatus |
| 117 | newStatus); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 118 | |
| 119 | ///////////////////////////////////////////////////////////////////// |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 120 | // ICameraService |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 121 | virtual int32_t getNumberOfCameras(); |
| 122 | virtual status_t getCameraInfo(int cameraId, |
| 123 | struct CameraInfo* cameraInfo); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 124 | virtual status_t getCameraCharacteristics(int cameraId, |
| 125 | CameraMetadata* cameraInfo); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 126 | virtual status_t getCameraVendorTagDescriptor(/*out*/ sp<VendorTagDescriptor>& desc); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 127 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 128 | virtual status_t connect(const sp<ICameraClient>& cameraClient, int cameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame^] | 129 | const String16& clientPackageName, int clientUid, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 130 | /*out*/ |
| 131 | sp<ICamera>& device); |
| 132 | |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 133 | virtual status_t connectLegacy(const sp<ICameraClient>& cameraClient, int cameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame^] | 134 | int halVersion, const String16& clientPackageName, int clientUid, |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 135 | /*out*/ |
| 136 | sp<ICamera>& device); |
| 137 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 138 | virtual status_t connectDevice( |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 139 | const sp<ICameraDeviceCallbacks>& cameraCb, |
| 140 | int cameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame^] | 141 | const String16& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 142 | int clientUid, |
| 143 | /*out*/ |
| 144 | sp<ICameraDeviceUser>& device); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 145 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 146 | virtual status_t addListener(const sp<ICameraServiceListener>& listener); |
| 147 | virtual status_t removeListener( |
| 148 | const sp<ICameraServiceListener>& listener); |
| 149 | |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 150 | virtual status_t getLegacyParameters( |
| 151 | int cameraId, |
| 152 | /*out*/ |
| 153 | String16* parameters); |
| 154 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 155 | virtual status_t setTorchMode(const String16& cameraId, bool enabled, |
| 156 | const sp<IBinder>& clientBinder); |
| 157 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 158 | virtual void notifySystemEvent(int eventId, int arg0); |
| 159 | |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 160 | // OK = supports api of that version, -EOPNOTSUPP = does not support |
| 161 | virtual status_t supportsCameraApi( |
| 162 | int cameraId, int apiVersion); |
| 163 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 164 | // Extra permissions checks |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 165 | virtual status_t onTransact(uint32_t code, const Parcel& data, |
| 166 | Parcel* reply, uint32_t flags); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 167 | |
| 168 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 169 | |
| 170 | ///////////////////////////////////////////////////////////////////// |
| 171 | // Client functionality |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 172 | |
| 173 | enum sound_kind { |
| 174 | SOUND_SHUTTER = 0, |
| 175 | SOUND_RECORDING = 1, |
| 176 | NUM_SOUNDS |
| 177 | }; |
| 178 | |
| 179 | void loadSound(); |
| 180 | void playSound(sound_kind kind); |
| 181 | void releaseSound(); |
| 182 | |
Igor Murashkin | 98e2472 | 2013-06-19 19:51:04 -0700 | [diff] [blame] | 183 | ///////////////////////////////////////////////////////////////////// |
| 184 | // CameraDeviceFactory functionality |
| 185 | int getDeviceVersion(int cameraId, int* facing = NULL); |
| 186 | |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 187 | ///////////////////////////////////////////////////////////////////// |
| 188 | // Shared utilities |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 189 | static status_t filterGetInfoErrorCode(status_t err); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 190 | |
| 191 | ///////////////////////////////////////////////////////////////////// |
| 192 | // CameraClient functionality |
| 193 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 194 | class BasicClient : public virtual RefBase { |
| 195 | public: |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 196 | virtual status_t initialize(CameraModule *module) = 0; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 197 | virtual void disconnect(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 198 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 199 | // because we can't virtually inherit IInterface, which breaks |
| 200 | // virtual inheritance |
| 201 | virtual sp<IBinder> asBinderWrapper() = 0; |
| 202 | |
Ruben Brunk | 9efdf95 | 2015-03-18 23:11:57 -0700 | [diff] [blame] | 203 | // Return the remote callback binder object (e.g. ICameraDeviceCallbacks) |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 204 | sp<IBinder> getRemote() { |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 205 | return mRemoteBinder; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 208 | virtual status_t dump(int fd, const Vector<String16>& args) = 0; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 209 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 210 | // Return the package name for this client |
| 211 | virtual String16 getPackageName() const; |
| 212 | |
| 213 | // Notify client about a fatal error |
| 214 | virtual void notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode, |
| 215 | const CaptureResultExtras& resultExtras) = 0; |
| 216 | |
| 217 | // Get the PID of the application client using this |
| 218 | virtual int getClientPid() const; |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 219 | |
| 220 | // Check what API level is used for this client. This is used to determine which |
| 221 | // superclass this can be cast to. |
| 222 | virtual bool canCastToApiClient(apiLevel level) const; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 223 | protected: |
| 224 | BasicClient(const sp<CameraService>& cameraService, |
| 225 | const sp<IBinder>& remoteCallback, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame^] | 226 | const String16& clientPackageName, |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 227 | int cameraId, |
| 228 | int cameraFacing, |
| 229 | int clientPid, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 230 | uid_t clientUid, |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 231 | int servicePid); |
| 232 | |
| 233 | virtual ~BasicClient(); |
| 234 | |
| 235 | // the instance is in the middle of destruction. When this is set, |
| 236 | // the instance should not be accessed from callback. |
| 237 | // CameraService's mClientLock should be acquired to access this. |
| 238 | // - subclasses should set this to true in their destructors. |
| 239 | bool mDestructionStarted; |
| 240 | |
| 241 | // these are initialized in the constructor. |
| 242 | sp<CameraService> mCameraService; // immutable after constructor |
| 243 | int mCameraId; // immutable after constructor |
| 244 | int mCameraFacing; // immutable after constructor |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame^] | 245 | const String16 mClientPackageName; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 246 | pid_t mClientPid; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 247 | uid_t mClientUid; // immutable after constructor |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 248 | pid_t mServicePid; // immutable after constructor |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 249 | bool mDisconnected; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 250 | |
| 251 | // - The app-side Binder interface to receive callbacks from us |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 252 | sp<IBinder> mRemoteBinder; // immutable after constructor |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 253 | |
| 254 | // permissions management |
| 255 | status_t startCameraOps(); |
| 256 | status_t finishCameraOps(); |
| 257 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 258 | private: |
| 259 | AppOpsManager mAppOpsManager; |
| 260 | |
| 261 | class OpsCallback : public BnAppOpsCallback { |
| 262 | public: |
| 263 | OpsCallback(wp<BasicClient> client); |
| 264 | virtual void opChanged(int32_t op, const String16& packageName); |
| 265 | |
| 266 | private: |
| 267 | wp<BasicClient> mClient; |
| 268 | |
| 269 | }; // class OpsCallback |
| 270 | |
| 271 | sp<OpsCallback> mOpsCallback; |
| 272 | // Track whether startCameraOps was called successfully, to avoid |
| 273 | // finishing what we didn't start. |
| 274 | bool mOpsActive; |
| 275 | |
| 276 | // IAppOpsCallback interface, indirected through opListener |
| 277 | virtual void opChanged(int32_t op, const String16& packageName); |
| 278 | }; // class BasicClient |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 279 | |
| 280 | class Client : public BnCamera, public BasicClient |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 281 | { |
| 282 | public: |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 283 | typedef ICameraClient TCamCallbacks; |
| 284 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 285 | // ICamera interface (see ICamera for details) |
| 286 | virtual void disconnect(); |
| 287 | virtual status_t connect(const sp<ICameraClient>& client) = 0; |
| 288 | virtual status_t lock() = 0; |
| 289 | virtual status_t unlock() = 0; |
Eino-Ville Talvala | 1ce7c34 | 2013-08-21 13:57:21 -0700 | [diff] [blame] | 290 | virtual status_t setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)=0; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 291 | virtual void setPreviewCallbackFlag(int flag) = 0; |
Eino-Ville Talvala | 3ee3550 | 2013-04-02 15:45:11 -0700 | [diff] [blame] | 292 | virtual status_t setPreviewCallbackTarget( |
| 293 | const sp<IGraphicBufferProducer>& callbackProducer) = 0; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 294 | virtual status_t startPreview() = 0; |
| 295 | virtual void stopPreview() = 0; |
| 296 | virtual bool previewEnabled() = 0; |
| 297 | virtual status_t storeMetaDataInBuffers(bool enabled) = 0; |
| 298 | virtual status_t startRecording() = 0; |
| 299 | virtual void stopRecording() = 0; |
| 300 | virtual bool recordingEnabled() = 0; |
| 301 | virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0; |
| 302 | virtual status_t autoFocus() = 0; |
| 303 | virtual status_t cancelAutoFocus() = 0; |
| 304 | virtual status_t takePicture(int msgType) = 0; |
| 305 | virtual status_t setParameters(const String8& params) = 0; |
| 306 | virtual String8 getParameters() const = 0; |
| 307 | virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0; |
| 308 | |
| 309 | // Interface used by CameraService |
| 310 | Client(const sp<CameraService>& cameraService, |
| 311 | const sp<ICameraClient>& cameraClient, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame^] | 312 | const String16& clientPackageName, |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 313 | int cameraId, |
| 314 | int cameraFacing, |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 315 | int clientPid, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 316 | uid_t clientUid, |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 317 | int servicePid); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 318 | ~Client(); |
| 319 | |
| 320 | // return our camera client |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 321 | const sp<ICameraClient>& getRemoteCallback() { |
| 322 | return mRemoteCallback; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 325 | virtual sp<IBinder> asBinderWrapper() { |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 326 | return asBinder(this); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 329 | virtual void notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode, |
| 330 | const CaptureResultExtras& resultExtras); |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 331 | |
| 332 | // Check what API level is used for this client. This is used to determine which |
| 333 | // superclass this can be cast to. |
| 334 | virtual bool canCastToApiClient(apiLevel level) const; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 335 | protected: |
| 336 | // Convert client from cookie. |
| 337 | static sp<CameraService::Client> getClientFromCookie(void* user); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 338 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 339 | // Initialized in constructor |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 340 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 341 | // - The app-side Binder interface to receive callbacks from us |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 342 | sp<ICameraClient> mRemoteCallback; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 343 | |
| 344 | }; // class Client |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 345 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 346 | typedef std::shared_ptr<resource_policy::ClientDescriptor<String8, |
| 347 | sp<CameraService::BasicClient>>> DescriptorPtr; |
| 348 | |
| 349 | /** |
| 350 | * A container class for managing active camera clients that are using HAL devices. Active |
| 351 | * clients are represented by ClientDescriptor objects that contain strong pointers to the |
| 352 | * actual BasicClient subclass binder interface implementation. |
| 353 | * |
| 354 | * This class manages the eviction behavior for the camera clients. See the parent class |
| 355 | * implementation in utils/ClientManager for the specifics of this behavior. |
| 356 | */ |
| 357 | class CameraClientManager : |
| 358 | public resource_policy::ClientManager<String8, sp<CameraService::BasicClient>> { |
| 359 | public: |
| 360 | virtual ~CameraClientManager(); |
| 361 | |
| 362 | /** |
| 363 | * Return a strong pointer to the active BasicClient for this camera ID, or an empty |
| 364 | * if none exists. |
| 365 | */ |
| 366 | sp<CameraService::BasicClient> getCameraClient(const String8& id) const; |
| 367 | |
| 368 | /** |
| 369 | * Return a string describing the current state. |
| 370 | */ |
| 371 | String8 toString() const; |
| 372 | |
| 373 | /** |
| 374 | * Make a ClientDescriptor object wrapping the given BasicClient strong pointer. |
| 375 | */ |
| 376 | static DescriptorPtr makeClientDescriptor(const String8& key, const sp<BasicClient>& value, |
| 377 | int32_t cost, const std::set<String8>& conflictingKeys, int32_t priority, |
| 378 | int32_t ownerId); |
| 379 | |
| 380 | /** |
| 381 | * Make a ClientDescriptor object wrapping the given BasicClient strong pointer with |
| 382 | * values intialized from a prior ClientDescriptor. |
| 383 | */ |
| 384 | static DescriptorPtr makeClientDescriptor(const sp<BasicClient>& value, |
| 385 | const CameraService::DescriptorPtr& partial); |
| 386 | |
| 387 | }; // class CameraClientManager |
| 388 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 389 | private: |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 390 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 391 | /** |
| 392 | * Container class for the state of each logical camera device, including: ID, status, and |
| 393 | * dependencies on other devices. The mapping of camera ID -> state saved in mCameraStates |
| 394 | * represents the camera devices advertised by the HAL (and any USB devices, when we add |
| 395 | * those). |
| 396 | * |
| 397 | * This container does NOT represent an active camera client. These are represented using |
| 398 | * the ClientDescriptors stored in mActiveClientManager. |
| 399 | */ |
| 400 | class CameraState { |
| 401 | public: |
| 402 | /** |
| 403 | * Make a new CameraState and set the ID, cost, and conflicting devices using the values |
| 404 | * returned in the HAL's camera_info struct for each device. |
| 405 | */ |
| 406 | CameraState(const String8& id, int cost, const std::set<String8>& conflicting); |
| 407 | virtual ~CameraState(); |
| 408 | |
| 409 | /** |
| 410 | * Return the status for this device. |
| 411 | * |
| 412 | * This method acquires mStatusLock. |
| 413 | */ |
| 414 | ICameraServiceListener::Status getStatus() const; |
| 415 | |
| 416 | /** |
| 417 | * This function updates the status for this camera device, unless the given status |
| 418 | * is in the given list of rejected status states, and execute the function passed in |
| 419 | * with a signature onStatusUpdateLocked(const String8&, ICameraServiceListener::Status) |
| 420 | * if the status has changed. |
| 421 | * |
| 422 | * This method is idempotent, and will not result in the function passed to |
| 423 | * onStatusUpdateLocked being called more than once for the same arguments. |
| 424 | * This method aquires mStatusLock. |
| 425 | */ |
| 426 | template<class Func> |
| 427 | void updateStatus(ICameraServiceListener::Status status, const String8& cameraId, |
| 428 | std::initializer_list<ICameraServiceListener::Status> rejectSourceStates, |
| 429 | Func onStatusUpdatedLocked); |
| 430 | |
| 431 | /** |
| 432 | * Return the last set CameraParameters object generated from the information returned by |
| 433 | * the HAL for this device (or an empty CameraParameters object if none has been set). |
| 434 | */ |
| 435 | CameraParameters getShimParams() const; |
| 436 | |
| 437 | /** |
| 438 | * Set the CameraParameters for this device. |
| 439 | */ |
| 440 | void setShimParams(const CameraParameters& params); |
| 441 | |
| 442 | /** |
| 443 | * Return the resource_cost advertised by the HAL for this device. |
| 444 | */ |
| 445 | int getCost() const; |
| 446 | |
| 447 | /** |
| 448 | * Return a set of the IDs of conflicting devices advertised by the HAL for this device. |
| 449 | */ |
| 450 | std::set<String8> getConflicting() const; |
| 451 | |
| 452 | /** |
| 453 | * Return the ID of this camera device. |
| 454 | */ |
| 455 | String8 getId() const; |
| 456 | |
| 457 | private: |
| 458 | const String8 mId; |
| 459 | ICameraServiceListener::Status mStatus; // protected by mStatusLock |
| 460 | const int mCost; |
| 461 | std::set<String8> mConflicting; |
| 462 | mutable Mutex mStatusLock; |
| 463 | CameraParameters mShimParams; |
| 464 | }; // class CameraState |
| 465 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 466 | // Delay-load the Camera HAL module |
| 467 | virtual void onFirstRef(); |
| 468 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 469 | // Check if we can connect, before we acquire the service lock. |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 470 | status_t validateConnectLocked(const String8& cameraId, /*inout*/int& clientUid) const; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 471 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 472 | // Handle active client evictions, and update service state. |
| 473 | // Only call with with mServiceLock held. |
| 474 | status_t handleEvictionsLocked(const String8& cameraId, int clientPid, |
| 475 | apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName, |
| 476 | /*out*/ |
| 477 | sp<BasicClient>* client, |
| 478 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 479 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 480 | // Single implementation shared between the various connect calls |
| 481 | template<class CALLBACK, class CLIENT> |
| 482 | status_t connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId, int halVersion, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame^] | 483 | const String16& clientPackageName, int clientUid, apiLevel effectiveApiLevel, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 484 | bool legacyMode, bool shimUpdateOnly, /*out*/sp<CLIENT>& device); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 485 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 486 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 487 | // Lock guarding camera service state |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 488 | Mutex mServiceLock; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 489 | |
| 490 | // Condition to use with mServiceLock, used to handle simultaneous connect calls from clients |
| 491 | std::shared_ptr<WaitableMutexWrapper> mServiceLockWrapper; |
| 492 | |
| 493 | // Return NO_ERROR if the device with a give ID can be connected to |
| 494 | status_t checkIfDeviceIsUsable(const String8& cameraId) const; |
| 495 | |
| 496 | // Container for managing currently active application-layer clients |
| 497 | CameraClientManager mActiveClientManager; |
| 498 | |
| 499 | // Mapping from camera ID -> state for each device, map is protected by mCameraStatesLock |
| 500 | std::map<String8, std::shared_ptr<CameraState>> mCameraStates; |
| 501 | |
| 502 | // Mutex guarding mCameraStates map |
| 503 | mutable Mutex mCameraStatesLock; |
| 504 | |
| 505 | // Circular buffer for storing event logging for dumps |
| 506 | RingBuffer<String8> mEventLog; |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 507 | Mutex mLogLock; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 508 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 509 | // UID of last user. |
| 510 | int mLastUserId; |
| 511 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 512 | /** |
| 513 | * Get the camera state for a given camera id. |
| 514 | * |
| 515 | * This acquires mCameraStatesLock. |
| 516 | */ |
| 517 | std::shared_ptr<CameraService::CameraState> getCameraState(const String8& cameraId) const; |
| 518 | |
| 519 | /** |
| 520 | * Evict client who's remote binder has died. Returns true if this client was in the active |
| 521 | * list and was disconnected. |
| 522 | * |
| 523 | * This method acquires mServiceLock. |
| 524 | */ |
| 525 | bool evictClientIdByRemote(const wp<IBinder>& cameraClient); |
| 526 | |
| 527 | /** |
| 528 | * Remove the given client from the active clients list; does not disconnect the client. |
| 529 | * |
| 530 | * This method acquires mServiceLock. |
| 531 | */ |
| 532 | void removeByClient(const BasicClient* client); |
| 533 | |
| 534 | /** |
| 535 | * Add new client to active clients list after conflicting clients have disconnected using the |
| 536 | * values set in the partial descriptor passed in to construct the actual client descriptor. |
| 537 | * This is typically called at the end of a connect call. |
| 538 | * |
| 539 | * This method must be called with mServiceLock held. |
| 540 | */ |
| 541 | void finishConnectLocked(const sp<BasicClient>& client, const DescriptorPtr& desc); |
| 542 | |
| 543 | /** |
| 544 | * Returns the integer corresponding to the given camera ID string, or -1 on failure. |
| 545 | */ |
| 546 | static int cameraIdToInt(const String8& cameraId); |
| 547 | |
| 548 | /** |
| 549 | * Remove a single client corresponding to the given camera id from the list of active clients. |
| 550 | * If none exists, return an empty strongpointer. |
| 551 | * |
| 552 | * This method must be called with mServiceLock held. |
| 553 | */ |
| 554 | sp<CameraService::BasicClient> removeClientLocked(const String8& cameraId); |
| 555 | |
| 556 | /** |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 557 | * Handle a notification that the current device user has changed. |
| 558 | */ |
| 559 | void doUserSwitch(int newUserId); |
| 560 | |
| 561 | /** |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 562 | * Add an event log message. |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 563 | */ |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 564 | void logEvent(const char* event); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 565 | |
| 566 | /** |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 567 | * Add an event log message that a client has been disconnected. |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 568 | */ |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 569 | void logDisconnected(const char* cameraId, int clientPid, const char* clientPackage); |
| 570 | |
| 571 | /** |
| 572 | * Add an event log message that a client has been connected. |
| 573 | */ |
| 574 | void logConnected(const char* cameraId, int clientPid, const char* clientPackage); |
| 575 | |
| 576 | /** |
| 577 | * Add an event log message that a client's connect attempt has been rejected. |
| 578 | */ |
| 579 | void logRejected(const char* cameraId, int clientPid, const char* clientPackage, |
| 580 | const char* reason); |
| 581 | |
| 582 | /** |
| 583 | * Add an event log message that the current device user has been switched. |
| 584 | */ |
| 585 | void logUserSwitch(int oldUserId, int newUserId); |
| 586 | |
| 587 | /** |
| 588 | * Add an event log message that a device has been removed by the HAL |
| 589 | */ |
| 590 | void logDeviceRemoved(const char* cameraId, const char* reason); |
| 591 | |
| 592 | /** |
| 593 | * Add an event log message that a device has been added by the HAL |
| 594 | */ |
| 595 | void logDeviceAdded(const char* cameraId, const char* reason); |
| 596 | |
| 597 | /** |
| 598 | * Add an event log message that a client has unexpectedly died. |
| 599 | */ |
| 600 | void logClientDied(int clientPid, const char* reason); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 601 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 602 | /** |
| 603 | * Add a event log message that a serious service-level error has occured |
| 604 | */ |
| 605 | void logServiceError(const char* msg, int errorCode); |
| 606 | |
| 607 | /** |
| 608 | * Dump the event log to an FD |
| 609 | */ |
| 610 | void dumpEventLog(int fd); |
| 611 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 612 | int mNumberOfCameras; |
| 613 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 614 | // sounds |
Chih-Chung Chang | ff4f55c | 2011-10-17 19:03:12 +0800 | [diff] [blame] | 615 | MediaPlayer* newMediaPlayer(const char *file); |
| 616 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 617 | Mutex mSoundLock; |
| 618 | sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS]; |
| 619 | int mSoundRef; // reference count (release all MediaPlayer when 0) |
| 620 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 621 | CameraModule* mModule; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 622 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 623 | // Guarded by mStatusListenerMutex |
| 624 | std::vector<sp<ICameraServiceListener>> mListenerList; |
| 625 | Mutex mStatusListenerLock; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 626 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 627 | /** |
| 628 | * Update the status for the given camera id (if that device exists), and broadcast the |
| 629 | * status update to all current ICameraServiceListeners if the status has changed. Any |
| 630 | * statuses in rejectedSourceStates will be ignored. |
| 631 | * |
| 632 | * This method must be idempotent. |
| 633 | * This method acquires mStatusLock and mStatusListenerLock. |
| 634 | */ |
| 635 | void updateStatus(ICameraServiceListener::Status status, const String8& cameraId, |
| 636 | std::initializer_list<ICameraServiceListener::Status> rejectedSourceStates); |
| 637 | void updateStatus(ICameraServiceListener::Status status, const String8& cameraId); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 638 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 639 | // flashlight control |
| 640 | sp<CameraFlashlight> mFlashlight; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 641 | // guard mTorchStatusMap |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 642 | Mutex mTorchStatusMutex; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 643 | // guard mTorchClientMap |
| 644 | Mutex mTorchClientMapMutex; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 645 | // camera id -> torch status |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 646 | KeyedVector<String8, ICameraServiceListener::TorchStatus> mTorchStatusMap; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 647 | // camera id -> torch client binder |
| 648 | // only store the last client that turns on each camera's torch mode |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 649 | KeyedVector<String8, sp<IBinder> > mTorchClientMap; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 650 | |
| 651 | // check and handle if torch client's process has died |
| 652 | void handleTorchClientBinderDied(const wp<IBinder> &who); |
| 653 | |
| 654 | // handle torch mode status change and invoke callbacks. mTorchStatusMutex |
| 655 | // should be locked. |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 656 | void onTorchStatusChangedLocked(const String8& cameraId, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 657 | ICameraServiceListener::TorchStatus newStatus); |
| 658 | |
| 659 | // get a camera's torch status. mTorchStatusMutex should be locked. |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 660 | status_t getTorchStatusLocked(const String8 &cameraId, |
| 661 | ICameraServiceListener::TorchStatus *status) const; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 662 | |
| 663 | // set a camera's torch status. mTorchStatusMutex should be locked. |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 664 | status_t setTorchStatusLocked(const String8 &cameraId, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 665 | ICameraServiceListener::TorchStatus status); |
| 666 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 667 | // IBinder::DeathRecipient implementation |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 668 | virtual void binderDied(const wp<IBinder> &who); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 669 | |
| 670 | // Helpers |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 671 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 672 | bool setUpVendorTags(); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 673 | |
| 674 | /** |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 675 | * Initialize and cache the metadata used by the HAL1 shim for a given cameraId. |
| 676 | * |
| 677 | * Returns OK on success, or a negative error code. |
| 678 | */ |
| 679 | status_t initializeShimMetadata(int cameraId); |
| 680 | |
| 681 | /** |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 682 | * Get the cached CameraParameters for the camera. If they haven't been |
| 683 | * cached yet, then initialize them for the first time. |
| 684 | * |
| 685 | * Returns OK on success, or a negative error code. |
| 686 | */ |
| 687 | status_t getLegacyParametersLazy(int cameraId, /*out*/CameraParameters* parameters); |
| 688 | |
| 689 | /** |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 690 | * Generate the CameraCharacteristics metadata required by the Camera2 API |
| 691 | * from the available HAL1 CameraParameters and CameraInfo. |
| 692 | * |
| 693 | * Returns OK on success, or a negative error code. |
| 694 | */ |
| 695 | status_t generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo); |
| 696 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 697 | static int getCallingPid(); |
| 698 | |
| 699 | static int getCallingUid(); |
| 700 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 701 | /** |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 702 | * Get the current system time as a formatted string. |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 703 | */ |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 704 | static String8 getFormattedCurrentTime(); |
| 705 | |
| 706 | /** |
| 707 | * Get the camera eviction priority from the current process state given by ActivityManager. |
| 708 | */ |
| 709 | static int getCameraPriorityFromProcState(int procState); |
| 710 | |
| 711 | static status_t makeClient(const sp<CameraService>& cameraService, |
| 712 | const sp<IInterface>& cameraCb, const String16& packageName, const String8& cameraId, |
| 713 | int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode, |
| 714 | int halVersion, int deviceVersion, apiLevel effectiveApiLevel, |
| 715 | /*out*/sp<BasicClient>* client); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 716 | }; |
| 717 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 718 | template<class Func> |
| 719 | void CameraService::CameraState::updateStatus(ICameraServiceListener::Status status, |
| 720 | const String8& cameraId, |
| 721 | std::initializer_list<ICameraServiceListener::Status> rejectSourceStates, |
| 722 | Func onStatusUpdatedLocked) { |
| 723 | Mutex::Autolock lock(mStatusLock); |
| 724 | ICameraServiceListener::Status oldStatus = mStatus; |
| 725 | mStatus = status; |
| 726 | |
| 727 | if (oldStatus == status) { |
| 728 | return; |
| 729 | } |
| 730 | |
| 731 | ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__, |
| 732 | cameraId.string(), oldStatus, status); |
| 733 | |
| 734 | if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT && |
| 735 | (status != ICameraServiceListener::STATUS_PRESENT && |
| 736 | status != ICameraServiceListener::STATUS_ENUMERATING)) { |
| 737 | |
| 738 | ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING", |
| 739 | __FUNCTION__); |
| 740 | mStatus = oldStatus; |
| 741 | return; |
| 742 | } |
| 743 | |
| 744 | /** |
| 745 | * Sometimes we want to conditionally do a transition. |
| 746 | * For example if a client disconnects, we want to go to PRESENT |
| 747 | * only if we weren't already in NOT_PRESENT or ENUMERATING. |
| 748 | */ |
| 749 | for (auto& rejectStatus : rejectSourceStates) { |
| 750 | if (oldStatus == rejectStatus) { |
| 751 | ALOGV("%s: Rejecting status transition for Camera ID %s, since the source " |
| 752 | "state was was in one of the bad states.", __FUNCTION__, cameraId.string()); |
| 753 | mStatus = oldStatus; |
| 754 | return; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | onStatusUpdatedLocked(cameraId, status); |
| 759 | } |
| 760 | |
| 761 | |
| 762 | template<class CALLBACK, class CLIENT> |
| 763 | status_t CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame^] | 764 | int halVersion, const String16& clientPackageName, int clientUid, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 765 | apiLevel effectiveApiLevel, bool legacyMode, bool shimUpdateOnly, |
| 766 | /*out*/sp<CLIENT>& device) { |
| 767 | status_t ret = NO_ERROR; |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame^] | 768 | String8 clientName8(clientPackageName); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 769 | int clientPid = getCallingPid(); |
| 770 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 771 | ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) for HAL version %s and " |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 772 | "Camera API version %d", clientPid, clientName8.string(), cameraId.string(), |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 773 | (halVersion == -1) ? "default" : std::to_string(halVersion).c_str(), |
| 774 | static_cast<int>(effectiveApiLevel)); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 775 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 776 | sp<CLIENT> client = nullptr; |
| 777 | { |
| 778 | // Acquire mServiceLock and prevent other clients from connecting |
| 779 | std::unique_ptr<AutoConditionLock> lock = |
| 780 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 781 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 782 | if (lock == nullptr) { |
| 783 | ALOGE("CameraService::connect X (PID %d) rejected (too many other clients connecting)." |
| 784 | , clientPid); |
| 785 | return -EBUSY; |
| 786 | } |
| 787 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 788 | // Enforce client permissions and do basic sanity checks |
| 789 | if((ret = validateConnectLocked(cameraId, /*inout*/clientUid)) != NO_ERROR) { |
| 790 | return ret; |
| 791 | } |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 792 | int userId = multiuser_get_user_id(clientUid); |
| 793 | |
| 794 | if (userId != mLastUserId && clientPid != getpid() ) { |
| 795 | // If no previous user ID had been set, set to the user of the caller. |
| 796 | logUserSwitch(mLastUserId, userId); |
| 797 | LOG_ALWAYS_FATAL_IF(mLastUserId != DEFAULT_LAST_USER_ID, |
| 798 | "Invalid state: Should never update user ID here unless was default"); |
| 799 | mLastUserId = userId; |
| 800 | } |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 801 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 802 | // Check the shim parameters after acquiring lock, if they have already been updated and |
| 803 | // we were doing a shim update, return immediately |
| 804 | if (shimUpdateOnly) { |
| 805 | auto cameraState = getCameraState(cameraId); |
| 806 | if (cameraState != nullptr) { |
| 807 | if (!cameraState->getShimParams().isEmpty()) return NO_ERROR; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | sp<BasicClient> clientTmp = nullptr; |
| 812 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial; |
| 813 | if ((ret = handleEvictionsLocked(cameraId, clientPid, effectiveApiLevel, |
| 814 | IInterface::asBinder(cameraCb), clientName8, /*out*/&clientTmp, |
| 815 | /*out*/&partial)) != NO_ERROR) { |
| 816 | return ret; |
| 817 | } |
| 818 | |
| 819 | if (clientTmp.get() != nullptr) { |
| 820 | // Handle special case for API1 MediaRecorder where the existing client is returned |
| 821 | device = static_cast<CLIENT*>(clientTmp.get()); |
| 822 | return NO_ERROR; |
| 823 | } |
| 824 | |
| 825 | // give flashlight a chance to close devices if necessary. |
| 826 | mFlashlight->prepareDeviceOpen(cameraId); |
| 827 | |
| 828 | // TODO: Update getDeviceVersion + HAL interface to use strings for Camera IDs |
| 829 | int id = cameraIdToInt(cameraId); |
| 830 | if (id == -1) { |
| 831 | ALOGE("%s: Invalid camera ID %s, cannot get device version from HAL.", __FUNCTION__, |
| 832 | cameraId.string()); |
| 833 | return BAD_VALUE; |
| 834 | } |
| 835 | |
| 836 | int facing = -1; |
| 837 | int deviceVersion = getDeviceVersion(id, /*out*/&facing); |
| 838 | sp<BasicClient> tmp = nullptr; |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame^] | 839 | if((ret = makeClient(this, cameraCb, clientPackageName, cameraId, facing, clientPid, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 840 | clientUid, getpid(), legacyMode, halVersion, deviceVersion, effectiveApiLevel, |
| 841 | /*out*/&tmp)) != NO_ERROR) { |
| 842 | return ret; |
| 843 | } |
| 844 | client = static_cast<CLIENT*>(tmp.get()); |
| 845 | |
| 846 | LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state", |
| 847 | __FUNCTION__); |
| 848 | |
| 849 | if ((ret = client->initialize(mModule)) != OK) { |
| 850 | ALOGE("%s: Could not initialize client from HAL module.", __FUNCTION__); |
| 851 | return ret; |
| 852 | } |
| 853 | |
| 854 | sp<IBinder> remoteCallback = client->getRemote(); |
| 855 | if (remoteCallback != nullptr) { |
| 856 | remoteCallback->linkToDeath(this); |
| 857 | } |
| 858 | |
| 859 | // Update shim paremeters for legacy clients |
| 860 | if (effectiveApiLevel == API_1) { |
| 861 | // Assume we have always received a Client subclass for API1 |
| 862 | sp<Client> shimClient = reinterpret_cast<Client*>(client.get()); |
| 863 | String8 rawParams = shimClient->getParameters(); |
| 864 | CameraParameters params(rawParams); |
| 865 | |
| 866 | auto cameraState = getCameraState(cameraId); |
| 867 | if (cameraState != nullptr) { |
| 868 | cameraState->setShimParams(params); |
| 869 | } else { |
| 870 | ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.", |
| 871 | __FUNCTION__, cameraId.string()); |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | if (shimUpdateOnly) { |
| 876 | // If only updating legacy shim parameters, immediately disconnect client |
| 877 | mServiceLock.unlock(); |
| 878 | client->disconnect(); |
| 879 | mServiceLock.lock(); |
| 880 | } else { |
| 881 | // Otherwise, add client to active clients list |
| 882 | finishConnectLocked(client, partial); |
| 883 | } |
| 884 | } // lock is destroyed, allow further connect calls |
| 885 | |
| 886 | // Important: release the mutex here so the client can call back into the service from its |
| 887 | // destructor (can be at the end of the call) |
| 888 | device = client; |
| 889 | return NO_ERROR; |
| 890 | } |
| 891 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 892 | } // namespace android |
| 893 | |
| 894 | #endif |