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));
79 * Prevent Valgrind from reporting cached configuration as memory leaks, see:
80 * http://git.alsa-project.org/?p=alsa-lib.git;a=blob;f=MEMORY-LEAK;hb=HEAD
82 snd_config_update_free_global();
84 ret = snd_seq_set_client_name (app->seq, DEFAULT_CLIENT_NAME);
86 GST_ERROR ("Cannot set client name - %s", snd_strerror (ret));
93 snd_seq_close (app->seq);
98 /* Parses one or more port addresses from the string */
100 parse_ports (const char *arg, App * app)
106 GST_DEBUG ("ports: %s", arg);
109 * Assume that ports are separated by commas.
111 * Commas are used instead of spaces because those are valid in client
114 ports_list = g_strsplit (arg, ",", 0);
116 app->port_count = g_strv_length (ports_list);
117 app->seq_ports = g_try_new (snd_seq_addr_t, app->port_count);
118 if (!app->seq_ports) {
119 GST_ERROR ("Out of memory");
121 goto out_free_ports_list;
124 for (i = 0; i < (guint)app->port_count; i++) {
125 gchar *port_name = ports_list[i];
127 ret = snd_seq_parse_address (app->seq, &app->seq_ports[i],
130 GST_ERROR ("Invalid port %s - %s", port_name,
132 goto error_free_seq_ports;
136 goto out_free_ports_list;
138 error_free_seq_ports:
139 g_free (app->seq_ports);
141 g_strfreev (ports_list);
146 create_port (App * app)
150 ret = snd_seq_create_simple_port (app->seq, DEFAULT_CLIENT_NAME,
151 SND_SEQ_PORT_CAP_WRITE |
152 SND_SEQ_PORT_CAP_SUBS_WRITE,
153 SND_SEQ_PORT_TYPE_MIDI_GENERIC | SND_SEQ_PORT_TYPE_APPLICATION);
155 GST_ERROR ("Cannot create port - %s", snd_strerror (ret));
161 connect_ports (App * app)
166 for (i = 0; i < app->port_count; ++i) {
168 snd_seq_connect_from (app->seq, 0, app->seq_ports[i].client,
169 app->seq_ports[i].port);
172 GST_WARNING ("Cannot connect from port %d:%d - %s",
173 app->seq_ports[i].client, app->seq_ports[i].port, snd_strerror (ret));
178 push_buffer (App * app, gpointer data, guint size)
185 buffer = gst_buffer_new ();
187 time = app->tick * DEFAULT_TICK_PERIOD_MS * GST_MSECOND;
189 GST_BUFFER_DTS (buffer) = time;
190 GST_BUFFER_PTS (buffer) = time;
192 local_data = g_memdup (data, size);
194 gst_buffer_append_memory (buffer,
195 gst_memory_new_wrapped (0, local_data, size, 0, size, local_data,
198 GST_MEMDUMP ("MIDI data:", local_data, size);
200 GST_DEBUG ("feed buffer %p, tick %" G_GUINT64_FORMAT " size: %u",
201 (gpointer) buffer, app->tick, size);
202 g_signal_emit_by_name (app->appsrc, "push-buffer", buffer, &ret);
203 gst_buffer_unref (buffer);
209 push_tick_buffer (App * app)
211 app->buffer[0] = 0xf9;
212 push_buffer (app, app->buffer, 1);
215 /* This method is called by the need-data signal callback, we feed data into the
219 feed_data (GstElement * appsrc, guint size, App * app)
225 snd_seq_poll_descriptors (app->seq, app->pfds, app->npfds, POLLIN);
226 ret = poll (app->pfds, app->npfds, DEFAULT_POLL_TIMEOUT_MS);
228 GST_ERROR ("ERROR in poll: %s", strerror (errno));
229 gst_app_src_end_of_stream (GST_APP_SRC (appsrc));
230 } else if (ret == 0) {
231 push_tick_buffer (app);
234 snd_seq_event_t *event;
235 err = snd_seq_event_input (app->seq, &event);
236 if (err < 0 && err != -EAGAIN) {
237 GST_ERROR ("Error in snd_seq_event_input: %s", snd_strerror (err));
238 gst_app_src_end_of_stream (GST_APP_SRC (appsrc));
243 snd_midi_event_decode (app->parser, app->buffer, DEFAULT_BUFSIZE,
246 /* ENOENT indicates an event that is not a MIDI message, silently skip it */
247 if (-ENOENT == size_ev) {
248 GST_WARNING ("Warning: Received non-MIDI message");
249 push_tick_buffer (app);
251 GST_ERROR ("Error decoding event from ALSA to output: %s",
252 strerror (-size_ev));
253 gst_app_src_end_of_stream (GST_APP_SRC (appsrc));
257 push_buffer (app, app->buffer, size_ev);
266 /* this callback is called when pipeline has constructed a source object to read
267 * from. Since we provided the appsrc:// uri to pipeline, this will be the
268 * appsrc that we must handle. We set up a signals to push data into appsrc. */
270 found_source (GObject * object, GObject * orig, GParamSpec * pspec, App * app)
272 /* get a handle to the appsrc */
273 g_object_get (orig, pspec->name, &app->appsrc, NULL);
275 GST_DEBUG ("got appsrc %p", (gpointer) app->appsrc);
277 /* configure the appsrc, we will push a buffer to appsrc when it needs more
279 g_signal_connect (app->appsrc, "need-data", G_CALLBACK (feed_data), app);
283 bus_message (GstBus * bus, GstMessage * message, App * app)
285 GST_DEBUG ("got message %s",
286 gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
288 switch (GST_MESSAGE_TYPE (message)) {
289 case GST_MESSAGE_ERROR:
291 GError *error = NULL;
292 gchar *dbg_info = NULL;
294 gst_message_parse_error (message, &error, &dbg_info);
295 g_printerr ("ERROR from element %s: %s",
296 GST_OBJECT_NAME (message->src), error->message);
297 g_printerr ("Debugging info: %s", (dbg_info) ? dbg_info : "none");
298 g_error_free (error);
300 g_main_loop_quit (app->loop);
303 case GST_MESSAGE_EOS:
304 g_main_loop_quit (app->loop);
313 app_init (App * app, char *ports)
319 ret = init_seq (app);
324 ret = parse_ports (ports, app);
329 ret = create_port (app);
335 ret = snd_seq_nonblock (app->seq, 1);
337 GST_ERROR ("Cannot set nonblock mode - %s", snd_strerror (ret));
341 snd_midi_event_new (DEFAULT_BUFSIZE, &app->parser);
342 snd_midi_event_init (app->parser);
343 snd_midi_event_reset_decode (app->parser);
345 snd_midi_event_no_status (app->parser, 1);
347 app->buffer = malloc (DEFAULT_BUFSIZE);
348 if (app->buffer == NULL) {
350 goto err_free_parser;
353 app->npfds = snd_seq_poll_descriptors_count (app->seq, POLLIN);
354 app->pfds = malloc (sizeof (*app->pfds) * app->npfds);
355 if (app->pfds == NULL) {
357 goto err_free_buffer;
365 snd_midi_event_free (app->parser);
367 g_free (app->seq_ports);
369 snd_seq_close (app->seq);
375 app_finalize (App * app)
377 /* free the resources */
380 snd_midi_event_free (app->parser);
381 g_free (app->seq_ports);
382 snd_seq_close (app->seq);
386 on_sigint (gpointer user_data)
388 GMainLoop *loop = (GMainLoop *) user_data;
389 g_message ("Caught SIGINT. Initiating shutdown.");
390 g_main_loop_quit (loop);
395 main (int argc, char *argv[])
405 gboolean verbose = FALSE;
406 GOptionEntry options[] = {
407 {"ports", 'p', 0, G_OPTION_ARG_STRING, &ports,
408 "Comma separated list of sequencer ports", "client:port,..."},
409 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
410 "Output status information and property notifications", NULL},
414 ctx = g_option_context_new (NULL);
415 g_option_context_add_main_entries (ctx, options, NULL);
416 g_option_context_add_group (ctx, gst_init_get_option_group ());
417 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
419 g_printerr ("Error initializing: %s\n", GST_STR_NULL (err->message));
421 g_printerr ("Error initializing: Unknown error!\n");
424 g_option_context_free (ctx);
426 gst_init (&argc, &argv);
428 GST_DEBUG_CATEGORY_INIT (mysource_debug, "mysource", 0,
429 "ALSA MIDI sequencer appsrc pipeline");
431 ret = app_init (app, ports);
436 if (app->port_count > 0)
437 printf ("Waiting for data.\n");
439 printf ("Waiting for data at port %d:0.\n", snd_seq_client_id (app->seq));
441 /* create a mainloop to get messages */
442 app->loop = g_main_loop_new (NULL, FALSE);
446 ("appsrc name=mysource ! fluiddec ! audioconvert ! autoaudiosink", NULL);
447 g_assert (app->pipeline);
450 g_signal_connect (app->pipeline, "deep-notify", G_CALLBACK (gst_object_default_deep_notify), NULL);
452 bus = gst_pipeline_get_bus (GST_PIPELINE (app->pipeline));
455 /* add watch for messages */
456 gst_bus_add_watch (bus, (GstBusFunc) bus_message, app);
459 app->appsrc = gst_bin_get_by_name (GST_BIN (app->pipeline), "mysource");
460 g_assert (app->appsrc);
461 g_assert (GST_IS_APP_SRC (app->appsrc));
462 g_signal_connect (app->appsrc, "need-data", G_CALLBACK (feed_data), app);
464 g_object_set (app->appsrc, "format", GST_FORMAT_TIME, NULL);
465 g_object_set (app->appsrc, "is-live", TRUE, NULL);
467 /* set the caps on the source */
468 caps = gst_caps_new_simple ("audio/x-midi-event", NULL, NULL);
469 gst_app_src_set_caps (GST_APP_SRC (app->appsrc), caps);
470 gst_caps_unref (caps);
472 /* get notification when the source is created so that we get a handle to it
473 * and can configure it */
474 g_signal_connect (app->pipeline, "deep-notify::source",
475 (GCallback) found_source, app);
477 /* go to playing and wait in a mainloop. */
478 gst_element_set_state (app->pipeline, GST_STATE_PLAYING);
480 /* this mainloop is stopped when we receive an error or EOS, or on SIGINT */
481 g_unix_signal_add (SIGINT, on_sigint, app->loop);
482 g_main_loop_run (app->loop);
484 g_main_loop_unref (app->loop);
486 GST_DEBUG ("stopping");
488 gst_element_set_state (app->pipeline, GST_STATE_NULL);
490 gst_object_unref (app->appsrc);
492 gst_object_unref (bus);
494 gst_object_unref (app->pipeline);