summaryrefslogtreecommitdiffstats
path: root/ext/pulse/pulsemixerctrl.c
blob: 8df084073191761ee71be316430ca6d6d64106ba (plain)
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
/*-*- Mode: C; c-basic-offset: 2 -*-*/

/*
 *  GStreamer pulseaudio plugin
 *
 *  Copyright (c) 2004-2008 Lennart Poettering
 *
 *  gst-pulse is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation; either version 2.1 of the
 *  License, or (at your option) any later version.
 *
 *  gst-pulse 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with gst-pulse; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 *  USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/gst.h>

#include "pulsemixerctrl.h"
#include "pulsemixertrack.h"
#include "pulseutil.h"

GST_DEBUG_CATEGORY_EXTERN (pulse_debug);
#define GST_CAT_DEFAULT pulse_debug

static void
gst_pulsemixer_ctrl_context_state_cb (pa_context * context, void *userdata)
{
  GstPulseMixerCtrl *c = GST_PULSEMIXER_CTRL (userdata);

  /* Called from the background thread! */

  switch (pa_context_get_state (context)) {
    case PA_CONTEXT_READY:
    case PA_CONTEXT_TERMINATED:
    case PA_CONTEXT_FAILED:
      pa_threaded_mainloop_signal (c->mainloop, 0);
      break;

    case PA_CONTEXT_UNCONNECTED:
    case PA_CONTEXT_CONNECTING:
    case PA_CONTEXT_AUTHORIZING:
    case PA_CONTEXT_SETTING_NAME:
      break;
  }
}

static void
gst_pulsemixer_ctrl_sink_info_cb (pa_context * context, const pa_sink_info * i,
    int eol, void *userdata)
{
  GstPulseMixerCtrl *c = userdata;

  /* Called from the background thread! */

  if (c->outstandig_queries > 0)
    c->outstandig_queries--;

  if (c->ignore_queries > 0 || c->time_event) {

    if (c->ignore_queries > 0)
      c->ignore_queries--;

    return;
  }

  if (!i && eol < 0) {
    c->operation_success = FALSE;
    pa_threaded_mainloop_signal (c->mainloop, 0);
    return;
  }

  if (eol)
    return;

  g_free (c->name);
  g_free (c->description);
  c->name = g_strdup (i->name);
  c->description = g_strdup (i->description);
  c->index = i->index;
  c->channel_map = i->channel_map;
  c->volume = i->volume;
  c->muted = !!i->mute;
  c->type = GST_PULSEMIXER_SINK;

  if (c->track) {
    GstMixerTrackFlags flags = c->track->flags;

    flags =
        (flags & ~GST_MIXER_TRACK_MUTE) | (c->muted ? GST_MIXER_TRACK_MUTE : 0);
    c->track->flags = flags;
  }

  c->operation_success = TRUE;
  pa_threaded_mainloop_signal (c->mainloop, 0);
}

static void
gst_pulsemixer_ctrl_source_info_cb (pa_context * context,
    const pa_source_info * i, int eol, void *userdata)
{
  GstPulseMixerCtrl *c = userdata;

  /* Called from the background thread! */

  if (c->outstandig_queries > 0)
    c->outstandig_queries--;

  if (c->ignore_queries > 0 || c->time_event) {

    if (c->ignore_queries > 0)
      c->ignore_queries--;

    return;
  }

  if (!i && eol < 0) {
    c->operation_success = FALSE;
    pa_threaded_mainloop_signal (c->mainloop, 0);
    return;
  }

  if (eol)
    return;

  g_free (c->name);
  g_free (c->description);
  c->name = g_strdup (i->name);
  c->description = g_strdup (i->description);
  c->index = i->index;
  c->channel_map = i->channel_map;
  c->volume = i->volume;
  c->muted = !!i->mute;
  c->type = GST_PULSEMIXER_SOURCE;

  if (c->track) {
    GstMixerTrackFlags flags = c->track->flags;

    flags =
        (flags & ~GST_MIXER_TRACK_MUTE) | (c->muted ? GST_MIXER_TRACK_MUTE : 0);
    c->track->flags = flags;
  }

  c->operation_success = TRUE;
  pa_threaded_mainloop_signal (c->mainloop, 0);
}

