1 /*
2 **
3 ** Copyright (C) 2013, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18 //#define LOG_NDEBUG 0
19 #define LOG_TAG "CameraBase"
20 #include <utils/Log.h>
21 #include <utils/threads.h>
22 #include <utils/Mutex.h>
23 #include <cutils/properties.h>
24
25 #include <android/hardware/ICameraService.h>
26 #include <com/android/internal/compat/IPlatformCompatNative.h>
27
28 #include <binder/IPCThreadState.h>
29 #include <binder/IServiceManager.h>
30 #include <binder/IMemory.h>
31
32 #include <camera/CameraBase.h>
33 #include <camera/CameraUtils.h>
34 #include <camera/StringUtils.h>
35
36 // needed to instantiate
37 #include <camera/Camera.h>
38
39 #include <system/camera_metadata.h>
40
41 namespace android {
42
43 namespace hardware {
44
writeToParcel(android::Parcel * parcel) const45 status_t CameraInfo::writeToParcel(android::Parcel* parcel) const {
46 status_t res;
47 res = parcel->writeInt32(facing);
48 if (res != OK) return res;
49 res = parcel->writeInt32(orientation);
50 return res;
51 }
52
readFromParcel(const android::Parcel * parcel)53 status_t CameraInfo::readFromParcel(const android::Parcel* parcel) {
54 status_t res;
55 res = parcel->readInt32(&facing);
56 if (res != OK) return res;
57 res = parcel->readInt32(&orientation);
58 return res;
59 }
60
writeToParcel(android::Parcel * parcel) const61 status_t CameraStatus::writeToParcel(android::Parcel* parcel) const {
62 auto res = parcel->writeString16(toString16(cameraId));
63 if (res != OK) return res;
64
65 res = parcel->writeInt32(status);
66 if (res != OK) return res;
67
68 std::vector<String16> unavailablePhysicalIds16;
69 for (auto& id8 : unavailablePhysicalIds) {
70 unavailablePhysicalIds16.push_back(toString16(id8));
71 }
72 res = parcel->writeString16Vector(unavailablePhysicalIds16);
73 if (res != OK) return res;
74
75 res = parcel->writeString16(toString16(clientPackage));
76 if (res != OK) return res;
77
78 res = parcel->writeInt32(deviceId);
79 return res;
80 }
81
readFromParcel(const android::Parcel * parcel)82 status_t CameraStatus::readFromParcel(const android::Parcel* parcel) {
83 String16 tempCameraId;
84 auto res = parcel->readString16(&tempCameraId);
85 if (res != OK) return res;
86 cameraId = toString8(tempCameraId);
87
88 res = parcel->readInt32(&status);
89 if (res != OK) return res;
90
91 std::vector<String16> unavailablePhysicalIds16;
92 res = parcel->readString16Vector(&unavailablePhysicalIds16);
93 if (res != OK) return res;
94 for (auto& id16 : unavailablePhysicalIds16) {
95 unavailablePhysicalIds.push_back(toStdString(id16));
96 }
97
98 String16 tempClientPackage;
99 res = parcel->readString16(&tempClientPackage);
100 if (res != OK) return res;
101 clientPackage = toStdString(tempClientPackage);
102
103 res = parcel->readInt32(&deviceId);
104 return res;
105 }
106
107 } // namespace hardware
108
109 namespace {
110 sp<::android::hardware::ICameraService> gCameraService;
111 const char* kCameraServiceName = "media.camera";
112
113 Mutex gLock;
114
115 class DeathNotifier : public IBinder::DeathRecipient
116 {
117 public:
DeathNotifier()118 DeathNotifier() {
119 }
120
binderDied(const wp<IBinder> &)121 virtual void binderDied(const wp<IBinder>& /*who*/) {
122 ALOGV("binderDied");
123 Mutex::Autolock _l(gLock);
124 gCameraService.clear();
125 ALOGW("Camera service died!");
126 }
127 };
128
129 sp<DeathNotifier> gDeathNotifier;
130 } // namespace anonymous
131
132 ///////////////////////////////////////////////////////////
133 // CameraBase definition
134 ///////////////////////////////////////////////////////////
135
136 // establish binder interface to camera service
137 template <typename TCam, typename TCamTraits>
getCameraService()138 const sp<::android::hardware::ICameraService> CameraBase<TCam, TCamTraits>::getCameraService()
139 {
140 Mutex::Autolock _l(gLock);
141 if (gCameraService.get() == 0) {
142 if (CameraUtils::isCameraServiceDisabled()) {
143 return gCameraService;
144 }
145
146 sp<IServiceManager> sm = defaultServiceManager();
147 sp<IBinder> binder;
148 binder = sm->waitForService(toString16(kCameraServiceName));
149 if (binder == nullptr) {
150 return nullptr;
151 }
152 if (gDeathNotifier == NULL) {
153 gDeathNotifier = new DeathNotifier();
154 }
155 binder->linkToDeath(gDeathNotifier);
156 gCameraService = interface_cast<::android::hardware::ICameraService>(binder);
157 }
158 ALOGE_IF(gCameraService == 0, "no CameraService!?");
159 return gCameraService;
160 }
161
162 template <typename TCam, typename TCamTraits>
connect(int cameraId,const std::string & clientPackageName,int clientUid,int clientPid,int targetSdkVersion,int rotationOverride,bool forceSlowJpegMode,int32_t deviceId,int32_t devicePolicy)163 sp<TCam> CameraBase<TCam, TCamTraits>::connect(int cameraId,
164 const std::string& clientPackageName,
165 int clientUid, int clientPid, int targetSdkVersion,
166 int rotationOverride, bool forceSlowJpegMode,
167 int32_t deviceId, int32_t devicePolicy)
168 {
169 ALOGV("%s: connect", __FUNCTION__);
170 sp<TCam> c = new TCam(cameraId);
171 sp<TCamCallbacks> cl = c;
172 const sp<::android::hardware::ICameraService> cs = getCameraService();
173
174 binder::Status ret;
175 if (cs != nullptr) {
176 TCamConnectService fnConnectService = TCamTraits::fnConnectService;
177 ALOGI("Connect camera (legacy API) - rotationOverride %d, forceSlowJpegMode %d",
178 rotationOverride, forceSlowJpegMode);
179 ret = (cs.get()->*fnConnectService)(cl, cameraId, clientPackageName, clientUid,
180 clientPid, targetSdkVersion, rotationOverride, forceSlowJpegMode, deviceId,
181 devicePolicy, /*out*/ &c->mCamera);
182 }
183 if (ret.isOk() && c->mCamera != nullptr) {
184 IInterface::asBinder(c->mCamera)->linkToDeath(c);
185 c->mStatus = NO_ERROR;
186 } else {
187 ALOGW("An error occurred while connecting to camera %d: %s", cameraId,
188 (cs == nullptr) ? "Service not available" : ret.toString8().c_str());
189 c.clear();
190 }
191 return c;
192 }
193
194 template <typename TCam, typename TCamTraits>
disconnect()195 void CameraBase<TCam, TCamTraits>::disconnect()
196 {
197 ALOGV("%s: disconnect", __FUNCTION__);
198 if (mCamera != 0) {
199 mCamera->disconnect();
200 IInterface::asBinder(mCamera)->unlinkToDeath(this);
201 mCamera = 0;
202 }
203 ALOGV("%s: disconnect (done)", __FUNCTION__);
204 }
205
206 template <typename TCam, typename TCamTraits>
CameraBase(int cameraId)207 CameraBase<TCam, TCamTraits>::CameraBase(int cameraId) :
208 mStatus(UNKNOWN_ERROR),
209 mCameraId(cameraId)
210 {
211 }
212
213 template <typename TCam, typename TCamTraits>
~CameraBase()214 CameraBase<TCam, TCamTraits>::~CameraBase()
215 {
216 }
217
218 template <typename TCam, typename TCamTraits>
remote()219 sp<typename TCamTraits::TCamUser> CameraBase<TCam, TCamTraits>::remote()
220 {
221 return mCamera;
222 }
223
224 template <typename TCam, typename TCamTraits>
getStatus()225 status_t CameraBase<TCam, TCamTraits>::getStatus()
226 {
227 return mStatus;
228 }
229
230 template <typename TCam, typename TCamTraits>
binderDied(const wp<IBinder> &)231 void CameraBase<TCam, TCamTraits>::binderDied(const wp<IBinder>& /*who*/) {
232 ALOGW("mediaserver's remote binder Camera object died");
233 notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_SERVER_DIED, /*ext2*/0);
234 }
235
236 template <typename TCam, typename TCamTraits>
setListener(const sp<TCamListener> & listener)237 void CameraBase<TCam, TCamTraits>::setListener(const sp<TCamListener>& listener)
238 {
239 Mutex::Autolock _l(mLock);
240 mListener = listener;
241 }
242
243 // callback from camera service
244 template <typename TCam, typename TCamTraits>
notifyCallback(int32_t msgType,int32_t ext1,int32_t ext2)245 void CameraBase<TCam, TCamTraits>::notifyCallback(int32_t msgType,
246 int32_t ext1,
247 int32_t ext2)
248 {
249 sp<TCamListener> listener;
250 {
251 Mutex::Autolock _l(mLock);
252 listener = mListener;
253 }
254 if (listener != NULL) {
255 listener->notify(msgType, ext1, ext2);
256 }
257 }
258
259 template <typename TCam, typename TCamTraits>
getNumberOfCameras(int32_t deviceId,int32_t devicePolicy)260 int CameraBase<TCam, TCamTraits>::getNumberOfCameras(int32_t deviceId, int32_t devicePolicy) {
261 const sp<::android::hardware::ICameraService> cs = getCameraService();
262
263 if (!cs.get()) {
264 // as required by the public Java APIs
265 return 0;
266 }
267 int32_t count;
268 binder::Status res = cs->getNumberOfCameras(
269 ::android::hardware::ICameraService::CAMERA_TYPE_BACKWARD_COMPATIBLE, deviceId,
270 devicePolicy, &count);
271 if (!res.isOk()) {
272 ALOGE("Error reading number of cameras: %s",
273 res.toString8().c_str());
274 count = 0;
275 }
276 return count;
277 }
278
279 // this can be in BaseCamera but it should be an instance method
280 template <typename TCam, typename TCamTraits>
getCameraInfo(int cameraId,int rotationOverride,int32_t deviceId,int32_t devicePolicy,struct hardware::CameraInfo * cameraInfo)281 status_t CameraBase<TCam, TCamTraits>::getCameraInfo(int cameraId,
282 int rotationOverride, int32_t deviceId, int32_t devicePolicy,
283 struct hardware::CameraInfo* cameraInfo) {
284 const sp<::android::hardware::ICameraService> cs = getCameraService();
285 if (cs == 0) return UNKNOWN_ERROR;
286 binder::Status res = cs->getCameraInfo(cameraId, rotationOverride, deviceId, devicePolicy,
287 cameraInfo);
288 return res.isOk() ? OK : res.serviceSpecificErrorCode();
289 }
290
291 template class CameraBase<Camera>;
292
293 } // namespace android
294