| 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 <math.h> |
| 35 |
|
| 36 |
#include "asterisk/module.h" |
| 37 |
#include "asterisk/cli.h" |
| 38 |
#include "asterisk/channel.h" |
| 39 |
#include "asterisk/framehook.h" |
| 40 |
#include "asterisk/rtp_engine.h" |
| 41 |
|
| 42 |
struct remb_values { |
| 43 |
|
| 44 |
float receive_bitrate; |
| 45 |
|
| 46 |
float send_bitrate; |
| 47 |
}; |
| 48 |
|
| 49 |
static void remb_values_free(void *data) |
| 50 |
{ |
| 51 |
ast_free(data); |
| 52 |
} |
| 53 |
|
| 54 |
static const struct ast_datastore_info remb_info = { |
| 55 |
.type = "REMB Values", |
| 56 |
.destroy = remb_values_free, |
| 57 |
}; |
| 58 |
|
| 59 |
static struct ast_frame *remb_hook_event_cb(struct ast_channel *chan, struct ast_frame *frame, enum ast_framehook_event event, void *data) |
| 60 |
{ |
| 61 |
struct ast_rtp_rtcp_feedback *feedback; |
| 62 |
struct ast_datastore *remb_store; |
| 63 |
struct remb_values *remb_values; |
| 64 |
int exp; |
| 65 |
float bitrate = 0.0; |
| 66 |
|
| 67 |
if (!frame) { |
| 68 |
return NULL; |
| 69 |
} |
| 70 |
|
| 71 |
switch (event) { |
| 72 |
case AST_FRAMEHOOK_EVENT_READ: |
| 73 |
case AST_FRAMEHOOK_EVENT_WRITE: |
| 74 |
break; |
| 75 |
case AST_FRAMEHOOK_EVENT_ATTACHED: |
| 76 |
case AST_FRAMEHOOK_EVENT_DETACHED: |
| 77 |
return frame; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
if (frame->subclass.integer != AST_RTP_RTCP_PSFB) { |
| 82 |
return frame; |
| 83 |
} |
| 84 |
|
| 85 |
feedback = frame->data.ptr; |
| 86 |
if (feedback->fmt != AST_RTP_RTCP_FMT_REMB) { |
| 87 |
return frame; |
| 88 |
} |
| 89 |
|
| 90 |
remb_store = ast_channel_datastore_find(chan, &remb_info, NULL); |
| 91 |
if (!remb_store) { |
| 92 |
return frame; |
| 93 |
} |
| 94 |
remb_values = remb_store->data; |
| 95 |
|
| 96 |
|
| 97 |
if (event == AST_FRAMEHOOK_EVENT_READ && remb_values->receive_bitrate) { |
| 98 |
bitrate = remb_values->receive_bitrate; |
| 99 |
} else if (event == AST_FRAMEHOOK_EVENT_WRITE && remb_values->send_bitrate) { |
| 100 |
bitrate = remb_values->send_bitrate; |
| 101 |
} else { |
| 102 |
return frame; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
frexp(bitrate, &exp); |
| 147 |
exp = exp > 18 ? exp - 18 : 0; |
| 148 |
|
| 149 |
feedback->remb.br_mantissa = bitrate / (1 << exp); |
| 150 |
feedback->remb.br_exp = exp; |
| 151 |
|
| 152 |
return frame; |
| 153 |
} |
| 154 |
|
| 155 |
static char *handle_remb_set(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 156 |
{ |
| 157 |
struct ast_channel *chan; |
| 158 |
unsigned int bitrate; |
| 159 |
struct ast_datastore *remb_store; |
| 160 |
struct remb_values *remb_values; |
| 161 |
struct ast_framehook_interface interface = { |
| 162 |
.version = AST_FRAMEHOOK_INTERFACE_VERSION, |
| 163 |
.event_cb = remb_hook_event_cb, |
| 164 |
}; |
| 165 |
|
| 166 |
switch(cmd) { |
| 167 |
case CLI_INIT: |
| 168 |
e->command = "remb set {send|receive}"; |
| 169 |
e->usage = |
| 170 |
"Usage: remb set {send|receive} <channel> <bitrate in bits>\n" |
| 171 |
" Set the REMB value which overwrites what we send or receive\n"; |
| 172 |
return NULL; |
| 173 |
case CLI_GENERATE: |
| 174 |
return ast_complete_channels(a->line, a->word, a->pos, a->n, 3); |
| 175 |
} |
| 176 |
|
| 177 |
if (a->argc != 5) { |
| 178 |
return CLI_SHOWUSAGE; |
| 179 |
} |
| 180 |
|
| 181 |
if (sscanf(a->argv[4], "%30d", &bitrate) != 1) { |
| 182 |
ast_cli(a->fd, "%s is not a valid bitrate in bits\n", a->argv[4]); |
| 183 |
return CLI_SUCCESS; |
| 184 |
} else if (strcasecmp(a->argv[2], "send") && strcasecmp(a->argv[2], "receive")) { |
| 185 |
ast_cli(a->fd, "%s is not a valid direction for REMB\n", a->argv[2]); |
| 186 |
return CLI_SUCCESS; |
| 187 |
} |
| 188 |
|
| 189 |
chan = ast_channel_get_by_name(a->argv[3]); |
| 190 |
if (!chan) { |
| 191 |
ast_cli(a->fd, "%s is not a known channel\n", a->argv[3]); |
| 192 |
return CLI_SUCCESS; |
| 193 |
} |
| 194 |
|
| 195 |
ast_channel_lock(chan); |
| 196 |
|
| 197 |
remb_store = ast_channel_datastore_find(chan, &remb_info, NULL); |
| 198 |
if (!remb_store) { |
| 199 |
int framehook_id; |
| 200 |
|
| 201 |
framehook_id = ast_framehook_attach(chan, &interface); |
| 202 |
if (framehook_id < 0) { |
| 203 |
ast_cli(a->fd, "Could not attach framehook for modifying REMB\n"); |
| 204 |
ast_channel_unlock(chan); |
| 205 |
ast_channel_unref(chan); |
| 206 |
return CLI_SUCCESS; |
| 207 |
} |
| 208 |
|
| 209 |
remb_values = ast_calloc(1, sizeof(*remb_values)); |
| 210 |
if (!remb_values) { |
| 211 |
ast_cli(a->fd, "Could not create a place to store provided REMB value\n"); |
| 212 |
ast_framehook_detach(chan, framehook_id); |
| 213 |
ast_channel_unlock(chan); |
| 214 |
ast_channel_unref(chan); |
| 215 |
return CLI_SUCCESS; |
| 216 |
} |
| 217 |
|
| 218 |
remb_store = ast_datastore_alloc(&remb_info, NULL); |
| 219 |
if (!remb_store) { |
| 220 |
ast_cli(a->fd, "Could not create a place to store provided REMB value\n"); |
| 221 |
ast_framehook_detach(chan, framehook_id); |
| 222 |
ast_channel_unlock(chan); |
| 223 |
ast_channel_unref(chan); |
| 224 |
ast_free(remb_values); |
| 225 |
return CLI_SUCCESS; |
| 226 |
} |
| 227 |
|
| 228 |
remb_store->data = remb_values; |
| 229 |
ast_channel_datastore_add(chan, remb_store); |
| 230 |
} else { |
| 231 |
remb_values = remb_store->data; |
| 232 |
} |
| 233 |
|
| 234 |
if (!strcasecmp(a->argv[2], "send")) { |
| 235 |
remb_values->send_bitrate = bitrate; |
| 236 |
} else if (!strcasecmp(a->argv[2], "receive")) { |
| 237 |
remb_values->receive_bitrate = bitrate; |
| 238 |
} |
| 239 |
|
| 240 |
ast_channel_unlock(chan); |
| 241 |
ast_channel_unref(chan); |
| 242 |
|
| 243 |
ast_cli(a->fd, "Set REMB %s override to a bitrate of %s on %s\n", a->argv[2], a->argv[3], a->argv[4]); |
| 244 |
|
| 245 |
return CLI_SUCCESS; |
| 246 |
} |
| 247 |
|
| 248 |
static struct ast_cli_entry remb_cli[] = { |
| 249 |
AST_CLI_DEFINE(handle_remb_set, "Set the REMB value which overwrites what is sent or received"), |
| 250 |
}; |
| 251 |
|
| 252 |
static int load_module(void) |
| 253 |
{ |
| 254 |
ast_cli_register_multiple(remb_cli, ARRAY_LEN(remb_cli)); |
| 255 |
return AST_MODULE_LOAD_SUCCESS; |
| 256 |
} |
| 257 |
|
| 258 |
static int unload_module(void) |
| 259 |
{ |
| 260 |
ast_cli_unregister_multiple(remb_cli, ARRAY_LEN(remb_cli)); |
| 261 |
return 0; |
| 262 |
} |
| 263 |
|
| 264 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "REMB Modifier Module", |
| 265 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 266 |
.load = load_module, |
| 267 |
.unload = unload_module, |
| 268 |
); |