Asterisk - The Open Source Telephony Project GIT-master-f36a736
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 <dvossel@digium.com>
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 <dvossel@digium.com>
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 */
46struct celt_attr {
47 unsigned int samplerate;
48 unsigned int maxbitrate;
49 unsigned int framesize;
50};
51
52static 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
59static 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
77static 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
102static 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
113static 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)) {
121 }
122
124}
125
126static 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
153static 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
197static int load_module(void)
198{
201 }
202
204}
205
206static int unload_module(void)
207{
208 return 0;
209}
210
211AST_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,
const char * str
Definition: app_jack.c:147
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_log
Definition: astobj2.c:42
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
Media Format API.
void * ast_format_get_attribute_data(const struct ast_format *format)
Get the attribute data on a format.
Definition: format.c:125
void ast_format_set_attribute_data(struct ast_format *format, void *attribute_data)
Set the attribute data on a format.
Definition: format.c:130
ast_format_cmp_res
Format comparison results.
Definition: format.h:34
@ AST_FORMAT_CMP_EQUAL
Definition: format.h:36
@ AST_FORMAT_CMP_NOT_EQUAL
Definition: format.h:38
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_format_interface_register(codec, interface)
Register a format interface for use with the provided codec.
Definition: format.h:273
static const char name[]
Definition: format_mp3.c:68
Support for logging to various files, console and syslog Configuration in file logger....
#define LOG_WARNING
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:331
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:557
@ AST_MODPRI_CHANNEL_DEPEND
Definition: module.h:340
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static struct ast_format * celt_set(const struct ast_format *format, const char *name, const char *value)
static void celt_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str)
static void celt_destroy(struct ast_format *format)
static struct ast_format_interface celt_interface
static int load_module(void)
static struct ast_format * celt_parse_sdp_fmtp(const struct ast_format *format, const char *attributes)
static int unload_module(void)
static enum ast_format_cmp_res celt_cmp(const struct ast_format *format1, const struct ast_format *format2)
static struct ast_format * celt_getjoint(const struct ast_format *format1, const struct ast_format *format2)
static int celt_clone(const struct ast_format *src, struct ast_format *dst)
#define NULL
Definition: resample.c:96
String manipulation functions.
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:1139
Optional format interface to extend format operations.
Definition: format.h:44
void(*const format_destroy)(struct ast_format *format)
Callback for when the format is destroyed, used to release attribute resources.
Definition: format.h:50
Definition of a media format.
Definition: format.c:43
Support for dynamic strings.
Definition: strings.h:623
CELT attribute structure.
unsigned int samplerate
unsigned int maxbitrate
unsigned int framesize
Definition: ast_expr2.c:325
int value
Definition: syslog.c:37
Utility functions.
#define MIN(a, b)
Definition: utils.h:231