close #333784 unref the result of gst_pad_get_parent() by: Christophe Fergeau.
[platform/upstream/gstreamer.git] / sys / osxaudio / gstosxaudiosink.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wim.taymans@chello.be>
4  *
5  * gstosxaudiosink.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 <CoreAudio/CoreAudio.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <string.h>
30
31 #include "gstosxaudiosink.h"
32
33 /* elementfactory information */
34 static GstElementDetails gst_osxaudiosink_details =
35 GST_ELEMENT_DETAILS ("Audio Sink (Mac OS X)",
36     "Sink/Audio",
37     "Output to a Mac OS X CoreAudio Sound Device",
38     "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
39
40 static void gst_osxaudiosink_base_init (gpointer g_class);
41 static void gst_osxaudiosink_class_init (GstOsxAudioSinkClass * klass);
42 static void gst_osxaudiosink_init (GstOsxAudioSink * osxaudiosink);
43 static void gst_osxaudiosink_dispose (GObject * object);
44
45 static GstStateChangeReturn gst_osxaudiosink_change_state (GstElement *
46     element);
47
48 static void gst_osxaudiosink_chain (GstPad * pad, GstData * _data);
49
50 /* OssSink signals and args */
51 enum
52 {
53   SIGNAL_HANDOFF,
54   LAST_SIGNAL
55 };
56
57 static GstStaticPadTemplate osxaudiosink_sink_factory =
58 GST_STATIC_PAD_TEMPLATE ("sink",
59     GST_PAD_SINK,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS ("audio/x-raw-float, "
62         "endianness = (int) BYTE_ORDER, "
63         "signed = (boolean) TRUE, "
64         "width = (int) 32, " "rate = (int) 44100, " "channels = (int) 2")
65     );
66
67 static GstElementClass *parent_class = NULL;
68 static guint gst_osssink_signals[LAST_SIGNAL] = { 0 };
69
70 GType
71 gst_osxaudiosink_get_type (void)
72 {
73   static GType osxaudiosink_type = 0;
74
75   if (!osxaudiosink_type) {
76     static const GTypeInfo osxaudiosink_info = {
77       sizeof (GstOsxAudioSinkClass),
78       gst_osxaudiosink_base_init,
79       NULL,
80       (GClassInitFunc) gst_osxaudiosink_class_init,
81       NULL,
82       NULL,
83       sizeof (GstOsxAudioSink),
84       0,
85       (GInstanceInitFunc) gst_osxaudiosink_init,
86     };
87
88     osxaudiosink_type =
89         g_type_register_static (GST_TYPE_OSXAUDIOELEMENT, "GstOsxAudioSink",
90         &osxaudiosink_info, 0);
91   }
92
93   return osxaudiosink_type;
94 }
95
96 static void
97 gst_osxaudiosink_dispose (GObject * object)
98 {
99   /* GstOsxAudioSink *osxaudiosink = (GstOsxAudioSink *) object; */
100
101   /*gst_object_unparent (GST_OBJECT (osxaudiosink->provided_clock)); */
102
103   G_OBJECT_CLASS (parent_class)->dispose (object);
104 }
105
106 static void
107 gst_osxaudiosink_base_init (gpointer g_class)
108 {
109   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
110
111   gst_element_class_set_details (element_class, &gst_osxaudiosink_details);
112   gst_element_class_add_pad_template (element_class,
113       gst_static_pad_template_get (&osxaudiosink_sink_factory));
114 }
115 static void
116 gst_osxaudiosink_class_init (GstOsxAudioSinkClass * klass)
117 {
118   GObjectClass *gobject_class;
119   GstElementClass *gstelement_class;
120
121   gobject_class = (GObjectClass *) klass;
122   gstelement_class = (GstElementClass *) klass;
123
124   parent_class = g_type_class_ref (GST_TYPE_OSXAUDIOELEMENT);
125
126   gst_osssink_signals[SIGNAL_HANDOFF] =
127       g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
128       G_STRUCT_OFFSET (GstOsxAudioSinkClass, handoff), NULL, NULL,
129       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
130
131   gobject_class->dispose = gst_osxaudiosink_dispose;
132
133   gstelement_class->change_state =
134       GST_DEBUG_FUNCPTR (gst_osxaudiosink_change_state);
135 }
136
137 static void
138 gst_osxaudiosink_init (GstOsxAudioSink * osxaudiosink)
139 {
140   osxaudiosink->sinkpad =
141       gst_pad_new_from_template (gst_static_pad_template_get
142       (&osxaudiosink_sink_factory), "sink");
143   gst_element_add_pad (GST_ELEMENT (osxaudiosink), osxaudiosink->sinkpad);
144
145   gst_pad_set_chain_function (osxaudiosink->sinkpad, gst_osxaudiosink_chain);
146
147   GST_DEBUG ("initializing osxaudiosink");
148
149   GST_OBJECT_FLAG_SET (osxaudiosink, GST_ELEMENT_THREAD_SUGGESTED);
150   GST_OBJECT_FLAG_SET (osxaudiosink, GST_ELEMENT_EVENT_AWARE);
151 }
152
153 static void
154 gst_osxaudiosink_chain (GstPad * pad, GstData * _data)
155 {
156   GstBuffer *buf = GST_BUFFER (_data);
157   GstOsxAudioSink *osxaudiosink;
158   guchar *data;
159   guint to_write;
160   gint amount_written;
161
162   /* this has to be an audio buffer */
163   osxaudiosink = GST_OSXAUDIOSINK (gst_pad_get_parent (pad));
164
165   if (GST_IS_EVENT (buf)) {
166     GstEvent *event = GST_EVENT (buf);
167
168     switch (GST_EVENT_TYPE (event)) {
169       case GST_EVENT_EOS:
170         gst_pad_event_default (pad, event);
171         gst_object_unref (osxaudiosink);
172         return;
173       case GST_EVENT_DISCONTINUOUS:
174         /* pass-through */
175       default:
176         gst_object_unref (osxaudiosink);
177         gst_pad_event_default (pad, event);
178         return;
179     }
180     g_assert_not_reached ();
181   }
182
183   data = GST_BUFFER_DATA (buf);
184   to_write = GST_BUFFER_SIZE (buf);
185   amount_written = 0;
186
187   while (amount_written < to_write) {
188     data += amount_written;
189     to_write -= amount_written;
190     amount_written =
191         write_buffer (GST_OSXAUDIOELEMENT (osxaudiosink), data, to_write);
192   }
193   gst_buffer_unref (buf);
194   gst_object_unref (osxaudiosink);
195 }
196
197 static GstStateChangeReturn
198 gst_osxaudiosink_change_state (GstElement * element, GstStateChange transition)
199 {
200   GstOsxAudioSink *osxaudiosink;
201   OSErr status;
202
203   osxaudiosink = GST_OSXAUDIOSINK (element);
204
205   switch (transition) {
206     case GST_STATE_CHANGE_READY_TO_PAUSED:
207       break;
208     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
209       status =
210           AudioDeviceStart (GST_OSXAUDIOELEMENT (osxaudiosink)->device_id,
211           outputAudioDeviceIOProc);
212       if (status)
213         GST_DEBUG ("AudioDeviceStart returned %d\n", (int) status);
214       break;
215     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
216       status =
217           AudioDeviceStop (GST_OSXAUDIOELEMENT (osxaudiosink)->device_id,
218           outputAudioDeviceIOProc);
219       if (status)
220         GST_DEBUG ("AudioDeviceStop returned %d\n", (int) status);
221       break;
222     case GST_STATE_CHANGE_PAUSED_TO_READY:
223       break;
224     default:
225       break;
226   }
227
228   if (GST_ELEMENT_CLASS (parent_class)->change_state)
229     return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
230
231   return GST_STATE_CHANGE_SUCCESS;
232 }