blob: 79e864d100f7dd689e7d39bad1899f03c9576fbd [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002**
3** Copyright 2007, The Android Open Source Project
4**
Glenn Kastene53b9ea2012-03-12 16:29:55 -07005** 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 Project89fa4ad2009-03-03 19:31:44 -08008**
Glenn Kastene53b9ea2012-03-12 16:29:55 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080010**
Glenn Kastene53b9ea2012-03-12 16:29:55 -070011** 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 Project89fa4ad2009-03-03 19:31:44 -080015** limitations under the License.
16*/
17
Eric Laurent34f1d8e2009-11-04 08:27:26 -080018#define LOG_TAG "IAudioTrack"
19//#define LOG_NDEBUG 0
20#include <utils/Log.h>
21
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080022#include <stdint.h>
23#include <sys/types.h>
24
Mathias Agopian75624082009-05-19 19:08:10 -070025#include <binder/Parcel.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080026
27#include <media/IAudioTrack.h>
28
29namespace android {
30
31enum {
32 GET_CBLK = IBinder::FIRST_CALL_TRANSACTION,
33 START,
34 STOP,
35 FLUSH,
Glenn Kastene4756fe2012-11-29 13:38:14 -080036 RESERVED, // was MUTE
Eric Laurentbe916aa2010-06-01 23:49:17 -070037 PAUSE,
John Grossman4ff14ba2012-02-08 16:37:41 -080038 ATTACH_AUX_EFFECT,
Glenn Kasten53cec222013-08-29 09:01:02 -070039 SET_PARAMETERS,
40 GET_TIMESTAMP,
Eric Laurent59fe0102013-09-27 18:48:26 -070041 SIGNAL,
Andy Hung9fc8b5c2017-01-24 13:36:48 -080042 APPLY_VOLUME_SHAPER,
43 GET_VOLUME_SHAPER_STATE,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080044};
45
46class BpAudioTrack : public BpInterface<IAudioTrack>
47{
48public:
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070049 explicit BpAudioTrack(const sp<IBinder>& impl)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080050 : BpInterface<IAudioTrack>(impl)
51 {
52 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -070053
Glenn Kasten10995862012-01-03 14:50:23 -080054 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());
Glenn Kastena1d401d2013-11-20 14:37:13 -080062 if (cblk != 0 && cblk->pointer() == NULL) {
63 cblk.clear();
64 }
Glenn Kasten10995862012-01-03 14:50:23 -080065 }
66 return cblk;
67 }
68
Glenn Kasten3acbd052012-02-28 10:39:56 -080069 virtual status_t start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080070 {
71 Parcel data, reply;
72 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
Eric Laurent34f1d8e2009-11-04 08:27:26 -080073 status_t status = remote()->transact(START, data, &reply);
74 if (status == NO_ERROR) {
75 status = reply.readInt32();
76 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +000077 ALOGW("start() error: %s", strerror(-status));
Eric Laurent34f1d8e2009-11-04 08:27:26 -080078 }
79 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080080 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -070081
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080082 virtual void stop()
83 {
84 Parcel data, reply;
85 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
86 remote()->transact(STOP, data, &reply);
87 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -070088
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080089 virtual void flush()
90 {
91 Parcel data, reply;
92 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
93 remote()->transact(FLUSH, data, &reply);
94 }
95
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080096 virtual void pause()
97 {
98 Parcel data, reply;
99 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
100 remote()->transact(PAUSE, data, &reply);
101 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700102
Eric Laurentbe916aa2010-06-01 23:49:17 -0700103 virtual status_t attachAuxEffect(int effectId)
104 {
105 Parcel data, reply;
106 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
107 data.writeInt32(effectId);
108 status_t status = remote()->transact(ATTACH_AUX_EFFECT, data, &reply);
109 if (status == NO_ERROR) {
110 status = reply.readInt32();
111 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +0000112 ALOGW("attachAuxEffect() error: %s", strerror(-status));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700113 }
114 return status;
115 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800116
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000117 virtual status_t setParameters(const String8& keyValuePairs) {
118 Parcel data, reply;
119 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
120 data.writeString8(keyValuePairs);
121 status_t status = remote()->transact(SET_PARAMETERS, data, &reply);
122 if (status == NO_ERROR) {
123 status = reply.readInt32();
124 }
125 return status;
126 }
Glenn Kasten53cec222013-08-29 09:01:02 -0700127
128 virtual status_t getTimestamp(AudioTimestamp& timestamp) {
129 Parcel data, reply;
130 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
131 status_t status = remote()->transact(GET_TIMESTAMP, data, &reply);
132 if (status == NO_ERROR) {
133 status = reply.readInt32();
134 if (status == NO_ERROR) {
135 timestamp.mPosition = reply.readInt32();
136 timestamp.mTime.tv_sec = reply.readInt32();
137 timestamp.mTime.tv_nsec = reply.readInt32();
138 }
139 }
140 return status;
141 }
Eric Laurent59fe0102013-09-27 18:48:26 -0700142
143 virtual void signal() {
144 Parcel data, reply;
145 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
146 remote()->transact(SIGNAL, data, &reply);
147 }
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800148
149 virtual VolumeShaper::Status applyVolumeShaper(
150 const sp<VolumeShaper::Configuration>& configuration,
151 const sp<VolumeShaper::Operation>& operation) {
152 Parcel data, reply;
153 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
154
155 status_t status = configuration.get() == nullptr
156 ? data.writeInt32(0)
157 : data.writeInt32(1)
158 ?: configuration->writeToParcel(&data);
159 if (status != NO_ERROR) {
160 return VolumeShaper::Status(status);
161 }
162
163 status = operation.get() == nullptr
164 ? status = data.writeInt32(0)
165 : data.writeInt32(1)
166 ?: operation->writeToParcel(&data);
167 if (status != NO_ERROR) {
168 return VolumeShaper::Status(status);
169 }
170
171 int32_t remoteVolumeShaperStatus;
172 status = remote()->transact(APPLY_VOLUME_SHAPER, data, &reply)
173 ?: reply.readInt32(&remoteVolumeShaperStatus);
174
175 return VolumeShaper::Status(status ?: remoteVolumeShaperStatus);
176 }
177
178 virtual sp<VolumeShaper::State> getVolumeShaperState(int id) {
179 Parcel data, reply;
180 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
181
182 data.writeInt32(id);
183 status_t status = remote()->transact(GET_VOLUME_SHAPER_STATE, data, &reply);
184 if (status != NO_ERROR) {
185 return nullptr;
186 }
187 sp<VolumeShaper::State> state = new VolumeShaper::State;
188 status = state->readFromParcel(reply);
189 if (status != NO_ERROR) {
190 return nullptr;
191 }
192 return state;
193 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800194};
195
196IMPLEMENT_META_INTERFACE(AudioTrack, "android.media.IAudioTrack");
197
198// ----------------------------------------------------------------------
199
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800200status_t BnAudioTrack::onTransact(
201 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
202{
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700203 switch (code) {
204 case GET_CBLK: {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800205 CHECK_INTERFACE(IAudioTrack, data, reply);
Marco Nelissen06b46062014-11-14 07:58:25 -0800206 reply->writeStrongBinder(IInterface::asBinder(getCblk()));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800207 return NO_ERROR;
208 } break;
209 case START: {
210 CHECK_INTERFACE(IAudioTrack, data, reply);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800211 reply->writeInt32(start());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800212 return NO_ERROR;
213 } break;
214 case STOP: {
215 CHECK_INTERFACE(IAudioTrack, data, reply);
216 stop();
217 return NO_ERROR;
218 } break;
219 case FLUSH: {
220 CHECK_INTERFACE(IAudioTrack, data, reply);
221 flush();
222 return NO_ERROR;
223 } break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800224 case PAUSE: {
225 CHECK_INTERFACE(IAudioTrack, data, reply);
226 pause();
227 return NO_ERROR;
228 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700229 case ATTACH_AUX_EFFECT: {
230 CHECK_INTERFACE(IAudioTrack, data, reply);
231 reply->writeInt32(attachAuxEffect(data.readInt32()));
232 return NO_ERROR;
233 } break;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000234 case SET_PARAMETERS: {
235 CHECK_INTERFACE(IAudioTrack, data, reply);
236 String8 keyValuePairs(data.readString8());
237 reply->writeInt32(setParameters(keyValuePairs));
238 return NO_ERROR;
239 } break;
Glenn Kasten53cec222013-08-29 09:01:02 -0700240 case GET_TIMESTAMP: {
241 CHECK_INTERFACE(IAudioTrack, data, reply);
242 AudioTimestamp timestamp;
243 status_t status = getTimestamp(timestamp);
244 reply->writeInt32(status);
245 if (status == NO_ERROR) {
246 reply->writeInt32(timestamp.mPosition);
247 reply->writeInt32(timestamp.mTime.tv_sec);
248 reply->writeInt32(timestamp.mTime.tv_nsec);
249 }
250 return NO_ERROR;
251 } break;
Eric Laurent59fe0102013-09-27 18:48:26 -0700252 case SIGNAL: {
253 CHECK_INTERFACE(IAudioTrack, data, reply);
254 signal();
255 return NO_ERROR;
256 } break;
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800257 case APPLY_VOLUME_SHAPER: {
258 CHECK_INTERFACE(IAudioTrack, data, reply);
259 sp<VolumeShaper::Configuration> configuration;
260 sp<VolumeShaper::Operation> operation;
261
262 int32_t present;
263 status_t status = data.readInt32(&present);
264 if (status == NO_ERROR && present != 0) {
265 configuration = new VolumeShaper::Configuration();
266 status = configuration->readFromParcel(data);
267 }
268 status = status ?: data.readInt32(&present);
269 if (status == NO_ERROR && present != 0) {
270 operation = new VolumeShaper::Operation();
271 status = operation->readFromParcel(data);
272 }
273 if (status == NO_ERROR) {
274 status = (status_t)applyVolumeShaper(configuration, operation);
275 }
276 reply->writeInt32(status);
277 return NO_ERROR;
278 } break;
279 case GET_VOLUME_SHAPER_STATE: {
280 CHECK_INTERFACE(IAudioTrack, data, reply);
281 int id;
282 status_t status = data.readInt32(&id);
283 if (status == NO_ERROR) {
284 sp<VolumeShaper::State> state = getVolumeShaperState(id);
285 if (state.get() != nullptr) {
286 status = state->writeToParcel(reply);
287 }
288 }
289 return NO_ERROR;
290 } break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800291 default:
292 return BBinder::onTransact(code, data, reply, flags);
293 }
294}
295
Glenn Kasten40bc9062015-03-20 09:09:33 -0700296} // namespace android