murphyif: Free the connect timer when unloading
[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, "platform-", 9))
158                 bus = "platform";
159             else if (!strncmp(name + 10, "usb-", 4))
160                 bus = "usb";
161         }
162     }
163
164     return (char *)bus;
165 }
166
167 char *pa_utils_get_sink_name(pa_sink *sink)
168 {
169     return (sink && sink->name) ? sink->name : "<unknown>";
170 }
171
172 char *pa_utils_get_source_name(pa_source *source)
173 {
174     return (source && source->name) ? source->name : "<unknown>";
175 }
176
177 char *pa_utils_get_sink_input_name(pa_sink_input *sinp)
178 {
179     char *name;
180
181     if (sinp && sinp->proplist && (name = stream_name(sinp->proplist)))
182         return name;
183
184     return "<unknown>";
185 }
186
187 char *pa_utils_get_sink_input_name_from_data(pa_sink_input_new_data *data)
188 {
189     char *name;
190
191     if (data && (name = stream_name(data->proplist)))
192         return name;
193
194     return "<unknown>";
195 }
196
197
198 char *pa_utils_get_source_output_name(pa_source_output *sout)
199 {
200     char *name;
201
202     if (sout && sout->proplist && (name = stream_name(sout->proplist)))
203         return name;
204
205     return "<unknown>";
206 }
207
208 char *pa_utils_get_source_output_name_from_data(pa_source_output_new_data*data)
209 {
210     char *name;
211
212     if (data && (name = stream_name(data->proplist)))
213         return name;
214
215     return "<unknown>";
216 }
217
218 char *pa_utils_get_zone(pa_proplist *pl)
219 {
220     const char *zone;
221
222     pa_assert(pl);
223
224     if (!(zone = pa_proplist_gets(pl, PA_PROP_ZONE_NAME)))
225         zone = PA_ZONE_NAME_DEFAULT;
226
227     return (char *)zone;
228 }
229
230 const char *pa_utils_get_appid(pa_proplist *pl)
231 {
232     const char *appid;
233
234     if (pl && (appid = pa_proplist_gets(pl, PA_PROP_RESOURCE_SET_APPID)))
235         return appid;
236
237     return "<unknown>";
238 }
239
240 bool pa_utils_set_stream_routing_properties(pa_proplist *pl,
241                                                  int          styp,
242                                                  void        *target)
243 {
244     const char    *clnam;
245     const char    *method;
246     char           clid[32];
247
248     pa_assert(pl);
249     pa_assert(styp >= 0);
250
251     snprintf(clid, sizeof(clid), "%d", styp);
252     clnam  = mir_node_type_str(styp);
253     method = target ? PA_ROUTING_EXPLICIT : PA_ROUTING_DEFAULT;
254
255     if (pa_proplist_sets(pl, PA_PROP_ROUTING_CLASS_NAME, clnam ) < 0 ||
256         pa_proplist_sets(pl, PA_PROP_ROUTING_CLASS_ID  , clid  ) < 0 ||
257         pa_proplist_sets(pl, PA_PROP_ROUTING_METHOD    , method) < 0  )
258     {
259         pa_log("failed to set some stream property");
260         return false;
261     }
262
263     return true;
264 }
265
266 bool pa_utils_unset_stream_routing_properties(pa_proplist *pl)
267 {
268     pa_assert(pl);
269
270     if (pa_proplist_unset(pl, PA_PROP_ROUTING_CLASS_NAME) < 0 ||
271         pa_proplist_unset(pl, PA_PROP_ROUTING_CLASS_ID  ) < 0 ||
272         pa_proplist_unset(pl, PA_PROP_ROUTING_METHOD    ) < 0  )
273     {
274         pa_log("failed to unset some stream property");
275         return false;
276     }
277
278     return true;
279 }
280
281 void pa_utils_set_stream_routing_method_property(pa_proplist *pl,
282                                                  bool explicit)
283 {
284     const char *method = explicit ? PA_ROUTING_EXPLICIT : PA_ROUTING_DEFAULT;
285
286     pa_assert(pl);
287
288     if (pa_proplist_sets(pl, PA_PROP_ROUTING_METHOD, method) < 0) {
289         pa_log("failed to set routing method property on sink-input");
290     }
291 }
292
293 bool pa_utils_stream_has_default_route(pa_proplist *pl)
294 {
295     const char *method;
296
297     pa_assert(pl);
298
299     method = pa_proplist_gets(pl, PA_PROP_ROUTING_METHOD);
300
301     if (method && pa_streq(method, PA_ROUTING_DEFAULT))
302         return true;
303
304     return false;
305 }
306
307 int pa_utils_get_stream_class(pa_proplist *pl)
308 {
309     const char *clid_str;
310     char *e;
311     unsigned long int clid = 0;
312
313     pa_assert(pl);
314
315     if ((clid_str = pa_proplist_gets(pl, PA_PROP_ROUTING_CLASS_ID))) {
316         clid = strtoul(clid_str, &e, 10);
317
318         if (*e)
319             clid = 0;
320     }
321
322     return (int)clid;
323 }
324
325
326 bool pa_utils_set_resource_properties(pa_proplist *pl,
327                                            pa_nodeset_resdef *resdef)
328 {
329     char priority[32];
330     char rsetflags[32];
331     char audioflags[32];
332
333     pa_assert(pl);
334
335     if (!resdef)
336         return false;
337
338     snprintf(priority  , sizeof(priority)  , "%d", resdef->priority   );
339     snprintf(rsetflags , sizeof(rsetflags) , "%d", resdef->flags.rset );
340     snprintf(audioflags, sizeof(audioflags), "%d", resdef->flags.audio);
341
342     if (pa_proplist_sets(pl, PA_PROP_RESOURCE_PRIORITY   , priority  ) < 0 ||
343         pa_proplist_sets(pl, PA_PROP_RESOURCE_SET_FLAGS  , rsetflags ) < 0 ||
344         pa_proplist_sets(pl, PA_PROP_RESOURCE_AUDIO_FLAGS, audioflags) < 0  )
345     {
346         pa_log("failed to set some resource property");
347         return false;
348     }
349
350     return true;
351 }
352
353 bool pa_utils_unset_resource_properties(pa_proplist *pl)
354 {
355     pa_assert(pl);
356
357     if (pa_proplist_unset(pl, PA_PROP_RESOURCE_PRIORITY   ) < 0 ||
358         pa_proplist_unset(pl, PA_PROP_RESOURCE_SET_FLAGS  ) < 0 ||
359         pa_proplist_unset(pl, PA_PROP_RESOURCE_AUDIO_FLAGS) < 0  )
360     {
361         pa_log("failed to unset some resource property");
362         return false;
363     }
364
365     return true;
366 }
367
368 pa_nodeset_resdef *pa_utils_get_resource_properties(pa_proplist *pl,
369                                                     pa_nodeset_resdef *rd)
370 {
371     pa_assert(pl);
372     pa_assert(rd);
373
374     int success;
375
376     memset(rd, 0, sizeof(pa_nodeset_resdef));
377
378     success  = get_unsigned_property(pl, PA_PROP_RESOURCE_PRIORITY,
379                                      &rd->priority);
380     success |= get_unsigned_property(pl, PA_PROP_RESOURCE_SET_FLAGS,
381                                      &rd->flags.rset);
382     success |= get_unsigned_property(pl, PA_PROP_RESOURCE_AUDIO_FLAGS,
383                                      &rd->flags.audio);
384     
385     return success ? rd : NULL;
386 }
387
388
389 static bool get_unsigned_property(pa_proplist *pl,
390                                        const char *name,
391                                        uint32_t *value)
392 {
393     const char *str;
394     char *e;
395
396     pa_assert(pl);
397     pa_assert(name);
398     pa_assert(value);
399
400     if (!(str = pa_proplist_gets(pl, name))) {
401         *value = 0;
402         return false;
403     }
404
405     *value = strtoul(str, &e, 10);
406
407     if (e == str || *e) {
408         *value = 0;
409         return false;
410     }
411
412     return true;
413 }
414
415
416 void pa_utils_set_port_properties(pa_device_port *port, mir_node *node)
417 {
418     const char *profile;
419     char propnam[512];
420     char nodeidx[256];
421
422     pa_assert(port);
423     pa_assert(port->proplist);
424     pa_assert(node);
425     pa_assert_se((profile = node->pacard.profile));
426
427     snprintf(propnam, sizeof(propnam), "%s.%s", PA_PROP_NODE_INDEX, profile);
428     snprintf(nodeidx, sizeof(nodeidx), "%u", node->index);
429
430     pa_proplist_sets(port->proplist, propnam, nodeidx);
431 }
432
433 mir_node *pa_utils_get_node_from_port(struct userdata *u,
434                                       pa_device_port *port,
435                                       void **state)
436 {
437     const char *name;
438     const char *value;
439     char *e;
440     uint32_t index = PA_IDXSET_INVALID;
441     mir_node *node = NULL;
442
443     pa_assert(u);
444     pa_assert(port);
445     pa_assert(port->proplist);
446
447     while ((name = pa_proplist_iterate(port->proplist, state))) {
448         if (!strncmp(name, PA_PROP_NODE_INDEX, sizeof(PA_PROP_NODE_INDEX)-1)) {
449             if ((value = pa_proplist_gets(port->proplist, name))) {
450                 index = strtoul(value, &e, 10);
451                 node = NULL;
452
453                 if (value[0] && !e[0])
454                     node = mir_node_find_by_index(u, index);
455                 
456                 if (node)
457                     return node;
458
459                 pa_log("Can't find node %u for port %s", index, port->name);
460             }
461         }
462     }
463
464     return NULL;
465 }
466
467 mir_node *pa_utils_get_node_from_stream(struct userdata *u,
468                                         mir_direction    type,
469                                         void            *ptr)
470 {
471     pa_sink_input    *sinp;
472     pa_source_output *sout;
473     pa_proplist      *pl;
474     mir_node         *node;
475     const char       *index_str;
476     uint32_t          index = PA_IDXSET_INVALID;
477     char             *e;
478     char              name[256];
479
480     pa_assert(u);
481     pa_assert(ptr);
482     pa_assert(type == mir_input || type == mir_output);
483
484     if (type == mir_input) {
485         sinp = (pa_sink_input *)ptr;
486         pl = sinp->proplist;
487         snprintf(name, sizeof(name), "sink-input.%u", sinp->index);
488     }
489     else {
490         sout = (pa_source_output *)ptr;
491         pl = sout->proplist;
492         snprintf(name, sizeof(name), "source-output.%u", sout->index);
493     }
494
495
496     if ((index_str = pa_proplist_gets(pl, PA_PROP_NODE_INDEX))) {
497         index = strtoul(index_str, &e, 10);
498         if (e != index_str && *e == '\0') {
499             if ((node = mir_node_find_by_index(u, index)))
500                 return node;
501
502             pa_log_debug("can't find find node for %s", name);
503         }
504     }
505
506     return NULL;
507 }
508
509 mir_node *pa_utils_get_node_from_data(struct userdata *u,
510                                       mir_direction    type,
511                                       void            *ptr)
512 {
513     pa_sink_input_new_data *sinp;
514     pa_source_output_new_data *sout;
515     pa_proplist  *pl;
516     mir_node     *node;
517     const char   *index_str;
518     uint32_t      index = PA_IDXSET_INVALID;
519     char         *e;
520     char          name[256];
521
522     pa_assert(u);
523     pa_assert(ptr);
524     pa_assert(type == mir_input || type == mir_output);
525
526     if (type == mir_input) {
527         sinp = (pa_sink_input_new_data *)ptr;
528         pl = sinp->proplist;
529         snprintf(name, sizeof(name), "sink-input");
530     }
531     else {
532         sout = (pa_source_output_new_data *)ptr;
533         pl = sout->proplist;
534         snprintf(name, sizeof(name), "source-output");
535     }
536
537
538     if ((index_str = pa_proplist_gets(pl, PA_PROP_NODE_INDEX))) {
539         index = strtoul(index_str, &e, 10);
540         if (e != index_str && *e == '\0') {
541             if ((node = mir_node_find_by_index(u, index)))
542                 return node;
543
544             pa_log_debug("can't find find node for %s", name);
545         }
546     }
547
548     return NULL;
549 }
550
551 static char *stream_name(pa_proplist *pl)
552 {
553     const char  *appnam;
554     const char  *binnam;
555
556     if ((appnam = pa_proplist_gets(pl, PA_PROP_APPLICATION_NAME)))
557         return (char *)appnam;
558
559     if ((binnam = pa_proplist_gets(pl, PA_PROP_APPLICATION_PROCESS_BINARY)))
560         return (char *)binnam;
561
562     return NULL;
563 }
564
565
566 const char *pa_utils_file_path(const char *dir, const char *file,
567                                char *buf, size_t len)
568 {
569     pa_assert(file);
570     pa_assert(buf);
571     pa_assert(len > 0);
572
573     snprintf(buf, len, "%s/%s", dir, file);
574
575     return buf;
576 }
577
578
579 uint32_t pa_utils_new_stamp(void)
580 {
581     return ++stamp;
582 }
583
584 uint32_t pa_utils_get_stamp(void)
585 {
586     return stamp;
587 }
588
589
590
591 /*
592  * Local Variables:
593  * c-basic-offset: 4
594  * indent-tabs-mode: nil
595  * End:
596  *
597  */
598
599