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> |
| 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 |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 32 | #include <camera/Camera.h> |
| 33 | |
| 34 | #include <system/camera_metadata.h> |
| 35 | |
| 36 | namespace android { |
| 37 | |
| 38 | namespace { |
| 39 | sp<ICameraService> gCameraService; |
| 40 | const int kCameraServicePollDelay = 500000; // 0.5s |
| 41 | const char* kCameraServiceName = "media.camera"; |
| 42 | |
| 43 | Mutex gLock; |
| 44 | |
| 45 | class DeathNotifier : public IBinder::DeathRecipient |
| 46 | { |
| 47 | public: |
| 48 | DeathNotifier() { |
| 49 | } |
| 50 | |
Mark Salyzyn | 7b73e71 | 2014-06-09 16:28:21 -0700 | [diff] [blame] | 51 | virtual void binderDied(const wp<IBinder>& /*who*/) { |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 52 | ALOGV("binderDied"); |
| 53 | Mutex::Autolock _l(gLock); |
| 54 | gCameraService.clear(); |
| 55 | ALOGW("Camera service died!"); |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | sp<DeathNotifier> gDeathNotifier; |
| 60 | }; // namespace anonymous |
| 61 | |
| 62 | /////////////////////////////////////////////////////////// |
| 63 | // CameraBase definition |
| 64 | /////////////////////////////////////////////////////////// |
| 65 | |
| 66 | // establish binder interface to camera service |
| 67 | template <typename TCam, typename TCamTraits> |
| 68 | const sp<ICameraService>& CameraBase<TCam, TCamTraits>::getCameraService() |
| 69 | { |
| 70 | Mutex::Autolock _l(gLock); |
| 71 | if (gCameraService.get() == 0) { |
| 72 | sp<IServiceManager> sm = defaultServiceManager(); |
| 73 | sp<IBinder> binder; |
| 74 | do { |
| 75 | binder = sm->getService(String16(kCameraServiceName)); |
| 76 | if (binder != 0) { |
| 77 | break; |
| 78 | } |
| 79 | ALOGW("CameraService not published, waiting..."); |
| 80 | usleep(kCameraServicePollDelay); |
| 81 | } while(true); |
| 82 | if (gDeathNotifier == NULL) { |
| 83 | gDeathNotifier = new DeathNotifier(); |
| 84 | } |
| 85 | binder->linkToDeath(gDeathNotifier); |
| 86 | gCameraService = interface_cast<ICameraService>(binder); |
| 87 | } |
| 88 | ALOGE_IF(gCameraService == 0, "no CameraService!?"); |
| 89 | return gCameraService; |
| 90 | } |
| 91 | |
| 92 | template <typename TCam, typename TCamTraits> |
| 93 | sp<TCam> CameraBase<TCam, TCamTraits>::connect(int cameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 94 | const String16& clientPackageName, |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 95 | int clientUid) |
| 96 | { |
| 97 | ALOGV("%s: connect", __FUNCTION__); |
| 98 | sp<TCam> c = new TCam(cameraId); |
| 99 | sp<TCamCallbacks> cl = c; |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 100 | status_t status = NO_ERROR; |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 101 | const sp<ICameraService>& cs = getCameraService(); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 102 | |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 103 | if (cs != 0) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 104 | TCamConnectService fnConnectService = TCamTraits::fnConnectService; |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 105 | status = (cs.get()->*fnConnectService)(cl, cameraId, clientPackageName, clientUid, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 106 | /*out*/ c->mCamera); |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 107 | } |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 108 | if (status == OK && c->mCamera != 0) { |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 109 | IInterface::asBinder(c->mCamera)->linkToDeath(c); |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 110 | c->mStatus = NO_ERROR; |
| 111 | } else { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 112 | ALOGW("An error occurred while connecting to camera: %d", cameraId); |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 113 | c.clear(); |
| 114 | } |
| 115 | return c; |
| 116 | } |
| 117 | |
| 118 | template <typename TCam, typename TCamTraits> |
| 119 | void CameraBase<TCam, TCamTraits>::disconnect() |
| 120 | { |
| 121 | ALOGV("%s: disconnect", __FUNCTION__); |
| 122 | if (mCamera != 0) { |
| 123 | mCamera->disconnect(); |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 124 | IInterface::asBinder(mCamera)->unlinkToDeath(this); |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 125 | mCamera = 0; |
| 126 | } |
| 127 | ALOGV("%s: disconnect (done)", __FUNCTION__); |
| 128 | } |
| 129 | |
| 130 | template <typename TCam, typename TCamTraits> |
| 131 | CameraBase<TCam, TCamTraits>::CameraBase(int cameraId) : |
| 132 | mStatus(UNKNOWN_ERROR), |
| 133 | mCameraId(cameraId) |
| 134 | { |
| 135 | } |
| 136 | |
| 137 | template <typename TCam, typename TCamTraits> |
| 138 | CameraBase<TCam, TCamTraits>::~CameraBase() |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | template <typename TCam, typename TCamTraits> |
| 143 | sp<typename TCamTraits::TCamUser> CameraBase<TCam, TCamTraits>::remote() |
| 144 | { |
| 145 | return mCamera; |
| 146 | } |
| 147 | |
| 148 | template <typename TCam, typename TCamTraits> |
| 149 | status_t CameraBase<TCam, TCamTraits>::getStatus() |
| 150 | { |
| 151 | return mStatus; |
| 152 | } |
| 153 | |
| 154 | template <typename TCam, typename TCamTraits> |
Mark Salyzyn | 7b73e71 | 2014-06-09 16:28:21 -0700 | [diff] [blame] | 155 | void CameraBase<TCam, TCamTraits>::binderDied(const wp<IBinder>& /*who*/) { |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 156 | ALOGW("mediaserver's remote binder Camera object died"); |
| 157 | notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_SERVER_DIED, /*ext2*/0); |
| 158 | } |
| 159 | |
| 160 | template <typename TCam, typename TCamTraits> |
| 161 | void CameraBase<TCam, TCamTraits>::setListener(const sp<TCamListener>& listener) |
| 162 | { |
| 163 | Mutex::Autolock _l(mLock); |
| 164 | mListener = listener; |
| 165 | } |
| 166 | |
| 167 | // callback from camera service |
| 168 | template <typename TCam, typename TCamTraits> |
| 169 | void CameraBase<TCam, TCamTraits>::notifyCallback(int32_t msgType, |
| 170 | int32_t ext1, |
| 171 | int32_t ext2) |
| 172 | { |
| 173 | sp<TCamListener> listener; |
| 174 | { |
| 175 | Mutex::Autolock _l(mLock); |
| 176 | listener = mListener; |
| 177 | } |
| 178 | if (listener != NULL) { |
| 179 | listener->notify(msgType, ext1, ext2); |
| 180 | } |
| 181 | } |
| 182 | |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 183 | template <typename TCam, typename TCamTraits> |
| 184 | int CameraBase<TCam, TCamTraits>::getNumberOfCameras() { |
| 185 | const sp<ICameraService> cs = getCameraService(); |
| 186 | |
| 187 | if (!cs.get()) { |
| 188 | // as required by the public Java APIs |
| 189 | return 0; |
| 190 | } |
| 191 | return cs->getNumberOfCameras(); |
| 192 | } |
| 193 | |
| 194 | // this can be in BaseCamera but it should be an instance method |
| 195 | template <typename TCam, typename TCamTraits> |
| 196 | status_t CameraBase<TCam, TCamTraits>::getCameraInfo(int cameraId, |
| 197 | struct CameraInfo* cameraInfo) { |
| 198 | const sp<ICameraService>& cs = getCameraService(); |
| 199 | if (cs == 0) return UNKNOWN_ERROR; |
| 200 | return cs->getCameraInfo(cameraId, cameraInfo); |
| 201 | } |
| 202 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 203 | template <typename TCam, typename TCamTraits> |
| 204 | status_t CameraBase<TCam, TCamTraits>::addServiceListener( |
| 205 | const sp<ICameraServiceListener>& listener) { |
| 206 | const sp<ICameraService>& cs = getCameraService(); |
| 207 | if (cs == 0) return UNKNOWN_ERROR; |
| 208 | return cs->addListener(listener); |
| 209 | } |
| 210 | |
| 211 | template <typename TCam, typename TCamTraits> |
| 212 | status_t CameraBase<TCam, TCamTraits>::removeServiceListener( |
| 213 | const sp<ICameraServiceListener>& listener) { |
| 214 | const sp<ICameraService>& cs = getCameraService(); |
| 215 | if (cs == 0) return UNKNOWN_ERROR; |
| 216 | return cs->removeListener(listener); |
| 217 | } |
| 218 | |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 219 | template class CameraBase<Camera>; |
| 220 | |
| 221 | } // namespace android |