pulse 4+ changes
[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/sink.h>
37 #include <pulsecore/card.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 static bool get_unsigned_property(pa_proplist *, const char *,uint32_t *);
60
61
62 pa_null_sink *pa_utils_create_null_sink(struct userdata *u, const char *name)
63 {
64     pa_core      *core;
65     pa_module    *module;
66     pa_null_sink *ns;
67     pa_sink      *sink;
68     pa_sink      *s;
69     uint32_t      idx;
70     char          args[256];
71
72     pa_assert(u);
73     pa_assert_se((core = u->core));
74
75
76     if (!name)
77         name = DEFAULT_NULL_SINK_NAME;
78
79
80     snprintf(args, sizeof(args), "sink_name=\"%s\" channels=2", name);
81     module = pa_module_load(core, "module-null-sink", args);
82     sink = NULL;
83
84     if (!module)
85         pa_log("failed to load null sink '%s'", name);
86     else {
87         PA_IDXSET_FOREACH(s, core->sinks, idx) {
88             if (s->module && s->module == module) {
89                 sink = s;
90                 pa_log_info("mir null sink is '%s'", name);
91                 break;
92             }
93         }
94     }
95
96     ns = pa_xnew0(pa_null_sink, 1);
97     ns->name = pa_xstrdup(name);
98     ns->module_index = module ? module->index : PA_IDXSET_INVALID;
99     ns->sink_index = sink ? sink->index : PA_IDXSET_INVALID;
100
101     return ns;
102 }
103
104 void pa_utils_destroy_null_sink(struct userdata *u)
105 {
106     pa_core      *core;
107     pa_module    *module;
108     pa_null_sink *ns;
109
110     if (u && (ns = u->nullsink) && (core = u->core)) {
111         if ((module = pa_idxset_get_by_index(core->modules,ns->module_index))){
112             pa_log_info("unloading null sink '%s'", ns->name);
113             pa_module_unload(core, module, false);
114         }
115
116         pa_xfree(ns->name);
117         pa_xfree(ns);
118     }
119 }
120
121 pa_sink *pa_utils_get_null_sink(struct userdata *u)
122 {
123     pa_core *core;
124     pa_null_sink *ns;
125
126     pa_assert(u);
127     pa_assert_se((core = u->core));
128     pa_assert_se((ns = u->nullsink));
129
130     return pa_idxset_get_by_index(core->sinks, ns->sink_index);
131 }
132
133 pa_source *pa_utils_get_null_source(struct userdata *u)
134 {
135     pa_sink *ns = pa_utils_get_null_sink(u);
136
137     return ns ? ns->monitor_source : NULL;
138 }
139
140
141
142 char *pa_utils_get_card_name(pa_card *card)
143 {
144     return (card && card->name) ? card->name : "<unknown>";
145 }
146
147 char *pa_utils_get_card_bus(pa_card *card)
148 {
149     const char *bus = NULL;
150     char       *name;
151
152     if (card && !(bus = pa_proplist_gets(card->proplist,PA_PROP_DEVICE_BUS))) {
153         name = pa_utils_get_card_name(card);
154         if (!strncmp(name, "alsa_card.", 10)) {
155             if (!strncmp(name + 10, "pci-", 4))
156                 bus = "pci";
157             else if (!strncmp(name + 10, "usb-", 4))
158                 bus = "usb";
159         }
160     }
161
162     return (char *)bus;
163 }
164
165 char *pa_utils_get_sink_name(pa_sink *sink)
166 {
167     return (sink && sink->name) ? sink->name : "<unknown>";
168 }
169
170 char *pa_utils_get_source_name(pa_source *source)
171 {
172     return (source && source->name) ? source->name : "<unknown>";
173 }
174
175 char *pa_utils_get_sink_input_name(pa_sink_input *sinp)
176 {
177     char *name;
178
179     if (sinp && (name = stream_name(sinp->proplist)))
180         return name;
181
182     return "<unknown>";
183 }
184
185 char *pa_utils_get_sink_input_name_from_data(pa_sink_input_new_data *data)
186 {
187     char *name;
188
189     if (data && (name = stream_name(data->proplist)))
190         return name;
191
192     return "<unknown>";
193 }
194
195
196 char *pa_utils_get_source_output_name(pa_source_output *sout)
197 {
198     char *name;
199
200     if (sout && (name = stream_name(sout->proplist)))
201         return name;
202
203     return "<unknown>";
204 }
205
206 char *pa_utils_get_source_output_name_from_data(pa_source_output_new_data*data)
207 {
208     char *name;
209
210     if (data && (name = stream_name(data->proplist)))
211         return name;
212
213     return "<unknown>";
214 }
215
216 char *pa_utils_get_zone(pa_proplist *pl)
217 {
218     const char *zone;
219
220     pa_assert(pl);
221
222     if (!(zone = pa_proplist_gets(pl, PA_PROP_ZONE_NAME)))
223         zone = PA_ZONE_NAME_DEFAULT;
224
225     return (char *)zone;
226 }
227
228 bool pa_utils_set_stream_routing_properties(pa_proplist *pl,
229                                                  int          styp,
230                                                  void        *target)
231 {
232     const char    *clnam;
233     const char    *method;
234     char           clid[32];
235
236     pa_assert(pl);
237     pa_assert(styp >= 0);
238
239     snprintf(clid, sizeof(clid), "%d", styp);
240     clnam  = mir_node_type_str(styp);
241     method = target ? PA_ROUTING_EXPLICIT : PA_ROUTING_DEFAULT;
242
243     if (pa_proplist_sets(pl, PA_PROP_ROUTING_CLASS_NAME, clnam ) < 0 ||
244         pa_proplist_sets(pl, PA_PROP_ROUTING_CLASS_ID  , clid  ) < 0 ||
245         pa_proplist_sets(pl, PA_PROP_ROUTING_METHOD    , method) < 0  )
246     {
247         pa_log("failed to set some stream property");
248         return false;
249     }
250
251     return true;
252 }
253
254 bool pa_utils_unset_stream_routing_properties(pa_proplist *pl)
255 {
256     pa_assert(pl);
257
258     if (pa_proplist_unset(pl, PA_PROP_ROUTING_CLASS_NAME) < 0 ||
259         pa_proplist_unset(pl, PA_PROP_ROUTING_CLASS_ID  ) < 0 ||
260         pa_proplist_unset(pl, PA_PROP_ROUTING_METHOD    ) < 0  )
261     {
262         pa_log("failed to unset some stream property");
263         return false;
264     }
265
266     return true;
267 }
268
269 void pa_utils_set_stream_routing_method_property(pa_proplist *pl,
270                                                  bool explicit)
271 {
272     const char *method = explicit ? PA_ROUTING_EXPLICIT : PA_ROUTING_DEFAULT;
273
274     pa_assert(pl);
275
276     if (pa_proplist_sets(pl, PA_PROP_ROUTING_METHOD, method) < 0) {
277         pa_log("failed to set routing method property on sink-input");
278     }
279 }
280
281 bool pa_utils_stream_has_default_route(pa_proplist *pl)
282 {
283     const char *method;
284
285     pa_assert(pl);
286
287     method = pa_proplist_gets(pl, PA_PROP_ROUTING_METHOD);
288
289     if (method && pa_streq(method, PA_ROUTING_DEFAULT))
290         return true;
291
292     return false;
293 }
294
295 int pa_utils_get_stream_class(pa_proplist *pl)
296 {
297     const char *clid_str;
298     char *e;
299     unsigned long int clid = 0;
300
301     pa_assert(pl);
302
303     if ((clid_str = pa_proplist_gets(pl, PA_PROP_ROUTING_CLASS_ID))) {
304         clid = strtoul(clid_str, &e, 10);
305
306         if (*e)
307             clid = 0;
308     }
309
310     return (int)clid;
311 }
312
313
314 bool pa_utils_set_resource_properties(pa_proplist *pl,
315                                            pa_nodeset_resdef *resdef)
316 {
317     char priority[32];
318     char rsetflags[32];
319     char audioflags[32];
320
321     pa_assert(pl);
322
323     if (!resdef)
324         return false;
325
326     snprintf(priority  , sizeof(priority)  , "%d", resdef->priority   );
327     snprintf(rsetflags , sizeof(rsetflags) , "%d", resdef->flags.rset );
328     snprintf(audioflags, sizeof(audioflags), "%d", resdef->flags.audio);
329
330     if (pa_proplist_sets(pl, PA_PROP_RESOURCE_PRIORITY   , priority  ) < 0 ||
331         pa_proplist_sets(pl, PA_PROP_RESOURCE_SET_FLAGS  , rsetflags ) < 0 ||
332         pa_proplist_sets(pl, PA_PROP_RESOURCE_AUDIO_FLAGS, audioflags) < 0  )
333     {
334         pa_log("failed to set some resource property");
335         return false;
336     }
337
338     return true;
339 }
340
341 bool pa_utils_unset_resource_properties(pa_proplist *pl)
342 {
343     pa_assert(pl);
344
345     if (pa_proplist_unset(pl, PA_PROP_RESOURCE_PRIORITY   ) < 0 ||
346         pa_proplist_unset(pl, PA_PROP_RESOURCE_SET_FLAGS  ) < 0 ||
347         pa_proplist_unset(pl, PA_PROP_RESOURCE_AUDIO_FLAGS) < 0  )
348     {
349         pa_log("failed to unset some resource property");
350         return false;
351     }
352
353     return true;
354 }
355
356 pa_nodeset_resdef *pa_utils_get_resource_properties(pa_proplist *pl,
357                                                     pa_nodeset_resdef *rd)
358 {
359     pa_assert(pl);
360     pa_assert(rd);
361
362     int success;
363
364     memset(rd, 0, sizeof(pa_nodeset_resdef));
365
366     success  = get_unsigned_property(pl, PA_PROP_RESOURCE_PRIORITY,
367                                      &rd->priority);
368     success |= get_unsigned_property(pl, PA_PROP_RESOURCE_SET_FLAGS,
369                                      &rd->flags.rset);
370     success |= get_unsigned_property(pl, PA_PROP_RESOURCE_AUDIO_FLAGS,
371                                      &rd->flags.audio);
372     
373     return success ? rd : NULL;
374 }
375
376
377 static bool get_unsigned_property(pa_proplist *pl,
378                                        const char *name,
379                                        uint32_t *value)
380 {
381     const char *str;
382     char *e;
383
384     pa_assert(pl);
385     pa_assert(name);
386     pa_assert(value);
387
388     if (!(str = pa_proplist_gets(pl, name))) {
389         *value = 0;
390         return false;
391     }
392
393     *value = strtoul(str, &e, 10);
394
395     if (e == str || *e) {
396         *value = 0;
397         return false;
398     }
399
400     return true;
401 }
402
403
404 void pa_utils_set_port_properties(pa_device_port *port, mir_node *node)
405 {
406     const char *profile;
407     char propnam[512];
408     char nodeidx[256];
409
410     pa_assert(port);
411     pa_assert(port->proplist);
412     pa_assert(node);
413     pa_assert_se((profile = node->pacard.profile));
414
415     snprintf(propnam, sizeof(propnam), "%s.%s", PA_PROP_NODE_INDEX, profile);
416     snprintf(nodeidx, sizeof(nodeidx), "%u", node->index);
417
418     pa_proplist_sets(port->proplist, propnam, nodeidx);
419 }
420
421 mir_node *pa_utils_get_node_from_port(struct userdata *u,
422                                       pa_device_port *port,
423                                       void **state)
424 {
425     const char *name;
426     const char *value;
427     char *e;
428     uint32_t index = PA_IDXSET_INVALID;
429     mir_node *node = NULL;
430
431     pa_assert(u);
432     pa_assert(port);
433     pa_assert(port->proplist);
434
435     while ((name = pa_proplist_iterate(port->proplist, state))) {
436         if (!strncmp(name, PA_PROP_NODE_INDEX, sizeof(PA_PROP_NODE_INDEX)-1)) {
437             if ((value = pa_proplist_gets(port->proplist, name))) {
438                 index = strtoul(value, &e, 10);
439                 node = NULL;
440
441                 if (value[0] && !e[0])
442                     node = mir_node_find_by_index(u, index);
443                 
444                 if (node)
445                     return node;
446
447                 pa_log("Can't find node %u for port %s", index, port->name);
448             }
449         }
450     }
451
452     return NULL;
453 }
454
455 mir_node *pa_utils_get_node_from_stream(struct userdata *u,
456                                         mir_direction    type,
457                                         void            *ptr)
458 {
459     pa_sink_input    *sinp;
460     pa_source_output *sout;
461     pa_proplist      *pl;
462     mir_node         *node;
463     const char       *index_str;
464     uint32_t          index = PA_IDXSET_INVALID;
465     char             *e;
466     char              name[256];
467
468     pa_assert(u);
469     pa_assert(ptr);
470     pa_assert(type == mir_input || type == mir_output);
471
472     if (type == mir_input) {
473         sinp = (pa_sink_input *)ptr;
474         pl = sinp->proplist;
475         snprintf(name, sizeof(name), "sink-input.%u", sinp->index);
476     }
477     else {
478         sout = (pa_source_output *)ptr;
479         pl = sout->proplist;
480         snprintf(name, sizeof(name), "source-output.%u", sout->index);
481     }
482
483
484     if ((index_str = pa_proplist_gets(pl, PA_PROP_NODE_INDEX))) {
485         index = strtoul(index_str, &e, 10);
486         if (e != index_str && *e == '\0') {
487             if ((node = mir_node_find_by_index(u, index)))
488                 return node;
489
490             pa_log_debug("can't find find node for %s", name);
491         }
492     }
493
494     return NULL;
495 }
496
497 mir_node *pa_utils_get_node_from_data(struct userdata *u,
498                                       mir_direction    type,
499                                       void            *ptr)
500 {
501     pa_sink_input_new_data *sinp;
502     pa_source_output_new_data *sout;
503     pa_proplist  *pl;
504     mir_node     *node;
505     const char   *index_str;
506     uint32_t      index = PA_IDXSET_INVALID;
507     char         *e;
508     char          name[256];
509
510     pa_assert(u);
511     pa_assert(ptr);
512     pa_assert(type == mir_input || type == mir_output);
513
514     if (type == mir_input) {
515         sinp = (pa_sink_input_new_data *)ptr;
516         pl = sinp->proplist;
517         snprintf(name, sizeof(name), "sink-input");
518     }
519     else {
520         sout = (pa_source_output_new_data *)ptr;
521         pl = sout->proplist;
522         snprintf(name, sizeof(name), "source-output");
523     }
524
525
526     if ((index_str = pa_proplist_gets(pl, PA_PROP_NODE_INDEX))) {
527         index = strtoul(index_str, &e, 10);
528         if (e != index_str && *e == '\0') {
529             if ((node = mir_node_find_by_index(u, index)))
530                 return node;
531
532             pa_log_debug("can't find find node for %s", name);
533         }
534     }
535
536     return NULL;
537 }
538
539 static char *stream_name(pa_proplist *pl)
540 {
541     const char  *appnam;
542     const char  *binnam;
543
544     if ((appnam = pa_proplist_gets(pl, PA_PROP_APPLICATION_NAME)))
545         return (char *)appnam;
546
547     if ((binnam = pa_proplist_gets(pl, PA_PROP_APPLICATION_PROCESS_BINARY)))
548         return (char *)binnam;
549
550     return NULL;
551 }
552
553
554 const char *pa_utils_file_path(const char *dir, const char *file,
555                                char *buf, size_t len)
556 {
557     pa_assert(file);
558     pa_assert(buf);
559     pa_assert(len > 0);
560
561     snprintf(buf, len, "%s/%s", dir, file);
562
563     return buf;
564 }
565
566
567 uint32_t pa_utils_new_stamp(void)
568 {
569     return ++stamp;
570 }
571
572 uint32_t pa_utils_get_stamp(void)
573 {
574     return stamp;
575 }
576
577
578
579 /*
580  * Local Variables:
581  * c-basic-offset: 4
582  * indent-tabs-mode: nil
583  * End:
584  *
585  */
586
587