Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Functions | Variables
res_format_attr_silk.c File Reference

SILK format attribute interface. More...

#include "asterisk.h"
#include <ctype.h>
#include "asterisk/module.h"
#include "asterisk/format.h"
#include "asterisk/logger.h"
#include "asterisk/strings.h"
#include "asterisk/utils.h"
Include dependency graph for res_format_attr_silk.c:

Go to the source code of this file.

Data Structures

struct  silk_attr
 SILK attribute structure. More...
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static void attr_init (struct silk_attr *attr)
 
static int load_module (void)
 
static int silk_clone (const struct ast_format *src, struct ast_format *dst)
 
static enum ast_format_cmp_res silk_cmp (const struct ast_format *format1, const struct ast_format *format2)
 
static void silk_destroy (struct ast_format *format)
 
static void silk_generate_sdp_fmtp (const struct ast_format *format, unsigned int payload, struct ast_str **str)
 
static const void * silk_get (const struct ast_format *format, const char *name)
 
static struct ast_formatsilk_getjoint (const struct ast_format *format1, const struct ast_format *format2)
 
static struct ast_formatsilk_parse_sdp_fmtp (const struct ast_format *format, const char *attributes)
 
static struct ast_formatsilk_set (const struct ast_format *format, const char *name, const char *value)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "SILK Format Attribute Module" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CHANNEL_DEPEND, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_format_interface silk_interface
 

Detailed Description

SILK format attribute interface.

Author
David Vossel dvoss.nosp@m.el@d.nosp@m.igium.nosp@m..com

Definition in file res_format_attr_silk.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 267 of file res_format_attr_silk.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 267 of file res_format_attr_silk.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 267 of file res_format_attr_silk.c.

◆ attr_init()

static void attr_init ( struct silk_attr attr)
static

Definition at line 59 of file res_format_attr_silk.c.

Referenced by silk_clone(), and silk_getjoint().

60 {
61  memset(attr, 0, sizeof(*attr));
62 }

◆ load_module()

static int load_module ( void  )
static

Definition at line 248 of file res_format_attr_silk.c.

References ast_format_interface_register, AST_MODULE_LOAD_DECLINE, and AST_MODULE_LOAD_SUCCESS.

Referenced by unload_module().

249 {
252  }
253 
255 }
static struct ast_format_interface silk_interface
#define ast_format_interface_register(codec, interface)
Register a format interface for use with the provided codec.
Definition: format.h:273
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78

◆ silk_clone()

static int silk_clone ( const struct ast_format src,
struct ast_format dst 
)
static

Definition at line 64 of file res_format_attr_silk.c.

References ast_format_get_attribute_data(), ast_format_set_attribute_data(), ast_malloc, and attr_init().

65 {
66  struct silk_attr *original = ast_format_get_attribute_data(src);
67  struct silk_attr *attr = ast_malloc(sizeof(*attr));
68 
69  if (!attr) {
70  return -1;
71  }
72 
73  if (original) {
74  *attr = *original;
75  } else {
76  attr_init(attr);
77  }
78 
80 
81  return 0;
82 }
SILK attribute structure.
void * ast_format_get_attribute_data(const struct ast_format *format)
Get the attribute data on a format.
Definition: format.c:125
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
static void attr_init(struct silk_attr *attr)
void ast_format_set_attribute_data(struct ast_format *format, void *attribute_data)
Set the attribute data on a format.
Definition: format.c:130

◆ silk_cmp()

static enum ast_format_cmp_res silk_cmp ( const struct ast_format format1,
const struct ast_format format2 
)
static

Definition at line 135 of file res_format_attr_silk.c.

References AST_FORMAT_CMP_EQUAL, AST_FORMAT_CMP_NOT_EQUAL, and ast_format_get_sample_rate().

136 {
137  if (ast_format_get_sample_rate(format1) == ast_format_get_sample_rate(format2)) {
138  return AST_FORMAT_CMP_EQUAL;
139  }
140 
142 }
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition: format.c:379

◆ silk_destroy()

static void silk_destroy ( struct ast_format format)
static

Definition at line 52 of file res_format_attr_silk.c.

References ast_format_get_attribute_data(), and ast_free.

53 {
54  struct silk_attr *attr = ast_format_get_attribute_data(format);
55 
56  ast_free(attr);
57 }
SILK attribute structure.
void * ast_format_get_attribute_data(const struct ast_format *format)
Get the attribute data on a format.
Definition: format.c:125
#define ast_free(a)
Definition: astmm.h:182

◆ silk_generate_sdp_fmtp()

static void silk_generate_sdp_fmtp ( const struct ast_format format,
unsigned int  payload,
struct ast_str **  str 
)
static

Definition at line 115 of file res_format_attr_silk.c.

