ICamera.cpp revision bef3f23f16f2fc575b3f425febcfcc436a7db70f
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/ICamera.h>
25#include <gui/IGraphicBufferProducer.h>
26#include <gui/Surface.h>
27
28namespace android {
29
30enum {
31    DISCONNECT = IBinder::FIRST_CALL_TRANSACTION,
32    SET_PREVIEW_TEXTURE,
33    SET_PREVIEW_CALLBACK_FLAG,
34    SET_PREVIEW_CALLBACK_TARGET,
35    START_PREVIEW,
36    STOP_PREVIEW,
37    AUTO_FOCUS,
38    CANCEL_AUTO_FOCUS,
39    TAKE_PICTURE,
40    SET_PARAMETERS,
41    GET_PARAMETERS,
42    SEND_COMMAND,
43    CONNECT,
44    LOCK,
45    UNLOCK,
46    PREVIEW_ENABLED,
47    START_RECORDING,
48    STOP_RECORDING,
49    RECORDING_ENABLED,
50    RELEASE_RECORDING_FRAME,
51    STORE_META_DATA_IN_BUFFERS,
52};
53
54class BpCamera: public BpInterface<ICamera>
55{
56public:
57    BpCamera(const sp<IBinder>& impl)
58        : BpInterface<ICamera>(impl)
59    {
60    }
61
62    // disconnect from camera service
63    void disconnect()
64    {
65        ALOGV("disconnect");
66        Parcel data, reply;
67        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
68        remote()->transact(DISCONNECT, data, &reply);
69        reply.readExceptionCode();
70    }
71
72    // pass the buffered IGraphicBufferProducer to the camera service
73    status_t setPreviewTexture(const sp<IGraphicBufferProducer>& bufferProducer)
74    {
75        ALOGV("setPreviewTexture");
76        Parcel data, reply;
77        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
78        sp<IBinder> b(bufferProducer->asBinder());
79        data.writeStrongBinder(b);
80        remote()->transact(SET_PREVIEW_TEXTURE, data, &reply);
81        return reply.readInt32();
82    }
83
84    // set the preview callback flag to affect how the received frames from
85    // preview are handled. See Camera.h for details.
86    void setPreviewCallbackFlag(int flag)
87    {
88        ALOGV("setPreviewCallbackFlag(%d)", flag);
89        Parcel data, reply;
90        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
91        data.writeInt32(flag);
92        remote()->transact(SET_PREVIEW_CALLBACK_FLAG, data, &reply);
93    }
94
95    status_t setPreviewCallbackTarget(
96            const sp<IGraphicBufferProducer>& callbackProducer)
97    {
98        ALOGV("setPreviewCallbackTarget");
99        Parcel data, reply;
100        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
101        sp<IBinder> b(callbackProducer->asBinder());
102        data.writeStrongBinder(b);
103        remote()->transact(SET_PREVIEW_CALLBACK_TARGET, data, &reply);
104        return reply.readInt32();
105    }
106
107    // start preview mode, must call setPreviewDisplay first
108    status_t startPreview()
109    {
110        ALOGV("startPreview");
111        Parcel data, reply;
112        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
113        remote()->transact(START_PREVIEW, data, &reply);
114        return reply.readInt32();
115    }
116
117    // start recording mode, must call setPreviewDisplay first
118    status_t startRecording()
119    {
120        ALOGV("startRecording");
121        Parcel data, reply;
122        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
123        remote()->transact(START_RECORDING, data, &reply);
124        return reply.readInt32();
125    }
126
127    // stop preview mode
128    void stopPreview()
129    {
130        ALOGV("stopPreview");
131        Parcel data, reply;
132        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
133        remote()->transact(STOP_PREVIEW, data, &reply);
134    }
135
136    // stop recording mode
137    void stopRecording()
138    {
139        ALOGV("stopRecording");
140        Parcel data, reply;
141        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
142        remote()->transact(STOP_RECORDING, data, &reply);
143    }
144
145    void releaseRecordingFrame(const sp<IMemory>& mem)
146    {
147        ALOGV("releaseRecordingFrame");
148        Parcel data, reply;
149        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
150        data.writeStrongBinder(mem->asBinder());
151        remote()->transact(RELEASE_RECORDING_FRAME, data, &reply);
152    }
153
154    status_t storeMetaDataInBuffers(bool enabled)
155    {
156        ALOGV("storeMetaDataInBuffers: %s", enabled? "true": "false");
157        Parcel data, reply;
158        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
159        data.writeInt32(enabled);
160        remote()->transact(STORE_META_DATA_IN_BUFFERS, data, &reply);
161        return reply.readInt32();
162    }
163
164    // check preview state
165    bool previewEnabled()
166    {
167        ALOGV("previewEnabled");
168        Parcel data, reply;
169        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
170        remote()->transact(PREVIEW_ENABLED, data, &reply);
171        return reply.readInt32();
172    }
173
174    // check recording state
175    bool recordingEnabled()
176    {
177        ALOGV("recordingEnabled");
178        Parcel data, reply;
179        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
180        remote()->transact(RECORDING_ENABLED, data, &reply);
181        return reply.readInt32();
182    }
183
184    // auto focus
185    status_t autoFocus()
186    {
187        ALOGV("autoFocus");
188        Parcel data, reply;
189        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
190        remote()->transact(AUTO_FOCUS, data, &reply);
191        status_t ret = reply.readInt32();
192        return ret;
193    }
194
195    // cancel focus
196    status_t cancelAutoFocus()
197    {
198        ALOGV("cancelAutoFocus");
199        Parcel data, reply;
200        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
201        remote()->transact(CANCEL_AUTO_FOCUS, data, &reply);
202        status_t ret = reply.readInt32();
203        return ret;
204    }
205
206    // take a picture - returns an IMemory (ref-counted mmap)
207    status_t takePicture(int msgType)
208    {
209        ALOGV("takePicture: 0x%x", msgType);
210        Parcel data, reply;
211        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
212        data.writeInt32(msgType);
213        remote()->transact(TAKE_PICTURE, data, &reply);
214        status_t ret = reply.readInt32();
215        return ret;
216    }
217
218    // set preview/capture parameters - key/value pairs
219    status_t setParameters(const String8& params)
220    {
221        ALOGV("setParameters");
222        Parcel data, reply;
223        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
224        data.writeString8(params);
225        remote()->transact(SET_PARAMETERS, data, &reply);
226        return reply.readInt32();
227    }
228
229    // get preview/capture parameters - key/value pairs
230    String8 getParameters() const
231    {
232        ALOGV("getParameters");
233        Parcel data, reply;
234        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
235        remote()->transact(GET_PARAMETERS, data, &reply);
236        return reply.readString8();
237    }
238    virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
239    {
240        ALOGV("sendCommand");
241        Parcel data, reply;
242        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
243        data.writeInt32(cmd);
244        data.writeInt32(arg1);
245        data.writeInt32(arg2);
246        remote()->transact(SEND_COMMAND, data, &reply);
247        return reply.readInt32();
248    }
249    virtual status_t connect(const sp<ICameraClient>& cameraClient)
250    {
251        Parcel data, reply;
252        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
253        data.writeStrongBinder(cameraClient->asBinder());
254        remote()->transact(CONNECT, data, &reply);
255        return reply.readInt32();
256    }
257    virtual status_t lock()
258    {
259        Parcel data, reply;
260        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
261        remote()->transact(LOCK, data, &reply);
262        return reply.readInt32();
263    }
264    virtual status_t unlock()
265    {
266        Parcel data, reply;
267        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
268        remote()->transact(UNLOCK, data, &reply);
269        return reply.readInt32();
270    }
271};
272
273IMPLEMENT_META_INTERFACE(Camera, "android.hardware.ICamera");
274
275// ----------------------------------------------------------------------
276
277status_t BnCamera::onTransact(
278    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
279{
280    switch(code) {
281        case DISCONNECT: {
282            ALOGV("DISCONNECT");
283            CHECK_INTERFACE(ICamera, data, reply);
284            disconnect();
285            reply->writeNoException();
286            return NO_ERROR;
287        } break;
288        case SET_PREVIEW_TEXTURE: {
289            ALOGV("SET_PREVIEW_TEXTURE");
290            CHECK_INTERFACE(ICamera, data, reply);
291            sp<IGraphicBufferProducer> st =
292                interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
293            reply->writeInt32(setPreviewTexture(st));
294            return NO_ERROR;
295        } break;
296        case SET_PREVIEW_CALLBACK_FLAG: {
297            ALOGV("SET_PREVIEW_CALLBACK_TYPE");
298            CHECK_INTERFACE(ICamera, data, reply);
299            int callback_flag = data.readInt32();
300            setPreviewCallbackFlag(callback_flag);
301            return NO_ERROR;
302        } break;
303        case SET_PREVIEW_CALLBACK_TARGET: {
304            ALOGV("SET_PREVIEW_CALLBACK_TARGET");
305            CHECK_INTERFACE(ICamera, data, reply);
306            sp<IGraphicBufferProducer> cp =
307                interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
308            reply->writeInt32(setPreviewCallbackTarget(cp));
309            return NO_ERROR;
310        }
311        case START_PREVIEW: {
312            ALOGV("START_PREVIEW");
313            CHECK_INTERFACE(ICamera, data, reply);
314            reply->writeInt32(startPreview());
315            return NO_ERROR;
316        } break;
317        case START_RECORDING: {
318            ALOGV("START_RECORDING");
319            CHECK_INTERFACE(ICamera, data, reply);
320            reply->writeInt32(startRecording());
321            return NO_ERROR;
322        } break;
323        case STOP_PREVIEW: {
324            ALOGV("STOP_PREVIEW");
325            CHECK_INTERFACE(ICamera, data, reply);
326            stopPreview();
327            return NO_ERROR;
328        } break;
329        case STOP_RECORDING: {
330            ALOGV("STOP_RECORDING");
331            CHECK_INTERFACE(ICamera, data, reply);
332            stopRecording();
333            return NO_ERROR;
334        } break;
335        case RELEASE_RECORDING_FRAME: {
336            ALOGV("RELEASE_RECORDING_FRAME");
337            CHECK_INTERFACE(ICamera, data, reply);
338            sp<IMemory> mem = interface_cast<IMemory>(data.readStrongBinder());
339            releaseRecordingFrame(mem);
340            return NO_ERROR;
341        } break;
342        case STORE_META_DATA_IN_BUFFERS: {
343            ALOGV("STORE_META_DATA_IN_BUFFERS");
344            CHECK_INTERFACE(ICamera, data, reply);
345            bool enabled = data.readInt32();
346            reply->writeInt32(storeMetaDataInBuffers(enabled));
347            return NO_ERROR;
348        } break;
349        case PREVIEW_ENABLED: {
350            ALOGV("PREVIEW_ENABLED");
351            CHECK_INTERFACE(ICamera, data, reply);
352            reply->writeInt32(previewEnabled());
353            return NO_ERROR;
354        } break;
355        case RECORDING_ENABLED: {
356            ALOGV("RECORDING_ENABLED");
357            CHECK_INTERFACE(ICamera, data, reply);
358            reply->writeInt32(recordingEnabled());
359            return NO_ERROR;
360        } break;
361        case AUTO_FOCUS: {
362            ALOGV("AUTO_FOCUS");
363            CHECK_INTERFACE(ICamera, data, reply);
364            reply->writeInt32(autoFocus());
365            return NO_ERROR;
366        } break;
367        case CANCEL_AUTO_FOCUS: {
368            ALOGV("CANCEL_AUTO_FOCUS");
369            CHECK_INTERFACE(ICamera, data, reply);
370            reply->writeInt32(cancelAutoFocus());
371            return NO_ERROR;
372        } break;
373        case TAKE_PICTURE: {
374            ALOGV("TAKE_PICTURE");
375            CHECK_INTERFACE(ICamera, data, reply);
376            int msgType = data.readInt32();
377            reply->writeInt32(takePicture(msgType));
378            return NO_ERROR;
379        } break;
380        case SET_PARAMETERS: {
381            ALOGV("SET_PARAMETERS");
382            CHECK_INTERFACE(ICamera, data, reply);
383            String8 params(data.readString8());
384            reply->writeInt32(setParameters(params));
385            return NO_ERROR;
386         } break;
387        case GET_PARAMETERS: {
388            ALOGV("GET_PARAMETERS");
389            CHECK_INTERFACE(ICamera, data, reply);
390             reply->writeString8(getParameters());
391            return NO_ERROR;
392         } break;
393        case SEND_COMMAND: {
394            ALOGV("SEND_COMMAND");
395            CHECK_INTERFACE(ICamera, data, reply);
396            int command = data.readInt32();
397            int arg1 = data.readInt32();
398            int arg2 = data.readInt32();
399            reply->writeInt32(sendCommand(command, arg1, arg2));
400            return NO_ERROR;
401         } break;
402        case CONNECT: {
403            CHECK_INTERFACE(ICamera, data, reply);
404            sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder());
405            reply->writeInt32(connect(cameraClient));
406            return NO_ERROR;
407        } break;
408        case LOCK: {
409            CHECK_INTERFACE(ICamera, data, reply);
410            reply->writeInt32(lock());
411            return NO_ERROR;
412        } break;
413        case UNLOCK: {
414            CHECK_INTERFACE(ICamera, data, reply);
415            reply->writeInt32(unlock());
416            return NO_ERROR;
417        } break;
418        default:
419            return BBinder::onTransact(code, data, reply, flags);
420    }
421}
422
423// ----------------------------------------------------------------------------
424
425}; // namespace android
426