• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()69 void 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 
onIdle(int64_t requestCount,int64_t resultErrorCount,bool deviceError,const std::vector<hardware::CameraStreamStats> & streamStats)80 void 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 
getCameraServiceProxy()100 sp<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 
pingCameraServiceProxy()117 void CameraServiceProxyWrapper::pingCameraServiceProxy() {
118     sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
119     if (proxyBinder == nullptr) return;
120     proxyBinder->pingForUserUpdate();
121 }
122 
isRotateAndCropOverrideNeeded(String16 packageName,int sensorOrientation,int lensFacing)123 bool CameraServiceProxyWrapper::isRotateAndCropOverrideNeeded(
124         String16 packageName, int sensorOrientation, int lensFacing) {
125     sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
126     if (proxyBinder == nullptr) return true;
127     bool ret = true;
128     auto status = proxyBinder->isRotateAndCropOverrideNeeded(packageName, sensorOrientation,
129             lensFacing, &ret);
130     if (!status.isOk()) {
131         ALOGE("%s: Failed during top activity orientation query: %s", __FUNCTION__,
132                 status.exceptionMessage().c_str());
133     }
134 
135     return ret;
136 }
137 
updateProxyDeviceState(const CameraSessionStats & sessionStats)138 void CameraServiceProxyWrapper::updateProxyDeviceState(const CameraSessionStats& sessionStats) {
139     sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
140     if (proxyBinder == nullptr) return;
141     proxyBinder->notifyCameraState(sessionStats);
142 }
143 
logStreamConfigured(const String8 & id,int operatingMode,bool internalConfig,int32_t latencyMs)144 void CameraServiceProxyWrapper::logStreamConfigured(const String8& id,
145         int operatingMode, bool internalConfig, int32_t latencyMs) {
146     std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
147     {
148         Mutex::Autolock l(mLock);
149         sessionStats = mSessionStatsMap[id];
150         if (sessionStats == nullptr) {
151             ALOGE("%s: SessionStatsMap should contain camera %s",
152                     __FUNCTION__, id.c_str());
153             return;
154         }
155     }
156 
157     ALOGV("%s: id %s, operatingMode %d, internalConfig %d, latencyMs %d",
158             __FUNCTION__, id.c_str(), operatingMode, internalConfig, latencyMs);
159     sessionStats->onStreamConfigured(operatingMode, internalConfig, latencyMs);
160 }
161 
logActive(const String8 & id)162 void CameraServiceProxyWrapper::logActive(const String8& id) {
163     std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
164     {
165         Mutex::Autolock l(mLock);
166         sessionStats = mSessionStatsMap[id];
167         if (sessionStats == nullptr) {
168             ALOGE("%s: SessionStatsMap should contain camera %s when logActive is called",
169                     __FUNCTION__, id.c_str());
170             return;
171         }
172     }
173 
174     ALOGV("%s: id %s", __FUNCTION__, id.c_str());
175     sessionStats->onActive();
176 }
177 
logIdle(const String8 & id,int64_t requestCount,int64_t resultErrorCount,bool deviceError,const std::vector<hardware::CameraStreamStats> & streamStats)178 void CameraServiceProxyWrapper::logIdle(const String8& id,
179         int64_t requestCount, int64_t resultErrorCount, bool deviceError,
180         const std::vector<hardware::CameraStreamStats>& streamStats) {
181     std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
182     {
183         Mutex::Autolock l(mLock);
184         sessionStats = mSessionStatsMap[id];
185     }
186 
187     if (sessionStats == nullptr) {
188         ALOGE("%s: SessionStatsMap should contain camera %s when logIdle is called",
189                 __FUNCTION__, id.c_str());
190         return;
191     }
192 
193     ALOGV("%s: id %s, requestCount %" PRId64 ", resultErrorCount %" PRId64 ", deviceError %d",
194             __FUNCTION__, id.c_str(), requestCount, resultErrorCount, deviceError);
195     for (size_t i = 0; i < streamStats.size(); i++) {
196         ALOGV("%s: streamStats[%zu]: w %d h %d, requestedCount %" PRId64 ", dropCount %"
197                 PRId64 ", startTimeMs %d" ,
198                 __FUNCTION__, i, streamStats[i].mWidth, streamStats[i].mHeight,
199                 streamStats[i].mRequestCount, streamStats[i].mErrorCount,
200                 streamStats[i].mStartLatencyMs);
201     }
202 
203     sessionStats->onIdle(requestCount, resultErrorCount, deviceError, streamStats);
204 }
205 
logOpen(const String8 & id,int facing,const String16 & clientPackageName,int effectiveApiLevel,bool isNdk,int32_t latencyMs)206 void CameraServiceProxyWrapper::logOpen(const String8& id, int facing,
207             const String16& clientPackageName, int effectiveApiLevel, bool isNdk,
208             int32_t latencyMs) {
209     std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
210     {
211         Mutex::Autolock l(mLock);
212         if (mSessionStatsMap.count(id) > 0) {
213             ALOGE("%s: SessionStatsMap shouldn't contain camera %s",
214                     __FUNCTION__, id.c_str());
215             return;
216         }
217 
218         int apiLevel = CameraSessionStats::CAMERA_API_LEVEL_1;
219         if (effectiveApiLevel == 2) {
220             apiLevel = CameraSessionStats::CAMERA_API_LEVEL_2;
221         }
222 
223         sessionStats = std::make_shared<CameraSessionStatsWrapper>(String16(id), facing,
224                 CameraSessionStats::CAMERA_STATE_OPEN, clientPackageName,
225                 apiLevel, isNdk, latencyMs);
226         mSessionStatsMap.emplace(id, sessionStats);
227         ALOGV("%s: Adding id %s", __FUNCTION__, id.c_str());
228     }
229 
230     ALOGV("%s: id %s, facing %d, effectiveApiLevel %d, isNdk %d, latencyMs %d",
231             __FUNCTION__, id.c_str(), facing, effectiveApiLevel, isNdk, latencyMs);
232     sessionStats->onOpen();
233 }
234 
logClose(const String8 & id,int32_t latencyMs)235 void CameraServiceProxyWrapper::logClose(const String8& id, int32_t latencyMs) {
236     std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
237     {
238         Mutex::Autolock l(mLock);
239         if (mSessionStatsMap.count(id) == 0) {
240             ALOGE("%s: SessionStatsMap should contain camera %s before it's closed",
241                     __FUNCTION__, id.c_str());
242             return;
243         }
244 
245         sessionStats = mSessionStatsMap[id];
246         if (sessionStats == nullptr) {
247             ALOGE("%s: SessionStatsMap should contain camera %s",
248                     __FUNCTION__, id.c_str());
249             return;
250         }
251         mSessionStatsMap.erase(id);
252         ALOGV("%s: Erasing id %s", __FUNCTION__, id.c_str());
253     }
254 
255     ALOGV("%s: id %s, latencyMs %d", __FUNCTION__, id.c_str(), latencyMs);
256     sessionStats->onClose(latencyMs);
257 }
258 
259 }; // namespace android
260