[asterisk-commits] file: branch file/media r167171 - in /team/file/media: include/asterisk/ main/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sun Jan 4 18:54:02 CST 2009


Author: file
Date: Sun Jan  4 18:54:01 2009
New Revision: 167171

URL: http://svn.digium.com/view/asterisk?view=rev&rev=167171
Log:
Add an implementation of media format lists. This uses an astobj2 reference counted object with a dynamic array inside. Will this be what is used in the end? We shall see...

Modified:
    team/file/media/include/asterisk/media.h
    team/file/media/main/media.c

Modified: team/file/media/include/asterisk/media.h
URL: http://svn.digium.com/view/asterisk/team/file/media/include/asterisk/media.h?view=diff&rev=167171&r1=167170&r2=167171
==============================================================================
--- team/file/media/include/asterisk/media.h (original)
+++ team/file/media/include/asterisk/media.h Sun Jan  4 18:54:01 2009
@@ -89,6 +89,9 @@
  * \brief Structure that represents a list of media formats
  */
 struct ast_media_format_list {
+	size_t size;                             /*!< Size of the dynamic array that contains the media formats */
+	size_t used;                             /*!< Number of media formats in the list */
+	struct ast_media_format **media_formats; /*!< Pointers to media formats in the list */
 };
 	
 /*! \brief Initialize core parts for media formats
@@ -122,6 +125,21 @@
  */
 struct ast_media_format *ast_media_format_find_by_name(const char *name);	
 
+/*! \brief Reference a media format
+ *
+ * \param media_format The media format structure to reference
+ *
+ * Example usage:
+ *
+ * \code
+ * ast_media_format_ref(media_format);
+ * \endcode
+ *
+ * This increases the reference count on the media format structure pointed to
+ * by media_format.
+ */
+struct ast_media_format *ast_media_format_ref(struct ast_media_format *media_format);	
+	
 /*! \brief Unreference a media format
  *
  * \param media_format The media format structure to unreference
@@ -137,9 +155,25 @@
  */
 struct ast_media_format *ast_media_format_unref(struct ast_media_format *media_format);	
 
-/*! \brief Initialize a media format list
- *
- * \param media_format_list The media format list structure to initialize
+/*! \brief Create a new media format lists structure
+ *
+ * \retval non-NULL on success
+ * \retval NULL on failure
+ *
+ * Example usage:
+ *
+ * \code
+ * struct ast_media_format_list *media_format_list = ast_media_format_list_new();
+ * \endcode
+ *
+ * This creates a new media format lists structure.
+ */
+struct ast_media_format_list *ast_media_format_list_new(void);
+	
+/*! \brief Add a media format to a media format list
+ *
+ * \param media_format_list List that the media format is to be added to
+ * \param media_format The media format structure to add
  *
  * \retval 0 on success
  * \retval -1 on failure
@@ -147,18 +181,18 @@
  * Example usage:
  *
  * \code
- * struct ast_media_format_list media_format_list;
- * ast_media_format_list_init(&media_format_list);
- * \endcode
- *
- * This initializes the defined media format list structure media_format_list.
- */
-int ast_media_format_list_init(struct ast_media_format_list *media_format_list);	
-
-/*! \brief Add a media format to a media format list
- *
- * \param media_format_list List that the media format is to be added to
- * \param media_format The media format structure to add
+ * ast_media_format_list_add(&media_format_list, media_format);
+ * \endcode
+ *
+ * This adds the media format pointed to by media_format to the media format list.
+ * It is added as the last element.
+ */
+int ast_media_format_list_add(struct ast_media_format_list *media_format_list, struct ast_media_format *media_format);	
+
+/*! \brief Remove a media format from a media format list
+ *
+ * \param media_format_list List to remove the media format from
+ * \param media_format The media format to remove
  *
  * \retval 0 on success
  * \retval -1 on failure
@@ -166,25 +200,6 @@
  * Example usage:
  *
  * \code
- * ast_media_format_list_add(&media_format_list, media_format);
- * \endcode
- *
- * This adds the media format pointed to by media_format to the media format list.
- * It is added as the last element.
- */
-int ast_media_format_list_add(struct ast_media_format_list *media_format_list, struct ast_media_format *media_format);	
-
-/*! \brief Remove a media format from a media format list
- *
- * \param media_format_list List to remove the media format from
- * \param media_format The media format to remove
- *
- * \retval 0 on success
- * \retval -1 on failure
- *
- * Example usage:
- *
- * \code
  * ast_media_format_list_remove(&media_format_list, media_format);
  * \endcode
  *
@@ -212,25 +227,36 @@
  *       ast_media_format_unref on it once done.
  */
 struct ast_media_format *ast_media_format_list_get(struct ast_media_format_list *media_format_list, unsigned int num);	
