Skip to content

Instantly share code, notes, and snippets.

@7marcus9
Created August 3, 2022 15:24
Show Gist options
  • Save 7marcus9/2702b4fd299983b61c1d516aecd7800f to your computer and use it in GitHub Desktop.
Save 7marcus9/2702b4fd299983b61c1d516aecd7800f to your computer and use it in GitHub Desktop.
/*
gcc -pipe -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -g3 -Iinclude -I../include -D_REENTRANT -D_GNU_SOURCE -O6 -fomit-frame-pointer -Wno-missing-prototypes -Wno-missing-declarations -DCRYPTO -fPIC -c -o codec_clearmode.o codec_clearmode.c
gcc -shared -Xlinker -x -o codec_clearmode.so codec_clearmode.o
*/
/*** MODULEINFO
<depend>codec2</depend>
<support_level>core</support_level>
***/
#define AST_MODULE "codec-clearmode"
#define AST_MODULE_SELF_SYM __internal_my_module_self
#include "asterisk.h"
#include "asterisk/codec.h" /* for AST_MEDIA_TYPE_AUDIO */
#include "asterisk/frame.h" /* for ast_frame */
//#include "asterisk/linkedlists.h" /* for AST_LIST_NEXT, etc */
#include "asterisk/logger.h" /* for ast_log, etc */
#include "asterisk/module.h"
#include "asterisk/rtp_engine.h" /* ast_rtp_engine_(un)load_format */
//#include "asterisk/translate.h" /* for ast_trans_pvt, etc */
#include "asterisk/format.h"
#define BUFFER_SAMPLES 8000
struct ast_format {
/*! Name of the format */
const char *name;
/*! \brief Pointer to the codec in use for this format */
struct ast_codec *codec;
/*! \brief Attribute specific data, implementation specific */
void *attribute_data;
/*! \brief Pointer to the optional format interface */
const struct ast_format_interface *interface;
/*! \brief The number if audio channels used, if more than one an interleaved format is required */
unsigned int channel_count;
};
static int clearmode_samples(struct ast_frame *frame)
{
return frame->datalen;
}
static int clearmode_length(unsigned int samples)
{
return samples;
}
static struct ast_codec cclear = {
.name = "CLEARMODE",
.description = "clearmode Pseudocodec",
.type = AST_MEDIA_TYPE_AUDIO,
.sample_rate = 8000,
.minimum_ms = 10,
.maximum_ms = 150,
.default_ms = 20,
.minimum_bytes = 80,
.samples_count = clearmode_samples,
.get_length = clearmode_length,
.smooth = 1,
};
const static char fname[] = "clearmode format";
static struct ast_format ast_format_clear = {&fname[0], &cclear, NULL, NULL, 1};
static int unload_module(void)
{
int res = 0;
//res |= ast_rtp_engine_unload_format(&ast_format_clear);
return res;
}
static int load_module(void)
{
int res = 0;
//res |= ast_rtp_engine_load_format(&ast_format_clear);
//res |= ast_codec_register(&cclear);
res |= __ast_codec_register_with_format(&cclear, "cmode", AST_MODULE_SELF);
if (res) {
ast_log(LOG_ERROR, "Cannot register Codec\n");
unload_module();
return AST_MODULE_LOAD_DECLINE;
}
struct ast_codec *codec = ast_codec_get("CLEARMODE", AST_MEDIA_TYPE_AUDIO, 8000);
if (!codec) {
ast_log(LOG_ERROR, "Cannot get Codec\n");
return AST_MODULE_LOAD_DECLINE;
}
struct ast_format *format = ast_format_create_named("cmode", codec);
if (!format) {
ast_log(LOG_ERROR, "Cannot create format\n");
return AST_MODULE_LOAD_DECLINE;
}
if (ast_format_cache_set(format)) {
ast_log(LOG_ERROR, "Cannot add format to cache\n");
return AST_MODULE_LOAD_DECLINE;
}
res |= ast_rtp_engine_load_format(format);
if (res) {
ast_log(LOG_ERROR, "Cannot load format\n");
return AST_MODULE_LOAD_DECLINE;
}
return AST_MODULE_LOAD_SUCCESS;
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "CLEARMODE Pseudocodec");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment