blob: 4225366f3e84112fa72f2a7ecf121cb524af91d1 [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(
Shuzhen Wang03fe6232023-02-05 12:41:15 -080049 sp<hardware::ICameraServiceProxy>& proxyBinder, int32_t latencyMs,
50 bool deviceError) {
Shuzhen Wang316781a2020-08-18 18:11:01 -070051 Mutex::Autolock l(mLock);
52
53 mSessionStats.mNewCameraState = CameraSessionStats::CAMERA_STATE_CLOSED;
54 mSessionStats.mLatencyMs = latencyMs;
Shuzhen Wang03fe6232023-02-05 12:41:15 -080055 mSessionStats.mDeviceError = deviceError;
Austin Borger74fca042022-05-23 12:41:21 -070056 updateProxyDeviceState(proxyBinder);
Shuzhen Wang316781a2020-08-18 18:11:01 -070057}
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 Borger74fca042022-05-23 12:41:21 -070071void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onActive(
72 sp<hardware::ICameraServiceProxy>& proxyBinder, float maxPreviewFps) {
Shuzhen Wang316781a2020-08-18 18:11:01 -070073 Mutex::Autolock l(mLock);
74
75 mSessionStats.mNewCameraState = CameraSessionStats::CAMERA_STATE_ACTIVE;
Austin Borger4a870a32022-02-25 01:48:41 +000076 mSessionStats.mMaxPreviewFps = maxPreviewFps;
Austin Borger74fca042022-05-23 12:41:21 -070077 updateProxyDeviceState(proxyBinder);
Shuzhen Wang316781a2020-08-18 18:11:01 -070078
79 // Reset mCreationDuration to -1 to distinguish between 1st session
80 // after configuration, and all other sessions after configuration.
81 mSessionStats.mLatencyMs = -1;
82}
83
84void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onIdle(
Austin Borger74fca042022-05-23 12:41:21 -070085 sp<hardware::ICameraServiceProxy>& proxyBinder,
Shuzhen Wang316781a2020-08-18 18:11:01 -070086 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
Shuzhen Wang9372b0b2022-05-11 18:55:19 -070087 const std::string& userTag, int32_t videoStabilizationMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -070088 const std::vector<hardware::CameraStreamStats>& streamStats) {
89 Mutex::Autolock l(mLock);
90
91 mSessionStats.mNewCameraState = CameraSessionStats::CAMERA_STATE_IDLE;
92 mSessionStats.mRequestCount = requestCount;
93 mSessionStats.mResultErrorCount = resultErrorCount;
94 mSessionStats.mDeviceError = deviceError;
Shuzhen Wangd26b1862022-03-07 12:05:05 -080095 mSessionStats.mUserTag = String16(userTag.c_str());
Shuzhen Wang9372b0b2022-05-11 18:55:19 -070096 mSessionStats.mVideoStabilizationMode = videoStabilizationMode;
Shuzhen Wang316781a2020-08-18 18:11:01 -070097 mSessionStats.mStreamStats = streamStats;
Austin Borger74fca042022-05-23 12:41:21 -070098 updateProxyDeviceState(proxyBinder);
Shuzhen Wang316781a2020-08-18 18:11:01 -070099
100 mSessionStats.mInternalReconfigure = 0;
101 mSessionStats.mStreamStats.clear();
102}
103
Avichal Rakesh1fff2d12023-03-03 15:05:48 -0800104int64_t CameraServiceProxyWrapper::CameraSessionStatsWrapper::getLogId() {
105 Mutex::Autolock l(mLock);
106 return mSessionStats.mLogId;
107}
108
Shuzhen Wang316781a2020-08-18 18:11:01 -0700109/**
110 * CameraServiceProxyWrapper functions
111 */
112
113sp<ICameraServiceProxy> CameraServiceProxyWrapper::getCameraServiceProxy() {
114#ifndef __BRILLO__
Austin Borger74fca042022-05-23 12:41:21 -0700115 Mutex::Autolock al(mProxyMutex);
116 if (mCameraServiceProxy == nullptr) {
117 mCameraServiceProxy = getDefaultCameraServiceProxy();
Shuzhen Wang316781a2020-08-18 18:11:01 -0700118 }
119#endif
Austin Borger74fca042022-05-23 12:41:21 -0700120 return mCameraServiceProxy;
121}
122
123sp<hardware::ICameraServiceProxy> CameraServiceProxyWrapper::getDefaultCameraServiceProxy() {
124#ifndef __BRILLO__
125 sp<IServiceManager> sm = defaultServiceManager();
126 // Use checkService because cameraserver normally starts before the
127 // system server and the proxy service. So the long timeout that getService
128 // has before giving up is inappropriate.
129 sp<IBinder> binder = sm->checkService(String16("media.camera.proxy"));
130 if (binder != nullptr) {
131 return interface_cast<ICameraServiceProxy>(binder);
132 }
133#endif
134 return nullptr;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700135}
136
137void CameraServiceProxyWrapper::pingCameraServiceProxy() {
138 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
139 if (proxyBinder == nullptr) return;
140 proxyBinder->pingForUserUpdate();
141}
142
Emilian Peev065b2c12021-11-23 13:12:57 -0800143int CameraServiceProxyWrapper::getRotateAndCropOverride(String16 packageName, int lensFacing,
144 int userId) {
Emilian Peevb91f1802021-03-23 14:50:28 -0700145 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
146 if (proxyBinder == nullptr) return true;
Emilian Peev5368ebf2021-10-08 17:52:18 -0700147 int ret = 0;
Emilian Peev065b2c12021-11-23 13:12:57 -0800148 auto status = proxyBinder->getRotateAndCropOverride(packageName, lensFacing, userId, &ret);
Emilian Peevb91f1802021-03-23 14:50:28 -0700149 if (!status.isOk()) {
150 ALOGE("%s: Failed during top activity orientation query: %s", __FUNCTION__,
151 status.exceptionMessage().c_str());
152 }
153
154 return ret;
155}
156
Bharatt Kukreja7146ced2022-10-25 15:45:29 +0000157int CameraServiceProxyWrapper::getAutoframingOverride(const String16& packageName) {
158 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
159 if (proxyBinder == nullptr) {
160 return ANDROID_CONTROL_AUTOFRAMING_OFF;
161 }
162 int ret = 0;
163 auto status = proxyBinder->getAutoframingOverride(packageName, &ret);
164 if (!status.isOk()) {
165 ALOGE("%s: Failed during autoframing override query: %s", __FUNCTION__,
166 status.exceptionMessage().c_str());
167 }
168
169 return ret;
170}
171
Shuzhen Wang316781a2020-08-18 18:11:01 -0700172void CameraServiceProxyWrapper::logStreamConfigured(const String8& id,
173 int operatingMode, bool internalConfig, int32_t latencyMs) {
174 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
175 {
176 Mutex::Autolock l(mLock);
177 sessionStats = mSessionStatsMap[id];
178 if (sessionStats == nullptr) {
179 ALOGE("%s: SessionStatsMap should contain camera %s",
180 __FUNCTION__, id.c_str());
181 return;
182 }
183 }
184
185 ALOGV("%s: id %s, operatingMode %d, internalConfig %d, latencyMs %d",
186 __FUNCTION__, id.c_str(), operatingMode, internalConfig, latencyMs);
187 sessionStats->onStreamConfigured(operatingMode, internalConfig, latencyMs);
188}
189
Austin Borger4a870a32022-02-25 01:48:41 +0000190void CameraServiceProxyWrapper::logActive(const String8& id, float maxPreviewFps) {
Shuzhen Wang316781a2020-08-18 18:11:01 -0700191 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
192 {
193 Mutex::Autolock l(mLock);
194 sessionStats = mSessionStatsMap[id];
195 if (sessionStats == nullptr) {
196 ALOGE("%s: SessionStatsMap should contain camera %s when logActive is called",
197 __FUNCTION__, id.c_str());
198 return;
199 }
200 }
201
202 ALOGV("%s: id %s", __FUNCTION__, id.c_str());
Austin Borger74fca042022-05-23 12:41:21 -0700203 sp<hardware::ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
204 sessionStats->onActive(proxyBinder, maxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700205}
206
207void CameraServiceProxyWrapper::logIdle(const String8& id,
208 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700209 const std::string& userTag, int32_t videoStabilizationMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700210 const std::vector<hardware::CameraStreamStats>& streamStats) {
211 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
212 {
213 Mutex::Autolock l(mLock);
214 sessionStats = mSessionStatsMap[id];
215 }
216
217 if (sessionStats == nullptr) {
218 ALOGE("%s: SessionStatsMap should contain camera %s when logIdle is called",
219 __FUNCTION__, id.c_str());
220 return;
221 }
222
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800223 ALOGV("%s: id %s, requestCount %" PRId64 ", resultErrorCount %" PRId64 ", deviceError %d"
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700224 ", userTag %s, videoStabilizationMode %d", __FUNCTION__, id.c_str(), requestCount,
225 resultErrorCount, deviceError, userTag.c_str(), videoStabilizationMode);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700226 for (size_t i = 0; i < streamStats.size(); i++) {
227 ALOGV("%s: streamStats[%zu]: w %d h %d, requestedCount %" PRId64 ", dropCount %"
228 PRId64 ", startTimeMs %d" ,
229 __FUNCTION__, i, streamStats[i].mWidth, streamStats[i].mHeight,
230 streamStats[i].mRequestCount, streamStats[i].mErrorCount,
231 streamStats[i].mStartLatencyMs);
232 }
233
Austin Borger74fca042022-05-23 12:41:21 -0700234 sp<hardware::ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
235 sessionStats->onIdle(proxyBinder, requestCount, resultErrorCount, deviceError, userTag,
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700236 videoStabilizationMode, streamStats);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700237}
238
239void CameraServiceProxyWrapper::logOpen(const String8& id, int facing,
240 const String16& clientPackageName, int effectiveApiLevel, bool isNdk,
241 int32_t latencyMs) {
242 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
243 {
244 Mutex::Autolock l(mLock);
245 if (mSessionStatsMap.count(id) > 0) {
246 ALOGE("%s: SessionStatsMap shouldn't contain camera %s",
247 __FUNCTION__, id.c_str());
248 return;
249 }
250
251 int apiLevel = CameraSessionStats::CAMERA_API_LEVEL_1;
252 if (effectiveApiLevel == 2) {
253 apiLevel = CameraSessionStats::CAMERA_API_LEVEL_2;
254 }
255
Avichal Rakesh88fc5222023-03-03 15:00:59 -0800256 // Generate a new log ID for open events
257 int64_t logId = generateLogId(mRandomDevice);
258
259 sessionStats = std::make_shared<CameraSessionStatsWrapper>(
260 String16(id), facing, CameraSessionStats::CAMERA_STATE_OPEN, clientPackageName,
261 apiLevel, isNdk, latencyMs, logId);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700262 mSessionStatsMap.emplace(id, sessionStats);
263 ALOGV("%s: Adding id %s", __FUNCTION__, id.c_str());
264 }
265
266 ALOGV("%s: id %s, facing %d, effectiveApiLevel %d, isNdk %d, latencyMs %d",
267 __FUNCTION__, id.c_str(), facing, effectiveApiLevel, isNdk, latencyMs);
Austin Borger74fca042022-05-23 12:41:21 -0700268 sp<hardware::ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
269 sessionStats->onOpen(proxyBinder);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700270}
271
Shuzhen Wang03fe6232023-02-05 12:41:15 -0800272void CameraServiceProxyWrapper::logClose(const String8& id, int32_t latencyMs, bool deviceError) {
Shuzhen Wang316781a2020-08-18 18:11:01 -0700273 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
274 {
275 Mutex::Autolock l(mLock);
276 if (mSessionStatsMap.count(id) == 0) {
277 ALOGE("%s: SessionStatsMap should contain camera %s before it's closed",
278 __FUNCTION__, id.c_str());
279 return;
280 }
281
282 sessionStats = mSessionStatsMap[id];
283 if (sessionStats == nullptr) {
284 ALOGE("%s: SessionStatsMap should contain camera %s",
285 __FUNCTION__, id.c_str());
286 return;
287 }
Shuzhen Wang03fe6232023-02-05 12:41:15 -0800288
Shuzhen Wang316781a2020-08-18 18:11:01 -0700289 mSessionStatsMap.erase(id);
Shuzhen Wang03fe6232023-02-05 12:41:15 -0800290 ALOGV("%s: Erasing id %s, deviceError %d", __FUNCTION__, id.c_str(), deviceError);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700291 }
292
Shuzhen Wang03fe6232023-02-05 12:41:15 -0800293 ALOGV("%s: id %s, latencyMs %d, deviceError %d", __FUNCTION__,
294 id.c_str(), latencyMs, deviceError);
Austin Borger74fca042022-05-23 12:41:21 -0700295 sp<hardware::ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
Shuzhen Wang03fe6232023-02-05 12:41:15 -0800296 sessionStats->onClose(proxyBinder, latencyMs, deviceError);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700297}
298
Austin Borger9bfa0a72022-08-03 17:50:40 -0700299bool CameraServiceProxyWrapper::isCameraDisabled(int userId) {
Austin Borger5f7abe22022-04-26 15:55:10 -0700300 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
301 if (proxyBinder == nullptr) return true;
302 bool ret = false;
Austin Borger9bfa0a72022-08-03 17:50:40 -0700303 auto status = proxyBinder->isCameraDisabled(userId, &ret);
Austin Borger5f7abe22022-04-26 15:55:10 -0700304 if (!status.isOk()) {
305 ALOGE("%s: Failed during camera disabled query: %s", __FUNCTION__,
306 status.exceptionMessage().c_str());
307 }
308 return ret;
309}
310
Avichal Rakesh1fff2d12023-03-03 15:05:48 -0800311int64_t CameraServiceProxyWrapper::getCurrentLogIdForCamera(const String8& cameraId) {
312 std::shared_ptr<CameraSessionStatsWrapper> stats;
313 {
314 Mutex::Autolock _l(mLock);
315 if (mSessionStatsMap.count(cameraId) == 0) {
316 ALOGE("%s: SessionStatsMap should contain camera %s before asking for its logging ID.",
317 __FUNCTION__, cameraId.c_str());
318 return 0;
319 }
320
321 stats = mSessionStatsMap[cameraId];
322 }
323 return stats->getLogId();
324}
325
Avichal Rakesh88fc5222023-03-03 15:00:59 -0800326int64_t CameraServiceProxyWrapper::generateLogId(std::random_device& randomDevice) {
327 int64_t ret = 0;
328 do {
329 // std::random_device generates 32 bits per call, so we call it twice
330 ret = randomDevice();
331 ret = ret << 32;
332 ret = ret | randomDevice();
333 } while (ret == 0); // 0 is not a valid identifier
334
335 return ret;
336}
337
338} // namespace android