Merge branch 'genivi-mir' of github.com:otcshare/policy-misc into genivi-mir
[profile/ivi/pulseaudio-module-murphy-ivi.git] / murphy / utils.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/stat.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <limits.h>
10
11 #include <sys/types.h>
12 #include <sys/socket.h>
13
14 #include <pulsecore/pulsecore-config.h>
15
16 #include <pulsecore/core-util.h>
17 #include <pulsecore/card.h>
18 #include <pulsecore/sink.h>
19 #include <pulsecore/source.h>
20 #include <pulsecore/sink-input.h>
21
22 #include "userdata.h"
23 #include "utils.h"
24 #include "node.h"
25
26 #ifndef DEFAULT_CONFIG_DIR
27 #define DEFAULT_CONFIG_DIR "/etc/pulse"
28 #endif
29
30 #define DEFAULT_NULL_SINK_NAME "null.mir"
31
32 struct pa_null_sink {
33     char      *name;
34     uint32_t   module_index;
35     uint32_t   sink_index;
36 };
37
38
39 static uint32_t stamp;
40
41 static char *sink_input_name(pa_proplist *);
42
43
44 pa_null_sink *pa_utils_create_null_sink(struct userdata *u, const char *name)
45 {
46     pa_core      *core;
47     pa_module    *module;
48     pa_null_sink *ns;
49     pa_sink      *sink;
50     pa_sink      *s;
51     uint32_t      idx;
52     char          args[256];
53
54     pa_assert(u);
55     pa_assert_se((core = u->core));
56
57
58     if (!name)
59         name = DEFAULT_NULL_SINK_NAME;
60
61
62     snprintf(args, sizeof(args), "sink_name=\"%s\" channels=2", name);
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     else {
69         PA_IDXSET_FOREACH(s, core->sinks, idx) {
70             if (s->module && s->module == module) {
71                 sink = s;
72                 pa_log_info("mir null sink is '%s'", name);
73                 break;
74             }
75         }
76     }
77
78     ns = pa_xnew0(pa_null_sink, 1);
79     ns->name = pa_xstrdup(name);
80     ns->module_index = module ? module->index : PA_IDXSET_INVALID;
81     ns->sink_index = sink ? sink->index : PA_IDXSET_INVALID;
82
83     return ns;
84 }
85
86 void pa_utils_destroy_null_sink(struct userdata *u)
87 {
88     pa_core      *core;
89     pa_module    *module;
90     pa_null_sink *ns;
91
92     if (u && (ns = u->nullsink) && (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)
104 {
105     pa_core *core;
106     pa_null_sink *ns;
107     
108     pa_assert(u);
109     pa_assert_se((core = u->core));
110     pa_assert_se((ns = u->nullsink));
111
112     return pa_idxset_get_by_index(core->sinks, ns->sink_index);
113 }
114
115
116
117 char *pa_utils_get_card_name(pa_card *card)
118 {
119     return (card && card->name) ? card->name : "<unknown>";
120 }
121
122 char *pa_utils_get_sink_name(pa_sink *sink)
123 {
124     return (sink && sink->name) ? sink->name : "<unknown>";
125 }
126
127 char *pa_utils_get_source_name(pa_source *source)
128 {
129     return (source && source->name) ? source->name : "<unknown>";
130 }
131
132 char *pa_utils_get_sink_input_name(pa_sink_input *sinp)
133 {
134     char *name;
135
136     if (sinp && (name = sink_input_name(sinp->proplist)))
137         return name;
138     
139     return "<unknown>";
140 }
141
142 char *pa_utils_get_sink_input_name_from_data(pa_sink_input_new_data *data)
143 {
144     char *name;
145
146     if (data && (name = sink_input_name(data->proplist)))
147         return name;
148     
149     return "<unknown>";
150 }
151
152
153 void pa_utils_set_stream_routing_properties(pa_proplist *pl,
154                                             int          styp,
155                                             pa_sink     *sink)
156 {
157     const char    *clnam;
158     const char    *method;
159     char           clid[32];
160
161     pa_assert(pl);
162     pa_assert(styp);
163     
164     snprintf(clid, sizeof(clid), "%d", styp);
165     clnam  = mir_node_type_str(styp);
166     method = sink ? PA_ROUTING_EXPLICIT : PA_ROUTING_DEFAULT;
167
168     if (pa_proplist_sets(pl, PA_PROP_ROUTING_CLASS_NAME, clnam ) < 0 ||
169         pa_proplist_sets(pl, PA_PROP_ROUTING_CLASS_ID  , clid  ) < 0 ||
170         pa_proplist_sets(pl, PA_PROP_ROUTING_METHOD    , method) < 0  )
171     {
172         pa_log("failed to set some property on sink-input");
173     }
174 }
175
176 void pa_utils_set_stream_routing_method_property(pa_proplist *pl,
177                                                  pa_bool_t explicit)
178 {
179     const char *method = explicit ? PA_ROUTING_EXPLICIT : PA_ROUTING_DEFAULT;
180
181     pa_assert(pl);
182     
183     if (pa_proplist_sets(pl, PA_PROP_ROUTING_METHOD, method) < 0) {
184         pa_log("failed to set routing method property on sink-input");
185     }
186 }
187
188 pa_bool_t pa_utils_stream_has_default_route(pa_proplist *pl)
189 {
190     const char *method;
191
192     pa_assert(pl);
193
194     method = pa_proplist_gets(pl, PA_PROP_ROUTING_METHOD);
195
196     if (method && pa_streq(method, PA_ROUTING_DEFAULT))
197         return TRUE;
198
199     return FALSE;
200 }
201
202
203 static char *sink_input_name(pa_proplist *pl)
204 {
205     const char  *appnam;
206     const char  *binnam;
207
208     if ((appnam = pa_proplist_gets(pl, PA_PROP_APPLICATION_NAME)))
209         return (char *)appnam;
210
211     if ((binnam = pa_proplist_gets(pl, PA_PROP_APPLICATION_PROCESS_BINARY)))
212         return (char *)binnam;
213
214     return NULL;
215 }
216
217
218 const char *pa_utils_file_path(const char *file, char *buf, size_t len)
219 {
220     pa_assert(file);
221     pa_assert(buf);
222     pa_assert(len > 0);
223
224     snprintf(buf, len, "%s/%s", DEFAULT_CONFIG_DIR, file);
225
226     return buf;
227 }
228
229
230 uint32_t pa_utils_new_stamp(void)
231 {
232     return ++stamp;
233 }
234
235 uint32_t pa_utils_get_stamp(void)
236 {
237     return stamp;
238 }
239
240
241
242
243 /*
244  * Local Variables:
245  * c-basic-offset: 4
246  * indent-tabs-mode: nil
247  * End:
248  *
249  */
250
251