9476c6914c9150b387e44bee5f2db8b00919efbe
[staging/agl-audio-plugin.git] / utils.c
1 /*
2  * module-agl-audio -- PulseAudio module for providing audio routing support
3  * (forked from "module-murphy-ivi" - https://github.com/otcshare )
4  * Copyright (c) 2012, Intel Corporation.
5  * Copyright (c) 2016, IoT.bzh
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms and conditions of the GNU Lesser General Public License,
9  * version 2.1, as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St - Fifth Floor, Boston,
19  * MA 02110-1301 USA.
20  *
21  */
22 #include "userdata.h"
23 #include "utils.h"
24 #include "node.h"
25
26 #define DEFAULT_NULL_SINK_NAME "null.agl"
27
28 #ifndef PA_PROP_PROCESS_ENVIRONMENT
29 #define PA_PROP_PROCESS_ENVIRONMENT "application.process.environment"
30 #endif
31 #define PA_ZONE_NAME_DEFAULT "driver"
32 #define PA_PROP_ZONE_NAME "zone.name"
33 #define PA_PROP_ENV_ZONE  PA_PROP_PROCESS_ENVIRONMENT ".AUDIO_ZONE"
34
35 #define PA_PROP_ROUTING_CLASS_NAME "routing.class.name"
36 #define PA_PROP_ROUTING_CLASS_ID   "routing.class.id"
37 #define PA_PROP_ROUTING_METHOD     "routing.method"
38
39 static uint32_t stamp;
40
41 struct agl_null_sink {
42         char      *name;
43         uint32_t   module_index;
44         uint32_t   sink_index;
45 };
46
47 agl_null_sink *agl_utils_create_null_sink (struct userdata *u, const char *name)
48 {
49         pa_core      *core;
50         pa_module    *module;
51         pa_sink      *s, *sink;
52         agl_null_sink *ns;
53         uint32_t      idx;
54         char          args[256];
55
56         pa_assert (u);
57         pa_assert_se (core = u->core);
58
59         if (!name)
60                 name = DEFAULT_NULL_SINK_NAME;  /* default is "null.agl" */
61
62         snprintf (args, sizeof(args), "sink_name=\"%s.%d\" channels=2", name, agl_utils_new_stamp ());
63         module = pa_module_load (core, "module-null-sink", args);
64         sink = NULL;
65
66         if (!module) {
67                 pa_log ("failed to load null sink '%s'", name);
68                 return NULL;
69         } else {
70                 PA_IDXSET_FOREACH(s, core->sinks, idx) {
71                         if (s->module && s->module == module) {
72                                 sink = s;
73                                 pa_log_info("created agl null sink named '%s'", name);
74                                 break;
75                         }
76                 }
77         }
78
79         ns = pa_xnew0 (agl_null_sink, 1);
80         ns->name = pa_xstrdup (name);
81         ns->module_index = module ? module->index : PA_IDXSET_INVALID;
82         ns->sink_index = sink ? sink->index : PA_IDXSET_INVALID;
83
84         return ns;
85 }
86
87 void agl_utils_destroy_null_sink (struct userdata *u, agl_null_sink *ns)
88 {
89         pa_core      *core;
90         pa_module    *module;
91
92         if (u && (core = u->core)) {
93                 if ((module = pa_idxset_get_by_index (core->modules, ns->module_index))){
94                         pa_log_info ("unloading null sink '%s'", ns->name);
95                         pa_module_unload (core, module, false);
96                 }
97
98                 pa_xfree (ns->name);
99                 pa_xfree (ns);
100         }
101 }
102
103 pa_sink *agl_utils_get_null_sink (struct userdata *u, struct agl_null_sink *ns)
104 {
105         pa_core *core;
106         pa_sink *sink;
107
108         pa_assert (u);
109         pa_assert_se ((core = u->core));
110
111         return pa_idxset_get_by_index (core->sinks, ns->sink_index);
112 }
113
114 pa_source *agl_utils_get_null_source (struct userdata *u, struct agl_null_sink *ns)
115 {
116         pa_sink *sink;
117
118         sink = agl_utils_get_null_sink (u, ns);
119
120         return sink ? sink->monitor_source : NULL;
121 }
122
123
124 const char *agl_utils_get_card_name (pa_card *card)
125 {
126         return (card && card->name) ? card->name : "<unknown>";
127 }
128
129 const char *agl_utils_get_card_bus (pa_card *card)
130 {
131         const char *bus = NULL;
132         const char *name;
133
134         if (card && !(bus = pa_proplist_gets (card->proplist,PA_PROP_DEVICE_BUS))) {
135                 name = agl_utils_get_card_name (card);
136                 if (!strncmp (name, "alsa_card.", 10)) {
137                         if (!strncmp (name + 10, "pci-", 4))
138                                 bus = "pci";
139                         else if (!strncmp (name + 10, "platform-", 9))
140                                 bus = "platform";
141                         else if (!strncmp (name + 10, "usb-", 4))
142                                 bus = "usb";
143                 }
144         }
145
146         return (char *)bus;
147 }
148
149 const char *agl_utils_get_sink_name (pa_sink *sink)
150 {
151         return (sink && sink->name) ? sink->name : "<unknown>";
152 }
153
154 const char *agl_utils_get_source_name (pa_source *source)
155 {
156         return (source && source->name) ? source->name : "<unknown>";
157 }
158
159 const char *agl_utils_get_sink_input_name (pa_sink_input *sinp)
160 {
161         char *name = NULL;
162
163         if (sinp && sinp->proplist) {
164                 name = (char *)pa_proplist_gets (sinp->proplist, PA_PROP_APPLICATION_NAME);
165                 if (!name)
166                         name = (char *)pa_proplist_gets (sinp->proplist, PA_PROP_APPLICATION_PROCESS_BINARY);
167                 if (!name)
168                         name = "<unknown>";
169         }
170
171         return (const char *)name;
172 }
173
174 const char *agl_utils_get_source_output_name (pa_source_output *sout)
175 {
176         char *name = NULL;
177
178         if (sout && sout->proplist) {
179                 name = (char *)pa_proplist_gets (sout->proplist, PA_PROP_APPLICATION_NAME);
180                 if (!name)
181                         name = (char *)pa_proplist_gets (sout->proplist, PA_PROP_APPLICATION_PROCESS_BINARY);
182                 if (!name)
183                         name = "<unknown>";
184         }
185
186         return (const char *)name;
187 }
188
189 pa_sink *agl_utils_get_primary_alsa_sink (struct userdata *u)
190 {
191         pa_core *core;
192         pa_sink *sink;
193         int idx;
194
195         pa_assert (u);
196         pa_assert_se ((core = u->core));
197
198         PA_IDXSET_FOREACH(sink, core->sinks, idx) {
199                 if (sink->name && strstr (sink->name, "alsa_output") && strstr (sink->name, "pci"))
200                         return sink;
201         }
202
203         return NULL;
204 }
205
206 pa_sink *agl_utils_get_alsa_sink (struct userdata *u, const char *name)
207 {
208         pa_core *core;
209         pa_sink *sink;
210         int idx;
211
212         pa_assert (u);
213         pa_assert_se ((core = u->core));
214
215         PA_IDXSET_FOREACH(sink, core->sinks, idx) {
216                 if (sink->name && strstr (sink->name, "alsa_output")
217                                && strstr (sink->name, name))
218                                 return sink;
219         }
220
221         return NULL;
222 }
223
224 void agl_utils_init_stamp (void)
225 {
226         stamp = 0;
227 }
228
229 uint32_t agl_utils_new_stamp (void)
230 {
231         return ++stamp;
232 }
233
234 uint32_t agl_utils_get_stamp (void)
235 {
236         return stamp;
237 }
238
239
240
241 char *agl_utils_get_zone (pa_proplist *pl, pa_proplist *client_props)
242 {
243         const char *zone;
244
245         pa_assert (pl);
246
247          /* grab the "zone.name" PA_PROP environment variable ;
248           * otherwise just fall back to default "driver" zone */
249         zone = pa_proplist_gets (pl, PA_PROP_ZONE_NAME);
250         if (!zone) {
251                 if (!client_props || !(zone = pa_proplist_gets (client_props, PA_PROP_ENV_ZONE)))
252                         zone = PA_ZONE_NAME_DEFAULT;            
253         }
254
255         return (char *)zone;
256 }
257
258 bool agl_utils_set_stream_routing_properties (pa_proplist *pl, int styp, void *target)
259 {
260         const char *clnam;      /* will become "agl_player" e.g. */
261         char clid[32];          /* will become "1" e.g. */
262         const char *method;     /* will become "default" (it target is NULL) or "explicit" */
263
264         pa_assert (pl);
265         pa_assert (styp >= 0);  /* type different from "agl_node_type_unknown" */
266
267         clnam = agl_node_type_str (styp);
268         snprintf (clid, sizeof(clid), "%d", styp);
269         method = target ? "explicit" : "default";
270
271         if (pa_proplist_sets (pl, PA_PROP_ROUTING_CLASS_NAME, clnam ) < 0 ||
272             pa_proplist_sets (pl, PA_PROP_ROUTING_CLASS_ID  , clid  ) < 0 ||
273             pa_proplist_sets (pl, PA_PROP_ROUTING_METHOD    , method) < 0)
274         {
275                 pa_log ("failed to set some stream property");
276                 return false;
277         }
278
279         return true;
280 }
281
282 bool agl_utils_unset_stream_routing_properties (pa_proplist *pl)
283 {
284         pa_assert (pl);
285
286         if (pa_proplist_unset (pl, PA_PROP_ROUTING_CLASS_NAME) < 0 ||
287             pa_proplist_unset (pl, PA_PROP_ROUTING_CLASS_ID  ) < 0 ||
288             pa_proplist_unset (pl, PA_PROP_ROUTING_METHOD    ) < 0)
289         {
290                 pa_log ("failed to unset some stream property");
291                 return false;
292         }
293
294         return true;    
295 }