3 ** Copyright 2008, The Android Open Source Project
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
9 ** http://www.apache.org/licenses/LICENSE-2.0
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.
18 //#define LOG_NDEBUG 0
19 #define LOG_TAG "ICamera"
20 #include <utils/Log.h>
22 #include <sys/types.h>
23 #include <binder/Parcel.h>
24 #include <camera/CameraUtils.h>
25 #include <android/hardware/ICamera.h>
26 #include <android/hardware/ICameraClient.h>
27 #include <gui/IGraphicBufferProducer.h>
28 #include <gui/Surface.h>
29 #include <media/hardware/HardwareAPI.h>
35 DISCONNECT = IBinder::FIRST_CALL_TRANSACTION,
37 SET_PREVIEW_CALLBACK_FLAG,
38 SET_PREVIEW_CALLBACK_TARGET,
54 RELEASE_RECORDING_FRAME,
55 SET_VIDEO_BUFFER_MODE,
56 SET_VIDEO_BUFFER_TARGET,
57 RELEASE_RECORDING_FRAME_HANDLE,
60 class BpCamera: public BpInterface<ICamera>
63 BpCamera(const sp<IBinder>& impl)
64 : BpInterface<ICamera>(impl)
68 // disconnect from camera service
69 binder::Status disconnect()
73 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
74 remote()->transact(DISCONNECT, data, &reply);
75 reply.readExceptionCode();
76 return binder::Status::ok();
79 // pass the buffered IGraphicBufferProducer to the camera service
80 status_t setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)
82 ALOGV("setPreviewTarget");
84 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
85 sp<IBinder> b(IInterface::asBinder(bufferProducer));
86 data.writeStrongBinder(b);
87 remote()->transact(SET_PREVIEW_TARGET, data, &reply);
88 return reply.readInt32();
91 // set the preview callback flag to affect how the received frames from
92 // preview are handled. See Camera.h for details.
93 void setPreviewCallbackFlag(int flag)
95 ALOGV("setPreviewCallbackFlag(%d)", flag);
97 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
98 data.writeInt32(flag);
99 remote()->transact(SET_PREVIEW_CALLBACK_FLAG, data, &reply);
102 status_t setPreviewCallbackTarget(
103 const sp<IGraphicBufferProducer>& callbackProducer)
105 ALOGV("setPreviewCallbackTarget");
107 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
108 sp<IBinder> b(IInterface::asBinder(callbackProducer));
109 data.writeStrongBinder(b);
110 remote()->transact(SET_PREVIEW_CALLBACK_TARGET, data, &reply);
111 return reply.readInt32();
114 // start preview mode, must call setPreviewTarget first
115 status_t startPreview()
117 ALOGV("startPreview");
119 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
120 remote()->transact(START_PREVIEW, data, &reply);
121 return reply.readInt32();
124 // start recording mode, must call setPreviewTarget first
125 status_t startRecording()
127 ALOGV("startRecording");
129 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
130 remote()->transact(START_RECORDING, data, &reply);
131 return reply.readInt32();
137 ALOGV("stopPreview");
139 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
140 remote()->transact(STOP_PREVIEW, data, &reply);
143 // stop recording mode
146 ALOGV("stopRecording");
148 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
149 remote()->transact(STOP_RECORDING, data, &reply);
152 void releaseRecordingFrame(const sp<IMemory>& mem)
154 ALOGV("releaseRecordingFrame");
156 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
157 data.writeStrongBinder(IInterface::asBinder(mem));
159 remote()->transact(RELEASE_RECORDING_FRAME, data, &reply);
162 void releaseRecordingFrameHandle(native_handle_t *handle) {
163 ALOGV("releaseRecordingFrameHandle");
165 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
166 data.writeNativeHandle(handle);
168 remote()->transact(RELEASE_RECORDING_FRAME_HANDLE, data, &reply);
170 // Close the native handle because camera received a dup copy.
171 native_handle_close(handle);
172 native_handle_delete(handle);
175 status_t setVideoBufferMode(int32_t videoBufferMode)
177 ALOGV("setVideoBufferMode: %d", videoBufferMode);
179 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
180 data.writeInt32(videoBufferMode);
181 remote()->transact(SET_VIDEO_BUFFER_MODE, data, &reply);
182 return reply.readInt32();
185 // check preview state
186 bool previewEnabled()
188 ALOGV("previewEnabled");
190 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
191 remote()->transact(PREVIEW_ENABLED, data, &reply);
192 return reply.readInt32();
195 // check recording state
196 bool recordingEnabled()
198 ALOGV("recordingEnabled");
200 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
201 remote()->transact(RECORDING_ENABLED, data, &reply);
202 return reply.readInt32();
210 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
211 remote()->transact(AUTO_FOCUS, data, &reply);
212 status_t ret = reply.readInt32();
217 status_t cancelAutoFocus()
219 ALOGV("cancelAutoFocus");
221 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
222 remote()->transact(CANCEL_AUTO_FOCUS, data, &reply);
223 status_t ret = reply.readInt32();
227 // take a picture - returns an IMemory (ref-counted mmap)
228 status_t takePicture(int msgType)
230 ALOGV("takePicture: 0x%x", msgType);
232 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
233 data.writeInt32(msgType);
234 remote()->transact(TAKE_PICTURE, data, &reply);
235 status_t ret = reply.readInt32();
239 // set preview/capture parameters - key/value pairs
240 status_t setParameters(const String8& params)
242 ALOGV("setParameters");
244 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
245 data.writeString8(params);
246 remote()->transact(SET_PARAMETERS, data, &reply);
247 return reply.readInt32();
250 // get preview/capture parameters - key/value pairs
251 String8 getParameters() const
253 ALOGV("getParameters");
255 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
256 remote()->transact(GET_PARAMETERS, data, &reply);
257 return reply.readString8();
259 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
261 ALOGV("sendCommand");
263 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
264 data.writeInt32(cmd);
265 data.writeInt32(arg1);
266 data.writeInt32(arg2);
267 remote()->transact(SEND_COMMAND, data, &reply);
268 return reply.readInt32();
270 virtual status_t connect(const sp<ICameraClient>& cameraClient)
273 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
274 data.writeStrongBinder(IInterface::asBinder(cameraClient));
275 remote()->transact(CONNECT, data, &reply);
276 return reply.readInt32();
278 virtual status_t lock()
281 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
282 remote()->transact(LOCK, data, &reply);
283 return reply.readInt32();
285 virtual status_t unlock()
288 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
289 remote()->transact(UNLOCK, data, &reply);
290 return reply.readInt32();
293 status_t setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer)
295 ALOGV("setVideoTarget");
297 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
298 sp<IBinder> b(IInterface::asBinder(bufferProducer));
299 data.writeStrongBinder(b);
300 remote()->transact(SET_VIDEO_BUFFER_TARGET, data, &reply);
301 return reply.readInt32();
305 IMPLEMENT_META_INTERFACE(Camera, "android.hardware.ICamera");
307 // ----------------------------------------------------------------------
309 status_t BnCamera::onTransact(
310 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
315 CHECK_INTERFACE(ICamera, data, reply);
317 reply->writeNoException();
320 case SET_PREVIEW_TARGET: {
321 ALOGV("SET_PREVIEW_TARGET");
322 CHECK_INTERFACE(ICamera, data, reply);
323 sp<IGraphicBufferProducer> st =
324 interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
325 reply->writeInt32(setPreviewTarget(st));
328 case SET_PREVIEW_CALLBACK_FLAG: {
329 ALOGV("SET_PREVIEW_CALLBACK_TYPE");
330 CHECK_INTERFACE(ICamera, data, reply);
331 int callback_flag = data.readInt32();
332 setPreviewCallbackFlag(callback_flag);
335 case SET_PREVIEW_CALLBACK_TARGET: {
336 ALOGV("SET_PREVIEW_CALLBACK_TARGET");
337 CHECK_INTERFACE(ICamera, data, reply);
338 sp<IGraphicBufferProducer> cp =
339 interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
340 reply->writeInt32(setPreviewCallbackTarget(cp));
343 case START_PREVIEW: {
344 ALOGV("START_PREVIEW");
345 CHECK_INTERFACE(ICamera, data, reply);
346 reply->writeInt32(startPreview());
349 case START_RECORDING: {
350 ALOGV("START_RECORDING");
351 CHECK_INTERFACE(ICamera, data, reply);
352 reply->writeInt32(startRecording());
356 ALOGV("STOP_PREVIEW");
357 CHECK_INTERFACE(ICamera, data, reply);
361 case STOP_RECORDING: {
362 ALOGV("STOP_RECORDING");
363 CHECK_INTERFACE(ICamera, data, reply);
367 case RELEASE_RECORDING_FRAME: {
368 ALOGV("RELEASE_RECORDING_FRAME");
369 CHECK_INTERFACE(ICamera, data, reply);
370 sp<IMemory> mem = interface_cast<IMemory>(data.readStrongBinder());
371 releaseRecordingFrame(mem);
374 case RELEASE_RECORDING_FRAME_HANDLE: {
375 ALOGV("RELEASE_RECORDING_FRAME_HANDLE");
376 CHECK_INTERFACE(ICamera, data, reply);
377 // releaseRecordingFrameHandle will be responsble to close the native handle.
378 releaseRecordingFrameHandle(data.readNativeHandle());
381 case SET_VIDEO_BUFFER_MODE: {
382 ALOGV("SET_VIDEO_BUFFER_MODE");
383 CHECK_INTERFACE(ICamera, data, reply);
384 int32_t mode = data.readInt32();
385 reply->writeInt32(setVideoBufferMode(mode));
388 case PREVIEW_ENABLED: {
389 ALOGV("PREVIEW_ENABLED");
390 CHECK_INTERFACE(ICamera, data, reply);
391 reply->writeInt32(previewEnabled());
394 case RECORDING_ENABLED: {
395 ALOGV("RECORDING_ENABLED");
396 CHECK_INTERFACE(ICamera, data, reply);
397 reply->writeInt32(recordingEnabled());
402 CHECK_INTERFACE(ICamera, data, reply);
403 reply->writeInt32(autoFocus());
406 case CANCEL_AUTO_FOCUS: {
407 ALOGV("CANCEL_AUTO_FOCUS");
408 CHECK_INTERFACE(ICamera, data, reply);
409 reply->writeInt32(cancelAutoFocus());
413 ALOGV("TAKE_PICTURE");
414 CHECK_INTERFACE(ICamera, data, reply);
415 int msgType = data.readInt32();
416 reply->writeInt32(takePicture(msgType));
419 case SET_PARAMETERS: {
420 ALOGV("SET_PARAMETERS");
421 CHECK_INTERFACE(ICamera, data, reply);
422 String8 params(data.readString8());
423 reply->writeInt32(setParameters(params));
426 case GET_PARAMETERS: {
427 ALOGV("GET_PARAMETERS");
428 CHECK_INTERFACE(ICamera, data, reply);
429 reply->writeString8(getParameters());
433 ALOGV("SEND_COMMAND");
434 CHECK_INTERFACE(ICamera, data, reply);
435 int command = data.readInt32();
436 int arg1 = data.readInt32();
437 int arg2 = data.readInt32();
438 reply->writeInt32(sendCommand(command, arg1, arg2));
442 CHECK_INTERFACE(ICamera, data, reply);
443 sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder());
444 reply->writeInt32(connect(cameraClient));
448 CHECK_INTERFACE(ICamera, data, reply);
449 reply->writeInt32(lock());
453 CHECK_INTERFACE(ICamera, data, reply);
454 reply->writeInt32(unlock());
457 case SET_VIDEO_BUFFER_TARGET: {
458 ALOGV("SET_VIDEO_BUFFER_TARGET");
459 CHECK_INTERFACE(ICamera, data, reply);
460 sp<IGraphicBufferProducer> st =
461 interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
462 reply->writeInt32(setVideoTarget(st));
466 return BBinder::onTransact(code, data, reply, flags);
470 // ----------------------------------------------------------------------------
472 } // namespace hardware
473 } // namespace android