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