[asterisk-commits] file: branch group/media_formats r406151 - in /team/group/media_formats: incl...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Wed Jan 22 07:19:01 CST 2014
Author: file
Date: Wed Jan 22 07:18:43 2014
New Revision: 406151
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=406151
Log:
Add a format cache.
The format cache allows formats to be pre-created for common codecs (ulaw, alaw, g722)
and then used elsewhere. Formats are cached using a unique name.
Added:
team/group/media_formats/include/asterisk/format_cache.h (with props)
team/group/media_formats/main/format_cache.c (with props)
Modified:
team/group/media_formats/main/asterisk.c
Added: team/group/media_formats/include/asterisk/format_cache.h
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats/include/asterisk/format_cache.h?view=auto&rev=406151
==============================================================================
--- team/group/media_formats/include/asterisk/format_cache.h (added)
+++ team/group/media_formats/include/asterisk/format_cache.h Wed Jan 22 07:18:43 2014
@@ -1,0 +1,63 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2014, Digium, Inc.
+ *
+ * Joshua Colp <jcolp at 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 at digium.com>
+ */
+
+#ifndef _AST_FORMAT_CACHE_H_
+#define _AST_FORMAT_CACHE_H_
+
+struct ast_format;
+
+/*!
+ * \brief Initialize format cache support within the core.
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ */
+int ast_format_cache_init(void);
+
+/*!
+ * \brief Add a named format cache entry.
+ *
+ * \param name Name of the cached format
+ * \param format A pointer to the format to cache
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ */
+int ast_format_cache_add(const char *name, struct ast_format *format);
+
+/*!
+ * \brief Retrieve a named format from the cache.
+ *
+ * \param name Name of the cached format
+ *
+ * \retval non-NULL if found
+ * \retval NULL if not found
+ *
+ * \note The returned format has its reference count incremented. It must be
+ * dropped using ao2_ref or ao2_cleanup.
+ */
+struct ast_format *ast_format_cache_get(const char *name);
+
+#endif /* _AST_FORMAT_CACHE_H */
Propchange: team/group/media_formats/include/asterisk/format_cache.h
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: team/group/media_formats/include/asterisk/format_cache.h
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Propchange: team/group/media_formats/include/asterisk/format_cache.h
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: team/group/media_formats/main/asterisk.c
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats/main/asterisk.c?view=diff&rev=406151&r1=406150&r2=406151
==============================================================================
--- team/group/media_formats/main/asterisk.c (original)
+++ team/group/media_formats/main/asterisk.c Wed Jan 22 07:18:43 2014
@@ -249,6 +249,7 @@
#include "asterisk/security_events.h"
#include "asterisk/endpoints.h"
#include "asterisk/codec.h"
+#include "asterisk/format_cache.h"
#include "../defaults.h"
@@ -4325,6 +4326,11 @@
exit(1);
}
+ if (ast_format_cache_init()) {
+ printf("%s", term_quit());
+ exit(1);
+ }
+
if (ast_codec_builtin_init()) {
printf("%s", term_quit());
exit(1);
Added: team/group/media_formats/main/format_cache.c
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats/main/format_cache.c?view=auto&rev=406151
==============================================================================
--- team/group/media_formats/main/format_cache.c (added)
+++ team/group/media_formats/main/format_cache.c Wed Jan 22 07:18:43 2014
@@ -1,0 +1,177 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2014, Digium, Inc.
+ *
+ * Joshua Colp <jcolp at 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 at digium.com>
+ */
+
+/*** MODULEINFO
+ <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/logger.h"
+#include "asterisk/format_cache.h"
+#include "asterisk/astobj2.h"
+#include "asterisk/strings.h"
+
+/*! \brief Number of buckets to use for the media format cache (should be prime for performance reasons) */
+#define CACHE_BUCKETS 53
+
+/*! \brief Cached format structure */
+struct cached_format {
+ /*! \brief Pointer to the format */
+ struct ast_format *format;
+ /*! \brief Name of the cached format */
+ char name[0];
+};
+
+/*! \brief Cached formats */
+static struct ao2_container *formats;
+
+static int cached_format_hash(const void *obj, int flags)
+{
+ const struct cached_format *format;
+ const char *key;
+
+ switch (flags & OBJ_SEARCH_MASK) {
+ case OBJ_SEARCH_KEY:
+ key = obj;
+ return ast_str_hash(key);
+ case OBJ_SEARCH_OBJECT:
+ format = obj;
+ return ast_str_hash(format->name);
+ default:
+ /* Hash can only work on something with a full key. */
+ ast_assert(0);
+ return 0;
+ }
+}
+
+static int cached_format_cmp(void *obj, void *arg, int flags)
+{
+ const struct cached_format *left = obj;
+ const struct cached_format *right = arg;
+ const char *right_key = arg;
+ int cmp;
+
+ switch (flags & OBJ_SEARCH_MASK) {
+ case OBJ_SEARCH_OBJECT:
+ right_key = right->name;
+ /* Fall through */
+ case OBJ_SEARCH_KEY:
+ cmp = strcmp(left->name, right_key);
+ break;
+ case OBJ_SEARCH_PARTIAL_KEY:
+ cmp = strncmp(left->name, 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);
+}
+
+int ast_format_cache_init(void)
+{
+ formats = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, CACHE_BUCKETS,
+ cached_format_hash, cached_format_cmp);
+ if (!formats) {
+ return -1;
+ }
+
+ ast_register_atexit(format_cache_shutdown);
+
+ return 0;
+}
+
+/*! \brief Destructor for cached formats */
+static void cached_format_destroy(void *obj)
+{
+ struct cached_format *cached_format = obj;
+
+ ao2_cleanup(cached_format->format);
+}
+
+int ast_format_cache_add(const char *name, struct ast_format *format)
+{
+ SCOPED_AO2WRLOCK(lock, formats);
+ struct cached_format *cached_format;
+
+ if (ast_strlen_zero(name) || !format) {
+ return -1;
+ }
+
+ cached_format = ao2_find(formats, name, OBJ_SEARCH_KEY | OBJ_NOLOCK);
+ if (cached_format) {
+ ast_log(LOG_ERROR, "A cached format with name '%s' already exists\n", name);
+ ao2_ref(cached_format, -1);
+ return -1;
+ }
+
+ cached_format = ao2_alloc_options(sizeof(*cached_format) + strlen(name) + 1,
+ cached_format_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
+ if (!cached_format) {
+ ast_log(LOG_ERROR, "Could not create a cached format with name '%s'\n", name);
+ return -1;
+ }
+
+ /* This is a safe strcpy, there is enough room from the allocation above */
+ strcpy(cached_format->name, name);
+ cached_format->format = ao2_bump(format);
+
+ ao2_link(formats, cached_format);
+ ao2_ref(cached_format, -1);
+
+ ast_verb(2, "Created cached format with name '%s'\n", name);
+
+ return 0;
+}
+
+struct ast_format *ast_format_cache_get(const char *name)
+{
+ struct cached_format *cached_format;
+ struct ast_format *format;
+
+ cached_format = ao2_find(formats, name, OBJ_SEARCH_KEY);
+ if (!cached_format) {
+ return NULL;
+ }
+
+ format = ao2_bump(cached_format->format);
+ ao2_ref(cached_format, -1);
+
+ return format;
+}
Propchange: team/group/media_formats/main/format_cache.c
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: team/group/media_formats/main/format_cache.c
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Propchange: team/group/media_formats/main/format_cache.c
------------------------------------------------------------------------------
svn:mime-type = text/plain
More information about the asterisk-commits
mailing list