Finalize basic configuration logic
[staging/agl-audio-plugin.git] / module.c
1 /*
2  * module-agl-audio -- PulseAudio module for providing audio routing support
3  * (forked from "module-murphy-ivi" - https://github.com/otcshare )
4  * Copyright (c) 2012, Intel Corporation.
5  * Copyright (c) 2016, IoT.bzh
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms and conditions of the GNU Lesser General Public License,
9  * version 2.1, as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St - Fifth Floor, Boston,
19  * MA 02110-1301 USA.
20  *
21  */
22
23 /* Load the module with :
24 $ pulseaudio --system --dl-search-path=/mypath
25 $ pactl load-module mypamodule
26  (PS : /mypath must be world-readable, because PulseAudio drops
27   its own privileges after initial startup)
28 */
29
30  /* THIS IS PROVIDED BY "pulseaudio-module-devel" package */
31
32 #include <pulsecore/pulsecore-config.h> /* required for "module.h" */
33 #include <pulsecore/module.h>
34 #include <pulsecore/modargs.h>          /* for "pa_modargs" */
35
36 #include "userdata.h"                   /* for "struct userdata" */
37 #include "config.h"                     /* for "agl_config_...()" */
38 #include "utils.h"                      /* for "struct agl_null_sink", "agl_utils_create_null_sink()"... */
39 #include "loopback.h"                   /* for "struct agl_loopback/loopnode" */
40 #include "zone.h"                       /* for "struct agl_zoneset" */
41 #include "node.h"                       /* for "struct agl_nodeset" */
42 #include "audiomgr.h"                   /* for "struct agl_audiomgr" */
43 #include "routerif.h"                   /* for "struct agl_routerif" */
44 #include "router.h"                     /* for "struct agl_router" */
45 #include "discover.h"                   /* for "struct agl_discover" */
46 #include "tracker.h"                    /* for "struct agl_tracker" */
47
48 #ifndef DEFAULT_CONFIG_DIR
49 #define DEFAULT_CONFIG_DIR "/etc/pulse"
50 #endif
51
52 #ifndef DEFAULT_CONFIG_FILE
53 #define DEFAULT_CONFIG_FILE "pulseaudio-agl.cfg"
54 #endif
55
56
57  /* VALID ARGUMENTS */
58
59 static const char* const valid_modargs[] = {
60         "config_dir",
61         "config_file",
62         "null_sink_name",
63         "audiomgr_socktype",
64         "audiomgr_address",
65         "audiomgr_port",
66         NULL
67 };
68
69
70  /* INITIALIZATION FUNCTION */
71
72 int pa__init (pa_module *m)
73 {
74         pa_log ("Initializing \"pulseaudio-agl\" module");
75
76         struct userdata *u = NULL;
77         pa_modargs      *ma = NULL;     /* will contain module arguments */
78         const char      *cfgdir;
79         const char      *cfgfile;
80         const char      *nsnam;         /* NULL sink name (default = "null.agl") */
81         const char      *amsocktype;    /* Optional external routing daemon: socket type ("unix"/"tcp") */
82         const char      *amaddr;        /* Optional external routing daemon: socket address (path/ip address) */
83         const char      *amport;        /* Optional external routing daemon: socket port ("tcp" type only) */
84         const char      *cfgpath;
85         char buf[4096];
86
87         pa_assert (m);
88
89          /* treat module arguments */
90
91         ma = pa_modargs_new (m->argument, valid_modargs);
92
93         cfgdir     = pa_modargs_get_value (ma, "config_dir", DEFAULT_CONFIG_DIR);
94         cfgfile    = pa_modargs_get_value (ma, "config_file", DEFAULT_CONFIG_FILE);
95         nsnam      = pa_modargs_get_value (ma, "null_sink_name", NULL);
96         amsocktype = pa_modargs_get_value (ma, "audiomgr_socktype", NULL);
97         amaddr     = pa_modargs_get_value (ma, "audiomgr_address", NULL);
98         amport     = pa_modargs_get_value (ma, "audiomgr_port", NULL);
99
100         pa_log ("cfgdir : %s", cfgdir);
101         pa_log ("cfgfile : %s", cfgfile);
102
103          /* initialize "stamp" incremental integer to 0 */
104
105         agl_utils_init_stamp ();
106
107          /* initialize userdata */
108
109         u = pa_xnew0 (struct userdata, 1);
110         u->core      = m->core;
111         u->module    = m;
112         u->nsnam     = pa_xstrdup (nsnam) ;
113         u->zoneset   = agl_zoneset_init (u);
114         u->nodeset   = agl_nodeset_init (u);
115         u->audiomgr  = agl_audiomgr_init (u);
116         u->routerif  = agl_routerif_init (u, amsocktype, amaddr, amport);
117         u->router    = agl_router_init (u);
118         u->discover  = agl_discover_init (u);
119         u->tracker   = agl_tracker_init (u);
120
121         m->userdata = u;
122
123          /* apply the config file */
124
125         cfgpath = agl_config_file_get_path (cfgdir, cfgfile, buf, sizeof(buf));
126         agl_config_parse_file (u, cfgpath);
127
128          /* really initialize the module's core logic */
129
130         agl_tracker_synchronize (u);
131
132          /* end */
133
134         pa_modargs_free (ma);
135
136         return 0;
137 }
138
139
140  /* CLOSEUP FUNCTION */
141 void pa__done (pa_module *m)
142 {
143         pa_log ("Closing \"pulseaudio-agl\" module");
144
145         struct userdata *u;
146
147         pa_assert (m);
148
149         if (u = m->userdata) {
150                 agl_tracker_done (u);
151                 agl_discover_done (u);
152                 agl_router_done (u);
153                 agl_routerif_done (u);
154                 agl_audiomgr_done (u);
155                 agl_nodeset_done (u);
156                 agl_zoneset_done (u);
157                 pa_xfree (u->nsnam);
158                 pa_xfree (u);
159         }       
160 }