Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 1 | /* |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2 | ** |
| 3 | ** Copyright 2007, The Android Open Source Project |
| 4 | ** |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 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 |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8 | ** |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10 | ** |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 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 |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 18 | #define LOG_TAG "IAudioTrack" |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | #include <utils/Log.h> |
| 21 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | #include <sys/types.h> |
| 24 | |
Mathias Agopian | 7562408 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 25 | #include <binder/Parcel.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
| 27 | #include <media/IAudioTrack.h> |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | enum { |
| 32 | GET_CBLK = IBinder::FIRST_CALL_TRANSACTION, |
| 33 | START, |
| 34 | STOP, |
| 35 | FLUSH, |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 36 | RESERVED, // was MUTE |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 37 | PAUSE, |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 38 | ATTACH_AUX_EFFECT, |
| 39 | ALLOCATE_TIMED_BUFFER, |
| 40 | QUEUE_TIMED_BUFFER, |
| 41 | SET_MEDIA_TIME_TRANSFORM, |
Glenn Kasten | 53cec22 | 2013-08-29 09:01:02 -0700 | [diff] [blame^] | 42 | SET_PARAMETERS, |
| 43 | GET_TIMESTAMP, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | class BpAudioTrack : public BpInterface<IAudioTrack> |
| 47 | { |
| 48 | public: |
| 49 | BpAudioTrack(const sp<IBinder>& impl) |
| 50 | : BpInterface<IAudioTrack>(impl) |
| 51 | { |
| 52 | } |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 53 | |
Glenn Kasten | 1099586 | 2012-01-03 14:50:23 -0800 | [diff] [blame] | 54 | virtual sp<IMemory> getCblk() const |
| 55 | { |
| 56 | Parcel data, reply; |
| 57 | sp<IMemory> cblk; |
| 58 | data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor()); |
| 59 | status_t status = remote()->transact(GET_CBLK, data, &reply); |
| 60 | if (status == NO_ERROR) { |
| 61 | cblk = interface_cast<IMemory>(reply.readStrongBinder()); |
| 62 | } |
| 63 | return cblk; |
| 64 | } |
| 65 | |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 66 | virtual status_t start() |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | { |
| 68 | Parcel data, reply; |
| 69 | data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor()); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 70 | status_t status = remote()->transact(START, data, &reply); |
| 71 | if (status == NO_ERROR) { |
| 72 | status = reply.readInt32(); |
| 73 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 74 | ALOGW("start() error: %s", strerror(-status)); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 75 | } |
| 76 | return status; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | } |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 78 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | virtual void stop() |
| 80 | { |
| 81 | Parcel data, reply; |
| 82 | data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor()); |
| 83 | remote()->transact(STOP, data, &reply); |
| 84 | } |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 85 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | virtual void flush() |
| 87 | { |
| 88 | Parcel data, reply; |
| 89 | data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor()); |
| 90 | remote()->transact(FLUSH, data, &reply); |
| 91 | } |
| 92 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | virtual void pause() |
| 94 | { |
| 95 | Parcel data, reply; |
| 96 | data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor()); |
| 97 | remote()->transact(PAUSE, data, &reply); |
| 98 | } |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 99 | |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 100 | virtual status_t attachAuxEffect(int effectId) |
| 101 | { |
| 102 | Parcel data, reply; |
| 103 | data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor()); |
| 104 | data.writeInt32(effectId); |
| 105 | status_t status = remote()->transact(ATTACH_AUX_EFFECT, data, &reply); |
| 106 | if (status == NO_ERROR) { |
| 107 | status = reply.readInt32(); |
| 108 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 109 | ALOGW("attachAuxEffect() error: %s", strerror(-status)); |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 110 | } |
| 111 | return status; |
| 112 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 113 | |
| 114 | virtual status_t allocateTimedBuffer(size_t size, sp<IMemory>* buffer) { |
| 115 | Parcel data, reply; |
| 116 | data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor()); |
| 117 | data.writeInt32(size); |
| 118 | status_t status = remote()->transact(ALLOCATE_TIMED_BUFFER, |
| 119 | data, &reply); |
| 120 | if (status == NO_ERROR) { |
| 121 | status = reply.readInt32(); |
| 122 | if (status == NO_ERROR) { |
| 123 | *buffer = interface_cast<IMemory>(reply.readStrongBinder()); |
| 124 | } |
| 125 | } |
| 126 | return status; |
| 127 | } |
| 128 | |
| 129 | virtual status_t queueTimedBuffer(const sp<IMemory>& buffer, |
| 130 | int64_t pts) { |
| 131 | Parcel data, reply; |
| 132 | data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor()); |
| 133 | data.writeStrongBinder(buffer->asBinder()); |
| 134 | data.writeInt64(pts); |
| 135 | status_t status = remote()->transact(QUEUE_TIMED_BUFFER, |
| 136 | data, &reply); |
| 137 | if (status == NO_ERROR) { |
| 138 | status = reply.readInt32(); |
| 139 | } |
| 140 | return status; |
| 141 | } |
| 142 | |
| 143 | virtual status_t setMediaTimeTransform(const LinearTransform& xform, |
| 144 | int target) { |
| 145 | Parcel data, reply; |
| 146 | data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor()); |
| 147 | data.writeInt64(xform.a_zero); |
| 148 | data.writeInt64(xform.b_zero); |
| 149 | data.writeInt32(xform.a_to_b_numer); |
| 150 | data.writeInt32(xform.a_to_b_denom); |
| 151 | data.writeInt32(target); |
| 152 | status_t status = remote()->transact(SET_MEDIA_TIME_TRANSFORM, |
| 153 | data, &reply); |
| 154 | if (status == NO_ERROR) { |
| 155 | status = reply.readInt32(); |
| 156 | } |
| 157 | return status; |
| 158 | } |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 159 | |
| 160 | virtual status_t setParameters(const String8& keyValuePairs) { |
| 161 | Parcel data, reply; |
| 162 | data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor()); |
| 163 | data.writeString8(keyValuePairs); |
| 164 | status_t status = remote()->transact(SET_PARAMETERS, data, &reply); |
| 165 | if (status == NO_ERROR) { |
| 166 | status = reply.readInt32(); |
| 167 | } |
| 168 | return status; |
| 169 | } |
Glenn Kasten | 53cec22 | 2013-08-29 09:01:02 -0700 | [diff] [blame^] | 170 | |
| 171 | virtual status_t getTimestamp(AudioTimestamp& timestamp) { |
| 172 | Parcel data, reply; |
| 173 | data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor()); |
| 174 | status_t status = remote()->transact(GET_TIMESTAMP, data, &reply); |
| 175 | if (status == NO_ERROR) { |
| 176 | status = reply.readInt32(); |
| 177 | if (status == NO_ERROR) { |
| 178 | timestamp.mPosition = reply.readInt32(); |
| 179 | timestamp.mTime.tv_sec = reply.readInt32(); |
| 180 | timestamp.mTime.tv_nsec = reply.readInt32(); |
| 181 | } |
| 182 | } |
| 183 | return status; |
| 184 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | IMPLEMENT_META_INTERFACE(AudioTrack, "android.media.IAudioTrack"); |
| 188 | |
| 189 | // ---------------------------------------------------------------------- |
| 190 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | status_t BnAudioTrack::onTransact( |
| 192 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 193 | { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 194 | switch (code) { |
| 195 | case GET_CBLK: { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | CHECK_INTERFACE(IAudioTrack, data, reply); |
| 197 | reply->writeStrongBinder(getCblk()->asBinder()); |
| 198 | return NO_ERROR; |
| 199 | } break; |
| 200 | case START: { |
| 201 | CHECK_INTERFACE(IAudioTrack, data, reply); |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 202 | reply->writeInt32(start()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | return NO_ERROR; |
| 204 | } break; |
| 205 | case STOP: { |
| 206 | CHECK_INTERFACE(IAudioTrack, data, reply); |
| 207 | stop(); |
| 208 | return NO_ERROR; |
| 209 | } break; |
| 210 | case FLUSH: { |
| 211 | CHECK_INTERFACE(IAudioTrack, data, reply); |
| 212 | flush(); |
| 213 | return NO_ERROR; |
| 214 | } break; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | case PAUSE: { |
| 216 | CHECK_INTERFACE(IAudioTrack, data, reply); |
| 217 | pause(); |
| 218 | return NO_ERROR; |
| 219 | } |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 220 | case ATTACH_AUX_EFFECT: { |
| 221 | CHECK_INTERFACE(IAudioTrack, data, reply); |
| 222 | reply->writeInt32(attachAuxEffect(data.readInt32())); |
| 223 | return NO_ERROR; |
| 224 | } break; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 225 | case ALLOCATE_TIMED_BUFFER: { |
| 226 | CHECK_INTERFACE(IAudioTrack, data, reply); |
| 227 | sp<IMemory> buffer; |
| 228 | status_t status = allocateTimedBuffer(data.readInt32(), &buffer); |
| 229 | reply->writeInt32(status); |
| 230 | if (status == NO_ERROR) { |
| 231 | reply->writeStrongBinder(buffer->asBinder()); |
| 232 | } |
| 233 | return NO_ERROR; |
| 234 | } break; |
| 235 | case QUEUE_TIMED_BUFFER: { |
| 236 | CHECK_INTERFACE(IAudioTrack, data, reply); |
| 237 | sp<IMemory> buffer = interface_cast<IMemory>( |
| 238 | data.readStrongBinder()); |
| 239 | uint64_t pts = data.readInt64(); |
| 240 | reply->writeInt32(queueTimedBuffer(buffer, pts)); |
| 241 | return NO_ERROR; |
| 242 | } break; |
| 243 | case SET_MEDIA_TIME_TRANSFORM: { |
| 244 | CHECK_INTERFACE(IAudioTrack, data, reply); |
| 245 | LinearTransform xform; |
| 246 | xform.a_zero = data.readInt64(); |
| 247 | xform.b_zero = data.readInt64(); |
| 248 | xform.a_to_b_numer = data.readInt32(); |
| 249 | xform.a_to_b_denom = data.readInt32(); |
| 250 | int target = data.readInt32(); |
| 251 | reply->writeInt32(setMediaTimeTransform(xform, target)); |
| 252 | return NO_ERROR; |
| 253 | } break; |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 254 | case SET_PARAMETERS: { |
| 255 | CHECK_INTERFACE(IAudioTrack, data, reply); |
| 256 | String8 keyValuePairs(data.readString8()); |
| 257 | reply->writeInt32(setParameters(keyValuePairs)); |
| 258 | return NO_ERROR; |
| 259 | } break; |
Glenn Kasten | 53cec22 | 2013-08-29 09:01:02 -0700 | [diff] [blame^] | 260 | case GET_TIMESTAMP: { |
| 261 | CHECK_INTERFACE(IAudioTrack, data, reply); |
| 262 | AudioTimestamp timestamp; |
| 263 | status_t status = getTimestamp(timestamp); |
| 264 | reply->writeInt32(status); |
| 265 | if (status == NO_ERROR) { |
| 266 | reply->writeInt32(timestamp.mPosition); |
| 267 | reply->writeInt32(timestamp.mTime.tv_sec); |
| 268 | reply->writeInt32(timestamp.mTime.tv_nsec); |
| 269 | } |
| 270 | return NO_ERROR; |
| 271 | } break; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | default: |
| 273 | return BBinder::onTransact(code, data, reply, flags); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | }; // namespace android |