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