959f58673afe521fd4d9e76404ff6f051974c6bd
[platform/upstream/gst-plugins-good.git] / ext / jack / gstjackbin.c
1 /* -*- Mode: C; c-basic-offset: 4 -*- */
2 /*
3     Copyright (C) 2002 Andy Wingo <wingo@pobox.com>
4
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.
9
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.
14
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.
18 */
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <signal.h>
23 #include "gstjack.h"
24
25
26 static GstBinClass *parent_class = NULL;
27
28 static void gst_jack_bin_init(GstJackBin *this);
29 static void gst_jack_bin_class_init(GstJackBinClass *klass);
30
31 static GstElementStateReturn gst_jack_bin_change_state(GstElement *element);
32
33 /* jack callbacks */
34 static int process (jack_nframes_t nframes, void *arg);
35 static int sample_rate (jack_nframes_t nframes, void *arg);
36 static void shutdown (void *arg);
37
38 static void sighup_handler (int sig);
39 static GstJackBin *_jackbin = NULL;
40 static gboolean watchdog_please_set_the_jackbin_to_ready = FALSE;
41
42 /* fixme: we need a watchdog thread to see if we have received a SIGHUP, and if
43  * so set the state of the bin to READY. */
44
45 GType
46 gst_jack_bin_get_type (void) 
47 {
48     static GType jack_bin_type = 0;
49 if (!jack_bin_type) {
50         static const GTypeInfo jack_bin_info = {
51             sizeof(GstJackBinClass),
52             NULL,
53             NULL,
54             (GClassInitFunc)gst_jack_bin_class_init,
55             NULL,
56             NULL,
57             sizeof(GstJackBin),
58             0,
59             (GInstanceInitFunc)gst_jack_bin_init,
60         };
61         jack_bin_type = g_type_register_static (GST_TYPE_BIN, "GstJackBin", &jack_bin_info, 0);
62     }
63     return jack_bin_type;
64 }
65
66 static void
67 gst_jack_bin_class_init(GstJackBinClass *klass)
68 {
69     GObjectClass *object_class;
70     GstElementClass *element_class;
71     
72     object_class = (GObjectClass *)klass;
73     element_class = (GstElementClass *)klass;
74     
75     parent_class = g_type_class_ref(GST_TYPE_BIN);
76
77     element_class->change_state = gst_jack_bin_change_state;
78 }
79
80 static void
81 gst_jack_bin_init(GstJackBin *this)
82 {
83     GST_DEBUG ("initializing jack bin");
84     
85     /* jack bins are managing bins and iterate themselves */
86     GST_FLAG_SET (this, GST_BIN_FLAG_MANAGER);
87     GST_FLAG_SET (this, GST_BIN_SELF_SCHEDULABLE);
88     
89     /* make a new scheduler and associate it with the bin */
90     gst_scheduler_factory_make (NULL, GST_ELEMENT (this));
91 }
92
93 static GstElementStateReturn
94 gst_jack_bin_change_state (GstElement *element)
95 {
96     GstJackBin *this;
97     GList *l = NULL;
98     GstJackPad *pad;
99     
100     g_return_val_if_fail (element != NULL, FALSE);
101     this = GST_JACK_BIN (element);
102     
103     switch (GST_STATE_PENDING (element)) {
104     case GST_STATE_NULL:
105         JACK_DEBUG ("jackbin: NULL state");
106         if (this->client) {
107             JACK_DEBUG ("jackbin: closing client");
108             jack_client_close (this->client);
109             this->client = NULL;
110         }
111         
112         if (_jackbin)
113             signal (SIGHUP, SIG_DFL);
114         _jackbin = NULL;
115
116         if (GST_ELEMENT_CLASS (parent_class)->change_state)
117             return GST_ELEMENT_CLASS (parent_class)->change_state (element);
118         break;
119         
120     case GST_STATE_READY:
121         JACK_DEBUG ("jackbin: READY");
122
123         _jackbin = this;
124         signal (SIGHUP, sighup_handler);
125
126         if (!this->client) {
127           if (!(this->client = jack_client_new ("gst-jack"))) {
128             g_warning ("jack server not running?");
129             return GST_STATE_FAILURE;
130           }
131                 
132           gst_scheduler_setup (GST_ELEMENT_SCHED (this));
133
134           jack_set_process_callback (this->client, process, this);
135           jack_set_sample_rate_callback (this->client, sample_rate, this);
136           jack_on_shutdown (this->client, shutdown, this);
137         }
138         
139         if (GST_FLAG_IS_SET (GST_OBJECT (this), GST_JACK_OPEN)) {
140             l = this->src_pads;
141             while (l) {
142                 JACK_DEBUG ("jackbin: unregistering pad %s:%s", GST_JACK_PAD (l)->name, GST_JACK_PAD (l)->peer_name);
143                 jack_port_unregister (this->client, GST_JACK_PAD (l)->port);
144                 l = g_list_next (l);
145             }
146             l = this->sink_pads;
147             while (l) {
148                 JACK_DEBUG ("jackbin: unregistering pad %s:%s", GST_JACK_PAD (l)->name, GST_JACK_PAD (l)->peer_name);
149                 jack_port_unregister (this->client, GST_JACK_PAD (l)->port);
150                 l = g_list_next (l);
151             }
152             GST_FLAG_UNSET (GST_OBJECT (this), GST_JACK_OPEN);
153
154             if (GST_FLAG_IS_SET (GST_OBJECT (this), GST_JACK_ACTIVE)) {
155                 JACK_DEBUG ("jackbin: deactivating client");
156                 jack_deactivate (this->client);
157                 GST_FLAG_UNSET (GST_OBJECT (this), GST_JACK_ACTIVE);
158             }
159         }
160             
161         if (GST_ELEMENT_CLASS (parent_class)->change_state)
162             return GST_ELEMENT_CLASS (parent_class)->change_state (element);
163         break;
164         
165     case GST_STATE_PAUSED:
166         JACK_DEBUG ("jackbin: PAUSED");
167         
168         if (!GST_FLAG_IS_SET (GST_OBJECT (this), GST_JACK_OPEN)) {
169             l = this->src_pads;
170             while (l) {
171                 pad = GST_JACK_PAD (l);
172                 JACK_DEBUG ("jackbin: registering input port %s (peer %s)", pad->name, pad->peer_name);
173                 pad->port = jack_port_register (this->client, pad->name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput|JackPortIsTerminal, 0);
174                 l = g_list_next (l);
175             }
176             l = this->sink_pads;
177             while (l) {
178                 pad = GST_JACK_PAD (l);
179                 JACK_DEBUG ("jackbin: registering output port %s (peer %s)", pad->name, pad->peer_name);
180                 pad->port = jack_port_register (this->client, pad->name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput|JackPortIsTerminal, 0);
181                 l = g_list_next (l);
182             }
183
184             /* must activate before connecting */
185             if (!GST_FLAG_IS_SET (GST_OBJECT (this), GST_JACK_ACTIVE)) {
186                 JACK_DEBUG ("jackbin: activating client");
187                 jack_activate (this->client);
188                 GST_FLAG_SET (GST_OBJECT (this), GST_JACK_ACTIVE);
189             }
190
191             l = this->src_pads;
192             while (l) {
193                 pad = GST_JACK_PAD (l);
194                 JACK_DEBUG ("connecting jack port %s to gst jack port %s", pad->peer_name, jack_port_name (pad->port));
195                 if (jack_connect (this->client, pad->peer_name, jack_port_name (pad->port))) {
196                     g_warning ("jackbin: could not connect %s and %s", pad->peer_name, jack_port_name (pad->port));
197                     return GST_STATE_FAILURE;
198                 }
199                 l = g_list_next (l);
200             }
201             l = this->sink_pads;
202             while (l) {
203                 pad = GST_JACK_PAD (l);
204                 JACK_DEBUG ("connecting gst jack port %s to jack port %s", jack_port_name (pad->port), pad->peer_name);
205                 if (jack_connect (this->client, jack_port_name (pad->port), pad->peer_name)) {
206                     g_warning ("jackbin: could not connect %s and %s", pad->peer_name, jack_port_name (pad->port));
207                     return GST_STATE_FAILURE;
208                 }
209                 l = g_list_next (l);
210             }
211
212             JACK_DEBUG ("jackbin: setting OPEN flag");
213             GST_FLAG_SET (GST_OBJECT (this), GST_JACK_OPEN);
214
215             if (GST_ELEMENT_CLASS (parent_class)->change_state)
216                 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
217         } else {
218             if (GST_ELEMENT_CLASS (parent_class)->change_state)
219                 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
220         }
221
222         break;
223     case GST_STATE_PLAYING:
224         JACK_DEBUG ("jackbin: PLAYING");
225
226         if (GST_ELEMENT_CLASS (parent_class)->change_state)
227             return GST_ELEMENT_CLASS (parent_class)->change_state (element);
228         break;
229     }
230     
231     JACK_DEBUG ("jackbin: state change finished");
232     
233     return GST_STATE_SUCCESS;
234 }
235
236 /* jack callbacks */
237
238 /* keep in mind that these run in another thread, mm-kay? */
239
240 static int
241 process (jack_nframes_t nframes, void *arg)
242 {
243     GstJackBin *bin = (GstJackBin*) arg;
244     GstJackPad *pad;
245     GList *l;
246     
247     g_assert (bin);
248     
249     JACK_DEBUG ("jackbin: process()");
250
251     if (GST_STATE (bin) != GST_STATE_PLAYING) {
252         JACK_DEBUG ("jackbin: bin is not PLAYING yet, returning");
253         return 0;
254     } else {
255         JACK_DEBUG ("jackbin: we are PLAYING, let's process()");
256     }
257
258     l = bin->src_pads;
259     while (l) {
260         pad = GST_JACK_PAD (l);
261         pad->data = jack_port_get_buffer (pad->port, nframes);
262         l = g_list_next (l);
263     }
264     
265     l = bin->sink_pads;
266     while (l) {
267         pad = GST_JACK_PAD (l);
268         pad->data = jack_port_get_buffer (pad->port, nframes);
269         l = g_list_next (l);
270     }
271     
272     bin->nframes = nframes;
273     
274     JACK_DEBUG ("jackbin: iterating to process %ld frames of audio...", nframes);
275     if (!gst_bin_iterate (GST_BIN_CAST (bin))) {
276         g_warning ("bin failed to iterate");
277         return -1;
278     }
279     
280     /* that's all folks */
281     
282     return 0;      
283 }
284
285 static int
286 sample_rate (jack_nframes_t nframes, void *arg)
287 {
288     GstJackBin *bin = (GstJackBin*) arg;
289     JACK_DEBUG ("the sample rate is now %lu/sec\n", nframes);
290     bin->rate = nframes;
291     return 0;
292 }
293
294 static void
295 shutdown (void *arg)
296 {
297 /*    GstJackClient *client = (GstJackClient*) arg; */
298     printf ("shutdown %p\n", arg);
299 /*    gst_element_set_state (GST_ELEMENT (client->manager), GST_STATE_READY); */
300 }
301
302 static void sighup_handler (int sig)
303 {
304     g_message ("got sighup, setting state to READY");
305     watchdog_please_set_the_jackbin_to_ready = TRUE;
306 }