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/ICameraServiceProxy.h> 21 22 #include <utils/Mutex.h> 23 #include <utils/String8.h> 24 #include <utils/String16.h> 25 #include <utils/StrongPointer.h> 26 #include <utils/Timers.h> 27 #include <random> 28 29 #include <camera/CameraSessionStats.h> 30 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 String16 & cameraId,int facing,int newCameraState,const String16 & clientName,int apiLevel,bool isNdk,int32_t latencyMs,int64_t logId)52 CameraSessionStatsWrapper(const String16& cameraId, int facing, int newCameraState, 53 const String16& 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, 66 const std::vector<hardware::CameraStreamStats>& streamStats); 67 68 String16 updateExtensionSessionStats(const hardware::CameraExtensionSessionStats& extStats); 69 70 // Returns the logId associated with this event. 71 int64_t getLogId(); 72 }; 73 74 // Lock for camera session stats map 75 Mutex mLock; 76 // Map from camera id to the camera's session statistics 77 std::map<String8, std::shared_ptr<CameraSessionStatsWrapper>> mSessionStatsMap; 78 79 std::random_device mRandomDevice; // pulls 32-bit random numbers from /dev/urandom 80 81 sp<hardware::ICameraServiceProxy> getCameraServiceProxy(); 82 83 // Returns a randomly generated ID that is suitable for logging the event. A new identifier 84 // should only be generated for an open event. All other events for the cameraId should use the 85 // ID generated for the open event associated with them. 86 static int64_t generateLogId(std::random_device& randomDevice); 87 88 public: 89 CameraServiceProxyWrapper(sp<hardware::ICameraServiceProxy> serviceProxy = nullptr) : mCameraServiceProxy(serviceProxy)90 mCameraServiceProxy(serviceProxy) 91 { } 92 93 static sp<hardware::ICameraServiceProxy> getDefaultCameraServiceProxy(); 94 95 // Open 96 void logOpen(const String8& id, int facing, 97 const String16& clientPackageName, int apiLevel, bool isNdk, 98 int32_t latencyMs); 99 100 // Close 101 void logClose(const String8& id, int32_t latencyMs, bool deviceError); 102 103 // Stream configuration 104 void logStreamConfigured(const String8& id, int operatingMode, bool internalReconfig, 105 int32_t latencyMs); 106 107 // Session state becomes active 108 void logActive(const String8& id, float maxPreviewFps); 109 110 // Session state becomes idle 111 void logIdle(const String8& id, 112 int64_t requestCount, int64_t resultErrorCount, bool deviceError, 113 const std::string& userTag, int32_t videoStabilizationMode, 114 const std::vector<hardware::CameraStreamStats>& streamStats); 115 116 // Ping camera service proxy for user update 117 void pingCameraServiceProxy(); 118 119 // Return the current top activity rotate and crop override. 120 int getRotateAndCropOverride(String16 packageName, int lensFacing, int userId); 121 122 // Return the current top activity autoframing. 123 int getAutoframingOverride(const String16& packageName); 124 125 // Detect if the camera is disabled by device policy. 126 bool isCameraDisabled(int userId); 127 128 // Returns the logId currently associated with the given cameraId. See 'mLogId' in 129 // frameworks/av/camera/include/camera/CameraSessionStats.h for more details about this 130 // identifier. Returns a non-0 value on success. 131 int64_t getCurrentLogIdForCamera(const String8& cameraId); 132 133 // Update the stored extension stats to the latest values 134 String16 updateExtensionStats(const hardware::CameraExtensionSessionStats& extStats); 135 }; 136 137 } // android 138 139 #endif // ANDROID_SERVERS_CAMERA_SERVICE_PROXY_WRAPPER_H_ 140