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