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