File: format_cache.c

package info (click to toggle)
asterisk 1:13.14.1~dfsg-2+deb9u4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 50,572 kB
  • sloc: ansic: 675,986; sh: 11,363; python: 3,925; sql: 3,634; makefile: 3,153; perl: 3,103; yacc: 2,164; cpp: 2,093; xml: 786; tcl: 113; php: 62
file content (552 lines) | stat: -rw-r--r-- 14,166 bytes parent folder | download
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
 * 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 Cache 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/format.h"
#include "asterisk/format_cache.h"
#include "asterisk/astobj2.h"
#include "asterisk/strings.h"

/*!
 * \brief Built-in cached signed linear 8kHz format.
 */
struct ast_format *ast_format_slin;

/*!
 * \brief Built-in cached signed linear 12kHz format.
 */
struct ast_format *ast_format_slin12;

/*!
 * \brief Built-in cached signed linear 16kHz format.
 */
struct ast_format *ast_format_slin16;

/*!
 * \brief Built-in cached signed linear 24kHz format.
 */
struct ast_format *ast_format_slin24;

/*!
 * \brief Built-in cached signed linear 32kHz format.
 */
struct ast_format *ast_format_slin32;

/*!
 * \brief Built-in cached signed linear 44kHz format.
 */
struct ast_format *ast_format_slin44;

/*!
 * \brief Built-in cached signed linear 48kHz format.
 */
struct ast_format *ast_format_slin48;

/*!
 * \brief Built-in cached signed linear 96kHz format.
 */
struct ast_format *ast_format_slin96;

/*!
 * \brief Built-in cached signed linear 192kHz format.
 */
struct ast_format *ast_format_slin192;

/*!
 * \brief Built-in cached ulaw format.
 */
struct ast_format *ast_format_ulaw;

/*!
 * \brief Built-in cached alaw format.
 */
struct ast_format *ast_format_alaw;

/*!
 * \brief Built-in cached testlaw format.
 */
struct ast_format *ast_format_testlaw;

/*!
 * \brief Built-in cached gsm format.
 */
struct ast_format *ast_format_gsm;

/*!
 * \brief Built-in cached adpcm format.
 */
struct ast_format *ast_format_adpcm;

/*!
 * \brief Built-in cached g722 format.
 */
struct ast_format *ast_format_g722;

/*!
 * \brief Built-in cached g726 format.
 */
struct ast_format *ast_format_g726;

/*!
 * \brief Built-in cached g726-aal2 format.
 */
struct ast_format *ast_format_g726_aal2;

/*!
 * \brief Built-in cached ilbc format.
 */
struct ast_format *ast_format_ilbc;

/*!
 * \brief Built-in cached ilbc format.
 */
struct ast_format *ast_format_lpc10;

/*!
 * \brief Built-in cached speex format.
 */
struct ast_format *ast_format_speex;

/*!
 * \brief Built-in cached speex at 16kHz format.
 */
struct ast_format *ast_format_speex16;

/*!
 * \brief Built-in cached speex at 32kHz format.
 */
struct ast_format *ast_format_speex32;

/*!
 * \brief Built-in cached g723.1 format.
 */
struct ast_format *ast_format_g723;

/*!
 * \brief Built-in cached g729 format.
 */
struct ast_format *ast_format_g729;

/*!
 * \brief Built-in cached g719 format.
 */
struct ast_format *ast_format_g719;

/*!
 * \brief Built-in cached h261 format.
 */
struct ast_format *ast_format_h261;

/*!
 * \brief Built-in cached h263 format.
 */
struct ast_format *ast_format_h263;

/*!
 * \brief Built-in cached h263 plus format.
 */
struct ast_format *ast_format_h263p;

/*!
 * \brief Built-in cached h264 format.
 */
struct ast_format *ast_format_h264;

/*!
 * \brief Built-in cached mp4 format.
 */
struct ast_format *ast_format_mp4;

/*!
 * \brief Built-in cached vp8 format.
 */
struct ast_format *ast_format_vp8;

/*!
 * \brief Built-in cached jpeg format.
 */
struct ast_format *ast_format_jpeg;

/*!
 * \brief Built-in cached png format.
 */
struct ast_format *ast_format_png;

/*!
 * \brief Built-in cached siren14 format.
 */
struct ast_format *ast_format_siren14;

/*!
 * \brief Built-in cached siren7 format.
 */
