IMediaPlayerService.cpp revision e2b1028852120bcfded33b8f06f66b780437fe92
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 30namespace android { 31 32enum { 33 CREATE_URL = IBinder::FIRST_CALL_TRANSACTION, 34 CREATE_FD, 35 CREATE_STREAM, 36 DECODE_URL, 37 DECODE_FD, 38 CREATE_MEDIA_RECORDER, 39 CREATE_METADATA_RETRIEVER, 40 GET_OMX 41}; 42 43class BpMediaPlayerService: public BpInterface<IMediaPlayerService> 44{ 45public: 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, 62 const char* url, const KeyedVector<String8, String8> *headers, int audioSessionId) { 63 Parcel data, reply; 64 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor()); 65 data.writeInt32(pid); 66 data.writeStrongBinder(client->asBinder()); 67 data.writeCString(url); 68 69 if (headers == NULL) { 70 data.writeInt32(0); 71 } else { 72 // serialize the headers 73 data.writeInt32(headers->size()); 74 for (size_t i = 0; i < headers->size(); ++i) { 75 data.writeString8(headers->keyAt(i)); 76 data.writeString8(headers->valueAt(i)); 77 } 78 } 79 data.writeInt32(audioSessionId); 80 81 remote()->transact(CREATE_URL, data, &reply); 82 83 return interface_cast<IMediaPlayer>(reply.readStrongBinder()); 84 } 85 86 virtual sp<IMediaRecorder> createMediaRecorder(pid_t pid) 87 { 88 Parcel data, reply; 89 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor()); 90 data.writeInt32(pid); 91 remote()->transact(CREATE_MEDIA_RECORDER, data, &reply); 92 return interface_cast<IMediaRecorder>(reply.readStrongBinder()); 93 } 94 95 virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, int fd, 96 int64_t offset, int64_t length, int audioSessionId) 97 { 98 Parcel data, reply; 99 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor()); 100 data.writeInt32(pid); 101 data.writeStrongBinder(client->asBinder()); 102 data.writeFileDescriptor(fd); 103 data.writeInt64(offset); 104 data.writeInt64(length); 105 data.writeInt32(audioSessionId); 106 107 remote()->transact(CREATE_FD, data, &reply); 108 109 return interface_cast<IMediaPlayer>(reply.readStrongBinder());; 110 } 111 112 virtual sp<IMediaPlayer> create( 113 pid_t pid, const sp<IMediaPlayerClient> &client, 114 const sp<IStreamSource> &source, int audioSessionId) { 115 Parcel data, reply; 116 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor()); 117 data.writeInt32(static_cast<int32_t>(pid)); 118 data.writeStrongBinder(client->asBinder()); 119 data.writeStrongBinder(source->asBinder()); 120 data.writeInt32(static_cast<int32_t>(audioSessionId)); 121 122 remote()->transact(CREATE_STREAM, data, &reply); 123 124 return interface_cast<IMediaPlayer>(reply.readStrongBinder());; 125 } 126 127 virtual sp<IMemory> decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat) 128 { 129 Parcel data, reply; 130 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor()); 131 data.writeCString(url); 132 remote()->transact(DECODE_URL, data, &reply); 133 *pSampleRate = uint32_t(reply.readInt32()); 134 *pNumChannels = reply.readInt32(); 135 *pFormat = reply.readInt32(); 136 return interface_cast<IMemory>(reply.readStrongBinder()); 137 } 138 139 virtual sp<IMemory> decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat) 140 { 141 Parcel data, reply; 142 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor()); 143 data.writeFileDescriptor(fd); 144 data.writeInt64(offset); 145 data.writeInt64(length); 146 remote()->transact(DECODE_FD, data, &reply); 147 *pSampleRate = uint32_t(reply.readInt32()); 148 *pNumChannels = reply.readInt32(); 149 *pFormat = reply.readInt32(); 150 return interface_cast<IMemory>(reply.readStrongBinder()); 151 } 152 153 virtual sp<IOMX> getOMX() { 154 Parcel data, reply; 155 data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor()); 156 remote()->transact(GET_OMX, data, &reply); 157 return interface_cast<IOMX>(reply.readStrongBinder()); 158 } 159}; 160 161IMPLEMENT_META_INTERFACE(MediaPlayerService, "android.media.IMediaPlayerService"); 162 163// ---------------------------------------------------------------------- 164 165status_t BnMediaPlayerService::onTransact( 166 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 167{ 168 switch(code) { 169 case CREATE_URL: { 170 CHECK_INTERFACE(IMediaPlayerService, data, reply); 171 pid_t pid = data.readInt32(); 172 sp<IMediaPlayerClient> client = 173 interface_cast<IMediaPlayerClient>(data.readStrongBinder()); 174 const char* url = data.readCString(); 175 176 KeyedVector<String8, String8> headers; 177 int32_t numHeaders = data.readInt32(); 178 for (int i = 0; i < numHeaders; ++i) { 179 String8 key = data.readString8(); 180 String8 value = data.readString8(); 181 headers.add(key, value); 182 } 183 int audioSessionId = data.readInt32(); 184 185 sp<IMediaPlayer> player = create( 186 pid, client, url, numHeaders > 0 ? &headers : NULL, audioSessionId); 187 188 reply->writeStrongBinder(player->asBinder()); 189 return NO_ERROR; 190 } break; 191 case CREATE_FD: { 192 CHECK_INTERFACE(IMediaPlayerService, data, reply); 193 pid_t pid = data.readInt32(); 194 sp<IMediaPlayerClient> client = interface_cast<IMediaPlayerClient>(data.readStrongBinder()); 195 int fd = dup(data.readFileDescriptor()); 196 int64_t offset = data.readInt64(); 197 int64_t length = data.readInt64(); 198 int audioSessionId = data.readInt32(); 199 200 sp<IMediaPlayer> player = create(pid, client, fd, offset, length, audioSessionId); 201 reply->writeStrongBinder(player->asBinder()); 202 return NO_ERROR; 203 } break; 204 case CREATE_STREAM: 205 { 206 CHECK_INTERFACE(IMediaPlayerService, data, reply); 207 208 pid_t pid = static_cast<pid_t>(data.readInt32()); 209 210 sp<IMediaPlayerClient> client = 211 interface_cast<IMediaPlayerClient>(data.readStrongBinder()); 212 213 sp<IStreamSource> source = 214 interface_cast<IStreamSource>(data.readStrongBinder()); 215 216 int audioSessionId = static_cast<int>(data.readInt32()); 217 218 sp<IMediaPlayer> player = 219 create(pid, client, source, audioSessionId); 220 221 reply->writeStrongBinder(player->asBinder()); 222 return OK; 223 break; 224 } 225 case DECODE_URL: { 226 CHECK_INTERFACE(IMediaPlayerService, data, reply); 227 const char* url = data.readCString(); 228 uint32_t sampleRate; 229 int numChannels; 230 int format; 231 sp<IMemory> player = decode(url, &sampleRate, &numChannels, &format); 232 reply->writeInt32(sampleRate); 233 reply->writeInt32(numChannels); 234 reply->writeInt32(format); 235 reply->writeStrongBinder(player->asBinder()); 236 return NO_ERROR; 237 } break; 238 case DECODE_FD: { 239 CHECK_INTERFACE(IMediaPlayerService, data, reply); 240 int fd = dup(data.readFileDescriptor()); 241 int64_t offset = data.readInt64(); 242 int64_t length = data.readInt64(); 243 uint32_t sampleRate; 244 int numChannels; 245 int format; 246 sp<IMemory> player = decode(fd, offset, length, &sampleRate, &numChannels, &format); 247 reply->writeInt32(sampleRate); 248 reply->writeInt32(numChannels); 249 reply->writeInt32(format); 250 reply->writeStrongBinder(player->asBinder()); 251 return NO_ERROR; 252 } break; 253 case CREATE_MEDIA_RECORDER: { 254 CHECK_INTERFACE(IMediaPlayerService, data, reply); 255 pid_t pid = data.readInt32(); 256 sp<IMediaRecorder> recorder = createMediaRecorder(pid); 257 reply->writeStrongBinder(recorder->asBinder()); 258 return NO_ERROR; 259 } break; 260 case CREATE_METADATA_RETRIEVER: { 261 CHECK_INTERFACE(IMediaPlayerService, data, reply); 262 pid_t pid = data.readInt32(); 263 sp<IMediaMetadataRetriever> retriever = createMetadataRetriever(pid); 264 reply->writeStrongBinder(retriever->asBinder()); 265 return NO_ERROR; 266 } break; 267 case GET_OMX: { 268 CHECK_INTERFACE(IMediaPlayerService, data, reply); 269 sp<IOMX> omx = getOMX(); 270 reply->writeStrongBinder(omx->asBinder()); 271 return NO_ERROR; 272 } break; 273 default: 274 return BBinder::onTransact(code, data, reply, flags); 275 } 276} 277 278// ---------------------------------------------------------------------------- 279 280}; // namespace android 281