Add a 'opus show' CLI command.
Ripping off codec_dahdi in a major way.
Sean Bright
9 years ago
42 | 42 | |
43 | 43 | #include "asterisk/translate.h" |
44 | 44 | #include "asterisk/module.h" |
45 | #include "asterisk/cli.h" | |
45 | 46 | #include "asterisk/config.h" |
46 | 47 | #include "asterisk/utils.h" |
47 | 48 | |
54 | 55 | #include "asterisk/slin.h" |
55 | 56 | #include "ex_opus.h" |
56 | 57 | |
57 | static int encoder_id; | |
58 | static int decoder_id; | |
58 | static struct codec_usage { | |
59 | int encoder_id; | |
60 | int decoder_id; | |
61 | int encoders; | |
62 | int decoders; | |
63 | } usage; | |
59 | 64 | |
60 | 65 | /* Private structures */ |
61 | 66 | struct opus_coder_pvt { |
112 | 117 | |
113 | 118 | opus_encoder_ctl(opvt->opus, OPUS_SET_INBAND_FEC(opvt->fec)); |
114 | 119 | opvt->framesize = sampling_rate/50; |
115 | opvt->id = ast_atomic_fetchadd_int(&encoder_id, 1) + 1; | |
120 | opvt->id = ast_atomic_fetchadd_int(&usage.encoder_id, 1) + 1; | |
121 | ||
122 | ast_atomic_fetchadd_int(&usage.encoders, +1); | |
116 | 123 | |
117 | 124 | ast_debug(3, "Created encoder #%d (%d -> opus)\n", opvt->id, sampling_rate); |
118 | 125 | |
139 | 146 | return -1; |
140 | 147 | } |
141 | 148 | |
142 | opvt->id = ast_atomic_fetchadd_int(&decoder_id, 1) + 1; | |
149 | opvt->id = ast_atomic_fetchadd_int(&usage.decoder_id, 1) + 1; | |
150 | ||
151 | ast_atomic_fetchadd_int(&usage.decoders, +1); | |
143 | 152 | |
144 | 153 | ast_debug(3, "Created decoder #%d (opus -> %d)\n", opvt->id, sampling_rate); |
145 | 154 | |
250 | 259 | opus_encoder_destroy(opvt->opus); |
251 | 260 | opvt->opus = NULL; |
252 | 261 | |
262 | ast_atomic_fetchadd_int(&usage.encoders, -1); | |
263 | ||
253 | 264 | ast_debug(3, "Destroyed encoder #%d (%d->opus)\n", opvt->id, opvt->sampling_rate); |
254 | 265 | } |
255 | 266 | |
264 | 275 | opus_decoder_destroy(opvt->opus); |
265 | 276 | opvt->opus = NULL; |
266 | 277 | |
278 | ast_atomic_fetchadd_int(&usage.decoders, -1); | |
279 | ||
267 | 280 | ast_debug(3, "Destroyed decoder #%d (opus->%d)\n", opvt->id, opvt->sampling_rate); |
281 | } | |
282 | ||
283 | static char *handle_cli_opus_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) | |
284 | { | |
285 | struct codec_usage copy; | |
286 | ||
287 | switch (cmd) { | |
288 | case CLI_INIT: | |
289 | e->command = "opus show"; | |
290 | e->usage = | |
291 | "Usage: opus show\n" | |
292 | " Displays Opus encoder/decoder utilization.\n"; | |
293 | return NULL; | |
294 | case CLI_GENERATE: | |
295 | return NULL; | |
296 | } | |
297 | ||
298 | if (a->argc != 2) { | |
299 | return CLI_SHOWUSAGE; | |
300 | } | |
301 | ||
302 | copy = usage; | |
303 | ||
304 | ast_cli(a->fd, "%d/%d encoders/decoders are in use.\n", copy.encoders, copy.decoders); | |
305 | ||
306 | return CLI_SUCCESS; | |
268 | 307 | } |
269 | 308 | |
270 | 309 | /* Translators */ |
488 | 527 | .desc_size = sizeof(struct opus_coder_pvt), |
489 | 528 | .buffer_samples = BUFFER_SAMPLES, |
490 | 529 | .buf_size = BUFFER_SAMPLES * 2, |
530 | }; | |
531 | ||
532 | static struct ast_cli_entry cli[] = { | |
533 | AST_CLI_DEFINE(handle_cli_opus_show, "Display Opus codec utilization.") | |
491 | 534 | }; |
492 | 535 | |
493 | 536 | static int reload(void) |
511 | 554 | res |= ast_unregister_translator(&opustolin48); |
512 | 555 | res |= ast_unregister_translator(&lin48toopus); |
513 | 556 | |
557 | ast_cli_unregister_multiple(cli, ARRAY_LEN(cli)); | |
558 | ||
514 | 559 | return res; |
515 | 560 | } |
516 | 561 | |
529 | 574 | res |= ast_register_translator(&opustolin48); |
530 | 575 | res |= ast_register_translator(&lin48toopus); |
531 | 576 | |
577 | ast_cli_register_multiple(cli, ARRAY_LEN(cli)); | |
578 | ||
532 | 579 | return res; |
533 | 580 | } |
534 | 581 |