blob: af35709e97e608853290149d0a7ae8772cb85317 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
Ruben Brunkd1176ef2014-02-21 10:51:38 -08002 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Mathias Agopian65ab4712010-07-14 17:59:35 -070016
17#ifndef ANDROID_SERVERS_CAMERA_CAMERASERVICE_H
18#define ANDROID_SERVERS_CAMERA_CAMERASERVICE_H
19
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080020#include <android/hardware/BnCameraService.h>
Michael Grooverd1d435a2018-12-18 17:39:42 -080021#include <android/hardware/BnSensorPrivacyListener.h>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080022#include <android/hardware/ICameraServiceListener.h>
malikakash82ed4352023-07-21 22:44:34 +000023#include <android/hardware/CameraIdRemapping.h>
Cliff Wud8cae102021-03-11 01:37:42 +080024#include <android/hardware/camera2/BnCameraInjectionSession.h>
25#include <android/hardware/camera2/ICameraInjectionCallback.h>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080026
Ruben Brunk36597b22015-03-20 22:15:57 -070027#include <cutils/multiuser.h>
Igor Murashkin634a5152013-02-20 17:15:11 -080028#include <utils/Vector.h>
Ruben Brunkb2119af2014-05-09 19:57:56 -070029#include <utils/KeyedVector.h>
Steven Moreland2f348142019-07-02 15:59:07 -070030#include <binder/ActivityManager.h>
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080031#include <binder/AppOpsManager.h>
Mathias Agopian5462fc92010-07-14 18:41:18 -070032#include <binder/BinderService.h>
Kunal Malhotrabfc96052023-02-28 23:25:34 +000033#include <binder/IServiceManager.h>
34#include <binder/IActivityManager.h>
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080035#include <binder/IAppOpsCallback.h>
Svet Ganova453d0d2018-01-11 15:37:58 -080036#include <binder/IUidObserver.h>
Iliyan Malchev8951a972011-04-14 16:55:59 -070037#include <hardware/camera.h>
Steven Moreland3cf67172020-01-29 11:44:22 -080038#include <sensorprivacy/SensorPrivacyManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070039
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080040#include <android/hardware/camera/common/1.0/types.h>
41
Ruben Brunkd1176ef2014-02-21 10:51:38 -080042#include <camera/VendorTagDescriptor.h>
Jianing Weicb0652e2014-03-12 18:29:36 -070043#include <camera/CaptureResult.h>
Ruben Brunkb2119af2014-05-09 19:57:56 -070044#include <camera/CameraParameters.h>
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -080045#include <camera/camera2/ConcurrentCamera.h>
Igor Murashkinc073ba52013-02-26 14:32:34 -080046
Chien-Yu Chen3068d732015-02-09 13:29:57 -080047#include "CameraFlashlight.h"
48
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080049#include "common/CameraProviderManager.h"
Ronghua Wu022ed722015-05-11 15:15:09 -070050#include "media/RingBuffer.h"
Ruben Brunkcc776712015-02-17 20:18:47 -080051#include "utils/AutoConditionLock.h"
52#include "utils/ClientManager.h"
Jayant Chowdharyffc5d682022-05-12 18:34:34 +000053#include "utils/IPCTransport.h"
Austin Borger74fca042022-05-23 12:41:21 -070054#include "utils/CameraServiceProxyWrapper.h"
Yin-Chia Yehe074a932015-01-30 10:29:02 -080055
Ruben Brunkcc776712015-02-17 20:18:47 -080056#include <set>
57#include <string>
Jayant Chowdhary5e2cd302020-08-14 02:48:34 +000058#include <list>
Ruben Brunkcc776712015-02-17 20:18:47 -080059#include <map>
60#include <memory>
Jooyung Hanb3f7cd22020-01-23 12:27:18 +090061#include <optional>
Ruben Brunk99e69712015-05-26 17:25:07 -070062#include <utility>
Svet Ganova453d0d2018-01-11 15:37:58 -080063#include <unordered_map>
64#include <unordered_set>
malikakash82ed4352023-07-21 22:44:34 +000065#include <vector>
Mathias Agopian65ab4712010-07-14 17:59:35 -070066
67namespace android {
68
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070069extern volatile int32_t gLogLevel;
70
Mathias Agopian65ab4712010-07-14 17:59:35 -070071class MemoryHeapBase;
72class MediaPlayer;
73
Mathias Agopian5462fc92010-07-14 18:41:18 -070074class CameraService :
75 public BinderService<CameraService>,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080076 public virtual ::android::hardware::BnCameraService,
77 public virtual IBinder::DeathRecipient,
Jyoti Bhayanacde601c2022-12-07 10:03:42 -080078 public virtual CameraProviderManager::StatusListener,
79 public virtual IServiceManager::LocalRegistrationCallback
Mathias Agopian65ab4712010-07-14 17:59:35 -070080{
Mathias Agopian5462fc92010-07-14 18:41:18 -070081 friend class BinderService<CameraService>;
Emilian Peevb2bc5a42019-11-20 16:02:14 -080082 friend class CameraOfflineSessionClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -070083public:
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070084 class Client;
Igor Murashkin634a5152013-02-20 17:15:11 -080085 class BasicClient;
Yin-Chia Yehb978c382019-10-30 00:22:37 -070086 class OfflineClient;
Igor Murashkin634a5152013-02-20 17:15:11 -080087
Ruben Brunk0bbf8b22015-04-30 14:35:42 -070088 // The effective API level. The Camera2 API running in LEGACY mode counts as API_1.
Ruben Brunkcc776712015-02-17 20:18:47 -080089 enum apiLevel {
90 API_1 = 1,
91 API_2 = 2
92 };
93
Ruben Brunkcc776712015-02-17 20:18:47 -080094 // 3 second busy timeout when other clients are connecting
95 static const nsecs_t DEFAULT_CONNECT_TIMEOUT_NS = 3000000000;
96
Ruben Brunk4f9576b2015-04-10 17:26:56 -070097 // 1 second busy timeout when other clients are disconnecting
98 static const nsecs_t DEFAULT_DISCONNECT_TIMEOUT_NS = 1000000000;
99
Ruben Brunkcc776712015-02-17 20:18:47 -0800100 // Default number of messages to store in eviction log
Ruben Brunka8ca9152015-04-07 14:23:40 -0700101 static const size_t DEFAULT_EVENT_LOG_LENGTH = 100;
Ruben Brunkcc776712015-02-17 20:18:47 -0800102
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800103 // Event log ID
104 static const int SN_EVENT_LOG_ID = 0x534e4554;
105
Charles Chena7b613c2023-01-24 21:57:33 +0000106 // Register camera service
107 static void instantiate();
108
Igor Murashkin634a5152013-02-20 17:15:11 -0800109 // Implementation of BinderService<T>
Mathias Agopian5462fc92010-07-14 18:41:18 -0700110 static char const* getServiceName() { return "media.camera"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700111
Jyoti Bhayanacde601c2022-12-07 10:03:42 -0800112 // Implementation of IServiceManager::LocalRegistrationCallback
113 virtual void onServiceRegistration(const String16& name, const sp<IBinder>& binder) override;
114
Austin Borger74fca042022-05-23 12:41:21 -0700115 // Non-null arguments for cameraServiceProxyWrapper should be provided for
116 // testing purposes only.
117 CameraService(std::shared_ptr<CameraServiceProxyWrapper>
118 cameraServiceProxyWrapper = nullptr);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700119 virtual ~CameraService();
120
Igor Murashkin634a5152013-02-20 17:15:11 -0800121 /////////////////////////////////////////////////////////////////////
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800122 // HAL Callbacks - implements CameraProviderManager::StatusListener
123
Austin Borgered99f642023-06-01 16:51:35 -0700124 virtual void onDeviceStatusChanged(const std::string &cameraId,
Jayant Chowdhary0bd38522021-11-05 17:49:27 -0700125 CameraDeviceStatus newHalStatus) override;
Austin Borgered99f642023-06-01 16:51:35 -0700126 virtual void onDeviceStatusChanged(const std::string &cameraId,
127 const std::string &physicalCameraId,
Jayant Chowdhary0bd38522021-11-05 17:49:27 -0700128 CameraDeviceStatus newHalStatus) override;
Jayant Chowdhary46ef0f52021-10-05 14:36:13 -0700129 // This method may hold CameraProviderManager::mInterfaceMutex as a part
130 // of calling getSystemCameraKind() internally. Care should be taken not to
131 // directly / indirectly call this from callers who also hold
132 // mInterfaceMutex.
Austin Borgered99f642023-06-01 16:51:35 -0700133 virtual void onTorchStatusChanged(const std::string& cameraId,
Jayant Chowdhary0bd38522021-11-05 17:49:27 -0700134 TorchModeStatus newStatus) override;
Jayant Chowdhary46ef0f52021-10-05 14:36:13 -0700135 // Does not hold CameraProviderManager::mInterfaceMutex.
Austin Borgered99f642023-06-01 16:51:35 -0700136 virtual void onTorchStatusChanged(const std::string& cameraId,
Jayant Chowdhary0bd38522021-11-05 17:49:27 -0700137 TorchModeStatus newStatus,
Jayant Chowdhary46ef0f52021-10-05 14:36:13 -0700138 SystemCameraKind kind) override;
Emilian Peevaee727d2017-05-04 16:35:48 +0100139 virtual void onNewProviderRegistered() override;
Igor Murashkincba2c162013-03-20 15:56:31 -0700140
141 /////////////////////////////////////////////////////////////////////
Igor Murashkin634a5152013-02-20 17:15:11 -0800142 // ICameraService
malikakash82ed4352023-07-21 22:44:34 +0000143 // IMPORTANT: All binder calls that deal with logicalCameraId should use
144 // resolveCameraId(logicalCameraId) to arrive at the correct cameraId to
145 // perform the operation on (in case of Id Remapping).
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800146 virtual binder::Status getNumberOfCameras(int32_t type, int32_t* numCameras);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700147
Austin Borger18b30a72022-10-27 12:20:29 -0700148 virtual binder::Status getCameraInfo(int cameraId, bool overrideToPortrait,
149 hardware::CameraInfo* cameraInfo) override;
Austin Borgered99f642023-06-01 16:51:35 -0700150 virtual binder::Status getCameraCharacteristics(const std::string& cameraId,
Austin Borger18b30a72022-10-27 12:20:29 -0700151 int targetSdkVersion, bool overrideToPortrait, CameraMetadata* cameraInfo) override;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800152 virtual binder::Status getCameraVendorTagDescriptor(
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700153 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800154 hardware::camera2::params::VendorTagDescriptor* desc);
Emilian Peev71c73a22017-03-21 16:35:51 +0000155 virtual binder::Status getCameraVendorTagCache(
156 /*out*/
157 hardware::camera2::params::VendorTagDescriptorCache* cache);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700158
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800159 virtual binder::Status connect(const sp<hardware::ICameraClient>& cameraClient,
Austin Borgered99f642023-06-01 16:51:35 -0700160 int32_t cameraId, const std::string& clientPackageName,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700161 int32_t clientUid, int clientPid, int targetSdkVersion,
Chengfei Taobe683db2023-01-31 18:52:49 +0000162 bool overrideToPortrait, bool forceSlowJpegMode,
Zhijun Heb10cdad2014-06-16 16:38:35 -0700163 /*out*/
Austin Borger18b30a72022-10-27 12:20:29 -0700164 sp<hardware::ICamera>* device) override;
Zhijun Heb10cdad2014-06-16 16:38:35 -0700165
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800166 virtual binder::Status connectDevice(
Austin Borgered99f642023-06-01 16:51:35 -0700167 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
168 const std::string& cameraId,
169 const std::string& clientPackageName, const std::optional<std::string>& clientFeatureId,
Austin Borger18b30a72022-10-27 12:20:29 -0700170 int32_t clientUid, int scoreOffset, int targetSdkVersion, bool overrideToPortrait,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800171 /*out*/
172 sp<hardware::camera2::ICameraDeviceUser>* device);
Igor Murashkinbfc99152013-02-27 12:55:20 -0800173
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800174 virtual binder::Status addListener(const sp<hardware::ICameraServiceListener>& listener,
175 /*out*/
176 std::vector<hardware::CameraStatus>* cameraStatuses);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800177 virtual binder::Status removeListener(
178 const sp<hardware::ICameraServiceListener>& listener);
179
Jayant Chowdharycad23c22020-03-10 15:04:59 -0700180 virtual binder::Status getConcurrentCameraIds(
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800181 /*out*/
182 std::vector<hardware::camera2::utils::ConcurrentCameraIdCombination>* concurrentCameraIds);
183
184 virtual binder::Status isConcurrentSessionConfigurationSupported(
185 const std::vector<hardware::camera2::utils::CameraIdAndSessionConfiguration>& sessions,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700186 int targetSdkVersion, /*out*/bool* supported);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800187
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800188 virtual binder::Status getLegacyParameters(
189 int32_t cameraId,
Igor Murashkin65d14b92014-06-17 12:03:20 -0700190 /*out*/
Austin Borgered99f642023-06-01 16:51:35 -0700191 std::string* parameters);
Igor Murashkin65d14b92014-06-17 12:03:20 -0700192
Austin Borgered99f642023-06-01 16:51:35 -0700193 virtual binder::Status setTorchMode(const std::string& cameraId, bool enabled,
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800194 const sp<IBinder>& clientBinder);
195
Austin Borgered99f642023-06-01 16:51:35 -0700196 virtual binder::Status turnOnTorchWithStrengthLevel(const std::string& cameraId,
Rucha Katakwar38284522021-11-10 11:25:21 -0800197 int32_t torchStrength, const sp<IBinder>& clientBinder);
198
Austin Borgered99f642023-06-01 16:51:35 -0700199 virtual binder::Status getTorchStrengthLevel(const std::string& cameraId,
Rucha Katakwar38284522021-11-10 11:25:21 -0800200 int32_t* torchStrength);
201
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800202 virtual binder::Status notifySystemEvent(int32_t eventId,
203 const std::vector<int32_t>& args);
Ruben Brunk36597b22015-03-20 22:15:57 -0700204
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800205 virtual binder::Status notifyDeviceStateChange(int64_t newState);
206
Emilian Peev8b64f282021-03-25 16:49:57 -0700207 virtual binder::Status notifyDisplayConfigurationChange();
208
Igor Murashkin65d14b92014-06-17 12:03:20 -0700209 // OK = supports api of that version, -EOPNOTSUPP = does not support
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800210 virtual binder::Status supportsCameraApi(
Austin Borgered99f642023-06-01 16:51:35 -0700211 const std::string& cameraId, int32_t apiVersion,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800212 /*out*/
213 bool *isSupported);
Igor Murashkin65d14b92014-06-17 12:03:20 -0700214
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700215 virtual binder::Status isHiddenPhysicalCamera(
Austin Borgered99f642023-06-01 16:51:35 -0700216 const std::string& cameraId,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700217 /*out*/
218 bool *isSupported);
219
Cliff Wud8cae102021-03-11 01:37:42 +0800220 virtual binder::Status injectCamera(
Austin Borgered99f642023-06-01 16:51:35 -0700221 const std::string& packageName, const std::string& internalCamId,
222 const std::string& externalCamId,
Cliff Wud8cae102021-03-11 01:37:42 +0800223 const sp<hardware::camera2::ICameraInjectionCallback>& callback,
224 /*out*/
225 sp<hardware::camera2::ICameraInjectionSession>* cameraInjectionSession);
226
Avichal Rakesh6e57a2b2023-05-01 17:53:37 -0700227 virtual binder::Status reportExtensionSessionStats(
Austin Borgered99f642023-06-01 16:51:35 -0700228 const hardware::CameraExtensionSessionStats& stats, std::string* sessionKey /*out*/);
Avichal Rakesh6e57a2b2023-05-01 17:53:37 -0700229
malikakash82ed4352023-07-21 22:44:34 +0000230 virtual binder::Status remapCameraIds(const hardware::CameraIdRemapping&
231 cameraIdRemapping);
232
Igor Murashkin634a5152013-02-20 17:15:11 -0800233 // Extra permissions checks
Mathias Agopian65ab4712010-07-14 17:59:35 -0700234 virtual status_t onTransact(uint32_t code, const Parcel& data,
235 Parcel* reply, uint32_t flags);
Igor Murashkin634a5152013-02-20 17:15:11 -0800236
237 virtual status_t dump(int fd, const Vector<String16>& args);
238
Svet Ganova453d0d2018-01-11 15:37:58 -0800239 virtual status_t shellCommand(int in, int out, int err, const Vector<String16>& args);
240
Jayant Chowdharyf949ddd2019-01-29 14:34:11 -0800241 binder::Status addListenerHelper(const sp<hardware::ICameraServiceListener>& listener,
242 /*out*/
Jayant Chowdhary32ced0e2021-04-09 14:00:22 -0700243 std::vector<hardware::CameraStatus>* cameraStatuses, bool isVendor = false,
244 bool isProcessLocalTest = false);
Jayant Chowdharyf949ddd2019-01-29 14:34:11 -0800245
Emilian Peev53722fa2019-02-22 17:47:20 -0800246 // Monitored UIDs availability notification
247 void notifyMonitoredUids();
Austin Borgerdddb7552023-03-30 17:53:01 -0700248 void notifyMonitoredUids(const std::unordered_set<uid_t> &notifyUidSet);
Emilian Peev53722fa2019-02-22 17:47:20 -0800249
Rucha Katakwardf223072021-06-15 10:21:00 -0700250 // Stores current open session device info in temp file.
251 void cacheDump();
252
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800253 // Register an offline client for a given active camera id
Austin Borgered99f642023-06-01 16:51:35 -0700254 status_t addOfflineClient(const std::string &cameraId, sp<BasicClient> offlineClient);
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800255
Igor Murashkin634a5152013-02-20 17:15:11 -0800256 /////////////////////////////////////////////////////////////////////
257 // Client functionality
Mathias Agopian65ab4712010-07-14 17:59:35 -0700258
259 enum sound_kind {
260 SOUND_SHUTTER = 0,
Chien-Yu Chen82104eb2015-10-14 11:29:31 -0700261 SOUND_RECORDING_START = 1,
262 SOUND_RECORDING_STOP = 2,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700263 NUM_SOUNDS
264 };
265
Mathias Agopian65ab4712010-07-14 17:59:35 -0700266 void playSound(sound_kind kind);
username5755fea2018-12-27 09:48:08 +0800267 void loadSoundLocked(sound_kind kind);
268 void decreaseSoundRef();
269 void increaseSoundRef();
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700270
Igor Murashkin98e24722013-06-19 19:51:04 -0700271 /////////////////////////////////////////////////////////////////////
272 // CameraDeviceFactory functionality
Austin Borgered99f642023-06-01 16:51:35 -0700273 std::pair<int, IPCTransport> getDeviceVersion(const std::string& cameraId,
Austin Borger18b30a72022-10-27 12:20:29 -0700274 bool overrideToPortrait, int* portraitRotation,
275 int* facing = nullptr, int* orientation = nullptr);
Igor Murashkin98e24722013-06-19 19:51:04 -0700276
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700277 /////////////////////////////////////////////////////////////////////
Jayant Chowdhary32ced0e2021-04-09 14:00:22 -0700278 // Methods to be used in CameraService class tests only
279 //
280 // CameraService class test method only - clear static variables in the
281 // cameraserver process, which otherwise might affect multiple test runs.
282 void clearCachedVariables();
283
284 // Add test listener, linkToDeath won't be called since this is for process
285 // local testing.
286 binder::Status addListenerTest(const sp<hardware::ICameraServiceListener>& listener,
287 /*out*/
288 std::vector<hardware::CameraStatus>* cameraStatuses);
289
290 /////////////////////////////////////////////////////////////////////
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700291 // Shared utilities
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800292 static binder::Status filterGetInfoErrorCode(status_t err);
Igor Murashkin634a5152013-02-20 17:15:11 -0800293
294 /////////////////////////////////////////////////////////////////////
295 // CameraClient functionality
296
Igor Murashkin634a5152013-02-20 17:15:11 -0800297 class BasicClient : public virtual RefBase {
Jayant Chowdhary32ced0e2021-04-09 14:00:22 -0700298 friend class CameraService;
Igor Murashkin634a5152013-02-20 17:15:11 -0800299 public:
Emilian Peevbd8c5032018-02-14 23:05:40 +0000300 virtual status_t initialize(sp<CameraProviderManager> manager,
Austin Borgered99f642023-06-01 16:51:35 -0700301 const std::string& monitorTags) = 0;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800302 virtual binder::Status disconnect();
Igor Murashkin634a5152013-02-20 17:15:11 -0800303
Igor Murashkine7ee7632013-06-11 18:10:18 -0700304 // because we can't virtually inherit IInterface, which breaks
305 // virtual inheritance
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800306 virtual sp<IBinder> asBinderWrapper() = 0;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700307
Ruben Brunk9efdf952015-03-18 23:11:57 -0700308 // Return the remote callback binder object (e.g. ICameraDeviceCallbacks)
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800309 sp<IBinder> getRemote() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800310 return mRemoteBinder;
Igor Murashkin634a5152013-02-20 17:15:11 -0800311 }
312
Austin Borger18b30a72022-10-27 12:20:29 -0700313 bool getOverrideToPortrait() const {
314 return mOverrideToPortrait;
315 }
316
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800317 // Disallows dumping over binder interface
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800318 virtual status_t dump(int fd, const Vector<String16>& args);
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800319 // Internal dump method to be called by CameraService
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800320 virtual status_t dumpClient(int fd, const Vector<String16>& args) = 0;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700321
Austin Borgered99f642023-06-01 16:51:35 -0700322 virtual status_t startWatchingTags(const std::string &tags, int outFd);
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700323 virtual status_t stopWatchingTags(int outFd);
324 virtual status_t dumpWatchedEventsToVector(std::vector<std::string> &out);
325
Ruben Brunkcc776712015-02-17 20:18:47 -0800326 // Return the package name for this client
Austin Borgered99f642023-06-01 16:51:35 -0700327 virtual std::string getPackageName() const;
Ruben Brunkcc776712015-02-17 20:18:47 -0800328
Emilian Peev8b64f282021-03-25 16:49:57 -0700329 // Return the camera facing for this client
330 virtual int getCameraFacing() const;
331
332 // Return the camera orientation for this client
333 virtual int getCameraOrientation() const;
334
Ruben Brunkcc776712015-02-17 20:18:47 -0800335 // Notify client about a fatal error
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800336 virtual void notifyError(int32_t errorCode,
Ruben Brunkcc776712015-02-17 20:18:47 -0800337 const CaptureResultExtras& resultExtras) = 0;
338
Ruben Brunk6267b532015-04-30 17:44:07 -0700339 // Get the UID of the application client using this
340 virtual uid_t getClientUid() const;
341
Ruben Brunkcc776712015-02-17 20:18:47 -0800342 // Get the PID of the application client using this
343 virtual int getClientPid() const;
Ruben Brunk0bbf8b22015-04-30 14:35:42 -0700344
345 // Check what API level is used for this client. This is used to determine which
346 // superclass this can be cast to.
347 virtual bool canCastToApiClient(apiLevel level) const;
Svet Ganova453d0d2018-01-11 15:37:58 -0800348
349 // Block the client form using the camera
350 virtual void block();
Yin-Chia Yehdba03232019-08-19 15:54:28 -0700351
352 // set audio restriction from client
353 // Will call into camera service and hold mServiceLock
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -0700354 virtual status_t setAudioRestriction(int32_t mode);
Yin-Chia Yehdba03232019-08-19 15:54:28 -0700355
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -0700356 // Get current global audio restriction setting
357 // Will call into camera service and hold mServiceLock
358 virtual int32_t getServiceAudioRestriction() const;
359
360 // Get current audio restriction setting for this client
Yin-Chia Yehdba03232019-08-19 15:54:28 -0700361 virtual int32_t getAudioRestriction() const;
362
363 static bool isValidAudioRestriction(int32_t mode);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800364
365 // Override rotate-and-crop AUTO behavior
366 virtual status_t setRotateAndCropOverride(uint8_t rotateAndCrop) = 0;
367
Bharatt Kukreja7146ced2022-10-25 15:45:29 +0000368 // Override autoframing AUTO behaviour
369 virtual status_t setAutoframingOverride(uint8_t autoframingValue) = 0;
370
Eino-Ville Talvala305cec62020-11-12 14:18:17 -0800371 // Whether the client supports camera muting (black only output)
372 virtual bool supportsCameraMute() = 0;
373
374 // Set/reset camera mute
375 virtual status_t setCameraMute(bool enabled) = 0;
376
Ravneetaeb20dc2022-03-30 05:33:03 +0000377 // Set Camera service watchdog
378 virtual status_t setCameraServiceWatchdog(bool enabled) = 0;
379
Shuzhen Wang16610a62022-12-15 22:38:07 -0800380 // Set stream use case overrides
381 virtual void setStreamUseCaseOverrides(
382 const std::vector<int64_t>& useCaseOverrides) = 0;
383
384 // Clear stream use case overrides
385 virtual void clearStreamUseCaseOverrides() = 0;
386
Shuzhen Wangaf22e912023-04-11 16:03:17 -0700387 // Whether the client supports camera zoom override
388 virtual bool supportsZoomOverride() = 0;
389
390 // Set/reset zoom override
391 virtual status_t setZoomOverride(int32_t zoomOverride) = 0;
392
Cliff Wud3a05312021-04-26 23:07:31 +0800393 // The injection camera session to replace the internal camera
394 // session.
Austin Borgered99f642023-06-01 16:51:35 -0700395 virtual status_t injectCamera(const std::string& injectedCamId,
Cliff Wud3a05312021-04-26 23:07:31 +0800396 sp<CameraProviderManager> manager) = 0;
397
398 // Stop the injection camera and restore to internal camera session.
399 virtual status_t stopInjection() = 0;
400
Igor Murashkin634a5152013-02-20 17:15:11 -0800401 protected:
402 BasicClient(const sp<CameraService>& cameraService,
403 const sp<IBinder>& remoteCallback,
Austin Borgered99f642023-06-01 16:51:35 -0700404 const std::string& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -0800405 bool nativeClient,
Austin Borgered99f642023-06-01 16:51:35 -0700406 const std::optional<std::string>& clientFeatureId,
407 const std::string& cameraIdStr,
Igor Murashkin634a5152013-02-20 17:15:11 -0800408 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -0700409 int sensorOrientation,
Igor Murashkin634a5152013-02-20 17:15:11 -0800410 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800411 uid_t clientUid,
Austin Borger18b30a72022-10-27 12:20:29 -0700412 int servicePid,
413 bool overrideToPortrait);
Igor Murashkin634a5152013-02-20 17:15:11 -0800414
415 virtual ~BasicClient();
416
417 // the instance is in the middle of destruction. When this is set,
418 // the instance should not be accessed from callback.
419 // CameraService's mClientLock should be acquired to access this.
420 // - subclasses should set this to true in their destructors.
421 bool mDestructionStarted;
422
423 // these are initialized in the constructor.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800424 static sp<CameraService> sCameraService;
Austin Borgered99f642023-06-01 16:51:35 -0700425 const std::string mCameraIdStr;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800426 const int mCameraFacing;
Emilian Peev8b64f282021-03-25 16:49:57 -0700427 const int mOrientation;
Austin Borgered99f642023-06-01 16:51:35 -0700428 std::string mClientPackageName;
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -0800429 bool mSystemNativeClient;
Austin Borgered99f642023-06-01 16:51:35 -0700430 std::optional<std::string> mClientFeatureId;
Igor Murashkin634a5152013-02-20 17:15:11 -0800431 pid_t mClientPid;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800432 const uid_t mClientUid;
433 const pid_t mServicePid;
Ruben Brunkcc776712015-02-17 20:18:47 -0800434 bool mDisconnected;
Shuzhen Wang2c656792020-04-13 17:36:49 -0700435 bool mUidIsTrusted;
Austin Borger18b30a72022-10-27 12:20:29 -0700436 bool mOverrideToPortrait;
Igor Murashkin634a5152013-02-20 17:15:11 -0800437
Yin-Chia Yehdba03232019-08-19 15:54:28 -0700438 mutable Mutex mAudioRestrictionLock;
439 int32_t mAudioRestriction;
440
Igor Murashkin634a5152013-02-20 17:15:11 -0800441 // - The app-side Binder interface to receive callbacks from us
Igor Murashkine7ee7632013-06-11 18:10:18 -0700442 sp<IBinder> mRemoteBinder; // immutable after constructor
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800443
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700444 // Permissions management methods for camera lifecycle
445
446 // Notify rest of system/apps about camera opening, and check appops
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800447 virtual status_t startCameraOps();
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700448 // Notify rest of system/apps about camera starting to stream data, and confirm appops
449 virtual status_t startCameraStreamingOps();
450 // Notify rest of system/apps about camera stopping streaming data
451 virtual status_t finishCameraStreamingOps();
452 // Notify rest of system/apps about camera closing
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800453 virtual status_t finishCameraOps();
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700454 // Handle errors for start/checkOps
455 virtual status_t handleAppOpMode(int32_t mode);
Valentin Iftimec0b8d472021-07-23 20:21:06 +0200456 // Just notify camera appops to trigger unblocking dialog if sensor
457 // privacy is enabled and camera mute is not supported
458 virtual status_t noteAppOp();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800459
Jayant Chowdharyb61526c2019-05-13 19:37:42 -0700460 std::unique_ptr<AppOpsManager> mAppOpsManager = nullptr;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800461
462 class OpsCallback : public BnAppOpsCallback {
463 public:
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -0700464 explicit OpsCallback(wp<BasicClient> client);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800465 virtual void opChanged(int32_t op, const String16& packageName);
466
467 private:
468 wp<BasicClient> mClient;
469
470 }; // class OpsCallback
471
472 sp<OpsCallback> mOpsCallback;
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700473 // Track whether checkOps was called successfully, to avoid
474 // finishing what we didn't start, on camera open.
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800475 bool mOpsActive;
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700476 // Track whether startOps was called successfully on start of
477 // camera streaming.
478 bool mOpsStreaming;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800479
480 // IAppOpsCallback interface, indirected through opListener
481 virtual void opChanged(int32_t op, const String16& packageName);
482 }; // class BasicClient
Igor Murashkin634a5152013-02-20 17:15:11 -0800483
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800484 class Client : public hardware::BnCamera, public BasicClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700485 {
486 public:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800487 typedef hardware::ICameraClient TCamCallbacks;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800488
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700489 // ICamera interface (see ICamera for details)
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800490 virtual binder::Status disconnect();
491 virtual status_t connect(const sp<hardware::ICameraClient>& client) = 0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700492 virtual status_t lock() = 0;
493 virtual status_t unlock() = 0;
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700494 virtual status_t setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)=0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700495 virtual void setPreviewCallbackFlag(int flag) = 0;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700496 virtual status_t setPreviewCallbackTarget(
497 const sp<IGraphicBufferProducer>& callbackProducer) = 0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700498 virtual status_t startPreview() = 0;
499 virtual void stopPreview() = 0;
500 virtual bool previewEnabled() = 0;
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800501 virtual status_t setVideoBufferMode(int32_t videoBufferMode) = 0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700502 virtual status_t startRecording() = 0;
503 virtual void stopRecording() = 0;
504 virtual bool recordingEnabled() = 0;
505 virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0;
506 virtual status_t autoFocus() = 0;
507 virtual status_t cancelAutoFocus() = 0;
508 virtual status_t takePicture(int msgType) = 0;
509 virtual status_t setParameters(const String8& params) = 0;
510 virtual String8 getParameters() const = 0;
511 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0;
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800512 virtual status_t setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer) = 0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700513
514 // Interface used by CameraService
515 Client(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800516 const sp<hardware::ICameraClient>& cameraClient,
Austin Borgered99f642023-06-01 16:51:35 -0700517 const std::string& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -0800518 bool systemNativeClient,
Austin Borgered99f642023-06-01 16:51:35 -0700519 const std::optional<std::string>& clientFeatureId,
520 const std::string& cameraIdStr,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800521 int api1CameraId,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700522 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -0700523 int sensorOrientation,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700524 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800525 uid_t clientUid,
Austin Borger18b30a72022-10-27 12:20:29 -0700526 int servicePid,
527 bool overrideToPortrait);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700528 ~Client();
529
530 // return our camera client
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800531 const sp<hardware::ICameraClient>& getRemoteCallback() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800532 return mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700533 }
534
Igor Murashkine7ee7632013-06-11 18:10:18 -0700535 virtual sp<IBinder> asBinderWrapper() {
Marco Nelissen06b46062014-11-14 07:58:25 -0800536 return asBinder(this);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700537 }
538
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800539 virtual void notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -0700540 const CaptureResultExtras& resultExtras);
Ruben Brunk0bbf8b22015-04-30 14:35:42 -0700541
542 // Check what API level is used for this client. This is used to determine which
543 // superclass this can be cast to.
544 virtual bool canCastToApiClient(apiLevel level) const;
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -0800545
546 void setImageDumpMask(int /*mask*/) { }
Ruben Brunkcc776712015-02-17 20:18:47 -0800547 protected:
Igor Murashkin634a5152013-02-20 17:15:11 -0800548 // Initialized in constructor
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700549
Igor Murashkin634a5152013-02-20 17:15:11 -0800550 // - The app-side Binder interface to receive callbacks from us
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800551 sp<hardware::ICameraClient> mRemoteCallback;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800552
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800553 int mCameraId; // All API1 clients use integer camera IDs
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800554 }; // class Client
Igor Murashkin634a5152013-02-20 17:15:11 -0800555
Ruben Brunk99e69712015-05-26 17:25:07 -0700556 /**
557 * A listener class that implements the LISTENER interface for use with a ClientManager, and
558 * implements the following methods:
559 * void onClientRemoved(const ClientDescriptor<KEY, VALUE>& descriptor);
560 * void onClientAdded(const ClientDescriptor<KEY, VALUE>& descriptor);
561 */
562 class ClientEventListener {
563 public:
Austin Borgered99f642023-06-01 16:51:35 -0700564 void onClientAdded(const resource_policy::ClientDescriptor<std::string,
Ruben Brunk99e69712015-05-26 17:25:07 -0700565 sp<CameraService::BasicClient>>& descriptor);
Austin Borgered99f642023-06-01 16:51:35 -0700566 void onClientRemoved(const resource_policy::ClientDescriptor<std::string,
Ruben Brunk99e69712015-05-26 17:25:07 -0700567 sp<CameraService::BasicClient>>& descriptor);
568 }; // class ClientEventListener
569
Austin Borgered99f642023-06-01 16:51:35 -0700570 typedef std::shared_ptr<resource_policy::ClientDescriptor<std::string,
Ruben Brunkcc776712015-02-17 20:18:47 -0800571 sp<CameraService::BasicClient>>> DescriptorPtr;
572
573 /**
574 * A container class for managing active camera clients that are using HAL devices. Active
575 * clients are represented by ClientDescriptor objects that contain strong pointers to the
576 * actual BasicClient subclass binder interface implementation.
577 *
578 * This class manages the eviction behavior for the camera clients. See the parent class
579 * implementation in utils/ClientManager for the specifics of this behavior.
580 */
Austin Borgered99f642023-06-01 16:51:35 -0700581 class CameraClientManager : public resource_policy::ClientManager<std::string,
Ruben Brunk99e69712015-05-26 17:25:07 -0700582 sp<CameraService::BasicClient>, ClientEventListener> {
Ruben Brunkcc776712015-02-17 20:18:47 -0800583 public:
Ruben Brunk99e69712015-05-26 17:25:07 -0700584 CameraClientManager();
Ruben Brunkcc776712015-02-17 20:18:47 -0800585 virtual ~CameraClientManager();
586
587 /**
588 * Return a strong pointer to the active BasicClient for this camera ID, or an empty
589 * if none exists.
590 */
Austin Borgered99f642023-06-01 16:51:35 -0700591 sp<CameraService::BasicClient> getCameraClient(const std::string& id) const;
Ruben Brunkcc776712015-02-17 20:18:47 -0800592
593 /**
594 * Return a string describing the current state.
595 */
Austin Borgered99f642023-06-01 16:51:35 -0700596 std::string toString() const;
Ruben Brunkcc776712015-02-17 20:18:47 -0800597
598 /**
599 * Make a ClientDescriptor object wrapping the given BasicClient strong pointer.
600 */
Austin Borgered99f642023-06-01 16:51:35 -0700601 static DescriptorPtr makeClientDescriptor(const std::string& key,
602 const sp<BasicClient>& value, int32_t cost,
603 const std::set<std::string>& conflictingKeys, int32_t score,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -0800604 int32_t ownerId, int32_t state, int oomScoreOffset, bool systemNativeClient);
Ruben Brunkcc776712015-02-17 20:18:47 -0800605
606 /**
607 * Make a ClientDescriptor object wrapping the given BasicClient strong pointer with
608 * values intialized from a prior ClientDescriptor.
609 */
610 static DescriptorPtr makeClientDescriptor(const sp<BasicClient>& value,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -0800611 const CameraService::DescriptorPtr& partial, int oomScoreOffset,
612 bool systemNativeClient);
Ruben Brunkcc776712015-02-17 20:18:47 -0800613
614 }; // class CameraClientManager
615
Yin-Chia Yehdba03232019-08-19 15:54:28 -0700616 int32_t updateAudioRestriction();
617 int32_t updateAudioRestrictionLocked();
618
Mathias Agopian65ab4712010-07-14 17:59:35 -0700619private:
Igor Murashkin634a5152013-02-20 17:15:11 -0800620
Kunal Malhotrabfc96052023-02-28 23:25:34 +0000621 // TODO: b/263304156 update this to make use of a death callback for more
622 // robust/fault tolerant logging
623 static const sp<IActivityManager>& getActivityManager() {
624 static const char* kActivityService = "activity";
625 static const auto activityManager = []() -> sp<IActivityManager> {
626 const sp<IServiceManager> sm(defaultServiceManager());
627 if (sm != nullptr) {
628 return interface_cast<IActivityManager>(sm->checkService(String16(kActivityService)));
629 }
630 return nullptr;
631 }();
632 return activityManager;
633 }
634
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800635 /**
636 * Typesafe version of device status, containing both the HAL-layer and the service interface-
637 * layer values.
638 */
639 enum class StatusInternal : int32_t {
640 NOT_PRESENT = static_cast<int32_t>(CameraDeviceStatus::NOT_PRESENT),
641 PRESENT = static_cast<int32_t>(CameraDeviceStatus::PRESENT),
642 ENUMERATING = static_cast<int32_t>(CameraDeviceStatus::ENUMERATING),
643 NOT_AVAILABLE = static_cast<int32_t>(hardware::ICameraServiceListener::STATUS_NOT_AVAILABLE),
644 UNKNOWN = static_cast<int32_t>(hardware::ICameraServiceListener::STATUS_UNKNOWN)
645 };
646
Ruben Brunkcc776712015-02-17 20:18:47 -0800647 /**
648 * Container class for the state of each logical camera device, including: ID, status, and
649 * dependencies on other devices. The mapping of camera ID -> state saved in mCameraStates
650 * represents the camera devices advertised by the HAL (and any USB devices, when we add
651 * those).
652 *
653 * This container does NOT represent an active camera client. These are represented using
654 * the ClientDescriptors stored in mActiveClientManager.
655 */
656 class CameraState {
657 public:
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800658
Ruben Brunkcc776712015-02-17 20:18:47 -0800659 /**
660 * Make a new CameraState and set the ID, cost, and conflicting devices using the values
661 * returned in the HAL's camera_info struct for each device.
662 */
Austin Borgered99f642023-06-01 16:51:35 -0700663 CameraState(const std::string& id, int cost, const std::set<std::string>& conflicting,
Shuzhen Wang403af6d2021-12-21 00:08:43 +0000664 SystemCameraKind deviceKind, const std::vector<std::string>& physicalCameras);
Ruben Brunkcc776712015-02-17 20:18:47 -0800665 virtual ~CameraState();
666
667 /**
668 * Return the status for this device.
669 *
670 * This method acquires mStatusLock.
671 */
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800672 StatusInternal getStatus() const;
Ruben Brunkcc776712015-02-17 20:18:47 -0800673
674 /**
675 * This function updates the status for this camera device, unless the given status
676 * is in the given list of rejected status states, and execute the function passed in
Austin Borgered99f642023-06-01 16:51:35 -0700677 * with a signature onStatusUpdateLocked(const std::string&, int32_t)
Ruben Brunkcc776712015-02-17 20:18:47 -0800678 * if the status has changed.
679 *
680 * This method is idempotent, and will not result in the function passed to
681 * onStatusUpdateLocked being called more than once for the same arguments.
682 * This method aquires mStatusLock.
683 */
684 template<class Func>
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800685 void updateStatus(StatusInternal status,
Austin Borgered99f642023-06-01 16:51:35 -0700686 const std::string& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800687 std::initializer_list<StatusInternal> rejectSourceStates,
Ruben Brunkcc776712015-02-17 20:18:47 -0800688 Func onStatusUpdatedLocked);
689
690 /**
691 * Return the last set CameraParameters object generated from the information returned by
692 * the HAL for this device (or an empty CameraParameters object if none has been set).
693 */
694 CameraParameters getShimParams() const;
695
696 /**
697 * Set the CameraParameters for this device.
698 */
699 void setShimParams(const CameraParameters& params);
700
701 /**
702 * Return the resource_cost advertised by the HAL for this device.
703 */
704 int getCost() const;
705
706 /**
707 * Return a set of the IDs of conflicting devices advertised by the HAL for this device.
708 */
Austin Borgered99f642023-06-01 16:51:35 -0700709 std::set<std::string> getConflicting() const;
Ruben Brunkcc776712015-02-17 20:18:47 -0800710
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700711 /**
712 * Return the kind (SystemCameraKind) of this camera device.
713 */
714 SystemCameraKind getSystemCameraKind() const;
715
Shuzhen Wang43858162020-01-10 13:42:15 -0800716 /**
Shuzhen Wang403af6d2021-12-21 00:08:43 +0000717 * Return whether this camera is a logical multi-camera and has a
718 * particular physical sub-camera.
719 */
720 bool containsPhysicalCamera(const std::string& physicalCameraId) const;
721
722 /**
Shuzhen Wang43858162020-01-10 13:42:15 -0800723 * Add/Remove the unavailable physical camera ID.
724 */
Austin Borgered99f642023-06-01 16:51:35 -0700725 bool addUnavailablePhysicalId(const std::string& physicalId);
726 bool removeUnavailablePhysicalId(const std::string& physicalId);
Shuzhen Wang43858162020-01-10 13:42:15 -0800727
728 /**
Shuzhen Wange7aa0342021-08-03 11:29:47 -0700729 * Set and get client package name.
730 */
Austin Borgered99f642023-06-01 16:51:35 -0700731 void setClientPackage(const std::string& clientPackage);
732 std::string getClientPackage() const;
Shuzhen Wange7aa0342021-08-03 11:29:47 -0700733
734 /**
Shuzhen Wang43858162020-01-10 13:42:15 -0800735 * Return the unavailable physical ids for this device.
736 *
737 * This method acquires mStatusLock.
738 */
Austin Borgered99f642023-06-01 16:51:35 -0700739 std::vector<std::string> getUnavailablePhysicalIds() const;
Ruben Brunkcc776712015-02-17 20:18:47 -0800740 private:
Austin Borgered99f642023-06-01 16:51:35 -0700741 const std::string mId;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800742 StatusInternal mStatus; // protected by mStatusLock
Ruben Brunkcc776712015-02-17 20:18:47 -0800743 const int mCost;
Austin Borgered99f642023-06-01 16:51:35 -0700744 std::set<std::string> mConflicting;
745 std::set<std::string> mUnavailablePhysicalIds;
746 std::string mClientPackage;
Ruben Brunkcc776712015-02-17 20:18:47 -0800747 mutable Mutex mStatusLock;
748 CameraParameters mShimParams;
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700749 const SystemCameraKind mSystemCameraKind;
Shuzhen Wang403af6d2021-12-21 00:08:43 +0000750 const std::vector<std::string> mPhysicalCameras; // Empty if not a logical multi-camera
Ruben Brunkcc776712015-02-17 20:18:47 -0800751 }; // class CameraState
752
Svet Ganova453d0d2018-01-11 15:37:58 -0800753 // Observer for UID lifecycle enforcing that UIDs in idle
754 // state cannot use the camera to protect user privacy.
Jyoti Bhayanacde601c2022-12-07 10:03:42 -0800755 class UidPolicy :
756 public BnUidObserver,
757 public virtual IBinder::DeathRecipient,
758 public virtual IServiceManager::LocalRegistrationCallback {
Svet Ganova453d0d2018-01-11 15:37:58 -0800759 public:
760 explicit UidPolicy(sp<CameraService> service)
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -0700761 : mRegistered(false), mService(service) {}
Svet Ganova453d0d2018-01-11 15:37:58 -0800762
763 void registerSelf();
764 void unregisterSelf();
765
Austin Borgered99f642023-06-01 16:51:35 -0700766 bool isUidActive(uid_t uid, const std::string &callingPackage);
Varun Shahb42f1eb2019-04-16 14:45:13 -0700767 int32_t getProcState(uid_t uid);
Svet Ganova453d0d2018-01-11 15:37:58 -0800768
Austin Borger65577682022-02-17 00:25:43 +0000769 // IUidObserver
770 void onUidGone(uid_t uid, bool disabled) override;
771 void onUidActive(uid_t uid) override;
772 void onUidIdle(uid_t uid, bool disabled) override;
Hui Yu13ad0eb2019-09-09 10:27:07 -0700773 void onUidStateChanged(uid_t uid, int32_t procState, int64_t procStateSeq,
Austin Borger65577682022-02-17 00:25:43 +0000774 int32_t capability) override;
Austin Borgerdddb7552023-03-30 17:53:01 -0700775 void onUidProcAdjChanged(uid_t uid, int adj) override;
Svet Ganova453d0d2018-01-11 15:37:58 -0800776
Austin Borgered99f642023-06-01 16:51:35 -0700777 void addOverrideUid(uid_t uid, const std::string &callingPackage, bool active);
778 void removeOverrideUid(uid_t uid, const std::string &callingPackage);
Svet Ganova453d0d2018-01-11 15:37:58 -0800779
Austin Borgerdddb7552023-03-30 17:53:01 -0700780 void registerMonitorUid(uid_t uid, bool openCamera);
781 void unregisterMonitorUid(uid_t uid, bool closeCamera);
Emilian Peev53722fa2019-02-22 17:47:20 -0800782
Jyoti Bhayanacde601c2022-12-07 10:03:42 -0800783 // Implementation of IServiceManager::LocalRegistrationCallback
784 virtual void onServiceRegistration(const String16& name,
785 const sp<IBinder>& binder) override;
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -0700786 // IBinder::DeathRecipient implementation
787 virtual void binderDied(const wp<IBinder> &who);
Svet Ganova453d0d2018-01-11 15:37:58 -0800788 private:
Austin Borgered99f642023-06-01 16:51:35 -0700789 bool isUidActiveLocked(uid_t uid, const std::string &callingPackage);
Varun Shahb42f1eb2019-04-16 14:45:13 -0700790 int32_t getProcStateLocked(uid_t uid);
Austin Borgered99f642023-06-01 16:51:35 -0700791 void updateOverrideUid(uid_t uid, const std::string &callingPackage, bool active,
792 bool insert);
Jyoti Bhayanacde601c2022-12-07 10:03:42 -0800793 void registerWithActivityManager();
Svet Ganova453d0d2018-01-11 15:37:58 -0800794
Austin Borger65577682022-02-17 00:25:43 +0000795 struct MonitoredUid {
796 int32_t procState;
Austin Borgerdddb7552023-03-30 17:53:01 -0700797 int32_t procAdj;
798 bool hasCamera;
Austin Borger65577682022-02-17 00:25:43 +0000799 size_t refCount;
800 };
801
Svet Ganova453d0d2018-01-11 15:37:58 -0800802 Mutex mUidLock;
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -0700803 bool mRegistered;
Steven Moreland2f348142019-07-02 15:59:07 -0700804 ActivityManager mAm;
Svet Ganova453d0d2018-01-11 15:37:58 -0800805 wp<CameraService> mService;
806 std::unordered_set<uid_t> mActiveUids;
Austin Borger65577682022-02-17 00:25:43 +0000807 // Monitored uid map
808 std::unordered_map<uid_t, MonitoredUid> mMonitoredUids;
Svet Ganova453d0d2018-01-11 15:37:58 -0800809 std::unordered_map<uid_t, bool> mOverrideUids;
Austin Borgerd0309d42023-04-21 20:07:18 -0700810 sp<IBinder> mObserverToken;
Svet Ganova453d0d2018-01-11 15:37:58 -0800811 }; // class UidPolicy
812
Michael Grooverd1d435a2018-12-18 17:39:42 -0800813 // If sensor privacy is enabled then all apps, including those that are active, should be
814 // prevented from accessing the camera.
815 class SensorPrivacyPolicy : public hardware::BnSensorPrivacyListener,
Jyoti Bhayanacde601c2022-12-07 10:03:42 -0800816 public virtual IBinder::DeathRecipient,
817 public virtual IServiceManager::LocalRegistrationCallback {
Michael Grooverd1d435a2018-12-18 17:39:42 -0800818 public:
819 explicit SensorPrivacyPolicy(wp<CameraService> service)
Evan Severson671806e2021-04-20 16:44:53 -0700820 : mService(service), mSensorPrivacyEnabled(false), mRegistered(false) {}
Michael Grooverd1d435a2018-12-18 17:39:42 -0800821
822 void registerSelf();
823 void unregisterSelf();
824
825 bool isSensorPrivacyEnabled();
Evan Seversond0b69922022-01-27 10:47:34 -0800826 bool isCameraPrivacyEnabled();
Michael Grooverd1d435a2018-12-18 17:39:42 -0800827
Evan Seversond0b69922022-01-27 10:47:34 -0800828 binder::Status onSensorPrivacyChanged(int toggleType, int sensor,
829 bool enabled);
Michael Grooverd1d435a2018-12-18 17:39:42 -0800830
Jyoti Bhayanacde601c2022-12-07 10:03:42 -0800831 // Implementation of IServiceManager::LocalRegistrationCallback
832 virtual void onServiceRegistration(const String16& name,
833 const sp<IBinder>& binder) override;
Michael Grooverd1d435a2018-12-18 17:39:42 -0800834 // IBinder::DeathRecipient implementation
835 virtual void binderDied(const wp<IBinder> &who);
836
837 private:
Steven Moreland3cf67172020-01-29 11:44:22 -0800838 SensorPrivacyManager mSpm;
Michael Grooverd1d435a2018-12-18 17:39:42 -0800839 wp<CameraService> mService;
840 Mutex mSensorPrivacyLock;
841 bool mSensorPrivacyEnabled;
842 bool mRegistered;
Evan Severson09ab4002021-02-10 14:15:19 -0800843
844 bool hasCameraPrivacyFeature();
Jyoti Bhayanacde601c2022-12-07 10:03:42 -0800845 void registerWithSensorPrivacyManager();
Michael Grooverd1d435a2018-12-18 17:39:42 -0800846 };
847
Svet Ganova453d0d2018-01-11 15:37:58 -0800848 sp<UidPolicy> mUidPolicy;
849
Michael Grooverd1d435a2018-12-18 17:39:42 -0800850 sp<SensorPrivacyPolicy> mSensorPrivacyPolicy;
851
Austin Borger74fca042022-05-23 12:41:21 -0700852 std::shared_ptr<CameraServiceProxyWrapper> mCameraServiceProxyWrapper;
853
Igor Murashkin634a5152013-02-20 17:15:11 -0800854 // Delay-load the Camera HAL module
855 virtual void onFirstRef();
856
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800857 // Eumerate all camera providers in the system
858 status_t enumerateProviders();
859
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800860 // Add/remove a new camera to camera and torch state lists or remove an unplugged one
861 // Caller must not hold mServiceLock
Austin Borgered99f642023-06-01 16:51:35 -0700862 void addStates(const std::string& id);
863 void removeStates(const std::string& id);
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +0100864
Ruben Brunkcc776712015-02-17 20:18:47 -0800865 // Check if we can connect, before we acquire the service lock.
Chien-Yu Chen18df60e2016-03-18 18:18:09 -0700866 // The returned originalClientPid is the PID of the original process that wants to connect to
867 // camera.
868 // The returned clientPid is the PID of the client that directly connects to camera.
869 // originalClientPid and clientPid are usually the same except when the application uses
870 // mediaserver to connect to camera (using MediaRecorder to connect to camera). In that case,
871 // clientPid is the PID of mediaserver and originalClientPid is the PID of the application.
Austin Borgered99f642023-06-01 16:51:35 -0700872 binder::Status validateConnectLocked(const std::string& cameraId, const std::string& clientName,
Chien-Yu Chen7939aee2016-03-21 18:19:33 -0700873 /*inout*/int& clientUid, /*inout*/int& clientPid, /*out*/int& originalClientPid) const;
Austin Borgered99f642023-06-01 16:51:35 -0700874 binder::Status validateClientPermissionsLocked(const std::string& cameraId,
875 const std::string& clientName, /*inout*/int& clientUid, /*inout*/int& clientPid,
876 /*out*/int& originalClientPid) const;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800877
Ruben Brunkcc776712015-02-17 20:18:47 -0800878 // Handle active client evictions, and update service state.
879 // Only call with with mServiceLock held.
Austin Borgered99f642023-06-01 16:51:35 -0700880 status_t handleEvictionsLocked(const std::string& cameraId, int clientPid,
881 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback,
882 const std::string& packageName, int scoreOffset, bool systemNativeClient,
Ruben Brunkcc776712015-02-17 20:18:47 -0800883 /*out*/
884 sp<BasicClient>* client,
Austin Borgered99f642023-06-01 16:51:35 -0700885 std::shared_ptr<resource_policy::ClientDescriptor<std::string, sp<BasicClient>>>* partial);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800886
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700887 // Should an operation attempt on a cameraId be rejected ? (this can happen
888 // under various conditions. For example if a camera device is advertised as
889 // system only or hidden secure camera, amongst possible others.
Austin Borgered99f642023-06-01 16:51:35 -0700890 bool shouldRejectSystemCameraConnection(const std::string& cameraId) const;
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700891
892 // Should a device status update be skipped for a particular camera device ? (this can happen
893 // under various conditions. For example if a camera device is advertised as
894 // system only or hidden secure camera, amongst possible others.
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700895 static bool shouldSkipStatusUpdates(SystemCameraKind systemCameraKind, bool isVendorListener,
896 int clientPid, int clientUid);
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700897
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700898 // Gets the kind of camera device (i.e public, hidden secure or system only)
899 // getSystemCameraKind() needs mInterfaceMutex which might lead to deadlocks
900 // if held along with mStatusListenerLock (depending on lock ordering, b/141756275), it is
901 // recommended that we don't call this function with mStatusListenerLock held.
Austin Borgered99f642023-06-01 16:51:35 -0700902 status_t getSystemCameraKind(const std::string& cameraId, SystemCameraKind *kind) const;
Jayant Chowdharyf949ddd2019-01-29 14:34:11 -0800903
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700904 // Update the set of API1Compatible camera devices without including system
905 // cameras and secure cameras. This is used for hiding system only cameras
906 // from clients using camera1 api and not having android.permission.SYSTEM_CAMERA.
907 // This function expects @param normalDeviceIds, to have normalDeviceIds
908 // sorted in alpha-numeric order.
909 void filterAPI1SystemCameraLocked(const std::vector<std::string> &normalDeviceIds);
910
Eino-Ville Talvala58106af2022-09-23 16:51:06 -0700911 // In some cases the calling code has no access to the package it runs under.
912 // For example, NDK camera API.
913 // In this case we will get the packages for the calling UID and pick the first one
914 // for attributing the app op. This will work correctly for runtime permissions
915 // as for legacy apps we will toggle the app op for all packages in the UID.
916 // The caveat is that the operation may be attributed to the wrong package and
917 // stats based on app ops may be slightly off.
Austin Borgered99f642023-06-01 16:51:35 -0700918 std::string getPackageNameFromUid(int clientUid);
Eino-Ville Talvala58106af2022-09-23 16:51:06 -0700919
Ruben Brunkcc776712015-02-17 20:18:47 -0800920 // Single implementation shared between the various connect calls
921 template<class CALLBACK, class CLIENT>
Austin Borgered99f642023-06-01 16:51:35 -0700922 binder::Status connectHelper(const sp<CALLBACK>& cameraCb, const std::string& cameraId,
923 int api1CameraId, const std::string& clientPackageNameMaybe, bool systemNativeClient,
924 const std::optional<std::string>& clientFeatureId, int clientUid, int clientPid,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700925 apiLevel effectiveApiLevel, bool shimUpdateOnly, int scoreOffset, int targetSdkVersion,
Austin Borgered99f642023-06-01 16:51:35 -0700926 bool overrideToPortrait, bool forceSlowJpegMode, const std::string& originalCameraId,
Chengfei Taobe683db2023-01-31 18:52:49 +0000927 /*out*/sp<CLIENT>& device);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800928
Ruben Brunkcc776712015-02-17 20:18:47 -0800929 // Lock guarding camera service state
Mathias Agopian65ab4712010-07-14 17:59:35 -0700930 Mutex mServiceLock;
Ruben Brunkcc776712015-02-17 20:18:47 -0800931
932 // Condition to use with mServiceLock, used to handle simultaneous connect calls from clients
933 std::shared_ptr<WaitableMutexWrapper> mServiceLockWrapper;
934
935 // Return NO_ERROR if the device with a give ID can be connected to
Austin Borgered99f642023-06-01 16:51:35 -0700936 status_t checkIfDeviceIsUsable(const std::string& cameraId) const;
Ruben Brunkcc776712015-02-17 20:18:47 -0800937
938 // Container for managing currently active application-layer clients
939 CameraClientManager mActiveClientManager;
940
Rucha Katakwardf223072021-06-15 10:21:00 -0700941 // Adds client logs during open session to the file pointed by fd.
Austin Borgered99f642023-06-01 16:51:35 -0700942 void dumpOpenSessionClientLogs(int fd, const Vector<String16>& args,
943 const std::string& cameraId);
Rucha Katakwardf223072021-06-15 10:21:00 -0700944
945 // Adds client logs during closed session to the file pointed by fd.
Austin Borgered99f642023-06-01 16:51:35 -0700946 void dumpClosedSessionClientLogs(int fd, const std::string& cameraId);
Rucha Katakwardf223072021-06-15 10:21:00 -0700947
Ruben Brunkcc776712015-02-17 20:18:47 -0800948 // Mapping from camera ID -> state for each device, map is protected by mCameraStatesLock
Austin Borgered99f642023-06-01 16:51:35 -0700949 std::map<std::string, std::shared_ptr<CameraState>> mCameraStates;
Ruben Brunkcc776712015-02-17 20:18:47 -0800950
951 // Mutex guarding mCameraStates map
952 mutable Mutex mCameraStatesLock;
953
malikakash82ed4352023-07-21 22:44:34 +0000954 /**
955 * Mapping from packageName -> {cameraIdToReplace -> newCameraIdtoUse}.
956 *
957 * This specifies that for packageName, for every binder operation targeting
958 * cameraIdToReplace, use newCameraIdToUse instead.
959 */
Austin Borgered99f642023-06-01 16:51:35 -0700960 typedef std::map<std::string, std::map<std::string, std::string>> TCameraIdRemapping;
malikakash82ed4352023-07-21 22:44:34 +0000961 TCameraIdRemapping mCameraIdRemapping{};
962 /** Mutex guarding mCameraIdRemapping. */
963 Mutex mCameraIdRemappingLock;
964
965 /** Parses cameraIdRemapping parcelable into the native cameraIdRemappingMap. */
966 binder::Status parseCameraIdRemapping(
malikakash214f6e62023-08-10 23:50:56 +0000967 const hardware::CameraIdRemapping& cameraIdRemapping,
968 /* out */ TCameraIdRemapping* cameraIdRemappingMap);
malikakash82ed4352023-07-21 22:44:34 +0000969
970 /**
971 * Resolve the (potentially remapped) camera Id to use for packageName.
972 *
973 * This returns the Camera Id to use in case inputCameraId was remapped to a
974 * different Id for the given packageName. Otherwise, it returns the inputCameraId.
malikakash82ed4352023-07-21 22:44:34 +0000975 *
malikakashedb38962023-09-06 00:03:35 +0000976 * If the packageName is not provided, it will be inferred from the clientUid.
malikakash82ed4352023-07-21 22:44:34 +0000977 */
malikakashedb38962023-09-06 00:03:35 +0000978 std::string resolveCameraId(
979 const std::string& inputCameraId,
980 int clientUid,
981 const std::string& packageName = "");
malikakash82ed4352023-07-21 22:44:34 +0000982
983 /**
984 * Updates the state of mCameraIdRemapping, while disconnecting active clients as necessary.
985 */
986 void remapCameraIds(const TCameraIdRemapping& cameraIdRemapping);
987
988 /**
989 * Finds the Camera Ids that were remapped to the inputCameraId for the given client.
990 */
Austin Borgered99f642023-06-01 16:51:35 -0700991 std::vector<std::string> findOriginalIdsForRemappedCameraId(
992 const std::string& inputCameraId, int clientUid);
malikakash82ed4352023-07-21 22:44:34 +0000993
Ruben Brunkcc776712015-02-17 20:18:47 -0800994 // Circular buffer for storing event logging for dumps
Austin Borgered99f642023-06-01 16:51:35 -0700995 RingBuffer<std::string> mEventLog;
Ruben Brunka8ca9152015-04-07 14:23:40 -0700996 Mutex mLogLock;
Ruben Brunkcc776712015-02-17 20:18:47 -0800997
Avichal Rakesh7fbc3982021-10-20 04:35:03 -0700998 // set of client package names to watch. if this set contains 'all', then all clients will
999 // be watched. Access should be guarded by mLogLock
Austin Borgered99f642023-06-01 16:51:35 -07001000 std::set<std::string> mWatchedClientPackages;
Avichal Rakesh7fbc3982021-10-20 04:35:03 -07001001 // cache of last monitored tags dump immediately before the client disconnects. If a client
1002 // re-connects, its entry is not updated until it disconnects again. Access should be guarded
1003 // by mLogLock
Austin Borgered99f642023-06-01 16:51:35 -07001004 std::map<std::string, std::string> mWatchedClientsDumpCache;
Avichal Rakesh7fbc3982021-10-20 04:35:03 -07001005
Emilian Peevbd8c5032018-02-14 23:05:40 +00001006 // The last monitored tags set by client
Austin Borgered99f642023-06-01 16:51:35 -07001007 std::string mMonitorTags;
Emilian Peevbd8c5032018-02-14 23:05:40 +00001008
Ruben Brunk6267b532015-04-30 17:44:07 -07001009 // Currently allowed user IDs
1010 std::set<userid_t> mAllowedUsers;
Ruben Brunk36597b22015-03-20 22:15:57 -07001011
Ruben Brunkcc776712015-02-17 20:18:47 -08001012 /**
1013 * Get the camera state for a given camera id.
1014 *
1015 * This acquires mCameraStatesLock.
1016 */
Austin Borgered99f642023-06-01 16:51:35 -07001017 std::shared_ptr<CameraService::CameraState> getCameraState(const std::string& cameraId) const;
Ruben Brunkcc776712015-02-17 20:18:47 -08001018
1019 /**
1020 * Evict client who's remote binder has died. Returns true if this client was in the active
1021 * list and was disconnected.
1022 *
1023 * This method acquires mServiceLock.
1024 */
1025 bool evictClientIdByRemote(const wp<IBinder>& cameraClient);
1026
1027 /**
1028 * Remove the given client from the active clients list; does not disconnect the client.
1029 *
1030 * This method acquires mServiceLock.
1031 */
1032 void removeByClient(const BasicClient* client);
1033
1034 /**
1035 * Add new client to active clients list after conflicting clients have disconnected using the
1036 * values set in the partial descriptor passed in to construct the actual client descriptor.
1037 * This is typically called at the end of a connect call.
1038 *
1039 * This method must be called with mServiceLock held.
1040 */
Jayant Chowdhary8eb8d912021-05-18 17:41:56 +00001041 void finishConnectLocked(const sp<BasicClient>& client, const DescriptorPtr& desc,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -08001042 int oomScoreOffset, bool systemNativeClient);
Ruben Brunkcc776712015-02-17 20:18:47 -08001043
1044 /**
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001045 * Returns the underlying camera Id string mapped to a camera id int
1046 * Empty string is returned when the cameraIdInt is invalid.
Ruben Brunkcc776712015-02-17 20:18:47 -08001047 */
Austin Borgered99f642023-06-01 16:51:35 -07001048 std::string cameraIdIntToStr(int cameraIdInt);
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001049
1050 /**
1051 * Returns the underlying camera Id string mapped to a camera id int
1052 * Empty string is returned when the cameraIdInt is invalid.
1053 */
1054 std::string cameraIdIntToStrLocked(int cameraIdInt);
Ruben Brunkcc776712015-02-17 20:18:47 -08001055
1056 /**
1057 * Remove a single client corresponding to the given camera id from the list of active clients.
1058 * If none exists, return an empty strongpointer.
1059 *
1060 * This method must be called with mServiceLock held.
1061 */
Austin Borgered99f642023-06-01 16:51:35 -07001062 sp<CameraService::BasicClient> removeClientLocked(const std::string& cameraId);
Ruben Brunkcc776712015-02-17 20:18:47 -08001063
1064 /**
Ruben Brunk36597b22015-03-20 22:15:57 -07001065 * Handle a notification that the current device user has changed.
1066 */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001067 void doUserSwitch(const std::vector<int32_t>& newUserIds);
Ruben Brunk36597b22015-03-20 22:15:57 -07001068
1069 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -07001070 * Add an event log message.
Ruben Brunkcc776712015-02-17 20:18:47 -08001071 */
Austin Borgered99f642023-06-01 16:51:35 -07001072 void logEvent(const std::string &event);
Ruben Brunkcc776712015-02-17 20:18:47 -08001073
1074 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -07001075 * Add an event log message that a client has been disconnected.
Ruben Brunkcc776712015-02-17 20:18:47 -08001076 */
Austin Borgered99f642023-06-01 16:51:35 -07001077 void logDisconnected(const std::string &cameraId, int clientPid,
1078 const std::string &clientPackage);
Ruben Brunka8ca9152015-04-07 14:23:40 -07001079
1080 /**
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001081 * Add an event log message that a client has been disconnected from offline device.
1082 */
Austin Borgered99f642023-06-01 16:51:35 -07001083 void logDisconnectedOffline(const std::string &cameraId, int clientPid,
1084 const std::string &clientPackage);
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001085
1086 /**
1087 * Add an event log message that an offline client has been connected.
1088 */
Austin Borgered99f642023-06-01 16:51:35 -07001089 void logConnectedOffline(const std::string &cameraId, int clientPid,
1090 const std::string &clientPackage);
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001091
1092 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -07001093 * Add an event log message that a client has been connected.
1094 */
Austin Borgered99f642023-06-01 16:51:35 -07001095 void logConnected(const std::string &cameraId, int clientPid, const std::string &clientPackage);
Ruben Brunka8ca9152015-04-07 14:23:40 -07001096
1097 /**
1098 * Add an event log message that a client's connect attempt has been rejected.
1099 */
Austin Borgered99f642023-06-01 16:51:35 -07001100 void logRejected(const std::string &cameraId, int clientPid, const std::string &clientPackage,
1101 const std::string &reason);
Ruben Brunka8ca9152015-04-07 14:23:40 -07001102
1103 /**
Jayant Chowdhary0e2eefd2019-04-18 14:05:43 -07001104 * Add an event log message when a client calls setTorchMode succesfully.
1105 */
Austin Borgered99f642023-06-01 16:51:35 -07001106 void logTorchEvent(const std::string &cameraId, const std::string &torchState, int clientPid);
Jayant Chowdhary0e2eefd2019-04-18 14:05:43 -07001107
1108 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -07001109 * Add an event log message that the current device user has been switched.
1110 */
Ruben Brunk6267b532015-04-30 17:44:07 -07001111 void logUserSwitch(const std::set<userid_t>& oldUserIds,
1112 const std::set<userid_t>& newUserIds);
Ruben Brunka8ca9152015-04-07 14:23:40 -07001113
1114 /**
1115 * Add an event log message that a device has been removed by the HAL
1116 */
Austin Borgered99f642023-06-01 16:51:35 -07001117 void logDeviceRemoved(const std::string &cameraId, const std::string &reason);
Ruben Brunka8ca9152015-04-07 14:23:40 -07001118
1119 /**
1120 * Add an event log message that a device has been added by the HAL
1121 */
Austin Borgered99f642023-06-01 16:51:35 -07001122 void logDeviceAdded(const std::string &cameraId, const std::string &reason);
Ruben Brunka8ca9152015-04-07 14:23:40 -07001123
1124 /**
1125 * Add an event log message that a client has unexpectedly died.
1126 */
Austin Borgered99f642023-06-01 16:51:35 -07001127 void logClientDied(int clientPid, const std::string &reason);
Ruben Brunkcc776712015-02-17 20:18:47 -08001128
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07001129 /**
1130 * Add a event log message that a serious service-level error has occured
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001131 * The errorCode should be one of the Android Errors
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07001132 */
Austin Borgered99f642023-06-01 16:51:35 -07001133 void logServiceError(const std::string &msg, int errorCode);
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07001134
1135 /**
1136 * Dump the event log to an FD
1137 */
1138 void dumpEventLog(int fd);
1139
Austin Borgered99f642023-06-01 16:51:35 -07001140 void cacheClientTagDumpIfNeeded(const std::string &cameraId, BasicClient *client);
Avichal Rakesh7fbc3982021-10-20 04:35:03 -07001141
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001142 /**
1143 * This method will acquire mServiceLock
1144 */
1145 void updateCameraNumAndIds();
1146
Shuzhen Wang89db2992021-05-20 13:09:48 -07001147 /**
Shuzhen Wangb38b53f2021-07-15 12:46:09 -07001148 * Filter camera characteristics for S Performance class primary cameras.
1149 * mServiceLock should be locked.
Shuzhen Wang89db2992021-05-20 13:09:48 -07001150 */
Shuzhen Wangb38b53f2021-07-15 12:46:09 -07001151 void filterSPerfClassCharacteristicsLocked();
Shuzhen Wang89db2992021-05-20 13:09:48 -07001152
Rucha Katakwardf223072021-06-15 10:21:00 -07001153 // File descriptor to temp file used for caching previous open
1154 // session dumpsys info.
1155 int mMemFd;
1156
Jayant Chowdhary847947d2019-08-30 18:02:59 -07001157 // Number of camera devices (excluding hidden secure cameras)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001158 int mNumberOfCameras;
Jayant Chowdhary847947d2019-08-30 18:02:59 -07001159 // Number of camera devices (excluding hidden secure cameras and
1160 // system cameras)
1161 int mNumberOfCamerasWithoutSystemCamera;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001162
1163 std::vector<std::string> mNormalDeviceIds;
Jayant Chowdhary847947d2019-08-30 18:02:59 -07001164 std::vector<std::string> mNormalDeviceIdsWithoutSystemCamera;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001165 std::set<std::string> mPerfClassPrimaryCameraIds;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001166
Mathias Agopian65ab4712010-07-14 17:59:35 -07001167 // sounds
Jaekyun Seokef498052018-03-23 13:09:44 +09001168 sp<MediaPlayer> newMediaPlayer(const char *file);
Chih-Chung Changff4f55c2011-10-17 19:03:12 +08001169
Mathias Agopian65ab4712010-07-14 17:59:35 -07001170 Mutex mSoundLock;
1171 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
1172 int mSoundRef; // reference count (release all MediaPlayer when 0)
1173
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001174 // Basic flag on whether the camera subsystem is in a usable state
1175 bool mInitialized;
1176
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001177 sp<CameraProviderManager> mCameraProviderManager;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001178
Emilian Peev53722fa2019-02-22 17:47:20 -08001179 class ServiceListener : public virtual IBinder::DeathRecipient {
1180 public:
1181 ServiceListener(sp<CameraService> parent, sp<hardware::ICameraServiceListener> listener,
Shuzhen Wang695044d2020-03-06 09:02:23 -08001182 int uid, int pid, bool isVendorClient, bool openCloseCallbackAllowed)
Jayant Chowdhary5216b212019-07-17 09:26:23 -07001183 : mParent(parent), mListener(listener), mListenerUid(uid), mListenerPid(pid),
Shuzhen Wang695044d2020-03-06 09:02:23 -08001184 mIsVendorListener(isVendorClient),
1185 mOpenCloseCallbackAllowed(openCloseCallbackAllowed) { }
Emilian Peev53722fa2019-02-22 17:47:20 -08001186
Jayant Chowdhary32ced0e2021-04-09 14:00:22 -07001187 status_t initialize(bool isProcessLocalTest) {
1188 if (isProcessLocalTest) {
1189 return OK;
1190 }
Emilian Peev53722fa2019-02-22 17:47:20 -08001191 return IInterface::asBinder(mListener)->linkToDeath(this);
1192 }
1193
Austin Borgere8e2c422022-05-12 13:45:24 -07001194 template<typename... args_t>
1195 void handleBinderStatus(const binder::Status &ret, const char *logOnError,
1196 args_t... args) {
1197 if (!ret.isOk() &&
1198 (ret.exceptionCode() != binder::Status::Exception::EX_TRANSACTION_FAILED
1199 || !mLastTransactFailed)) {
1200 ALOGE(logOnError, args...);
1201 }
1202
1203 // If the transaction failed, the process may have died (or other things, see
1204 // b/28321379). Mute consecutive errors from this listener to avoid log spam.
1205 if (ret.exceptionCode() == binder::Status::Exception::EX_TRANSACTION_FAILED) {
1206 if (!mLastTransactFailed) {
1207 ALOGE("%s: Muting similar errors from listener %d:%d", __FUNCTION__,
1208 mListenerUid, mListenerPid);
1209 }
1210 mLastTransactFailed = true;
1211 } else {
1212 // Reset mLastTransactFailed when binder becomes healthy again.
1213 mLastTransactFailed = false;
1214 }
1215 }
1216
Emilian Peev53722fa2019-02-22 17:47:20 -08001217 virtual void binderDied(const wp<IBinder> &/*who*/) {
1218 auto parent = mParent.promote();
1219 if (parent.get() != nullptr) {
1220 parent->removeListener(mListener);
1221 }
1222 }
1223
1224 int getListenerUid() { return mListenerUid; }
Jayant Chowdhary5216b212019-07-17 09:26:23 -07001225 int getListenerPid() { return mListenerPid; }
Emilian Peev53722fa2019-02-22 17:47:20 -08001226 sp<hardware::ICameraServiceListener> getListener() { return mListener; }
Jayant Chowdhary5216b212019-07-17 09:26:23 -07001227 bool isVendorListener() { return mIsVendorListener; }
Shuzhen Wang695044d2020-03-06 09:02:23 -08001228 bool isOpenCloseCallbackAllowed() { return mOpenCloseCallbackAllowed; }
Emilian Peev53722fa2019-02-22 17:47:20 -08001229
1230 private:
1231 wp<CameraService> mParent;
1232 sp<hardware::ICameraServiceListener> mListener;
Jayant Chowdhary5216b212019-07-17 09:26:23 -07001233 int mListenerUid = -1;
1234 int mListenerPid = -1;
1235 bool mIsVendorListener = false;
Shuzhen Wang695044d2020-03-06 09:02:23 -08001236 bool mOpenCloseCallbackAllowed = false;
Austin Borgere8e2c422022-05-12 13:45:24 -07001237
1238 // Flag for preventing log spam when binder becomes unhealthy
1239 bool mLastTransactFailed = false;
Emilian Peev53722fa2019-02-22 17:47:20 -08001240 };
1241
Ruben Brunkcc776712015-02-17 20:18:47 -08001242 // Guarded by mStatusListenerMutex
Jayant Chowdhary5216b212019-07-17 09:26:23 -07001243 std::vector<sp<ServiceListener>> mListenerList;
Jayant Chowdharyf949ddd2019-01-29 14:34:11 -08001244
Ruben Brunkcc776712015-02-17 20:18:47 -08001245 Mutex mStatusListenerLock;
Igor Murashkinbfc99152013-02-27 12:55:20 -08001246
Ruben Brunkcc776712015-02-17 20:18:47 -08001247 /**
1248 * Update the status for the given camera id (if that device exists), and broadcast the
1249 * status update to all current ICameraServiceListeners if the status has changed. Any
1250 * statuses in rejectedSourceStates will be ignored.
1251 *
1252 * This method must be idempotent.
1253 * This method acquires mStatusLock and mStatusListenerLock.
1254 */
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001255 void updateStatus(StatusInternal status,
Austin Borgered99f642023-06-01 16:51:35 -07001256 const std::string& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001257 std::initializer_list<StatusInternal>
1258 rejectedSourceStates);
1259 void updateStatus(StatusInternal status,
Austin Borgered99f642023-06-01 16:51:35 -07001260 const std::string& cameraId);
Igor Murashkinbfc99152013-02-27 12:55:20 -08001261
Shuzhen Wang695044d2020-03-06 09:02:23 -08001262 /**
1263 * Update the opened/closed status of the given camera id.
1264 *
1265 * This method acqiures mStatusListenerLock.
1266 */
Austin Borgered99f642023-06-01 16:51:35 -07001267 void updateOpenCloseStatus(const std::string& cameraId, bool open,
1268 const std::string& packageName);
Shuzhen Wang695044d2020-03-06 09:02:23 -08001269
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001270 // flashlight control
1271 sp<CameraFlashlight> mFlashlight;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001272 // guard mTorchStatusMap
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001273 Mutex mTorchStatusMutex;
Chien-Yu Chenfe751be2015-09-01 14:16:44 -07001274 // guard mTorchClientMap
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001275 Mutex mTorchClientMapMutex;
Chien-Yu Chenfe751be2015-09-01 14:16:44 -07001276 // guard mTorchUidMap
1277 Mutex mTorchUidMapMutex;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001278 // camera id -> torch status
Austin Borgered99f642023-06-01 16:51:35 -07001279 KeyedVector<std::string, TorchModeStatus>
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001280 mTorchStatusMap;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001281 // camera id -> torch client binder
1282 // only store the last client that turns on each camera's torch mode
Austin Borgered99f642023-06-01 16:51:35 -07001283 KeyedVector<std::string, sp<IBinder>> mTorchClientMap;
Ruben Brunk99e69712015-05-26 17:25:07 -07001284 // camera id -> [incoming uid, current uid] pair
Austin Borgered99f642023-06-01 16:51:35 -07001285 std::map<std::string, std::pair<int, int>> mTorchUidMap;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001286
1287 // check and handle if torch client's process has died
1288 void handleTorchClientBinderDied(const wp<IBinder> &who);
1289
1290 // handle torch mode status change and invoke callbacks. mTorchStatusMutex
1291 // should be locked.
Austin Borgered99f642023-06-01 16:51:35 -07001292 void onTorchStatusChangedLocked(const std::string& cameraId,
Jayant Chowdhary0bd38522021-11-05 17:49:27 -07001293 TorchModeStatus newStatus,
Jayant Chowdhary8c62d892021-03-31 02:13:46 -07001294 SystemCameraKind systemCameraKind);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001295
1296 // get a camera's torch status. mTorchStatusMutex should be locked.
Austin Borgered99f642023-06-01 16:51:35 -07001297 status_t getTorchStatusLocked(const std::string &cameraId,
Jayant Chowdhary0bd38522021-11-05 17:49:27 -07001298 TorchModeStatus *status) const;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001299
1300 // set a camera's torch status. mTorchStatusMutex should be locked.
Austin Borgered99f642023-06-01 16:51:35 -07001301 status_t setTorchStatusLocked(const std::string &cameraId,
Jayant Chowdhary0bd38522021-11-05 17:49:27 -07001302 TorchModeStatus status);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001303
Shuzhen Wang43858162020-01-10 13:42:15 -08001304 // notify physical camera status when the physical camera is public.
Jayant Chowdhary5e2cd302020-08-14 02:48:34 +00001305 // Expects mStatusListenerLock to be locked.
Austin Borgered99f642023-06-01 16:51:35 -07001306 void notifyPhysicalCameraStatusLocked(int32_t status, const std::string& physicalCameraId,
1307 const std::list<std::string>& logicalCameraIds, SystemCameraKind deviceKind);
Jayant Chowdhary5e2cd302020-08-14 02:48:34 +00001308
1309 // get list of logical cameras which are backed by physicalCameraId
Austin Borgered99f642023-06-01 16:51:35 -07001310 std::list<std::string> getLogicalCameras(const std::string& physicalCameraId);
Jayant Chowdhary5e2cd302020-08-14 02:48:34 +00001311
Shuzhen Wang43858162020-01-10 13:42:15 -08001312
Igor Murashkinecf17e82012-10-02 16:05:11 -07001313 // IBinder::DeathRecipient implementation
Igor Murashkinbfc99152013-02-27 12:55:20 -08001314 virtual void binderDied(const wp<IBinder> &who);
Igor Murashkin634a5152013-02-20 17:15:11 -08001315
Ruben Brunkb2119af2014-05-09 19:57:56 -07001316 /**
Ruben Brunkb2119af2014-05-09 19:57:56 -07001317 * Initialize and cache the metadata used by the HAL1 shim for a given cameraId.
1318 *
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001319 * Sets Status to a service-specific error on failure
Ruben Brunkb2119af2014-05-09 19:57:56 -07001320 */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001321 binder::Status initializeShimMetadata(int cameraId);
Ruben Brunkb2119af2014-05-09 19:57:56 -07001322
1323 /**
Igor Murashkin65d14b92014-06-17 12:03:20 -07001324 * Get the cached CameraParameters for the camera. If they haven't been
1325 * cached yet, then initialize them for the first time.
1326 *
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001327 * Sets Status to a service-specific error on failure
Igor Murashkin65d14b92014-06-17 12:03:20 -07001328 */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001329 binder::Status getLegacyParametersLazy(int cameraId, /*out*/CameraParameters* parameters);
Igor Murashkin65d14b92014-06-17 12:03:20 -07001330
Svet Ganova453d0d2018-01-11 15:37:58 -08001331 // Blocks all clients from the UID
1332 void blockClientsForUid(uid_t uid);
1333
Michael Grooverd1d435a2018-12-18 17:39:42 -08001334 // Blocks all active clients.
1335 void blockAllClients();
1336
Svet Ganova453d0d2018-01-11 15:37:58 -08001337 // Overrides the UID state as if it is idle
1338 status_t handleSetUidState(const Vector<String16>& args, int err);
1339
1340 // Clears the override for the UID state
1341 status_t handleResetUidState(const Vector<String16>& args, int err);
1342
1343 // Gets the UID state
1344 status_t handleGetUidState(const Vector<String16>& args, int out, int err);
1345
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001346 // Set the rotate-and-crop AUTO override behavior
1347 status_t handleSetRotateAndCrop(const Vector<String16>& args);
1348
1349 // Get the rotate-and-crop AUTO override behavior
1350 status_t handleGetRotateAndCrop(int out);
1351
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00001352 // Set the autoframing AUTO override behaviour.
1353 status_t handleSetAutoframing(const Vector<String16>& args);
1354
1355 // Get the autoframing AUTO override behaviour
1356 status_t handleGetAutoframing(int out);
1357
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -08001358 // Set the mask for image dump to disk
1359 status_t handleSetImageDumpMask(const Vector<String16>& args);
1360
1361 // Get the mask for image dump to disk
1362 status_t handleGetImageDumpMask(int out);
1363
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08001364 // Set the camera mute state
1365 status_t handleSetCameraMute(const Vector<String16>& args);
1366
Shuzhen Wang16610a62022-12-15 22:38:07 -08001367 // Set the stream use case overrides
1368 status_t handleSetStreamUseCaseOverrides(const Vector<String16>& args);
1369
1370 // Clear the stream use case overrides
1371 void handleClearStreamUseCaseOverrides();
1372
Shuzhen Wangaf22e912023-04-11 16:03:17 -07001373 // Set or clear the zoom override flag
1374 status_t handleSetZoomOverride(const Vector<String16>& args);
1375
malikakash82ed4352023-07-21 22:44:34 +00001376 // Set Camera Id remapping using 'cmd'
1377 status_t handleCameraIdRemapping(const Vector<String16>& args, int errFd);
1378
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001379 // Handle 'watch' command as passed through 'cmd'
Avichal Rakesh84147132021-11-11 17:47:11 -08001380 status_t handleWatchCommand(const Vector<String16> &args, int inFd, int outFd);
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001381
Ravneetaeb20dc2022-03-30 05:33:03 +00001382 // Set the camera service watchdog
1383 status_t handleSetCameraServiceWatchdog(const Vector<String16>& args);
1384
Avichal Rakesh7fbc3982021-10-20 04:35:03 -07001385 // Enable tag monitoring of the given tags in provided clients
1386 status_t startWatchingTags(const Vector<String16> &args, int outFd);
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001387
Avichal Rakesh7fbc3982021-10-20 04:35:03 -07001388 // Disable tag monitoring
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001389 status_t stopWatchingTags(int outFd);
1390
Avichal Rakesh3a85d2d2021-11-10 16:21:33 -08001391 // Clears mWatchedClientsDumpCache
1392 status_t clearCachedMonitoredTagDumps(int outFd);
1393
Avichal Rakesh7fbc3982021-10-20 04:35:03 -07001394 // Print events of monitored tags in all cached and attached clients
Avichal Rakesh9e5a1e42021-11-15 12:11:21 -08001395 status_t printWatchedTags(int outFd);
Avichal Rakesh7fbc3982021-10-20 04:35:03 -07001396
1397 // Print events of monitored tags in all attached clients as they are captured. New events are
Avichal Rakesh84147132021-11-11 17:47:11 -08001398 // fetched every `refreshMillis` ms
1399 // NOTE: This function does not terminate until user passes '\n' to inFd.
Avichal Rakesh9e5a1e42021-11-15 12:11:21 -08001400 status_t printWatchedTagsUntilInterrupt(const Vector<String16> &args, int inFd, int outFd);
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001401
Avichal Rakesh7fbc3982021-10-20 04:35:03 -07001402 // Parses comma separated clients list and adds them to mWatchedClientPackages.
1403 // Does not acquire mLogLock before modifying mWatchedClientPackages. It is the caller's
1404 // responsibility to acquire mLogLock before calling this function.
Austin Borgered99f642023-06-01 16:51:35 -07001405 void parseClientsToWatchLocked(const std::string &clients);
Avichal Rakesh7fbc3982021-10-20 04:35:03 -07001406
Svet Ganova453d0d2018-01-11 15:37:58 -08001407 // Prints the shell command help
1408 status_t printHelp(int out);
1409
Avichal Rakesh7fbc3982021-10-20 04:35:03 -07001410 // Returns true if client should monitor tags based on the contents of mWatchedClientPackages.
1411 // Acquires mLogLock before querying mWatchedClientPackages.
1412 bool isClientWatched(const BasicClient *client);
1413
1414 // Returns true if client should monitor tags based on the contents of mWatchedClientPackages.
1415 // Does not acquire mLogLock before querying mWatchedClientPackages. It is the caller's
1416 // responsibility to acquire mLogLock before calling this functions.
1417 bool isClientWatchedLocked(const BasicClient *client);
1418
Ruben Brunkb2119af2014-05-09 19:57:56 -07001419 /**
Ruben Brunkcc776712015-02-17 20:18:47 -08001420 * Get the current system time as a formatted string.
Ruben Brunkb2119af2014-05-09 19:57:56 -07001421 */
Austin Borgered99f642023-06-01 16:51:35 -07001422 static std::string getFormattedCurrentTime();
Ruben Brunkcc776712015-02-17 20:18:47 -08001423
malikakash82ed4352023-07-21 22:44:34 +00001424 static binder::Status makeClient(
1425 const sp<CameraService>& cameraService, const sp<IInterface>& cameraCb,
Austin Borgered99f642023-06-01 16:51:35 -07001426 const std::string& packageName, bool systemNativeClient,
1427 const std::optional<std::string>& featureId, const std::string& cameraId, int api1CameraId,
malikakash82ed4352023-07-21 22:44:34 +00001428 int facing, int sensorOrientation, int clientPid, uid_t clientUid, int servicePid,
Jayant Chowdharyffc5d682022-05-12 18:34:34 +00001429 std::pair<int, IPCTransport> deviceVersionAndIPCTransport, apiLevel effectiveApiLevel,
Chengfei Taobe683db2023-01-31 18:52:49 +00001430 bool overrideForPerfClass, bool overrideToPortrait, bool forceSlowJpegMode,
Austin Borgered99f642023-06-01 16:51:35 -07001431 const std::string& originalCameraId,
malikakash82ed4352023-07-21 22:44:34 +00001432 /*out*/ sp<BasicClient>* client);
Ruben Brunk6267b532015-04-30 17:44:07 -07001433
Austin Borgered99f642023-06-01 16:51:35 -07001434 static std::string toString(std::set<userid_t> intSet);
Jayant Chowdhary0bd38522021-11-05 17:49:27 -07001435 static int32_t mapToInterface(TorchModeStatus status);
1436 static StatusInternal mapToInternal(CameraDeviceStatus status);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001437 static int32_t mapToInterface(StatusInternal status);
Ruben Brunk6267b532015-04-30 17:44:07 -07001438
Ruben Brunk2823ce02015-05-19 17:25:13 -07001439
Austin Borgered99f642023-06-01 16:51:35 -07001440 void broadcastTorchModeStatus(const std::string& cameraId,
Jayant Chowdhary0bd38522021-11-05 17:49:27 -07001441 TorchModeStatus status, SystemCameraKind systemCameraKind);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001442
Austin Borgered99f642023-06-01 16:51:35 -07001443 void broadcastTorchStrengthLevel(const std::string& cameraId, int32_t newTorchStrengthLevel);
Rucha Katakwar38284522021-11-10 11:25:21 -08001444
Austin Borgered99f642023-06-01 16:51:35 -07001445 void disconnectClient(const std::string& id, sp<BasicClient> clientToDisconnect);
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001446
1447 // Regular online and offline devices must not be in conflict at camera service layer.
1448 // Use separate keys for offline devices.
Austin Borgered99f642023-06-01 16:51:35 -07001449 static const std::string kOfflineDevice;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001450
Avichal Rakesh7fbc3982021-10-20 04:35:03 -07001451 // Sentinel value to be stored in `mWatchedClientsPackages` to indicate that all clients should
1452 // be watched.
Austin Borgered99f642023-06-01 16:51:35 -07001453 static const std::string kWatchAllClientsFlag;
Avichal Rakesh7fbc3982021-10-20 04:35:03 -07001454
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001455 // TODO: right now each BasicClient holds one AppOpsManager instance.
1456 // We can refactor the code so all of clients share this instance
1457 AppOpsManager mAppOps;
1458
1459 // Aggreated audio restriction mode for all camera clients
1460 int32_t mAudioRestriction;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001461
Emilian Peev8b64f282021-03-25 16:49:57 -07001462 // Current override cmd rotate-and-crop mode; AUTO means no override
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001463 uint8_t mOverrideRotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_AUTO;
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -08001464
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00001465 // Current autoframing mode
1466 uint8_t mOverrideAutoframingMode = ANDROID_CONTROL_AUTOFRAMING_AUTO;
1467
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -08001468 // Current image dump mask
1469 uint8_t mImageDumpMask = 0;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08001470
1471 // Current camera mute mode
1472 bool mOverrideCameraMuteMode = false;
Cliff Wud8cae102021-03-11 01:37:42 +08001473
Ravneetaeb20dc2022-03-30 05:33:03 +00001474 // Camera Service watchdog flag
1475 bool mCameraServiceWatchdogEnabled = true;
1476
Shuzhen Wang16610a62022-12-15 22:38:07 -08001477 // Current stream use case overrides
1478 std::vector<int64_t> mStreamUseCaseOverrides;
1479
Shuzhen Wangaf22e912023-04-11 16:03:17 -07001480 // Current zoom override value
1481 int32_t mZoomOverrideValue = -1;
1482
Cliff Wud8cae102021-03-11 01:37:42 +08001483 /**
1484 * A listener class that implements the IBinder::DeathRecipient interface
1485 * for use to call back the error state injected by the external camera, and
1486 * camera service can kill the injection when binder signals process death.
1487 */
1488 class InjectionStatusListener : public virtual IBinder::DeathRecipient {
1489 public:
1490 InjectionStatusListener(sp<CameraService> parent) : mParent(parent) {}
1491
1492 void addListener(const sp<hardware::camera2::ICameraInjectionCallback>& callback);
1493 void removeListener();
Austin Borgered99f642023-06-01 16:51:35 -07001494 void notifyInjectionError(const std::string &injectedCamId, status_t err);
Cliff Wud8cae102021-03-11 01:37:42 +08001495
1496 // IBinder::DeathRecipient implementation
1497 virtual void binderDied(const wp<IBinder>& who);
1498
1499 private:
1500 Mutex mListenerLock;
1501 wp<CameraService> mParent;
1502 sp<hardware::camera2::ICameraInjectionCallback> mCameraInjectionCallback;
1503 };
1504
1505 sp<InjectionStatusListener> mInjectionStatusListener;
1506
1507 /**
1508 * A class that implements the hardware::camera2::BnCameraInjectionSession interface
1509 */
1510 class CameraInjectionSession : public hardware::camera2::BnCameraInjectionSession {
1511 public:
1512 CameraInjectionSession(sp<CameraService> parent) : mParent(parent) {}
1513 virtual ~CameraInjectionSession() {}
1514 binder::Status stopInjection() override;
1515
1516 private:
1517 Mutex mInjectionSessionLock;
1518 wp<CameraService> mParent;
1519 };
1520
Cliff Wu646bd612021-11-23 23:21:29 +08001521 // When injecting the camera, it will check whether the injecting camera status is unavailable.
1522 // If it is, the disconnect function will be called to to prevent camera access on the device.
Austin Borgered99f642023-06-01 16:51:35 -07001523 status_t checkIfInjectionCameraIsPresent(const std::string& externalCamId,
Cliff Wu646bd612021-11-23 23:21:29 +08001524 sp<BasicClient> clientSp);
1525
Cliff Wud3a05312021-04-26 23:07:31 +08001526 void clearInjectionParameters();
1527
1528 // This is the existing camera id being replaced.
Austin Borgered99f642023-06-01 16:51:35 -07001529 std::string mInjectionInternalCamId;
Cliff Wud3a05312021-04-26 23:07:31 +08001530 // This is the external camera Id replacing the internalId.
Austin Borgered99f642023-06-01 16:51:35 -07001531 std::string mInjectionExternalCamId;
Cliff Wu646bd612021-11-23 23:21:29 +08001532 bool mInjectionInitPending = false;
Cliff Wud3a05312021-04-26 23:07:31 +08001533 // Guard mInjectionInternalCamId and mInjectionInitPending.
1534 Mutex mInjectionParametersLock;
Rucha Katakwar38284522021-11-10 11:25:21 -08001535
Austin Borger18b30a72022-10-27 12:20:29 -07001536 // Track the folded/unfoled device state. 0 == UNFOLDED, 4 == FOLDED
1537 int64_t mDeviceState;
1538
Austin Borgered99f642023-06-01 16:51:35 -07001539 void updateTorchUidMapLocked(const std::string& cameraId, int uid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001540};
1541
Mathias Agopian65ab4712010-07-14 17:59:35 -07001542} // namespace android
1543
1544#endif