blob: 4e21e58279cbcd227bcc10dbb09aa7d9a3045c70 [file] [log] [blame]
Shuzhen Wang316781a2020-08-18 18:11:01 -07001/*
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 Wang316781a2020-08-18 18:11:01 -070023#include <utils/StrongPointer.h>
24#include <utils/Timers.h>
Avichal Rakesh88fc5222023-03-03 15:00:59 -080025#include <random>
Austin Borger1c1bee02023-06-01 16:51:35 -070026#include <string>
Shuzhen Wang316781a2020-08-18 18:11:01 -070027
28#include <camera/CameraSessionStats.h>
29
30namespace android {
31
32class CameraServiceProxyWrapper {
33private:
34 // Guard mCameraServiceProxy
Austin Borger74fca042022-05-23 12:41:21 -070035 Mutex mProxyMutex;
Shuzhen Wang316781a2020-08-18 18:11:01 -070036 // Cached interface to the camera service proxy in system service
Austin Borger74fca042022-05-23 12:41:21 -070037 sp<hardware::ICameraServiceProxy> mCameraServiceProxy;
Shuzhen Wang316781a2020-08-18 18:11:01 -070038
Austin Borger74fca042022-05-23 12:41:21 -070039 class CameraSessionStatsWrapper {
Avichal Rakesh88fc5222023-03-03 15:00:59 -080040 private:
Shuzhen Wang316781a2020-08-18 18:11:01 -070041 hardware::CameraSessionStats mSessionStats;
42 Mutex mLock; // lock for per camera session stats
43
Austin Borger74fca042022-05-23 12:41:21 -070044 /**
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 Rakesh88fc5222023-03-03 15:00:59 -080050 public:
Austin Borger1c1bee02023-06-01 16:51:35 -070051 CameraSessionStatsWrapper(const std::string& cameraId, int facing, int newCameraState,
52 const std::string& clientName, int apiLevel, bool isNdk,
Avichal Rakesh88fc5222023-03-03 15:00:59 -080053 int32_t latencyMs, int64_t logId)
54 : mSessionStats(cameraId, facing, newCameraState, clientName, apiLevel, isNdk,
55 latencyMs, logId) {}
Shuzhen Wang316781a2020-08-18 18:11:01 -070056
Austin Borger74fca042022-05-23 12:41:21 -070057 void onOpen(sp<hardware::ICameraServiceProxy>& proxyBinder);
Shuzhen Wang03fe6232023-02-05 12:41:15 -080058 void onClose(sp<hardware::ICameraServiceProxy>& proxyBinder, int32_t latencyMs,
59 bool deviceError);
Shuzhen Wang316781a2020-08-18 18:11:01 -070060 void onStreamConfigured(int operatingMode, bool internalReconfig, int32_t latencyMs);
Austin Borger74fca042022-05-23 12:41:21 -070061 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 Talvala6f1a9c12023-09-14 17:26:28 -070064 const std::string& userTag, int32_t videoStabilizationMode, bool usedUltraWide,
Shuzhen Wang316781a2020-08-18 18:11:01 -070065 const std::vector<hardware::CameraStreamStats>& streamStats);
Avichal Rakesh1fff2d12023-03-03 15:05:48 -080066
Austin Borger1c1bee02023-06-01 16:51:35 -070067 std::string updateExtensionSessionStats(
68 const hardware::CameraExtensionSessionStats& extStats);
Avichal Rakesh6e57a2b2023-05-01 17:53:37 -070069
Avichal Rakesh1fff2d12023-03-03 15:05:48 -080070 // Returns the logId associated with this event.
71 int64_t getLogId();
Shuzhen Wang316781a2020-08-18 18:11:01 -070072 };
73
74 // Lock for camera session stats map
Austin Borger74fca042022-05-23 12:41:21 -070075 Mutex mLock;
Shuzhen Wang316781a2020-08-18 18:11:01 -070076 // Map from camera id to the camera's session statistics
Austin Borger1c1bee02023-06-01 16:51:35 -070077 std::map<std::string, std::shared_ptr<CameraSessionStatsWrapper>> mSessionStatsMap;
Shuzhen Wang316781a2020-08-18 18:11:01 -070078
Avichal Rakesh88fc5222023-03-03 15:00:59 -080079 std::random_device mRandomDevice; // pulls 32-bit random numbers from /dev/urandom
80
Austin Borger74fca042022-05-23 12:41:21 -070081 sp<hardware::ICameraServiceProxy> getCameraServiceProxy();
Shuzhen Wang316781a2020-08-18 18:11:01 -070082
Avichal Rakesh88fc5222023-03-03 15:00:59 -080083 // 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 Wang316781a2020-08-18 18:11:01 -070088public:
Austin Borger74fca042022-05-23 12:41:21 -070089 CameraServiceProxyWrapper(sp<hardware::ICameraServiceProxy> serviceProxy = nullptr) :
90 mCameraServiceProxy(serviceProxy)
91 { }
92
93 static sp<hardware::ICameraServiceProxy> getDefaultCameraServiceProxy();
94
Shuzhen Wang316781a2020-08-18 18:11:01 -070095 // Open
Austin Borger1c1bee02023-06-01 16:51:35 -070096 void logOpen(const std::string& id, int facing,
97 const std::string& clientPackageName, int apiLevel, bool isNdk,
Shuzhen Wang316781a2020-08-18 18:11:01 -070098 int32_t latencyMs);
99
100 // Close
Austin Borger1c1bee02023-06-01 16:51:35 -0700101 void logClose(const std::string& id, int32_t latencyMs, bool deviceError);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700102
103 // Stream configuration
Austin Borger1c1bee02023-06-01 16:51:35 -0700104 void logStreamConfigured(const std::string& id, int operatingMode, bool internalReconfig,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700105 int32_t latencyMs);
106
107 // Session state becomes active
Austin Borger1c1bee02023-06-01 16:51:35 -0700108 void logActive(const std::string& id, float maxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700109
110 // Session state becomes idle
Austin Borger1c1bee02023-06-01 16:51:35 -0700111 void logIdle(const std::string& id,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700112 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
Eino-Ville Talvala6f1a9c12023-09-14 17:26:28 -0700113 const std::string& userTag, int32_t videoStabilizationMode, bool usedUltraWide,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700114 const std::vector<hardware::CameraStreamStats>& streamStats);
115
116 // Ping camera service proxy for user update
Austin Borger74fca042022-05-23 12:41:21 -0700117 void pingCameraServiceProxy();
Emilian Peevb91f1802021-03-23 14:50:28 -0700118
Emilian Peev5368ebf2021-10-08 17:52:18 -0700119 // Return the current top activity rotate and crop override.
Austin Borger1c1bee02023-06-01 16:51:35 -0700120 int getRotateAndCropOverride(const std::string &packageName, int lensFacing, int userId);
Austin Borger5f7abe22022-04-26 15:55:10 -0700121
Bharatt Kukreja7146ced2022-10-25 15:45:29 +0000122 // Return the current top activity autoframing.
Austin Borger1c1bee02023-06-01 16:51:35 -0700123 int getAutoframingOverride(const std::string& packageName);
Bharatt Kukreja7146ced2022-10-25 15:45:29 +0000124
Austin Borger5f7abe22022-04-26 15:55:10 -0700125 // Detect if the camera is disabled by device policy.
Austin Borger9bfa0a72022-08-03 17:50:40 -0700126 bool isCameraDisabled(int userId);
Avichal Rakesh1fff2d12023-03-03 15:05:48 -0800127
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 Borger1c1bee02023-06-01 16:51:35 -0700131 int64_t getCurrentLogIdForCamera(const std::string& cameraId);
Avichal Rakesh6e57a2b2023-05-01 17:53:37 -0700132
133 // Update the stored extension stats to the latest values
Austin Borger1c1bee02023-06-01 16:51:35 -0700134 std::string updateExtensionStats(const hardware::CameraExtensionSessionStats& extStats);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700135};
136
137} // android
138
139#endif // ANDROID_SERVERS_CAMERA_SERVICE_PROXY_WRAPPER_H_