blob: 6219e7a4ba0299ea12b65e7df42a54deb087a90c [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
Ivan Lozano8cf3a072017-08-09 09:01:33 -070031using media::VolumeShaper;
32
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080033enum {
34 GET_CBLK = IBinder::FIRST_CALL_TRANSACTION,
35 START,
36 STOP,
37 FLUSH,
Glenn Kastene4756fe2012-11-29 13:38:14 -080038 RESERVED, // was MUTE
Eric Laurentbe916aa2010-06-01 23:49:17 -070039 PAUSE,
John Grossman4ff14ba2012-02-08 16:37:41 -080040 ATTACH_AUX_EFFECT,
Glenn Kasten53cec222013-08-29 09:01:02 -070041 SET_PARAMETERS,
Mikhail Naganovac917ac2018-11-28 14:03:52 -080042 SELECT_PRESENTATION,
Glenn Kasten53cec222013-08-29 09:01:02 -070043 GET_TIMESTAMP,
Eric Laurent59fe0102013-09-27 18:48:26 -070044 SIGNAL,
Andy Hung9fc8b5c2017-01-24 13:36:48 -080045 APPLY_VOLUME_SHAPER,
46 GET_VOLUME_SHAPER_STATE,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080047};
48
49class BpAudioTrack : public BpInterface<IAudioTrack>
50{
51public:
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070052 explicit BpAudioTrack(const sp<IBinder>& impl)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080053 : BpInterface<IAudioTrack>(impl)
54 {
55 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -070056
Glenn Kasten10995862012-01-03 14:50:23 -080057 virtual sp<IMemory> getCblk() const
58 {
59 Parcel data, reply;
60 sp<IMemory> cblk;
61 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
62 status_t status = remote()->transact(GET_CBLK, data, &reply);
63 if (status == NO_ERROR) {
64 cblk = interface_cast<IMemory>(reply.readStrongBinder());
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -070065 if (cblk != 0 && cblk->unsecurePointer() == NULL) {
Glenn Kastena1d401d2013-11-20 14:37:13 -080066 cblk.clear();
67 }
Glenn Kasten10995862012-01-03 14:50:23 -080068 }
69 return cblk;
70 }
71
Glenn Kasten3acbd052012-02-28 10:39:56 -080072 virtual status_t start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080073 {
74 Parcel data, reply;
75 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
Eric Laurent34f1d8e2009-11-04 08:27:26 -080076 status_t status = remote()->transact(START, data, &reply);
77 if (status == NO_ERROR) {
78 status = reply.readInt32();
79 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +000080 ALOGW("start() error: %s", strerror(-status));
Eric Laurent34f1d8e2009-11-04 08:27:26 -080081 }
82 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080083 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -070084
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080085 virtual void stop()
86 {
87 Parcel data, reply;
88 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
89 remote()->transact(STOP, data, &reply);
90 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -070091
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080092 virtual void flush()
93 {
94 Parcel data, reply;
95 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
96 remote()->transact(FLUSH, data, &reply);
97 }
98
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080099 virtual void pause()
100 {
101 Parcel data, reply;
102 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
103 remote()->transact(PAUSE, data, &reply);
104 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700105
Eric Laurentbe916aa2010-06-01 23:49:17 -0700106 virtual status_t attachAuxEffect(int effectId)
107 {
108 Parcel data, reply;
109 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
110 data.writeInt32(effectId);
111 status_t status = remote()->transact(ATTACH_AUX_EFFECT, data, &reply);
112 if (status == NO_ERROR) {
113 status = reply.readInt32();
114 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +0000115 ALOGW("attachAuxEffect() error: %s", strerror(-status));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700116 }
117 return status;
118 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800119
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000120 virtual status_t setParameters(const String8& keyValuePairs) {
121 Parcel data, reply;
122 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
123 data.writeString8(keyValuePairs);
124 status_t status = remote()->transact(SET_PARAMETERS, data, &reply);
125 if (status == NO_ERROR) {
126 status = reply.readInt32();
127 }
128 return status;
129 }
Glenn Kasten53cec222013-08-29 09:01:02 -0700130
Mikhail Naganovac917ac2018-11-28 14:03:52 -0800131 /* Selects the presentation (if available) */
132 virtual status_t selectPresentation(int presentationId, int programId) {
133 Parcel data, reply;
134 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
135 data.writeInt32(presentationId);
136 data.writeInt32(programId);
137 status_t status = remote()->transact(SELECT_PRESENTATION, data, &reply);
138 if (status == NO_ERROR) {
139 status = reply.readInt32();
140 }
141 return status;
142 }
143
Glenn Kasten53cec222013-08-29 09:01:02 -0700144 virtual status_t getTimestamp(AudioTimestamp& timestamp) {
145 Parcel data, reply;
146 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
147 status_t status = remote()->transact(GET_TIMESTAMP, data, &reply);
148 if (status == NO_ERROR) {
149 status = reply.readInt32();
150 if (status == NO_ERROR) {
151 timestamp.mPosition = reply.readInt32();
152 timestamp.mTime.tv_sec = reply.readInt32();
153 timestamp.mTime.tv_nsec = reply.readInt32();
154 }
155 }
156 return status;
157 }
Eric Laurent59fe0102013-09-27 18:48:26 -0700158
159 virtual void signal() {
160 Parcel data, reply;
161 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
162 remote()->transact(SIGNAL, data, &reply);
163 }
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800164
165 virtual VolumeShaper::Status applyVolumeShaper(
166 const sp<VolumeShaper::Configuration>& configuration,
167 const sp<VolumeShaper::Operation>& operation) {
168 Parcel data, reply;
169 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
170
171 status_t status = configuration.get() == nullptr
172 ? data.writeInt32(0)
173 : data.writeInt32(1)
174 ?: configuration->writeToParcel(&data);
175 if (status != NO_ERROR) {
176 return VolumeShaper::Status(status);
177 }
178
179 status = operation.get() == nullptr
180 ? status = data.writeInt32(0)
181 : data.writeInt32(1)
182 ?: operation->writeToParcel(&data);
183 if (status != NO_ERROR) {
184 return VolumeShaper::Status(status);
185 }
186
187 int32_t remoteVolumeShaperStatus;
188 status = remote()->transact(APPLY_VOLUME_SHAPER, data, &reply)
189 ?: reply.readInt32(&remoteVolumeShaperStatus);
190
191 return VolumeShaper::Status(status ?: remoteVolumeShaperStatus);
192 }
193
194 virtual sp<VolumeShaper::State> getVolumeShaperState(int id) {
195 Parcel data, reply;
196 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
197
198 data.writeInt32(id);
199 status_t status = remote()->transact(GET_VOLUME_SHAPER_STATE, data, &reply);
200 if (status != NO_ERROR) {
201 return nullptr;
202 }
203 sp<VolumeShaper::State> state = new VolumeShaper::State;
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700204 status = state->readFromParcel(&reply);
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800205 if (status != NO_ERROR) {
206 return nullptr;
207 }
208 return state;
209 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800210};
211
212IMPLEMENT_META_INTERFACE(AudioTrack, "android.media.IAudioTrack");
213
214// ----------------------------------------------------------------------
215
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800216status_t BnAudioTrack::onTransact(
217 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
218{
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700219 switch (code) {
220 case GET_CBLK: {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800221 CHECK_INTERFACE(IAudioTrack, data, reply);
Marco Nelissen06b46062014-11-14 07:58:25 -0800222 reply->writeStrongBinder(IInterface::asBinder(getCblk()));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800223 return NO_ERROR;
224 } break;
225 case START: {
226 CHECK_INTERFACE(IAudioTrack, data, reply);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800227 reply->writeInt32(start());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800228 return NO_ERROR;
229 } break;
230 case STOP: {
231 CHECK_INTERFACE(IAudioTrack, data, reply);
232 stop();
233 return NO_ERROR;
234 } break;
235 case FLUSH: {
236 CHECK_INTERFACE(IAudioTrack, data, reply);
237 flush();
238 return NO_ERROR;
239 } break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800240 case PAUSE: {
241 CHECK_INTERFACE(IAudioTrack, data, reply);
242 pause();
243 return NO_ERROR;
244 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700245 case ATTACH_AUX_EFFECT: {
246 CHECK_INTERFACE(IAudioTrack, data, reply);
247 reply->writeInt32(attachAuxEffect(data.readInt32()));
248 return NO_ERROR;
249 } break;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000250 case SET_PARAMETERS: {
251 CHECK_INTERFACE(IAudioTrack, data, reply);
252 String8 keyValuePairs(data.readString8());
253 reply->writeInt32(setParameters(keyValuePairs));
254 return NO_ERROR;
255 } break;
Mikhail Naganovac917ac2018-11-28 14:03:52 -0800256 case SELECT_PRESENTATION: {
257 CHECK_INTERFACE(IAudioTrack, data, reply);
258 reply->writeInt32(selectPresentation(data.readInt32(), data.readInt32()));
259 return NO_ERROR;
260 } break;
Glenn Kasten53cec222013-08-29 09:01:02 -0700261 case GET_TIMESTAMP: {
262 CHECK_INTERFACE(IAudioTrack, data, reply);
263 AudioTimestamp timestamp;
264 status_t status = getTimestamp(timestamp);
265 reply->writeInt32(status);
266 if (status == NO_ERROR) {
267 reply->writeInt32(timestamp.mPosition);
268 reply->writeInt32(timestamp.mTime.tv_sec);
269 reply->writeInt32(timestamp.mTime.tv_nsec);
270 }
271 return NO_ERROR;
272 } break;
Eric Laurent59fe0102013-09-27 18:48:26 -0700273 case SIGNAL: {
274 CHECK_INTERFACE(IAudioTrack, data, reply);
275 signal();
276 return NO_ERROR;
277 } break;
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800278 case APPLY_VOLUME_SHAPER: {
279 CHECK_INTERFACE(IAudioTrack, data, reply);
280 sp<VolumeShaper::Configuration> configuration;
281 sp<VolumeShaper::Operation> operation;
282
283 int32_t present;
284 status_t status = data.readInt32(&present);
285 if (status == NO_ERROR && present != 0) {
286 configuration = new VolumeShaper::Configuration();
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700287 status = configuration->readFromParcel(&data);
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800288 }
289 status = status ?: data.readInt32(&present);
290 if (status == NO_ERROR && present != 0) {
291 operation = new VolumeShaper::Operation();
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700292 status = operation->readFromParcel(&data);
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800293 }
294 if (status == NO_ERROR) {
295 status = (status_t)applyVolumeShaper(configuration, operation);
296 }
297 reply->writeInt32(status);
298 return NO_ERROR;
299 } break;
300 case GET_VOLUME_SHAPER_STATE: {
301 CHECK_INTERFACE(IAudioTrack, data, reply);
302 int id;
303 status_t status = data.readInt32(&id);
304 if (status == NO_ERROR) {
305 sp<VolumeShaper::State> state = getVolumeShaperState(id);
306 if (state.get() != nullptr) {
307 status = state->writeToParcel(reply);
308 }
309 }
310 return NO_ERROR;
311 } break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800312 default:
313 return BBinder::onTransact(code, data, reply, flags);
314 }
315}
316
Glenn Kasten40bc9062015-03-20 09:09:33 -0700317} // namespace android