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 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 20 | #include <utils/Vector.h> |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 21 | #include <utils/KeyedVector.h> |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 22 | #include <binder/AppOpsManager.h> |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 23 | #include <binder/BinderService.h> |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 24 | #include <binder/IAppOpsCallback.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 25 | #include <camera/ICameraService.h> |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 26 | #include <hardware/camera.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 27 | |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 28 | #include <camera/ICamera.h> |
| 29 | #include <camera/ICameraClient.h> |
| 30 | #include <camera/IProCameraUser.h> |
| 31 | #include <camera/IProCameraCallbacks.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 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 41 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 42 | #include "common/CameraModule.h" |
| 43 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 44 | /* This needs to be increased if we can have more cameras */ |
| 45 | #define MAX_CAMERAS 2 |
| 46 | |
| 47 | namespace android { |
| 48 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 49 | extern volatile int32_t gLogLevel; |
| 50 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 51 | class MemoryHeapBase; |
| 52 | class MediaPlayer; |
| 53 | |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 54 | class CameraService : |
| 55 | public BinderService<CameraService>, |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 56 | public BnCameraService, |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 57 | public IBinder::DeathRecipient, |
| 58 | public camera_module_callbacks_t |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 59 | { |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 60 | friend class BinderService<CameraService>; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 61 | public: |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 62 | class Client; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 63 | class BasicClient; |
| 64 | |
| 65 | // Implementation of BinderService<T> |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 66 | static char const* getServiceName() { return "media.camera"; } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 67 | |
| 68 | CameraService(); |
| 69 | virtual ~CameraService(); |
| 70 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 71 | ///////////////////////////////////////////////////////////////////// |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 72 | // HAL Callbacks |
| 73 | virtual void onDeviceStatusChanged(int cameraId, |
| 74 | int newStatus); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 75 | virtual void onTorchStatusChanged(const String8& cameraId, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 76 | ICameraServiceListener::TorchStatus |
| 77 | newStatus); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 78 | |
| 79 | ///////////////////////////////////////////////////////////////////// |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 80 | // ICameraService |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 81 | virtual int32_t getNumberOfCameras(); |
| 82 | virtual status_t getCameraInfo(int cameraId, |
| 83 | struct CameraInfo* cameraInfo); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 84 | virtual status_t getCameraCharacteristics(int cameraId, |
| 85 | CameraMetadata* cameraInfo); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 86 | virtual status_t getCameraVendorTagDescriptor(/*out*/ sp<VendorTagDescriptor>& desc); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 87 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 88 | virtual status_t connect(const sp<ICameraClient>& cameraClient, int cameraId, |
| 89 | const String16& clientPackageName, int clientUid, |
| 90 | /*out*/ |
| 91 | sp<ICamera>& device); |
| 92 | |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 93 | virtual status_t connectLegacy(const sp<ICameraClient>& cameraClient, int cameraId, |
| 94 | int halVersion, const String16& clientPackageName, int clientUid, |
| 95 | /*out*/ |
| 96 | sp<ICamera>& device); |
| 97 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 98 | virtual status_t connectPro(const sp<IProCameraCallbacks>& cameraCb, |
| 99 | int cameraId, const String16& clientPackageName, int clientUid, |
| 100 | /*out*/ |
| 101 | sp<IProCameraUser>& device); |
| 102 | |
| 103 | virtual status_t connectDevice( |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 104 | const sp<ICameraDeviceCallbacks>& cameraCb, |
| 105 | int cameraId, |
| 106 | const String16& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 107 | int clientUid, |
| 108 | /*out*/ |
| 109 | sp<ICameraDeviceUser>& device); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 110 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 111 | virtual status_t addListener(const sp<ICameraServiceListener>& listener); |
| 112 | virtual status_t removeListener( |
| 113 | const sp<ICameraServiceListener>& listener); |
| 114 | |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 115 | virtual status_t getLegacyParameters( |
| 116 | int cameraId, |
| 117 | /*out*/ |
| 118 | String16* parameters); |
| 119 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 120 | virtual status_t setTorchMode(const String16& cameraId, bool enabled, |
| 121 | const sp<IBinder>& clientBinder); |
| 122 | |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 123 | // OK = supports api of that version, -EOPNOTSUPP = does not support |
| 124 | virtual status_t supportsCameraApi( |
| 125 | int cameraId, int apiVersion); |
| 126 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 127 | // Extra permissions checks |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 128 | virtual status_t onTransact(uint32_t code, const Parcel& data, |
| 129 | Parcel* reply, uint32_t flags); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 130 | |
| 131 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 132 | |
| 133 | ///////////////////////////////////////////////////////////////////// |
| 134 | // Client functionality |
| 135 | virtual void removeClientByRemote(const wp<IBinder>& remoteBinder); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 136 | |
| 137 | enum sound_kind { |
| 138 | SOUND_SHUTTER = 0, |
| 139 | SOUND_RECORDING = 1, |
| 140 | NUM_SOUNDS |
| 141 | }; |
| 142 | |
| 143 | void loadSound(); |
| 144 | void playSound(sound_kind kind); |
| 145 | void releaseSound(); |
| 146 | |
Igor Murashkin | 98e2472 | 2013-06-19 19:51:04 -0700 | [diff] [blame] | 147 | ///////////////////////////////////////////////////////////////////// |
| 148 | // CameraDeviceFactory functionality |
| 149 | int getDeviceVersion(int cameraId, int* facing = NULL); |
| 150 | |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 151 | ///////////////////////////////////////////////////////////////////// |
| 152 | // Shared utilities |
| 153 | static status_t filterOpenErrorCode(status_t err); |
| 154 | static status_t filterGetInfoErrorCode(status_t err); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 155 | |
| 156 | ///////////////////////////////////////////////////////////////////// |
| 157 | // CameraClient functionality |
| 158 | |
| 159 | // returns plain pointer of client. Note that mClientLock should be acquired to |
| 160 | // prevent the client from destruction. The result can be NULL. |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 161 | virtual BasicClient* getClientByIdUnsafe(int cameraId); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 162 | virtual Mutex* getClientLockById(int cameraId); |
| 163 | |
| 164 | class BasicClient : public virtual RefBase { |
| 165 | public: |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 166 | virtual status_t initialize(CameraModule *module) = 0; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 167 | virtual void disconnect(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 168 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 169 | // because we can't virtually inherit IInterface, which breaks |
| 170 | // virtual inheritance |
| 171 | virtual sp<IBinder> asBinderWrapper() = 0; |
| 172 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 173 | // Return the remote callback binder object (e.g. IProCameraCallbacks) |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 174 | sp<IBinder> getRemote() { |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 175 | return mRemoteBinder; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 178 | virtual status_t dump(int fd, const Vector<String16>& args) = 0; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 179 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 180 | protected: |
| 181 | BasicClient(const sp<CameraService>& cameraService, |
| 182 | const sp<IBinder>& remoteCallback, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 183 | const String16& clientPackageName, |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 184 | int cameraId, |
| 185 | int cameraFacing, |
| 186 | int clientPid, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 187 | uid_t clientUid, |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 188 | int servicePid); |
| 189 | |
| 190 | virtual ~BasicClient(); |
| 191 | |
| 192 | // the instance is in the middle of destruction. When this is set, |
| 193 | // the instance should not be accessed from callback. |
| 194 | // CameraService's mClientLock should be acquired to access this. |
| 195 | // - subclasses should set this to true in their destructors. |
| 196 | bool mDestructionStarted; |
| 197 | |
| 198 | // these are initialized in the constructor. |
| 199 | sp<CameraService> mCameraService; // immutable after constructor |
| 200 | int mCameraId; // immutable after constructor |
| 201 | int mCameraFacing; // immutable after constructor |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 202 | const String16 mClientPackageName; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 203 | pid_t mClientPid; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 204 | uid_t mClientUid; // immutable after constructor |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 205 | pid_t mServicePid; // immutable after constructor |
| 206 | |
| 207 | // - The app-side Binder interface to receive callbacks from us |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 208 | sp<IBinder> mRemoteBinder; // immutable after constructor |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 209 | |
| 210 | // permissions management |
| 211 | status_t startCameraOps(); |
| 212 | status_t finishCameraOps(); |
| 213 | |
| 214 | // Notify client about a fatal error |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 215 | virtual void notifyError( |
| 216 | ICameraDeviceCallbacks::CameraErrorCode errorCode, |
| 217 | const CaptureResultExtras& resultExtras) = 0; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 218 | private: |
| 219 | AppOpsManager mAppOpsManager; |
| 220 | |
| 221 | class OpsCallback : public BnAppOpsCallback { |
| 222 | public: |
| 223 | OpsCallback(wp<BasicClient> client); |
| 224 | virtual void opChanged(int32_t op, const String16& packageName); |
| 225 | |
| 226 | private: |
| 227 | wp<BasicClient> mClient; |
| 228 | |
| 229 | }; // class OpsCallback |
| 230 | |
| 231 | sp<OpsCallback> mOpsCallback; |
| 232 | // Track whether startCameraOps was called successfully, to avoid |
| 233 | // finishing what we didn't start. |
| 234 | bool mOpsActive; |
| 235 | |
| 236 | // IAppOpsCallback interface, indirected through opListener |
| 237 | virtual void opChanged(int32_t op, const String16& packageName); |
| 238 | }; // class BasicClient |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 239 | |
| 240 | class Client : public BnCamera, public BasicClient |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 241 | { |
| 242 | public: |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 243 | typedef ICameraClient TCamCallbacks; |
| 244 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 245 | // ICamera interface (see ICamera for details) |
| 246 | virtual void disconnect(); |
| 247 | virtual status_t connect(const sp<ICameraClient>& client) = 0; |
| 248 | virtual status_t lock() = 0; |
| 249 | virtual status_t unlock() = 0; |
Eino-Ville Talvala | 1ce7c34 | 2013-08-21 13:57:21 -0700 | [diff] [blame] | 250 | virtual status_t setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)=0; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 251 | virtual void setPreviewCallbackFlag(int flag) = 0; |
Eino-Ville Talvala | 3ee3550 | 2013-04-02 15:45:11 -0700 | [diff] [blame] | 252 | virtual status_t setPreviewCallbackTarget( |
| 253 | const sp<IGraphicBufferProducer>& callbackProducer) = 0; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 254 | virtual status_t startPreview() = 0; |
| 255 | virtual void stopPreview() = 0; |
| 256 | virtual bool previewEnabled() = 0; |
| 257 | virtual status_t storeMetaDataInBuffers(bool enabled) = 0; |
| 258 | virtual status_t startRecording() = 0; |
| 259 | virtual void stopRecording() = 0; |
| 260 | virtual bool recordingEnabled() = 0; |
| 261 | virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0; |
| 262 | virtual status_t autoFocus() = 0; |
| 263 | virtual status_t cancelAutoFocus() = 0; |
| 264 | virtual status_t takePicture(int msgType) = 0; |
| 265 | virtual status_t setParameters(const String8& params) = 0; |
| 266 | virtual String8 getParameters() const = 0; |
| 267 | virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0; |
| 268 | |
| 269 | // Interface used by CameraService |
| 270 | Client(const sp<CameraService>& cameraService, |
| 271 | const sp<ICameraClient>& cameraClient, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 272 | const String16& clientPackageName, |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 273 | int cameraId, |
| 274 | int cameraFacing, |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 275 | int clientPid, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 276 | uid_t clientUid, |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 277 | int servicePid); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 278 | ~Client(); |
| 279 | |
| 280 | // return our camera client |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 281 | const sp<ICameraClient>& getRemoteCallback() { |
| 282 | return mRemoteCallback; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 285 | virtual sp<IBinder> asBinderWrapper() { |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 286 | return asBinder(this); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 289 | protected: |
| 290 | static Mutex* getClientLockFromCookie(void* user); |
| 291 | // convert client from cookie. Client lock should be acquired before getting Client. |
| 292 | static Client* getClientFromCookie(void* user); |
| 293 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 294 | virtual void notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode, |
| 295 | const CaptureResultExtras& resultExtras); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 296 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 297 | // Initialized in constructor |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 298 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 299 | // - The app-side Binder interface to receive callbacks from us |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 300 | sp<ICameraClient> mRemoteCallback; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 301 | |
| 302 | }; // class Client |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 303 | |
| 304 | class ProClient : public BnProCameraUser, public BasicClient { |
| 305 | public: |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 306 | typedef IProCameraCallbacks TCamCallbacks; |
| 307 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 308 | ProClient(const sp<CameraService>& cameraService, |
| 309 | const sp<IProCameraCallbacks>& remoteCallback, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 310 | const String16& clientPackageName, |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 311 | int cameraId, |
| 312 | int cameraFacing, |
| 313 | int clientPid, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 314 | uid_t clientUid, |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 315 | int servicePid); |
| 316 | |
| 317 | virtual ~ProClient(); |
| 318 | |
| 319 | const sp<IProCameraCallbacks>& getRemoteCallback() { |
| 320 | return mRemoteCallback; |
| 321 | } |
| 322 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 323 | /*** |
| 324 | IProCamera implementation |
| 325 | ***/ |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 326 | virtual status_t connect(const sp<IProCameraCallbacks>& callbacks) |
| 327 | = 0; |
| 328 | virtual status_t exclusiveTryLock() = 0; |
| 329 | virtual status_t exclusiveLock() = 0; |
| 330 | virtual status_t exclusiveUnlock() = 0; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 331 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 332 | virtual bool hasExclusiveLock() = 0; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 333 | |
| 334 | // Note that the callee gets a copy of the metadata. |
| 335 | virtual int submitRequest(camera_metadata_t* metadata, |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 336 | bool streaming = false) = 0; |
| 337 | virtual status_t cancelRequest(int requestId) = 0; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 338 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 339 | // Callbacks from camera service |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 340 | virtual void onExclusiveLockStolen() = 0; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 341 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 342 | protected: |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 343 | virtual void notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode, |
| 344 | const CaptureResultExtras& resultExtras); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 345 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 346 | sp<IProCameraCallbacks> mRemoteCallback; |
| 347 | }; // class ProClient |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 348 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 349 | private: |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 350 | |
| 351 | // Delay-load the Camera HAL module |
| 352 | virtual void onFirstRef(); |
| 353 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 354 | // Step 1. Check if we can connect, before we acquire the service lock. |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 355 | status_t validateConnect(int cameraId, |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 356 | /*inout*/ |
| 357 | int& clientUid) const; |
| 358 | |
| 359 | // Step 2. Check if we can connect, after we acquire the service lock. |
| 360 | bool canConnectUnsafe(int cameraId, |
| 361 | const String16& clientPackageName, |
| 362 | const sp<IBinder>& remoteCallback, |
| 363 | /*out*/ |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 364 | sp<BasicClient> &client); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 365 | |
| 366 | // When connection is successful, initialize client and track its death |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 367 | status_t connectFinishUnsafe(const sp<BasicClient>& client, |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 368 | const sp<IBinder>& remoteCallback); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 369 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 370 | virtual sp<BasicClient> getClientByRemote(const wp<IBinder>& cameraClient); |
| 371 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 372 | Mutex mServiceLock; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 373 | // either a Client or CameraDeviceClient |
| 374 | wp<BasicClient> mClient[MAX_CAMERAS]; // protected by mServiceLock |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 375 | Mutex mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 376 | int mNumberOfCameras; |
| 377 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 378 | typedef wp<ProClient> weak_pro_client_ptr; |
| 379 | Vector<weak_pro_client_ptr> mProClientList[MAX_CAMERAS]; |
| 380 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 381 | // needs to be called with mServiceLock held |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 382 | sp<BasicClient> findClientUnsafe(const wp<IBinder>& cameraClient, int& outIndex); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 383 | sp<ProClient> findProClientUnsafe( |
| 384 | const wp<IBinder>& cameraCallbacksRemote); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 385 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 386 | // atomics to record whether the hardware is allocated to some client. |
| 387 | volatile int32_t mBusy[MAX_CAMERAS]; |
| 388 | void setCameraBusy(int cameraId); |
| 389 | void setCameraFree(int cameraId); |
| 390 | |
| 391 | // sounds |
Chih-Chung Chang | ff4f55c | 2011-10-17 19:03:12 +0800 | [diff] [blame] | 392 | MediaPlayer* newMediaPlayer(const char *file); |
| 393 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 394 | Mutex mSoundLock; |
| 395 | sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS]; |
| 396 | int mSoundRef; // reference count (release all MediaPlayer when 0) |
| 397 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 398 | CameraModule* mModule; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 399 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 400 | Vector<sp<ICameraServiceListener> > |
| 401 | mListenerList; |
| 402 | |
| 403 | // guard only mStatusList and the broadcasting of ICameraServiceListener |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 404 | mutable Mutex mStatusMutex; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 405 | ICameraServiceListener::Status |
| 406 | mStatusList[MAX_CAMERAS]; |
| 407 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 408 | // Read the current status (locks mStatusMutex) |
| 409 | ICameraServiceListener::Status |
| 410 | getStatus(int cameraId) const; |
| 411 | |
Igor Murashkin | 93747b9 | 2013-05-01 15:42:20 -0700 | [diff] [blame] | 412 | typedef Vector<ICameraServiceListener::Status> StatusVector; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 413 | // Broadcast the new status if it changed (locks the service mutex) |
| 414 | void updateStatus( |
| 415 | ICameraServiceListener::Status status, |
Igor Murashkin | 93747b9 | 2013-05-01 15:42:20 -0700 | [diff] [blame] | 416 | int32_t cameraId, |
| 417 | const StatusVector *rejectSourceStates = NULL); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 418 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 419 | // flashlight control |
| 420 | sp<CameraFlashlight> mFlashlight; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 421 | // guard mTorchStatusMap |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 422 | Mutex mTorchStatusMutex; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 423 | // guard mTorchClientMap |
| 424 | Mutex mTorchClientMapMutex; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 425 | // camera id -> torch status |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 426 | KeyedVector<String8, ICameraServiceListener::TorchStatus> mTorchStatusMap; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 427 | // camera id -> torch client binder |
| 428 | // only store the last client that turns on each camera's torch mode |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 429 | KeyedVector<String8, sp<IBinder> > mTorchClientMap; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 430 | |
| 431 | // check and handle if torch client's process has died |
| 432 | void handleTorchClientBinderDied(const wp<IBinder> &who); |
| 433 | |
| 434 | // handle torch mode status change and invoke callbacks. mTorchStatusMutex |
| 435 | // should be locked. |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 436 | void onTorchStatusChangedLocked(const String8& cameraId, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 437 | ICameraServiceListener::TorchStatus newStatus); |
| 438 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 439 | // validate the camera id for use of setting a torch mode. |
| 440 | bool validCameraIdForSetTorchMode(const String8& cameraId); |
| 441 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 442 | // get a camera's torch status. mTorchStatusMutex should be locked. |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 443 | status_t getTorchStatusLocked(const String8 &cameraId, |
| 444 | ICameraServiceListener::TorchStatus *status) const; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 445 | |
| 446 | // set a camera's torch status. mTorchStatusMutex should be locked. |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 447 | status_t setTorchStatusLocked(const String8 &cameraId, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 448 | ICameraServiceListener::TorchStatus status); |
| 449 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 450 | // IBinder::DeathRecipient implementation |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 451 | virtual void binderDied(const wp<IBinder> &who); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 452 | |
| 453 | // Helpers |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 454 | |
| 455 | bool isValidCameraId(int cameraId); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 456 | |
| 457 | bool setUpVendorTags(); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 458 | |
| 459 | /** |
| 460 | * A mapping of camera ids to CameraParameters returned by that camera device. |
| 461 | * |
| 462 | * This cache is used to generate CameraCharacteristic metadata when using |
| 463 | * the HAL1 shim. |
| 464 | */ |
| 465 | KeyedVector<int, CameraParameters> mShimParams; |
| 466 | |
| 467 | /** |
| 468 | * Initialize and cache the metadata used by the HAL1 shim for a given cameraId. |
| 469 | * |
| 470 | * Returns OK on success, or a negative error code. |
| 471 | */ |
| 472 | status_t initializeShimMetadata(int cameraId); |
| 473 | |
| 474 | /** |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 475 | * Get the cached CameraParameters for the camera. If they haven't been |
| 476 | * cached yet, then initialize them for the first time. |
| 477 | * |
| 478 | * Returns OK on success, or a negative error code. |
| 479 | */ |
| 480 | status_t getLegacyParametersLazy(int cameraId, /*out*/CameraParameters* parameters); |
| 481 | |
| 482 | /** |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 483 | * Generate the CameraCharacteristics metadata required by the Camera2 API |
| 484 | * from the available HAL1 CameraParameters and CameraInfo. |
| 485 | * |
| 486 | * Returns OK on success, or a negative error code. |
| 487 | */ |
| 488 | status_t generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo); |
| 489 | |
| 490 | /** |
| 491 | * Connect a new camera client. This should only be used while holding the |
| 492 | * mutex for mServiceLock. |
| 493 | * |
| 494 | * Returns OK on success, or a negative error code. |
| 495 | */ |
Igor Murashkin | a858ea0 | 2014-08-19 14:53:08 -0700 | [diff] [blame] | 496 | status_t connectHelperLocked( |
| 497 | /*out*/ |
| 498 | sp<Client>& client, |
| 499 | /*in*/ |
| 500 | const sp<ICameraClient>& cameraClient, |
| 501 | int cameraId, |
| 502 | const String16& clientPackageName, |
| 503 | int clientUid, |
| 504 | int callingPid, |
| 505 | int halVersion = CAMERA_HAL_API_VERSION_UNSPECIFIED, |
| 506 | bool legacyMode = false); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 507 | }; |
| 508 | |
| 509 | } // namespace android |
| 510 | |
| 511 | #endif |