-	
-/*! \brief Destroy the contents of a media format list
- *
- * \param media_format_list List that is to be destroyed
- *
- * \retval 0 on success
- * \retval -1 on failure
- *
- * Example usage:
- *
- * \code
- * ast_media_format_list_destroy(&media_format_list);
- * \endcode
- *
- * This destroys the contents of the media format list.
- *
- * \note This does not destroy the actual media_format_list structure itself.
- */
-int ast_media_format_list_destroy(struct ast_media_format_list *media_format_list);	
+
+/*! \brief Reference a media format list
+ *
+ * \param media_format_list The media format list structure to reference
+ *
+ * Example usage:
+ *
+ * \code
+ * ast_media_format_list_ref(media_format_list);
+ * \endcode
+ *
+ * This increases the reference count on the media format list structure pointed to
+ * by media_format_list.
+ */
+struct ast_media_format_list *ast_media_format_list_ref(struct ast_media_format_list *media_format_list);
+
+/*! \brief Unreference a media format list
+ *
+ * \param media_format_list The media format List structure to unreference
+ *
+ * Example usage:
+ *
+ * \code
+ * media_format_list = ast_media_format_list_unref(media_format_list);
+ * \endcode
+ *
+ * This decrements the reference count on the media format list structure pointed to
+ * by media_format_list.
+ */
+struct ast_media_format_list *ast_media_format_list_unref(struct ast_media_format_list *media_format_list);
 	
 #if defined(__cplusplus) || defined(c_plusplus)
 }

Modified: team/file/media/main/media.c
URL: http://svn.digium.com/view/asterisk/team/file/media/main/media.c?view=diff&rev=167171&r1=167170&r2=167171
==============================================================================
--- team/file/media/main/media.c (original)
+++ team/file/media/main/media.c Sun Jan  4 18:54:01 2009
@@ -38,6 +38,9 @@
 /* Number of buckets to use for media formats */
 #define MAX_MEDIA_FORMATS_BUCKETS 53
 
+/* Rate at which the dynamic array for media format lists will grow */
+#define MEDIA_FORMAT_LIST_GROW_RATE 6
+
 /* Configuration file for user defined media formats */
 static const char config_file[] = "media_formats.conf";
 
@@ -80,35 +83,111 @@
 	return ao2_find(media_formats, &tmp, OBJ_POINTER);
 }
 
+struct ast_media_format *ast_media_format_ref(struct ast_media_format *media_format)
+{
+	ao2_ref(media_format, +1);
+	return media_format;
+}
+
 struct ast_media_format *ast_media_format_unref(struct ast_media_format *media_format)
 {
 	ao2_ref(media_format, -1);
 	return NULL;
 }
 