References ast_format_get_attribute_data(), ast_str_append(), silk_attr::dtx, silk_attr::fec, and silk_attr::maxbitrate.

116 {
117  struct silk_attr *attr = ast_format_get_attribute_data(format);
118 
119  if (!attr) {
120  return;
121  }
122 
123  if ((attr->maxbitrate > 5000) && (attr->maxbitrate < 40000)) {
124  ast_str_append(str, 0, "a=fmtp:%u maxaveragebitrate=%u\r\n", payload, attr->maxbitrate);
125  }
126 
127  if (attr->dtx) {
128  ast_str_append(str, 0, "a=fmtp:%u usedtx=%u\r\n", payload, attr->dtx);
129  }
130  if (attr->fec) {
131  ast_str_append(str, 0, "a=fmtp:%u useinbandfec=%u\r\n", payload, attr->fec);
132  }
133 }
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1091
SILK attribute structure.
void * ast_format_get_attribute_data(const struct ast_format *format)
Get the attribute data on a format.
Definition: format.c:125
unsigned int fec
unsigned int maxbitrate
unsigned int dtx

◆ silk_get()

static const void* silk_get ( const struct ast_format format,
const char *  name 
)
static

Definition at line 216 of file res_format_attr_silk.c.

References ast_format_get_attribute_data(), ast_log, silk_attr::dtx, silk_attr::fec, LOG_WARNING, silk_attr::maxbitrate, NULL, and silk_attr::packetloss_percentage.

217 {
218  struct silk_attr *attr = ast_format_get_attribute_data(format);
219  unsigned int *val;
220 
221  if (!strcasecmp(name, "max_bitrate")) {
222  val = &attr->maxbitrate;
223  } else if (!strcasecmp(name, "dtx")) {
224  val = &attr->dtx;
225  } else if (!strcasecmp(name, "fec")) {
226  val = &attr->fec;
227  } else if (!strcasecmp(name, "packetloss_percentage")) {
228  val = &attr->packetloss_percentage;
229  } else {
230  ast_log(LOG_WARNING, "unknown attribute type %s\n", name);
231  return NULL;
232  }
233 
234  return val;
235 }
Definition: ast_expr2.c:325
#define LOG_WARNING
Definition: logger.h:274
SILK attribute structure.
void * ast_format_get_attribute_data(const struct ast_format *format)
Get the attribute data on a format.
Definition: format.c:125
#define NULL
Definition: resample.c:96
unsigned int packetloss_percentage
#define ast_log
Definition: astobj2.c:42
unsigned int fec
unsigned int maxbitrate
unsigned int dtx
static const char name[]
Definition: cdr_mysql.c:74

◆ silk_getjoint()

static struct ast_format* silk_getjoint ( const struct ast_format format1,
const struct ast_format format2 
)
static

Definition at line 144 of file res_format_attr_silk.c.

References ast_format_clone(), ast_format_get_attribute_data(), ast_format_get_sample_rate(), attr_init(), silk_attr::dtx, silk_attr::fec, MAX, silk_attr::maxbitrate, MIN, NULL, and silk_attr::packetloss_percentage.

145 {
146  struct silk_attr *attr1 = ast_format_get_attribute_data(format1);
147  struct silk_attr *attr2 = ast_format_get_attribute_data(format2);
148  struct ast_format *jointformat;
149  struct silk_attr *attr_res;
150 
151  if (ast_format_get_sample_rate(format1) != ast_format_get_sample_rate(format2)) {
152  return NULL;
153  }
154 
155  jointformat = ast_format_clone(format1);
156  if (!jointformat) {
157  return NULL;
158  }
159  attr_res = ast_format_get_attribute_data(jointformat);
160 
161  if (!attr1 || !attr2) {
162  attr_init(attr_res);
163  } else {
164  /* Take the lowest max bitrate */
165  attr_res->maxbitrate = MIN(attr1->maxbitrate, attr2->maxbitrate);
166 
167  /* Only do dtx if both sides want it. DTX is a trade off between
168  * computational complexity and bandwidth. */
169  attr_res->dtx = attr1->dtx && attr2->dtx ? 1 : 0;
170 
171  /* Only do FEC if both sides want it. If a peer specifically requests not
172  * to receive with FEC, it may be a waste of bandwidth. */
173  attr_res->fec = attr1->fec && attr2->fec ? 1 : 0;
174 
175  /* Use the maximum packetloss percentage between the two attributes. This affects how
176  * much redundancy is used in the FEC. */
178  }
179 
180  return jointformat;
181 }
Definition of a media format.
Definition: format.c:43
SILK attribute structure.
void * ast_format_get_attribute_data(const struct ast_format *format)
Get the attribute data on a format.
Definition: format.c:125
#define NULL
Definition: resample.c:96
#define MIN(a, b)
Definition: utils.h:226
struct ast_format * ast_format_clone(const struct ast_format *format)
Clone an existing media format so it can be modified.
Definition: format.c:180
unsigned int packetloss_percentage
#define MAX(a, b)
Definition: utils.h:228
static void attr_init(struct silk_attr *attr)
unsigned int fec
unsigned int maxbitrate
unsigned int dtx
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition: format.c:379

