1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
3
* Copyright (C) 2006-2008 Lennart Poettering
4
* Copyright (C) 2008 Sjoerd Simons <sjoerd@luon.net>
5
* Copyright (C) 2008 William Jon McCann
6
* Copyright (C) 2012 Conor Curran
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31
#include <glib/gi18n-lib.h>
33
#include <pulse/pulseaudio.h>
34
#include <pulse/glib-mainloop.h>
35
#include <pulse/ext-stream-restore.h>
37
#include "gvc-mixer-control.h"
38
#include "gvc-mixer-sink.h"
39
#include "gvc-mixer-source.h"
40
#include "gvc-mixer-sink-input.h"
41
#include "gvc-mixer-source-output.h"
42
#include "gvc-mixer-event-role.h"
43
#include "gvc-mixer-card.h"
44
#include "gvc-mixer-card-private.h"
45
#include "gvc-channel-map-private.h"
46
#include "gvc-mixer-control-private.h"
47
#include "gvc-mixer-ui-device.h"
49
#define GVC_MIXER_CONTROL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GVC_TYPE_MIXER_CONTROL, GvcMixerControlPrivate))
51
#define RECONNECT_DELAY 5
58
struct GvcMixerControlPrivate
60
pa_glib_mainloop *pa_mainloop;
61
pa_mainloop_api *pa_api;
62
pa_context *pa_context;
67
gboolean default_sink_is_set;
68
guint default_sink_id;
69
char *default_sink_name;
70
gboolean default_source_is_set;
71
guint default_source_id;
72
char *default_source_name;
74
gboolean event_sink_input_is_set;
75
guint event_sink_input_id;
77
GHashTable *all_streams;
78
GHashTable *sinks; /* fixed outputs */
79
GHashTable *sources; /* fixed inputs */
80
GHashTable *sink_inputs; /* routable output streams */
81
GHashTable *source_outputs; /* routable input streams */
85
GvcMixerStream *new_default_sink_stream; /* new default sink stream, used in gvc_mixer_control_set_default_sink () */
86
GvcMixerStream *new_default_source_stream; /* new default source stream, used in gvc_mixer_control_set_default_source () */
88
GHashTable *ui_outputs; /* UI visible outputs */
89
GHashTable *ui_inputs; /* UI visible inputs */
91
/* When we change profile on a device that is not the server default sink,
92
* it will jump back to the default sink set by the server to prevent the
93
* audio setup from being 'outputless'.
95
* All well and good but then when we get the new stream created for the
96
* new profile how do we know that this is the intended default or selected
97
* device the user wishes to use. */
98
guint profile_swapping_device_id;
100
GvcMixerControlState state;
109
DEFAULT_SINK_CHANGED,
110
DEFAULT_SOURCE_CHANGED,
111
ACTIVE_OUTPUT_UPDATE,
120
static guint signals [LAST_SIGNAL] = { 0, };
122
static void gvc_mixer_control_class_init (GvcMixerControlClass *klass);
123
static void gvc_mixer_control_init (GvcMixerControl *mixer_control);
124
static void gvc_mixer_control_finalize (GObject *object);
126
G_DEFINE_TYPE (GvcMixerControl, gvc_mixer_control, G_TYPE_OBJECT)
129
gvc_mixer_control_get_pa_context (GvcMixerControl *control)
131
g_return_val_if_fail (GVC_IS_MIXER_CONTROL (control), NULL);
132
return control->priv->pa_context;
136
* gvc_mixer_control_get_event_sink_input:
140
* Returns: (transfer none):
143
gvc_mixer_control_get_event_sink_input (GvcMixerControl *control)
145
GvcMixerStream *stream;
147
g_return_val_if_fail (GVC_IS_MIXER_CONTROL (control), NULL);
149
stream = g_hash_table_lookup (control->priv->all_streams,
150
GUINT_TO_POINTER (control->priv->event_sink_input_id));
156
gvc_mixer_control_stream_restore_cb (pa_context *c,
157
GvcMixerStream *new_stream,
158
const pa_ext_stream_restore_info *info,
159
GvcMixerControl *control)
162
pa_ext_stream_restore_info new_info;
164
if (new_stream == NULL)
167
new_info.name = info->name;
168
new_info.channel_map = info->channel_map;
169
new_info.volume = info->volume;
170
new_info.mute = info->mute;
172
new_info.device = gvc_mixer_stream_get_name (new_stream);
174
o = pa_ext_stream_restore_write (control->priv->pa_context,
180
g_warning ("pa_ext_stream_restore_write() failed: %s",
181
pa_strerror (pa_context_errno (control->priv->pa_context)));
185
g_debug ("Changed default device for %s to %s", info->name, new_info.device);
187
pa_operation_unref (o);
191
gvc_mixer_control_stream_restore_sink_cb (pa_context *c,
192
const pa_ext_stream_restore_info *info,
196
GvcMixerControl *control = (GvcMixerControl *) userdata;
197
if (eol || info == NULL || !g_str_has_prefix(info->name, "sink-input-by"))
199
gvc_mixer_control_stream_restore_cb (c, control->priv->new_default_sink_stream, info, control);
203
gvc_mixer_control_stream_restore_source_cb (pa_context *c,
204
const pa_ext_stream_restore_info *info,
208
GvcMixerControl *control = (GvcMixerControl *) userdata;
209
if (eol || info == NULL || !g_str_has_prefix(info->name, "source-output-by"))
211
gvc_mixer_control_stream_restore_cb (c, control->priv->new_default_source_stream, info, control);
215
* gvc_mixer_control_lookup_device_from_stream:
218
* Returns: (transfer none): a #GvcUIDevice or %NULL
221
gvc_mixer_control_lookup_device_from_stream (GvcMixerControl *control,
222
GvcMixerStream *stream)
225
gboolean is_network_stream;
227
GvcMixerUIDevice *ret;
229
if (GVC_IS_MIXER_SOURCE (stream))
230
devices = g_hash_table_get_values (control->priv->ui_inputs);
232
devices = g_hash_table_get_values (control->priv->ui_outputs);
235
ports = gvc_mixer_stream_get_ports (stream);
236
is_network_stream = (ports == NULL);
238
for (d = devices; d != NULL; d = d->next) {
239
GvcMixerUIDevice *device = d->data;
240
gint stream_id = G_MAXINT;
242
g_object_get (G_OBJECT (device),
243
"stream-id", &stream_id,
246
if (is_network_stream &&
247
stream_id == gvc_mixer_stream_get_id (stream)) {
248
g_debug ("lookup device from stream - %s - it is a network_stream ",
249
gvc_mixer_ui_device_get_description (device));
252
} else if (!is_network_stream) {
253
const GvcMixerStreamPort *port;
254
port = gvc_mixer_stream_get_port (stream);
256
if (stream_id == gvc_mixer_stream_get_id (stream) &&
257
g_strcmp0 (gvc_mixer_ui_device_get_port (device),
259
g_debug ("lookup-device-from-stream found device: device description '%s', device port = '%s', device stream id %i AND stream port = '%s' stream id '%u' and stream description '%s'",
260
gvc_mixer_ui_device_get_description (device),
261
gvc_mixer_ui_device_get_port (device),
264
gvc_mixer_stream_get_id (stream),
265
gvc_mixer_stream_get_description (stream));
272
g_debug ("gvc_mixer_control_lookup_device_from_stream - Could not find a device for stream '%s'",gvc_mixer_stream_get_description (stream));
274
g_list_free (devices);
280
gvc_mixer_control_set_default_sink (GvcMixerControl *control,
281
GvcMixerStream *stream)
285
g_return_val_if_fail (GVC_IS_MIXER_CONTROL (control), FALSE);
286
g_return_val_if_fail (GVC_IS_MIXER_STREAM (stream), FALSE);
288
g_debug ("about to set default sink on server");
289
o = pa_context_set_default_sink (control->priv->pa_context,
290
gvc_mixer_stream_get_name (stream),
294
g_warning ("pa_context_set_default_sink() failed: %s",
295
pa_strerror (pa_context_errno (control->priv->pa_context)));
299
pa_operation_unref (o);
301
control->priv->new_default_sink_stream = stream;
302
g_object_add_weak_pointer (G_OBJECT (stream), (gpointer *) &control->priv->new_default_sink_stream);
304
o = pa_ext_stream_restore_read (control->priv->pa_context,
305
gvc_mixer_control_stream_restore_sink_cb,
309
g_warning ("pa_ext_stream_restore_read() failed: %s",
310
pa_strerror (pa_context_errno (control->priv->pa_context)));
314
pa_operation_unref (o);
320
gvc_mixer_control_set_default_source (GvcMixerControl *control,
321
GvcMixerStream *stream)
323
GvcMixerUIDevice* input;
326
g_return_val_if_fail (GVC_IS_MIXER_CONTROL (control), FALSE);
327
g_return_val_if_fail (GVC_IS_MIXER_STREAM (stream), FALSE);
329
o = pa_context_set_default_source (control->priv->pa_context,
330
gvc_mixer_stream_get_name (stream),
334
g_warning ("pa_context_set_default_source() failed");
338
pa_operation_unref (o);
340
control->priv->new_default_source_stream = stream;
341
g_object_add_weak_pointer (G_OBJECT (stream), (gpointer *) &control->priv->new_default_source_stream);
343
o = pa_ext_stream_restore_read (control->priv->pa_context,
344
gvc_mixer_control_stream_restore_source_cb,
348
g_warning ("pa_ext_stream_restore_read() failed: %s",
349
pa_strerror (pa_context_errno (control->priv->pa_context)));
353
pa_operation_unref (o);
355
/* source change successful, update the UI. */
356
input = gvc_mixer_control_lookup_device_from_stream (control, stream);
357
g_signal_emit (G_OBJECT (control),
358
signals[ACTIVE_INPUT_UPDATE],
360
gvc_mixer_ui_device_get_id (input));
366
* gvc_mixer_control_get_default_sink:
370
* Returns: (transfer none):
373
gvc_mixer_control_get_default_sink (GvcMixerControl *control)
375
GvcMixerStream *stream;
377
g_return_val_if_fail (GVC_IS_MIXER_CONTROL (control), NULL);
379
if (control->priv->default_sink_is_set) {
380
stream = g_hash_table_lookup (control->priv->all_streams,
381
GUINT_TO_POINTER (control->priv->default_sink_id));
390
* gvc_mixer_control_get_default_source:
394
* Returns: (transfer none):
397
gvc_mixer_control_get_default_source (GvcMixerControl *control)
399
GvcMixerStream *stream;
401
g_return_val_if_fail (GVC_IS_MIXER_CONTROL (control), NULL);
403
if (control->priv->default_source_is_set) {
404
stream = g_hash_table_lookup (control->priv->all_streams,
405
GUINT_TO_POINTER (control->priv->default_source_id));
414
gvc_mixer_control_lookup_id (GHashTable *hash_table,
417
return g_hash_table_lookup (hash_table,
418
GUINT_TO_POINTER (id));
422
* gvc_mixer_control_lookup_stream_id:
427
* Returns: (transfer none):
430
gvc_mixer_control_lookup_stream_id (GvcMixerControl *control,
433
g_return_val_if_fail (GVC_IS_MIXER_CONTROL (control), NULL);
435
return gvc_mixer_control_lookup_id (control->priv->all_streams, id);
439
* gvc_mixer_control_lookup_card_id:
444
* Returns: (transfer none):
447
gvc_mixer_control_lookup_card_id (GvcMixerControl *control,
450
g_return_val_if_fail (GVC_IS_MIXER_CONTROL (control), NULL);
452
return gvc_mixer_control_lookup_id (control->priv->cards, id);
456
* gvc_mixer_control_lookup_output_id:
459
* Returns: (transfer none):
462
gvc_mixer_control_lookup_output_id (GvcMixerControl *control,
465
g_return_val_if_fail (GVC_IS_MIXER_CONTROL (control), NULL);
467
return gvc_mixer_control_lookup_id (control->priv->ui_outputs, id);
471
* gvc_mixer_control_lookup_input_id:
474
* Returns: (transfer none):
477
gvc_mixer_control_lookup_input_id (GvcMixerControl *control,
480
g_return_val_if_fail (GVC_IS_MIXER_CONTROL (control), NULL);
482
return gvc_mixer_control_lookup_id (control->priv->ui_inputs, id);
486
* gvc_mixer_control_get_stream_from_device:
489
* Returns: (transfer none):
492
gvc_mixer_control_get_stream_from_device (GvcMixerControl *control,
493
GvcMixerUIDevice *device)
497
g_return_val_if_fail (GVC_IS_MIXER_CONTROL (control), NULL);
498
g_return_val_if_fail (GVC_IS_MIXER_UI_DEVICE (device), NULL);
500
stream_id = gvc_mixer_ui_device_get_stream_id (device);
502
if (stream_id == GVC_MIXER_UI_DEVICE_INVALID) {
503
g_debug ("gvc_mixer_control_get_stream_from_device - device has a null stream");
506
return gvc_mixer_control_lookup_stream_id (control, stream_id);
510
* gvc_mixer_control_change_profile_on_selected_device:
513
* @profile: Can be null if any profile present on this port is okay
514
* Returns: This method will attempt to swap the profile on the card of
515
* the device with given profile name. If successfull it will set the
516
* preferred profile on that device so as we know the next time the user
517
* moves to that device it should have this profile active.
520
gvc_mixer_control_change_profile_on_selected_device (GvcMixerControl *control,
521
GvcMixerUIDevice *device,
522
const gchar *profile)
524
const gchar *best_profile;
525
GvcMixerCardProfile *current_profile;
528
g_object_get (G_OBJECT (device), "card", &card, NULL);
529
current_profile = gvc_mixer_card_get_profile (card);
532
best_profile = gvc_mixer_ui_device_get_best_profile (device, profile, current_profile->profile);
534
best_profile = profile;
536
g_assert (best_profile);
538
g_debug ("Selected '%s', moving to profile '%s' on card '%s' on stream id %i",
539
profile ? profile : "(any)", best_profile,
540
gvc_mixer_card_get_name (card),
541
gvc_mixer_ui_device_get_stream_id (device));
543
g_debug ("default sink name = %s and default sink id %u",
544
control->priv->default_sink_name,
545
control->priv->default_sink_id);
547
control->priv->profile_swapping_device_id = gvc_mixer_ui_device_get_id (device);
549
if (gvc_mixer_card_change_profile (card, best_profile)) {
550
gvc_mixer_ui_device_set_user_preferred_profile (device, best_profile);
557
* gvc_mixer_control_change_output:
560
* This method is called from the UI when the user selects a previously unselected device.
561
* - Firstly it queries the stream from the device.
562
* - It assumes that if the stream is null that it cannot be a bluetooth or network stream (they never show unless they have valid sinks and sources)
563
* In the scenario of a NULL stream on the device
564
* - It fetches the device's preferred profile or if NUll the profile with the highest priority on that device.
565
* - It then caches this device in control->priv->cached_desired_output_id so that when the update_sink triggered
566
* from when we attempt to change profile we will know exactly what device to highlight on that stream.
567
* - It attempts to swap the profile on the card from that device and returns.
568
* - Next, it handles network or bluetooth streams that only require their stream to be made the default.
569
* - Next it deals with port changes so if the stream's active port is not the same as the port on the device
570
* it will attempt to change the port on that stream to be same as the device. If this fails it will return.
571
* - Finally it will set this new stream to be the default stream and emit a signal for the UI confirming the active output device.
574
gvc_mixer_control_change_output (GvcMixerControl *control,
575
GvcMixerUIDevice* output)
577
GvcMixerStream *stream;
578
GvcMixerStream *default_stream;
579
const GvcMixerStreamPort *active_port;
580
const gchar *output_port;
582
g_debug ("control change output");
584
stream = gvc_mixer_control_get_stream_from_device (control, output);
585
if (stream == NULL) {
586
gvc_mixer_control_change_profile_on_selected_device (control,
591
/* Handle a network sink as a portless or cardless device */
592
if (!gvc_mixer_ui_device_has_ports (output)) {
593
g_debug ("Did we try to move to a software/bluetooth sink ?");
594
if (gvc_mixer_control_set_default_sink (control, stream)) {
595
/* sink change was successful, update the UI.*/
596
g_signal_emit (G_OBJECT (control),
597
signals[ACTIVE_OUTPUT_UPDATE],
599
gvc_mixer_ui_device_get_id (output));
602
g_warning ("Failed to set default sink with stream from output %s",
603
gvc_mixer_ui_device_get_description (output));
608
active_port = gvc_mixer_stream_get_port (stream);
609
output_port = gvc_mixer_ui_device_get_port (output);
610
/* First ensure the correct port is active on the sink */
611
if (g_strcmp0 (active_port->port, output_port) != 0) {
612
g_debug ("Port change, switch to = %s", output_port);
613
if (gvc_mixer_stream_change_port (stream, output_port) == FALSE) {
614
g_warning ("Could not change port !");
619
default_stream = gvc_mixer_control_get_default_sink (control);
621
/* Finally if we are not on the correct stream, swap over. */
622
if (stream != default_stream) {
623
GvcMixerUIDevice* output;
625
g_debug ("Attempting to swap over to stream %s ",
626
gvc_mixer_stream_get_description (stream));
627
if (gvc_mixer_control_set_default_sink (control, stream)) {
628
output = gvc_mixer_control_lookup_device_from_stream (control, stream);
629
g_signal_emit (G_OBJECT (control),
630
signals[ACTIVE_OUTPUT_UPDATE],
632
gvc_mixer_ui_device_get_id (output));
634
/* If the move failed for some reason reset the UI. */
635
output = gvc_mixer_control_lookup_device_from_stream (control, default_stream);
636
g_signal_emit (G_OBJECT (control),
637
signals[ACTIVE_OUTPUT_UPDATE],
639
gvc_mixer_ui_device_get_id (output));
646
* gvc_mixer_control_change_input:
649
* This method is called from the UI when the user selects a previously unselected device.
650
* - Firstly it queries the stream from the device.
651
* - It assumes that if the stream is null that it cannot be a bluetooth or network stream (they never show unless they have valid sinks and sources)
652
* In the scenario of a NULL stream on the device
653
* - It fetches the device's preferred profile or if NUll the profile with the highest priority on that device.
654
* - It then caches this device in control->priv->cached_desired_input_id so that when the update_source triggered
655
* from when we attempt to change profile we will know exactly what device to highlight on that stream.
656
* - It attempts to swap the profile on the card from that device and returns.
657
* - Next, it handles network or bluetooth streams that only require their stream to be made the default.
658
* - Next it deals with port changes so if the stream's active port is not the same as the port on the device
659
* it will attempt to change the port on that stream to be same as the device. If this fails it will return.
660
* - Finally it will set this new stream to be the default stream and emit a signal for the UI confirming the active input device.
663
gvc_mixer_control_change_input (GvcMixerControl *control,
664
GvcMixerUIDevice* input)
666
GvcMixerStream *stream;
667
GvcMixerStream *default_stream;
668
const GvcMixerStreamPort *active_port;
669
const gchar *input_port;
671
stream = gvc_mixer_control_get_stream_from_device (control, input);
672
if (stream == NULL) {
673
gvc_mixer_control_change_profile_on_selected_device (control,
678
/* Handle a network sink as a portless/cardless device */
679
if (!gvc_mixer_ui_device_has_ports (input)) {
680
g_debug ("Did we try to move to a software/bluetooth source ?");
681
if (! gvc_mixer_control_set_default_source (control, stream)) {
682
g_warning ("Failed to set default source with stream from input %s",
683
gvc_mixer_ui_device_get_description (input));
688
active_port = gvc_mixer_stream_get_port (stream);
689
input_port = gvc_mixer_ui_device_get_port (input);
690
/* First ensure the correct port is active on the sink */
691
if (g_strcmp0 (active_port->port, input_port) != 0) {
692
g_debug ("Port change, switch to = %s", input_port);
693
if (gvc_mixer_stream_change_port (stream, input_port) == FALSE) {
694
g_warning ("Could not change port!");
699
default_stream = gvc_mixer_control_get_default_source (control);
701
/* Finally if we are not on the correct stream, swap over. */
702
if (stream != default_stream) {
703
g_debug ("change-input - attempting to swap over to stream %s",
704
gvc_mixer_stream_get_description (stream));
705
gvc_mixer_control_set_default_source (control, stream);
711
listify_hash_values_hfunc (gpointer key,
715
GSList **list = user_data;
717
*list = g_slist_prepend (*list, value);
721
gvc_name_collate (const char *namea,
724
if (nameb == NULL && namea == NULL)
731
return g_utf8_collate (namea, nameb);
735
gvc_card_collate (GvcMixerCard *a,
741
g_return_val_if_fail (a == NULL || GVC_IS_MIXER_CARD (a), 0);
742
g_return_val_if_fail (b == NULL || GVC_IS_MIXER_CARD (b), 0);
744
namea = gvc_mixer_card_get_name (a);
745
nameb = gvc_mixer_card_get_name (b);
747
return gvc_name_collate (namea, nameb);
751
* gvc_mixer_control_get_cards:
755
* Returns: (transfer container) (element-type Gvc.MixerCard):
758
gvc_mixer_control_get_cards (GvcMixerControl *control)
762
g_return_val_if_fail (GVC_IS_MIXER_CONTROL (control), NULL);
765
g_hash_table_foreach (control->priv->cards,
766
listify_hash_values_hfunc,
768
return g_slist_sort (retval, (GCompareFunc) gvc_card_collate);
772
gvc_stream_collate (GvcMixerStream *a,
778
g_return_val_if_fail (a == NULL || GVC_IS_MIXER_STREAM (a), 0);
779
g_return_val_if_fail (b == NULL || GVC_IS_MIXER_STREAM (b), 0);
781
namea = gvc_mixer_stream_get_name (a);
782
nameb = gvc_mixer_stream_get_name (b);
784
return gvc_name_collate (namea, nameb);
788
* gvc_mixer_control_get_streams:
792
* Returns: (transfer container) (element-type Gvc.MixerStream):
795
gvc_mixer_control_get_streams (GvcMixerControl *control)
799
g_return_val_if_fail (GVC_IS_MIXER_CONTROL (control), NULL);
802
g_hash_table_foreach (control->priv->all_streams,
803
listify_hash_values_hfunc,
805
return g_slist_sort (retval, (GCompareFunc) gvc_stream_collate);
809
* gvc_mixer_control_get_sinks:
813
* Returns: (transfer container) (element-type Gvc.MixerSink):
816
gvc_mixer_control_get_sinks (GvcMixerControl *control)
820
g_return_val_if_fail (GVC_IS_MIXER_CONTROL (control), NULL);
823
g_hash_table_foreach (control->priv->sinks,
824
listify_hash_values_hfunc,
826
return g_slist_sort (retval, (GCompareFunc) gvc_stream_collate);
830
* gvc_mixer_control_get_sources:
834
* Returns: (transfer container) (element-type Gvc.MixerSource):
837
gvc_mixer_control_get_sources (GvcMixerControl *control)
841
g_return_val_if_fail (GVC_IS_MIXER_CONTROL (control), NULL);
844
g_hash_table_foreach (control->priv->sources,
845
listify_hash_values_hfunc,
847
return g_slist_sort (retval, (GCompareFunc) gvc_stream_collate);
851
* gvc_mixer_control_get_sink_inputs:
855
* Returns: (transfer container) (element-type Gvc.MixerSinkInput):
858
gvc_mixer_control_get_sink_inputs (GvcMixerControl *control)
862
g_return_val_if_fail (GVC_IS_MIXER_CONTROL (control), NULL);
865
g_hash_table_foreach (control->priv->sink_inputs,
866
listify_hash_values_hfunc,
868
return g_slist_sort (retval, (GCompareFunc) gvc_stream_collate);
872
* gvc_mixer_control_get_source_outputs:
876
* Returns: (transfer container) (element-type Gvc.MixerSourceOutput):
879
gvc_mixer_control_get_source_outputs (GvcMixerControl *control)
883
g_return_val_if_fail (GVC_IS_MIXER_CONTROL (control), NULL);
886
g_hash_table_foreach (control->priv->source_outputs,
887
listify_hash_values_hfunc,
889
return g_slist_sort (retval, (GCompareFunc) gvc_stream_collate);
893
dec_outstanding (GvcMixerControl *control)
895
if (control->priv->n_outstanding <= 0) {
899
if (--control->priv->n_outstanding <= 0) {
900
control->priv->state = GVC_STATE_READY;
901
g_signal_emit (G_OBJECT (control), signals[STATE_CHANGED], 0, GVC_STATE_READY);
906
gvc_mixer_control_get_state (GvcMixerControl *control)
908
g_return_val_if_fail (GVC_IS_MIXER_CONTROL (control), FALSE);
910
return control->priv->state;
914
on_default_source_port_notify (GObject *object,
916
GvcMixerControl *control)
919
GvcMixerUIDevice *input;
921
g_object_get (object, "port", &port, NULL);
922
input = gvc_mixer_control_lookup_device_from_stream (control,
923
GVC_MIXER_STREAM (object));
925
g_debug ("on_default_source_port_notify - moved to port '%s' which SHOULD ?? correspond to output '%s'",
927
gvc_mixer_ui_device_get_description (input));
929
g_signal_emit (G_OBJECT (control),
930
signals[ACTIVE_INPUT_UPDATE],
932
gvc_mixer_ui_device_get_id (input));
939
_set_default_source (GvcMixerControl *control,
940
GvcMixerStream *stream)
944
if (stream == NULL) {
945
control->priv->default_source_id = 0;
946
control->priv->default_source_is_set = FALSE;
947
g_signal_emit (control,
948
signals[DEFAULT_SOURCE_CHANGED],
954
new_id = gvc_mixer_stream_get_id (stream);
956
if (control->priv->default_source_id != new_id) {
957
GvcMixerUIDevice *input;
958
control->priv->default_source_id = new_id;
959
control->priv->default_source_is_set = TRUE;
960
g_signal_emit (control,
961
signals[DEFAULT_SOURCE_CHANGED],
965
if (control->priv->default_source_is_set) {
966
g_signal_handlers_disconnect_by_func (gvc_mixer_control_get_default_source (control),
967
on_default_source_port_notify,
971
g_signal_connect (stream,
973
G_CALLBACK (on_default_source_port_notify),
976
input = gvc_mixer_control_lookup_device_from_stream (control, stream);
978
g_signal_emit (G_OBJECT (control),
979
signals[ACTIVE_INPUT_UPDATE],
981
gvc_mixer_ui_device_get_id (input));
986
on_default_sink_port_notify (GObject *object,
988
GvcMixerControl *control)
991
GvcMixerUIDevice *output;
993
g_object_get (object, "port", &port, NULL);
995
output = gvc_mixer_control_lookup_device_from_stream (control,
996
GVC_MIXER_STREAM (object));
997
if (output != NULL) {
998
g_debug ("on_default_sink_port_notify - moved to port %s - which SHOULD correspond to output %s",
1000
gvc_mixer_ui_device_get_description (output));
1001
g_signal_emit (G_OBJECT (control),
1002
signals[ACTIVE_OUTPUT_UPDATE],
1004
gvc_mixer_ui_device_get_id (output));
1010
_set_default_sink (GvcMixerControl *control,
1011
GvcMixerStream *stream)
1015
if (stream == NULL) {
1016
/* Don't tell front-ends about an unset default
1017
* sink if it's already unset */
1018
if (control->priv->default_sink_is_set == FALSE)
1020
control->priv->default_sink_id = 0;
1021
control->priv->default_sink_is_set = FALSE;
1022
g_signal_emit (control,
1023
signals[DEFAULT_SINK_CHANGED],
1029
new_id = gvc_mixer_stream_get_id (stream);
1031
if (control->priv->default_sink_id != new_id) {
1032
GvcMixerUIDevice *output;
1033
if (control->priv->default_sink_is_set) {
1034
g_signal_handlers_disconnect_by_func (gvc_mixer_control_get_default_sink (control),
1035
on_default_sink_port_notify,
1039
control->priv->default_sink_id = new_id;
1041
control->priv->default_sink_is_set = TRUE;
1042
g_signal_emit (control,
1043
signals[DEFAULT_SINK_CHANGED],
1047
g_signal_connect (stream,
1049
G_CALLBACK (on_default_sink_port_notify),
1052
output = gvc_mixer_control_lookup_device_from_stream (control, stream);
1054
g_debug ("active_sink change");
1056
g_signal_emit (G_OBJECT (control),
1057
signals[ACTIVE_OUTPUT_UPDATE],
1059
gvc_mixer_ui_device_get_id (output));
1064
_stream_has_name (gpointer key,
1065
GvcMixerStream *stream,
1070
t_name = gvc_mixer_stream_get_name (stream);
1074
&& strcmp (t_name, name) == 0) {
1081
static GvcMixerStream *
1082
find_stream_for_name (GvcMixerControl *control,
1085
GvcMixerStream *stream;
1087
stream = g_hash_table_find (control->priv->all_streams,
1088
(GHRFunc)_stream_has_name,
1094
update_default_source_from_name (GvcMixerControl *control,
1097
gboolean changed = FALSE;
1099
if ((control->priv->default_source_name == NULL
1101
|| (control->priv->default_source_name != NULL
1103
|| (name != NULL && strcmp (control->priv->default_source_name, name) != 0)) {
1108
GvcMixerStream *stream;
1110
g_free (control->priv->default_source_name);
1111
control->priv->default_source_name = g_strdup (name);
1113
stream = find_stream_for_name (control, name);
1114
_set_default_source (control, stream);
1119
update_default_sink_from_name (GvcMixerControl *control,
1122
gboolean changed = FALSE;
1124
if ((control->priv->default_sink_name == NULL
1126
|| (control->priv->default_sink_name != NULL
1128
|| (name != NULL && strcmp (control->priv->default_sink_name, name) != 0)) {
1133
GvcMixerStream *stream;
1134
g_free (control->priv->default_sink_name);
1135
control->priv->default_sink_name = g_strdup (name);
1137
stream = find_stream_for_name (control, name);
1138
_set_default_sink (control, stream);
1143
update_server (GvcMixerControl *control,
1144
const pa_server_info *info)
1146
if (info->default_source_name != NULL) {
1147
update_default_source_from_name (control, info->default_source_name);
1149
if (info->default_sink_name != NULL) {
1150
g_debug ("update server");
1151
update_default_sink_from_name (control, info->default_sink_name);
1156
remove_stream (GvcMixerControl *control,
1157
GvcMixerStream *stream)
1161
g_object_ref (stream);
1163
id = gvc_mixer_stream_get_id (stream);
1165
if (id == control->priv->default_sink_id) {
1166
_set_default_sink (control, NULL);
1167
} else if (id == control->priv->default_source_id) {
1168
_set_default_source (control, NULL);
1171
g_hash_table_remove (control->priv->all_streams,
1172
GUINT_TO_POINTER (id));
1173
g_signal_emit (G_OBJECT (control),
1174
signals[STREAM_REMOVED],
1176
gvc_mixer_stream_get_id (stream));
1177
g_object_unref (stream);
1181
add_stream (GvcMixerControl *control,
1182
GvcMixerStream *stream)
1184
g_hash_table_insert (control->priv->all_streams,
1185
GUINT_TO_POINTER (gvc_mixer_stream_get_id (stream)),
1187
g_signal_emit (G_OBJECT (control),
1188
signals[STREAM_ADDED],
1190
gvc_mixer_stream_get_id (stream));
1193
/* This method will match individual stream ports against its corresponding device
1195
* - iterates through our devices and finds the one where the card-id on the device is the same as the card-id on the stream
1196
* and the port-name on the device is the same as the streamport-name.
1197
* This should always find a match and is used exclusively by sync_devices().
1200
match_stream_with_devices (GvcMixerControl *control,
1201
GvcMixerStreamPort *stream_port,
1202
GvcMixerStream *stream)
1205
guint stream_card_id;
1207
gboolean in_possession = FALSE;
1209
stream_id = gvc_mixer_stream_get_id (stream);
1210
stream_card_id = gvc_mixer_stream_get_card_index (stream);
1212
devices = g_hash_table_get_values (GVC_IS_MIXER_SOURCE (stream) ? control->priv->ui_inputs : control->priv->ui_outputs);
1214
for (d = devices; d != NULL; d = d->next) {
1215
GvcMixerUIDevice *device;
1216
gint device_stream_id;
1217
gchar *device_port_name;
1224
g_object_get (G_OBJECT (device),
1225
"stream-id", &device_stream_id,
1228
"description", &description,
1229
"port-name", &device_port_name,
1232
card_id = gvc_mixer_card_get_index (card);
1234
g_debug ("Attempt to match_stream update_with_existing_outputs - Try description : '%s', origin : '%s', device port name : '%s', card : %p, AGAINST stream port: '%s', sink card id %i",
1242
if (stream_card_id == card_id &&
1243
g_strcmp0 (device_port_name, stream_port->port) == 0) {
1244
g_debug ("Match device with stream: We have a match with description: '%s', origin: '%s', cached already with device id %u, so set stream id to %i",
1247
gvc_mixer_ui_device_get_id (device),
1250
g_object_set (G_OBJECT (device),
1251
"stream-id", (gint)stream_id,
1253
in_possession = TRUE;
1256
g_free (device_port_name);
1258
g_free (description);
1260
if (in_possession == TRUE)
1264
g_list_free (devices);
1265
return in_possession;
1269
* This method attempts to match a sink or source with its relevant UI device.
1270
* GvcMixerStream can represent both a sink or source.
1271
* Using static card port introspection implies that we know beforehand what
1272
* outputs and inputs are available to the user.
1273
* But that does not mean that all of these inputs and outputs are available to be used.
1274
* For instance we might be able to see that there is a HDMI port available but if
1275
* we are on the default analog stereo output profile there is no valid sink for
1276
* that HDMI device. We first need to change profile and when update_sink() is called
1277
* only then can we match the new hdmi sink with its corresponding device.
1279
* Firstly it checks to see if the incoming stream has no ports.
1280
* - If a stream has no ports but has a valid card ID (bluetooth), it will attempt
1281
* to match the device with the stream using the card id.
1282
* - If a stream has no ports and no valid card id, it goes ahead and makes a new
1283
* device (software/network devices are only detectable at the sink/source level)
1284
* If the stream has ports it will match each port against the stream using match_stream_with_devices().
1286
* This method should always find a match.
1289
sync_devices (GvcMixerControl *control,
1290
GvcMixerStream* stream)
1292
/* Go through ports to see what outputs can be created. */
1293
const GList *stream_ports;
1294
const GList *n = NULL;
1295
gboolean is_output = !GVC_IS_MIXER_SOURCE (stream);
1296
gint stream_port_count = 0;
1298
stream_ports = gvc_mixer_stream_get_ports (stream);
1300
if (stream_ports == NULL) {
1301
GvcMixerUIDevice *device;
1302
/* Bluetooth, no ports but a valid card */
1303
if (gvc_mixer_stream_get_card_index (stream) != PA_INVALID_INDEX) {
1305
gboolean in_possession = FALSE;
1307
devices = g_hash_table_get_values (is_output ? control->priv->ui_outputs : control->priv->ui_inputs);
1309
for (d = devices; d != NULL; d = d->next) {
1315
g_object_get (G_OBJECT (device),
1318
card_id = gvc_mixer_card_get_index (card);
1319
g_debug ("sync devices, device description - '%s', device card id - %i, stream description - %s, stream card id - %i",
1320
gvc_mixer_ui_device_get_description (device),
1322
gvc_mixer_stream_get_description (stream),
1323
gvc_mixer_stream_get_card_index (stream));
1324
if (card_id == gvc_mixer_stream_get_card_index (stream)) {
1325
in_possession = TRUE;
1329
g_list_free (devices);
1331
if (!in_possession) {
1332
g_warning ("Couldn't match the portless stream (with card) - '%s' is it an input ? -> %i, streams card id -> %i",
1333
gvc_mixer_stream_get_description (stream),
1334
GVC_IS_MIXER_SOURCE (stream),
1335
gvc_mixer_stream_get_card_index (stream));
1339
g_object_set (G_OBJECT (device),
1340
"stream-id", (gint)gvc_mixer_stream_get_id (stream),
1341
"description", gvc_mixer_stream_get_description (stream),
1342
"origin", "", /*Leave it empty for these special cases*/
1344
"port-available", TRUE,
1346
} else { /* Network sink/source has no ports and no card. */
1349
object = g_object_new (GVC_TYPE_MIXER_UI_DEVICE,
1350
"stream-id", (gint)gvc_mixer_stream_get_id (stream),
1351
"description", gvc_mixer_stream_get_description (stream),
1352
"origin", "", /* Leave it empty for these special cases */
1354
"port-available", TRUE,
1356
device = GVC_MIXER_UI_DEVICE (object);
1358
g_hash_table_insert (is_output ? control->priv->ui_outputs : control->priv->ui_inputs,
1359
GUINT_TO_POINTER (gvc_mixer_ui_device_get_id (device)),
1360
g_object_ref (device));
1363
g_signal_emit (G_OBJECT (control),
1364
signals[is_output ? OUTPUT_ADDED : INPUT_ADDED],
1366
gvc_mixer_ui_device_get_id (device));
1371
/* Go ahead and make sure to match each port against a previously created device */
1372
for (n = stream_ports; n != NULL; n = n->next) {
1374
GvcMixerStreamPort *stream_port;
1375
stream_port = n->data;
1376
stream_port_count ++;
1378
if (match_stream_with_devices (control, stream_port, stream))
1381
g_warning ("Sync_devices: Failed to match stream id: %u, description: '%s', origin: '%s'",
1382
gvc_mixer_stream_get_id (stream),
1383
stream_port->human_port,
1384
gvc_mixer_stream_get_description (stream));
1389
set_icon_name_from_proplist (GvcMixerStream *stream,
1391
const char *default_icon_name)
1395
if ((t = pa_proplist_gets (l, PA_PROP_DEVICE_ICON_NAME))) {
1399
if ((t = pa_proplist_gets (l, PA_PROP_MEDIA_ICON_NAME))) {
1403
if ((t = pa_proplist_gets (l, PA_PROP_WINDOW_ICON_NAME))) {
1407
if ((t = pa_proplist_gets (l, PA_PROP_APPLICATION_ICON_NAME))) {
1411
if ((t = pa_proplist_gets (l, PA_PROP_MEDIA_ROLE))) {
1413
if (strcmp (t, "video") == 0 ||
1414
strcmp (t, "phone") == 0) {
1418
if (strcmp (t, "music") == 0) {
1423
if (strcmp (t, "game") == 0) {
1424
t = "applications-games";
1428
if (strcmp (t, "event") == 0) {
1429
t = "dialog-information";
1434
t = default_icon_name;
1437
gvc_mixer_stream_set_icon_name (stream, t);
1441
* Called when anything changes with a sink.
1444
update_sink (GvcMixerControl *control,
1445
const pa_sink_info *info)
1447
GvcMixerStream *stream;
1449
pa_volume_t max_volume;
1451
char map_buff[PA_CHANNEL_MAP_SNPRINT_MAX];
1453
pa_channel_map_snprint (map_buff, PA_CHANNEL_MAP_SNPRINT_MAX, &info->channel_map);
1455
g_debug ("Updating sink: index=%u name='%s' description='%s' map='%s'",
1464
stream = g_hash_table_lookup (control->priv->sinks,
1465
GUINT_TO_POINTER (info->index));
1467
if (stream == NULL) {
1471
map = gvc_channel_map_new_from_pa_channel_map (&info->channel_map);
1472
stream = gvc_mixer_sink_new (control->priv->pa_context,
1476
for (i = 0; i < info->n_ports; i++) {
1477
GvcMixerStreamPort *port;
1479
port = g_new0 (GvcMixerStreamPort, 1);
1480
port->port = g_strdup (info->ports[i]->name);
1481
port->human_port = g_strdup (info->ports[i]->description);
1482
port->priority = info->ports[i]->priority;
1483
port->available = info->ports[i]->available != PA_PORT_AVAILABLE_NO;
1485
list = g_list_prepend (list, port);
1487
gvc_mixer_stream_set_ports (stream, list);
1489
g_object_unref (map);
1492
} else if (gvc_mixer_stream_is_running (stream)) {
1493
/* Ignore events if volume changes are outstanding */
1494
g_debug ("Ignoring event, volume changes are outstanding");
1498
max_volume = pa_cvolume_max (&info->volume);
1499
gvc_mixer_stream_set_name (stream, info->name);
1500
gvc_mixer_stream_set_card_index (stream, info->card);
1501
gvc_mixer_stream_set_description (stream, info->description);
1502
set_icon_name_from_proplist (stream, info->proplist, "audio-card");
1503
gvc_mixer_stream_set_sysfs_path (stream, pa_proplist_gets (info->proplist, "sysfs.path"));
1504
gvc_mixer_stream_set_volume (stream, (guint)max_volume);
1505
gvc_mixer_stream_set_is_muted (stream, info->mute);
1506
gvc_mixer_stream_set_can_decibel (stream, !!(info->flags & PA_SINK_DECIBEL_VOLUME));
1507
gvc_mixer_stream_set_base_volume (stream, (guint32) info->base_volume);
1509
/* Messy I know but to set the port everytime regardless of whether it has changed will cost us a
1510
* port change notify signal which causes the frontend to resync.
1511
* Only update the UI when something has changed. */
1512
if (info->active_port != NULL) {
1514
gvc_mixer_stream_set_port (stream, info->active_port->name);
1516
const GvcMixerStreamPort *active_port;
1517
active_port = gvc_mixer_stream_get_port (stream);
1518
if (active_port == NULL ||
1519
g_strcmp0 (active_port->port, info->active_port->name) != 0) {
1520
g_debug ("update sink - apparently a port update");
1521
gvc_mixer_stream_set_port (stream, info->active_port->name);
1527
g_debug ("update sink - is new");
1529
g_hash_table_insert (control->priv->sinks,
1530
GUINT_TO_POINTER (info->index),
1531
g_object_ref (stream));
1532
add_stream (control, stream);
1533
/* Always sink on a new stream to able to assign the right stream id
1534
* to the appropriate outputs (multiple potential outputs per stream). */
1535
sync_devices (control, stream);
1539
* When we change profile on a device that is not the server default sink,
1540
* it will jump back to the default sink set by the server to prevent the audio setup from being 'outputless'.
1541
* All well and good but then when we get the new stream created for the new profile how do we know
1542
* that this is the intended default or selected device the user wishes to use.
1543
* This is messy but it's the only reliable way that it can be done without ripping the whole thing apart.
1545
if (control->priv->profile_swapping_device_id != GVC_MIXER_UI_DEVICE_INVALID) {
1546
GvcMixerUIDevice *dev = NULL;
1547
dev = gvc_mixer_control_lookup_output_id (control, control->priv->profile_swapping_device_id);
1549
/* now check to make sure this new stream is the same stream just matched and set on the device object */
1550
if (gvc_mixer_ui_device_get_stream_id (dev) == gvc_mixer_stream_get_id (stream)) {
1551
g_debug ("Looks like we profile swapped on a non server default sink");
1552
gvc_mixer_control_set_default_sink (control, stream);
1555
control->priv->profile_swapping_device_id = GVC_MIXER_UI_DEVICE_INVALID;
1558
if (control->priv->default_sink_name != NULL
1559
&& info->name != NULL
1560
&& strcmp (control->priv->default_sink_name, info->name) == 0) {
1561
_set_default_sink (control, stream);
1565
map = (GvcChannelMap *) gvc_mixer_stream_get_channel_map (stream);
1567
gvc_channel_map_volume_changed (map, &info->volume, FALSE);
1571
update_source (GvcMixerControl *control,
1572
const pa_source_info *info)
1574
GvcMixerStream *stream;
1576
pa_volume_t max_volume;
1579
g_debug ("Updating source: index=%u name='%s' description='%s'",
1585
/* completely ignore monitors, they're not real sources */
1586
if (info->monitor_of_sink != PA_INVALID_INDEX) {
1592
stream = g_hash_table_lookup (control->priv->sources,
1593
GUINT_TO_POINTER (info->index));
1594
if (stream == NULL) {
1599
map = gvc_channel_map_new_from_pa_channel_map (&info->channel_map);
1600
stream = gvc_mixer_source_new (control->priv->pa_context,
1604
for (i = 0; i < info->n_ports; i++) {
1605
GvcMixerStreamPort *port;
1607
port = g_new0 (GvcMixerStreamPort, 1);
1608
port->port = g_strdup (info->ports[i]->name);
1609
port->human_port = g_strdup (info->ports[i]->description);
1610
port->priority = info->ports[i]->priority;
1611
list = g_list_prepend (list, port);
1613
gvc_mixer_stream_set_ports (stream, list);
1615
g_object_unref (map);
1617
} else if (gvc_mixer_stream_is_running (stream)) {
1618
/* Ignore events if volume changes are outstanding */
1619
g_debug ("Ignoring event, volume changes are outstanding");
1623
max_volume = pa_cvolume_max (&info->volume);
1625
gvc_mixer_stream_set_name (stream, info->name);
1626
gvc_mixer_stream_set_card_index (stream, info->card);
1627
gvc_mixer_stream_set_description (stream, info->description);
1628
set_icon_name_from_proplist (stream, info->proplist, "audio-input-microphone");
1629
gvc_mixer_stream_set_volume (stream, (guint)max_volume);
1630
gvc_mixer_stream_set_is_muted (stream, info->mute);
1631
gvc_mixer_stream_set_can_decibel (stream, !!(info->flags & PA_SOURCE_DECIBEL_VOLUME));
1632
gvc_mixer_stream_set_base_volume (stream, (guint32) info->base_volume);
1633
g_debug ("update source");
1635
if (info->active_port != NULL) {
1637
gvc_mixer_stream_set_port (stream, info->active_port->name);
1639
const GvcMixerStreamPort *active_port;
1640
active_port = gvc_mixer_stream_get_port (stream);
1641
if (active_port == NULL ||
1642
g_strcmp0 (active_port->port, info->active_port->name) != 0) {
1643
g_debug ("update source - apparently a port update");
1644
gvc_mixer_stream_set_port (stream, info->active_port->name);
1650
g_hash_table_insert (control->priv->sources,
1651
GUINT_TO_POINTER (info->index),
1652
g_object_ref (stream));
1653
add_stream (control, stream);
1654
sync_devices (control, stream);
1657
if (control->priv->profile_swapping_device_id != GVC_MIXER_UI_DEVICE_INVALID) {
1658
GvcMixerUIDevice *dev = NULL;
1660
dev = gvc_mixer_control_lookup_input_id (control, control->priv->profile_swapping_device_id);
1663
/* now check to make sure this new stream is the same stream just matched and set on the device object */
1664
if (gvc_mixer_ui_device_get_stream_id (dev) == gvc_mixer_stream_get_id (stream)) {
1665
g_debug ("Looks like we profile swapped on a non server default sink");
1666
gvc_mixer_control_set_default_source (control, stream);
1669
control->priv->profile_swapping_device_id = GVC_MIXER_UI_DEVICE_INVALID;
1671
if (control->priv->default_source_name != NULL
1672
&& info->name != NULL
1673
&& strcmp (control->priv->default_source_name, info->name) == 0) {
1674
_set_default_source (control, stream);
1679
set_is_event_stream_from_proplist (GvcMixerStream *stream,
1683
gboolean is_event_stream;
1685
is_event_stream = FALSE;
1687
if ((t = pa_proplist_gets (l, PA_PROP_MEDIA_ROLE))) {
1688
if (g_str_equal (t, "event"))
1689
is_event_stream = TRUE;
1692
gvc_mixer_stream_set_is_event_stream (stream, is_event_stream);
1696
set_application_id_from_proplist (GvcMixerStream *stream,
1701
if ((t = pa_proplist_gets (l, PA_PROP_APPLICATION_ID))) {
1702
gvc_mixer_stream_set_application_id (stream, t);
1707
update_sink_input (GvcMixerControl *control,
1708
const pa_sink_input_info *info)
1710
GvcMixerStream *stream;
1712
pa_volume_t max_volume;
1716
g_debug ("Updating sink input: index=%u name='%s' client=%u sink=%u",
1725
stream = g_hash_table_lookup (control->priv->sink_inputs,
1726
GUINT_TO_POINTER (info->index));
1727
if (stream == NULL) {
1729
map = gvc_channel_map_new_from_pa_channel_map (&info->channel_map);
1730
stream = gvc_mixer_sink_input_new (control->priv->pa_context,
1733
g_object_unref (map);
1735
} else if (gvc_mixer_stream_is_running (stream)) {
1736
/* Ignore events if volume changes are outstanding */
1737
g_debug ("Ignoring event, volume changes are outstanding");
1741
max_volume = pa_cvolume_max (&info->volume);
1743
name = (const char *)g_hash_table_lookup (control->priv->clients,
1744
GUINT_TO_POINTER (info->client));
1745
gvc_mixer_stream_set_name (stream, name);
1746
gvc_mixer_stream_set_description (stream, info->name);
1748
set_application_id_from_proplist (stream, info->proplist);
1749
set_is_event_stream_from_proplist (stream, info->proplist);
1750
set_icon_name_from_proplist (stream, info->proplist, "applications-multimedia");
1751
gvc_mixer_stream_set_volume (stream, (guint)max_volume);
1752
gvc_mixer_stream_set_is_muted (stream, info->mute);
1753
gvc_mixer_stream_set_is_virtual (stream, info->client == PA_INVALID_INDEX);
1756
g_hash_table_insert (control->priv->sink_inputs,
1757
GUINT_TO_POINTER (info->index),
1758
g_object_ref (stream));
1759
add_stream (control, stream);
1764
update_source_output (GvcMixerControl *control,
1765
const pa_source_output_info *info)
1767
GvcMixerStream *stream;
1772
g_debug ("Updating source output: index=%u name='%s' client=%u source=%u",
1780
stream = g_hash_table_lookup (control->priv->source_outputs,
1781
GUINT_TO_POINTER (info->index));
1782
if (stream == NULL) {
1784
map = gvc_channel_map_new_from_pa_channel_map (&info->channel_map);
1785
stream = gvc_mixer_source_output_new (control->priv->pa_context,
1788
g_object_unref (map);
1792
name = (const char *)g_hash_table_lookup (control->priv->clients,
1793
GUINT_TO_POINTER (info->client));
1795
gvc_mixer_stream_set_name (stream, name);
1796
gvc_mixer_stream_set_description (stream, info->name);
1797
set_application_id_from_proplist (stream, info->proplist);
1798
set_is_event_stream_from_proplist (stream, info->proplist);
1799
set_icon_name_from_proplist (stream, info->proplist, "audio-input-microphone");
1802
g_hash_table_insert (control->priv->source_outputs,
1803
GUINT_TO_POINTER (info->index),
1804
g_object_ref (stream));
1805
add_stream (control, stream);
1810
update_client (GvcMixerControl *control,
1811
const pa_client_info *info)
1814
g_debug ("Updating client: index=%u name='%s'",
1818
g_hash_table_insert (control->priv->clients,
1819
GUINT_TO_POINTER (info->index),
1820
g_strdup (info->name));
1824
card_num_streams_to_status (guint sinks,
1831
if (sinks == 0 && sources == 0) {
1833
* The device has been disabled */
1834
return g_strdup (_("Disabled"));
1840
* The number of sound outputs on a particular device */
1841
sinks_str = g_strdup_printf (ngettext ("%u Output",
1850
* The number of sound inputs on a particular device */
1851
sources_str = g_strdup_printf (ngettext ("%u Input",
1856
if (sources_str == NULL)
1858
if (sinks_str == NULL)
1860
ret = g_strdup_printf ("%s / %s", sinks_str, sources_str);
1862
g_free (sources_str);
1867
* A utility method to gather which card profiles are relevant to the port .
1870
determine_profiles_for_port (pa_card_port_info *port,
1871
GList* card_profiles)
1874
GList *supported_profiles = NULL;
1876
for (i = 0; i < port->n_profiles; i++) {
1877
for (p = card_profiles; p != NULL; p = p->next) {
1878
GvcMixerCardProfile *prof;
1880
if (g_strcmp0 (port->profiles[i]->name, prof->profile) == 0)
1881
supported_profiles = g_list_append (supported_profiles, prof);
1884
g_debug ("%i profiles supported on port %s",
1885
g_list_length (supported_profiles),
1887
return g_list_sort (supported_profiles, (GCompareFunc) gvc_mixer_card_profile_compare);
1891
is_card_port_an_output (GvcMixerCardPort* port)
1893
return port->direction == PA_DIRECTION_OUTPUT ? TRUE : FALSE;
1897
* This method will create a ui device for the given port.
1900
create_ui_device_from_port (GvcMixerControl* control,
1901
GvcMixerCardPort* port,
1904
GvcMixerUIDeviceDirection direction;
1906
GvcMixerUIDevice *uidevice;
1907
gboolean available = port->available != PA_PORT_AVAILABLE_NO;
1909
direction = (is_card_port_an_output (port) == TRUE) ? UIDeviceOutput : UIDeviceInput;
1911
object = g_object_new (GVC_TYPE_MIXER_UI_DEVICE,
1912
"type", (uint)direction,
1914
"port-name", port->port,
1915
"description", port->human_port,
1916
"origin", gvc_mixer_card_get_name (card),
1917
"port-available", available,
1920
uidevice = GVC_MIXER_UI_DEVICE (object);
1921
gvc_mixer_ui_device_set_profiles (uidevice, port->profiles);
1923
g_hash_table_insert (is_card_port_an_output (port) ? control->priv->ui_outputs : control->priv->ui_inputs,
1924
GUINT_TO_POINTER (gvc_mixer_ui_device_get_id (uidevice)),
1925
g_object_ref (uidevice));
1929
g_signal_emit (G_OBJECT (control),
1930
signals[is_card_port_an_output (port) ? OUTPUT_ADDED : INPUT_ADDED],
1932
gvc_mixer_ui_device_get_id (uidevice));
1935
g_debug ("create_ui_device_from_port, direction %u, description '%s', origin '%s', port available %i",
1938
gvc_mixer_card_get_name (card),
1943
* This method will match up GvcMixerCardPorts with existing devices.
1944
* A match is achieved if the device's card-id and the port's card-id are the same
1945
* && the device's port-name and the card-port's port member are the same.
1946
* A signal is then sent adding or removing that device from the UI depending on the availability of the port.
1949
match_card_port_with_existing_device (GvcMixerControl *control,
1950
GvcMixerCardPort *card_port,
1956
GvcMixerUIDevice *device;
1957
gboolean is_output = is_card_port_an_output (card_port);
1959
devices = g_hash_table_get_values (is_output ? control->priv->ui_outputs : control->priv->ui_inputs);
1961
for (d = devices; d != NULL; d = d->next) {
1962
GvcMixerCard *device_card;
1963
gchar *device_port_name;
1966
g_object_get (G_OBJECT (device),
1967
"card", &device_card,
1968
"port-name", &device_port_name,
1971
if (g_strcmp0 (card_port->port, device_port_name) == 0 &&
1972
device_card == card) {
1973
g_debug ("Found the relevant device %s, update its port availability flag to %i, is_output %i",
1977
g_object_set (G_OBJECT (device),
1978
"port-available", available, NULL);
1979
g_signal_emit (G_OBJECT (control),
1980
is_output ? signals[available ? OUTPUT_ADDED : OUTPUT_REMOVED] : signals[available ? INPUT_ADDED : INPUT_REMOVED],
1982
gvc_mixer_ui_device_get_id (device));
1984
g_free (device_port_name);
1987
g_list_free (devices);
1991
create_ui_device_from_card (GvcMixerControl *control,
1995
GvcMixerUIDevice *in;
1996
GvcMixerUIDevice *out;
1997
const GList *profiles;
1999
/* For now just create two devices and presume this device is multi directional
2000
* Ensure to remove both on card removal (available to false by default) */
2001
profiles = gvc_mixer_card_get_profiles (card);
2003
g_debug ("Portless card just registered - %i", gvc_mixer_card_get_index (card));
2005
object = g_object_new (GVC_TYPE_MIXER_UI_DEVICE,
2006
"type", UIDeviceInput,
2007
"description", gvc_mixer_card_get_name (card),
2008
"origin", "", /* Leave it empty for these special cases */
2010
"port-available", FALSE,
2013
in = GVC_MIXER_UI_DEVICE (object);
2014
gvc_mixer_ui_device_set_profiles (in, profiles);
2016
g_hash_table_insert (control->priv->ui_inputs,
2017
GUINT_TO_POINTER (gvc_mixer_ui_device_get_id (in)),
2019
object = g_object_new (GVC_TYPE_MIXER_UI_DEVICE,
2020
"type", UIDeviceOutput,
2021
"description", gvc_mixer_card_get_name (card),
2022
"origin", "", /* Leave it empty for these special cases */
2024
"port-available", FALSE,
2027
out = GVC_MIXER_UI_DEVICE (object);
2028
gvc_mixer_ui_device_set_profiles (out, profiles);
2030
g_hash_table_insert (control->priv->ui_outputs,
2031
GUINT_TO_POINTER (gvc_mixer_ui_device_get_id (out)),
2032
g_object_ref (out));
2036
* At this point we can determine all devices available to us (besides network 'ports')
2037
* This is done by the following:
2039
* - gvc_mixer_card and gvc_mixer_card_ports are created and relevant setters are called.
2040
* - First it checks to see if it's a portless card. Bluetooth devices are portless AFAIHS.
2041
* If so it creates two devices, an input and an output.
2042
* - If it's a 'normal' card with ports it will create a new ui-device or
2043
* synchronise port availability with the existing device cached for that port on this card. */
2046
update_card (GvcMixerControl *control,
2047
const pa_card_info *info)
2049
const GList *card_ports = NULL;
2050
const GList *m = NULL;
2052
gboolean is_new = FALSE;
2058
g_debug ("Udpating card %s (index: %u driver: %s):",
2059
info->name, info->index, info->driver);
2061
for (i = 0; i < info->n_profiles; i++) {
2062
struct pa_card_profile_info pi = info->profiles[i];
2063
gboolean is_default;
2065
is_default = (g_strcmp0 (pi.name, info->active_profile->name) == 0);
2066
g_debug ("\tProfile '%s': %d sources %d sinks%s",
2067
pi.name, pi.n_sources, pi.n_sinks,
2068
is_default ? " (Current)" : "");
2071
key = pa_proplist_iterate (info->proplist, &state);
2072
while (key != NULL) {
2073
g_debug ("\tProperty: '%s' = '%s'",
2074
key, pa_proplist_gets (info->proplist, key));
2075
key = pa_proplist_iterate (info->proplist, &state);
2078
card = g_hash_table_lookup (control->priv->cards,
2079
GUINT_TO_POINTER (info->index));
2081
GList *profile_list = NULL;
2082
GList *port_list = NULL;
2084
for (i = 0; i < info->n_profiles; i++) {
2085
GvcMixerCardProfile *profile;
2086
struct pa_card_profile_info pi = info->profiles[i];
2088
profile = g_new0 (GvcMixerCardProfile, 1);
2089
profile->profile = g_strdup (pi.name);
2090
profile->human_profile = g_strdup (pi.description);
2091
profile->status = card_num_streams_to_status (pi.n_sinks, pi.n_sources);
2092
profile->n_sinks = pi.n_sinks;
2093
profile->n_sources = pi.n_sources;
2094
profile->priority = pi.priority;
2095
profile_list = g_list_prepend (profile_list, profile);
2097
card = gvc_mixer_card_new (control->priv->pa_context,
2099
gvc_mixer_card_set_profiles (card, profile_list);
2101
for (i = 0; i < info->n_ports; i++) {
2102
GvcMixerCardPort *port;
2103
port = g_new0 (GvcMixerCardPort, 1);
2104
port->port = g_strdup (info->ports[i]->name);
2105
port->human_port = g_strdup (info->ports[i]->description);
2106
port->priority = info->ports[i]->priority;
2107
port->available = info->ports[i]->available;
2108
port->direction = info->ports[i]->direction;
2109
port->profiles = determine_profiles_for_port (info->ports[i], profile_list);
2110
port_list = g_list_prepend (port_list, port);
2112
gvc_mixer_card_set_ports (card, port_list);
2116
gvc_mixer_card_set_name (card, pa_proplist_gets (info->proplist, "device.description"));
2117
gvc_mixer_card_set_icon_name (card, pa_proplist_gets (info->proplist, "device.icon_name"));
2118
gvc_mixer_card_set_profile (card, info->active_profile->name);
2121
g_hash_table_insert (control->priv->cards,
2122
GUINT_TO_POINTER (info->index),
2123
g_object_ref (card));
2126
card_ports = gvc_mixer_card_get_ports (card);
2128
if (card_ports == NULL && is_new) {
2129
g_debug ("Portless card just registered - %s", gvc_mixer_card_get_name (card));
2130
create_ui_device_from_card (control, card);
2133
for (m = card_ports; m != NULL; m = m->next) {
2134
GvcMixerCardPort *card_port;
2135
card_port = m->data;
2137
create_ui_device_from_port (control, card_port, card);
2139
for (i = 0; i < info->n_ports; i++) {
2140
if (g_strcmp0 (card_port->port, info->ports[i]->name) == 0) {
2141
if ((card_port->available == PA_PORT_AVAILABLE_NO) != (info->ports[i]->available == PA_PORT_AVAILABLE_NO)) {
2142
card_port->available = info->ports[i]->available;
2143
g_debug ("sync port availability on card %i, card port name '%s', new available value %i",
2144
gvc_mixer_card_get_index (card),
2146
card_port->available);
2147
match_card_port_with_existing_device (control,
2150
card_port->available != PA_PORT_AVAILABLE_NO);
2156
g_signal_emit (G_OBJECT (control),
2157
signals[CARD_ADDED],
2163
_pa_context_get_sink_info_cb (pa_context *context,
2164
const pa_sink_info *i,
2168
GvcMixerControl *control = GVC_MIXER_CONTROL (userdata);
2171
if (pa_context_errno (context) == PA_ERR_NOENTITY) {
2175
g_warning ("Sink callback failure");
2180
dec_outstanding (control);
2184
update_sink (control, i);
2188
_pa_context_get_source_info_cb (pa_context *context,
2189
const pa_source_info *i,
2193
GvcMixerControl *control = GVC_MIXER_CONTROL (userdata);
2196
if (pa_context_errno (context) == PA_ERR_NOENTITY) {
2200
g_warning ("Source callback failure");
2205
dec_outstanding (control);
2209
update_source (control, i);
2213
_pa_context_get_sink_input_info_cb (pa_context *context,
2214
const pa_sink_input_info *i,
2218
GvcMixerControl *control = GVC_MIXER_CONTROL (userdata);
2221
if (pa_context_errno (context) == PA_ERR_NOENTITY) {
2225
g_warning ("Sink input callback failure");
2230
dec_outstanding (control);
2234
update_sink_input (control, i);
2238
_pa_context_get_source_output_info_cb (pa_context *context,
2239
const pa_source_output_info *i,
2243
GvcMixerControl *control = GVC_MIXER_CONTROL (userdata);
2246
if (pa_context_errno (context) == PA_ERR_NOENTITY) {
2250
g_warning ("Source output callback failure");
2255
dec_outstanding (control);
2259
update_source_output (control, i);
2263
_pa_context_get_client_info_cb (pa_context *context,
2264
const pa_client_info *i,
2268
GvcMixerControl *control = GVC_MIXER_CONTROL (userdata);
2271
if (pa_context_errno (context) == PA_ERR_NOENTITY) {
2275
g_warning ("Client callback failure");
2280
dec_outstanding (control);
2284
update_client (control, i);
2288
_pa_context_get_card_info_by_index_cb (pa_context *context,
2289
const pa_card_info *i,
2293
GvcMixerControl *control = GVC_MIXER_CONTROL (userdata);
2296
if (pa_context_errno (context) == PA_ERR_NOENTITY)
2299
g_warning ("Card callback failure");
2304
dec_outstanding (control);
2308
update_card (control, i);
2312
_pa_context_get_server_info_cb (pa_context *context,
2313
const pa_server_info *i,
2316
GvcMixerControl *control = GVC_MIXER_CONTROL (userdata);
2319
g_warning ("Server info callback failure");
2322
g_debug ("get server info");
2323
update_server (control, i);
2324
dec_outstanding (control);
2328
remove_event_role_stream (GvcMixerControl *control)
2330
g_debug ("Removing event role");
2334
update_event_role_stream (GvcMixerControl *control,
2335
const pa_ext_stream_restore_info *info)
2337
GvcMixerStream *stream;
2339
pa_volume_t max_volume;
2341
if (strcmp (info->name, "sink-input-by-media-role:event") != 0) {
2346
g_debug ("Updating event role: name='%s' device='%s'",
2353
if (!control->priv->event_sink_input_is_set) {
2354
pa_channel_map pa_map;
2357
pa_map.channels = 1;
2358
pa_map.map[0] = PA_CHANNEL_POSITION_MONO;
2359
map = gvc_channel_map_new_from_pa_channel_map (&pa_map);
2361
stream = gvc_mixer_event_role_new (control->priv->pa_context,
2364
control->priv->event_sink_input_id = gvc_mixer_stream_get_id (stream);
2365
control->priv->event_sink_input_is_set = TRUE;
2369
stream = g_hash_table_lookup (control->priv->all_streams,
2370
GUINT_TO_POINTER (control->priv->event_sink_input_id));
2373
max_volume = pa_cvolume_max (&info->volume);
2375
gvc_mixer_stream_set_name (stream, _("System Sounds"));
2376
gvc_mixer_stream_set_icon_name (stream, "unity-sound-panel");
2377
gvc_mixer_stream_set_volume (stream, (guint)max_volume);
2378
gvc_mixer_stream_set_is_muted (stream, info->mute);
2381
add_stream (control, stream);
2386
_pa_ext_stream_restore_read_cb (pa_context *context,
2387
const pa_ext_stream_restore_info *i,
2391
GvcMixerControl *control = GVC_MIXER_CONTROL (userdata);
2394
g_debug ("Failed to initialized stream_restore extension: %s",
2395
pa_strerror (pa_context_errno (context)));
2396
remove_event_role_stream (control);
2401
dec_outstanding (control);
2402
/* If we don't have an event stream to restore, then
2403
* set one up with a default 100% volume */
2404
if (!control->priv->event_sink_input_is_set) {
2405
pa_ext_stream_restore_info info;
2407
memset (&info, 0, sizeof(info));
2408
info.name = "sink-input-by-media-role:event";
2409
info.volume.channels = 1;
2410
info.volume.values[0] = PA_VOLUME_NORM;
2411
update_event_role_stream (control, &info);
2416
update_event_role_stream (control, i);
2420
_pa_ext_stream_restore_subscribe_cb (pa_context *context,
2423
GvcMixerControl *control = GVC_MIXER_CONTROL (userdata);
2426
o = pa_ext_stream_restore_read (context,
2427
_pa_ext_stream_restore_read_cb,
2430
g_warning ("pa_ext_stream_restore_read() failed");
2434
pa_operation_unref (o);
2438
req_update_server_info (GvcMixerControl *control,
2443
o = pa_context_get_server_info (control->priv->pa_context,
2444
_pa_context_get_server_info_cb,
2447
g_warning ("pa_context_get_server_info() failed");
2450
pa_operation_unref (o);
2454
req_update_client_info (GvcMixerControl *control,
2460
o = pa_context_get_client_info_list (control->priv->pa_context,
2461
_pa_context_get_client_info_cb,
2464
o = pa_context_get_client_info (control->priv->pa_context,
2466
_pa_context_get_client_info_cb,
2471
g_warning ("pa_context_client_info_list() failed");
2474
pa_operation_unref (o);
2478
req_update_card (GvcMixerControl *control,
2484
o = pa_context_get_card_info_list (control->priv->pa_context,
2485
_pa_context_get_card_info_by_index_cb,
2488
o = pa_context_get_card_info_by_index (control->priv->pa_context,
2490
_pa_context_get_card_info_by_index_cb,
2495
g_warning ("pa_context_get_card_info_by_index() failed");
2498
pa_operation_unref (o);
2502
req_update_sink_info (GvcMixerControl *control,
2508
o = pa_context_get_sink_info_list (control->priv->pa_context,
2509
_pa_context_get_sink_info_cb,
2512
o = pa_context_get_sink_info_by_index (control->priv->pa_context,
2514
_pa_context_get_sink_info_cb,
2519
g_warning ("pa_context_get_sink_info_list() failed");
2522
pa_operation_unref (o);
2526
req_update_source_info (GvcMixerControl *control,
2532
o = pa_context_get_source_info_list (control->priv->pa_context,
2533
_pa_context_get_source_info_cb,
2536
o = pa_context_get_source_info_by_index(control->priv->pa_context,
2538
_pa_context_get_source_info_cb,
2543
g_warning ("pa_context_get_source_info_list() failed");
2546
pa_operation_unref (o);
2550
req_update_sink_input_info (GvcMixerControl *control,
2556
o = pa_context_get_sink_input_info_list (control->priv->pa_context,
2557
_pa_context_get_sink_input_info_cb,
2560
o = pa_context_get_sink_input_info (control->priv->pa_context,
2562
_pa_context_get_sink_input_info_cb,
2567
g_warning ("pa_context_get_sink_input_info_list() failed");
2570
pa_operation_unref (o);
2574
req_update_source_output_info (GvcMixerControl *control,
2580
o = pa_context_get_source_output_info_list (control->priv->pa_context,
2581
_pa_context_get_source_output_info_cb,
2584
o = pa_context_get_source_output_info (control->priv->pa_context,
2586
_pa_context_get_source_output_info_cb,
2591
g_warning ("pa_context_get_source_output_info_list() failed");
2594
pa_operation_unref (o);
2598
remove_client (GvcMixerControl *control,
2601
g_hash_table_remove (control->priv->clients,
2602
GUINT_TO_POINTER (index));
2606
remove_card (GvcMixerControl *control,
2612
devices = g_list_concat (g_hash_table_get_values (control->priv->ui_inputs),
2613
g_hash_table_get_values (control->priv->ui_outputs));
2615
for (d = devices; d != NULL; d = d->next) {
2617
GvcMixerUIDevice *device = d->data;
2619
g_object_get (G_OBJECT (device), "card", &card, NULL);
2621
if (gvc_mixer_card_get_index (card) == index) {
2622
g_signal_emit (G_OBJECT (control),
2623
signals[gvc_mixer_ui_device_is_output (device) ? OUTPUT_REMOVED : INPUT_REMOVED],
2625
gvc_mixer_ui_device_get_id (device));
2626
g_debug ("Card removal remove device %s",
2627
gvc_mixer_ui_device_get_description (device));
2628
g_hash_table_remove (gvc_mixer_ui_device_is_output (device) ? control->priv->ui_outputs : control->priv->ui_inputs,
2629
GUINT_TO_POINTER (gvc_mixer_ui_device_get_id (device)));
2633
g_list_free (devices);
2635
g_hash_table_remove (control->priv->cards,
2636
GUINT_TO_POINTER (index));
2638
g_signal_emit (G_OBJECT (control),
2639
signals[CARD_REMOVED],
2645
remove_sink (GvcMixerControl *control,
2648
GvcMixerStream *stream;
2649
GvcMixerUIDevice *device;
2651
g_debug ("Removing sink: index=%u", index);
2653
stream = g_hash_table_lookup (control->priv->sinks,
2654
GUINT_TO_POINTER (index));
2658
device = gvc_mixer_control_lookup_device_from_stream (control, stream);
2660
if (device != NULL) {
2661
gvc_mixer_ui_device_invalidate_stream (device);
2662
if (!gvc_mixer_ui_device_has_ports (device)) {
2663
g_signal_emit (G_OBJECT (control),
2664
signals[OUTPUT_REMOVED],
2666
gvc_mixer_ui_device_get_id (device));
2670
devices = g_hash_table_get_values (control->priv->ui_outputs);
2672
for (d = devices; d != NULL; d = d->next) {
2673
gint stream_id = GVC_MIXER_UI_DEVICE_INVALID;
2675
g_object_get (G_OBJECT (device),
2676
"stream-id", &stream_id,
2678
if (stream_id == gvc_mixer_stream_get_id (stream))
2679
gvc_mixer_ui_device_invalidate_stream (device);
2682
g_list_free (devices);
2686
g_hash_table_remove (control->priv->sinks,
2687
GUINT_TO_POINTER (index));
2689
remove_stream (control, stream);
2693
remove_source (GvcMixerControl *control,
2696
GvcMixerStream *stream;
2697
GvcMixerUIDevice *device;
2699
g_debug ("Removing source: index=%u", index);
2701
stream = g_hash_table_lookup (control->priv->sources,
2702
GUINT_TO_POINTER (index));
2706
device = gvc_mixer_control_lookup_device_from_stream (control, stream);
2708
if (device != NULL) {
2709
gvc_mixer_ui_device_invalidate_stream (device);
2710
if (!gvc_mixer_ui_device_has_ports (device)) {
2711
g_signal_emit (G_OBJECT (control),
2712
signals[INPUT_REMOVED],
2714
gvc_mixer_ui_device_get_id (device));
2718
devices = g_hash_table_get_values (control->priv->ui_inputs);
2720
for (d = devices; d != NULL; d = d->next) {
2721
gint stream_id = GVC_MIXER_UI_DEVICE_INVALID;
2723
g_object_get (G_OBJECT (device),
2724
"stream-id", &stream_id,
2726
if (stream_id == gvc_mixer_stream_get_id (stream))
2727
gvc_mixer_ui_device_invalidate_stream (device);
2730
g_list_free (devices);
2734
g_hash_table_remove (control->priv->sources,
2735
GUINT_TO_POINTER (index));
2737
remove_stream (control, stream);
2741
remove_sink_input (GvcMixerControl *control,
2744
GvcMixerStream *stream;
2746
g_debug ("Removing sink input: index=%u", index);
2748
stream = g_hash_table_lookup (control->priv->sink_inputs,
2749
GUINT_TO_POINTER (index));
2750
if (stream == NULL) {
2753
g_hash_table_remove (control->priv->sink_inputs,
2754
GUINT_TO_POINTER (index));
2756
remove_stream (control, stream);
2760
remove_source_output (GvcMixerControl *control,
2763
GvcMixerStream *stream;
2765
g_debug ("Removing source output: index=%u", index);
2767
stream = g_hash_table_lookup (control->priv->source_outputs,
2768
GUINT_TO_POINTER (index));
2769
if (stream == NULL) {
2772
g_hash_table_remove (control->priv->source_outputs,
2773
GUINT_TO_POINTER (index));
2775
remove_stream (control, stream);
2779
_pa_context_subscribe_cb (pa_context *context,
2780
pa_subscription_event_type_t t,
2784
GvcMixerControl *control = GVC_MIXER_CONTROL (userdata);
2786
switch (t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) {
2787
case PA_SUBSCRIPTION_EVENT_SINK:
2788
if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
2789
remove_sink (control, index);
2791
req_update_sink_info (control, index);
2795
case PA_SUBSCRIPTION_EVENT_SOURCE:
2796
if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
2797
remove_source (control, index);
2799
req_update_source_info (control, index);
2803
case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
2804
if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
2805
remove_sink_input (control, index);
2807
req_update_sink_input_info (control, index);
2811
case PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT:
2812
if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
2813
remove_source_output (control, index);
2815
req_update_source_output_info (control, index);
2819
case PA_SUBSCRIPTION_EVENT_CLIENT:
2820
if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
2821
remove_client (control, index);
2823
req_update_client_info (control, index);
2827
case PA_SUBSCRIPTION_EVENT_SERVER:
2828
req_update_server_info (control, index);
2831
case PA_SUBSCRIPTION_EVENT_CARD:
2832
if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
2833
remove_card (control, index);
2835
req_update_card (control, index);
2842
gvc_mixer_control_ready (GvcMixerControl *control)
2846
pa_context_set_subscribe_callback (control->priv->pa_context,
2847
_pa_context_subscribe_cb,
2849
o = pa_context_subscribe (control->priv->pa_context,
2850
(pa_subscription_mask_t)
2851
(PA_SUBSCRIPTION_MASK_SINK|
2852
PA_SUBSCRIPTION_MASK_SOURCE|
2853
PA_SUBSCRIPTION_MASK_SINK_INPUT|
2854
PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT|
2855
PA_SUBSCRIPTION_MASK_CLIENT|
2856
PA_SUBSCRIPTION_MASK_SERVER|
2857
PA_SUBSCRIPTION_MASK_CARD),
2862
g_warning ("pa_context_subscribe() failed");
2865
pa_operation_unref (o);
2867
req_update_server_info (control, -1);
2868
req_update_card (control, -1);
2869
req_update_client_info (control, -1);
2870
req_update_sink_info (control, -1);
2871
req_update_source_info (control, -1);
2872
req_update_sink_input_info (control, -1);
2873
req_update_source_output_info (control, -1);
2876
control->priv->n_outstanding = 6;
2878
/* This call is not always supported */
2879
o = pa_ext_stream_restore_read (control->priv->pa_context,
2880
_pa_ext_stream_restore_read_cb,
2883
pa_operation_unref (o);
2884
control->priv->n_outstanding++;
2886
pa_ext_stream_restore_set_subscribe_cb (control->priv->pa_context,
2887
_pa_ext_stream_restore_subscribe_cb,
2890
o = pa_ext_stream_restore_subscribe (control->priv->pa_context,
2895
pa_operation_unref (o);
2899
g_debug ("Failed to initialized stream_restore extension: %s",
2900
pa_strerror (pa_context_errno (control->priv->pa_context)));
2905
gvc_mixer_new_pa_context (GvcMixerControl *self)
2907
pa_proplist *proplist;
2909
g_return_if_fail (self);
2910
g_return_if_fail (!self->priv->pa_context);
2912
proplist = pa_proplist_new ();
2913
pa_proplist_sets (proplist,
2914
PA_PROP_APPLICATION_NAME,
2916
pa_proplist_sets (proplist,
2917
PA_PROP_APPLICATION_ID,
2918
"org.gnome.VolumeControl");
2919
pa_proplist_sets (proplist,
2920
PA_PROP_APPLICATION_ICON_NAME,
2921
"unity-sound-panel");
2922
pa_proplist_sets (proplist,
2923
PA_PROP_APPLICATION_VERSION,
2926
self->priv->pa_context = pa_context_new_with_proplist (self->priv->pa_api, NULL, proplist);
2928
pa_proplist_free (proplist);
2929
g_assert (self->priv->pa_context);
2933
remove_all_streams (GvcMixerControl *control, GHashTable *hash_table)
2935
GHashTableIter iter;
2936
gpointer key, value;
2938
g_hash_table_iter_init (&iter, hash_table);
2939
while (g_hash_table_iter_next (&iter, &key, &value)) {
2940
remove_stream (control, value);
2941
g_hash_table_iter_remove (&iter);
2946
idle_reconnect (gpointer data)
2948
GvcMixerControl *control = GVC_MIXER_CONTROL (data);
2949
GHashTableIter iter;
2950
gpointer key, value;
2952
g_return_val_if_fail (control, FALSE);
2954
if (control->priv->pa_context) {
2955
pa_context_unref (control->priv->pa_context);
2956
control->priv->pa_context = NULL;
2957
gvc_mixer_new_pa_context (control);
2960
remove_all_streams (control, control->priv->sinks);
2961
remove_all_streams (control, control->priv->sources);
2962
remove_all_streams (control, control->priv->sink_inputs);
2963
remove_all_streams (control, control->priv->source_outputs);
2965
g_hash_table_iter_init (&iter, control->priv->clients);
2966
while (g_hash_table_iter_next (&iter, &key, &value))
2967
g_hash_table_iter_remove (&iter);
2969
gvc_mixer_control_open (control); /* cannot fail */
2971
control->priv->reconnect_id = 0;
2976
_pa_context_state_cb (pa_context *context,
2979
GvcMixerControl *control = GVC_MIXER_CONTROL (userdata);
2981
switch (pa_context_get_state (context)) {
2982
case PA_CONTEXT_UNCONNECTED:
2983
case PA_CONTEXT_CONNECTING:
2984
case PA_CONTEXT_AUTHORIZING:
2985
case PA_CONTEXT_SETTING_NAME:
2988
case PA_CONTEXT_READY:
2989
gvc_mixer_control_ready (control);
2992
case PA_CONTEXT_FAILED:
2993
control->priv->state = GVC_STATE_FAILED;
2994
g_signal_emit (control, signals[STATE_CHANGED], 0, GVC_STATE_FAILED);
2995
if (control->priv->reconnect_id == 0)
2996
control->priv->reconnect_id = g_timeout_add_seconds (RECONNECT_DELAY, idle_reconnect, control);
2999
case PA_CONTEXT_TERMINATED:
3007
gvc_mixer_control_open (GvcMixerControl *control)
3011
g_return_val_if_fail (GVC_IS_MIXER_CONTROL (control), FALSE);
3012
g_return_val_if_fail (control->priv->pa_context != NULL, FALSE);
3013
g_return_val_if_fail (pa_context_get_state (control->priv->pa_context) == PA_CONTEXT_UNCONNECTED, FALSE);
3015
pa_context_set_state_callback (control->priv->pa_context,
3016
_pa_context_state_cb,
3019
control->priv->state = GVC_STATE_CONNECTING;
3020
g_signal_emit (G_OBJECT (control), signals[STATE_CHANGED], 0, GVC_STATE_CONNECTING);
3021
res = pa_context_connect (control->priv->pa_context, NULL, (pa_context_flags_t) PA_CONTEXT_NOFAIL, NULL);
3023
g_warning ("Failed to connect context: %s",
3024
pa_strerror (pa_context_errno (control->priv->pa_context)));
3031
gvc_mixer_control_close (GvcMixerControl *control)
3033
g_return_val_if_fail (GVC_IS_MIXER_CONTROL (control), FALSE);
3034
g_return_val_if_fail (control->priv->pa_context != NULL, FALSE);
3036
pa_context_disconnect (control->priv->pa_context);
3038
control->priv->state = GVC_STATE_CLOSED;
3039
g_signal_emit (G_OBJECT (control), signals[STATE_CHANGED], 0, GVC_STATE_CLOSED);
3044
gvc_mixer_control_dispose (GObject *object)
3046
GvcMixerControl *control = GVC_MIXER_CONTROL (object);
3048
if (control->priv->reconnect_id != 0) {
3049
g_source_remove (control->priv->reconnect_id);
3050
control->priv->reconnect_id = 0;
3053
if (control->priv->pa_context != NULL) {
3054
pa_context_unref (control->priv->pa_context);
3055
control->priv->pa_context = NULL;
3058
if (control->priv->default_source_name != NULL) {
3059
g_free (control->priv->default_source_name);
3060
control->priv->default_source_name = NULL;
3062
if (control->priv->default_sink_name != NULL) {
3063
g_free (control->priv->default_sink_name);
3064
control->priv->default_sink_name = NULL;
3067
if (control->priv->pa_mainloop != NULL) {
3068
pa_glib_mainloop_free (control->priv->pa_mainloop);
3069
control->priv->pa_mainloop = NULL;
3072
if (control->priv->all_streams != NULL) {
3073
g_hash_table_destroy (control->priv->all_streams);
3074
control->priv->all_streams = NULL;
3077
if (control->priv->sinks != NULL) {
3078
g_hash_table_destroy (control->priv->sinks);
3079
control->priv->sinks = NULL;
3081
if (control->priv->sources != NULL) {
3082
g_hash_table_destroy (control->priv->sources);
3083
control->priv->sources = NULL;
3085
if (control->priv->sink_inputs != NULL) {
3086
g_hash_table_destroy (control->priv->sink_inputs);
3087
control->priv->sink_inputs = NULL;
3089
if (control->priv->source_outputs != NULL) {
3090
g_hash_table_destroy (control->priv->source_outputs);
3091
control->priv->source_outputs = NULL;
3093
if (control->priv->clients != NULL) {
3094
g_hash_table_destroy (control->priv->clients);
3095
control->priv->clients = NULL;
3097
if (control->priv->cards != NULL) {
3098
g_hash_table_destroy (control->priv->cards);
3099
control->priv->cards = NULL;
3101
if (control->priv->ui_outputs != NULL) {
3102
g_hash_table_destroy (control->priv->ui_outputs);
3103
control->priv->ui_outputs = NULL;
3105
if (control->priv->ui_inputs != NULL) {
3106
g_hash_table_destroy (control->priv->ui_inputs);
3107
control->priv->ui_inputs = NULL;
3110
G_OBJECT_CLASS (gvc_mixer_control_parent_class)->dispose (object);
3114
gvc_mixer_control_set_property (GObject *object,
3116
const GValue *value,
3119
GvcMixerControl *self = GVC_MIXER_CONTROL (object);
3123
g_free (self->priv->name);
3124
self->priv->name = g_value_dup_string (value);
3125
g_object_notify (G_OBJECT (self), "name");
3128
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3134
gvc_mixer_control_get_property (GObject *object,
3139
GvcMixerControl *self = GVC_MIXER_CONTROL (object);
3143
g_value_set_string (value, self->priv->name);
3146
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3153
gvc_mixer_control_constructor (GType type,
3154
guint n_construct_properties,
3155
GObjectConstructParam *construct_params)
3158
GvcMixerControl *self;
3160
object = G_OBJECT_CLASS (gvc_mixer_control_parent_class)->constructor (type, n_construct_properties, construct_params);
3162
self = GVC_MIXER_CONTROL (object);
3164
gvc_mixer_new_pa_context (self);
3165
self->priv->profile_swapping_device_id = GVC_MIXER_UI_DEVICE_INVALID;
3171
gvc_mixer_control_class_init (GvcMixerControlClass *klass)
3173
GObjectClass *object_class = G_OBJECT_CLASS (klass);
3175
object_class->constructor = gvc_mixer_control_constructor;
3176
object_class->dispose = gvc_mixer_control_dispose;
3177
object_class->finalize = gvc_mixer_control_finalize;
3178
object_class->set_property = gvc_mixer_control_set_property;
3179
object_class->get_property = gvc_mixer_control_get_property;
3181
g_object_class_install_property (object_class,
3183
g_param_spec_string ("name",
3185
"Name to display for this mixer control",
3187
G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
3189
signals [STATE_CHANGED] =
3190
g_signal_new ("state-changed",
3191
G_TYPE_FROM_CLASS (klass),
3193
G_STRUCT_OFFSET (GvcMixerControlClass, state_changed),
3195
g_cclosure_marshal_VOID__UINT,
3196
G_TYPE_NONE, 1, G_TYPE_UINT);
3197
signals [STREAM_ADDED] =
3198
g_signal_new ("stream-added",
3199
G_TYPE_FROM_CLASS (klass),
3201
G_STRUCT_OFFSET (GvcMixerControlClass, stream_added),
3203
g_cclosure_marshal_VOID__UINT,
3204
G_TYPE_NONE, 1, G_TYPE_UINT);
3205
signals [STREAM_REMOVED] =
3206
g_signal_new ("stream-removed",
3207
G_TYPE_FROM_CLASS (klass),
3209
G_STRUCT_OFFSET (GvcMixerControlClass, stream_removed),
3211
g_cclosure_marshal_VOID__UINT,
3212
G_TYPE_NONE, 1, G_TYPE_UINT);
3213
signals [CARD_ADDED] =
3214
g_signal_new ("card-added",
3215
G_TYPE_FROM_CLASS (klass),
3217
G_STRUCT_OFFSET (GvcMixerControlClass, card_added),
3219
g_cclosure_marshal_VOID__UINT,
3220
G_TYPE_NONE, 1, G_TYPE_UINT);
3221
signals [CARD_REMOVED] =
3222
g_signal_new ("card-removed",
3223
G_TYPE_FROM_CLASS (klass),
3225
G_STRUCT_OFFSET (GvcMixerControlClass, card_removed),
3227
g_cclosure_marshal_VOID__UINT,
3228
G_TYPE_NONE, 1, G_TYPE_UINT);
3229
signals [DEFAULT_SINK_CHANGED] =
3230
g_signal_new ("default-sink-changed",
3231
G_TYPE_FROM_CLASS (klass),
3233
G_STRUCT_OFFSET (GvcMixerControlClass, default_sink_changed),
3235
g_cclosure_marshal_VOID__UINT,
3236
G_TYPE_NONE, 1, G_TYPE_UINT);
3237
signals [DEFAULT_SOURCE_CHANGED] =
3238
g_signal_new ("default-source-changed",
3239
G_TYPE_FROM_CLASS (klass),
3241
G_STRUCT_OFFSET (GvcMixerControlClass, default_source_changed),
3243
g_cclosure_marshal_VOID__UINT,
3244
G_TYPE_NONE, 1, G_TYPE_UINT);
3245
signals [ACTIVE_OUTPUT_UPDATE] =
3246
g_signal_new ("active-output-update",
3247
G_TYPE_FROM_CLASS (klass),
3249
G_STRUCT_OFFSET (GvcMixerControlClass, active_output_update),
3251
g_cclosure_marshal_VOID__UINT,
3252
G_TYPE_NONE, 1, G_TYPE_UINT);
3253
signals [ACTIVE_INPUT_UPDATE] =
3254
g_signal_new ("active-input-update",
3255
G_TYPE_FROM_CLASS (klass),
3257
G_STRUCT_OFFSET (GvcMixerControlClass, active_input_update),
3259
g_cclosure_marshal_VOID__UINT,
3260
G_TYPE_NONE, 1, G_TYPE_UINT);
3261
signals [OUTPUT_ADDED] =
3262
g_signal_new ("output-added",
3263
G_TYPE_FROM_CLASS (klass),
3265
G_STRUCT_OFFSET (GvcMixerControlClass, output_added),
3267
g_cclosure_marshal_VOID__UINT,
3268
G_TYPE_NONE, 1, G_TYPE_UINT);
3269
signals [INPUT_ADDED] =
3270
g_signal_new ("input-added",
3271
G_TYPE_FROM_CLASS (klass),
3273
G_STRUCT_OFFSET (GvcMixerControlClass, input_added),
3275
g_cclosure_marshal_VOID__UINT,
3276
G_TYPE_NONE, 1, G_TYPE_UINT);
3277
signals [OUTPUT_REMOVED] =
3278
g_signal_new ("output-removed",
3279
G_TYPE_FROM_CLASS (klass),
3281
G_STRUCT_OFFSET (GvcMixerControlClass, output_removed),
3283
g_cclosure_marshal_VOID__UINT,
3284
G_TYPE_NONE, 1, G_TYPE_UINT);
3285
signals [INPUT_REMOVED] =
3286
g_signal_new ("input-removed",
3287
G_TYPE_FROM_CLASS (klass),
3289
G_STRUCT_OFFSET (GvcMixerControlClass, input_removed),
3291
g_cclosure_marshal_VOID__UINT,
3292
G_TYPE_NONE, 1, G_TYPE_UINT);
3293
g_type_class_add_private (klass, sizeof (GvcMixerControlPrivate));
3298
gvc_mixer_control_init (GvcMixerControl *control)
3300
control->priv = GVC_MIXER_CONTROL_GET_PRIVATE (control);
3302
control->priv->pa_mainloop = pa_glib_mainloop_new (g_main_context_default ());
3303
g_assert (control->priv->pa_mainloop);
3305
control->priv->pa_api = pa_glib_mainloop_get_api (control->priv->pa_mainloop);
3306
g_assert (control->priv->pa_api);
3308
control->priv->all_streams = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_object_unref);
3309
control->priv->sinks = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_object_unref);
3310
control->priv->sources = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_object_unref);
3311
control->priv->sink_inputs = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_object_unref);
3312
control->priv->source_outputs = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_object_unref);
3313
control->priv->cards = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_object_unref);
3314
control->priv->ui_outputs = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_object_unref);
3315
control->priv->ui_inputs = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_object_unref);
3317
control->priv->clients = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
3319
control->priv->state = GVC_STATE_CLOSED;
3323
gvc_mixer_control_finalize (GObject *object)
3325
GvcMixerControl *mixer_control;
3327
g_return_if_fail (object != NULL);
3328
g_return_if_fail (GVC_IS_MIXER_CONTROL (object));
3330
mixer_control = GVC_MIXER_CONTROL (object);
3331
g_free (mixer_control->priv->name);
3332
mixer_control->priv->name = NULL;
3334
g_return_if_fail (mixer_control->priv != NULL);
3335
G_OBJECT_CLASS (gvc_mixer_control_parent_class)->finalize (object);
3339
gvc_mixer_control_new (const char *name)
3342
control = g_object_new (GVC_TYPE_MIXER_CONTROL,
3345
return GVC_MIXER_CONTROL (control);
3349
gvc_mixer_control_get_vol_max_norm (GvcMixerControl *control)
3351
return (gdouble) PA_VOLUME_NORM;
3355
gvc_mixer_control_get_vol_max_amplified (GvcMixerControl *control)
3357
return (gdouble) PA_VOLUME_UI_MAX;