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;
56 snd_seq_addr_t *seq_ports;
57 snd_midi_event_t *parser;
58 unsigned char *buffer;
73 ret = snd_seq_open (&app->seq, "default", SND_SEQ_OPEN_DUPLEX, 0);
75 GST_ERROR ("Cannot open sequencer - %s", snd_strerror (ret));
80 * Prevent Valgrind from reporting cached configuration as memory leaks, see:
81 * http://git.alsa-project.org/?p=alsa-lib.git;a=blob;f=MEMORY-LEAK;hb=HEAD
83 snd_config_update_free_global();
85 ret = snd_seq_set_client_name (app->seq, DEFAULT_CLIENT_NAME);
87 GST_ERROR ("Cannot set client name - %s", snd_strerror (ret));
94 snd_seq_close (app->seq);
99 /* Parses one or more port addresses from the string */
101 parse_ports (const char *arg, App * app)
107 GST_DEBUG ("ports: %s", arg);
110 * Assume that ports are separated by commas.
112 * Commas are used instead of spaces because those are valid in client
115 ports_list = g_strsplit (arg, ",", 0);
117 app->port_count = g_strv_length (ports_list);
118 app->seq_ports = g_try_new (snd_seq_addr_t, app->port_count);
119 if (!app->seq_ports) {
120 GST_ERROR ("Out of memory");
122 goto out_free_ports_list;
125 for (i = 0; i < (guint)app->port_count; i++) {
126 gchar *port_name = ports_list[i];
128 ret = snd_seq_parse_address (app->seq, &app->seq_ports[i],
131 GST_ERROR ("Invalid port %s - %s", port_name,
133 goto error_free_seq_ports;
137 goto out_free_ports_list;
139 error_free_seq_ports:
140 g_free (app->seq_ports);
142 g_strfreev (ports_list);
147 start_queue_timer (snd_seq_t *seq, int queue)
151 ret = snd_seq_start_queue (seq, queue, NULL);
153 GST_ERROR ("Timer event output error: %s\n", snd_strerror (ret));
157 ret = snd_seq_drain_output (seq);
159 GST_ERROR ("Drain output error: %s\n", snd_strerror (ret));
165 create_port (App * app)
167 snd_seq_port_info_t *pinfo;
170 snd_seq_port_info_alloca (&pinfo);
171 snd_seq_port_info_set_name (pinfo, DEFAULT_CLIENT_NAME);
172 snd_seq_port_info_set_type (pinfo, SND_SEQ_PORT_TYPE_MIDI_GENERIC | SND_SEQ_PORT_TYPE_APPLICATION);
173 snd_seq_port_info_set_capability (pinfo, SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE);
175 ret = snd_seq_alloc_queue (app->seq);
177 GST_ERROR ("Cannot allocate queue: %s\n", snd_strerror (ret));
183 snd_seq_port_info_set_timestamping (pinfo, 1);
184 snd_seq_port_info_set_timestamp_real (pinfo, 1);
185 snd_seq_port_info_set_timestamp_queue (pinfo, app->queue);
187 ret = snd_seq_create_port (app->seq, pinfo);
189 GST_ERROR ("Cannot create port - %s", snd_strerror (ret));
191 ret = start_queue_timer (app->seq, app->queue);
197 connect_ports (App * app)
202 for (i = 0; i < app->port_count; ++i) {
204 snd_seq_connect_from (app->seq, 0, app->seq_ports[i].client,
205 app->seq_ports[i].port);
208 GST_WARNING ("Cannot connect from port %d:%d - %s",
209 app->seq_ports[i].client, app->seq_ports[i].port, snd_strerror (ret));
214 push_buffer (App * app, gpointer data, guint size)
221 buffer = gst_buffer_new ();
223 time = app->tick * DEFAULT_TICK_PERIOD_MS * GST_MSECOND;
225 GST_BUFFER_DTS (buffer) = time;
226 GST_BUFFER_PTS (buffer) = time;
228 local_data = g_memdup (data, size);
230 gst_buffer_append_memory (buffer,
231 gst_memory_new_wrapped (0, local_data, size, 0, size, local_data,
234 GST_MEMDUMP ("MIDI data:", local_data, size);
236 GST_DEBUG ("feed buffer %p, tick %" G_GUINT64_FORMAT " size: %u",
237 (gpointer) buffer, app->tick, size);
238 g_signal_emit_by_name (app->appsrc, "push-buffer", buffer, &ret);
239 gst_buffer_unref (buffer);
245 push_tick_buffer (App * app)
247 app->buffer[0] = 0xf9;
248 push_buffer (app, app->buffer, 1);
251 /* This method is called by the need-data signal callback, we feed data into the
255 feed_data (GstElement * appsrc, guint size, App * app)
261 snd_seq_poll_descriptors (app->seq, app->pfds, app->npfds, POLLIN);
262 ret = poll (app->pfds, app->npfds, DEFAULT_POLL_TIMEOUT_MS);
264 GST_ERROR ("ERROR in poll: %s", strerror (errno));
265 gst_app_src_end_of_stream (GST_APP_SRC (appsrc));
266 } else if (ret == 0) {
267 push_tick_buffer (app);
270 snd_seq_event_t *event;
271 err = snd_seq_event_input (app->seq, &event);
272 if (err < 0 && err != -EAGAIN) {
273 GST_ERROR ("Error in snd_seq_event_input: %s", snd_strerror (err));
274 gst_app_src_end_of_stream (GST_APP_SRC (appsrc));
279 snd_midi_event_decode (app->parser, app->buffer, DEFAULT_BUFSIZE,
282 /* ENOENT indicates an event that is not a MIDI message, silently skip it */
283 if (-ENOENT == size_ev) {
284 GST_WARNING ("Warning: Received non-MIDI message");
285 push_tick_buffer (app);
287 GST_ERROR ("Error decoding event from ALSA to output: %s",
288 strerror (-size_ev));
289 gst_app_src_end_of_stream (GST_APP_SRC (appsrc));
293 push_buffer (app, app->buffer, size_ev);
302 /* this callback is called when pipeline has constructed a source object to read
303 * from. Since we provided the appsrc:// uri to pipeline, this will be the
304 * appsrc that we must handle. We set up a signals to push data into appsrc. */
306 found_source (GObject * object, GObject * orig, GParamSpec * pspec, App * app)
308 /* get a handle to the appsrc */
309 g_object_get (orig, pspec->name, &app->appsrc, NULL);
311 GST_DEBUG ("got appsrc %p", (gpointer) app->appsrc);
313 /* configure the appsrc, we will push a buffer to appsrc when it needs more
315 g_signal_connect (app->appsrc, "need-data", G_CALLBACK (feed_data), app);
319 bus_message (GstBus * bus, GstMessage * message, App * app)
321 GST_DEBUG ("got message %s",
322 gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
324 switch (GST_MESSAGE_TYPE (message)) {
325 case GST_MESSAGE_ERROR:
327 GError *error = NULL;
328 gchar *dbg_info = NULL;
330 gst_message_parse_error (message, &error, &dbg_info);
331 g_printerr ("ERROR from element %s: %s",
332 GST_OBJECT_NAME (message->src), error->message);
333 g_printerr ("Debugging info: %s", (dbg_info) ? dbg_info : "none");
334 g_error_free (error);
336 g_main_loop_quit (app->loop);
339 case GST_MESSAGE_EOS:
340 g_main_loop_quit (app->loop);
349 app_init (App * app, char *ports)
355 ret = init_seq (app);
360 ret = parse_ports (ports, app);
365 ret = create_port (app);
371 ret = snd_seq_nonblock (app->seq, 1);
373 GST_ERROR ("Cannot set nonblock mode - %s", snd_strerror (ret));
377 snd_midi_event_new (DEFAULT_BUFSIZE, &app->parser);
378 snd_midi_event_init (app->parser);
379 snd_midi_event_reset_decode (app->parser);
381 snd_midi_event_no_status (app->parser, 1);
383 app->buffer = malloc (DEFAULT_BUFSIZE);
384 if (app->buffer == NULL) {
386 goto err_free_parser;
389 app->npfds = snd_seq_poll_descriptors_count (app->seq, POLLIN);
390 app->pfds = malloc (sizeof (*app->pfds) * app->npfds);
391 if (app->pfds == NULL) {
393 goto err_free_buffer;
401 snd_midi_event_free (app->parser);
403 g_free (app->seq_ports);
405 snd_seq_close (app->seq);
411 app_finalize (App * app)
413 /* free the resources */
416 snd_midi_event_free (app->parser);
417 g_free (app->seq_ports);
418 snd_seq_close (app->seq);
422 on_sigint (gpointer user_data)
424 GMainLoop *loop = (GMainLoop *) user_data;
425 g_message ("Caught SIGINT. Initiating shutdown.");
426 g_main_loop_quit (loop);
431 main (int argc, char *argv[])
441 gboolean verbose = FALSE;
442 GOptionEntry options[] = {
443 {"ports", 'p', 0, G_OPTION_ARG_STRING, &ports,
444 "Comma separated list of sequencer ports", "client:port,..."},
445 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
446 "Output status information and property notifications", NULL},
450 ctx = g_option_context_new (NULL);
451 g_option_context_add_main_entries (ctx, options, NULL);
452 g_option_context_add_group (ctx, gst_init_get_option_group ());
453 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
455 g_printerr ("Error initializing: %s\n", GST_STR_NULL (err->message));
457 g_printerr ("Error initializing: Unknown error!\n");
460 g_option_context_free (ctx);
462 gst_init (&argc, &argv);
464 GST_DEBUG_CATEGORY_INIT (mysource_debug, "mysource", 0,
465 "ALSA MIDI sequencer appsrc pipeline");
467 ret = app_init (app, ports);
472 if (app->port_count > 0)
473 printf ("Waiting for data.\n");
475 printf ("Waiting for data at port %d:0.\n", snd_seq_client_id (app->seq));
477 /* create a mainloop to get messages */
478 app->loop = g_main_loop_new (NULL, FALSE);
482 ("appsrc name=mysource ! fluiddec ! audioconvert ! autoaudiosink", NULL);
483 g_assert (app->pipeline);
486 g_signal_connect (app->pipeline, "deep-notify", G_CALLBACK (gst_object_default_deep_notify), NULL);
488 bus = gst_pipeline_get_bus (GST_PIPELINE (app->pipeline));
491 /* add watch for messages */
492 gst_bus_add_watch (bus, (GstBusFunc) bus_message, app);
495 app->appsrc = gst_bin_get_by_name (GST_BIN (app->pipeline), "mysource");
496 g_assert (app->appsrc);
497 g_assert (GST_IS_APP_SRC (app->appsrc));
498 g_signal_connect (app->appsrc, "need-data", G_CALLBACK (feed_data), app);
500 g_object_set (app->appsrc, "format", GST_FORMAT_TIME, NULL);
501 g_object_set (app->appsrc, "is-live", TRUE, NULL);
503 /* set the caps on the source */
504 caps = gst_caps_new_simple ("audio/x-midi-event", NULL, NULL);
505 gst_app_src_set_caps (GST_APP_SRC (app->appsrc), caps);
506 gst_caps_unref (caps);
508 /* get notification when the source is created so that we get a handle to it
509 * and can configure it */
510 g_signal_connect (app->pipeline, "deep-notify::source",
511 (GCallback) found_source, app);
513 /* go to playing and wait in a mainloop. */
514 gst_element_set_state (app->pipeline, GST_STATE_PLAYING);
516 /* this mainloop is stopped when we receive an error or EOS, or on SIGINT */
517 g_unix_signal_add (SIGINT, on_sigint, app->loop);
518 g_main_loop_run (app->loop);
520 g_main_loop_unref (app->loop);
522 GST_DEBUG ("stopping");
524 gst_element_set_state (app->pipeline, GST_STATE_NULL);
526 gst_object_unref (app->appsrc);
528 gst_object_unref (bus);
530 gst_object_unref (app->pipeline);