blob: f7cede8ab0f3c988bcc43af37a8f2b60f6ff5ddd [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
32Mutex CameraServiceProxyWrapper::sProxyMutex;
33sp<hardware::ICameraServiceProxy> CameraServiceProxyWrapper::sCameraServiceProxy;
34
35Mutex CameraServiceProxyWrapper::mLock;
36std::map<String8, std::shared_ptr<CameraServiceProxyWrapper::CameraSessionStatsWrapper>>
37 CameraServiceProxyWrapper::mSessionStatsMap;
38
39/**
40 * CameraSessionStatsWrapper functions
41 */
42
43void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onOpen() {
44 Mutex::Autolock l(mLock);
45
46 updateProxyDeviceState(mSessionStats);
47}
48
49void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onClose(int32_t latencyMs) {
50 Mutex::Autolock l(mLock);
51
52 mSessionStats.mNewCameraState = CameraSessionStats::CAMERA_STATE_CLOSED;
53 mSessionStats.mLatencyMs = latencyMs;
54 updateProxyDeviceState(mSessionStats);
55}
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 Borger4a870a32022-02-25 01:48:41 +000069void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onActive(float maxPreviewFps) {
Shuzhen Wang316781a2020-08-18 18:11:01 -070070 Mutex::Autolock l(mLock);
71
72 mSessionStats.mNewCameraState = CameraSessionStats::CAMERA_STATE_ACTIVE;
Austin Borger4a870a32022-02-25 01:48:41 +000073 mSessionStats.mMaxPreviewFps = maxPreviewFps;
Shuzhen Wang316781a2020-08-18 18:11:01 -070074 updateProxyDeviceState(mSessionStats);
75
76 // Reset mCreationDuration to -1 to distinguish between 1st session
77 // after configuration, and all other sessions after configuration.
78 mSessionStats.mLatencyMs = -1;
79}
80
81void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onIdle(
82 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
Shuzhen Wangd26b1862022-03-07 12:05:05 -080083 const std::string& userTag,
Shuzhen Wang316781a2020-08-18 18:11:01 -070084 const std::vector<hardware::CameraStreamStats>& streamStats) {
85 Mutex::Autolock l(mLock);
86
87 mSessionStats.mNewCameraState = CameraSessionStats::CAMERA_STATE_IDLE;
88 mSessionStats.mRequestCount = requestCount;
89 mSessionStats.mResultErrorCount = resultErrorCount;
90 mSessionStats.mDeviceError = deviceError;
Shuzhen Wangd26b1862022-03-07 12:05:05 -080091 mSessionStats.mUserTag = String16(userTag.c_str());
Shuzhen Wang316781a2020-08-18 18:11:01 -070092 mSessionStats.mStreamStats = streamStats;
93 updateProxyDeviceState(mSessionStats);
94
95 mSessionStats.mInternalReconfigure = 0;
96 mSessionStats.mStreamStats.clear();
97}
98
99/**
100 * CameraServiceProxyWrapper functions
101 */
102
103sp<ICameraServiceProxy> CameraServiceProxyWrapper::getCameraServiceProxy() {
104#ifndef __BRILLO__
105 Mutex::Autolock al(sProxyMutex);
106 if (sCameraServiceProxy == nullptr) {
107 sp<IServiceManager> sm = defaultServiceManager();
108 // Use checkService because cameraserver normally starts before the
109 // system server and the proxy service. So the long timeout that getService
110 // has before giving up is inappropriate.
111 sp<IBinder> binder = sm->checkService(String16("media.camera.proxy"));
112 if (binder != nullptr) {
113 sCameraServiceProxy = interface_cast<ICameraServiceProxy>(binder);
114 }
115 }
116#endif
117 return sCameraServiceProxy;
118}
119
120void CameraServiceProxyWrapper::pingCameraServiceProxy() {
121 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
122 if (proxyBinder == nullptr) return;
123 proxyBinder->pingForUserUpdate();
124}
125
Emilian Peev065b2c12021-11-23 13:12:57 -0800126int CameraServiceProxyWrapper::getRotateAndCropOverride(String16 packageName, int lensFacing,
127 int userId) {
Emilian Peevb91f1802021-03-23 14:50:28 -0700128 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
129 if (proxyBinder == nullptr) return true;
Emilian Peev5368ebf2021-10-08 17:52:18 -0700130 int ret = 0;
Emilian Peev065b2c12021-11-23 13:12:57 -0800131 auto status = proxyBinder->getRotateAndCropOverride(packageName, lensFacing, userId, &ret);
Emilian Peevb91f1802021-03-23 14:50:28 -0700132 if (!status.isOk()) {
133 ALOGE("%s: Failed during top activity orientation query: %s", __FUNCTION__,
134 status.exceptionMessage().c_str());
135 }
136
137 return ret;
138}
139
Shuzhen Wang316781a2020-08-18 18:11:01 -0700140void CameraServiceProxyWrapper::updateProxyDeviceState(const CameraSessionStats& sessionStats) {
141 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
142 if (proxyBinder == nullptr) return;
143 proxyBinder->notifyCameraState(sessionStats);
144}
145
146void CameraServiceProxyWrapper::logStreamConfigured(const String8& id,
147 int operatingMode, bool internalConfig, int32_t latencyMs) {
148 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
149 {
150 Mutex::Autolock l(mLock);
151 sessionStats = mSessionStatsMap[id];
152 if (sessionStats == nullptr) {
153 ALOGE("%s: SessionStatsMap should contain camera %s",
154 __FUNCTION__, id.c_str());
155 return;
156 }
157 }
158
159 ALOGV("%s: id %s, operatingMode %d, internalConfig %d, latencyMs %d",
160 __FUNCTION__, id.c_str(), operatingMode, internalConfig, latencyMs);
161 sessionStats->onStreamConfigured(operatingMode, internalConfig, latencyMs);
162}
163
Austin Borger4a870a32022-02-25 01:48:41 +0000164void CameraServiceProxyWrapper::logActive(const String8& id, float maxPreviewFps) {
Shuzhen Wang316781a2020-08-18 18:11:01 -0700165 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
166 {
167 Mutex::Autolock l(mLock);
168 sessionStats = mSessionStatsMap[id];
169 if (sessionStats == nullptr) {
170 ALOGE("%s: SessionStatsMap should contain camera %s when logActive is called",
171 __FUNCTION__, id.c_str());
172 return;
173 }
174 }
175
176 ALOGV("%s: id %s", __FUNCTION__, id.c_str());
Austin Borger4a870a32022-02-25 01:48:41 +0000177 sessionStats->onActive(maxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700178}
179
180void CameraServiceProxyWrapper::logIdle(const String8& id,
181 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800182 const std::string& userTag,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700183 const std::vector<hardware::CameraStreamStats>& streamStats) {
184 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
185 {
186 Mutex::Autolock l(mLock);
187 sessionStats = mSessionStatsMap[id];
188 }
189
190 if (sessionStats == nullptr) {
191 ALOGE("%s: SessionStatsMap should contain camera %s when logIdle is called",
192 __FUNCTION__, id.c_str());
193 return;
194 }
195
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800196 ALOGV("%s: id %s, requestCount %" PRId64 ", resultErrorCount %" PRId64 ", deviceError %d"
197 ", userTag %s", __FUNCTION__, id.c_str(), requestCount, resultErrorCount,
198 deviceError, userTag.c_str());
Shuzhen Wang316781a2020-08-18 18:11:01 -0700199 for (size_t i = 0; i < streamStats.size(); i++) {
200 ALOGV("%s: streamStats[%zu]: w %d h %d, requestedCount %" PRId64 ", dropCount %"
201 PRId64 ", startTimeMs %d" ,
202 __FUNCTION__, i, streamStats[i].mWidth, streamStats[i].mHeight,
203 streamStats[i].mRequestCount, streamStats[i].mErrorCount,
204 streamStats[i].mStartLatencyMs);
205 }
206
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800207 sessionStats->onIdle(requestCount, resultErrorCount, deviceError, userTag, streamStats);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700208}
209
210void CameraServiceProxyWrapper::logOpen(const String8& id, int facing,
211 const String16& clientPackageName, int effectiveApiLevel, bool isNdk,
212 int32_t latencyMs) {
213 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
214 {
215 Mutex::Autolock l(mLock);
216 if (mSessionStatsMap.count(id) > 0) {
217 ALOGE("%s: SessionStatsMap shouldn't contain camera %s",
218 __FUNCTION__, id.c_str());
219 return;
220 }
221
222 int apiLevel = CameraSessionStats::CAMERA_API_LEVEL_1;
223 if (effectiveApiLevel == 2) {
224 apiLevel = CameraSessionStats::CAMERA_API_LEVEL_2;
225 }
226
227 sessionStats = std::make_shared<CameraSessionStatsWrapper>(String16(id), facing,
228 CameraSessionStats::CAMERA_STATE_OPEN, clientPackageName,
229 apiLevel, isNdk, latencyMs);
230 mSessionStatsMap.emplace(id, sessionStats);
231 ALOGV("%s: Adding id %s", __FUNCTION__, id.c_str());
232 }
233
234 ALOGV("%s: id %s, facing %d, effectiveApiLevel %d, isNdk %d, latencyMs %d",
235 __FUNCTION__, id.c_str(), facing, effectiveApiLevel, isNdk, latencyMs);
236 sessionStats->onOpen();
237}
238
239void CameraServiceProxyWrapper::logClose(const String8& id, int32_t latencyMs) {
240 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
241 {
242 Mutex::Autolock l(mLock);
243 if (mSessionStatsMap.count(id) == 0) {
244 ALOGE("%s: SessionStatsMap should contain camera %s before it's closed",
245 __FUNCTION__, id.c_str());
246 return;
247 }
248
249 sessionStats = mSessionStatsMap[id];
250 if (sessionStats == nullptr) {
251 ALOGE("%s: SessionStatsMap should contain camera %s",
252 __FUNCTION__, id.c_str());
253 return;
254 }
255 mSessionStatsMap.erase(id);
256 ALOGV("%s: Erasing id %s", __FUNCTION__, id.c_str());
257 }
258
259 ALOGV("%s: id %s, latencyMs %d", __FUNCTION__, id.c_str(), latencyMs);
260 sessionStats->onClose(latencyMs);
261}
262
Austin Borger5f7abe22022-04-26 15:55:10 -0700263bool CameraServiceProxyWrapper::isCameraDisabled() {
264 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
265 if (proxyBinder == nullptr) return true;
266 bool ret = false;
267 auto status = proxyBinder->isCameraDisabled(&ret);
268 if (!status.isOk()) {
269 ALOGE("%s: Failed during camera disabled query: %s", __FUNCTION__,
270 status.exceptionMessage().c_str());
271 }
272 return ret;
273}
274
Shuzhen Wang316781a2020-08-18 18:11:01 -0700275}; // namespace android