summaryrefslogtreecommitdiff
path: root/backends/pulse/pulse-ext-stream.c
blob: 5d2ec758ee77741fff021e7b42db607990f37f5a (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
/*
 * Copyright (C) 2014 Michal Ratajsky <michal.ratajsky@gmail.com>
 *
 * This library 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 of the licence, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include <string.h>
#include <glib.h>
#include <glib-object.h>

#include <libmatemixer/matemixer.h>
#include <libmatemixer/matemixer-private.h>

#include <pulse/pulseaudio.h>
#include <pulse/ext-stream-restore.h>

#include "pulse-connection.h"
#include "pulse-ext-stream.h"
#include "pulse-helpers.h"
#include "pulse-stream.h"
#include "pulse-stream-control.h"

struct _PulseExtStreamPrivate
{
    MateMixerAppInfo  *app_info;
    MateMixerDirection direction;
};

enum {
    PROP_0,
    PROP_DIRECTION
};

static void mate_mixer_stored_control_interface_init (MateMixerStoredControlInterface *iface);

static void pulse_ext_stream_class_init   (PulseExtStreamClass *klass);

static void pulse_ext_stream_get_property (GObject             *object,
                                           guint                param_id,
                                           GValue              *value,
                                           GParamSpec          *pspec);
static void pulse_ext_stream_set_property (GObject             *object,
                                           guint                param_id,
                                           const GValue        *value,
                                           GParamSpec          *pspec);

static void pulse_ext_stream_init         (PulseExtStream      *ext);

G_DEFINE_TYPE_WITH_CODE (PulseExtStream, pulse_ext_stream, PULSE_TYPE_STREAM_CONTROL,
                         G_IMPLEMENT_INTERFACE (MATE_MIXER_TYPE_STORED_CONTROL,
                                                mate_mixer_stored_control_interface_init))

static MateMixerDirection pulse_ext_stream_get_direction (MateMixerStoredControl     *mmsc);

static MateMixerAppInfo * pulse_ext_stream_get_app_info  (MateMixerStreamControl     *mmsc);

static gboolean           pulse_ext_stream_set_stream    (MateMixerStreamControl     *mmsc,
                                                          MateMixerStream            *mms);

static gboolean           pulse_ext_stream_set_mute      (PulseStreamControl         *control,
                                                          gboolean                    mute);
static gboolean           pulse_ext_stream_set_volume    (PulseStreamControl         *control,
                                                          pa_cvolume                 *cvolume);

static void               fill_ext_stream_restore_info   (PulseStreamControl         *control,
                                                          pa_ext_stream_restore_info *info);

static void
mate_mixer_stored_control_interface_init (MateMixerStoredControlInterface *iface)
{
    iface->get_direction = pulse_ext_stream_get_direction;
}

static void
pulse_ext_stream_class_init (PulseExtStreamClass *klass)
{
    GObjectClass                *object_class;
    MateMixerStreamControlClass *control_class;
    PulseStreamControlClass     *pulse_class;

    object_class = G_OBJECT_CLASS (klass);
    object_class->get_property = pulse_ext_stream_get_property;
    object_class->set_property = pulse_ext_stream_set_property;

    control_class = MATE_MIXER_STREAM_CONTROL_CLASS (klass);
    control_class->get_app_info = pulse_ext_stream_get_app_info;
    control_class->set_stream   = pulse_ext_stream_set_stream;

    pulse_class = PULSE_STREAM_CONTROL_CLASS (klass);
    pulse_class->set_mute   = pulse_ext_stream_set_mute;
    pulse_class->set_volume = pulse_ext_stream_set_volume;

    g_object_class_override_property (object_class, PROP_DIRECTION, "direction");

    g_type_class_add_private (object_class, sizeof (PulseExtStreamPrivate));
}

static void
pulse_ext_stream_get_property (GObject      *object,
                               guint         param_id,
                               GValue       *value,
                               GParamSpec   *pspec)
{
    PulseExtStream *ext;

    ext = PULSE_EXT_STREAM (object);

    switch (param_id) {
    case PROP_DIRECTION:
        g_value_set_enum (value, ext->priv->direction);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
    }
}

static void
pulse_ext_stream_set_property (GObject      *object,
                               guint         param_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
    PulseExtStream *ext;

    ext = PULSE_EXT_STREAM (object);

    switch (param_id) {
    case PROP_DIRECTION:
        ext->priv->direction = g_value_get_enum (value);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
    }
}

static void
pulse_ext_stream_init (PulseExtStream *ext)
{
    ext->priv = G_TYPE_INSTANCE_GET_PRIVATE (ext,
                                             PULSE_TYPE_EXT_STREAM,
                                             PulseExtStreamPrivate);
}

PulseExtStream *
pulse_ext_stream_new (PulseConnection                  *connection,
                      const pa_ext_stream_restore_info *info,
                      PulseStream                      *parent)
{
    PulseExtStream             *ext;
    gchar                      *suffix;
    MateMixerDirection          direction;
    MateMixerStreamControlFlags flags = MATE_MIXER_STREAM_CONTROL_VOLUME_READABLE |
                                        MATE_MIXER_STREAM_CONTROL_VOLUME_WRITABLE |
                                        MATE_MIXER_STREAM_CONTROL_MUTE_READABLE |
                                        MATE_MIXER_STREAM_CONTROL_MUTE_WRITABLE;
    MateMixerStreamControlRole  role  = MATE_MIXER_STREAM_CONTROL_ROLE_UNKNOWN;
    MateMixerAppInfo           *app_info;

    MateMixerStreamControlMediaRole media_role = MATE_MIXER_STREAM_CONTROL_MEDIA_ROLE_UNKNOWN;

    g_return_val_if_fail (PULSE_IS_CONNECTION (connection), NULL);
    g_return_val_if_fail (info != NULL, NULL);

    if (g_str_has_prefix (info->name, "sink-input"))
        direction = MATE_MIXER_DIRECTION_OUTPUT;
    else if (g_str_has_prefix (info->name, "source-output"))
        direction = MATE_MIXER_DIRECTION_INPUT;
    else
        direction = MATE_MIXER_DIRECTION_UNKNOWN;

    app_info = _mate_mixer_app_info_new ();

    suffix = strchr (info->name, ':');
    if (suffix != NULL)
        suffix++;

    if (strstr (info->name, "-by-media-role:")) {
        if (G_LIKELY (suffix != NULL))
            media_role = pulse_convert_media_role_name (suffix);
    }
    else if (strstr (info->name, "-by-application-name:")) {
        role = MATE_MIXER_STREAM_CONTROL_ROLE_APPLICATION;

        if (G_LIKELY (suffix != NULL))
            _mate_mixer_app_info_set_name (app_info, suffix);
    }
    else if (strstr (info->name, "-by-application-id:")) {
        role = MATE_MIXER_STREAM_CONTROL_ROLE_APPLICATION;

        if (G_LIKELY (suffix != NULL))
            _mate_mixer_app_info_set_id (app_info, suffix);
    }

    ext = g_object_new (PULSE_TYPE_EXT_STREAM,
                        "flags", flags,
                        "role", role,
                        "media-role", media_role,
                        "name", info->name,
                        "connection", connection,
                        "direction", direction,
                        "stream", parent,
                        NULL);

    if (role == MATE_MIXER_STREAM_CONTROL_ROLE_APPLICATION)
        ext->priv->app_info = app_info;
    else
        _mate_mixer_app_info_free (app_info);

    pulse_ext_stream_update (ext, info, parent);
    return ext;
}

void
pulse_ext_stream_update (PulseExtStream                   *ext,
                         const pa_ext_stream_restore_info *info,
                         PulseStream                      *parent)
{
    g_return_if_fail (PULSE_IS_EXT_STREAM (ext));
    g_return_if_fail (info != NULL);

    /* Let all the information update before emitting notify signals */
    g_object_freeze_notify (G_OBJECT (ext));

    _mate_mixer_stream_control_set_mute (MATE_MIXER_STREAM_CONTROL (ext),
                                         info->mute ? TRUE : FALSE);

    pulse_stream_control_set_channel_map (PULSE_STREAM_CONTROL (ext),
                                          &info->channel_map);

    pulse_stream_control_set_cvolume (PULSE_STREAM_CONTROL (ext),
                                      &info->volume,
                                      0);

    _mate_mixer_stream_control_set_stream (MATE_MIXER_STREAM_CONTROL (ext),
                                           MATE_MIXER_STREAM (parent));

    g_object_thaw_notify (G_OBJECT (ext));
}

