The Android Open Source Project | 2729ea9 | 2008-10-21 07:00:00 -0700 | [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> |
| 20 | #include <utils/Parcel.h> |
| 21 | |
| 22 | #include <utils/IMemory.h> |
| 23 | #include <media/IMediaPlayerService.h> |
The Android Open Source Project | 7b5eb02 | 2008-12-17 18:05:43 -0800 | [diff] [blame^] | 24 | #include <media/IMediaRecorder.h> |
The Android Open Source Project | 2729ea9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | enum { |
| 29 | CREATE_URL = IBinder::FIRST_CALL_TRANSACTION, |
| 30 | CREATE_FD, |
| 31 | DECODE_URL, |
| 32 | DECODE_FD, |
The Android Open Source Project | 7b5eb02 | 2008-12-17 18:05:43 -0800 | [diff] [blame^] | 33 | CREATE_MEDIA_RECORDER, |
| 34 | CREATE_METADATA_RETRIEVER, |
The Android Open Source Project | 2729ea9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | class BpMediaPlayerService: public BpInterface<IMediaPlayerService> |
| 38 | { |
| 39 | public: |
| 40 | BpMediaPlayerService(const sp<IBinder>& impl) |
| 41 | : BpInterface<IMediaPlayerService>(impl) |
| 42 | { |
| 43 | } |
| 44 | |
The Android Open Source Project | 7b5eb02 | 2008-12-17 18:05:43 -0800 | [diff] [blame^] | 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 | |
The Android Open Source Project | 2729ea9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
The Android Open Source Project | 7b5eb02 | 2008-12-17 18:05:43 -0800 | [diff] [blame^] | 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 | |
The Android Open Source Project | 2729ea9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
The Android Open Source Project | 7b5eb02 | 2008-12-17 18:05:43 -0800 | [diff] [blame^] | 87 | virtual sp<IMemory> decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat) |
The Android Open Source Project | 2729ea9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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(); |
The Android Open Source Project | 7b5eb02 | 2008-12-17 18:05:43 -0800 | [diff] [blame^] | 95 | *pFormat = reply.readInt32(); |
The Android Open Source Project | 2729ea9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 96 | return interface_cast<IMemory>(reply.readStrongBinder()); |
| 97 | } |
| 98 | |
The Android Open Source Project | 7b5eb02 | 2008-12-17 18:05:43 -0800 | [diff] [blame^] | 99 | virtual sp<IMemory> decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat) |
The Android Open Source Project | 2729ea9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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(); |
The Android Open Source Project | 7b5eb02 | 2008-12-17 18:05:43 -0800 | [diff] [blame^] | 109 | *pFormat = reply.readInt32(); |
The Android Open Source Project | 2729ea9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 110 | return interface_cast<IMemory>(reply.readStrongBinder()); |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | IMPLEMENT_META_INTERFACE(MediaPlayerService, "android.hardware.IMediaPlayerService"); |
| 115 | |
| 116 | // ---------------------------------------------------------------------- |
| 117 | |
| 118 | #define CHECK_INTERFACE(interface, data, reply) \ |
| 119 | do { if (!data.enforceInterface(interface::getInterfaceDescriptor())) { \ |
| 120 | LOGW("Call incorrectly routed to " #interface); \ |
| 121 | return PERMISSION_DENIED; \ |
| 122 | } } while (0) |
| 123 | |
| 124 | status_t BnMediaPlayerService::onTransact( |
| 125 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 126 | { |
| 127 | switch(code) { |
| 128 | case CREATE_URL: { |
| 129 | CHECK_INTERFACE(IMediaPlayerService, data, reply); |
| 130 | pid_t pid = data.readInt32(); |
| 131 | sp<IMediaPlayerClient> client = interface_cast<IMediaPlayerClient>(data.readStrongBinder()); |
| 132 | const char* url = data.readCString(); |
| 133 | sp<IMediaPlayer> player = create(pid, client, url); |
| 134 | reply->writeStrongBinder(player->asBinder()); |
| 135 | return NO_ERROR; |
| 136 | } break; |
| 137 | case CREATE_FD: { |
| 138 | CHECK_INTERFACE(IMediaPlayerService, data, reply); |
| 139 | pid_t pid = data.readInt32(); |
| 140 | sp<IMediaPlayerClient> client = interface_cast<IMediaPlayerClient>(data.readStrongBinder()); |
| 141 | int fd = dup(data.readFileDescriptor()); |
| 142 | int64_t offset = data.readInt64(); |
| 143 | int64_t length = data.readInt64(); |
| 144 | sp<IMediaPlayer> player = create(pid, client, fd, offset, length); |
| 145 | reply->writeStrongBinder(player->asBinder()); |
| 146 | return NO_ERROR; |
| 147 | } break; |
| 148 | case DECODE_URL: { |
| 149 | CHECK_INTERFACE(IMediaPlayerService, data, reply); |
| 150 | const char* url = data.readCString(); |
| 151 | uint32_t sampleRate; |
| 152 | int numChannels; |
The Android Open Source Project | 7b5eb02 | 2008-12-17 18:05:43 -0800 | [diff] [blame^] | 153 | int format; |
| 154 | sp<IMemory> player = decode(url, &sampleRate, &numChannels, &format); |
The Android Open Source Project | 2729ea9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 155 | reply->writeInt32(sampleRate); |
| 156 | reply->writeInt32(numChannels); |
The Android Open Source Project | 7b5eb02 | 2008-12-17 18:05:43 -0800 | [diff] [blame^] | 157 | reply->writeInt32(format); |
The Android Open Source Project | 2729ea9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 158 | reply->writeStrongBinder(player->asBinder()); |
| 159 | return NO_ERROR; |
| 160 | } break; |
| 161 | case DECODE_FD: { |
| 162 | CHECK_INTERFACE(IMediaPlayerService, data, reply); |
| 163 | int fd = dup(data.readFileDescriptor()); |
| 164 | int64_t offset = data.readInt64(); |
| 165 | int64_t length = data.readInt64(); |
| 166 | uint32_t sampleRate; |
| 167 | int numChannels; |
The Android Open Source Project | 7b5eb02 | 2008-12-17 18:05:43 -0800 | [diff] [blame^] | 168 | int format; |
| 169 | sp<IMemory> player = decode(fd, offset, length, &sampleRate, &numChannels, &format); |
The Android Open Source Project | 2729ea9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 170 | reply->writeInt32(sampleRate); |
| 171 | reply->writeInt32(numChannels); |
The Android Open Source Project | 7b5eb02 | 2008-12-17 18:05:43 -0800 | [diff] [blame^] | 172 | reply->writeInt32(format); |
The Android Open Source Project | 2729ea9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 173 | reply->writeStrongBinder(player->asBinder()); |
| 174 | return NO_ERROR; |
| 175 | } break; |
The Android Open Source Project | 7b5eb02 | 2008-12-17 18:05:43 -0800 | [diff] [blame^] | 176 | case CREATE_MEDIA_RECORDER: { |
| 177 | CHECK_INTERFACE(IMediaPlayerService, data, reply); |
| 178 | pid_t pid = data.readInt32(); |
| 179 | sp<IMediaRecorder> recorder = createMediaRecorder(pid); |
| 180 | reply->writeStrongBinder(recorder->asBinder()); |
| 181 | return NO_ERROR; |
| 182 | } break; |
| 183 | case CREATE_METADATA_RETRIEVER: { |
| 184 | CHECK_INTERFACE(IMediaPlayerService, data, reply); |
| 185 | pid_t pid = data.readInt32(); |
| 186 | sp<IMediaMetadataRetriever> retriever = createMetadataRetriever(pid); |
| 187 | reply->writeStrongBinder(retriever->asBinder()); |
| 188 | return NO_ERROR; |
| 189 | } break; |
The Android Open Source Project | 2729ea9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 190 | default: |
| 191 | return BBinder::onTransact(code, data, reply, flags); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // ---------------------------------------------------------------------------- |
| 196 | |
| 197 | }; // namespace android |
| 198 | |