ICameraRecordingProxy.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (C) 2011 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_NDEBUG 0
  17. #define LOG_TAG "ICameraRecordingProxy"
  18. #include <camera/CameraUtils.h>
  19. #include <camera/ICameraRecordingProxy.h>
  20. #include <camera/ICameraRecordingProxyListener.h>
  21. #include <binder/IMemory.h>
  22. #include <binder/Parcel.h>
  23. #include <media/hardware/HardwareAPI.h>
  24. #include <stdint.h>
  25. #include <utils/Log.h>
  26. namespace android {
  27. enum {
  28. START_RECORDING = IBinder::FIRST_CALL_TRANSACTION,
  29. STOP_RECORDING,
  30. RELEASE_RECORDING_FRAME,
  31. RELEASE_RECORDING_FRAME_HANDLE,
  32. RELEASE_RECORDING_FRAME_HANDLE_BATCH,
  33. };
  34. class BpCameraRecordingProxy: public BpInterface<ICameraRecordingProxy>
  35. {
  36. public:
  37. explicit BpCameraRecordingProxy(const sp<IBinder>& impl)
  38. : BpInterface<ICameraRecordingProxy>(impl)
  39. {
  40. }
  41. status_t startRecording(const sp<ICameraRecordingProxyListener>& listener)
  42. {
  43. ALOGV("startRecording");
  44. Parcel data, reply;
  45. data.writeInterfaceToken(ICameraRecordingProxy::getInterfaceDescriptor());
  46. data.writeStrongBinder(IInterface::asBinder(listener));
  47. remote()->transact(START_RECORDING, data, &reply);
  48. return reply.readInt32();
  49. }
  50. void stopRecording()
  51. {
  52. ALOGV("stopRecording");
  53. Parcel data, reply;
  54. data.writeInterfaceToken(ICameraRecordingProxy::getInterfaceDescriptor());
  55. remote()->transact(STOP_RECORDING, data, &reply);
  56. }
  57. void releaseRecordingFrame(const sp<IMemory>& mem)
  58. {
  59. ALOGV("releaseRecordingFrame");
  60. Parcel data, reply;
  61. data.writeInterfaceToken(ICameraRecordingProxy::getInterfaceDescriptor());
  62. data.writeStrongBinder(IInterface::asBinder(mem));
  63. remote()->transact(RELEASE_RECORDING_FRAME, data, &reply);
  64. }
  65. void releaseRecordingFrameHandle(native_handle_t *handle) {
  66. ALOGV("releaseRecordingFrameHandle");
  67. Parcel data, reply;
  68. data.writeInterfaceToken(ICameraRecordingProxy::getInterfaceDescriptor());
  69. data.writeNativeHandle(handle);
  70. remote()->transact(RELEASE_RECORDING_FRAME_HANDLE, data, &reply);
  71. // Close the native handle because camera received a dup copy.
  72. native_handle_close(handle);
  73. native_handle_delete(handle);
  74. }
  75. void releaseRecordingFrameHandleBatch(const std::vector<native_handle_t*>& handles) {
  76. ALOGV("releaseRecordingFrameHandleBatch");
  77. Parcel data, reply;
  78. data.writeInterfaceToken(ICameraRecordingProxy::getInterfaceDescriptor());
  79. uint32_t n = handles.size();
  80. data.writeUint32(n);
  81. for (auto& handle : handles) {
  82. data.writeNativeHandle(handle);
  83. }
  84. remote()->transact(RELEASE_RECORDING_FRAME_HANDLE_BATCH, data, &reply);
  85. // Close the native handle because camera received a dup copy.
  86. for (auto& handle : handles) {
  87. native_handle_close(handle);
  88. native_handle_delete(handle);
  89. }
  90. }
  91. };
  92. IMPLEMENT_META_INTERFACE(CameraRecordingProxy, "android.hardware.ICameraRecordingProxy");
  93. // ----------------------------------------------------------------------
  94. status_t BnCameraRecordingProxy::onTransact(
  95. uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
  96. {
  97. switch(code) {
  98. case START_RECORDING: {
  99. ALOGV("START_RECORDING");
  100. CHECK_INTERFACE(ICameraRecordingProxy, data, reply);
  101. sp<ICameraRecordingProxyListener> listener =
  102. interface_cast<ICameraRecordingProxyListener>(data.readStrongBinder());
  103. reply->writeInt32(startRecording(listener));
  104. return NO_ERROR;
  105. } break;
  106. case STOP_RECORDING: {
  107. ALOGV("STOP_RECORDING");
  108. CHECK_INTERFACE(ICameraRecordingProxy, data, reply);
  109. stopRecording();
  110. return NO_ERROR;
  111. } break;
  112. case RELEASE_RECORDING_FRAME: {
  113. ALOGV("RELEASE_RECORDING_FRAME");
  114. CHECK_INTERFACE(ICameraRecordingProxy, data, reply);
  115. sp<IMemory> mem = interface_cast<IMemory>(data.readStrongBinder());
  116. releaseRecordingFrame(mem);
  117. return NO_ERROR;
  118. } break;
  119. case RELEASE_RECORDING_FRAME_HANDLE: {
  120. ALOGV("RELEASE_RECORDING_FRAME_HANDLE");
  121. CHECK_INTERFACE(ICameraRecordingProxy, data, reply);
  122. // releaseRecordingFrameHandle will be responsble to close the native handle.
  123. releaseRecordingFrameHandle(data.readNativeHandle());
  124. return NO_ERROR;
  125. } break;
  126. case RELEASE_RECORDING_FRAME_HANDLE_BATCH: {
  127. ALOGV("RELEASE_RECORDING_FRAME_HANDLE_BATCH");
  128. CHECK_INTERFACE(ICameraRecordingProxy, data, reply);
  129. uint32_t n = 0;
  130. status_t res = data.readUint32(&n);
  131. if (res != OK) {
  132. ALOGE("%s: Failed to read batch size: %s (%d)", __FUNCTION__, strerror(-res), res);
  133. return BAD_VALUE;
  134. }
  135. std::vector<native_handle_t*> handles;
  136. handles.reserve(n);
  137. for (uint32_t i = 0; i < n; i++) {
  138. native_handle_t* handle = data.readNativeHandle();
  139. if (handle == nullptr) {
  140. ALOGE("%s: Received a null native handle at handles[%d]",
  141. __FUNCTION__, i);
  142. return BAD_VALUE;
  143. }
  144. handles.push_back(handle);
  145. }
  146. // releaseRecordingFrameHandleBatch will be responsble to close the native handle.
  147. releaseRecordingFrameHandleBatch(handles);
  148. return NO_ERROR;
  149. } break;
  150. default:
  151. return BBinder::onTransact(code, data, reply, flags);
  152. }
  153. }
  154. // ----------------------------------------------------------------------------
  155. }; // namespace android