2012-09-20 09:50:50 +00:00
|
|
|
/* GStreamer
|
2012-09-19 16:11:54 +00:00
|
|
|
* Copyright (C) 2012 Fluendo S.A. <support@fluendo.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-03 20:38:00 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2012-09-19 16:11:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2013-03-21 14:43:12 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2012-11-01 14:35:17 +00:00
|
|
|
#include "opensles.h"
|
2012-09-19 16:11:54 +00:00
|
|
|
#include "openslesringbuffer.h"
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (opensles_ringbuffer_debug);
|
|
|
|
#define GST_CAT_DEFAULT opensles_ringbuffer_debug
|
|
|
|
|
2012-10-18 14:23:49 +00:00
|
|
|
#define _do_init \
|
|
|
|
GST_DEBUG_CATEGORY_INIT (opensles_ringbuffer_debug, \
|
2012-09-19 16:11:54 +00:00
|
|
|
"opensles_ringbuffer", 0, "OpenSL ES ringbuffer");
|
|
|
|
|
2012-10-18 14:23:49 +00:00
|
|
|
#define parent_class gst_opensles_ringbuffer_parent_class
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstOpenSLESRingBuffer, gst_opensles_ringbuffer,
|
|
|
|
GST_TYPE_AUDIO_RING_BUFFER, _do_init);
|
2012-09-19 16:11:54 +00:00
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/*
|
|
|
|
* Some generic helper functions
|
|
|
|
*/
|
2012-09-19 16:11:54 +00:00
|
|
|
|
|
|
|
static inline SLuint32
|
|
|
|
_opensles_sample_rate (guint rate)
|
|
|
|
{
|
|
|
|
switch (rate) {
|
|
|
|
case 8000:
|
|
|
|
return SL_SAMPLINGRATE_8;
|
|
|
|
case 11025:
|
|
|
|
return SL_SAMPLINGRATE_11_025;
|
|
|
|
case 12000:
|
|
|
|
return SL_SAMPLINGRATE_12;
|
|
|
|
case 16000:
|
|
|
|
return SL_SAMPLINGRATE_16;
|
|
|
|
case 22050:
|
|
|
|
return SL_SAMPLINGRATE_22_05;
|
|
|
|
case 24000:
|
|
|
|
return SL_SAMPLINGRATE_24;
|
|
|
|
case 32000:
|
|
|
|
return SL_SAMPLINGRATE_32;
|
|
|
|
case 44100:
|
|
|
|
return SL_SAMPLINGRATE_44_1;
|
|
|
|
case 48000:
|
|
|
|
return SL_SAMPLINGRATE_48;
|
|
|
|
case 64000:
|
|
|
|
return SL_SAMPLINGRATE_64;
|
|
|
|
case 88200:
|
|
|
|
return SL_SAMPLINGRATE_88_2;
|
|
|
|
case 96000:
|
|
|
|
return SL_SAMPLINGRATE_96;
|
|
|
|
case 192000:
|
|
|
|
return SL_SAMPLINGRATE_192;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline SLuint32
|
2012-10-18 14:23:49 +00:00
|
|
|
_opensles_channel_mask (GstAudioRingBufferSpec * spec)
|
2012-09-19 16:11:54 +00:00
|
|
|
{
|
2012-10-18 14:23:49 +00:00
|
|
|
switch (spec->info.channels) {
|
2012-09-19 16:11:54 +00:00
|
|
|
case 1:
|
|
|
|
return (SL_SPEAKER_FRONT_CENTER);
|
|
|
|
case 2:
|
|
|
|
return (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT);
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2012-10-18 14:23:49 +00:00
|
|
|
_opensles_format (GstAudioRingBufferSpec * spec, SLDataFormat_PCM * format)
|
2012-09-19 16:11:54 +00:00
|
|
|
{
|
|
|
|
format->formatType = SL_DATAFORMAT_PCM;
|
2012-10-18 14:23:49 +00:00
|
|
|
format->numChannels = spec->info.channels;
|
|
|
|
format->samplesPerSec = _opensles_sample_rate (spec->info.rate);
|
|
|
|
format->bitsPerSample = spec->info.finfo->depth;
|
|
|
|
format->containerSize = spec->info.finfo->width;
|
2012-09-19 16:11:54 +00:00
|
|
|
format->channelMask = _opensles_channel_mask (spec);
|
|
|
|
format->endianness =
|
2012-10-18 14:23:49 +00:00
|
|
|
((spec->info.finfo->endianness ==
|
|
|
|
G_BIG_ENDIAN) ? SL_BYTEORDER_BIGENDIAN : SL_BYTEORDER_LITTLEENDIAN);
|
2012-09-19 16:11:54 +00:00
|
|
|
}
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/*
|
|
|
|
* Recorder related functions
|
|
|
|
*/
|
2012-09-25 16:04:27 +00:00
|
|
|
|
2012-09-19 16:11:54 +00:00
|
|
|
static gboolean
|
2012-10-18 14:23:49 +00:00
|
|
|
_opensles_recorder_acquire (GstAudioRingBuffer * rb,
|
|
|
|
GstAudioRingBufferSpec * spec)
|
2012-09-19 16:11:54 +00:00
|
|
|
{
|
|
|
|
GstOpenSLESRingBuffer *thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
SLresult result;
|
2012-09-20 12:16:25 +00:00
|
|
|
SLDataFormat_PCM format;
|
2012-09-19 16:11:54 +00:00
|
|
|
|
|
|
|
/* Configure audio source */
|
2012-09-21 14:11:42 +00:00
|
|
|
SLDataLocator_IODevice loc_dev = {
|
|
|
|
SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT,
|
2012-09-19 16:11:54 +00:00
|
|
|
SL_DEFAULTDEVICEID_AUDIOINPUT, NULL
|
|
|
|
};
|
|
|
|
SLDataSource audioSrc = { &loc_dev, NULL };
|
|
|
|
|
|
|
|
/* Configure audio sink */
|
2012-09-21 14:11:42 +00:00
|
|
|
SLDataLocator_AndroidSimpleBufferQueue loc_bq = {
|
2012-10-01 08:59:08 +00:00
|
|
|
SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2
|
2012-09-21 14:11:42 +00:00
|
|
|
};
|
2012-09-20 12:16:25 +00:00
|
|
|
SLDataSink audioSink = { &loc_bq, &format };
|
2012-09-19 16:11:54 +00:00
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Required optional interfaces */
|
2012-09-19 16:11:54 +00:00
|
|
|
const SLInterfaceID id[1] = { SL_IID_ANDROIDSIMPLEBUFFERQUEUE };
|
|
|
|
const SLboolean req[1] = { SL_BOOLEAN_TRUE };
|
2012-09-20 12:16:25 +00:00
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Define the audio format in OpenSL ES terminology */
|
2012-09-20 12:16:25 +00:00
|
|
|
_opensles_format (spec, &format);
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Create the audio recorder object (requires the RECORD_AUDIO permission) */
|
2012-09-19 16:11:54 +00:00
|
|
|
result = (*thiz->engineEngine)->CreateAudioRecorder (thiz->engineEngine,
|
|
|
|
&thiz->recorderObject, &audioSrc, &audioSink, 1, id, req);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "engine.CreateAudioRecorder failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Realize the audio recorder object */
|
2012-09-19 16:11:54 +00:00
|
|
|
result =
|
|
|
|
(*thiz->recorderObject)->Realize (thiz->recorderObject, SL_BOOLEAN_FALSE);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "recorder.Realize failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the record interface */
|
|
|
|
result = (*thiz->recorderObject)->GetInterface (thiz->recorderObject,
|
|
|
|
SL_IID_RECORD, &thiz->recorderRecord);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "recorder.GetInterface(Record) failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the buffer queue interface */
|
|
|
|
result =
|
|
|
|
(*thiz->recorderObject)->GetInterface (thiz->recorderObject,
|
|
|
|
SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &thiz->bufferQueue);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "recorder.GetInterface(BufferQueue) failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* This callback function is executed when the ringbuffer is started to preroll
|
|
|
|
* the output buffer queue with empty buffers, from app thread, and each time
|
|
|
|
* there's a filled buffer, from audio device processing thread,
|
|
|
|
* the callback behaviour.
|
|
|
|
*/
|
2012-09-27 02:00:28 +00:00
|
|
|
static void
|
|
|
|
_opensles_recorder_cb (SLAndroidSimpleBufferQueueItf bufferQueue, void *context)
|
|
|
|
{
|
2012-10-18 14:23:49 +00:00
|
|
|
GstAudioRingBuffer *rb = GST_AUDIO_RING_BUFFER_CAST (context);
|
2012-09-27 02:00:28 +00:00
|
|
|
GstOpenSLESRingBuffer *thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
SLresult result;
|
|
|
|
guint8 *ptr;
|
|
|
|
gint seg;
|
|
|
|
gint len;
|
|
|
|
|
2012-10-01 08:59:08 +00:00
|
|
|
/* Advance only when we are called by the callback function */
|
|
|
|
if (bufferQueue) {
|
2012-10-18 14:23:49 +00:00
|
|
|
gst_audio_ring_buffer_advance (rb, 1);
|
2012-10-01 08:59:08 +00:00
|
|
|
}
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Get a segment form the GStreamer ringbuffer to write in */
|
2012-10-18 14:23:49 +00:00
|
|
|
if (!gst_audio_ring_buffer_prepare_read (rb, &seg, &ptr, &len)) {
|
2012-09-27 02:00:28 +00:00
|
|
|
GST_WARNING_OBJECT (rb, "No segment available");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (thiz, "enqueue: %p size %d segment: %d", ptr, len, seg);
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Enqueue the sefment as buffer to be written */
|
|
|
|
result = (*thiz->bufferQueue)->Enqueue (thiz->bufferQueue, ptr, len);
|
2012-09-27 02:00:28 +00:00
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "bufferQueue.Enqueue failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 16:11:54 +00:00
|
|
|
static gboolean
|
2012-10-18 14:23:49 +00:00
|
|
|
_opensles_recorder_start (GstAudioRingBuffer * rb)
|
2012-09-19 16:11:54 +00:00
|
|
|
{
|
|
|
|
GstOpenSLESRingBuffer *thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
SLresult result;
|
|
|
|
|
2012-09-26 10:32:44 +00:00
|
|
|
/* Register callback on the buffer queue */
|
2012-09-27 16:33:09 +00:00
|
|
|
if (!thiz->is_queue_callback_registered) {
|
|
|
|
result = (*thiz->bufferQueue)->RegisterCallback (thiz->bufferQueue,
|
|
|
|
_opensles_recorder_cb, rb);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "bufferQueue.RegisterCallback failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
thiz->is_queue_callback_registered = TRUE;
|
2012-09-19 16:11:54 +00:00
|
|
|
}
|
|
|
|
|
2012-10-01 08:59:08 +00:00
|
|
|
/* Preroll one buffer */
|
|
|
|
_opensles_recorder_cb (NULL, rb);
|
2012-09-19 16:11:54 +00:00
|
|
|
|
2012-09-26 10:32:44 +00:00
|
|
|
/* Start recording */
|
2012-09-19 16:11:54 +00:00
|
|
|
result =
|
|
|
|
(*thiz->recorderRecord)->SetRecordState (thiz->recorderRecord,
|
|
|
|
SL_RECORDSTATE_RECORDING);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "recorder.SetRecordState failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-09-29 17:00:13 +00:00
|
|
|
|
2012-09-19 16:11:54 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-10-18 14:23:49 +00:00
|
|
|
_opensles_recorder_stop (GstAudioRingBuffer * rb)
|
2012-09-19 16:11:54 +00:00
|
|
|
{
|
|
|
|
GstOpenSLESRingBuffer *thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
SLresult result;
|
|
|
|
|
2012-09-26 10:32:44 +00:00
|
|
|
/* Stop recording */
|
2012-09-19 16:11:54 +00:00
|
|
|
result =
|
|
|
|
(*thiz->recorderRecord)->SetRecordState (thiz->recorderRecord,
|
|
|
|
SL_RECORDSTATE_STOPPED);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "recorder.SetRecordState failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-09-26 10:32:44 +00:00
|
|
|
|
|
|
|
/* Unregister callback on the buffer queue */
|
|
|
|
result = (*thiz->bufferQueue)->RegisterCallback (thiz->bufferQueue,
|
|
|
|
NULL, NULL);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "bufferQueue.RegisterCallback failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-09-27 16:33:09 +00:00
|
|
|
thiz->is_queue_callback_registered = FALSE;
|
2012-09-26 10:32:44 +00:00
|
|
|
|
|
|
|
/* Reset the queue */
|
|
|
|
result = (*thiz->bufferQueue)->Clear (thiz->bufferQueue);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "bufferQueue.Clear failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-09-19 16:11:54 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/*
|
|
|
|
* Player related functions
|
|
|
|
*/
|
2012-09-19 16:11:54 +00:00
|
|
|
|
2012-09-20 16:04:29 +00:00
|
|
|
static gboolean
|
2012-10-18 14:23:49 +00:00
|
|
|
_opensles_player_change_volume (GstAudioRingBuffer * rb)
|
2012-09-20 16:04:29 +00:00
|
|
|
{
|
|
|
|
GstOpenSLESRingBuffer *thiz;
|
|
|
|
SLresult result;
|
|
|
|
|
|
|
|
thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
|
|
|
|
if (thiz->playerVolume) {
|
|
|
|
gint millibel = (1.0 - thiz->volume) * -5000.0;
|
|
|
|
result =
|
|
|
|
(*thiz->playerVolume)->SetVolumeLevel (thiz->playerVolume, millibel);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "player.SetVolumeLevel failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
GST_DEBUG_OBJECT (thiz, "changed volume to %d", millibel);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-09-20 16:41:50 +00:00
|
|
|
static gboolean
|
2012-10-18 14:23:49 +00:00
|
|
|
_opensles_player_change_mute (GstAudioRingBuffer * rb)
|
2012-09-20 16:41:50 +00:00
|
|
|
{
|
|
|
|
GstOpenSLESRingBuffer *thiz;
|
|
|
|
SLresult result;
|
|
|
|
|
|
|
|
thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
|
|
|
|
if (thiz->playerVolume) {
|
|
|
|
result = (*thiz->playerVolume)->SetMute (thiz->playerVolume, thiz->mute);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "player.SetMute failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
GST_DEBUG_OBJECT (thiz, "changed mute to %d", thiz->mute);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* This is a callback function invoked by the playback device thread and
|
|
|
|
* it's used to monitor position changes */
|
2012-09-27 12:52:59 +00:00
|
|
|
static void
|
|
|
|
_opensles_player_event_cb (SLPlayItf caller, void *context, SLuint32 event)
|
|
|
|
{
|
|
|
|
if (event & SL_PLAYEVENT_HEADATNEWPOS) {
|
|
|
|
SLmillisecond position;
|
|
|
|
|
|
|
|
(*caller)->GetPosition (caller, &position);
|
2012-11-02 10:24:37 +00:00
|
|
|
GST_LOG_OBJECT (context, "at position=%u ms", (guint) position);
|
2012-09-27 12:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 16:11:54 +00:00
|
|
|
static gboolean
|
2012-10-18 14:23:49 +00:00
|
|
|
_opensles_player_acquire (GstAudioRingBuffer * rb,
|
|
|
|
GstAudioRingBufferSpec * spec)
|
2012-09-19 16:11:54 +00:00
|
|
|
{
|
|
|
|
GstOpenSLESRingBuffer *thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
SLresult result;
|
2012-09-20 12:16:25 +00:00
|
|
|
SLDataFormat_PCM format;
|
2012-09-19 16:11:54 +00:00
|
|
|
|
|
|
|
/* Configure audio source */
|
2012-09-21 14:11:42 +00:00
|
|
|
SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {
|
2012-09-27 15:46:27 +00:00
|
|
|
SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
|
|
|
|
MIN (32, (spec->segtotal >> 1))
|
2012-09-21 14:11:42 +00:00
|
|
|
};
|
2012-09-20 12:16:25 +00:00
|
|
|
SLDataSource audioSrc = { &loc_bufq, &format };
|
2012-09-19 16:11:54 +00:00
|
|
|
|
|
|
|
/* Configure audio sink */
|
2012-09-21 14:11:42 +00:00
|
|
|
SLDataLocator_OutputMix loc_outmix = {
|
|
|
|
SL_DATALOCATOR_OUTPUTMIX, thiz->outputMixObject
|
|
|
|
};
|
2012-09-19 16:11:54 +00:00
|
|
|
SLDataSink audioSink = { &loc_outmix, NULL };
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Define the required interfaces */
|
2012-09-19 16:11:54 +00:00
|
|
|
const SLInterfaceID ids[2] = { SL_IID_BUFFERQUEUE, SL_IID_VOLUME };
|
|
|
|
const SLboolean req[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Define the format in OpenSL ES terminology */
|
2012-09-20 12:16:25 +00:00
|
|
|
_opensles_format (spec, &format);
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Create the player object */
|
2012-09-19 16:11:54 +00:00
|
|
|
result = (*thiz->engineEngine)->CreateAudioPlayer (thiz->engineEngine,
|
|
|
|
&thiz->playerObject, &audioSrc, &audioSink, 2, ids, req);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "engine.CreateAudioPlayer failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Realize the player object */
|
2012-09-19 16:11:54 +00:00
|
|
|
result =
|
|
|
|
(*thiz->playerObject)->Realize (thiz->playerObject, SL_BOOLEAN_FALSE);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "player.Realize failed(0x%08x)", (guint32) result);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the play interface */
|
|
|
|
result = (*thiz->playerObject)->GetInterface (thiz->playerObject,
|
|
|
|
SL_IID_PLAY, &thiz->playerPlay);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "player.GetInterface(Play) failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the buffer queue interface */
|
|
|
|
result = (*thiz->playerObject)->GetInterface (thiz->playerObject,
|
|
|
|
SL_IID_BUFFERQUEUE, &thiz->bufferQueue);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "player.GetInterface(BufferQueue) failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the volume interface */
|
|
|
|
result = (*thiz->playerObject)->GetInterface (thiz->playerObject,
|
|
|
|
SL_IID_VOLUME, &thiz->playerVolume);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "player.GetInterface(Volume) failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Request position update events at each 20 ms */
|
|
|
|
result = (*thiz->playerPlay)->SetPositionUpdatePeriod (thiz->playerPlay, 20);
|
2012-09-27 12:52:59 +00:00
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "player.SetPositionUpdatePeriod failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Define the event mask to be monitorized */
|
|
|
|
result = (*thiz->playerPlay)->SetCallbackEventsMask (thiz->playerPlay,
|
2012-09-29 17:00:13 +00:00
|
|
|
SL_PLAYEVENT_HEADATNEWPOS);
|
2012-09-27 12:52:59 +00:00
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "player.SetCallbackEventsMask failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Register a callback to process the events */
|
|
|
|
result = (*thiz->playerPlay)->RegisterCallback (thiz->playerPlay,
|
|
|
|
_opensles_player_event_cb, thiz);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "player.RegisterCallback(event_cb) failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2012-09-20 16:41:50 +00:00
|
|
|
/* Configure the volume and mute state */
|
2012-09-20 16:04:29 +00:00
|
|
|
_opensles_player_change_volume (rb);
|
2012-09-20 16:41:50 +00:00
|
|
|
_opensles_player_change_mute (rb);
|
2012-09-20 16:04:29 +00:00
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Allocate the queue associated ringbuffer memory */
|
2012-09-27 15:46:27 +00:00
|
|
|
thiz->data_segtotal = loc_bufq.numBuffers;
|
2012-10-01 09:14:24 +00:00
|
|
|
thiz->data_size = spec->segsize * thiz->data_segtotal;
|
|
|
|
thiz->data = g_malloc0 (thiz->data_size);
|
|
|
|
g_atomic_int_set (&thiz->segqueued, 0);
|
2012-10-01 10:54:15 +00:00
|
|
|
g_atomic_int_set (&thiz->is_prerolled, 0);
|
2012-09-27 02:00:28 +00:00
|
|
|
thiz->cursor = 0;
|
2012-09-20 12:16:25 +00:00
|
|
|
|
2012-09-19 16:11:54 +00:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* This callback function is executed when the ringbuffer is started to preroll
|
|
|
|
* the input buffer queue with few buffers, from app thread, and each time
|
|
|
|
* that rendering of one buffer finishes, from audio device processing thread,
|
|
|
|
* the callback behaviour.
|
|
|
|
*
|
|
|
|
* We wrap the queue behaviour with an appropriate chunk of memory (queue len *
|
|
|
|
* ringbuffer segment size) which is used to hold the audio data while it's
|
|
|
|
* being processed in the queue. The memory region is used whit a ringbuffer
|
|
|
|
* behaviour.
|
|
|
|
*/
|
2012-09-27 02:00:28 +00:00
|
|
|
static void
|
|
|
|
_opensles_player_cb (SLAndroidSimpleBufferQueueItf bufferQueue, void *context)
|
|
|
|
{
|
2012-10-18 14:23:49 +00:00
|
|
|
GstAudioRingBuffer *rb = GST_AUDIO_RING_BUFFER_CAST (context);
|
2012-09-27 02:00:28 +00:00
|
|
|
GstOpenSLESRingBuffer *thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
SLresult result;
|
|
|
|
guint8 *ptr, *cur;
|
|
|
|
gint seg;
|
|
|
|
gint len;
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Get a segment form the GStreamer ringbuffer to read some samples */
|
2012-10-18 14:23:49 +00:00
|
|
|
if (!gst_audio_ring_buffer_prepare_read (rb, &seg, &ptr, &len)) {
|
2012-09-27 02:00:28 +00:00
|
|
|
GST_WARNING_OBJECT (rb, "No segment available");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* copy the segment data to our queue associated ringbuffer memory */
|
2012-09-27 02:00:28 +00:00
|
|
|
cur = thiz->data + (thiz->cursor * rb->spec.segsize);
|
|
|
|
memcpy (cur, ptr, len);
|
2012-09-27 05:22:47 +00:00
|
|
|
g_atomic_int_inc (&thiz->segqueued);
|
2012-09-27 02:00:28 +00:00
|
|
|
|
2012-09-27 12:52:59 +00:00
|
|
|
GST_LOG_OBJECT (thiz, "enqueue: %p size %d segment: %d in queue[%d]",
|
|
|
|
cur, len, seg, thiz->cursor);
|
2012-09-29 17:00:13 +00:00
|
|
|
/* advance the cursor in our queue associated ringbuffer */
|
2012-09-27 12:52:59 +00:00
|
|
|
thiz->cursor = (thiz->cursor + 1) % thiz->data_segtotal;
|
2012-09-27 02:00:28 +00:00
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Enqueue the buffer to be rendered */
|
|
|
|
result = (*thiz->bufferQueue)->Enqueue (thiz->bufferQueue, cur, len);
|
2012-09-27 02:00:28 +00:00
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "bufferQueue.Enqueue failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Fill with silence samples the segment of the GStreamer ringbuffer */
|
2012-10-18 14:23:49 +00:00
|
|
|
gst_audio_ring_buffer_clear (rb, seg);
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Make the segment reusable */
|
2012-10-18 14:23:49 +00:00
|
|
|
gst_audio_ring_buffer_advance (rb, 1);
|
2012-09-27 02:00:28 +00:00
|
|
|
}
|
|
|
|
|
2012-09-19 16:11:54 +00:00
|
|
|
static gboolean
|
2012-10-18 14:23:49 +00:00
|
|
|
_opensles_player_start (GstAudioRingBuffer * rb)
|
2012-09-19 16:11:54 +00:00
|
|
|
{
|
|
|
|
GstOpenSLESRingBuffer *thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
SLresult result;
|
2012-09-25 17:25:01 +00:00
|
|
|
gint i;
|
|
|
|
|
2012-09-26 10:32:44 +00:00
|
|
|
/* Register callback on the buffer queue */
|
2012-09-27 16:33:09 +00:00
|
|
|
if (!thiz->is_queue_callback_registered) {
|
|
|
|
result = (*thiz->bufferQueue)->RegisterCallback (thiz->bufferQueue,
|
|
|
|
_opensles_player_cb, rb);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "bufferQueue.RegisterCallback failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
thiz->is_queue_callback_registered = TRUE;
|
2012-09-26 10:32:44 +00:00
|
|
|
}
|
|
|
|
|
2012-09-25 17:25:01 +00:00
|
|
|
/* Fill the queue by enqueing buffers */
|
2012-10-01 10:54:15 +00:00
|
|
|
if (!g_atomic_int_get (&thiz->is_prerolled)) {
|
|
|
|
for (i = 0; i < thiz->data_segtotal; i++) {
|
|
|
|
_opensles_player_cb (NULL, rb);
|
|
|
|
}
|
|
|
|
g_atomic_int_set (&thiz->is_prerolled, 1);
|
2012-09-25 17:25:01 +00:00
|
|
|
}
|
2012-09-19 16:11:54 +00:00
|
|
|
|
2012-09-26 10:32:44 +00:00
|
|
|
/* Change player state into PLAYING */
|
2012-09-19 16:11:54 +00:00
|
|
|
result =
|
|
|
|
(*thiz->playerPlay)->SetPlayState (thiz->playerPlay,
|
|
|
|
SL_PLAYSTATE_PLAYING);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "player.SetPlayState failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-10-18 14:23:49 +00:00
|
|
|
_opensles_player_pause (GstAudioRingBuffer * rb)
|
2012-09-19 16:11:54 +00:00
|
|
|
{
|
|
|
|
GstOpenSLESRingBuffer *thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
SLresult result;
|
|
|
|
|
|
|
|
result =
|
|
|
|
(*thiz->playerPlay)->SetPlayState (thiz->playerPlay, SL_PLAYSTATE_PAUSED);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "player.SetPlayState failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-09-29 17:00:13 +00:00
|
|
|
|
2012-09-19 16:11:54 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-10-18 14:23:49 +00:00
|
|
|
_opensles_player_stop (GstAudioRingBuffer * rb)
|
2012-09-19 16:11:54 +00:00
|
|
|
{
|
|
|
|
GstOpenSLESRingBuffer *thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
SLresult result;
|
|
|
|
|
2012-09-26 10:32:44 +00:00
|
|
|
/* Change player state into STOPPED */
|
2012-09-19 16:11:54 +00:00
|
|
|
result =
|
|
|
|
(*thiz->playerPlay)->SetPlayState (thiz->playerPlay,
|
|
|
|
SL_PLAYSTATE_STOPPED);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "player.SetPlayState failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-09-26 10:32:44 +00:00
|
|
|
|
|
|
|
/* Unregister callback on the buffer queue */
|
|
|
|
result = (*thiz->bufferQueue)->RegisterCallback (thiz->bufferQueue,
|
|
|
|
NULL, NULL);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "bufferQueue.RegisterCallback failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-09-27 16:33:09 +00:00
|
|
|
thiz->is_queue_callback_registered = FALSE;
|
2012-09-26 10:32:44 +00:00
|
|
|
|
|
|
|
/* Reset the queue */
|
|
|
|
result = (*thiz->bufferQueue)->Clear (thiz->bufferQueue);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "bufferQueue.Clear failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Reset our state */
|
2012-09-27 05:22:47 +00:00
|
|
|
g_atomic_int_set (&thiz->segqueued, 0);
|
2012-09-27 16:33:09 +00:00
|
|
|
thiz->cursor = 0;
|
2012-09-27 03:40:46 +00:00
|
|
|
|
2012-09-19 16:11:54 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/*
|
|
|
|
* OpenSL ES ringbuffer wrapper
|
|
|
|
*/
|
|
|
|
|
2012-10-18 14:23:49 +00:00
|
|
|
GstAudioRingBuffer *
|
2012-09-19 16:11:54 +00:00
|
|
|
gst_opensles_ringbuffer_new (RingBufferMode mode)
|
|
|
|
{
|
|
|
|
GstOpenSLESRingBuffer *thiz;
|
|
|
|
|
|
|
|
g_return_val_if_fail (mode > RB_MODE_NONE && mode < RB_MODE_LAST, NULL);
|
|
|
|
|
|
|
|
thiz = g_object_new (GST_TYPE_OPENSLES_RING_BUFFER, NULL);
|
|
|
|
|
|
|
|
if (thiz) {
|
|
|
|
thiz->mode = mode;
|
|
|
|
if (mode == RB_MODE_SRC) {
|
|
|
|
thiz->acquire = _opensles_recorder_acquire;
|
|
|
|
thiz->start = _opensles_recorder_start;
|
|
|
|
thiz->pause = _opensles_recorder_stop;
|
|
|
|
thiz->stop = _opensles_recorder_stop;
|
2012-09-20 16:04:29 +00:00
|
|
|
thiz->change_volume = NULL;
|
2012-09-19 16:11:54 +00:00
|
|
|
} else if (mode == RB_MODE_SINK_PCM) {
|
|
|
|
thiz->acquire = _opensles_player_acquire;
|
|
|
|
thiz->start = _opensles_player_start;
|
|
|
|
thiz->pause = _opensles_player_pause;
|
|
|
|
thiz->stop = _opensles_player_stop;
|
2012-09-20 16:04:29 +00:00
|
|
|
thiz->change_volume = _opensles_player_change_volume;
|
2012-09-19 16:11:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (thiz, "ringbuffer created");
|
2012-09-29 17:00:13 +00:00
|
|
|
|
2012-10-18 14:23:49 +00:00
|
|
|
return GST_AUDIO_RING_BUFFER (thiz);
|
2012-09-19 16:11:54 +00:00
|
|
|
}
|
|
|
|
|
2012-09-20 16:04:29 +00:00
|
|
|
void
|
2012-10-18 14:23:49 +00:00
|
|
|
gst_opensles_ringbuffer_set_volume (GstAudioRingBuffer * rb, gfloat volume)
|
2012-09-20 16:04:29 +00:00
|
|
|
{
|
|
|
|
GstOpenSLESRingBuffer *thiz;
|
|
|
|
|
|
|
|
thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
|
|
|
|
thiz->volume = volume;
|
|
|
|
|
|
|
|
if (thiz->change_volume) {
|
|
|
|
thiz->change_volume (rb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-20 16:41:50 +00:00
|
|
|
void
|
2012-10-18 14:23:49 +00:00
|
|
|
gst_opensles_ringbuffer_set_mute (GstAudioRingBuffer * rb, gboolean mute)
|
2012-09-20 16:41:50 +00:00
|
|
|
{
|
|
|
|
GstOpenSLESRingBuffer *thiz;
|
|
|
|
|
|
|
|
thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
|
|
|
|
thiz->mute = mute;
|
|
|
|
|
|
|
|
if (thiz->change_mute) {
|
|
|
|
thiz->change_mute (rb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 16:11:54 +00:00
|
|
|
static gboolean
|
2012-10-18 14:23:49 +00:00
|
|
|
gst_opensles_ringbuffer_open_device (GstAudioRingBuffer * rb)
|
2012-09-19 16:11:54 +00:00
|
|
|
{
|
|
|
|
GstOpenSLESRingBuffer *thiz;
|
|
|
|
SLresult result;
|
|
|
|
|
|
|
|
thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
|
2012-11-01 14:35:17 +00:00
|
|
|
/* Create and realize the engine object */
|
|
|
|
thiz->engineObject = gst_opensles_get_engine ();
|
|
|
|
if (!thiz->engineObject) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "Failed to get engine object");
|
2012-09-19 16:11:54 +00:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Get the engine interface, which is needed in order to create other objects */
|
2012-09-19 16:11:54 +00:00
|
|
|
result = (*thiz->engineObject)->GetInterface (thiz->engineObject,
|
|
|
|
SL_IID_ENGINE, &thiz->engineEngine);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "engine.GetInterface(Engine) failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thiz->mode == RB_MODE_SINK_PCM) {
|
2012-09-28 13:10:19 +00:00
|
|
|
SLOutputMixItf outputMix;
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Create an output mixer object */
|
2012-09-19 16:11:54 +00:00
|
|
|
result = (*thiz->engineEngine)->CreateOutputMix (thiz->engineEngine,
|
|
|
|
&thiz->outputMixObject, 0, NULL, NULL);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "engine.CreateOutputMix failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Realize the output mixer object */
|
2012-09-19 16:11:54 +00:00
|
|
|
result = (*thiz->outputMixObject)->Realize (thiz->outputMixObject,
|
|
|
|
SL_BOOLEAN_FALSE);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "outputMix.Realize failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
goto failed;
|
|
|
|
}
|
2012-09-28 13:10:19 +00:00
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Get the mixer interface */
|
2012-09-28 13:10:19 +00:00
|
|
|
result = (*thiz->outputMixObject)->GetInterface (thiz->outputMixObject,
|
|
|
|
SL_IID_OUTPUTMIX, &outputMix);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_WARNING_OBJECT (thiz, "outputMix.GetInterface failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
} else {
|
2014-07-16 22:11:48 +00:00
|
|
|
SLint32 numDevices = MAX_NUMBER_OUTPUT_DEVICES;
|
2012-09-29 17:00:13 +00:00
|
|
|
SLuint32 deviceIDs[MAX_NUMBER_OUTPUT_DEVICES];
|
2012-09-28 13:10:19 +00:00
|
|
|
gint i;
|
2012-09-29 17:00:13 +00:00
|
|
|
|
|
|
|
/* Query the list of output devices */
|
2012-09-28 13:10:19 +00:00
|
|
|
(*outputMix)->GetDestinationOutputDeviceIDs (outputMix, &numDevices,
|
|
|
|
deviceIDs);
|
|
|
|
GST_DEBUG_OBJECT (thiz, "Found %d output devices", (gint) numDevices);
|
|
|
|
for (i = 0; i < numDevices; i++) {
|
|
|
|
GST_DEBUG_OBJECT (thiz, " DeviceID: %08x", (guint) deviceIDs[i]);
|
|
|
|
}
|
|
|
|
}
|
2012-09-19 16:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (thiz, "device opened");
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-10-18 14:23:49 +00:00
|
|
|
gst_opensles_ringbuffer_close_device (GstAudioRingBuffer * rb)
|
2012-09-19 16:11:54 +00:00
|
|
|
{
|
|
|
|
GstOpenSLESRingBuffer *thiz;
|
|
|
|
|
|
|
|
thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Destroy the output mix object */
|
2012-09-19 16:11:54 +00:00
|
|
|
if (thiz->outputMixObject) {
|
|
|
|
(*thiz->outputMixObject)->Destroy (thiz->outputMixObject);
|
|
|
|
thiz->outputMixObject = NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Destroy the engine object and invalidate all associated interfaces */
|
2012-09-19 16:11:54 +00:00
|
|
|
if (thiz->engineObject) {
|
2012-11-01 14:35:17 +00:00
|
|
|
gst_opensles_release_engine (thiz->engineObject);
|
2012-09-19 16:11:54 +00:00
|
|
|
thiz->engineObject = NULL;
|
|
|
|
thiz->engineEngine = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
thiz->bufferQueue = NULL;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (thiz, "device closed");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-10-18 14:23:49 +00:00
|
|
|
gst_opensles_ringbuffer_acquire (GstAudioRingBuffer * rb,
|
|
|
|
GstAudioRingBufferSpec * spec)
|
2012-09-19 16:11:54 +00:00
|
|
|
{
|
|
|
|
GstOpenSLESRingBuffer *thiz;
|
|
|
|
|
|
|
|
thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
|
2012-09-20 12:16:25 +00:00
|
|
|
/* Instantiate and configure the OpenSL ES interfaces */
|
|
|
|
if (!thiz->acquire (rb, spec)) {
|
2012-09-19 16:11:54 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize our ringbuffer memory region */
|
2012-10-18 14:23:49 +00:00
|
|
|
rb->size = spec->segtotal * spec->segsize;
|
|
|
|
rb->memory = g_malloc0 (rb->size);
|
2012-09-19 16:11:54 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (thiz, "ringbuffer acquired");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-10-18 14:23:49 +00:00
|
|
|
gst_opensles_ringbuffer_release (GstAudioRingBuffer * rb)
|
2012-09-19 16:11:54 +00:00
|
|
|
{
|
|
|
|
GstOpenSLESRingBuffer *thiz;
|
|
|
|
|
|
|
|
thiz = GST_OPENSLES_RING_BUFFER (rb);
|
|
|
|
|
2014-05-16 07:10:48 +00:00
|
|
|
/* XXX: We need to sleep a bit before destroying the player object
|
|
|
|
* because of a bug in Android in versions < 4.2.
|
|
|
|
*
|
|
|
|
* OpenSLES is using AudioTrack for rendering the sound. AudioTrack
|
|
|
|
* has a thread that pulls raw audio from the buffer queue and then
|
|
|
|
* passes it forward to AudioFlinger (AudioTrack::processAudioBuffer()).
|
|
|
|
* This thread is calling various callbacks on events, e.g. when
|
|
|
|
* an underrun happens or to request data. OpenSLES sets this callback
|
|
|
|
* on AudioTrack (audioTrack_callBack_pullFromBuffQueue() from
|
|
|
|
* android_AudioPlayer.cpp). Among other things this is taking a lock
|
|
|
|
* on the player interface.
|
|
|
|
*
|
|
|
|
* Now if we destroy the player interface object, it will first of all
|
|
|
|
* take the player interface lock (IObject_Destroy()). Then it destroys
|
|
|
|
* the audio player instance (android_audioPlayer_destroy()) which then
|
|
|
|
* calls stop() on the AudioTrack and deletes it. Now the destructor of
|
|
|
|
* AudioTrack will wait until the rendering thread (AudioTrack::processAudioBuffer())
|
|
|
|
* has finished.
|
|
|
|
*
|
|
|
|
* If all this happens with bad timing it can happen that the rendering
|
|
|
|
* thread is currently e.g. handling underrun but did not lock the player
|
|
|
|
* interface object yet. Then destroying happens and takes the lock and waits
|
|
|
|
* for the thread to finish. Then the thread tries to take the lock and waits
|
|
|
|
* forever.
|
|
|
|
*
|
|
|
|
* We wait a bit before destroying the player object to make sure that
|
|
|
|
* the rendering thread finished whatever it was doing, and then stops
|
|
|
|
* (note: we called gst_opensles_ringbuffer_stop() before this already).
|
|
|
|
*/
|
|
|
|
|
2012-09-19 16:11:54 +00:00
|
|
|
/* Destroy audio player object, and invalidate all associated interfaces */
|
|
|
|
if (thiz->playerObject) {
|
2014-05-16 07:10:48 +00:00
|
|
|
g_usleep (50000);
|
2012-09-19 16:11:54 +00:00
|
|
|
(*thiz->playerObject)->Destroy (thiz->playerObject);
|
|
|
|
thiz->playerObject = NULL;
|
|
|
|
thiz->playerPlay = NULL;
|
|
|
|
thiz->playerVolume = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Destroy audio recorder object, and invalidate all associated interfaces */
|
|
|
|
if (thiz->recorderObject) {
|
2014-05-16 07:10:48 +00:00
|
|
|
g_usleep (50000);
|
2012-09-19 16:11:54 +00:00
|
|
|
(*thiz->recorderObject)->Destroy (thiz->recorderObject);
|
|
|
|
thiz->recorderObject = NULL;
|
|
|
|
thiz->recorderRecord = NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-27 02:00:28 +00:00
|
|
|
if (thiz->data) {
|
|
|
|
g_free (thiz->data);
|
|
|
|
thiz->data = NULL;
|
|
|
|
}
|
|
|
|
|
2012-10-18 14:23:49 +00:00
|
|
|
if (rb->memory) {
|
|
|
|
g_free (rb->memory);
|
|
|
|
rb->memory = NULL;
|
|
|
|
rb->size = 0;
|
2012-09-19 16:11:54 +00:00
|
|
|
}
|
2012-09-29 17:00:13 +00:00
|
|
|
|
2012-09-19 16:11:54 +00:00
|
|
|
GST_DEBUG_OBJECT (thiz, "ringbuffer released");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-10-18 14:23:49 +00:00
|
|
|
gst_opensles_ringbuffer_start (GstAudioRingBuffer * rb)
|
2012-09-19 16:11:54 +00:00
|
|
|
{
|
|
|
|
GstOpenSLESRingBuffer *thiz;
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
res = thiz->start (rb);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (thiz, "ringbuffer %s started", (res ? "" : "not"));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-10-18 14:23:49 +00:00
|
|
|
gst_opensles_ringbuffer_pause (GstAudioRingBuffer * rb)
|
2012-09-19 16:11:54 +00:00
|
|
|
{
|
|
|
|
GstOpenSLESRingBuffer *thiz;
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
res = thiz->pause (rb);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (thiz, "ringbuffer %s paused", (res ? "" : "not"));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-10-18 14:23:49 +00:00
|
|
|
gst_opensles_ringbuffer_stop (GstAudioRingBuffer * rb)
|
2012-09-19 16:11:54 +00:00
|
|
|
{
|
|
|
|
GstOpenSLESRingBuffer *thiz;
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
res = thiz->stop (rb);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (thiz, "ringbuffer %s stopped", (res ? " " : "not"));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
2012-10-18 14:23:49 +00:00
|
|
|
gst_opensles_ringbuffer_delay (GstAudioRingBuffer * rb)
|
2012-09-19 16:11:54 +00:00
|
|
|
{
|
2012-09-27 03:40:46 +00:00
|
|
|
GstOpenSLESRingBuffer *thiz;
|
|
|
|
guint res = 0;
|
|
|
|
|
|
|
|
thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
|
|
|
|
if (thiz->playerPlay) {
|
2012-10-12 08:08:59 +00:00
|
|
|
SLuint32 state;
|
2012-09-27 05:22:47 +00:00
|
|
|
SLmillisecond position;
|
2012-10-12 08:08:59 +00:00
|
|
|
guint64 playedpos = 0, queuedpos = 0;
|
|
|
|
(*thiz->playerPlay)->GetPlayState (thiz->playerPlay, &state);
|
|
|
|
if (state == SL_PLAYSTATE_PLAYING) {
|
|
|
|
(*thiz->playerPlay)->GetPosition (thiz->playerPlay, &position);
|
2012-10-18 14:23:49 +00:00
|
|
|
playedpos =
|
|
|
|
gst_util_uint64_scale_round (position, rb->spec.info.rate, 1000);
|
2012-10-12 08:08:59 +00:00
|
|
|
queuedpos = g_atomic_int_get (&thiz->segqueued) * rb->samples_per_seg;
|
|
|
|
res = queuedpos - playedpos;
|
|
|
|
}
|
2012-09-27 03:40:46 +00:00
|
|
|
|
|
|
|
GST_LOG_OBJECT (thiz, "queued samples %" G_GUINT64_FORMAT " position %u ms "
|
|
|
|
"(%" G_GUINT64_FORMAT " samples) delay %u samples",
|
2012-09-27 05:22:47 +00:00
|
|
|
queuedpos, (guint) position, playedpos, res);
|
2012-09-27 03:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2012-09-19 16:11:54 +00:00
|
|
|
}
|
|
|
|
|
2012-10-01 09:14:24 +00:00
|
|
|
static void
|
2012-10-18 14:23:49 +00:00
|
|
|
gst_opensles_ringbuffer_clear_all (GstAudioRingBuffer * rb)
|
2012-10-01 09:14:24 +00:00
|
|
|
{
|
|
|
|
GstOpenSLESRingBuffer *thiz;
|
|
|
|
|
|
|
|
thiz = GST_OPENSLES_RING_BUFFER_CAST (rb);
|
|
|
|
|
|
|
|
if (thiz->data) {
|
2012-10-01 10:54:15 +00:00
|
|
|
SLresult result;
|
|
|
|
|
2012-10-01 09:14:24 +00:00
|
|
|
memset (thiz->data, 0, thiz->data_size);
|
|
|
|
g_atomic_int_set (&thiz->segqueued, 0);
|
|
|
|
thiz->cursor = 0;
|
2012-10-01 10:54:15 +00:00
|
|
|
/* Reset the queue */
|
|
|
|
result = (*thiz->bufferQueue)->Clear (thiz->bufferQueue);
|
|
|
|
if (result != SL_RESULT_SUCCESS) {
|
|
|
|
GST_WARNING_OBJECT (thiz, "bufferQueue.Clear failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
}
|
|
|
|
g_atomic_int_set (&thiz->is_prerolled, 0);
|
2012-10-01 09:14:24 +00:00
|
|
|
}
|
2012-10-01 09:46:50 +00:00
|
|
|
|
2012-10-18 14:23:49 +00:00
|
|
|
GST_CALL_PARENT (GST_AUDIO_RING_BUFFER_CLASS, clear_all, (rb));
|
2012-10-01 09:14:24 +00:00
|
|
|
}
|
|
|
|
|
2012-09-19 16:11:54 +00:00
|
|
|
static void
|
|
|
|
gst_opensles_ringbuffer_dispose (GObject * object)
|
|
|
|
{
|
2012-10-18 14:23:49 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->dispose (object);
|
2012-09-19 16:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_opensles_ringbuffer_finalize (GObject * object)
|
|
|
|
{
|
2012-10-18 14:23:49 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
2012-09-19 16:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_opensles_ringbuffer_class_init (GstOpenSLESRingBufferClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
2012-10-18 14:23:49 +00:00
|
|
|
GstAudioRingBufferClass *gstringbuffer_class;
|
2012-09-19 16:11:54 +00:00
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
2012-10-18 14:23:49 +00:00
|
|
|
gstringbuffer_class = (GstAudioRingBufferClass *) klass;
|
2012-09-19 16:11:54 +00:00
|
|
|
|
|
|
|
gobject_class->dispose = gst_opensles_ringbuffer_dispose;
|
|
|
|
gobject_class->finalize = gst_opensles_ringbuffer_finalize;
|
|
|
|
|
|
|
|
gstringbuffer_class->open_device =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_opensles_ringbuffer_open_device);
|
|
|
|
gstringbuffer_class->close_device =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_opensles_ringbuffer_close_device);
|
|
|
|
gstringbuffer_class->acquire =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_opensles_ringbuffer_acquire);
|
|
|
|
gstringbuffer_class->release =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_opensles_ringbuffer_release);
|
|
|
|
gstringbuffer_class->start =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_opensles_ringbuffer_start);
|
|
|
|
gstringbuffer_class->pause =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_opensles_ringbuffer_pause);
|
|
|
|
gstringbuffer_class->resume =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_opensles_ringbuffer_start);
|
|
|
|
gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_opensles_ringbuffer_stop);
|
|
|
|
gstringbuffer_class->delay =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_opensles_ringbuffer_delay);
|
2012-10-01 09:14:24 +00:00
|
|
|
gstringbuffer_class->clear_all =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_opensles_ringbuffer_clear_all);
|
2012-09-19 16:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-10-18 14:23:49 +00:00
|
|
|
gst_opensles_ringbuffer_init (GstOpenSLESRingBuffer * thiz)
|
2012-09-19 16:11:54 +00:00
|
|
|
{
|
|
|
|
thiz->mode = RB_MODE_NONE;
|
|
|
|
thiz->engineObject = NULL;
|
|
|
|
thiz->outputMixObject = NULL;
|
|
|
|
thiz->playerObject = NULL;
|
|
|
|
thiz->recorderObject = NULL;
|
2012-09-27 16:33:09 +00:00
|
|
|
thiz->is_queue_callback_registered = FALSE;
|
2012-09-19 16:11:54 +00:00
|
|
|
}
|