2 * Copyright (C) <2009> Prajnashi S <prajnashi@gmail.com>
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.
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.
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.
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>
33 #define LOG_TAG "audioflinger_wrapper"
36 using namespace android;
39 typedef struct _AudioFlingerDevice
41 AudioTrack* audio_track;
43 sp<MediaPlayerBase::AudioSink> audio_sink;
44 bool audio_sink_specified;
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)
56 AudioFlingerDeviceHandle audioflinger_device_create()
58 AudioFlingerDevice* audiodev = NULL;
59 AudioTrack *audiotr = NULL;
61 // create a new instance of AudioFlinger
62 audiodev = new AudioFlingerDevice;
63 if (audiodev == NULL) {
64 LOGE("Error to create AudioFlingerDevice\n");
69 audiotr = new AudioTrack ();
70 if (audiotr == NULL) {
71 LOGE("Error to create AudioTrack\n");
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);
81 return (AudioFlingerDeviceHandle)audiodev;
84 AudioFlingerDeviceHandle audioflinger_device_open(void* audio_sink)
86 AudioFlingerDevice* audiodev = NULL;
88 // audio_sink shall be an MediaPlayerBase::AudioSink instance
89 if(audio_sink == NULL)
92 // create a new instance of AudioFlinger
93 audiodev = new AudioFlingerDevice;
94 if (audiodev == NULL) {
95 LOGE("Error to create AudioFlingerDevice\n");
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);
107 return (AudioFlingerDeviceHandle)audiodev;
110 int audioflinger_device_set (AudioFlingerDeviceHandle handle,
111 int streamType, int channelCount, uint32_t sampleRate, int bufferCount)
113 status_t status = NO_ERROR;
114 #ifndef STECONF_ANDROID_VERSION_DONUT
115 uint32_t channels = 0;
118 int format = AudioSystem::PCM_16_BIT;
123 if(AUDIO_FLINGER_DEVICE_TRACK(handle)) {
124 // bufferCount is not the number of internal buffer, but the internal
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);
133 switch (channelCount)
136 channels = AudioSystem::CHANNEL_OUT_FRONT_LEFT;
139 channels = AudioSystem::CHANNEL_OUT_STEREO;
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);
152 AUDIO_FLINGER_DEVICE_TRACK(handle)->setPositionUpdatePeriod(bufferCount);
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
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);
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);
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);
177 if (status != NO_ERROR)
180 AUDIO_FLINGER_DEVICE(handle)->init = true;
185 void audioflinger_device_release (AudioFlingerDeviceHandle handle)
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);
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;
203 delete AUDIO_FLINGER_DEVICE(handle);
207 void audioflinger_device_start (AudioFlingerDeviceHandle handle)
209 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
212 LOGD("handle : %p Start Device\n", handle);
214 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
215 AUDIO_FLINGER_DEVICE_SINK(handle)->start();
218 AUDIO_FLINGER_DEVICE_TRACK(handle)->start();
222 void audioflinger_device_stop (AudioFlingerDeviceHandle handle)
224 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
227 LOGD("handle : %p Stop Device\n", handle);
229 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
230 AUDIO_FLINGER_DEVICE_SINK(handle)->stop();
233 AUDIO_FLINGER_DEVICE_TRACK(handle)->stop();
238 void audioflinger_device_flush (AudioFlingerDeviceHandle handle)
240 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
243 LOGD("handle : %p Flush device\n", handle);
245 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
246 AUDIO_FLINGER_DEVICE_SINK(handle)->flush();
249 AUDIO_FLINGER_DEVICE_TRACK(handle)->flush();
253 void audioflinger_device_pause (AudioFlingerDeviceHandle handle)
255 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
258 LOGD("handle : %p Pause Device\n", handle);
261 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
262 AUDIO_FLINGER_DEVICE_SINK(handle)->pause();
265 AUDIO_FLINGER_DEVICE_TRACK(handle)->pause();
270 void audioflinger_device_mute (AudioFlingerDeviceHandle handle, int mute)
272 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
275 LOGD("handle : %p Mute Device\n", handle);
277 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
278 // do nothing here, because the volume/mute is set in media service layer
280 else if (AUDIO_FLINGER_DEVICE_TRACK(handle)) {
281 AUDIO_FLINGER_DEVICE_TRACK(handle)->mute((bool)mute);
285 int audioflinger_device_muted (AudioFlingerDeviceHandle handle)
287 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
290 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
291 // do nothing here, because the volume/mute is set in media service layer
294 else if (AUDIO_FLINGER_DEVICE_TRACK(handle)) {
295 return (int) AUDIO_FLINGER_DEVICE_TRACK(handle)->muted ();
301 void audioflinger_device_set_volume (AudioFlingerDeviceHandle handle, float left,
304 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
307 LOGD("handle : %p Set volume Device %f,%f\n", handle,left,right);
309 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
310 // do nothing here, because the volume/mute is set in media service layer
313 else if (AUDIO_FLINGER_DEVICE_TRACK(handle)) {
314 AUDIO_FLINGER_DEVICE_TRACK(handle)->setVolume (left, right);
318 ssize_t audioflinger_device_write (AudioFlingerDeviceHandle handle, const void *buffer,
321 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
324 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
325 return AUDIO_FLINGER_DEVICE_SINK(handle)->write(buffer, size);
327 else if (AUDIO_FLINGER_DEVICE_TRACK(handle)) {
328 return AUDIO_FLINGER_DEVICE_TRACK(handle)->write(buffer, size);
330 #ifndef STECONF_ANDROID_VERSION_DONUT
335 int audioflinger_device_frameCount (AudioFlingerDeviceHandle handle)
337 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
340 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
341 return (int)AUDIO_FLINGER_DEVICE_SINK(handle)->frameCount();
343 else if (AUDIO_FLINGER_DEVICE_TRACK(handle)) {
344 return (int)AUDIO_FLINGER_DEVICE_TRACK(handle)->frameCount();
349 int audioflinger_device_frameSize (AudioFlingerDeviceHandle handle)
351 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
354 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
355 return (int)AUDIO_FLINGER_DEVICE_SINK(handle)->frameSize();
357 else if (AUDIO_FLINGER_DEVICE_TRACK(handle)) {
358 return (int)AUDIO_FLINGER_DEVICE_TRACK(handle)->frameSize();
360 #ifndef STECONF_ANDROID_VERSION_DONUT
365 int64_t audioflinger_device_latency (AudioFlingerDeviceHandle handle)
367 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
370 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
371 return (int64_t)AUDIO_FLINGER_DEVICE_SINK(handle)->latency();
373 else if (AUDIO_FLINGER_DEVICE_TRACK(handle)) {
374 return (int64_t)AUDIO_FLINGER_DEVICE_TRACK(handle)->latency();
379 int audioflinger_device_format (AudioFlingerDeviceHandle handle)
381 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
384 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
385 // do nothing here, MediaPlayerBase::AudioSink doesn't provide format()
389 else if (AUDIO_FLINGER_DEVICE_TRACK(handle)) {
390 return (int)AUDIO_FLINGER_DEVICE_TRACK(handle)->format();
395 int audioflinger_device_channelCount (AudioFlingerDeviceHandle handle)
397 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
399 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
400 return (int)AUDIO_FLINGER_DEVICE_SINK(handle)->channelCount();
402 else if (AUDIO_FLINGER_DEVICE_TRACK(handle)) {
403 return (int)AUDIO_FLINGER_DEVICE_TRACK(handle)->channelCount();
408 uint32_t audioflinger_device_sampleRate (AudioFlingerDeviceHandle handle)
410 if (handle == NULL || AUDIO_FLINGER_DEVICE(handle)->init == false)
412 if(AUDIO_FLINGER_DEVICE(handle)->audio_sink_specified) {
413 // do nothing here, MediaPlayerBase::AudioSink doesn't provide sampleRate()
417 else if (AUDIO_FLINGER_DEVICE_TRACK(handle)) {
418 return (int)AUDIO_FLINGER_DEVICE_TRACK(handle)->getSampleRate();
423 int audioflinger_device_obtain_buffer (AudioFlingerDeviceHandle handle,
424 void **buffer_handle, int8_t **data, size_t *samples, uint64_t offset)
426 AudioTrack *track = AUDIO_FLINGER_DEVICE_TRACK (handle);
428 AudioTrack::Buffer *audioBuffer;
430 if(track == 0) return(-1);
431 audioBuffer = new AudioTrack::Buffer();
432 audioBuffer->frameCount = *samples;
433 res = track->obtainBufferAtOffset (audioBuffer, offset, -1);
440 *samples = audioBuffer->frameCount;
441 *buffer_handle = static_cast<void *> (audioBuffer);
442 *data = audioBuffer->i8;
447 void audioflinger_device_release_buffer (AudioFlingerDeviceHandle handle,
450 AudioTrack *track = AUDIO_FLINGER_DEVICE_TRACK (handle);
451 AudioTrack::Buffer *audioBuffer = static_cast<AudioTrack::Buffer *>(buffer_handle);
453 if(track == 0) return;
455 track->releaseBuffer (audioBuffer);
459 uint32_t audioflinger_device_get_position (AudioFlingerDeviceHandle handle)
463 AudioTrack *track = AUDIO_FLINGER_DEVICE_TRACK (handle);
465 if(track == 0) return(-1);
467 status = track->getPosition (&ret);