Asterisk - The Open Source Telephony Project GIT-master-f36a736
Data Structures | Macros | Functions
codec_pref.h File Reference

Media Format Bitfield Compatibility API. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  iax2_codec_pref
 

Macros

#define IAX2_CODEC_PREF_SIZE   64
 

Functions

void iax2_codec_pref_append (struct iax2_codec_pref *pref, struct ast_format *format, unsigned int framing)
 Append a audio codec to a preference list, removing it first if it was already there. More...
 
int iax2_codec_pref_best_bitfield2cap (uint64_t bitfield, struct iax2_codec_pref *prefs, struct ast_format_cap *cap)
 Convert a bitfield to a format capabilities structure in the "best" order. More...
 
void iax2_codec_pref_convert (struct iax2_codec_pref *pref, char *buf, size_t size, int right)
 Shift an audio codec preference list up or down 65 bytes so that it becomes an ASCII string. More...
 
int iax2_codec_pref_format_bitfield_to_order_value (uint64_t bitfield)
 Convert a format bitfield into an iax2_codec_pref order value. More...
 
uint64_t iax2_codec_pref_from_bitfield (struct iax2_codec_pref *pref, uint64_t bitfield)
 Create codec preference list from the given bitfield formats. More...
 
struct ast_formatiax2_codec_pref_index (struct iax2_codec_pref *pref, int index, struct ast_format **result)
 Codec located at a particular place in the preference index. More...
 
uint64_t iax2_codec_pref_order_value_to_format_bitfield (int order_value)
 Convert an iax2_codec_pref order value into a format bitfield. More...
 
void iax2_codec_pref_prepend (struct iax2_codec_pref *pref, struct ast_format *format, unsigned int framing, int only_if_existing)
 Prepend an audio codec to a preference list, removing it first if it was already there. More...
 
void iax2_codec_pref_remove_missing (struct iax2_codec_pref *pref, uint64_t bitfield)
 Removes format from the pref list that aren't in the bitfield. More...
 
int iax2_codec_pref_string (struct iax2_codec_pref *pref, char *buf, size_t size)
 Dump audio codec preference list into a string. More...
 
int iax2_codec_pref_to_cap (struct iax2_codec_pref *pref, struct ast_format_cap *cap)
 Convert a preference structure to a capabilities structure. More...
 

Detailed Description

Media Format Bitfield Compatibility API.

Author
Joshua Colp jcolp.nosp@m.@dig.nosp@m.ium.c.nosp@m.om

Definition in file codec_pref.h.

Macro Definition Documentation

◆ IAX2_CODEC_PREF_SIZE

#define IAX2_CODEC_PREF_SIZE   64

Definition at line 33 of file codec_pref.h.

Function Documentation

◆ iax2_codec_pref_append()

void iax2_codec_pref_append ( struct iax2_codec_pref pref,
struct ast_format format,
unsigned int  framing 
)

Append a audio codec to a preference list, removing it first if it was already there.

Definition at line 420 of file codec_pref.c.

421{
422 uint64_t bitfield;
423
425 if (!bitfield) {
426 return;
427 }
428
429 iax2_codec_pref_append_bitfield(pref, bitfield, framing);
430}
static void iax2_codec_pref_append_bitfield(struct iax2_codec_pref *pref, uint64_t bitfield, unsigned int framing)
Definition: codec_pref.c:399
uint64_t ast_format_compatibility_format2bitfield(const struct ast_format *format)
Convert a format structure to its respective bitfield.

References ast_format_compatibility_format2bitfield(), and iax2_codec_pref_append_bitfield().

Referenced by iax2_parse_allow_disallow().

◆ iax2_codec_pref_best_bitfield2cap()

int iax2_codec_pref_best_bitfield2cap ( uint64_t  bitfield,
struct iax2_codec_pref prefs,
struct ast_format_cap cap 
)

Convert a bitfield to a format capabilities structure in the "best" order.

Parameters
bitfieldThe bitfield for the media formats
prefsFormat preference order to use as a guide. (May be NULL)
capCapabilities structure to place formats into
Return values
0on success.
-1on error.
Note
If failure occurs the capabilities structure may contain a partial set of formats

Definition at line 112 of file codec_pref.c.

