~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
/*
 * 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 "gst-devices.h"
#include "support.h" // _(x)
#include "log.h"
#include "utility.h"
#include "rec-manager-struct.h"

#include <stdio.h>
#include <string.h>
#include <glib.h>

// Collect audio input devices and audio output devices w/ connected loudspeakers.
// This module is using gst_device_monitor_* functions that were introduced in GStreamer 1.4.

// You may also check the output of these pa (pulseaudio) commands.

// Get list of input devices:
// $ pactl list short sources | cut -f2
// $ pactl list | grep -A6  'Source #' | egrep "Name: |Description: "
//
// Get list of sink (output) devices:
// $ pactl list short sinks | cut -f2
// $ pactl list | grep -A6  'Sink #' | egrep "Name: |Description: "


// Audio sink devices
static GList *g_sink_list = NULL;

// Audio source devices
G_LOCK_DEFINE_STATIC(g_source_list);
static GList *g_source_list = NULL;

static GstDeviceMonitor *g_dev_monitor = NULL;

static void gstdev_get_devices();
static void gstdev_read_fields(GstDevice *dev, gchar **dev_id, gchar **dev_descr, gchar **dev_class, gchar **dev_caps_str);
static void gstdev_add_to_list(GstDevice *dev);
static void gstdev_remove_from_list(GstDevice *dev);
static void gstdev_clear_lists();

void gstdev_module_init() {
    LOG_DEBUG("Init gst-devices.c.\n");
    g_source_list = NULL;
    g_sink_list = NULL;
    g_dev_monitor = NULL;
}

void gstdev_module_exit() {
    LOG_DEBUG("Clean up gst-devices.c.\n");

    if (GST_IS_DEVICE_MONITOR(g_dev_monitor)) {
        gst_device_monitor_stop(g_dev_monitor);
        gst_object_unref(g_dev_monitor);
    }

    g_dev_monitor = NULL;

    // Clear lists
    gstdev_clear_lists();
}

GList *gstdev_get_source_list() {
    // Return g_source_list to the caller

    gstdev_get_devices();

    return g_source_list;
}

static void gstdev_clear_lists() {
    LOG_DEBUG("gstdev_clear_lists(). Clear g_sink_list and g_source_list.\n");

    G_LOCK(g_source_list);

    // Free g_sink_list
    audio_sources_free_list(g_sink_list);
    g_sink_list = NULL;

    // Free g_source_list
    audio_sources_free_list(g_source_list);
    g_source_list = NULL;

    G_UNLOCK(g_source_list);
}

void gstdev_update_GUI() {
    // Device list changed. Update GUI.

    RecorderCommand *cmd = g_malloc0(sizeof(RecorderCommand));
    cmd->type = RECORDING_DEVICE_CHANGED;

    // Send command to rec-manager.c and GUI.
    // It will free the cmd structure after processing.
    rec_manager_send_command(cmd);
}

static gboolean message_func(GstBus *bus, GstMessage *message, gpointer user_data) {
    GstDevice *device = NULL;
    gchar *name = NULL;

    LOG_DEBUG("message_func(): function to add or remove device called.\n");

    switch (GST_MESSAGE_TYPE (message)) {
    case GST_MESSAGE_DEVICE_ADDED:
        gst_message_parse_device_added(message, &device);

        name = gst_device_get_display_name(device);

        LOG_DEBUG("Audio device added: %s\n", name);
        g_free (name);

        gstdev_add_to_list(device);

        gstdev_update_GUI();

        break;

    case GST_MESSAGE_DEVICE_REMOVED:
        gst_message_parse_device_removed(message, &device);

        name = gst_device_get_display_name(device);

        LOG_DEBUG("Audio device removed: %s\n", name);
        g_free (name);

        gstdev_remove_from_list(device);

        gstdev_update_GUI();

        break;

    default:
        break;
    }

    return G_SOURCE_CONTINUE;
}

GstDeviceMonitor *setup_raw_audio_source_device_monitor() {

    LOG_DEBUG("Setup monitor to detect new and unplugged devices.\n");

    GstDeviceMonitor *monitor = gst_device_monitor_new ();

    GstBus *bus = gst_device_monitor_get_bus(monitor);
    gst_bus_add_watch(bus, message_func, NULL);
    gst_object_unref(bus);

    GstCaps *caps = gst_caps_new_empty_simple ("audio/x-raw");
    gst_device_monitor_add_filter(monitor, NULL, caps); // "Audio/Source", "Audio/Sink"
    gst_caps_unref (caps);

    gst_device_monitor_start(monitor);

    return monitor;
}

static void gstdev_read_fields(GstDevice *dev, gchar **dev_id, gchar **dev_descr, gchar **dev_class, gchar **dev_caps_str) {

    gchar *disp_name = gst_device_get_display_name(dev);

    // Cut it nicely
    *dev_descr = g_strdup(disp_name);
    str_cut_nicely(*dev_descr, 39/*to len*/, 25/*minimum len*/);

    g_free(disp_name);

    *dev_class = gst_device_get_device_class(dev);

    GstCaps *caps = gst_device_get_caps(dev);
    *dev_caps_str = gst_caps_to_string(caps);
    gst_caps_unref(caps);

    // Read device id (this should work for pulsesrc and alsasrc)
    GstElement *e = gst_device_create_element(dev, "element");

    GValue value = {0, };
    g_value_init (&value, G_TYPE_STRING);
    g_object_get_property(G_OBJECT(e), "device", &value);

    *dev_id = g_value_dup_string(&value);

    g_value_unset(&value);
    gst_object_unref(GST_OBJECT(e));
}

