blob: bed576fb2f078992d99ee6109ed82ab1cb3fdc27 [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>
23#include <binder/IServiceManager.h>
24
25#include "CameraServiceProxyWrapper.h"
26
27namespace android {
28
29using hardware::ICameraServiceProxy;
30using hardware::CameraSessionStats;
31
Shuzhen Wang316781a2020-08-18 18:11:01 -070032/**
33 * CameraSessionStatsWrapper functions
34 */
35
Austin Borger74fca042022-05-23 12:41:21 -070036void CameraServiceProxyWrapper::CameraSessionStatsWrapper::updateProxyDeviceState(
37 sp<hardware::ICameraServiceProxy>& proxyBinder) {
38 if (proxyBinder == nullptr) return;
39 proxyBinder->notifyCameraState(mSessionStats);
Shuzhen Wang316781a2020-08-18 18:11:01 -070040}
41
Austin Borger74fca042022-05-23 12:41:21 -070042void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onOpen(
43 sp<hardware::ICameraServiceProxy>& proxyBinder) {
44 Mutex::Autolock l(mLock);
45 updateProxyDeviceState(proxyBinder);
46}
47
48void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onClose(
49 sp<hardware::ICameraServiceProxy>& proxyBinder, int32_t latencyMs) {
Shuzhen Wang316781a2020-08-18 18:11:01 -070050 Mutex::Autolock l(mLock);
51
52 mSessionStats.mNewCameraState = CameraSessionStats::CAMERA_STATE_CLOSED;
53 mSessionStats.mLatencyMs = latencyMs;
Austin Borger74fca042022-05-23 12:41:21 -070054 updateProxyDeviceState(proxyBinder);
Shuzhen Wang316781a2020-08-18 18:11:01 -070055}
56
57void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onStreamConfigured(
58 int operatingMode, bool internalReconfig, int32_t latencyMs) {
59 Mutex::Autolock l(mLock);
60
61 if (internalReconfig) {
62 mSessionStats.mInternalReconfigure++;
63 } else {
64 mSessionStats.mLatencyMs = latencyMs;
65 mSessionStats.mSessionType = operatingMode;
66 }
67}
68
Austin Borger74fca042022-05-23 12:41:21 -070069void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onActive(
70 sp<hardware::ICameraServiceProxy>& proxyBinder, float maxPreviewFps) {
Shuzhen Wang316781a2020-08-18 18:11:01 -070071 Mutex::Autolock l(mLock);
72
73 mSessionStats.mNewCameraState = CameraSessionStats::CAMERA_STATE_ACTIVE;
Austin Borger4a870a32022-02-25 01:48:41 +000074 mSessionStats.mMaxPreviewFps = maxPreviewFps;
Austin Borger74fca042022-05-23 12:41:21 -070075 updateProxyDeviceState(proxyBinder);
Shuzhen Wang316781a2020-08-18 18:11:01 -070076
77 // Reset mCreationDuration to -1 to distinguish between 1st session
78 // after configuration, and all other sessions after configuration.
79 mSessionStats.mLatencyMs = -1;
80}
81
82void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onIdle(
Austin Borger74fca042022-05-23 12:41:21 -070083 sp<hardware::ICameraServiceProxy>& proxyBinder,
Shuzhen Wang316781a2020-08-18 18:11:01 -070084 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;
Shuzhen Wangd26b1862022-03-07 12:05:05 -080093 mSessionStats.mUserTag = String16(userTag.c_str());
Shuzhen Wang9372b0b2022-05-11 18:55:19 -070094 mSessionStats.mVideoStabilizationMode = videoStabilizationMode;
Shuzhen Wang316781a2020-08-18 18:11:01 -070095 mSessionStats.mStreamStats = streamStats;
Austin Borger74fca042022-05-23 12:41:21 -070096 updateProxyDeviceState(proxyBinder);
Shuzhen Wang316781a2020-08-18 18:11:01 -070097
98 mSessionStats.mInternalReconfigure = 0;
99 mSessionStats.mStreamStats.clear();
100}
101
102/**
103 * CameraServiceProxyWrapper functions
104 */
105
106sp<ICameraServiceProxy> CameraServiceProxyWrapper::getCameraServiceProxy() {
107#ifndef __BRILLO__
Austin Borger74fca042022-05-23 12:41:21 -0700108 Mutex::Autolock al(mProxyMutex);
109 if (mCameraServiceProxy == nullptr) {
110 mCameraServiceProxy = getDefaultCameraServiceProxy();
Shuzhen Wang316781a2020-08-18 18:11:01 -0700111 }
112#endif
Austin Borger74fca042022-05-23 12:41:21 -0700113 return mCameraServiceProxy;
114}
115
116sp<hardware::ICameraServiceProxy> CameraServiceProxyWrapper::getDefaultCameraServiceProxy() {
117#ifndef __BRILLO__
118 sp<IServiceManager> sm = defaultServiceManager();
119 // Use checkService because cameraserver normally starts before the
120 // system server and the proxy service. So the long timeout that getService
121 // has before giving up is inappropriate.
122 sp<IBinder> binder = sm->checkService(String16("media.camera.proxy"));
123 if (binder != nullptr) {
124 return interface_cast<ICameraServiceProxy>(binder);
125 }
126#endif
127 return nullptr;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700128}
129
130void CameraServiceProxyWrapper::pingCameraServiceProxy() {
131 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
132 if (proxyBinder == nullptr) return;
133 proxyBinder->pingForUserUpdate();
134}
135
Emilian Peev065b2c12021-11-23 13:12:57 -0800136int CameraServiceProxyWrapper::getRotateAndCropOverride(String16 packageName, int lensFacing,
137 int userId) {
Emilian Peevb91f1802021-03-23 14:50:28 -0700138 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
139 if (proxyBinder == nullptr) return true;
Emilian Peev5368ebf2021-10-08 17:52:18 -0700140 int ret = 0;
Emilian Peev065b2c12021-11-23 13:12:57 -0800141 auto status = proxyBinder->getRotateAndCropOverride(packageName, lensFacing, userId, &ret);
Emilian Peevb91f1802021-03-23 14:50:28 -0700142 if (!status.isOk()) {
143 ALOGE("%s: Failed during top activity orientation query: %s", __FUNCTION__,
144 status.exceptionMessage().c_str());
145 }
146
147 return ret;
148}
149
Bharatt Kukreja7146ced2022-10-25 15:45:29 +0000150int CameraServiceProxyWrapper::getAutoframingOverride(const String16& packageName) {
151 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
152 if (proxyBinder == nullptr) {
153 return ANDROID_CONTROL_AUTOFRAMING_OFF;
154 }
155 int ret = 0;
156 auto status = proxyBinder->getAutoframingOverride(packageName, &ret);
157 if (!status.isOk()) {
158 ALOGE("%s: Failed during autoframing override query: %s", __FUNCTION__,
159 status.exceptionMessage().c_str());
160 }
161
162 return ret;
163}
164
Shuzhen Wang316781a2020-08-18 18:11:01 -0700165void CameraServiceProxyWrapper::logStreamConfigured(const String8& id,
166 int operatingMode, bool internalConfig, int32_t latencyMs) {
167 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
168 {
169 Mutex::Autolock l(mLock);
170 sessionStats = mSessionStatsMap[id];
171 if (sessionStats == nullptr) {
172 ALOGE("%s: SessionStatsMap should contain camera %s",
173 __FUNCTION__, id.c_str());
174 return;
175 }
176 }
177
178 ALOGV("%s: id %s, operatingMode %d, internalConfig %d, latencyMs %d",
179 __FUNCTION__, id.c_str(), operatingMode, internalConfig, latencyMs);
180 sessionStats->onStreamConfigured(operatingMode, internalConfig, latencyMs);
181}
182
Austin Borger4a870a32022-02-25 01:48:41 +0000183void CameraServiceProxyWrapper::logActive(const String8& id, float maxPreviewFps) {
Shuzhen Wang316781a2020-08-18 18:11:01 -0700184 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
185 {
186 Mutex::Autolock l(mLock);
187 sessionStats = mSessionStatsMap[id];
188 if (sessionStats == nullptr) {
189 ALOGE("%s: SessionStatsMap should contain camera %s when logActive is called",
190 __FUNCTION__, id.c_str());
191 return;
192 }
193 }
194
195 ALOGV("%s: id %s", __FUNCTION__, id.c_str());
Austin Borger74fca042022-05-23 12:41:21 -0700196 sp<hardware::ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
197 sessionStats->onActive(proxyBinder, maxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700198}
199
200void CameraServiceProxyWrapper::logIdle(const String8& id,
201 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700202 const std::string& userTag, int32_t videoStabilizationMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700203 const std::vector<hardware::CameraStreamStats>& streamStats) {
204 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
205 {
206 Mutex::Autolock l(mLock);
207 sessionStats = mSessionStatsMap[id];
208 }
209
210 if (sessionStats == nullptr) {
211 ALOGE("%s: SessionStatsMap should contain camera %s when logIdle is called",
212 __FUNCTION__, id.c_str());
213 return;
214 }
215
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800216 ALOGV("%s: id %s, requestCount %" PRId64 ", resultErrorCount %" PRId64 ", deviceError %d"
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700217 ", userTag %s, videoStabilizationMode %d", __FUNCTION__, id.c_str(), requestCount,
218 resultErrorCount, deviceError, userTag.c_str(), videoStabilizationMode);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700219 for (size_t i = 0; i < streamStats.size(); i++) {
220 ALOGV("%s: streamStats[%zu]: w %d h %d, requestedCount %" PRId64 ", dropCount %"
221 PRId64 ", startTimeMs %d" ,
222 __FUNCTION__, i, streamStats[i].mWidth, streamStats[i].mHeight,
223 streamStats[i].mRequestCount, streamStats[i].mErrorCount,
224 streamStats[i].mStartLatencyMs);
225 }
226
Austin Borger74fca042022-05-23 12:41:21 -0700227 sp<hardware::ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
228 sessionStats->onIdle(proxyBinder, requestCount, resultErrorCount, deviceError, userTag,
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700229 videoStabilizationMode, streamStats);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700230}
231
232void CameraServiceProxyWrapper::logOpen(const String8& id, int facing,
233 const String16& clientPackageName, int effectiveApiLevel, bool isNdk,
234 int32_t latencyMs) {
235 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
236 {
237 Mutex::Autolock l(mLock);
238 if (mSessionStatsMap.count(id) > 0) {
239 ALOGE("%s: SessionStatsMap shouldn't contain camera %s",
240 __FUNCTION__, id.c_str());
241 return;
242 }
243
244 int apiLevel = CameraSessionStats::CAMERA_API_LEVEL_1;
245 if (effectiveApiLevel == 2) {
246 apiLevel = CameraSessionStats::CAMERA_API_LEVEL_2;
247 }
248
249 sessionStats = std::make_shared<CameraSessionStatsWrapper>(String16(id), facing,
250 CameraSessionStats::CAMERA_STATE_OPEN, clientPackageName,
251 apiLevel, isNdk, latencyMs);
252 mSessionStatsMap.emplace(id, sessionStats);
253 ALOGV("%s: Adding id %s", __FUNCTION__, id.c_str());
254 }
255
256 ALOGV("%s: id %s, facing %d, effectiveApiLevel %d, isNdk %d, latencyMs %d",
257 __FUNCTION__, id.c_str(), facing, effectiveApiLevel, isNdk, latencyMs);
Austin Borger74fca042022-05-23 12:41:21 -0700258 sp<hardware::ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
259 sessionStats->onOpen(proxyBinder);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700260}
261
262void CameraServiceProxyWrapper::logClose(const String8& id, int32_t latencyMs) {
263 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
264 {
265 Mutex::Autolock l(mLock);
266 if (mSessionStatsMap.count(id) == 0) {
267 ALOGE("%s: SessionStatsMap should contain camera %s before it's closed",
268 __FUNCTION__, id.c_str());
269 return;
270 }
271
272 sessionStats = mSessionStatsMap[id];
273 if (sessionStats == nullptr) {
274 ALOGE("%s: SessionStatsMap should contain camera %s",
275 __FUNCTION__, id.c_str());
276 return;
277 }
278 mSessionStatsMap.erase(id);
279 ALOGV("%s: Erasing id %s", __FUNCTION__, id.c_str());
280 }
281
282 ALOGV("%s: id %s, latencyMs %d", __FUNCTION__, id.c_str(), latencyMs);
Austin Borger74fca042022-05-23 12:41:21 -0700283 sp<hardware::ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
284 sessionStats->onClose(proxyBinder, latencyMs);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700285}
286
Austin Borger9bfa0a72022-08-03 17:50:40 -0700287bool CameraServiceProxyWrapper::isCameraDisabled(int userId) {
Austin Borger5f7abe22022-04-26 15:55:10 -0700288 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
289 if (proxyBinder == nullptr) return true;
290 bool ret = false;
Austin Borger9bfa0a72022-08-03 17:50:40 -0700291 auto status = proxyBinder->isCameraDisabled(userId, &ret);
Austin Borger5f7abe22022-04-26 15:55:10 -0700292 if (!status.isOk()) {
293 ALOGE("%s: Failed during camera disabled query: %s", __FUNCTION__,
294 status.exceptionMessage().c_str());
295 }
296 return ret;
297}
298
Shuzhen Wang316781a2020-08-18 18:11:01 -0700299}; // namespace android