diff --git a/include/asterisk/format_cache.h b/include/asterisk/format_cache.h index 5a2add69842c8a2b32e549bfca9cfee20be27cf3..7dc0276de4fd4e4d2cb056e56641845d55591ed8 100644 --- a/include/asterisk/format_cache.h +++ b/include/asterisk/format_cache.h @@ -314,4 +314,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 */ diff --git a/main/format_cache.c b/main/format_cache.c index fbe659f9604a98d37d6b6f98444200b1dc20e347..3ce8ee02bc88825c8431e04cb610870a1f153b6a 100644 --- a/main/format_cache.c +++ b/main/format_cache.c @@ -555,3 +555,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; +} diff --git a/main/format_cap.c b/main/format_cap.c index a97fd6d112d93b67712bd841fe8031f92f4fcc9d..4dc8e491471a0914dcde74f83bb1df76d86416c5 100644 --- a/main/format_cap.c +++ b/main/format_cap.c @@ -232,7 +232,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);