• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <cutils/properties.h>
24 
25 #include <android/hardware/ICameraService.h>
26 #include <com/android/internal/compat/IPlatformCompatNative.h>
27 
28 #include <binder/IPCThreadState.h>
29 #include <binder/IServiceManager.h>
30 #include <binder/IMemory.h>
31 
32 #include <camera/CameraBase.h>
33 #include <camera/CameraUtils.h>
34 
35 // needed to instantiate
36 #include <camera/Camera.h>
37 
38 #include <system/camera_metadata.h>
39 
40 namespace android {
41 
42 namespace hardware {
43 
writeToParcel(android::Parcel * parcel) const44 status_t CameraInfo::writeToParcel(android::Parcel* parcel) const {
45     status_t res;
46     res = parcel->writeInt32(facing);
47     if (res != OK) return res;
48     res = parcel->writeInt32(orientation);
49     return res;
50 }
51 
readFromParcel(const android::Parcel * parcel)52 status_t CameraInfo::readFromParcel(const android::Parcel* parcel) {
53     status_t res;
54     res = parcel->readInt32(&facing);
55     if (res != OK) return res;
56     res = parcel->readInt32(&orientation);
57     return res;
58 }
59 
writeToParcel(android::Parcel * parcel) const60 status_t CameraStatus::writeToParcel(android::Parcel* parcel) const {
61     auto res = parcel->writeString16(String16(cameraId));
62     if (res != OK) return res;
63 
64     res = parcel->writeInt32(status);
65     if (res != OK) return res;
66 
67     std::vector<String16> unavailablePhysicalIds16;
68     for (auto& id8 : unavailablePhysicalIds) {
69         unavailablePhysicalIds16.push_back(String16(id8));
70     }
71     res = parcel->writeString16Vector(unavailablePhysicalIds16);
72     if (res != OK) return res;
73 
74     res = parcel->writeString16(String16(clientPackage));
75     return res;
76 }
77 
readFromParcel(const android::Parcel * parcel)78 status_t CameraStatus::readFromParcel(const android::Parcel* parcel) {
79     String16 tempCameraId;
80     auto res = parcel->readString16(&tempCameraId);
81     if (res != OK) return res;
82     cameraId = String8(tempCameraId);
83 
84     res = parcel->readInt32(&status);
85     if (res != OK) return res;
86 
87     std::vector<String16> unavailablePhysicalIds16;
88     res = parcel->readString16Vector(&unavailablePhysicalIds16);
89     if (res != OK) return res;
90     for (auto& id16 : unavailablePhysicalIds16) {
91         unavailablePhysicalIds.push_back(String8(id16));
92     }
93 
94     String16 tempClientPackage;
95     res = parcel->readString16(&tempClientPackage);
96     if (res != OK) return res;
97     clientPackage = String8(tempClientPackage);
98 
99     return res;
100 }
101 
102 } // namespace hardware
103 
104 namespace {
105     sp<::android::hardware::ICameraService> gCameraService;
106     const int                 kCameraServicePollDelay = 500000; // 0.5s
107     const char*               kCameraServiceName      = "media.camera";
108 
109     Mutex                     gLock;
110 
111     class DeathNotifier : public IBinder::DeathRecipient
112     {
113     public:
DeathNotifier()114         DeathNotifier() {
115         }
116 
binderDied(const wp<IBinder> &)117         virtual void binderDied(const wp<IBinder>& /*who*/) {
118             ALOGV("binderDied");
119             Mutex::Autolock _l(gLock);
120             gCameraService.clear();
121             ALOGW("Camera service died!");
122         }
123     };
124 
125     sp<DeathNotifier>         gDeathNotifier;
126 }; // namespace anonymous
127 
128 ///////////////////////////////////////////////////////////
129 // CameraBase definition
130 ///////////////////////////////////////////////////////////
131 
132 // establish binder interface to camera service
133 template <typename TCam, typename TCamTraits>
getCameraService()134 const sp<::android::hardware::ICameraService> CameraBase<TCam, TCamTraits>::getCameraService()
135 {
136     Mutex::Autolock _l(gLock);
137     if (gCameraService.get() == 0) {
138         if (CameraUtils::isCameraServiceDisabled()) {
139             return gCameraService;
140         }
141 
142         sp<IServiceManager> sm = defaultServiceManager();
143         sp<IBinder> binder;
144         do {
145             binder = sm->getService(String16(kCameraServiceName));
146             if (binder != 0) {
147                 break;
148             }
149             ALOGW("CameraService not published, waiting...");
150             usleep(kCameraServicePollDelay);
151         } while(true);
152         if (gDeathNotifier == NULL) {
153             gDeathNotifier = new DeathNotifier();
154         }
155         binder->linkToDeath(gDeathNotifier);
156         gCameraService = interface_cast<::android::hardware::ICameraService>(binder);
157     }
158     ALOGE_IF(gCameraService == 0, "no CameraService!?");
159     return gCameraService;
160 }
161 
162 template <typename TCam, typename TCamTraits>
connect(int cameraId,const String16 & clientPackageName,int clientUid,int clientPid,int targetSdkVersion,bool overrideToPortrait,bool forceSlowJpegMode)163 sp<TCam> CameraBase<TCam, TCamTraits>::connect(int cameraId,
164                                                const String16& clientPackageName,
165                                                int clientUid, int clientPid, int targetSdkVersion,
166                                                bool overrideToPortrait, bool forceSlowJpegMode)
167 {
168     ALOGV("%s: connect", __FUNCTION__);
169     sp<TCam> c = new TCam(cameraId);
170     sp<TCamCallbacks> cl = c;
171     const sp<::android::hardware::ICameraService> cs = getCameraService();
172 
173     binder::Status ret;
174     if (cs != nullptr) {
175         TCamConnectService fnConnectService = TCamTraits::fnConnectService;
176         ALOGI("Connect camera (legacy API) - overrideToPortrait %d, forceSlowJpegMode %d",
177                 overrideToPortrait, forceSlowJpegMode);
178         ret = (cs.get()->*fnConnectService)(cl, cameraId, clientPackageName, clientUid,
179                 clientPid, targetSdkVersion, overrideToPortrait, forceSlowJpegMode,
180                  /*out*/ &c->mCamera);
181     }
182     if (ret.isOk() && c->mCamera != nullptr) {
183         IInterface::asBinder(c->mCamera)->linkToDeath(c);
184         c->mStatus = NO_ERROR;
185     } else {
186         ALOGW("An error occurred while connecting to camera %d: %s", cameraId,
187                 (cs == nullptr) ? "Service not available" : ret.toString8().string());
188         c.clear();
189     }
190     return c;
191 }
192 
193 template <typename TCam, typename TCamTraits>
disconnect()194 void CameraBase<TCam, TCamTraits>::disconnect()
195 {
196     ALOGV("%s: disconnect", __FUNCTION__);
197     if (mCamera != 0) {
198         mCamera->disconnect();
199         IInterface::asBinder(mCamera)->unlinkToDeath(this);
200         mCamera = 0;
201     }
202     ALOGV("%s: disconnect (done)", __FUNCTION__);
203 }
204 
205 template <typename TCam, typename TCamTraits>
CameraBase(int cameraId)206 CameraBase<TCam, TCamTraits>::CameraBase(int cameraId) :
207     mStatus(UNKNOWN_ERROR),
208     mCameraId(cameraId)
209 {
210 }
211 
212 template <typename TCam, typename TCamTraits>
~CameraBase()213 CameraBase<TCam, TCamTraits>::~CameraBase()
214 {
215 }
216 
217 template <typename TCam, typename TCamTraits>
remote()218 sp<typename TCamTraits::TCamUser> CameraBase<TCam, TCamTraits>::remote()
219 {
220     return mCamera;
221 }
222 
223 template <typename TCam, typename TCamTraits>
getStatus()224 status_t CameraBase<TCam, TCamTraits>::getStatus()
225 {
226     return mStatus;
227 }
228 
229 template <typename TCam, typename TCamTraits>
binderDied(const wp<IBinder> &)230 void CameraBase<TCam, TCamTraits>::binderDied(const wp<IBinder>& /*who*/) {
231     ALOGW("mediaserver's remote binder Camera object died");
232     notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_SERVER_DIED, /*ext2*/0);
233 }
234 
235 template <typename TCam, typename TCamTraits>
setListener(const sp<TCamListener> & listener)236 void CameraBase<TCam, TCamTraits>::setListener(const sp<TCamListener>& listener)
237 {
238     Mutex::Autolock _l(mLock);
239     mListener = listener;
240 }
241 
242 // callback from camera service
243 template <typename TCam, typename TCamTraits>
notifyCallback(int32_t msgType,int32_t ext1,int32_t ext2)244 void CameraBase<TCam, TCamTraits>::notifyCallback(int32_t msgType,
245                                                   int32_t ext1,
246                                                   int32_t ext2)
247 {
248     sp<TCamListener> listener;
249     {
250         Mutex::Autolock _l(mLock);
251         listener = mListener;
252     }
253     if (listener != NULL) {
254         listener->notify(msgType, ext1, ext2);
255     }
256 }
257 
258 template <typename TCam, typename TCamTraits>
getNumberOfCameras()259 int CameraBase<TCam, TCamTraits>::getNumberOfCameras() {
260     const sp<::android::hardware::ICameraService> cs = getCameraService();
261 
262     if (!cs.get()) {
263         // as required by the public Java APIs
264         return 0;
265     }
266     int32_t count;
267     binder::Status res = cs->getNumberOfCameras(
268             ::android::hardware::ICameraService::CAMERA_TYPE_BACKWARD_COMPATIBLE,
269             &count);
270     if (!res.isOk()) {
271         ALOGE("Error reading number of cameras: %s",
272                 res.toString8().string());
273         count = 0;
274     }
275     return count;
276 }
277 
278 // this can be in BaseCamera but it should be an instance method
279 template <typename TCam, typename TCamTraits>
getCameraInfo(int cameraId,bool overrideToPortrait,struct hardware::CameraInfo * cameraInfo)280 status_t CameraBase<TCam, TCamTraits>::getCameraInfo(int cameraId,
281         bool overrideToPortrait,
282         struct hardware::CameraInfo* cameraInfo) {
283     const sp<::android::hardware::ICameraService> cs = getCameraService();
284     if (cs == 0) return UNKNOWN_ERROR;
285     binder::Status res = cs->getCameraInfo(cameraId, overrideToPortrait, cameraInfo);
286     return res.isOk() ? OK : res.serviceSpecificErrorCode();
287 }
288 
289 template class CameraBase<Camera>;
290 
291 } // namespace android
292