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