utils: add directory support to pa_utils_file_path()
[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_sink_name(pa_sink *sink)
147 {
148     return (sink && sink->name) ? sink->name : "<unknown>";
149 }
150
151 char *pa_utils_get_source_name(pa_source *source)
152 {
153     return (source && source->name) ? source->name : "<unknown>";
154 }
155
156 char *pa_utils_get_sink_input_name(pa_sink_input *sinp)
157 {
158     char *name;
159
160     if (sinp && (name = stream_name(sinp->proplist)))
161         return name;
162
163     return "<unknown>";
164 }
165
166 char *pa_utils_get_sink_input_name_from_data(pa_sink_input_new_data *data)
167 {
168     char *name;
169
170     if (data && (name = stream_name(data->proplist)))
171         return name;
172
173     return "<unknown>";
174 }
175
176
177 char *pa_utils_get_source_output_name(pa_source_output *sout)
178 {
179     char *name;
180
181     if (sout && (name = stream_name(sout->proplist)))
182         return name;
183
184     return "<unknown>";
185 }
186
187 char *pa_utils_get_source_output_name_from_data(pa_source_output_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 void pa_utils_set_stream_routing_properties(pa_proplist *pl,
199                                             int          styp,
200                                             void        *target)
201 {
202     const char    *clnam;
203     const char    *method;
204     char           clid[32];
205
206     pa_assert(pl);
207     pa_assert(styp >= 0);
208
209     snprintf(clid, sizeof(clid), "%d", styp);
210     clnam  = mir_node_type_str(styp);
211     method = target ? PA_ROUTING_EXPLICIT : PA_ROUTING_DEFAULT;
212
213     if (pa_proplist_sets(pl, PA_PROP_ROUTING_CLASS_NAME, clnam ) < 0 ||
214         pa_proplist_sets(pl, PA_PROP_ROUTING_CLASS_ID  , clid  ) < 0 ||
215         pa_proplist_sets(pl, PA_PROP_ROUTING_METHOD    , method) < 0  )
216     {
217         pa_log("failed to set some stream property");
218     }
219 }
220
221 void pa_utils_set_stream_routing_method_property(pa_proplist *pl,
222                                                  pa_bool_t explicit)
223 {
224     const char *method = explicit ? PA_ROUTING_EXPLICIT : PA_ROUTING_DEFAULT;
225
226     pa_assert(pl);
227
228     if (pa_proplist_sets(pl, PA_PROP_ROUTING_METHOD, method) < 0) {
229         pa_log("failed to set routing method property on sink-input");
230     }
231 }
232
233 pa_bool_t pa_utils_stream_has_default_route(pa_proplist *pl)
234 {
235     const char *method;
236
237     pa_assert(pl);
238
239     method = pa_proplist_gets(pl, PA_PROP_ROUTING_METHOD);
240
241     if (method && pa_streq(method, PA_ROUTING_DEFAULT))
242         return TRUE;
243
244     return FALSE;
245 }
246
247 int pa_utils_get_stream_class(pa_proplist *pl)
248 {
249     const char *clid_str;
250     char *e;
251     unsigned long int clid = 0;
252
253     pa_assert(pl);
254
255     if ((clid_str = pa_proplist_gets(pl, PA_PROP_ROUTING_CLASS_ID))) {
256         clid = strtoul(clid_str, &e, 10);
257
258         if (*e)
259             clid = 0;
260     }
261
262     return (int)clid;
263 }
264
265 void pa_utils_set_port_properties(pa_device_port *port, mir_node *node)
266 {
267     char nodeidx[256];
268
269     pa_assert(port);
270     pa_assert(port->proplist);
271     pa_assert(node);
272
273     snprintf(nodeidx, sizeof(nodeidx), "%u", node->index);
274
275     pa_proplist_sets(port->proplist, PA_PROP_NODE_INDEX, nodeidx);
276 }
277
278 mir_node *pa_utils_get_node_from_port(struct userdata *u,
279                                       pa_device_port *port)
280 {
281     const char *value;
282     char *e;
283     uint32_t index = PA_IDXSET_INVALID;
284     mir_node *node = NULL;
285
286     pa_assert(u);
287     pa_assert(port);
288     pa_assert(port->proplist);
289
290     if ((value = pa_proplist_gets(port->proplist, PA_PROP_NODE_INDEX))) {
291         index = strtoul(value, &e, 10);
292
293         if (value[0] && !e[0])
294             node = mir_node_find_by_index(u, index);
295     }
296
297     return node;
298 }
299
300 mir_node *pa_utils_get_node_from_stream(struct userdata *u,
301                                         mir_direction    type,
302                                         void            *ptr)
303 {
304     pa_sink_input    *sinp;
305     pa_source_output *sout;
306     pa_proplist      *pl;
307     mir_node         *node;
308     const char       *index_str;
309     uint32_t          index = PA_IDXSET_INVALID;
310     char             *e;
311     char              name[256];
312
313     pa_assert(u);
314     pa_assert(ptr);
315     pa_assert(type == mir_input || type == mir_output);
316
317     if (type == mir_input) {
318         sinp = (pa_sink_input *)ptr;
319         pl = sinp->proplist;
320         snprintf(name, sizeof(name), "sink-input.%u", sinp->index);
321     }
322     else {
323         sout = (pa_source_output *)ptr;
324         pl = sout->proplist;
325         snprintf(name, sizeof(name), "source-output.%u", sout->index);
326     }
327
328
329     if ((index_str = pa_proplist_gets(pl, PA_PROP_NODE_INDEX))) {
330         index = strtoul(index_str, &e, 10);
331         if (e != index_str && *e == '\0') {
332             if ((node = mir_node_find_by_index(u, index)))
333                 return node;
334
335             pa_log_debug("can't find find node for %s", name);
336         }
337     }
338
339     return NULL;
340 }
341
342 mir_node *pa_utils_get_node_from_data(struct userdata *u,
343                                       mir_direction    type,
344                                       void            *ptr)
345 {
346     pa_sink_input_new_data *sinp;
347     pa_source_output_new_data *sout;
348     pa_proplist  *pl;
349     mir_node     *node;
350     const char   *index_str;
351     uint32_t      index = PA_IDXSET_INVALID;
352     char         *e;
353     char          name[256];
354
355     pa_assert(u);
356     pa_assert(ptr);
357     pa_assert(type == mir_input || type == mir_output);
358
359     if (type == mir_input) {
360         sinp = (pa_sink_input_new_data *)ptr;
361         pl = sinp->proplist;
362         snprintf(name, sizeof(name), "sink-input");
363     }
364     else {
365         sout = (pa_source_output_new_data *)ptr;
366         pl = sout->proplist;
367         snprintf(name, sizeof(name), "source-output");
368     }
369
370
371     if ((index_str = pa_proplist_gets(pl, PA_PROP_NODE_INDEX))) {
372         index = strtoul(index_str, &e, 10);
373         if (e != index_str && *e == '\0') {
374             if ((node = mir_node_find_by_index(u, index)))
375                 return node;
376
377             pa_log_debug("can't find find node for %s", name);
378         }
379     }
380
381     return NULL;
382 }
383
384 static char *stream_name(pa_proplist *pl)
385 {
386     const char  *appnam;
387     const char  *binnam;
388
389     if ((appnam = pa_proplist_gets(pl, PA_PROP_APPLICATION_NAME)))
390         return (char *)appnam;
391
392     if ((binnam = pa_proplist_gets(pl, PA_PROP_APPLICATION_PROCESS_BINARY)))
393         return (char *)binnam;
394
395     return NULL;
396 }
397
398
399 const char *pa_utils_file_path(const char *dir, const char *file,
400                                char *buf, size_t len)
401 {
402     pa_assert(file);
403     pa_assert(buf);
404     pa_assert(len > 0);
405
406     snprintf(buf, len, "%s/%s", dir, file);
407
408     return buf;
409 }
410
411
412 uint32_t pa_utils_new_stamp(void)
413 {
414     return ++stamp;
415 }
416
417 uint32_t pa_utils_get_stamp(void)
418 {
419     return stamp;
420 }
421
422
423
424
425 /*
426  * Local Variables:
427  * c-basic-offset: 4
428  * indent-tabs-mode: nil
429  * End:
430  *
431  */
432
433