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> |
| 23 | #include <utils/String8.h> |
| 24 | #include <utils/String16.h> |
| 25 | #include <utils/StrongPointer.h> |
| 26 | #include <utils/Timers.h> |
Avichal Rakesh | 88fc522 | 2023-03-03 15:00:59 -0800 | [diff] [blame^] | 27 | #include <random> |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 28 | |
| 29 | #include <camera/CameraSessionStats.h> |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | class CameraServiceProxyWrapper { |
| 34 | private: |
| 35 | // Guard mCameraServiceProxy |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 36 | Mutex mProxyMutex; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 37 | // Cached interface to the camera service proxy in system service |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 38 | sp<hardware::ICameraServiceProxy> mCameraServiceProxy; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 39 | |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 40 | class CameraSessionStatsWrapper { |
Avichal Rakesh | 88fc522 | 2023-03-03 15:00:59 -0800 | [diff] [blame^] | 41 | private: |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 42 | hardware::CameraSessionStats mSessionStats; |
| 43 | Mutex mLock; // lock for per camera session stats |
| 44 | |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 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 | |
Avichal Rakesh | 88fc522 | 2023-03-03 15:00:59 -0800 | [diff] [blame^] | 51 | public: |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 52 | CameraSessionStatsWrapper(const String16& cameraId, int facing, int newCameraState, |
Avichal Rakesh | 88fc522 | 2023-03-03 15:00:59 -0800 | [diff] [blame^] | 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) {} |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 57 | |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 58 | void onOpen(sp<hardware::ICameraServiceProxy>& proxyBinder); |
Shuzhen Wang | 03fe623 | 2023-02-05 12:41:15 -0800 | [diff] [blame] | 59 | void onClose(sp<hardware::ICameraServiceProxy>& proxyBinder, int32_t latencyMs, |
| 60 | bool deviceError); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 61 | void onStreamConfigured(int operatingMode, bool internalReconfig, int32_t latencyMs); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 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, |
Shuzhen Wang | 9372b0b | 2022-05-11 18:55:19 -0700 | [diff] [blame] | 65 | const std::string& userTag, int32_t videoStabilizationMode, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 66 | const std::vector<hardware::CameraStreamStats>& streamStats); |
| 67 | }; |
| 68 | |
| 69 | // Lock for camera session stats map |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 70 | Mutex mLock; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 71 | // Map from camera id to the camera's session statistics |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 72 | std::map<String8, std::shared_ptr<CameraSessionStatsWrapper>> mSessionStatsMap; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 73 | |
Avichal Rakesh | 88fc522 | 2023-03-03 15:00:59 -0800 | [diff] [blame^] | 74 | std::random_device mRandomDevice; // pulls 32-bit random numbers from /dev/urandom |
| 75 | |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 76 | sp<hardware::ICameraServiceProxy> getCameraServiceProxy(); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 77 | |
Avichal Rakesh | 88fc522 | 2023-03-03 15:00:59 -0800 | [diff] [blame^] | 78 | // Returns a randomly generated ID that is suitable for logging the event. A new identifier |
| 79 | // should only be generated for an open event. All other events for the cameraId should use the |
| 80 | // ID generated for the open event associated with them. |
| 81 | static int64_t generateLogId(std::random_device& randomDevice); |
| 82 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 83 | public: |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 84 | CameraServiceProxyWrapper(sp<hardware::ICameraServiceProxy> serviceProxy = nullptr) : |
| 85 | mCameraServiceProxy(serviceProxy) |
| 86 | { } |
| 87 | |
| 88 | static sp<hardware::ICameraServiceProxy> getDefaultCameraServiceProxy(); |
| 89 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 90 | // Open |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 91 | void logOpen(const String8& id, int facing, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 92 | const String16& clientPackageName, int apiLevel, bool isNdk, |
| 93 | int32_t latencyMs); |
| 94 | |
| 95 | // Close |
Shuzhen Wang | 03fe623 | 2023-02-05 12:41:15 -0800 | [diff] [blame] | 96 | void logClose(const String8& id, int32_t latencyMs, bool deviceError); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 97 | |
| 98 | // Stream configuration |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 99 | void logStreamConfigured(const String8& id, int operatingMode, bool internalReconfig, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 100 | int32_t latencyMs); |
| 101 | |
| 102 | // Session state becomes active |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 103 | void logActive(const String8& id, float maxPreviewFps); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 104 | |
| 105 | // Session state becomes idle |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 106 | void logIdle(const String8& id, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 107 | int64_t requestCount, int64_t resultErrorCount, bool deviceError, |
Shuzhen Wang | 9372b0b | 2022-05-11 18:55:19 -0700 | [diff] [blame] | 108 | const std::string& userTag, int32_t videoStabilizationMode, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 109 | const std::vector<hardware::CameraStreamStats>& streamStats); |
| 110 | |
| 111 | // Ping camera service proxy for user update |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 112 | void pingCameraServiceProxy(); |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 113 | |
Emilian Peev | 5368ebf | 2021-10-08 17:52:18 -0700 | [diff] [blame] | 114 | // Return the current top activity rotate and crop override. |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 115 | int getRotateAndCropOverride(String16 packageName, int lensFacing, int userId); |
Austin Borger | 5f7abe2 | 2022-04-26 15:55:10 -0700 | [diff] [blame] | 116 | |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 117 | // Return the current top activity autoframing. |
| 118 | int getAutoframingOverride(const String16& packageName); |
| 119 | |
Austin Borger | 5f7abe2 | 2022-04-26 15:55:10 -0700 | [diff] [blame] | 120 | // Detect if the camera is disabled by device policy. |
Austin Borger | 9bfa0a7 | 2022-08-03 17:50:40 -0700 | [diff] [blame] | 121 | bool isCameraDisabled(int userId); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | } // android |
| 125 | |
| 126 | #endif // ANDROID_SERVERS_CAMERA_SERVICE_PROXY_WRAPPER_H_ |