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
|
|
|
*/
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/**
|
|
|
|
* SECTION:element-openslessink
|
2017-03-08 18:01:13 +00:00
|
|
|
* @title: openslessink
|
2012-09-29 17:00:13 +00:00
|
|
|
* @see_also: openslessrc
|
|
|
|
*
|
|
|
|
* This element renders raw audio samples using the OpenSL ES API in Android OS.
|
|
|
|
*
|
2017-03-08 18:01:13 +00:00
|
|
|
* ## Example pipelines
|
2012-09-29 17:00:13 +00:00
|
|
|
* |[
|
2015-12-14 02:09:46 +00:00
|
|
|
* gst-launch-1.0 -v filesrc location=music.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! opeslessink
|
2012-09-29 17:00:13 +00:00
|
|
|
* ]| Play an Ogg/Vorbis file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-09-19 16:11:54 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2012-11-01 14:35:17 +00:00
|
|
|
#include "opensles.h"
|
2012-09-19 16:11:54 +00:00
|
|
|
#include "openslessink.h"
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (opensles_sink_debug);
|
|
|
|
#define GST_CAT_DEFAULT opensles_sink_debug
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_VOLUME,
|
2012-09-20 16:41:50 +00:00
|
|
|
PROP_MUTE,
|
2015-01-28 07:36:39 +00:00
|
|
|
PROP_STREAM_TYPE,
|
2012-09-19 16:11:54 +00:00
|
|
|
PROP_LAST
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DEFAULT_VOLUME 1.0
|
2012-09-20 16:41:50 +00:00
|
|
|
#define DEFAULT_MUTE FALSE
|
2012-09-19 16:11:54 +00:00
|
|
|
|
2015-01-28 07:36:39 +00:00
|
|
|
#define DEFAULT_STREAM_TYPE GST_OPENSLES_STREAM_TYPE_NONE
|
|
|
|
|
2012-09-21 12:15:12 +00:00
|
|
|
|
|
|
|
/* According to Android's NDK doc the following are the supported rates */
|
2020-02-29 12:49:06 +00:00
|
|
|
#define RATES "8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000"
|
2012-09-19 16:11:54 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2012-10-18 14:23:49 +00:00
|
|
|
GST_STATIC_CAPS ("audio/x-raw, "
|
|
|
|
"format = (string) { " GST_AUDIO_NE (S16) ", " GST_AUDIO_NE (U8) "}, "
|
2015-04-08 05:22:17 +00:00
|
|
|
"rate = (int) { " RATES "}, " "channels = (int) [1, 2], "
|
|
|
|
"layout = (string) interleaved")
|
2012-09-19 16:11:54 +00:00
|
|
|
);
|
|
|
|
|
2012-10-18 14:23:49 +00:00
|
|
|
#define _do_init \
|
2015-01-28 06:42:26 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (opensles_sink_debug, "openslessink", 0, \
|
|
|
|
"OpenSLES Sink");
|
2012-10-18 14:23:49 +00:00
|
|
|
#define parent_class gst_opensles_sink_parent_class
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstOpenSLESSink, gst_opensles_sink,
|
|
|
|
GST_TYPE_AUDIO_BASE_SINK, _do_init);
|
2021-03-26 10:00:50 +00:00
|
|
|
GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (openslessink, "openslessink",
|
|
|
|
GST_RANK_PRIMARY, GST_TYPE_OPENSLES_SINK, opensles_element_init (plugin));
|
2012-09-19 16:11:54 +00:00
|
|
|
|
2012-10-18 14:23:49 +00:00
|
|
|
static GstAudioRingBuffer *
|
|
|
|
gst_opensles_sink_create_ringbuffer (GstAudioBaseSink * base)
|
2012-09-19 16:11:54 +00:00
|
|
|
{
|
2012-09-20 16:04:29 +00:00
|
|
|
GstOpenSLESSink *sink = GST_OPENSLES_SINK (base);
|
2012-10-18 14:23:49 +00:00
|
|
|
GstAudioRingBuffer *rb;
|
2012-09-19 16:11:54 +00:00
|
|
|
|
|
|
|
rb = gst_opensles_ringbuffer_new (RB_MODE_SINK_PCM);
|
2012-09-20 16:04:29 +00:00
|
|
|
gst_opensles_ringbuffer_set_volume (rb, sink->volume);
|
2012-09-20 16:41:50 +00:00
|
|
|
gst_opensles_ringbuffer_set_mute (rb, sink->mute);
|
2015-01-28 07:36:39 +00:00
|
|
|
|
|
|
|
GST_OPENSLES_RING_BUFFER (rb)->stream_type = sink->stream_type;
|
|
|
|
|
2012-09-19 16:11:54 +00:00
|
|
|
return rb;
|
|
|
|
}
|
|
|
|
|
2012-09-21 12:15:12 +00:00
|
|
|
#define AUDIO_OUTPUT_DESC_FORMAT \
|
|
|
|
"deviceName: %s deviceConnection: %d deviceScope: %d deviceLocation: %d " \
|
|
|
|
"isForTelephony: %d minSampleRate: %d maxSampleRate: %d " \
|
|
|
|
"isFreqRangeContinuous: %d maxChannels: %d"
|
|
|
|
|
|
|
|
#define AUDIO_OUTPUT_DESC_ARGS(aod) \
|
|
|
|
(gchar*) (aod)->pDeviceName, (gint) (aod)->deviceConnection, \
|
|
|
|
(gint) (aod)->deviceScope, (gint) (aod)->deviceLocation, \
|
|
|
|
(gint) (aod)->isForTelephony, (gint) (aod)->minSampleRate, \
|
|
|
|
(gint) (aod)->maxSampleRate, (gint) (aod)->isFreqRangeContinuous, \
|
|
|
|
(gint) (aod)->maxChannels
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_opensles_query_capabilities (GstOpenSLESSink * sink)
|
|
|
|
{
|
|
|
|
gboolean res = FALSE;
|
|
|
|
SLresult result;
|
|
|
|
SLObjectItf engineObject = NULL;
|
|
|
|
SLAudioIODeviceCapabilitiesItf audioIODeviceCapabilities;
|
|
|
|
SLint32 i, j, numOutputs = MAX_NUMBER_OUTPUT_DEVICES;
|
|
|
|
SLuint32 outputDeviceIDs[MAX_NUMBER_OUTPUT_DEVICES];
|
|
|
|
SLAudioOutputDescriptor audioOutputDescriptor;
|
|
|
|
|
2012-11-01 14:35:17 +00:00
|
|
|
/* Create and realize engine */
|
|
|
|
engineObject = gst_opensles_get_engine ();
|
|
|
|
if (!engineObject) {
|
|
|
|
GST_ERROR_OBJECT (sink, "Getting engine failed");
|
2012-09-21 12:15:12 +00:00
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Get the engine interface, which is needed in order to create other objects */
|
2012-09-21 12:15:12 +00:00
|
|
|
result = (*engineObject)->GetInterface (engineObject,
|
|
|
|
SL_IID_AUDIOIODEVICECAPABILITIES, &audioIODeviceCapabilities);
|
2014-07-16 22:11:21 +00:00
|
|
|
if (result == SL_RESULT_FEATURE_UNSUPPORTED) {
|
|
|
|
GST_LOG_OBJECT (sink,
|
|
|
|
"engine.GetInterface(IODeviceCapabilities) unsupported(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
goto beach;
|
|
|
|
} else if (result != SL_RESULT_SUCCESS) {
|
2012-09-21 12:15:12 +00:00
|
|
|
GST_ERROR_OBJECT (sink,
|
|
|
|
"engine.GetInterface(IODeviceCapabilities) failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Query the list of available audio outputs */
|
2012-09-21 12:15:12 +00:00
|
|
|
result = (*audioIODeviceCapabilities)->GetAvailableAudioOutputs
|
|
|
|
(audioIODeviceCapabilities, &numOutputs, outputDeviceIDs);
|
2014-07-16 22:11:21 +00:00
|
|
|
if (result == SL_RESULT_FEATURE_UNSUPPORTED) {
|
|
|
|
GST_LOG_OBJECT (sink,
|
|
|
|
"IODeviceCapabilities.GetAvailableAudioOutputs unsupported(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
goto beach;
|
|
|
|
} else if (result != SL_RESULT_SUCCESS) {
|
2012-09-21 12:15:12 +00:00
|
|
|
GST_ERROR_OBJECT (sink,
|
|
|
|
"IODeviceCapabilities.GetAvailableAudioOutputs failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (sink, "Found %d output devices", (gint32) numOutputs);
|
|
|
|
|
|
|
|
for (i = 0; i < numOutputs; i++) {
|
|
|
|
result = (*audioIODeviceCapabilities)->QueryAudioOutputCapabilities
|
|
|
|
(audioIODeviceCapabilities, outputDeviceIDs[i], &audioOutputDescriptor);
|
2014-07-16 22:11:21 +00:00
|
|
|
|
|
|
|
if (result == SL_RESULT_FEATURE_UNSUPPORTED) {
|
|
|
|
GST_LOG_OBJECT (sink,
|
|
|
|
"IODeviceCapabilities.QueryAudioOutputCapabilities unsupported(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
continue;
|
|
|
|
} else if (result != SL_RESULT_SUCCESS) {
|
2012-09-21 12:15:12 +00:00
|
|
|
GST_ERROR_OBJECT (sink,
|
|
|
|
"IODeviceCapabilities.QueryAudioOutputCapabilities failed(0x%08x)",
|
|
|
|
(guint32) result);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (sink, " ID: %08x " AUDIO_OUTPUT_DESC_FORMAT,
|
|
|
|
(guint) outputDeviceIDs[i],
|
|
|
|
AUDIO_OUTPUT_DESC_ARGS (&audioOutputDescriptor));
|
|
|
|
GST_DEBUG_OBJECT (sink, " Found %d supported sample rated",
|
|
|
|
audioOutputDescriptor.numOfSamplingRatesSupported);
|
|
|
|
|
|
|
|
for (j = 0; j < audioOutputDescriptor.numOfSamplingRatesSupported; j++) {
|
|
|
|
GST_DEBUG_OBJECT (sink, " %d Hz",
|
|
|
|
(gint) audioOutputDescriptor.samplingRatesSupported[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res = TRUE;
|
|
|
|
beach:
|
2012-09-29 17:00:13 +00:00
|
|
|
/* Destroy the engine object */
|
2012-09-21 12:15:12 +00:00
|
|
|
if (engineObject) {
|
2012-11-01 14:35:17 +00:00
|
|
|
gst_opensles_release_engine (engineObject);
|
2012-09-21 12:15:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-09-19 16:11:54 +00:00
|
|
|
static void
|
|
|
|
gst_opensles_sink_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstOpenSLESSink *sink = GST_OPENSLES_SINK (object);
|
2012-10-18 14:23:49 +00:00
|
|
|
GstAudioRingBuffer *rb = GST_AUDIO_BASE_SINK (sink)->ringbuffer;
|
2012-09-19 16:11:54 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_VOLUME:
|
|
|
|
sink->volume = g_value_get_double (value);
|
2012-09-20 16:41:50 +00:00
|
|
|
if (rb && GST_IS_OPENSLES_RING_BUFFER (rb)) {
|
|
|
|
gst_opensles_ringbuffer_set_volume (rb, sink->volume);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PROP_MUTE:
|
|
|
|
sink->mute = g_value_get_boolean (value);
|
|
|
|
if (rb && GST_IS_OPENSLES_RING_BUFFER (rb)) {
|
|
|
|
gst_opensles_ringbuffer_set_mute (rb, sink->mute);
|
|
|
|
}
|
2012-09-19 16:11:54 +00:00
|
|
|
break;
|
2015-01-28 07:36:39 +00:00
|
|
|
case PROP_STREAM_TYPE:
|
|
|
|
sink->stream_type = g_value_get_enum (value);
|
|
|
|
break;
|
2012-09-19 16:11:54 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_opensles_sink_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstOpenSLESSink *sink = GST_OPENSLES_SINK (object);
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_VOLUME:
|
|
|
|
g_value_set_double (value, sink->volume);
|
|
|
|
break;
|
2012-09-20 16:41:50 +00:00
|
|
|
case PROP_MUTE:
|
|
|
|
g_value_set_boolean (value, sink->mute);
|
|
|
|
break;
|
2015-01-28 07:36:39 +00:00
|
|
|
case PROP_STREAM_TYPE:
|
|
|
|
g_value_set_enum (value, sink->stream_type);
|
|
|
|
break;
|
2012-09-19 16:11:54 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_opensles_sink_class_init (GstOpenSLESSinkClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
2012-10-18 14:23:49 +00:00
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
GstAudioBaseSinkClass *gstbaseaudiosink_class;
|
2012-09-19 16:11:54 +00:00
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
2012-10-18 14:23:49 +00:00
|
|
|
gstelement_class = (GstElementClass *) klass;
|
|
|
|
gstbaseaudiosink_class = (GstAudioBaseSinkClass *) klass;
|
2012-09-19 16:11:54 +00:00
|
|
|
|
|
|
|
gobject_class->set_property = gst_opensles_sink_set_property;
|
|
|
|
gobject_class->get_property = gst_opensles_sink_get_property;
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_VOLUME,
|
|
|
|
g_param_spec_double ("volume", "Volume", "Volume of this stream",
|
|
|
|
0, 1.0, 1.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2012-09-20 16:41:50 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_MUTE,
|
|
|
|
g_param_spec_boolean ("mute", "Mute", "Mute state of this stream",
|
|
|
|
DEFAULT_MUTE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2015-01-28 07:36:39 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_STREAM_TYPE,
|
|
|
|
g_param_spec_enum ("stream-type", "Stream type",
|
|
|
|
"Stream type that this stream should be tagged with",
|
|
|
|
GST_TYPE_OPENSLES_STREAM_TYPE, DEFAULT_STREAM_TYPE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2016-03-04 06:50:26 +00:00
|
|
|
gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
|
2012-10-18 14:23:49 +00:00
|
|
|
|
|
|
|
gst_element_class_set_static_metadata (gstelement_class, "OpenSL ES Sink",
|
|
|
|
"Sink/Audio",
|
|
|
|
"Output sound using the OpenSL ES APIs",
|
|
|
|
"Josep Torra <support@fluendo.com>");
|
|
|
|
|
2012-09-19 16:11:54 +00:00
|
|
|
gstbaseaudiosink_class->create_ringbuffer =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_opensles_sink_create_ringbuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-10-18 14:23:49 +00:00
|
|
|
gst_opensles_sink_init (GstOpenSLESSink * sink)
|
2012-09-19 16:11:54 +00:00
|
|
|
{
|
2015-01-28 07:36:39 +00:00
|
|
|
sink->stream_type = DEFAULT_STREAM_TYPE;
|
2012-09-19 16:11:54 +00:00
|
|
|
sink->volume = DEFAULT_VOLUME;
|
2012-09-20 16:41:50 +00:00
|
|
|
sink->mute = DEFAULT_MUTE;
|
2012-09-21 12:15:12 +00:00
|
|
|
|
|
|
|
_opensles_query_capabilities (sink);
|
2012-09-26 10:50:42 +00:00
|
|
|
|
2012-10-18 14:23:49 +00:00
|
|
|
gst_audio_base_sink_set_provide_clock (GST_AUDIO_BASE_SINK (sink), TRUE);
|
2012-09-19 16:11:54 +00:00
|
|
|
}
|