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
24 #include <binder/IPCThreadState.h>
25 #include <binder/IServiceManager.h>
26 #include <binder/IMemory.h>
27
28 #include <camera/CameraBase.h>
29 #include <camera/ICameraService.h>
30
31 // needed to instantiate
32 #include <camera/ProCamera.h>
33 #include <camera/Camera.h>
34
35 #include <system/camera_metadata.h>
36
37 namespace android {
38
39 namespace {
40 sp<ICameraService> gCameraService;
41 const int kCameraServicePollDelay = 500000; // 0.5s
42 const char* kCameraServiceName = "media.camera";
43
44 Mutex gLock;
45
46 class DeathNotifier : public IBinder::DeathRecipient
47 {
48 public:
DeathNotifier()49 DeathNotifier() {
50 }
51
binderDied(const wp<IBinder> & who)52 virtual void binderDied(const wp<IBinder>& who) {
53 ALOGV("binderDied");
54 Mutex::Autolock _l(gLock);
55 gCameraService.clear();
56 ALOGW("Camera service died!");
57 }
58 };
59
60 sp<DeathNotifier> gDeathNotifier;
61 }; // namespace anonymous
62
63 ///////////////////////////////////////////////////////////
64 // CameraBase definition
65 ///////////////////////////////////////////////////////////
66
67 // establish binder interface to camera service
68 template <typename TCam, typename TCamTraits>
getCameraService()69 const sp<ICameraService>& CameraBase<TCam, TCamTraits>::getCameraService()
70 {
71 Mutex::Autolock _l(gLock);
72 if (gCameraService.get() == 0) {
73 sp<IServiceManager> sm = defaultServiceManager();
74 sp<IBinder> binder;
75 do {
76 binder = sm->getService(String16(kCameraServiceName));
77 if (binder != 0) {
78 break;
79 }
80 ALOGW("CameraService not published, waiting...");
81 usleep(kCameraServicePollDelay);
82 } while(true);
83 if (gDeathNotifier == NULL) {
84 gDeathNotifier = new DeathNotifier();
85 }
86 binder->linkToDeath(gDeathNotifier);
87 gCameraService = interface_cast<ICameraService>(binder);
88 }
89 ALOGE_IF(gCameraService == 0, "no CameraService!?");
90 return gCameraService;
91 }
92
93 template <typename TCam, typename TCamTraits>
connect(int cameraId,const String16 & clientPackageName,int clientUid)94 sp<TCam> CameraBase<TCam, TCamTraits>::connect(int cameraId,
95 const String16& clientPackageName,
96 int clientUid)
97 {
98 ALOGV("%s: connect", __FUNCTION__);
99 sp<TCam> c = new TCam(cameraId);
100 sp<TCamCallbacks> cl = c;
101 const sp<ICameraService>& cs = getCameraService();
102 if (cs != 0) {
103 c->mCamera = cs->connect(cl, cameraId, clientPackageName, clientUid);
104 }
105 if (c->mCamera != 0) {
106 c->mCamera->asBinder()->linkToDeath(c);
107 c->mStatus = NO_ERROR;
108 } else {
109 c.clear();
110 }
111 return c;
112 }
113
114 template <typename TCam, typename TCamTraits>
disconnect()115 void CameraBase<TCam, TCamTraits>::disconnect()
116 {
117 ALOGV("%s: disconnect", __FUNCTION__);
118 if (mCamera != 0) {
119 mCamera->disconnect();
120 mCamera->asBinder()->unlinkToDeath(this);
121 mCamera = 0;
122 }
123 ALOGV("%s: disconnect (done)", __FUNCTION__);
124 }
125
126 template <typename TCam, typename TCamTraits>
CameraBase(int cameraId)127 CameraBase<TCam, TCamTraits>::CameraBase(int cameraId) :
128 mStatus(UNKNOWN_ERROR),
129 mCameraId(cameraId)
130 {
131 }
132
133 template <typename TCam, typename TCamTraits>
~CameraBase()134 CameraBase<TCam, TCamTraits>::~CameraBase()
135 {
136 }
137
138 template <typename TCam, typename TCamTraits>
remote()139 sp<typename TCamTraits::TCamUser> CameraBase<TCam, TCamTraits>::remote()
140 {
141 return mCamera;
142 }
143
144 template <typename TCam, typename TCamTraits>
getStatus()145 status_t CameraBase<TCam, TCamTraits>::getStatus()
146 {
147 return mStatus;
148 }
149
150 template <typename TCam, typename TCamTraits>
binderDied(const wp<IBinder> & who)151 void CameraBase<TCam, TCamTraits>::binderDied(const wp<IBinder>& who) {
152 ALOGW("mediaserver's remote binder Camera object died");
153 notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_SERVER_DIED, /*ext2*/0);
154 }
155
156 template <typename TCam, typename TCamTraits>
setListener(const sp<TCamListener> & listener)157 void CameraBase<TCam, TCamTraits>::setListener(const sp<TCamListener>& listener)
158 {
159 Mutex::Autolock _l(mLock);
160 mListener = listener;
161 }
162
163 // callback from camera service
164 template <typename TCam, typename TCamTraits>
notifyCallback(int32_t msgType,int32_t ext1,int32_t ext2)165 void CameraBase<TCam, TCamTraits>::notifyCallback(int32_t msgType,
166 int32_t ext1,
167 int32_t ext2)
168 {
169 sp<TCamListener> listener;
170 {
171 Mutex::Autolock _l(mLock);
172 listener = mListener;
173 }
174 if (listener != NULL) {
175 listener->notify(msgType, ext1, ext2);
176 }
177 }
178
179 template <typename TCam, typename TCamTraits>
getNumberOfCameras()180 int CameraBase<TCam, TCamTraits>::getNumberOfCameras() {
181 const sp<ICameraService> cs = getCameraService();
182
183 if (!cs.get()) {
184 // as required by the public Java APIs
185 return 0;
186 }
187 return cs->getNumberOfCameras();
188 }
189
190 // this can be in BaseCamera but it should be an instance method
191 template <typename TCam, typename TCamTraits>
getCameraInfo(int cameraId,struct CameraInfo * cameraInfo)192 status_t CameraBase<TCam, TCamTraits>::getCameraInfo(int cameraId,
193 struct CameraInfo* cameraInfo) {
194 const sp<ICameraService>& cs = getCameraService();
195 if (cs == 0) return UNKNOWN_ERROR;
196 return cs->getCameraInfo(cameraId, cameraInfo);
197 }
198
199 template <typename TCam, typename TCamTraits>
addServiceListener(const sp<ICameraServiceListener> & listener)200 status_t CameraBase<TCam, TCamTraits>::addServiceListener(
201 const sp<ICameraServiceListener>& listener) {
202 const sp<ICameraService>& cs = getCameraService();
203 if (cs == 0) return UNKNOWN_ERROR;
204 return cs->addListener(listener);
205 }
206
207 template <typename TCam, typename TCamTraits>
removeServiceListener(const sp<ICameraServiceListener> & listener)208 status_t CameraBase<TCam, TCamTraits>::removeServiceListener(
209 const sp<ICameraServiceListener>& listener) {
210 const sp<ICameraService>& cs = getCameraService();
211 if (cs == 0) return UNKNOWN_ERROR;
212 return cs->removeListener(listener);
213 }
214
215 template class CameraBase<ProCamera>;
216 template class CameraBase<Camera>;
217
218 } // namespace android
219