| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include <ctype.h> |
| 35 |
|
| 36 |
#include "asterisk/module.h" |
| 37 |
#include "asterisk/format.h" |
| 38 |
#include "asterisk/logger.h" |
| 39 |
#include "asterisk/strings.h" |
| 40 |
#include "asterisk/utils.h" |
| 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 |
|
| 94 |
for (attrib = attribs; *attrib; ++attrib) { |
| 95 |
*attrib = tolower(*attrib); |
| 96 |
} |
| 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 |
|
| 121 |
|
| 122 |
|
| 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 |
); |