summaryrefslogtreecommitdiff
path: root/src/baresip.c
blob: 4ea493f2c466826ffb70f146f39ae167e45d6006 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
 * @file baresip.c Top-level baresip struct
 *
 * Copyright (C) 2010 - 2016 Creytiv.com
 */
#include <re.h>
#include <baresip.h>
#include "core.h"


/*
 * Top-level struct that holds all other subsystems
 * (move this instance to main.c later)
 */
static struct baresip {
	struct network *net;
	struct contacts contacts;
	struct commands *commands;
	struct player *player;
	struct message *message;
	struct list mnatl;
	struct list mencl;
	struct list aucodecl;
	struct list ausrcl;
	struct list auplayl;
	struct list aufiltl;
	struct list vidcodecl;
	struct list vidsrcl;
	struct list vidispl;
	struct list vidfiltl;
} baresip;


static int cmd_quit(struct re_printf *pf, void *unused)
{
	int err;

	(void)unused;

	err = re_hprintf(pf, "Quit\n");

	ua_stop_all(false);

	return err;
}


static int insmod_handler(struct re_printf *pf, void *arg)
{
       const struct cmd_arg *carg = arg;
       int err;

       err = module_load(carg->prm);
       if (err) {
               return re_hprintf(pf, "insmod: ERROR: could not load module"
                                 " '%s': %m\n", carg->prm, err);
       }

       return re_hprintf(pf, "loaded module %s\n", carg->prm);
}


static int rmmod_handler(struct re_printf *pf, void *arg)
{
       const struct cmd_arg *carg = arg;
       (void)pf;

       module_unload(carg->prm);

       return 0;
}


static const struct cmd corecmdv[] = {
	{"quit", 'q', 0, "Quit",                     cmd_quit             },
	{"insmod", 0, CMD_PRM, "Load module",        insmod_handler       },
	{"rmmod",  0, CMD_PRM, "Unload module",      rmmod_handler        },
};


int baresip_init(struct config *cfg, bool prefer_ipv6)
{
	int err;

	if (!cfg)
		return EINVAL;

	baresip.net = mem_deref(baresip.net);

	list_init(&baresip.mnatl);
	list_init(&baresip.mencl);
	list_init(&baresip.aucodecl);
	list_init(&baresip.ausrcl);
	list_init(&baresip.auplayl);
	list_init(&baresip.vidcodecl);
	list_init(&baresip.vidsrcl);
	list_init(&baresip.vidispl);
	list_init(&baresip.vidfiltl);

	/* Initialise Network */
	err = net_alloc(&baresip.net, &cfg->net,
			prefer_ipv6 ? AF_INET6 : AF_INET);
	if (err) {
		warning("ua: network init failed: %m\n", err);
		return err;
	}

	err = contact_init(&baresip.contacts);
	if (err)
		return err;

	err = cmd_init(&baresip.commands);
	if (err)
		return err;

	err = play_init(&baresip.player);
	if (err)
		return err;

	err = message_init(&baresip.message);
	if (err) {
		warning("baresip: message init failed: %m\n", err);
		return err;
	}

	err = cmd_register(baresip.commands, corecmdv, ARRAY_SIZE(corecmdv));
	if (err)
		return err;

	return 0;
}


void baresip_close(void)
{
	cmd_unregister(baresip.commands, corecmdv);

	baresip.message = mem_deref(baresip.message);
	baresip.player = mem_deref(baresip.player);
	baresip.commands = mem_deref(baresip.commands);
	contact_close(&baresip.contacts);

	baresip.net = mem_deref(baresip.net);
}


struct network *baresip_network(void)
{
	return baresip.net;
}


struct contacts *baresip_contacts(void)
{
	return &baresip.contacts;
}


struct commands *baresip_commands(void)
{
	return baresip.commands;
}


struct player *baresip_player(void)
{
	return baresip.player;
}


struct list *baresip_mnatl(void)
{
	return &baresip.mnatl;
}


struct list *baresip_mencl(void)
{
	return &baresip.mencl;
}


struct message *baresip_message(void)
{
	return baresip.message;
}


/**
 * Get the list of Audio Codecs
 *
 * @return List of audio-codecs
 */
struct list *baresip_aucodecl(void)
{
	return &baresip.aucodecl;
}


struct list *baresip_ausrcl(void)
{
	return &baresip.ausrcl;
}


struct list *baresip_auplayl(void)
{
	return &baresip.auplayl;
}


struct list *baresip_aufiltl(void)
{
	return &baresip.aufiltl;
}


struct list *baresip_vidcodecl(void)
{
	return &baresip.vidcodecl;
}


struct list *baresip_vidsrcl(void)
{
	return &baresip.vidsrcl;
}


struct list *baresip_vidispl(void)
{
	return &baresip.vidispl;
}


struct list *baresip_vidfiltl(void)
{
	return &baresip.vidfiltl;
}