2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
31 #include <CoreAudio/CoreAudio.h>
33 #include <gstosxaudiosrc.h>
34 #include <gstosxaudioelement.h>
36 /* elementfactory information */
37 static GstElementDetails gst_osxaudiosrc_details =
38 GST_ELEMENT_DETAILS ("Audio Source (Mac OS X)",
40 "Read from the sound card",
41 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
44 /* Osxaudiosrc signals and args */
51 static GstStaticPadTemplate osxaudiosrc_src_factory =
52 GST_STATIC_PAD_TEMPLATE ("src",
55 GST_STATIC_CAPS ("audio/x-raw-float, "
56 "endianness = (int) BYTE_ORDER, "
57 "signed = (boolean) TRUE , "
58 "width = (int) 32, " "rate = (int) 44100, " "channels = (int) 2")
61 static void gst_osxaudiosrc_base_init (gpointer g_class);
62 static void gst_osxaudiosrc_class_init (GstOsxAudioSrcClass * klass);
63 static void gst_osxaudiosrc_init (GstOsxAudioSrc * osxaudiosrc);
64 static void gst_osxaudiosrc_dispose (GObject * object);
66 static GstStateChangeReturn gst_osxaudiosrc_change_state (GstElement * element);
68 static GstData *gst_osxaudiosrc_get (GstPad * pad);
70 static GstElementClass *parent_class = NULL;
72 /*static guint gst_osxaudiosrc_signals[LAST_SIGNAL] = { 0 }; */
75 gst_osxaudiosrc_get_type (void)
77 static GType osxaudiosrc_type = 0;
79 if (!osxaudiosrc_type) {
80 static const GTypeInfo osxaudiosrc_info = {
81 sizeof (GstOsxAudioSrcClass),
82 gst_osxaudiosrc_base_init,
84 (GClassInitFunc) gst_osxaudiosrc_class_init,
87 sizeof (GstOsxAudioSrc),
89 (GInstanceInitFunc) gst_osxaudiosrc_init,
93 g_type_register_static (GST_TYPE_OSXAUDIOELEMENT, "GstOsxAudioSrc",
94 &osxaudiosrc_info, 0);
96 return osxaudiosrc_type;
100 gst_osxaudiosrc_base_init (gpointer g_class)
102 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
104 gst_element_class_set_details (element_class, &gst_osxaudiosrc_details);
105 gst_element_class_add_pad_template (element_class,
106 gst_static_pad_template_get (&osxaudiosrc_src_factory));
109 gst_osxaudiosrc_class_init (GstOsxAudioSrcClass * klass)
111 GObjectClass *gobject_class;
112 GstElementClass *gstelement_class;
114 gobject_class = (GObjectClass *) klass;
115 gstelement_class = (GstElementClass *) klass;
117 parent_class = g_type_class_ref (GST_TYPE_OSXAUDIOELEMENT);
119 gobject_class->dispose = gst_osxaudiosrc_dispose;
121 gstelement_class->change_state = gst_osxaudiosrc_change_state;
126 gst_osxaudiosrc_init (GstOsxAudioSrc * osxaudiosrc)
128 osxaudiosrc->srcpad =
129 gst_pad_new_from_template (gst_static_pad_template_get
130 (&osxaudiosrc_src_factory), "src");
131 gst_pad_set_get_function (osxaudiosrc->srcpad, gst_osxaudiosrc_get);
133 gst_element_add_pad (GST_ELEMENT (osxaudiosrc), osxaudiosrc->srcpad);
138 gst_osxaudiosrc_dispose (GObject * object)
140 /* GstOsxAudioSrc *osxaudiosrc = (GstOsxAudioSrc *) object; */
142 G_OBJECT_CLASS (parent_class)->dispose (object);
146 gst_osxaudiosrc_get (GstPad * pad)
152 src = GST_OSXAUDIOSRC (gst_pad_get_parent (pad));
154 buf = gst_buffer_new_and_alloc ((GST_OSXAUDIOELEMENT (src))->buffer_len);
157 read_buffer (GST_OSXAUDIOELEMENT (src), (char *) GST_BUFFER_DATA (buf));
160 gst_buffer_unref (buf);
161 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
162 gst_object_unref (src);
163 return GST_DATA (gst_event_new (GST_EVENT_INTERRUPT));
166 if (readbytes == 0) {
167 gst_buffer_unref (buf);
168 gst_object_unref (src);
169 return GST_DATA (gst_event_new (GST_EVENT_INTERRUPT));
172 GST_BUFFER_SIZE (buf) = readbytes;
173 GST_BUFFER_OFFSET (buf) = src->curoffset;
175 src->curoffset += readbytes;
177 GST_DEBUG ("pushed buffer from soundcard of %ld bytes", readbytes);
179 gst_object_unref (src);
181 return GST_DATA (buf);
184 static GstStateChangeReturn
185 gst_osxaudiosrc_change_state (GstElement * element, GstStateChange transition)
187 GstOsxAudioSrc *osxaudiosrc = GST_OSXAUDIOSRC (element);
190 GST_DEBUG ("osxaudiosrc: state change");
192 switch (transition) {
193 case GST_STATE_CHANGE_READY_TO_PAUSED:
194 osxaudiosrc->curoffset = 0;
196 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
198 AudioDeviceStart (GST_OSXAUDIOELEMENT (osxaudiosrc)->device_id,
199 inputAudioDeviceIOProc);
201 GST_DEBUG ("AudioDeviceStart returned %d\n", (int) status);
203 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
205 AudioDeviceStop (GST_OSXAUDIOELEMENT (osxaudiosrc)->device_id,
206 inputAudioDeviceIOProc);
208 GST_DEBUG ("AudioDeviceStop returned %d\n", (int) status);
210 case GST_STATE_CHANGE_PAUSED_TO_READY:
216 if (GST_ELEMENT_CLASS (parent_class)->change_state)
217 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
219 return GST_STATE_CHANGE_SUCCESS;