static void
gst_pulsemixer_ctrl_subscribe_cb (pa_context * context,
    pa_subscription_event_type_t t, uint32_t idx, void *userdata)
{
  GstPulseMixerCtrl *c = GST_PULSEMIXER_CTRL (userdata);
  pa_operation *o = NULL;

  /* Called from the background thread! */

  if (c->index != idx)
    return;

  if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_CHANGE)
    return;

  if (c->type == GST_PULSEMIXER_SINK)
    o = pa_context_get_sink_info_by_index (c->context, c->index,
        gst_pulsemixer_ctrl_sink_info_cb, c);
  else
    o = pa_context_get_source_info_by_index (c->context, c->index,
        gst_pulsemixer_ctrl_source_info_cb, c);

  if (!o) {
    GST_WARNING_OBJECT (c->object, "Failed to get sink info: %s",
        pa_strerror (pa_context_errno (c->context)));
    return;
  }

  pa_operation_unref (o);

  c->outstandig_queries++;
}

static void
gst_pulsemixer_ctrl_success_cb (pa_context * context, int success,
    void *userdata)
{
  GstPulseMixerCtrl *c = (GstPulseMixerCtrl *) userdata;

  c->operation_success = !!success;
  pa_threaded_mainloop_signal (c->mainloop, 0);
}

#define CHECK_DEAD_GOTO(c, label)                                       \
  G_STMT_START {                                                        \
    if (!(c)->context ||                                                \
        !PA_CONTEXT_IS_GOOD(pa_context_get_state((c)->context))) {      \
      GST_WARNING_OBJECT ((c)->object, "Not connected: %s",             \
                          (c)->context ? pa_strerror(pa_context_errno((c)->context)) : "NULL"); \
      goto label;                                                       \
    }                                                                   \
  } G_STMT_END

static gboolean
gst_pulsemixer_ctrl_open (GstPulseMixerCtrl * c)
{
  int e;
  gchar *name = gst_pulse_client_name ();
  pa_operation *o = NULL;

  g_assert (c);

  GST_DEBUG_OBJECT (c->object, "ctrl open");

  c->mainloop = pa_threaded_mainloop_new ();
  g_assert (c->mainloop);

  e = pa_threaded_mainloop_start (c->mainloop);
  g_assert (e == 0);

  pa_threaded_mainloop_lock (c->mainloop);

  if (!(c->context =
          pa_context_new (pa_threaded_mainloop_get_api (c->mainloop), name))) {
    GST_WARNING_OBJECT (c->object, "Failed to create context");
    goto unlock_and_fail;
  }

  pa_context_set_state_callback (c->context,
      gst_pulsemixer_ctrl_context_state_cb, c);
  pa_context_set_subscribe_callback (c->context,
      gst_pulsemixer_ctrl_subscribe_cb, c);

  if (pa_context_connect (c->context, c->server, 0, NULL) < 0) {
    GST_WARNING_OBJECT (c->object, "Failed to connect context: %s",
        pa_strerror (pa_context_errno (c->context)));
    goto unlock_and_fail;
  }

  /* Wait until the context is ready */
  pa_threaded_mainloop_wait (c->mainloop);

  if (pa_context_get_state (c->context) != PA_CONTEXT_READY) {
    GST_WARNING_OBJECT (c->object, "Failed to connect context: %s",
        pa_strerror (pa_context_errno (c->context)));
    goto unlock_and_fail;
  }

  /* Subscribe to events */

  if (!(o =
          pa_context_subscribe (c->context,
              PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SOURCE,
              gst_pulsemixer_ctrl_success_cb, c))) {
    GST_WARNING_OBJECT (c->object, "Failed to subscribe to events: %s",
        pa_strerror (pa_context_errno (c->context)));
    goto unlock_and_fail;
  }

  c->operation_success = FALSE;
  while (pa_operation_get_state (o) != PA_OPERATION_DONE) {
    pa_threaded_mainloop_wait (c->mainloop);
    CHECK_DEAD_GOTO (c, unlock_and_fail);
  }

  if (!c->operation_success) {
    GST_WARNING_OBJECT (c->object, "Failed to subscribe to events: %s",
        pa_strerror (pa_context_errno (c->context)));
    goto unlock_and_fail;
  }
  pa_operation_unref (o);
  o = NULL;

  /* Get sink info */

  if (c->type == GST_PULSEMIXER_UNKNOWN || c->type == GST_PULSEMIXER_SINK) {
    GST_WARNING_OBJECT (c->object, "Get info for '%s'", c->device);
    if (!(o =
            pa_context_get_sink_info_by_name (c->context, c->device,
                gst_pulsemixer_ctrl_sink_info_cb, c))) {
      GST_WARNING_OBJECT (c->object, "Failed to get sink info: %s",
          pa_strerror (pa_context_errno (c->context)));
      goto unlock_and_fail;
    }

    c->operation_success = FALSE;
    while (pa_operation_get_state (o) != PA_OPERATION_DONE) {
      pa_threaded_mainloop_wait (c->mainloop);
      CHECK_DEAD_GOTO (c, unlock_and_fail);
    }

    pa_operation_unref (o);
    o = NULL;

    if (!c->operation_success && (c->type == GST_PULSEMIXER_SINK
            || pa_context_errno (c->context) != PA_ERR_NOENTITY)) {
      GST_WARNING_OBJECT (c->object, "Failed to get sink info: %s",
          pa_strerror (pa_context_errno (c->context)));
      goto unlock_and_fail;
    }
  }

  if (c->type == GST_PULSEMIXER_UNKNOWN || c->type == GST_PULSEMIXER_SOURCE) {
    if (!(o =
            pa_context_get_source_info_by_name (c->context, c->device,
                gst_pulsemixer_ctrl_source_info_cb, c))) {
      GST_WARNING_OBJECT (c->object, "Failed to get source info: %s",
          pa_strerror (pa_context_errno (c->context)));
      goto unlock_and_fail;
    }

    c->operation_success = FALSE;
    while (pa_operation_get_state (o) != PA_OPERATION_DONE) {
      pa_threaded_mainloop_wait (c->mainloop);
      CHECK_DEAD_GOTO (c, unlock_and_fail);
    }

    pa_operation_unref (o);
    o = NULL;

    if (!c->operation_success) {
      GST_WARNING_OBJECT (c->object, "Failed to get source info: %s",
          pa_strerror (pa_context_errno (c->context)));
      goto unlock_and_fail;
    }
  }

  g_assert (c->type != GST_PULSEMIXER_UNKNOWN);

  c->track = gst_pulsemixer_track_new (c);
  c->tracklist = g_list_append (c->tracklist, c->track);

  pa_threaded_mainloop_unlock (c->mainloop);
  g_free (name);

  return TRUE;

unlock_and_fail:

  if (o)
    pa_operation_unref (o);

  if (c->mainloop)
    pa_threaded_mainloop_unlock (c->mainloop);

  g_free (name);

  return FALSE;
}

