~osmoma/audio-recorder/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/*
 * Copyright (c) 2011- Osmo Antero.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License (GPL3), or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Library General Public License 3 for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License 3 along with this program; if not, see /usr/share/common-licenses/GPL file
 * or <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include "gst-pipeline.h"
#include "audio-sources.h"

/*
 Create Gstreamer pipelines for recording.

 Please see src/media-profiles.c file. It has some hard-coded audio profiles.
*/

static GstElement *pipeline_create_simple(PipelineParms *parms, gchar **err_msg);
static GstElement *pipeline_create_complex(PipelineParms *parms, gchar **err_msg);

//static GstElement *pipeline_create_simple_VAD(PipelineParms *parms, gchar **err_msg);
static GstElement *pipeline_create_complex_VAD(PipelineParms *parms, gchar **err_msg);

static GString *pipeline_create_command_str_simple(PipelineParms *parms);
static GString *pipeline_create_command_str_complex(PipelineParms *parms);

void pipeline_free_parms(PipelineParms *parms) {
    if (!parms) return;
    g_free(parms->source);
    str_list_free(parms->dev_list);
    g_free(parms->profile_str);
    g_free(parms->file_ext);
    g_free(parms->filename);

    g_free(parms);
}

static GstElement *create_element(const gchar *elem, const gchar *name) {
    GstElement *e = gst_element_factory_make(elem, name);
    if (!GST_IS_ELEMENT(e)) {
        LOG_ERROR("Cannot create element \"%s\" (%s).\n", elem, name);
        return NULL;
    }
    return e;
}

GstElement *pipeline_create(PipelineParms *parms, gchar **err_msg) {
    if (!parms) return NULL;

    // Create a GStreamer pipeline for audio recording
    GstElement *pipeline = NULL;

    // Wash the device list. User may have disconnected microphones and webcams.
    // Invalid devices will crash the GStreamer pipeline.
    // Remove invalid devices.
    GList *new_list = audio_sources_wash_device_list(parms->dev_list);

    if (g_list_length(new_list) < 1) {
        new_list = g_list_append(new_list, NULL);
    }

    // Zero or one device?
    if (g_list_length(new_list) < 2) {

        // Create a simple pipeline that can record from max 1 device
        pipeline = pipeline_create_simple(parms, err_msg);

    } else {

        // Create a complex pipeline that can record from 2 or more devices
        pipeline = pipeline_create_complex(parms, err_msg);
    }

    // Free new_list
    str_list_free(new_list);
    new_list = NULL;

    return pipeline;
}

static GstElement *pipeline_create_simple(PipelineParms *parms, gchar **err_msg) {
    // Create a simple pipeline that can record from one (1) device only.
    //
    // Typical pipeline:
    // $ gst-launch-1.0 pulsesrc device=alsa_output.pci-0000_04_02.0.analog-stereo.monitor
    //        ! queue
    //        ! level name=level
    //        ! audioconvert ! vorbisenc ! oggmux ! filesink location=test1.ogg
    //
    // Get list of available devices:
    // $ pactl list | grep -A2 'Source #' | grep 'Name: ' | cut -d" " -f2
    // $ pactl list short sources | cut -f2

    GstElement *pipeline = gst_pipeline_new("Audio-Recorder");

    // Source
    const gchar *source_name = (parms->source ? parms->source : "pulsesrc");
    GstElement *source = create_element(source_name, NULL);

    // Set device
    const gchar *device = g_list_nth_data(parms->dev_list, 0);
    if (device) {
        g_object_set(G_OBJECT(source), "device", device, NULL);
    }

    GstElement *level = create_element("level", "level");

    gchar *str = g_strdup_printf("capsfilter caps=%s", parms->profile_str);

    GError *error = NULL;
    GstElement *bin = gst_parse_bin_from_description(str, TRUE, &error);
    if (error) {
        // Set err_msg
        gchar *tmp = g_strdup_printf("%s. (%s)", error->message, str);
        *err_msg = g_strdup_printf(_("Cannot create audio pipeline. %s.\n"), tmp);
        g_free(tmp);
        g_error_free(error);
        g_free(str);
        goto LBL_1;
    }

    g_free(str);

    GstElement *resample = create_element("audioresample", NULL);
    GstElement *convert = create_element("audioconvert", NULL);

    // Filesink. Caller must set its "location" property.
    GstElement *filesink = create_element("filesink", "filesink");

    gst_bin_add_many(GST_BIN(pipeline), source, level, resample, convert, bin, filesink, NULL);

    // Link
    if (!gst_element_link_many(source, level, resample, convert, bin, filesink, NULL)) {
        *err_msg = g_strdup_printf(_("Cannot create audio pipeline. %s.\n"), "Cannot link.");
        goto LBL_1;
    }

    // Ok
    return pipeline;

LBL_1:
    // Got an error
    return NULL;
}

