]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
format_cap: Perform codec lookups by pointer instead of name
authorSean Bright <sean.bright@gmail.com>
Mon, 14 Sep 2020 18:23:27 +0000 (14:23 -0400)
committerSean Bright <sean.bright@gmail.com>
Mon, 14 Sep 2020 19:02:40 +0000 (15:02 -0400)
ASTERISK-28416 #close

Change-Id: I069420875ebdbcaada52d92599a5f7de3cb2cdf4

include/asterisk/format_cache.h
main/format_cache.c
main/format_cap.c

index d716cea6c6f44bbdc3bd7940f717bf79cb257029..50f3856e0c63a3273cf3535265fbcd88682c64b4 100644 (file)
@@ -306,4 +306,17 @@ struct ast_format *ast_format_cache_get_slin_by_rate(unsigned int rate);
  */
 int ast_format_cache_is_slinear(struct ast_format *format);
 
+/*!
+ * \brief Retrieve a format from the cache by its codec
+ *
+ * \param codec The codec to search by
+ *
+ * \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_by_codec(const struct ast_codec *codec);
+
 #endif /* _AST_FORMAT_CACHE_H */
index e6277ee11ef6994c84e0316df0f0909f5e464e35..3f73c610d0698a4231f2f96037a6e847517f31e1 100644 (file)
@@ -541,3 +541,24 @@ int ast_format_cache_is_slinear(struct ast_format *format)
 
        return 0;
 }
+
+struct ast_format *ast_format_cache_get_by_codec(const struct ast_codec *codec)
+{
+       struct ast_format *format;
+       struct ao2_iterator it;
+
+       for (it = ao2_iterator_init(formats, 0);
+                (format = ao2_iterator_next(&it));
+                ao2_ref(format, -1)) {
+               struct ast_codec *candidate = ast_format_get_codec(format);
+               if (codec == candidate) {
+                       ao2_cleanup(candidate);
+                       ao2_iterator_destroy(&it);
+                       return format;
+               }
+               ao2_cleanup(candidate);
+       }
+
+       ao2_iterator_destroy(&it);
+       return NULL;
+}
index 86e8e1ff0b4b94b88e1b13c8a721924a3a620142..2558c4a142af7dc62702408db7c737e1f8a17fdd 100644 (file)
@@ -268,7 +268,7 @@ int ast_format_cap_append_by_type(struct ast_format_cap *cap, enum ast_media_typ
                        continue;
                }
 
-               format = ast_format_cache_get(codec->name);
+               format = ast_format_cache_get_by_codec(codec);
 
                if (format == ast_format_none) {
                        ao2_ref(format, -1);