PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/frameworks/av/services/camera/libcameraservice/CameraClient.cpp

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk
C++ | 959 lines | 692 code | 156 blank | 111 comment | 215 complexity | 5ae71b4bde399d524707c83b7fbfd699 MD5 | raw file
  1. /*
  2. * Copyright (C) 2012 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #define LOG_TAG "CameraClient"
  17. //#define LOG_NDEBUG 0
  18. #include <cutils/properties.h>
  19. #include <gui/SurfaceTextureClient.h>
  20. #include <gui/Surface.h>
  21. #include "CameraClient.h"
  22. #include "CameraHardwareInterface.h"
  23. #include "CameraService.h"
  24. namespace android {
  25. #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
  26. #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
  27. static int getCallingPid() {
  28. return IPCThreadState::self()->getCallingPid();
  29. }
  30. static int getCallingUid() {
  31. return IPCThreadState::self()->getCallingUid();
  32. }
  33. CameraClient::CameraClient(const sp<CameraService>& cameraService,
  34. const sp<ICameraClient>& cameraClient,
  35. int cameraId, int cameraFacing, int clientPid, int servicePid):
  36. Client(cameraService, cameraClient,
  37. cameraId, cameraFacing, clientPid, servicePid)
  38. {
  39. int callingPid = getCallingPid();
  40. LOG1("CameraClient::CameraClient E (pid %d, id %d)", callingPid, cameraId);
  41. mHardware = NULL;
  42. mMsgEnabled = 0;
  43. mSurface = 0;
  44. mPreviewWindow = 0;
  45. mDestructionStarted = false;
  46. // Callback is disabled by default
  47. mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
  48. mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
  49. mPlayShutterSound = true;
  50. LOG1("CameraClient::CameraClient X (pid %d, id %d)", callingPid, cameraId);
  51. }
  52. status_t CameraClient::initialize(camera_module_t *module) {
  53. int callingPid = getCallingPid();
  54. LOG1("CameraClient::initialize E (pid %d, id %d)", callingPid, mCameraId);
  55. char camera_device_name[10];
  56. status_t res;
  57. snprintf(camera_device_name, sizeof(camera_device_name), "%d", mCameraId);
  58. mHardware = new CameraHardwareInterface(camera_device_name);
  59. res = mHardware->initialize(&module->common);
  60. if (res != OK) {
  61. ALOGE("%s: Camera %d: unable to initialize device: %s (%d)",
  62. __FUNCTION__, mCameraId, strerror(-res), res);
  63. mHardware.clear();
  64. return NO_INIT;
  65. }
  66. mHardware->setCallbacks(notifyCallback,
  67. dataCallback,
  68. dataCallbackTimestamp,
  69. (void *)mCameraId);
  70. // Enable zoom, error, focus, and metadata messages by default
  71. enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS |
  72. CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE);
  73. LOG1("CameraClient::initialize X (pid %d, id %d)", callingPid, mCameraId);
  74. return OK;
  75. }
  76. // tear down the client
  77. CameraClient::~CameraClient() {
  78. // this lock should never be NULL
  79. Mutex* lock = mCameraService->getClientLockById(mCameraId);
  80. lock->lock();
  81. mDestructionStarted = true;
  82. // client will not be accessed from callback. should unlock to prevent dead-lock in disconnect
  83. lock->unlock();
  84. int callingPid = getCallingPid();
  85. LOG1("CameraClient::~CameraClient E (pid %d, this %p)", callingPid, this);
  86. disconnect();
  87. LOG1("CameraClient::~CameraClient X (pid %d, this %p)", callingPid, this);
  88. }
  89. status_t CameraClient::dump(int fd, const Vector<String16>& args) {
  90. const size_t SIZE = 256;
  91. char buffer[SIZE];
  92. size_t len = snprintf(buffer, SIZE, "Client[%d] (%p) PID: %d\n",
  93. mCameraId,
  94. getCameraClient()->asBinder().get(),
  95. mClientPid);
  96. len = (len > SIZE - 1) ? SIZE - 1 : len;
  97. write(fd, buffer, len);
  98. return mHardware->dump(fd, args);
  99. }
  100. // ----------------------------------------------------------------------------
  101. status_t CameraClient::checkPid() const {
  102. int callingPid = getCallingPid();
  103. if (callingPid == mClientPid) return NO_ERROR;
  104. ALOGW("attempt to use a locked camera from a different process"
  105. " (old pid %d, new pid %d)", mClientPid, callingPid);
  106. return EBUSY;
  107. }
  108. status_t CameraClient::checkPidAndHardware() const {
  109. status_t result = checkPid();
  110. if (result != NO_ERROR) return result;
  111. if (mHardware == 0) {
  112. ALOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
  113. return INVALID_OPERATION;
  114. }
  115. return NO_ERROR;
  116. }
  117. status_t CameraClient::lock() {
  118. int callingPid = getCallingPid();
  119. LOG1("lock (pid %d)", callingPid);
  120. Mutex::Autolock lock(mLock);
  121. // lock camera to this client if the the camera is unlocked
  122. if (mClientPid == 0) {
  123. mClientPid = callingPid;
  124. return NO_ERROR;
  125. }
  126. // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
  127. return checkPid();
  128. }
  129. status_t CameraClient::unlock() {
  130. int callingPid = getCallingPid();
  131. LOG1("unlock (pid %d)", callingPid);
  132. Mutex::Autolock lock(mLock);
  133. // allow anyone to use camera (after they lock the camera)
  134. status_t result = checkPid();
  135. if (result == NO_ERROR) {
  136. if (mHardware->recordingEnabled()) {
  137. ALOGE("Not allowed to unlock camera during recording.");
  138. return INVALID_OPERATION;
  139. }
  140. mClientPid = 0;
  141. LOG1("clear mCameraClient (pid %d)", callingPid);
  142. // we need to remove the reference to ICameraClient so that when the app
  143. // goes away, the reference count goes to 0.
  144. mCameraClient.clear();
  145. }
  146. return result;
  147. }
  148. // connect a new client to the camera
  149. status_t CameraClient::connect(const sp<ICameraClient>& client) {
  150. int callingPid = getCallingPid();
  151. LOG1("connect E (pid %d)", callingPid);
  152. Mutex::Autolock lock(mLock);
  153. if (mClientPid != 0 && checkPid() != NO_ERROR) {
  154. ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
  155. mClientPid, callingPid);
  156. return EBUSY;
  157. }
  158. if (mCameraClient != 0 && (client->asBinder() == mCameraClient->asBinder())) {
  159. LOG1("Connect to the same client");
  160. return NO_ERROR;
  161. }
  162. mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
  163. mClientPid = callingPid;
  164. mCameraClient = client;
  165. LOG1("connect X (pid %d)", callingPid);
  166. return NO_ERROR;
  167. }
  168. static void disconnectWindow(const sp<ANativeWindow>& window) {
  169. if (window != 0) {
  170. status_t result = native_window_api_disconnect(window.get(),
  171. NATIVE_WINDOW_API_CAMERA);
  172. if (result != NO_ERROR) {
  173. ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
  174. result);
  175. }
  176. }
  177. }
  178. void CameraClient::disconnect() {
  179. int callingPid = getCallingPid();
  180. LOG1("disconnect E (pid %d)", callingPid);
  181. Mutex::Autolock lock(mLock);
  182. // Allow both client and the media server to disconnect at all times
  183. if (callingPid != mClientPid && callingPid != mServicePid) {
  184. ALOGW("different client - don't disconnect");
  185. return;
  186. }
  187. if (mClientPid <= 0) {
  188. LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
  189. return;
  190. }
  191. // Make sure disconnect() is done once and once only, whether it is called
  192. // from the user directly, or called by the destructor.
  193. if (mHardware == 0) return;
  194. LOG1("hardware teardown");
  195. // Before destroying mHardware, we must make sure it's in the
  196. // idle state.
  197. // Turn off all messages.
  198. disableMsgType(CAMERA_MSG_ALL_MSGS);
  199. mHardware->stopPreview();
  200. mHardware->cancelPicture();
  201. // Release the hardware resources.
  202. mHardware->release();
  203. // Release the held ANativeWindow resources.
  204. if (mPreviewWindow != 0) {
  205. disconnectWindow(mPreviewWindow);
  206. mPreviewWindow = 0;
  207. mHardware->setPreviewWindow(mPreviewWindow);
  208. }
  209. mHardware.clear();
  210. CameraService::Client::disconnect();
  211. LOG1("disconnect X (pid %d)", callingPid);
  212. }
  213. // ----------------------------------------------------------------------------
  214. status_t CameraClient::setPreviewWindow(const sp<IBinder>& binder,
  215. const sp<ANativeWindow>& window) {
  216. Mutex::Autolock lock(mLock);
  217. status_t result = checkPidAndHardware();
  218. if (result != NO_ERROR) return result;
  219. // return if no change in surface.
  220. if (binder == mSurface) {
  221. return NO_ERROR;
  222. }
  223. if (window != 0) {
  224. result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
  225. if (result != NO_ERROR) {
  226. ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
  227. result);
  228. return result;
  229. }
  230. }
  231. // If preview has been already started, register preview buffers now.
  232. if (mHardware->previewEnabled()) {
  233. if (window != 0) {
  234. native_window_set_scaling_mode(window.get(),
  235. NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
  236. native_window_set_buffers_transform(window.get(), mOrientation);
  237. result = mHardware->setPreviewWindow(window);
  238. }
  239. }
  240. if (result == NO_ERROR) {
  241. // Everything has succeeded. Disconnect the old window and remember the
  242. // new window.
  243. disconnectWindow(mPreviewWindow);
  244. mSurface = binder;
  245. mPreviewWindow = window;
  246. } else {
  247. // Something went wrong after we connected to the new window, so
  248. // disconnect here.
  249. disconnectWindow(window);
  250. }
  251. return result;
  252. }
  253. // set the Surface that the preview will use
  254. status_t CameraClient::setPreviewDisplay(const sp<Surface>& surface) {
  255. LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid());
  256. sp<IBinder> binder(surface != 0 ? surface->asBinder() : 0);
  257. sp<ANativeWindow> window(surface);
  258. return setPreviewWindow(binder, window);
  259. }
  260. // set the SurfaceTexture that the preview will use
  261. status_t CameraClient::setPreviewTexture(
  262. const sp<ISurfaceTexture>& surfaceTexture) {
  263. LOG1("setPreviewTexture(%p) (pid %d)", surfaceTexture.get(),
  264. getCallingPid());
  265. sp<IBinder> binder;
  266. sp<ANativeWindow> window;
  267. if (surfaceTexture != 0) {
  268. binder = surfaceTexture->asBinder();
  269. window = new SurfaceTextureClient(surfaceTexture);
  270. }
  271. return setPreviewWindow(binder, window);
  272. }
  273. // set the preview callback flag to affect how the received frames from
  274. // preview are handled.
  275. void CameraClient::setPreviewCallbackFlag(int callback_flag) {
  276. LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
  277. Mutex::Autolock lock(mLock);
  278. if (checkPidAndHardware() != NO_ERROR) return;
  279. mPreviewCallbackFlag = callback_flag;
  280. if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
  281. enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
  282. } else {
  283. disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
  284. }
  285. }
  286. // start preview mode
  287. status_t CameraClient::startPreview() {
  288. LOG1("startPreview (pid %d)", getCallingPid());
  289. return startCameraMode(CAMERA_PREVIEW_MODE);
  290. }
  291. // start recording mode
  292. status_t CameraClient::startRecording() {
  293. LOG1("startRecording (pid %d)", getCallingPid());
  294. return startCameraMode(CAMERA_RECORDING_MODE);
  295. }
  296. // start preview or recording
  297. status_t CameraClient::startCameraMode(camera_mode mode) {
  298. LOG1("startCameraMode(%d)", mode);
  299. Mutex::Autolock lock(mLock);
  300. status_t result = checkPidAndHardware();
  301. if (result != NO_ERROR) return result;
  302. switch(mode) {
  303. case CAMERA_PREVIEW_MODE:
  304. if (mSurface == 0 && mPreviewWindow == 0) {
  305. LOG1("mSurface is not set yet.");
  306. // still able to start preview in this case.
  307. }
  308. return startPreviewMode();
  309. case CAMERA_RECORDING_MODE:
  310. if (mSurface == 0 && mPreviewWindow == 0) {
  311. ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
  312. return INVALID_OPERATION;
  313. }
  314. return startRecordingMode();
  315. default:
  316. return UNKNOWN_ERROR;
  317. }
  318. }
  319. status_t CameraClient::startPreviewMode() {
  320. LOG1("startPreviewMode");
  321. status_t result = NO_ERROR;
  322. // if preview has been enabled, nothing needs to be done
  323. if (mHardware->previewEnabled()) {
  324. return NO_ERROR;
  325. }
  326. if (mPreviewWindow != 0) {
  327. native_window_set_scaling_mode(mPreviewWindow.get(),
  328. NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
  329. native_window_set_buffers_transform(mPreviewWindow.get(),
  330. mOrientation);
  331. }
  332. mHardware->setPreviewWindow(mPreviewWindow);
  333. result = mHardware->startPreview();
  334. return result;
  335. }
  336. status_t CameraClient::startRecordingMode() {
  337. LOG1("startRecordingMode");
  338. status_t result = NO_ERROR;
  339. // if recording has been enabled, nothing needs to be done
  340. if (mHardware->recordingEnabled()) {
  341. return NO_ERROR;
  342. }
  343. // if preview has not been started, start preview first
  344. if (!mHardware->previewEnabled()) {
  345. result = startPreviewMode();
  346. if (result != NO_ERROR) {
  347. return result;
  348. }
  349. }
  350. // start recording mode
  351. enableMsgType(CAMERA_MSG_VIDEO_FRAME);
  352. mCameraService->playSound(CameraService::SOUND_RECORDING);
  353. result = mHardware->startRecording();
  354. if (result != NO_ERROR) {
  355. ALOGE("mHardware->startRecording() failed with status %d", result);
  356. }
  357. return result;
  358. }
  359. // stop preview mode
  360. void CameraClient::stopPreview() {
  361. LOG1("stopPreview (pid %d)", getCallingPid());
  362. Mutex::Autolock lock(mLock);
  363. if (checkPidAndHardware() != NO_ERROR) return;
  364. disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
  365. mHardware->stopPreview();
  366. mPreviewBuffer.clear();
  367. }
  368. // stop recording mode
  369. void CameraClient::stopRecording() {
  370. LOG1("stopRecording (pid %d)", getCallingPid());
  371. Mutex::Autolock lock(mLock);
  372. if (checkPidAndHardware() != NO_ERROR) return;
  373. disableMsgType(CAMERA_MSG_VIDEO_FRAME);
  374. mHardware->stopRecording();
  375. mCameraService->playSound(CameraService::SOUND_RECORDING);
  376. mPreviewBuffer.clear();
  377. }
  378. // release a recording frame
  379. void CameraClient::releaseRecordingFrame(const sp<IMemory>& mem) {
  380. Mutex::Autolock lock(mLock);
  381. if (checkPidAndHardware() != NO_ERROR) return;
  382. mHardware->releaseRecordingFrame(mem);
  383. }
  384. status_t CameraClient::storeMetaDataInBuffers(bool enabled)
  385. {
  386. LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
  387. Mutex::Autolock lock(mLock);
  388. if (checkPidAndHardware() != NO_ERROR) {
  389. return UNKNOWN_ERROR;
  390. }
  391. return mHardware->storeMetaDataInBuffers(enabled);
  392. }
  393. bool CameraClient::previewEnabled() {
  394. LOG1("previewEnabled (pid %d)", getCallingPid());
  395. Mutex::Autolock lock(mLock);
  396. if (checkPidAndHardware() != NO_ERROR) return false;
  397. return mHardware->previewEnabled();
  398. }
  399. bool CameraClient::recordingEnabled() {
  400. LOG1("recordingEnabled (pid %d)", getCallingPid());
  401. Mutex::Autolock lock(mLock);
  402. if (checkPidAndHardware() != NO_ERROR) return false;
  403. return mHardware->recordingEnabled();
  404. }
  405. status_t CameraClient::autoFocus() {
  406. LOG1("autoFocus (pid %d)", getCallingPid());
  407. Mutex::Autolock lock(mLock);
  408. status_t result = checkPidAndHardware();
  409. if (result != NO_ERROR) return result;
  410. return mHardware->autoFocus();
  411. }
  412. status_t CameraClient::cancelAutoFocus() {
  413. LOG1("cancelAutoFocus (pid %d)", getCallingPid());
  414. Mutex::Autolock lock(mLock);
  415. status_t result = checkPidAndHardware();
  416. if (result != NO_ERROR) return result;
  417. return mHardware->cancelAutoFocus();
  418. }
  419. // take a picture - image is returned in callback
  420. status_t CameraClient::takePicture(int msgType) {
  421. LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
  422. Mutex::Autolock lock(mLock);
  423. status_t result = checkPidAndHardware();
  424. if (result != NO_ERROR) return result;
  425. if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
  426. (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
  427. ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
  428. " cannot be both enabled");
  429. return BAD_VALUE;
  430. }
  431. // We only accept picture related message types
  432. // and ignore other types of messages for takePicture().
  433. int picMsgType = msgType
  434. & (CAMERA_MSG_SHUTTER |
  435. CAMERA_MSG_POSTVIEW_FRAME |
  436. CAMERA_MSG_RAW_IMAGE |
  437. CAMERA_MSG_RAW_IMAGE_NOTIFY |
  438. CAMERA_MSG_COMPRESSED_IMAGE);
  439. enableMsgType(picMsgType);
  440. return mHardware->takePicture();
  441. }
  442. // set preview/capture parameters - key/value pairs
  443. status_t CameraClient::setParameters(const String8& params) {
  444. LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
  445. Mutex::Autolock lock(mLock);
  446. status_t result = checkPidAndHardware();
  447. if (result != NO_ERROR) return result;
  448. CameraParameters p(params);
  449. return mHardware->setParameters(p);
  450. }
  451. // get preview/capture parameters - key/value pairs
  452. String8 CameraClient::getParameters() const {
  453. Mutex::Autolock lock(mLock);
  454. if (checkPidAndHardware() != NO_ERROR) return String8();
  455. String8 params(mHardware->getParameters().flatten());
  456. LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
  457. return params;
  458. }
  459. // enable shutter sound
  460. status_t CameraClient::enableShutterSound(bool enable) {
  461. LOG1("enableShutterSound (pid %d)", getCallingPid());
  462. status_t result = checkPidAndHardware();
  463. if (result != NO_ERROR) return result;
  464. if (enable) {
  465. mPlayShutterSound = true;
  466. return OK;
  467. }
  468. // Disabling shutter sound may not be allowed. In that case only
  469. // allow the mediaserver process to disable the sound.
  470. char value[PROPERTY_VALUE_MAX];
  471. property_get("ro.camera.sound.forced", value, "0");
  472. if (strcmp(value, "0") != 0) {
  473. // Disabling shutter sound is not allowed. Deny if the current
  474. // process is not mediaserver.
  475. if (getCallingPid() != getpid()) {
  476. ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
  477. return PERMISSION_DENIED;
  478. }
  479. }
  480. mPlayShutterSound = false;
  481. return OK;
  482. }
  483. status_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
  484. LOG1("sendCommand (pid %d)", getCallingPid());
  485. int orientation;
  486. Mutex::Autolock lock(mLock);
  487. status_t result = checkPidAndHardware();
  488. if (result != NO_ERROR) return result;
  489. if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
  490. // Mirror the preview if the camera is front-facing.
  491. orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
  492. if (orientation == -1) return BAD_VALUE;
  493. if (mOrientation != orientation) {
  494. mOrientation = orientation;
  495. if (mPreviewWindow != 0) {
  496. native_window_set_buffers_transform(mPreviewWindow.get(),
  497. mOrientation);
  498. }
  499. }
  500. return OK;
  501. } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
  502. switch (arg1) {
  503. case 0:
  504. return enableShutterSound(false);
  505. case 1:
  506. return enableShutterSound(true);
  507. default:
  508. return BAD_VALUE;
  509. }
  510. return OK;
  511. } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
  512. mCameraService->playSound(CameraService::SOUND_RECORDING);
  513. } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) {
  514. // Silently ignore this command
  515. return INVALID_OPERATION;
  516. } else if (cmd == CAMERA_CMD_PING) {
  517. // If mHardware is 0, checkPidAndHardware will return error.
  518. return OK;
  519. }
  520. return mHardware->sendCommand(cmd, arg1, arg2);
  521. }
  522. // ----------------------------------------------------------------------------
  523. void CameraClient::enableMsgType(int32_t msgType) {
  524. android_atomic_or(msgType, &mMsgEnabled);
  525. mHardware->enableMsgType(msgType);
  526. }
  527. void CameraClient::disableMsgType(int32_t msgType) {
  528. android_atomic_and(~msgType, &mMsgEnabled);
  529. mHardware->disableMsgType(msgType);
  530. }
  531. #define CHECK_MESSAGE_INTERVAL 10 // 10ms
  532. bool CameraClient::lockIfMessageWanted(int32_t msgType) {
  533. int sleepCount = 0;
  534. while (mMsgEnabled & msgType) {
  535. if (mLock.tryLock() == NO_ERROR) {
  536. if (sleepCount > 0) {
  537. LOG1("lockIfMessageWanted(%d): waited for %d ms",
  538. msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
  539. }
  540. return true;
  541. }
  542. if (sleepCount++ == 0) {
  543. LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
  544. }
  545. usleep(CHECK_MESSAGE_INTERVAL * 1000);
  546. }
  547. ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
  548. return false;
  549. }
  550. // Callback messages can be dispatched to internal handlers or pass to our
  551. // client's callback functions, depending on the message type.
  552. //
  553. // notifyCallback:
  554. // CAMERA_MSG_SHUTTER handleShutter
  555. // (others) c->notifyCallback
  556. // dataCallback:
  557. // CAMERA_MSG_PREVIEW_FRAME handlePreviewData
  558. // CAMERA_MSG_POSTVIEW_FRAME handlePostview
  559. // CAMERA_MSG_RAW_IMAGE handleRawPicture
  560. // CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
  561. // (others) c->dataCallback
  562. // dataCallbackTimestamp
  563. // (others) c->dataCallbackTimestamp
  564. //
  565. // NOTE: the *Callback functions grab mLock of the client before passing
  566. // control to handle* functions. So the handle* functions must release the
  567. // lock before calling the ICameraClient's callbacks, so those callbacks can
  568. // invoke methods in the Client class again (For example, the preview frame
  569. // callback may want to releaseRecordingFrame). The handle* functions must
  570. // release the lock after all accesses to member variables, so it must be
  571. // handled very carefully.
  572. void CameraClient::notifyCallback(int32_t msgType, int32_t ext1,
  573. int32_t ext2, void* user) {
  574. LOG2("notifyCallback(%d)", msgType);
  575. Mutex* lock = getClientLockFromCookie(user);
  576. if (lock == NULL) return;
  577. Mutex::Autolock alock(*lock);
  578. CameraClient* client =
  579. static_cast<CameraClient*>(getClientFromCookie(user));
  580. if (client == NULL) return;
  581. if (!client->lockIfMessageWanted(msgType)) return;
  582. switch (msgType) {
  583. case CAMERA_MSG_SHUTTER:
  584. // ext1 is the dimension of the yuv picture.
  585. client->handleShutter();
  586. break;
  587. default:
  588. client->handleGenericNotify(msgType, ext1, ext2);
  589. break;
  590. }
  591. }
  592. void CameraClient::dataCallback(int32_t msgType,
  593. const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
  594. LOG2("dataCallback(%d)", msgType);
  595. Mutex* lock = getClientLockFromCookie(user);
  596. if (lock == NULL) return;
  597. Mutex::Autolock alock(*lock);
  598. CameraClient* client =
  599. static_cast<CameraClient*>(getClientFromCookie(user));
  600. if (client == NULL) return;
  601. if (!client->lockIfMessageWanted(msgType)) return;
  602. if (dataPtr == 0 && metadata == NULL) {
  603. ALOGE("Null data returned in data callback");
  604. client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
  605. return;
  606. }
  607. switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
  608. case CAMERA_MSG_PREVIEW_FRAME:
  609. client->handlePreviewData(msgType, dataPtr, metadata);
  610. break;
  611. case CAMERA_MSG_POSTVIEW_FRAME:
  612. client->handlePostview(dataPtr);
  613. break;
  614. case CAMERA_MSG_RAW_IMAGE:
  615. client->handleRawPicture(dataPtr);
  616. break;
  617. case CAMERA_MSG_COMPRESSED_IMAGE:
  618. client->handleCompressedPicture(dataPtr);
  619. break;
  620. default:
  621. client->handleGenericData(msgType, dataPtr, metadata);
  622. break;
  623. }
  624. }
  625. void CameraClient::dataCallbackTimestamp(nsecs_t timestamp,
  626. int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
  627. LOG2("dataCallbackTimestamp(%d)", msgType);
  628. Mutex* lock = getClientLockFromCookie(user);
  629. if (lock == NULL) return;
  630. Mutex::Autolock alock(*lock);
  631. CameraClient* client =
  632. static_cast<CameraClient*>(getClientFromCookie(user));
  633. if (client == NULL) return;
  634. if (!client->lockIfMessageWanted(msgType)) return;
  635. if (dataPtr == 0) {
  636. ALOGE("Null data returned in data with timestamp callback");
  637. client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
  638. return;
  639. }
  640. client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
  641. }
  642. // snapshot taken callback
  643. void CameraClient::handleShutter(void) {
  644. if (mPlayShutterSound) {
  645. mCameraService->playSound(CameraService::SOUND_SHUTTER);
  646. }
  647. sp<ICameraClient> c = mCameraClient;
  648. if (c != 0) {
  649. mLock.unlock();
  650. c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
  651. if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
  652. }
  653. disableMsgType(CAMERA_MSG_SHUTTER);
  654. mLock.unlock();
  655. }
  656. // preview callback - frame buffer update
  657. void CameraClient::handlePreviewData(int32_t msgType,
  658. const sp<IMemory>& mem,
  659. camera_frame_metadata_t *metadata) {
  660. ssize_t offset;
  661. size_t size;
  662. sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
  663. // local copy of the callback flags
  664. int flags = mPreviewCallbackFlag;
  665. // is callback enabled?
  666. if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
  667. // If the enable bit is off, the copy-out and one-shot bits are ignored
  668. LOG2("frame callback is disabled");
  669. mLock.unlock();
  670. return;
  671. }
  672. // hold a strong pointer to the client
  673. sp<ICameraClient> c = mCameraClient;
  674. // clear callback flags if no client or one-shot mode
  675. if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
  676. LOG2("Disable preview callback");
  677. mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
  678. CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
  679. CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
  680. disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
  681. }
  682. if (c != 0) {
  683. // Is the received frame copied out or not?
  684. if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
  685. LOG2("frame is copied");
  686. copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
  687. } else {
  688. LOG2("frame is forwarded");
  689. mLock.unlock();
  690. c->dataCallback(msgType, mem, metadata);
  691. }
  692. } else {
  693. mLock.unlock();
  694. }
  695. }
  696. // picture callback - postview image ready
  697. void CameraClient::handlePostview(const sp<IMemory>& mem) {
  698. disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
  699. sp<ICameraClient> c = mCameraClient;
  700. mLock.unlock();
  701. if (c != 0) {
  702. c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
  703. }
  704. }
  705. // picture callback - raw image ready
  706. void CameraClient::handleRawPicture(const sp<IMemory>& mem) {
  707. disableMsgType(CAMERA_MSG_RAW_IMAGE);
  708. ssize_t offset;
  709. size_t size;
  710. sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
  711. sp<ICameraClient> c = mCameraClient;
  712. mLock.unlock();
  713. if (c != 0) {
  714. c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
  715. }
  716. }
  717. // picture callback - compressed picture ready
  718. void CameraClient::handleCompressedPicture(const sp<IMemory>& mem) {
  719. disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
  720. sp<ICameraClient> c = mCameraClient;
  721. mLock.unlock();
  722. if (c != 0) {
  723. c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
  724. }
  725. }
  726. void CameraClient::handleGenericNotify(int32_t msgType,
  727. int32_t ext1, int32_t ext2) {
  728. sp<ICameraClient> c = mCameraClient;
  729. mLock.unlock();
  730. if (c != 0) {
  731. c->notifyCallback(msgType, ext1, ext2);
  732. }
  733. }
  734. void CameraClient::handleGenericData(int32_t msgType,
  735. const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
  736. sp<ICameraClient> c = mCameraClient;
  737. mLock.unlock();
  738. if (c != 0) {
  739. c->dataCallback(msgType, dataPtr, metadata);
  740. }
  741. }
  742. void CameraClient::handleGenericDataTimestamp(nsecs_t timestamp,
  743. int32_t msgType, const sp<IMemory>& dataPtr) {
  744. sp<ICameraClient> c = mCameraClient;
  745. mLock.unlock();
  746. if (c != 0) {
  747. c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
  748. }
  749. }
  750. void CameraClient::copyFrameAndPostCopiedFrame(
  751. int32_t msgType, const sp<ICameraClient>& client,
  752. const sp<IMemoryHeap>& heap, size_t offset, size_t size,
  753. camera_frame_metadata_t *metadata) {
  754. LOG2("copyFrameAndPostCopiedFrame");
  755. // It is necessary to copy out of pmem before sending this to
  756. // the callback. For efficiency, reuse the same MemoryHeapBase
  757. // provided it's big enough. Don't allocate the memory or
  758. // perform the copy if there's no callback.
  759. // hold the preview lock while we grab a reference to the preview buffer
  760. sp<MemoryHeapBase> previewBuffer;
  761. if (mPreviewBuffer == 0) {
  762. mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
  763. } else if (size > mPreviewBuffer->virtualSize()) {
  764. mPreviewBuffer.clear();
  765. mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
  766. }
  767. if (mPreviewBuffer == 0) {
  768. ALOGE("failed to allocate space for preview buffer");
  769. mLock.unlock();
  770. return;
  771. }
  772. previewBuffer = mPreviewBuffer;
  773. memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
  774. sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
  775. if (frame == 0) {
  776. ALOGE("failed to allocate space for frame callback");
  777. mLock.unlock();
  778. return;
  779. }
  780. mLock.unlock();
  781. client->dataCallback(msgType, frame, metadata);
  782. }
  783. int CameraClient::getOrientation(int degrees, bool mirror) {
  784. if (!mirror) {
  785. if (degrees == 0) return 0;
  786. else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
  787. else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
  788. else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
  789. } else { // Do mirror (horizontal flip)
  790. if (degrees == 0) { // FLIP_H and ROT_0
  791. return HAL_TRANSFORM_FLIP_H;
  792. } else if (degrees == 90) { // FLIP_H and ROT_90
  793. return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
  794. } else if (degrees == 180) { // FLIP_H and ROT_180
  795. return HAL_TRANSFORM_FLIP_V;
  796. } else if (degrees == 270) { // FLIP_H and ROT_270
  797. return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
  798. }
  799. }
  800. ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
  801. return -1;
  802. }
  803. }; // namespace android