Debian Codegraph @

Xamr/res/res_format_attr_amr.c

raw symbols SCIP
1 #include "asterisk.h"
2
3 /* version 4.0, compatiblity overview as of October 2015 */
4 /* based on res/res_format_attr_silk.c */
5
6 #include <ctype.h> /* for tolower */
7 #include <math.h> /* for log10, floor */
8
9 #include "asterisk/module.h"
10 #include "asterisk/format.h"
11 #include "asterisk/astobj2.h" /* for ao2_bump */
12 #include "asterisk/format_cache.h" /* for ast_format_amr(wb) */
13 #include "asterisk/logger.h" /* for ast_debug, ast_log, etc */
14 #include "asterisk/strings.h" /* for ast_str_append */
15 #include "asterisk/utils.h" /* for MAX, ast_calloc, ast_free, etc */
16
17 #include "asterisk/amr.h"
18
19 /* Asterisk internal defaults; can differ from RFC defaults */
20 static struct amr_attr default_amr_attr = {
21 .octet_align = 0, /* bandwidth efficient */
22 .mode_set = 0, /* all modes */
23 .mode_change_period = 0, /* not specified */
24 .mode_change_capability = 0, /* not supported */
25 .mode_change_neighbor = 0, /* change to any */
26 .crc = 0, /* off */
27 .robust_sorting = 0, /* off */
28 .interleaving = 0, /* off */
29 .max_red = -1, /* no redundancy limit */
30 };
31
32 static void amr_destroy(struct ast_format *format)
33 {
34 struct amr_attr *attr = ast_format_get_attribute_data(format);
35
36 ast_free(attr);
37 }
38
39 static int amr_clone(const struct ast_format *src, struct ast_format *dst)
40 {
41 struct amr_attr *original = ast_format_get_attribute_data(src);
42 struct amr_attr *attr = ast_malloc(sizeof(*attr));
43
44 if (!attr) {
45 return -1;
46 }
47
48 if (original) {
49 *attr = *original;
50 } else {
51 *attr = default_amr_attr;
52 /* internal variables for transcoding module */
53 if (16000 == ast_format_get_sample_rate(src)) {
54 attr->mode_current = 8;
55 attr->vad = 0;
56 } else {
57 attr->mode_current = 7;
58 attr->vad = 1;
59 }
60 }
61
62 ast_format_set_attribute_data(dst, attr);
63
64 return 0;
65 }
66
67 static struct ast_format *amr_parse_sdp_fmtp(const struct ast_format *format, const char *attrib)
68 {
69 struct ast_format *cloned;
70 struct amr_attr *attr;
71 unsigned int val;
72 char *attributes;
73 char *tmp;
74 const unsigned int size = 9; /* same as bit-field definition of mode_set */
75 int v[size];
76 /* init each slot as 'not specified' */
77 for (val = 0; val < size; val = val + 1) {
78 v[val] = -1;
79 }
80
81 cloned = ast_format_clone(format);
82 if (!cloned) {
83 return NULL;
84 }
85 attr = ast_format_get_attribute_data(cloned);
86
87 /* lower-case everything, so we are case-insensitive */
88 /* no implementation is known which is affected by this */
89 attributes = ast_strdupa(attrib);
90 for (tmp = attributes; *tmp; ++tmp) {
91 *tmp = tolower(*tmp);
92 } /* based on channels/chan_sip.c:process_a_sdp_image() */
93
94 attr->octet_align = 0;
95 tmp = strstr(attributes, "octet-align=");
96 if (tmp) {
97 if (sscanf(tmp, "octet-align=%30u", &val) == 1) {
98 attr->octet_align = val;
99 }
100 }
101
102 attr->mode_set = 0;
103 tmp = strstr(attributes, "mode-set=");
104 if (tmp) {
105 if (sscanf(tmp, "mode-set=%30u,%30u,%30u,%30u,%30u,%30u,%30u,%30u,%30u",
106 &v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &v[6], &v[7], &v[8]) > 0) {
107 for (val = 0; val < size; val = val + 1) {
108 if (0 <= v[val] && v[val] < size) {
109 attr->mode_set = (attr->mode_set | (1 << v[val]));
110 }
111 }
112 }
113 }
114 if (0 < attr->mode_set) {
115 attr->mode_current = floor(log10(attr->mode_set) / log10(2));
116 }
117
118 attr->mode_change_capability = 0;
119 tmp = strstr(attributes, "mode-change-capability=");
120 if (tmp) {
121 if (sscanf(tmp, "mode-change-capability=%30u", &val) == 1) {
122 attr->mode_change_capability = val;
123 }
124 }
125
126 attr->mode_change_period = 0;
127 tmp = strstr(attributes, "mode-change-period=");
128 if (tmp) {
129 if (sscanf(tmp, "mode-change-period=%30u", &val) == 1) {
130 attr->mode_change_period = val;
131 }
132 }
133
134 attr->mode_change_neighbor = 0;
135 tmp = strstr(attributes, "mode-change-neighbor=");
136 if (tmp) {
137 if (sscanf(tmp, "mode-change-neighbor=%30u", &val) == 1) {
138 attr->mode_change_neighbor = val;
139 }
140 }
141
142 attr->crc = 0;
143 tmp = strstr(attributes, "crc=");
144 if (tmp) {
145 if (sscanf(tmp, "crc=%30u", &val) == 1) {
146 attr->crc = val;
147 if (attr->crc) {
148 attr->octet_align = 1;
149 }
150 }
151 }
152
153 attr->robust_sorting = 0;
154 tmp = strstr(attributes, "robust-sorting=");
155 if (tmp) {
156 if (sscanf(tmp, "robust-sorting=%30u", &val) == 1) {
157 attr->robust_sorting = val;
158 if (attr->robust_sorting) {
159 attr->octet_align = 1;
160 }
161 }
162 }
163
164 attr->interleaving = 0;
165 tmp = strstr(attributes, "interleaving=");
166 if (tmp) {
167 if (sscanf(tmp, "interleaving=%30u", &val) == 1) {
168 attr->interleaving = val;
169 if (attr->interleaving) {
170 attr->octet_align = 1;
171 }
172 }
173 }
174
175 attr->max_red = -1;
176 tmp = strstr(attributes, "max-red=");
177 if (tmp) {
178 if (sscanf(tmp, "max-red=%30u", &val) == 1) {
179 attr->max_red = val;
180 }
181 }
182
183 return cloned;
184 }
185
186 static void amr_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str)
187 {
188 int appended = 0;
189 int listed = 0;
190 struct amr_attr *attr = ast_format_get_attribute_data(format);
191
192 if (!attr) {
193 attr = &default_amr_attr;
194 }
195
196 if (0 != attr->octet_align) {
197 if (0 == appended) {
198 ast_str_append(str, 0, "a=fmtp:%d ", payload);
199 } else {
200 ast_str_append(str, 0, ";");
201 }
202 ast_str_append(str, 0, "octet-align=%d", attr->octet_align);
203 appended = appended + 1;
204 }
205 if (0 != attr->mode_set)
206 {
207 if (0 == appended) {
208 ast_str_append(str, 0, "a=fmtp:%d ", payload);
209 } else {
210 ast_str_append(str, 0, ";");
211 }
212 ast_str_append(str, 0, "mode-set=");
213 if (attr->mode_set & 0x01) {
214 if (0 == listed) {
215 ast_str_append(str, 0, "0");
216 } else {
217 ast_str_append(str, 0, ",0");
218 }
219 listed = listed + 1;
220 }
221 if (attr->mode_set & 0x02) {
222 if (0 == listed) {
223 ast_str_append(str, 0, "1");
224 } else {
225 ast_str_append(str, 0, ",1");
226 }
227 listed = listed + 1;
228 }
229 if (attr->mode_set & 0x04) {
230 if (0 == listed) {
231 ast_str_append(str, 0, "2");
232 } else {
233 ast_str_append(str, 0, ",2");
234 }
235 listed = listed + 1;
236 }
237 if (attr->mode_set & 0x08) {
238 if (0 == listed) {
239 ast_str_append(str, 0, "3");
240 } else {
241 ast_str_append(str, 0, ",3");
242 }
243 listed = listed + 1;
244 }
245 if (attr->mode_set & 0x10) {
246 if (0 == listed) {
247 ast_str_append(str, 0, "4");
248 } else {
249 ast_str_append(str, 0, ",4");
250 }
251 listed = listed + 1;
252 }
253 if (attr->mode_set & 0x20) {
254 if (0 == listed) {
255 ast_str_append(str, 0, "5");
256 } else {
257 ast_str_append(str, 0, ",5");
258 }
259 listed = listed + 1;
260 }
261 if (attr->mode_set & 0x40) {
262 if (0 == listed) {
263 ast_str_append(str, 0, "6");
264 } else {
265 ast_str_append(str, 0, ",6");
266 }
267 listed = listed + 1;
268 }
269 if (attr->mode_set & 0x80) {
270 if (0 == listed) {
271 ast_str_append(str, 0, "7");
272 } else {
273 ast_str_append(str, 0, ",7");
274 }
275 listed = listed + 1;
276 }
277 if (attr->mode_set & 0x100) {
278 if (0 == listed) {
279 ast_str_append(str, 0, "8");
280 } else {
281 ast_str_append(str, 0, ",8");
282 }
283 listed = listed + 1;
284 }
285 appended = appended + 1;
286 }
287 if (0 != attr->mode_change_capability) {
288 if (0 == appended) {
289 ast_str_append(str, 0, "a=fmtp:%d ", payload);
290 } else {
291 ast_str_append(str, 0, ";");
292 }
293 ast_str_append(str, 0, "mode-change-capability=%d", attr->mode_change_capability);
294 appended = appended + 1;
295 }
296 if (0 != attr->mode_change_period) {
297 if (0 == appended) {
298 ast_str_append(str, 0, "a=fmtp:%d ", payload);
299 } else {
300 ast_str_append(str, 0, ";");
301 }
302 ast_str_append(str, 0, "mode-change-period=%d", attr->mode_change_period);
303 appended = appended + 1;
304 }
305 if (0 != attr->mode_change_neighbor) {
306 if (0 == appended) {
307 ast_str_append(str, 0, "a=fmtp:%d ", payload);
308 } else {
309 ast_str_append(str, 0, ";");
310 }
311 ast_str_append(str, 0, "mode-change-neighbor=%d", attr->mode_change_neighbor);
312 appended = appended + 1;
313 }
314 if (0 != attr->crc) {
315 if (0 == appended) {
316 ast_str_append(str, 0, "a=fmtp:%d ", payload);
317 } else {
318 ast_str_append(str, 0, ";");
319 }
320 ast_str_append(str, 0, "crc=%d", attr->crc);
321 appended = appended + 1;
322 }
323 if (0 != attr->robust_sorting) {
324 if (0 == appended) {
325 ast_str_append(str, 0, "a=fmtp:%d ", payload);
326 } else {
327 ast_str_append(str, 0, ";");
328 }
329 ast_str_append(str, 0, "robust-sorting=%d", attr->robust_sorting);
330 appended = appended + 1;
331 }
332 if (0 != attr->interleaving) {
333 if (0 == appended) {
334 ast_str_append(str, 0, "a=fmtp:%d ", payload);
335 } else {
336 ast_str_append(str, 0, ";");
337 }
338 ast_str_append(str, 0, "interleaving=%d", attr->interleaving);
339 appended = appended + 1;
340 }
341 if (0 <= attr->max_red) {
342 if (0 == appended) {
343 ast_str_append(str, 0, "a=fmtp:%d ", payload);
344 } else {
345 ast_str_append(str, 0, ";");
346 }
347 ast_str_append(str, 0, "max-red=%d", attr->max_red);
348 appended = appended + 1;
349 }
350 if (0 != appended) {
351 ast_str_append(str, 0, "\r\n");
352 }
353 /*
354 * AMR-WB, GSM gateway compatible setting:
355 * ast_str_append(str, 0, "a=fmtp:%d mode-set=0,1,2; mode-change-period=2; mode-change-neighbor=1\r\n", payload);
356 * less than 25kb/s in SIP RTP which equals iLBC:30 in data traffic, and AMR-WB is a wide-band codec!
357 */
358 }
359
360 /*
361 * Octet Alignment
362 *
363 * AMR and AMR-WB support 'octet-align=1' which does not stuff the
364 * header bits over octet borders and which is readable in
365 * Wireshark. This parameter is negotiated via SDP. Belledonne and
366 * CounterPath do not offer AMR, only AMR-WB. Android offers only AMR.
367 *
368 * Nokia Symbian/S60: configurable via user-interface; default: off
369 * Nokia Series 40: configurable via OMA Client Provisioning (OMA CP); default: off
370 * Nokia Asha Software Platform: configurable via OMA CP; default: on
371 * CounterPath Bria (iOS): no setting known; off
372 * CounterPath Bria (Android): see iOS
373 * CounterPath Bria (BlackBerry 10): AMR/AMR-WB not available, as of version 1.3.1
374 * Bug: since version 3.4, fmtp is missing = negotiating octet-aligned mode fails
375 * BeeHD (iOS): no setting known; on
376 * Bug: Nokia Symbian/S60 calls BeeHD, no audio in BeeHD; AMR-WB works
377 * Belledonne Linphone 1.0.2 (Windows Phone 8): no setting known; on
378 * Bug: ignores octet-align=0 in fmtp = distorted audio
379 * CSipSimple 1.02r2330 (Android): no setting known; off
380 * Bug: CSipSimple calls a Nokia Symbian/S60, no audio in Nokia; AMR works
381 * Bug: AMR-WB ignores octet-align=1 in fmtp = distorted audio; AMR works
382 * fixed upstream with <https://trac.pjsip.org/repos/changeset/5122>
383 * Google Android 5: no setting known; off
384 * Join (iOS): no setting known; on
385 * Bug: AMR-WB ignores octet-align=0 in fmtp = distorted audio; AMR works
386 * PortGo (iOS): no setting known; off (is able to change alignment through negotiation)
387 */
388 static struct ast_format *amr_getjoint(const struct ast_format *format1, const struct ast_format *format2)
389 {
390 struct amr_attr *attr1 = ast_format_get_attribute_data(format1);
391 struct amr_attr *attr2 = ast_format_get_attribute_data(format2);
392 struct amr_attr *attr_res;
393 struct ast_format *jointformat = NULL;
394
395 if (!attr1) {
396 attr1 = &default_amr_attr;
397 }
398
399 if (!attr2) {
400 attr2 = &default_amr_attr;
401 }
402
403 if (format1 == ast_format_amrwb || format1 == ast_format_amr) {
404 jointformat = (struct ast_format *) format2;
405 }
406 if (format2 == ast_format_amrwb || format2 == ast_format_amr) {
407 jointformat = (struct ast_format *) format1;
408 }
409 if (format1 == format2) {
410 if (!jointformat) {
411 ast_debug(3, "Both formats were not cached but the same.\n");
412 jointformat = (struct ast_format *) format1;
413 } else {
414 ast_debug(3, "Both formats were cached.\n");
415 jointformat = NULL;
416 }
417 }
418 if (!jointformat) {
419 ast_debug(3, "Which pointer shall be returned? Let us create a new one!\n");
420 jointformat = ast_format_clone(format1);
421 } else {
422 ao2_bump(jointformat);
423 }
424 if (!jointformat) {
425 return NULL;
426 }
427 attr_res = ast_format_get_attribute_data(jointformat);
428
429 if (0 == attr1->mode_set && 0 == attr2->mode_set) {
430 attr_res->mode_set = 0; /* both allowed all = 0 */
431 } else if (0 != attr1->mode_set && 0 == attr2->mode_set) {
432 attr_res->mode_set = attr1->mode_set; /* attr2 allowed all */
433 } else if (0 == attr1->mode_set && 0 != attr2->mode_set) {
434 attr_res->mode_set = attr2->mode_set; /* attr1 allowed all */
435 } else { /* both parties restrict, let us check if they match */
436 attr_res->mode_set = (attr1->mode_set & attr2->mode_set);
437 if (0 == attr_res->mode_set) {
438 /* not expected because everyone supports 0,1,2 */
439 ast_log(LOG_WARNING, "mode-set did not match\n");
440 return NULL;
441 }
442 }
443 attr_res->mode_change_period = MAX(attr1->mode_change_period, attr2->mode_change_period);
444 attr_res->mode_change_capability = MAX(attr1->mode_change_capability, attr2->mode_change_capability);
445 attr_res->mode_change_neighbor = MAX(attr1->mode_change_neighbor, attr2->mode_change_neighbor);
446 attr_res->crc = MAX(attr1->crc, attr2->crc);
447 attr_res->robust_sorting = MAX(attr1->robust_sorting, attr2->robust_sorting);
448 attr_res->interleaving = MAX(attr1->interleaving, attr2->interleaving);
449 attr_res->max_red = MAX(attr1->max_red, attr2->max_red);
450
451 /* internal variables for transcoding module */
452 /* starting point; later, changes with a change-mode request (CMR) */
453 if (0 < attr_res->mode_set) {
454 attr_res->mode_current = floor(log10(attr_res->mode_set) / log10(2));
455 }
456 attr_res->vad = MAX(attr1->vad, attr2->vad);
457
458 return jointformat;
459 }
460
461 static struct ast_format_interface amr_interface = {
462 .format_destroy = amr_destroy,
463 .format_clone = amr_clone,
464 .format_cmp = NULL,
465 .format_get_joint = amr_getjoint,
466 .format_attribute_set = NULL,
467 .format_parse_sdp_fmtp = amr_parse_sdp_fmtp,
468 .format_generate_sdp_fmtp = amr_generate_sdp_fmtp,
469 };
470
471 static int load_module(void)
472 {
473 if (ast_format_interface_register("amr", &amr_interface)) {
474 return AST_MODULE_LOAD_DECLINE;
475 }
476
477 if (ast_format_interface_register("amrwb", &amr_interface)) {
478 return AST_MODULE_LOAD_DECLINE;
479 }
480
481 return AST_MODULE_LOAD_SUCCESS;
482 }
483
484 static int unload_module(void)
485 {
486 return 0;
487 }
488
489 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER,
490 "AMR Format Attribute Module",
491 .load = load_module,
492 .unload = unload_module,
493 .load_pri = AST_MODPRI_CHANNEL_DEPEND,
494 );