blob: c25c5fdcf46e22714f7863bd70d7dd23e6b767dc [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
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
37namespace android {
38
39namespace {
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:
49 DeathNotifier() {
50 }
51
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
68template <typename TCam, typename TCamTraits>
69const 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
93template <typename TCam, typename TCamTraits>
94sp<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
114template <typename TCam, typename TCamTraits>
115void 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
126template <typename TCam, typename TCamTraits>
127CameraBase<TCam, TCamTraits>::CameraBase(int cameraId) :
128 mStatus(UNKNOWN_ERROR),
129 mCameraId(cameraId)
130{
131}
132
133template <typename TCam, typename TCamTraits>
134CameraBase<TCam, TCamTraits>::~CameraBase()
135{
136}
137
138template <typename TCam, typename TCamTraits>
139sp<typename TCamTraits::TCamUser> CameraBase<TCam, TCamTraits>::remote()
140{
141 return mCamera;
142}
143
144template <typename TCam, typename TCamTraits>
145status_t CameraBase<TCam, TCamTraits>::getStatus()
146{
147 return mStatus;
148}
149
150template <typename TCam, typename TCamTraits>
151void 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
156template <typename TCam, typename TCamTraits>
157void CameraBase<TCam, TCamTraits>::setListener(const sp<TCamListener>& listener)
158{
159 Mutex::Autolock _l(mLock);
160 mListener = listener;
161}
162
163// callback from camera service
164template <typename TCam, typename TCamTraits>
165void 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
Igor Murashkinc073ba52013-02-26 14:32:34 -0800179template <typename TCam, typename TCamTraits>
180int 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
191template <typename TCam, typename TCamTraits>
192status_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
Igor Murashkinbfc99152013-02-27 12:55:20 -0800199template <typename TCam, typename TCamTraits>
200status_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
207template <typename TCam, typename TCamTraits>
208status_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
Igor Murashkinc073ba52013-02-26 14:32:34 -0800215template class CameraBase<ProCamera>;
216template class CameraBase<Camera>;
217
218} // namespace android