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