1 /* GStreamer
2 * Copyright (C) 2012 Fluendo S.A. <support@fluendo.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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * SECTION:element-openslessink
22 * @title: openslessink
23 * @see_also: openslessrc
24 *
25 * This element renders raw audio samples using the OpenSL ES API in Android OS.
26 *
27 * ## Example pipelines
28 * |[
29 * gst-launch-1.0 -v filesrc location=music.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! opeslessink
30 * ]| Play an Ogg/Vorbis file.
31 *
32 */
33
34 #ifdef HAVE_CONFIG_H
35 # include <config.h>
36 #endif
37
38 #include "opensles.h"
39 #include "openslessink.h"
40
41 GST_DEBUG_CATEGORY_STATIC (opensles_sink_debug);
42 #define GST_CAT_DEFAULT opensles_sink_debug
43
44 enum
45 {
46 PROP_0,
47 PROP_VOLUME,
48 PROP_MUTE,
49 PROP_STREAM_TYPE,
50 PROP_LAST
51 };
52
53 #define DEFAULT_VOLUME 1.0
54 #define DEFAULT_MUTE FALSE
55
56 #define DEFAULT_STREAM_TYPE GST_OPENSLES_STREAM_TYPE_NONE
57
58
59 /* According to Android's NDK doc the following are the supported rates */
60 #define RATES "8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000"
61
62 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
63 GST_PAD_SINK,
64 GST_PAD_ALWAYS,
65 GST_STATIC_CAPS ("audio/x-raw, "
66 "format = (string) { " GST_AUDIO_NE (S16) ", " GST_AUDIO_NE (U8) "}, "
67 "rate = (int) { " RATES "}, " "channels = (int) [1, 2], "
68 "layout = (string) interleaved")
69 );
70
71 #define _do_init \
72 GST_DEBUG_CATEGORY_INIT (opensles_sink_debug, "openslessink", 0, \
73 "OpenSLES Sink");
74 #define parent_class gst_opensles_sink_parent_class
75 G_DEFINE_TYPE_WITH_CODE (GstOpenSLESSink, gst_opensles_sink,
76 GST_TYPE_AUDIO_BASE_SINK, _do_init);
77 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (openslessink, "openslessink",
78 GST_RANK_PRIMARY, GST_TYPE_OPENSLES_SINK, opensles_element_init (plugin));
79
80 static GstAudioRingBuffer *
gst_opensles_sink_create_ringbuffer(GstAudioBaseSink * base)81 gst_opensles_sink_create_ringbuffer (GstAudioBaseSink * base)
82 {
83 GstOpenSLESSink *sink = GST_OPENSLES_SINK (base);
84 GstAudioRingBuffer *rb;
85
86 rb = gst_opensles_ringbuffer_new (RB_MODE_SINK_PCM);
87 gst_opensles_ringbuffer_set_volume (rb, sink->volume);
88 gst_opensles_ringbuffer_set_mute (rb, sink->mute);
89
90 GST_OPENSLES_RING_BUFFER (rb)->stream_type = sink->stream_type;
91
92 return rb;
93 }
94
95 #define AUDIO_OUTPUT_DESC_FORMAT \
96 "deviceName: %s deviceConnection: %d deviceScope: %d deviceLocation: %d " \
97 "isForTelephony: %d minSampleRate: %d maxSampleRate: %d " \
98 "isFreqRangeContinuous: %d maxChannels: %d"
99
100 #define AUDIO_OUTPUT_DESC_ARGS(aod) \
101 (gchar*) (aod)->pDeviceName, (gint) (aod)->deviceConnection, \
102 (gint) (aod)->deviceScope, (gint) (aod)->deviceLocation, \
103 (gint) (aod)->isForTelephony, (gint) (aod)->minSampleRate, \
104 (gint) (aod)->maxSampleRate, (gint) (aod)->isFreqRangeContinuous, \
105 (gint) (aod)->maxChannels
106
107 static gboolean
_opensles_query_capabilities(GstOpenSLESSink * sink)108 _opensles_query_capabilities (GstOpenSLESSink * sink)
109 {
110 gboolean res = FALSE;
111 SLresult result;
112 SLObjectItf engineObject = NULL;
113 SLAudioIODeviceCapabilitiesItf audioIODeviceCapabilities;
114 SLint32 i, j, numOutputs = MAX_NUMBER_OUTPUT_DEVICES;
115 SLuint32 outputDeviceIDs[MAX_NUMBER_OUTPUT_DEVICES];
116 SLAudioOutputDescriptor audioOutputDescriptor;
117
118 /* Create and realize engine */
119 engineObject = gst_opensles_get_engine ();
120 if (!engineObject) {
121 GST_ERROR_OBJECT (sink, "Getting engine failed");
122 goto beach;
123 }
124
125 /* Get the engine interface, which is needed in order to create other objects */
126 result = (*engineObject)->GetInterface (engineObject,
127 SL_IID_AUDIOIODEVICECAPABILITIES, &audioIODeviceCapabilities);
128 if (result == SL_RESULT_FEATURE_UNSUPPORTED) {
129 GST_LOG_OBJECT (sink,
130 "engine.GetInterface(IODeviceCapabilities) unsupported(0x%08x)",
131 (guint32) result);
132 goto beach;
133 } else if (result != SL_RESULT_SUCCESS) {
134 GST_ERROR_OBJECT (sink,
135 "engine.GetInterface(IODeviceCapabilities) failed(0x%08x)",
136 (guint32) result);
137 goto beach;
138 }
139
140 /* Query the list of available audio outputs */
141 result = (*audioIODeviceCapabilities)->GetAvailableAudioOutputs
142 (audioIODeviceCapabilities, &numOutputs, outputDeviceIDs);
143 if (result == SL_RESULT_FEATURE_UNSUPPORTED) {
144 GST_LOG_OBJECT (sink,
145 "IODeviceCapabilities.GetAvailableAudioOutputs unsupported(0x%08x)",
146 (guint32) result);
147 goto beach;
148 } else if (result != SL_RESULT_SUCCESS) {
149 GST_ERROR_OBJECT (sink,
150 "IODeviceCapabilities.GetAvailableAudioOutputs failed(0x%08x)",
151 (guint32) result);
152 goto beach;
153 }
154
155 GST_DEBUG_OBJECT (sink, "Found %d output devices", (gint32) numOutputs);
156
157 for (i = 0; i < numOutputs; i++) {
158 result = (*audioIODeviceCapabilities)->QueryAudioOutputCapabilities
159 (audioIODeviceCapabilities, outputDeviceIDs[i], &audioOutputDescriptor);
160
161 if (result == SL_RESULT_FEATURE_UNSUPPORTED) {
162 GST_LOG_OBJECT (sink,
163 "IODeviceCapabilities.QueryAudioOutputCapabilities unsupported(0x%08x)",
164 (guint32) result);
165 continue;
166 } else if (result != SL_RESULT_SUCCESS) {
167 GST_ERROR_OBJECT (sink,
168 "IODeviceCapabilities.QueryAudioOutputCapabilities failed(0x%08x)",
169 (guint32) result);
170 continue;
171 }
172
173 GST_DEBUG_OBJECT (sink, " ID: %08x " AUDIO_OUTPUT_DESC_FORMAT,
174 (guint) outputDeviceIDs[i],
175 AUDIO_OUTPUT_DESC_ARGS (&audioOutputDescriptor));
176 GST_DEBUG_OBJECT (sink, " Found %d supported sample rated",
177 audioOutputDescriptor.numOfSamplingRatesSupported);
178
179 for (j = 0; j < audioOutputDescriptor.numOfSamplingRatesSupported; j++) {
180 GST_DEBUG_OBJECT (sink, " %d Hz",
181 (gint) audioOutputDescriptor.samplingRatesSupported[j]);
182 }
183 }
184
185 res = TRUE;
186 beach:
187 /* Destroy the engine object */
188 if (engineObject) {
189 gst_opensles_release_engine (engineObject);
190 }
191
192 return res;
193 }
194
195 static void
gst_opensles_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)196 gst_opensles_sink_set_property (GObject * object, guint prop_id,
197 const GValue * value, GParamSpec * pspec)
198 {
199 GstOpenSLESSink *sink = GST_OPENSLES_SINK (object);
200 GstAudioRingBuffer *rb = GST_AUDIO_BASE_SINK (sink)->ringbuffer;
201
202 switch (prop_id) {
203 case PROP_VOLUME:
204 sink->volume = g_value_get_double (value);
205 if (rb && GST_IS_OPENSLES_RING_BUFFER (rb)) {
206 gst_opensles_ringbuffer_set_volume (rb, sink->volume);
207 }
208 break;
209 case PROP_MUTE:
210 sink->mute = g_value_get_boolean (value);
211 if (rb && GST_IS_OPENSLES_RING_BUFFER (rb)) {
212 gst_opensles_ringbuffer_set_mute (rb, sink->mute);
213 }
214 break;
215 case PROP_STREAM_TYPE:
216 sink->stream_type = g_value_get_enum (value);
217 break;
218 default:
219 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220 break;
221 }
222 }
223
224 static void
gst_opensles_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)225 gst_opensles_sink_get_property (GObject * object, guint prop_id,
226 GValue * value, GParamSpec * pspec)
227 {
228 GstOpenSLESSink *sink = GST_OPENSLES_SINK (object);
229 switch (prop_id) {
230 case PROP_VOLUME:
231 g_value_set_double (value, sink->volume);
232 break;
233 case PROP_MUTE:
234 g_value_set_boolean (value, sink->mute);
235 break;
236 case PROP_STREAM_TYPE:
237 g_value_set_enum (value, sink->stream_type);
238 break;
239 default:
240 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241 break;
242 }
243 }
244
245 static void
gst_opensles_sink_class_init(GstOpenSLESSinkClass * klass)246 gst_opensles_sink_class_init (GstOpenSLESSinkClass * klass)
247 {
248 GObjectClass *gobject_class;
249 GstElementClass *gstelement_class;
250 GstAudioBaseSinkClass *gstbaseaudiosink_class;
251
252 gobject_class = (GObjectClass *) klass;
253 gstelement_class = (GstElementClass *) klass;
254 gstbaseaudiosink_class = (GstAudioBaseSinkClass *) klass;
255
256 gobject_class->set_property = gst_opensles_sink_set_property;
257 gobject_class->get_property = gst_opensles_sink_get_property;
258
259 g_object_class_install_property (gobject_class, PROP_VOLUME,
260 g_param_spec_double ("volume", "Volume", "Volume of this stream",
261 0, 1.0, 1.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
262
263 g_object_class_install_property (gobject_class, PROP_MUTE,
264 g_param_spec_boolean ("mute", "Mute", "Mute state of this stream",
265 DEFAULT_MUTE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
266
267 g_object_class_install_property (gobject_class, PROP_STREAM_TYPE,
268 g_param_spec_enum ("stream-type", "Stream type",
269 "Stream type that this stream should be tagged with",
270 GST_TYPE_OPENSLES_STREAM_TYPE, DEFAULT_STREAM_TYPE,
271 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
272
273 gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
274
275 gst_element_class_set_static_metadata (gstelement_class, "OpenSL ES Sink",
276 "Sink/Audio",
277 "Output sound using the OpenSL ES APIs",
278 "Josep Torra <support@fluendo.com>");
279
280 gstbaseaudiosink_class->create_ringbuffer =
281 GST_DEBUG_FUNCPTR (gst_opensles_sink_create_ringbuffer);
282 }
283
284 static void
gst_opensles_sink_init(GstOpenSLESSink * sink)285 gst_opensles_sink_init (GstOpenSLESSink * sink)
286 {
287 sink->stream_type = DEFAULT_STREAM_TYPE;
288 sink->volume = DEFAULT_VOLUME;
289 sink->mute = DEFAULT_MUTE;
290
291 _opensles_query_capabilities (sink);
292
293 gst_audio_base_sink_set_provide_clock (GST_AUDIO_BASE_SINK (sink), TRUE);
294 }
295