Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2008, The Android Open Source Project |
| 4 | ** |
Praveen Chavan | 6773d47 | 2016-01-13 01:24:30 -0800 | [diff] [blame] | 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 |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 8 | ** |
Praveen Chavan | 6773d47 | 2016-01-13 01:24:30 -0800 | [diff] [blame] | 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 10 | ** |
Praveen Chavan | 6773d47 | 2016-01-13 01:24:30 -0800 | [diff] [blame] | 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 |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "ICameraClient" |
| 20 | #include <utils/Log.h> |
| 21 | #include <stdint.h> |
| 22 | #include <sys/types.h> |
Praveen Chavan | 6773d47 | 2016-01-13 01:24:30 -0800 | [diff] [blame] | 23 | #include <camera/CameraUtils.h> |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 24 | #include <android/hardware/ICameraClient.h> |
Praveen Chavan | 6773d47 | 2016-01-13 01:24:30 -0800 | [diff] [blame] | 25 | #include <media/hardware/HardwareAPI.h> |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 26 | |
| 27 | namespace android { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 28 | namespace hardware { |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 29 | |
| 30 | enum { |
| 31 | NOTIFY_CALLBACK = IBinder::FIRST_CALL_TRANSACTION, |
| 32 | DATA_CALLBACK, |
| 33 | DATA_CALLBACK_TIMESTAMP, |
Chien-Yu Chen | 2d13b1d | 2016-04-28 12:11:20 -0700 | [diff] [blame] | 34 | RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP, |
Yin-Chia Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 35 | RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP_BATCH, |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | class BpCameraClient: public BpInterface<ICameraClient> |
| 39 | { |
| 40 | public: |
Chih-Hung Hsieh | 090ef60 | 2016-04-27 10:39:54 -0700 | [diff] [blame] | 41 | explicit BpCameraClient(const sp<IBinder>& impl) |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 42 | : BpInterface<ICameraClient>(impl) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | // generic callback from camera service to app |
| 47 | void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2) |
| 48 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 49 | ALOGV("notifyCallback"); |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 50 | Parcel data, reply; |
| 51 | data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor()); |
| 52 | data.writeInt32(msgType); |
| 53 | data.writeInt32(ext1); |
| 54 | data.writeInt32(ext2); |
| 55 | remote()->transact(NOTIFY_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY); |
| 56 | } |
| 57 | |
| 58 | // generic data callback from camera service to app with image data |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 59 | void dataCallback(int32_t msgType, const sp<IMemory>& imageData, |
| 60 | camera_frame_metadata_t *metadata) |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 61 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 62 | ALOGV("dataCallback"); |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 63 | Parcel data, reply; |
| 64 | data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor()); |
| 65 | data.writeInt32(msgType); |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 66 | data.writeStrongBinder(IInterface::asBinder(imageData)); |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 67 | if (metadata) { |
| 68 | data.writeInt32(metadata->number_of_faces); |
| 69 | data.write(metadata->faces, sizeof(camera_face_t) * metadata->number_of_faces); |
| 70 | } |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 71 | remote()->transact(DATA_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY); |
| 72 | } |
| 73 | |
| 74 | // generic data callback from camera service to app with image data |
| 75 | void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& imageData) |
| 76 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 77 | ALOGV("dataCallback"); |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 78 | Parcel data, reply; |
| 79 | data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor()); |
| 80 | data.writeInt64(timestamp); |
| 81 | data.writeInt32(msgType); |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 82 | data.writeStrongBinder(IInterface::asBinder(imageData)); |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 83 | remote()->transact(DATA_CALLBACK_TIMESTAMP, data, &reply, IBinder::FLAG_ONEWAY); |
| 84 | } |
Chien-Yu Chen | 2d13b1d | 2016-04-28 12:11:20 -0700 | [diff] [blame] | 85 | |
| 86 | void recordingFrameHandleCallbackTimestamp(nsecs_t timestamp, native_handle_t* handle) { |
| 87 | ALOGV("recordingFrameHandleCallbackTimestamp"); |
| 88 | Parcel data, reply; |
| 89 | data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor()); |
| 90 | data.writeInt64(timestamp); |
| 91 | data.writeNativeHandle(handle); |
| 92 | remote()->transact(RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP, data, &reply, |
| 93 | IBinder::FLAG_ONEWAY); |
| 94 | } |
Yin-Chia Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 95 | |
| 96 | void recordingFrameHandleCallbackTimestampBatch( |
| 97 | const std::vector<nsecs_t>& timestamps, |
| 98 | const std::vector<native_handle_t*>& handles) { |
| 99 | ALOGV("recordingFrameHandleCallbackTimestampBatch"); |
| 100 | Parcel data, reply; |
| 101 | data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor()); |
| 102 | uint32_t n = timestamps.size(); |
| 103 | if (n != handles.size()) { |
| 104 | ALOGE("%s: size of timestamps(%zu) and handles(%zu) mismatch!", |
| 105 | __FUNCTION__, timestamps.size(), handles.size()); |
| 106 | return; |
| 107 | } |
| 108 | data.writeUint32(n); |
| 109 | for (auto ts : timestamps) { |
| 110 | data.writeInt64(ts); |
| 111 | } |
| 112 | for (auto& handle : handles) { |
| 113 | data.writeNativeHandle(handle); |
| 114 | } |
| 115 | remote()->transact(RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP_BATCH, data, &reply, |
| 116 | IBinder::FLAG_ONEWAY); |
| 117 | } |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | IMPLEMENT_META_INTERFACE(CameraClient, "android.hardware.ICameraClient"); |
| 121 | |
| 122 | // ---------------------------------------------------------------------- |
| 123 | |
| 124 | status_t BnCameraClient::onTransact( |
| 125 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 126 | { |
| 127 | switch(code) { |
| 128 | case NOTIFY_CALLBACK: { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 129 | ALOGV("NOTIFY_CALLBACK"); |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 130 | CHECK_INTERFACE(ICameraClient, data, reply); |
| 131 | int32_t msgType = data.readInt32(); |
| 132 | int32_t ext1 = data.readInt32(); |
| 133 | int32_t ext2 = data.readInt32(); |
| 134 | notifyCallback(msgType, ext1, ext2); |
| 135 | return NO_ERROR; |
| 136 | } break; |
| 137 | case DATA_CALLBACK: { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 138 | ALOGV("DATA_CALLBACK"); |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 139 | CHECK_INTERFACE(ICameraClient, data, reply); |
| 140 | int32_t msgType = data.readInt32(); |
| 141 | sp<IMemory> imageData = interface_cast<IMemory>(data.readStrongBinder()); |
Eino-Ville Talvala | 3ac6bae | 2020-05-08 14:02:22 -0700 | [diff] [blame] | 142 | camera_frame_metadata_t metadata; |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 143 | if (data.dataAvail() > 0) { |
Eino-Ville Talvala | 3ac6bae | 2020-05-08 14:02:22 -0700 | [diff] [blame] | 144 | metadata.number_of_faces = data.readInt32(); |
Eino-Ville Talvala | fbaedd7 | 2020-08-17 13:41:08 -0700 | [diff] [blame] | 145 | // Zero faces is a valid case, to notify clients that no faces are now visible |
| 146 | if (metadata.number_of_faces < 0 || |
Eino-Ville Talvala | 3ac6bae | 2020-05-08 14:02:22 -0700 | [diff] [blame] | 147 | metadata.number_of_faces > (int32_t)(INT32_MAX / sizeof(camera_face_t))) { |
| 148 | ALOGE("%s: Too large face count: %d", __FUNCTION__, metadata.number_of_faces); |
Eino-Ville Talvala | b393a2d | 2020-04-28 18:08:50 -0700 | [diff] [blame] | 149 | return BAD_VALUE; |
| 150 | } |
Eino-Ville Talvala | 3ac6bae | 2020-05-08 14:02:22 -0700 | [diff] [blame] | 151 | metadata.faces = (camera_face_t *) data.readInplace( |
| 152 | sizeof(camera_face_t) * metadata.number_of_faces); |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 153 | } |
Eino-Ville Talvala | 3ac6bae | 2020-05-08 14:02:22 -0700 | [diff] [blame] | 154 | dataCallback(msgType, imageData, &metadata); |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 155 | return NO_ERROR; |
| 156 | } break; |
| 157 | case DATA_CALLBACK_TIMESTAMP: { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 158 | ALOGV("DATA_CALLBACK_TIMESTAMP"); |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 159 | CHECK_INTERFACE(ICameraClient, data, reply); |
| 160 | nsecs_t timestamp = data.readInt64(); |
| 161 | int32_t msgType = data.readInt32(); |
| 162 | sp<IMemory> imageData = interface_cast<IMemory>(data.readStrongBinder()); |
Chien-Yu Chen | 2d13b1d | 2016-04-28 12:11:20 -0700 | [diff] [blame] | 163 | dataCallbackTimestamp(timestamp, msgType, imageData); |
| 164 | return NO_ERROR; |
| 165 | } break; |
| 166 | case RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP: { |
| 167 | ALOGV("RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP"); |
| 168 | CHECK_INTERFACE(ICameraClient, data, reply); |
| 169 | nsecs_t timestamp; |
| 170 | status_t res = data.readInt64(×tamp); |
| 171 | if (res != OK) { |
| 172 | ALOGE("%s: Failed to read timestamp: %s (%d)", __FUNCTION__, strerror(-res), res); |
| 173 | return BAD_VALUE; |
| 174 | } |
| 175 | native_handle_t* handle = data.readNativeHandle(); |
| 176 | if (handle == nullptr) { |
| 177 | ALOGE("%s: Received a null native handle", __FUNCTION__); |
| 178 | return BAD_VALUE; |
Praveen Chavan | 6773d47 | 2016-01-13 01:24:30 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Chien-Yu Chen | 2d13b1d | 2016-04-28 12:11:20 -0700 | [diff] [blame] | 181 | // The native handle will be freed in BpCamera::releaseRecordingFrameHandle. |
| 182 | recordingFrameHandleCallbackTimestamp(timestamp, handle); |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 183 | return NO_ERROR; |
| 184 | } break; |
Yin-Chia Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 185 | case RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP_BATCH: { |
| 186 | ALOGV("RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP_BATCH"); |
| 187 | CHECK_INTERFACE(ICameraClient, data, reply); |
| 188 | uint32_t n = 0; |
| 189 | status_t res = data.readUint32(&n); |
| 190 | if (res != OK) { |
| 191 | ALOGE("%s: Failed to read batch size: %s (%d)", __FUNCTION__, strerror(-res), res); |
| 192 | return BAD_VALUE; |
| 193 | } |
| 194 | std::vector<nsecs_t> timestamps; |
| 195 | std::vector<native_handle_t*> handles; |
| 196 | timestamps.reserve(n); |
| 197 | handles.reserve(n); |
| 198 | for (uint32_t i = 0; i < n; i++) { |
Yin-Chia Yeh | e387edc | 2017-09-29 14:13:28 -0700 | [diff] [blame] | 199 | nsecs_t t; |
| 200 | res = data.readInt64(&t); |
Yin-Chia Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 201 | if (res != OK) { |
| 202 | ALOGE("%s: Failed to read timestamp[%d]: %s (%d)", |
| 203 | __FUNCTION__, i, strerror(-res), res); |
| 204 | return BAD_VALUE; |
| 205 | } |
Yin-Chia Yeh | e387edc | 2017-09-29 14:13:28 -0700 | [diff] [blame] | 206 | timestamps.push_back(t); |
Yin-Chia Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 207 | } |
| 208 | for (uint32_t i = 0; i < n; i++) { |
| 209 | native_handle_t* handle = data.readNativeHandle(); |
| 210 | if (handle == nullptr) { |
| 211 | ALOGE("%s: Received a null native handle at handles[%d]", |
| 212 | __FUNCTION__, i); |
| 213 | return BAD_VALUE; |
| 214 | } |
| 215 | handles.push_back(handle); |
| 216 | } |
| 217 | |
| 218 | // The native handle will be freed in BpCamera::releaseRecordingFrameHandleBatch. |
| 219 | recordingFrameHandleCallbackTimestampBatch(timestamps, handles); |
| 220 | return NO_ERROR; |
| 221 | } break; |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 222 | default: |
| 223 | return BBinder::onTransact(code, data, reply, flags); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // ---------------------------------------------------------------------------- |
| 228 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 229 | } // namespace hardware |
| 230 | } // namespace android |