113{
114 uint64_t best_bitfield;
115 struct ast_format *format;
116
117 /* Add any user preferred codecs first. */
118 if (prefs) {
119 int idx;
120
121 for (idx = 0; bitfield && idx < ARRAY_LEN(prefs->order); ++idx) {
122 best_bitfield = iax2_codec_pref_order_value_to_format_bitfield(prefs->order[idx]);
123 if (!best_bitfield) {
124 break;
125 }
126
127 if (best_bitfield & bitfield) {
128 format = ast_format_compatibility_bitfield2format(best_bitfield);
129 if (format && ast_format_cap_append(cap, format, prefs->framing[idx])) {
130 return -1;
131 }
132
133 /* Remove just added codec. */
134 bitfield &= ~best_bitfield;
135 }
136 }
137 }
138
139 /* Add the hard coded "best" codecs. */
140 while (bitfield) {
141 best_bitfield = iax2_format_compatibility_best(bitfield);
142 if (!best_bitfield) {
143 /* No more codecs considered best. */
144 break;
145 }
146
147 format = ast_format_compatibility_bitfield2format(best_bitfield);
148 /* The best_bitfield should always be convertible to a format. */
149 ast_assert(format != NULL);
150
151 if (ast_format_cap_append(cap, format, 0)) {
152 return -1;
153 }
154
155 /* Remove just added "best" codec to find the next "best". */
156 bitfield &= ~best_bitfield;
157 }
158
159 /* Add any remaining codecs. */
160 if (bitfield) {
161 int bit;
162
163 for (bit = 0; bit < 64; ++bit) {
164 uint64_t mask = (1ULL << bit);
165
166 if (mask & bitfield) {
168 if (format && ast_format_cap_append(cap, format, 0)) {
169 return -1;
170 }
171 }
172 }
173 }
174
175 return 0;
176}
uint64_t iax2_format_compatibility_best(uint64_t formats)
Pick the best format from the given bitfield formats.
uint64_t iax2_codec_pref_order_value_to_format_bitfield(int order_value)
Convert an iax2_codec_pref order value into a format bitfield.
Definition: codec_pref.c:367
#define ast_format_cap_append(cap, format, framing)
Add format capability to capabilities structure.
Definition: format_cap.h:99
struct ast_format * ast_format_compatibility_bitfield2format(uint64_t bitfield)
Convert a bitfield to its respective format structure.
#define NULL
Definition: resample.c:96
Definition of a media format.
Definition: format.c:43
unsigned int framing[IAX2_CODEC_PREF_SIZE]
Definition: codec_pref.h:38
char order[IAX2_CODEC_PREF_SIZE]
Definition: codec_pref.h:36
#define ast_assert(a)
Definition: utils.h:739
#define ARRAY_LEN(a)
Definition: utils.h:666

References ARRAY_LEN, ast_assert, ast_format_cap_append, ast_format_compatibility_bitfield2format(), iax2_codec_pref::framing, iax2_codec_pref_order_value_to_format_bitfield(), iax2_format_compatibility_best(), NULL, and iax2_codec_pref::order.

Referenced by ast_iax2_new(), and socket_process_helper().

◆ iax2_codec_pref_convert()

void iax2_codec_pref_convert ( struct iax2_codec_pref pref,
char *  buf,
size_t  size,
int  right 
)

Shift an audio codec preference list up or down 65 bytes so that it becomes an ASCII string.

Note
Due to a misunderstanding in how codec preferences are stored, this list starts at 'B', not 'A'. For backwards compatibility reasons, this cannot change.
Parameters
prefA codec preference list structure
bufA string denoting codec preference, appropriate for use in line transmission
sizeSize of buf
rightBoolean: if 0, convert from buf to pref; if 1, convert from pref to buf.

Definition at line 44 of file codec_pref.c.

45{
46 static int differential = (int) 'A';
47 int x;
48
49 if (right) {
50 --size;/* Save room for the nul string terminator. */
51 for (x = 0; x < ARRAY_LEN(pref->order) && x < size; ++x) {
52 if (!pref->order[x]) {
53 break;
54 }
55
56 buf[x] = pref->order[x] + differential;
57 }
58
59 buf[x] = '\0';
60 } else {
61 for (x = 0; x < ARRAY_LEN(pref->order) && x < size; ++x) {
62 if (buf[x] == '\0') {
63 break;
64 }
65
66 pref->order[x] = buf[x] - differential;
67 pref->framing[x] = 0;
68 }
69
70 if (x < ARRAY_LEN(pref->order)) {
71 pref->order[x] = 0;
72 pref->framing[x] = 0;
73 }
74 }
75}
char buf[BUFSIZE]
Definition: eagi_proxy.c:66

References ARRAY_LEN, buf, iax2_codec_pref::framing, and iax2_codec_pref::order.

