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