Debian Codegraph @

main/format_cache.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2014, Digium, Inc.
5 *
6 * Joshua Colp <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*! \file
20 *
21 * \brief Media Format Cache API
22 *
23 * \author Joshua Colp <[email protected]>
24 */
25
26 /*** MODULEINFO
27 <support_level>core</support_level>
28 ***/
29
30 #include "asterisk.h"
31
32 #include "asterisk/logger.h"
33 #include "asterisk/format.h"
34 #include "asterisk/format_cache.h"
35 #include "asterisk/astobj2.h"
36 #include "asterisk/strings.h"
37
38 /*!
39 * \brief Built-in cached signed linear 8kHz format.
40 */
41 struct ast_format *ast_format_slin;
42
43 /*!
44 * \brief Built-in cached signed linear 12kHz format.
45 */
46 struct ast_format *ast_format_slin12;
47
48 /*!
49 * \brief Built-in cached signed linear 16kHz format.
50 */
51 struct ast_format *ast_format_slin16;
52
53 /*!
54 * \brief Built-in cached signed linear 24kHz format.
55 */
56 struct ast_format *ast_format_slin24;
57
58 /*!
59 * \brief Built-in cached signed linear 32kHz format.
60 */
61 struct ast_format *ast_format_slin32;
62
63 /*!
64 * \brief Built-in cached signed linear 44kHz format.
65 */
66 struct ast_format *ast_format_slin44;
67
68 /*!
69 * \brief Built-in cached signed linear 48kHz format.
70 */
71 struct ast_format *ast_format_slin48;
72
73 /*!
74 * \brief Built-in cached signed linear 96kHz format.
75 */
76 struct ast_format *ast_format_slin96;
77
78 /*!
79 * \brief Built-in cached signed linear 192kHz format.
80 */
81 struct ast_format *ast_format_slin192;
82
83 /*!
84 * \brief Built-in cached ulaw format.
85 */
86 struct ast_format *ast_format_ulaw;
87
88 /*!
89 * \brief Built-in cached alaw format.
90 */
91 struct ast_format *ast_format_alaw;
92
93 /*!
94 * \brief Built-in cached gsm format.
95 */
96 struct ast_format *ast_format_gsm;
97
98 /*!
99 * \brief Built-in cached adpcm format.
100 */
101 struct ast_format *ast_format_adpcm;
102
103 /*!
104 * \brief Built-in cached g722 format.
105 */
106 struct ast_format *ast_format_g722;
107
108 /*!
109 * \brief Built-in cached g726 format.
110 */
111 struct ast_format *ast_format_g726;
112
113 /*!
114 * \brief Built-in cached g726-aal2 format.
115 */
116 struct ast_format *ast_format_g726_aal2;
117
118 /*!
119 * \brief Built-in cached ilbc format.
120 */
121 struct ast_format *ast_format_ilbc;
122
123 /*!
124 * \brief Built-in cached ilbc format.
125 */
126 struct ast_format *ast_format_lpc10;
127
128 /*!
129 * \brief Built-in cached speex format.
130 */
131 struct ast_format *ast_format_speex;
132
133 /*!
134 * \brief Built-in cached speex at 16kHz format.
135 */
136 struct ast_format *ast_format_speex16;
137
138 /*!
139 * \brief Built-in cached speex at 32kHz format.
140 */
141 struct ast_format *ast_format_speex32;
142
143 /*!
144 * \brief Built-in cached g723.1 format.
145 */
146 struct ast_format *ast_format_g723;
147
148 /*!
149 * \brief Built-in cached g729 format.
150 */
151 struct ast_format *ast_format_g729;
152
153 /*!
154 * \brief Built-in cached g719 format.
155 */
156 struct ast_format *ast_format_g719;
157
158 /*!
159 * \brief Built-in cached h261 format.
160 */
161 struct ast_format *ast_format_h261;
162
163 /*!
164 * \brief Built-in cached h263 format.
165 */
166 struct ast_format *ast_format_h263;
167
168 /*!
169 * \brief Built-in cached h263 plus format.
170 */
171 struct ast_format *ast_format_h263p;
172
173 /*!
174 * \brief Built-in cached h264 format.
175 */
176 struct ast_format *ast_format_h264;
177
178 /*!
179 * \brief Built-in cached h265 format.
180 */
181 struct ast_format *ast_format_h265;
182
183 /*!
184 * \brief Built-in cached mp4 format.
185 */
186 struct ast_format *ast_format_mp4;
187
188 /*!
189 * \brief Built-in cached vp8 format.
190 */
191 struct ast_format *ast_format_vp8;
192
193 /*!
194 * \brief Built-in cached vp9 format.
195 */
196 struct ast_format *ast_format_vp9;
197
198 /*!
199 * \brief Built-in cached jpeg format.
200 */
201 struct ast_format *ast_format_jpeg;
202
203 /*!
204 * \brief Built-in cached png format.
205 */
206 struct ast_format *ast_format_png;
207
208 /*!
209 * \brief Built-in cached siren14 format.
210 */
211 struct ast_format *ast_format_siren14;
212
213 /*!
214 * \brief Built-in cached siren7 format.
215 */
216 struct ast_format *ast_format_siren7;
217
218 /*!
219 * \brief Built-in cached opus format.
220 */
221 struct ast_format *ast_format_opus;
222
223 /*!
224 * \brief Built-in cached codec2 format.
225 */
226 struct ast_format *ast_format_codec2;
227
228 /*!
•229 * \brief Built-in cached AMR format.
230 */
231 struct ast_format *ast_format_amr;
232
233 /*!
234 * \brief Built-in cached AMR-WB format.
235 */
236 struct ast_format *ast_format_amrwb;
237
238 /*!
239 * \brief Built-in cached t140 format.
240 */
241 struct ast_format *ast_format_t140;
242
243 /*!
244 * \brief Built-in cached t140 red format.
245 */
246 struct ast_format *ast_format_t140_red;
247
248 /*!
249 * \brief Built-in cached T.38 format.
250 */
251 struct ast_format *ast_format_t38;
252
253 /*!
254 * \brief Built-in "null" format.
255 */
256 struct ast_format *ast_format_none;
257
258 /*!
259 * \brief Built-in "silk" format
260 */
261 struct ast_format *ast_format_silk8;
262 struct ast_format *ast_format_silk12;
263 struct ast_format *ast_format_silk16;
264 struct ast_format *ast_format_silk24;
265
266 /*! \brief Number of buckets to use for the media format cache (should be prime for performance reasons) */
267 #define CACHE_BUCKETS 53
268
269 /*! \brief Cached formats */
270 static struct ao2_container *formats;
271
272 static int format_hash_cb(const void *obj, int flags)
273 {
274 const struct ast_format *format;
275 const char *key;
276
277 switch (flags & OBJ_SEARCH_MASK) {
278 case OBJ_SEARCH_KEY:
279 key = obj;
280 return ast_str_case_hash(key);
281 case OBJ_SEARCH_OBJECT:
282 format = obj;
283 return ast_str_case_hash(ast_format_get_name(format));
284 default:
285 /* Hash can only work on something with a full key. */
286 ast_assert(0);
287 return 0;
288 }
289 }
290
291 static int format_cmp_cb(void *obj, void *arg, int flags)
292 {
293 const struct ast_format *left = obj;
294 const struct ast_format *right = arg;
295 const char *right_key = arg;
296 int cmp;
297
298 switch (flags & OBJ_SEARCH_MASK) {
299 case OBJ_SEARCH_OBJECT:
300 right_key = ast_format_get_name(right);
301 /* Fall through */
302 case OBJ_SEARCH_KEY:
303 cmp = strcasecmp(ast_format_get_name(left), right_key);
304 break;
305 case OBJ_SEARCH_PARTIAL_KEY:
306 cmp = strncasecmp(ast_format_get_name(left), right_key, strlen(right_key));
307 break;
308 default:
309 ast_assert(0);
310 cmp = 0;
311 break;
312 }
313 if (cmp) {
314 return 0;
315 }
316
317 return CMP_MATCH;
318 }
319
320 /*! \brief Function called when the process is shutting down */
321 static void format_cache_shutdown(void)
322 {
323 ao2_cleanup(formats);
324 formats = NULL;
325
•326 ao2_replace(ast_format_amr, NULL);
327 ao2_replace(ast_format_amrwb, NULL);
328
329 ao2_replace(ast_format_g723, NULL);
330 ao2_replace(ast_format_ulaw, NULL);
331 ao2_replace(ast_format_alaw, NULL);
332 ao2_replace(ast_format_gsm, NULL);
333 ao2_replace(ast_format_g726, NULL);
334 ao2_replace(ast_format_g726_aal2, NULL);
335 ao2_replace(ast_format_adpcm, NULL);
336 ao2_replace(ast_format_slin, NULL);
337 ao2_replace(ast_format_slin12, NULL);
338 ao2_replace(ast_format_slin16, NULL);
339 ao2_replace(ast_format_slin24, NULL);
340 ao2_replace(ast_format_slin32, NULL);
341 ao2_replace(ast_format_slin44, NULL);
342 ao2_replace(ast_format_slin48, NULL);
343 ao2_replace(ast_format_slin96, NULL);
344 ao2_replace(ast_format_slin192, NULL);
345 ao2_replace(ast_format_lpc10, NULL);
346 ao2_replace(ast_format_g729, NULL);
347 ao2_replace(ast_format_speex, NULL);
348 ao2_replace(ast_format_speex16, NULL);
349 ao2_replace(ast_format_speex32, NULL);
350 ao2_replace(ast_format_ilbc, NULL);
351 ao2_replace(ast_format_g722, NULL);
352 ao2_replace(ast_format_siren7, NULL);
353 ao2_replace(ast_format_siren14, NULL);
354 ao2_replace(ast_format_g719, NULL);
355 ao2_replace(ast_format_opus, NULL);
356 ao2_replace(ast_format_codec2, NULL);
357 ao2_replace(ast_format_jpeg, NULL);
358 ao2_replace(ast_format_png, NULL);
359 ao2_replace(ast_format_h261, NULL);
360 ao2_replace(ast_format_h263, NULL);
361 ao2_replace(ast_format_h263p, NULL);
362 ao2_replace(ast_format_h264, NULL);
363 ao2_replace(ast_format_h265, NULL);
364 ao2_replace(ast_format_mp4, NULL);
365 ao2_replace(ast_format_vp8, NULL);
366 ao2_replace(ast_format_vp9, NULL);
367 ao2_replace(ast_format_t140_red, NULL);
368 ao2_replace(ast_format_t140, NULL);
369 ao2_replace(ast_format_t38, NULL);
370 ao2_replace(ast_format_none, NULL);
371 ao2_replace(ast_format_silk8, NULL);
372 ao2_replace(ast_format_silk12, NULL);
373 ao2_replace(ast_format_silk16, NULL);
374 ao2_replace(ast_format_silk24, NULL);
375 }
376
377 int ast_format_cache_init(void)
378 {
379 formats = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, CACHE_BUCKETS,
380 format_hash_cb, NULL, format_cmp_cb);
381 if (!formats) {
382 return -1;
383 }
384
385 ast_register_cleanup(format_cache_shutdown);
386
387 return 0;
388 }
389
390 static void set_cached_format(const char *name, struct ast_format *format)
391 {
392 if (!strcmp(name, "codec2")) {
393 ao2_replace(ast_format_codec2, format);
394 } else if (!strcmp(name, "g723")) {
395 ao2_replace(ast_format_g723, format);
396 } else if (!strcmp(name, "ulaw")) {
397 ao2_replace(ast_format_ulaw, format);
398 } else if (!strcmp(name, "alaw")) {
399 ao2_replace(ast_format_alaw, format);
400 } else if (!strcmp(name, "gsm")) {
401 ao2_replace(ast_format_gsm, format);
402 } else if (!strcmp(name, "g726")) {
403 ao2_replace(ast_format_g726, format);
404 } else if (!strcmp(name, "g726aal2")) {
405 ao2_replace(ast_format_g726_aal2, format);
406 } else if (!strcmp(name, "adpcm")) {
407 ao2_replace(ast_format_adpcm, format);
408 } else if (!strcmp(name, "slin")) {
409 ao2_replace(ast_format_slin, format);
410 } else if (!strcmp(name, "slin12")) {
411 ao2_replace(ast_format_slin12, format);
412 } else if (!strcmp(name, "slin16")) {
413 ao2_replace(ast_format_slin16, format);
414 } else if (!strcmp(name, "slin24")) {
415 ao2_replace(ast_format_slin24, format);
416 } else if (!strcmp(name, "slin32")) {
417 ao2_replace(ast_format_slin32, format);
418 } else if (!strcmp(name, "slin44")) {
419 ao2_replace(ast_format_slin44, format);
420 } else if (!strcmp(name, "slin48")) {
421 ao2_replace(ast_format_slin48, format);
422 } else if (!strcmp(name, "slin96")) {
423 ao2_replace(ast_format_slin96, format);
424 } else if (!strcmp(name, "slin192")) {
425 ao2_replace(ast_format_slin192, format);
426 } else if (!strcmp(name, "lpc10")) {
427 ao2_replace(ast_format_lpc10, format);
428 } else if (!strcmp(name, "g729")) {
429 ao2_replace(ast_format_g729, format);
430 } else if (!strcmp(name, "speex")) {
431 ao2_replace(ast_format_speex, format);
432 } else if (!strcmp(name, "speex16")) {
433 ao2_replace(ast_format_speex16, format);
434 } else if (!strcmp(name, "speex32")) {
435 ao2_replace(ast_format_speex32, format);
436 } else if (!strcmp(name, "ilbc")) {
437 ao2_replace(ast_format_ilbc, format);
438 } else if (!strcmp(name, "g722")) {
439 ao2_replace(ast_format_g722, format);
440 } else if (!strcmp(name, "siren7")) {
441 ao2_replace(ast_format_siren7, format);
442 } else if (!strcmp(name, "siren14")) {
443 ao2_replace(ast_format_siren14, format);
444 } else if (!strcmp(name, "g719")) {
445 ao2_replace(ast_format_g719, format);
446 } else if (!strcmp(name, "opus")) {
447 ao2_replace(ast_format_opus, format);
•448 } else if (!strcmp(name, "amr")) {
449 ao2_replace(ast_format_amr, format);
450 } else if (!strcmp(name, "amrwb")) {
451 ao2_replace(ast_format_amrwb, format);
452 } else if (!strcmp(name, "jpeg")) {
453 ao2_replace(ast_format_jpeg, format);
454 } else if (!strcmp(name, "png")) {
455 ao2_replace(ast_format_png, format);
456 } else if (!strcmp(name, "h261")) {
457 ao2_replace(ast_format_h261, format);
458 } else if (!strcmp(name, "h263")) {
459 ao2_replace(ast_format_h263, format);
460 } else if (!strcmp(name, "h263p")) {
461 ao2_replace(ast_format_h263p, format);
462 } else if (!strcmp(name, "h264")) {
463 ao2_replace(ast_format_h264, format);
464 } else if (!strcmp(name, "h265")) {
465 ao2_replace(ast_format_h265, format);
466 } else if (!strcmp(name, "mpeg4")) {
467 ao2_replace(ast_format_mp4, format);
468 } else if (!strcmp(name, "vp8")) {
469 ao2_replace(ast_format_vp8, format);
470 } else if (!strcmp(name, "vp9")) {
471 ao2_replace(ast_format_vp9, format);
472 } else if (!strcmp(name, "red")) {
473 ao2_replace(ast_format_t140_red, format);
474 } else if (!strcmp(name, "t140")) {
475 ao2_replace(ast_format_t140, format);
476 } else if (!strcmp(name, "t38")) {
477 ao2_replace(ast_format_t38, format);
478 } else if (!strcmp(name, "none")) {
479 ao2_replace(ast_format_none, format);
480 } else if (!strcmp(name, "silk8")) {
481 ao2_replace(ast_format_silk8, format);
482 } else if (!strcmp(name, "silk12")) {
483 ao2_replace(ast_format_silk12, format);
484 } else if (!strcmp(name, "silk16")) {
485 ao2_replace(ast_format_silk16, format);
486 } else if (!strcmp(name, "silk24")) {
487 ao2_replace(ast_format_silk24, format);
488 }
489 }
490
491 int ast_format_cache_set(struct ast_format *format)
492 {
493 SCOPED_AO2WRLOCK(lock, formats);
494 struct ast_format *old_format;
495
496 ast_assert(format != NULL);
497
498 if (ast_strlen_zero(ast_format_get_name(format))) {
499 return -1;
500 }
501
502 old_format = ao2_find(formats, ast_format_get_name(format), OBJ_SEARCH_KEY | OBJ_NOLOCK);
503 if (old_format) {
504 ao2_unlink_flags(formats, old_format, OBJ_NOLOCK);
505 }
506 ao2_link_flags(formats, format, OBJ_NOLOCK);
507
508 set_cached_format(ast_format_get_name(format), format);
509
510 ast_verb(5, "%s cached format with name '%s'\n",
511 old_format ? "Updated" : "Created",
512 ast_format_get_name(format));
513
514 ao2_cleanup(old_format);
515
516 return 0;
517 }
518
519 struct ast_format *__ast_format_cache_get(const char *name,
520 const char *tag, const char *file, int line, const char *func)
521 {
522 if (ast_strlen_zero(name)) {
523 return NULL;
524 }
525
526 return __ao2_find(formats, name, OBJ_SEARCH_KEY, tag, file, line, func);
527 }
528
529 struct ast_format *ast_format_cache_get_slin_by_rate(unsigned int rate)
530 {
531 if (rate >= 192000) {
532 return ast_format_slin192;
533 } else if (rate >= 96000) {
534 return ast_format_slin96;
535 } else if (rate >= 48000) {
536 return ast_format_slin48;
537 } else if (rate >= 44100) {
538 return ast_format_slin44;
539 } else if (rate >= 32000) {
540 return ast_format_slin32;
541 } else if (rate >= 24000) {
542 return ast_format_slin24;
543 } else if (rate >= 16000) {
544 return ast_format_slin16;
545 } else if (rate >= 12000) {
546 return ast_format_slin12;
547 }
548 return ast_format_slin;
549 }
550
551 int ast_format_cache_is_slinear(struct ast_format *format)
552 {
553 if ((ast_format_cmp(format, ast_format_slin) == AST_FORMAT_CMP_EQUAL)
554 || (ast_format_cmp(format, ast_format_slin12) == AST_FORMAT_CMP_EQUAL)
555 || (ast_format_cmp(format, ast_format_slin16) == AST_FORMAT_CMP_EQUAL)
556 || (ast_format_cmp(format, ast_format_slin24) == AST_FORMAT_CMP_EQUAL)
557 || (ast_format_cmp(format, ast_format_slin32) == AST_FORMAT_CMP_EQUAL)
558 || (ast_format_cmp(format, ast_format_slin44) == AST_FORMAT_CMP_EQUAL)
559 || (ast_format_cmp(format, ast_format_slin48) == AST_FORMAT_CMP_EQUAL)
560 || (ast_format_cmp(format, ast_format_slin96) == AST_FORMAT_CMP_EQUAL)
561 || (ast_format_cmp(format, ast_format_slin192) == AST_FORMAT_CMP_EQUAL)) {
562 return 1;
563 }
564
565 return 0;
566 }
567
568 struct ast_format *ast_format_cache_get_by_codec(const struct ast_codec *codec)
569 {
570 struct ast_format *format;
571 struct ao2_iterator it;
572
573 for (it = ao2_iterator_init(formats, 0);
574 (format = ao2_iterator_next(&it));
575 ao2_ref(format, -1)) {
576 struct ast_codec *candidate = ast_format_get_codec(format);
577 if (codec == candidate) {
578 ao2_cleanup(candidate);
579 ao2_iterator_destroy(&it);
580 return format;
581 }
582 ao2_cleanup(candidate);
583 }
584
585 ao2_iterator_destroy(&it);
586 return NULL;
587 }