summaryrefslogtreecommitdiff
path: root/main/stasis_bridging.c
blob: 49e1fcfdf95f8bc8e10bceae17eb5a3e9708c8b2 (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2013, Digium, Inc.
 *
 * Kinsey Moore <kmoore@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Stasis Messages and Data Types for Bridge Objects
 *
 * \author Kinsey Moore <kmoore@digium.com>
 */

/*** MODULEINFO
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include "asterisk/astobj2.h"
#include "asterisk/stasis.h"
#include "asterisk/channel.h"
#include "asterisk/stasis_bridging.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/bridging.h"
#include "asterisk/bridging_technology.h"

#define SNAPSHOT_CHANNELS_BUCKETS 13

/*!
 * @{ \brief Define bridge message types.
 */
STASIS_MESSAGE_TYPE_DEFN(ast_bridge_snapshot_type);
STASIS_MESSAGE_TYPE_DEFN(ast_bridge_merge_message_type);
STASIS_MESSAGE_TYPE_DEFN(ast_channel_entered_bridge_type);
STASIS_MESSAGE_TYPE_DEFN(ast_channel_left_bridge_type);
/*! @} */

/*! \brief Aggregate topic for bridge messages */
static struct stasis_topic *bridge_topic_all;

/*! \brief Caching aggregate topic for bridge snapshots */
static struct stasis_caching_topic *bridge_topic_all_cached;

/*! \brief Topic pool for individual bridge topics */
static struct stasis_topic_pool *bridge_topic_pool;

/*! \brief Destructor for bridge snapshots */
static void bridge_snapshot_dtor(void *obj)
{
	struct ast_bridge_snapshot *snapshot = obj;
	ast_string_field_free_memory(snapshot);
	ao2_cleanup(snapshot->channels);
	snapshot->channels = NULL;
}

struct ast_bridge_snapshot *ast_bridge_snapshot_create(struct ast_bridge *bridge)
{
	RAII_VAR(struct ast_bridge_snapshot *, snapshot, NULL, ao2_cleanup);
	struct ast_bridge_channel *bridge_channel;

	snapshot = ao2_alloc(sizeof(*snapshot), bridge_snapshot_dtor);
	if (!snapshot || ast_string_field_init(snapshot, 128)) {
		return NULL;
	}

	snapshot->channels = ast_str_container_alloc(SNAPSHOT_CHANNELS_BUCKETS);
	if (!snapshot->channels) {
		return NULL;
	}

	AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
		if (ast_str_container_add(snapshot->channels,
				ast_channel_uniqueid(bridge_channel->chan))) {
			return NULL;
		}
	}

	ast_string_field_set(snapshot, uniqueid, bridge->uniqueid);
	ast_string_field_set(snapshot, technology, bridge->technology->name);

	snapshot->feature_flags = bridge->feature_flags;
	snapshot->num_channels = bridge->num_channels;
	snapshot->num_active = bridge->num_active;

	ao2_ref(snapshot, +1);
	return snapshot;
}

struct stasis_topic *ast_bridge_topic(struct ast_bridge *bridge)
{
	struct stasis_topic *bridge_topic = stasis_topic_pool_get_topic(bridge_topic_pool, bridge->uniqueid);
	if (!bridge_topic) {
		return ast_bridge_topic_all();
	}
	return bridge_topic;
}

struct stasis_topic *ast_bridge_topic_all(void)
{
	return bridge_topic_all;
}

struct stasis_caching_topic *ast_bridge_topic_all_cached(void)
{
	return bridge_topic_all_cached;
}

void ast_bridge_publish_state(struct ast_bridge *bridge)
{
	RAII_VAR(struct ast_bridge_snapshot *, snapshot, NULL, ao2_cleanup);
	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);

	ast_assert(bridge != NULL);

	snapshot = ast_bridge_snapshot_create(bridge);
	if (!snapshot) {
		return;
	}

	msg = stasis_message_create(ast_bridge_snapshot_type(), snapshot);
	if (!msg) {
		return;
	}

	stasis_publish(ast_bridge_topic(bridge), msg);
}

static void bridge_publish_state_from_blob(struct ast_bridge_blob *obj)
{
	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);

	ast_assert(obj != NULL);

	msg = stasis_message_create(ast_bridge_snapshot_type(), obj->bridge);
	if (!msg) {
		return;
	}

	stasis_publish(stasis_topic_pool_get_topic(bridge_topic_pool, obj->bridge->uniqueid), msg);
}

/*! \brief Destructor for bridge merge messages */
static void bridge_merge_message_dtor(void *obj)
{
	struct ast_bridge_merge_message *msg = obj;

	ao2_cleanup(msg->to);
	msg->to = NULL;
	ao2_cleanup(msg->from);
	msg->from = NULL;
}

/*! \brief Bridge merge message creation helper */
static struct ast_bridge_merge_message *bridge_merge_message_create(struct ast_bridge *to, struct ast_bridge *from)
{
	RAII_VAR(struct ast_bridge_merge_message *, msg, NULL, ao2_cleanup);

	msg = ao2_alloc(sizeof(*msg), bridge_merge_message_dtor);
	if (!msg) {
		return NULL;
	}

	msg->to = ast_bridge_snapshot_create(to);
	if (!msg->to) {
		return NULL;
	}

	msg->from = ast_bridge_snapshot_create(from);
	if (!msg->from) {
		return NULL;
	}

	ao2_ref(msg, +1);
	return msg;
}