static GstElement *pipeline_create_complex(PipelineParms *parms, gchar **err_msg) {
    // Create a complex pipeline using the audiomixer or GstAdder elements.
    // Ref: https://gstreamer.freedesktop.org/data/doc/gstreamer/head/gst-plugins-base-plugins/html/gst-plugins-base-plugins-adder.html
    // This can record from 2 or more devices.

    // Typical pipeline (using the "audiomixer" element):
    // $ gst-launch-1.0 audiomixer name=mixer mix.
    //      ! audioconvert
    //      ! vorbisenc ! oggmux
    //      ! filesink location=test1.ogg
    //      pulsesrc device=alsa_output.pci-0000_00_1b.0.analog-stereo.monitor ! queue ! mix.
    //      pulsesrc device=alsa_input.usb-Creative_Technology_Ltd._VF110_Live_Mic ! queue ! mix.

    // Using the "adder" element:
    // $ gst-launch-1.0 adder name=mixer mix.
    //      ! audioconvert
    //      ! vorbisenc ! oggmux
    //      ! filesink location=test1.ogg
    //      pulsesrc device=alsa_output.pci-0000_00_1b.0.analog-stereo.monitor ! queue ! mix.
    //      pulsesrc device=alsa_input.usb-Creative_Technology_Ltd._VF110_Live_Mic ! queue ! mix.
    //
    // Get list of available devices:
    // $ pactl list | grep -A2 'Source #' | grep 'Name: ' | cut -d" " -f2
    // $ pactl list short sources | cut -f2

    GstElement *pipeline = gst_pipeline_new("Audio-Recorder");

    // Create either "audiomixer" or "adder". Audiomixer is an improved version of adder.
    GstElement *mixer = create_element("audiomixer", "mixer");
    if (!GST_IS_ELEMENT(mixer)) {
        mixer = create_element("adder", "mixer");
    }

    // Level (dB) data
    GstElement *level = create_element("level", "level");

    // Create a GstCapsfilter + all encoder elements from the GNOME's profile_str.
    // See: gconf-editor,  system -> gstreamer -> 0.10 -> audio -> profiles.
    gchar *str = g_strdup_printf("capsfilter caps=%s", parms->profile_str);

    GError *error = NULL;
    GstElement *bin = gst_parse_bin_from_description(str, TRUE, &error);
    if (error) {
        // Set err_msg
        gchar *tmp = g_strdup_printf("%s. (%s)", error->message, str);
        *err_msg = g_strdup_printf(_("Cannot create audio pipeline. %s.\n"), tmp);
        g_free(tmp);
        g_error_free(error);
        g_free(str);
        goto LBL_1;
    }

    g_free(str);

    GstElement *resample = create_element("audioresample", NULL);
    GstElement *convert = create_element("audioconvert", NULL);

    // Filesink. Caller must set its "location" property.
    GstElement *filesink = create_element("filesink", "filesink");

    gst_bin_add_many(GST_BIN(pipeline), mixer, level, resample, convert, bin, filesink, NULL);

    // Link
    if (!gst_element_link_many(mixer, level, resample, convert, bin, filesink, NULL)) {
        *err_msg = g_strdup_printf(_("Cannot create audio pipeline. %s.\n"), "Cannot link.");
        goto LBL_1;
    }

    // Now create audio source for all devices
    GList *item = g_list_first(parms->dev_list);
    while (item) {
        // Device name
        const gchar *device = (gchar*)item->data;

        // Source
        const gchar *source_name = (parms->source ? parms->source : "pulsesrc");
        GstElement *source = create_element(source_name, NULL);
        g_object_set(G_OBJECT(source), "device", device, NULL);

        //Queue
        GstElement *queue = create_element("queue", NULL);
        gst_bin_add_many(GST_BIN(pipeline), source, queue, NULL);

        // Link source# -> queue#
        gst_element_link(source, queue);

        // Link queue# -> mixer
        // Gstreamer 1.0:
        gst_element_link_pads(queue, NULL, mixer, NULL);

        // Next source
        item = g_list_next(item);
    }

    // Ok
    return pipeline;

LBL_1:
    // Got an error
    return NULL;
}

