3 * gst-aseq-appsrc: a GStreamer appsrc for the ALSA MIDI sequencer API
5 * Copyright (C) 2014 Antonio Ospite <ao2@ao2.it>
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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
23 /* Code _heavily_ inspired to aseqdump and amidicat:
24 * http://git.alsa-project.org/?p=alsa-utils.git;a=tree;f=seq/aseqdump
25 * http://krellan.com/amidicat/
29 #include <gst/app/gstappsrc.h>
33 #include <alsa/asoundlib.h>
34 #include <glib-unix.h>
36 #define DEFAULT_BUFSIZE 65536
37 #define DEFAULT_CLIENT_NAME "gst-aseq-appsrc"
38 #define DEFAULT_TICK_PERIOD_MS 10
39 #define DEFAULT_POLL_TIMEOUT_MS (DEFAULT_TICK_PERIOD_MS / 2)
41 GST_DEBUG_CATEGORY (mysource_debug);
42 #define GST_CAT_DEFAULT mysource_debug
44 typedef struct _App App;
55 snd_seq_addr_t *seq_ports;
56 snd_midi_event_t *parser;
57 unsigned char *buffer;
72 ret = snd_seq_open (&app->seq, "default", SND_SEQ_OPEN_DUPLEX, 0);
74 GST_ERROR ("Cannot open sequencer - %s", snd_strerror (ret));
78 ret = snd_seq_set_client_name (app->seq, DEFAULT_CLIENT_NAME);
80 GST_ERROR ("Cannot set client name - %s", snd_strerror (ret));
87 snd_seq_close (app->seq);
92 /* Parses one or more port addresses from the string */
94 parse_ports (const char *arg, App * app)
100 GST_DEBUG ("ports: %s", arg);
103 * Assume that ports are separated by commas.
105 * Commas are used instead of spaces because those are valid in client
108 ports_list = g_strsplit (arg, ",", 0);
110 app->port_count = g_strv_length (ports_list);
111 app->seq_ports = g_try_new (snd_seq_addr_t, app->port_count);
112 if (!app->seq_ports) {
113 GST_ERROR ("Out of memory");
115 goto out_free_ports_list;
118 for (i = 0; i < (guint)app->port_count; i++) {
119 gchar *port_name = ports_list[i];
121 ret = snd_seq_parse_address (app->seq, &app->seq_ports[i],
124 GST_ERROR ("Invalid port %s - %s", port_name,
126 goto error_free_seq_ports;
130 goto out_free_ports_list;
132 error_free_seq_ports:
133 g_free (app->seq_ports);
135 g_strfreev (ports_list);
140 create_port (App * app)
144 ret = snd_seq_create_simple_port (app->seq, DEFAULT_CLIENT_NAME,
145 SND_SEQ_PORT_CAP_WRITE |
146 SND_SEQ_PORT_CAP_SUBS_WRITE,
147 SND_SEQ_PORT_TYPE_MIDI_GENERIC | SND_SEQ_PORT_TYPE_APPLICATION);
149 GST_ERROR ("Cannot create port - %s", snd_strerror (ret));
155 connect_ports (App * app)
160 for (i = 0; i < app->port_count; ++i) {
162 snd_seq_connect_from (app->seq, 0, app->seq_ports[i].client,
163 app->seq_ports[i].port);
166 GST_WARNING ("Cannot connect from port %d:%d - %s",
167 app->seq_ports[i].client, app->seq_ports[i].port, snd_strerror (ret));
172 push_buffer (App * app, gpointer data, guint size)
179 /* read the next chunk */
180 buffer = gst_buffer_new ();
182 time = app->tick * DEFAULT_TICK_PERIOD_MS * GST_MSECOND;
184 GST_BUFFER_DTS (buffer) = time;
185 GST_BUFFER_PTS (buffer) = time;
186 GST_BUFFER_OFFSET (buffer) = time;
187 GST_BUFFER_DURATION (buffer) = DEFAULT_TICK_PERIOD_MS * GST_MSECOND;
189 local_data = g_memdup (data, size);
191 gst_buffer_append_memory (buffer,
192 gst_memory_new_wrapped (0, local_data, size, 0, size, local_data,
195 GST_DEBUG ("feed buffer %p, tick %" G_GUINT64_FORMAT " size: %u",
196 (gpointer) buffer, app->tick, size);
197 g_signal_emit_by_name (app->appsrc, "push-buffer", buffer, &ret);
198 gst_buffer_unref (buffer);
204 push_tick_buffer (App * app)
206 app->buffer[0] = 0xf9;
207 push_buffer (app, app->buffer, 1);
210 /* This method is called by the need-data signal callback, we feed data into the
214 feed_data (GstElement * appsrc, guint size, App * app)
220 snd_seq_poll_descriptors (app->seq, app->pfds, app->npfds, POLLIN);
221 ret = poll (app->pfds, app->npfds, DEFAULT_POLL_TIMEOUT_MS);
223 GST_ERROR ("ERROR in poll: %s", strerror (errno));
224 } else if (ret == 0) {
225 push_tick_buffer (app);
228 snd_seq_event_t *event;
229 err = snd_seq_event_input (app->seq, &event);
234 snd_midi_event_decode (app->parser, app->buffer, DEFAULT_BUFSIZE,
237 /* ENOENT indicates an event that is not a MIDI message, silently skip it */
238 if (-ENOENT == size_ev) {
239 GST_WARNING ("Warning: Received non-MIDI message");
240 push_tick_buffer (app);
242 GST_ERROR ("Error decoding event from ALSA to output: %s",
243 strerror (-size_ev));
247 push_buffer (app, app->buffer, size_ev);
256 /* this callback is called when pipeline has constructed a source object to read
257 * from. Since we provided the appsrc:// uri to pipeline, this will be the
258 * appsrc that we must handle. We set up a signals to push data into appsrc. */
260 found_source (GObject * object, GObject * orig, GParamSpec * pspec, App * app)
262 /* get a handle to the appsrc */
263 g_object_get (orig, pspec->name, &app->appsrc, NULL);
265 GST_DEBUG ("got appsrc %p", (gpointer) app->appsrc);
267 /* configure the appsrc, we will push a buffer to appsrc when it needs more
269 g_signal_connect (app->appsrc, "need-data", G_CALLBACK (feed_data), app);
273 bus_message (GstBus * bus, GstMessage * message, App * app)
275 GST_DEBUG ("got message %s",
276 gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
278 switch (GST_MESSAGE_TYPE (message)) {
279 case GST_MESSAGE_ERROR:
281 GError *error = NULL;
282 gchar *dbg_info = NULL;
284 gst_message_parse_error (message, &error, &dbg_info);
285 g_printerr ("ERROR from element %s: %s",
286 GST_OBJECT_NAME (message->src), error->message);
287 g_printerr ("Debugging info: %s", (dbg_info) ? dbg_info : "none");
288 g_error_free (error);
290 g_main_loop_quit (app->loop);
293 case GST_MESSAGE_EOS:
294 g_main_loop_quit (app->loop);
303 app_init (App * app, char *ports)
309 ret = init_seq (app);
314 ret = parse_ports (ports, app);
319 ret = create_port (app);
325 ret = snd_seq_nonblock (app->seq, 1);
327 GST_ERROR ("Cannot set nonblock mode - %s", snd_strerror (ret));
331 snd_midi_event_new (DEFAULT_BUFSIZE, &app->parser);
332 snd_midi_event_init (app->parser);
333 snd_midi_event_reset_decode (app->parser);
335 snd_midi_event_no_status (app->parser, 1);
337 app->buffer = malloc (DEFAULT_BUFSIZE);
338 if (app->buffer == NULL) {
340 goto err_free_parser;
343 app->npfds = snd_seq_poll_descriptors_count (app->seq, POLLIN);
344 app->pfds = malloc (sizeof (*app->pfds) * app->npfds);
345 if (app->pfds == NULL) {
347 goto err_free_buffer;
355 snd_midi_event_free (app->parser);
357 g_free (app->seq_ports);
359 snd_seq_close (app->seq);
365 app_finalize (App * app)
367 /* free the resources */
370 snd_midi_event_free (app->parser);
371 g_free (app->seq_ports);
372 snd_seq_close (app->seq);
376 on_sigint (gpointer user_data)
378 GMainLoop *loop = (GMainLoop *) user_data;
379 g_message ("Caught SIGINT. Initiating shutdown.");
380 g_main_loop_quit (loop);
385 main (int argc, char *argv[])
395 GOptionEntry options[] = {
396 {"ports", 'p', 0, G_OPTION_ARG_STRING, &ports,
397 "Comma separated list of sequencer ports", "client:port,..."},
401 ctx = g_option_context_new (NULL);
402 g_option_context_add_main_entries (ctx, options, NULL);
403 g_option_context_add_group (ctx, gst_init_get_option_group ());
404 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
406 g_printerr ("Error initializing: %s\n", GST_STR_NULL (err->message));
408 g_printerr ("Error initializing: Unknown error!\n");
411 g_option_context_free (ctx);
413 gst_init (&argc, &argv);
415 GST_DEBUG_CATEGORY_INIT (mysource_debug, "mysource", 0,
416 "ALSA MIDI sequencer appsrc pipeline");
418 ret = app_init (app, ports);
423 if (app->port_count > 0)
424 printf ("Waiting for data.\n");
426 printf ("Waiting for data at port %d:0.\n", snd_seq_client_id (app->seq));
428 /* create a mainloop to get messages */
429 app->loop = g_main_loop_new (NULL, FALSE);
433 ("appsrc name=mysource ! fluiddec ! audioconvert ! autoaudiosink", NULL);
434 g_assert (app->pipeline);
436 bus = gst_pipeline_get_bus (GST_PIPELINE (app->pipeline));
439 /* add watch for messages */
440 gst_bus_add_watch (bus, (GstBusFunc) bus_message, app);
443 app->appsrc = gst_bin_get_by_name (GST_BIN (app->pipeline), "mysource");
444 g_assert (app->appsrc);
445 g_assert (GST_IS_APP_SRC (app->appsrc));
446 g_signal_connect (app->appsrc, "need-data", G_CALLBACK (feed_data), app);
448 g_object_set (app->appsrc, "format", GST_FORMAT_TIME, NULL);
449 g_object_set (app->appsrc, "is-live", TRUE, NULL);
451 /* set the caps on the source */
452 caps = gst_caps_new_simple ("audio/x-midi-event", NULL, NULL);
453 gst_app_src_set_caps (GST_APP_SRC (app->appsrc), caps);
454 gst_caps_unref (caps);
456 /* get notification when the source is created so that we get a handle to it
457 * and can configure it */
458 g_signal_connect (app->pipeline, "deep-notify::source",
459 (GCallback) found_source, app);
461 /* go to playing and wait in a mainloop. */
462 gst_element_set_state (app->pipeline, GST_STATE_PLAYING);
464 /* this mainloop is stopped when we receive an error or EOS, or on SIGINT */
465 g_unix_signal_add (SIGINT, on_sigint, app->loop);
466 g_main_loop_run (app->loop);
468 g_main_loop_unref (app->loop);
470 GST_DEBUG ("stopping");
472 gst_element_set_state (app->pipeline, GST_STATE_NULL);
474 gst_object_unref (app->appsrc);
476 gst_object_unref (bus);
478 gst_object_unref (app->pipeline);