dc9c756902b8fb21605bad8187e0bb8cc3c4085d
[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 pa_null_sink {
42         char      *name;
43         uint32_t   module_index;
44         uint32_t   sink_index;
45 };
46
47 pa_null_sink *pa_utils_create_null_sink (struct userdata *u, const char *name)
48 {
49         pa_core      *core;
50         pa_module    *module;
51         pa_null_sink *ns;
52         pa_sink      *s, *sink;
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, pa_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 (pa_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 pa_utils_destroy_null_sink (struct userdata *u, pa_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 *pa_utils_get_null_sink (struct userdata *u, struct pa_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 *pa_utils_get_null_source (struct userdata *u, struct pa_null_sink *ns)
115 {
116         pa_sink *sink;
117
118         sink = pa_utils_get_null_sink (u, ns);
119
120         return sink ? sink->monitor_source : NULL;
121 }
122
123
124 const char *pa_utils_get_card_name (pa_card *card)
125 {
126         return (card && card->name) ? card->name : "<unknown>";
127 }
128
129 const char *pa_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 = pa_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 *pa_utils_get_sink_name (pa_sink *sink)
150 {
151         return (sink && sink->name) ? sink->name : "<unknown>";
152 }
153
154 const char *pa_utils_get_source_name (pa_source *source)
155 {
156         return (source && source->name) ? source->name : "<unknown>";
157 }
158
159 const char *pa_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 *pa_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 *pa_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"))
200                         return sink;
201         }
202
203         return NULL;
204 }
205
206 void pa_utils_init_stamp (void)
207 {
208         stamp = 0;
209 }
210
211 uint32_t pa_utils_new_stamp (void)
212 {
213         return ++stamp;
214 }
215
216 uint32_t pa_utils_get_stamp (void)
217 {
218         return stamp;
219 }
220
221
222
223 char *pa_utils_get_zone (pa_proplist *pl, pa_proplist *client_props)
224 {
225         const char *zone;
226
227         pa_assert (pl);
228
229          /* grab the "zone.name" PA_PROP environment variable ;
230           * otherwise just fall back to default "driver" zone */
231         zone = pa_proplist_gets (pl, PA_PROP_ZONE_NAME);
232         if (!zone) {
233                 if (!client_props || !(zone = pa_proplist_gets (client_props, PA_PROP_ENV_ZONE)))
234                         zone = PA_ZONE_NAME_DEFAULT;            
235         }
236
237         return (char *)zone;
238 }
239
240 bool pa_utils_set_stream_routing_properties (pa_proplist *pl, int styp, void *target)
241 {
242         const char *clnam;      /* will become "agl_player" e.g. */
243         char clid[32];          /* will become "1" e.g. */
244         const char *method;     /* will become "default" (it target is NULL) or "explicit" */
245
246         pa_assert (pl);
247         pa_assert (styp >= 0);  /* type different from "agl_node_type_unknown" */
248
249         clnam = agl_node_type_str (styp);
250         snprintf (clid, sizeof(clid), "%d", styp);
251         method = target ? "explicit" : "default";
252
253         if (pa_proplist_sets (pl, PA_PROP_ROUTING_CLASS_NAME, clnam ) < 0 ||
254             pa_proplist_sets (pl, PA_PROP_ROUTING_CLASS_ID  , clid  ) < 0 ||
255             pa_proplist_sets (pl, PA_PROP_ROUTING_METHOD    , method) < 0)
256         {
257                 pa_log ("failed to set some stream property");
258                 return false;
259         }
260
261         return true;
262 }
263
264 bool pa_utils_unset_stream_routing_properties (pa_proplist *pl)
265 {
266         pa_assert (pl);
267
268         if (pa_proplist_unset (pl, PA_PROP_ROUTING_CLASS_NAME) < 0 ||
269             pa_proplist_unset (pl, PA_PROP_ROUTING_CLASS_ID  ) < 0 ||
270             pa_proplist_unset (pl, PA_PROP_ROUTING_METHOD    ) < 0)
271         {
272                 pa_log ("failed to unset some stream property");
273                 return false;
274         }
275
276         return true;    
277 }