GList *remove_item(GList *list, gchar *dev_id) {

    GList *item = g_list_first(list);
    while (item) {

        DeviceItem *rec = (DeviceItem*)item->data;
        if (rec && g_strcmp0(rec->id, dev_id) == 0) {

            list = g_list_delete_link(list, item);
            device_item_free(rec);

            return list;
        }

        item = g_list_next(item);
    }

    // Return unmodified list
    return list;
}

static void gstdev_remove_from_list(GstDevice *dev) {
    gchar *dev_id = NULL;
    gchar *dev_descr = NULL;
    gchar *dev_class = NULL;
    gchar *dev_caps_str = NULL;

    LOG_DEBUG("Remove (input or output) device from the list.\n");

    G_LOCK(g_source_list);

    gstdev_read_fields(dev, &dev_id, &dev_descr, &dev_class, &dev_caps_str);

    gchar *dev_class_l = g_ascii_strdown(dev_class, -1);

    // Audio/Source
    if (g_str_has_prefix(dev_class_l, "audio/source")) {

        LOG_DEBUG("Remove audio input device (from g_source_list):%s, decr:%s, class:%s\n", dev_id, dev_descr, dev_class);

        g_source_list = remove_item(g_source_list, dev_id);

    }
    // Audio/Sink
    else if (g_str_has_prefix(dev_class_l, "audio/sink")) {

        LOG_DEBUG("Remove audio output device (from g_sink_list):%s, decr:%s, class:%s\n", dev_id, dev_descr, dev_class);

        g_sink_list = remove_item(g_sink_list, dev_id);
    }

    g_free(dev_id);
    g_free(dev_descr);
    g_free(dev_class);
    g_free(dev_class_l);
    g_free(dev_caps_str);

    G_UNLOCK(g_source_list);
}