-int ast_media_format_list_init(struct ast_media_format_list *media_format_list)
-{
-	return -1;
+/*! \brief Destroy function used for media format lists */
+static void media_format_list_destroy_cb(void *obj)
+{
+	struct ast_media_format_list *media_format_list = obj;
+	int i;
+	
+	for (i = 0; i < media_format_list->used; i++) {
+		ast_media_format_unref(media_format_list->media_formats[i]);
+	}
+
+	ast_free(media_format_list->media_formats);
+	
+	return;
+}
+
+struct ast_media_format_list *ast_media_format_list_new(void)
+{
+	struct ast_media_format_list *media_format_list;
+
+	if (!(media_format_list = ao2_alloc(sizeof(*media_format_list), media_format_list_destroy_cb))) {
+		return NULL;
+	}
+
+	if (!(media_format_list->media_formats = ast_calloc(MEDIA_FORMAT_LIST_GROW_RATE, sizeof(struct ast_media_format*)))) {
+		return ast_media_format_list_unref(media_format_list);
+	}
+	
+	media_format_list->size = MEDIA_FORMAT_LIST_GROW_RATE;
+	
+	return media_format_list;
 }
 
 int ast_media_format_list_add(struct ast_media_format_list *media_format_list, struct ast_media_format *media_format)
 {
-	return -1;
+	if (media_format_list->used == media_format_list->size) {
+		struct ast_media_format **tmp;
+		media_format_list->size += MEDIA_FORMAT_LIST_GROW_RATE;
+		tmp = ast_realloc(media_format_list->media_formats, media_format_list->size * sizeof(struct ast_media_format*));
+		media_format_list->media_formats = tmp;
+	}
+
+	media_format_list->media_formats[media_format_list->used++] = ast_media_format_ref(media_format);
+	
+	return 0;
 }
 
 int ast_media_format_list_remove(struct ast_media_format_list *media_format_list, struct ast_media_format *media_format)
 {
-	return -1;
+	int i;
+	
+	if (!media_format_list->used) {
+		return -1;
+	}
+
+	for (i = 0; i < media_format_list->used; i++) {
+		if (media_format_list->media_formats[i] == media_format) {
+			if (i < media_format_list->size) {
+				memmove(media_format_list->media_formats + i, media_format_list->media_formats + i + 1, sizeof(media_format_list->media_formats) - 1);
+			}
+			media_format_list->used--;
+			ast_media_format_unref(media_format);
+			break;
+		}
+	}
+	
+	return 0;
 }
 
 struct ast_media_format *ast_media_format_list_get(struct ast_media_format_list *media_format_list, unsigned int num)
 {
+	struct ast_media_format *media_format;
+	
+	if (!media_format_list->used || num >= media_format_list->used) {
+		return NULL;
+	}
+
+	if ((media_format = media_format_list->media_formats[num])) {
+		ast_media_format_ref(media_format);
+	}
+	
+	return media_format;
+}
+
+struct ast_media_format_list *ast_media_format_list_ref(struct ast_media_format_list *media_format_list)
+{
+	ao2_ref(media_format_list, +1);
+	return media_format_list;
+}
+
+struct ast_media_format_list *ast_media_format_list_unref(struct ast_media_format_list *media_format_list)
+{
+	ao2_ref(media_format_list, -1);
 	return NULL;
-}
-
-int ast_media_format_list_destroy(struct ast_media_format_list *media_format_list)
-{
-	return -1;
 }
 
 /*! \brief Conversion function which takes a media format enum and turns it into a string */
@@ -283,7 +362,7 @@
 		ao2_link(media_formats, media_format);
 	}
 
-	ao2_ref(media_format, -1);
+	ast_media_format_unref(media_format);
 
 	return;
 }
@@ -367,7 +446,7 @@
 		ast_cli(a->fd, FORMAT2, media_format->name, media_format_type_enum2str(media_format->type),
 			ast_strlen_zero(media_format->codec) ? "None" : media_format->codec,
 			media_format->channels);
-		ao2_ref(media_format, -1);
+		ast_media_format_unref(media_format);
 	}
 
 	ast_cli(a->fd, "%d media formats configured\n", ao2_container_count(media_formats));
@@ -409,7 +488,7 @@
 	ast_cli(a->fd, "Codec: %s\n", media_format->codec);
 	ast_cli(a->fd, "Channels: %d\n", media_format->channels);
 	
-	ao2_ref(media_format, -1);
+	ast_media_format_unref(media_format);
 	
 	return CLI_SUCCESS;
 }




More information about the asterisk-commits mailing list