static void
gst_pulsemixer_ctrl_close (GstPulseMixerCtrl * c)
{
  g_assert (c);

  GST_DEBUG_OBJECT (c->object, "ctrl close");

  if (c->mainloop)
    pa_threaded_mainloop_stop (c->mainloop);

  if (c->context) {
    pa_context_disconnect (c->context);
    pa_context_unref (c->context);
    c->context = NULL;
  }

  if (c->mainloop) {
    pa_threaded_mainloop_free (c->mainloop);
    c->mainloop = NULL;
    c->time_event = NULL;
  }

  if (c->tracklist) {
    g_list_free (c->tracklist);
    c->tracklist = NULL;
  }

  if (c->track) {
    GST_PULSEMIXER_TRACK (c->track)->control = NULL;
    g_object_unref (c->track);
    c->track = NULL;
  }
}

GstPulseMixerCtrl *
gst_pulsemixer_ctrl_new (GObject * object, const gchar * server,
    const gchar * device, GstPulseMixerType type)
{
  GstPulseMixerCtrl *c = NULL;

  GST_DEBUG_OBJECT (object, "new mixer ctrl for %s", device);
  c = g_new (GstPulseMixerCtrl, 1);
  c->object = g_object_ref (object);
  c->tracklist = NULL;
  c->server = g_strdup (server);
  c->device = g_strdup (device);
  c->mainloop = NULL;
  c->context = NULL;
  c->track = NULL;
  c->ignore_queries = c->outstandig_queries = 0;

  pa_cvolume_mute (&c->volume, PA_CHANNELS_MAX);
  pa_channel_map_init (&c->channel_map);
  c->muted = FALSE;
  c->index = PA_INVALID_INDEX;
  c->type = type;
  c->name = NULL;
  c->description = NULL;

  c->time_event = NULL;
  c->update_volume = c->update_mute = FALSE;

  if (!(gst_pulsemixer_ctrl_open (c))) {
    gst_pulsemixer_ctrl_free (c);
    return NULL;
  }

  return c;
}

void
gst_pulsemixer_ctrl_free (GstPulseMixerCtrl * c)
{
  g_assert (c);

  gst_pulsemixer_ctrl_close (c);

  g_free (c->server);
  g_free (c->device);
  g_free (c->name);
  g_free (c->description);
  g_object_unref (c->object);
  g_free (c);
}

