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"
61 /* 48000 Hz is also claimed to be supported but the AudioFlinger downsampling
62  * doesn't seems to work properly so we relay GStreamer audioresample element
63  * to cope with this samplerate. */
64 
65 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
66     GST_PAD_SINK,
67     GST_PAD_ALWAYS,
68     GST_STATIC_CAPS ("audio/x-raw, "
69         "format = (string) { " GST_AUDIO_NE (S16) ", " GST_AUDIO_NE (U8) "}, "
70         "rate = (int) { " RATES "}, " "channels = (int) [1, 2], "
71         "layout = (string) interleaved")
72     );
73 
74 #define _do_init \
75   GST_DEBUG_CATEGORY_INIT (opensles_sink_debug, "openslessink", 0, \
76       "OpenSLES Sink");
77 #define parent_class gst_opensles_sink_parent_class
78 G_DEFINE_TYPE_WITH_CODE (GstOpenSLESSink, gst_opensles_sink,
79     GST_TYPE_AUDIO_BASE_SINK, _do_init);
80 
81 static GstAudioRingBuffer *
gst_opensles_sink_create_ringbuffer(GstAudioBaseSink * base)82 gst_opensles_sink_create_ringbuffer (GstAudioBaseSink * base)
83 {
84   GstOpenSLESSink *sink = GST_OPENSLES_SINK (base);
85   GstAudioRingBuffer *rb;
86 
87   rb = gst_opensles_ringbuffer_new (RB_MODE_SINK_PCM);
88   gst_opensles_ringbuffer_set_volume (rb, sink->volume);
89   gst_opensles_ringbuffer_set_mute (rb, sink->mute);
90 
91   GST_OPENSLES_RING_BUFFER (rb)->stream_type = sink->stream_type;
92 
93   return rb;
94 }
95 
96 #define AUDIO_OUTPUT_DESC_FORMAT                                              \
97     "deviceName: %s deviceConnection: %d deviceScope: %d deviceLocation: %d " \
98     "isForTelephony: %d minSampleRate: %d maxSampleRate: %d "                 \
99     "isFreqRangeContinuous: %d maxChannels: %d"
100 
101 #define AUDIO_OUTPUT_DESC_ARGS(aod)                                           \
102     (gchar*) (aod)->pDeviceName, (gint) (aod)->deviceConnection,              \
103     (gint) (aod)->deviceScope, (gint) (aod)->deviceLocation,                  \
104     (gint) (aod)->isForTelephony, (gint) (aod)->minSampleRate,                \
105     (gint) (aod)->maxSampleRate, (gint) (aod)->isFreqRangeContinuous,         \
106     (gint) (aod)->maxChannels
107 
108 static gboolean
_opensles_query_capabilities(GstOpenSLESSink * sink)109 _opensles_query_capabilities (GstOpenSLESSink * sink)
110 {
111   gboolean res = FALSE;
112   SLresult result;
113   SLObjectItf engineObject = NULL;
114   SLAudioIODeviceCapabilitiesItf audioIODeviceCapabilities;
115   SLint32 i, j, numOutputs = MAX_NUMBER_OUTPUT_DEVICES;
116   SLuint32 outputDeviceIDs[MAX_NUMBER_OUTPUT_DEVICES];
117   SLAudioOutputDescriptor audioOutputDescriptor;
118 
119   /* Create and realize engine */
120   engineObject = gst_opensles_get_engine ();
121   if (!engineObject) {
122     GST_ERROR_OBJECT (sink, "Getting engine failed");
123     goto beach;
124   }
125 
126   /* Get the engine interface, which is needed in order to create other objects */
127   result = (*engineObject)->GetInterface (engineObject,
128       SL_IID_AUDIOIODEVICECAPABILITIES, &audioIODeviceCapabilities);
129   if (result == SL_RESULT_FEATURE_UNSUPPORTED) {
130     GST_LOG_OBJECT (sink,
131         "engine.GetInterface(IODeviceCapabilities) unsupported(0x%08x)",
132         (guint32) result);
133     goto beach;
134   } else if (result != SL_RESULT_SUCCESS) {
135     GST_ERROR_OBJECT (sink,
136         "engine.GetInterface(IODeviceCapabilities) failed(0x%08x)",
137         (guint32) result);
138     goto beach;
139   }
140 
141   /* Query the list of available audio outputs */
142   result = (*audioIODeviceCapabilities)->GetAvailableAudioOutputs
143       (audioIODeviceCapabilities, &numOutputs, outputDeviceIDs);
144   if (result == SL_RESULT_FEATURE_UNSUPPORTED) {
145     GST_LOG_OBJECT (sink,
146         "IODeviceCapabilities.GetAvailableAudioOutputs unsupported(0x%08x)",
147         (guint32) result);
148     goto beach;
149   } else if (result != SL_RESULT_SUCCESS) {
150     GST_ERROR_OBJECT (sink,
151         "IODeviceCapabilities.GetAvailableAudioOutputs failed(0x%08x)",
152         (guint32) result);
153     goto beach;
154   }
155 
156   GST_DEBUG_OBJECT (sink, "Found %d output devices", (gint32) numOutputs);
157 
158   for (i = 0; i < numOutputs; i++) {
159     result = (*audioIODeviceCapabilities)->QueryAudioOutputCapabilities
160         (audioIODeviceCapabilities, outputDeviceIDs[i], &audioOutputDescriptor);
161 
162     if (result == SL_RESULT_FEATURE_UNSUPPORTED) {
163       GST_LOG_OBJECT (sink,
164           "IODeviceCapabilities.QueryAudioOutputCapabilities unsupported(0x%08x)",
165           (guint32) result);
166       continue;
167     } else if (result != SL_RESULT_SUCCESS) {
168       GST_ERROR_OBJECT (sink,
169           "IODeviceCapabilities.QueryAudioOutputCapabilities failed(0x%08x)",
170           (guint32) result);
171       continue;
172     }
173 
174     GST_DEBUG_OBJECT (sink, "  ID: %08x " AUDIO_OUTPUT_DESC_FORMAT,
175         (guint) outputDeviceIDs[i],
176         AUDIO_OUTPUT_DESC_ARGS (&audioOutputDescriptor));
177     GST_DEBUG_OBJECT (sink, "  Found %d supported sample rated",
178         audioOutputDescriptor.numOfSamplingRatesSupported);
179 
180     for (j = 0; j < audioOutputDescriptor.numOfSamplingRatesSupported; j++) {
181       GST_DEBUG_OBJECT (sink, "    %d Hz",
182           (gint) audioOutputDescriptor.samplingRatesSupported[j]);
183     }
184   }
185 
186   res = TRUE;
187 beach:
188   /* Destroy the engine object */
189   if (engineObject) {
190     gst_opensles_release_engine (engineObject);
191   }
192 
193   return res;
194 }
195 
196 static void
gst_opensles_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)197 gst_opensles_sink_set_property (GObject * object, guint prop_id,
198     const GValue * value, GParamSpec * pspec)
199 {
200   GstOpenSLESSink *sink = GST_OPENSLES_SINK (object);
201   GstAudioRingBuffer *rb = GST_AUDIO_BASE_SINK (sink)->ringbuffer;
202 
203   switch (prop_id) {
204     case PROP_VOLUME:
205       sink->volume = g_value_get_double (value);
206       if (rb && GST_IS_OPENSLES_RING_BUFFER (rb)) {
207         gst_opensles_ringbuffer_set_volume (rb, sink->volume);
208       }
209       break;
210     case PROP_MUTE:
211       sink->mute = g_value_get_boolean (value);
212       if (rb && GST_IS_OPENSLES_RING_BUFFER (rb)) {
213         gst_opensles_ringbuffer_set_mute (rb, sink->mute);
214       }
215       break;
216     case PROP_STREAM_TYPE:
217       sink->stream_type = g_value_get_enum (value);
218       break;
219     default:
220       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221       break;
222   }
223 }
224 
225 static void
gst_opensles_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)226 gst_opensles_sink_get_property (GObject * object, guint prop_id,
227     GValue * value, GParamSpec * pspec)
228 {
229   GstOpenSLESSink *sink = GST_OPENSLES_SINK (object);
230   switch (prop_id) {
231     case PROP_VOLUME:
232       g_value_set_double (value, sink->volume);
233       break;
234     case PROP_MUTE:
235       g_value_set_boolean (value, sink->mute);
236       break;
237     case PROP_STREAM_TYPE:
238       g_value_set_enum (value, sink->stream_type);
239       break;
240     default:
241       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242       break;
243   }
244 }
245 
246 static void
gst_opensles_sink_class_init(GstOpenSLESSinkClass * klass)247 gst_opensles_sink_class_init (GstOpenSLESSinkClass * klass)
248 {
249   GObjectClass *gobject_class;
250   GstElementClass *gstelement_class;
251   GstAudioBaseSinkClass *gstbaseaudiosink_class;
252 
253   gobject_class = (GObjectClass *) klass;
254   gstelement_class = (GstElementClass *) klass;
255   gstbaseaudiosink_class = (GstAudioBaseSinkClass *) klass;
256 
257   gobject_class->set_property = gst_opensles_sink_set_property;
258   gobject_class->get_property = gst_opensles_sink_get_property;
259 
260   g_object_class_install_property (gobject_class, PROP_VOLUME,
261       g_param_spec_double ("volume", "Volume", "Volume of this stream",
262           0, 1.0, 1.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
263 
264   g_object_class_install_property (gobject_class, PROP_MUTE,
265       g_param_spec_boolean ("mute", "Mute", "Mute state of this stream",
266           DEFAULT_MUTE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
267 
268   g_object_class_install_property (gobject_class, PROP_STREAM_TYPE,
269       g_param_spec_enum ("stream-type", "Stream type",
270           "Stream type that this stream should be tagged with",
271           GST_TYPE_OPENSLES_STREAM_TYPE, DEFAULT_STREAM_TYPE,
272           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
273 
274   gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
275 
276   gst_element_class_set_static_metadata (gstelement_class, "OpenSL ES Sink",
277       "Sink/Audio",
278       "Output sound using the OpenSL ES APIs",
279       "Josep Torra <support@fluendo.com>");
280 
281   gstbaseaudiosink_class->create_ringbuffer =
282       GST_DEBUG_FUNCPTR (gst_opensles_sink_create_ringbuffer);
283 }
284 
285 static void
gst_opensles_sink_init(GstOpenSLESSink * sink)286 gst_opensles_sink_init (GstOpenSLESSink * sink)
287 {
288   sink->stream_type = DEFAULT_STREAM_TYPE;
289   sink->volume = DEFAULT_VOLUME;
290   sink->mute = DEFAULT_MUTE;
291 
292   _opensles_query_capabilities (sink);
293 
294   gst_audio_base_sink_set_provide_clock (GST_AUDIO_BASE_SINK (sink), TRUE);
295   /* Override some default values to fit on the AudioFlinger behaviour of
296    * processing 20ms buffers as minimum buffer size. */
297   GST_AUDIO_BASE_SINK (sink)->buffer_time = 200000;
298   GST_AUDIO_BASE_SINK (sink)->latency_time = 20000;
299 }
300