3 ** Copyright (C) 2013, The Android Open Source Project
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
9 ** http://www.apache.org/licenses/LICENSE-2.0
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.
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>
24 #include <android/hardware/ICameraService.h>
26 #include <binder/IPCThreadState.h>
27 #include <binder/IServiceManager.h>
28 #include <binder/IMemory.h>
30 #include <camera/CameraBase.h>
32 // needed to instantiate
33 #include <camera/Camera.h>
35 #include <system/camera_metadata.h>
41 status_t CameraInfo::writeToParcel(Parcel* parcel) const {
43 res = parcel->writeInt32(facing);
44 if (res != OK) return res;
45 res = parcel->writeInt32(orientation);
49 status_t CameraInfo::readFromParcel(const Parcel* parcel) {
51 res = parcel->readInt32(&facing);
52 if (res != OK) return res;
53 res = parcel->readInt32(&orientation);
60 sp<::android::hardware::ICameraService> gCameraService;
61 const int kCameraServicePollDelay = 500000; // 0.5s
62 const char* kCameraServiceName = "media.camera";
66 class DeathNotifier : public IBinder::DeathRecipient
72 virtual void binderDied(const wp<IBinder>& /*who*/) {
74 Mutex::Autolock _l(gLock);
75 gCameraService.clear();
76 ALOGW("Camera service died!");
80 sp<DeathNotifier> gDeathNotifier;
81 }; // namespace anonymous
83 ///////////////////////////////////////////////////////////
84 // CameraBase definition
85 ///////////////////////////////////////////////////////////
87 // establish binder interface to camera service
88 template <typename TCam, typename TCamTraits>
89 const sp<::android::hardware::ICameraService> CameraBase<TCam, TCamTraits>::getCameraService()
91 Mutex::Autolock _l(gLock);
92 if (gCameraService.get() == 0) {
93 sp<IServiceManager> sm = defaultServiceManager();
96 binder = sm->getService(String16(kCameraServiceName));
100 ALOGW("CameraService not published, waiting...");
101 usleep(kCameraServicePollDelay);
103 if (gDeathNotifier == NULL) {
104 gDeathNotifier = new DeathNotifier();
106 binder->linkToDeath(gDeathNotifier);
107 gCameraService = interface_cast<::android::hardware::ICameraService>(binder);
109 ALOGE_IF(gCameraService == 0, "no CameraService!?");
110 return gCameraService;
113 template <typename TCam, typename TCamTraits>
114 sp<TCam> CameraBase<TCam, TCamTraits>::connect(int cameraId,
115 const String16& clientPackageName,
116 int clientUid, int clientPid)
118 ALOGV("%s: connect", __FUNCTION__);
119 sp<TCam> c = new TCam(cameraId);
120 sp<TCamCallbacks> cl = c;
121 const sp<::android::hardware::ICameraService> cs = getCameraService();
125 TCamConnectService fnConnectService = TCamTraits::fnConnectService;
126 ret = (cs.get()->*fnConnectService)(cl, cameraId, clientPackageName, clientUid,
127 clientPid, /*out*/ &c->mCamera);
129 if (ret.isOk() && c->mCamera != nullptr) {
130 IInterface::asBinder(c->mCamera)->linkToDeath(c);
131 c->mStatus = NO_ERROR;
133 ALOGW("An error occurred while connecting to camera %d: %s", cameraId,
134 (cs != nullptr) ? "Service not available" : ret.toString8().string());
140 template <typename TCam, typename TCamTraits>
141 void CameraBase<TCam, TCamTraits>::disconnect()
143 ALOGV("%s: disconnect", __FUNCTION__);
145 mCamera->disconnect();
146 IInterface::asBinder(mCamera)->unlinkToDeath(this);
149 ALOGV("%s: disconnect (done)", __FUNCTION__);
152 template <typename TCam, typename TCamTraits>
153 CameraBase<TCam, TCamTraits>::CameraBase(int cameraId) :
154 mStatus(UNKNOWN_ERROR),
159 template <typename TCam, typename TCamTraits>
160 CameraBase<TCam, TCamTraits>::~CameraBase()
164 template <typename TCam, typename TCamTraits>
165 sp<typename TCamTraits::TCamUser> CameraBase<TCam, TCamTraits>::remote()
170 template <typename TCam, typename TCamTraits>
171 status_t CameraBase<TCam, TCamTraits>::getStatus()
176 template <typename TCam, typename TCamTraits>
177 void CameraBase<TCam, TCamTraits>::binderDied(const wp<IBinder>& /*who*/) {
178 ALOGW("mediaserver's remote binder Camera object died");
179 notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_SERVER_DIED, /*ext2*/0);
182 template <typename TCam, typename TCamTraits>
183 void CameraBase<TCam, TCamTraits>::setListener(const sp<TCamListener>& listener)
185 Mutex::Autolock _l(mLock);
186 mListener = listener;
189 // callback from camera service
190 template <typename TCam, typename TCamTraits>
191 void CameraBase<TCam, TCamTraits>::notifyCallback(int32_t msgType,
195 sp<TCamListener> listener;
197 Mutex::Autolock _l(mLock);
198 listener = mListener;
200 if (listener != NULL) {
201 listener->notify(msgType, ext1, ext2);
205 template <typename TCam, typename TCamTraits>
206 int CameraBase<TCam, TCamTraits>::getNumberOfCameras() {
207 const sp<::android::hardware::ICameraService> cs = getCameraService();
210 // as required by the public Java APIs
214 binder::Status res = cs->getNumberOfCameras(
215 ::android::hardware::ICameraService::CAMERA_TYPE_BACKWARD_COMPATIBLE,
218 ALOGE("Error reading number of cameras: %s",
219 res.toString8().string());
225 // this can be in BaseCamera but it should be an instance method
226 template <typename TCam, typename TCamTraits>
227 status_t CameraBase<TCam, TCamTraits>::getCameraInfo(int cameraId,
228 struct hardware::CameraInfo* cameraInfo) {
229 const sp<::android::hardware::ICameraService> cs = getCameraService();
230 if (cs == 0) return UNKNOWN_ERROR;
231 binder::Status res = cs->getCameraInfo(cameraId, cameraInfo);
232 return res.isOk() ? OK : res.serviceSpecificErrorCode();
235 template <typename TCam, typename TCamTraits>
236 status_t CameraBase<TCam, TCamTraits>::addServiceListener(
237 const sp<::android::hardware::ICameraServiceListener>& listener) {
238 const sp<::android::hardware::ICameraService>& cs = getCameraService();
239 if (cs == 0) return UNKNOWN_ERROR;
240 binder::Status res = cs->addListener(listener);
241 return res.isOk() ? OK : res.serviceSpecificErrorCode();
244 template <typename TCam, typename TCamTraits>
245 status_t CameraBase<TCam, TCamTraits>::removeServiceListener(
246 const sp<::android::hardware::ICameraServiceListener>& listener) {
247 const sp<::android::hardware::ICameraService>& cs = getCameraService();
248 if (cs == 0) return UNKNOWN_ERROR;
249 binder::Status res = cs->removeListener(listener);
250 return res.isOk() ? OK : res.serviceSpecificErrorCode();
253 template class CameraBase<Camera>;
255 } // namespace android