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-openslessrc
22  * @title: openslessrc
23  * @see_also: openslessink
24  *
25  * This element reads data from default audio input using the OpenSL ES API in Android OS.
26  *
27  * ## Example pipelines
28  * |[
29  * gst-launch-1.0 -v openslessrc ! audioconvert ! vorbisenc ! oggmux ! filesink location=recorded.ogg
30  * ]| Record from default audio input and encode to Ogg/Vorbis.
31  *
32  */
33 
34 #ifdef HAVE_CONFIG_H
35 #  include <config.h>
36 #endif
37 
38 #include "openslessrc.h"
39 
40 GST_DEBUG_CATEGORY_STATIC (opensles_src_debug);
41 #define GST_CAT_DEFAULT opensles_src_debug
42 
43 /* *INDENT-OFF* */
44 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
45     GST_PAD_SRC,
46     GST_PAD_ALWAYS,
47     GST_STATIC_CAPS ("audio/x-raw, "
48         "format = (string) " GST_AUDIO_NE (S16) ", "
49         "rate = (int) 16000, "
50         "channels = (int) 1, "
51         "layout = (string) interleaved")
52     );
53 /* *INDENT-ON* */
54 
55 #define _do_init \
56   GST_DEBUG_CATEGORY_INIT (opensles_src_debug, "openslessrc", 0, \
57       "OpenSLES Source");
58 #define parent_class gst_opensles_src_parent_class
59 G_DEFINE_TYPE_WITH_CODE (GstOpenSLESSrc, gst_opensles_src,
60     GST_TYPE_AUDIO_BASE_SRC, _do_init);
61 
62 enum
63 {
64   PROP_0,
65   PROP_PRESET,
66 };
67 
68 #define DEFAULT_PRESET GST_OPENSLES_RECORDING_PRESET_NONE
69 
70 
71 static void
gst_opensles_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)72 gst_opensles_src_set_property (GObject * object, guint prop_id,
73     const GValue * value, GParamSpec * pspec)
74 {
75   GstOpenSLESSrc *src = GST_OPENSLES_SRC (object);
76 
77   switch (prop_id) {
78     case PROP_PRESET:
79       src->preset = g_value_get_enum (value);
80       break;
81     default:
82       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
83       break;
84   }
85 }
86 
87 static void
gst_opensles_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)88 gst_opensles_src_get_property (GObject * object, guint prop_id,
89     GValue * value, GParamSpec * pspec)
90 {
91   GstOpenSLESSrc *src = GST_OPENSLES_SRC (object);
92 
93   switch (prop_id) {
94     case PROP_PRESET:
95       g_value_set_enum (value, src->preset);
96       break;
97     default:
98       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
99       break;
100   }
101 }
102 
103 static GstAudioRingBuffer *
gst_opensles_src_create_ringbuffer(GstAudioBaseSrc * base)104 gst_opensles_src_create_ringbuffer (GstAudioBaseSrc * base)
105 {
106   GstAudioRingBuffer *rb;
107 
108   rb = gst_opensles_ringbuffer_new (RB_MODE_SRC);
109   GST_OPENSLES_RING_BUFFER (rb)->preset = GST_OPENSLES_SRC (base)->preset;
110 
111   return rb;
112 }
113 
114 static void
gst_opensles_src_class_init(GstOpenSLESSrcClass * klass)115 gst_opensles_src_class_init (GstOpenSLESSrcClass * klass)
116 {
117   GObjectClass *gobject_class;
118   GstElementClass *gstelement_class;
119   GstAudioBaseSrcClass *gstaudiobasesrc_class;
120 
121   gobject_class = (GObjectClass *) klass;
122   gstelement_class = (GstElementClass *) klass;
123   gstaudiobasesrc_class = (GstAudioBaseSrcClass *) klass;
124 
125   gobject_class->set_property = gst_opensles_src_set_property;
126   gobject_class->get_property = gst_opensles_src_get_property;
127 
128   g_object_class_install_property (gobject_class, PROP_PRESET,
129       g_param_spec_enum ("preset", "Preset", "Recording preset to use",
130           GST_TYPE_OPENSLES_RECORDING_PRESET, DEFAULT_PRESET,
131           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
132 
133   gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
134 
135   gst_element_class_set_static_metadata (gstelement_class, "OpenSL ES Src",
136       "Source/Audio",
137       "Input sound using the OpenSL ES APIs",
138       "Josep Torra <support@fluendo.com>");
139 
140   gstaudiobasesrc_class->create_ringbuffer =
141       GST_DEBUG_FUNCPTR (gst_opensles_src_create_ringbuffer);
142 }
143 
144 static void
gst_opensles_src_init(GstOpenSLESSrc * src)145 gst_opensles_src_init (GstOpenSLESSrc * src)
146 {
147   /* Override some default values to fit on the AudioFlinger behaviour of
148    * processing 20ms buffers as minimum buffer size. */
149   GST_AUDIO_BASE_SRC (src)->buffer_time = 200000;
150   GST_AUDIO_BASE_SRC (src)->latency_time = 20000;
151 
152   src->preset = DEFAULT_PRESET;
153 }
154