d047b26c29c0636c60ad0a3bf18f5b3dd03b32c8
[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 "pa_config_...()" */
38 #include "utils.h"                      /* for "struct pa_null_sink", "pa_utils_create_null_sink()"... */
39 #include "loopback.h"                   /* for "struct pa_loopback/loopnode" */
40 #include "zone.h"                       /* for "struct pa_zoneset" */
41 #include "node.h"                       /* for "struct pa_nodeset" */
42 #include "audiomgr.h"                   /* for "struct pa_audiomgr" */
43 #include "routerif.h"                   /* for "struct pa_routerif" */
44 #include "discover.h"                   /* for "struct pa_discover" */
45 #include "tracker.h"                    /* for "struct pa_tracker" */
46 #include "router.h"                     /* for "struct pa_router" */
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         pa_utils_init_stamp ();
104
105          /* initialize userdata */
106
107         u = pa_xnew0 (struct userdata, 1);
108         u->core      = m->core;
109         u->module    = m;
110         u->nsnam     = pa_xstrdup (nsnam) ;
111         u->zoneset   = pa_zoneset_init (u);
112         u->nodeset   = pa_nodeset_init (u);
113         u->audiomgr  = pa_audiomgr_init (u);
114         u->routerif  = pa_routerif_init (u, amsocktype, amaddr, amport);
115         u->router    = pa_router_init (u);
116         u->discover  = pa_discover_init (u);
117         u->tracker   = pa_tracker_init (u);
118
119         m->userdata = u;
120
121          /* apply the config file */
122
123         cfgpath = pa_config_file_get_path (cfgdir, cfgfile, buf, sizeof(buf));
124         pa_config_parse_file (u, cfgpath);
125
126          /* really initialize the module's core logic */
127
128         pa_tracker_synchronize (u);
129
130          /* end */
131
132         pa_modargs_free(ma);
133
134         return 0;
135 }
136
137
138  /* CLOSEUP FUNCTION */
139 void pa__done (pa_module *m)
140 {
141         pa_log ("Closing \"pulseaudio-agl\" module");
142
143         struct userdata *u;
144
145         pa_assert (m);
146
147         if (u = m->userdata) {
148                 pa_tracker_done (u);
149                 pa_discover_done (u);
150                 pa_routerif_done (u);
151                 pa_audiomgr_done (u);
152                 pa_nodeset_done (u);
153                 pa_zoneset_done (u);
154                 pa_xfree (u->nsnam);
155                 pa_xfree (u);
156         }       
157 }