◆ silk_parse_sdp_fmtp()

static struct ast_format* silk_parse_sdp_fmtp ( const struct ast_format format,
const char *  attributes 
)
static

Definition at line 84 of file res_format_attr_silk.c.

References ast_format_clone(), ast_format_get_attribute_data(), ast_strdupa, silk_attr::dtx, silk_attr::fec, silk_attr::maxbitrate, and NULL.

85 {
86  char *attribs = ast_strdupa(attributes), *attrib;
87  struct ast_format *cloned;
88  struct silk_attr *attr;
89  unsigned int val;
90 
91  cloned = ast_format_clone(format);
92  if (!cloned) {
93  return NULL;
94  }
95  attr = ast_format_get_attribute_data(cloned);
96 
97  /* lower-case everything, so we are case-insensitive */
98  for (attrib = attribs; *attrib; ++attrib) {
99  *attrib = tolower(*attrib);
100  } /* based on channels/chan_sip.c:process_a_sdp_image() */
101 
102  if (sscanf(attribs, "maxaveragebitrate=%30u", &val) == 1) {
103  attr->maxbitrate = val;
104  }
105  if (sscanf(attribs, "usedtx=%30u", &val) == 1) {
106  attr->dtx = val;
107  }
108  if (sscanf(attribs, "useinbandfec=%30u", &val) == 1) {
109  attr->fec = val;
110  }
111 
112  return cloned;
113 }
Definition: ast_expr2.c:325
Definition of a media format.
Definition: format.c:43
SILK attribute structure.
void * ast_format_get_attribute_data(const struct ast_format *format)
Get the attribute data on a format.
Definition: format.c:125
#define NULL
Definition: resample.c:96
struct ast_format * ast_format_clone(const struct ast_format *format)
Clone an existing media format so it can be modified.
Definition: format.c:180
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
unsigned int fec
unsigned int maxbitrate
unsigned int dtx

◆ silk_set()

static struct ast_format* silk_set ( const struct ast_format format,
const char *  name,
const char *  value 
)
static

Definition at line 183 of file res_format_attr_silk.c.

References ast_format_clone(), ast_format_get_attribute_data(), ast_log, silk_attr::dtx, silk_attr::fec, LOG_WARNING, silk_attr::maxbitrate, NULL, and silk_attr::packetloss_percentage.

184 {
185  struct ast_format *cloned;
186  struct silk_attr *attr;
187  unsigned int val;
188 
189  if (sscanf(value, "%30u", &val) != 1) {
190  ast_log(LOG_WARNING, "Unknown value '%s' for attribute type '%s'\n",
191  value, name);
192  return NULL;
193  }
194 
195  cloned = ast_format_clone(format);
196  if (!cloned) {
197  return NULL;
198  }
199  attr = ast_format_get_attribute_data(cloned);
200 
201  if (!strcasecmp(name, "max_bitrate")) {
202  attr->maxbitrate = val;
203  } else if (!strcasecmp(name, "dtx")) {
204  attr->dtx = val;
205  } else if (!strcasecmp(name, "fec")) {
206  attr->fec = val;
207  } else if (!strcasecmp(name, "packetloss_percentage")) {
208  attr->packetloss_percentage = val;
209  } else {
210  ast_log(LOG_WARNING, "unknown attribute type %s\n", name);
211  }
212 
213  return cloned;
214 }
Definition: ast_expr2.c:325
#define LOG_WARNING
Definition: logger.h:274
Definition of a media format.
Definition: format.c:43
SILK attribute structure.
void * ast_format_get_attribute_data(const struct ast_format *format)
Get the attribute data on a format.
Definition: format.c:125
#define NULL
Definition: resample.c:96
int value
Definition: syslog.c:37
struct ast_format * ast_format_clone(const struct ast_format *format)
Clone an existing media format so it can be modified.
Definition: format.c:180
unsigned int packetloss_percentage
#define ast_log
Definition: astobj2.c:42
unsigned int fec
unsigned int maxbitrate
unsigned int dtx
static const char name[]
Definition: cdr_mysql.c:74

◆ unload_module()

static int unload_module ( void  )
static

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "SILK Format Attribute Module" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CHANNEL_DEPEND, }
static

Definition at line 267 of file res_format_attr_silk.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 267 of file res_format_attr_silk.c.

◆ silk_interface

struct ast_format_interface silk_interface
static

Definition at line 237 of file res_format_attr_silk.c.