1 /*
2  * Copyright (C) 2014 Michal Ratajsky <michal.ratajsky@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the licence, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <glib.h>
19 #include <glib-object.h>
20 #include <libmatemixer/matemixer.h>
21 #include <libmatemixer/matemixer-private.h>
22 
23 #include <pulse/pulseaudio.h>
24 
25 #include "pulse-connection.h"
26 #include "pulse-device.h"
27 #include "pulse-helpers.h"
28 #include "pulse-monitor.h"
29 #include "pulse-port.h"
30 #include "pulse-stream.h"
31 
32 struct _PulseStreamPrivate
33 {
34     guint32          index;
35     PulseConnection *connection;
36 };
37 
38 enum {
39     PROP_0,
40     PROP_INDEX,
41     PROP_CONNECTION,
42     N_PROPERTIES
43 };
44 
45 static GParamSpec *properties[N_PROPERTIES] = { NULL, };
46 
47 static void pulse_stream_get_property (GObject          *object,
48                                        guint             param_id,
49                                        GValue           *value,
50                                        GParamSpec       *pspec);
51 static void pulse_stream_set_property (GObject          *object,
52                                        guint             param_id,
53                                        const GValue     *value,
54                                        GParamSpec       *pspec);
55 
56 static void pulse_stream_dispose      (GObject          *object);
57 
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE(PulseStream,pulse_stream,MATE_MIXER_TYPE_STREAM)58 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (PulseStream, pulse_stream, MATE_MIXER_TYPE_STREAM)
59 
60 static void
61 pulse_stream_class_init (PulseStreamClass *klass)
62 {
63     GObjectClass *object_class;
64 
65     object_class = G_OBJECT_CLASS (klass);
66     object_class->dispose      = pulse_stream_dispose;
67     object_class->get_property = pulse_stream_get_property;
68     object_class->set_property = pulse_stream_set_property;
69 
70     properties[PROP_INDEX] =
71         g_param_spec_uint ("index",
72                            "Index",
73                            "Index of the stream",
74                            0,
75                            G_MAXUINT,
76                            0,
77                            G_PARAM_READWRITE |
78                            G_PARAM_CONSTRUCT_ONLY |
79                            G_PARAM_STATIC_STRINGS);
80 
81     properties[PROP_CONNECTION] =
82         g_param_spec_object ("connection",
83                              "Connection",
84                              "PulseAudio connection",
85                              PULSE_TYPE_CONNECTION,
86                              G_PARAM_READWRITE |
87                              G_PARAM_CONSTRUCT_ONLY |
88                              G_PARAM_STATIC_STRINGS);
89 
90     g_object_class_install_properties (object_class, N_PROPERTIES, properties);
91 }
92 
93 static void
pulse_stream_get_property(GObject * object,guint param_id,GValue * value,GParamSpec * pspec)94 pulse_stream_get_property (GObject    *object,
95                            guint       param_id,
96                            GValue     *value,
97                            GParamSpec *pspec)
98 {
99     PulseStream *stream;
100 
101     stream = PULSE_STREAM (object);
102 
103     switch (param_id) {
104     case PROP_INDEX:
105         g_value_set_uint (value, stream->priv->index);
106         break;
107     case PROP_CONNECTION:
108         g_value_set_object (value, stream->priv->connection);
109         break;
110     default:
111         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
112         break;
113     }
114 }
115 
116 static void
pulse_stream_set_property(GObject * object,guint param_id,const GValue * value,GParamSpec * pspec)117 pulse_stream_set_property (GObject      *object,
118                            guint         param_id,
119                            const GValue *value,
120                            GParamSpec   *pspec)
121 {
122     PulseStream *stream;
123 
124     stream = PULSE_STREAM (object);
125 
126     switch (param_id) {
127     case PROP_INDEX:
128         stream->priv->index = g_value_get_uint (value);
129         break;
130     case PROP_CONNECTION:
131         stream->priv->connection = g_value_dup_object (value);
132         break;
133     default:
134         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
135         break;
136     }
137 }
138 
139 static void
pulse_stream_init(PulseStream * stream)140 pulse_stream_init (PulseStream *stream)
141 {
142     stream->priv = pulse_stream_get_instance_private (stream);
143 }
144 
145 static void
pulse_stream_dispose(GObject * object)146 pulse_stream_dispose (GObject *object)
147 {
148     PulseStream *stream;
149 
150     stream = PULSE_STREAM (object);
151 
152     g_clear_object (&stream->priv->connection);
153 
154     G_OBJECT_CLASS (pulse_stream_parent_class)->dispose (object);
155 }
156 
157 guint32
pulse_stream_get_index(PulseStream * stream)158 pulse_stream_get_index (PulseStream *stream)
159 {
160     g_return_val_if_fail (PULSE_IS_STREAM (stream), PA_INVALID_INDEX);
161 
162     return stream->priv->index;
163 }
164 
165 PulseConnection *
pulse_stream_get_connection(PulseStream * stream)166 pulse_stream_get_connection (PulseStream *stream)
167 {
168     g_return_val_if_fail (PULSE_IS_STREAM (stream), NULL);
169 
170     return stream->priv->connection;
171 }
172 
173 PulseDevice *
pulse_stream_get_device(PulseStream * stream)174 pulse_stream_get_device (PulseStream *stream)
175 {
176     MateMixerDevice *device;
177 
178     g_return_val_if_fail (PULSE_IS_STREAM (stream), NULL);
179 
180     device = mate_mixer_stream_get_device (MATE_MIXER_STREAM (stream));
181     if (device != NULL)
182         return PULSE_DEVICE (device);
183 
184     return NULL;
185 }
186