void ast_bridge_publish_merge(struct ast_bridge *to, struct ast_bridge *from)
{
	RAII_VAR(struct ast_bridge_merge_message *, merge_msg, NULL, ao2_cleanup);
	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);

	ast_assert(to != NULL);
	ast_assert(from != NULL);

	merge_msg = bridge_merge_message_create(to, from);
	if (!merge_msg) {
		return;
	}

	msg = stasis_message_create(ast_bridge_merge_message_type(), merge_msg);
	if (!msg) {
		return;
	}

	stasis_publish(ast_bridge_topic_all(), msg);
}

static void bridge_blob_dtor(void *obj)
{
	struct ast_bridge_blob *event = obj;
	ao2_cleanup(event->bridge);
	event->bridge = NULL;
	ao2_cleanup(event->channel);
	event->channel = NULL;
	ast_json_unref(event->blob);
	event->blob = NULL;
}

struct stasis_message *ast_bridge_blob_create(
	struct stasis_message_type *message_type,
	struct ast_bridge *bridge,
	struct ast_channel *chan,
	struct ast_json *blob)
{
	RAII_VAR(struct ast_bridge_blob *, obj, NULL, ao2_cleanup);
	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);

	obj = ao2_alloc(sizeof(*obj), bridge_blob_dtor);
	if (!obj) {
		return NULL;
	}

	if (bridge) {
		obj->bridge = ast_bridge_snapshot_create(bridge);
		if (obj->bridge == NULL) {
			return NULL;
		}
	}

	if (chan) {
		obj->channel = ast_channel_snapshot_create(chan);
		if (obj->channel == NULL) {
			return NULL;
		}
	}

	if (blob) {
		obj->blob = ast_json_ref(blob);
	}

	msg = stasis_message_create(message_type, obj);
	if (!msg) {
		return NULL;
	}

	ao2_ref(msg, +1);
	return msg;
}

const char *ast_bridge_blob_json_type(struct ast_bridge_blob *obj)
{
	if (obj == NULL) {
		return NULL;
	}

	return ast_json_string_get(ast_json_object_get(obj->blob, "type"));
}

void ast_bridge_publish_enter(struct ast_bridge *bridge, struct ast_channel *chan)
{
	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);

	msg = ast_bridge_blob_create(ast_channel_entered_bridge_type(), bridge, chan, NULL);
	if (!msg) {
		return;
	}

	/* enter blob first, then state */
	stasis_publish(ast_bridge_topic(bridge), msg);
	bridge_publish_state_from_blob(stasis_message_data(msg));
}

void ast_bridge_publish_leave(struct ast_bridge *bridge, struct ast_channel *chan)
{
	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);

	msg = ast_bridge_blob_create(ast_channel_left_bridge_type(), bridge, chan, NULL);
	if (!msg) {
		return;
	}

	/* state first, then leave blob (opposite of enter, preserves nesting of events) */
	bridge_publish_state_from_blob(stasis_message_data(msg));
	stasis_publish(ast_bridge_topic(bridge), msg);
}

struct ast_json *ast_bridge_snapshot_to_json(const struct ast_bridge_snapshot *snapshot)
{
	RAII_VAR(struct ast_json *, json_chan, NULL, ast_json_unref);
	int r = 0;

	if (snapshot == NULL) {
		return NULL;
	}

	json_chan = ast_json_object_create();
	if (!json_chan) { ast_log(LOG_ERROR, "Error creating channel json object\n"); return NULL; }

	r = ast_json_object_set(json_chan, "bridge-uniqueid", ast_json_string_create(snapshot->uniqueid));
	if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
	r = ast_json_object_set(json_chan, "bridge-technology", ast_json_string_create(snapshot->technology));
	if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }

	return ast_json_ref(json_chan);
}

void ast_stasis_bridging_shutdown(void)
{
	ao2_cleanup(bridge_topic_all);
	bridge_topic_all = NULL;
	bridge_topic_all_cached = stasis_caching_unsubscribe_and_join(
		bridge_topic_all_cached);
	ao2_cleanup(bridge_topic_pool);
	bridge_topic_pool = NULL;

	STASIS_MESSAGE_TYPE_CLEANUP(ast_bridge_snapshot_type);
	STASIS_MESSAGE_TYPE_CLEANUP(ast_bridge_merge_message_type);
	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_entered_bridge_type);
	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_left_bridge_type);
}

/*! \brief snapshot ID getter for caching topic */
static const char *bridge_snapshot_get_id(struct stasis_message *msg)
{
	struct ast_bridge_snapshot *snapshot;
	if (stasis_message_type(msg) != ast_bridge_snapshot_type()) {
		return NULL;
	}
	snapshot = stasis_message_data(msg);
	return snapshot->uniqueid;
}

int ast_stasis_bridging_init(void)
{
	STASIS_MESSAGE_TYPE_INIT(ast_bridge_snapshot_type);
	STASIS_MESSAGE_TYPE_INIT(ast_bridge_merge_message_type);
	STASIS_MESSAGE_TYPE_INIT(ast_channel_entered_bridge_type);
	STASIS_MESSAGE_TYPE_INIT(ast_channel_left_bridge_type);
	bridge_topic_all = stasis_topic_create("ast_bridge_topic_all");
	bridge_topic_all_cached = stasis_caching_topic_create(bridge_topic_all, bridge_snapshot_get_id);
	bridge_topic_pool = stasis_topic_pool_create(bridge_topic_all);
	return !bridge_topic_all
		|| !bridge_topic_all_cached
		|| !bridge_topic_pool ? -1 : 0;
}