ICameraClient.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. **
  3. ** Copyright 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 "ICameraClient"
  19. #include <utils/Log.h>
  20. #include <stdint.h>
  21. #include <sys/types.h>
  22. #include <camera/CameraUtils.h>
  23. #include <android/hardware/ICameraClient.h>
  24. #include <media/hardware/HardwareAPI.h>
  25. namespace android {
  26. namespace hardware {
  27. enum {
  28. NOTIFY_CALLBACK = IBinder::FIRST_CALL_TRANSACTION,
  29. DATA_CALLBACK,
  30. DATA_CALLBACK_TIMESTAMP,
  31. RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP,
  32. RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP_BATCH,
  33. };
  34. class BpCameraClient: public BpInterface<ICameraClient>
  35. {
  36. public:
  37. explicit BpCameraClient(const sp<IBinder>& impl)
  38. : BpInterface<ICameraClient>(impl)
  39. {
  40. }
  41. // generic callback from camera service to app
  42. void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
  43. {
  44. ALOGV("notifyCallback");
  45. Parcel data, reply;
  46. data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor());
  47. data.writeInt32(msgType);
  48. data.writeInt32(ext1);
  49. data.writeInt32(ext2);
  50. remote()->transact(NOTIFY_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY);
  51. }
  52. // generic data callback from camera service to app with image data
  53. void dataCallback(int32_t msgType, const sp<IMemory>& imageData,
  54. camera_frame_metadata_t *metadata)
  55. {
  56. ALOGV("dataCallback");
  57. Parcel data, reply;
  58. data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor());
  59. data.writeInt32(msgType);
  60. data.writeStrongBinder(IInterface::asBinder(imageData));
  61. if (metadata) {
  62. data.writeInt32(metadata->number_of_faces);
  63. data.write(metadata->faces, sizeof(camera_face_t) * metadata->number_of_faces);
  64. }
  65. remote()->transact(DATA_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY);
  66. }
  67. // generic data callback from camera service to app with image data
  68. void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& imageData)
  69. {
  70. ALOGV("dataCallback");
  71. Parcel data, reply;
  72. data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor());
  73. data.writeInt64(timestamp);
  74. data.writeInt32(msgType);
  75. data.writeStrongBinder(IInterface::asBinder(imageData));
  76. remote()->transact(DATA_CALLBACK_TIMESTAMP, data, &reply, IBinder::FLAG_ONEWAY);
  77. }
  78. void recordingFrameHandleCallbackTimestamp(nsecs_t timestamp, native_handle_t* handle) {
  79. ALOGV("recordingFrameHandleCallbackTimestamp");
  80. Parcel data, reply;
  81. data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor());
  82. data.writeInt64(timestamp);
  83. data.writeNativeHandle(handle);
  84. remote()->transact(RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP, data, &reply,
  85. IBinder::FLAG_ONEWAY);
  86. }
  87. void recordingFrameHandleCallbackTimestampBatch(
  88. const std::vector<nsecs_t>& timestamps,
  89. const std::vector<native_handle_t*>& handles) {
  90. ALOGV("recordingFrameHandleCallbackTimestampBatch");
  91. Parcel data, reply;
  92. data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor());
  93. uint32_t n = timestamps.size();
  94. if (n != handles.size()) {
  95. ALOGE("%s: size of timestamps(%zu) and handles(%zu) mismatch!",
  96. __FUNCTION__, timestamps.size(), handles.size());
  97. return;
  98. }
  99. data.writeUint32(n);
  100. for (auto ts : timestamps) {
  101. data.writeInt64(ts);
  102. }
  103. for (auto& handle : handles) {
  104. data.writeNativeHandle(handle);
  105. }
  106. remote()->transact(RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP_BATCH, data, &reply,
  107. IBinder::FLAG_ONEWAY);
  108. }
  109. };
  110. IMPLEMENT_META_INTERFACE(CameraClient, "android.hardware.ICameraClient");
  111. // ----------------------------------------------------------------------
  112. status_t BnCameraClient::onTransact(
  113. uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
  114. {
  115. switch(code) {
  116. case NOTIFY_CALLBACK: {
  117. ALOGV("NOTIFY_CALLBACK");
  118. CHECK_INTERFACE(ICameraClient, data, reply);
  119. int32_t msgType = data.readInt32();
  120. int32_t ext1 = data.readInt32();
  121. int32_t ext2 = data.readInt32();
  122. notifyCallback(msgType, ext1, ext2);
  123. return NO_ERROR;
  124. } break;
  125. case DATA_CALLBACK: {
  126. ALOGV("DATA_CALLBACK");
  127. CHECK_INTERFACE(ICameraClient, data, reply);
  128. int32_t msgType = data.readInt32();
  129. sp<IMemory> imageData = interface_cast<IMemory>(data.readStrongBinder());
  130. camera_frame_metadata_t *metadata = NULL;
  131. if (data.dataAvail() > 0) {
  132. metadata = new camera_frame_metadata_t;
  133. metadata->number_of_faces = data.readInt32();
  134. metadata->faces = (camera_face_t *) data.readInplace(
  135. sizeof(camera_face_t) * metadata->number_of_faces);
  136. }
  137. dataCallback(msgType, imageData, metadata);
  138. if (metadata) delete metadata;
  139. return NO_ERROR;
  140. } break;
  141. case DATA_CALLBACK_TIMESTAMP: {
  142. ALOGV("DATA_CALLBACK_TIMESTAMP");
  143. CHECK_INTERFACE(ICameraClient, data, reply);
  144. nsecs_t timestamp = data.readInt64();
  145. int32_t msgType = data.readInt32();
  146. sp<IMemory> imageData = interface_cast<IMemory>(data.readStrongBinder());
  147. dataCallbackTimestamp(timestamp, msgType, imageData);
  148. return NO_ERROR;
  149. } break;
  150. case RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP: {
  151. ALOGV("RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP");
  152. CHECK_INTERFACE(ICameraClient, data, reply);
  153. nsecs_t timestamp;
  154. status_t res = data.readInt64(&timestamp);
  155. if (res != OK) {
  156. ALOGE("%s: Failed to read timestamp: %s (%d)", __FUNCTION__, strerror(-res), res);
  157. return BAD_VALUE;
  158. }
  159. native_handle_t* handle = data.readNativeHandle();
  160. if (handle == nullptr) {
  161. ALOGE("%s: Received a null native handle", __FUNCTION__);
  162. return BAD_VALUE;
  163. }
  164. // The native handle will be freed in BpCamera::releaseRecordingFrameHandle.
  165. recordingFrameHandleCallbackTimestamp(timestamp, handle);
  166. return NO_ERROR;
  167. } break;
  168. case RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP_BATCH: {
  169. ALOGV("RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP_BATCH");
  170. CHECK_INTERFACE(ICameraClient, data, reply);
  171. uint32_t n = 0;
  172. status_t res = data.readUint32(&n);
  173. if (res != OK) {
  174. ALOGE("%s: Failed to read batch size: %s (%d)", __FUNCTION__, strerror(-res), res);
  175. return BAD_VALUE;
  176. }
  177. std::vector<nsecs_t> timestamps;
  178. std::vector<native_handle_t*> handles;
  179. timestamps.reserve(n);
  180. handles.reserve(n);
  181. for (uint32_t i = 0; i < n; i++) {
  182. nsecs_t t;
  183. res = data.readInt64(&t);
  184. if (res != OK) {
  185. ALOGE("%s: Failed to read timestamp[%d]: %s (%d)",
  186. __FUNCTION__, i, strerror(-res), res);
  187. return BAD_VALUE;
  188. }
  189. timestamps.push_back(t);
  190. }
  191. for (uint32_t i = 0; i < n; i++) {
  192. native_handle_t* handle = data.readNativeHandle();
  193. if (handle == nullptr) {
  194. ALOGE("%s: Received a null native handle at handles[%d]",
  195. __FUNCTION__, i);
  196. return BAD_VALUE;
  197. }
  198. handles.push_back(handle);
  199. }
  200. // The native handle will be freed in BpCamera::releaseRecordingFrameHandleBatch.
  201. recordingFrameHandleCallbackTimestampBatch(timestamps, handles);
  202. return NO_ERROR;
  203. } break;
  204. default:
  205. return BBinder::onTransact(code, data, reply, flags);
  206. }
  207. }
  208. // ----------------------------------------------------------------------------
  209. } // namespace hardware
  210. } // namespace android