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/IMediaCodecList.h>
     24 #include <media/IMediaHTTPService.h>
     25 #include <media/IMediaPlayerService.h>
     26 #include <media/IMediaPlayer.h>
     27 #include <media/IMediaRecorder.h>
     28 #include <media/IOMX.h>
     29 #include <media/IRemoteDisplay.h>
     30 #include <media/IRemoteDisplayClient.h>
     31 #include <media/IStreamSource.h>
     32 
     33 #include <utils/Errors.h>  // for status_t
     34 #include <utils/String8.h>
     35 
     36 namespace android {
     37 
     38 enum {
     39     CREATE = IBinder::FIRST_CALL_TRANSACTION,
     40     CREATE_MEDIA_RECORDER,
     41     CREATE_METADATA_RETRIEVER,
     42     ADD_BATTERY_DATA,
     43     PULL_BATTERY_DATA,
     44     LISTEN_FOR_REMOTE_DISPLAY,
     45     GET_CODEC_LIST,
     46 };
     47 
     48 class BpMediaPlayerService: public BpInterface<IMediaPlayerService>
     49 {
     50 public:
     51     explicit BpMediaPlayerService(const sp<IBinder>& impl)
     52         : BpInterface<IMediaPlayerService>(impl)
     53     {
     54     }
     55 
     56     virtual sp<IMediaMetadataRetriever> createMetadataRetriever()
     57     {
     58         Parcel data, reply;
     59         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
     60         remote()->transact(CREATE_METADATA_RETRIEVER, data, &reply);
     61         return interface_cast<IMediaMetadataRetriever>(reply.readStrongBinder());
     62     }
     63 
     64     virtual sp<IMediaPlayer> create(
     65             const sp<IMediaPlayerClient>& client, audio_session_t audioSessionId) {
     66         Parcel data, reply;
     67         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
     68         data.writeStrongBinder(IInterface::asBinder(client));
     69         data.writeInt32(audioSessionId);
     70 
     71         remote()->transact(CREATE, data, &reply);
     72         return interface_cast<IMediaPlayer>(reply.readStrongBinder());
     73     }
     74 
     75     virtual sp<IMediaRecorder> createMediaRecorder(const String16 &opPackageName)
     76     {
     77         Parcel data, reply;
     78         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
     79         data.writeString16(opPackageName);
     80         remote()->transact(CREATE_MEDIA_RECORDER, data, &reply);
     81         return interface_cast<IMediaRecorder>(reply.readStrongBinder());
     82     }
     83 
     84     virtual void addBatteryData(uint32_t params) {
     85         Parcel data, reply;
     86         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
     87         data.writeInt32(params);
     88         remote()->transact(ADD_BATTERY_DATA, data, &reply);
     89     }
     90 
     91     virtual status_t pullBatteryData(Parcel* reply) {
     92         Parcel data;
     93         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
     94         return remote()->transact(PULL_BATTERY_DATA, data, reply);
     95     }
     96 
     97     virtual sp<IRemoteDisplay> listenForRemoteDisplay(const String16 &opPackageName,
     98             const sp<IRemoteDisplayClient>& client, const String8& iface)
     99     {
    100         Parcel data, reply;
    101         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
    102         data.writeString16(opPackageName);
    103         data.writeStrongBinder(IInterface::asBinder(client));
    104         data.writeString8(iface);
    105         remote()->transact(LISTEN_FOR_REMOTE_DISPLAY, data, &reply);
    106         return interface_cast<IRemoteDisplay>(reply.readStrongBinder());
    107     }
    108 
    109     virtual sp<IMediaCodecList> getCodecList() const {
    110         Parcel data, reply;
    111         data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
    112         remote()->transact(GET_CODEC_LIST, data, &reply);
    113         return interface_cast<IMediaCodecList>(reply.readStrongBinder());
    114     }
    115 };
    116 
    117 IMPLEMENT_META_INTERFACE(MediaPlayerService, "android.media.IMediaPlayerService");
    118 
    119 // ----------------------------------------------------------------------
    120 
    121 status_t BnMediaPlayerService::onTransact(
    122     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
    123 {
    124     switch (code) {
    125         case CREATE: {
    126             CHECK_INTERFACE(IMediaPlayerService, data, reply);
    127             sp<IMediaPlayerClient> client =
    128                 interface_cast<IMediaPlayerClient>(data.readStrongBinder());
    129             audio_session_t audioSessionId = (audio_session_t) data.readInt32();
    130             sp<IMediaPlayer> player = create(client, audioSessionId);
    131             reply->writeStrongBinder(IInterface::asBinder(player));
    132             return NO_ERROR;
    133         } break;
    134         case CREATE_MEDIA_RECORDER: {
    135             CHECK_INTERFACE(IMediaPlayerService, data, reply);
    136             const String16 opPackageName = data.readString16();
    137             sp<IMediaRecorder> recorder = createMediaRecorder(opPackageName);
    138             reply->writeStrongBinder(IInterface::asBinder(recorder));
    139             return NO_ERROR;
    140         } break;
    141         case CREATE_METADATA_RETRIEVER: {
    142             CHECK_INTERFACE(IMediaPlayerService, data, reply);
    143             sp<IMediaMetadataRetriever> retriever = createMetadataRetriever();
    144             reply->writeStrongBinder(IInterface::asBinder(retriever));
    145             return NO_ERROR;
    146         } break;
    147         case ADD_BATTERY_DATA: {
    148             CHECK_INTERFACE(IMediaPlayerService, data, reply);
    149             uint32_t params = data.readInt32();
    150             addBatteryData(params);
    151             return NO_ERROR;
    152         } break;
    153         case PULL_BATTERY_DATA: {
    154             CHECK_INTERFACE(IMediaPlayerService, data, reply);
    155             pullBatteryData(reply);
    156             return NO_ERROR;
    157         } break;
    158         case LISTEN_FOR_REMOTE_DISPLAY: {
    159             CHECK_INTERFACE(IMediaPlayerService, data, reply);
    160             const String16 opPackageName = data.readString16();
    161             sp<IRemoteDisplayClient> client(
    162                     interface_cast<IRemoteDisplayClient>(data.readStrongBinder()));
    163             if (client == NULL) {
    164                 reply->writeStrongBinder(NULL);
    165                 return NO_ERROR;
    166             }
    167             String8 iface(data.readString8());
    168             sp<IRemoteDisplay> display(listenForRemoteDisplay(opPackageName, client, iface));
    169             reply->writeStrongBinder(IInterface::asBinder(display));
    170             return NO_ERROR;
    171         } break;
    172         case GET_CODEC_LIST: {
    173             CHECK_INTERFACE(IMediaPlayerService, data, reply);
    174             sp<IMediaCodecList> mcl = getCodecList();
    175             reply->writeStrongBinder(IInterface::asBinder(mcl));
    176             return NO_ERROR;
    177         } break;
    178         default:
    179             return BBinder::onTransact(code, data, reply, flags);
    180     }
    181 }
    182 
    183 // ----------------------------------------------------------------------------
    184 
    185 } // namespace android
    186