2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2014, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
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.
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.
21 * \brief Media Format Cache API
23 * \author Joshua Colp <jcolp@digium.com>
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/logger.h"
35 #include "asterisk/format.h"
36 #include "asterisk/format_cache.h"
37 #include "asterisk/astobj2.h"
38 #include "asterisk/strings.h"
41 * \brief Built-in cached signed linear 8kHz format.
43 struct ast_format *ast_format_slin;
46 * \brief Built-in cached signed linear 12kHz format.
48 struct ast_format *ast_format_slin12;
51 * \brief Built-in cached signed linear 16kHz format.
53 struct ast_format *ast_format_slin16;
56 * \brief Built-in cached signed linear 24kHz format.
58 struct ast_format *ast_format_slin24;
61 * \brief Built-in cached signed linear 32kHz format.
63 struct ast_format *ast_format_slin32;
66 * \brief Built-in cached signed linear 44kHz format.
68 struct ast_format *ast_format_slin44;
71 * \brief Built-in cached signed linear 48kHz format.
73 struct ast_format *ast_format_slin48;
76 * \brief Built-in cached signed linear 96kHz format.
78 struct ast_format *ast_format_slin96;
81 * \brief Built-in cached signed linear 192kHz format.
83 struct ast_format *ast_format_slin192;
86 * \brief Built-in cached ulaw format.
88 struct ast_format *ast_format_ulaw;
91 * \brief Built-in cached alaw format.
93 struct ast_format *ast_format_alaw;
96 * \brief Built-in cached testlaw format.
98 struct ast_format *ast_format_testlaw;
101 * \brief Built-in cached gsm format.
103 struct ast_format *ast_format_gsm;
106 * \brief Built-in cached adpcm format.
108 struct ast_format *ast_format_adpcm;
111 * \brief Built-in cached g722 format.
113 struct ast_format *ast_format_g722;
116 * \brief Built-in cached g726 format.
118 struct ast_format *ast_format_g726;
121 * \brief Built-in cached g726-aal2 format.
123 struct ast_format *ast_format_g726_aal2;
126 * \brief Built-in cached ilbc format.
128 struct ast_format *ast_format_ilbc;
131 * \brief Built-in cached ilbc format.
133 struct ast_format *ast_format_lpc10;
136 * \brief Built-in cached speex format.
138 struct ast_format *ast_format_speex;
141 * \brief Built-in cached speex at 16kHz format.
143 struct ast_format *ast_format_speex16;
146 * \brief Built-in cached speex at 32kHz format.
148 struct ast_format *ast_format_speex32;
151 * \brief Built-in cached g723.1 format.
153 struct ast_format *ast_format_g723;
156 * \brief Built-in cached g729 format.
158 struct ast_format *ast_format_g729;
161 * \brief Built-in cached g719 format.
163 struct ast_format *ast_format_g719;
166 * \brief Built-in cached h261 format.
168 struct ast_format *ast_format_h261;
171 * \brief Built-in cached h263 format.
173 struct ast_format *ast_format_h263;
176 * \brief Built-in cached h263 plus format.
178 struct ast_format *ast_format_h263p;
181 * \brief Built-in cached h264 format.
183 struct ast_format *ast_format_h264;
186 * \brief Built-in cached mp4 format.
188 struct ast_format *ast_format_mp4;
191 * \brief Built-in cached vp8 format.
193 struct ast_format *ast_format_vp8;
196 * \brief Built-in cached jpeg format.
198 struct ast_format *ast_format_jpeg;
201 * \brief Built-in cached png format.
203 struct ast_format *ast_format_png;
206 * \brief Built-in cached siren14 format.
208 struct ast_format *ast_format_siren14;
211 * \brief Built-in cached siren7 format.
213 struct ast_format *ast_format_siren7;
216 * \brief Built-in cached opus format.
218 struct ast_format *ast_format_opus;
221 * \brief Built-in cached t140 format.
223 struct ast_format *ast_format_t140;
226 * \brief Built-in cached t140 red format.
228 struct ast_format *ast_format_t140_red;
231 * \brief Built-in "null" format.
233 struct ast_format *ast_format_none;
235 /*! \brief Number of buckets to use for the media format cache (should be prime for performance reasons) */
236 #define CACHE_BUCKETS 53
238 /*! \brief Cached formats */
239 static struct ao2_container *formats;
241 static int format_hash_cb(const void *obj, int flags)
243 const struct ast_format *format;
246 switch (flags & OBJ_SEARCH_MASK) {
249 return ast_str_case_hash(key);
250 case OBJ_SEARCH_OBJECT:
252 return ast_str_case_hash(ast_format_get_name(format));
254 /* Hash can only work on something with a full key. */
260 static int format_cmp_cb(void *obj, void *arg, int flags)
262 const struct ast_format *left = obj;
263 const struct ast_format *right = arg;
264 const char *right_key = arg;
267 switch (flags & OBJ_SEARCH_MASK) {
268 case OBJ_SEARCH_OBJECT:
269 right_key = ast_format_get_name(right);
272 cmp = strcasecmp(ast_format_get_name(left), right_key);
274 case OBJ_SEARCH_PARTIAL_KEY:
275 cmp = strncasecmp(ast_format_get_name(left), right_key, strlen(right_key));
289 /*! \brief Function called when the process is shutting down */
290 static void format_cache_shutdown(void)
292 ao2_cleanup(formats);
295 ao2_replace(ast_format_g723, NULL);
296 ao2_replace(ast_format_ulaw, NULL);
297 ao2_replace(ast_format_alaw, NULL);
298 ao2_replace(ast_format_gsm, NULL);
299 ao2_replace(ast_format_g726, NULL);
300 ao2_replace(ast_format_g726_aal2, NULL);
301 ao2_replace(ast_format_adpcm, NULL);
302 ao2_replace(ast_format_slin, NULL);
303 ao2_replace(ast_format_slin12, NULL);
304 ao2_replace(ast_format_slin16, NULL);
305 ao2_replace(ast_format_slin24, NULL);
306 ao2_replace(ast_format_slin32, NULL);
307 ao2_replace(ast_format_slin44, NULL);
308 ao2_replace(ast_format_slin48, NULL);
309 ao2_replace(ast_format_slin96, NULL);
310 ao2_replace(ast_format_slin192, NULL);
311 ao2_replace(ast_format_lpc10, NULL);
312 ao2_replace(ast_format_g729, NULL);
313 ao2_replace(ast_format_speex, NULL);
314 ao2_replace(ast_format_speex16, NULL);
315 ao2_replace(ast_format_speex32, NULL);
316 ao2_replace(ast_format_ilbc, NULL);
317 ao2_replace(ast_format_g722, NULL);
318 ao2_replace(ast_format_siren7, NULL);
319 ao2_replace(ast_format_siren14, NULL);
320 ao2_replace(ast_format_testlaw, NULL);
321 ao2_replace(ast_format_g719, NULL);
322 ao2_replace(ast_format_opus, NULL);
323 ao2_replace(ast_format_jpeg, NULL);
324 ao2_replace(ast_format_png, NULL);
325 ao2_replace(ast_format_h261, NULL);
326 ao2_replace(ast_format_h263, NULL);
327 ao2_replace(ast_format_h263p, NULL);
328 ao2_replace(ast_format_h264, NULL);
329 ao2_replace(ast_format_mp4, NULL);
330 ao2_replace(ast_format_vp8, NULL);
331 ao2_replace(ast_format_t140_red, NULL);
332 ao2_replace(ast_format_t140, NULL);
333 ao2_replace(ast_format_none, NULL);
336 int ast_format_cache_init(void)
338 formats = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, CACHE_BUCKETS,
339 format_hash_cb, format_cmp_cb);
344 ast_register_atexit(format_cache_shutdown);
349 static void set_cached_format(const char *name, struct ast_format *format)
351 if (!strcmp(name, "g723")) {
352 ao2_replace(ast_format_g723, format);
353 } else if (!strcmp(name, "ulaw")) {
354 ao2_replace(ast_format_ulaw, format);
355 } else if (!strcmp(name, "alaw")) {
356 ao2_replace(ast_format_alaw, format);
357 } else if (!strcmp(name, "gsm")) {
358 ao2_replace(ast_format_gsm, format);
359 } else if (!strcmp(name, "g726")) {
360 ao2_replace(ast_format_g726, format);
361 } else if (!strcmp(name, "g726aal2")) {
362 ao2_replace(ast_format_g726_aal2, format);
363 } else if (!strcmp(name, "adpcm")) {
364 ao2_replace(ast_format_adpcm, format);
365 } else if (!strcmp(name, "slin")) {
366 ao2_replace(ast_format_slin, format);
367 } else if (!strcmp(name, "slin12")) {
368 ao2_replace(ast_format_slin12, format);
369 } else if (!strcmp(name, "slin16")) {
370 ao2_replace(ast_format_slin16, format);
371 } else if (!strcmp(name, "slin24")) {
372 ao2_replace(ast_format_slin24, format);
373 } else if (!strcmp(name, "slin32")) {
374 ao2_replace(ast_format_slin32, format);
375 } else if (!strcmp(name, "slin44")) {
376 ao2_replace(ast_format_slin44, format);
377 } else if (!strcmp(name, "slin48")) {
378 ao2_replace(ast_format_slin48, format);
379 } else if (!strcmp(name, "slin96")) {
380 ao2_replace(ast_format_slin96, format);
381 } else if (!strcmp(name, "slin192")) {
382 ao2_replace(ast_format_slin192, format);
383 } else if (!strcmp(name, "lpc10")) {
384 ao2_replace(ast_format_lpc10, format);
385 } else if (!strcmp(name, "g729")) {
386 ao2_replace(ast_format_g729, format);
387 } else if (!strcmp(name, "speex")) {
388 ao2_replace(ast_format_speex, format);
389 } else if (!strcmp(name, "speex16")) {
390 ao2_replace(ast_format_speex16, format);
391 } else if (!strcmp(name, "speex32")) {
392 ao2_replace(ast_format_speex32, format);
393 } else if (!strcmp(name, "ilbc")) {
394 ao2_replace(ast_format_ilbc, format);
395 } else if (!strcmp(name, "g722")) {
396 ao2_replace(ast_format_g722, format);
397 } else if (!strcmp(name, "siren7")) {
398 ao2_replace(ast_format_siren7, format);
399 } else if (!strcmp(name, "siren14")) {
400 ao2_replace(ast_format_siren14, format);
401 } else if (!strcmp(name, "testlaw")) {
402 ao2_replace(ast_format_testlaw, format);
403 } else if (!strcmp(name, "g719")) {
404 ao2_replace(ast_format_g719, format);
405 } else if (!strcmp(name, "opus")) {
406 ao2_replace(ast_format_opus, format);
407 } else if (!strcmp(name, "jpeg")) {
408 ao2_replace(ast_format_jpeg, format);
409 } else if (!strcmp(name, "png")) {
410 ao2_replace(ast_format_png, format);
411 } else if (!strcmp(name, "h261")) {
412 ao2_replace(ast_format_h261, format);
413 } else if (!strcmp(name, "h263")) {
414 ao2_replace(ast_format_h263, format);
415 } else if (!strcmp(name, "h263p")) {
416 ao2_replace(ast_format_h263p, format);
417 } else if (!strcmp(name, "h264")) {
418 ao2_replace(ast_format_h264, format);
419 } else if (!strcmp(name, "mpeg4")) {
420 ao2_replace(ast_format_mp4, format);
421 } else if (!strcmp(name, "vp8")) {
422 ao2_replace(ast_format_vp8, format);
423 } else if (!strcmp(name, "red")) {
424 ao2_replace(ast_format_t140_red, format);
425 } else if (!strcmp(name, "t140")) {
426 ao2_replace(ast_format_t140, format);
427 } else if (!strcmp(name, "none")) {
428 ao2_replace(ast_format_none, format);
432 int ast_format_cache_set(struct ast_format *format)
434 SCOPED_AO2WRLOCK(lock, formats);
435 struct ast_format *old_format;
437 ast_assert(format != NULL);
439 if (ast_strlen_zero(ast_format_get_name(format))) {
443 old_format = ao2_find(formats, ast_format_get_name(format), OBJ_SEARCH_KEY | OBJ_NOLOCK);
445 ao2_unlink_flags(formats, old_format, OBJ_NOLOCK);
447 ao2_link_flags(formats, format, OBJ_NOLOCK);
449 set_cached_format(ast_format_get_name(format), format);
451 ast_verb(2, "%s cached format with name '%s'\n",
452 old_format ? "Updated" : "Created",
453 ast_format_get_name(format));
455 ao2_cleanup(old_format);
460 struct ast_format *__ast_format_cache_get(const char *name)
462 if (ast_strlen_zero(name)) {
466 return ao2_find(formats, name, OBJ_SEARCH_KEY);
469 struct ast_format *__ast_format_cache_get_debug(const char *name, const char *tag, const char *file, int line, const char *func)
471 if (ast_strlen_zero(name)) {
475 return __ao2_find_debug(formats, name, OBJ_SEARCH_KEY, S_OR(tag, "ast_format_cache_get"), file, line, func);
478 struct ast_format *ast_format_cache_get_slin_by_rate(unsigned int rate)
480 if (rate >= 192000) {
481 return ast_format_slin192;
482 } else if (rate >= 96000) {
483 return ast_format_slin96;
484 } else if (rate >= 48000) {
485 return ast_format_slin48;
486 } else if (rate >= 44100) {
487 return ast_format_slin44;
488 } else if (rate >= 32000) {
489 return ast_format_slin32;
490 } else if (rate >= 24000) {
491 return ast_format_slin24;
492 } else if (rate >= 16000) {
493 return ast_format_slin16;
494 } else if (rate >= 12000) {
495 return ast_format_slin12;
497 return ast_format_slin;
500 int ast_format_cache_is_slinear(struct ast_format *format)
502 if ((ast_format_cmp(format, ast_format_slin) == AST_FORMAT_CMP_EQUAL)
503 || (ast_format_cmp(format, ast_format_slin16) == AST_FORMAT_CMP_EQUAL)
504 || (ast_format_cmp(format, ast_format_slin24) == AST_FORMAT_CMP_EQUAL)
505 || (ast_format_cmp(format, ast_format_slin32) == AST_FORMAT_CMP_EQUAL)
506 || (ast_format_cmp(format, ast_format_slin44) == AST_FORMAT_CMP_EQUAL)
507 || (ast_format_cmp(format, ast_format_slin48) == AST_FORMAT_CMP_EQUAL)
508 || (ast_format_cmp(format, ast_format_slin96) == AST_FORMAT_CMP_EQUAL)
509 || (ast_format_cmp(format, ast_format_slin192) == AST_FORMAT_CMP_EQUAL)) {