summaryrefslogtreecommitdiff
path: root/main/format.c
blob: b81a1f1d4ca0c2acba1fca105ee6ab0ee711d21d (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2014, Digium, Inc.
 *
 * Joshua Colp <jcolp@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 Media Format API
 *
 * \author Joshua Colp <jcolp@digium.com>
 */

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

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include "asterisk/logger.h"
#include "asterisk/codec.h"
#include "asterisk/format.h"
#include "asterisk/astobj2.h"
#include "asterisk/strings.h"
#include "asterisk/module.h"

/*! \brief Number of buckets to use for format interfaces (should be prime for performance reasons) */
#define FORMAT_INTERFACE_BUCKETS 53

/*! \brief Definition of a media format */
struct ast_format {
	/*! Name of the format */
	const char *name;
	/*! \brief Pointer to the codec in use for this format */
	struct ast_codec *codec;
	/*! \brief Attribute specific data, implementation specific */
	void *attribute_data;
	/*! \brief Pointer to the optional format interface */
	const struct ast_format_interface *interface;
};

/*! \brief Structure used when registering a format interface */
struct format_interface {
	/*! \brief Pointer to the format interface itself */
	const struct ast_format_interface *interface;
	/*! \brief Name of the codec the interface is for */
	char codec[0];
};

/*! \brief Container for registered format interfaces */
static struct ao2_container *interfaces;

AO2_STRING_FIELD_HASH_FN(format_interface, codec)
AO2_STRING_FIELD_CMP_FN(format_interface, codec)

/*! \brief Function called when the process is shutting down */
static void format_shutdown(void)
{
	ao2_cleanup(interfaces);
	interfaces = NULL;
}

int ast_format_init(void)
{
	interfaces = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, FORMAT_INTERFACE_BUCKETS,
		format_interface_hash_fn, format_interface_cmp_fn);
	if (!interfaces) {
		return -1;
	}

	ast_register_cleanup(format_shutdown);

	return 0;
}

int __ast_format_interface_register(const char *codec, const struct ast_format_interface *interface, struct ast_module *mod)
{
	SCOPED_AO2WRLOCK(lock, interfaces);
	struct format_interface *format_interface;

	if (!interface->format_clone || !interface->format_destroy) {
		ast_log(LOG_ERROR, "Format interface for codec '%s' does not implement required callbacks\n", codec);
		return -1;
	}

	format_interface = ao2_find(interfaces, codec, OBJ_SEARCH_KEY | OBJ_NOLOCK);
	if (format_interface) {
		ast_log(LOG_ERROR, "A format interface is already present for codec '%s'\n", codec);
		ao2_ref(format_interface, -1);
		return -1;
	}

	format_interface = ao2_alloc_options(sizeof(*format_interface) + strlen(codec) + 1,
		NULL, AO2_ALLOC_OPT_LOCK_NOLOCK);
	if (!format_interface) {
		return -1;
	}
	format_interface->interface = interface;
	strcpy(format_interface->codec, codec); /* Safe */

	/* Once registered a format interface cannot be unregistered. */
	ast_module_shutdown_ref(mod);
	ao2_link_flags(interfaces, format_interface, OBJ_NOLOCK);
	ao2_ref(format_interface, -1);

	ast_verb(2, "Registered format interface for codec '%s'\n", codec);

	return 0;
}

void *ast_format_get_attribute_data(const struct ast_format *format)
{
	return format->attribute_data;
}

void ast_format_set_attribute_data(struct ast_format *format, void *attribute_data)
{
	format->attribute_data = attribute_data;
}

/*! \brief Destructor for media formats */
static void format_destroy(void *obj)
{
	struct ast_format *format = obj;

	if (format->interface) {
		format->interface->format_destroy(format);
	}

	ao2_cleanup(format->codec);
}

struct ast_format *ast_format_create_named(const char *format_name, struct ast_codec *codec)
{
	struct ast_format *format;
	struct format_interface *format_interface;

	format = ao2_t_alloc_options(sizeof(*format), format_destroy,
		AO2_ALLOC_OPT_LOCK_NOLOCK, S_OR(codec->description, ""));
	if (!format) {
		return NULL;
	}
	format->name = format_name;
	format->codec = ao2_bump(codec);

	format_interface = ao2_find(interfaces, codec->name, OBJ_SEARCH_KEY);
	if (format_interface) {
		format->interface = format_interface->interface;
		ao2_ref(format_interface, -1);
	}

	return format;
}

struct ast_format *ast_format_clone(const struct ast_format *format)
{
	struct ast_format *cloned = ast_format_create_named(format->name, format->codec);

	if (!cloned) {
		return NULL;
	}

	if (cloned->interface && cloned->interface->format_clone(format, cloned)) {
		ao2_ref(cloned, -1);
		return NULL;
	}

	return cloned;
}

struct ast_format *ast_format_create(struct ast_codec *codec)
{
	return ast_format_create_named(codec->name, codec);
}

enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
{
	const struct ast_format_interface *interface;

	if (format1 == NULL || format2 == NULL) {
		return AST_FORMAT_CMP_NOT_EQUAL;
	}

	if (format1 == format2) {
		return AST_FORMAT_CMP_EQUAL;
	}

	if (format1->codec != format2->codec) {
		return AST_FORMAT_CMP_NOT_EQUAL;
	}

	interface = format1->interface ? format1->interface : format2->interface;

	if (interface && interface->format_cmp) {
		return interface->format_cmp(format1, format2);
	}

	return AST_FORMAT_CMP_EQUAL;
}

struct ast_format *ast_format_joint(const struct ast_format *format1, const struct ast_format *format2)
{
	const struct ast_format_interface *interface;

	if (format1->codec != format2->codec) {
		return NULL;
	}

	/* If the two formats are the same structure OR if the codec is the same and no attributes
	 * exist we can immediately return a format with reference count bumped up, since they are
	 * the same.
	 */
	if ((ast_format_cmp(format1, format2) == AST_FORMAT_CMP_EQUAL && !format1->attribute_data && !format2->attribute_data)) {
		return ao2_bump((struct ast_format*)format1);
	}

	interface = format1->interface ? format1->interface : format2->interface;

	/* If there is attribute data on either there has to be an interface */
	return interface->format_get_joint(format1, format2);
}

struct ast_format *ast_format_attribute_set(const struct ast_format *format, const char *name, const char *value)
{
	const struct ast_format_interface *interface = format->interface;

	if (!interface) {
		struct format_interface *format_interface = ao2_find(interfaces, format->codec->name, OBJ_SEARCH_KEY);
		if (format_interface) {
			interface = format_interface->interface;
			ao2_ref(format_interface, -1);
		}
	}

	if (!interface || !interface->format_attribute_set) {
		return ao2_bump((struct ast_format*)format);
	}

	return interface->format_attribute_set(format, name, value);
}

const void *ast_format_attribute_get(const struct ast_format *format, const char *name)
{
	const struct ast_format_interface *interface = format->interface;

	if (!interface) {
		struct format_interface *format_interface = ao2_find(interfaces, format->codec->name, OBJ_SEARCH_KEY);
		if (format_interface) {
			interface = format_interface->interface;
			ao2_ref(format_interface, -1);
		}
	}

	if (!interface || !interface->format_attribute_get) {
		return NULL;
	}

	return interface->format_attribute_get(format, name);
}

struct ast_format *ast_format_parse_sdp_fmtp(const struct ast_format *format, const char *attributes)
{
	const struct ast_format_interface *interface = format->interface;

	if (!interface) {
		struct format_interface *format_interface = ao2_find(interfaces, format->codec->name, OBJ_SEARCH_KEY);
		if (format_interface) {
			interface = format_interface->interface;
			ao2_ref(format_interface, -1);
		}
	}

	if (!interface || !interface->format_parse_sdp_fmtp) {
		return ao2_bump((struct ast_format*)format);
	}

	return interface->format_parse_sdp_fmtp(format, attributes);
}

void ast_format_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str)
{
	const struct ast_format_interface *interface = format->interface;

	if (!interface) {
		struct format_interface *format_interface = ao2_find(interfaces, format->codec->name, OBJ_SEARCH_KEY);
		if (format_interface) {
			interface = format_interface->interface;
			ao2_ref(format_interface, -1);
		}
	}

	if (!interface || !interface->format_generate_sdp_fmtp) {
		return;
	}

	interface->format_generate_sdp_fmtp(format, payload, str);
}

struct ast_codec *ast_format_get_codec(const struct ast_format *format)
{
	return ao2_bump(format->codec);
}

unsigned int ast_format_get_codec_id(const struct ast_format *format)
{
	return format->codec->id;
}

const char *ast_format_get_name(const struct ast_format *format)
{
	return format->name;
}

const char *ast_format_get_codec_name(const struct ast_format *format)
{
	return format->codec->name;
}

int ast_format_can_be_smoothed(const struct ast_format *format)
{
	/* Coalesce to 1 if non-zero */
	return format->codec->smooth ? 1 : 0;
}

int ast_format_get_smoother_flags(const struct ast_format *format)
{
	return AST_SMOOTHER_FLAGS_UNPACK(format->codec->smooth);
}

enum ast_media_type ast_format_get_type(const struct ast_format *format)
{
	return format->codec->type;
}

unsigned int ast_format_get_default_ms(const struct ast_format *format)
{
	return format->codec->default_ms;
}

unsigned int ast_format_get_minimum_ms(const struct ast_format *format)
{
	return format->codec->minimum_ms;
}

unsigned int ast_format_get_maximum_ms(const struct ast_format *format)
{
	return format->codec->maximum_ms;
}

unsigned int ast_format_get_minimum_bytes(const struct ast_format *format)
{
	return format->codec->minimum_bytes;
}

unsigned int ast_format_get_sample_rate(const struct ast_format *format)
{
	return format->codec->sample_rate ?: 8000;
}

unsigned int ast_format_determine_length(const struct ast_format *format, unsigned int samples)
{
	return ast_codec_determine_length(format->codec, samples);
}