utils: guessing bus type for alsa cards if device.bus property were not set
[profile/ivi/pulseaudio-module-murphy-ivi.git] / murphy / utils.c
1 /*
2  * module-murphy-ivi -- PulseAudio module for providing audio routing support
3  * Copyright (c) 2012, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU Lesser General Public License,
7  * version 2.1, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.
12  * See the GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St - Fifth Floor, Boston,
17  * MA 02110-1301 USA.
18  *
19  */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/stat.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <limits.h>
29
30 #include <sys/types.h>
31 #include <sys/socket.h>
32
33 #include <pulsecore/pulsecore-config.h>
34
35 #include <pulsecore/core-util.h>
36 #include <pulsecore/card.h>
37 #include <pulsecore/sink.h>
38 #include <pulsecore/source.h>
39 #include <pulsecore/sink-input.h>
40 #include <pulsecore/source-output.h>
41
42 #include "userdata.h"
43 #include "utils.h"
44 #include "node.h"
45
46
47 #define DEFAULT_NULL_SINK_NAME "null.mir"
48
49 struct pa_null_sink {
50     char      *name;
51     uint32_t   module_index;
52     uint32_t   sink_index;
53 };
54
55
56 static uint32_t stamp;
57
58 static char *stream_name(pa_proplist *);
59
60
61 pa_null_sink *pa_utils_create_null_sink(struct userdata *u, const char *name)
62 {
63     pa_core      *core;
64     pa_module    *module;
65     pa_null_sink *ns;
66     pa_sink      *sink;
67     pa_sink      *s;
68     uint32_t      idx;
69     char          args[256];
70
71     pa_assert(u);
72     pa_assert_se((core = u->core));
73
74
75     if (!name)
76         name = DEFAULT_NULL_SINK_NAME;
77
78
79     snprintf(args, sizeof(args), "sink_name=\"%s\" channels=2", name);
80     module = pa_module_load(core, "module-null-sink", args);
81     sink = NULL;
82
83     if (!module)
84         pa_log("failed to load null sink '%s'", name);
85     else {
86         PA_IDXSET_FOREACH(s, core->sinks, idx) {
87             if (s->module && s->module == module) {
88                 sink = s;
89                 pa_log_info("mir null sink is '%s'", name);
90                 break;
91             }
92         }
93     }
94
95     ns = pa_xnew0(pa_null_sink, 1);
96     ns->name = pa_xstrdup(name);
97     ns->module_index = module ? module->index : PA_IDXSET_INVALID;
98     ns->sink_index = sink ? sink->index : PA_IDXSET_INVALID;
99
100     return ns;
101 }
102
103 void pa_utils_destroy_null_sink(struct userdata *u)
104 {
105     pa_core      *core;
106     pa_module    *module;
107     pa_null_sink *ns;
108
109     if (u && (ns = u->nullsink) && (core = u->core)) {
110         if ((module = pa_idxset_get_by_index(core->modules,ns->module_index))){
111             pa_log_info("unloading null sink '%s'", ns->name);
112             pa_module_unload(core, module, FALSE);
113         }
114
115         pa_xfree(ns->name);
116         pa_xfree(ns);
117     }
118 }
119
120 pa_sink *pa_utils_get_null_sink(struct userdata *u)
121 {
122     pa_core *core;
123     pa_null_sink *ns;
124
125     pa_assert(u);
126     pa_assert_se((core = u->core));
127     pa_assert_se((ns = u->nullsink));
128
129     return pa_idxset_get_by_index(core->sinks, ns->sink_index);
130 }
131
132 pa_source *pa_utils_get_null_source(struct userdata *u)
133 {
134     pa_sink *ns = pa_utils_get_null_sink(u);
135
136     return ns ? ns->monitor_source : NULL;
137 }
138
139
140
141 char *pa_utils_get_card_name(pa_card *card)
142 {
143     return (card && card->name) ? card->name : "<unknown>";
144 }
145
146 char *pa_utils_get_card_bus(pa_card *card)
147 {
148     const char *bus = NULL;
149     char       *name;
150
151     if (card && !(bus = pa_proplist_gets(card->proplist,PA_PROP_DEVICE_BUS))) {
152         name = pa_utils_get_card_name(card);
153         if (!strncmp(name, "alsa_card.", 10)) {
154             if (!strncmp(name + 10, "pci-", 4))
155                 bus = "pci";
156             else if (!strncmp(name + 10, "usb-", 4))
157                 bus = "usb";
158         }
159     }
160
161     return (char *)bus;
162 }
163
164 char *pa_utils_get_sink_name(pa_sink *sink)
165 {
166     return (sink && sink->name) ? sink->name : "<unknown>";
167 }
168
169 char *pa_utils_get_source_name(pa_source *source)
170 {
171     return (source && source->name) ? source->name : "<unknown>";
172 }
173
174 char *pa_utils_get_sink_input_name(pa_sink_input *sinp)
175 {
176     char *name;
177
178     if (sinp && (name = stream_name(sinp->proplist)))
179         return name;
180
181     return "<unknown>";
182 }
183
184 char *pa_utils_get_sink_input_name_from_data(pa_sink_input_new_data *data)
185 {
186     char *name;
187
188     if (data && (name = stream_name(data->proplist)))
189         return name;
190
191     return "<unknown>";
192 }
193
194
195 char *pa_utils_get_source_output_name(pa_source_output *sout)
196 {
197     char *name;
198
199     if (sout && (name = stream_name(sout->proplist)))
200         return name;
201
202     return "<unknown>";
203 }
204
205 char *pa_utils_get_source_output_name_from_data(pa_source_output_new_data*data)
206 {
207     char *name;
208
209     if (data && (name = stream_name(data->proplist)))
210         return name;
211
212     return "<unknown>";
213 }
214
215
216 void pa_utils_set_stream_routing_properties(pa_proplist *pl,
217                                             int          styp,
218                                             void        *target)
219 {
220     const char    *clnam;
221     const char    *method;
222     char           clid[32];
223
224     pa_assert(pl);
225     pa_assert(styp >= 0);
226
227     snprintf(clid, sizeof(clid), "%d", styp);
228     clnam  = mir_node_type_str(styp);
229     method = target ? PA_ROUTING_EXPLICIT : PA_ROUTING_DEFAULT;
230
231     if (pa_proplist_sets(pl, PA_PROP_ROUTING_CLASS_NAME, clnam ) < 0 ||
232         pa_proplist_sets(pl, PA_PROP_ROUTING_CLASS_ID  , clid  ) < 0 ||
233         pa_proplist_sets(pl, PA_PROP_ROUTING_METHOD    , method) < 0  )
234     {
235         pa_log("failed to set some stream property");
236     }
237 }
238
239 void pa_utils_set_stream_routing_method_property(pa_proplist *pl,
240                                                  pa_bool_t explicit)
241 {
242     const char *method = explicit ? PA_ROUTING_EXPLICIT : PA_ROUTING_DEFAULT;
243
244     pa_assert(pl);
245
246     if (pa_proplist_sets(pl, PA_PROP_ROUTING_METHOD, method) < 0) {
247         pa_log("failed to set routing method property on sink-input");
248     }
249 }
250
251 pa_bool_t pa_utils_stream_has_default_route(pa_proplist *pl)
252 {
253     const char *method;
254
255     pa_assert(pl);
256
257     method = pa_proplist_gets(pl, PA_PROP_ROUTING_METHOD);
258
259     if (method && pa_streq(method, PA_ROUTING_DEFAULT))
260         return TRUE;
261
262     return FALSE;
263 }
264
265 int pa_utils_get_stream_class(pa_proplist *pl)
266 {
267     const char *clid_str;
268     char *e;
269     unsigned long int clid = 0;
270
271     pa_assert(pl);
272
273     if ((clid_str = pa_proplist_gets(pl, PA_PROP_ROUTING_CLASS_ID))) {
274         clid = strtoul(clid_str, &e, 10);
275
276         if (*e)
277             clid = 0;
278     }
279
280     return (int)clid;
281 }
282
283 void pa_utils_set_port_properties(pa_device_port *port, mir_node *node)
284 {
285     char nodeidx[256];
286
287     pa_assert(port);
288     pa_assert(port->proplist);
289     pa_assert(node);
290
291     snprintf(nodeidx, sizeof(nodeidx), "%u", node->index);
292
293     pa_proplist_sets(port->proplist, PA_PROP_NODE_INDEX, nodeidx);
294 }
295
296 mir_node *pa_utils_get_node_from_port(struct userdata *u,
297                                       pa_device_port *port)
298 {
299     const char *value;
300     char *e;
301     uint32_t index = PA_IDXSET_INVALID;
302     mir_node *node = NULL;
303
304     pa_assert(u);
305     pa_assert(port);
306     pa_assert(port->proplist);
307
308     if ((value = pa_proplist_gets(port->proplist, PA_PROP_NODE_INDEX))) {
309         index = strtoul(value, &e, 10);
310
311         if (value[0] && !e[0])
312             node = mir_node_find_by_index(u, index);
313     }
314
315     return node;
316 }
317
318 mir_node *pa_utils_get_node_from_stream(struct userdata *u,
319                                         mir_direction    type,
320                                         void            *ptr)
321 {
322     pa_sink_input    *sinp;
323     pa_source_output *sout;
324     pa_proplist      *pl;
325     mir_node         *node;
326     const char       *index_str;
327     uint32_t          index = PA_IDXSET_INVALID;
328     char             *e;
329     char              name[256];
330
331     pa_assert(u);
332     pa_assert(ptr);
333     pa_assert(type == mir_input || type == mir_output);
334
335     if (type == mir_input) {
336         sinp = (pa_sink_input *)ptr;
337         pl = sinp->proplist;
338         snprintf(name, sizeof(name), "sink-input.%u", sinp->index);
339     }
340     else {
341         sout = (pa_source_output *)ptr;
342         pl = sout->proplist;
343         snprintf(name, sizeof(name), "source-output.%u", sout->index);
344     }
345
346
347     if ((index_str = pa_proplist_gets(pl, PA_PROP_NODE_INDEX))) {
348         index = strtoul(index_str, &e, 10);
349         if (e != index_str && *e == '\0') {
350             if ((node = mir_node_find_by_index(u, index)))
351                 return node;
352
353             pa_log_debug("can't find find node for %s", name);
354         }
355     }
356
357     return NULL;
358 }
359
360 mir_node *pa_utils_get_node_from_data(struct userdata *u,
361                                       mir_direction    type,
362                                       void            *ptr)
363 {
364     pa_sink_input_new_data *sinp;
365     pa_source_output_new_data *sout;
366     pa_proplist  *pl;
367     mir_node     *node;
368     const char   *index_str;
369     uint32_t      index = PA_IDXSET_INVALID;
370     char         *e;
371     char          name[256];
372
373     pa_assert(u);
374     pa_assert(ptr);
375     pa_assert(type == mir_input || type == mir_output);
376
377     if (type == mir_input) {
378         sinp = (pa_sink_input_new_data *)ptr;
379         pl = sinp->proplist;
380         snprintf(name, sizeof(name), "sink-input");
381     }
382     else {
383         sout = (pa_source_output_new_data *)ptr;
384         pl = sout->proplist;
385         snprintf(name, sizeof(name), "source-output");
386     }
387
388
389     if ((index_str = pa_proplist_gets(pl, PA_PROP_NODE_INDEX))) {
390         index = strtoul(index_str, &e, 10);
391         if (e != index_str && *e == '\0') {
392             if ((node = mir_node_find_by_index(u, index)))
393                 return node;
394
395             pa_log_debug("can't find find node for %s", name);
396         }
397     }
398
399     return NULL;
400 }
401
402 static char *stream_name(pa_proplist *pl)
403 {
404     const char  *appnam;
405     const char  *binnam;
406
407     if ((appnam = pa_proplist_gets(pl, PA_PROP_APPLICATION_NAME)))
408         return (char *)appnam;
409
410     if ((binnam = pa_proplist_gets(pl, PA_PROP_APPLICATION_PROCESS_BINARY)))
411         return (char *)binnam;
412
413     return NULL;
414 }
415
416
417 const char *pa_utils_file_path(const char *dir, const char *file,
418                                char *buf, size_t len)
419 {
420     pa_assert(file);
421     pa_assert(buf);
422     pa_assert(len > 0);
423
424     snprintf(buf, len, "%s/%s", dir, file);
425
426     return buf;
427 }
428
429
430 uint32_t pa_utils_new_stamp(void)
431 {
432     return ++stamp;
433 }
434
435 uint32_t pa_utils_get_stamp(void)
436 {
437     return stamp;
438 }
439
440
441
442
443 /*
444  * Local Variables:
445  * c-basic-offset: 4
446  * indent-tabs-mode: nil
447  * End:
448  *
449  */
450
451