1 /*
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
27 namespace android {
28
29 using hardware::ICameraServiceProxy;
30 using hardware::CameraSessionStats;
31
32 Mutex CameraServiceProxyWrapper::sProxyMutex;
33 sp<hardware::ICameraServiceProxy> CameraServiceProxyWrapper::sCameraServiceProxy;
34
35 Mutex CameraServiceProxyWrapper::mLock;
36 std::map<String8, std::shared_ptr<CameraServiceProxyWrapper::CameraSessionStatsWrapper>>
37 CameraServiceProxyWrapper::mSessionStatsMap;
38
39 /**
40 * CameraSessionStatsWrapper functions
41 */
42
onOpen()43 void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onOpen() {
44 Mutex::Autolock l(mLock);
45
46 updateProxyDeviceState(mSessionStats);
47 }
48
onClose(int32_t latencyMs)49 void 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
onStreamConfigured(int operatingMode,bool internalReconfig,int32_t latencyMs)57 void 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
onActive(float maxPreviewFps)69 void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onActive(float maxPreviewFps) {
70 Mutex::Autolock l(mLock);
71
72 mSessionStats.mNewCameraState = CameraSessionStats::CAMERA_STATE_ACTIVE;
73 mSessionStats.mMaxPreviewFps = maxPreviewFps;
74 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
onIdle(int64_t requestCount,int64_t resultErrorCount,bool deviceError,const std::string & userTag,int32_t videoStabilizationMode,const std::vector<hardware::CameraStreamStats> & streamStats)81 void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onIdle(
82 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
83 const std::string& userTag, int32_t videoStabilizationMode,
84 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;
91 mSessionStats.mUserTag = String16(userTag.c_str());
92 mSessionStats.mVideoStabilizationMode = videoStabilizationMode;
93 mSessionStats.mStreamStats = streamStats;
94 updateProxyDeviceState(mSessionStats);
95
96 mSessionStats.mInternalReconfigure = 0;
97 mSessionStats.mStreamStats.clear();
98 }
99
100 /**
101 * CameraServiceProxyWrapper functions
102 */
103
getCameraServiceProxy()104 sp<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
pingCameraServiceProxy()121 void CameraServiceProxyWrapper::pingCameraServiceProxy() {
122 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
123 if (proxyBinder == nullptr) return;
124 proxyBinder->pingForUserUpdate();
125 }
126
getRotateAndCropOverride(String16 packageName,int lensFacing,int userId)127 int CameraServiceProxyWrapper::getRotateAndCropOverride(String16 packageName, int lensFacing,
128 int userId) {
129 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
130 if (proxyBinder == nullptr) return true;
131 int ret = 0;
132 auto status = proxyBinder->getRotateAndCropOverride(packageName, lensFacing, userId, &ret);
133 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
updateProxyDeviceState(const CameraSessionStats & sessionStats)141 void CameraServiceProxyWrapper::updateProxyDeviceState(const CameraSessionStats& sessionStats) {
142 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
143 if (proxyBinder == nullptr) return;
144 proxyBinder->notifyCameraState(sessionStats);
145 }
146
logStreamConfigured(const String8 & id,int operatingMode,bool internalConfig,int32_t latencyMs)147 void 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
logActive(const String8 & id,float maxPreviewFps)165 void CameraServiceProxyWrapper::logActive(const String8& id, float maxPreviewFps) {
166 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());
178 sessionStats->onActive(maxPreviewFps);
179 }
180
logIdle(const String8 & id,int64_t requestCount,int64_t resultErrorCount,bool deviceError,const std::string & userTag,int32_t videoStabilizationMode,const std::vector<hardware::CameraStreamStats> & streamStats)181 void CameraServiceProxyWrapper::logIdle(const String8& id,
182 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
183 const std::string& userTag, int32_t videoStabilizationMode,
184 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
197 ALOGV("%s: id %s, requestCount %" PRId64 ", resultErrorCount %" PRId64 ", deviceError %d"
198 ", userTag %s, videoStabilizationMode %d", __FUNCTION__, id.c_str(), requestCount,
199 resultErrorCount, deviceError, userTag.c_str(), videoStabilizationMode);
200 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
208 sessionStats->onIdle(requestCount, resultErrorCount, deviceError, userTag,
209 videoStabilizationMode, streamStats);
210 }
211
logOpen(const String8 & id,int facing,const String16 & clientPackageName,int effectiveApiLevel,bool isNdk,int32_t latencyMs)212 void 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
logClose(const String8 & id,int32_t latencyMs)241 void 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
isCameraDisabled(int userId)265 bool CameraServiceProxyWrapper::isCameraDisabled(int userId) {
266 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
267 if (proxyBinder == nullptr) return true;
268 bool ret = false;
269 auto status = proxyBinder->isCameraDisabled(userId, &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
277 }; // namespace android
278