summaryrefslogtreecommitdiff
path: root/sys/audioflingersink/audioflinger_wrapper.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'sys/audioflingersink/audioflinger_wrapper.cpp')
-rw-r--r--sys/audioflingersink/audioflinger_wrapper.cpp470
1 files changed, 470 insertions, 0 deletions
diff --git a/sys/audioflingersink/audioflinger_wrapper.cpp b/sys/audioflingersink/audioflinger_wrapper.cpp
new file mode 100644
index 000000000..64ad7faf0
--- /dev/null
+++ b/sys/audioflingersink/audioflinger_wrapper.cpp
@@ -0,0 +1,470 @@
1/* GStreamer
2 * Copyright (C) <2009> Prajnashi S <prajnashi@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19#define ENABLE_GST_PLAYER_LOG
20#include <media/AudioTrack.h>
21#include <utils/Log.h>
22#include <AudioFlinger.h>
23#include <MediaPlayerInterface.h>
24#include <MediaPlayerService.h>
25#include "audioflinger_wrapper.h"
26#include <glib/glib.h>
27//#include <GstLog.h>
28
29
30#define LOG_NDEBUG 0
31
32#undef LOG_TAG
33#define LOG_TAG "audioflinger_wrapper"
34
35
36using namespace android;
37
38
39typedef struct _AudioFlingerDevice
40{
41 AudioTrack* audio_track;
42 bool init;
43 sp<MediaPlayerBase::AudioSink> audio_sink;
44 bool audio_sink_specified;
45} AudioFlingerDevice;
46
47
48/* commonly used macro */
49#define AUDIO_FLINGER_DEVICE(handle) ((AudioFlingerDevice*)handle)
50#define AUDIO_FLINGER_DEVICE_TRACK(handle) \
51 (AUDIO_FLINGER_DEVICE(handle)->audio_track)
52#define AUDIO_FLINGER_DEVICE_SINK(handle) \
53 (AUDIO_FLINGER_DEVICE(handle)->audio_sink)
54
55
56AudioFlingerDeviceHandle audioflinger_device_create()
57{
58 AudioFlingerDevice* audiodev = NULL;
59 AudioTrack *audiotr = NULL;
60
61 // create a new instance of AudioFlinger
62 audiodev = new AudioFlingerDevice;
63 if (audiodev == NULL) {
64 LOGE("Error to create AudioFlingerDevice\n");
65 return NULL;
66 }
67
68 // create AudioTrack
69 audiotr = new AudioTrack ();
70 if (audiotr == NULL) {
71 LOGE("Error to create AudioTrack\n");
72 return NULL;
73 }
74
75 audiodev->init = false;
76 audiodev->audio_track = (AudioTrack *) audiotr;
77 audiodev->audio_sink = 0;
78 audiodev->audio_sink_specified = false;
79 LOGD("Create AudioTrack successfully %p\n",audiodev);
80
81 return (AudioFlingerDeviceHandle)audiodev;
82}
83
84AudioFlingerDeviceHandle audioflinger_device_open(void* audio_sink)
85{
86 AudioFlingerDevice* audiodev = NULL;
87
88 // audio_sink shall be an MediaPlayerBase::AudioSink instance
89 if(audio_sink == NULL)
90 return NULL;
91
92 // create a new instance of AudioFlinger
93 audiodev = new AudioFlingerDevice;
94 if (audiodev == NULL) {
95 LOGE("Error to create AudioFlingerDevice\n");
96 return NULL;
97 }
98
99 // set AudioSink
100
101 audiodev->audio_sink = (MediaPlayerBase::AudioSink*)audio_sink;
102 audiodev->audio_track = NULL;
103 audiodev->init = false;
104 audiodev->audio_sink_specified = true;
105 LOGD("Open AudioSink successfully : %p\n",audiodev);
106
107 return (AudioFlingerDeviceHandle)audiodev;
108}
109
110int audioflinger_device_set (AudioFlingerDeviceHandle handle,
111 int streamType, int channelCount, uint32_t sampleRate, int bufferCount)
112{
113 status_t status = NO_ERROR;
114#ifndef STECONF_ANDROID_VERSION_DONUT
115 uint32_t channels = 0;
116#endif
117
118 int format = AudioSystem::PCM_16_BIT;
119
120 if (handle == NULL)
121 return -1;
122
123 if(AUDIO_FLINGER_DEVICE_TRACK(handle)) {
124 // bufferCount is not the number of internal buffer, but the internal
125 // buffer size
126#ifdef STECONF_ANDROID_VERSION_DONUT
127 status = AUDIO_FLINGER_DEVICE_TRACK(handle)->set(streamType, sampleRate,
128 format, channelCount);
129 LOGD("SET : handle : %p : Set AudioTrack, status: %d, streamType: %d, sampleRate: %d, "
130 "channelCount: %d, bufferCount: %d\n",handle, status, streamType, sampleRate,
131 channelCount, bufferCount);
132#else
133 switch (channelCount)
134 {
135 case 1:
136 channels = AudioSystem::CHANNEL_OUT_FRONT_LEFT;
137 break;
138 case 2:
139 channels = AudioSystem::CHANNEL_OUT_STEREO;
140 break;
141 case 0:
142 default:
143 channels = 0;
144 break;
145 }
146 status = AUDIO_FLINGER_DEVICE_TRACK(handle)->set(streamType, sampleRate,
147 format, channels/*, bufferCount*/);
148 LOGD("SET handle : %p : Set AudioTrack, status: %d, streamType: %d, sampleRate: %d, "
149 "channelCount: %d(%d), bufferCount: %d\n",handle, status, streamType, sampleRate,
150 channelCount, channels, bufferCount);
151#endif
152 AUDIO_FLINGER_DEVICE_TRACK(handle)->setPositionUpdatePeriod(bufferCount);
153
154 }
155 else if(AUDIO_FLINGER_DEVICE_SINK(handle).get()) {
156#ifdef STECONF_ANDROID_VERSION_DONUT
157 status = AUDIO_FLINGER_DEVICE_SINK(handle)->open(sampleRate, channelCount,
158 format/*, bufferCount*/); //SDA
159
160 LOGD("OPEN : handle : %p : Set AudioSink, status: %d, streamType: %d, sampleRate: %d,"
161 "channelCount: %d, bufferCount: %d\n", handle, status, streamType, sampleRate,
162 channelCount, bufferCount);
163#else
164 channels = channelCount;
165 status = AUDIO_FLINGER_DEVICE_SINK(handle)->open(sampleRate, channels,
166 format/*, bufferCount*/);
167 LOGD("OPEN handle : %p : Set AudioSink, status: %d, streamType: %d, sampleRate: %d,"
168 "channelCount: %d(%d), bufferCount: %d\n", handle, status, streamType, sampleRate,
169 channelCount, channels, bufferCount);
170#endif
171 AUDIO_FLINGER_DEVICE_TRACK(handle) = (AudioTrack *)(AUDIO_FLINGER_DEVICE_SINK(handle)->getTrack());
172 if(AUDIO_FLINGER_DEVICE_TRACK(handle)) {
173 AUDIO_FLINGER_DEVICE_TRACK(handle)->setPositionUpdatePeriod(bufferCount);
174 }
175 }
176
177 if (status != NO_ERROR)
178 return -1;
179
180 AUDIO_FLINGER_DEVICE(handle)->init = true;
181
182 return 0;
183}
184
185void audioflinger_device_release (AudioFlingerDeviceHandle handle)
186{
187 if (handle == NULL)
188 return;
189
190 LOGD("Enter\n");
191 if(! AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified ) {
192 if (AUDIO_FLINGER_DEVICE_TRACK(handle) ) {
193 LOGD("handle : %p Release AudioTrack\n", handle);
194 delete AUDIO_FLINGER_DEVICE_TRACK(handle);
195 }
196 }
197 if (AUDIO_FLINGER_DEVICE_SINK(handle).get()) {
198 LOGD("handle : %p Release AudioSink\n", handle);
199 AUDIO_FLINGER_DEVICE_SINK(handle).clear();
200 AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified = false;
201 }
202
203 delete AUDIO_FLINGER_DEVICE(handle);
204}
205
206
207void audioflinger_device_start (AudioFlingerDeviceHandle handle)
208{
209 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
210 return;
211
212 LOGD("handle : %p Start Device\n", handle);
213
214 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
215 AUDIO_FLINGER_DEVICE_SINK(handle)->start();
216 }
217 else {
218 AUDIO_FLINGER_DEVICE_TRACK(handle)->start();
219 }
220}
221
222void audioflinger_device_stop (AudioFlingerDeviceHandle handle)
223{
224 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
225 return;
226
227 LOGD("handle : %p Stop Device\n", handle);
228
229 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
230 AUDIO_FLINGER_DEVICE_SINK(handle)->stop();
231 }
232 else {
233 AUDIO_FLINGER_DEVICE_TRACK(handle)->stop();
234 }
235
236}
237
238void audioflinger_device_flush (AudioFlingerDeviceHandle handle)
239{
240 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
241 return;
242
243 LOGD("handle : %p Flush device\n", handle);
244
245 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
246 AUDIO_FLINGER_DEVICE_SINK(handle)->flush();
247 }
248 else {
249 AUDIO_FLINGER_DEVICE_TRACK(handle)->flush();
250 }
251}
252
253void audioflinger_device_pause (AudioFlingerDeviceHandle handle)
254{
255 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
256 return;
257
258 LOGD("handle : %p Pause Device\n", handle);
259
260
261 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
262 AUDIO_FLINGER_DEVICE_SINK(handle)->pause();
263 }
264 else {
265 AUDIO_FLINGER_DEVICE_TRACK(handle)->pause();
266 }
267
268}
269
270void audioflinger_device_mute (AudioFlingerDeviceHandle handle, int mute)
271{
272 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
273 return;
274
275 LOGD("handle : %p Mute Device\n", handle);
276
277 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
278 // do nothing here, because the volume/mute is set in media service layer
279 }
280 else if (AUDIO_FLINGER_DEVICE_TRACK(handle)) {
281 AUDIO_FLINGER_DEVICE_TRACK(handle)->mute((bool)mute);
282 }
283}
284
285int audioflinger_device_muted (AudioFlingerDeviceHandle handle)
286{
287 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
288 return -1;
289
290 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
291 // do nothing here, because the volume/mute is set in media service layer
292 return -1;
293 }
294 else if (AUDIO_FLINGER_DEVICE_TRACK(handle)) {
295 return (int) AUDIO_FLINGER_DEVICE_TRACK(handle)->muted ();
296 }
297 return -1;
298}
299
300
301void audioflinger_device_set_volume (AudioFlingerDeviceHandle handle, float left,
302 float right)
303{
304 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
305 return;
306
307 LOGD("handle : %p Set volume Device %f,%f\n", handle,left,right);
308
309 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
310 // do nothing here, because the volume/mute is set in media service layer
311 return ;
312 }
313 else if (AUDIO_FLINGER_DEVICE_TRACK(handle)) {
314 AUDIO_FLINGER_DEVICE_TRACK(handle)->setVolume (left, right);
315 }
316}
317
318ssize_t audioflinger_device_write (AudioFlingerDeviceHandle handle, const void *buffer,
319 size_t size)
320{
321 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
322 return -1;
323
324 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
325 return AUDIO_FLINGER_DEVICE_SINK(handle)->write(buffer, size);
326 }
327 else if (AUDIO_FLINGER_DEVICE_TRACK(handle)) {
328 return AUDIO_FLINGER_DEVICE_TRACK(handle)->write(buffer, size);
329 }
330#ifndef STECONF_ANDROID_VERSION_DONUT
331 return -1;
332#endif
333}
334
335int audioflinger_device_frameCount (AudioFlingerDeviceHandle handle)
336{
337 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
338 return -1;
339
340 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
341 return (int)AUDIO_FLINGER_DEVICE_SINK(handle)->frameCount();
342 }
343 else if (AUDIO_FLINGER_DEVICE_TRACK(handle)) {
344 return (int)AUDIO_FLINGER_DEVICE_TRACK(handle)->frameCount();
345 }
346 return -1;
347}
348
349int audioflinger_device_frameSize (AudioFlingerDeviceHandle handle)
350{
351 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
352 return -1;
353
354 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
355 return (int)AUDIO_FLINGER_DEVICE_SINK(handle)->frameSize();
356 }
357 else if (AUDIO_FLINGER_DEVICE_TRACK(handle)) {
358 return (int)AUDIO_FLINGER_DEVICE_TRACK(handle)->frameSize();
359 }
360#ifndef STECONF_ANDROID_VERSION_DONUT
361 return -1;
362#endif
363}
364
365int64_t audioflinger_device_latency (AudioFlingerDeviceHandle handle)
366{
367 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
368 return -1;
369
370 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
371 return (int64_t)AUDIO_FLINGER_DEVICE_SINK(handle)->latency();
372 }
373 else if (AUDIO_FLINGER_DEVICE_TRACK(handle)) {
374 return (int64_t)AUDIO_FLINGER_DEVICE_TRACK(handle)->latency();
375 }
376 return -1;
377}
378
379int audioflinger_device_format (AudioFlingerDeviceHandle handle)
380{
381 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
382 return -1;
383
384 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
385 // do nothing here, MediaPlayerBase::AudioSink doesn't provide format()
386 // interface
387 return -1;
388 }
389 else if (AUDIO_FLINGER_DEVICE_TRACK(handle)) {
390 return (int)AUDIO_FLINGER_DEVICE_TRACK(handle)->format();
391 }
392 return -1;
393}
394
395int audioflinger_device_channelCount (AudioFlingerDeviceHandle handle)
396{
397 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
398 return -1;
399 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
400 return (int)AUDIO_FLINGER_DEVICE_SINK(handle)->channelCount();
401 }
402 else if (AUDIO_FLINGER_DEVICE_TRACK(handle)) {
403 return (int)AUDIO_FLINGER_DEVICE_TRACK(handle)->channelCount();
404 }
405 return -1;
406}
407
408uint32_t audioflinger_device_sampleRate (AudioFlingerDeviceHandle handle)
409{
410 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
411 return 0;
412 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
413 // do nothing here, MediaPlayerBase::AudioSink doesn't provide sampleRate()
414 // interface
415 return -1;
416 }
417 else if (AUDIO_FLINGER_DEVICE_TRACK(handle)) {
418 return (int)AUDIO_FLINGER_DEVICE_TRACK(handle)->getSampleRate();
419}
420 return(-1);
421}
422
423int audioflinger_device_obtain_buffer (AudioFlingerDeviceHandle handle,
424 void **buffer_handle, int8_t **data, size_t *samples, uint64_t offset)
425{
426 AudioTrack *track = AUDIO_FLINGER_DEVICE_TRACK (handle);
427 status_t res;
428 AudioTrack::Buffer *audioBuffer;
429
430 if(track == 0) return(-1);
431 audioBuffer = new AudioTrack::Buffer();
432 audioBuffer->frameCount = *samples;
433 res = track->obtainBufferAtOffset (audioBuffer, offset, -1);
434 if (res < 0) {
435 delete audioBuffer;
436
437 return (int) res;
438 }
439
440 *samples = audioBuffer->frameCount;
441 *buffer_handle = static_cast<void *> (audioBuffer);
442 *data = audioBuffer->i8;
443
444 return res;
445}
446
447void audioflinger_device_release_buffer (AudioFlingerDeviceHandle handle,
448 void *buffer_handle)
449{
450 AudioTrack *track = AUDIO_FLINGER_DEVICE_TRACK (handle);
451 AudioTrack::Buffer *audioBuffer = static_cast<AudioTrack::Buffer *>(buffer_handle);
452
453 if(track == 0) return;
454
455 track->releaseBuffer (audioBuffer);
456 delete audioBuffer;
457}
458
459uint32_t audioflinger_device_get_position (AudioFlingerDeviceHandle handle)
460{
461 status_t status;
462 uint32_t ret = -1;
463 AudioTrack *track = AUDIO_FLINGER_DEVICE_TRACK (handle);
464
465 if(track == 0) return(-1);
466
467 status = track->getPosition (&ret);
468
469 return ret;
470}