Camera.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. **
  3. ** Copyright (C) 2008, 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. //#define LOG_NDEBUG 0
  18. #define LOG_TAG "Camera"
  19. #include <utils/Log.h>
  20. #include <utils/threads.h>
  21. #include <utils/String16.h>
  22. #include <binder/IPCThreadState.h>
  23. #include <binder/IServiceManager.h>
  24. #include <binder/IMemory.h>
  25. #include <Camera.h>
  26. #include <ICameraRecordingProxyListener.h>
  27. #include <android/hardware/ICameraService.h>
  28. #include <android/hardware/ICamera.h>
  29. #include <gui/IGraphicBufferProducer.h>
  30. #include <gui/Surface.h>
  31. namespace android {
  32. Camera::Camera(int cameraId)
  33. : CameraBase(cameraId)
  34. {
  35. }
  36. CameraTraits<Camera>::TCamConnectService CameraTraits<Camera>::fnConnectService =
  37. &::android::hardware::ICameraService::connect;
  38. // construct a camera client from an existing camera remote
  39. sp<Camera> Camera::create(const sp<::android::hardware::ICamera>& camera)
  40. {
  41. ALOGV("create");
  42. if (camera == 0) {
  43. ALOGE("camera remote is a NULL pointer");
  44. return 0;
  45. }
  46. sp<Camera> c = new Camera(-1);
  47. if (camera->connect(c) == NO_ERROR) {
  48. c->mStatus = NO_ERROR;
  49. c->mCamera = camera;
  50. IInterface::asBinder(camera)->linkToDeath(c);
  51. return c;
  52. }
  53. return 0;
  54. }
  55. Camera::~Camera()
  56. {
  57. // We don't need to call disconnect() here because if the CameraService
  58. // thinks we are the owner of the hardware, it will hold a (strong)
  59. // reference to us, and we can't possibly be here. We also don't want to
  60. // call disconnect() here if we are in the same process as mediaserver,
  61. // because we may be invoked by CameraService::Client::connect() and will
  62. // deadlock if we call any method of ICamera here.
  63. }
  64. sp<Camera> Camera::connect(int cameraId, const String16& clientPackageName,
  65. int clientUid, int clientPid)
  66. {
  67. return CameraBaseT::connect(cameraId, clientPackageName, clientUid, clientPid);
  68. }
  69. status_t Camera::connectLegacy(int cameraId, int halVersion,
  70. const String16& clientPackageName,
  71. int clientUid,
  72. sp<Camera>& camera)
  73. {
  74. ALOGV("%s: connect legacy camera device", __FUNCTION__);
  75. sp<Camera> c = new Camera(cameraId);
  76. sp<::android::hardware::ICameraClient> cl = c;
  77. status_t status = NO_ERROR;
  78. const sp<::android::hardware::ICameraService>& cs = CameraBaseT::getCameraService();
  79. binder::Status ret;
  80. if (cs != nullptr) {
  81. ret = cs.get()->connectLegacy(cl, cameraId, halVersion, clientPackageName,
  82. clientUid, /*out*/&(c->mCamera));
  83. }
  84. if (ret.isOk() && c->mCamera != nullptr) {
  85. IInterface::asBinder(c->mCamera)->linkToDeath(c);
  86. c->mStatus = NO_ERROR;
  87. camera = c;
  88. } else {
  89. switch(ret.serviceSpecificErrorCode()) {
  90. case hardware::ICameraService::ERROR_DISCONNECTED:
  91. status = -ENODEV;
  92. break;
  93. case hardware::ICameraService::ERROR_CAMERA_IN_USE:
  94. status = -EBUSY;
  95. break;
  96. case hardware::ICameraService::ERROR_INVALID_OPERATION:
  97. status = -EINVAL;
  98. break;
  99. case hardware::ICameraService::ERROR_MAX_CAMERAS_IN_USE:
  100. status = -EUSERS;
  101. break;
  102. case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT:
  103. status = BAD_VALUE;
  104. break;
  105. case hardware::ICameraService::ERROR_DEPRECATED_HAL:
  106. status = -EOPNOTSUPP;
  107. break;
  108. case hardware::ICameraService::ERROR_DISABLED:
  109. status = -EACCES;
  110. break;
  111. case hardware::ICameraService::ERROR_PERMISSION_DENIED:
  112. status = PERMISSION_DENIED;
  113. break;
  114. default:
  115. status = -EINVAL;
  116. ALOGW("An error occurred while connecting to camera %d: %s", cameraId,
  117. (cs != nullptr) ? "Service not available" : ret.toString8().string());
  118. break;
  119. }
  120. c.clear();
  121. }
  122. return status;
  123. }
  124. status_t Camera::reconnect()
  125. {
  126. ALOGV("reconnect");
  127. sp <::android::hardware::ICamera> c = mCamera;
  128. if (c == 0) return NO_INIT;
  129. return c->connect(this);
  130. }
  131. status_t Camera::lock()
  132. {
  133. sp <::android::hardware::ICamera> c = mCamera;
  134. if (c == 0) return NO_INIT;
  135. return c->lock();
  136. }
  137. status_t Camera::unlock()
  138. {
  139. sp <::android::hardware::ICamera> c = mCamera;
  140. if (c == 0) return NO_INIT;
  141. return c->unlock();
  142. }
  143. // pass the buffered IGraphicBufferProducer to the camera service
  144. status_t Camera::setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)
  145. {
  146. ALOGV("setPreviewTarget(%p)", bufferProducer.get());
  147. sp <::android::hardware::ICamera> c = mCamera;
  148. if (c == 0) return NO_INIT;
  149. ALOGD_IF(bufferProducer == 0, "app passed NULL surface");
  150. return c->setPreviewTarget(bufferProducer);
  151. }
  152. status_t Camera::setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer)
  153. {
  154. ALOGV("setVideoTarget(%p)", bufferProducer.get());
  155. sp <::android::hardware::ICamera> c = mCamera;
  156. if (c == 0) return NO_INIT;
  157. ALOGD_IF(bufferProducer == 0, "app passed NULL video surface");
  158. return c->setVideoTarget(bufferProducer);
  159. }
  160. // start preview mode
  161. status_t Camera::startPreview()
  162. {
  163. ALOGV("startPreview");
  164. sp <::android::hardware::ICamera> c = mCamera;
  165. if (c == 0) return NO_INIT;
  166. return c->startPreview();
  167. }
  168. status_t Camera::setVideoBufferMode(int32_t videoBufferMode)
  169. {
  170. ALOGV("setVideoBufferMode: %d", videoBufferMode);
  171. sp <::android::hardware::ICamera> c = mCamera;
  172. if (c == 0) return NO_INIT;
  173. return c->setVideoBufferMode(videoBufferMode);
  174. }
  175. // start recording mode, must call setPreviewTarget first
  176. status_t Camera::startRecording()
  177. {
  178. ALOGV("startRecording");
  179. sp <::android::hardware::ICamera> c = mCamera;
  180. if (c == 0) return NO_INIT;
  181. return c->startRecording();
  182. }
  183. // stop preview mode
  184. void Camera::stopPreview()
  185. {
  186. ALOGV("stopPreview");
  187. sp <::android::hardware::ICamera> c = mCamera;
  188. if (c == 0) return;
  189. c->stopPreview();
  190. }
  191. // stop recording mode
  192. void Camera::stopRecording()
  193. {
  194. ALOGV("stopRecording");
  195. {
  196. Mutex::Autolock _l(mLock);
  197. mRecordingProxyListener.clear();
  198. }
  199. sp <::android::hardware::ICamera> c = mCamera;
  200. if (c == 0) return;
  201. c->stopRecording();
  202. }
  203. // release a recording frame
  204. void Camera::releaseRecordingFrame(const sp<IMemory>& mem)
  205. {
  206. ALOGV("releaseRecordingFrame");
  207. sp <::android::hardware::ICamera> c = mCamera;
  208. if (c == 0) return;
  209. c->releaseRecordingFrame(mem);
  210. }
  211. void Camera::releaseRecordingFrameHandle(native_handle_t* handle)
  212. {
  213. ALOGV("releaseRecordingFrameHandle");
  214. sp <::android::hardware::ICamera> c = mCamera;
  215. if (c == 0) return;
  216. c->releaseRecordingFrameHandle(handle);
  217. }
  218. void Camera::releaseRecordingFrameHandleBatch(
  219. const std::vector<native_handle_t*> handles) {
  220. ALOGV("releaseRecordingFrameHandleBatch");
  221. sp <::android::hardware::ICamera> c = mCamera;
  222. if (c == 0) return;
  223. c->releaseRecordingFrameHandleBatch(handles);
  224. }
  225. // get preview state
  226. bool Camera::previewEnabled()
  227. {
  228. ALOGV("previewEnabled");
  229. sp <::android::hardware::ICamera> c = mCamera;
  230. if (c == 0) return false;
  231. return c->previewEnabled();
  232. }
  233. // get recording state
  234. bool Camera::recordingEnabled()
  235. {
  236. ALOGV("recordingEnabled");
  237. sp <::android::hardware::ICamera> c = mCamera;
  238. if (c == 0) return false;
  239. return c->recordingEnabled();
  240. }
  241. status_t Camera::autoFocus()
  242. {
  243. ALOGV("autoFocus");
  244. sp <::android::hardware::ICamera> c = mCamera;
  245. if (c == 0) return NO_INIT;
  246. return c->autoFocus();
  247. }
  248. status_t Camera::cancelAutoFocus()
  249. {
  250. ALOGV("cancelAutoFocus");
  251. sp <::android::hardware::ICamera> c = mCamera;
  252. if (c == 0) return NO_INIT;
  253. return c->cancelAutoFocus();
  254. }
  255. // take a picture
  256. status_t Camera::takePicture(int msgType)
  257. {
  258. ALOGV("takePicture: 0x%x", msgType);
  259. sp <::android::hardware::ICamera> c = mCamera;
  260. if (c == 0) return NO_INIT;
  261. return c->takePicture(msgType);
  262. }
  263. // set preview/capture parameters - key/value pairs
  264. status_t Camera::setParameters(const String8& params)
  265. {
  266. ALOGV("setParameters");
  267. sp <::android::hardware::ICamera> c = mCamera;
  268. if (c == 0) return NO_INIT;
  269. return c->setParameters(params);
  270. }
  271. // get preview/capture parameters - key/value pairs
  272. String8 Camera::getParameters() const
  273. {
  274. ALOGV("getParameters");
  275. String8 params;
  276. sp <::android::hardware::ICamera> c = mCamera;
  277. if (c != 0) params = mCamera->getParameters();
  278. return params;
  279. }
  280. // send command to camera driver
  281. status_t Camera::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
  282. {
  283. ALOGV("sendCommand");
  284. sp <::android::hardware::ICamera> c = mCamera;
  285. if (c == 0) return NO_INIT;
  286. return c->sendCommand(cmd, arg1, arg2);
  287. }
  288. void Camera::setListener(const sp<CameraListener>& listener)
  289. {
  290. Mutex::Autolock _l(mLock);
  291. mListener = listener;
  292. }
  293. void Camera::setRecordingProxyListener(const sp<ICameraRecordingProxyListener>& listener)
  294. {
  295. Mutex::Autolock _l(mLock);
  296. mRecordingProxyListener = listener;
  297. }
  298. void Camera::setPreviewCallbackFlags(int flag)
  299. {
  300. ALOGV("setPreviewCallbackFlags");
  301. sp <::android::hardware::ICamera> c = mCamera;
  302. if (c == 0) return;
  303. mCamera->setPreviewCallbackFlag(flag);
  304. }
  305. status_t Camera::setPreviewCallbackTarget(
  306. const sp<IGraphicBufferProducer>& callbackProducer)
  307. {
  308. sp <::android::hardware::ICamera> c = mCamera;
  309. if (c == 0) return NO_INIT;
  310. return c->setPreviewCallbackTarget(callbackProducer);
  311. }
  312. // callback from camera service
  313. void Camera::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
  314. {
  315. return CameraBaseT::notifyCallback(msgType, ext1, ext2);
  316. }
  317. // callback from camera service when frame or image is ready
  318. void Camera::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
  319. camera_frame_metadata_t *metadata)
  320. {
  321. sp<CameraListener> listener;
  322. {
  323. Mutex::Autolock _l(mLock);
  324. listener = mListener;
  325. }
  326. if (listener != NULL) {
  327. listener->postData(msgType, dataPtr, metadata);
  328. }
  329. }
  330. // callback from camera service when timestamped frame is ready
  331. void Camera::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
  332. {
  333. // If recording proxy listener is registered, forward the frame and return.
  334. // The other listener (mListener) is ignored because the receiver needs to
  335. // call releaseRecordingFrame.
  336. sp<ICameraRecordingProxyListener> proxylistener;
  337. {
  338. Mutex::Autolock _l(mLock);
  339. proxylistener = mRecordingProxyListener;
  340. }
  341. if (proxylistener != NULL) {
  342. proxylistener->dataCallbackTimestamp(timestamp, msgType, dataPtr);
  343. return;
  344. }
  345. sp<CameraListener> listener;
  346. {
  347. Mutex::Autolock _l(mLock);
  348. listener = mListener;
  349. }
  350. if (listener != NULL) {
  351. listener->postDataTimestamp(timestamp, msgType, dataPtr);
  352. } else {
  353. ALOGW("No listener was set. Drop a recording frame.");
  354. releaseRecordingFrame(dataPtr);
  355. }
  356. }
  357. void Camera::recordingFrameHandleCallbackTimestamp(nsecs_t timestamp, native_handle_t* handle)
  358. {
  359. // If recording proxy listener is registered, forward the frame and return.
  360. // The other listener (mListener) is ignored because the receiver needs to
  361. // call releaseRecordingFrameHandle.
  362. sp<ICameraRecordingProxyListener> proxylistener;
  363. {
  364. Mutex::Autolock _l(mLock);
  365. proxylistener = mRecordingProxyListener;
  366. }
  367. if (proxylistener != NULL) {
  368. proxylistener->recordingFrameHandleCallbackTimestamp(timestamp, handle);
  369. return;
  370. }
  371. sp<CameraListener> listener;
  372. {
  373. Mutex::Autolock _l(mLock);
  374. listener = mListener;
  375. }
  376. if (listener != NULL) {
  377. listener->postRecordingFrameHandleTimestamp(timestamp, handle);
  378. } else {
  379. ALOGW("No listener was set. Drop a recording frame.");
  380. releaseRecordingFrameHandle(handle);
  381. }
  382. }
  383. void Camera::recordingFrameHandleCallbackTimestampBatch(
  384. const std::vector<nsecs_t>& timestamps,
  385. const std::vector<native_handle_t*>& handles)
  386. {
  387. // If recording proxy listener is registered, forward the frame and return.
  388. // The other listener (mListener) is ignored because the receiver needs to
  389. // call releaseRecordingFrameHandle.
  390. sp<ICameraRecordingProxyListener> proxylistener;
  391. {
  392. Mutex::Autolock _l(mLock);
  393. proxylistener = mRecordingProxyListener;
  394. }
  395. if (proxylistener != NULL) {
  396. proxylistener->recordingFrameHandleCallbackTimestampBatch(timestamps, handles);
  397. return;
  398. }
  399. sp<CameraListener> listener;
  400. {
  401. Mutex::Autolock _l(mLock);
  402. listener = mListener;
  403. }
  404. if (listener != NULL) {
  405. listener->postRecordingFrameHandleTimestampBatch(timestamps, handles);
  406. } else {
  407. ALOGW("No listener was set. Drop a batch of recording frames.");
  408. releaseRecordingFrameHandleBatch(handles);
  409. }
  410. }
  411. sp<ICameraRecordingProxy> Camera::getRecordingProxy() {
  412. ALOGV("getProxy");
  413. return new RecordingProxy(this);
  414. }
  415. status_t Camera::RecordingProxy::startRecording(const sp<ICameraRecordingProxyListener>& listener)
  416. {
  417. ALOGV("RecordingProxy::startRecording");
  418. mCamera->setRecordingProxyListener(listener);
  419. mCamera->reconnect();
  420. return mCamera->startRecording();
  421. }
  422. void Camera::RecordingProxy::stopRecording()
  423. {
  424. ALOGV("RecordingProxy::stopRecording");
  425. mCamera->stopRecording();
  426. }
  427. void Camera::RecordingProxy::releaseRecordingFrame(const sp<IMemory>& mem)
  428. {
  429. ALOGV("RecordingProxy::releaseRecordingFrame");
  430. mCamera->releaseRecordingFrame(mem);
  431. }
  432. void Camera::RecordingProxy::releaseRecordingFrameHandle(native_handle_t* handle) {
  433. ALOGV("RecordingProxy::releaseRecordingFrameHandle");
  434. mCamera->releaseRecordingFrameHandle(handle);
  435. }
  436. void Camera::RecordingProxy::releaseRecordingFrameHandleBatch(
  437. const std::vector<native_handle_t*>& handles) {
  438. ALOGV("RecordingProxy::releaseRecordingFrameHandleBatch");
  439. mCamera->releaseRecordingFrameHandleBatch(handles);
  440. }
  441. Camera::RecordingProxy::RecordingProxy(const sp<Camera>& camera)
  442. {
  443. mCamera = camera;
  444. }
  445. }; // namespace android