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