40 #include "asterisk/opus.h"
55 int spropmaxcapturerate;
62 static struct opus_attr default_opus_attr = {
63 .maxbitrate = CODEC_OPUS_DEFAULT_BITRATE,
65 .ptime = CODEC_OPUS_DEFAULT_PTIME,
66 .stereo = CODEC_OPUS_DEFAULT_STEREO,
67 .cbr = CODEC_OPUS_DEFAULT_CBR,
68 .fec = CODEC_OPUS_DEFAULT_FEC,
69 .dtx = CODEC_OPUS_DEFAULT_DTX,
71 .spropstereo = CODEC_OPUS_DEFAULT_STEREO,
72 .maxptime = CODEC_OPUS_DEFAULT_MAX_PTIME
75 static void opus_destroy(
struct ast_format *format)
83 ao2_cleanup(attr->data);
96 *attr = original ? *original : default_opus_attr;
105 static void sdp_fmtp_get(
const char *attributes,
const char *name,
int *attr)
107 const char *kvp = attributes;
110 if (ast_strlen_zero(attributes)) {
122 if (!strncmp(kvp, name, strlen(name)) && kvp[strlen(name)] ==
'=') {
123 if (sscanf(kvp,
"%*[^=]=%30d", &val) == 1) {
130 kvp = strchr(kvp,
';');
139 static struct ast_format *opus_parse_sdp_fmtp(
const struct ast_format *format,
const char *attributes)
153 for (attrib = attribs; *attrib; ++attrib) {
154 *attrib = tolower(*attrib);
161 &attr->spropmaxcapturerate);
177 static void opus_generate_sdp_fmtp(
const struct ast_format *format,
unsigned int payload,
struct ast_str **str)
189 attr = &default_opus_attr;
205 if (CODEC_OPUS_DEFAULT_BITRATE != attr->maxbitrate || attr->maxbitrate > 0) {
210 if (CODEC_OPUS_DEFAULT_STEREO != attr->stereo) {
215 if (CODEC_OPUS_DEFAULT_STEREO != attr->spropstereo) {
220 if (CODEC_OPUS_DEFAULT_CBR != attr->cbr) {
225 if (CODEC_OPUS_DEFAULT_FEC!= attr->fec) {
230 if (CODEC_OPUS_DEFAULT_DTX != attr->dtx) {
251 attr1 = &default_opus_attr;
255 attr2 = &default_opus_attr;
268 attr_res->dtx = attr1->dtx || attr2->dtx ? 1 : 0;
272 attr_res->fec = attr1->fec && attr2->fec ? 1 : 0;
274 attr_res->cbr = attr1->cbr || attr2->cbr ? 1 : 0;
275 attr_res->spropstereo = attr1->spropstereo || attr2->spropstereo ? 1 : 0;
279 attr_res->stereo = attr1->stereo && attr2->stereo ? 1 : 0;
281 if (attr1->maxbitrate < 0) {
282 attr_res->maxbitrate = attr2->maxbitrate;
283 }
else if (attr2->maxbitrate < 0) {
284 attr_res->maxbitrate = attr1->maxbitrate;
286 attr_res->maxbitrate = MIN(attr1->maxbitrate, attr2->maxbitrate);
289 attr_res->spropmaxcapturerate = MIN(attr1->spropmaxcapturerate, attr2->spropmaxcapturerate);
290 attr_res->maxplayrate = MIN(attr1->maxplayrate, attr2->maxplayrate);
296 const char *name,
const char *value)
309 ao2_cleanup(attr->data);
310 attr->data =
ao2_bump((
void*)value);
314 if (sscanf(value,
"%30d", &val) != 1) {
315 ast_log(LOG_WARNING,
"Unknown value '%s' for attribute type '%s'\n",
322 attr->maxplayrate = val;
324 attr->maxplayrate = val;
326 attr->spropmaxcapturerate = val;
328 attr->maxptime = val;
332 attr->maxbitrate = val;
336 attr->spropstereo = val;
344 ast_log(LOG_WARNING,
"unknown attribute type %s\n", name);
350 static const void *opus_get(
const struct ast_format *format,
const char *name)
362 val = &attr->maxplayrate;
364 val = &attr->spropmaxcapturerate;
366 val = &attr->maxptime;
370 val = &attr->maxbitrate;
374 val = &attr->spropstereo;
382 ast_log(LOG_WARNING,
"unknown attribute type %s\n", name);
390 .format_clone = opus_clone,
391 .format_get_joint = opus_getjoint,
392 .format_attribute_set = opus_set,
393 .format_parse_sdp_fmtp = opus_parse_sdp_fmtp,
394 .format_generate_sdp_fmtp = opus_generate_sdp_fmtp,
395 .format_attribute_get = opus_get
398 static int load_module(
void)
407 static int unload_module(
void)
412 AST_MODULE_INFO(
ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER,
"Opus Format Attribute Module",
413 .support_level = AST_MODULE_SUPPORT_CORE,
415 .unload = unload_module,
#define CODEC_OPUS_DEFAULT_SAMPLE_RATE
Default attribute values.
Asterisk main include file. File version handling, generic pbx functions.
String manipulation functions.
#define CODEC_OPUS_ATTR_MAX_PTIME
Maximum duration of packet (in milliseconds)
#define CODEC_OPUS_ATTR_STEREO
Decode stereo (1) vs mono (0)
#define CODEC_OPUS_ATTR_PTIME
Duration of packet (in milliseconds)
#define CODEC_OPUS_ATTR_DTX
Use discontinuous transmission (1) or not (0)
#define CODEC_OPUS_ATTR_FEC
Use forward error correction (1) or not (0)
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
#define CODEC_OPUS_ATTR_DATA
Custom data object.
Opus attribute structure.
char * ast_str_truncate(struct ast_str *buf, ssize_t len)
Truncates the enclosed string to the given length.
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
#define CODEC_OPUS_ATTR_SPROP_MAX_CAPTURE_RATE
Maximum sampling rate an endpoint is capable of sending.
#define CODEC_OPUS_ATTR_MAX_AVERAGE_BITRATE
Maximum average received bit rate (in bits per second)
#define ast_strdupa(s)
duplicate a string in memory from the stack
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
#define ast_malloc(len)
A wrapper for malloc()
#define CODEC_OPUS_ATTR_CBR
Decoder prefers a constant (1) vs variable (0) bitrate.
Support for dynamic strings.
char * ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
Support for logging to various files, console and syslog Configuration in file logger.conf.
Module has failed to load, may be in an inconsistent state.
#define CODEC_OPUS_ATTR_MAX_PLAYBACK_RATE
Maximum sampling rate an endpoint is capable of receiving.
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
#define CODEC_OPUS_ATTR_SPROP_STEREO
Likeliness of sender producing stereo (1) vs mono (0)
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
#define CODEC_OPUS_ATTR_MAX_CODED_AUDIO_BANDWIDTH
An alias for maxplaybackrate (used in older versions)