blob: f6ad2fed7deae6a2710697c86e098ae19af3c8e6 [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#define LOG_TAG "CameraServiceProxyWrapper"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20
21#include <inttypes.h>
22#include <utils/Log.h>
Austin Borger0fb3ad92023-06-01 16:51:35 -070023#include <utils/String16.h>
24#include <camera/StringUtils.h>
Shuzhen Wang316781a2020-08-18 18:11:01 -070025#include <binder/IServiceManager.h>
26
27#include "CameraServiceProxyWrapper.h"
28
29namespace android {
30
31using hardware::ICameraServiceProxy;
32using hardware::CameraSessionStats;
33
34Mutex CameraServiceProxyWrapper::sProxyMutex;
35sp<hardware::ICameraServiceProxy> CameraServiceProxyWrapper::sCameraServiceProxy;
36
37Mutex CameraServiceProxyWrapper::mLock;
Austin Borger0fb3ad92023-06-01 16:51:35 -070038std::map<std::string, std::shared_ptr<CameraServiceProxyWrapper::CameraSessionStatsWrapper>>
Shuzhen Wang316781a2020-08-18 18:11:01 -070039 CameraServiceProxyWrapper::mSessionStatsMap;
40
41/**
42 * CameraSessionStatsWrapper functions
43 */
44
45void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onOpen() {
46 Mutex::Autolock l(mLock);
47
48 updateProxyDeviceState(mSessionStats);
49}
50
51void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onClose(int32_t latencyMs) {
52 Mutex::Autolock l(mLock);
53
54 mSessionStats.mNewCameraState = CameraSessionStats::CAMERA_STATE_CLOSED;
55 mSessionStats.mLatencyMs = latencyMs;
56 updateProxyDeviceState(mSessionStats);
57}
58
59void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onStreamConfigured(
60 int operatingMode, bool internalReconfig, int32_t latencyMs) {
61 Mutex::Autolock l(mLock);
62
63 if (internalReconfig) {
64 mSessionStats.mInternalReconfigure++;
65 } else {
66 mSessionStats.mLatencyMs = latencyMs;
67 mSessionStats.mSessionType = operatingMode;
68 }
69}
70
Austin Borger4a870a32022-02-25 01:48:41 +000071void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onActive(float maxPreviewFps) {
Shuzhen Wang316781a2020-08-18 18:11:01 -070072 Mutex::Autolock l(mLock);
73
74 mSessionStats.mNewCameraState = CameraSessionStats::CAMERA_STATE_ACTIVE;
Austin Borger4a870a32022-02-25 01:48:41 +000075 mSessionStats.mMaxPreviewFps = maxPreviewFps;
Shuzhen Wang316781a2020-08-18 18:11:01 -070076 updateProxyDeviceState(mSessionStats);
77
78 // Reset mCreationDuration to -1 to distinguish between 1st session
79 // after configuration, and all other sessions after configuration.
80 mSessionStats.mLatencyMs = -1;
81}
82
83void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onIdle(
84 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
Shuzhen Wang9372b0b2022-05-11 18:55:19 -070085 const std::string& userTag, int32_t videoStabilizationMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -070086 const std::vector<hardware::CameraStreamStats>& streamStats) {
87 Mutex::Autolock l(mLock);
88
89 mSessionStats.mNewCameraState = CameraSessionStats::CAMERA_STATE_IDLE;
90 mSessionStats.mRequestCount = requestCount;
91 mSessionStats.mResultErrorCount = resultErrorCount;
92 mSessionStats.mDeviceError = deviceError;
Austin Borger0fb3ad92023-06-01 16:51:35 -070093 mSessionStats.mUserTag = userTag;
Shuzhen Wang9372b0b2022-05-11 18:55:19 -070094 mSessionStats.mVideoStabilizationMode = videoStabilizationMode;
Shuzhen Wang316781a2020-08-18 18:11:01 -070095 mSessionStats.mStreamStats = streamStats;
96 updateProxyDeviceState(mSessionStats);
97
98 mSessionStats.mInternalReconfigure = 0;
99 mSessionStats.mStreamStats.clear();
100}
101
102/**
103 * CameraServiceProxyWrapper functions
104 */
105
106sp<ICameraServiceProxy> CameraServiceProxyWrapper::getCameraServiceProxy() {
107#ifndef __BRILLO__
108 Mutex::Autolock al(sProxyMutex);
109 if (sCameraServiceProxy == nullptr) {
110 sp<IServiceManager> sm = defaultServiceManager();
111 // Use checkService because cameraserver normally starts before the
112 // system server and the proxy service. So the long timeout that getService
113 // has before giving up is inappropriate.
114 sp<IBinder> binder = sm->checkService(String16("media.camera.proxy"));
115 if (binder != nullptr) {
116 sCameraServiceProxy = interface_cast<ICameraServiceProxy>(binder);
117 }
118 }
119#endif
120 return sCameraServiceProxy;
121}
122
123void CameraServiceProxyWrapper::pingCameraServiceProxy() {
124 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
125 if (proxyBinder == nullptr) return;
126 proxyBinder->pingForUserUpdate();
127}
128
Austin Borger0fb3ad92023-06-01 16:51:35 -0700129int CameraServiceProxyWrapper::getRotateAndCropOverride(const std::string &packageName,
130 int lensFacing, int userId) {
Emilian Peevb91f1802021-03-23 14:50:28 -0700131 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
132 if (proxyBinder == nullptr) return true;
Emilian Peev5368ebf2021-10-08 17:52:18 -0700133 int ret = 0;
Austin Borger0fb3ad92023-06-01 16:51:35 -0700134 auto status = proxyBinder->getRotateAndCropOverride(packageName, lensFacing,
135 userId, &ret);
Emilian Peevb91f1802021-03-23 14:50:28 -0700136 if (!status.isOk()) {
137 ALOGE("%s: Failed during top activity orientation query: %s", __FUNCTION__,
138 status.exceptionMessage().c_str());
139 }
140
141 return ret;
142}
143
Shuzhen Wang316781a2020-08-18 18:11:01 -0700144void CameraServiceProxyWrapper::updateProxyDeviceState(const CameraSessionStats& sessionStats) {
145 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
146 if (proxyBinder == nullptr) return;
147 proxyBinder->notifyCameraState(sessionStats);
148}
149
Austin Borger0fb3ad92023-06-01 16:51:35 -0700150void CameraServiceProxyWrapper::logStreamConfigured(const std::string& id,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700151 int operatingMode, bool internalConfig, int32_t latencyMs) {
152 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
153 {
154 Mutex::Autolock l(mLock);
155 sessionStats = mSessionStatsMap[id];
156 if (sessionStats == nullptr) {
157 ALOGE("%s: SessionStatsMap should contain camera %s",
158 __FUNCTION__, id.c_str());
159 return;
160 }
161 }
162
163 ALOGV("%s: id %s, operatingMode %d, internalConfig %d, latencyMs %d",
164 __FUNCTION__, id.c_str(), operatingMode, internalConfig, latencyMs);
165 sessionStats->onStreamConfigured(operatingMode, internalConfig, latencyMs);
166}
167
Austin Borger0fb3ad92023-06-01 16:51:35 -0700168void CameraServiceProxyWrapper::logActive(const std::string& id, float maxPreviewFps) {
Shuzhen Wang316781a2020-08-18 18:11:01 -0700169 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
170 {
171 Mutex::Autolock l(mLock);
172 sessionStats = mSessionStatsMap[id];
173 if (sessionStats == nullptr) {
174 ALOGE("%s: SessionStatsMap should contain camera %s when logActive is called",
175 __FUNCTION__, id.c_str());
176 return;
177 }
178 }
179
180 ALOGV("%s: id %s", __FUNCTION__, id.c_str());
Austin Borger4a870a32022-02-25 01:48:41 +0000181 sessionStats->onActive(maxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700182}
183
Austin Borger0fb3ad92023-06-01 16:51:35 -0700184void CameraServiceProxyWrapper::logIdle(const std::string& id,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700185 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700186 const std::string& userTag, int32_t videoStabilizationMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700187 const std::vector<hardware::CameraStreamStats>& streamStats) {
188 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
189 {
190 Mutex::Autolock l(mLock);
191 sessionStats = mSessionStatsMap[id];
192 }
193
194 if (sessionStats == nullptr) {
195 ALOGE("%s: SessionStatsMap should contain camera %s when logIdle is called",
196 __FUNCTION__, id.c_str());
197 return;
198 }
199
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800200 ALOGV("%s: id %s, requestCount %" PRId64 ", resultErrorCount %" PRId64 ", deviceError %d"
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700201 ", userTag %s, videoStabilizationMode %d", __FUNCTION__, id.c_str(), requestCount,
202 resultErrorCount, deviceError, userTag.c_str(), videoStabilizationMode);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700203 for (size_t i = 0; i < streamStats.size(); i++) {
204 ALOGV("%s: streamStats[%zu]: w %d h %d, requestedCount %" PRId64 ", dropCount %"
205 PRId64 ", startTimeMs %d" ,
206 __FUNCTION__, i, streamStats[i].mWidth, streamStats[i].mHeight,
207 streamStats[i].mRequestCount, streamStats[i].mErrorCount,
208 streamStats[i].mStartLatencyMs);
209 }
210
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700211 sessionStats->onIdle(requestCount, resultErrorCount, deviceError, userTag,
212 videoStabilizationMode, streamStats);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700213}
214
Austin Borger0fb3ad92023-06-01 16:51:35 -0700215void CameraServiceProxyWrapper::logOpen(const std::string& id, int facing,
216 const std::string& clientPackageName, int effectiveApiLevel, bool isNdk,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700217 int32_t latencyMs) {
218 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
219 {
220 Mutex::Autolock l(mLock);
221 if (mSessionStatsMap.count(id) > 0) {
222 ALOGE("%s: SessionStatsMap shouldn't contain camera %s",
223 __FUNCTION__, id.c_str());
224 return;
225 }
226
227 int apiLevel = CameraSessionStats::CAMERA_API_LEVEL_1;
228 if (effectiveApiLevel == 2) {
229 apiLevel = CameraSessionStats::CAMERA_API_LEVEL_2;
230 }
231
Austin Borger0fb3ad92023-06-01 16:51:35 -0700232 sessionStats = std::make_shared<CameraSessionStatsWrapper>(id, facing,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700233 CameraSessionStats::CAMERA_STATE_OPEN, clientPackageName,
234 apiLevel, isNdk, latencyMs);
235 mSessionStatsMap.emplace(id, sessionStats);
236 ALOGV("%s: Adding id %s", __FUNCTION__, id.c_str());
237 }
238
239 ALOGV("%s: id %s, facing %d, effectiveApiLevel %d, isNdk %d, latencyMs %d",
240 __FUNCTION__, id.c_str(), facing, effectiveApiLevel, isNdk, latencyMs);
241 sessionStats->onOpen();
242}
243
Austin Borger0fb3ad92023-06-01 16:51:35 -0700244void CameraServiceProxyWrapper::logClose(const std::string& id, int32_t latencyMs) {
Shuzhen Wang316781a2020-08-18 18:11:01 -0700245 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
246 {
247 Mutex::Autolock l(mLock);
248 if (mSessionStatsMap.count(id) == 0) {
249 ALOGE("%s: SessionStatsMap should contain camera %s before it's closed",
250 __FUNCTION__, id.c_str());
251 return;
252 }
253
254 sessionStats = mSessionStatsMap[id];
255 if (sessionStats == nullptr) {
256 ALOGE("%s: SessionStatsMap should contain camera %s",
257 __FUNCTION__, id.c_str());
258 return;
259 }
260 mSessionStatsMap.erase(id);
261 ALOGV("%s: Erasing id %s", __FUNCTION__, id.c_str());
262 }
263
264 ALOGV("%s: id %s, latencyMs %d", __FUNCTION__, id.c_str(), latencyMs);
265 sessionStats->onClose(latencyMs);
266}
267
Austin Borger80173a92022-08-03 17:50:40 -0700268bool CameraServiceProxyWrapper::isCameraDisabled(int userId) {
Austin Borger5f7abe22022-04-26 15:55:10 -0700269 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
270 if (proxyBinder == nullptr) return true;
271 bool ret = false;
Austin Borger80173a92022-08-03 17:50:40 -0700272 auto status = proxyBinder->isCameraDisabled(userId, &ret);
Austin Borger5f7abe22022-04-26 15:55:10 -0700273 if (!status.isOk()) {
274 ALOGE("%s: Failed during camera disabled query: %s", __FUNCTION__,
275 status.exceptionMessage().c_str());
276 }
277 return ret;
278}
279
Shuzhen Wang316781a2020-08-18 18:11:01 -0700280}; // namespace android