blob: 733ecd956c842e7a910e40ac8675954b1a874e03 [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
Shuzhen Wang316781a2020-08-18 18:11:01 -0700150void CameraServiceProxyWrapper::logStreamConfigured(const String8& id,
151 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 Borger4a870a32022-02-25 01:48:41 +0000168void CameraServiceProxyWrapper::logActive(const String8& 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 Borger74fca042022-05-23 12:41:21 -0700181 sp<hardware::ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
182 sessionStats->onActive(proxyBinder, maxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700183}
184
185void CameraServiceProxyWrapper::logIdle(const String8& id,
186 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700187 const std::string& userTag, int32_t videoStabilizationMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700188 const std::vector<hardware::CameraStreamStats>& streamStats) {
189 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
190 {
191 Mutex::Autolock l(mLock);
192 sessionStats = mSessionStatsMap[id];
193 }
194
195 if (sessionStats == nullptr) {
196 ALOGE("%s: SessionStatsMap should contain camera %s when logIdle is called",
197 __FUNCTION__, id.c_str());
198 return;
199 }
200
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800201 ALOGV("%s: id %s, requestCount %" PRId64 ", resultErrorCount %" PRId64 ", deviceError %d"
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700202 ", userTag %s, videoStabilizationMode %d", __FUNCTION__, id.c_str(), requestCount,
203 resultErrorCount, deviceError, userTag.c_str(), videoStabilizationMode);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700204 for (size_t i = 0; i < streamStats.size(); i++) {
205 ALOGV("%s: streamStats[%zu]: w %d h %d, requestedCount %" PRId64 ", dropCount %"
206 PRId64 ", startTimeMs %d" ,
207 __FUNCTION__, i, streamStats[i].mWidth, streamStats[i].mHeight,
208 streamStats[i].mRequestCount, streamStats[i].mErrorCount,
209 streamStats[i].mStartLatencyMs);
210 }
211
Austin Borger74fca042022-05-23 12:41:21 -0700212 sp<hardware::ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
213 sessionStats->onIdle(proxyBinder, requestCount, resultErrorCount, deviceError, userTag,
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700214 videoStabilizationMode, streamStats);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700215}
216
217void CameraServiceProxyWrapper::logOpen(const String8& id, int facing,
218 const String16& clientPackageName, int effectiveApiLevel, bool isNdk,
219 int32_t latencyMs) {
220 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
221 {
222 Mutex::Autolock l(mLock);
223 if (mSessionStatsMap.count(id) > 0) {
224 ALOGE("%s: SessionStatsMap shouldn't contain camera %s",
225 __FUNCTION__, id.c_str());
226 return;
227 }
228
229 int apiLevel = CameraSessionStats::CAMERA_API_LEVEL_1;
230 if (effectiveApiLevel == 2) {
231 apiLevel = CameraSessionStats::CAMERA_API_LEVEL_2;
232 }
233
234 sessionStats = std::make_shared<CameraSessionStatsWrapper>(String16(id), facing,
235 CameraSessionStats::CAMERA_STATE_OPEN, clientPackageName,
236 apiLevel, isNdk, latencyMs);
237 mSessionStatsMap.emplace(id, sessionStats);
238 ALOGV("%s: Adding id %s", __FUNCTION__, id.c_str());
239 }
240
241 ALOGV("%s: id %s, facing %d, effectiveApiLevel %d, isNdk %d, latencyMs %d",
242 __FUNCTION__, id.c_str(), facing, effectiveApiLevel, isNdk, latencyMs);
Austin Borger74fca042022-05-23 12:41:21 -0700243 sp<hardware::ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
244 sessionStats->onOpen(proxyBinder);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700245}
246
247void CameraServiceProxyWrapper::logClose(const String8& id, int32_t latencyMs) {
248 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
249 {
250 Mutex::Autolock l(mLock);
251 if (mSessionStatsMap.count(id) == 0) {
252 ALOGE("%s: SessionStatsMap should contain camera %s before it's closed",
253 __FUNCTION__, id.c_str());
254 return;
255 }
256
257 sessionStats = mSessionStatsMap[id];
258 if (sessionStats == nullptr) {
259 ALOGE("%s: SessionStatsMap should contain camera %s",
260 __FUNCTION__, id.c_str());
261 return;
262 }
263 mSessionStatsMap.erase(id);
264 ALOGV("%s: Erasing id %s", __FUNCTION__, id.c_str());
265 }
266
267 ALOGV("%s: id %s, latencyMs %d", __FUNCTION__, id.c_str(), latencyMs);
Austin Borger74fca042022-05-23 12:41:21 -0700268 sp<hardware::ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
269 sessionStats->onClose(proxyBinder, latencyMs);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700270}
271
Austin Borger5f7abe22022-04-26 15:55:10 -0700272bool CameraServiceProxyWrapper::isCameraDisabled() {
273 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
274 if (proxyBinder == nullptr) return true;
275 bool ret = false;
276 auto status = proxyBinder->isCameraDisabled(&ret);
277 if (!status.isOk()) {
278 ALOGE("%s: Failed during camera disabled query: %s", __FUNCTION__,
279 status.exceptionMessage().c_str());
280 }
281 return ret;
282}
283
Shuzhen Wang316781a2020-08-18 18:11:01 -0700284}; // namespace android