Home | History | Annotate | Download | only in libmedia
      1 /*
      2  **
      3  ** Copyright 2008, HTC Inc.
      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 "IMediaRecorder"
     20 #include <utils/Log.h>
     21 #include <binder/Parcel.h>
     22 #include <surfaceflinger/ISurface.h>
     23 #include <camera/ICamera.h>
     24 #include <media/IMediaPlayerClient.h>
     25 #include <media/IMediaRecorder.h>
     26 
     27 namespace android {
     28 
     29 enum {
     30     RELEASE = IBinder::FIRST_CALL_TRANSACTION,
     31     INIT,
     32     CLOSE,
     33     RESET,
     34     STOP,
     35     START,
     36     PREPARE,
     37     GET_MAX_AMPLITUDE,
     38     SET_VIDEO_SOURCE,
     39     SET_AUDIO_SOURCE,
     40     SET_OUTPUT_FORMAT,
     41     SET_VIDEO_ENCODER,
     42     SET_AUDIO_ENCODER,
     43     SET_OUTPUT_FILE_PATH,
     44     SET_OUTPUT_FILE_FD,
     45     SET_VIDEO_SIZE,
     46     SET_VIDEO_FRAMERATE,
     47     SET_PARAMETERS,
     48     SET_PREVIEW_SURFACE,
     49     SET_CAMERA,
     50     SET_LISTENER
     51 };
     52 
     53 class BpMediaRecorder: public BpInterface<IMediaRecorder>
     54 {
     55 public:
     56     BpMediaRecorder(const sp<IBinder>& impl)
     57     : BpInterface<IMediaRecorder>(impl)
     58     {
     59     }
     60 
     61     status_t setCamera(const sp<ICamera>& camera)
     62     {
     63         LOGV("setCamera(%p)", camera.get());
     64         Parcel data, reply;
     65         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
     66         data.writeStrongBinder(camera->asBinder());
     67         remote()->transact(SET_CAMERA, data, &reply);
     68         return reply.readInt32();
     69     }
     70 
     71     status_t setPreviewSurface(const sp<ISurface>& surface)
     72     {
     73         LOGV("setPreviewSurface(%p)", surface.get());
     74         Parcel data, reply;
     75         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
     76         data.writeStrongBinder(surface->asBinder());
     77         remote()->transact(SET_PREVIEW_SURFACE, data, &reply);
     78         return reply.readInt32();
     79     }
     80 
     81     status_t init()
     82     {
     83         LOGV("init");
     84         Parcel data, reply;
     85         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
     86         remote()->transact(INIT, data, &reply);
     87         return reply.readInt32();
     88     }
     89 
     90     status_t setVideoSource(int vs)
     91     {
     92         LOGV("setVideoSource(%d)", vs);
     93         Parcel data, reply;
     94         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
     95         data.writeInt32(vs);
     96         remote()->transact(SET_VIDEO_SOURCE, data, &reply);
     97         return reply.readInt32();
     98     }
     99 
    100     status_t setAudioSource(int as)
    101     {
    102         LOGV("setAudioSource(%d)", as);
    103         Parcel data, reply;
    104         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
    105         data.writeInt32(as);
    106         remote()->transact(SET_AUDIO_SOURCE, data, &reply);
    107         return reply.readInt32();
    108     }
    109 
    110     status_t setOutputFormat(int of)
    111     {
    112         LOGV("setOutputFormat(%d)", of);
    113         Parcel data, reply;
    114         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
    115         data.writeInt32(of);
    116         remote()->transact(SET_OUTPUT_FORMAT, data, &reply);
    117         return reply.readInt32();
    118     }
    119 
    120     status_t setVideoEncoder(int ve)
    121     {
    122         LOGV("setVideoEncoder(%d)", ve);
    123         Parcel data, reply;
    124         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
    125         data.writeInt32(ve);
    126         remote()->transact(SET_VIDEO_ENCODER, data, &reply);
    127         return reply.readInt32();
    128     }
    129 
    130     status_t setAudioEncoder(int ae)
    131     {
    132         LOGV("setAudioEncoder(%d)", ae);
    133         Parcel data, reply;
    134         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
    135         data.writeInt32(ae);
    136         remote()->transact(SET_AUDIO_ENCODER, data, &reply);
    137         return reply.readInt32();
    138     }
    139 
    140     status_t setOutputFile(const char* path)
    141     {
    142         LOGV("setOutputFile(%s)", path);
    143         Parcel data, reply;
    144         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
    145         data.writeCString(path);
    146         remote()->transact(SET_OUTPUT_FILE_PATH, data, &reply);
    147         return reply.readInt32();
    148     }
    149 
    150     status_t setOutputFile(int fd, int64_t offset, int64_t length) {
    151         LOGV("setOutputFile(%d, %lld, %lld)", fd, offset, length);
    152         Parcel data, reply;
    153         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
    154         data.writeFileDescriptor(fd);
    155         data.writeInt64(offset);
    156         data.writeInt64(length);
    157         remote()->transact(SET_OUTPUT_FILE_FD, data, &reply);
    158         return reply.readInt32();
    159     }
    160 
    161     status_t setVideoSize(int width, int height)
    162     {
    163         LOGV("setVideoSize(%dx%d)", width, height);
    164         Parcel data, reply;
    165         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
    166         data.writeInt32(width);
    167         data.writeInt32(height);
    168         remote()->transact(SET_VIDEO_SIZE, data, &reply);
    169         return reply.readInt32();
    170     }
    171 
    172     status_t setVideoFrameRate(int frames_per_second)
    173     {
    174         LOGV("setVideoFrameRate(%d)", frames_per_second);
    175         Parcel data, reply;
    176         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
    177         data.writeInt32(frames_per_second);
    178         remote()->transact(SET_VIDEO_FRAMERATE, data, &reply);
    179         return reply.readInt32();
    180     }
    181 
    182     status_t setParameters(const String8& params)
    183     {
    184         LOGV("setParameter(%s)", params.string());
    185         Parcel data, reply;
    186         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
    187         data.writeString8(params);
    188         remote()->transact(SET_PARAMETERS, data, &reply);
    189         return reply.readInt32();
    190     }
    191 
    192     status_t setListener(const sp<IMediaPlayerClient>& listener)
    193     {
    194         LOGV("setListener(%p)", listener.get());
    195         Parcel data, reply;
    196         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
    197         data.writeStrongBinder(listener->asBinder());
    198         remote()->transact(SET_LISTENER, data, &reply);
    199         return reply.readInt32();
    200     }
    201 
    202     status_t prepare()
    203     {
    204         LOGV("prepare");
    205         Parcel data, reply;
    206         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
    207         remote()->transact(PREPARE, data, &reply);
    208         return reply.readInt32();
    209     }
    210 
    211     status_t getMaxAmplitude(int* max)
    212     {
    213         LOGV("getMaxAmplitude");
    214         Parcel data, reply;
    215         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
    216         remote()->transact(GET_MAX_AMPLITUDE, data, &reply);
    217         *max = reply.readInt32();
    218         return reply.readInt32();
    219     }
    220 
    221     status_t start()
    222     {
    223         LOGV("start");
    224         Parcel data, reply;
    225         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
    226         remote()->transact(START, data, &reply);
    227         return reply.readInt32();
    228     }
    229 
    230     status_t stop()
    231     {
    232         LOGV("stop");
    233         Parcel data, reply;
    234         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
    235         remote()->transact(STOP, data, &reply);
    236         return reply.readInt32();
    237     }
    238 
    239     status_t reset()
    240     {
    241         LOGV("reset");
    242         Parcel data, reply;
    243         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
    244         remote()->transact(RESET, data, &reply);
    245         return reply.readInt32();
    246     }
    247 
    248     status_t close()
    249     {
    250         LOGV("close");
    251         Parcel data, reply;
    252         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
    253         remote()->transact(CLOSE, data, &reply);
    254         return reply.readInt32();
    255     }
    256 
    257     status_t release()
    258     {
    259         LOGV("release");
    260         Parcel data, reply;
    261         data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor());
    262         remote()->transact(RELEASE, data, &reply);
    263         return reply.readInt32();
    264     }
    265 };
    266 
    267 IMPLEMENT_META_INTERFACE(MediaRecorder, "android.media.IMediaRecorder");
    268 
    269 // ----------------------------------------------------------------------
    270 
    271 status_t BnMediaRecorder::onTransact(
    272                                      uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
    273 {
    274     switch(code) {
    275         case RELEASE: {
    276             LOGV("RELEASE");
    277             CHECK_INTERFACE(IMediaRecorder, data, reply);
    278             reply->writeInt32(release());
    279             return NO_ERROR;
    280         } break;
    281         case INIT: {
    282             LOGV("INIT");
    283             CHECK_INTERFACE(IMediaRecorder, data, reply);
    284             reply->writeInt32(init());
    285             return NO_ERROR;
    286         } break;
    287         case CLOSE: {
    288             LOGV("CLOSE");
    289             CHECK_INTERFACE(IMediaRecorder, data, reply);
    290             reply->writeInt32(close());
    291             return NO_ERROR;
    292         } break;
    293         case RESET: {
    294             LOGV("RESET");
    295             CHECK_INTERFACE(IMediaRecorder, data, reply);
    296             reply->writeInt32(reset());
    297             return NO_ERROR;
    298         } break;
    299         case STOP: {
    300             LOGV("STOP");
    301             CHECK_INTERFACE(IMediaRecorder, data, reply);
    302             reply->writeInt32(stop());
    303             return NO_ERROR;
    304         } break;
    305         case START: {
    306             LOGV("START");
    307             CHECK_INTERFACE(IMediaRecorder, data, reply);
    308             reply->writeInt32(start());
    309             return NO_ERROR;
    310         } break;
    311         case PREPARE: {
    312             LOGV("PREPARE");
    313             CHECK_INTERFACE(IMediaRecorder, data, reply);
    314             reply->writeInt32(prepare());
    315             return NO_ERROR;
    316         } break;
    317         case GET_MAX_AMPLITUDE: {
    318             LOGV("GET_MAX_AMPLITUDE");
    319             CHECK_INTERFACE(IMediaRecorder, data, reply);
    320             int max = 0;
    321             status_t ret = getMaxAmplitude(&max);
    322             reply->writeInt32(max);
    323             reply->writeInt32(ret);
    324             return NO_ERROR;
    325         } break;
    326         case SET_VIDEO_SOURCE: {
    327             LOGV("SET_VIDEO_SOURCE");
    328             CHECK_INTERFACE(IMediaRecorder, data, reply);
    329             int vs = data.readInt32();
    330             reply->writeInt32(setVideoSource(vs));
    331             return NO_ERROR;
    332         } break;
    333         case SET_AUDIO_SOURCE: {
    334             LOGV("SET_AUDIO_SOURCE");
    335             CHECK_INTERFACE(IMediaRecorder, data, reply);
    336             int as = data.readInt32();
    337             reply->writeInt32(setAudioSource(as));
    338             return NO_ERROR;
    339         } break;
    340         case SET_OUTPUT_FORMAT: {
    341             LOGV("SET_OUTPUT_FORMAT");
    342             CHECK_INTERFACE(IMediaRecorder, data, reply);
    343             int of = data.readInt32();
    344             reply->writeInt32(setOutputFormat(of));
    345             return NO_ERROR;
    346         } break;
    347         case SET_VIDEO_ENCODER: {
    348             LOGV("SET_VIDEO_ENCODER");
    349             CHECK_INTERFACE(IMediaRecorder, data, reply);
    350             int ve = data.readInt32();
    351             reply->writeInt32(setVideoEncoder(ve));
    352             return NO_ERROR;
    353         } break;
    354         case SET_AUDIO_ENCODER: {
    355             LOGV("SET_AUDIO_ENCODER");
    356             CHECK_INTERFACE(IMediaRecorder, data, reply);
    357             int ae = data.readInt32();
    358             reply->writeInt32(setAudioEncoder(ae));
    359             return NO_ERROR;
    360 
    361         } break;
    362         case SET_OUTPUT_FILE_PATH: {
    363             LOGV("SET_OUTPUT_FILE_PATH");
    364             CHECK_INTERFACE(IMediaRecorder, data, reply);
    365             const char* path = data.readCString();
    366             reply->writeInt32(setOutputFile(path));
    367             return NO_ERROR;
    368         } break;
    369         case SET_OUTPUT_FILE_FD: {
    370             LOGV("SET_OUTPUT_FILE_FD");
    371             CHECK_INTERFACE(IMediaRecorder, data, reply);
    372             int fd = dup(data.readFileDescriptor());
    373             int64_t offset = data.readInt64();
    374             int64_t length = data.readInt64();
    375             reply->writeInt32(setOutputFile(fd, offset, length));
    376             return NO_ERROR;
    377         } break;
    378         case SET_VIDEO_SIZE: {
    379             LOGV("SET_VIDEO_SIZE");
    380             CHECK_INTERFACE(IMediaRecorder, data, reply);
    381             int width = data.readInt32();
    382             int height = data.readInt32();
    383             reply->writeInt32(setVideoSize(width, height));
    384             return NO_ERROR;
    385         } break;
    386         case SET_VIDEO_FRAMERATE: {
    387             LOGV("SET_VIDEO_FRAMERATE");
    388             CHECK_INTERFACE(IMediaRecorder, data, reply);
    389             int frames_per_second = data.readInt32();
    390             reply->writeInt32(setVideoFrameRate(frames_per_second));
    391             return NO_ERROR;
    392         } break;
    393         case SET_PARAMETERS: {
    394             LOGV("SET_PARAMETER");
    395             CHECK_INTERFACE(IMediaRecorder, data, reply);
    396             reply->writeInt32(setParameters(data.readString8()));
    397             return NO_ERROR;
    398         } break;
    399         case SET_LISTENER: {
    400             LOGV("SET_LISTENER");
    401             CHECK_INTERFACE(IMediaRecorder, data, reply);
    402             sp<IMediaPlayerClient> listener =
    403                 interface_cast<IMediaPlayerClient>(data.readStrongBinder());
    404             reply->writeInt32(setListener(listener));
    405             return NO_ERROR;
    406         } break;
    407         case SET_PREVIEW_SURFACE: {
    408             LOGV("SET_PREVIEW_SURFACE");
    409             CHECK_INTERFACE(IMediaRecorder, data, reply);
    410             sp<ISurface> surface = interface_cast<ISurface>(data.readStrongBinder());
    411             reply->writeInt32(setPreviewSurface(surface));
    412             return NO_ERROR;
    413         } break;
    414         case SET_CAMERA: {
    415             LOGV("SET_CAMERA");
    416             CHECK_INTERFACE(IMediaRecorder, data, reply);
    417             sp<ICamera> camera = interface_cast<ICamera>(data.readStrongBinder());
    418             reply->writeInt32(setCamera(camera));
    419             return NO_ERROR;
    420         } break;
    421         default:
    422             return BBinder::onTransact(code, data, reply, flags);
    423     }
    424 }
    425 
    426 // ----------------------------------------------------------------------------
    427 
    428 }; // namespace android
    429