blob: d47c73828d0bd1c87aaa242ef8231e0080b19a00 [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>
23#include <utils/String8.h>
24#include <utils/String16.h>
25#include <utils/StrongPointer.h>
26#include <utils/Timers.h>
Avichal Rakesh88fc5222023-03-03 15:00:59 -080027#include <random>
Shuzhen Wang316781a2020-08-18 18:11:01 -070028
29#include <camera/CameraSessionStats.h>
30
31namespace android {
32
33class CameraServiceProxyWrapper {
34private:
35 // Guard mCameraServiceProxy
Austin Borger74fca042022-05-23 12:41:21 -070036 Mutex mProxyMutex;
Shuzhen Wang316781a2020-08-18 18:11:01 -070037 // Cached interface to the camera service proxy in system service
Austin Borger74fca042022-05-23 12:41:21 -070038 sp<hardware::ICameraServiceProxy> mCameraServiceProxy;
Shuzhen Wang316781a2020-08-18 18:11:01 -070039
Austin Borger74fca042022-05-23 12:41:21 -070040 class CameraSessionStatsWrapper {
Avichal Rakesh88fc5222023-03-03 15:00:59 -080041 private:
Shuzhen Wang316781a2020-08-18 18:11:01 -070042 hardware::CameraSessionStats mSessionStats;
43 Mutex mLock; // lock for per camera session stats
44
Austin Borger74fca042022-05-23 12:41:21 -070045 /**
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 Rakesh88fc5222023-03-03 15:00:59 -080051 public:
Shuzhen Wang316781a2020-08-18 18:11:01 -070052 CameraSessionStatsWrapper(const String16& cameraId, int facing, int newCameraState,
Avichal Rakesh88fc5222023-03-03 15:00:59 -080053 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 Wang316781a2020-08-18 18:11:01 -070057
Austin Borger74fca042022-05-23 12:41:21 -070058 void onOpen(sp<hardware::ICameraServiceProxy>& proxyBinder);
Shuzhen Wang03fe6232023-02-05 12:41:15 -080059 void onClose(sp<hardware::ICameraServiceProxy>& proxyBinder, int32_t latencyMs,
60 bool deviceError);
Shuzhen Wang316781a2020-08-18 18:11:01 -070061 void onStreamConfigured(int operatingMode, bool internalReconfig, int32_t latencyMs);
Austin Borger74fca042022-05-23 12:41:21 -070062 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 Wang9372b0b2022-05-11 18:55:19 -070065 const std::string& userTag, int32_t videoStabilizationMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -070066 const std::vector<hardware::CameraStreamStats>& streamStats);
Avichal Rakesh1fff2d12023-03-03 15:05:48 -080067
68 // Returns the logId associated with this event.
69 int64_t getLogId();
Shuzhen Wang316781a2020-08-18 18:11:01 -070070 };
71
72 // Lock for camera session stats map
Austin Borger74fca042022-05-23 12:41:21 -070073 Mutex mLock;
Shuzhen Wang316781a2020-08-18 18:11:01 -070074 // Map from camera id to the camera's session statistics
Austin Borger74fca042022-05-23 12:41:21 -070075 std::map<String8, std::shared_ptr<CameraSessionStatsWrapper>> mSessionStatsMap;
Shuzhen Wang316781a2020-08-18 18:11:01 -070076
Avichal Rakesh88fc5222023-03-03 15:00:59 -080077 std::random_device mRandomDevice; // pulls 32-bit random numbers from /dev/urandom
78
Austin Borger74fca042022-05-23 12:41:21 -070079 sp<hardware::ICameraServiceProxy> getCameraServiceProxy();
Shuzhen Wang316781a2020-08-18 18:11:01 -070080
Avichal Rakesh88fc5222023-03-03 15:00:59 -080081 // Returns a randomly generated ID that is suitable for logging the event. A new identifier
82 // should only be generated for an open event. All other events for the cameraId should use the
83 // ID generated for the open event associated with them.
84 static int64_t generateLogId(std::random_device& randomDevice);
85
Shuzhen Wang316781a2020-08-18 18:11:01 -070086public:
Austin Borger74fca042022-05-23 12:41:21 -070087 CameraServiceProxyWrapper(sp<hardware::ICameraServiceProxy> serviceProxy = nullptr) :
88 mCameraServiceProxy(serviceProxy)
89 { }
90
91 static sp<hardware::ICameraServiceProxy> getDefaultCameraServiceProxy();
92
Shuzhen Wang316781a2020-08-18 18:11:01 -070093 // Open
Austin Borger74fca042022-05-23 12:41:21 -070094 void logOpen(const String8& id, int facing,
Shuzhen Wang316781a2020-08-18 18:11:01 -070095 const String16& clientPackageName, int apiLevel, bool isNdk,
96 int32_t latencyMs);
97
98 // Close
Shuzhen Wang03fe6232023-02-05 12:41:15 -080099 void logClose(const String8& id, int32_t latencyMs, bool deviceError);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700100
101 // Stream configuration
Austin Borger74fca042022-05-23 12:41:21 -0700102 void logStreamConfigured(const String8& id, int operatingMode, bool internalReconfig,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700103 int32_t latencyMs);
104
105 // Session state becomes active
Austin Borger74fca042022-05-23 12:41:21 -0700106 void logActive(const String8& id, float maxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700107
108 // Session state becomes idle
Austin Borger74fca042022-05-23 12:41:21 -0700109 void logIdle(const String8& id,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700110 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700111 const std::string& userTag, int32_t videoStabilizationMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700112 const std::vector<hardware::CameraStreamStats>& streamStats);
113
114 // Ping camera service proxy for user update
Austin Borger74fca042022-05-23 12:41:21 -0700115 void pingCameraServiceProxy();
Emilian Peevb91f1802021-03-23 14:50:28 -0700116
Emilian Peev5368ebf2021-10-08 17:52:18 -0700117 // Return the current top activity rotate and crop override.
Austin Borger74fca042022-05-23 12:41:21 -0700118 int getRotateAndCropOverride(String16 packageName, int lensFacing, int userId);
Austin Borger5f7abe22022-04-26 15:55:10 -0700119
Bharatt Kukreja7146ced2022-10-25 15:45:29 +0000120 // Return the current top activity autoframing.
121 int getAutoframingOverride(const String16& packageName);
122
Austin Borger5f7abe22022-04-26 15:55:10 -0700123 // Detect if the camera is disabled by device policy.
Austin Borger9bfa0a72022-08-03 17:50:40 -0700124 bool isCameraDisabled(int userId);
Avichal Rakesh1fff2d12023-03-03 15:05:48 -0800125
126 // Returns the logId currently associated with the given cameraId. See 'mLogId' in
127 // frameworks/av/camera/include/camera/CameraSessionStats.h for more details about this
128 // identifier. Returns a non-0 value on success.
129 int64_t getCurrentLogIdForCamera(const String8& cameraId);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700130};
131
132} // android
133
134#endif // ANDROID_SERVERS_CAMERA_SERVICE_PROXY_WRAPPER_H_