blob: df209fd9a8ccbbbca233b1bc3497eb0b72f02608 [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,
39 ALLOCATE_TIMED_BUFFER,
40 QUEUE_TIMED_BUFFER,
41 SET_MEDIA_TIME_TRANSFORM,
Glenn Kasten53cec222013-08-29 09:01:02 -070042 SET_PARAMETERS,
43 GET_TIMESTAMP,
Eric Laurent59fe0102013-09-27 18:48:26 -070044 SIGNAL,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080045};
46
47class BpAudioTrack : public BpInterface<IAudioTrack>
48{
49public:
50 BpAudioTrack(const sp<IBinder>& impl)
51 : BpInterface<IAudioTrack>(impl)
52 {
53 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -070054
Glenn Kasten10995862012-01-03 14:50:23 -080055 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 Kastena1d401d2013-11-20 14:37:13 -080063 if (cblk != 0 && cblk->pointer() == NULL) {
64 cblk.clear();
65 }
Glenn Kasten10995862012-01-03 14:50:23 -080066 }
67 return cblk;
68 }
69
Glenn Kasten3acbd052012-02-28 10:39:56 -080070 virtual status_t start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080071 {
72 Parcel data, reply;
73 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
Eric Laurent34f1d8e2009-11-04 08:27:26 -080074 status_t status = remote()->transact(START, data, &reply);
75 if (status == NO_ERROR) {
76 status = reply.readInt32();
77 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +000078 ALOGW("start() error: %s", strerror(-status));
Eric Laurent34f1d8e2009-11-04 08:27:26 -080079 }
80 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080081 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -070082
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080083 virtual void stop()
84 {
85 Parcel data, reply;
86 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
87 remote()->transact(STOP, data, &reply);
88 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -070089
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080090 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 Project89fa4ad2009-03-03 19:31:44 -080097 virtual void pause()
98 {
99 Parcel data, reply;
100 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
101 remote()->transact(PAUSE, data, &reply);
102 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700103
Eric Laurentbe916aa2010-06-01 23:49:17 -0700104 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 Block5ff1dd52012-01-05 23:22:43 +0000113 ALOGW("attachAuxEffect() error: %s", strerror(-status));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700114 }
115 return status;
116 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800117
118 virtual status_t allocateTimedBuffer(size_t size, sp<IMemory>* buffer) {
119 Parcel data, reply;
120 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
Glenn Kastene03dd222014-01-28 11:04:39 -0800121 data.writeInt64(size);
John Grossman4ff14ba2012-02-08 16:37:41 -0800122 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 Kastena1d401d2013-11-20 14:37:13 -0800128 if (*buffer != 0 && (*buffer)->pointer() == NULL) {
129 (*buffer).clear();
130 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800131 }
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 Nelissen06b46062014-11-14 07:58:25 -0800140 data.writeStrongBinder(IInterface::asBinder(buffer));
John Grossman4ff14ba2012-02-08 16:37:41 -0800141 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 Fitzgeraldad3af332013-03-25 16:54:37 +0000166
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 Kasten53cec222013-08-29 09:01:02 -0700177
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 Laurent59fe0102013-09-27 18:48:26 -0700192
193 virtual void signal() {
194 Parcel data, reply;
195 data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
196 remote()->transact(SIGNAL, data, &reply);
197 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800198};
199
200IMPLEMENT_META_INTERFACE(AudioTrack, "android.media.IAudioTrack");
201
202// ----------------------------------------------------------------------
203
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800204status_t BnAudioTrack::onTransact(
205 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
206{
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700207 switch (code) {
208 case GET_CBLK: {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800209 CHECK_INTERFACE(IAudioTrack, data, reply);
Marco Nelissen06b46062014-11-14 07:58:25 -0800210 reply->writeStrongBinder(IInterface::asBinder(getCblk()));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800211 return NO_ERROR;
212 } break;
213 case START: {
214 CHECK_INTERFACE(IAudioTrack, data, reply);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800215 reply->writeInt32(start());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800216 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 Project89fa4ad2009-03-03 19:31:44 -0800228 case PAUSE: {
229 CHECK_INTERFACE(IAudioTrack, data, reply);
230 pause();
231 return NO_ERROR;
232 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700233 case ATTACH_AUX_EFFECT: {
234 CHECK_INTERFACE(IAudioTrack, data, reply);
235 reply->writeInt32(attachAuxEffect(data.readInt32()));
236 return NO_ERROR;
237 } break;
John Grossman4ff14ba2012-02-08 16:37:41 -0800238 case ALLOCATE_TIMED_BUFFER: {
239 CHECK_INTERFACE(IAudioTrack, data, reply);
240 sp<IMemory> buffer;
Glenn Kastene03dd222014-01-28 11:04:39 -0800241 status_t status = allocateTimedBuffer(data.readInt64(), &buffer);
John Grossman4ff14ba2012-02-08 16:37:41 -0800242 reply->writeInt32(status);
243 if (status == NO_ERROR) {
Marco Nelissen06b46062014-11-14 07:58:25 -0800244 reply->writeStrongBinder(IInterface::asBinder(buffer));
John Grossman4ff14ba2012-02-08 16:37:41 -0800245 }
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 Fitzgeraldad3af332013-03-25 16:54:37 +0000267 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 Kasten53cec222013-08-29 09:01:02 -0700273 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 Laurent59fe0102013-09-27 18:48:26 -0700285 case SIGNAL: {
286 CHECK_INTERFACE(IAudioTrack, data, reply);
287 signal();
288 return NO_ERROR;
289 } break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800290 default:
291 return BBinder::onTransact(code, data, reply, flags);
292 }
293}
294
295}; // namespace android