3 * Copyright (C) 2005 Thomas Vander Stichele <thomas@apestaart.org>
4 * Copyright (C) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
5 * Copyright (C) 2008 Victor Lin <bornstub@gmail.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
25 * Alternatively, the contents of this file may be used under the
26 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
27 * which case the following provisions apply instead of the ones
30 * This library is free software; you can redistribute it and/or
31 * modify it under the terms of the GNU Library General Public
32 * License as published by the Free Software Foundation; either
33 * version 2 of the License, or (at your option) any later version.
35 * This library is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
38 * Library General Public License for more details.
40 * You should have received a copy of the GNU Library General Public
41 * License along with this library; if not, write to the
42 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
43 * Boston, MA 02111-1307, USA.
47 * SECTION:element-openalsrc
48 * @short_description: record sound from your sound card using OpenAL
52 * This element lets you record sound using the OpenAL
54 * <title>Example pipelines</title>
57 * gst-launch -v openalsrc ! audioconvert ! vorbisenc ! oggmux ! filesink location=mymusic.ogg
59 * will record sound from your sound card using OpenAL and encode it to an Ogg/Vorbis file
69 #include <gst/gsterror.h>
71 #include "gstopenalsrc.h"
73 GST_DEBUG_CATEGORY_STATIC (openalsrc_debug);
75 #define GST_CAT_DEFAULT openalsrc_debug
77 #define DEFAULT_DEVICE NULL
78 #define DEFAULT_DEVICE_NAME NULL
81 Filter signals and args
100 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
103 GST_STATIC_CAPS ("audio/x-raw-int, "
104 "endianness = (int) BYTE_ORDER, "
105 "signed = (boolean) TRUE, "
108 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]; "
110 "signed = (boolean) TRUE, "
113 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]")
116 GST_BOILERPLATE (GstOpenalSrc, gst_openal_src, GstAudioSrc, GST_TYPE_AUDIO_SRC);
118 static void gst_openal_src_set_property (GObject * object, guint prop_id,
119 const GValue * value, GParamSpec * pspec);
120 static void gst_openal_src_get_property (GObject * object, guint prop_id,
121 GValue * value, GParamSpec * pspec);
122 static gboolean gst_openal_src_open (GstAudioSrc * src);
124 gst_openal_src_prepare (GstAudioSrc * src, GstRingBufferSpec * spec);
125 static gboolean gst_openal_src_unprepare (GstAudioSrc * src);
126 static gboolean gst_openal_src_close (GstAudioSrc * src);
128 gst_openal_src_read (GstAudioSrc * src, gpointer data, guint length);
129 static guint gst_openal_src_delay (GstAudioSrc * src);
130 static void gst_openal_src_reset (GstAudioSrc * src);
132 static void gst_openal_src_finalize (GObject * object);
135 gst_openal_src_base_init (gpointer gclass)
138 GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
140 gst_element_class_set_details_simple (element_class, "OpenAL src",
142 "OpenAL source capture audio from device",
143 "Victor Lin <bornstub@gmail.com>");
145 gst_element_class_add_static_pad_template (element_class, &src_factory);
149 gst_openal_src_class_init (GstOpenalSrcClass * klass)
151 GObjectClass *gobject_class;
152 GstAudioSrcClass *gstaudio_src_class;
154 gobject_class = G_OBJECT_CLASS (klass);
155 gstaudio_src_class = GST_AUDIO_SRC_CLASS (klass);
157 GST_DEBUG_CATEGORY_INIT (openalsrc_debug, "openalsrc",
158 0, "OpenAL source capture audio from device");
160 gobject_class->set_property = gst_openal_src_set_property;
161 gobject_class->get_property = gst_openal_src_get_property;
162 gobject_class->finalize = gst_openal_src_finalize;
164 gstaudio_src_class->open = GST_DEBUG_FUNCPTR (gst_openal_src_open);
165 gstaudio_src_class->prepare = GST_DEBUG_FUNCPTR (gst_openal_src_prepare);
166 gstaudio_src_class->unprepare = GST_DEBUG_FUNCPTR (gst_openal_src_unprepare);
167 gstaudio_src_class->close = GST_DEBUG_FUNCPTR (gst_openal_src_close);
168 gstaudio_src_class->read = GST_DEBUG_FUNCPTR (gst_openal_src_read);
169 gstaudio_src_class->delay = GST_DEBUG_FUNCPTR (gst_openal_src_delay);
170 gstaudio_src_class->reset = GST_DEBUG_FUNCPTR (gst_openal_src_reset);
172 g_object_class_install_property (gobject_class,
174 g_param_spec_string ("device",
176 "Specific capture device to open, NULL indicate default device",
177 DEFAULT_DEVICE, G_PARAM_READWRITE)
180 g_object_class_install_property (gobject_class,
182 g_param_spec_string ("device-name",
184 "Readable name of device", DEFAULT_DEVICE_NAME, G_PARAM_READABLE)
189 gst_openal_src_init (GstOpenalSrc * osrc, GstOpenalSrcClass * gclass)
191 osrc->deviceName = g_strdup (DEFAULT_DEVICE_NAME);
192 osrc->device = DEFAULT_DEVICE;
193 osrc->deviceHandle = NULL;
197 gst_openal_src_set_property (GObject * object, guint prop_id,
198 const GValue * value, GParamSpec * pspec)
200 GstOpenalSrc *osrc = GST_OPENAL_SRC (object);
204 osrc->device = g_value_dup_string (value);
206 case PROP_DEVICE_NAME:
207 osrc->deviceName = g_value_dup_string (value);
210 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216 gst_openal_src_get_property (GObject * object, guint prop_id, GValue * value,
219 GstOpenalSrc *osrc = GST_OPENAL_SRC (object);
223 g_value_set_string (value, osrc->device);
225 case PROP_DEVICE_NAME:
226 g_value_set_string (value, osrc->deviceName);
229 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
235 gst_openal_src_open (GstAudioSrc * asrc)
237 /* We don't do anything here */
242 gst_openal_src_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
245 GstOpenalSrc *osrc = GST_OPENAL_SRC (asrc);
249 switch (spec->width) {
251 format = AL_FORMAT_STEREO8;
254 format = AL_FORMAT_STEREO16;
257 g_assert_not_reached ();
261 spec->buffer_time * spec->rate * spec->bytes_per_sample / 1000000;
263 GST_INFO_OBJECT (osrc, "Open device : %s", osrc->deviceName);
265 alcCaptureOpenDevice (osrc->device, spec->rate, format, bufferSize);
267 if (!osrc->deviceHandle) {
268 GST_ELEMENT_ERROR (osrc,
271 ("Can't open device \"%s\"", osrc->device),
272 ("Can't open device \"%s\"", osrc->device)
278 g_strdup (alcGetString (osrc->deviceHandle, ALC_DEVICE_SPECIFIER));
279 osrc->bytes_per_sample = spec->bytes_per_sample;
281 GST_INFO_OBJECT (osrc, "Start capture");
282 alcCaptureStart (osrc->deviceHandle);
288 gst_openal_src_unprepare (GstAudioSrc * asrc)
291 GstOpenalSrc *osrc = GST_OPENAL_SRC (asrc);
293 GST_INFO_OBJECT (osrc, "Close device : %s", osrc->deviceName);
294 if (osrc->deviceHandle) {
295 alcCaptureStop (osrc->deviceHandle);
296 alcCaptureCloseDevice (osrc->deviceHandle);
303 gst_openal_src_close (GstAudioSrc * asrc)
305 /* We don't do anything here */
310 gst_openal_src_read (GstAudioSrc * asrc, gpointer data, guint length)
312 GstOpenalSrc *osrc = GST_OPENAL_SRC (asrc);
315 alcGetIntegerv (osrc->deviceHandle, ALC_CAPTURE_SAMPLES, sizeof (samples),
318 if (samples * osrc->bytes_per_sample > length) {
319 samples = length / osrc->bytes_per_sample;
323 GST_DEBUG_OBJECT (osrc, "Read samples : %d", samples);
324 alcCaptureSamples (osrc->deviceHandle, data, samples);
327 return samples * osrc->bytes_per_sample;
331 gst_openal_src_delay (GstAudioSrc * asrc)
333 GstOpenalSrc *osrc = GST_OPENAL_SRC (asrc);
336 alcGetIntegerv (osrc->deviceHandle, ALC_CAPTURE_SAMPLES, sizeof (samples),
343 gst_openal_src_reset (GstAudioSrc * asrc)
345 /* We don't do anything here */
349 gst_openal_src_finalize (GObject * object)
351 GstOpenalSrc *osrc = GST_OPENAL_SRC (object);
353 g_free (osrc->deviceName);
354 g_free (osrc->device);
356 G_OBJECT_CLASS (parent_class)->finalize (object);