blob: c53e6c3358e92ec5259cfbd8db5aa4dce673462a [file] [log] [blame]
Igor Murashkinc073ba52013-02-26 14:32:34 -08001/*
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>
Ivan Podogovee844a82016-09-15 11:32:41 +010023#include <cutils/properties.h>
Igor Murashkinc073ba52013-02-26 14:32:34 -080024
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080025#include <android/hardware/ICameraService.h>
26
Igor Murashkinc073ba52013-02-26 14:32:34 -080027#include <binder/IPCThreadState.h>
28#include <binder/IServiceManager.h>
29#include <binder/IMemory.h>
30
31#include <camera/CameraBase.h>
Igor Murashkinc073ba52013-02-26 14:32:34 -080032
33// needed to instantiate
Igor Murashkinc073ba52013-02-26 14:32:34 -080034#include <camera/Camera.h>
35
36#include <system/camera_metadata.h>
37
38namespace android {
39
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080040namespace hardware {
41
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080042status_t CameraInfo::writeToParcel(android::Parcel* parcel) const {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080043 status_t res;
44 res = parcel->writeInt32(facing);
45 if (res != OK) return res;
46 res = parcel->writeInt32(orientation);
47 return res;
48}
49
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080050status_t CameraInfo::readFromParcel(const android::Parcel* parcel) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080051 status_t res;
52 res = parcel->readInt32(&facing);
53 if (res != OK) return res;
54 res = parcel->readInt32(&orientation);
55 return res;
56}
57
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080058status_t CameraStatus::writeToParcel(android::Parcel* parcel) const {
Oleksiy Vyalovead84722017-03-24 14:06:03 -070059 auto res = parcel->writeString16(String16(cameraId));
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080060 if (res != OK) return res;
Oleksiy Vyalovead84722017-03-24 14:06:03 -070061
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080062 res = parcel->writeInt32(status);
63 return res;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080064}
65
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080066status_t CameraStatus::readFromParcel(const android::Parcel* parcel) {
Oleksiy Vyalovead84722017-03-24 14:06:03 -070067 String16 tempCameraId;
68 auto res = parcel->readString16(&tempCameraId);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080069 if (res != OK) return res;
Oleksiy Vyalovead84722017-03-24 14:06:03 -070070 cameraId = String8(tempCameraId);
71
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080072 res = parcel->readInt32(&status);
73 return res;
74}
75
76} // namespace hardware
77
Igor Murashkinc073ba52013-02-26 14:32:34 -080078namespace {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080079 sp<::android::hardware::ICameraService> gCameraService;
Igor Murashkinc073ba52013-02-26 14:32:34 -080080 const int kCameraServicePollDelay = 500000; // 0.5s
81 const char* kCameraServiceName = "media.camera";
82
83 Mutex gLock;
84
85 class DeathNotifier : public IBinder::DeathRecipient
86 {
87 public:
88 DeathNotifier() {
89 }
90
Mark Salyzyn7b73e712014-06-09 16:28:21 -070091 virtual void binderDied(const wp<IBinder>& /*who*/) {
Igor Murashkinc073ba52013-02-26 14:32:34 -080092 ALOGV("binderDied");
93 Mutex::Autolock _l(gLock);
94 gCameraService.clear();
95 ALOGW("Camera service died!");
96 }
97 };
98
99 sp<DeathNotifier> gDeathNotifier;
100}; // namespace anonymous
101
102///////////////////////////////////////////////////////////
103// CameraBase definition
104///////////////////////////////////////////////////////////
105
106// establish binder interface to camera service
107template <typename TCam, typename TCamTraits>
Eino-Ville Talvalaf86177d2017-02-01 15:27:41 -0800108const sp<::android::hardware::ICameraService> CameraBase<TCam, TCamTraits>::getCameraService()
Igor Murashkinc073ba52013-02-26 14:32:34 -0800109{
110 Mutex::Autolock _l(gLock);
111 if (gCameraService.get() == 0) {
Ivan Podogovee844a82016-09-15 11:32:41 +0100112 char value[PROPERTY_VALUE_MAX];
113 property_get("config.disable_cameraservice", value, "0");
114 if (strncmp(value, "0", 2) != 0 && strncasecmp(value, "false", 6) != 0) {
115 return gCameraService;
116 }
117
Igor Murashkinc073ba52013-02-26 14:32:34 -0800118 sp<IServiceManager> sm = defaultServiceManager();
119 sp<IBinder> binder;
120 do {
121 binder = sm->getService(String16(kCameraServiceName));
122 if (binder != 0) {
123 break;
124 }
125 ALOGW("CameraService not published, waiting...");
126 usleep(kCameraServicePollDelay);
127 } while(true);
128 if (gDeathNotifier == NULL) {
129 gDeathNotifier = new DeathNotifier();
130 }
131 binder->linkToDeath(gDeathNotifier);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800132 gCameraService = interface_cast<::android::hardware::ICameraService>(binder);
Igor Murashkinc073ba52013-02-26 14:32:34 -0800133 }
134 ALOGE_IF(gCameraService == 0, "no CameraService!?");
135 return gCameraService;
136}
137
138template <typename TCam, typename TCamTraits>
139sp<TCam> CameraBase<TCam, TCamTraits>::connect(int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000140 const String16& clientPackageName,
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800141 int clientUid, int clientPid)
Igor Murashkinc073ba52013-02-26 14:32:34 -0800142{
143 ALOGV("%s: connect", __FUNCTION__);
144 sp<TCam> c = new TCam(cameraId);
145 sp<TCamCallbacks> cl = c;
Eino-Ville Talvalaf86177d2017-02-01 15:27:41 -0800146 const sp<::android::hardware::ICameraService> cs = getCameraService();
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700147
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800148 binder::Status ret;
149 if (cs != nullptr) {
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700150 TCamConnectService fnConnectService = TCamTraits::fnConnectService;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800151 ret = (cs.get()->*fnConnectService)(cl, cameraId, clientPackageName, clientUid,
152 clientPid, /*out*/ &c->mCamera);
Igor Murashkinc073ba52013-02-26 14:32:34 -0800153 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800154 if (ret.isOk() && c->mCamera != nullptr) {
Marco Nelissen06b46062014-11-14 07:58:25 -0800155 IInterface::asBinder(c->mCamera)->linkToDeath(c);
Igor Murashkinc073ba52013-02-26 14:32:34 -0800156 c->mStatus = NO_ERROR;
157 } else {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800158 ALOGW("An error occurred while connecting to camera %d: %s", cameraId,
Tobias Lindskog0ccb13b2016-11-01 14:25:52 +0100159 (cs == nullptr) ? "Service not available" : ret.toString8().string());
Igor Murashkinc073ba52013-02-26 14:32:34 -0800160 c.clear();
161 }
162 return c;
163}
164
165template <typename TCam, typename TCamTraits>
166void CameraBase<TCam, TCamTraits>::disconnect()
167{
168 ALOGV("%s: disconnect", __FUNCTION__);
169 if (mCamera != 0) {
170 mCamera->disconnect();
Marco Nelissen06b46062014-11-14 07:58:25 -0800171 IInterface::asBinder(mCamera)->unlinkToDeath(this);
Igor Murashkinc073ba52013-02-26 14:32:34 -0800172 mCamera = 0;
173 }
174 ALOGV("%s: disconnect (done)", __FUNCTION__);
175}
176
177template <typename TCam, typename TCamTraits>
178CameraBase<TCam, TCamTraits>::CameraBase(int cameraId) :
179 mStatus(UNKNOWN_ERROR),
180 mCameraId(cameraId)
181{
182}
183
184template <typename TCam, typename TCamTraits>
185CameraBase<TCam, TCamTraits>::~CameraBase()
186{
187}
188
189template <typename TCam, typename TCamTraits>
190sp<typename TCamTraits::TCamUser> CameraBase<TCam, TCamTraits>::remote()
191{
192 return mCamera;
193}
194
195template <typename TCam, typename TCamTraits>
196status_t CameraBase<TCam, TCamTraits>::getStatus()
197{
198 return mStatus;
199}
200
201template <typename TCam, typename TCamTraits>
Mark Salyzyn7b73e712014-06-09 16:28:21 -0700202void CameraBase<TCam, TCamTraits>::binderDied(const wp<IBinder>& /*who*/) {
Igor Murashkinc073ba52013-02-26 14:32:34 -0800203 ALOGW("mediaserver's remote binder Camera object died");
204 notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_SERVER_DIED, /*ext2*/0);
205}
206
207template <typename TCam, typename TCamTraits>
208void CameraBase<TCam, TCamTraits>::setListener(const sp<TCamListener>& listener)
209{
210 Mutex::Autolock _l(mLock);
211 mListener = listener;
212}
213
214// callback from camera service
215template <typename TCam, typename TCamTraits>
216void CameraBase<TCam, TCamTraits>::notifyCallback(int32_t msgType,
217 int32_t ext1,
218 int32_t ext2)
219{
220 sp<TCamListener> listener;
221 {
222 Mutex::Autolock _l(mLock);
223 listener = mListener;
224 }
225 if (listener != NULL) {
226 listener->notify(msgType, ext1, ext2);
227 }
228}
229
Igor Murashkinc073ba52013-02-26 14:32:34 -0800230template <typename TCam, typename TCamTraits>
231int CameraBase<TCam, TCamTraits>::getNumberOfCameras() {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800232 const sp<::android::hardware::ICameraService> cs = getCameraService();
Igor Murashkinc073ba52013-02-26 14:32:34 -0800233
234 if (!cs.get()) {
235 // as required by the public Java APIs
236 return 0;
237 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800238 int32_t count;
239 binder::Status res = cs->getNumberOfCameras(
240 ::android::hardware::ICameraService::CAMERA_TYPE_BACKWARD_COMPATIBLE,
241 &count);
242 if (!res.isOk()) {
243 ALOGE("Error reading number of cameras: %s",
244 res.toString8().string());
245 count = 0;
246 }
247 return count;
Igor Murashkinc073ba52013-02-26 14:32:34 -0800248}
249
250// this can be in BaseCamera but it should be an instance method
251template <typename TCam, typename TCamTraits>
252status_t CameraBase<TCam, TCamTraits>::getCameraInfo(int cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800253 struct hardware::CameraInfo* cameraInfo) {
Eino-Ville Talvalaf86177d2017-02-01 15:27:41 -0800254 const sp<::android::hardware::ICameraService> cs = getCameraService();
Igor Murashkinc073ba52013-02-26 14:32:34 -0800255 if (cs == 0) return UNKNOWN_ERROR;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800256 binder::Status res = cs->getCameraInfo(cameraId, cameraInfo);
257 return res.isOk() ? OK : res.serviceSpecificErrorCode();
Igor Murashkinc073ba52013-02-26 14:32:34 -0800258}
259
Igor Murashkinc073ba52013-02-26 14:32:34 -0800260template class CameraBase<Camera>;
261
262} // namespace android