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