• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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_SERVICE_PROXY_WRAPPER_H_
18 #define ANDROID_SERVERS_CAMERA_SERVICE_PROXY_WRAPPER_H_
19 
20 #include <android/hardware/CameraFeatureCombinationStats.h>
21 #include <android/hardware/ICameraServiceProxy.h>
22 
23 #include <utils/Mutex.h>
24 #include <utils/StrongPointer.h>
25 #include <utils/Timers.h>
26 #include <random>
27 #include <string>
28 
29 #include <camera/CameraSessionStats.h>
30 #include <camera/camera2/SessionConfiguration.h>
31 namespace android {
32 
33 class CameraServiceProxyWrapper {
34 private:
35     // Guard mCameraServiceProxy
36     Mutex mProxyMutex;
37     // Cached interface to the camera service proxy in system service
38     sp<hardware::ICameraServiceProxy> mCameraServiceProxy;
39 
40     class CameraSessionStatsWrapper {
41       private:
42         hardware::CameraSessionStats mSessionStats;
43         Mutex mLock; // lock for per camera session stats
44 
45         /**
46          * Update the session stats of a given camera device (open/close/active/idle) with
47          * the camera proxy service in the system service
48          */
49         void updateProxyDeviceState(sp<hardware::ICameraServiceProxy>& proxyBinder);
50 
51       public:
CameraSessionStatsWrapper(const std::string & cameraId,int facing,int newCameraState,const std::string & clientName,int apiLevel,bool isNdk,int32_t latencyMs,int64_t logId)52         CameraSessionStatsWrapper(const std::string& cameraId, int facing, int newCameraState,
53                                   const std::string& clientName, int apiLevel, bool isNdk,
54                                   int32_t latencyMs, int64_t logId)
55             : mSessionStats(cameraId, facing, newCameraState, clientName, apiLevel, isNdk,
56                             latencyMs, logId) {}
57 
58         void onOpen(sp<hardware::ICameraServiceProxy>& proxyBinder);
59         void onClose(sp<hardware::ICameraServiceProxy>& proxyBinder, int32_t latencyMs,
60                 bool deviceError);
61         void onStreamConfigured(int operatingMode, bool internalReconfig, int32_t latencyMs);
62         void onActive(sp<hardware::ICameraServiceProxy>& proxyBinder, float maxPreviewFps);
63         void onIdle(sp<hardware::ICameraServiceProxy>& proxyBinder,
64                 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
65                 const std::string& userTag, int32_t videoStabilizationMode, bool usedUltraWide,
66                 bool usedZoomOverride, std::pair<int32_t, int32_t> mostRequestedFpsRange,
67                 const std::vector<hardware::CameraStreamStats>& streamStats);
68 
69         std::string updateExtensionSessionStats(
70                 const hardware::CameraExtensionSessionStats& extStats);
71 
72         // Returns the logId associated with this event.
73         int64_t getLogId();
74     };
75 
76     // Lock for camera session stats map
77     Mutex mLock;
78     // Map from camera id to the camera's session statistics
79     std::map<std::string, std::shared_ptr<CameraSessionStatsWrapper>> mSessionStatsMap;
80 
81     std::random_device mRandomDevice;  // pulls 32-bit random numbers from /dev/urandom
82 
83     sp<hardware::ICameraServiceProxy> getCameraServiceProxy();
84 
85     // Returns a randomly generated ID that is suitable for logging the event. A new identifier
86     // should only be generated for an open event. All other events for the cameraId should use the
87     // ID generated for the open event associated with them.
88     static int64_t generateLogId(std::random_device& randomDevice);
89 
90     static int64_t encodeSessionConfiguration(const SessionConfiguration& sessionConfig);
91 
92     void logFeatureCombinationInternal(const std::string &cameraId, int clientUid,
93             const hardware::camera2::params::SessionConfiguration& sessionConfiguration,
94             binder::Status ret, int type);
95 public:
96     CameraServiceProxyWrapper(sp<hardware::ICameraServiceProxy> serviceProxy = nullptr) :
mCameraServiceProxy(serviceProxy)97             mCameraServiceProxy(serviceProxy)
98     { }
99 
100     static sp<hardware::ICameraServiceProxy> getDefaultCameraServiceProxy();
101 
102     // Open
103     void logOpen(const std::string& id, int facing,
104             const std::string& clientPackageName, int apiLevel, bool isNdk,
105             int32_t latencyMs);
106 
107     // Close
108     void logClose(const std::string& id, int32_t latencyMs, bool deviceError);
109 
110     // Stream configuration
111     void logStreamConfigured(const std::string& id, int operatingMode, bool internalReconfig,
112             int32_t latencyMs);
113 
114     // Session state becomes active
115     void logActive(const std::string& id, float maxPreviewFps);
116 
117     // Session state becomes idle
118     void logIdle(const std::string& id,
119             int64_t requestCount, int64_t resultErrorCount, bool deviceError,
120             const std::string& userTag, int32_t videoStabilizationMode, bool usedUltraWide,
121             bool usedZoomOverride, std::pair<int32_t, int32_t> mostRequestedFpsRange,
122             const std::vector<hardware::CameraStreamStats>& streamStats);
123 
124     // Feature combination query
logFeatureCombinationQuery(const std::string & id,int clientUid,const hardware::camera2::params::SessionConfiguration & sessionConfiguration,binder::Status ret)125     void logFeatureCombinationQuery(const std::string &id, int clientUid,
126             const hardware::camera2::params::SessionConfiguration& sessionConfiguration,
127             binder::Status ret) {
128         logFeatureCombinationInternal(id, clientUid, sessionConfiguration, ret,
129                 (int)hardware::CameraFeatureCombinationStats::QueryType::QUERY_FEATURE_COMBINATION);
130     }
logSessionCharacteristicsQuery(const std::string & id,int clientUid,const hardware::camera2::params::SessionConfiguration & sessionConfiguration,binder::Status ret)131     void logSessionCharacteristicsQuery(const std::string &id, int clientUid,
132             const hardware::camera2::params::SessionConfiguration& sessionConfiguration,
133             binder::Status ret) {
134         logFeatureCombinationInternal(id, clientUid, sessionConfiguration, ret, (int)
135                 hardware::CameraFeatureCombinationStats::QueryType::QUERY_SESSION_CHARACTERISTICS);
136     }
137 
138     // Ping camera service proxy for user update
139     void pingCameraServiceProxy();
140 
141     // Return the current top activity rotate and crop override.
142     int getRotateAndCropOverride(const std::string &packageName, int lensFacing, int userId);
143 
144     // Return the current top activity autoframing.
145     int getAutoframingOverride(const std::string& packageName);
146 
147     // Detect if the camera is disabled by device policy.
148     bool isCameraDisabled(int userId);
149 
150     // Returns the logId currently associated with the given cameraId. See 'mLogId' in
151     // frameworks/av/camera/include/camera/CameraSessionStats.h for more details about this
152     // identifier. Returns a non-0 value on success.
153     int64_t getCurrentLogIdForCamera(const std::string& cameraId);
154 
155     // Update the stored extension stats to the latest values
156     std::string updateExtensionStats(const hardware::CameraExtensionSessionStats& extStats);
157 };
158 
159 } // android
160 
161 #endif // ANDROID_SERVERS_CAMERA_SERVICE_PROXY_WRAPPER_H_
162