blob: 8620f36022f979a77c7108b39ad5690b06f11084 [file] [log] [blame]
Mathias Agopian3cf61352010-02-09 17:46:37 -08001/*
2**
3** Copyright 2008, The Android Open Source Project
4**
Praveen Chavan6773d472016-01-13 01:24:30 -08005** 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
Mathias Agopian3cf61352010-02-09 17:46:37 -08008**
Praveen Chavan6773d472016-01-13 01:24:30 -08009** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian3cf61352010-02-09 17:46:37 -080010**
Praveen Chavan6773d472016-01-13 01:24:30 -080011** 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
Mathias Agopian3cf61352010-02-09 17:46:37 -080015** 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>
Praveen Chavan6773d472016-01-13 01:24:30 -080023#include <camera/CameraUtils.h>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080024#include <android/hardware/ICameraClient.h>
Praveen Chavan6773d472016-01-13 01:24:30 -080025#include <media/hardware/HardwareAPI.h>
Mathias Agopian3cf61352010-02-09 17:46:37 -080026
27namespace android {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080028namespace hardware {
Mathias Agopian3cf61352010-02-09 17:46:37 -080029
30enum {
31 NOTIFY_CALLBACK = IBinder::FIRST_CALL_TRANSACTION,
32 DATA_CALLBACK,
33 DATA_CALLBACK_TIMESTAMP,
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -070034 RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP,
Yin-Chia Yehb5df5472017-03-20 19:32:19 -070035 RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP_BATCH,
Mathias Agopian3cf61352010-02-09 17:46:37 -080036};
37
38class BpCameraClient: public BpInterface<ICameraClient>
39{
40public:
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070041 explicit BpCameraClient(const sp<IBinder>& impl)
Mathias Agopian3cf61352010-02-09 17:46:37 -080042 : BpInterface<ICameraClient>(impl)
43 {
44 }
45
46 // generic callback from camera service to app
47 void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
48 {
Steve Block3856b092011-10-20 11:56:00 +010049 ALOGV("notifyCallback");
Mathias Agopian3cf61352010-02-09 17:46:37 -080050 Parcel data, reply;
51 data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor());
52 data.writeInt32(msgType);
53 data.writeInt32(ext1);
54 data.writeInt32(ext2);
55 remote()->transact(NOTIFY_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY);
56 }
57
58 // generic data callback from camera service to app with image data
Wu-cheng Li57c86182011-07-30 05:00:37 +080059 void dataCallback(int32_t msgType, const sp<IMemory>& imageData,
60 camera_frame_metadata_t *metadata)
Mathias Agopian3cf61352010-02-09 17:46:37 -080061 {
Steve Block3856b092011-10-20 11:56:00 +010062 ALOGV("dataCallback");
Mathias Agopian3cf61352010-02-09 17:46:37 -080063 Parcel data, reply;
64 data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor());
65 data.writeInt32(msgType);
Marco Nelissen06b46062014-11-14 07:58:25 -080066 data.writeStrongBinder(IInterface::asBinder(imageData));
Wu-cheng Li57c86182011-07-30 05:00:37 +080067 if (metadata) {
68 data.writeInt32(metadata->number_of_faces);
69 data.write(metadata->faces, sizeof(camera_face_t) * metadata->number_of_faces);
70 }
Mathias Agopian3cf61352010-02-09 17:46:37 -080071 remote()->transact(DATA_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY);
72 }
73
74 // generic data callback from camera service to app with image data
75 void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& imageData)
76 {
Steve Block3856b092011-10-20 11:56:00 +010077 ALOGV("dataCallback");
Mathias Agopian3cf61352010-02-09 17:46:37 -080078 Parcel data, reply;
79 data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor());
80 data.writeInt64(timestamp);
81 data.writeInt32(msgType);
Marco Nelissen06b46062014-11-14 07:58:25 -080082 data.writeStrongBinder(IInterface::asBinder(imageData));
Mathias Agopian3cf61352010-02-09 17:46:37 -080083 remote()->transact(DATA_CALLBACK_TIMESTAMP, data, &reply, IBinder::FLAG_ONEWAY);
84 }
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -070085
86 void recordingFrameHandleCallbackTimestamp(nsecs_t timestamp, native_handle_t* handle) {
87 ALOGV("recordingFrameHandleCallbackTimestamp");
88 Parcel data, reply;
89 data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor());
90 data.writeInt64(timestamp);
91 data.writeNativeHandle(handle);
92 remote()->transact(RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP, data, &reply,
93 IBinder::FLAG_ONEWAY);
94 }
Yin-Chia Yehb5df5472017-03-20 19:32:19 -070095
96 void recordingFrameHandleCallbackTimestampBatch(
97 const std::vector<nsecs_t>& timestamps,
98 const std::vector<native_handle_t*>& handles) {
99 ALOGV("recordingFrameHandleCallbackTimestampBatch");
100 Parcel data, reply;
101 data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor());
102 uint32_t n = timestamps.size();
103 if (n != handles.size()) {
104 ALOGE("%s: size of timestamps(%zu) and handles(%zu) mismatch!",
105 __FUNCTION__, timestamps.size(), handles.size());
106 return;
107 }
108 data.writeUint32(n);
109 for (auto ts : timestamps) {
110 data.writeInt64(ts);
111 }
112 for (auto& handle : handles) {
113 data.writeNativeHandle(handle);
114 }
115 remote()->transact(RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP_BATCH, data, &reply,
116 IBinder::FLAG_ONEWAY);
117 }
Mathias Agopian3cf61352010-02-09 17:46:37 -0800118};
119
120IMPLEMENT_META_INTERFACE(CameraClient, "android.hardware.ICameraClient");
121
122// ----------------------------------------------------------------------
123
124status_t BnCameraClient::onTransact(
125 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
126{
127 switch(code) {
128 case NOTIFY_CALLBACK: {
Steve Block3856b092011-10-20 11:56:00 +0100129 ALOGV("NOTIFY_CALLBACK");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800130 CHECK_INTERFACE(ICameraClient, data, reply);
131 int32_t msgType = data.readInt32();
132 int32_t ext1 = data.readInt32();
133 int32_t ext2 = data.readInt32();
134 notifyCallback(msgType, ext1, ext2);
135 return NO_ERROR;
136 } break;
137 case DATA_CALLBACK: {
Steve Block3856b092011-10-20 11:56:00 +0100138 ALOGV("DATA_CALLBACK");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800139 CHECK_INTERFACE(ICameraClient, data, reply);
140 int32_t msgType = data.readInt32();
141 sp<IMemory> imageData = interface_cast<IMemory>(data.readStrongBinder());
Wu-cheng Li57c86182011-07-30 05:00:37 +0800142 camera_frame_metadata_t *metadata = NULL;
143 if (data.dataAvail() > 0) {
144 metadata = new camera_frame_metadata_t;
145 metadata->number_of_faces = data.readInt32();
146 metadata->faces = (camera_face_t *) data.readInplace(
147 sizeof(camera_face_t) * metadata->number_of_faces);
148 }
149 dataCallback(msgType, imageData, metadata);
150 if (metadata) delete metadata;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800151 return NO_ERROR;
152 } break;
153 case DATA_CALLBACK_TIMESTAMP: {
Steve Block3856b092011-10-20 11:56:00 +0100154 ALOGV("DATA_CALLBACK_TIMESTAMP");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800155 CHECK_INTERFACE(ICameraClient, data, reply);
156 nsecs_t timestamp = data.readInt64();
157 int32_t msgType = data.readInt32();
158 sp<IMemory> imageData = interface_cast<IMemory>(data.readStrongBinder());
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700159 dataCallbackTimestamp(timestamp, msgType, imageData);
160 return NO_ERROR;
161 } break;
162 case RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP: {
163 ALOGV("RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP");
164 CHECK_INTERFACE(ICameraClient, data, reply);
165 nsecs_t timestamp;
166 status_t res = data.readInt64(&timestamp);
167 if (res != OK) {
168 ALOGE("%s: Failed to read timestamp: %s (%d)", __FUNCTION__, strerror(-res), res);
169 return BAD_VALUE;
170 }
171 native_handle_t* handle = data.readNativeHandle();
172 if (handle == nullptr) {
173 ALOGE("%s: Received a null native handle", __FUNCTION__);
174 return BAD_VALUE;
Praveen Chavan6773d472016-01-13 01:24:30 -0800175 }
176
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700177 // The native handle will be freed in BpCamera::releaseRecordingFrameHandle.
178 recordingFrameHandleCallbackTimestamp(timestamp, handle);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800179 return NO_ERROR;
180 } break;
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700181 case RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP_BATCH: {
182 ALOGV("RECORDING_FRAME_HANDLE_CALLBACK_TIMESTAMP_BATCH");
183 CHECK_INTERFACE(ICameraClient, data, reply);
184 uint32_t n = 0;
185 status_t res = data.readUint32(&n);
186 if (res != OK) {
187 ALOGE("%s: Failed to read batch size: %s (%d)", __FUNCTION__, strerror(-res), res);
188 return BAD_VALUE;
189 }
190 std::vector<nsecs_t> timestamps;
191 std::vector<native_handle_t*> handles;
192 timestamps.reserve(n);
193 handles.reserve(n);
194 for (uint32_t i = 0; i < n; i++) {
Yin-Chia Yehe387edc2017-09-29 14:13:28 -0700195 nsecs_t t;
196 res = data.readInt64(&t);
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700197 if (res != OK) {
198 ALOGE("%s: Failed to read timestamp[%d]: %s (%d)",
199 __FUNCTION__, i, strerror(-res), res);
200 return BAD_VALUE;
201 }
Yin-Chia Yehe387edc2017-09-29 14:13:28 -0700202 timestamps.push_back(t);
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700203 }
204 for (uint32_t i = 0; i < n; i++) {
205 native_handle_t* handle = data.readNativeHandle();
206 if (handle == nullptr) {
207 ALOGE("%s: Received a null native handle at handles[%d]",
208 __FUNCTION__, i);
209 return BAD_VALUE;
210 }
211 handles.push_back(handle);
212 }
213
214 // The native handle will be freed in BpCamera::releaseRecordingFrameHandleBatch.
215 recordingFrameHandleCallbackTimestampBatch(timestamps, handles);
216 return NO_ERROR;
217 } break;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800218 default:
219 return BBinder::onTransact(code, data, reply, flags);
220 }
221}
222
223// ----------------------------------------------------------------------------
224
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800225} // namespace hardware
226} // namespace android