ICameraClient.cpp revision d56db1d2bee182d1851097a9c712712fc094d117
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 "ICameraClient" 20#include <utils/Log.h> 21#include <stdint.h> 22#include <sys/types.h> 23#include <camera/CameraUtils.h> 24#include <android/hardware/ICameraClient.h> 25#include <media/hardware/HardwareAPI.h> 26 27namespace android { 28namespace hardware { 29 30enum { 31 NOTIFY_CALLBACK = IBinder::FIRST_CALL_TRANSACTION, 32 DATA_CALLBACK, 33 DATA_CALLBACK_TIMESTAMP, 34}; 35 36class BpCameraClient: public BpInterface<ICameraClient> 37{ 38public: 39 BpCameraClient(const sp<IBinder>& impl) 40 : BpInterface<ICameraClient>(impl) 41 { 42 } 43 44 // generic callback from camera service to app 45 void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2) 46 { 47 ALOGV("notifyCallback"); 48 Parcel data, reply; 49 data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor()); 50 data.writeInt32(msgType); 51 data.writeInt32(ext1); 52 data.writeInt32(ext2); 53 remote()->transact(NOTIFY_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY); 54 } 55 56 // generic data callback from camera service to app with image data 57 void dataCallback(int32_t msgType, const sp<IMemory>& imageData, 58 camera_frame_metadata_t *metadata) 59 { 60 ALOGV("dataCallback"); 61 Parcel data, reply; 62 data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor()); 63 data.writeInt32(msgType); 64 data.writeStrongBinder(IInterface::asBinder(imageData)); 65 if (metadata) { 66 data.writeInt32(metadata->number_of_faces); 67 data.write(metadata->faces, sizeof(camera_face_t) * metadata->number_of_faces); 68 } 69 remote()->transact(DATA_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY); 70 } 71 72 // generic data callback from camera service to app with image data 73 void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& imageData) 74 { 75 ALOGV("dataCallback"); 76 Parcel data, reply; 77 data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor()); 78 data.writeInt64(timestamp); 79 data.writeInt32(msgType); 80 data.writeStrongBinder(IInterface::asBinder(imageData)); 81 // If imageData is metadata and it contains a native handle, write the native handle to 82 // parcel. 83 if (CameraUtils::isNativeHandleMetadata(imageData)) { 84 VideoNativeHandleMetadata *metadata = 85 (VideoNativeHandleMetadata*)(imageData->pointer()); 86 data.writeNativeHandle(metadata->pHandle); 87 } 88 remote()->transact(DATA_CALLBACK_TIMESTAMP, data, &reply, IBinder::FLAG_ONEWAY); 89 } 90}; 91 92IMPLEMENT_META_INTERFACE(CameraClient, "android.hardware.ICameraClient"); 93 94// ---------------------------------------------------------------------- 95 96status_t BnCameraClient::onTransact( 97 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 98{ 99 switch(code) { 100 case NOTIFY_CALLBACK: { 101 ALOGV("NOTIFY_CALLBACK"); 102 CHECK_INTERFACE(ICameraClient, data, reply); 103 int32_t msgType = data.readInt32(); 104 int32_t ext1 = data.readInt32(); 105 int32_t ext2 = data.readInt32(); 106 notifyCallback(msgType, ext1, ext2); 107 return NO_ERROR; 108 } break; 109 case DATA_CALLBACK: { 110 ALOGV("DATA_CALLBACK"); 111 CHECK_INTERFACE(ICameraClient, data, reply); 112 int32_t msgType = data.readInt32(); 113 sp<IMemory> imageData = interface_cast<IMemory>(data.readStrongBinder()); 114 camera_frame_metadata_t *metadata = NULL; 115 if (data.dataAvail() > 0) { 116 metadata = new camera_frame_metadata_t; 117 metadata->number_of_faces = data.readInt32(); 118 metadata->faces = (camera_face_t *) data.readInplace( 119 sizeof(camera_face_t) * metadata->number_of_faces); 120 } 121 dataCallback(msgType, imageData, metadata); 122 if (metadata) delete metadata; 123 return NO_ERROR; 124 } break; 125 case DATA_CALLBACK_TIMESTAMP: { 126 ALOGV("DATA_CALLBACK_TIMESTAMP"); 127 CHECK_INTERFACE(ICameraClient, data, reply); 128 nsecs_t timestamp = data.readInt64(); 129 int32_t msgType = data.readInt32(); 130 sp<IMemory> imageData = interface_cast<IMemory>(data.readStrongBinder()); 131 132 // If the image data contains a native handle, read the native handle from the parcel 133 // and replace the native handle in the image data. (The native handle in image data is 134 // not serielized/deserialized so it's not valid in the process.) 135 if (CameraUtils::isNativeHandleMetadata(imageData)) { 136 VideoNativeHandleMetadata *metadata = 137 (VideoNativeHandleMetadata*)(imageData->pointer()); 138 metadata->pHandle = data.readNativeHandle(); 139 140 // The native handle will be freed in 141 // BpCameraRecordingProxyListener::releaseRecordingFrame. 142 } 143 144 dataCallbackTimestamp(timestamp, msgType, imageData); 145 return NO_ERROR; 146 } break; 147 default: 148 return BBinder::onTransact(code, data, reply, flags); 149 } 150} 151 152// ---------------------------------------------------------------------------- 153 154} // namespace hardware 155} // namespace android 156