[asterisk-commits] kharwell: branch kharwell/media_formats_translation_core r413143 - in /team/k...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Wed Apr 30 15:55:00 CDT 2014
Author: kharwell
Date: Wed Apr 30 15:54:56 2014
New Revision: 413143
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=413143
Log:
changes to review
Modified:
team/kharwell/media_formats_translation_core/include/asterisk/codec.h
team/kharwell/media_formats_translation_core/include/asterisk/format.h
team/kharwell/media_formats_translation_core/main/codec.c
team/kharwell/media_formats_translation_core/main/format.c
team/kharwell/media_formats_translation_core/main/translate.c
Modified: team/kharwell/media_formats_translation_core/include/asterisk/codec.h
URL: http://svnview.digium.com/svn/asterisk/team/kharwell/media_formats_translation_core/include/asterisk/codec.h?view=diff&rev=413143&r1=413142&r2=413143
==============================================================================
--- team/kharwell/media_formats_translation_core/include/asterisk/codec.h (original)
+++ team/kharwell/media_formats_translation_core/include/asterisk/codec.h Wed Apr 30 15:54:56 2014
@@ -179,4 +179,15 @@
*/
unsigned int ast_codec_determine_length(const struct ast_codec *codec, unsigned int samples);
+/*!
+ * \brief Get the names of all codecs, separated by a '|', matching the given
+ * codec's original id.
+ *
+ * \param codec The codec to match against
+ * \param buf the string buffer to write names to
+ *
+ * \retval the given string buffer.
+ */
+struct ast_str **ast_codec_get_names(const struct ast_codec *codec, struct ast_str **buf);
+
#endif /* _AST_CODEC_H */
Modified: team/kharwell/media_formats_translation_core/include/asterisk/format.h
URL: http://svnview.digium.com/svn/asterisk/team/kharwell/media_formats_translation_core/include/asterisk/format.h?view=diff&rev=413143&r1=413142&r2=413143
==============================================================================
--- team/kharwell/media_formats_translation_core/include/asterisk/format.h (original)
+++ team/kharwell/media_formats_translation_core/include/asterisk/format.h Wed Apr 30 15:54:56 2014
@@ -137,6 +137,14 @@
*/
enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2);
+/*!
+ * \brief Compare the format's codec to the given codec.
+ *
+ * \retval ast_format_cmp_res representing the result of comparing format->codec and codec
+ */
+enum ast_format_cmp_res ast_format_cmp_codec(
+ const struct ast_format *format, const struct ast_codec *codec);
+
/*1
* \brief Get a common joint capability between two formats
*
Modified: team/kharwell/media_formats_translation_core/main/codec.c
URL: http://svnview.digium.com/svn/asterisk/team/kharwell/media_formats_translation_core/main/codec.c?view=diff&rev=413143&r1=413142&r2=413143
==============================================================================
--- team/kharwell/media_formats_translation_core/main/codec.c (original)
+++ team/kharwell/media_formats_translation_core/main/codec.c Wed Apr 30 15:54:56 2014
@@ -379,28 +379,3 @@
}
return buf;
}
-
-struct ast_codec *ast_codec_complete(
- const char *name, enum ast_media_type type, int state)
-{
- struct ao2_iterator i;
- struct ast_codec *obj;
- int which = 0;
- int size = strlen(name);
- char *res = NULL;
-
- ao2_rdlock(codecs);
- i = ao2_iterator_init(codecs, AO2_ITERATOR_DONTLOCK);
- for (; (obj = ao2_iterator_next(&i)); ao2_ref(obj, -1)) {
- if (obj->type != type) {
- continue;
- }
- if (!strncasecmp(name, obj->name, size) && ++which > state) {
- res = ast_strdup(obj->name);
- break;
- }
- }
- ao2_iterator_destroy(&i);
- ao2_unlock(codecs);
- return res;
-}
Modified: team/kharwell/media_formats_translation_core/main/format.c
URL: http://svnview.digium.com/svn/asterisk/team/kharwell/media_formats_translation_core/main/format.c?view=diff&rev=413143&r1=413142&r2=413143
==============================================================================
--- team/kharwell/media_formats_translation_core/main/format.c (original)
+++ team/kharwell/media_formats_translation_core/main/format.c Wed Apr 30 15:54:56 2014
@@ -205,6 +205,11 @@
return AST_FORMAT_CMP_EQUAL;
}
+enum ast_format_cmp_res ast_format_cmp_codec(const struct ast_format *format1, const struct ast_codec *codec)
+{
+ return format->codec == codec ? AST_FORMAT_CMP_EQUAL : AST_FORMAT_CMP_NOT_EQUAL;
+}
+
struct ast_format *ast_format_joint(const struct ast_format *format1, const struct ast_format *format2)
{
if (format1->codec != format2->codec) {
Modified: team/kharwell/media_formats_translation_core/main/translate.c
URL: http://svnview.digium.com/svn/asterisk/team/kharwell/media_formats_translation_core/main/translate.c?view=diff&rev=413143&r1=413142&r2=413143
==============================================================================
--- team/kharwell/media_formats_translation_core/main/translate.c (original)
+++ team/kharwell/media_formats_translation_core/main/translate.c Wed Apr 30 15:54:56 2014
@@ -43,6 +43,7 @@
#include "asterisk/sched.h"
#include "asterisk/cli.h"
#include "asterisk/term.h"
+#include "asterisk/format.h"
/*! \todo
* TODO: sample frames for each supported input format.
@@ -99,15 +100,15 @@
/*!
* \internal
- * \brief converts codec to index value.
- */
-static int codec2index(struct ast_codec *codec)
+ * \brief converts codec id to index value.
+ */
+static int codec_to_index(unsigned int id)
{
int x;
ast_rwlock_rdlock(&tablelock);
for (x = 0; x < cur_max_index; x++) {
- if (__indextable[x] == codec->id) {
+ if (__indextable[x] == id) {
/* format already exists in index2format table */
ast_rwlock_unlock(&tablelock);
return x;
@@ -119,11 +120,20 @@
/*!
* \internal
+ * \brief converts codec to index value.
+ */
+static int codec2index(struct ast_codec *codec)
+{
+ return codec_to_index(codec->id);
+}
+
+/*!
+ * \internal
* \brief converts format to codec index value.
*/
static int format2index(struct ast_format *format)
{
- return codec2index(ast_format_get_codec_id(format));
+ return codec_to_index(ast_format_get_codec_id(format));
}
/*!
@@ -147,7 +157,7 @@
ast_rwlock_unlock(&tablelock);
return -1; /* hit max length */
}
- __indextable[cur_max_index] = codec;
+ __indextable[cur_max_index] = codec->id;
cur_max_index++;
ast_rwlock_unlock(&tablelock);
@@ -185,7 +195,7 @@
static int matrix_resize(int init)
{
struct translator_path **tmp_matrix = NULL;
- int *tmp_table = NULL;
+ unsigned int *tmp_table = NULL;
int old_index;
int x;
@@ -286,7 +296,7 @@
* \brief Allocate the descriptor, required outbuf space,
* and possibly desc.
*/
-static void *newpvt(struct ast_translator *t, const struct ast_format *explicit_dst)
+static void *newpvt(struct ast_translator *t, struct ast_format *explicit_dst)
{
struct ast_trans_pvt *pvt;
int len;
@@ -384,13 +394,15 @@
struct ast_frame *ast_trans_frameout(struct ast_trans_pvt *pvt,
int datalen, int samples)
{
+ struct ast_codec *codec;
struct ast_frame *f = &pvt->f;
if (samples) {
f->samples = samples;
} else {
- if (pvt->samples == 0)
+ if (pvt->samples == 0) {
return NULL;
+ }
f->samples = pvt->samples;
pvt->samples = 0;
}
@@ -401,11 +413,16 @@
pvt->datalen = 0;
}
+
+ if (!(codec = ast_codec_get(pvt->t->dst_codec.name,
+ pvt->t->dst_codec.type,
+ pvt->t->dst_codec.sample_rate))) {
+ return NULL;
+ }
+ f->subclass.format = ast_format_create(codec);
+ ao2_ref(codec, -1);
+
f->frametype = AST_FRAME_VOICE;
- f->subclass.format = ast_format_create(
- ast_codec_get(pvt->t->dst_codec->name,
- pvt->t->dst_codec->type,
- pvt->t->dst_codec->sample_rate));
f->mallocd = 0;
f->offset = AST_FRIENDLY_OFFSET;
f->src = pvt->t->name;
@@ -584,7 +601,7 @@
struct rusage start;
struct rusage end;
int cost;
- int out_rate = t->dst_codec->sample_rate;
+ int out_rate = t->dst_codec.sample_rate;
if (!seconds) {
seconds = 1;
@@ -655,23 +672,23 @@
* \retval Table Cost value greater than 0.
* \retval 0 on error.
*/
-static int generate_table_cost(struct ast_format *src, struct ast_format *dst)
-{
- int src_rate = ast_format_get_sample_rate(src);
+static int generate_table_cost(struct ast_codec *src, struct ast_codec *dst)
+{
+ int src_rate = src->sample_rate;
int src_ll = 0;
- int dst_rate = ast_format_get_sample_rate(dst);
+ int dst_rate = dst->sample_rate;
int dst_ll = 0;
- if ((ast_format_get_type(src) != AST_MEDIA_TYPE_AUDIO) ||
- (ast_format_get_type(dst) != AST_MEDIA_TYPE_AUDIO)) {
+ if ((src->type != AST_MEDIA_TYPE_AUDIO) ||
+ (dst->type != AST_MEDIA_TYPE_AUDIO)) {
/* This method of generating table cost is limited to audio.
* Translators for media other than audio must manually set their
* table cost. */
return 0;
}
- src_ll = ast_format_cmp(src, ast_format_slin) == AST_FORMAT_CMP_EQUAL;
- dst_ll = ast_format_cmp(dst, ast_format_slin) == AST_FORMAT_CMP_EQUAL;
+ src_ll = ast_format_cmp_codec(ast_format_slin, src) == AST_FORMAT_CMP_EQUAL;
+ dst_ll = ast_format_cmp_codec(ast_format_slin, dst) == AST_FORMAT_CMP_EQUAL;
if (src_ll) {
if (dst_ll && (src_rate == dst_rate)) {
return AST_TRANS_COST_LL_LL_ORIGSAMP;
@@ -803,16 +820,14 @@
const char *ast_translate_path_to_str(struct ast_trans_pvt *p, struct ast_str **str)
{
- struct ast_trans_pvt *pn = p;
-
if (!p || !p->t) {
return "";
}
- ast_codec_get_names(p->t->src_codec, str);
+ ast_codec_get_names(&p->t->src_codec, str);
while (p) {
ast_str_append(str, 0, "->");
- ast_codec_get_names(p->t->dst_codec, str);
+ ast_codec_get_names(&p->t->dst_codec, str);
p = p->next;
}
@@ -821,7 +836,24 @@
static char *complete_trans_path_choice(const char *line, const char *word, int pos, int state)
{
- return ast_codec_complete(word, AST_MEDIA_TYPE_AUDIO, state);
+ int i = 1, which = 0;
+ int wordlen = strlen(word);
+ struct ast_codec *codec;
+
+ while ((codec = ast_codec_get_by_id(i))) {
+ ++i;
+ if (codec->type != AST_MEDIA_TYPE_AUDIO) {
+ ao2_ref(codec, -1);
+ continue;
+ }
+ if (!strncasecmp(word, codec->name, wordlen) && ++which > state) {
+ char *res = ast_strdup(codec->name);
+ ao2_ref(codec, -1);
+ return res;
+ }
+ ao2_ref(codec, -1);
+ }
+ return NULL;
}
static void handle_cli_recalc(struct ast_cli_args *a)
@@ -846,7 +878,7 @@
static char *handle_show_translation_table(struct ast_cli_args *a)
{
int x, y, i, k;
- int longest, num_codecs, curlen = 0;
+ int longest, num_codecs = 0, curlen = 0;
struct ast_str *out = ast_str_create(1024);
struct ast_codec *codec;
@@ -867,7 +899,7 @@
ast_cli(a->fd, " Source Format (Rows) Destination Format (Columns)\n\n");
for (i = 0; i < num_codecs; i++) {
- struct ast_codec *row = i ? ast_codec_get_by_id(i) : NULL
+ struct ast_codec *row = i ? ast_codec_get_by_id(i) : NULL;
x = -1;
if ((i > 0) && (row->type != AST_MEDIA_TYPE_AUDIO) &&
@@ -919,7 +951,6 @@
}
ast_free(out);
AST_RWLIST_UNLOCK(&translators);
- ast_format_list_destroy(f_list);
return CLI_SUCCESS;
}
@@ -929,7 +960,7 @@
struct ast_str *str = ast_str_alloca(1024);
struct ast_translator *step;
struct ast_codec *dst_codec, *src_codec = ast_codec_get(
- a->argv[4], AST_FORMAT_TYPE_AUDIO, 0);
+ a->argv[4], AST_MEDIA_TYPE_AUDIO, 0);
if (!src_codec) {
ast_cli(a->fd, "Source codec \"%s\" is not found.\n", a->argv[4]);
@@ -940,7 +971,7 @@
ast_cli(a->fd, "--- Translation paths SRC Codec \"%s\" sample rate %d ---\n",
a->argv[4], src_codec->sample_rate);
- while (dst_codec = ast_codec_get_by_id(i)) {
+ while ((dst_codec = ast_codec_get_by_id(i))) {
int src, dst;
++i;
@@ -956,8 +987,8 @@
step = matrix_get(src, dst)->step;
ast_str_reset(str);
- if ((len >= cur_max_index) && (src != -1) && (dst != -1) && step) {
- ast_codec_get_names(step->src_codec, &str);
+ if (step) {
+ ast_codec_get_names(&step->src_codec, &str);
while (src != dst) {
step = matrix_get(src, dst)->step;
if (!step) {
@@ -965,7 +996,7 @@
break;
}
ast_str_append(&str, 0, "->");
- ast_codec_get_names(step->src_codec, &str);
+ ast_codec_get_names(&step->src_codec, &str);
src = step->dst_fmt_index;
}
}
@@ -974,7 +1005,7 @@
ast_str_set(&str, 0, "No Translation Path");
}
ast_cli(a->fd, "\t%-10.10s To %-10.10s: %-60.60s\n",
- a->argv[4], format_list[i].name, ast_str_buffer(str));
+ a->argv[4], dst_codec->name, ast_str_buffer(str));
ao2_ref(dst_codec, -1);
}
AST_RWLIST_UNLOCK(&translators);
@@ -1033,13 +1064,13 @@
struct ast_translator *u;
char tmp[80];
- if (add_codec2index(t->src_codec) || add_codec2index(t->dst_codec)) {
+ if (add_codec2index(&t->src_codec) || add_codec2index(&t->dst_codec)) {
if (matrix_resize(0)) {
ast_log(LOG_WARNING, "Translator matrix can not represent any more translators. Out of resources.\n");
return -1;
}
- add_codec2index(t->src_codec);
- add_codec2index(t->dst_codec);
+ add_codec2index(&t->src_codec);
+ add_codec2index(&t->dst_codec);
}
if (!mod) {
@@ -1058,8 +1089,8 @@
}
t->module = mod;
- t->src_fmt_index = codec2index(t->src_codec);
- t->dst_fmt_index = codec2index(t->dst_codec);
+ t->src_fmt_index = codec2index(&t->src_codec);
+ t->dst_fmt_index = codec2index(&t->dst_codec);
t->active = 1;
if (t->src_fmt_index == -1 || t->dst_fmt_index == -1) {
@@ -1067,12 +1098,12 @@
return -1;
}
if (t->src_fmt_index >= cur_max_index) {
- ast_log(LOG_WARNING, "Source codec %s is larger than cur_max_index\n", &t->src_codec->name);
+ ast_log(LOG_WARNING, "Source codec %s is larger than cur_max_index\n", t->src_codec.name);
return -1;
}
if (t->dst_fmt_index >= cur_max_index) {
- ast_log(LOG_WARNING, "Destination codec %s is larger than cur_max_index\n", &t->dst_codec->name);
+ ast_log(LOG_WARNING, "Destination codec %s is larger than cur_max_index\n", t->dst_codec.name);
return -1;
}
@@ -1095,7 +1126,7 @@
ast_verb(2, "Registered translator '%s' from codec %s to %s, table cost, %d, computational cost %d\n",
term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)),
- &t->src_codec->name, &t->dst_codec->name, t->table_cost, t->comp_cost);
+ t->src_codec.name, t->dst_codec.name, t->table_cost, t->comp_cost);
AST_RWLIST_WRLOCK(&translators);
@@ -1138,7 +1169,7 @@
AST_RWLIST_REMOVE_CURRENT(list);
ast_verb(2, "Unregistered translator '%s' from codec %s to %s\n",
term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)),
- &t->src_codec->name), &t->dst_codec->name);
+ t->src_codec.name, t->dst_codec.name);
found = 1;
break;
}
@@ -1179,7 +1210,7 @@
unsigned int besttablecost = INT_MAX;
unsigned int beststeps = INT_MAX;
struct ast_format *best = NULL;
- struct ast_format *bestdst, *cur_dst, *cur_src;
+ struct ast_format *bestdst = NULL;
struct ast_format_cap *joint_cap;
int i, j;
@@ -1203,7 +1234,7 @@
}
if (ast_format_get_sample_rate(best) <
- ast_format_get_sampe_rate(fmt)) {
+ ast_format_get_sample_rate(fmt)) {
ao2_ref(best, -1);
best = ast_format_copy(fmt);
}
@@ -1275,7 +1306,6 @@
unsigned int ast_translate_path_steps(struct ast_format *dst_format, struct ast_format *src_format)
{
unsigned int res = -1;
- int src, dest;
/* convert bitwise format numbers into array indices */
int src = format2index(src_format);
int dest = format2index(dst_format);
@@ -1295,26 +1325,71 @@
return res;
}
+static void check_translation_path(
+ struct ast_format_cap *dest, struct ast_format_cap *src,
+ struct ast_format_cap *result, struct ast_format *src_fmt,
+ enum ast_media_type type)
+{
+ int index, src_index = format2index(src_fmt);
+ /* For a given source format, traverse the list of
+ known formats to determine whether there exists
+ a translation path from the source format to the
+ destination format. */
+ for (index = 0; (src_index >= 0) && index < cur_max_index; index++) {
+ struct ast_codec *codec = index2codec(index);
+ RAII_VAR(struct ast_format *, fmt,
+ ast_format_create(codec), ao2_cleanup);
+ ao2_ref(codec, -1);
+
+ if (ast_format_get_type(fmt) != type) {
+ continue;
+ }
+
+ /* if this is not a desired format, nothing to do */
+ if (ast_format_cap_iscompatible_format(
+ dest, fmt) == AST_FORMAT_CMP_EQUAL) {
+ continue;
+ }
+
+ /* if the source is supplying this format, then
+ we can leave it in the result */
+ if (ast_format_cap_iscompatible_format(
+ src, fmt) == AST_FORMAT_CMP_EQUAL) {
+ continue;
+ }
+
+ /* if we don't have a translation path from the src
+ to this format, remove it from the result */
+ if (!matrix_get(src_index, index)->step) {
+ ast_format_cap_remove(result, fmt);
+ continue;
+ }
+
+ /* now check the opposite direction */
+ if (!matrix_get(index, src_index)->step) {
+ ast_format_cap_remove(result, fmt);
+ }
+ }
+
+}
+
void ast_translate_available_formats(struct ast_format_cap *dest, struct ast_format_cap *src, struct ast_format_cap *result)
{
- struct ast_format tmp_fmt;
- struct ast_format cur_dest, cur_src;
- int src_audio = 0;
- int src_video = 0;
+ struct ast_format *cur_dest, *cur_src;
int index;
for (index = 0; index < ast_format_cap_count(dest); ++index) {
- if ((!cur_dest = ast_format_cap_get_format(joint_cap, index))) {
+ if (!(cur_dest = ast_format_cap_get_format(dest, index))) {
continue;
}
/* We give preference to a joint format structure if possible */
- if ((cur_src = ast_format_cap_get_compatible_format)) {
- ast_format_cap_add(result, cur_src);
+ if ((cur_src = ast_format_cap_get_compatible_format(src, cur_dest))) {
+ ast_format_cap_add(result, cur_src, 0);
ao2_ref(cur_src, -1);
} else {
/* Otherwise we just use the destination format */
- ast_format_cap_add(result, fmt)
+ ast_format_cap_add(result, cur_dest, 0);
}
ao2_ref(cur_dest, -1);
}
@@ -1325,91 +1400,19 @@
return;
}
- ast_format_cap_iter_start(src);
- while (!ast_format_cap_iter_next(src, &cur_src)) {
- /* If we have a source audio format, get its format index */
- if (AST_FORMAT_GET_TYPE(cur_src.id) == AST_FORMAT_TYPE_AUDIO) {
- src_audio = format2index(cur_src.id);
- }
-
- /* If we have a source video format, get its format index */
- if (AST_FORMAT_GET_TYPE(cur_src.id) == AST_FORMAT_TYPE_VIDEO) {
- src_video = format2index(cur_src.id);
+ for (index = 0; index < ast_format_cap_count(src); ++index) {
+ if (!(cur_src = ast_format_cap_get_format(src, index))) {
+ continue;
}
AST_RWLIST_RDLOCK(&translators);
-
- /* For a given source audio format, traverse the list of
- known audio formats to determine whether there exists
- a translation path from the source format to the
- destination format. */
- for (index = 0; (src_audio >= 0) && index < cur_max_index; index++) {
- ast_format_set(&tmp_fmt, index2format(index), 0);
-
- if (AST_FORMAT_GET_TYPE(tmp_fmt.id) != AST_FORMAT_TYPE_AUDIO) {
- continue;
- }
-
- /* if this is not a desired format, nothing to do */
- if (!ast_format_cap_iscompatible(dest, &tmp_fmt)) {
- continue;
- }
-
- /* if the source is supplying this format, then
- we can leave it in the result */
- if (ast_format_cap_iscompatible(src, &tmp_fmt)) {
- continue;
- }
-
- /* if we don't have a translation path from the src
- to this format, remove it from the result */
- if (!matrix_get(src_audio, index)->step) {
- ast_format_cap_remove_byid(result, tmp_fmt.id);
- continue;
- }
-
- /* now check the opposite direction */
- if (!matrix_get(index, src_audio)->step) {
- ast_format_cap_remove_byid(result, tmp_fmt.id);
- }
- }
-
- /* For a given source video format, traverse the list of
- known video formats to determine whether there exists
- a translation path from the source format to the
- destination format. */
- for (index = 0; (src_video >= 0) && index < cur_max_index; index++) {
- ast_format_set(&tmp_fmt, index2format(index), 0);
- if (AST_FORMAT_GET_TYPE(tmp_fmt.id) != AST_FORMAT_TYPE_VIDEO) {
- continue;
- }
-
- /* if this is not a desired format, nothing to do */
- if (!ast_format_cap_iscompatible(dest, &tmp_fmt)) {
- continue;
- }
-
- /* if the source is supplying this format, then
- we can leave it in the result */
- if (ast_format_cap_iscompatible(src, &tmp_fmt)) {
- continue;
- }
-
- /* if we don't have a translation path from the src
- to this format, remove it from the result */
- if (!matrix_get(src_video, index)->step) {
- ast_format_cap_remove_byid(result, tmp_fmt.id);
- continue;
- }
-
- /* now check the opposite direction */
- if (!matrix_get(index, src_video)->step) {
- ast_format_cap_remove_byid(result, tmp_fmt.id);
- }
- }
+ check_translation_path(dest, src, result,
+ cur_src, AST_MEDIA_TYPE_AUDIO);
+ check_translation_path(dest, src, result,
+ cur_src, AST_MEDIA_TYPE_AUDIO);
AST_RWLIST_UNLOCK(&translators);
- }
- ast_format_cap_iter_end(src);
+ ao2_ref(cur_src, -1);
+ }
}
static void translate_shutdown(void)
More information about the asterisk-commits
mailing list