GstElement *pipeline_create_VAD(PipelineParms *parms, gchar **err_msg) {
    if (!parms) return NULL;
    // Create a GStreamer pipeline for VAD, Voice Activity Detection.

    GstElement *pipeline = NULL;

    // Wash the device list. User may have disconnected microphones and webcams.
    // Invalid devices will crash the GStreamer pipeline.
    // Remove invalid devices.
    GList *new_list = audio_sources_wash_device_list(parms->dev_list);

    if (g_list_length(new_list) < 1) {
        new_list = g_list_append(new_list, NULL);
    }

    pipeline = pipeline_create_complex_VAD(parms, err_msg);

    // Free new_list
    str_list_free(new_list);
    new_list = NULL;

    return pipeline;
}

#if 0
static GstElement *pipeline_create_simple_VAD(PipelineParms *parms, gchar **err_msg) {
    // Creater a simple pipelinewwaY TO MAKE for VAD, Voice Activity Detection.
    //
    // Typical pipelines:
    // gst-launch pulsesrc device=alsa_input.usb-Creative_Technology_Cam_Socialize !
    //            cutter name=cutter threshold=0.3 run-length=4000000000 ! fakesink

    // Or using the GstVader element (see .../gst-plugin/src/)
    // gst-launch pulsesrc device=alsa_input.usb-Creative_Technology_Cam_Socialize !
    //            vader name=cutter threshold=0.3 run-length=4000000000 ! fakesink"

    GstElement *pipeline = gst_pipeline_new("Voice Activity Detector");

    // Source
    const gchar *source_name = (parms->source ? parms->source : "pulsesrc");
    GstElement *source = create_element(source_name, NULL);

    // Set device
    const gchar *device = g_list_nth_data(parms->dev_list, 0);
    if (device) {
        g_object_set(G_OBJECT(source), "device", device, NULL);
    }

    // Do we find a GstVader element? (we compile it from source. see .../audio-recorder/gst-plugin/ folder)
    // Vader is a VAD (Voice Activity Detector), it's an improved version of GstCutter.
    // Ref: https://sourceforge.net/projects/cmusphinx/
    // Ref: https://developer.gnome.org/gst-plugins-libs/0.11/gst-plugins-good-plugins-cutter.html
    GstElement *cutter = gst_element_factory_make("vader", "cutter");

    if (!GST_IS_ELEMENT(cutter)) {
        // Standard GstCutter element should do fine
        cutter = create_element("cutter", "cutter");
    }

    // Disable "cutter" messages. Set threshold to 1.0.
    // We set this value later.
    g_object_set(G_OBJECT(cutter), "threshold", 1.0, NULL);

    // Fakesink
    GstElement *fakesink = create_element("fakesink", NULL);

    gst_bin_add_many(GST_BIN(pipeline), source, cutter, fakesink, NULL);

    // Link
    if (!gst_element_link_many(source, cutter, fakesink, NULL)) {
        *err_msg = g_strdup_printf(_("Cannot create audio pipeline. %s.\n"), "Cannot link.");
        goto LBL_1;
    }

    // Ok
    return pipeline;

LBL_1:
    // Got an error
    return NULL;
}
#endif

static GstElement *pipeline_create_complex_VAD(PipelineParms *parms, gchar **err_msg) {
    // Create a complex pipeline for VAD. Use audiomixer or GstAdder elements.
    // Ref: https://gstreamer.freedesktop.org/data/doc/gstreamer/head/gst-plugins-base-plugins/html/gst-plugins-base-plugins-adder.html
    // https://cgit.freedesktop.org/gstreamer/gst-plugins-bad/tree/gst/audiomixer
    // For 2 more devices.

    // Typical pipelines:
    // $ gst-launch-1.0 audiomixer name=mix
    //        ! level name=level
    //        ! fakesink
    //        { pulsesrc device=alsa_output.pci-0000_04_02.0.analog-stereo.monitor ! queue ! mix. }
    //        { pulsesrc device=alsa_input.pci_8086_24c5_alsa_WebCam ! queue ! mix. }
    //
    // $ gst-launch-0.10 audiomixer name=mix ! level ! fakesink { pulsesrc ! queue ! mix. }

    GstElement *pipeline = gst_pipeline_new("Voice Activity Detector");

    // Mix audio from several sources.
    // Use "audiomixer" or "adder". Audiomixer is an improved version of adder.
    GstElement *mixer = create_element("audiomixer", "mixer");
    if (!GST_IS_ELEMENT(mixer)) {
        mixer = create_element("adder", "mixer");
    }

    // Level (dB) data
    GstElement *level = create_element("level", "level");

    // Fakesink
    GstElement *fakesink = create_element("fakesink", "fakesink");

    gst_bin_add_many(GST_BIN(pipeline), mixer, level, fakesink, NULL);

    // Link
    if (!gst_element_link_many(mixer, level, fakesink, NULL)) {
        *err_msg = g_strdup_printf(_("Cannot create audio pipeline. %s.\n"), "Cannot link.");
        goto LBL_1;
    }

    // Now create audio source for all devices
    GList *item = g_list_first(parms->dev_list);
    while (item) {
        // Device name
        const gchar *device = (gchar*)item->data;

        // Source
        const gchar *source_name = (parms->source ? parms->source : "pulsesrc");
        GstElement *source = create_element(source_name, NULL);

        if (device) {
            g_object_set(G_OBJECT(source), "device", device, NULL);
        }

        // Queue
        GstElement *queue = create_element("queue", NULL);

        gst_bin_add_many(GST_BIN(pipeline), source, queue, NULL);

        // Link source# -> queue#
        gst_element_link(source, queue);

        // Link queue# -> mixer
        // Gstreamer 1.0:
        gst_element_link_pads(queue, NULL, mixer, NULL);

        // Next source
        item = g_list_next(item);
    }

    // Ok
    return pipeline;

LBL_1:
    // Got an error
    return NULL;
}

