blob: 9aa0b4e3032b09624768e407bee17bf7f8a6a5a5 [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>
23
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080024#include <android/hardware/ICameraService.h>
25
Igor Murashkinc073ba52013-02-26 14:32:34 -080026#include <binder/IPCThreadState.h>
27#include <binder/IServiceManager.h>
28#include <binder/IMemory.h>
29
30#include <camera/CameraBase.h>
Igor Murashkinc073ba52013-02-26 14:32:34 -080031
32// needed to instantiate
Igor Murashkinc073ba52013-02-26 14:32:34 -080033#include <camera/Camera.h>
34
35#include <system/camera_metadata.h>
36
37namespace android {
38
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080039namespace hardware {
40
41status_t CameraInfo::writeToParcel(Parcel* parcel) const {
42 status_t res;
43 res = parcel->writeInt32(facing);
44 if (res != OK) return res;
45 res = parcel->writeInt32(orientation);
46 return res;
47}
48
49status_t CameraInfo::readFromParcel(const Parcel* parcel) {
50 status_t res;
51 res = parcel->readInt32(&facing);
52 if (res != OK) return res;
53 res = parcel->readInt32(&orientation);
54 return res;
55}
56
57}
58
Igor Murashkinc073ba52013-02-26 14:32:34 -080059namespace {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080060 sp<::android::hardware::ICameraService> gCameraService;
Igor Murashkinc073ba52013-02-26 14:32:34 -080061 const int kCameraServicePollDelay = 500000; // 0.5s
62 const char* kCameraServiceName = "media.camera";
63
64 Mutex gLock;
65
66 class DeathNotifier : public IBinder::DeathRecipient
67 {
68 public:
69 DeathNotifier() {
70 }
71
Mark Salyzyn7b73e712014-06-09 16:28:21 -070072 virtual void binderDied(const wp<IBinder>& /*who*/) {
Igor Murashkinc073ba52013-02-26 14:32:34 -080073 ALOGV("binderDied");
74 Mutex::Autolock _l(gLock);
75 gCameraService.clear();
76 ALOGW("Camera service died!");
77 }
78 };
79
80 sp<DeathNotifier> gDeathNotifier;
81}; // namespace anonymous
82
83///////////////////////////////////////////////////////////
84// CameraBase definition
85///////////////////////////////////////////////////////////
86
87// establish binder interface to camera service
88template <typename TCam, typename TCamTraits>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080089const sp<::android::hardware::ICameraService>& CameraBase<TCam, TCamTraits>::getCameraService()
Igor Murashkinc073ba52013-02-26 14:32:34 -080090{
91 Mutex::Autolock _l(gLock);
92 if (gCameraService.get() == 0) {
93 sp<IServiceManager> sm = defaultServiceManager();
94 sp<IBinder> binder;
95 do {
96 binder = sm->getService(String16(kCameraServiceName));
97 if (binder != 0) {
98 break;
99 }
100 ALOGW("CameraService not published, waiting...");
101 usleep(kCameraServicePollDelay);
102 } while(true);
103 if (gDeathNotifier == NULL) {
104 gDeathNotifier = new DeathNotifier();
105 }
106 binder->linkToDeath(gDeathNotifier);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800107 gCameraService = interface_cast<::android::hardware::ICameraService>(binder);
Igor Murashkinc073ba52013-02-26 14:32:34 -0800108 }
109 ALOGE_IF(gCameraService == 0, "no CameraService!?");
110 return gCameraService;
111}
112
113template <typename TCam, typename TCamTraits>
114sp<TCam> CameraBase<TCam, TCamTraits>::connect(int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000115 const String16& clientPackageName,
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800116 int clientUid, int clientPid)
Igor Murashkinc073ba52013-02-26 14:32:34 -0800117{
118 ALOGV("%s: connect", __FUNCTION__);
119 sp<TCam> c = new TCam(cameraId);
120 sp<TCamCallbacks> cl = c;
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700121 status_t status = NO_ERROR;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800122 const sp<::android::hardware::ICameraService>& cs = getCameraService();
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700123
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800124 binder::Status ret;
125 if (cs != nullptr) {
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700126 TCamConnectService fnConnectService = TCamTraits::fnConnectService;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800127 ret = (cs.get()->*fnConnectService)(cl, cameraId, clientPackageName, clientUid,
128 clientPid, /*out*/ &c->mCamera);
Igor Murashkinc073ba52013-02-26 14:32:34 -0800129 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800130 if (ret.isOk() && c->mCamera != nullptr) {
Marco Nelissen06b46062014-11-14 07:58:25 -0800131 IInterface::asBinder(c->mCamera)->linkToDeath(c);
Igor Murashkinc073ba52013-02-26 14:32:34 -0800132 c->mStatus = NO_ERROR;
133 } else {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800134 ALOGW("An error occurred while connecting to camera %d: %s", cameraId,
135 (cs != nullptr) ? "Service not available" : ret.toString8().string());
Igor Murashkinc073ba52013-02-26 14:32:34 -0800136 c.clear();
137 }
138 return c;
139}
140
141template <typename TCam, typename TCamTraits>
142void CameraBase<TCam, TCamTraits>::disconnect()
143{
144 ALOGV("%s: disconnect", __FUNCTION__);
145 if (mCamera != 0) {
146 mCamera->disconnect();
Marco Nelissen06b46062014-11-14 07:58:25 -0800147 IInterface::asBinder(mCamera)->unlinkToDeath(this);
Igor Murashkinc073ba52013-02-26 14:32:34 -0800148 mCamera = 0;
149 }
150 ALOGV("%s: disconnect (done)", __FUNCTION__);
151}
152
153template <typename TCam, typename TCamTraits>
154CameraBase<TCam, TCamTraits>::CameraBase(int cameraId) :
155 mStatus(UNKNOWN_ERROR),
156 mCameraId(cameraId)
157{
158}
159
160template <typename TCam, typename TCamTraits>
161CameraBase<TCam, TCamTraits>::~CameraBase()
162{
163}
164
165template <typename TCam, typename TCamTraits>
166sp<typename TCamTraits::TCamUser> CameraBase<TCam, TCamTraits>::remote()
167{
168 return mCamera;
169}
170
171template <typename TCam, typename TCamTraits>
172status_t CameraBase<TCam, TCamTraits>::getStatus()
173{
174 return mStatus;
175}
176
177template <typename TCam, typename TCamTraits>
Mark Salyzyn7b73e712014-06-09 16:28:21 -0700178void CameraBase<TCam, TCamTraits>::binderDied(const wp<IBinder>& /*who*/) {
Igor Murashkinc073ba52013-02-26 14:32:34 -0800179 ALOGW("mediaserver's remote binder Camera object died");
180 notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_SERVER_DIED, /*ext2*/0);
181}
182
183template <typename TCam, typename TCamTraits>
184void CameraBase<TCam, TCamTraits>::setListener(const sp<TCamListener>& listener)
185{
186 Mutex::Autolock _l(mLock);
187 mListener = listener;
188}
189
190// callback from camera service
191template <typename TCam, typename TCamTraits>
192void CameraBase<TCam, TCamTraits>::notifyCallback(int32_t msgType,
193 int32_t ext1,
194 int32_t ext2)
195{
196 sp<TCamListener> listener;
197 {
198 Mutex::Autolock _l(mLock);
199 listener = mListener;
200 }
201 if (listener != NULL) {
202 listener->notify(msgType, ext1, ext2);
203 }
204}
205
Igor Murashkinc073ba52013-02-26 14:32:34 -0800206template <typename TCam, typename TCamTraits>
207int CameraBase<TCam, TCamTraits>::getNumberOfCameras() {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800208 const sp<::android::hardware::ICameraService> cs = getCameraService();
Igor Murashkinc073ba52013-02-26 14:32:34 -0800209
210 if (!cs.get()) {
211 // as required by the public Java APIs
212 return 0;
213 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800214 int32_t count;
215 binder::Status res = cs->getNumberOfCameras(
216 ::android::hardware::ICameraService::CAMERA_TYPE_BACKWARD_COMPATIBLE,
217 &count);
218 if (!res.isOk()) {
219 ALOGE("Error reading number of cameras: %s",
220 res.toString8().string());
221 count = 0;
222 }
223 return count;
Igor Murashkinc073ba52013-02-26 14:32:34 -0800224}
225
226// this can be in BaseCamera but it should be an instance method
227template <typename TCam, typename TCamTraits>
228status_t CameraBase<TCam, TCamTraits>::getCameraInfo(int cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800229 struct hardware::CameraInfo* cameraInfo) {
230 const sp<::android::hardware::ICameraService>& cs = getCameraService();
Igor Murashkinc073ba52013-02-26 14:32:34 -0800231 if (cs == 0) return UNKNOWN_ERROR;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800232 binder::Status res = cs->getCameraInfo(cameraId, cameraInfo);
233 return res.isOk() ? OK : res.serviceSpecificErrorCode();
Igor Murashkinc073ba52013-02-26 14:32:34 -0800234}
235
Igor Murashkinbfc99152013-02-27 12:55:20 -0800236template <typename TCam, typename TCamTraits>
237status_t CameraBase<TCam, TCamTraits>::addServiceListener(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800238 const sp<::android::hardware::ICameraServiceListener>& listener) {
239 const sp<::android::hardware::ICameraService>& cs = getCameraService();
Igor Murashkinbfc99152013-02-27 12:55:20 -0800240 if (cs == 0) return UNKNOWN_ERROR;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800241 binder::Status res = cs->addListener(listener);
242 return res.isOk() ? OK : res.serviceSpecificErrorCode();
Igor Murashkinbfc99152013-02-27 12:55:20 -0800243}
244
245template <typename TCam, typename TCamTraits>
246status_t CameraBase<TCam, TCamTraits>::removeServiceListener(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800247 const sp<::android::hardware::ICameraServiceListener>& listener) {
248 const sp<::android::hardware::ICameraService>& cs = getCameraService();
Igor Murashkinbfc99152013-02-27 12:55:20 -0800249 if (cs == 0) return UNKNOWN_ERROR;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800250 binder::Status res = cs->removeListener(listener);
251 return res.isOk() ? OK : res.serviceSpecificErrorCode();
Igor Murashkinbfc99152013-02-27 12:55:20 -0800252}
253
Igor Murashkinc073ba52013-02-26 14:32:34 -0800254template class CameraBase<Camera>;
255
256} // namespace android