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