#if 0
static GstElement *pipeline_create_test(PipelineParms *parms, gchar **err_msg) {
    GstElement *pipeline = NULL;
    gchar *pipeline_cmd = g_strdup_printf ("%s name=source"
                                           " ! level name=level"
                                           " ! queue"
                                           " ! audioconvert"
                                           " ! %s"
                                           " ! filesink name=filesink",
                                           parms->source,
                                           parms->profile_str);

    LOG_DEBUG("\n\n");
    LOG_DEBUG("Going to create pipeline:\ngst-launch %s\n\n", pipeline_cmd);

    GError *err = NULL;
    pipeline = gst_parse_launch(pipeline_cmd, &err);
    g_free(pipeline_cmd);

    if (err) {
        // Set err_msg
        *err_msg = g_strdup_printf(_("Cannot create audio pipeline. %s.\n"), err->message);
        g_error_free(err);
        goto LBL_1;
    }

    // Get the "source0" element
    GstElement *source = gst_bin_get_by_name(GST_BIN(pipeline), "source");

    if (!GST_IS_ELEMENT(source)) {
        // Set err_msg
        *err_msg = g_strdup_printf(_("Cannot find audio element %s.\n"), parms->source);
        goto LBL_1;
    }

    // Set audio device
    if (g_list_length(parms->dev_list) > 0) {
        gchar *device = parms->dev_list->data;
        g_object_set(G_OBJECT(source), "device", device, NULL);
    }

    g_object_unref(source);

    // Ok
    return pipeline;

LBL_1:
    // Got an error
    return NULL;
}

static GstElement *pipeline_create_TEST(PipelineParms *parms, gchar **err_msg) {
    GstElement *pipeline = NULL;

    gchar *pipeline_cmd = g_strdup_printf ("pulsesrc device=alsa_output.pci-0000_01_00.1.hdmi-stereo.monitor"
                                           " ! queue"
                                           " ! level name=level"
                                           " ! audioconvert"
                                           " ! audio/x-raw,rate=44100,channels=2 ! vorbisenc name=enc quality=0.5 ! oggmux"
                                           " ! filesink name=filesink");

    LOG_DEBUG("\n\n");
    LOG_DEBUG("Going to create pipeline:\ngst-launch %s\n\n", pipeline_cmd);

    GError *err = NULL;
    pipeline = gst_parse_launch(pipeline_cmd, &err);
    g_free(pipeline_cmd);

    if (err) {
        // Set err_msg
        *err_msg = g_strdup_printf(_("Cannot create audio pipeline. %s.\n"), err->message);
        g_error_free(err);
        goto LBL_1;
    }

    // Ok
    return pipeline;

LBL_1:
    // Got an error
    return NULL;
}
#endif

