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