diff options
Diffstat (limited to 'sys/osxaudio/gstosxaudiosrc.c')
-rw-r--r-- | sys/osxaudio/gstosxaudiosrc.c | 318 |
1 files changed, 188 insertions, 130 deletions
diff --git a/sys/osxaudio/gstosxaudiosrc.c b/sys/osxaudio/gstosxaudiosrc.c index e12e18af..fcd5e577 100644 --- a/sys/osxaudio/gstosxaudiosrc.c +++ b/sys/osxaudio/gstosxaudiosrc.c @@ -1,8 +1,29 @@ -/* GStreamer - * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu> - * 2000 Wim Taymans <wtay@chello.be> +/* + * GStreamer + * Copyright 2005,2006 Zaheer Abbas Merali <zaheerabbas at merali dot org> + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: * - * gstosxaudiosrc.c: + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Alternatively, the contents of this file may be used under the + * GNU Lesser General Public License Version 2.1 (the "LGPL"), in + * which case the following provisions apply instead of the ones + * mentioned above: * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -20,201 +41,238 @@ * Boston, MA 02111-1307, USA. */ +/** + * SECTION:element-plugin + * + * <refsect2> + * <title>Example launch line</title> + * <para> + * <programlisting> + * gst-launch -v -m osxaudiosrc ! fakesink + * </programlisting> + * </para> + * </refsect2> + */ + #ifdef HAVE_CONFIG_H -#include "config.h" +# include <config.h> #endif -#include <fcntl.h> -#include <errno.h> -#include <unistd.h> -#include <string.h> +#include <gst/gst.h> #include <CoreAudio/CoreAudio.h> +#include "gstosxaudiosrc.h" +#include "gstosxaudioelement.h" -#include <gstosxaudiosrc.h> -#include <gstosxaudioelement.h> +GST_DEBUG_CATEGORY_STATIC (osx_audiosrc_debug); +#define GST_CAT_DEFAULT osx_audiosrc_debug -/* elementfactory information */ -static const GstElementDetails gst_osxaudiosrc_details = -GST_ELEMENT_DETAILS ("Audio Source (Mac OS X)", +static GstElementDetails gst_osx_audio_src_details = +GST_ELEMENT_DETAILS ("Audio Source (OSX)", "Source/Audio", - "Read from the sound card", + "Input from a sound card in OS X", "Zaheer Abbas Merali <zaheerabbas at merali dot org>"); - -/* Osxaudiosrc signals and args */ +/* Filter signals and args */ enum { /* FILL ME */ LAST_SIGNAL }; -static GstStaticPadTemplate osxaudiosrc_src_factory = -GST_STATIC_PAD_TEMPLATE ("src", +enum +{ + ARG_0, + ARG_DEVICE +}; + +static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/x-raw-float, " - "endianness = (int) BYTE_ORDER, " - "signed = (boolean) TRUE , " - "width = (int) 32, " "rate = (int) 44100, " "channels = (int) 2") + "endianness = (int) {" G_STRINGIFY (G_BYTE_ORDER) " }, " + "signed = (boolean) { TRUE }, " + "width = (int) 32, " + "depth = (int) 32, " "rate = (int) 44100, " "channels = (int) 2") ); -static void gst_osxaudiosrc_base_init (gpointer g_class); -static void gst_osxaudiosrc_class_init (GstOsxAudioSrcClass * klass); -static void gst_osxaudiosrc_init (GstOsxAudioSrc * osxaudiosrc); -static void gst_osxaudiosrc_dispose (GObject * object); +static void gst_osx_audio_src_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec); +static void gst_osx_audio_src_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec); -static GstStateChangeReturn gst_osxaudiosrc_change_state (GstElement * element); -static GstData *gst_osxaudiosrc_get (GstPad * pad); +static GstRingBuffer *gst_osx_audio_src_create_ringbuffer (GstBaseAudioSrc * + src); +/*static GstCaps* gst_osx_audio_sink_getcaps (GstBaseSink * bsrc);*/ +static void gst_osx_audio_src_osxelement_init (gpointer g_iface, + gpointer iface_data); +OSStatus gst_osx_audio_src_io_proc (AudioDeviceID inDevice, + const AudioTimeStamp * inNow, const AudioBufferList * inInputData, + const AudioTimeStamp * inInputTime, AudioBufferList * outOutputData, + const AudioTimeStamp * inOutputTime, void *inClientData); -static GstElementClass *parent_class = NULL; - -/*static guint gst_osxaudiosrc_signals[LAST_SIGNAL] = { 0 }; */ - -GType -gst_osxaudiosrc_get_type (void) +static void +gst_osx_audio_src_osxelement_do_init (GType type) { - static GType osxaudiosrc_type = 0; - - if (!osxaudiosrc_type) { - static const GTypeInfo osxaudiosrc_info = { - sizeof (GstOsxAudioSrcClass), - gst_osxaudiosrc_base_init, - NULL, - (GClassInitFunc) gst_osxaudiosrc_class_init, - NULL, - NULL, - sizeof (GstOsxAudioSrc), - 0, - (GInstanceInitFunc) gst_osxaudiosrc_init, - }; - - osxaudiosrc_type = - g_type_register_static (GST_TYPE_OSXAUDIOELEMENT, "GstOsxAudioSrc", - &osxaudiosrc_info, 0); - } - return osxaudiosrc_type; + static const GInterfaceInfo osxelement_info = { + gst_osx_audio_src_osxelement_init, + NULL, + NULL + }; + + GST_DEBUG_CATEGORY_INIT (osx_audiosrc_debug, "osxaudiosrc", 0, + "OSX Audio Src"); + GST_DEBUG ("Adding static interface\n"); + g_type_add_interface_static (type, GST_OSX_AUDIO_ELEMENT_TYPE, + &osxelement_info); } -static void -gst_osxaudiosrc_base_init (gpointer g_class) +GST_BOILERPLATE_FULL (GstOsxAudioSrc, gst_osx_audio_src, GstBaseAudioSrc, + GST_TYPE_BASE_AUDIO_SRC, gst_osx_audio_src_osxelement_do_init) + + + static void gst_osx_audio_src_base_init (gpointer g_class) { GstElementClass *element_class = GST_ELEMENT_CLASS (g_class); - gst_element_class_set_details (element_class, &gst_osxaudiosrc_details); gst_element_class_add_pad_template (element_class, - gst_static_pad_template_get (&osxaudiosrc_src_factory)); + gst_static_pad_template_get (&src_factory)); + + gst_element_class_set_details (element_class, &gst_osx_audio_src_details); } + +/* initialize the plugin's class */ static void -gst_osxaudiosrc_class_init (GstOsxAudioSrcClass * klass) +gst_osx_audio_src_class_init (GstOsxAudioSrcClass * klass) { GObjectClass *gobject_class; GstElementClass *gstelement_class; + GstBaseAudioSrcClass *gstbaseaudiosrc_class; gobject_class = (GObjectClass *) klass; gstelement_class = (GstElementClass *) klass; + gstbaseaudiosrc_class = (GstBaseAudioSrcClass *) klass; parent_class = g_type_class_peek_parent (klass); - gobject_class->dispose = gst_osxaudiosrc_dispose; + gobject_class->set_property = + GST_DEBUG_FUNCPTR (gst_osx_audio_src_set_property); + gobject_class->get_property = + GST_DEBUG_FUNCPTR (gst_osx_audio_src_get_property); - gstelement_class->change_state = gst_osxaudiosrc_change_state; + g_object_class_install_property (gobject_class, ARG_DEVICE, + g_param_spec_int ("device", "Device ID", "Device ID of output device", + 0, G_MAXINT, 0, G_PARAM_READWRITE)); + + gstbaseaudiosrc_class->create_ringbuffer = + GST_DEBUG_FUNCPTR (gst_osx_audio_src_create_ringbuffer); } +/* initialize the new element + * instantiate pads and add them to element + * set functions + * initialize structure + */ static void -gst_osxaudiosrc_init (GstOsxAudioSrc * osxaudiosrc) +gst_osx_audio_src_init (GstOsxAudioSrc * src, GstOsxAudioSrcClass * gclass) { - osxaudiosrc->srcpad = - gst_pad_new_from_template (gst_static_pad_template_get - (&osxaudiosrc_src_factory), "src"); - gst_pad_set_get_function (osxaudiosrc->srcpad, gst_osxaudiosrc_get); - - gst_element_add_pad (GST_ELEMENT (osxaudiosrc), osxaudiosrc->srcpad); - +/* GstElementClass *klass = GST_ELEMENT_GET_CLASS (sink); */ + gst_base_src_set_live (GST_BASE_SRC (src), TRUE); + src->ringbuffer = NULL; + GST_DEBUG ("Initialising object\n"); + gst_osx_audio_src_create_ringbuffer (GST_BASE_AUDIO_SRC (src)); } static void -gst_osxaudiosrc_dispose (GObject * object) +gst_osx_audio_src_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) { - /* GstOsxAudioSrc *osxaudiosrc = (GstOsxAudioSrc *) object; */ + GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object); - G_OBJECT_CLASS (parent_class)->dispose (object); + switch (prop_id) { + case ARG_DEVICE: + if (src->ringbuffer) + src->ringbuffer->device_id = g_value_get_int (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } } -static GstData * -gst_osxaudiosrc_get (GstPad * pad) +static void +gst_osx_audio_src_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec) { - GstOsxAudioSrc *src; - GstBuffer *buf; - glong readbytes; + GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object); + int val = 0; - src = GST_OSXAUDIOSRC (gst_pad_get_parent (pad)); + switch (prop_id) { + case ARG_DEVICE: + if (src->ringbuffer) + val = src->ringbuffer->device_id; - buf = gst_buffer_new_and_alloc ((GST_OSXAUDIOELEMENT (src))->buffer_len); + g_value_set_int (value, val); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} - readbytes = - read_buffer (GST_OSXAUDIOELEMENT (src), (char *) GST_BUFFER_DATA (buf)); +/* GstElement vmethod implementations */ - if (readbytes < 0) { - gst_buffer_unref (buf); - GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM); - gst_object_unref (src); - return GST_DATA (gst_event_new (GST_EVENT_INTERRUPT)); - } - if (readbytes == 0) { - gst_buffer_unref (buf); - gst_object_unref (src); - return GST_DATA (gst_event_new (GST_EVENT_INTERRUPT)); +/* GstBaseAudioSink vmethod implementations */ +static GstRingBuffer * +gst_osx_audio_src_create_ringbuffer (GstBaseAudioSrc * src) +{ + GstOsxAudioSrc *osxsrc; + + osxsrc = GST_OSX_AUDIO_SRC (src); + if (!osxsrc->ringbuffer) { + GST_DEBUG ("Creating ringbuffer\n"); + osxsrc->ringbuffer = g_object_new (GST_TYPE_OSX_RING_BUFFER, NULL); + GST_DEBUG ("osx src 0x%x element 0x%x ioproc 0x%x\n", osxsrc, + GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsrc), + (void *) gst_osx_audio_src_io_proc); + osxsrc->ringbuffer->element = GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsrc); } - GST_BUFFER_SIZE (buf) = readbytes; - GST_BUFFER_OFFSET (buf) = src->curoffset; - - src->curoffset += readbytes; + return GST_RING_BUFFER (osxsrc->ringbuffer); +} - GST_DEBUG ("pushed buffer from soundcard of %ld bytes", readbytes); +OSStatus +gst_osx_audio_src_io_proc (AudioDeviceID inDevice, const AudioTimeStamp * inNow, + const AudioBufferList * inInputData, const AudioTimeStamp * inInputTime, + AudioBufferList * outOutputData, const AudioTimeStamp * inOutputTime, + void *inClientData) +{ + GstOsxRingBuffer *buf = GST_OSX_RING_BUFFER (inClientData); - gst_object_unref (src); + guint8 *writeptr; + gint writeseg; + gint len; - return GST_DATA (buf); -} + if (gst_ring_buffer_prepare_read (GST_RING_BUFFER (buf), &writeseg, &writeptr, + &len)) { -static GstStateChangeReturn -gst_osxaudiosrc_change_state (GstElement * element, GstStateChange transition) -{ - GstOsxAudioSrc *osxaudiosrc = GST_OSXAUDIOSRC (element); - OSErr status; + memcpy (writeptr, (char *) inInputData->mBuffers[0].mData, len); - GST_DEBUG ("osxaudiosrc: state change"); + /* clear written samples */ + /*gst_ring_buffer_clear (GST_RING_BUFFER(buf), writeseg); */ - switch (transition) { - case GST_STATE_CHANGE_READY_TO_PAUSED: - osxaudiosrc->curoffset = 0; - break; - case GST_STATE_CHANGE_PAUSED_TO_PLAYING: - status = - AudioDeviceStart (GST_OSXAUDIOELEMENT (osxaudiosrc)->device_id, - inputAudioDeviceIOProc); - if (status) - GST_DEBUG ("AudioDeviceStart returned %d\n", (int) status); - break; - case GST_STATE_CHANGE_PLAYING_TO_PAUSED: - status = - AudioDeviceStop (GST_OSXAUDIOELEMENT (osxaudiosrc)->device_id, - inputAudioDeviceIOProc); - if (status) - GST_DEBUG ("AudioDeviceStop returned %d\n", (int) status); - break; - case GST_STATE_CHANGE_PAUSED_TO_READY: - break; - default: - break; + /* we wrote one segment */ + gst_ring_buffer_advance (GST_RING_BUFFER (buf), 1); } + return 0; +} - if (GST_ELEMENT_CLASS (parent_class)->change_state) - return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); +static void +gst_osx_audio_src_osxelement_init (gpointer g_iface, gpointer iface_data) +{ + GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface; - return GST_STATE_CHANGE_SUCCESS; + iface->io_proc = gst_osx_audio_src_io_proc; } |