blob: 0557fcc8c764db89f4d578a51fd80375bf80d563 [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
123void CameraServiceProxyWrapper::updateProxyDeviceState(const CameraSessionStats& sessionStats) {
124 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
125 if (proxyBinder == nullptr) return;
126 proxyBinder->notifyCameraState(sessionStats);
127}
128
129void CameraServiceProxyWrapper::logStreamConfigured(const String8& id,
130 int operatingMode, bool internalConfig, int32_t latencyMs) {
131 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
132 {
133 Mutex::Autolock l(mLock);
134 sessionStats = mSessionStatsMap[id];
135 if (sessionStats == nullptr) {
136 ALOGE("%s: SessionStatsMap should contain camera %s",
137 __FUNCTION__, id.c_str());
138 return;
139 }
140 }
141
142 ALOGV("%s: id %s, operatingMode %d, internalConfig %d, latencyMs %d",
143 __FUNCTION__, id.c_str(), operatingMode, internalConfig, latencyMs);
144 sessionStats->onStreamConfigured(operatingMode, internalConfig, latencyMs);
145}
146
147void CameraServiceProxyWrapper::logActive(const String8& id) {
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 when logActive is called",
154 __FUNCTION__, id.c_str());
155 return;
156 }
157 }
158
159 ALOGV("%s: id %s", __FUNCTION__, id.c_str());
160 sessionStats->onActive();
161}
162
163void CameraServiceProxyWrapper::logIdle(const String8& id,
164 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
165 const std::vector<hardware::CameraStreamStats>& streamStats) {
166 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
167 {
168 Mutex::Autolock l(mLock);
169 sessionStats = mSessionStatsMap[id];
170 }
171
172 if (sessionStats == nullptr) {
173 ALOGE("%s: SessionStatsMap should contain camera %s when logIdle is called",
174 __FUNCTION__, id.c_str());
175 return;
176 }
177
178 ALOGV("%s: id %s, requestCount %" PRId64 ", resultErrorCount %" PRId64 ", deviceError %d",
179 __FUNCTION__, id.c_str(), requestCount, resultErrorCount, deviceError);
180 for (size_t i = 0; i < streamStats.size(); i++) {
181 ALOGV("%s: streamStats[%zu]: w %d h %d, requestedCount %" PRId64 ", dropCount %"
182 PRId64 ", startTimeMs %d" ,
183 __FUNCTION__, i, streamStats[i].mWidth, streamStats[i].mHeight,
184 streamStats[i].mRequestCount, streamStats[i].mErrorCount,
185 streamStats[i].mStartLatencyMs);
186 }
187
188 sessionStats->onIdle(requestCount, resultErrorCount, deviceError, streamStats);
189}
190
191void CameraServiceProxyWrapper::logOpen(const String8& id, int facing,
192 const String16& clientPackageName, int effectiveApiLevel, bool isNdk,
193 int32_t latencyMs) {
194 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
195 {
196 Mutex::Autolock l(mLock);
197 if (mSessionStatsMap.count(id) > 0) {
198 ALOGE("%s: SessionStatsMap shouldn't contain camera %s",
199 __FUNCTION__, id.c_str());
200 return;
201 }
202
203 int apiLevel = CameraSessionStats::CAMERA_API_LEVEL_1;
204 if (effectiveApiLevel == 2) {
205 apiLevel = CameraSessionStats::CAMERA_API_LEVEL_2;
206 }
207
208 sessionStats = std::make_shared<CameraSessionStatsWrapper>(String16(id), facing,
209 CameraSessionStats::CAMERA_STATE_OPEN, clientPackageName,
210 apiLevel, isNdk, latencyMs);
211 mSessionStatsMap.emplace(id, sessionStats);
212 ALOGV("%s: Adding id %s", __FUNCTION__, id.c_str());
213 }
214
215 ALOGV("%s: id %s, facing %d, effectiveApiLevel %d, isNdk %d, latencyMs %d",
216 __FUNCTION__, id.c_str(), facing, effectiveApiLevel, isNdk, latencyMs);
217 sessionStats->onOpen();
218}
219
220void CameraServiceProxyWrapper::logClose(const String8& id, int32_t latencyMs) {
221 std::shared_ptr<CameraSessionStatsWrapper> sessionStats;
222 {
223 Mutex::Autolock l(mLock);
224 if (mSessionStatsMap.count(id) == 0) {
225 ALOGE("%s: SessionStatsMap should contain camera %s before it's closed",
226 __FUNCTION__, id.c_str());
227 return;
228 }
229
230 sessionStats = mSessionStatsMap[id];
231 if (sessionStats == nullptr) {
232 ALOGE("%s: SessionStatsMap should contain camera %s",
233 __FUNCTION__, id.c_str());
234 return;
235 }
236 mSessionStatsMap.erase(id);
237 ALOGV("%s: Erasing id %s", __FUNCTION__, id.c_str());
238 }
239
240 ALOGV("%s: id %s, latencyMs %d", __FUNCTION__, id.c_str(), latencyMs);
241 sessionStats->onClose(latencyMs);
242}
243
244}; // namespace android