OSDN Git Service

Merge "OMXNodeInstance: use a lock in freeNode" into lmp-dev am: 288566faba -s ours...
[android-x86/frameworks-av.git] / camera / CameraBase.cpp
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 <android/hardware/ICameraService.h>
25
26 #include <binder/IPCThreadState.h>
27 #include <binder/IServiceManager.h>
28 #include <binder/IMemory.h>
29
30 #include <camera/CameraBase.h>
31
32 // needed to instantiate
33 #include <camera/Camera.h>
34
35 #include <system/camera_metadata.h>
36
37 namespace android {
38
39 namespace hardware {
40
41 status_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
49 status_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
59 namespace {
60     sp<::android::hardware::ICameraService> gCameraService;
61     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
72         virtual void binderDied(const wp<IBinder>& /*who*/) {
73             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
88 template <typename TCam, typename TCamTraits>
89 const sp<::android::hardware::ICameraService> CameraBase<TCam, TCamTraits>::getCameraService()
90 {
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);
107         gCameraService = interface_cast<::android::hardware::ICameraService>(binder);
108     }
109     ALOGE_IF(gCameraService == 0, "no CameraService!?");
110     return gCameraService;
111 }
112
113 template <typename TCam, typename TCamTraits>
114 sp<TCam> CameraBase<TCam, TCamTraits>::connect(int cameraId,
115                                                const String16& clientPackageName,
116                                                int clientUid, int clientPid)
117 {
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();
122
123     binder::Status ret;
124     if (cs != nullptr) {
125         TCamConnectService fnConnectService = TCamTraits::fnConnectService;
126         ret = (cs.get()->*fnConnectService)(cl, cameraId, clientPackageName, clientUid,
127                                                clientPid, /*out*/ &c->mCamera);
128     }
129     if (ret.isOk() && c->mCamera != nullptr) {
130         IInterface::asBinder(c->mCamera)->linkToDeath(c);
131         c->mStatus = NO_ERROR;
132     } else {
133         ALOGW("An error occurred while connecting to camera %d: %s", cameraId,
134                 (cs != nullptr) ? "Service not available" : ret.toString8().string());
135         c.clear();
136     }
137     return c;
138 }
139
140 template <typename TCam, typename TCamTraits>
141 void CameraBase<TCam, TCamTraits>::disconnect()
142 {
143     ALOGV("%s: disconnect", __FUNCTION__);
144     if (mCamera != 0) {
145         mCamera->disconnect();
146         IInterface::asBinder(mCamera)->unlinkToDeath(this);
147         mCamera = 0;
148     }
149     ALOGV("%s: disconnect (done)", __FUNCTION__);
150 }
151
152 template <typename TCam, typename TCamTraits>
153 CameraBase<TCam, TCamTraits>::CameraBase(int cameraId) :
154     mStatus(UNKNOWN_ERROR),
155     mCameraId(cameraId)
156 {
157 }
158
159 template <typename TCam, typename TCamTraits>
160 CameraBase<TCam, TCamTraits>::~CameraBase()
161 {
162 }
163
164 template <typename TCam, typename TCamTraits>
165 sp<typename TCamTraits::TCamUser> CameraBase<TCam, TCamTraits>::remote()
166 {
167     return mCamera;
168 }
169
170 template <typename TCam, typename TCamTraits>
171 status_t CameraBase<TCam, TCamTraits>::getStatus()
172 {
173     return mStatus;
174 }
175
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);
180 }
181
182 template <typename TCam, typename TCamTraits>
183 void CameraBase<TCam, TCamTraits>::setListener(const sp<TCamListener>& listener)
184 {
185     Mutex::Autolock _l(mLock);
186     mListener = listener;
187 }
188
189 // callback from camera service
190 template <typename TCam, typename TCamTraits>
191 void CameraBase<TCam, TCamTraits>::notifyCallback(int32_t msgType,
192                                                   int32_t ext1,
193                                                   int32_t ext2)
194 {
195     sp<TCamListener> listener;
196     {
197         Mutex::Autolock _l(mLock);
198         listener = mListener;
199     }
200     if (listener != NULL) {
201         listener->notify(msgType, ext1, ext2);
202     }
203 }
204
205 template <typename TCam, typename TCamTraits>
206 int CameraBase<TCam, TCamTraits>::getNumberOfCameras() {
207     const sp<::android::hardware::ICameraService> cs = getCameraService();
208
209     if (!cs.get()) {
210         // as required by the public Java APIs
211         return 0;
212     }
213     int32_t count;
214     binder::Status res = cs->getNumberOfCameras(
215             ::android::hardware::ICameraService::CAMERA_TYPE_BACKWARD_COMPATIBLE,
216             &count);
217     if (!res.isOk()) {
218         ALOGE("Error reading number of cameras: %s",
219                 res.toString8().string());
220         count = 0;
221     }
222     return count;
223 }
224
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();
233 }
234
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();
242 }
243
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();
251 }
252
253 template class CameraBase<Camera>;
254
255 } // namespace android