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