blob: 0515bd712f91fadab85201e2f179a1c2d7356653 [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>
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
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,
43};
44
45class BpCameraDeviceUser : public BpInterface<ICameraDeviceUser>
46{
47public:
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
154 virtual status_t getCameraInfo(int cameraId, camera_metadata** info)
155 {
156 Parcel data, reply;
157 data.writeInterfaceToken(ICameraDeviceUser::getInterfaceDescriptor());
158 data.writeInt32(cameraId);
159 remote()->transact(GET_CAMERA_INFO, data, &reply);
160
161
162 reply.readExceptionCode();
163 status_t result = reply.readInt32();
164
165 if (reply.readInt32() != 0) {
166 CameraMetadata::readFromParcel(reply, /*out*/info);
167 } else if (info) {
168 *info = NULL;
169 }
170
171 return result;
172 }
173
174
175private:
176
177
178};
179
180IMPLEMENT_META_INTERFACE(CameraDeviceUser,
181 "android.hardware.photography.ICameraDeviceUser");
182
183// ----------------------------------------------------------------------
184
185status_t BnCameraDeviceUser::onTransact(
186 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
187{
188 switch(code) {
189 case DISCONNECT: {
190 ALOGV("DISCONNECT");
191 CHECK_INTERFACE(ICameraDeviceUser, data, reply);
192 disconnect();
193 reply->writeNoException();
194 return NO_ERROR;
195 } break;
196 case SUBMIT_REQUEST: {
197 CHECK_INTERFACE(ICameraDeviceUser, data, reply);
198
199 // arg0 = request
200 sp<CaptureRequest> request;
201 if (data.readInt32() != 0) {
202 request = new CaptureRequest();
203 request->readFromParcel(const_cast<Parcel*>(&data));
204 }
205
206 // arg1 = streaming (bool)
207 bool streaming = data.readInt32();
208
209 // return code: requestId (int32)
210 reply->writeNoException();
211 reply->writeInt32(submitRequest(request, streaming));
212
213 return NO_ERROR;
214 } break;
215 case CANCEL_REQUEST: {
216 CHECK_INTERFACE(ICameraDeviceUser, data, reply);
217 int requestId = data.readInt32();
218 reply->writeNoException();
219 reply->writeInt32(cancelRequest(requestId));
220 return NO_ERROR;
221 } break;
222 case DELETE_STREAM: {
223 CHECK_INTERFACE(ICameraDeviceUser, data, reply);
224 int streamId = data.readInt32();
225 reply->writeNoException();
226 reply->writeInt32(deleteStream(streamId));
227 return NO_ERROR;
228 } break;
229 case CREATE_STREAM: {
230 CHECK_INTERFACE(ICameraDeviceUser, data, reply);
231 int width, height, format;
232
233 width = data.readInt32();
234 ALOGV("%s: CREATE_STREAM: width = %d", __FUNCTION__, width);
235 height = data.readInt32();
236 ALOGV("%s: CREATE_STREAM: height = %d", __FUNCTION__, height);
237 format = data.readInt32();
238 ALOGV("%s: CREATE_STREAM: format = %d", __FUNCTION__, format);
239
240 sp<IGraphicBufferProducer> bp;
241 if (data.readInt32() != 0) {
242 String16 name = data.readString16();
243 bp = interface_cast<IGraphicBufferProducer>(
244 data.readStrongBinder());
245
246 ALOGV("%s: CREATE_STREAM: bp = %p, name = %s", __FUNCTION__,
247 bp.get(), String8(name).string());
248 } else {
249 ALOGV("%s: CREATE_STREAM: bp = unset, name = unset",
250 __FUNCTION__);
251 }
252
253 status_t ret;
254 ret = createStream(width, height, format, bp);
255
256 reply->writeNoException();
257 ALOGV("%s: CREATE_STREAM: write noException", __FUNCTION__);
258 reply->writeInt32(ret);
259 ALOGV("%s: CREATE_STREAM: write ret = %d", __FUNCTION__, ret);
260
261 return NO_ERROR;
262 } break;
263
264 case CREATE_DEFAULT_REQUEST: {
265 CHECK_INTERFACE(ICameraDeviceUser, data, reply);
266
267 int templateId = data.readInt32();
268
269 CameraMetadata request;
270 status_t ret;
271 ret = createDefaultRequest(templateId, &request);
272
273 reply->writeNoException();
274 reply->writeInt32(ret);
275
276 reply->writeInt32(1); // to mark presence of metadata object
277 request.writeToParcel(const_cast<Parcel*>(reply));
278
279 return NO_ERROR;
280 } break;
281 case GET_CAMERA_INFO: {
282 CHECK_INTERFACE(ICameraDeviceUser, data, reply);
283
284 int cameraId = data.readInt32();
285
286 camera_metadata_t* info = NULL;
287 status_t ret;
288 ret = getCameraInfo(cameraId, &info);
289
290 reply->writeInt32(1); // to mark presence of metadata object
291 CameraMetadata::writeToParcel(*reply, info);
292
293 reply->writeNoException();
294 reply->writeInt32(ret);
295
296 free_camera_metadata(info);
297
298 return NO_ERROR;
299 } break;
300 default:
301 return BBinder::onTransact(code, data, reply, flags);
302 }
303}
304
305// ----------------------------------------------------------------------------
306
307}; // namespace android