Asterisk - The Open Source Telephony Project  18.5.0
res_format_attr_vp8.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2015, Alexander Traud
5  *
6  * Alexander Traud <[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 VP8 format attribute interface
22  *
23  * \author Alexander Traud <[email protected]>
24  *
25  * \note http://tools.ietf.org/html/draft-ietf-payload-vp8
26  */
27 
28 /*** MODULEINFO
29  <support_level>core</support_level>
30  ***/
31 
32 #include "asterisk.h"
33 
34 #include <ctype.h> /* for tolower */
35 
36 #include "asterisk/module.h"
37 #include "asterisk/format.h"
38 #include "asterisk/logger.h" /* for ast_log, LOG_WARNING */
39 #include "asterisk/strings.h" /* for ast_str_append */
40 #include "asterisk/utils.h" /* for MIN, ast_malloc, ast_free */
41 
42 struct vp8_attr {
43  unsigned int maximum_frame_rate;
44  unsigned int maximum_frame_size;
45 };
46 
47 static struct vp8_attr default_vp8_attr = {
48  .maximum_frame_rate = UINT_MAX,
49  .maximum_frame_size = UINT_MAX,
50 };
51 
52 static void vp8_destroy(struct ast_format *format)
53 {
54  struct vp8_attr *attr = ast_format_get_attribute_data(format);
55 
56  ast_free(attr);
57 }
58 
59 static int vp8_clone(const struct ast_format *src, struct ast_format *dst)
60 {
61  struct vp8_attr *original = ast_format_get_attribute_data(src);
62  struct vp8_attr *attr = ast_malloc(sizeof(*attr));
63 
64  if (!attr) {
65  return -1;
66  }
67 
68  if (original) {
69  *attr = *original;
70  } else {
71  *attr = default_vp8_attr;
72  }
73 
75 
76  return 0;
77 }
78 
79 static struct ast_format *vp8_parse_sdp_fmtp(const struct ast_format *format, const char *attributes)
80 {
81  char *attribs = ast_strdupa(attributes), *attrib;
82  struct ast_format *cloned;
83  struct vp8_attr *attr;
84  const char *kvp;
85  unsigned int val;
86 
87  cloned = ast_format_clone(format);
88  if (!cloned) {
89  return NULL;
90  }
91  attr = ast_format_get_attribute_data(cloned);
92 
93  /* lower-case everything, so we are case-insensitive */
94  for (attrib = attribs; *attrib; ++attrib) {
95  *attrib = tolower(*attrib);
96  } /* based on channels/chan_sip.c:process_a_sdp_image() */
97 
98  if ((kvp = strstr(attribs, "max-fr")) && sscanf(kvp, "max-fr=%30u", &val) == 1) {
99  attr->maximum_frame_rate = val;
100  } else {
101  attr->maximum_frame_rate = UINT_MAX;
102  }
103 
104  if ((kvp = strstr(attribs, "max-fs")) && sscanf(kvp, "max-fs=%30u", &val) == 1) {
105  attr->maximum_frame_size = val;
106  } else {
107  attr->maximum_frame_size = UINT_MAX;
108  }
109 
110  return cloned;
111 }
112 
113 static void vp8_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str)
114 {
115  struct vp8_attr *attr = ast_format_get_attribute_data(format);
116  int added = 0;
117 
118  if (!attr) {
119  /*
120  * (Only) cached formats do not have attribute data assigned because
121  * they were created before this attribute module was registered.
122  * Therefore, we assume the default attribute values here.
123  */
124  attr = &default_vp8_attr;
125  }
126 
127  if (UINT_MAX != attr->maximum_frame_rate) {
128  if (added) {
129  ast_str_append(str, 0, ";");
130  } else if (0 < ast_str_append(str, 0, "a=fmtp:%u ", payload)) {
131  added = 1;
132  }
133  ast_str_append(str, 0, "max-fr=%u", attr->maximum_frame_rate);
134  }
135 
136  if (UINT_MAX != attr->maximum_frame_size) {
137  if (added) {
138  ast_str_append(str, 0, ";");
139  } else if (0 < ast_str_append(str, 0, "a=fmtp:%u ", payload)) {
140  added = 1;
141  }
142  ast_str_append(str, 0, "max-fs=%u", attr->maximum_frame_size);
143  }
144 
145  if (added) {
146  ast_str_append(str, 0, "\r\n");
147  }
148 }
149 
150 static struct ast_format *vp8_getjoint(const struct ast_format *format1, const struct ast_format *format2)
151 {
152  struct vp8_attr *attr1 = ast_format_get_attribute_data(format1);
153  struct vp8_attr *attr2 = ast_format_get_attribute_data(format2);
154  struct ast_format *jointformat;
155  struct vp8_attr *attr_res;
156 
157  if (!attr1) {
158  attr1 = &default_vp8_attr;
159  }
160 
161  if (!attr2) {
162  attr2 = &default_vp8_attr;
163  }
164 
165  jointformat = ast_format_clone(format1);
166  if (!jointformat) {
167  return NULL;
168  }
169  attr_res = ast_format_get_attribute_data(jointformat);
170 
171  attr_res->maximum_frame_rate = MIN(attr1->maximum_frame_rate, attr2->maximum_frame_rate);
172  attr_res->maximum_frame_size = MIN(attr1->maximum_frame_size, attr2->maximum_frame_size);
173 
174  return jointformat;
175 }
176 
177 static struct ast_format *vp8_set(const struct ast_format *format, const char *name, const char *value)
178 {
179  struct ast_format *cloned;
180  struct vp8_attr *attr;
181  unsigned int val;
182 
183  if (sscanf(value, "%30u", &val) != 1) {
184  ast_log(LOG_WARNING, "Unknown value '%s' for attribute type '%s'\n",
185  value, name);
186  return NULL;
187  }
188 
189  cloned = ast_format_clone(format);
190  if (!cloned) {
191  return NULL;
192  }
193  attr = ast_format_get_attribute_data(cloned);
194 
195  if (!strcasecmp(name, "maximum_frame_rate")) {
196  attr->maximum_frame_rate = val;
197  } else if (!strcasecmp(name, "maximum_frame_size")) {
198  attr->maximum_frame_size = val;
199  } else {
200  ast_log(LOG_WARNING, "unknown attribute type %s\n", name);
201  }
202 
203  return cloned;
204 }
205 
208  .format_clone = vp8_clone,
209  .format_get_joint = vp8_getjoint,
210  .format_attribute_set = vp8_set,
211  .format_parse_sdp_fmtp = vp8_parse_sdp_fmtp,
212  .format_generate_sdp_fmtp = vp8_generate_sdp_fmtp,
213 };
214 
215 static int load_module(void)
216 {
217  if (ast_format_interface_register("vp8", &vp8_interface)) {
219  }
220 
222 }
223 
224 static int unload_module(void)
225 {
226  return 0;
227 }
228 
229 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "VP8 Format Attribute Module",
230  .support_level = AST_MODULE_SUPPORT_CORE,
231  .load = load_module,
232  .unload = unload_module,
233  .load_pri = AST_MODPRI_CHANNEL_DEPEND,
234 );
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
void * ast_format_get_attribute_data(const struct ast_format *format)
Get the attribute data on a format.
Definition: format.c:125
static struct ast_format_interface vp8_interface
const char * str
Definition: app_jack.c:147
#define NULL
Definition: resample.c:96
int value
Definition: syslog.c:37
unsigned int maximum_frame_size
Utility functions.
Media Format API.
unsigned int maximum_frame_rate
#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
#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 ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
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 void vp8_destroy(struct ast_format *format)
static const char name[]
Definition: cdr_mysql.c:74
#define ast_free(a)
Definition: astmm.h:182
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
Support for logging to various files, console and syslog Configuration in file logger.conf.
static struct ast_format * vp8_getjoint(const struct ast_format *format1, const struct ast_format *format2)
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 struct vp8_attr default_vp8_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
static int unload_module(void)
static int load_module(void)
static int vp8_clone(const struct ast_format *src, struct ast_format *dst)
static void vp8_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str)
#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
static struct ast_format * vp8_parse_sdp_fmtp(const struct ast_format *format, const char *attributes)
static struct ast_format * vp8_set(const struct ast_format *format, const char *name, const char *value)