struct ast_format *ast_format_siren7;

/*!
 * \brief Built-in cached opus format.
 */
struct ast_format *ast_format_opus;

/*!
 * \brief Built-in cached AMR format.
 */
struct ast_format *ast_format_amr;

/*!
 * \brief Built-in cached AMR-WB format.
 */
struct ast_format *ast_format_amrwb;

/*!
 * \brief Built-in cached t140 format.
 */
struct ast_format *ast_format_t140;

/*!
 * \brief Built-in cached t140 red format.
 */
struct ast_format *ast_format_t140_red;

/*!
 * \brief Built-in "null" format.
 */
struct ast_format *ast_format_none;

/*!
 * \brief Built-in "silk" format
 */
struct ast_format *ast_format_silk8;
struct ast_format *ast_format_silk12;
struct ast_format *ast_format_silk16;
struct ast_format *ast_format_silk24;

/*! \brief Number of buckets to use for the media format cache (should be prime for performance reasons) */
#define CACHE_BUCKETS 53

/*! \brief Cached formats */
static struct ao2_container *formats;

static int format_hash_cb(const void *obj, int flags)
{
	const struct ast_format *format;
	const char *key;

	switch (flags & OBJ_SEARCH_MASK) {
	case OBJ_SEARCH_KEY:
		key = obj;
		return ast_str_case_hash(key);
	case OBJ_SEARCH_OBJECT:
		format = obj;
		return ast_str_case_hash(ast_format_get_name(format));
	default:
		/* Hash can only work on something with a full key. */
		ast_assert(0);
		return 0;
	}
}

static int format_cmp_cb(void *obj, void *arg, int flags)
{
	const struct ast_format *left = obj;
	const struct ast_format *right = arg;
	const char *right_key = arg;
	int cmp;

	switch (flags & OBJ_SEARCH_MASK) {
	case OBJ_SEARCH_OBJECT:
		right_key = ast_format_get_name(right);
		/* Fall through */
	case OBJ_SEARCH_KEY:
		cmp = strcasecmp(ast_format_get_name(left), right_key);
		break;
	case OBJ_SEARCH_PARTIAL_KEY:
		cmp = strncasecmp(ast_format_get_name(left), right_key, strlen(right_key));
		break;
	default:
		ast_assert(0);
		cmp = 0;
		break;
	}
	if (cmp) {
		return 0;
	}

	return CMP_MATCH;
}

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

	ao2_replace(ast_format_amr, NULL);
	ao2_replace(ast_format_amrwb, NULL);
	ao2_replace(ast_format_g723, NULL);
	ao2_replace(ast_format_ulaw, NULL);
	ao2_replace(ast_format_alaw, NULL);
	ao2_replace(ast_format_gsm, NULL);
	ao2_replace(ast_format_g726, NULL);
	ao2_replace(ast_format_g726_aal2, NULL);
	ao2_replace(ast_format_adpcm, NULL);
	ao2_replace(ast_format_slin, NULL);
	ao2_replace(ast_format_slin12, NULL);
	ao2_replace(ast_format_slin16, NULL);
	ao2_replace(ast_format_slin24, NULL);
	ao2_replace(ast_format_slin32, NULL);
	ao2_replace(ast_format_slin44, NULL);
	ao2_replace(ast_format_slin48, NULL);
	ao2_replace(ast_format_slin96, NULL);
	ao2_replace(ast_format_slin192, NULL);
	ao2_replace(ast_format_lpc10, NULL);
	ao2_replace(ast_format_g729, NULL);
	ao2_replace(ast_format_speex, NULL);
	ao2_replace(ast_format_speex16, NULL);
	ao2_replace(ast_format_speex32, NULL);
	ao2_replace(ast_format_ilbc, NULL);
	ao2_replace(ast_format_g722, NULL);
	ao2_replace(ast_format_siren7, NULL);
	ao2_replace(ast_format_siren14, NULL);
	ao2_replace(ast_format_testlaw, NULL);
	ao2_replace(ast_format_g719, NULL);
	ao2_replace(ast_format_opus, NULL);
	ao2_replace(ast_format_jpeg, NULL);
	ao2_replace(ast_format_png, NULL);
	ao2_replace(ast_format_h261, NULL);
	ao2_replace(ast_format_h263, NULL);
	ao2_replace(ast_format_h263p, NULL);
	ao2_replace(ast_format_h264, NULL);
	ao2_replace(ast_format_mp4, NULL);
	ao2_replace(ast_format_vp8, NULL);
	ao2_replace(ast_format_t140_red, NULL);
	ao2_replace(ast_format_t140, NULL);
	ao2_replace(ast_format_none, NULL);
	ao2_replace(ast_format_silk8, NULL);
	ao2_replace(ast_format_silk12, NULL);
	ao2_replace(ast_format_silk16, NULL);
	ao2_replace(ast_format_silk24, NULL);
}

