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