static MateMixerDirection
pulse_ext_stream_get_direction (MateMixerStoredControl *mmsc)
{
    g_return_val_if_fail (PULSE_IS_EXT_STREAM (mmsc), MATE_MIXER_DIRECTION_UNKNOWN);

    return PULSE_EXT_STREAM (mmsc)->priv->direction;
}

static MateMixerAppInfo *
pulse_ext_stream_get_app_info (MateMixerStreamControl *mmsc)
{
    g_return_val_if_fail (PULSE_IS_EXT_STREAM (mmsc), NULL);

    return PULSE_EXT_STREAM (mmsc)->priv->app_info;
}

static gboolean
pulse_ext_stream_set_stream (MateMixerStreamControl *mmsc, MateMixerStream *mms)
{
    pa_ext_stream_restore_info info;

    g_return_val_if_fail (PULSE_IS_EXT_STREAM (mmsc), FALSE);
    g_return_val_if_fail (mms == NULL || PULSE_IS_STREAM (mms), FALSE);

    fill_ext_stream_restore_info (PULSE_STREAM_CONTROL (mmsc), &info);

    if (mms != NULL)
        info.device = mate_mixer_stream_get_name (mms);
    else
        info.device = NULL;

    return pulse_connection_write_ext_stream (pulse_stream_control_get_connection (PULSE_STREAM_CONTROL (mmsc)),
                                              &info);
}

