blob: 8d170f137f01bf736958bdaf0eb196c96a493e22 [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
69void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onActive() {
70 Mutex::Autolock l(mLock);
71
72 mSessionStats.mNewCameraState = CameraSessionStats::CAMERA_STATE_ACTIVE;
73 updateProxyDeviceState(mSessionStats);
74
75 // Reset mCreationDuration to -1 to distinguish between 1st session
76 // after configuration, and all other sessions after configuration.
77 mSessionStats.mLatencyMs = -1;
78}
79
80void CameraServiceProxyWrapper::CameraSessionStatsWrapper::onIdle(
81 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
82 const std::vector<hardware::CameraStreamStats>& streamStats) {
83 Mutex::Autolock l(mLock);
84
85 mSessionStats.mNewCameraState = CameraSessionStats::CAMERA_STATE_IDLE;
86 mSessionStats.mRequestCount = requestCount;
87 mSessionStats.mResultErrorCount = resultErrorCount;
88 mSessionStats.mDeviceError = deviceError;
89 mSessionStats.mStreamStats = streamStats;
90 updateProxyDeviceState(mSessionStats);
91
92 mSessionStats.mInternalReconfigure = 0;
93 mSessionStats.mStreamStats.clear();
94}
95
96/**
97 * CameraServiceProxyWrapper functions
98 */
99
100sp<ICameraServiceProxy> CameraServiceProxyWrapper::getCameraServiceProxy() {
101#ifndef __BRILLO__
102 Mutex::Autolock al(sProxyMutex);
103 if (sCameraServiceProxy == nullptr) {
104 sp<IServiceManager> sm = defaultServiceManager();
105 // Use checkService because cameraserver normally starts before the
106 // system server and the proxy service. So the long timeout that getService
107 // has before giving up is inappropriate.
108 sp<IBinder> binder = sm->checkService(String16("media.camera.proxy"));
109 if (binder != nullptr) {
110 sCameraServiceProxy = interface_cast<ICameraServiceProxy>(binder);
111 }
112 }
113#endif
114 return sCameraServiceProxy;
115}
116
117void CameraServiceProxyWrapper::pingCameraServiceProxy() {
118 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
119 if (proxyBinder == nullptr) return;
120 proxyBinder->pingForUserUpdate();
121}
122
Emilian Peev5368ebf2021-10-08 17:52:18 -0700123int CameraServiceProxyWrapper::getRotateAndCropOverride(String16 packageName, int lensFacing) {
Emilian Peevb91f1802021-03-23 14:50:28 -0700124 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
125 if (proxyBinder == nullptr) return true;
Emilian Peev5368ebf2021-10-08 17:52:18 -0700126 int ret = 0;
127 auto status = proxyBinder->getRotateAndCropOverride(packageName, lensFacing, &ret);
Emilian Peevb91f1802021-03-23 14:50:28 -0700128 if (!status.isOk()) {
129 ALOGE("%s: Failed during top activity orientation query: %s", __FUNCTION__,
130 status.exceptionMessage().c_str());
131 }
132
133 return ret;
134}
135
Shuzhen Wang316781a2020-08-18 18:11:01 -0700136void CameraServiceProxyWrapper::updateProxyDeviceState(const CameraSessionStats& sessionStats) {
137 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
138 if (proxyBinder == nullptr) return;
139 proxyBinder->notifyCameraState(sessionStats);
140}
141
142void CameraServiceProxyWrapper::logStreamConfigured(const String8& id,
143 int operatingMode, bool internalConfig, int32_t latencyMs) {
144 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
145 {
146 Mutex::Autolock l(mLock);
147 sessionStats = mSessionStatsMap[id];
148 if (sessionStats == nullptr) {
149 ALOGE("%s: SessionStatsMap should contain camera %s",
150 __FUNCTION__, id.c_str());
151 return;
152 }
153 }
154
155 ALOGV("%s: id %s, operatingMode %d, internalConfig %d, latencyMs %d",
156 __FUNCTION__, id.c_str(), operatingMode, internalConfig, latencyMs);
157 sessionStats->onStreamConfigured(operatingMode, internalConfig, latencyMs);
158}
159
160void CameraServiceProxyWrapper::logActive(const String8& id) {
161 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
162 {
163 Mutex::Autolock l(mLock);
164 sessionStats = mSessionStatsMap[id];
165 if (sessionStats == nullptr) {
166 ALOGE("%s: SessionStatsMap should contain camera %s when logActive is called",
167 __FUNCTION__, id.c_str());
168 return;
169 }
170 }
171
172 ALOGV("%s: id %s", __FUNCTION__, id.c_str());
173 sessionStats->onActive();
174}
175
176void CameraServiceProxyWrapper::logIdle(const String8& id,
177 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
178 const std::vector<hardware::CameraStreamStats>& streamStats) {
179 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
180 {
181 Mutex::Autolock l(mLock);
182 sessionStats = mSessionStatsMap[id];
183 }
184
185 if (sessionStats == nullptr) {
186 ALOGE("%s: SessionStatsMap should contain camera %s when logIdle is called",
187 __FUNCTION__, id.c_str());
188 return;
189 }
190
191 ALOGV("%s: id %s, requestCount %" PRId64 ", resultErrorCount %" PRId64 ", deviceError %d",
192 __FUNCTION__, id.c_str(), requestCount, resultErrorCount, deviceError);
193 for (size_t i = 0; i < streamStats.size(); i++) {
194 ALOGV("%s: streamStats[%zu]: w %d h %d, requestedCount %" PRId64 ", dropCount %"
195 PRId64 ", startTimeMs %d" ,
196 __FUNCTION__, i, streamStats[i].mWidth, streamStats[i].mHeight,
197 streamStats[i].mRequestCount, streamStats[i].mErrorCount,
198 streamStats[i].mStartLatencyMs);
199 }
200
201 sessionStats->onIdle(requestCount, resultErrorCount, deviceError, streamStats);
202}
203
204void CameraServiceProxyWrapper::logOpen(const String8& id, int facing,
205 const String16& clientPackageName, int effectiveApiLevel, bool isNdk,
206 int32_t latencyMs) {
207 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
208 {
209 Mutex::Autolock l(mLock);
210 if (mSessionStatsMap.count(id) > 0) {
211 ALOGE("%s: SessionStatsMap shouldn't contain camera %s",
212 __FUNCTION__, id.c_str());
213 return;
214 }
215
216 int apiLevel = CameraSessionStats::CAMERA_API_LEVEL_1;
217 if (effectiveApiLevel == 2) {
218 apiLevel = CameraSessionStats::CAMERA_API_LEVEL_2;
219 }
220
221 sessionStats = std::make_shared<CameraSessionStatsWrapper>(String16(id), facing,
222 CameraSessionStats::CAMERA_STATE_OPEN, clientPackageName,
223 apiLevel, isNdk, latencyMs);
224 mSessionStatsMap.emplace(id, sessionStats);
225 ALOGV("%s: Adding id %s", __FUNCTION__, id.c_str());
226 }
227
228 ALOGV("%s: id %s, facing %d, effectiveApiLevel %d, isNdk %d, latencyMs %d",
229 __FUNCTION__, id.c_str(), facing, effectiveApiLevel, isNdk, latencyMs);
230 sessionStats->onOpen();
231}
232
233void CameraServiceProxyWrapper::logClose(const String8& id, int32_t latencyMs) {
234 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
235 {
236 Mutex::Autolock l(mLock);
237 if (mSessionStatsMap.count(id) == 0) {
238 ALOGE("%s: SessionStatsMap should contain camera %s before it's closed",
239 __FUNCTION__, id.c_str());
240 return;
241 }
242
243 sessionStats = mSessionStatsMap[id];
244 if (sessionStats == nullptr) {
245 ALOGE("%s: SessionStatsMap should contain camera %s",
246 __FUNCTION__, id.c_str());
247 return;
248 }
249 mSessionStatsMap.erase(id);
250 ALOGV("%s: Erasing id %s", __FUNCTION__, id.c_str());
251 }
252
253 ALOGV("%s: id %s, latencyMs %d", __FUNCTION__, id.c_str(), latencyMs);
254 sessionStats->onClose(latencyMs);
255}
256
257}; // namespace android