blob: 69175cc8d5be2a1c75e717c68673318c0b9584a1 [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 Wang9372b0b2022-05-11 18:55:19 -070083 const std::string& userTag, int32_t videoStabilizationMode,
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 Wang9372b0b2022-05-11 18:55:19 -070092 mSessionStats.mVideoStabilizationMode = videoStabilizationMode;
Shuzhen Wang316781a2020-08-18 18:11:01 -070093 mSessionStats.mStreamStats = streamStats;
94 updateProxyDeviceState(mSessionStats);
95
96 mSessionStats.mInternalReconfigure = 0;
97 mSessionStats.mStreamStats.clear();
98}
99
100/**
101 * CameraServiceProxyWrapper functions
102 */
103
104sp<ICameraServiceProxy> CameraServiceProxyWrapper::getCameraServiceProxy() {
105#ifndef __BRILLO__
106 Mutex::Autolock al(sProxyMutex);
107 if (sCameraServiceProxy == nullptr) {
108 sp<IServiceManager> sm = defaultServiceManager();
109 // Use checkService because cameraserver normally starts before the
110 // system server and the proxy service. So the long timeout that getService
111 // has before giving up is inappropriate.
112 sp<IBinder> binder = sm->checkService(String16("media.camera.proxy"));
113 if (binder != nullptr) {
114 sCameraServiceProxy = interface_cast<ICameraServiceProxy>(binder);
115 }
116 }
117#endif
118 return sCameraServiceProxy;
119}
120
121void CameraServiceProxyWrapper::pingCameraServiceProxy() {
122 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
123 if (proxyBinder == nullptr) return;
124 proxyBinder->pingForUserUpdate();
125}
126
Emilian Peev065b2c12021-11-23 13:12:57 -0800127int CameraServiceProxyWrapper::getRotateAndCropOverride(String16 packageName, int lensFacing,
128 int userId) {
Emilian Peevb91f1802021-03-23 14:50:28 -0700129 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
130 if (proxyBinder == nullptr) return true;
Emilian Peev5368ebf2021-10-08 17:52:18 -0700131 int ret = 0;
Emilian Peev065b2c12021-11-23 13:12:57 -0800132 auto status = proxyBinder->getRotateAndCropOverride(packageName, lensFacing, userId, &ret);
Emilian Peevb91f1802021-03-23 14:50:28 -0700133 if (!status.isOk()) {
134 ALOGE("%s: Failed during top activity orientation query: %s", __FUNCTION__,
135 status.exceptionMessage().c_str());
136 }
137
138 return ret;
139}
140
Shuzhen Wang316781a2020-08-18 18:11:01 -0700141void CameraServiceProxyWrapper::updateProxyDeviceState(const CameraSessionStats& sessionStats) {
142 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
143 if (proxyBinder == nullptr) return;
144 proxyBinder->notifyCameraState(sessionStats);
145}
146
147void CameraServiceProxyWrapper::logStreamConfigured(const String8& id,
148 int operatingMode, bool internalConfig, int32_t latencyMs) {
149 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
150 {
151 Mutex::Autolock l(mLock);
152 sessionStats = mSessionStatsMap[id];
153 if (sessionStats == nullptr) {
154 ALOGE("%s: SessionStatsMap should contain camera %s",
155 __FUNCTION__, id.c_str());
156 return;
157 }
158 }
159
160 ALOGV("%s: id %s, operatingMode %d, internalConfig %d, latencyMs %d",
161 __FUNCTION__, id.c_str(), operatingMode, internalConfig, latencyMs);
162 sessionStats->onStreamConfigured(operatingMode, internalConfig, latencyMs);
163}
164
Austin Borger4a870a32022-02-25 01:48:41 +0000165void CameraServiceProxyWrapper::logActive(const String8& id, float maxPreviewFps) {
Shuzhen Wang316781a2020-08-18 18:11:01 -0700166 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
167 {
168 Mutex::Autolock l(mLock);
169 sessionStats = mSessionStatsMap[id];
170 if (sessionStats == nullptr) {
171 ALOGE("%s: SessionStatsMap should contain camera %s when logActive is called",
172 __FUNCTION__, id.c_str());
173 return;
174 }
175 }
176
177 ALOGV("%s: id %s", __FUNCTION__, id.c_str());
Austin Borger4a870a32022-02-25 01:48:41 +0000178 sessionStats->onActive(maxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700179}
180
181void CameraServiceProxyWrapper::logIdle(const String8& id,
182 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700183 const std::string& userTag, int32_t videoStabilizationMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700184 const std::vector<hardware::CameraStreamStats>& streamStats) {
185 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
186 {
187 Mutex::Autolock l(mLock);
188 sessionStats = mSessionStatsMap[id];
189 }
190
191 if (sessionStats == nullptr) {
192 ALOGE("%s: SessionStatsMap should contain camera %s when logIdle is called",
193 __FUNCTION__, id.c_str());
194 return;
195 }
196
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800197 ALOGV("%s: id %s, requestCount %" PRId64 ", resultErrorCount %" PRId64 ", deviceError %d"
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700198 ", userTag %s, videoStabilizationMode %d", __FUNCTION__, id.c_str(), requestCount,
199 resultErrorCount, deviceError, userTag.c_str(), videoStabilizationMode);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700200 for (size_t i = 0; i < streamStats.size(); i++) {
201 ALOGV("%s: streamStats[%zu]: w %d h %d, requestedCount %" PRId64 ", dropCount %"
202 PRId64 ", startTimeMs %d" ,
203 __FUNCTION__, i, streamStats[i].mWidth, streamStats[i].mHeight,
204 streamStats[i].mRequestCount, streamStats[i].mErrorCount,
205 streamStats[i].mStartLatencyMs);
206 }
207
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700208 sessionStats->onIdle(requestCount, resultErrorCount, deviceError, userTag,
209 videoStabilizationMode, streamStats);
Shuzhen Wang316781a2020-08-18 18:11:01 -0700210}
211
212void CameraServiceProxyWrapper::logOpen(const String8& id, int facing,
213 const String16& clientPackageName, int effectiveApiLevel, bool isNdk,
214 int32_t latencyMs) {
215 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
216 {
217 Mutex::Autolock l(mLock);
218 if (mSessionStatsMap.count(id) > 0) {
219 ALOGE("%s: SessionStatsMap shouldn't contain camera %s",
220 __FUNCTION__, id.c_str());
221 return;
222 }
223
224 int apiLevel = CameraSessionStats::CAMERA_API_LEVEL_1;
225 if (effectiveApiLevel == 2) {
226 apiLevel = CameraSessionStats::CAMERA_API_LEVEL_2;
227 }
228
229 sessionStats = std::make_shared<CameraSessionStatsWrapper>(String16(id), facing,
230 CameraSessionStats::CAMERA_STATE_OPEN, clientPackageName,
231 apiLevel, isNdk, latencyMs);
232 mSessionStatsMap.emplace(id, sessionStats);
233 ALOGV("%s: Adding id %s", __FUNCTION__, id.c_str());
234 }
235
236 ALOGV("%s: id %s, facing %d, effectiveApiLevel %d, isNdk %d, latencyMs %d",
237 __FUNCTION__, id.c_str(), facing, effectiveApiLevel, isNdk, latencyMs);
238 sessionStats->onOpen();
239}
240
241void CameraServiceProxyWrapper::logClose(const String8& id, 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 should contain camera %s before it's closed",
247 __FUNCTION__, id.c_str());
248 return;
249 }
250
251 sessionStats = mSessionStatsMap[id];
252 if (sessionStats == nullptr) {
253 ALOGE("%s: SessionStatsMap should contain camera %s",
254 __FUNCTION__, id.c_str());
255 return;
256 }
257 mSessionStatsMap.erase(id);
258 ALOGV("%s: Erasing id %s", __FUNCTION__, id.c_str());
259 }
260
261 ALOGV("%s: id %s, latencyMs %d", __FUNCTION__, id.c_str(), latencyMs);
262 sessionStats->onClose(latencyMs);
263}
264
Austin Borger5f7abe22022-04-26 15:55:10 -0700265bool CameraServiceProxyWrapper::isCameraDisabled() {
266 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
267 if (proxyBinder == nullptr) return true;
268 bool ret = false;
269 auto status = proxyBinder->isCameraDisabled(&ret);
270 if (!status.isOk()) {
271 ALOGE("%s: Failed during camera disabled query: %s", __FUNCTION__,
272 status.exceptionMessage().c_str());
273 }
274 return ret;
275}
276
Shuzhen Wang316781a2020-08-18 18:11:01 -0700277}; // namespace android