Asterisk - The Open Source Telephony Project  18.5.0
res_format_attr_celt.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2011, Digium, Inc.
5  *
6  * David Vossel <[email protected]>
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 /*!
20  * \file
21  * \brief CELT format attribute interface
22  *
23  * \author David Vossel <[email protected]>
24  */
25 
26 /*** MODULEINFO
27  <support_level>core</support_level>
28  ***/
29 
30 #include "asterisk.h"
31 
32 #include <ctype.h> /* for tolower */
33 
34 #include "asterisk/module.h"
35 #include "asterisk/format.h"
36 #include "asterisk/astobj2.h" /* for ao2_ref */
37 #include "asterisk/logger.h" /* for ast_log, LOG_WARNING */
38 #include "asterisk/strings.h" /* for ast_str_append */
39 #include "asterisk/utils.h" /* for MIN */
40 
41 /*!
42  * \brief CELT attribute structure.
43  *
44  * \note The only attribute that affects compatibility here is the sample rate.
45  */
46 struct celt_attr {
47  unsigned int samplerate;
48  unsigned int maxbitrate;
49  unsigned int framesize;
50 };
51 
52 static void celt_destroy(struct ast_format *format)
53 {
54  struct celt_attr *attr = ast_format_get_attribute_data(format);
55 
56  ast_free(attr);
57 }
58 
59 static int celt_clone(const struct ast_format *src, struct ast_format *dst)
60 {
61  struct celt_attr *original = ast_format_get_attribute_data(src);
62  struct celt_attr *attr = ast_calloc(1, sizeof(*attr));
63 
64  if (!attr) {
65  return -1;
66  }
67 
68  if (original) {
69  *attr = *original;
70  }
71 
73 
74  return 0;
75 }
76 
77 static struct ast_format *celt_parse_sdp_fmtp(const struct ast_format *format, const char *attributes)
78 {
79  char *attribs = ast_strdupa(attributes), *attrib;
80  struct ast_format *cloned;
81  struct celt_attr *attr;
82  unsigned int val;
83 
84  cloned = ast_format_clone(format);
85  if (!cloned) {
86  return NULL;
87  }
88  attr = ast_format_get_attribute_data(cloned);
89 
90  /* lower-case everything, so we are case-insensitive */
91  for (attrib = attribs; *attrib; ++attrib) {
92  *attrib = tolower(*attrib);
93  } /* based on channels/chan_sip.c:process_a_sdp_image() */
94 
95  if (sscanf(attribs, "framesize=%30u", &val) == 1) {
96  attr->framesize = val;
97  }
98 
99  return cloned;
100 }
101 
102 static void celt_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str)
103 {
104  struct celt_attr *attr = ast_format_get_attribute_data(format);
105 
106  if (!attr || !attr->framesize) {
107  return;
108  }
109 
110  ast_str_append(str, 0, "a=fmtp:%u framesize=%u\r\n", payload, attr->framesize);
111 }
112 
113 static enum ast_format_cmp_res celt_cmp(const struct ast_format *format1, const struct ast_format *format2)
114 {
115  struct celt_attr *attr1 = ast_format_get_attribute_data(format1);
116  struct celt_attr *attr2 = ast_format_get_attribute_data(format2);
117 
118  if (((!attr1 || !attr1->samplerate) && (!attr2 || !attr2->samplerate)) ||
119  (attr1->samplerate == attr2->samplerate)) {
120  return AST_FORMAT_CMP_EQUAL;
121  }
122 
124 }
125 
126 static struct ast_format *celt_getjoint(const struct ast_format *format1, const struct ast_format *format2)
127 {
128  struct celt_attr *attr1 = ast_format_get_attribute_data(format1);
129  struct celt_attr *attr2 = ast_format_get_attribute_data(format2);
130  struct ast_format *jointformat;
131  struct celt_attr *jointattr;
132 
133  if (attr1 && attr2 && (attr1->samplerate != attr2->samplerate)) {
134  return NULL;
135  }
136 
137  jointformat = ast_format_clone(format1);
138  if (!jointformat) {
139  return NULL;
140  }
141  jointattr = ast_format_get_attribute_data(jointformat);
142 
143  /* either would work, they are guaranteed the same at this point. */
144  jointattr->samplerate = attr1->samplerate;
145  /* Take the lowest max bitrate */
146  jointattr->maxbitrate = MIN(attr1->maxbitrate, attr2->maxbitrate);
147 
148  jointattr->framesize = attr2->framesize; /* TODO figure out what joint framesize means */
149 
150  return jointformat;
151 }
152 
153 static struct ast_format *celt_set(const struct ast_format *format, const char *name, const char *value)
154 {
155  struct ast_format *cloned;
156  struct celt_attr *attr;
157  unsigned int val;
158 
159  cloned = ast_format_clone(format);
160  if (!cloned) {
161  return NULL;
162  }
163  attr = ast_format_get_attribute_data(cloned);
164 
165  if (sscanf(value, "%30u", &val) != 1) {
166  ast_log(LOG_WARNING, "Unknown value '%s' for attribute type '%s'\n",
167  value, name);
168  ao2_ref(cloned, -1);
169  return NULL;
170  }
171 
172  if (!strcasecmp(name, "sample_rate")) {
173  attr->samplerate = val;
174  } else if (!strcasecmp(name, "max_bitrate")) {
175  attr->maxbitrate = val;
176  } else if (!strcasecmp(name, "frame_size")) {
177  attr->framesize = val;
178  } else {
179  ast_log(LOG_WARNING, "Unknown attribute type '%s'\n", name);
180  ao2_ref(cloned, -1);
181  return NULL;
182  }
183 
184  return cloned;
185 }
186 
189  .format_clone = celt_clone,
190  .format_cmp = celt_cmp,
191  .format_get_joint = celt_getjoint,
192  .format_attribute_set = celt_set,
193  .format_parse_sdp_fmtp = celt_parse_sdp_fmtp,
194  .format_generate_sdp_fmtp = celt_generate_sdp_fmtp,
195 };
196 
197 static int load_module(void)
198 {
199  if (ast_format_interface_register("celt", &celt_interface)) {
201  }
202 
204 }
205 
206 static int unload_module(void)
207 {
208  return 0;
209 }
210 
211 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "CELT Format Attribute Module",
212  .support_level = AST_MODULE_SUPPORT_CORE,
213  .load = load_module,
214  .unload = unload_module,
215  .load_pri = AST_MODPRI_CHANNEL_DEPEND,
216 );
Asterisk main include file. File version handling, generic pbx functions.
Optional format interface to extend format operations.
Definition: format.h:44
String manipulation functions.
Definition: ast_expr2.c:325
#define LOG_WARNING
Definition: logger.h:274
Definition of a media format.
Definition: format.c:43
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
static struct ast_format * celt_set(const struct ast_format *format, const char *name, const char *value)
void * ast_format_get_attribute_data(const struct ast_format *format)
Get the attribute data on a format.
Definition: format.c:125
unsigned int maxbitrate
const char * str
Definition: app_jack.c:147
#define NULL
Definition: resample.c:96
int value
Definition: syslog.c:37
Utility functions.
Media Format API.
static void celt_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str)
#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
static int load_module(void)
#define ast_log
Definition: astobj2.c:42
void(*const format_destroy)(struct ast_format *format)
Callback for when the format is destroyed, used to release attribute resources.
Definition: format.h:50
#define ao2_ref(o, delta)
Definition: astobj2.h:464
ast_format_cmp_res
Format comparison results.
Definition: format.h:34
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
static enum ast_format_cmp_res celt_cmp(const struct ast_format *format1, const struct ast_format *format2)
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
#define ast_format_interface_register(codec, interface)
Register a format interface for use with the provided codec.
Definition: format.h:273
static struct ast_format * celt_parse_sdp_fmtp(const struct ast_format *format, const char *attributes)
static const char name[]
Definition: cdr_mysql.c:74
#define ast_free(a)
Definition: astmm.h:182
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static void celt_destroy(struct ast_format *format)
unsigned int framesize
Support for logging to various files, console and syslog Configuration in file logger.conf.
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
static int celt_clone(const struct ast_format *src, struct ast_format *dst)
void ast_format_set_attribute_data(struct ast_format *format, void *attribute_data)
Set the attribute data on a format.
Definition: format.c:130
unsigned int samplerate
static struct ast_format_interface celt_interface
static int unload_module(void)
static struct ast_format * celt_getjoint(const struct ast_format *format1, const struct ast_format *format2)
CELT attribute structure.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
static snd_pcm_format_t format
Definition: chan_alsa.c:102