Home | History | Annotate | Download | only in libmedia
      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 #include <stdint.h>
     19 #include <sys/types.h>
     20 
     21 #include <binder/Parcel.h>
     22 #include <binder/IMemory.h>
     23 #include <media/ICrypto.h>
     24 #include <media/IMediaPlayerService.h>
     25 #include <media/IMediaRecorder.h>
     26 #include <media/IOMX.h>
     27 #include <media/IStreamSource.h>
     28 
     29 #include <utils/Errors.h>  // for status_t
     30 
     31 namespace android {
     32 
     33 enum {
     34     CREATE = IBinder::FIRST_CALL_TRANSACTION,
     35     DECODE_URL,
     36     DECODE_FD,
     37     CREATE_MEDIA_RECORDER,
     38     CREATE_METADATA_RETRIEVER,
     39     GET_OMX,
     40     MAKE_CRYPTO,
     41     ADD_BATTERY_DATA,
     42     PULL_BATTERY_DATA
     43 };
     44 
     45 class BpMediaPlayerService: public BpInterface<IMediaPlayerService>
     46 {
     47 public:
     48     BpMediaPlayerService(const sp<IBinder>& impl)
     49         : BpInterface<IMediaPlayerService>(impl)
     50     {
     51     }
     52 
     53     virtual sp<IMediaMetadataRetriever> createMetadataRetriever(pid_t pid)
     54     {
     55         Parcel data, reply;
     56         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
     57         data.writeInt32(pid);
     58         remote()->transact(CREATE_METADATA_RETRIEVER, data, &reply);
     59         return interface_cast<IMediaMetadataRetriever>(reply.readStrongBinder());
     60     }
     61 
     62     virtual sp<IMediaPlayer> create(
     63             pid_t pid, const sp<IMediaPlayerClient>& client, int audioSessionId) {
     64         Parcel data, reply;
     65         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
     66         data.writeInt32(pid);
     67         data.writeStrongBinder(client->asBinder());
     68         data.writeInt32(audioSessionId);
     69 
     70         remote()->transact(CREATE, data, &reply);
     71         return interface_cast<IMediaPlayer>(reply.readStrongBinder());
     72     }
     73 
     74     virtual sp<IMediaRecorder> createMediaRecorder(pid_t pid)
     75     {
     76         Parcel data, reply;
     77         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
     78         data.writeInt32(pid);
     79         remote()->transact(CREATE_MEDIA_RECORDER, data, &reply);
     80         return interface_cast<IMediaRecorder>(reply.readStrongBinder());
     81     }
     82 
     83     virtual sp<IMemory> decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, audio_format_t* pFormat)
     84     {
     85         Parcel data, reply;
     86         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
     87         data.writeCString(url);
     88         remote()->transact(DECODE_URL, data, &reply);
     89         *pSampleRate = uint32_t(reply.readInt32());
     90         *pNumChannels = reply.readInt32();
     91         *pFormat = (audio_format_t) reply.readInt32();
     92         return interface_cast<IMemory>(reply.readStrongBinder());
     93     }
     94 
     95     virtual sp<IMemory> decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, audio_format_t* pFormat)
     96     {
     97         Parcel data, reply;
     98         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
     99         data.writeFileDescriptor(fd);
    100         data.writeInt64(offset);
    101         data.writeInt64(length);
    102         remote()->transact(DECODE_FD, data, &reply);
    103         *pSampleRate = uint32_t(reply.readInt32());
    104         *pNumChannels = reply.readInt32();
    105         *pFormat = (audio_format_t) reply.readInt32();
    106         return interface_cast<IMemory>(reply.readStrongBinder());
    107     }
    108 
    109     virtual sp<IOMX> getOMX() {
    110         Parcel data, reply;
    111         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
    112         remote()->transact(GET_OMX, data, &reply);
    113         return interface_cast<IOMX>(reply.readStrongBinder());
    114     }
    115 
    116     virtual sp<ICrypto> makeCrypto() {
    117         Parcel data, reply;
    118         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
    119         remote()->transact(MAKE_CRYPTO, data, &reply);
    120         return interface_cast<ICrypto>(reply.readStrongBinder());
    121     }
    122 
    123     virtual void addBatteryData(uint32_t params) {
    124         Parcel data, reply;
    125         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
    126         data.writeInt32(params);
    127         remote()->transact(ADD_BATTERY_DATA, data, &reply);
    128     }
    129 
    130     virtual status_t pullBatteryData(Parcel* reply) {
    131         Parcel data;
    132         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
    133         return remote()->transact(PULL_BATTERY_DATA, data, reply);
    134     }
    135 };
    136 
    137 IMPLEMENT_META_INTERFACE(MediaPlayerService, "android.media.IMediaPlayerService");
    138 
    139 // ----------------------------------------------------------------------
    140 
    141 status_t BnMediaPlayerService::onTransact(
    142     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
    143 {
    144     switch (code) {
    145         case CREATE: {
    146             CHECK_INTERFACE(IMediaPlayerService, data, reply);
    147             pid_t pid = data.readInt32();
    148             sp<IMediaPlayerClient> client =
    149                 interface_cast<IMediaPlayerClient>(data.readStrongBinder());
    150             int audioSessionId = data.readInt32();
    151             sp<IMediaPlayer> player = create(pid, client, audioSessionId);
    152             reply->writeStrongBinder(player->asBinder());
    153             return NO_ERROR;
    154         } break;
    155         case DECODE_URL: {
    156             CHECK_INTERFACE(IMediaPlayerService, data, reply);
    157             const char* url = data.readCString();
    158             uint32_t sampleRate;
    159             int numChannels;
    160             audio_format_t format;
    161             sp<IMemory> player = decode(url, &sampleRate, &numChannels, &format);
    162             reply->writeInt32(sampleRate);
    163             reply->writeInt32(numChannels);
    164             reply->writeInt32((int32_t) format);
    165             reply->writeStrongBinder(player->asBinder());
    166             return NO_ERROR;
    167         } break;
    168         case DECODE_FD: {
    169             CHECK_INTERFACE(IMediaPlayerService, data, reply);
    170             int fd = dup(data.readFileDescriptor());
    171             int64_t offset = data.readInt64();
    172             int64_t length = data.readInt64();
    173             uint32_t sampleRate;
    174             int numChannels;
    175             audio_format_t format;
    176             sp<IMemory> player = decode(fd, offset, length, &sampleRate, &numChannels, &format);
    177             reply->writeInt32(sampleRate);
    178             reply->writeInt32(numChannels);
    179             reply->writeInt32((int32_t) format);
    180             reply->writeStrongBinder(player->asBinder());
    181             return NO_ERROR;
    182         } break;
    183         case CREATE_MEDIA_RECORDER: {
    184             CHECK_INTERFACE(IMediaPlayerService, data, reply);
    185             pid_t pid = data.readInt32();
    186             sp<IMediaRecorder> recorder = createMediaRecorder(pid);
    187             reply->writeStrongBinder(recorder->asBinder());
    188             return NO_ERROR;
    189         } break;
    190         case CREATE_METADATA_RETRIEVER: {
    191             CHECK_INTERFACE(IMediaPlayerService, data, reply);
    192             pid_t pid = data.readInt32();
    193             sp<IMediaMetadataRetriever> retriever = createMetadataRetriever(pid);
    194             reply->writeStrongBinder(retriever->asBinder());
    195             return NO_ERROR;
    196         } break;
    197         case GET_OMX: {
    198             CHECK_INTERFACE(IMediaPlayerService, data, reply);
    199             sp<IOMX> omx = getOMX();
    200             reply->writeStrongBinder(omx->asBinder());
    201             return NO_ERROR;
    202         } break;
    203         case MAKE_CRYPTO: {
    204             CHECK_INTERFACE(IMediaPlayerService, data, reply);
    205             sp<ICrypto> crypto = makeCrypto();
    206             reply->writeStrongBinder(crypto->asBinder());
    207             return NO_ERROR;
    208         } break;
    209         case ADD_BATTERY_DATA: {
    210             CHECK_INTERFACE(IMediaPlayerService, data, reply);
    211             uint32_t params = data.readInt32();
    212             addBatteryData(params);
    213             return NO_ERROR;
    214         } break;
    215         case PULL_BATTERY_DATA: {
    216             CHECK_INTERFACE(IMediaPlayerService, data, reply);
    217             pullBatteryData(reply);
    218             return NO_ERROR;
    219         } break;
    220         default:
    221             return BBinder::onTransact(code, data, reply, flags);
    222     }
    223 }
    224 
    225 // ----------------------------------------------------------------------------
    226 
    227 }; // namespace android
    228