diff options
Diffstat (limited to 'sys/osxaudio/gstosxaudiosink.c')
-rw-r--r-- | sys/osxaudio/gstosxaudiosink.c | 403 |
1 files changed, 254 insertions, 149 deletions
diff --git a/sys/osxaudio/gstosxaudiosink.c b/sys/osxaudio/gstosxaudiosink.c index 7f4a6fca9..b58d34b3a 100644 --- a/sys/osxaudio/gstosxaudiosink.c +++ b/sys/osxaudio/gstosxaudiosink.c @@ -1,8 +1,29 @@ -/* GStreamer - * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu> - * 2000 Wim Taymans <wim.taymans@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: * - * gstosxaudiosink.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,213 +41,297 @@ * Boston, MA 02111-1307, USA. */ +/** + * SECTION:element-plugin + * + * <refsect2> + * <title>Example launch line</title> + * <para> + * <programlisting> + * gst-launch -v -m audiotestsrc ! audioconvert ! osxaudiosink + * </programlisting> + * </para> + * </refsect2> + */ + #ifdef HAVE_CONFIG_H -#include "config.h" +# include <config.h> #endif -#include <CoreAudio/CoreAudio.h> -#include <errno.h> -#include <unistd.h> -#include <string.h> +#include <gst/gst.h> +#include <CoreAudio/CoreAudio.h> #include "gstosxaudiosink.h" +#include "gstosxaudioelement.h" + +GST_DEBUG_CATEGORY_STATIC (osx_audiosink_debug); +#define GST_CAT_DEFAULT osx_audiosink_debug -/* elementfactory information */ -static const GstElementDetails gst_osxaudiosink_details = -GST_ELEMENT_DETAILS ("Audio Sink (Mac OS X)", +static GstElementDetails gst_osx_audio_sink_details = +GST_ELEMENT_DETAILS ("Audio Sink (OSX)", "Sink/Audio", - "Output to a Mac OS X CoreAudio Sound Device", + "Output to a sound card in OS X", "Zaheer Abbas Merali <zaheerabbas at merali dot org>"); -static void gst_osxaudiosink_base_init (gpointer g_class); -static void gst_osxaudiosink_class_init (GstOsxAudioSinkClass * klass); -static void gst_osxaudiosink_init (GstOsxAudioSink * osxaudiosink); -static void gst_osxaudiosink_dispose (GObject * object); - -static GstStateChangeReturn gst_osxaudiosink_change_state (GstElement * - element); - -static void gst_osxaudiosink_chain (GstPad * pad, GstData * _data); - -/* OssSink signals and args */ +/* Filter signals and args */ enum { - SIGNAL_HANDOFF, + /* FILL ME */ LAST_SIGNAL }; -static GstStaticPadTemplate osxaudiosink_sink_factory = -GST_STATIC_PAD_TEMPLATE ("sink", +enum +{ + ARG_0, + ARG_DEVICE +}; + +static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/x-raw-float, " - "endianness = (int) BYTE_ORDER, " - "signed = (boolean) TRUE, " + "endianness = (int) {" G_STRINGIFY (G_BYTE_ORDER) " }, " + "signed = (boolean) { TRUE }, " "width = (int) 32, " "rate = (int) 44100, " "channels = (int) 2") ); -static GstElementClass *parent_class = NULL; -static guint gst_osssink_signals[LAST_SIGNAL] = { 0 }; - -GType -gst_osxaudiosink_get_type (void) -{ - static GType osxaudiosink_type = 0; - - if (!osxaudiosink_type) { - static const GTypeInfo osxaudiosink_info = { - sizeof (GstOsxAudioSinkClass), - gst_osxaudiosink_base_init, - NULL, - (GClassInitFunc) gst_osxaudiosink_class_init, - NULL, - NULL, - sizeof (GstOsxAudioSink), - 0, - (GInstanceInitFunc) gst_osxaudiosink_init, - }; - - osxaudiosink_type = - g_type_register_static (GST_TYPE_OSXAUDIOELEMENT, "GstOsxAudioSink", - &osxaudiosink_info, 0); - } - - return osxaudiosink_type; -} - +static void gst_osx_audio_sink_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec); +static void gst_osx_audio_sink_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec); +static GstCaps *gst_osx_audio_sink_getcaps (GstBaseSink * sink); + + +static GstRingBuffer *gst_osx_audio_sink_create_ringbuffer (GstBaseAudioSink * + sink); +/*static GstCaps* gst_osx_audio_sink_getcaps (GstBaseSink * bsink);*/ +static void gst_osx_audio_sink_osxelement_init (gpointer g_iface, + gpointer iface_data); +OSStatus gst_osx_audio_sink_io_proc (AudioDeviceID inDevice, + const AudioTimeStamp * inNow, const AudioBufferList * inInputData, + const AudioTimeStamp * inInputTime, AudioBufferList * outOutputData, + const AudioTimeStamp * inOutputTime, void *inClientData); static void -gst_osxaudiosink_dispose (GObject * object) +gst_osx_audio_sink_osxelement_do_init (GType type) { - /* GstOsxAudioSink *osxaudiosink = (GstOsxAudioSink *) object; */ + static const GInterfaceInfo osxelement_info = { + gst_osx_audio_sink_osxelement_init, + NULL, + NULL + }; + + GST_DEBUG_CATEGORY_INIT (osx_audiosink_debug, "osxaudiosink", 0, + "OSX Audio Sink"); + GST_DEBUG ("Adding static interface\n"); + g_type_add_interface_static (type, GST_OSX_AUDIO_ELEMENT_TYPE, + &osxelement_info); +} - /*gst_object_unparent (GST_OBJECT (osxaudiosink->provided_clock)); */ +GST_BOILERPLATE_FULL (GstOsxAudioSink, gst_osx_audio_sink, GstBaseAudioSink, + GST_TYPE_BASE_AUDIO_SINK, gst_osx_audio_sink_osxelement_do_init) - G_OBJECT_CLASS (parent_class)->dispose (object); -} -static void -gst_osxaudiosink_base_init (gpointer g_class) + static void gst_osx_audio_sink_base_init (gpointer g_class) { GstElementClass *element_class = GST_ELEMENT_CLASS (g_class); - gst_element_class_set_details (element_class, &gst_osxaudiosink_details); gst_element_class_add_pad_template (element_class, - gst_static_pad_template_get (&osxaudiosink_sink_factory)); + gst_static_pad_template_get (&sink_factory)); + + gst_element_class_set_details (element_class, &gst_osx_audio_sink_details); } + +/* initialize the plugin's class */ static void -gst_osxaudiosink_class_init (GstOsxAudioSinkClass * klass) +gst_osx_audio_sink_class_init (GstOsxAudioSinkClass * klass) { GObjectClass *gobject_class; GstElementClass *gstelement_class; + GstBaseSinkClass *gstbasesink_class; + GstBaseAudioSinkClass *gstbaseaudiosink_class; gobject_class = (GObjectClass *) klass; gstelement_class = (GstElementClass *) klass; + gstbasesink_class = (GstBaseSinkClass *) klass; + gstbaseaudiosink_class = (GstBaseAudioSinkClass *) klass; parent_class = g_type_class_peek_parent (klass); - gst_osssink_signals[SIGNAL_HANDOFF] = - g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, - G_STRUCT_OFFSET (GstOsxAudioSinkClass, handoff), NULL, NULL, - g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0); + gobject_class->set_property = + GST_DEBUG_FUNCPTR (gst_osx_audio_sink_set_property); + gobject_class->get_property = + GST_DEBUG_FUNCPTR (gst_osx_audio_sink_get_property); - gobject_class->dispose = gst_osxaudiosink_dispose; + 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)); + + gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_osx_audio_sink_getcaps); + gstbaseaudiosink_class->create_ringbuffer = + GST_DEBUG_FUNCPTR (gst_osx_audio_sink_create_ringbuffer); - gstelement_class->change_state = - GST_DEBUG_FUNCPTR (gst_osxaudiosink_change_state); } +/* initialize the new element + * instantiate pads and add them to element + * set functions + * initialize structure + */ static void -gst_osxaudiosink_init (GstOsxAudioSink * osxaudiosink) +gst_osx_audio_sink_init (GstOsxAudioSink * sink, GstOsxAudioSinkClass * gclass) { - osxaudiosink->sinkpad = - gst_pad_new_from_template (gst_static_pad_template_get - (&osxaudiosink_sink_factory), "sink"); - gst_element_add_pad (GST_ELEMENT (osxaudiosink), osxaudiosink->sinkpad); +/* GstElementClass *klass = GST_ELEMENT_GET_CLASS (sink); */ + sink->ringbuffer = NULL; + GST_DEBUG ("Initialising object\n"); + gst_osx_audio_sink_create_ringbuffer (sink); - gst_pad_set_chain_function (osxaudiosink->sinkpad, gst_osxaudiosink_chain); +} - GST_DEBUG ("initializing osxaudiosink"); +static void +gst_osx_audio_sink_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) +{ + GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object); - GST_OBJECT_FLAG_SET (osxaudiosink, GST_ELEMENT_THREAD_SUGGESTED); - GST_OBJECT_FLAG_SET (osxaudiosink, GST_ELEMENT_EVENT_AWARE); + switch (prop_id) { + case ARG_DEVICE: + if (sink->ringbuffer) + sink->ringbuffer->device_id = g_value_get_int (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } } static void -gst_osxaudiosink_chain (GstPad * pad, GstData * _data) +gst_osx_audio_sink_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec) { - GstBuffer *buf = GST_BUFFER (_data); - GstOsxAudioSink *osxaudiosink; - guchar *data; - guint to_write; - gint amount_written; - - /* this has to be an audio buffer */ - osxaudiosink = GST_OSXAUDIOSINK (gst_pad_get_parent (pad)); - - if (GST_IS_EVENT (buf)) { - GstEvent *event = GST_EVENT (buf); - - switch (GST_EVENT_TYPE (event)) { - case GST_EVENT_EOS: - gst_pad_event_default (pad, event); - gst_object_unref (osxaudiosink); - return; - case GST_EVENT_DISCONTINUOUS: - /* pass-through */ - default: - gst_object_unref (osxaudiosink); - gst_pad_event_default (pad, event); - return; - } - g_assert_not_reached (); + GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object); + int val = 0; + + switch (prop_id) { + case ARG_DEVICE: + if (sink->ringbuffer) + val = sink->ringbuffer->device_id; + + g_value_set_int (value, val); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; } +} + +/* GstElement vmethod implementations */ + +/* GstBaseSink vmethod implementations */ +static GstCaps * +gst_osx_audio_sink_getcaps (GstBaseSink * sink) +{ + GstCaps *caps; + GstOsxAudioSink *osxsink; + OSStatus status; + AudioValueRange rates[10]; + UInt32 propertySize; + int i; + + propertySize = sizeof (AudioValueRange) * 9; + osxsink = GST_OSX_AUDIO_SINK (sink); + + caps = gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD + (sink))); - data = GST_BUFFER_DATA (buf); - to_write = GST_BUFFER_SIZE (buf); - amount_written = 0; - while (amount_written < to_write) { - data += amount_written; - to_write -= amount_written; - amount_written = - write_buffer (GST_OSXAUDIOELEMENT (osxaudiosink), data, to_write); + status = AudioDeviceGetProperty (osxsink->ringbuffer->device_id, 0, FALSE, + kAudioDevicePropertyAvailableNominalSampleRates, &propertySize, &rates); + + GST_DEBUG + ("Getting available sample rates: Status: %d number of ranges: %d\n", + status, propertySize / sizeof (AudioValueRange)); + + for (i = 0; i < propertySize / sizeof (AudioValueRange); i++) { + g_print ("Range from %f to %f\n", rates[i].mMinimum, rates[i].mMaximum); } - gst_buffer_unref (buf); - gst_object_unref (osxaudiosink); + + return caps; } -static GstStateChangeReturn -gst_osxaudiosink_change_state (GstElement * element, GstStateChange transition) +/* GstBaseAudioSink vmethod implementations */ +static GstRingBuffer * +gst_osx_audio_sink_create_ringbuffer (GstBaseAudioSink * sink) { - GstOsxAudioSink *osxaudiosink; - OSErr status; + GstOsxAudioSink *osxsink; + + osxsink = GST_OSX_AUDIO_SINK (sink); + if (!osxsink->ringbuffer) { + GST_DEBUG ("Creating ringbuffer\n"); + osxsink->ringbuffer = g_object_new (GST_TYPE_OSX_RING_BUFFER, NULL); + GST_DEBUG ("osx sink 0x%x element 0x%x ioproc 0x%x\n", osxsink, + GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsink), + (void *) gst_osx_audio_sink_io_proc); + osxsink->ringbuffer->element = + GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsink); + } - osxaudiosink = GST_OSXAUDIOSINK (element); + return GST_RING_BUFFER (osxsink->ringbuffer); +} - switch (transition) { - case GST_STATE_CHANGE_READY_TO_PAUSED: - break; - case GST_STATE_CHANGE_PAUSED_TO_PLAYING: - status = - AudioDeviceStart (GST_OSXAUDIOELEMENT (osxaudiosink)->device_id, - outputAudioDeviceIOProc); - if (status) - GST_DEBUG ("AudioDeviceStart returned %d\n", (int) status); - break; - case GST_STATE_CHANGE_PLAYING_TO_PAUSED: - status = - AudioDeviceStop (GST_OSXAUDIOELEMENT (osxaudiosink)->device_id, - outputAudioDeviceIOProc); - if (status) - GST_DEBUG ("AudioDeviceStop returned %d\n", (int) status); - break; - case GST_STATE_CHANGE_PAUSED_TO_READY: - break; - default: - break; +OSStatus +gst_osx_audio_sink_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); + + guint8 *readptr; + gint readseg; + gint len; + + if (gst_ring_buffer_prepare_read (GST_RING_BUFFER (buf), &readseg, &readptr, + &len)) { + outOutputData->mBuffers[0].mDataByteSize = len; + memcpy ((char *) outOutputData->mBuffers[0].mData, readptr, len); + + /* clear written samples */ + gst_ring_buffer_clear (GST_RING_BUFFER (buf), readseg); + + /* we wrote one segment */ + gst_ring_buffer_advance (GST_RING_BUFFER (buf), 1); } + return 0; +} + +static void +gst_osx_audio_sink_osxelement_init (gpointer g_iface, gpointer iface_data) +{ + GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface; - if (GST_ELEMENT_CLASS (parent_class)->change_state) - return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); + iface->io_proc = gst_osx_audio_sink_io_proc; +} - return GST_STATE_CHANGE_SUCCESS; +/* entry point to initialize the plug-in + * initialize the plug-in itself + * register the element factories and pad templates + * register the features + * + * exchange the string 'plugin' with your elemnt name + */ +static gboolean +plugin_init (GstPlugin * plugin) +{ + return gst_element_register (plugin, "osxaudiosink", + GST_RANK_NONE, GST_TYPE_OSX_AUDIO_SINK); } + +/* this is the structure that gstreamer looks for to register plugins + * + * exchange the strings 'plugin' and 'Template plugin' with you plugin name and + * description + */ +GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, + GST_VERSION_MINOR, + "osxaudio", + "OSX Audio plugin", + plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/") |