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/ICamera.h>
     25 #include <gui/ISurfaceTexture.h>
     26 #include <gui/Surface.h>
     27 
     28 namespace android {
     29 
     30 enum {
     31     DISCONNECT = IBinder::FIRST_CALL_TRANSACTION,
     32     SET_PREVIEW_DISPLAY,
     33     SET_PREVIEW_TEXTURE,
     34     SET_PREVIEW_CALLBACK_FLAG,
     35     START_PREVIEW,
     36     STOP_PREVIEW,
     37     AUTO_FOCUS,
     38     CANCEL_AUTO_FOCUS,
     39     TAKE_PICTURE,
     40     SET_PARAMETERS,
     41     GET_PARAMETERS,
     42     SEND_COMMAND,
     43     CONNECT,
     44     LOCK,
     45     UNLOCK,
     46     PREVIEW_ENABLED,
     47     START_RECORDING,
     48     STOP_RECORDING,
     49     RECORDING_ENABLED,
     50     RELEASE_RECORDING_FRAME,
     51     STORE_META_DATA_IN_BUFFERS,
     52 };
     53 
     54 class BpCamera: public BpInterface<ICamera>
     55 {
     56 public:
     57     BpCamera(const sp<IBinder>& impl)
     58         : BpInterface<ICamera>(impl)
     59     {
     60     }
     61 
     62     // disconnect from camera service
     63     void disconnect()
     64     {
     65         ALOGV("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         ALOGV("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         ALOGV("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         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 
    105     // start preview mode, must call setPreviewDisplay first
    106     status_t startPreview()
    107     {
    108         ALOGV("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         ALOGV("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         ALOGV("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         ALOGV("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         ALOGV("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     status_t storeMetaDataInBuffers(bool enabled)
    153     {
    154         ALOGV("storeMetaDataInBuffers: %s", enabled? "true": "false");
    155         Parcel data, reply;
    156         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
    157         data.writeInt32(enabled);
    158         remote()->transact(STORE_META_DATA_IN_BUFFERS, data, &reply);
    159         return reply.readInt32();
    160     }
    161 
    162     // check preview state
    163     bool previewEnabled()
    164     {
    165         ALOGV("previewEnabled");
    166         Parcel data, reply;
    167         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
    168         remote()->transact(PREVIEW_ENABLED, data, &reply);
    169         return reply.readInt32();
    170     }
    171 
    172     // check recording state
    173     bool recordingEnabled()
    174     {
    175         ALOGV("recordingEnabled");
    176         Parcel data, reply;
    177         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
    178         remote()->transact(RECORDING_ENABLED, data, &reply);
    179         return reply.readInt32();
    180     }
    181 
    182     // auto focus
    183     status_t autoFocus()
    184     {
    185         ALOGV("autoFocus");
    186         Parcel data, reply;
    187         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
    188         remote()->transact(AUTO_FOCUS, data, &reply);
    189         status_t ret = reply.readInt32();
    190         return ret;
    191     }
    192 
    193     // cancel focus
    194     status_t cancelAutoFocus()
    195     {
    196         ALOGV("cancelAutoFocus");
    197         Parcel data, reply;
    198         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
    199         remote()->transact(CANCEL_AUTO_FOCUS, data, &reply);
    200         status_t ret = reply.readInt32();
    201         return ret;
    202     }
    203 
    204     // take a picture - returns an IMemory (ref-counted mmap)
    205     status_t takePicture(int msgType)
    206     {
    207         ALOGV("takePicture: 0x%x", msgType);
    208         Parcel data, reply;
    209         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
    210         data.writeInt32(msgType);
    211         remote()->transact(TAKE_PICTURE, data, &reply);
    212         status_t ret = reply.readInt32();
    213         return ret;
    214     }
    215 
    216     // set preview/capture parameters - key/value pairs
    217     status_t setParameters(const String8& params)
    218     {
    219         ALOGV("setParameters");
    220         Parcel data, reply;
    221         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
    222         data.writeString8(params);
    223         remote()->transact(SET_PARAMETERS, data, &reply);
    224         return reply.readInt32();
    225     }
    226 
    227     // get preview/capture parameters - key/value pairs
    228     String8 getParameters() const
    229     {
    230         ALOGV("getParameters");
    231         Parcel data, reply;
    232         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
    233         remote()->transact(GET_PARAMETERS, data, &reply);
    234         return reply.readString8();
    235     }
    236     virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
    237     {
    238         ALOGV("sendCommand");
    239         Parcel data, reply;
    240         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
    241         data.writeInt32(cmd);
    242         data.writeInt32(arg1);
    243         data.writeInt32(arg2);
    244         remote()->transact(SEND_COMMAND, data, &reply);
    245         return reply.readInt32();
    246     }
    247     virtual status_t connect(const sp<ICameraClient>& cameraClient)
    248     {
    249         Parcel data, reply;
    250         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
    251         data.writeStrongBinder(cameraClient->asBinder());
    252         remote()->transact(CONNECT, data, &reply);
    253         return reply.readInt32();
    254     }
    255     virtual status_t lock()
    256     {
    257         Parcel data, reply;
    258         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
    259         remote()->transact(LOCK, data, &reply);
    260         return reply.readInt32();
    261     }
    262     virtual status_t unlock()
    263     {
    264         Parcel data, reply;
    265         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
    266         remote()->transact(UNLOCK, data, &reply);
    267         return reply.readInt32();
    268     }
    269 };
    270 
    271 IMPLEMENT_META_INTERFACE(Camera, "android.hardware.ICamera");
    272 
    273 // ----------------------------------------------------------------------
    274 
    275 status_t BnCamera::onTransact(
    276     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
    277 {
    278     switch(code) {
    279         case DISCONNECT: {
    280             ALOGV("DISCONNECT");
    281             CHECK_INTERFACE(ICamera, data, reply);
    282             disconnect();
    283             return NO_ERROR;
    284         } break;
    285         case SET_PREVIEW_DISPLAY: {
    286             ALOGV("SET_PREVIEW_DISPLAY");
    287             CHECK_INTERFACE(ICamera, data, reply);
    288             sp<Surface> surface = Surface::readFromParcel(data);
    289             reply->writeInt32(setPreviewDisplay(surface));
    290             return NO_ERROR;
    291         } break;
    292         case SET_PREVIEW_TEXTURE: {
    293             ALOGV("SET_PREVIEW_TEXTURE");
    294             CHECK_INTERFACE(ICamera, data, reply);
    295             sp<ISurfaceTexture> st = interface_cast<ISurfaceTexture>(data.readStrongBinder());
    296             reply->writeInt32(setPreviewTexture(st));
    297             return NO_ERROR;
    298         } break;
    299         case SET_PREVIEW_CALLBACK_FLAG: {
    300             ALOGV("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             ALOGV("START_PREVIEW");
    308             CHECK_INTERFACE(ICamera, data, reply);
    309             reply->writeInt32(startPreview());
    310             return NO_ERROR;
    311         } break;
    312         case START_RECORDING: {
    313             ALOGV("START_RECORDING");
    314             CHECK_INTERFACE(ICamera, data, reply);
    315             reply->writeInt32(startRecording());
    316             return NO_ERROR;
    317         } break;
    318         case STOP_PREVIEW: {
    319             ALOGV("STOP_PREVIEW");
    320             CHECK_INTERFACE(ICamera, data, reply);
    321             stopPreview();
    322             return NO_ERROR;
    323         } break;
    324         case STOP_RECORDING: {
    325             ALOGV("STOP_RECORDING");
    326             CHECK_INTERFACE(ICamera, data, reply);
    327             stopRecording();
    328             return NO_ERROR;
    329         } break;
    330         case RELEASE_RECORDING_FRAME: {
    331             ALOGV("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 STORE_META_DATA_IN_BUFFERS: {
    338             ALOGV("STORE_META_DATA_IN_BUFFERS");
    339             CHECK_INTERFACE(ICamera, data, reply);
    340             bool enabled = data.readInt32();
    341             reply->writeInt32(storeMetaDataInBuffers(enabled));
    342             return NO_ERROR;
    343         } break;
    344         case PREVIEW_ENABLED: {
    345             ALOGV("PREVIEW_ENABLED");
    346             CHECK_INTERFACE(ICamera, data, reply);
    347             reply->writeInt32(previewEnabled());
    348             return NO_ERROR;
    349         } break;
    350         case RECORDING_ENABLED: {
    351             ALOGV("RECORDING_ENABLED");
    352             CHECK_INTERFACE(ICamera, data, reply);
    353             reply->writeInt32(recordingEnabled());
    354             return NO_ERROR;
    355         } break;
    356         case AUTO_FOCUS: {
    357             ALOGV("AUTO_FOCUS");
    358             CHECK_INTERFACE(ICamera, data, reply);
    359             reply->writeInt32(autoFocus());
    360             return NO_ERROR;
    361         } break;
    362         case CANCEL_AUTO_FOCUS: {
    363             ALOGV("CANCEL_AUTO_FOCUS");
    364             CHECK_INTERFACE(ICamera, data, reply);
    365             reply->writeInt32(cancelAutoFocus());
    366             return NO_ERROR;
    367         } break;
    368         case TAKE_PICTURE: {
    369             ALOGV("TAKE_PICTURE");
    370             CHECK_INTERFACE(ICamera, data, reply);
    371             int msgType = data.readInt32();
    372             reply->writeInt32(takePicture(msgType));
    373             return NO_ERROR;
    374         } break;
    375         case SET_PARAMETERS: {
    376             ALOGV("SET_PARAMETERS");
    377             CHECK_INTERFACE(ICamera, data, reply);
    378             String8 params(data.readString8());
    379             reply->writeInt32(setParameters(params));
    380             return NO_ERROR;
    381          } break;
    382         case GET_PARAMETERS: {
    383             ALOGV("GET_PARAMETERS");
    384             CHECK_INTERFACE(ICamera, data, reply);
    385              reply->writeString8(getParameters());
    386             return NO_ERROR;
    387          } break;
    388         case SEND_COMMAND: {
    389             ALOGV("SEND_COMMAND");
    390             CHECK_INTERFACE(ICamera, data, reply);
    391             int command = data.readInt32();
    392             int arg1 = data.readInt32();
    393             int arg2 = data.readInt32();
    394             reply->writeInt32(sendCommand(command, arg1, arg2));
    395             return NO_ERROR;
    396          } break;
    397         case CONNECT: {
    398             CHECK_INTERFACE(ICamera, data, reply);
    399             sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder());
    400             reply->writeInt32(connect(cameraClient));
    401             return NO_ERROR;
    402         } break;
    403         case LOCK: {
    404             CHECK_INTERFACE(ICamera, data, reply);
    405             reply->writeInt32(lock());
    406             return NO_ERROR;
    407         } break;
    408         case UNLOCK: {
    409             CHECK_INTERFACE(ICamera, data, reply);
    410             reply->writeInt32(unlock());
    411             return NO_ERROR;
    412         } break;
    413         default:
    414             return BBinder::onTransact(code, data, reply, flags);
    415     }
    416 }
    417 
    418 // ----------------------------------------------------------------------------
    419 
    420 }; // namespace android
    421