Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 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> |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 23 | #include <utils/StrongPointer.h> |
| 24 | #include <utils/Timers.h> |
Avichal Rakesh | 88fc522 | 2023-03-03 15:00:59 -0800 | [diff] [blame] | 25 | #include <random> |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 26 | #include <string> |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 27 | |
| 28 | #include <camera/CameraSessionStats.h> |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | class CameraServiceProxyWrapper { |
| 33 | private: |
| 34 | // Guard mCameraServiceProxy |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 35 | Mutex mProxyMutex; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 36 | // Cached interface to the camera service proxy in system service |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 37 | sp<hardware::ICameraServiceProxy> mCameraServiceProxy; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 38 | |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 39 | class CameraSessionStatsWrapper { |
Avichal Rakesh | 88fc522 | 2023-03-03 15:00:59 -0800 | [diff] [blame] | 40 | private: |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 41 | hardware::CameraSessionStats mSessionStats; |
| 42 | Mutex mLock; // lock for per camera session stats |
| 43 | |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Update the session stats of a given camera device (open/close/active/idle) with |
| 46 | * the camera proxy service in the system service |
| 47 | */ |
| 48 | void updateProxyDeviceState(sp<hardware::ICameraServiceProxy>& proxyBinder); |
| 49 | |
Avichal Rakesh | 88fc522 | 2023-03-03 15:00:59 -0800 | [diff] [blame] | 50 | public: |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 51 | CameraSessionStatsWrapper(const std::string& cameraId, int facing, int newCameraState, |
| 52 | const std::string& clientName, int apiLevel, bool isNdk, |
Avichal Rakesh | 88fc522 | 2023-03-03 15:00:59 -0800 | [diff] [blame] | 53 | int32_t latencyMs, int64_t logId) |
| 54 | : mSessionStats(cameraId, facing, newCameraState, clientName, apiLevel, isNdk, |
| 55 | latencyMs, logId) {} |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 56 | |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 57 | void onOpen(sp<hardware::ICameraServiceProxy>& proxyBinder); |
Shuzhen Wang | 03fe623 | 2023-02-05 12:41:15 -0800 | [diff] [blame] | 58 | void onClose(sp<hardware::ICameraServiceProxy>& proxyBinder, int32_t latencyMs, |
| 59 | bool deviceError); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 60 | void onStreamConfigured(int operatingMode, bool internalReconfig, int32_t latencyMs); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 61 | void onActive(sp<hardware::ICameraServiceProxy>& proxyBinder, float maxPreviewFps); |
| 62 | void onIdle(sp<hardware::ICameraServiceProxy>& proxyBinder, |
| 63 | int64_t requestCount, int64_t resultErrorCount, bool deviceError, |
Eino-Ville Talvala | 6f1a9c1 | 2023-09-14 17:26:28 -0700 | [diff] [blame] | 64 | const std::string& userTag, int32_t videoStabilizationMode, bool usedUltraWide, |
Shuzhen Wang | 6e08d20 | 2023-10-24 20:27:14 +0000 | [diff] [blame] | 65 | bool usedZoomOverride, const std::vector<hardware::CameraStreamStats>& streamStats); |
Avichal Rakesh | 1fff2d1 | 2023-03-03 15:05:48 -0800 | [diff] [blame] | 66 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 67 | std::string updateExtensionSessionStats( |
| 68 | const hardware::CameraExtensionSessionStats& extStats); |
Avichal Rakesh | 6e57a2b | 2023-05-01 17:53:37 -0700 | [diff] [blame] | 69 | |
Avichal Rakesh | 1fff2d1 | 2023-03-03 15:05:48 -0800 | [diff] [blame] | 70 | // Returns the logId associated with this event. |
| 71 | int64_t getLogId(); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | // Lock for camera session stats map |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 75 | Mutex mLock; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 76 | // Map from camera id to the camera's session statistics |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 77 | std::map<std::string, std::shared_ptr<CameraSessionStatsWrapper>> mSessionStatsMap; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 78 | |
Avichal Rakesh | 88fc522 | 2023-03-03 15:00:59 -0800 | [diff] [blame] | 79 | std::random_device mRandomDevice; // pulls 32-bit random numbers from /dev/urandom |
| 80 | |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 81 | sp<hardware::ICameraServiceProxy> getCameraServiceProxy(); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 82 | |
Avichal Rakesh | 88fc522 | 2023-03-03 15:00:59 -0800 | [diff] [blame] | 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 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 88 | public: |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 89 | CameraServiceProxyWrapper(sp<hardware::ICameraServiceProxy> serviceProxy = nullptr) : |
| 90 | mCameraServiceProxy(serviceProxy) |
| 91 | { } |
| 92 | |
| 93 | static sp<hardware::ICameraServiceProxy> getDefaultCameraServiceProxy(); |
| 94 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 95 | // Open |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 96 | void logOpen(const std::string& id, int facing, |
| 97 | const std::string& clientPackageName, int apiLevel, bool isNdk, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 98 | int32_t latencyMs); |
| 99 | |
| 100 | // Close |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 101 | void logClose(const std::string& id, int32_t latencyMs, bool deviceError); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 102 | |
| 103 | // Stream configuration |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 104 | void logStreamConfigured(const std::string& id, int operatingMode, bool internalReconfig, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 105 | int32_t latencyMs); |
| 106 | |
| 107 | // Session state becomes active |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 108 | void logActive(const std::string& id, float maxPreviewFps); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 109 | |
| 110 | // Session state becomes idle |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 111 | void logIdle(const std::string& id, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 112 | int64_t requestCount, int64_t resultErrorCount, bool deviceError, |
Eino-Ville Talvala | 6f1a9c1 | 2023-09-14 17:26:28 -0700 | [diff] [blame] | 113 | const std::string& userTag, int32_t videoStabilizationMode, bool usedUltraWide, |
Shuzhen Wang | 6e08d20 | 2023-10-24 20:27:14 +0000 | [diff] [blame] | 114 | bool usedZoomOverride, const std::vector<hardware::CameraStreamStats>& streamStats); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 115 | |
| 116 | // Ping camera service proxy for user update |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 117 | void pingCameraServiceProxy(); |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 118 | |
Emilian Peev | 5368ebf | 2021-10-08 17:52:18 -0700 | [diff] [blame] | 119 | // Return the current top activity rotate and crop override. |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 120 | int getRotateAndCropOverride(const std::string &packageName, int lensFacing, int userId); |
Austin Borger | 5f7abe2 | 2022-04-26 15:55:10 -0700 | [diff] [blame] | 121 | |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 122 | // Return the current top activity autoframing. |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 123 | int getAutoframingOverride(const std::string& packageName); |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 124 | |
Austin Borger | 5f7abe2 | 2022-04-26 15:55:10 -0700 | [diff] [blame] | 125 | // Detect if the camera is disabled by device policy. |
Austin Borger | 9bfa0a7 | 2022-08-03 17:50:40 -0700 | [diff] [blame] | 126 | bool isCameraDisabled(int userId); |
Avichal Rakesh | 1fff2d1 | 2023-03-03 15:05:48 -0800 | [diff] [blame] | 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. |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 131 | int64_t getCurrentLogIdForCamera(const std::string& cameraId); |
Avichal Rakesh | 6e57a2b | 2023-05-01 17:53:37 -0700 | [diff] [blame] | 132 | |
| 133 | // Update the stored extension stats to the latest values |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 134 | std::string updateExtensionStats(const hardware::CameraExtensionSessionStats& extStats); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | } // android |
| 138 | |
| 139 | #endif // ANDROID_SERVERS_CAMERA_SERVICE_PROXY_WRAPPER_H_ |