int ast_format_cache_init(void)
{
	formats = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, CACHE_BUCKETS,
		format_hash_cb, format_cmp_cb);
	if (!formats) {
		return -1;
	}

	ast_register_cleanup(format_cache_shutdown);

	return 0;
}

static void set_cached_format(const char *name, struct ast_format *format)
{
	if (!strcmp(name, "g723")) {
		ao2_replace(ast_format_g723, format);
	} else if (!strcmp(name, "ulaw")) {
		ao2_replace(ast_format_ulaw, format);
	} else if (!strcmp(name, "alaw")) {
		ao2_replace(ast_format_alaw, format);
	} else if (!strcmp(name, "gsm")) {
		ao2_replace(ast_format_gsm, format);
	} else if (!strcmp(name, "g726")) {
		ao2_replace(ast_format_g726, format);
	} else if (!strcmp(name, "g726aal2")) {
		ao2_replace(ast_format_g726_aal2, format);
	} else if (!strcmp(name, "adpcm")) {
		ao2_replace(ast_format_adpcm, format);
	} else if (!strcmp(name, "slin")) {
		ao2_replace(ast_format_slin, format);
	} else if (!strcmp(name, "slin12")) {
		ao2_replace(ast_format_slin12, format);
	} else if (!strcmp(name, "slin16")) {
		ao2_replace(ast_format_slin16, format);
	} else if (!strcmp(name, "slin24")) {
		ao2_replace(ast_format_slin24, format);
	} else if (!strcmp(name, "slin32")) {
		ao2_replace(ast_format_slin32, format);
	} else if (!strcmp(name, "slin44")) {
		ao2_replace(ast_format_slin44, format);
	} else if (!strcmp(name, "slin48")) {
		ao2_replace(ast_format_slin48, format);
	} else if (!strcmp(name, "slin96")) {
		ao2_replace(ast_format_slin96, format);
	} else if (!strcmp(name, "slin192")) {
		ao2_replace(ast_format_slin192, format);
	} else if (!strcmp(name, "lpc10")) {
		ao2_replace(ast_format_lpc10, format);
	} else if (!strcmp(name, "g729")) {
		ao2_replace(ast_format_g729, format);
	} else if (!strcmp(name, "speex")) {
		ao2_replace(ast_format_speex, format);
	} else if (!strcmp(name, "speex16")) {
		ao2_replace(ast_format_speex16, format);
	} else if (!strcmp(name, "speex32")) {
		ao2_replace(ast_format_speex32, format);
	} else if (!strcmp(name, "ilbc")) {
		ao2_replace(ast_format_ilbc, format);
	} else if (!strcmp(name, "g722")) {
		ao2_replace(ast_format_g722, format);
	} else if (!strcmp(name, "siren7")) {
		ao2_replace(ast_format_siren7, format);
	} else if (!strcmp(name, "siren14")) {
		ao2_replace(ast_format_siren14, format);
	} else if (!strcmp(name, "testlaw")) {
		ao2_replace(ast_format_testlaw, format);
	} else if (!strcmp(name, "g719")) {
		ao2_replace(ast_format_g719, format);
	} else if (!strcmp(name, "opus")) {
		ao2_replace(ast_format_opus, format);
	} else if (!strcmp(name, "amr")) {
		ao2_replace(ast_format_amr, format);
	} else if (!strcmp(name, "amrwb")) {
		ao2_replace(ast_format_amrwb, format);
	} else if (!strcmp(name, "jpeg")) {
		ao2_replace(ast_format_jpeg, format);
	} else if (!strcmp(name, "png")) {
		ao2_replace(ast_format_png, format);
	} else if (!strcmp(name, "h261")) {
		ao2_replace(ast_format_h261, format);
	} else if (!strcmp(name, "h263")) {
		ao2_replace(ast_format_h263, format);
	} else if (!strcmp(name, "h263p")) {
		ao2_replace(ast_format_h263p, format);
	} else if (!strcmp(name, "h264")) {
		ao2_replace(ast_format_h264, format);
	} else if (!strcmp(name, "mpeg4")) {
		ao2_replace(ast_format_mp4, format);
	} else if (!strcmp(name, "vp8")) {
		ao2_replace(ast_format_vp8, format);
	} else if (!strcmp(name, "red")) {
		ao2_replace(ast_format_t140_red, format);
	} else if (!strcmp(name, "t140")) {
		ao2_replace(ast_format_t140, format);
	} else if (!strcmp(name, "none")) {
		ao2_replace(ast_format_none, format);
	} else if (!strcmp(name, "silk8")) {
		ao2_replace(ast_format_silk8, format);
	} else if (!strcmp(name, "silk12")) {
		ao2_replace(ast_format_silk12, format);
	} else if (!strcmp(name, "silk16")) {
		ao2_replace(ast_format_silk16, format);
	} else if (!strcmp(name, "silk24")) {
		ao2_replace(ast_format_silk24, format);
	}
}

