Debian Codegraph @

res/res_format_attr_vp8.c

raw symbols SCIP
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
74 ast_format_set_attribute_data(dst, attr);
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
206 static struct ast_format_interface vp8_interface = {
207 .format_destroy = vp8_destroy,
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)) {
218 return AST_MODULE_LOAD_DECLINE;
219 }
220
221 return AST_MODULE_LOAD_SUCCESS;
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 );