const GList *
gst_pulsemixer_ctrl_list_tracks (GstPulseMixerCtrl * c)
{
  g_assert (c);

  return c->tracklist;
}

static void
gst_pulsemixer_ctrl_timeout_event (pa_mainloop_api * a, pa_time_event * e,
    const struct timeval *tv, void *userdata)
{
  pa_operation *o;
  GstPulseMixerCtrl *c = GST_PULSEMIXER_CTRL (userdata);

  if (c->update_volume) {
    if (c->type == GST_PULSEMIXER_SINK)
      o = pa_context_set_sink_volume_by_index (c->context, c->index, &c->volume,
          NULL, NULL);
    else
      o = pa_context_set_source_volume_by_index (c->context, c->index,
          &c->volume, NULL, NULL);

    if (!o)
      GST_WARNING_OBJECT (c->object, "Failed to set device volume: %s",
          pa_strerror (pa_context_errno (c->context)));
    else
      pa_operation_unref (o);

    c->update_volume = FALSE;
  }

  if (c->update_mute) {
    if (c->type == GST_PULSEMIXER_SINK)
      o = pa_context_set_sink_mute_by_index (c->context, c->index, c->muted,
          NULL, NULL);
    else
      o = pa_context_set_source_mute_by_index (c->context, c->index, c->muted,
          NULL, NULL);

    if (!o)
      GST_WARNING_OBJECT (c->object, "Failed to set device mute: %s",
          pa_strerror (pa_context_errno (c->context)));
    else
      pa_operation_unref (o);

    c->update_mute = FALSE;
  }

  /* Make sure that all outstanding queries are being ignored */
  c->ignore_queries = c->outstandig_queries;

  g_assert (e == c->time_event);
  a->time_free (e);
  c->time_event = NULL;
}

#define UPDATE_DELAY 50000

static void
restart_time_event (GstPulseMixerCtrl * c)
{
  struct timeval tv;
  pa_mainloop_api *api;

  g_assert (c);

  if (c->time_event)
    return;

  /* Updating the volume too often will cause a lot of traffic
   * when accessing a networked server. Therefore we make sure
   * to update the volume only once every 50ms */

  api = pa_threaded_mainloop_get_api (c->mainloop);

  c->time_event =
      api->time_new (api, pa_timeval_add (pa_gettimeofday (&tv), UPDATE_DELAY),
      gst_pulsemixer_ctrl_timeout_event, c);
}

void
gst_pulsemixer_ctrl_set_volume (GstPulseMixerCtrl * c, GstMixerTrack * track,
    gint * volumes)
{
  pa_cvolume v;
  int i;

  g_assert (c);
  g_assert (track == c->track);

  pa_threaded_mainloop_lock (c->mainloop);

  for (i = 0; i < c->channel_map.channels; i++)
    v.values[i] = (pa_volume_t) volumes[i];

  v.channels = c->channel_map.channels;

  c->volume = v;
  c->update_volume = TRUE;

  restart_time_event (c);

  pa_threaded_mainloop_unlock (c->mainloop);
}

void
gst_pulsemixer_ctrl_get_volume (GstPulseMixerCtrl * c, GstMixerTrack * track,
    gint * volumes)
{
  int i;

  g_assert (c);
  g_assert (track == c->track);

  pa_threaded_mainloop_lock (c->mainloop);

  for (i = 0; i < c->channel_map.channels; i++)
    volumes[i] = c->volume.values[i];

  pa_threaded_mainloop_unlock (c->mainloop);
}

void
gst_pulsemixer_ctrl_set_record (GstPulseMixerCtrl * c, GstMixerTrack * track,
    gboolean record)
{
  g_assert (c);
  g_assert (track == c->track);
}

void
gst_pulsemixer_ctrl_set_mute (GstPulseMixerCtrl * c, GstMixerTrack * track,
    gboolean mute)
{
  g_assert (c);
  g_assert (track == c->track);

  pa_threaded_mainloop_lock (c->mainloop);

  c->muted = mute;
  c->update_mute = TRUE;

  if (c->track) {
    GstMixerTrackFlags flags = c->track->flags;

    flags =
        (flags & ~GST_MIXER_TRACK_MUTE) | (c->muted ? GST_MIXER_TRACK_MUTE : 0);
    c->track->flags = flags;
  }

  restart_time_event (c);

  pa_threaded_mainloop_unlock (c->mainloop);
}