ICamera.cpp revision e468ac57f6e8afc6078c76d4eb1ac327112a3de0
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(int msgType)
227    {
228        LOGV("takePicture: 0x%x", msgType);
229        Parcel data, reply;
230        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
231        data.writeInt32(msgType);
232        remote()->transact(TAKE_PICTURE, data, &reply);
233        status_t ret = reply.readInt32();
234        return ret;
235    }
236
237    // set preview/capture parameters - key/value pairs
238    status_t setParameters(const String8& params)
239    {
240        LOGV("setParameters");
241        Parcel data, reply;
242        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
243        data.writeString8(params);
244        remote()->transact(SET_PARAMETERS, data, &reply);
245        return reply.readInt32();
246    }
247
248    // get preview/capture parameters - key/value pairs
249    String8 getParameters() const
250    {
251        LOGV("getParameters");
252        Parcel data, reply;
253        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
254        remote()->transact(GET_PARAMETERS, data, &reply);
255        return reply.readString8();
256    }
257    virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
258    {
259        LOGV("sendCommand");
260        Parcel data, reply;
261        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
262        data.writeInt32(cmd);
263        data.writeInt32(arg1);
264        data.writeInt32(arg2);
265        remote()->transact(SEND_COMMAND, data, &reply);
266        return reply.readInt32();
267    }
268    virtual status_t connect(const sp<ICameraClient>& cameraClient)
269    {
270        Parcel data, reply;
271        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
272        data.writeStrongBinder(cameraClient->asBinder());
273        remote()->transact(CONNECT, data, &reply);
274        return reply.readInt32();
275    }
276    virtual status_t lock()
277    {
278        Parcel data, reply;
279        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
280        remote()->transact(LOCK, data, &reply);
281        return reply.readInt32();
282    }
283    virtual status_t unlock()
284    {
285        Parcel data, reply;
286        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
287        remote()->transact(UNLOCK, data, &reply);
288        return reply.readInt32();
289    }
290};
291
292IMPLEMENT_META_INTERFACE(Camera, "android.hardware.ICamera");
293
294// ----------------------------------------------------------------------
295
296status_t BnCamera::onTransact(
297    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
298{
299    switch(code) {
300        case DISCONNECT: {
301            LOGV("DISCONNECT");
302            CHECK_INTERFACE(ICamera, data, reply);
303            disconnect();
304            return NO_ERROR;
305        } break;
306        case SET_PREVIEW_DISPLAY: {
307            LOGV("SET_PREVIEW_DISPLAY");
308            CHECK_INTERFACE(ICamera, data, reply);
309            sp<Surface> surface = Surface::readFromParcel(data);
310            reply->writeInt32(setPreviewDisplay(surface));
311            return NO_ERROR;
312        } break;
313        case SET_PREVIEW_TEXTURE: {
314            LOGV("SET_PREVIEW_TEXTURE");
315            CHECK_INTERFACE(ICamera, data, reply);
316            sp<ISurfaceTexture> st = interface_cast<ISurfaceTexture>(data.readStrongBinder());
317            reply->writeInt32(setPreviewTexture(st));
318            return NO_ERROR;
319        } break;
320        case SET_PREVIEW_CALLBACK_FLAG: {
321            LOGV("SET_PREVIEW_CALLBACK_TYPE");
322            CHECK_INTERFACE(ICamera, data, reply);
323            int callback_flag = data.readInt32();
324            setPreviewCallbackFlag(callback_flag);
325            return NO_ERROR;
326        } break;
327        case START_PREVIEW: {
328            LOGV("START_PREVIEW");
329            CHECK_INTERFACE(ICamera, data, reply);
330            reply->writeInt32(startPreview());
331            return NO_ERROR;
332        } break;
333        case START_RECORDING: {
334            LOGV("START_RECORDING");
335            CHECK_INTERFACE(ICamera, data, reply);
336            reply->writeInt32(startRecording());
337            return NO_ERROR;
338        } break;
339        case STOP_PREVIEW: {
340            LOGV("STOP_PREVIEW");
341            CHECK_INTERFACE(ICamera, data, reply);
342            stopPreview();
343            return NO_ERROR;
344        } break;
345        case STOP_RECORDING: {
346            LOGV("STOP_RECORDING");
347            CHECK_INTERFACE(ICamera, data, reply);
348            stopRecording();
349            return NO_ERROR;
350        } break;
351        case RELEASE_RECORDING_FRAME: {
352            LOGV("RELEASE_RECORDING_FRAME");
353            CHECK_INTERFACE(ICamera, data, reply);
354            sp<IMemory> mem = interface_cast<IMemory>(data.readStrongBinder());
355            releaseRecordingFrame(mem);
356            return NO_ERROR;
357        } break;
358        case GET_NUM_VIDEO_BUFFERS: {
359            LOGV("GET_NUM_VIDEO_BUFFERS");
360            CHECK_INTERFACE(ICamera, data, reply);
361            reply->writeInt32(getNumberOfVideoBuffers());
362            return NO_ERROR;
363        } break;
364        case GET_VIDEO_BUFFER: {
365            LOGV("GET_VIDEO_BUFFER");
366            CHECK_INTERFACE(ICamera, data, reply);
367            int32_t index = data.readInt32();
368            reply->writeStrongBinder(getVideoBuffer(index)->asBinder());
369            return NO_ERROR;
370        } break;
371        case STORE_META_DATA_IN_BUFFERS: {
372            LOGV("STORE_META_DATA_IN_BUFFERS");
373            CHECK_INTERFACE(ICamera, data, reply);
374            bool enabled = data.readInt32();
375            reply->writeInt32(storeMetaDataInBuffers(enabled));
376            return NO_ERROR;
377        } break;
378        case PREVIEW_ENABLED: {
379            LOGV("PREVIEW_ENABLED");
380            CHECK_INTERFACE(ICamera, data, reply);
381            reply->writeInt32(previewEnabled());
382            return NO_ERROR;
383        } break;
384        case RECORDING_ENABLED: {
385            LOGV("RECORDING_ENABLED");
386            CHECK_INTERFACE(ICamera, data, reply);
387            reply->writeInt32(recordingEnabled());
388            return NO_ERROR;
389        } break;
390        case AUTO_FOCUS: {
391            LOGV("AUTO_FOCUS");
392            CHECK_INTERFACE(ICamera, data, reply);
393            reply->writeInt32(autoFocus());
394            return NO_ERROR;
395        } break;
396        case CANCEL_AUTO_FOCUS: {
397            LOGV("CANCEL_AUTO_FOCUS");
398            CHECK_INTERFACE(ICamera, data, reply);
399            reply->writeInt32(cancelAutoFocus());
400            return NO_ERROR;
401        } break;
402        case TAKE_PICTURE: {
403            LOGV("TAKE_PICTURE");
404            CHECK_INTERFACE(ICamera, data, reply);
405            int msgType = data.readInt32();
406            reply->writeInt32(takePicture(msgType));
407            return NO_ERROR;
408        } break;
409        case SET_PARAMETERS: {
410            LOGV("SET_PARAMETERS");
411            CHECK_INTERFACE(ICamera, data, reply);
412            String8 params(data.readString8());
413            reply->writeInt32(setParameters(params));
414            return NO_ERROR;
415         } break;
416        case GET_PARAMETERS: {
417            LOGV("GET_PARAMETERS");
418            CHECK_INTERFACE(ICamera, data, reply);
419             reply->writeString8(getParameters());
420            return NO_ERROR;
421         } break;
422        case SEND_COMMAND: {
423            LOGV("SEND_COMMAND");
424            CHECK_INTERFACE(ICamera, data, reply);
425            int command = data.readInt32();
426            int arg1 = data.readInt32();
427            int arg2 = data.readInt32();
428            reply->writeInt32(sendCommand(command, arg1, arg2));
429            return NO_ERROR;
430         } break;
431        case CONNECT: {
432            CHECK_INTERFACE(ICamera, data, reply);
433            sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder());
434            reply->writeInt32(connect(cameraClient));
435            return NO_ERROR;
436        } break;
437        case LOCK: {
438            CHECK_INTERFACE(ICamera, data, reply);
439            reply->writeInt32(lock());
440            return NO_ERROR;
441        } break;
442        case UNLOCK: {
443            CHECK_INTERFACE(ICamera, data, reply);
444            reply->writeInt32(unlock());
445            return NO_ERROR;
446        } break;
447        default:
448            return BBinder::onTransact(code, data, reply, flags);
449    }
450}
451
452// ----------------------------------------------------------------------------
453
454}; // namespace android
455