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