• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/CameraUtils.h>
25 #include <android/hardware/ICamera.h>
26 #include <android/hardware/ICameraClient.h>
27 #include <gui/IGraphicBufferProducer.h>
28 #include <gui/Surface.h>
29 #include <media/hardware/HardwareAPI.h>
30 
31 namespace android {
32 namespace hardware {
33 
34 enum {
35     DISCONNECT = IBinder::FIRST_CALL_TRANSACTION,
36     SET_PREVIEW_TARGET,
37     SET_PREVIEW_CALLBACK_FLAG,
38     SET_PREVIEW_CALLBACK_TARGET,
39     START_PREVIEW,
40     STOP_PREVIEW,
41     AUTO_FOCUS,
42     CANCEL_AUTO_FOCUS,
43     TAKE_PICTURE,
44     SET_PARAMETERS,
45     GET_PARAMETERS,
46     SEND_COMMAND,
47     CONNECT,
48     LOCK,
49     UNLOCK,
50     PREVIEW_ENABLED,
51     START_RECORDING,
52     STOP_RECORDING,
53     RECORDING_ENABLED,
54     RELEASE_RECORDING_FRAME,
55     SET_VIDEO_BUFFER_MODE,
56     SET_VIDEO_BUFFER_TARGET,
57     RELEASE_RECORDING_FRAME_HANDLE,
58     RELEASE_RECORDING_FRAME_HANDLE_BATCH,
59     SET_AUDIO_RESTRICTION,
60     GET_GLOBAL_AUDIO_RESTRICTION,
61 };
62 
63 class BpCamera: public BpInterface<ICamera>
64 {
65 public:
BpCamera(const sp<IBinder> & impl)66     explicit BpCamera(const sp<IBinder>& impl)
67         : BpInterface<ICamera>(impl)
68     {
69     }
70 
71     // disconnect from camera service
disconnect()72     binder::Status disconnect()
73     {
74         ALOGV("disconnect");
75         Parcel data, reply;
76         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
77         remote()->transact(DISCONNECT, data, &reply);
78         reply.readExceptionCode();
79         return binder::Status::ok();
80     }
81 
82     // pass the buffered IGraphicBufferProducer to the camera service
setPreviewTarget(const sp<IGraphicBufferProducer> & bufferProducer)83     status_t setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)
84     {
85         ALOGV("setPreviewTarget");
86         Parcel data, reply;
87         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
88         sp<IBinder> b(IInterface::asBinder(bufferProducer));
89         data.writeStrongBinder(b);
90         remote()->transact(SET_PREVIEW_TARGET, 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.
setPreviewCallbackFlag(int flag)96     void setPreviewCallbackFlag(int flag)
97     {
98         ALOGV("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 
setPreviewCallbackTarget(const sp<IGraphicBufferProducer> & callbackProducer)105     status_t setPreviewCallbackTarget(
106             const sp<IGraphicBufferProducer>& callbackProducer)
107     {
108         ALOGV("setPreviewCallbackTarget");
109         Parcel data, reply;
110         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
111         sp<IBinder> b(IInterface::asBinder(callbackProducer));
112         data.writeStrongBinder(b);
113         remote()->transact(SET_PREVIEW_CALLBACK_TARGET, data, &reply);
114         return reply.readInt32();
115     }
116 
117     // start preview mode, must call setPreviewTarget first
startPreview()118     status_t startPreview()
119     {
120         ALOGV("startPreview");
121         Parcel data, reply;
122         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
123         remote()->transact(START_PREVIEW, data, &reply);
124         return reply.readInt32();
125     }
126 
127     // start recording mode, must call setPreviewTarget first
startRecording()128     status_t startRecording()
129     {
130         ALOGV("startRecording");
131         Parcel data, reply;
132         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
133         remote()->transact(START_RECORDING, data, &reply);
134         return reply.readInt32();
135     }
136 
137     // stop preview mode
stopPreview()138     void stopPreview()
139     {
140         ALOGV("stopPreview");
141         Parcel data, reply;
142         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
143         remote()->transact(STOP_PREVIEW, data, &reply);
144     }
145 
146     // stop recording mode
stopRecording()147     void stopRecording()
148     {
149         ALOGV("stopRecording");
150         Parcel data, reply;
151         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
152         remote()->transact(STOP_RECORDING, data, &reply);
153     }
154 
releaseRecordingFrame(const sp<IMemory> & mem)155     void releaseRecordingFrame(const sp<IMemory>& mem)
156     {
157         ALOGV("releaseRecordingFrame");
158         Parcel data, reply;
159         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
160         data.writeStrongBinder(IInterface::asBinder(mem));
161 
162         remote()->transact(RELEASE_RECORDING_FRAME, data, &reply);
163     }
164 
releaseRecordingFrameHandle(native_handle_t * handle)165     void releaseRecordingFrameHandle(native_handle_t *handle) {
166         ALOGV("releaseRecordingFrameHandle");
167         Parcel data, reply;
168         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
169         data.writeNativeHandle(handle);
170 
171         remote()->transact(RELEASE_RECORDING_FRAME_HANDLE, data, &reply);
172 
173         // Close the native handle because camera received a dup copy.
174         native_handle_close(handle);
175         native_handle_delete(handle);
176     }
177 
releaseRecordingFrameHandleBatch(const std::vector<native_handle_t * > & handles)178     void releaseRecordingFrameHandleBatch(const std::vector<native_handle_t*>& handles) {
179         ALOGV("releaseRecordingFrameHandleBatch");
180         Parcel data, reply;
181         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
182         uint32_t n = handles.size();
183         data.writeUint32(n);
184         for (auto& handle : handles) {
185             data.writeNativeHandle(handle);
186         }
187         remote()->transact(RELEASE_RECORDING_FRAME_HANDLE_BATCH, data, &reply);
188 
189         // Close the native handle because camera received a dup copy.
190         for (auto& handle : handles) {
191             native_handle_close(handle);
192             native_handle_delete(handle);
193         }
194     }
195 
setAudioRestriction(int32_t mode)196     status_t setAudioRestriction(int32_t mode) {
197         Parcel data, reply;
198         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
199         data.writeInt32(mode);
200         remote()->transact(SET_AUDIO_RESTRICTION, data, &reply);
201         return reply.readInt32();
202     }
203 
getGlobalAudioRestriction()204     int32_t getGlobalAudioRestriction() {
205         Parcel data, reply;
206         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
207         remote()->transact(GET_GLOBAL_AUDIO_RESTRICTION, data, &reply);
208         return reply.readInt32();
209     }
210 
setVideoBufferMode(int32_t videoBufferMode)211     status_t setVideoBufferMode(int32_t videoBufferMode)
212     {
213         ALOGV("setVideoBufferMode: %d", videoBufferMode);
214         Parcel data, reply;
215         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
216         data.writeInt32(videoBufferMode);
217         remote()->transact(SET_VIDEO_BUFFER_MODE, data, &reply);
218         return reply.readInt32();
219     }
220 
221     // check preview state
previewEnabled()222     bool previewEnabled()
223     {
224         ALOGV("previewEnabled");
225         Parcel data, reply;
226         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
227         remote()->transact(PREVIEW_ENABLED, data, &reply);
228         return reply.readInt32();
229     }
230 
231     // check recording state
recordingEnabled()232     bool recordingEnabled()
233     {
234         ALOGV("recordingEnabled");
235         Parcel data, reply;
236         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
237         remote()->transact(RECORDING_ENABLED, data, &reply);
238         return reply.readInt32();
239     }
240 
241     // auto focus
autoFocus()242     status_t autoFocus()
243     {
244         ALOGV("autoFocus");
245         Parcel data, reply;
246         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
247         remote()->transact(AUTO_FOCUS, data, &reply);
248         status_t ret = reply.readInt32();
249         return ret;
250     }
251 
252     // cancel focus
cancelAutoFocus()253     status_t cancelAutoFocus()
254     {
255         ALOGV("cancelAutoFocus");
256         Parcel data, reply;
257         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
258         remote()->transact(CANCEL_AUTO_FOCUS, data, &reply);
259         status_t ret = reply.readInt32();
260         return ret;
261     }
262 
263     // take a picture - returns an IMemory (ref-counted mmap)
takePicture(int msgType)264     status_t takePicture(int msgType)
265     {
266         ALOGV("takePicture: 0x%x", msgType);
267         Parcel data, reply;
268         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
269         data.writeInt32(msgType);
270         remote()->transact(TAKE_PICTURE, data, &reply);
271         status_t ret = reply.readInt32();
272         return ret;
273     }
274 
275     // set preview/capture parameters - key/value pairs
setParameters(const String8 & params)276     status_t setParameters(const String8& params)
277     {
278         ALOGV("setParameters");
279         Parcel data, reply;
280         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
281         data.writeString8(params);
282         remote()->transact(SET_PARAMETERS, data, &reply);
283         return reply.readInt32();
284     }
285 
286     // get preview/capture parameters - key/value pairs
getParameters() const287     String8 getParameters() const
288     {
289         ALOGV("getParameters");
290         Parcel data, reply;
291         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
292         remote()->transact(GET_PARAMETERS, data, &reply);
293         return reply.readString8();
294     }
sendCommand(int32_t cmd,int32_t arg1,int32_t arg2)295     virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
296     {
297         ALOGV("sendCommand");
298         Parcel data, reply;
299         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
300         data.writeInt32(cmd);
301         data.writeInt32(arg1);
302         data.writeInt32(arg2);
303         remote()->transact(SEND_COMMAND, data, &reply);
304         return reply.readInt32();
305     }
connect(const sp<ICameraClient> & cameraClient)306     virtual status_t connect(const sp<ICameraClient>& cameraClient)
307     {
308         Parcel data, reply;
309         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
310         data.writeStrongBinder(IInterface::asBinder(cameraClient));
311         remote()->transact(CONNECT, data, &reply);
312         return reply.readInt32();
313     }
lock()314     virtual status_t lock()
315     {
316         Parcel data, reply;
317         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
318         remote()->transact(LOCK, data, &reply);
319         return reply.readInt32();
320     }
unlock()321     virtual status_t unlock()
322     {
323         Parcel data, reply;
324         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
325         remote()->transact(UNLOCK, data, &reply);
326         return reply.readInt32();
327     }
328 
setVideoTarget(const sp<IGraphicBufferProducer> & bufferProducer)329     status_t setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer)
330     {
331         ALOGV("setVideoTarget");
332         Parcel data, reply;
333         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
334         sp<IBinder> b(IInterface::asBinder(bufferProducer));
335         data.writeStrongBinder(b);
336         remote()->transact(SET_VIDEO_BUFFER_TARGET, data, &reply);
337         return reply.readInt32();
338     }
339 };
340 
341 IMPLEMENT_META_INTERFACE(Camera, "android.hardware.ICamera");
342 
343 // ----------------------------------------------------------------------
344 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)345 status_t BnCamera::onTransact(
346     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
347 {
348     switch(code) {
349         case DISCONNECT: {
350             ALOGV("DISCONNECT");
351             CHECK_INTERFACE(ICamera, data, reply);
352             disconnect();
353             reply->writeNoException();
354             return NO_ERROR;
355         } break;
356         case SET_PREVIEW_TARGET: {
357             ALOGV("SET_PREVIEW_TARGET");
358             CHECK_INTERFACE(ICamera, data, reply);
359             sp<IGraphicBufferProducer> st =
360                 interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
361             reply->writeInt32(setPreviewTarget(st));
362             return NO_ERROR;
363         } break;
364         case SET_PREVIEW_CALLBACK_FLAG: {
365             ALOGV("SET_PREVIEW_CALLBACK_TYPE");
366             CHECK_INTERFACE(ICamera, data, reply);
367             int callback_flag = data.readInt32();
368             setPreviewCallbackFlag(callback_flag);
369             return NO_ERROR;
370         } break;
371         case SET_PREVIEW_CALLBACK_TARGET: {
372             ALOGV("SET_PREVIEW_CALLBACK_TARGET");
373             CHECK_INTERFACE(ICamera, data, reply);
374             sp<IGraphicBufferProducer> cp =
375                 interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
376             reply->writeInt32(setPreviewCallbackTarget(cp));
377             return NO_ERROR;
378         }
379         case START_PREVIEW: {
380             ALOGV("START_PREVIEW");
381             CHECK_INTERFACE(ICamera, data, reply);
382             reply->writeInt32(startPreview());
383             return NO_ERROR;
384         } break;
385         case START_RECORDING: {
386             ALOGV("START_RECORDING");
387             CHECK_INTERFACE(ICamera, data, reply);
388             reply->writeInt32(startRecording());
389             return NO_ERROR;
390         } break;
391         case STOP_PREVIEW: {
392             ALOGV("STOP_PREVIEW");
393             CHECK_INTERFACE(ICamera, data, reply);
394             stopPreview();
395             return NO_ERROR;
396         } break;
397         case STOP_RECORDING: {
398             ALOGV("STOP_RECORDING");
399             CHECK_INTERFACE(ICamera, data, reply);
400             stopRecording();
401             return NO_ERROR;
402         } break;
403         case RELEASE_RECORDING_FRAME: {
404             ALOGV("RELEASE_RECORDING_FRAME");
405             CHECK_INTERFACE(ICamera, data, reply);
406             sp<IMemory> mem = interface_cast<IMemory>(data.readStrongBinder());
407             releaseRecordingFrame(mem);
408             return NO_ERROR;
409         } break;
410         case RELEASE_RECORDING_FRAME_HANDLE: {
411             ALOGV("RELEASE_RECORDING_FRAME_HANDLE");
412             CHECK_INTERFACE(ICamera, data, reply);
413             // releaseRecordingFrameHandle will be responsble to close the native handle.
414             releaseRecordingFrameHandle(data.readNativeHandle());
415             return NO_ERROR;
416         } break;
417         case RELEASE_RECORDING_FRAME_HANDLE_BATCH: {
418             ALOGV("RELEASE_RECORDING_FRAME_HANDLE_BATCH");
419             CHECK_INTERFACE(ICamera, data, reply);
420             // releaseRecordingFrameHandle will be responsble to close the native handle.
421             uint32_t n = data.readUint32();
422             std::vector<native_handle_t*> handles;
423             handles.reserve(n);
424             for (uint32_t i = 0; i < n; i++) {
425                 handles.push_back(data.readNativeHandle());
426             }
427             releaseRecordingFrameHandleBatch(handles);
428             return NO_ERROR;
429         } break;
430         case SET_VIDEO_BUFFER_MODE: {
431             ALOGV("SET_VIDEO_BUFFER_MODE");
432             CHECK_INTERFACE(ICamera, data, reply);
433             int32_t mode = data.readInt32();
434             reply->writeInt32(setVideoBufferMode(mode));
435             return NO_ERROR;
436         } break;
437         case PREVIEW_ENABLED: {
438             ALOGV("PREVIEW_ENABLED");
439             CHECK_INTERFACE(ICamera, data, reply);
440             reply->writeInt32(previewEnabled());
441             return NO_ERROR;
442         } break;
443         case RECORDING_ENABLED: {
444             ALOGV("RECORDING_ENABLED");
445             CHECK_INTERFACE(ICamera, data, reply);
446             reply->writeInt32(recordingEnabled());
447             return NO_ERROR;
448         } break;
449         case AUTO_FOCUS: {
450             ALOGV("AUTO_FOCUS");
451             CHECK_INTERFACE(ICamera, data, reply);
452             reply->writeInt32(autoFocus());
453             return NO_ERROR;
454         } break;
455         case CANCEL_AUTO_FOCUS: {
456             ALOGV("CANCEL_AUTO_FOCUS");
457             CHECK_INTERFACE(ICamera, data, reply);
458             reply->writeInt32(cancelAutoFocus());
459             return NO_ERROR;
460         } break;
461         case TAKE_PICTURE: {
462             ALOGV("TAKE_PICTURE");
463             CHECK_INTERFACE(ICamera, data, reply);
464             int msgType = data.readInt32();
465             reply->writeInt32(takePicture(msgType));
466             return NO_ERROR;
467         } break;
468         case SET_PARAMETERS: {
469             ALOGV("SET_PARAMETERS");
470             CHECK_INTERFACE(ICamera, data, reply);
471             String8 params(data.readString8());
472             reply->writeInt32(setParameters(params));
473             return NO_ERROR;
474          } break;
475         case GET_PARAMETERS: {
476             ALOGV("GET_PARAMETERS");
477             CHECK_INTERFACE(ICamera, data, reply);
478              reply->writeString8(getParameters());
479             return NO_ERROR;
480          } break;
481         case SEND_COMMAND: {
482             ALOGV("SEND_COMMAND");
483             CHECK_INTERFACE(ICamera, data, reply);
484             int command = data.readInt32();
485             int arg1 = data.readInt32();
486             int arg2 = data.readInt32();
487             reply->writeInt32(sendCommand(command, arg1, arg2));
488             return NO_ERROR;
489          } break;
490         case CONNECT: {
491             CHECK_INTERFACE(ICamera, data, reply);
492             sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder());
493             reply->writeInt32(connect(cameraClient));
494             return NO_ERROR;
495         } break;
496         case LOCK: {
497             CHECK_INTERFACE(ICamera, data, reply);
498             reply->writeInt32(lock());
499             return NO_ERROR;
500         } break;
501         case UNLOCK: {
502             CHECK_INTERFACE(ICamera, data, reply);
503             reply->writeInt32(unlock());
504             return NO_ERROR;
505         } break;
506         case SET_VIDEO_BUFFER_TARGET: {
507             ALOGV("SET_VIDEO_BUFFER_TARGET");
508             CHECK_INTERFACE(ICamera, data, reply);
509             sp<IGraphicBufferProducer> st =
510                 interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
511             reply->writeInt32(setVideoTarget(st));
512             return NO_ERROR;
513         } break;
514         case SET_AUDIO_RESTRICTION: {
515             CHECK_INTERFACE(ICamera, data, reply);
516             int32_t mode = data.readInt32();
517             reply->writeInt32(setAudioRestriction(mode));
518             return NO_ERROR;
519         } break;
520         case GET_GLOBAL_AUDIO_RESTRICTION: {
521             CHECK_INTERFACE(ICamera, data, reply);
522             reply->writeInt32(getGlobalAudioRestriction());
523             return NO_ERROR;
524         } break;
525         default:
526             return BBinder::onTransact(code, data, reply, flags);
527     }
528 }
529 
530 // ----------------------------------------------------------------------------
531 
532 } // namespace hardware
533 } // namespace android
534