Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2013, 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 | |
| 18 | // #define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "ICameraDeviceUser" |
| 20 | #include <utils/Log.h> |
| 21 | #include <stdint.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <binder/Parcel.h> |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 24 | #include <camera/camera2/ICameraDeviceUser.h> |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 25 | #include <gui/IGraphicBufferProducer.h> |
| 26 | #include <gui/Surface.h> |
| 27 | #include <camera/CameraMetadata.h> |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 28 | #include <camera/camera2/CaptureRequest.h> |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | typedef Parcel::WritableBlob WritableBlob; |
| 33 | typedef Parcel::ReadableBlob ReadableBlob; |
| 34 | |
| 35 | enum { |
| 36 | DISCONNECT = IBinder::FIRST_CALL_TRANSACTION, |
| 37 | SUBMIT_REQUEST, |
| 38 | CANCEL_REQUEST, |
| 39 | DELETE_STREAM, |
| 40 | CREATE_STREAM, |
| 41 | CREATE_DEFAULT_REQUEST, |
| 42 | GET_CAMERA_INFO, |
Zhijun He | 2ab500c | 2013-07-23 08:02:53 -0700 | [diff] [blame] | 43 | WAIT_UNTIL_IDLE, |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame^] | 44 | FLUSH |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | class BpCameraDeviceUser : public BpInterface<ICameraDeviceUser> |
| 48 | { |
| 49 | public: |
| 50 | BpCameraDeviceUser(const sp<IBinder>& impl) |
| 51 | : BpInterface<ICameraDeviceUser>(impl) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | // disconnect from camera service |
| 56 | void disconnect() |
| 57 | { |
| 58 | ALOGV("disconnect"); |
| 59 | Parcel data, reply; |
| 60 | data.writeInterfaceToken(ICameraDeviceUser::getInterfaceDescriptor()); |
| 61 | remote()->transact(DISCONNECT, data, &reply); |
| 62 | reply.readExceptionCode(); |
| 63 | } |
| 64 | |
| 65 | virtual int submitRequest(sp<CaptureRequest> request, bool streaming) |
| 66 | { |
| 67 | Parcel data, reply; |
| 68 | data.writeInterfaceToken(ICameraDeviceUser::getInterfaceDescriptor()); |
| 69 | |
| 70 | // arg0 = CaptureRequest |
| 71 | if (request != 0) { |
| 72 | data.writeInt32(1); |
| 73 | request->writeToParcel(&data); |
| 74 | } else { |
| 75 | data.writeInt32(0); |
| 76 | } |
| 77 | |
| 78 | // arg1 = streaming (bool) |
| 79 | data.writeInt32(streaming); |
| 80 | |
| 81 | remote()->transact(SUBMIT_REQUEST, data, &reply); |
| 82 | |
| 83 | reply.readExceptionCode(); |
| 84 | return reply.readInt32(); |
| 85 | } |
| 86 | |
| 87 | virtual status_t cancelRequest(int requestId) |
| 88 | { |
| 89 | Parcel data, reply; |
| 90 | data.writeInterfaceToken(ICameraDeviceUser::getInterfaceDescriptor()); |
| 91 | data.writeInt32(requestId); |
| 92 | |
| 93 | remote()->transact(CANCEL_REQUEST, data, &reply); |
| 94 | |
| 95 | reply.readExceptionCode(); |
| 96 | return reply.readInt32(); |
| 97 | } |
| 98 | |
| 99 | virtual status_t deleteStream(int streamId) |
| 100 | { |
| 101 | Parcel data, reply; |
| 102 | data.writeInterfaceToken(ICameraDeviceUser::getInterfaceDescriptor()); |
| 103 | data.writeInt32(streamId); |
| 104 | |
| 105 | remote()->transact(DELETE_STREAM, data, &reply); |
| 106 | |
| 107 | reply.readExceptionCode(); |
| 108 | return reply.readInt32(); |
| 109 | } |
| 110 | |
| 111 | virtual status_t createStream(int width, int height, int format, |
| 112 | const sp<IGraphicBufferProducer>& bufferProducer) |
| 113 | { |
| 114 | Parcel data, reply; |
| 115 | data.writeInterfaceToken(ICameraDeviceUser::getInterfaceDescriptor()); |
| 116 | data.writeInt32(width); |
| 117 | data.writeInt32(height); |
| 118 | data.writeInt32(format); |
| 119 | |
| 120 | data.writeInt32(1); // marker that bufferProducer is not null |
| 121 | data.writeString16(String16("unknown_name")); // name of surface |
| 122 | sp<IBinder> b(bufferProducer->asBinder()); |
| 123 | data.writeStrongBinder(b); |
| 124 | |
| 125 | remote()->transact(CREATE_STREAM, data, &reply); |
| 126 | |
| 127 | reply.readExceptionCode(); |
| 128 | return reply.readInt32(); |
| 129 | } |
| 130 | |
| 131 | // Create a request object from a template. |
| 132 | virtual status_t createDefaultRequest(int templateId, |
| 133 | /*out*/ |
| 134 | CameraMetadata* request) |
| 135 | { |
| 136 | Parcel data, reply; |
| 137 | data.writeInterfaceToken(ICameraDeviceUser::getInterfaceDescriptor()); |
| 138 | data.writeInt32(templateId); |
| 139 | remote()->transact(CREATE_DEFAULT_REQUEST, data, &reply); |
| 140 | |
| 141 | reply.readExceptionCode(); |
| 142 | status_t result = reply.readInt32(); |
| 143 | |
| 144 | CameraMetadata out; |
| 145 | if (reply.readInt32() != 0) { |
| 146 | out.readFromParcel(&reply); |
| 147 | } |
| 148 | |
| 149 | if (request != NULL) { |
| 150 | request->swap(out); |
| 151 | } |
| 152 | return result; |
| 153 | } |
| 154 | |
| 155 | |
Igor Murashkin | 099b457 | 2013-07-12 17:52:16 -0700 | [diff] [blame] | 156 | virtual status_t getCameraInfo(CameraMetadata* info) |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 157 | { |
| 158 | Parcel data, reply; |
| 159 | data.writeInterfaceToken(ICameraDeviceUser::getInterfaceDescriptor()); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 160 | remote()->transact(GET_CAMERA_INFO, data, &reply); |
| 161 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 162 | reply.readExceptionCode(); |
| 163 | status_t result = reply.readInt32(); |
| 164 | |
Igor Murashkin | 099b457 | 2013-07-12 17:52:16 -0700 | [diff] [blame] | 165 | CameraMetadata out; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 166 | if (reply.readInt32() != 0) { |
Igor Murashkin | 099b457 | 2013-07-12 17:52:16 -0700 | [diff] [blame] | 167 | out.readFromParcel(&reply); |
| 168 | } |
| 169 | |
| 170 | if (info != NULL) { |
| 171 | info->swap(out); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | return result; |
| 175 | } |
| 176 | |
Zhijun He | 2ab500c | 2013-07-23 08:02:53 -0700 | [diff] [blame] | 177 | virtual status_t waitUntilIdle() |
| 178 | { |
| 179 | ALOGV("waitUntilIdle"); |
| 180 | Parcel data, reply; |
| 181 | data.writeInterfaceToken(ICameraDeviceUser::getInterfaceDescriptor()); |
| 182 | remote()->transact(WAIT_UNTIL_IDLE, data, &reply); |
| 183 | reply.readExceptionCode(); |
| 184 | return reply.readInt32(); |
| 185 | } |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 186 | |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame^] | 187 | virtual status_t flush() |
| 188 | { |
| 189 | ALOGV("flush"); |
| 190 | Parcel data, reply; |
| 191 | data.writeInterfaceToken(ICameraDeviceUser::getInterfaceDescriptor()); |
| 192 | remote()->transact(FLUSH, data, &reply); |
| 193 | reply.readExceptionCode(); |
| 194 | return reply.readInt32(); |
| 195 | } |
| 196 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 197 | private: |
| 198 | |
| 199 | |
| 200 | }; |
| 201 | |
| 202 | IMPLEMENT_META_INTERFACE(CameraDeviceUser, |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 203 | "android.hardware.camera2.ICameraDeviceUser"); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 204 | |
| 205 | // ---------------------------------------------------------------------- |
| 206 | |
| 207 | status_t BnCameraDeviceUser::onTransact( |
| 208 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 209 | { |
| 210 | switch(code) { |
| 211 | case DISCONNECT: { |
| 212 | ALOGV("DISCONNECT"); |
| 213 | CHECK_INTERFACE(ICameraDeviceUser, data, reply); |
| 214 | disconnect(); |
| 215 | reply->writeNoException(); |
| 216 | return NO_ERROR; |
| 217 | } break; |
| 218 | case SUBMIT_REQUEST: { |
| 219 | CHECK_INTERFACE(ICameraDeviceUser, data, reply); |
| 220 | |
| 221 | // arg0 = request |
| 222 | sp<CaptureRequest> request; |
| 223 | if (data.readInt32() != 0) { |
| 224 | request = new CaptureRequest(); |
| 225 | request->readFromParcel(const_cast<Parcel*>(&data)); |
| 226 | } |
| 227 | |
| 228 | // arg1 = streaming (bool) |
| 229 | bool streaming = data.readInt32(); |
| 230 | |
| 231 | // return code: requestId (int32) |
| 232 | reply->writeNoException(); |
| 233 | reply->writeInt32(submitRequest(request, streaming)); |
| 234 | |
| 235 | return NO_ERROR; |
| 236 | } break; |
| 237 | case CANCEL_REQUEST: { |
| 238 | CHECK_INTERFACE(ICameraDeviceUser, data, reply); |
| 239 | int requestId = data.readInt32(); |
| 240 | reply->writeNoException(); |
| 241 | reply->writeInt32(cancelRequest(requestId)); |
| 242 | return NO_ERROR; |
| 243 | } break; |
| 244 | case DELETE_STREAM: { |
| 245 | CHECK_INTERFACE(ICameraDeviceUser, data, reply); |
| 246 | int streamId = data.readInt32(); |
| 247 | reply->writeNoException(); |
| 248 | reply->writeInt32(deleteStream(streamId)); |
| 249 | return NO_ERROR; |
| 250 | } break; |
| 251 | case CREATE_STREAM: { |
| 252 | CHECK_INTERFACE(ICameraDeviceUser, data, reply); |
| 253 | int width, height, format; |
| 254 | |
| 255 | width = data.readInt32(); |
| 256 | ALOGV("%s: CREATE_STREAM: width = %d", __FUNCTION__, width); |
| 257 | height = data.readInt32(); |
| 258 | ALOGV("%s: CREATE_STREAM: height = %d", __FUNCTION__, height); |
| 259 | format = data.readInt32(); |
| 260 | ALOGV("%s: CREATE_STREAM: format = %d", __FUNCTION__, format); |
| 261 | |
| 262 | sp<IGraphicBufferProducer> bp; |
| 263 | if (data.readInt32() != 0) { |
| 264 | String16 name = data.readString16(); |
| 265 | bp = interface_cast<IGraphicBufferProducer>( |
| 266 | data.readStrongBinder()); |
| 267 | |
| 268 | ALOGV("%s: CREATE_STREAM: bp = %p, name = %s", __FUNCTION__, |
| 269 | bp.get(), String8(name).string()); |
| 270 | } else { |
| 271 | ALOGV("%s: CREATE_STREAM: bp = unset, name = unset", |
| 272 | __FUNCTION__); |
| 273 | } |
| 274 | |
| 275 | status_t ret; |
| 276 | ret = createStream(width, height, format, bp); |
| 277 | |
| 278 | reply->writeNoException(); |
| 279 | ALOGV("%s: CREATE_STREAM: write noException", __FUNCTION__); |
| 280 | reply->writeInt32(ret); |
| 281 | ALOGV("%s: CREATE_STREAM: write ret = %d", __FUNCTION__, ret); |
| 282 | |
| 283 | return NO_ERROR; |
| 284 | } break; |
| 285 | |
| 286 | case CREATE_DEFAULT_REQUEST: { |
| 287 | CHECK_INTERFACE(ICameraDeviceUser, data, reply); |
| 288 | |
| 289 | int templateId = data.readInt32(); |
| 290 | |
| 291 | CameraMetadata request; |
| 292 | status_t ret; |
| 293 | ret = createDefaultRequest(templateId, &request); |
| 294 | |
| 295 | reply->writeNoException(); |
| 296 | reply->writeInt32(ret); |
| 297 | |
Igor Murashkin | 099b457 | 2013-07-12 17:52:16 -0700 | [diff] [blame] | 298 | // out-variables are after exception and return value |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 299 | reply->writeInt32(1); // to mark presence of metadata object |
| 300 | request.writeToParcel(const_cast<Parcel*>(reply)); |
| 301 | |
| 302 | return NO_ERROR; |
| 303 | } break; |
| 304 | case GET_CAMERA_INFO: { |
| 305 | CHECK_INTERFACE(ICameraDeviceUser, data, reply); |
| 306 | |
Igor Murashkin | 099b457 | 2013-07-12 17:52:16 -0700 | [diff] [blame] | 307 | CameraMetadata info; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 308 | status_t ret; |
Igor Murashkin | 099b457 | 2013-07-12 17:52:16 -0700 | [diff] [blame] | 309 | ret = getCameraInfo(&info); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 310 | |
| 311 | reply->writeNoException(); |
| 312 | reply->writeInt32(ret); |
| 313 | |
Igor Murashkin | 099b457 | 2013-07-12 17:52:16 -0700 | [diff] [blame] | 314 | // out-variables are after exception and return value |
| 315 | reply->writeInt32(1); // to mark presence of metadata object |
| 316 | info.writeToParcel(reply); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 317 | |
| 318 | return NO_ERROR; |
| 319 | } break; |
Zhijun He | 2ab500c | 2013-07-23 08:02:53 -0700 | [diff] [blame] | 320 | case WAIT_UNTIL_IDLE: { |
| 321 | CHECK_INTERFACE(ICameraDeviceUser, data, reply); |
| 322 | reply->writeNoException(); |
| 323 | reply->writeInt32(waitUntilIdle()); |
| 324 | return NO_ERROR; |
| 325 | } break; |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame^] | 326 | case FLUSH: { |
| 327 | CHECK_INTERFACE(ICameraDeviceUser, data, reply); |
| 328 | reply->writeNoException(); |
| 329 | reply->writeInt32(flush()); |
| 330 | return NO_ERROR; |
| 331 | } |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 332 | default: |
| 333 | return BBinder::onTransact(code, data, reply, flags); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // ---------------------------------------------------------------------------- |
| 338 | |
| 339 | }; // namespace android |