[Asterisk-code-review] Add data buffer API to store packets. (asterisk[15])

Benjamin Keith Ford asteriskteam at digium.com
Mon Mar 19 09:46:05 CDT 2018


Benjamin Keith Ford has uploaded this change for review. ( https://gerrit.asterisk.org/8604


Change subject: Add data buffer API to store packets.
......................................................................

Add data buffer API to store packets.

Adds a data buffer with a configurable size that can store different
kinds of packets (like RTP packets for retransmission). Given a number
it will store a data packet at that position relative to the others.
Given a number it will retrieve the given data packet if it is present.
This is purposely a storage of arbitrary things so it can be used not
just for RTP packets but also Asterisk frames in the future if needed.
The API does not internally use a lock, so it will be up to the user of
the API to properly protect the data buffer.

For more information, refer to the wiki page:
https://wiki.asterisk.org/wiki/display/AST/WebRTC+User+Experience+Improvements

Change-Id: Iff13c5d4795d52356959fe2a57360cd57dfade07
---
A include/asterisk/data_buffer.h
A main/data_buffer.c
2 files changed, 471 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/04/8604/1

diff --git a/include/asterisk/data_buffer.h b/include/asterisk/data_buffer.h
new file mode 100644
index 0000000..119cdbf
--- /dev/null
+++ b/include/asterisk/data_buffer.h
@@ -0,0 +1,157 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2018, 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 Data Buffer API
+ *
+ * \author Joshua Colp <jcolp at digium.com>
+ */
+
+#ifndef _AST_DATA_BUFFER_H_
+#define _AST_DATA_BUFFER_H_
+
+/*!
+ * \brief The number of cached payloads a data buffer starts out with
+ */
+#define CACHED_PAYLOADS_START 2
+
+/*!
+ * \brief A buffer of data payloads.
+ */
+struct ast_data_buffer;
+
+/*!
+ * \brief A callback function to free a data payload in a data buffer
+ *
+ * \param The data payload
+ */
+typedef void (*ast_data_buffer_free_callback)(void *data);
+
+/*!
+ * \brief Allocate a data buffer
+ *
+ * \param free_fn Callback function to free a data payload
+ * \param size The maximum number of data payloads to contain in the data buffer
+ *
+ * \retval non-NULL success
+ * \retval NULL failure
+ *
+ * \since 15.4.0
+ */
+struct ast_data_buffer *ast_data_buffer_alloc(ast_data_buffer_free_callback free_fn, size_t size);
+
+/*!
+ * \brief Sets the data buffer's cache to its max payload count
+ *
+ * \param buffer The data buffer
+ *
+ * \since 15.4.0
+ */
+void ast_data_buffer_cache_adjust(struct ast_data_buffer *buffer);
+
+/*!
+ * \brief Resize a data buffer
+ *
+ * \param buffer The data buffer
+ * \param size The new maximum size of the data buffer
+ *
+ * \note If the data buffer is shrunk any old data paylods will be freed using the configured callback.
+ * The data buffer is flexible and can be used for multiple purposes. Therefore it is up to the
+ * caller of the function to know whether or not a buffer should have its size changed. Increasing
+ * the size of the buffer may make sense in some scenarios, but shrinking should always be handled
+ * with caution since data can be lost.
+ *
+ * \since 15.4.0
+ */
+void ast_data_buffer_resize(struct ast_data_buffer *buffer, size_t size);
+
+/*!
+ * \brief Place a data payload at a position in the data buffer
+ *
+ * \param buffer The data buffer
+ * \param pos The position of the data payload
+ * \param payload The data payload
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ *
+ * \note It is up to the consumer of this API to ensure proper memory management of data payloads
+ *
+ * \since 15.4.0
+ */
+int ast_data_buffer_put(struct ast_data_buffer *buffer, int pos, void *payload);
+
+/*!
+ * \brief Retrieve a data payload from the data buffer
+ *
+ * \param buffer The data buffer
+ * \param pos The position of the data payload (-1 to get the HEAD data payload)
+ *
+ * \retval non-NULL success
+ * \retval NULL failure
+ *
+ * \note This does not remove the data payload from the data buffer. It will be removed when it is displaced.
+ *
+ * \since 15.4.0
+ */
+void *ast_data_buffer_get(const struct ast_data_buffer *buffer, int pos);
+
+/*!
+ * \brief Free a data buffer (and all held data payloads)
+ *
+ * \param buffer The data buffer
+ *
+ * \since 15.4.0
+ */
+void ast_data_buffer_free(struct ast_data_buffer *buffer);
+
+/*!
+ * \brief Return the number of payloads in a data buffer
+ *
+ * \param buffer The data buffer
+ *
+ * \retval the number of data payloads
+ *
+ * \since 15.4.0
+ */
+size_t ast_data_buffer_count(const struct ast_data_buffer *buffer);
+
+/*!
+ * \brief Return the maximum number of payloads a data buffer can hold
+ *
+ * \param buffer The data buffer
+ *
+ * \retval the maximum number of data payloads
+ *
+ * \since 15.4.0
+ */
+size_t ast_data_buffer_max(const struct ast_data_buffer *buffer);
+
+/*!
+ * \brief Return the number of cached payloads in a data buffer
+ *
+ * \param buffer The data buffer
+ *
+ * \retval the number of cached data payloads
+ *
+ * \since 15.4.0
+ */
+size_t ast_data_buffer_cache_count(const struct ast_data_buffer *buffer);
+
+#endif /* _AST_DATA_BUFFER_H */
diff --git a/main/data_buffer.c b/main/data_buffer.c
new file mode 100644
index 0000000..b06dd4b
--- /dev/null
+++ b/main/data_buffer.c
@@ -0,0 +1,314 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2018, 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 Data Buffer API
+ *
+ * \author Joshua Colp <jcolp at digium.com>
+ */
+
+/*** MODULEINFO
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include "asterisk/logger.h"
+#include "asterisk/strings.h"
+#include "asterisk/data_buffer.h"
+#include "asterisk/linkedlists.h"
+
+/*!
+ * \brief Payload placed inside of the data buffer vector
+ */
+struct data_buffer_payload {
+	/*! \brief The payload for this position */
+	void *payload;
+	/*! \brief The provided position for this */
+	int pos;
+	/*! \brief Linked list information */
+	AST_LIST_ENTRY(data_buffer_payload) list;
+};
+
+/*!
+ * \brief Data buffer containing fixed number of data payloads
+ */
+struct ast_data_buffer {
+    /*! \brief Callback function to free a data payload */
+    ast_data_buffer_free_callback free_fn;
+    /*! \brief A linked list of data payloads */
+    AST_LIST_HEAD_NOLOCK(, data_buffer_payload) payloads;
+    /*! \brief A linked list of unused cached data payloads */
+    AST_LIST_HEAD_NOLOCK(, data_buffer_payload) cached_payloads;
+    /*! \brief The current number of data payloads in the buffer */
+    size_t count;
+    /*! \brief Maximum number of data payloads in the buffer */
+    size_t max;
+    /*! \brief The current number of data payloads in the cache */
+    size_t cache_count;
+};
+
+/*!
+ * \brief Helper function to allocate a data payload
+ */
+static struct data_buffer_payload *data_buffer_payload_alloc(void *payload, int pos)
+{
+	struct data_buffer_payload *data_payload;
+
+	data_payload = ast_calloc(1, sizeof(*data_payload));
+	if (!data_payload) {
+		return NULL;
+	}
+
+	data_payload->payload = payload;
+	data_payload->pos = pos;
+
+	return data_payload;
+}
+
+void ast_data_buffer_cache_adjust(struct ast_data_buffer *buffer)
+{
+	int difference;
+
+	ast_assert(buffer != NULL);
+
+	/* How many payloads to add or remove from the cache. We find the max number of
+	 * payloads the cache can hold (buffer->max - buffer->count) and then subtract
+	 * the number of payloads currently in the cache.
+	 */
+	difference = (buffer->max - buffer->count) - buffer->cache_count;
+
+	if (difference == 0) {
+		return;
+	}
+
+	if (difference > 0) {
+		/* Add payloads to the cache */
+		while (difference) {
+			struct data_buffer_payload *buffer_payload;
+
+			buffer_payload = data_buffer_payload_alloc(NULL, -1);
+			if (buffer_payload) {
+				AST_LIST_INSERT_TAIL(&buffer->cached_payloads, buffer_payload, list);
+				buffer->cache_count++;
+			}
+
+			difference--;
+		}
+	} else if (difference < 0) {
+		/* Remove payloads from the cache */
+		while (difference) {
+			struct data_buffer_payload *buffer_payload;
+
+			buffer_payload = AST_LIST_REMOVE_HEAD(&buffer->cached_payloads, list);
+			if (buffer_payload) {
+				ast_free(buffer_payload);
+				buffer->cache_count--;
+			}
+
+			difference++;
+		}
+	}
+
+}
+
+struct ast_data_buffer *ast_data_buffer_alloc(ast_data_buffer_free_callback free_fn, size_t size)
+{
+	struct ast_data_buffer *buffer;
+	int i;
+
+	ast_assert(free_fn != NULL);
+	ast_assert(size != 0);
+
+	buffer = ast_calloc(1, sizeof(*buffer));
+	if (!buffer) {
+		return NULL;
+	}
+
+	AST_LIST_HEAD_INIT_NOLOCK(&buffer->payloads);
+	AST_LIST_HEAD_INIT_NOLOCK(&buffer->cached_payloads);
+
+	buffer->free_fn = free_fn;
+	buffer->max = size;
+
+	for (i = 0; i < CACHED_PAYLOADS_START; ++i) {
+		struct data_buffer_payload *buffer_payload;
+
+		/* We don't treat this as fatal since we will try again when a put happens */
+		buffer_payload = data_buffer_payload_alloc(NULL, -1);
+		if (buffer_payload) {
+			AST_LIST_INSERT_TAIL(&buffer->cached_payloads, buffer_payload, list);
+			buffer->cache_count++;
+		}
+	}
+
+	return buffer;
+}
+
+void ast_data_buffer_resize(struct ast_data_buffer *buffer, size_t size)
+{
+	struct data_buffer_payload *existing_payload;
+	int node = 1;
+
+	ast_assert(buffer != NULL);
+
+	/* The buffer must have at least a size of 1 */
+	if (size < 1) {
+		return;
+	}
+
+	if (buffer->max == size) {
+		return;
+	}
+
+	/* If the size is decreasing, some payloads will need to be freed */
+	if (buffer->max > size) {
+		AST_LIST_TRAVERSE_SAFE_BEGIN(&buffer->payloads, existing_payload, list) {
+			if (node > size) {
+				AST_LIST_REMOVE_CURRENT(list);
+				buffer->free_fn(existing_payload->payload);
+				ast_free(existing_payload);
+				buffer->count--;
+			}
+			node++;
+		}
+		AST_LIST_TRAVERSE_SAFE_END;
+	}
+
+	buffer->max = size;
+	ast_data_buffer_cache_adjust(buffer);
+}
+
+int ast_data_buffer_put(struct ast_data_buffer *buffer, int pos, void *payload)
+{
+	struct data_buffer_payload *buffer_payload = NULL;
+	struct data_buffer_payload *existing_payload;
+	int inserted = 0;
+
+	ast_assert(buffer != NULL);
+	ast_assert(payload != NULL);
+
+	/* If the data buffer has reached its maximum size then the head goes away and
+	 * we will reuse its buffer payload
+	 */
+	if (buffer->count == buffer->max) {
+		buffer_payload = AST_LIST_REMOVE_HEAD(&buffer->payloads, list);
+		buffer->free_fn(buffer_payload->payload);
+		buffer->count--;
+
+		/* Update this buffer payload with its new information */
+		buffer_payload->payload = payload;
+		buffer_payload->pos = pos;
+	}
+	if (!buffer_payload && buffer->cache_count) {
+		buffer_payload = AST_LIST_REMOVE_HEAD(&buffer->cached_payloads, list);
+		buffer->cache_count--;
+
+		/* Update the payload from the cache with its new information */
+		buffer_payload->payload = payload;
+		buffer_payload->pos = pos;
+	}
+	if (!buffer_payload) {
+		ast_data_buffer_cache_adjust(buffer);
+		buffer_payload = data_buffer_payload_alloc(payload, pos);
+		if (!buffer_payload) {
+			return -1;
+		}
+	}
+
+	/* Given the position find its ideal spot within the buffer */
+	AST_LIST_TRAVERSE_SAFE_BEGIN(&buffer->payloads, existing_payload, list) {
+		if (existing_payload->pos > pos) {
+			AST_LIST_INSERT_BEFORE_CURRENT(buffer_payload, list);
+			inserted = 1;
+			break;
+		}
+	}
+	AST_LIST_TRAVERSE_SAFE_END;
+
+	if (!inserted) {
+		AST_LIST_INSERT_TAIL(&buffer->payloads, buffer_payload, list);
+	}
+
+	buffer->count++;
+
+	return 0;
+}
+
+void *ast_data_buffer_get(const struct ast_data_buffer *buffer, int pos)
+{
+	struct data_buffer_payload *buffer_payload;
+
+	ast_assert(buffer != NULL);
+
+	if (pos == -1) {
+		buffer_payload = AST_LIST_FIRST(&buffer->payloads);
+
+		if (buffer_payload) {
+			return buffer_payload->payload;
+		}
+	} else {
+		AST_LIST_TRAVERSE(&buffer->payloads, buffer_payload, list) {
+			if (buffer_payload->pos == pos) {
+				return buffer_payload->payload;
+			}
+		}
+	}
+
+	return NULL;
+}
+
+void ast_data_buffer_free(struct ast_data_buffer *buffer)
+{
+	struct data_buffer_payload *buffer_payload;
+
+	ast_assert(buffer != NULL);
+
+	while ((buffer_payload = AST_LIST_REMOVE_HEAD(&buffer->payloads, list))) {
+		buffer->free_fn(buffer_payload->payload);
+		ast_free(buffer_payload);
+	}
+
+	while ((buffer_payload = AST_LIST_REMOVE_HEAD(&buffer->cached_payloads, list))) {
+		ast_free(buffer_payload);
+	}
+
+	ast_free(buffer);
+}
+
+size_t ast_data_buffer_count(const struct ast_data_buffer *buffer)
+{
+	ast_assert(buffer != NULL);
+
+	return buffer->count;
+}
+
+size_t ast_data_buffer_max(const struct ast_data_buffer *buffer)
+{
+	ast_assert(buffer != NULL);
+
+	return buffer->max;
+}
+
+size_t ast_data_buffer_cache_count(const struct ast_data_buffer *buffer)
+{
+	ast_assert(buffer != NULL);
+
+	return buffer->cache_count;
+}

-- 
To view, visit https://gerrit.asterisk.org/8604
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 15
Gerrit-MessageType: newchange
Gerrit-Change-Id: Iff13c5d4795d52356959fe2a57360cd57dfade07
Gerrit-Change-Number: 8604
Gerrit-PatchSet: 1
Gerrit-Owner: Benjamin Keith Ford <bford at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180319/e686adb6/attachment-0001.html>


More information about the asterisk-code-review mailing list