GString *pipeline_create_command_str(PipelineParms *parms) {
    // Return a gst-launch command for the given pipeline and parameters.
    // User can run this command in a terminal window to test various pipelines for recording.

    if (!parms) return NULL;

    // Create a gst-launch command
    GString *str = NULL;

    // Wash the device list. User may have disconnected microphones and webcams.
    // Invalid devices may crash the GStreamer pipeline.
    // Remove invalid devices.
    GList *new_list = audio_sources_wash_device_list(parms->dev_list);

    if (g_list_length(new_list) < 1) {
        new_list = g_list_append(new_list, NULL);
    }

    // Zero or one device?
    if (g_list_length(new_list) < 2) {

        // Create a simple gst-launch command. It can record from 1 device only.
        str = pipeline_create_command_str_simple(parms);

    } else {

        // Create a complex gst-launch command. This can record from 2 or more devices.
        str = pipeline_create_command_str_complex(parms);
    }

    // Free new_list
    str_list_free(new_list);
    new_list = NULL;

    // Caller must free this with g_string_free.
    return str;
}

static GString *pipeline_create_command_str_simple(PipelineParms *parms) {
    // Return a gst-launch command that can record from 1 device only.

    // Typical command string:
    // "gst-launch-1.0 pulsesrc device=alsa_output.pci-0000_04_02.0.analog-stereo.monitor
    //        ! queue
    //        ! level name=level
    //        ! audioconvert ! vorbisenc ! oggmux ! filesink location=test.ogg"

    guint major, minor, micro, nano;
    gst_version(&major, &minor, &micro, &nano);

    GString *str = g_string_new(NULL);
    g_string_append_printf(str, "gst-launch-%d.0 ", major);

    // Add -e (--eos-on-shutdow) option.
    // This sends an EOS before shutting the pipeline down. This keeps the recorded file readable.
    str = g_string_append(str, " -e ");

    // Source
    const gchar *source_name = (parms->source ? parms->source : "pulsesrc");

    str = g_string_append(str, source_name);
    str = g_string_append(str, " ");

    // Set device
    const gchar *device = g_list_nth_data(parms->dev_list, 0);
    if (device) {
        g_string_append_printf(str, "device=%s \\\n", device);
    } else {
        str = g_string_append(str, " \\\n");
    }

    str = g_string_append(str, "! queue \\\n");

    str = g_string_append(str, "! audioresample ! audioconvert \\\n");

    str = g_string_append(str, "! ");

    str = g_string_append(str, parms->profile_str);

    str = g_string_append(str, " \\\n");

    g_string_append_printf(str, "! filesink location=%s\n", parms->filename);

    // Caller must free this with g_string_free.
    return str;
}

static GString *pipeline_create_command_str_complex(PipelineParms *parms) {
    // Return a gst-launch command that can record from two (2) or more audio input devices.
    // This uses audiomixer or GstAdder elements to mix input from several sources.

    // Typical command string:
    //  "gst-launch-1.0 -e audiomixer name=mixer mix.
    //      ! audioconvert
    //      ! vorbisenc ! oggmux
    //      ! filesink location=test.ogg
    //      pulsesrc device=alsa_output.pci-0000_00_1b.0.analog-stereo.monitor ! queue ! mix.
    //      pulsesrc device=alsa_input.usb-Creative_Technology_Ltd._VF110_Live_Mic ! queue ! mix."

    guint major, minor, micro, nano;
    gst_version(&major, &minor, &micro, &nano);

    GString *str = g_string_new(NULL);
    g_string_append_printf(str, "gst-launch-%d.0", major);

    // Add -e (--eos-on-shutdow) option.
    // This sends an EOS before shutting the pipeline down. This keeps the recorded file readable.
    str = g_string_append(str, " -e ");

    // Use "audiomixer" or "adder". Audiomixer is an improved version of adder.
    GstElement *e = create_element("audiomixer", "mixer");
    if (GST_IS_ELEMENT(e)) {
        str = g_string_append(str, " audiomixer name=mixer \\\n");
        gst_object_unref(GST_OBJECT(e));
    } else {
        str = g_string_append(str, " adder name=mixer \\\n");
    }

    str = g_string_append(str, "! level \\\n");

    str = g_string_append(str, "! audioresample ! audioconvert \\\n");

    str = g_string_append(str, "! ");

    str = g_string_append(str, parms->profile_str);

    str = g_string_append(str, " \\\n");

    g_string_append_printf(str, "! filesink location=%s \\\n", parms->filename);

    // Now create audio source for all devices
    GList *item = g_list_first(parms->dev_list);
    while (item) {
        // Device name
        const gchar *device = (gchar*)item->data;

        // Source
        const gchar *source_name = (parms->source ? parms->source : "pulsesrc");

        g_string_append_printf(str, " %s device=%s ! queue ! mixer. %s\n", source_name, device, (item->next ? "\\" : ""));

        // Next source
        item = g_list_next(item);
    }

    // Caller must free this with g_string_free.
    return str;
}