int ast_format_cache_set(struct ast_format *format)
{
	SCOPED_AO2WRLOCK(lock, formats);
	struct ast_format *old_format;

	ast_assert(format != NULL);

	if (ast_strlen_zero(ast_format_get_name(format))) {
		return -1;
	}

	old_format = ao2_find(formats, ast_format_get_name(format), OBJ_SEARCH_KEY | OBJ_NOLOCK);
	if (old_format) {
		ao2_unlink_flags(formats, old_format, OBJ_NOLOCK);
	}
	ao2_link_flags(formats, format, OBJ_NOLOCK);

	set_cached_format(ast_format_get_name(format), format);

	ast_verb(2, "%s cached format with name '%s'\n",
		old_format ? "Updated" : "Created",
		ast_format_get_name(format));

	ao2_cleanup(old_format);

	return 0;
}

struct ast_format *__ast_format_cache_get(const char *name)
{
	if (ast_strlen_zero(name)) {
		return NULL;
	}

	return ao2_find(formats, name, OBJ_SEARCH_KEY);
}

struct ast_format *__ast_format_cache_get_debug(const char *name, const char *tag, const char *file, int line, const char *func)
{
	if (ast_strlen_zero(name)) {
		return NULL;
	}

	return __ao2_find_debug(formats, name, OBJ_SEARCH_KEY, S_OR(tag, "ast_format_cache_get"), file, line, func);
}

struct ast_format *ast_format_cache_get_slin_by_rate(unsigned int rate)
{
	if (rate >= 192000) {
		return ast_format_slin192;
	} else if (rate >= 96000) {
		return ast_format_slin96;
	} else if (rate >= 48000) {
		return ast_format_slin48;
	} else if (rate >= 44100) {
		return ast_format_slin44;
	} else if (rate >= 32000) {
		return ast_format_slin32;
	} else if (rate >= 24000) {
		return ast_format_slin24;
	} else if (rate >= 16000) {
		return ast_format_slin16;
	} else if (rate >= 12000) {
		return ast_format_slin12;
	}
	return ast_format_slin;
}

int ast_format_cache_is_slinear(struct ast_format *format)
{
	if ((ast_format_cmp(format, ast_format_slin) == AST_FORMAT_CMP_EQUAL)
		|| (ast_format_cmp(format, ast_format_slin12) == AST_FORMAT_CMP_EQUAL)
		|| (ast_format_cmp(format, ast_format_slin16) == AST_FORMAT_CMP_EQUAL)
		|| (ast_format_cmp(format, ast_format_slin24) == AST_FORMAT_CMP_EQUAL)
		|| (ast_format_cmp(format, ast_format_slin32) == AST_FORMAT_CMP_EQUAL)
		|| (ast_format_cmp(format, ast_format_slin44) == AST_FORMAT_CMP_EQUAL)
		|| (ast_format_cmp(format, ast_format_slin48) == AST_FORMAT_CMP_EQUAL)
		|| (ast_format_cmp(format, ast_format_slin96) == AST_FORMAT_CMP_EQUAL)
		|| (ast_format_cmp(format, ast_format_slin192) == AST_FORMAT_CMP_EQUAL)) {
		return 1;
	}

	return 0;
}