Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 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> |
Ivan Podogov | ee844a8 | 2016-09-15 11:32:41 +0100 | [diff] [blame] | 23 | #include <cutils/properties.h> |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 24 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 25 | #include <android/hardware/ICameraService.h> |
| 26 | |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 27 | #include <binder/IPCThreadState.h> |
| 28 | #include <binder/IServiceManager.h> |
| 29 | #include <binder/IMemory.h> |
| 30 | |
| 31 | #include <camera/CameraBase.h> |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 32 | |
| 33 | // needed to instantiate |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 34 | #include <camera/Camera.h> |
| 35 | |
| 36 | #include <system/camera_metadata.h> |
| 37 | |
| 38 | namespace android { |
| 39 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 40 | namespace hardware { |
| 41 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 42 | status_t CameraInfo::writeToParcel(android::Parcel* parcel) const { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 43 | status_t res; |
| 44 | res = parcel->writeInt32(facing); |
| 45 | if (res != OK) return res; |
| 46 | res = parcel->writeInt32(orientation); |
| 47 | return res; |
| 48 | } |
| 49 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 50 | status_t CameraInfo::readFromParcel(const android::Parcel* parcel) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 51 | status_t res; |
| 52 | res = parcel->readInt32(&facing); |
| 53 | if (res != OK) return res; |
| 54 | res = parcel->readInt32(&orientation); |
| 55 | return res; |
| 56 | } |
| 57 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 58 | status_t CameraStatus::writeToParcel(android::Parcel* parcel) const { |
Oleksiy Vyalov | ead8472 | 2017-03-24 14:06:03 -0700 | [diff] [blame^] | 59 | auto res = parcel->writeString16(String16(cameraId)); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 60 | if (res != OK) return res; |
Oleksiy Vyalov | ead8472 | 2017-03-24 14:06:03 -0700 | [diff] [blame^] | 61 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 62 | res = parcel->writeInt32(status); |
| 63 | return res; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 66 | status_t CameraStatus::readFromParcel(const android::Parcel* parcel) { |
Oleksiy Vyalov | ead8472 | 2017-03-24 14:06:03 -0700 | [diff] [blame^] | 67 | String16 tempCameraId; |
| 68 | auto res = parcel->readString16(&tempCameraId); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 69 | if (res != OK) return res; |
Oleksiy Vyalov | ead8472 | 2017-03-24 14:06:03 -0700 | [diff] [blame^] | 70 | cameraId = String8(tempCameraId); |
| 71 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 72 | res = parcel->readInt32(&status); |
| 73 | return res; |
| 74 | } |
| 75 | |
| 76 | } // namespace hardware |
| 77 | |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 78 | namespace { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 79 | sp<::android::hardware::ICameraService> gCameraService; |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 80 | const int kCameraServicePollDelay = 500000; // 0.5s |
| 81 | const char* kCameraServiceName = "media.camera"; |
| 82 | |
| 83 | Mutex gLock; |
| 84 | |
| 85 | class DeathNotifier : public IBinder::DeathRecipient |
| 86 | { |
| 87 | public: |
| 88 | DeathNotifier() { |
| 89 | } |
| 90 | |
Mark Salyzyn | 7b73e71 | 2014-06-09 16:28:21 -0700 | [diff] [blame] | 91 | virtual void binderDied(const wp<IBinder>& /*who*/) { |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 92 | ALOGV("binderDied"); |
| 93 | Mutex::Autolock _l(gLock); |
| 94 | gCameraService.clear(); |
| 95 | ALOGW("Camera service died!"); |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | sp<DeathNotifier> gDeathNotifier; |
| 100 | }; // namespace anonymous |
| 101 | |
| 102 | /////////////////////////////////////////////////////////// |
| 103 | // CameraBase definition |
| 104 | /////////////////////////////////////////////////////////// |
| 105 | |
| 106 | // establish binder interface to camera service |
| 107 | template <typename TCam, typename TCamTraits> |
Eino-Ville Talvala | f86177d | 2017-02-01 15:27:41 -0800 | [diff] [blame] | 108 | const sp<::android::hardware::ICameraService> CameraBase<TCam, TCamTraits>::getCameraService() |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 109 | { |
| 110 | Mutex::Autolock _l(gLock); |
| 111 | if (gCameraService.get() == 0) { |
Ivan Podogov | ee844a8 | 2016-09-15 11:32:41 +0100 | [diff] [blame] | 112 | char value[PROPERTY_VALUE_MAX]; |
| 113 | property_get("config.disable_cameraservice", value, "0"); |
| 114 | if (strncmp(value, "0", 2) != 0 && strncasecmp(value, "false", 6) != 0) { |
| 115 | return gCameraService; |
| 116 | } |
| 117 | |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 118 | sp<IServiceManager> sm = defaultServiceManager(); |
| 119 | sp<IBinder> binder; |
| 120 | do { |
| 121 | binder = sm->getService(String16(kCameraServiceName)); |
| 122 | if (binder != 0) { |
| 123 | break; |
| 124 | } |
| 125 | ALOGW("CameraService not published, waiting..."); |
| 126 | usleep(kCameraServicePollDelay); |
| 127 | } while(true); |
| 128 | if (gDeathNotifier == NULL) { |
| 129 | gDeathNotifier = new DeathNotifier(); |
| 130 | } |
| 131 | binder->linkToDeath(gDeathNotifier); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 132 | gCameraService = interface_cast<::android::hardware::ICameraService>(binder); |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 133 | } |
| 134 | ALOGE_IF(gCameraService == 0, "no CameraService!?"); |
| 135 | return gCameraService; |
| 136 | } |
| 137 | |
| 138 | template <typename TCam, typename TCamTraits> |
| 139 | sp<TCam> CameraBase<TCam, TCamTraits>::connect(int cameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 140 | const String16& clientPackageName, |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 141 | int clientUid, int clientPid) |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 142 | { |
| 143 | ALOGV("%s: connect", __FUNCTION__); |
| 144 | sp<TCam> c = new TCam(cameraId); |
| 145 | sp<TCamCallbacks> cl = c; |
Eino-Ville Talvala | f86177d | 2017-02-01 15:27:41 -0800 | [diff] [blame] | 146 | const sp<::android::hardware::ICameraService> cs = getCameraService(); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 147 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 148 | binder::Status ret; |
| 149 | if (cs != nullptr) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 150 | TCamConnectService fnConnectService = TCamTraits::fnConnectService; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 151 | ret = (cs.get()->*fnConnectService)(cl, cameraId, clientPackageName, clientUid, |
| 152 | clientPid, /*out*/ &c->mCamera); |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 153 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 154 | if (ret.isOk() && c->mCamera != nullptr) { |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 155 | IInterface::asBinder(c->mCamera)->linkToDeath(c); |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 156 | c->mStatus = NO_ERROR; |
| 157 | } else { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 158 | ALOGW("An error occurred while connecting to camera %d: %s", cameraId, |
Tobias Lindskog | 0ccb13b | 2016-11-01 14:25:52 +0100 | [diff] [blame] | 159 | (cs == nullptr) ? "Service not available" : ret.toString8().string()); |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 160 | c.clear(); |
| 161 | } |
| 162 | return c; |
| 163 | } |
| 164 | |
| 165 | template <typename TCam, typename TCamTraits> |
| 166 | void CameraBase<TCam, TCamTraits>::disconnect() |
| 167 | { |
| 168 | ALOGV("%s: disconnect", __FUNCTION__); |
| 169 | if (mCamera != 0) { |
| 170 | mCamera->disconnect(); |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 171 | IInterface::asBinder(mCamera)->unlinkToDeath(this); |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 172 | mCamera = 0; |
| 173 | } |
| 174 | ALOGV("%s: disconnect (done)", __FUNCTION__); |
| 175 | } |
| 176 | |
| 177 | template <typename TCam, typename TCamTraits> |
| 178 | CameraBase<TCam, TCamTraits>::CameraBase(int cameraId) : |
| 179 | mStatus(UNKNOWN_ERROR), |
| 180 | mCameraId(cameraId) |
| 181 | { |
| 182 | } |
| 183 | |
| 184 | template <typename TCam, typename TCamTraits> |
| 185 | CameraBase<TCam, TCamTraits>::~CameraBase() |
| 186 | { |
| 187 | } |
| 188 | |
| 189 | template <typename TCam, typename TCamTraits> |
| 190 | sp<typename TCamTraits::TCamUser> CameraBase<TCam, TCamTraits>::remote() |
| 191 | { |
| 192 | return mCamera; |
| 193 | } |
| 194 | |
| 195 | template <typename TCam, typename TCamTraits> |
| 196 | status_t CameraBase<TCam, TCamTraits>::getStatus() |
| 197 | { |
| 198 | return mStatus; |
| 199 | } |
| 200 | |
| 201 | template <typename TCam, typename TCamTraits> |
Mark Salyzyn | 7b73e71 | 2014-06-09 16:28:21 -0700 | [diff] [blame] | 202 | void CameraBase<TCam, TCamTraits>::binderDied(const wp<IBinder>& /*who*/) { |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 203 | ALOGW("mediaserver's remote binder Camera object died"); |
| 204 | notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_SERVER_DIED, /*ext2*/0); |
| 205 | } |
| 206 | |
| 207 | template <typename TCam, typename TCamTraits> |
| 208 | void CameraBase<TCam, TCamTraits>::setListener(const sp<TCamListener>& listener) |
| 209 | { |
| 210 | Mutex::Autolock _l(mLock); |
| 211 | mListener = listener; |
| 212 | } |
| 213 | |
| 214 | // callback from camera service |
| 215 | template <typename TCam, typename TCamTraits> |
| 216 | void CameraBase<TCam, TCamTraits>::notifyCallback(int32_t msgType, |
| 217 | int32_t ext1, |
| 218 | int32_t ext2) |
| 219 | { |
| 220 | sp<TCamListener> listener; |
| 221 | { |
| 222 | Mutex::Autolock _l(mLock); |
| 223 | listener = mListener; |
| 224 | } |
| 225 | if (listener != NULL) { |
| 226 | listener->notify(msgType, ext1, ext2); |
| 227 | } |
| 228 | } |
| 229 | |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 230 | template <typename TCam, typename TCamTraits> |
| 231 | int CameraBase<TCam, TCamTraits>::getNumberOfCameras() { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 232 | const sp<::android::hardware::ICameraService> cs = getCameraService(); |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 233 | |
| 234 | if (!cs.get()) { |
| 235 | // as required by the public Java APIs |
| 236 | return 0; |
| 237 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 238 | int32_t count; |
| 239 | binder::Status res = cs->getNumberOfCameras( |
| 240 | ::android::hardware::ICameraService::CAMERA_TYPE_BACKWARD_COMPATIBLE, |
| 241 | &count); |
| 242 | if (!res.isOk()) { |
| 243 | ALOGE("Error reading number of cameras: %s", |
| 244 | res.toString8().string()); |
| 245 | count = 0; |
| 246 | } |
| 247 | return count; |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // this can be in BaseCamera but it should be an instance method |
| 251 | template <typename TCam, typename TCamTraits> |
| 252 | status_t CameraBase<TCam, TCamTraits>::getCameraInfo(int cameraId, |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 253 | struct hardware::CameraInfo* cameraInfo) { |
Eino-Ville Talvala | f86177d | 2017-02-01 15:27:41 -0800 | [diff] [blame] | 254 | const sp<::android::hardware::ICameraService> cs = getCameraService(); |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 255 | if (cs == 0) return UNKNOWN_ERROR; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 256 | binder::Status res = cs->getCameraInfo(cameraId, cameraInfo); |
| 257 | return res.isOk() ? OK : res.serviceSpecificErrorCode(); |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 260 | template class CameraBase<Camera>; |
| 261 | |
| 262 | } // namespace android |