static gboolean
pulse_ext_stream_set_mute (PulseStreamControl *control, gboolean mute)
{
    pa_ext_stream_restore_info info;

    g_return_val_if_fail (PULSE_IS_EXT_STREAM (control), FALSE);

    fill_ext_stream_restore_info (control, &info);

    info.mute = mute;

    return pulse_connection_write_ext_stream (pulse_stream_control_get_connection (control),
                                              &info);
}

static gboolean
pulse_ext_stream_set_volume (PulseStreamControl *control, pa_cvolume *cvolume)
{
    pa_ext_stream_restore_info info;

    g_return_val_if_fail (PULSE_IS_EXT_STREAM (control), FALSE);
    g_return_val_if_fail (cvolume != NULL, FALSE);

    fill_ext_stream_restore_info (control, &info);

    info.volume = *cvolume;

    return pulse_connection_write_ext_stream (pulse_stream_control_get_connection (control),
                                              &info);
}

static void
fill_ext_stream_restore_info (PulseStreamControl         *control,
                              pa_ext_stream_restore_info *info)
{
    MateMixerStream        *stream;
    MateMixerStreamControl *mmsc;
    const pa_channel_map   *map;
    const pa_cvolume       *cvolume;

    mmsc = MATE_MIXER_STREAM_CONTROL (control);

    info->name = mate_mixer_stream_control_get_name (mmsc);
    info->mute = mate_mixer_stream_control_get_mute (mmsc);

    map = pulse_stream_control_get_channel_map (control);
    if G_LIKELY (map != NULL)
        info->channel_map = *map;
    else
        pa_channel_map_init (&info->channel_map);

    cvolume = pulse_stream_control_get_cvolume (control);
    if G_LIKELY (cvolume != NULL)
        info->volume = *cvolume;
    else
        pa_cvolume_init (&info->volume);

    stream = mate_mixer_stream_control_get_stream (mmsc);
    if (stream != NULL)
        info->device = mate_mixer_stream_get_name (stream);
    else
        info->device = NULL;
}