1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ANDROID_SERVERS_CAMERA_CAMERASERVICE_H 18 #define ANDROID_SERVERS_CAMERA_CAMERASERVICE_H 19 20 #include <android/content/AttributionSourceState.h> 21 #include <android/hardware/BnCameraService.h> 22 #include <android/hardware/BnSensorPrivacyListener.h> 23 #include <android/hardware/ICameraServiceListener.h> 24 #include <android/hardware/camera2/BnCameraInjectionSession.h> 25 #include <android/hardware/camera2/ICameraInjectionCallback.h> 26 27 #include <binder/ActivityManager.h> 28 #include <binder/AppOpsManager.h> 29 #include <binder/BinderService.h> 30 #include <binder/IActivityManager.h> 31 #include <binder/IAppOpsCallback.h> 32 #include <binder/IServiceManager.h> 33 #include <binder/IUidObserver.h> 34 #include <cutils/multiuser.h> 35 #include <gui/Flags.h> 36 #include <hardware/camera.h> 37 #include <sensorprivacy/SensorPrivacyManager.h> 38 #include <utils/KeyedVector.h> 39 #include <utils/Vector.h> 40 41 #include <android/hardware/camera/common/1.0/types.h> 42 43 #include <camera/VendorTagDescriptor.h> 44 #include <camera/CaptureResult.h> 45 #include <camera/CameraParameters.h> 46 #include <camera/camera2/ConcurrentCamera.h> 47 48 #include "CameraFlashlight.h" 49 50 #include "common/CameraProviderManager.h" 51 #include "media/RingBuffer.h" 52 #include "utils/AutoConditionLock.h" 53 #include "utils/ClientManager.h" 54 #include "utils/IPCTransport.h" 55 #include "utils/CameraServiceProxyWrapper.h" 56 #include "utils/AttributionAndPermissionUtils.h" 57 #include "utils/VirtualDeviceCameraIdMapper.h" 58 59 #include <set> 60 #include <string> 61 #include <list> 62 #include <map> 63 #include <memory> 64 #include <mutex> 65 #include <optional> 66 #include <utility> 67 #include <unordered_map> 68 #include <unordered_set> 69 #include <vector> 70 71 namespace android { 72 73 extern volatile int32_t gLogLevel; 74 75 class MemoryHeapBase; 76 class MediaPlayer; 77 78 class CameraService : 79 public BinderService<CameraService>, 80 public virtual ::android::hardware::BnCameraService, 81 public virtual IBinder::DeathRecipient, 82 public virtual CameraProviderManager::StatusListener, 83 public virtual IServiceManager::LocalRegistrationCallback, 84 public AttributionAndPermissionUtilsEncapsulator 85 { 86 friend class BinderService<CameraService>; 87 friend class CameraOfflineSessionClient; 88 public: 89 class Client; 90 class BasicClient; 91 class OfflineClient; 92 93 // The effective API level. The Camera2 API running in LEGACY mode counts as API_1. 94 enum apiLevel { 95 API_1 = 1, 96 API_2 = 2 97 }; 98 99 // 3 second busy timeout when other clients are connecting 100 static const nsecs_t DEFAULT_CONNECT_TIMEOUT_NS = 3000000000; 101 102 // 1 second busy timeout when other clients are disconnecting 103 static const nsecs_t DEFAULT_DISCONNECT_TIMEOUT_NS = 1000000000; 104 105 // Default number of messages to store in eviction log 106 static const size_t DEFAULT_EVENT_LOG_LENGTH = 100; 107 108 // Event log ID 109 static const int SN_EVENT_LOG_ID = 0x534e4554; 110 111 // Keep this in sync with frameworks/base/core/java/android/os/UserHandle.java 112 static const userid_t USER_SYSTEM = 0; 113 114 // Register camera service 115 static void instantiate(); 116 117 // Implementation of BinderService<T> getServiceName()118 static char const* getServiceName() { return "media.camera"; } 119 120 // Implementation of IServiceManager::LocalRegistrationCallback 121 virtual void onServiceRegistration(const String16& name, const sp<IBinder>& binder) override; 122 123 // Non-null arguments for cameraServiceProxyWrapper should be provided for 124 // testing purposes only. 125 CameraService(std::shared_ptr<CameraServiceProxyWrapper> 126 cameraServiceProxyWrapper = nullptr, 127 std::shared_ptr<AttributionAndPermissionUtils> 128 attributionAndPermissionUtils = nullptr); 129 virtual ~CameraService(); 130 131 ///////////////////////////////////////////////////////////////////// 132 // HAL Callbacks - implements CameraProviderManager::StatusListener 133 134 virtual void onDeviceStatusChanged(const std::string &cameraId, 135 CameraDeviceStatus newHalStatus) override; 136 virtual void onDeviceStatusChanged(const std::string &cameraId, 137 const std::string &physicalCameraId, 138 CameraDeviceStatus newHalStatus) override; 139 // This method may hold CameraProviderManager::mInterfaceMutex as a part 140 // of calling getSystemCameraKind() internally. Care should be taken not to 141 // directly / indirectly call this from callers who also hold 142 // mInterfaceMutex. 143 virtual void onTorchStatusChanged(const std::string& cameraId, 144 TorchModeStatus newStatus) override; 145 // Does not hold CameraProviderManager::mInterfaceMutex. 146 virtual void onTorchStatusChanged(const std::string& cameraId, 147 TorchModeStatus newStatus, 148 SystemCameraKind kind) override; 149 virtual void onNewProviderRegistered() override; 150 151 ///////////////////////////////////////////////////////////////////// 152 // ICameraService 153 // IMPORTANT: All binder calls that deal with logicalCameraId should use 154 // resolveCameraId(logicalCameraId, deviceId, devicePolicy) to arrive at the correct 155 // cameraId to perform the operation on (in case of contexts 156 // associated with virtual devices). 157 virtual binder::Status getNumberOfCameras(int32_t type, 158 const AttributionSourceState& clientAttribution, 159 int32_t devicePolicy, int32_t* numCameras); 160 161 virtual binder::Status getCameraInfo(int cameraId, int rotationOverride, 162 const AttributionSourceState& clientAttribution, 163 int32_t devicePolicy, hardware::CameraInfo* cameraInfo) override; 164 virtual binder::Status getCameraCharacteristics(const std::string& cameraId, 165 int targetSdkVersion, int rotationOverride, 166 const AttributionSourceState& clientAttribution, 167 int32_t devicePolicy, CameraMetadata* cameraInfo) override; 168 virtual binder::Status getCameraVendorTagDescriptor( 169 /*out*/ 170 hardware::camera2::params::VendorTagDescriptor* desc); 171 virtual binder::Status getCameraVendorTagCache( 172 /*out*/ 173 hardware::camera2::params::VendorTagDescriptorCache* cache); 174 175 virtual binder::Status connect(const sp<hardware::ICameraClient>& cameraClient, 176 int32_t cameraId, int targetSdkVersion, int rotationOverride, bool forceSlowJpegMode, 177 const AttributionSourceState& clientAttribution, 178 int32_t devicePolicy, /*out*/ sp<hardware::ICamera>* device) override; 179 180 virtual binder::Status connectDevice( 181 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb, 182 const std::string& cameraId, int scoreOffset, int targetSdkVersion, 183 int rotationOverride, const AttributionSourceState& clientAttribution, 184 int32_t devicePolicy, bool sharedMode, 185 /*out*/ 186 sp<hardware::camera2::ICameraDeviceUser>* device); 187 188 virtual binder::Status addListener(const sp<hardware::ICameraServiceListener>& listener, 189 /*out*/ 190 std::vector<hardware::CameraStatus>* cameraStatuses); 191 virtual binder::Status removeListener( 192 const sp<hardware::ICameraServiceListener>& listener); 193 194 virtual binder::Status getConcurrentCameraIds( 195 /*out*/ 196 std::vector<hardware::camera2::utils::ConcurrentCameraIdCombination>* concurrentCameraIds); 197 198 virtual binder::Status isConcurrentSessionConfigurationSupported( 199 const std::vector<hardware::camera2::utils::CameraIdAndSessionConfiguration>& sessions, 200 int targetSdkVersion, const AttributionSourceState& clientAttribution, int32_t devicePolicy, 201 /*out*/bool* supported); 202 203 virtual binder::Status getLegacyParameters( 204 int32_t cameraId, 205 /*out*/ 206 std::string* parameters); 207 208 virtual binder::Status setTorchMode(const std::string& cameraId, bool enabled, 209 const sp<IBinder>& clientBinder, const AttributionSourceState& clientAttribution, 210 int32_t devicePolicy); 211 212 virtual binder::Status turnOnTorchWithStrengthLevel(const std::string& cameraId, 213 int32_t torchStrength, const sp<IBinder>& clientBinder, 214 const AttributionSourceState& clientAttribution, 215 int32_t devicePolicy); 216 217 virtual binder::Status getTorchStrengthLevel(const std::string& cameraId, 218 const AttributionSourceState& clientAttribution, 219 int32_t devicePolicy, int32_t* torchStrength); 220 221 virtual binder::Status notifySystemEvent(int32_t eventId, 222 const std::vector<int32_t>& args); 223 224 virtual binder::Status notifyDeviceStateChange(int64_t newState); 225 226 virtual binder::Status notifyDisplayConfigurationChange(); 227 228 // OK = supports api of that version, -EOPNOTSUPP = does not support 229 virtual binder::Status supportsCameraApi( 230 const std::string& cameraId, int32_t apiVersion, 231 /*out*/ 232 bool *isSupported); 233 234 virtual binder::Status isHiddenPhysicalCamera( 235 const std::string& cameraId, 236 /*out*/ 237 bool *isSupported); 238 239 virtual binder::Status injectCamera( 240 const std::string& packageName, const std::string& internalCamId, 241 const std::string& externalCamId, 242 const sp<hardware::camera2::ICameraInjectionCallback>& callback, 243 /*out*/ 244 sp<hardware::camera2::ICameraInjectionSession>* cameraInjectionSession); 245 246 virtual binder::Status reportExtensionSessionStats( 247 const hardware::CameraExtensionSessionStats& stats, std::string* sessionKey /*out*/); 248 249 virtual binder::Status injectSessionParams( 250 const std::string& cameraId, 251 const hardware::camera2::impl::CameraMetadataNative& sessionParams); 252 253 virtual binder::Status createDefaultRequest(const std::string& cameraId, int templateId, 254 const AttributionSourceState& clientAttribution, int32_t devicePolicy, 255 /*out*/ 256 hardware::camera2::impl::CameraMetadataNative* request); 257 258 virtual binder::Status isSessionConfigurationWithParametersSupported( 259 const std::string& cameraId, int targetSdkVersion, 260 const SessionConfiguration& sessionConfiguration, 261 const AttributionSourceState& clientAttribution, int32_t devicePolicy, 262 /*out*/ bool* supported); 263 264 virtual binder::Status getSessionCharacteristics( 265 const std::string& cameraId, int targetSdkVersion, int rotationOverride, 266 const SessionConfiguration& sessionConfiguration, 267 const AttributionSourceState& clientAttribution, 268 int32_t devicePolicy, /*out*/ CameraMetadata* outMetadata); 269 270 // Extra permissions checks 271 virtual status_t onTransact(uint32_t code, const Parcel& data, 272 Parcel* reply, uint32_t flags); 273 274 virtual status_t dump(int fd, const Vector<String16>& args); 275 276 virtual status_t shellCommand(int in, int out, int err, const Vector<String16>& args); 277 278 binder::Status addListenerHelper(const sp<hardware::ICameraServiceListener>& listener, 279 /*out*/ 280 std::vector<hardware::CameraStatus>* cameraStatuses, bool isVendor = false, 281 bool isProcessLocalTest = false); 282 283 binder::Status connectDeviceVendor( 284 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb, 285 const std::string& cameraId, int scoreOffset, int targetSdkVersion, 286 int rotationOverride, const AttributionSourceState& clientAttribution, 287 int32_t devicePolicy, bool sharedMode, 288 /*out*/ 289 sp<hardware::camera2::ICameraDeviceUser>* device); 290 291 // Monitored UIDs availability notification 292 void notifyMonitoredUids(); 293 void notifyMonitoredUids(const std::unordered_set<uid_t> ¬ifyUidSet); 294 295 // Stores current open session device info in temp file. 296 void cacheDump(); 297 298 // Register an offline client for a given active camera id 299 status_t addOfflineClient(const std::string &cameraId, sp<BasicClient> offlineClient); 300 301 ///////////////////////////////////////////////////////////////////// 302 // Client functionality 303 304 enum sound_kind { 305 SOUND_SHUTTER = 0, 306 SOUND_RECORDING_START = 1, 307 SOUND_RECORDING_STOP = 2, 308 NUM_SOUNDS 309 }; 310 311 void playSound(sound_kind kind); 312 void loadSoundLocked(sound_kind kind); 313 void decreaseSoundRef(); 314 void increaseSoundRef(); 315 316 ///////////////////////////////////////////////////////////////////// 317 // CameraDeviceFactory functionality 318 std::pair<int, IPCTransport> getDeviceVersion(const std::string& cameraId, 319 int rotationOverride, 320 int* portraitRotation, 321 int* facing = nullptr, int* orientation = nullptr); 322 323 ///////////////////////////////////////////////////////////////////// 324 // Methods to be used in CameraService class tests only 325 // 326 // CameraService class test method only - clear static variables in the 327 // cameraserver process, which otherwise might affect multiple test runs. 328 void clearCachedVariables(); 329 330 // Add test listener, linkToDeath won't be called since this is for process 331 // local testing. 332 binder::Status addListenerTest(const sp<hardware::ICameraServiceListener>& listener, 333 /*out*/ 334 std::vector<hardware::CameraStatus>* cameraStatuses); 335 336 ///////////////////////////////////////////////////////////////////// 337 // Shared utilities 338 static binder::Status filterGetInfoErrorCode(status_t err); 339 340 /** 341 * Returns true if the device is an automotive device and cameraId is system 342 * only camera which has characteristic AUTOMOTIVE_LOCATION value as either 343 * AUTOMOTIVE_LOCATION_EXTERIOR_LEFT,AUTOMOTIVE_LOCATION_EXTERIOR_RIGHT, 344 * AUTOMOTIVE_LOCATION_EXTERIOR_FRONT or AUTOMOTIVE_LOCATION_EXTERIOR_REAR. 345 */ 346 bool isAutomotiveExteriorSystemCamera(const std::string& cameraId) const; 347 348 ///////////////////////////////////////////////////////////////////// 349 // CameraClient functionality 350 351 class BasicClient : 352 public virtual RefBase, 353 public AttributionAndPermissionUtilsEncapsulator { 354 friend class CameraService; 355 public: 356 virtual status_t initialize(sp<CameraProviderManager> manager, 357 const std::string& monitorTags) = 0; 358 virtual binder::Status disconnect(); 359 360 // because we can't virtually inherit IInterface, which breaks 361 // virtual inheritance 362 virtual sp<IBinder> asBinderWrapper() = 0; 363 364 // Return the remote callback binder object (e.g. ICameraDeviceCallbacks) getRemote()365 sp<IBinder> getRemote() { 366 return mRemoteBinder; 367 } 368 getOverrideToPortrait()369 bool getOverrideToPortrait() const { 370 return mRotationOverride == ICameraService::ROTATION_OVERRIDE_OVERRIDE_TO_PORTRAIT; 371 } 372 373 // Disallows dumping over binder interface 374 virtual status_t dump(int fd, const Vector<String16>& args); 375 // Internal dump method to be called by CameraService 376 virtual status_t dumpClient(int fd, const Vector<String16>& args) = 0; 377 378 virtual status_t startWatchingTags(const std::string &tags, int outFd); 379 virtual status_t stopWatchingTags(int outFd); 380 virtual status_t dumpWatchedEventsToVector(std::vector<std::string> &out); 381 382 // Return the package name for this client 383 virtual std::string getPackageName() const; 384 385 // Return the camera facing for this client 386 virtual int getCameraFacing() const; 387 388 // Return the camera orientation for this client 389 virtual int getCameraOrientation() const; 390 391 // Notify client about a fatal error 392 virtual void notifyError(int32_t errorCode, 393 const CaptureResultExtras& resultExtras) = 0; 394 395 virtual void notifyClientSharedAccessPriorityChanged(bool primaryClient) = 0; 396 397 // Get the UID of the application client using this 398 virtual uid_t getClientUid() const; 399 400 // Get the calling PID of the application client using this 401 virtual int getClientCallingPid() const; 402 403 // Get the attribution tag (previously featureId) of the application client using this 404 virtual const std::optional<std::string>& getClientAttributionTag() const; 405 406 // Check what API level is used for this client. This is used to determine which 407 // superclass this can be cast to. 408 virtual bool canCastToApiClient(apiLevel level) const; 409 410 // Block the client form using the camera 411 virtual void block(); 412 413 // set audio restriction from client 414 // Will call into camera service and hold mServiceLock 415 virtual status_t setAudioRestriction(int32_t mode); 416 417 // Get current global audio restriction setting 418 // Will call into camera service and hold mServiceLock 419 virtual int32_t getServiceAudioRestriction() const; 420 421 // Get current audio restriction setting for this client 422 virtual int32_t getAudioRestriction() const; 423 424 static bool isValidAudioRestriction(int32_t mode); 425 426 // Override rotate-and-crop AUTO behavior 427 virtual status_t setRotateAndCropOverride(uint8_t rotateAndCrop, bool fromHal = false) = 0; 428 429 // Override autoframing AUTO behaviour 430 virtual status_t setAutoframingOverride(uint8_t autoframingValue) = 0; 431 432 // Whether the client supports camera muting (black only output) 433 virtual bool supportsCameraMute() = 0; 434 435 // Set/reset camera mute 436 virtual status_t setCameraMute(bool enabled) = 0; 437 438 // Set Camera service watchdog 439 virtual status_t setCameraServiceWatchdog(bool enabled) = 0; 440 441 // Set stream use case overrides 442 virtual void setStreamUseCaseOverrides( 443 const std::vector<int64_t>& useCaseOverrides) = 0; 444 445 // Clear stream use case overrides 446 virtual void clearStreamUseCaseOverrides() = 0; 447 448 // Whether the client supports camera zoom override 449 virtual bool supportsZoomOverride() = 0; 450 451 // Set/reset zoom override 452 virtual status_t setZoomOverride(int32_t zoomOverride) = 0; 453 454 // The injection camera session to replace the internal camera 455 // session. 456 virtual status_t injectCamera(const std::string& injectedCamId, 457 sp<CameraProviderManager> manager) = 0; 458 459 // Stop the injection camera and restore to internal camera session. 460 virtual status_t stopInjection() = 0; 461 462 // Inject session parameters into an existing session. 463 virtual status_t injectSessionParams( 464 const hardware::camera2::impl::CameraMetadataNative& sessionParams) = 0; 465 466 status_t isPrimaryClient(/*out*/bool* isPrimary); 467 468 status_t setPrimaryClient(bool isPrimary); 469 470 protected: 471 BasicClient(const sp<CameraService>& cameraService, const sp<IBinder>& remoteCallback, 472 std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils, 473 const AttributionSourceState& clientAttribution, int callingPid, 474 bool nativeClient, const std::string& cameraIdStr, int cameraFacing, 475 int sensorOrientation, int servicePid, int rotationOverride, bool sharedMode); 476 477 virtual ~BasicClient(); 478 479 // The instance is in the middle of destruction. When this is set, 480 // the instance should not be accessed from callback. 481 // CameraService's mClientLock should be acquired to access this. 482 // - subclasses should set this to true in their destructors. 483 bool mDestructionStarted; 484 485 // These are initialized in the constructor. 486 static sp<CameraService> sCameraService; 487 const std::string mCameraIdStr; 488 const int mCameraFacing; 489 const int mOrientation; 490 AttributionSourceState mClientAttribution; 491 int mCallingPid; 492 bool mSystemNativeClient; 493 const pid_t mServicePid; 494 bool mDisconnected; 495 bool mUidIsTrusted; 496 int mRotationOverride; 497 bool mSharedMode; 498 bool mIsPrimaryClient; 499 500 mutable Mutex mAudioRestrictionLock; 501 int32_t mAudioRestriction; 502 503 // - The app-side Binder interface to receive callbacks from us 504 sp<IBinder> mRemoteBinder; // immutable after constructor 505 506 // Permissions management methods for camera lifecycle 507 508 // Notify rest of system/apps about camera opening, and (legacy) check appops 509 virtual status_t notifyCameraOpening(); 510 // Notify rest of system/apps about camera starting to stream data, and confirm appops 511 virtual status_t startCameraStreamingOps(); 512 // Notify rest of system/apps about camera stopping streaming data 513 virtual status_t finishCameraStreamingOps(); 514 // Notify rest of system/apps about camera closing 515 virtual status_t notifyCameraClosing(); 516 // Handle errors for start/checkOps, startDataDelivery 517 virtual status_t handleAppOpMode(int32_t mode); 518 virtual status_t handlePermissionResult( 519 PermissionChecker::PermissionResult result); 520 // Just notify camera appops to trigger unblocking dialog if sensor 521 // privacy is enabled and camera mute is not supported 522 virtual status_t noteAppOp(); 523 524 std::unique_ptr<AppOpsManager> mAppOpsManager = nullptr; 525 526 class OpsCallback : public BnAppOpsCallback { 527 public: 528 explicit OpsCallback(wp<BasicClient> client); 529 virtual void opChanged(int32_t op, const String16& packageName); 530 531 private: 532 wp<BasicClient> mClient; 533 534 }; // class OpsCallback 535 536 sp<OpsCallback> mOpsCallback; 537 // Track if the camera is currently active. 538 bool mCameraOpen; 539 // Track if the camera is currently streaming. 540 bool mCameraStreaming; 541 542 // IAppOpsCallback interface, indirected through opListener 543 virtual void opChanged(int32_t op, const String16& packageName); 544 }; // class BasicClient 545 546 class Client : public hardware::BnCamera, public BasicClient 547 { 548 public: 549 typedef hardware::ICameraClient TCamCallbacks; 550 551 // ICamera interface (see ICamera for details) 552 virtual binder::Status disconnect(); 553 virtual status_t connect(const sp<hardware::ICameraClient>& client) = 0; 554 virtual status_t lock() = 0; 555 virtual status_t unlock() = 0; 556 virtual status_t setPreviewTarget(const sp<SurfaceType>& target) = 0; 557 virtual void setPreviewCallbackFlag(int flag) = 0; 558 virtual status_t setPreviewCallbackTarget(const sp<SurfaceType>& target) = 0; 559 virtual status_t startPreview() = 0; 560 virtual void stopPreview() = 0; 561 virtual bool previewEnabled() = 0; 562 virtual status_t setVideoBufferMode(int32_t videoBufferMode) = 0; 563 virtual status_t startRecording() = 0; 564 virtual void stopRecording() = 0; 565 virtual bool recordingEnabled() = 0; 566 virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0; 567 virtual status_t autoFocus() = 0; 568 virtual status_t cancelAutoFocus() = 0; 569 virtual status_t takePicture(int msgType) = 0; 570 virtual status_t setParameters(const String8& params) = 0; 571 virtual String8 getParameters() const = 0; 572 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0; 573 virtual status_t setVideoTarget(const sp<SurfaceType>& target) = 0; 574 575 // Interface used by CameraService 576 Client(const sp<CameraService>& cameraService, 577 const sp<hardware::ICameraClient>& cameraClient, 578 std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils, 579 const AttributionSourceState& clientAttribution, int callingPid, 580 bool systemNativeClient, const std::string& cameraIdStr, int api1CameraId, 581 int cameraFacing, int sensorOrientation, int servicePid, int rotationOverride, 582 bool sharedMode); 583 ~Client(); 584 585 // return our camera client getRemoteCallback()586 const sp<hardware::ICameraClient>& getRemoteCallback() { 587 return mRemoteCallback; 588 } 589 asBinderWrapper()590 virtual sp<IBinder> asBinderWrapper() { 591 return asBinder(this); 592 } 593 594 virtual void notifyError(int32_t errorCode, 595 const CaptureResultExtras& resultExtras); 596 597 // Check what API level is used for this client. This is used to determine which 598 // superclass this can be cast to. 599 virtual bool canCastToApiClient(apiLevel level) const; 600 setImageDumpMask(int)601 void setImageDumpMask(int /*mask*/) { } 602 protected: 603 // Initialized in constructor 604 605 // - The app-side Binder interface to receive callbacks from us 606 sp<hardware::ICameraClient> mRemoteCallback; 607 608 int mCameraId; // All API1 clients use integer camera IDs 609 }; // class Client 610 611 /** 612 * A listener class that implements the LISTENER interface for use with a ClientManager, and 613 * implements the following methods: 614 * void onClientRemoved(const ClientDescriptor<KEY, VALUE>& descriptor); 615 * void onClientAdded(const ClientDescriptor<KEY, VALUE>& descriptor); 616 */ 617 class ClientEventListener { 618 public: 619 void onClientAdded(const resource_policy::ClientDescriptor<std::string, 620 sp<CameraService::BasicClient>>& descriptor); 621 void onClientRemoved(const resource_policy::ClientDescriptor<std::string, 622 sp<CameraService::BasicClient>>& descriptor); 623 }; // class ClientEventListener 624 625 typedef std::shared_ptr<resource_policy::ClientDescriptor<std::string, 626 sp<CameraService::BasicClient>>> DescriptorPtr; 627 628 /** 629 * A container class for managing active camera clients that are using HAL devices. Active 630 * clients are represented by ClientDescriptor objects that contain strong pointers to the 631 * actual BasicClient subclass binder interface implementation. 632 * 633 * This class manages the eviction behavior for the camera clients. See the parent class 634 * implementation in utils/ClientManager for the specifics of this behavior. 635 */ 636 class CameraClientManager : public resource_policy::ClientManager<std::string, 637 sp<CameraService::BasicClient>, ClientEventListener> { 638 public: 639 CameraClientManager(); 640 virtual ~CameraClientManager(); 641 642 // Bring all remove() functions into scope 643 using ClientManager::remove; 644 645 virtual void remove(const DescriptorPtr& value) override; 646 647 /** 648 * Return a strong pointer to the active BasicClient for this camera ID, or an empty 649 * if none exists. 650 */ 651 sp<CameraService::BasicClient> getCameraClient(const std::string& id) const; 652 653 /** 654 * Return a string describing the current state. 655 */ 656 std::string toString() const; 657 658 /** 659 * Make a ClientDescriptor object wrapping the given BasicClient strong pointer. 660 */ 661 static DescriptorPtr makeClientDescriptor(const std::string& key, 662 const sp<BasicClient>& value, int32_t cost, 663 const std::set<std::string>& conflictingKeys, int32_t score, 664 int32_t ownerId, int32_t state, int oomScoreOffset, bool systemNativeClient, 665 bool sharedMode); 666 667 /** 668 * Make a ClientDescriptor object wrapping the given BasicClient strong pointer with 669 * values intialized from a prior ClientDescriptor. 670 */ 671 static DescriptorPtr makeClientDescriptor(const sp<BasicClient>& value, 672 const CameraService::DescriptorPtr& partial, int oomScoreOffset, 673 bool systemNativeClient); 674 675 }; // class CameraClientManager 676 677 int32_t updateAudioRestriction(); 678 int32_t updateAudioRestrictionLocked(); 679 680 /** 681 * Returns true if the given client is the only client in the active clients list for a given 682 * camera. 683 * 684 * This method acquires mServiceLock. 685 */ 686 bool isOnlyClient(const BasicClient* client); 687 688 689 private: 690 691 // TODO: b/263304156 update this to make use of a death callback for more 692 // robust/fault tolerant logging getActivityManager()693 static const sp<IActivityManager>& getActivityManager() { 694 static const char* kActivityService = "activity"; 695 static const auto activityManager = []() -> sp<IActivityManager> { 696 const sp<IServiceManager> sm(defaultServiceManager()); 697 if (sm != nullptr) { 698 return interface_cast<IActivityManager>(sm->checkService(String16(kActivityService))); 699 } 700 return nullptr; 701 }(); 702 return activityManager; 703 } 704 705 static int32_t getUidProcessState(int32_t uid); 706 707 /** 708 * Typesafe version of device status, containing both the HAL-layer and the service interface- 709 * layer values. 710 */ 711 enum class StatusInternal : int32_t { 712 NOT_PRESENT = static_cast<int32_t>(CameraDeviceStatus::NOT_PRESENT), 713 PRESENT = static_cast<int32_t>(CameraDeviceStatus::PRESENT), 714 ENUMERATING = static_cast<int32_t>(CameraDeviceStatus::ENUMERATING), 715 NOT_AVAILABLE = static_cast<int32_t>(hardware::ICameraServiceListener::STATUS_NOT_AVAILABLE), 716 UNKNOWN = static_cast<int32_t>(hardware::ICameraServiceListener::STATUS_UNKNOWN) 717 }; 718 719 friend int32_t format_as(StatusInternal s); 720 721 /** 722 * Container class for the state of each logical camera device, including: ID, status, and 723 * dependencies on other devices. The mapping of camera ID -> state saved in mCameraStates 724 * represents the camera devices advertised by the HAL (and any USB devices, when we add 725 * those). 726 * 727 * This container does NOT represent an active camera client. These are represented using 728 * the ClientDescriptors stored in mActiveClientManager. 729 */ 730 class CameraState { 731 public: 732 733 /** 734 * Make a new CameraState and set the ID, cost, and conflicting devices using the values 735 * returned in the HAL's camera_info struct for each device. 736 */ 737 CameraState(const std::string& id, int cost, const std::set<std::string>& conflicting, 738 SystemCameraKind deviceKind, const std::vector<std::string>& physicalCameras); 739 virtual ~CameraState(); 740 741 /** 742 * Return the status for this device. 743 * 744 * This method acquires mStatusLock. 745 */ 746 StatusInternal getStatus() const; 747 748 /** 749 * This function updates the status for this camera device, unless the given status 750 * is in the given list of rejected status states, and execute the function passed in 751 * with a signature onStatusUpdateLocked(const std::string&, int32_t) 752 * if the status has changed. 753 * 754 * This method is idempotent, and will not result in the function passed to 755 * onStatusUpdateLocked being called more than once for the same arguments. 756 * This method aquires mStatusLock. 757 */ 758 template<class Func> 759 void updateStatus(StatusInternal status, 760 const std::string& cameraId, 761 std::initializer_list<StatusInternal> rejectSourceStates, 762 Func onStatusUpdatedLocked); 763 764 /** 765 * Return the last set CameraParameters object generated from the information returned by 766 * the HAL for this device (or an empty CameraParameters object if none has been set). 767 */ 768 CameraParameters getShimParams() const; 769 770 /** 771 * Set the CameraParameters for this device. 772 */ 773 void setShimParams(const CameraParameters& params); 774 775 /** 776 * Return the resource_cost advertised by the HAL for this device. 777 */ 778 int getCost() const; 779 780 /** 781 * Return a set of the IDs of conflicting devices advertised by the HAL for this device. 782 */ 783 std::set<std::string> getConflicting() const; 784 785 /** 786 * Return the kind (SystemCameraKind) of this camera device. 787 */ 788 SystemCameraKind getSystemCameraKind() const; 789 790 /** 791 * Return whether this camera is a logical multi-camera and has a 792 * particular physical sub-camera. 793 */ 794 bool containsPhysicalCamera(const std::string& physicalCameraId) const; 795 796 /** 797 * Add/Remove the unavailable physical camera ID. 798 */ 799 bool addUnavailablePhysicalId(const std::string& physicalId); 800 bool removeUnavailablePhysicalId(const std::string& physicalId); 801 802 /** 803 * Set and get client package name. 804 */ 805 void setClientPackage(const std::string& clientPackage); 806 std::string getClientPackage() const; 807 808 void addClientPackage(const std::string& clientPackage); 809 void removeClientPackage(const std::string& clientPackage); 810 std::set<std::string> getClientPackages() const; 811 812 /** 813 * Return the unavailable physical ids for this device. 814 * 815 * This method acquires mStatusLock. 816 */ 817 std::vector<std::string> getUnavailablePhysicalIds() const; 818 private: 819 const std::string mId; 820 StatusInternal mStatus; // protected by mStatusLock 821 const int mCost; 822 std::set<std::string> mConflicting; 823 std::set<std::string> mUnavailablePhysicalIds; 824 std::set<std::string> mClientPackages; 825 mutable Mutex mStatusLock; 826 CameraParameters mShimParams; 827 const SystemCameraKind mSystemCameraKind; 828 const std::vector<std::string> mPhysicalCameras; // Empty if not a logical multi-camera 829 }; // class CameraState 830 831 // Observer for UID lifecycle enforcing that UIDs in idle 832 // state cannot use the camera to protect user privacy. 833 class UidPolicy : 834 public BnUidObserver, 835 public virtual IBinder::DeathRecipient, 836 public virtual IServiceManager::LocalRegistrationCallback { 837 public: UidPolicy(sp<CameraService> service)838 explicit UidPolicy(sp<CameraService> service) 839 : mRegistered(false), mService(service) {} 840 841 void registerSelf(); 842 void unregisterSelf(); 843 844 bool isUidActive(uid_t uid, const std::string &callingPackage); 845 int32_t getProcState(uid_t uid); 846 847 // IUidObserver 848 void onUidGone(uid_t uid, bool disabled) override; 849 void onUidActive(uid_t uid) override; 850 void onUidIdle(uid_t uid, bool disabled) override; 851 void onUidStateChanged(uid_t uid, int32_t procState, int64_t procStateSeq, 852 int32_t capability) override; 853 void onUidProcAdjChanged(uid_t uid, int adj) override; 854 855 void addOverrideUid(uid_t uid, const std::string &callingPackage, bool active); 856 void removeOverrideUid(uid_t uid, const std::string &callingPackage); 857 858 void registerMonitorUid(uid_t uid, bool openCamera); 859 void unregisterMonitorUid(uid_t uid, bool closeCamera); 860 861 // Implementation of IServiceManager::LocalRegistrationCallback 862 virtual void onServiceRegistration(const String16& name, 863 const sp<IBinder>& binder) override; 864 // IBinder::DeathRecipient implementation 865 virtual void binderDied(const wp<IBinder> &who); 866 private: 867 bool isUidActiveLocked(uid_t uid, const std::string &callingPackage); 868 int32_t getProcStateLocked(uid_t uid); 869 void updateOverrideUid(uid_t uid, const std::string &callingPackage, bool active, 870 bool insert); 871 void registerWithActivityManager(); 872 873 struct MonitoredUid { 874 int32_t procState; 875 int32_t procAdj; 876 bool hasCamera; 877 size_t refCount; 878 }; 879 880 Mutex mUidLock; 881 bool mRegistered; 882 ActivityManager mAm; 883 wp<CameraService> mService; 884 std::unordered_set<uid_t> mActiveUids; 885 // Monitored uid map 886 std::unordered_map<uid_t, MonitoredUid> mMonitoredUids; 887 std::unordered_map<uid_t, bool> mOverrideUids; 888 sp<IBinder> mObserverToken; 889 }; // class UidPolicy 890 891 // If sensor privacy is enabled then all apps, including those that are active, should be 892 // prevented from accessing the camera. 893 class SensorPrivacyPolicy : public hardware::BnSensorPrivacyListener, 894 public virtual IBinder::DeathRecipient, 895 public virtual IServiceManager::LocalRegistrationCallback, 896 public AttributionAndPermissionUtilsEncapsulator { 897 public: SensorPrivacyPolicy(wp<CameraService> service,std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils)898 explicit SensorPrivacyPolicy(wp<CameraService> service, 899 std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils) 900 : AttributionAndPermissionUtilsEncapsulator(attributionAndPermissionUtils), 901 mService(service), 902 mSensorPrivacyEnabled(false), 903 mCameraPrivacyState(SensorPrivacyManager::DISABLED), mRegistered(false) {} 904 905 void registerSelf(); 906 void unregisterSelf(); 907 908 bool isSensorPrivacyEnabled(); 909 bool isCameraPrivacyEnabled(); 910 int getCameraPrivacyState(); 911 bool isCameraPrivacyEnabled(const String16& packageName); 912 913 binder::Status onSensorPrivacyChanged(int toggleType, int sensor, 914 bool enabled); 915 binder::Status onSensorPrivacyStateChanged(int toggleType, int sensor, int state); 916 917 // Implementation of IServiceManager::LocalRegistrationCallback 918 virtual void onServiceRegistration(const String16& name, 919 const sp<IBinder>& binder) override; 920 // IBinder::DeathRecipient implementation 921 virtual void binderDied(const wp<IBinder> &who); 922 923 private: 924 SensorPrivacyManager mSpm; 925 wp<CameraService> mService; 926 Mutex mSensorPrivacyLock; 927 bool mSensorPrivacyEnabled; 928 int mCameraPrivacyState; 929 bool mRegistered; 930 931 bool hasCameraPrivacyFeature(); 932 void registerWithSensorPrivacyManager(); 933 }; 934 935 sp<UidPolicy> mUidPolicy; 936 937 sp<SensorPrivacyPolicy> mSensorPrivacyPolicy; 938 939 std::shared_ptr<CameraServiceProxyWrapper> mCameraServiceProxyWrapper; 940 941 // Delay-load the Camera HAL module 942 virtual void onFirstRef(); 943 944 // Eumerate all camera providers in the system 945 status_t enumerateProviders(); 946 947 // Add/remove a new camera to camera and torch state lists or remove an unplugged one 948 // Caller must not hold mServiceLock 949 void addStates(const std::string& id); 950 void removeStates(const std::string& id); 951 952 // Check if we can connect, before we acquire the service lock. 953 binder::Status validateConnectLocked(const std::string& cameraId, 954 const AttributionSourceState& clientAttribution, 955 bool sharedMode) const; 956 binder::Status validateClientPermissionsLocked( 957 const std::string& cameraId, const AttributionSourceState& clientAttribution, 958 bool sharedMode) const; 959 960 void logConnectionAttempt(int clientPid, const std::string& clientPackageName, 961 const std::string& cameraId, apiLevel effectiveApiLevel) const; 962 963 bool isCameraPrivacyEnabled(const String16& packageName,const std::string& cameraId, 964 int clientPid, int ClientUid); 965 966 // Handle active client evictions, and update service state. 967 // Only call with with mServiceLock held. 968 status_t handleEvictionsLocked(const std::string& cameraId, int clientPid, 969 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, 970 const std::string& packageName, int scoreOffset, bool systemNativeClient, bool sharedMode, 971 /*out*/ 972 sp<BasicClient>* client, 973 std::shared_ptr<resource_policy::ClientDescriptor<std::string, sp<BasicClient>>>* partial); 974 975 // Should an operation attempt on a cameraId be rejected ? (this can happen 976 // under various conditions. For example if a camera device is advertised as 977 // system only or hidden secure camera, amongst possible others. 978 bool shouldRejectSystemCameraConnection(const std::string& cameraId) const; 979 980 // Should a device status update be skipped for a particular camera device ? (this can happen 981 // under various conditions. For example if a camera device is advertised as 982 // system only or hidden secure camera, amongst possible others. 983 bool shouldSkipStatusUpdates(SystemCameraKind systemCameraKind, bool isVendorListener, 984 int clientPid, int clientUid); 985 986 // Gets the kind of camera device (i.e public, hidden secure or system only) 987 // getSystemCameraKind() needs mInterfaceMutex which might lead to deadlocks 988 // if held along with mStatusListenerLock (depending on lock ordering, b/141756275), it is 989 // recommended that we don't call this function with mStatusListenerLock held. 990 status_t getSystemCameraKind(const std::string& cameraId, SystemCameraKind *kind) const; 991 992 // Update the set of API1Compatible camera devices without including system 993 // cameras and secure cameras. This is used for hiding system only cameras 994 // from clients using camera1 api and not having android.permission.SYSTEM_CAMERA. 995 // This function expects @param normalDeviceIds, to have normalDeviceIds 996 // sorted in alpha-numeric order. 997 void filterAPI1SystemCameraLocked(const std::vector<std::string> &normalDeviceIds); 998 999 // Single implementation shared between the various connect calls 1000 template <class CALLBACK, class CLIENT> 1001 binder::Status connectHelper(const sp<CALLBACK>& cameraCb, const std::string& cameraId, 1002 int api1CameraId, const AttributionSourceState& clientAttribution, 1003 bool systemNativeClient, apiLevel effectiveApiLevel, 1004 bool shimUpdateOnly, int scoreOffset, int targetSdkVersion, 1005 int rotationOverride, bool forceSlowJpegMode, 1006 const std::string& originalCameraId, bool isNonSystemNdk, 1007 bool sharedMode, bool isVendorClient, 1008 /*out*/ sp<CLIENT>& device); 1009 1010 binder::Status connectDeviceImpl( 1011 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb, 1012 const std::string& cameraId, int scoreOffset, int targetSdkVersion, 1013 int rotationOverride, const AttributionSourceState& clientAttribution, 1014 int32_t devicePolicy, bool sharedMode, bool isVendorClient, 1015 /*out*/ 1016 sp<hardware::camera2::ICameraDeviceUser>* device); 1017 1018 // Lock guarding camera service state 1019 Mutex mServiceLock; 1020 1021 // Condition to use with mServiceLock, used to handle simultaneous connect calls from clients 1022 std::shared_ptr<WaitableMutexWrapper> mServiceLockWrapper; 1023 1024 // Return NO_ERROR if the device with a give ID can be connected to 1025 status_t checkIfDeviceIsUsable(const std::string& cameraId) const; 1026 1027 // Container for managing currently active application-layer clients 1028 CameraClientManager mActiveClientManager; 1029 1030 // Adds client logs during open session to the file pointed by fd. 1031 void dumpOpenSessionClientLogs(int fd, const Vector<String16>& args, 1032 const std::string& cameraId); 1033 1034 // Adds client logs during closed session to the file pointed by fd. 1035 void dumpClosedSessionClientLogs(int fd, const std::string& cameraId); 1036 1037 binder::Status isSessionConfigurationWithParametersSupportedUnsafe( 1038 const std::string& cameraId, const SessionConfiguration& sessionConfiguration, 1039 bool overrideForPerfClass, /*out*/ bool* supported); 1040 1041 // Mapping from camera ID -> state for each device, map is protected by mCameraStatesLock 1042 std::map<std::string, std::shared_ptr<CameraState>> mCameraStates; 1043 1044 // Mutex guarding mCameraStates map 1045 mutable Mutex mCameraStatesLock; 1046 1047 /** 1048 * Resolve the (potentially remapped) camera id for the given input camera id and the given 1049 * device id and device policy (for the device associated with the context of the caller). 1050 * 1051 * For any context associated with a virtual device with custom camera policy, this will return 1052 * the actual camera id if inputCameraId corresponds to the mapped id of a virtual camera 1053 * (for virtual devices with custom camera policy, the back and front virtual cameras of that 1054 * device would have 0 and 1 respectively as their mapped camera id). 1055 */ 1056 std::optional<std::string> resolveCameraId( 1057 const std::string& inputCameraId, 1058 int32_t deviceId, 1059 int32_t devicePolicy); 1060 1061 // Circular buffer for storing event logging for dumps 1062 RingBuffer<std::string> mEventLog; 1063 Mutex mLogLock; 1064 1065 // set of client package names to watch. if this set contains 'all', then all clients will 1066 // be watched. Access should be guarded by mLogLock 1067 std::set<std::string> mWatchedClientPackages; 1068 // cache of last monitored tags dump immediately before the client disconnects. If a client 1069 // re-connects, its entry is not updated until it disconnects again. Access should be guarded 1070 // by mLogLock 1071 std::map<std::string, std::string> mWatchedClientsDumpCache; 1072 1073 // The last monitored tags set by client 1074 std::string mMonitorTags; 1075 1076 // Currently allowed user IDs 1077 std::set<userid_t> mAllowedUsers; 1078 1079 /** 1080 * Get the camera state for a given camera id. 1081 * 1082 * This acquires mCameraStatesLock. 1083 */ 1084 std::shared_ptr<CameraService::CameraState> getCameraState(const std::string& cameraId) const; 1085 1086 /** 1087 * Evict client who's remote binder has died. Returns true if this client was in the active 1088 * list and was disconnected. 1089 * 1090 * This method acquires mServiceLock. 1091 */ 1092 bool evictClientIdByRemote(const wp<IBinder>& cameraClient); 1093 1094 /** 1095 * Remove the given client from the active clients list; does not disconnect the client. 1096 * 1097 * This method acquires mServiceLock. 1098 */ 1099 void removeByClient(const BasicClient* client); 1100 1101 /** 1102 * Add new client to active clients list after conflicting clients have disconnected using the 1103 * values set in the partial descriptor passed in to construct the actual client descriptor. 1104 * This is typically called at the end of a connect call. 1105 * 1106 * This method must be called with mServiceLock held. 1107 */ 1108 void finishConnectLocked(const sp<BasicClient>& client, const DescriptorPtr& desc, 1109 int oomScoreOffset, bool systemNativeClient); 1110 1111 /** 1112 * Returns the underlying camera Id string mapped to a camera id int 1113 * Empty string is returned when the cameraIdInt is invalid. 1114 */ 1115 std::string cameraIdIntToStr(int cameraIdInt, int32_t deviceId, int32_t devicePolicy); 1116 1117 /** 1118 * Returns the underlying camera Id string mapped to a camera id int 1119 * Empty string is returned when the cameraIdInt is invalid. 1120 */ 1121 std::string cameraIdIntToStrLocked(int cameraIdInt, int32_t deviceId, int32_t devicePolicy); 1122 1123 /** 1124 * Remove all the clients corresponding to the given camera id from the list of active clients. 1125 * If none exists, return an empty strongpointer. 1126 * 1127 * This method must be called with mServiceLock held. 1128 */ 1129 std::vector<sp<CameraService::BasicClient>> removeClientsLocked(const std::string& cameraId); 1130 1131 /** 1132 * Handle a notification that the current device user has changed. 1133 */ 1134 void doUserSwitch(const std::vector<int32_t>& newUserIds); 1135 1136 /** 1137 * Add an event log message. 1138 */ 1139 void logEvent(const std::string &event); 1140 1141 /** 1142 * Add an event log message that a client has been disconnected. 1143 */ 1144 void logDisconnected(const std::string &cameraId, int clientPid, 1145 const std::string &clientPackage); 1146 1147 /** 1148 * Add an event log message that a client has been disconnected from offline device. 1149 */ 1150 void logDisconnectedOffline(const std::string &cameraId, int clientPid, 1151 const std::string &clientPackage); 1152 1153 /** 1154 * Add an event log message that an offline client has been connected. 1155 */ 1156 void logConnectedOffline(const std::string &cameraId, int clientPid, 1157 const std::string &clientPackage); 1158 1159 /** 1160 * Add an event log message that a client has been connected. 1161 */ 1162 void logConnected(const std::string &cameraId, int clientPid, const std::string &clientPackage); 1163 1164 /** 1165 * Add an event log message that a client's connect attempt has been rejected. 1166 */ 1167 void logRejected(const std::string &cameraId, int clientPid, const std::string &clientPackage, 1168 const std::string &reason); 1169 1170 /** 1171 * Add an event log message when a client calls setTorchMode succesfully. 1172 */ 1173 void logTorchEvent(const std::string &cameraId, const std::string &torchState, int clientPid); 1174 1175 /** 1176 * Add an event log message that the current device user has been switched. 1177 */ 1178 void logUserSwitch(const std::set<userid_t>& oldUserIds, 1179 const std::set<userid_t>& newUserIds); 1180 1181 /** 1182 * Add an event log message that a device has been removed by the HAL 1183 */ 1184 void logDeviceRemoved(const std::string &cameraId, const std::string &reason); 1185 1186 /** 1187 * Add an event log message that a device has been added by the HAL 1188 */ 1189 void logDeviceAdded(const std::string &cameraId, const std::string &reason); 1190 1191 /** 1192 * Add an event log message that a client has unexpectedly died. 1193 */ 1194 void logClientDied(int clientPid, const std::string &reason); 1195 1196 /** 1197 * Add a event log message that a serious service-level error has occured 1198 * The errorCode should be one of the Android Errors 1199 */ 1200 void logServiceError(const std::string &msg, int errorCode); 1201 1202 /** 1203 * Dump the event log to an FD 1204 */ 1205 void dumpEventLog(int fd); 1206 1207 void cacheClientTagDumpIfNeeded(const std::string &cameraId, BasicClient *client); 1208 1209 /** 1210 * This method will acquire mServiceLock 1211 */ 1212 void updateCameraNumAndIds(); 1213 1214 /** 1215 * Filter camera characteristics for S Performance class primary cameras. 1216 * mServiceLock should be locked. 1217 */ 1218 void filterSPerfClassCharacteristicsLocked(); 1219 1220 // File descriptor to temp file used for caching previous open 1221 // session dumpsys info. 1222 int mMemFd; 1223 1224 // Number of camera devices (excluding hidden secure cameras) 1225 int mNumberOfCameras; 1226 // Number of camera devices (excluding hidden secure cameras and 1227 // system cameras) 1228 int mNumberOfCamerasWithoutSystemCamera; 1229 1230 std::vector<std::string> mNormalDeviceIds; 1231 std::vector<std::string> mNormalDeviceIdsWithoutSystemCamera; 1232 std::set<std::string> mPerfClassPrimaryCameraIds; 1233 1234 // sounds 1235 sp<MediaPlayer> newMediaPlayer(const char *file); 1236 1237 Mutex mSoundLock; 1238 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS]; 1239 int mSoundRef; // reference count (release all MediaPlayer when 0) 1240 1241 // Basic flag on whether the camera subsystem is in a usable state 1242 bool mInitialized; 1243 1244 sp<CameraProviderManager> mCameraProviderManager; 1245 1246 class ServiceListener : public virtual IBinder::DeathRecipient { 1247 public: ServiceListener(sp<CameraService> parent,sp<hardware::ICameraServiceListener> listener,int uid,int pid,bool isVendorClient,bool openCloseCallbackAllowed)1248 ServiceListener(sp<CameraService> parent, sp<hardware::ICameraServiceListener> listener, 1249 int uid, int pid, bool isVendorClient, bool openCloseCallbackAllowed) 1250 : mParent(parent), mListener(listener), mListenerUid(uid), mListenerPid(pid), 1251 mIsVendorListener(isVendorClient), 1252 mOpenCloseCallbackAllowed(openCloseCallbackAllowed) { } 1253 initialize(bool isProcessLocalTest)1254 status_t initialize(bool isProcessLocalTest) { 1255 if (isProcessLocalTest) { 1256 return OK; 1257 } 1258 return IInterface::asBinder(mListener)->linkToDeath(this); 1259 } 1260 1261 template<typename... args_t> handleBinderStatus(const binder::Status & ret,const char * logOnError,args_t...args)1262 void handleBinderStatus(const binder::Status &ret, const char *logOnError, 1263 args_t... args) { 1264 if (!ret.isOk() && 1265 (ret.exceptionCode() != binder::Status::Exception::EX_TRANSACTION_FAILED 1266 || !mLastTransactFailed)) { 1267 ALOGE(logOnError, args...); 1268 } 1269 1270 // If the transaction failed, the process may have died (or other things, see 1271 // b/28321379). Mute consecutive errors from this listener to avoid log spam. 1272 if (ret.exceptionCode() == binder::Status::Exception::EX_TRANSACTION_FAILED) { 1273 if (!mLastTransactFailed) { 1274 ALOGE("%s: Muting similar errors from listener %d:%d", __FUNCTION__, 1275 mListenerUid, mListenerPid); 1276 } 1277 mLastTransactFailed = true; 1278 } else { 1279 // Reset mLastTransactFailed when binder becomes healthy again. 1280 mLastTransactFailed = false; 1281 } 1282 } 1283 binderDied(const wp<IBinder> &)1284 virtual void binderDied(const wp<IBinder> &/*who*/) { 1285 auto parent = mParent.promote(); 1286 if (parent.get() != nullptr) { 1287 parent->removeListener(mListener); 1288 } 1289 } 1290 getListenerUid()1291 int getListenerUid() { return mListenerUid; } getListenerPid()1292 int getListenerPid() { return mListenerPid; } getListener()1293 sp<hardware::ICameraServiceListener> getListener() { return mListener; } isVendorListener()1294 bool isVendorListener() { return mIsVendorListener; } isOpenCloseCallbackAllowed()1295 bool isOpenCloseCallbackAllowed() { return mOpenCloseCallbackAllowed; } 1296 1297 private: 1298 wp<CameraService> mParent; 1299 sp<hardware::ICameraServiceListener> mListener; 1300 int mListenerUid = -1; 1301 int mListenerPid = -1; 1302 bool mIsVendorListener = false; 1303 bool mOpenCloseCallbackAllowed = false; 1304 1305 // Flag for preventing log spam when binder becomes unhealthy 1306 bool mLastTransactFailed = false; 1307 }; 1308 1309 // Guarded by mStatusListenerMutex 1310 std::vector<sp<ServiceListener>> mListenerList; 1311 1312 Mutex mStatusListenerLock; 1313 1314 /** 1315 * Update the status for the given camera id (if that device exists), and broadcast the 1316 * status update to all current ICameraServiceListeners if the status has changed. Any 1317 * statuses in rejectedSourceStates will be ignored. 1318 * 1319 * This method must be idempotent. 1320 * This method acquires mStatusLock and mStatusListenerLock. 1321 * For any virtual camera, this method must pass its mapped camera id and device id to 1322 * ICameraServiceListeners (using mVirtualDeviceCameraIdMapper). 1323 */ 1324 void updateStatus(StatusInternal status, 1325 const std::string& cameraId, 1326 std::initializer_list<StatusInternal> 1327 rejectedSourceStates); 1328 void updateStatus(StatusInternal status, 1329 const std::string& cameraId); 1330 1331 /** 1332 * Update the opened/closed status of the given camera id. 1333 * 1334 * This method acqiures mStatusListenerLock. 1335 */ 1336 void updateOpenCloseStatus(const std::string& cameraId, bool open, 1337 const std::string& packageName, bool sharedMode); 1338 1339 // flashlight control 1340 sp<CameraFlashlight> mFlashlight; 1341 // guard mTorchStatusMap 1342 Mutex mTorchStatusMutex; 1343 // guard mTorchClientMap 1344 Mutex mTorchClientMapMutex; 1345 // guard mTorchUidMap 1346 Mutex mTorchUidMapMutex; 1347 // camera id -> torch status 1348 KeyedVector<std::string, TorchModeStatus> 1349 mTorchStatusMap; 1350 // camera id -> torch client binder 1351 // only store the last client that turns on each camera's torch mode 1352 KeyedVector<std::string, sp<IBinder>> mTorchClientMap; 1353 // camera id -> [incoming uid, current uid] pair 1354 std::map<std::string, std::pair<int, int>> mTorchUidMap; 1355 1356 // check and handle if torch client's process has died 1357 void handleTorchClientBinderDied(const wp<IBinder> &who); 1358 1359 // handle torch mode status change and invoke callbacks. mTorchStatusMutex 1360 // should be locked. 1361 void onTorchStatusChangedLocked(const std::string& cameraId, 1362 TorchModeStatus newStatus, 1363 SystemCameraKind systemCameraKind); 1364 1365 // get a camera's torch status. mTorchStatusMutex should be locked. 1366 status_t getTorchStatusLocked(const std::string &cameraId, 1367 TorchModeStatus *status) const; 1368 1369 // set a camera's torch status. mTorchStatusMutex should be locked. 1370 status_t setTorchStatusLocked(const std::string &cameraId, 1371 TorchModeStatus status); 1372 1373 // notify physical camera status when the physical camera is public. 1374 // Expects mStatusListenerLock to be locked. 1375 void notifyPhysicalCameraStatusLocked(int32_t status, const std::string& physicalCameraId, 1376 const std::list<std::string>& logicalCameraIds, SystemCameraKind deviceKind, 1377 int32_t virtualDeviceId); 1378 1379 // get list of logical cameras which are backed by physicalCameraId 1380 std::list<std::string> getLogicalCameras(const std::string& physicalCameraId); 1381 1382 1383 // IBinder::DeathRecipient implementation 1384 virtual void binderDied(const wp<IBinder> &who); 1385 1386 /** 1387 * Initialize and cache the metadata used by the HAL1 shim for a given cameraId. 1388 * 1389 * Sets Status to a service-specific error on failure 1390 */ 1391 binder::Status initializeShimMetadata(int cameraId); 1392 1393 /** 1394 * Get the cached CameraParameters for the camera. If they haven't been 1395 * cached yet, then initialize them for the first time. 1396 * 1397 * Sets Status to a service-specific error on failure 1398 */ 1399 binder::Status getLegacyParametersLazy(int cameraId, /*out*/CameraParameters* parameters); 1400 1401 // Blocks all clients from the UID 1402 void blockClientsForUid(uid_t uid); 1403 1404 // Blocks all active clients. 1405 void blockAllClients(); 1406 1407 // Blocks clients whose privacy is enabled. 1408 void blockPrivacyEnabledClients(); 1409 1410 // Overrides the UID state as if it is idle 1411 status_t handleSetUidState(const Vector<String16>& args, int err); 1412 1413 // Clears the override for the UID state 1414 status_t handleResetUidState(const Vector<String16>& args, int err); 1415 1416 // Gets the UID state 1417 status_t handleGetUidState(const Vector<String16>& args, int out, int err); 1418 1419 // Set the rotate-and-crop AUTO override behavior 1420 status_t handleSetRotateAndCrop(const Vector<String16>& args); 1421 1422 // Get the rotate-and-crop AUTO override behavior 1423 status_t handleGetRotateAndCrop(int out); 1424 1425 // Set the autoframing AUTO override behaviour. 1426 status_t handleSetAutoframing(const Vector<String16>& args); 1427 1428 // Get the autoframing AUTO override behaviour 1429 status_t handleGetAutoframing(int out); 1430 1431 // Set the mask for image dump to disk 1432 status_t handleSetImageDumpMask(const Vector<String16>& args); 1433 1434 // Get the mask for image dump to disk 1435 status_t handleGetImageDumpMask(int out); 1436 1437 // Set the camera mute state 1438 status_t handleSetCameraMute(const Vector<String16>& args); 1439 1440 // Set the stream use case overrides 1441 status_t handleSetStreamUseCaseOverrides(const Vector<String16>& args); 1442 1443 // Clear the stream use case overrides 1444 void handleClearStreamUseCaseOverrides(); 1445 1446 // Set or clear the zoom override flag 1447 status_t handleSetZoomOverride(const Vector<String16>& args); 1448 1449 // Set Camera Id remapping using 'cmd' 1450 status_t handleCameraIdRemapping(const Vector<String16>& args, int errFd); 1451 1452 // Handle 'watch' command as passed through 'cmd' 1453 status_t handleWatchCommand(const Vector<String16> &args, int inFd, int outFd); 1454 1455 // Set the camera service watchdog 1456 status_t handleSetCameraServiceWatchdog(const Vector<String16>& args); 1457 1458 // Enable tag monitoring of the given tags in provided clients 1459 status_t startWatchingTags(const Vector<String16> &args, int outFd); 1460 1461 // Disable tag monitoring 1462 status_t stopWatchingTags(int outFd); 1463 1464 // Clears mWatchedClientsDumpCache 1465 status_t clearCachedMonitoredTagDumps(int outFd); 1466 1467 // Print events of monitored tags in all cached and attached clients 1468 status_t printWatchedTags(int outFd); 1469 1470 // Print events of monitored tags in all attached clients as they are captured. New events are 1471 // fetched every `refreshMillis` ms 1472 // NOTE: This function does not terminate until user passes '\n' to inFd. 1473 status_t printWatchedTagsUntilInterrupt(const Vector<String16> &args, int inFd, int outFd); 1474 1475 // Parses comma separated clients list and adds them to mWatchedClientPackages. 1476 // Does not acquire mLogLock before modifying mWatchedClientPackages. It is the caller's 1477 // responsibility to acquire mLogLock before calling this function. 1478 void parseClientsToWatchLocked(const std::string &clients); 1479 1480 // Prints the shell command help 1481 status_t printHelp(int out); 1482 1483 // Returns true if client should monitor tags based on the contents of mWatchedClientPackages. 1484 // Acquires mLogLock before querying mWatchedClientPackages. 1485 bool isClientWatched(const BasicClient *client); 1486 1487 // Returns true if client should monitor tags based on the contents of mWatchedClientPackages. 1488 // Does not acquire mLogLock before querying mWatchedClientPackages. It is the caller's 1489 // responsibility to acquire mLogLock before calling this functions. 1490 bool isClientWatchedLocked(const BasicClient *client); 1491 1492 // Filters out fingerprintable keys if the calling process does not have CAMERA permission. 1493 // Note: function caller should ensure that shouldRejectSystemCameraConnection is checked 1494 // for the calling process before calling this function. 1495 binder::Status filterSensitiveMetadataIfNeeded(const std::string& cameraId, 1496 CameraMetadata* metadata); 1497 1498 /** 1499 * Get the current system time as a formatted string. 1500 */ 1501 static std::string getFormattedCurrentTime(); 1502 1503 static binder::Status makeClient(const sp<CameraService>& cameraService, 1504 const sp<IInterface>& cameraCb, 1505 const AttributionSourceState& clientAttribution, 1506 int callingPid, bool systemNativeClient, 1507 const std::string& cameraId, int api1CameraId, int facing, 1508 int sensorOrientation, int servicePid, 1509 std::pair<int, IPCTransport> deviceVersionAndIPCTransport, 1510 apiLevel effectiveApiLevel, bool overrideForPerfClass, 1511 int rotationOverride, bool forceSlowJpegMode, 1512 const std::string& originalCameraId, bool sharedMode, 1513 bool isVendorClient, 1514 /*out*/ sp<BasicClient>* client); 1515 1516 static std::string toString(std::set<userid_t> intSet); 1517 static int32_t mapToInterface(TorchModeStatus status); 1518 static StatusInternal mapToInternal(CameraDeviceStatus status); 1519 static int32_t mapToInterface(StatusInternal status); 1520 1521 1522 void broadcastTorchModeStatus(const std::string& cameraId, 1523 TorchModeStatus status, SystemCameraKind systemCameraKind); 1524 1525 void broadcastTorchStrengthLevel(const std::string& cameraId, int32_t newTorchStrengthLevel); 1526 1527 void disconnectClient(const std::string& id, sp<BasicClient> clientToDisconnect); 1528 1529 void disconnectClients(const std::string& id, 1530 std::vector<sp<BasicClient>> clientsToDisconnect); 1531 1532 // Regular online and offline devices must not be in conflict at camera service layer. 1533 // Use separate keys for offline devices. 1534 static const std::string kOfflineDevice; 1535 1536 // Sentinel value to be stored in `mWatchedClientsPackages` to indicate that all clients should 1537 // be watched. 1538 static const std::string kWatchAllClientsFlag; 1539 1540 // TODO: right now each BasicClient holds one AppOpsManager instance. 1541 // We can refactor the code so all of clients share this instance 1542 AppOpsManager mAppOps; 1543 1544 // Aggreated audio restriction mode for all camera clients 1545 int32_t mAudioRestriction; 1546 1547 // Current override cmd rotate-and-crop mode; AUTO means no override 1548 uint8_t mOverrideRotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_AUTO; 1549 1550 // Current autoframing mode 1551 uint8_t mOverrideAutoframingMode = ANDROID_CONTROL_AUTOFRAMING_AUTO; 1552 1553 // Current image dump mask 1554 uint8_t mImageDumpMask = 0; 1555 1556 // Current camera mute mode 1557 bool mOverrideCameraMuteMode = false; 1558 1559 // Camera Service watchdog flag 1560 bool mCameraServiceWatchdogEnabled = true; 1561 1562 // Current stream use case overrides 1563 std::vector<int64_t> mStreamUseCaseOverrides; 1564 1565 // Current zoom override value 1566 int32_t mZoomOverrideValue = -1; 1567 1568 /** 1569 * A listener class that implements the IBinder::DeathRecipient interface 1570 * for use to call back the error state injected by the external camera, and 1571 * camera service can kill the injection when binder signals process death. 1572 */ 1573 class InjectionStatusListener : public virtual IBinder::DeathRecipient { 1574 public: InjectionStatusListener(sp<CameraService> parent)1575 InjectionStatusListener(sp<CameraService> parent) : mParent(parent) {} 1576 1577 void addListener(const sp<hardware::camera2::ICameraInjectionCallback>& callback); 1578 void removeListener(); 1579 void notifyInjectionError(const std::string &injectedCamId, status_t err); 1580 1581 // IBinder::DeathRecipient implementation 1582 virtual void binderDied(const wp<IBinder>& who); 1583 1584 private: 1585 Mutex mListenerLock; 1586 wp<CameraService> mParent; 1587 sp<hardware::camera2::ICameraInjectionCallback> mCameraInjectionCallback; 1588 }; 1589 1590 sp<InjectionStatusListener> mInjectionStatusListener; 1591 1592 /** 1593 * A class that implements the hardware::camera2::BnCameraInjectionSession interface 1594 */ 1595 class CameraInjectionSession : public hardware::camera2::BnCameraInjectionSession { 1596 public: CameraInjectionSession(sp<CameraService> parent)1597 CameraInjectionSession(sp<CameraService> parent) : mParent(parent) {} ~CameraInjectionSession()1598 virtual ~CameraInjectionSession() {} 1599 binder::Status stopInjection() override; 1600 1601 private: 1602 Mutex mInjectionSessionLock; 1603 wp<CameraService> mParent; 1604 }; 1605 1606 // When injecting the camera, it will check whether the injecting camera status is unavailable. 1607 // If it is, the disconnect function will be called to to prevent camera access on the device. 1608 status_t checkIfInjectionCameraIsPresent(const std::string& externalCamId, 1609 sp<BasicClient> clientSp); 1610 1611 void clearInjectionParameters(); 1612 1613 // This is the existing camera id being replaced. 1614 std::string mInjectionInternalCamId; 1615 // This is the external camera Id replacing the internalId. 1616 std::string mInjectionExternalCamId; 1617 bool mInjectionInitPending = false; 1618 // Guard mInjectionInternalCamId and mInjectionInitPending. 1619 Mutex mInjectionParametersLock; 1620 1621 // Track the folded/unfoled device state. 0 == UNFOLDED, 4 == FOLDED 1622 int64_t mDeviceState; 1623 1624 void updateTorchUidMapLocked(const std::string& cameraId, int uid); 1625 1626 VirtualDeviceCameraIdMapper mVirtualDeviceCameraIdMapper; 1627 }; 1628 1629 } // namespace android 1630 1631 #endif 1632