close #333784 unref the result of gst_pad_get_parent() by: Christophe Fergeau.
[platform/upstream/gstreamer.git] / sys / osxaudio / gstosxaudiosrc.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstosxaudiosrc.c: 
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <string.h>
30
31 #include <CoreAudio/CoreAudio.h>
32
33 #include <gstosxaudiosrc.h>
34 #include <gstosxaudioelement.h>
35
36 /* elementfactory information */
37 static GstElementDetails gst_osxaudiosrc_details =
38 GST_ELEMENT_DETAILS ("Audio Source (Mac OS X)",
39     "Source/Audio",
40     "Read from the sound card",
41     "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
42
43
44 /* Osxaudiosrc signals and args */
45 enum
46 {
47   /* FILL ME */
48   LAST_SIGNAL
49 };
50
51 static GstStaticPadTemplate osxaudiosrc_src_factory =
52 GST_STATIC_PAD_TEMPLATE ("src",
53     GST_PAD_SRC,
54     GST_PAD_ALWAYS,
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")
59     );
60
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);
65
66 static GstStateChangeReturn gst_osxaudiosrc_change_state (GstElement * element);
67
68 static GstData *gst_osxaudiosrc_get (GstPad * pad);
69
70 static GstElementClass *parent_class = NULL;
71
72 /*static guint gst_osxaudiosrc_signals[LAST_SIGNAL] = { 0 }; */
73
74 GType
75 gst_osxaudiosrc_get_type (void)
76 {
77   static GType osxaudiosrc_type = 0;
78
79   if (!osxaudiosrc_type) {
80     static const GTypeInfo osxaudiosrc_info = {
81       sizeof (GstOsxAudioSrcClass),
82       gst_osxaudiosrc_base_init,
83       NULL,
84       (GClassInitFunc) gst_osxaudiosrc_class_init,
85       NULL,
86       NULL,
87       sizeof (GstOsxAudioSrc),
88       0,
89       (GInstanceInitFunc) gst_osxaudiosrc_init,
90     };
91
92     osxaudiosrc_type =
93         g_type_register_static (GST_TYPE_OSXAUDIOELEMENT, "GstOsxAudioSrc",
94         &osxaudiosrc_info, 0);
95   }
96   return osxaudiosrc_type;
97 }
98
99 static void
100 gst_osxaudiosrc_base_init (gpointer g_class)
101 {
102   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
103
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));
107 }
108 static void
109 gst_osxaudiosrc_class_init (GstOsxAudioSrcClass * klass)
110 {
111   GObjectClass *gobject_class;
112   GstElementClass *gstelement_class;
113
114   gobject_class = (GObjectClass *) klass;
115   gstelement_class = (GstElementClass *) klass;
116
117   parent_class = g_type_class_ref (GST_TYPE_OSXAUDIOELEMENT);
118
119   gobject_class->dispose = gst_osxaudiosrc_dispose;
120
121   gstelement_class->change_state = gst_osxaudiosrc_change_state;
122
123 }
124
125 static void
126 gst_osxaudiosrc_init (GstOsxAudioSrc * osxaudiosrc)
127 {
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);
132
133   gst_element_add_pad (GST_ELEMENT (osxaudiosrc), osxaudiosrc->srcpad);
134
135 }
136
137 static void
138 gst_osxaudiosrc_dispose (GObject * object)
139 {
140   /* GstOsxAudioSrc *osxaudiosrc = (GstOsxAudioSrc *) object; */
141
142   G_OBJECT_CLASS (parent_class)->dispose (object);
143 }
144
145 static GstData *
146 gst_osxaudiosrc_get (GstPad * pad)
147 {
148   GstOsxAudioSrc *src;
149   GstBuffer *buf;
150   glong readbytes;
151
152   src = GST_OSXAUDIOSRC (gst_pad_get_parent (pad));
153
154   buf = gst_buffer_new_and_alloc ((GST_OSXAUDIOELEMENT (src))->buffer_len);
155
156   readbytes =
157       read_buffer (GST_OSXAUDIOELEMENT (src), (char *) GST_BUFFER_DATA (buf));
158
159   if (readbytes < 0) {
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));
164   }
165
166   if (readbytes == 0) {
167     gst_buffer_unref (buf);
168     gst_object_unref (src);
169     return GST_DATA (gst_event_new (GST_EVENT_INTERRUPT));
170   }
171
172   GST_BUFFER_SIZE (buf) = readbytes;
173   GST_BUFFER_OFFSET (buf) = src->curoffset;
174
175   src->curoffset += readbytes;
176
177   GST_DEBUG ("pushed buffer from soundcard of %ld bytes", readbytes);
178
179   gst_object_unref (src);
180
181   return GST_DATA (buf);
182 }
183
184 static GstStateChangeReturn
185 gst_osxaudiosrc_change_state (GstElement * element, GstStateChange transition)
186 {
187   GstOsxAudioSrc *osxaudiosrc = GST_OSXAUDIOSRC (element);
188   OSErr status;
189
190   GST_DEBUG ("osxaudiosrc: state change");
191
192   switch (transition) {
193     case GST_STATE_CHANGE_READY_TO_PAUSED:
194       osxaudiosrc->curoffset = 0;
195       break;
196     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
197       status =
198           AudioDeviceStart (GST_OSXAUDIOELEMENT (osxaudiosrc)->device_id,
199           inputAudioDeviceIOProc);
200       if (status)
201         GST_DEBUG ("AudioDeviceStart returned %d\n", (int) status);
202       break;
203     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
204       status =
205           AudioDeviceStop (GST_OSXAUDIOELEMENT (osxaudiosrc)->device_id,
206           inputAudioDeviceIOProc);
207       if (status)
208         GST_DEBUG ("AudioDeviceStop returned %d\n", (int) status);
209       break;
210     case GST_STATE_CHANGE_PAUSED_TO_READY:
211       break;
212     default:
213       break;
214   }
215
216   if (GST_ELEMENT_CLASS (parent_class)->change_state)
217     return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
218
219   return GST_STATE_CHANGE_SUCCESS;
220 }