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 18 //#define LOG_NDEBUG 0 19 #define LOG_TAG "ICamera" 20 #include <utils/Log.h> 21 #include <stdint.h> 22 #include <sys/types.h> 23 #include <binder/Parcel.h> 24 #include <camera/ICamera.h> 25 26 namespace android { 27 28 enum { 29 DISCONNECT = IBinder::FIRST_CALL_TRANSACTION, 30 SET_PREVIEW_DISPLAY, 31 SET_PREVIEW_CALLBACK_FLAG, 32 START_PREVIEW, 33 STOP_PREVIEW, 34 AUTO_FOCUS, 35 CANCEL_AUTO_FOCUS, 36 TAKE_PICTURE, 37 SET_PARAMETERS, 38 GET_PARAMETERS, 39 SEND_COMMAND, 40 CONNECT, 41 LOCK, 42 UNLOCK, 43 PREVIEW_ENABLED, 44 START_RECORDING, 45 STOP_RECORDING, 46 RECORDING_ENABLED, 47 RELEASE_RECORDING_FRAME, 48 }; 49 50 class BpCamera: public BpInterface<ICamera> 51 { 52 public: 53 BpCamera(const sp<IBinder>& impl) 54 : BpInterface<ICamera>(impl) 55 { 56 } 57 58 // disconnect from camera service 59 void disconnect() 60 { 61 LOGV("disconnect"); 62 Parcel data, reply; 63 data.writeInterfaceToken(ICamera::getInterfaceDescriptor()); 64 remote()->transact(DISCONNECT, data, &reply); 65 } 66 67 // pass the buffered ISurface to the camera service 68 status_t setPreviewDisplay(const sp<ISurface>& surface) 69 { 70 LOGV("setPreviewDisplay"); 71 Parcel data, reply; 72 data.writeInterfaceToken(ICamera::getInterfaceDescriptor()); 73 data.writeStrongBinder(surface->asBinder()); 74 remote()->transact(SET_PREVIEW_DISPLAY, data, &reply); 75 return reply.readInt32(); 76 } 77 78 // set the preview callback flag to affect how the received frames from 79 // preview are handled. See Camera.h for details. 80 void setPreviewCallbackFlag(int flag) 81 { 82 LOGV("setPreviewCallbackFlag(%d)", flag); 83 Parcel data, reply; 84 data.writeInterfaceToken(ICamera::getInterfaceDescriptor()); 85 data.writeInt32(flag); 86 remote()->transact(SET_PREVIEW_CALLBACK_FLAG, data, &reply); 87 } 88 89 // start preview mode, must call setPreviewDisplay first 90 status_t startPreview() 91 { 92 LOGV("startPreview"); 93 Parcel data, reply; 94 data.writeInterfaceToken(ICamera::getInterfaceDescriptor()); 95 remote()->transact(START_PREVIEW, data, &reply); 96 return reply.readInt32(); 97 } 98 99 // start recording mode, must call setPreviewDisplay first 100 status_t startRecording() 101 { 102 LOGV("startRecording"); 103 Parcel data, reply; 104 data.writeInterfaceToken(ICamera::getInterfaceDescriptor()); 105 remote()->transact(START_RECORDING, data, &reply); 106 return reply.readInt32(); 107 } 108 109 // stop preview mode 110 void stopPreview() 111 { 112 LOGV("stopPreview"); 113 Parcel data, reply; 114 data.writeInterfaceToken(ICamera::getInterfaceDescriptor()); 115 remote()->transact(STOP_PREVIEW, data, &reply); 116 } 117 118 // stop recording mode 119 void stopRecording() 120 { 121 LOGV("stopRecording"); 122 Parcel data, reply; 123 data.writeInterfaceToken(ICamera::getInterfaceDescriptor()); 124 remote()->transact(STOP_RECORDING, data, &reply); 125 } 126 127 void releaseRecordingFrame(const sp<IMemory>& mem) 128 { 129 LOGV("releaseRecordingFrame"); 130 Parcel data, reply; 131 data.writeInterfaceToken(ICamera::getInterfaceDescriptor()); 132 data.writeStrongBinder(mem->asBinder()); 133 remote()->transact(RELEASE_RECORDING_FRAME, data, &reply); 134 } 135 136 // check preview state 137 bool previewEnabled() 138 { 139 LOGV("previewEnabled"); 140 Parcel data, reply; 141 data.writeInterfaceToken(ICamera::getInterfaceDescriptor()); 142 remote()->transact(PREVIEW_ENABLED, data, &reply); 143 return reply.readInt32(); 144 } 145 146 // check recording state 147 bool recordingEnabled() 148 { 149 LOGV("recordingEnabled"); 150 Parcel data, reply; 151 data.writeInterfaceToken(ICamera::getInterfaceDescriptor()); 152 remote()->transact(RECORDING_ENABLED, data, &reply); 153 return reply.readInt32(); 154 } 155 156 // auto focus 157 status_t autoFocus() 158 { 159 LOGV("autoFocus"); 160 Parcel data, reply; 161 data.writeInterfaceToken(ICamera::getInterfaceDescriptor()); 162 remote()->transact(AUTO_FOCUS, data, &reply); 163 status_t ret = reply.readInt32(); 164 return ret; 165 } 166 167 // cancel focus 168 status_t cancelAutoFocus() 169 { 170 LOGV("cancelAutoFocus"); 171 Parcel data, reply; 172 data.writeInterfaceToken(ICamera::getInterfaceDescriptor()); 173 remote()->transact(CANCEL_AUTO_FOCUS, data, &reply); 174 status_t ret = reply.readInt32(); 175 return ret; 176 } 177 178 // take a picture - returns an IMemory (ref-counted mmap) 179 status_t takePicture() 180 { 181 LOGV("takePicture"); 182 Parcel data, reply; 183 data.writeInterfaceToken(ICamera::getInterfaceDescriptor()); 184 remote()->transact(TAKE_PICTURE, data, &reply); 185 status_t ret = reply.readInt32(); 186 return ret; 187 } 188 189 // set preview/capture parameters - key/value pairs 190 status_t setParameters(const String8& params) 191 { 192 LOGV("setParameters"); 193 Parcel data, reply; 194 data.writeInterfaceToken(ICamera::getInterfaceDescriptor()); 195 data.writeString8(params); 196 remote()->transact(SET_PARAMETERS, data, &reply); 197 return reply.readInt32(); 198 } 199 200 // get preview/capture parameters - key/value pairs 201 String8 getParameters() const 202 { 203 LOGV("getParameters"); 204 Parcel data, reply; 205 data.writeInterfaceToken(ICamera::getInterfaceDescriptor()); 206 remote()->transact(GET_PARAMETERS, data, &reply); 207 return reply.readString8(); 208 } 209 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) 210 { 211 LOGV("sendCommand"); 212 Parcel data, reply; 213 data.writeInterfaceToken(ICamera::getInterfaceDescriptor()); 214 data.writeInt32(cmd); 215 data.writeInt32(arg1); 216 data.writeInt32(arg2); 217 remote()->transact(SEND_COMMAND, data, &reply); 218 return reply.readInt32(); 219 } 220 virtual status_t connect(const sp<ICameraClient>& cameraClient) 221 { 222 Parcel data, reply; 223 data.writeInterfaceToken(ICamera::getInterfaceDescriptor()); 224 data.writeStrongBinder(cameraClient->asBinder()); 225 remote()->transact(CONNECT, data, &reply); 226 return reply.readInt32(); 227 } 228 virtual status_t lock() 229 { 230 Parcel data, reply; 231 data.writeInterfaceToken(ICamera::getInterfaceDescriptor()); 232 remote()->transact(LOCK, data, &reply); 233 return reply.readInt32(); 234 } 235 virtual status_t unlock() 236 { 237 Parcel data, reply; 238 data.writeInterfaceToken(ICamera::getInterfaceDescriptor()); 239 remote()->transact(UNLOCK, data, &reply); 240 return reply.readInt32(); 241 } 242 }; 243 244 IMPLEMENT_META_INTERFACE(Camera, "android.hardware.ICamera"); 245 246 // ---------------------------------------------------------------------- 247 248 status_t BnCamera::onTransact( 249 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 250 { 251 switch(code) { 252 case DISCONNECT: { 253 LOGV("DISCONNECT"); 254 CHECK_INTERFACE(ICamera, data, reply); 255 disconnect(); 256 return NO_ERROR; 257 } break; 258 case SET_PREVIEW_DISPLAY: { 259 LOGV("SET_PREVIEW_DISPLAY"); 260 CHECK_INTERFACE(ICamera, data, reply); 261 sp<ISurface> surface = interface_cast<ISurface>(data.readStrongBinder()); 262 reply->writeInt32(setPreviewDisplay(surface)); 263 return NO_ERROR; 264 } break; 265 case SET_PREVIEW_CALLBACK_FLAG: { 266 LOGV("SET_PREVIEW_CALLBACK_TYPE"); 267 CHECK_INTERFACE(ICamera, data, reply); 268 int callback_flag = data.readInt32(); 269 setPreviewCallbackFlag(callback_flag); 270 return NO_ERROR; 271 } break; 272 case START_PREVIEW: { 273 LOGV("START_PREVIEW"); 274 CHECK_INTERFACE(ICamera, data, reply); 275 reply->writeInt32(startPreview()); 276 return NO_ERROR; 277 } break; 278 case START_RECORDING: { 279 LOGV("START_RECORDING"); 280 CHECK_INTERFACE(ICamera, data, reply); 281 reply->writeInt32(startRecording()); 282 return NO_ERROR; 283 } break; 284 case STOP_PREVIEW: { 285 LOGV("STOP_PREVIEW"); 286 CHECK_INTERFACE(ICamera, data, reply); 287 stopPreview(); 288 return NO_ERROR; 289 } break; 290 case STOP_RECORDING: { 291 LOGV("STOP_RECORDING"); 292 CHECK_INTERFACE(ICamera, data, reply); 293 stopRecording(); 294 return NO_ERROR; 295 } break; 296 case RELEASE_RECORDING_FRAME: { 297 LOGV("RELEASE_RECORDING_FRAME"); 298 CHECK_INTERFACE(ICamera, data, reply); 299 sp<IMemory> mem = interface_cast<IMemory>(data.readStrongBinder()); 300 releaseRecordingFrame(mem); 301 return NO_ERROR; 302 } break; 303 case PREVIEW_ENABLED: { 304 LOGV("PREVIEW_ENABLED"); 305 CHECK_INTERFACE(ICamera, data, reply); 306 reply->writeInt32(previewEnabled()); 307 return NO_ERROR; 308 } break; 309 case RECORDING_ENABLED: { 310 LOGV("RECORDING_ENABLED"); 311 CHECK_INTERFACE(ICamera, data, reply); 312 reply->writeInt32(recordingEnabled()); 313 return NO_ERROR; 314 } break; 315 case AUTO_FOCUS: { 316 LOGV("AUTO_FOCUS"); 317 CHECK_INTERFACE(ICamera, data, reply); 318 reply->writeInt32(autoFocus()); 319 return NO_ERROR; 320 } break; 321 case CANCEL_AUTO_FOCUS: { 322 LOGV("CANCEL_AUTO_FOCUS"); 323 CHECK_INTERFACE(ICamera, data, reply); 324 reply->writeInt32(cancelAutoFocus()); 325 return NO_ERROR; 326 } break; 327 case TAKE_PICTURE: { 328 LOGV("TAKE_PICTURE"); 329 CHECK_INTERFACE(ICamera, data, reply); 330 reply->writeInt32(takePicture()); 331 return NO_ERROR; 332 } break; 333 case SET_PARAMETERS: { 334 LOGV("SET_PARAMETERS"); 335 CHECK_INTERFACE(ICamera, data, reply); 336 String8 params(data.readString8()); 337 reply->writeInt32(setParameters(params)); 338 return NO_ERROR; 339 } break; 340 case GET_PARAMETERS: { 341 LOGV("GET_PARAMETERS"); 342 CHECK_INTERFACE(ICamera, data, reply); 343 reply->writeString8(getParameters()); 344 return NO_ERROR; 345 } break; 346 case SEND_COMMAND: { 347 LOGV("SEND_COMMAND"); 348 CHECK_INTERFACE(ICamera, data, reply); 349 int command = data.readInt32(); 350 int arg1 = data.readInt32(); 351 int arg2 = data.readInt32(); 352 reply->writeInt32(sendCommand(command, arg1, arg2)); 353 return NO_ERROR; 354 } break; 355 case CONNECT: { 356 CHECK_INTERFACE(ICamera, data, reply); 357 sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder()); 358 reply->writeInt32(connect(cameraClient)); 359 return NO_ERROR; 360 } break; 361 case LOCK: { 362 CHECK_INTERFACE(ICamera, data, reply); 363 reply->writeInt32(lock()); 364 return NO_ERROR; 365 } break; 366 case UNLOCK: { 367 CHECK_INTERFACE(ICamera, data, reply); 368 reply->writeInt32(unlock()); 369 return NO_ERROR; 370 } break; 371 default: 372 return BBinder::onTransact(code, data, reply, flags); 373 } 374 } 375 376 // ---------------------------------------------------------------------------- 377 378 }; // namespace android 379 380