Referenced by check_access(), dump_prefs(), iax2_call(), and socket_process_helper().

◆ iax2_codec_pref_format_bitfield_to_order_value()

int iax2_codec_pref_format_bitfield_to_order_value ( uint64_t  bitfield)

Convert a format bitfield into an iax2_codec_pref order value.

Parameters
bitfieldvalue being converted
Returns
the iax2_codec_pref order value of the most significant format in the bitfield.
Note
This is really meant to be used on single format bitfields. It will work with multiformat bitfields, but it can only return the index of the most significant one if that is the case.

Definition at line 376 of file codec_pref.c.

377{
378 int idx;
379
380 if (bitfield) {
381 for (idx = 0; idx < ARRAY_LEN(iax2_supported_formats); ++idx) {
382 if (iax2_supported_formats[idx] == bitfield) {
383 return idx + 1;
384 }
385 }
386 }
387 return 0;
388}
static const uint64_t iax2_supported_formats[]
Formats supported by IAX2.
Definition: codec_pref.c:324

References ARRAY_LEN, and iax2_supported_formats.

Referenced by iax2_codec_pref_append_bitfield(), and iax2_codec_pref_prepend().

◆ iax2_codec_pref_from_bitfield()

uint64_t iax2_codec_pref_from_bitfield ( struct iax2_codec_pref pref,
uint64_t  bitfield 
)

Create codec preference list from the given bitfield formats.

Since
13.0.0
Parameters
prefCodec preference list to setup from the given bitfield.
bitfieldFormat bitfield to guide preference list creation.
Returns
Updated bitfield with any bits not mapped to a format cleared.

Definition at line 481 of file codec_pref.c.

482{
483 int bit;
484 uint64_t working_bitfield;
485 uint64_t best_bitfield;
486 struct ast_format *format;
487
488 /* Init the preference list. */
489 memset(pref, 0, sizeof(*pref));
490
491 working_bitfield = bitfield;
492
493 /* Add the "best" codecs first. */
494 while (working_bitfield) {
495 best_bitfield = iax2_format_compatibility_best(working_bitfield);
496 if (!best_bitfield) {
497 /* No more codecs considered best. */
498 break;
499 }
500
501 /* Remove current "best" codec to find the next "best". */
502 working_bitfield &= ~best_bitfield;
503
504 format = ast_format_compatibility_bitfield2format(best_bitfield);
505 /* The best_bitfield should always be convertible to a format. */
506 ast_assert(format != NULL);
507
508 iax2_codec_pref_append_bitfield(pref, best_bitfield, 0);
509 }
510
511 /* Add any remaining codecs. */
512 if (working_bitfield) {
513 for (bit = 0; bit < 64; ++bit) {
514 uint64_t mask = (1ULL << bit);
515
516 if (mask & working_bitfield) {
518 if (!format) {
519 /* The bit is not associated with any format. */
520 bitfield &= ~mask;
521 continue;
522 }
523
524 iax2_codec_pref_append_bitfield(pref, mask, 0);
525 }
526 }
527 }
528
529 return bitfield;
530}

References ast_assert, ast_format_compatibility_bitfield2format(), iax2_codec_pref_append_bitfield(), iax2_format_compatibility_best(), and NULL.

Referenced by set_config().

◆ iax2_codec_pref_index()

struct ast_format * iax2_codec_pref_index ( struct iax2_codec_pref pref,
int  index,
struct ast_format **  result 
)

Codec located at a particular place in the preference index.

Parameters
prefpreference structure to get the codec out of
indexto retrieve from
resultast_format structure to store the index value in
Returns
pointer to input ast_format on success, NULL on failure

Definition at line 77 of file codec_pref.c.

78{
79 if (0 <= idx && idx < ARRAY_LEN(pref->order) && pref->order[idx]) {
80 uint64_t pref_bitfield;
81
82 pref_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[idx]);
84 } else {
85 *result = NULL;
86 }
87
88 return *result;
89}
static PGresult * result
Definition: cel_pgsql.c:84

References ARRAY_LEN, ast_format_compatibility_bitfield2format(), iax2_codec_pref_order_value_to_format_bitfield(), NULL, iax2_codec_pref::order, and result.

Referenced by function_iaxpeer(), and socket_process_helper().

◆ iax2_codec_pref_order_value_to_format_bitfield()

uint64_t iax2_codec_pref_order_value_to_format_bitfield ( int  order_value)

Convert an iax2_codec_pref order value into a format bitfield.

Parameters
order_valuevalue being converted
Returns
the bitfield value of the order_value format

