1 /* -*- Mode: C; c-basic-offset: 4 -*- */
3 Copyright (C) 2002, 2003 Andy Wingo <wingo@pobox.com>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public
16 License along with this library; if not, write to the Free
17 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 static GstBinClass *parent_class = NULL;
32 static void gst_jack_bin_init (GstJackBin * this);
33 static void gst_jack_bin_class_init (GstJackBinClass * klass);
35 static GstStateChangeReturn gst_jack_bin_change_state (GstElement * element,
36 GstStateChange transition);
39 static int process (jack_nframes_t nframes, void *arg);
40 static int sample_rate (jack_nframes_t nframes, void *arg);
41 static int buffer_size (jack_nframes_t nframes, void *arg);
42 static void shutdown (void *arg);
44 static void sighup_handler (int sig);
45 static GstJackBin *_jackbin = NULL;
46 static gboolean watchdog_please_set_the_jackbin_to_ready = FALSE;
48 /* fixme: we need a watchdog thread to see if we have received a SIGHUP, and if
49 * so set the state of the bin to READY. */
52 gst_jack_bin_get_type (void)
54 static GType jack_bin_type = 0;
57 static const GTypeInfo jack_bin_info = {
58 sizeof (GstJackBinClass),
61 (GClassInitFunc) gst_jack_bin_class_init,
66 (GInstanceInitFunc) gst_jack_bin_init,
70 g_type_register_static (GST_TYPE_BIN, "GstJackBin", &jack_bin_info, 0);
76 gst_jack_bin_class_init (GstJackBinClass * klass)
78 GObjectClass *object_class;
79 GstElementClass *element_class;
81 object_class = (GObjectClass *) klass;
82 element_class = (GstElementClass *) klass;
84 parent_class = g_type_class_peek_parent (klass);
86 element_class->change_state = gst_jack_bin_change_state;
90 gst_jack_bin_init (GstJackBin * this)
92 GST_DEBUG ("initializing jack bin");
94 /* jack bins are managing bins and iterate themselves */
95 GST_OBJECT_FLAG_SET (this, GST_BIN_FLAG_MANAGER);
96 GST_OBJECT_FLAG_SET (this, GST_BIN_SELF_SCHEDULABLE);
98 /* make a new scheduler and associate it with the bin */
99 gst_scheduler_factory_make (NULL, GST_ELEMENT (this));
102 static GstStateChangeReturn
103 gst_jack_bin_change_state (GstElement * element, GstStateChange transition)
109 g_return_val_if_fail (element != NULL, FALSE);
110 this = GST_JACK_BIN (element);
112 switch (GST_STATE_PENDING (element)) {
114 JACK_DEBUG ("jackbin: NULL state");
116 JACK_DEBUG ("jackbin: closing client");
117 jack_client_close (this->client);
122 signal (SIGHUP, SIG_DFL);
125 if (GST_ELEMENT_CLASS (parent_class)->change_state)
126 return GST_ELEMENT_CLASS (parent_class)->change_state (element,
130 case GST_STATE_READY:
131 JACK_DEBUG ("jackbin: READY");
134 signal (SIGHUP, sighup_handler);
137 if (!(this->client = jack_client_new ("gst-jack"))) {
138 g_warning ("jack server not running?");
139 return GST_STATE_CHANGE_FAILURE;
142 gst_scheduler_setup (GST_ELEMENT_SCHED (this));
144 jack_set_process_callback (this->client, process, this);
145 jack_set_sample_rate_callback (this->client, sample_rate, this);
146 jack_set_buffer_size_callback (this->client, buffer_size, this);
147 this->nframes = jack_get_buffer_size (this->client);
148 jack_on_shutdown (this->client, shutdown, this);
151 if (GST_OBJECT_FLAG_IS_SET (GST_OBJECT (this), GST_JACK_OPEN)) {
154 JACK_DEBUG ("jackbin: unregistering pad %s:%s",
155 GST_JACK_PAD (l)->name, GST_JACK_PAD (l)->peer_name);
156 jack_port_unregister (this->client, GST_JACK_PAD (l)->port);
161 JACK_DEBUG ("jackbin: unregistering pad %s:%s",
162 GST_JACK_PAD (l)->name, GST_JACK_PAD (l)->peer_name);
163 jack_port_unregister (this->client, GST_JACK_PAD (l)->port);
166 GST_OBJECT_FLAG_UNSET (GST_OBJECT (this), GST_JACK_OPEN);
168 if (GST_OBJECT_FLAG_IS_SET (GST_OBJECT (this), GST_JACK_ACTIVE)) {
169 JACK_DEBUG ("jackbin: deactivating client");
170 jack_deactivate (this->client);
171 GST_OBJECT_FLAG_UNSET (GST_OBJECT (this), GST_JACK_ACTIVE);
175 if (GST_ELEMENT_CLASS (parent_class)->change_state)
176 return GST_ELEMENT_CLASS (parent_class)->change_state (element,
180 case GST_STATE_PAUSED:
181 JACK_DEBUG ("jackbin: PAUSED");
183 if (!GST_OBJECT_FLAG_IS_SET (GST_OBJECT (this), GST_JACK_OPEN)) {
186 pad = GST_JACK_PAD (l);
187 JACK_DEBUG ("jackbin: registering input port %s (peer %s)", pad->name,
190 jack_port_register (this->client, pad->name,
191 JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput | JackPortIsTerminal, 0);
196 pad = GST_JACK_PAD (l);
197 JACK_DEBUG ("jackbin: registering output port %s (peer %s)",
198 pad->name, pad->peer_name);
200 jack_port_register (this->client, pad->name,
201 JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput | JackPortIsTerminal,
206 /* must activate before connecting */
207 if (!GST_OBJECT_FLAG_IS_SET (GST_OBJECT (this), GST_JACK_ACTIVE)) {
208 JACK_DEBUG ("jackbin: activating client");
209 jack_activate (this->client);
210 GST_OBJECT_FLAG_SET (GST_OBJECT (this), GST_JACK_ACTIVE);
215 pad = GST_JACK_PAD (l);
216 JACK_DEBUG ("connecting jack port %s to gst jack port %s",
217 pad->peer_name, jack_port_name (pad->port));
218 if (jack_connect (this->client, pad->peer_name,
219 jack_port_name (pad->port))) {
220 g_warning ("jackbin: could not connect %s and %s", pad->peer_name,
221 jack_port_name (pad->port));
222 return GST_STATE_CHANGE_FAILURE;
228 pad = GST_JACK_PAD (l);
229 JACK_DEBUG ("connecting gst jack port %s to jack port %s",
230 jack_port_name (pad->port), pad->peer_name);
231 if (jack_connect (this->client, jack_port_name (pad->port),
233 g_warning ("jackbin: could not connect %s and %s", pad->peer_name,
234 jack_port_name (pad->port));
235 return GST_STATE_CHANGE_FAILURE;
240 JACK_DEBUG ("jackbin: setting OPEN flag");
241 GST_OBJECT_FLAG_SET (GST_OBJECT (this), GST_JACK_OPEN);
243 if (GST_ELEMENT_CLASS (parent_class)->change_state)
244 return GST_ELEMENT_CLASS (parent_class)->change_state (element,
247 if (GST_ELEMENT_CLASS (parent_class)->change_state)
248 return GST_ELEMENT_CLASS (parent_class)->change_state (element,
253 case GST_STATE_PLAYING:
254 JACK_DEBUG ("jackbin: PLAYING");
256 if (GST_ELEMENT_CLASS (parent_class)->change_state)
257 return GST_ELEMENT_CLASS (parent_class)->change_state (element,
262 JACK_DEBUG ("jackbin: state change finished");
264 return GST_STATE_CHANGE_SUCCESS;
269 /* keep in mind that these run in another thread, mm-kay? */
272 process (jack_nframes_t nframes, void *arg)
274 GstJackBin *bin = (GstJackBin *) arg;
280 JACK_DEBUG ("jackbin: process()");
282 if (GST_STATE (bin) != GST_STATE_PLAYING) {
283 JACK_DEBUG ("jackbin: bin is not PLAYING yet, returning");
286 JACK_DEBUG ("jackbin: we are PLAYING, let's process()");
291 pad = GST_JACK_PAD (l);
292 pad->data = jack_port_get_buffer (pad->port, nframes);
298 pad = GST_JACK_PAD (l);
299 pad->data = jack_port_get_buffer (pad->port, nframes);
303 bin->nframes = nframes;
305 JACK_DEBUG ("jackbin: iterating to process %ld frames of audio...", nframes);
306 if (!gst_bin_iterate (GST_BIN (bin))) {
307 g_warning ("bin failed to iterate");
311 /* that's all folks */
317 sample_rate (jack_nframes_t nframes, void *arg)
319 GstJackBin *bin = (GstJackBin *) arg;
321 JACK_DEBUG ("the sample rate is now %lu/sec\n", nframes);
327 buffer_size (jack_nframes_t nframes, void *arg)
329 GstJackBin *bin = (GstJackBin *) arg;
331 JACK_DEBUG ("the buffer size is now %lu\n", nframes);
332 bin->nframes = nframes;
339 /* GstJackClient *client = (GstJackClient*) arg; */
340 printf ("shutdown %p\n", arg);
341 /* gst_element_set_state (GST_ELEMENT (client->manager), GST_STATE_READY); */
345 sighup_handler (int sig)
347 g_message ("got sighup, setting state to READY");
348 watchdog_please_set_the_jackbin_to_ready = TRUE;