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 <binder/IPCThreadState.h>
25 #include <binder/IServiceManager.h>
26 #include <binder/IMemory.h>
28 #include <camera/CameraBase.h>
29 #include <camera/ICameraService.h>
31 // needed to instantiate
32 #include <camera/ProCamera.h>
33 #include <camera/Camera.h>
35 #include <system/camera_metadata.h>
40 sp<ICameraService> gCameraService;
41 const int kCameraServicePollDelay = 500000; // 0.5s
42 const char* kCameraServiceName = "media.camera";
46 class DeathNotifier : public IBinder::DeathRecipient
52 virtual void binderDied(const wp<IBinder>& who) {
54 Mutex::Autolock _l(gLock);
55 gCameraService.clear();
56 ALOGW("Camera service died!");
60 sp<DeathNotifier> gDeathNotifier;
61 }; // namespace anonymous
63 ///////////////////////////////////////////////////////////
64 // CameraBase definition
65 ///////////////////////////////////////////////////////////
67 // establish binder interface to camera service
68 template <typename TCam, typename TCamTraits>
69 const sp<ICameraService> CameraBase<TCam, TCamTraits>::getCameraService()
71 Mutex::Autolock _l(gLock);
72 if (gCameraService.get() == 0) {
73 sp<IServiceManager> sm = defaultServiceManager();
76 binder = sm->getService(String16(kCameraServiceName));
80 ALOGW("CameraService not published, waiting...");
81 usleep(kCameraServicePollDelay);
83 if (gDeathNotifier == NULL) {
84 gDeathNotifier = new DeathNotifier();
86 binder->linkToDeath(gDeathNotifier);
87 gCameraService = interface_cast<ICameraService>(binder);
89 ALOGE_IF(gCameraService == 0, "no CameraService!?");
90 return gCameraService;
93 template <typename TCam, typename TCamTraits>
94 sp<TCam> CameraBase<TCam, TCamTraits>::connect(int cameraId,
95 const String16& clientPackageName,
98 ALOGV("%s: connect", __FUNCTION__);
99 sp<TCam> c = new TCam(cameraId);
100 sp<TCamCallbacks> cl = c;
101 status_t status = NO_ERROR;
102 const sp<ICameraService> cs = getCameraService();
105 TCamConnectService fnConnectService = TCamTraits::fnConnectService;
106 status = (cs.get()->*fnConnectService)(cl, cameraId, clientPackageName, clientUid,
109 if (status == OK && c->mCamera != 0) {
110 c->mCamera->asBinder()->linkToDeath(c);
111 c->mStatus = NO_ERROR;
113 ALOGW("An error occurred while connecting to camera: %d", cameraId);
119 template <typename TCam, typename TCamTraits>
120 void CameraBase<TCam, TCamTraits>::disconnect()
122 ALOGV("%s: disconnect", __FUNCTION__);
124 mCamera->disconnect();
125 mCamera->asBinder()->unlinkToDeath(this);
128 ALOGV("%s: disconnect (done)", __FUNCTION__);
131 template <typename TCam, typename TCamTraits>
132 CameraBase<TCam, TCamTraits>::CameraBase(int cameraId) :
133 mStatus(UNKNOWN_ERROR),
138 template <typename TCam, typename TCamTraits>
139 CameraBase<TCam, TCamTraits>::~CameraBase()
143 template <typename TCam, typename TCamTraits>
144 sp<typename TCamTraits::TCamUser> CameraBase<TCam, TCamTraits>::remote()
149 template <typename TCam, typename TCamTraits>
150 status_t CameraBase<TCam, TCamTraits>::getStatus()
155 template <typename TCam, typename TCamTraits>
156 void CameraBase<TCam, TCamTraits>::binderDied(const wp<IBinder>& who) {
157 ALOGW("mediaserver's remote binder Camera object died");
158 notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_SERVER_DIED, /*ext2*/0);
161 template <typename TCam, typename TCamTraits>
162 void CameraBase<TCam, TCamTraits>::setListener(const sp<TCamListener>& listener)
164 Mutex::Autolock _l(mLock);
165 mListener = listener;
168 // callback from camera service
169 template <typename TCam, typename TCamTraits>
170 void CameraBase<TCam, TCamTraits>::notifyCallback(int32_t msgType,
174 sp<TCamListener> listener;
176 Mutex::Autolock _l(mLock);
177 listener = mListener;
179 if (listener != NULL) {
180 listener->notify(msgType, ext1, ext2);
184 template <typename TCam, typename TCamTraits>
185 int CameraBase<TCam, TCamTraits>::getNumberOfCameras() {
186 const sp<ICameraService> cs = getCameraService();
189 // as required by the public Java APIs
192 return cs->getNumberOfCameras();
195 // this can be in BaseCamera but it should be an instance method
196 template <typename TCam, typename TCamTraits>
197 status_t CameraBase<TCam, TCamTraits>::getCameraInfo(int cameraId,
198 struct CameraInfo* cameraInfo) {
199 const sp<ICameraService> cs = getCameraService();
200 if (cs == 0) return UNKNOWN_ERROR;
201 return cs->getCameraInfo(cameraId, cameraInfo);
204 template <typename TCam, typename TCamTraits>
205 status_t CameraBase<TCam, TCamTraits>::addServiceListener(
206 const sp<ICameraServiceListener>& listener) {
207 const sp<ICameraService> cs = getCameraService();
208 if (cs == 0) return UNKNOWN_ERROR;
209 return cs->addListener(listener);
212 template <typename TCam, typename TCamTraits>
213 status_t CameraBase<TCam, TCamTraits>::removeServiceListener(
214 const sp<ICameraServiceListener>& listener) {
215 const sp<ICameraService> cs = getCameraService();
216 if (cs == 0) return UNKNOWN_ERROR;
217 return cs->removeListener(listener);
220 template class CameraBase<ProCamera>;
221 template class CameraBase<Camera>;
223 } // namespace android