Definition at line 367 of file codec_pref.c.

368{
369 if (order_value < 1 || ARRAY_LEN(iax2_supported_formats) < order_value) {
370 return 0;
371 }
372
373 return iax2_supported_formats[order_value - 1];
374}

References ARRAY_LEN, and iax2_supported_formats.

Referenced by codec_choose_from_prefs(), iax2_codec_pref_best_bitfield2cap(), iax2_codec_pref_index(), iax2_codec_pref_remove_missing(), and iax2_codec_pref_to_cap().

◆ iax2_codec_pref_prepend()

void iax2_codec_pref_prepend ( struct iax2_codec_pref pref,
struct ast_format format,
unsigned int  framing,
int  only_if_existing 
)

Prepend an audio codec to a preference list, removing it first if it was already there.

Definition at line 432 of file codec_pref.c.

434{
435 uint64_t bitfield;
436 int format_index;
437 int x;
438
440 if (!bitfield) {
441 return;
442 }
443 format_index = iax2_codec_pref_format_bitfield_to_order_value(bitfield);
444 if (!format_index) {
445 return;
446 }
447
448 /* Now find any existing occurrence, or the end */
449 for (x = 0; x < ARRAY_LEN(pref->order); ++x) {
450 if (!pref->order[x] || pref->order[x] == format_index)
451 break;
452 }
453
454 /*
455 * The array can never be full without format_index
456 * also being in the array.
457 */
458 ast_assert(x < ARRAY_LEN(pref->order));
459
460 /* If we failed to find any occurrence, set to the end for safety. */
461 if (ARRAY_LEN(pref->order) <= x) {
462 x = ARRAY_LEN(pref->order) - 1;
463 }
464
465 if (only_if_existing && !pref->order[x]) {
466 return;
467 }
468
469 /* Move down to make space to insert - either all the way to the end,
470 or as far as the existing location (which will be overwritten) */
471 for (; x > 0; --x) {
472 pref->order[x] = pref->order[x - 1];
473 pref->framing[x] = pref->framing[x - 1];
474 }
475
476 /* And insert the new entry */
477 pref->order[0] = format_index;
478 pref->framing[0] = framing;
479}
int iax2_codec_pref_format_bitfield_to_order_value(uint64_t bitfield)
Convert a format bitfield into an iax2_codec_pref order value.
Definition: codec_pref.c:376

References ARRAY_LEN, ast_assert, ast_format_compatibility_format2bitfield(), iax2_codec_pref::framing, iax2_codec_pref_format_bitfield_to_order_value(), and iax2_codec_pref::order.

Referenced by create_addr().

◆ iax2_codec_pref_remove_missing()

void iax2_codec_pref_remove_missing ( struct iax2_codec_pref pref,
uint64_t  bitfield 
)

Removes format from the pref list that aren't in the bitfield.

Definition at line 288 of file codec_pref.c.

289{
290 int idx;
291
292 if (!pref->order[0]) {
293 return;
294 }
295
296 /*
297 * Work from the end of the list so we always deal with
298 * unmodified entries in case we have to remove a pref.
299 */
300 for (idx = ARRAY_LEN(pref->order); idx--;) {
301 uint64_t pref_bitfield;
302
303 pref_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[idx]);
304 if (!pref_bitfield) {
305 continue;
306 }
307
308 /* If this format isn't in the bitfield, remove it from the prefs. */
309 if (!(pref_bitfield & bitfield)) {
310 codec_pref_remove_index(pref, idx);
311 }
312 }
313}
static void codec_pref_remove_index(struct iax2_codec_pref *pref, int codec_pref_index)
Definition: codec_pref.c:246

References ARRAY_LEN, codec_pref_remove_index(), iax2_codec_pref_order_value_to_format_bitfield(), and iax2_codec_pref::order.

Referenced by iax2_parse_allow_disallow().

◆ iax2_codec_pref_string()

int iax2_codec_pref_string ( struct iax2_codec_pref pref,
char *  buf,
size_t  size 
)

Dump audio codec preference list into a string.

Parameters
prefpreference structure to dump string representation of order for
bufcharacter buffer to put string into
sizesize of the character buffer
Returns
-1 on error. Otherwise returns the remaining spaaaaaace in the buffer.
Note
Format is (codec1|codec2|codec3|...) – if the list is too long for the size of the buffer, codecs will be written until they exceed the length remaining in which case the list will be closed with '...)' after the last writable codec.

Definition at line 178 of file codec_pref.c.

