Asterisk - The Open Source Telephony Project GIT-master-f36a736
channels/iax2/format_compatibility.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2014, Digium, Inc.
5 *
6 * Joshua Colp <jcolp@digium.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*! \file
20 *
21 * \brief Media Format Bitfield Compatibility API
22 *
23 * \author Joshua Colp <jcolp@digium.com>
24 */
25
26/*** MODULEINFO
27 <support_level>core</support_level>
28 ***/
29
30#include "asterisk.h"
31
32#include "asterisk/logger.h"
33#include "asterisk/astobj2.h"
34#include "asterisk/codec.h"
35#include "asterisk/format.h"
38#include "asterisk/format_cap.h"
39#include "asterisk/utils.h"
40
42
44{
45 uint64_t bitfield = 0;
46 int x;
47
48 for (x = 0; x < ast_format_cap_count(cap); x++) {
49 struct ast_format *format = ast_format_cap_get_format(cap, x);
50
52
53 ao2_ref(format, -1);
54 }
55
56 return bitfield;
57}
58
59int iax2_format_compatibility_bitfield2cap(uint64_t bitfield, struct ast_format_cap *cap)
60{
61 int bit;
62
63 for (bit = 0; bit < 64; ++bit) {
64 uint64_t mask = (1ULL << bit);
65
66 if (mask & bitfield) {
67 struct ast_format *format;
68
70 if (format && ast_format_cap_append(cap, format, 0)) {
71 return -1;
72 }
73 }
74 }
75
76 return 0;
77}
78
80{
81 /*
82 * This just our opinion, expressed in code. We are
83 * asked to choose the best codec to use, given no
84 * information.
85 */
86 static const uint64_t best[] = {
87 /*! Okay, ulaw is used by all telephony equipment, so start with it */
89 /*! Unless of course, you're a silly European, so then prefer ALAW */
94 /*! G.722 is better then all below, but not as common as the above... so give ulaw and alaw priority */
96 /*! Okay, well, signed linear is easy to translate into other stuff */
99 /*! G.726 is standard ADPCM, in RFC3551 packing order */
101 /*! G.726 is standard ADPCM, in AAL2 packing order */
103 /*! ADPCM has great sound quality and is still pretty easy to translate */
105 /*! Okay, we're down to vocoders now, so pick GSM because it's small and easier to
106 translate and sounds pretty good */
108 /*! iLBC is not too bad */
110 /*! Speex is free, but computationally more expensive than GSM */
113 /*! Opus */
115 /*! Ick, LPC10 sounds terrible, but at least we have code for it, if you're tacky enough
116 to use it */
118 /*! G.729a is faster than 723 and slightly less expensive */
120 /*! Down to G.723.1 which is proprietary but at least designed for voice */
122 };
123 int idx;
124
125 /* Find the first preferred codec in the format given */
126 for (idx = 0; idx < ARRAY_LEN(best); ++idx) {
127 if (formats & best[idx]) {
128 return best[idx];
129 }
130 }
131
132 return 0;
133}
Asterisk main include file. File version handling, generic pbx functions.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
uint64_t iax2_format_compatibility_best(uint64_t formats)
Pick the best format from the given bitfield formats.
uint64_t iax2_format_compatibility_cap2bitfield(const struct ast_format_cap *cap)
Convert a format capabilities structure to a bitfield.
int iax2_format_compatibility_bitfield2cap(uint64_t bitfield, struct ast_format_cap *cap)
Convert a bitfield to a format capabilities structure.
Media Format Bitfield Compatibility API.
Codec API.
Media Format API.
Media Format Cache API.
Format Capabilities API.
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
#define ast_format_cap_append(cap, format, framing)
Add format capability to capabilities structure.
Definition: format_cap.h:99
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
Media Format Bitfield Compatibility API.
uint64_t ast_format_compatibility_format2bitfield(const struct ast_format *format)
Convert a format structure to its respective bitfield.
struct ast_format * ast_format_compatibility_bitfield2format(uint64_t bitfield)
Convert a bitfield to its respective format structure.
Support for logging to various files, console and syslog Configuration in file logger....
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
Definition of a media format.
Definition: format.c:43
Definition: file.c:69
Utility functions.
#define ARRAY_LEN(a)
Definition: utils.h:666