ICamera.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 "ICamera"
20#include <utils/Log.h>
21#include <stdint.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>
30
31namespace android {
32namespace hardware {
33
34enum {
35    DISCONNECT = IBinder::FIRST_CALL_TRANSACTION,
36    SET_PREVIEW_TARGET,
37    SET_PREVIEW_CALLBACK_FLAG,
38    SET_PREVIEW_CALLBACK_TARGET,
39    START_PREVIEW,
40    STOP_PREVIEW,
41    AUTO_FOCUS,
42    CANCEL_AUTO_FOCUS,
43    TAKE_PICTURE,
44    SET_PARAMETERS,
45    GET_PARAMETERS,
46    SEND_COMMAND,
47    CONNECT,
48    LOCK,
49    UNLOCK,
50    PREVIEW_ENABLED,
51    START_RECORDING,
52    STOP_RECORDING,
53    RECORDING_ENABLED,
54    RELEASE_RECORDING_FRAME,
55    SET_VIDEO_BUFFER_MODE,
56    SET_VIDEO_BUFFER_TARGET,
57};
58
59class BpCamera: public BpInterface<ICamera>
60{
61public:
62    BpCamera(const sp<IBinder>& impl)
63        : BpInterface<ICamera>(impl)
64    {
65    }
66
67    // disconnect from camera service
68    binder::Status disconnect()
69    {
70        ALOGV("disconnect");
71        Parcel data, reply;
72        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
73        remote()->transact(DISCONNECT, data, &reply);
74        reply.readExceptionCode();
75        return binder::Status::ok();
76    }
77
78    // pass the buffered IGraphicBufferProducer to the camera service
79    status_t setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)
80    {
81        ALOGV("setPreviewTarget");
82        Parcel data, reply;
83        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
84        sp<IBinder> b(IInterface::asBinder(bufferProducer));
85        data.writeStrongBinder(b);
86        remote()->transact(SET_PREVIEW_TARGET, data, &reply);
87        return reply.readInt32();
88    }
89
90    // set the preview callback flag to affect how the received frames from
91    // preview are handled. See Camera.h for details.
92    void setPreviewCallbackFlag(int flag)
93    {
94        ALOGV("setPreviewCallbackFlag(%d)", flag);
95        Parcel data, reply;
96        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
97        data.writeInt32(flag);
98        remote()->transact(SET_PREVIEW_CALLBACK_FLAG, data, &reply);
99    }
100
101    status_t setPreviewCallbackTarget(
102            const sp<IGraphicBufferProducer>& callbackProducer)
103    {
104        ALOGV("setPreviewCallbackTarget");
105        Parcel data, reply;
106        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
107        sp<IBinder> b(IInterface::asBinder(callbackProducer));
108        data.writeStrongBinder(b);
109        remote()->transact(SET_PREVIEW_CALLBACK_TARGET, data, &reply);
110        return reply.readInt32();
111    }
112
113    // start preview mode, must call setPreviewTarget first
114    status_t startPreview()
115    {
116        ALOGV("startPreview");
117        Parcel data, reply;
118        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
119        remote()->transact(START_PREVIEW, data, &reply);
120        return reply.readInt32();
121    }
122
123    // start recording mode, must call setPreviewTarget first
124    status_t startRecording()
125    {
126        ALOGV("startRecording");
127        Parcel data, reply;
128        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
129        remote()->transact(START_RECORDING, data, &reply);
130        return reply.readInt32();
131    }
132
133    // stop preview mode
134    void stopPreview()
135    {
136        ALOGV("stopPreview");
137        Parcel data, reply;
138        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
139        remote()->transact(STOP_PREVIEW, data, &reply);
140    }
141
142    // stop recording mode
143    void stopRecording()
144    {
145        ALOGV("stopRecording");
146        Parcel data, reply;
147        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
148        remote()->transact(STOP_RECORDING, data, &reply);
149    }
150
151    void releaseRecordingFrame(const sp<IMemory>& mem)
152    {
153        ALOGV("releaseRecordingFrame");
154        Parcel data, reply;
155        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
156        data.writeStrongBinder(IInterface::asBinder(mem));
157
158        native_handle_t *nh = nullptr;
159        if (CameraUtils::isNativeHandleMetadata(mem)) {
160            VideoNativeHandleMetadata *metadata =
161                        (VideoNativeHandleMetadata*)(mem->pointer());
162            nh = metadata->pHandle;
163            data.writeNativeHandle(nh);
164        }
165
166        remote()->transact(RELEASE_RECORDING_FRAME, data, &reply);
167
168        if (nh) {
169            // Close the native handle because camera received a dup copy.
170            native_handle_close(nh);
171            native_handle_delete(nh);
172        }
173    }
174
175    status_t setVideoBufferMode(int32_t videoBufferMode)
176    {
177        ALOGV("setVideoBufferMode: %d", videoBufferMode);
178        Parcel data, reply;
179        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
180        data.writeInt32(videoBufferMode);
181        remote()->transact(SET_VIDEO_BUFFER_MODE, data, &reply);
182        return reply.readInt32();
183    }
184
185    // check preview state
186    bool previewEnabled()
187    {
188        ALOGV("previewEnabled");
189        Parcel data, reply;
190        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
191        remote()->transact(PREVIEW_ENABLED, data, &reply);
192        return reply.readInt32();
193    }
194
195    // check recording state
196    bool recordingEnabled()
197    {
198        ALOGV("recordingEnabled");
199        Parcel data, reply;
200        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
201        remote()->transact(RECORDING_ENABLED, data, &reply);
202        return reply.readInt32();
203    }
204
205    // auto focus
206    status_t autoFocus()
207    {
208        ALOGV("autoFocus");
209        Parcel data, reply;
210        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
211        remote()->transact(AUTO_FOCUS, data, &reply);
212        status_t ret = reply.readInt32();
213        return ret;
214    }
215
216    // cancel focus
217    status_t cancelAutoFocus()
218    {
219        ALOGV("cancelAutoFocus");
220        Parcel data, reply;
221        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
222        remote()->transact(CANCEL_AUTO_FOCUS, data, &reply);
223        status_t ret = reply.readInt32();
224        return ret;
225    }
226
227    // take a picture - returns an IMemory (ref-counted mmap)
228    status_t takePicture(int msgType)
229    {
230        ALOGV("takePicture: 0x%x", msgType);
231        Parcel data, reply;
232        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
233        data.writeInt32(msgType);
234        remote()->transact(TAKE_PICTURE, data, &reply);
235        status_t ret = reply.readInt32();
236        return ret;
237    }
238
239    // set preview/capture parameters - key/value pairs
240    status_t setParameters(const String8& params)
241    {
242        ALOGV("setParameters");
243        Parcel data, reply;
244        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
245        data.writeString8(params);
246        remote()->transact(SET_PARAMETERS, data, &reply);
247        return reply.readInt32();
248    }
249
250    // get preview/capture parameters - key/value pairs
251    String8 getParameters() const
252    {
253        ALOGV("getParameters");
254        Parcel data, reply;
255        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
256        remote()->transact(GET_PARAMETERS, data, &reply);
257        return reply.readString8();
258    }
259    virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
260    {
261        ALOGV("sendCommand");
262        Parcel data, reply;
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();
269    }
270    virtual status_t connect(const sp<ICameraClient>& cameraClient)
271    {
272        Parcel data, reply;
273        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
274        data.writeStrongBinder(IInterface::asBinder(cameraClient));
275        remote()->transact(CONNECT, data, &reply);
276        return reply.readInt32();
277    }
278    virtual status_t lock()
279    {
280        Parcel data, reply;
281        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
282        remote()->transact(LOCK, data, &reply);
283        return reply.readInt32();
284    }
285    virtual status_t unlock()
286    {
287        Parcel data, reply;
288        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
289        remote()->transact(UNLOCK, data, &reply);
290        return reply.readInt32();
291    }
292
293    status_t setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer)
294    {
295        ALOGV("setVideoTarget");
296        Parcel data, reply;
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();
302    }
303};
304
305IMPLEMENT_META_INTERFACE(Camera, "android.hardware.ICamera");
306
307// ----------------------------------------------------------------------
308
309status_t BnCamera::onTransact(
310    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
311{
312    switch(code) {
313        case DISCONNECT: {
314            ALOGV("DISCONNECT");
315            CHECK_INTERFACE(ICamera, data, reply);
316            disconnect();
317            reply->writeNoException();
318            return NO_ERROR;
319        } break;
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));
326            return NO_ERROR;
327        } break;
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);
333            return NO_ERROR;
334        } break;
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));
341            return NO_ERROR;
342        }
343        case START_PREVIEW: {
344            ALOGV("START_PREVIEW");
345            CHECK_INTERFACE(ICamera, data, reply);
346            reply->writeInt32(startPreview());
347            return NO_ERROR;
348        } break;
349        case START_RECORDING: {
350            ALOGV("START_RECORDING");
351            CHECK_INTERFACE(ICamera, data, reply);
352            reply->writeInt32(startRecording());
353            return NO_ERROR;
354        } break;
355        case STOP_PREVIEW: {
356            ALOGV("STOP_PREVIEW");
357            CHECK_INTERFACE(ICamera, data, reply);
358            stopPreview();
359            return NO_ERROR;
360        } break;
361        case STOP_RECORDING: {
362            ALOGV("STOP_RECORDING");
363            CHECK_INTERFACE(ICamera, data, reply);
364            stopRecording();
365            return NO_ERROR;
366        } break;
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
372            if (CameraUtils::isNativeHandleMetadata(mem)) {
373                VideoNativeHandleMetadata *metadata =
374                        (VideoNativeHandleMetadata*)(mem->pointer());
375                metadata->pHandle = data.readNativeHandle();
376                // releaseRecordingFrame will be responsble to close the native handle.
377            }
378
379            releaseRecordingFrame(mem);
380            return NO_ERROR;
381        } break;
382        case SET_VIDEO_BUFFER_MODE: {
383            ALOGV("SET_VIDEO_BUFFER_MODE");
384            CHECK_INTERFACE(ICamera, data, reply);
385            int32_t mode = data.readInt32();
386            reply->writeInt32(setVideoBufferMode(mode));
387            return NO_ERROR;
388        } break;
389        case PREVIEW_ENABLED: {
390            ALOGV("PREVIEW_ENABLED");
391            CHECK_INTERFACE(ICamera, data, reply);
392            reply->writeInt32(previewEnabled());
393            return NO_ERROR;
394        } break;
395        case RECORDING_ENABLED: {
396            ALOGV("RECORDING_ENABLED");
397            CHECK_INTERFACE(ICamera, data, reply);
398            reply->writeInt32(recordingEnabled());
399            return NO_ERROR;
400        } break;
401        case AUTO_FOCUS: {
402            ALOGV("AUTO_FOCUS");
403            CHECK_INTERFACE(ICamera, data, reply);
404            reply->writeInt32(autoFocus());
405            return NO_ERROR;
406        } break;
407        case CANCEL_AUTO_FOCUS: {
408            ALOGV("CANCEL_AUTO_FOCUS");
409            CHECK_INTERFACE(ICamera, data, reply);
410            reply->writeInt32(cancelAutoFocus());
411            return NO_ERROR;
412        } break;
413        case TAKE_PICTURE: {
414            ALOGV("TAKE_PICTURE");
415            CHECK_INTERFACE(ICamera, data, reply);
416            int msgType = data.readInt32();
417            reply->writeInt32(takePicture(msgType));
418            return NO_ERROR;
419        } break;
420        case SET_PARAMETERS: {
421            ALOGV("SET_PARAMETERS");
422            CHECK_INTERFACE(ICamera, data, reply);
423            String8 params(data.readString8());
424            reply->writeInt32(setParameters(params));
425            return NO_ERROR;
426         } break;
427        case GET_PARAMETERS: {
428            ALOGV("GET_PARAMETERS");
429            CHECK_INTERFACE(ICamera, data, reply);
430             reply->writeString8(getParameters());
431            return NO_ERROR;
432         } break;
433        case SEND_COMMAND: {
434            ALOGV("SEND_COMMAND");
435            CHECK_INTERFACE(ICamera, data, reply);
436            int command = data.readInt32();
437            int arg1 = data.readInt32();
438            int arg2 = data.readInt32();
439            reply->writeInt32(sendCommand(command, arg1, arg2));
440            return NO_ERROR;
441         } break;
442        case CONNECT: {
443            CHECK_INTERFACE(ICamera, data, reply);
444            sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder());
445            reply->writeInt32(connect(cameraClient));
446            return NO_ERROR;
447        } break;
448        case LOCK: {
449            CHECK_INTERFACE(ICamera, data, reply);
450            reply->writeInt32(lock());
451            return NO_ERROR;
452        } break;
453        case UNLOCK: {
454            CHECK_INTERFACE(ICamera, data, reply);
455            reply->writeInt32(unlock());
456            return NO_ERROR;
457        } break;
458        case SET_VIDEO_BUFFER_TARGET: {
459            ALOGV("SET_VIDEO_BUFFER_TARGET");
460            CHECK_INTERFACE(ICamera, data, reply);
461            sp<IGraphicBufferProducer> st =
462                interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
463            reply->writeInt32(setVideoTarget(st));
464            return NO_ERROR;
465        } break;
466        default:
467            return BBinder::onTransact(code, data, reply, flags);
468    }
469}
470
471// ----------------------------------------------------------------------------
472
473} // namespace hardware
474} // namespace android
475