179{
180 int x;
181 struct ast_format_cap *cap;
182 size_t total_len;
183 char *cur;
184
185 /* This function is useless if you have less than a 6 character buffer.
186 * '(...)' is six characters. */
187 if (size < 6) {
188 return -1;
189 }
190
191 /* Convert the preferences into a format cap so that we can read the format names */
193 if (!cap || iax2_codec_pref_to_cap(pref, cap)) {
194 strcpy(buf, "(...)"); /* Safe */
195 ao2_cleanup(cap);
196 return -1;
197 }
198
199 /* We know that at a minimum, 3 characters are used - (, ), and \0 */
200 total_len = size - 3;
201
202 /* This character has already been accounted for total_len purposes */
203 buf[0] = '(';
204 cur = buf + 1;
205
206 /* Loop through the formats and write as many into the buffer as we can */
207 for (x = 0; x < ast_format_cap_count(cap); x++) {
208 size_t name_len;
209 struct ast_format *fmt = ast_format_cap_get_format(cap, x);
210 const char *name = ast_format_get_name(fmt);
211
212 name_len = strlen(name);
213
214 /* all entries after the first need a delimiter character */
215 if (x) {
216 name_len++;
217 }
218
219 /* Terminate the list early if we don't have room for the entry.
220 * If it's not the last entry in the list, save enough room to write '...'.
221 */
222 if (((x == ast_format_cap_count(cap) - 1) && (total_len < name_len)) ||
223 ((x < ast_format_cap_count(cap) - 1) && (total_len < name_len + 3))) {
224 strcpy(cur, "...");
225 cur += 3;
226 total_len -= 3;
227 ao2_ref(fmt, -1);
228 break;
229 }
230
231 sprintf(cur, "%s%s", x ? "|" : "", name);
232 cur += name_len;
233 total_len -= name_len;
234
235 ao2_ref(fmt, -1);
236 }
237 ao2_ref(cap, -1);
238
239 /* These two characters have already been accounted for total_len purposes */
240 cur[0] = ')';
241 cur[1] = '\0';
242
243 return size - total_len;
244}
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
int iax2_codec_pref_to_cap(struct iax2_codec_pref *pref, struct ast_format_cap *cap)
Convert a preference structure to a capabilities structure.
Definition: codec_pref.c:91
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
struct ast_format * ast_format_cap_get_format(const struct ast_format_cap *cap, int position)
Get the format at a specific index.
Definition: format_cap.c:400
@ AST_FORMAT_CAP_FLAG_DEFAULT
Definition: format_cap.h:38
#define ast_format_cap_alloc(flags)
Allocate a new ast_format_cap structure.
Definition: format_cap.h:49
size_t ast_format_cap_count(const struct ast_format_cap *cap)
Get the number of formats present within the capabilities structure.
Definition: format_cap.c:395
static const char name[]
Definition: format_mp3.c:68
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54

References ao2_cleanup, ao2_ref, ast_format_cap_alloc, ast_format_cap_count(), AST_FORMAT_CAP_FLAG_DEFAULT, ast_format_cap_get_format(), ast_format_get_name(), buf, iax2_codec_pref_to_cap(), and name.

Referenced by dump_prefs(), handle_cli_iax2_show_peer(), and socket_process_helper().

◆ iax2_codec_pref_to_cap()

int iax2_codec_pref_to_cap ( struct iax2_codec_pref pref,
struct ast_format_cap cap 
)

Convert a preference structure to a capabilities structure.

Parameters
prefFormats in preference order to build the capabilities.
capCapabilities structure to place formats into
Return values
0on success.
-1on error.
Note
If failure occurs the capabilities structure may contain a partial set of formats

Definition at line 91 of file codec_pref.c.

92{
93 int idx;
94
95 for (idx = 0; idx < ARRAY_LEN(pref->order); ++idx) {
96 uint64_t pref_bitfield;
97 struct ast_format *pref_format;
98
99 pref_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[idx]);
100 if (!pref_bitfield) {
101 break;
102 }
103
104 pref_format = ast_format_compatibility_bitfield2format(pref_bitfield);
105 if (pref_format && ast_format_cap_append(cap, pref_format, pref->framing[idx])) {
106 return -1;
107 }
108 }
109 return 0;
110}

References ARRAY_LEN, ast_format_cap_append, ast_format_compatibility_bitfield2format(), iax2_codec_pref::framing, iax2_codec_pref_order_value_to_format_bitfield(), and iax2_codec_pref::order.

Referenced by iax2_codec_pref_string(), and iax2_parse_allow_disallow().