blob: 1e5822fdafa0b97beba2bb0b0ded9ae875160581 [file] [log] [blame]
Igor Murashkine7ee7632013-06-11 18:10:18 -07001/*
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>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070024#include <camera/camera2/ICameraDeviceUser.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070025#include <gui/IGraphicBufferProducer.h>
26#include <gui/Surface.h>
27#include <camera/CameraMetadata.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070028#include <camera/camera2/CaptureRequest.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070029
30namespace android {
31
32typedef Parcel::WritableBlob WritableBlob;
33typedef Parcel::ReadableBlob ReadableBlob;
34
35enum {
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 He2ab500c2013-07-23 08:02:53 -070043 WAIT_UNTIL_IDLE,
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -070044 FLUSH
Igor Murashkine7ee7632013-06-11 18:10:18 -070045};
46
Igor Murashkin88aef232013-08-23 17:47:06 -070047namespace {
48 // Read empty strings without printing a false error message.
49 String16 readMaybeEmptyString16(const Parcel& parcel) {
50 size_t len;
51 const char16_t* str = parcel.readString16Inplace(&len);
52 if (str != NULL) {
53 return String16(str, len);
54 } else {
55 return String16();
56 }
57 }
58};
59
Igor Murashkine7ee7632013-06-11 18:10:18 -070060class BpCameraDeviceUser : public BpInterface<ICameraDeviceUser>
61{
62public:
63 BpCameraDeviceUser(const sp<IBinder>& impl)
64 : BpInterface<ICameraDeviceUser>(impl)
65 {
66 }
67
68 // disconnect from camera service
69 void disconnect()
70 {
71 ALOGV("disconnect");
72 Parcel data, reply;
73 data.writeInterfaceToken(ICameraDeviceUser::getInterfaceDescriptor());
74 remote()->transact(DISCONNECT, data, &reply);
75 reply.readExceptionCode();
76 }
77
78 virtual int submitRequest(sp<CaptureRequest> request, bool streaming)
79 {
80 Parcel data, reply;
81 data.writeInterfaceToken(ICameraDeviceUser::getInterfaceDescriptor());
82
83 // arg0 = CaptureRequest
84 if (request != 0) {
85 data.writeInt32(1);
86 request->writeToParcel(&data);
87 } else {
88 data.writeInt32(0);
89 }
90
91 // arg1 = streaming (bool)
92 data.writeInt32(streaming);
93
94 remote()->transact(SUBMIT_REQUEST, data, &reply);
95
96 reply.readExceptionCode();
97 return reply.readInt32();
98 }
99
100 virtual status_t cancelRequest(int requestId)
101 {
102 Parcel data, reply;
103 data.writeInterfaceToken(ICameraDeviceUser::getInterfaceDescriptor());
104 data.writeInt32(requestId);
105
106 remote()->transact(CANCEL_REQUEST, data, &reply);
107
108 reply.readExceptionCode();
109 return reply.readInt32();
110 }
111
112 virtual status_t deleteStream(int streamId)
113 {
114 Parcel data, reply;
115 data.writeInterfaceToken(ICameraDeviceUser::getInterfaceDescriptor());
116 data.writeInt32(streamId);
117
118 remote()->transact(DELETE_STREAM, data, &reply);
119
120 reply.readExceptionCode();
121 return reply.readInt32();
122 }
123
124 virtual status_t createStream(int width, int height, int format,
125 const sp<IGraphicBufferProducer>& bufferProducer)
126 {
127 Parcel data, reply;
128 data.writeInterfaceToken(ICameraDeviceUser::getInterfaceDescriptor());
129 data.writeInt32(width);
130 data.writeInt32(height);
131 data.writeInt32(format);
132
133 data.writeInt32(1); // marker that bufferProducer is not null
134 data.writeString16(String16("unknown_name")); // name of surface
135 sp<IBinder> b(bufferProducer->asBinder());
136 data.writeStrongBinder(b);
137
138 remote()->transact(CREATE_STREAM, data, &reply);
139
140 reply.readExceptionCode();
141 return reply.readInt32();
142 }
143
144 // Create a request object from a template.
145 virtual status_t createDefaultRequest(int templateId,
146 /*out*/
147 CameraMetadata* request)
148 {
149 Parcel data, reply;
150 data.writeInterfaceToken(ICameraDeviceUser::getInterfaceDescriptor());
151 data.writeInt32(templateId);
152 remote()->transact(CREATE_DEFAULT_REQUEST, data, &reply);
153
154 reply.readExceptionCode();
155 status_t result = reply.readInt32();
156
157 CameraMetadata out;
158 if (reply.readInt32() != 0) {
159 out.readFromParcel(&reply);
160 }
161
162 if (request != NULL) {
163 request->swap(out);
164 }
165 return result;
166 }
167
168
Igor Murashkin099b4572013-07-12 17:52:16 -0700169 virtual status_t getCameraInfo(CameraMetadata* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700170 {
171 Parcel data, reply;
172 data.writeInterfaceToken(ICameraDeviceUser::getInterfaceDescriptor());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700173 remote()->transact(GET_CAMERA_INFO, data, &reply);
174
Igor Murashkine7ee7632013-06-11 18:10:18 -0700175 reply.readExceptionCode();
176 status_t result = reply.readInt32();
177
Igor Murashkin099b4572013-07-12 17:52:16 -0700178 CameraMetadata out;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700179 if (reply.readInt32() != 0) {
Igor Murashkin099b4572013-07-12 17:52:16 -0700180 out.readFromParcel(&reply);
181 }
182
183 if (info != NULL) {
184 info->swap(out);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700185 }
186
187 return result;
188 }
189
Zhijun He2ab500c2013-07-23 08:02:53 -0700190 virtual status_t waitUntilIdle()
191 {
192 ALOGV("waitUntilIdle");
193 Parcel data, reply;
194 data.writeInterfaceToken(ICameraDeviceUser::getInterfaceDescriptor());
195 remote()->transact(WAIT_UNTIL_IDLE, data, &reply);
196 reply.readExceptionCode();
197 return reply.readInt32();
198 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700199
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700200 virtual status_t flush()
201 {
202 ALOGV("flush");
203 Parcel data, reply;
204 data.writeInterfaceToken(ICameraDeviceUser::getInterfaceDescriptor());
205 remote()->transact(FLUSH, data, &reply);
206 reply.readExceptionCode();
207 return reply.readInt32();
208 }
209
Igor Murashkine7ee7632013-06-11 18:10:18 -0700210private:
211
212
213};
214
215IMPLEMENT_META_INTERFACE(CameraDeviceUser,
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700216 "android.hardware.camera2.ICameraDeviceUser");
Igor Murashkine7ee7632013-06-11 18:10:18 -0700217
218// ----------------------------------------------------------------------
219
220status_t BnCameraDeviceUser::onTransact(
221 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
222{
223 switch(code) {
224 case DISCONNECT: {
225 ALOGV("DISCONNECT");
226 CHECK_INTERFACE(ICameraDeviceUser, data, reply);
227 disconnect();
228 reply->writeNoException();
229 return NO_ERROR;
230 } break;
231 case SUBMIT_REQUEST: {
232 CHECK_INTERFACE(ICameraDeviceUser, data, reply);
233
234 // arg0 = request
235 sp<CaptureRequest> request;
236 if (data.readInt32() != 0) {
237 request = new CaptureRequest();
238 request->readFromParcel(const_cast<Parcel*>(&data));
239 }
240
241 // arg1 = streaming (bool)
242 bool streaming = data.readInt32();
243
244 // return code: requestId (int32)
245 reply->writeNoException();
246 reply->writeInt32(submitRequest(request, streaming));
247
248 return NO_ERROR;
249 } break;
250 case CANCEL_REQUEST: {
251 CHECK_INTERFACE(ICameraDeviceUser, data, reply);
252 int requestId = data.readInt32();
253 reply->writeNoException();
254 reply->writeInt32(cancelRequest(requestId));
255 return NO_ERROR;
256 } break;
257 case DELETE_STREAM: {
258 CHECK_INTERFACE(ICameraDeviceUser, data, reply);
259 int streamId = data.readInt32();
260 reply->writeNoException();
261 reply->writeInt32(deleteStream(streamId));
262 return NO_ERROR;
263 } break;
264 case CREATE_STREAM: {
265 CHECK_INTERFACE(ICameraDeviceUser, data, reply);
266 int width, height, format;
267
268 width = data.readInt32();
269 ALOGV("%s: CREATE_STREAM: width = %d", __FUNCTION__, width);
270 height = data.readInt32();
271 ALOGV("%s: CREATE_STREAM: height = %d", __FUNCTION__, height);
272 format = data.readInt32();
273 ALOGV("%s: CREATE_STREAM: format = %d", __FUNCTION__, format);
274
275 sp<IGraphicBufferProducer> bp;
276 if (data.readInt32() != 0) {
Igor Murashkin88aef232013-08-23 17:47:06 -0700277 String16 name = readMaybeEmptyString16(data);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700278 bp = interface_cast<IGraphicBufferProducer>(
279 data.readStrongBinder());
280
281 ALOGV("%s: CREATE_STREAM: bp = %p, name = %s", __FUNCTION__,
282 bp.get(), String8(name).string());
283 } else {
284 ALOGV("%s: CREATE_STREAM: bp = unset, name = unset",
285 __FUNCTION__);
286 }
287
288 status_t ret;
289 ret = createStream(width, height, format, bp);
290
291 reply->writeNoException();
292 ALOGV("%s: CREATE_STREAM: write noException", __FUNCTION__);
293 reply->writeInt32(ret);
294 ALOGV("%s: CREATE_STREAM: write ret = %d", __FUNCTION__, ret);
295
296 return NO_ERROR;
297 } break;
298
299 case CREATE_DEFAULT_REQUEST: {
300 CHECK_INTERFACE(ICameraDeviceUser, data, reply);
301
302 int templateId = data.readInt32();
303
304 CameraMetadata request;
305 status_t ret;
306 ret = createDefaultRequest(templateId, &request);
307
308 reply->writeNoException();
309 reply->writeInt32(ret);
310
Igor Murashkin099b4572013-07-12 17:52:16 -0700311 // out-variables are after exception and return value
Igor Murashkine7ee7632013-06-11 18:10:18 -0700312 reply->writeInt32(1); // to mark presence of metadata object
313 request.writeToParcel(const_cast<Parcel*>(reply));
314
315 return NO_ERROR;
316 } break;
317 case GET_CAMERA_INFO: {
318 CHECK_INTERFACE(ICameraDeviceUser, data, reply);
319
Igor Murashkin099b4572013-07-12 17:52:16 -0700320 CameraMetadata info;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700321 status_t ret;
Igor Murashkin099b4572013-07-12 17:52:16 -0700322 ret = getCameraInfo(&info);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700323
324 reply->writeNoException();
325 reply->writeInt32(ret);
326
Igor Murashkin099b4572013-07-12 17:52:16 -0700327 // out-variables are after exception and return value
328 reply->writeInt32(1); // to mark presence of metadata object
329 info.writeToParcel(reply);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700330
331 return NO_ERROR;
332 } break;
Zhijun He2ab500c2013-07-23 08:02:53 -0700333 case WAIT_UNTIL_IDLE: {
334 CHECK_INTERFACE(ICameraDeviceUser, data, reply);
335 reply->writeNoException();
336 reply->writeInt32(waitUntilIdle());
337 return NO_ERROR;
338 } break;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700339 case FLUSH: {
340 CHECK_INTERFACE(ICameraDeviceUser, data, reply);
341 reply->writeNoException();
342 reply->writeInt32(flush());
343 return NO_ERROR;
344 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700345 default:
346 return BBinder::onTransact(code, data, reply, flags);
347 }
348}
349
350// ----------------------------------------------------------------------------
351
352}; // namespace android