static void gstdev_add_to_list(GstDevice *dev) {
    gchar *dev_descr = NULL;
    gchar *dev_id = NULL;
    gchar *dev_class = NULL;
    gchar *dev_caps_str = NULL;

    LOG_DEBUG("Add new (input or output) device to the list.\n");

    G_LOCK(g_source_list);

    gstdev_read_fields(dev, &dev_id, &dev_descr, &dev_class, &dev_caps_str);

    // Create new DeviceItem
    DeviceItem *item = device_item_create(dev_id, dev_descr);

    gchar *dev_class_l = g_ascii_strdown(dev_class, -1);

    // Audio/Source
    if (g_str_has_prefix(dev_class_l, "audio/source")) {

        LOG_DEBUG("Add audio input device (to g_source_list):%s, decr:%s, class:%s\n", dev_id, dev_descr, dev_class);

        if (g_str_has_suffix(dev_id, ".monitor")) {

            // Monitor device for a real sound-card (we can record from this)
            item->type = AUDIO_SINK_MONITOR;

            // Set icon (audio card/loudspeaker)
            item->icon_name = g_strdup("loudspeaker.png");

        } else {
            // Device with audio input (microphone)
            item->type = AUDIO_INPUT;

            // Add "Microphone" to the device description
            gchar *descr = g_strdup_printf("%s %s", item->description, _("(Microphone)"));
            g_free(item->description);
            item->description = descr;

            // Find a suitable icon.
            // TODO: Make this test better.
            if (audio_sources_device_is_webcam(item->description)) {
                // Most likely a webcam
                item->icon_name = g_strdup("webcam.png");
            } else {
                // Ordinary microphone
                item->icon_name = g_strdup("microphone.png");
            }
        }

        // Add to g_sources list
        g_source_list = g_list_append(g_source_list, item);
    }


    // Audio/Sink
    else if (g_str_has_prefix(dev_class_l, "audio/sink")) {

        LOG_DEBUG("Add audio output device (to g_sink_list):%s, decr:%s, class:%s\n", dev_id, dev_descr, dev_class);

        // This is a sound sink, normally real audio card with loudspeakers
        item->type = AUDIO_SINK;

        // Add to g_sinks list
        g_sink_list = g_list_append(g_sink_list, item);

        // Set icon (audio card)
        item->icon_name = g_strdup("audio-card.png");
    }

    g_free(dev_id);
    g_free(dev_descr);
    g_free(dev_class);
    g_free(dev_class_l);
    g_free(dev_caps_str);

    G_UNLOCK(g_source_list);
}

void gstdev_fix_description() {
    // Remove "Monitor of" from the description text, and add "(Audio ouput)" word to it. It means "loudspeakers".

    // For example: "Monitor of Audio Stereo Card" becomes "Audio Stereo Card (Audio ouput)"
    // This is easier to understand.

    // Note: Check listing of these commands:
    //
    // Input devices:
    // pactl list | grep -A6  'Source #' | egrep "Name: |Description: "
    // pactl list short sources | cut -f2
    //
    // And sink (output) devices:
    // pactl list | grep -A6  'Sink #' | egrep "Name: |Description: "
    // pactl list short sinks | cut -f2

    G_LOCK(g_source_list);

    GList *item = g_list_first(g_source_list);
    while (item) {

        DeviceItem *rec = (DeviceItem*)item->data;

        // Take device-id without ".monitor" suffix
        if (g_str_has_suffix(rec->id, ".monitor")) {

            gchar *tmp = g_strdup(rec->id);
            gchar *p = g_strrstr(tmp, ".monitor");
            if (p) {
                // NULL terminate
                *p = '\0';

                // Find equivalent sink device (real audio card) and steal its description + add "(Audio output)" to it.
                DeviceItem *sink_rec = audio_sources_find_in_list(g_sink_list, tmp);
                if (sink_rec) {
                    g_free(rec->description);
                    rec->description = g_strdup_printf("%s %s", sink_rec->description, _("(Audio output)"));
                }
            }

            g_free(tmp);
        }

        item = g_list_next(item);
    }

    G_UNLOCK(g_source_list);
}

static void gstdev_get_devices() {

    LOG_DEBUG("Get list of audio input/output devices from GStreamer.\n");

    gstdev_clear_lists();

    // Set up device monitor
    if (!GST_IS_DEVICE_MONITOR(g_dev_monitor)) {
        g_dev_monitor = setup_raw_audio_source_device_monitor();
    }

    GList *list = gst_device_monitor_get_devices(g_dev_monitor);

    // Ref: http://code.metager.de/source/xref/freedesktop/gstreamer/gstreamer/gst/gstdevice.c

    GList *item = g_list_first(list);
    while (item) {
        GstDevice *dev = (GstDevice*)item->data;

        gstdev_add_to_list(dev);

        item = g_list_next(item);
    }

    g_list_free_full(list, (GDestroyNotify)gst_object_unref);

    // Remove "Monitor of" from the description text.
    gstdev_fix_description();
}