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