| 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 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#include "asterisk.h" |
| 66 |
|
| 67 |
#include "asterisk/module.h" |
| 68 |
#include "asterisk/channel.h" |
| 69 |
#include "asterisk/pbx.h" |
| 70 |
#include "asterisk/utils.h" |
| 71 |
#include "asterisk/audiohook.h" |
| 72 |
#include "asterisk/app.h" |
| 73 |
|
| 74 |
#include <stdio.h> |
| 75 |
#include <string.h> |
| 76 |
|
| 77 |
struct scramble_information { |
| 78 |
struct ast_audiohook audiohook; |
| 79 |
unsigned short int tx; |
| 80 |
unsigned short int rx; |
| 81 |
unsigned short int state; |
| 82 |
}; |
| 83 |
|
| 84 |
static void destroy_callback(void *data) |
| 85 |
{ |
| 86 |
struct scramble_information *ni = data; |
| 87 |
|
| 88 |
|
| 89 |
ast_audiohook_lock(&ni->audiohook); |
| 90 |
ast_audiohook_detach(&ni->audiohook); |
| 91 |
ast_audiohook_unlock(&ni->audiohook); |
| 92 |
ast_audiohook_destroy(&ni->audiohook); |
| 93 |
ast_free(ni); |
| 94 |
|
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
static const struct ast_datastore_info scramble_datastore = { |
| 100 |
.type = "scramble", |
| 101 |
.destroy = destroy_callback |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
static inline void freq_invert(short *amp, int samples) |
| 106 |
{ |
| 107 |
int i; |
| 108 |
|
| 109 |
for (i = 0; i < samples; i += 2) { |
| 110 |
amp[i] = -amp[i]; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
static int scramble_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction) |
| 115 |
{ |
| 116 |
struct ast_datastore *datastore = NULL; |
| 117 |
struct scramble_information *ni = NULL; |
| 118 |
|
| 119 |
|
| 120 |
if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) { |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
if (!(datastore = ast_channel_datastore_find(chan, &scramble_datastore, NULL))) { |
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
if (frame->frametype == AST_FRAME_VOICE) { |
| 130 |
ni = datastore->data; |
| 131 |
|
| 132 |
if (!(direction == AST_AUDIOHOOK_DIRECTION_READ ? ni->rx : ni->tx)) { |
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
freq_invert(frame->data.ptr, frame->samples); |
| 137 |
} |
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
static int remove_scrambler(struct ast_channel *chan) |
| 143 |
{ |
| 144 |
struct ast_datastore *datastore = NULL; |
| 145 |
struct scramble_information *data; |
| 146 |
SCOPED_CHANNELLOCK(chan_lock, chan); |
| 147 |
|
| 148 |
datastore = ast_channel_datastore_find(chan, &scramble_datastore, NULL); |
| 149 |
if (!datastore) { |
| 150 |
ast_log(AST_LOG_WARNING, "Cannot remove SCRAMBLE from %s: SCRAMBLE not currently enabled\n", |
| 151 |
ast_channel_name(chan)); |
| 152 |
return -1; |
| 153 |
} |
| 154 |
data = datastore->data; |
| 155 |
|
| 156 |
if (ast_audiohook_remove(chan, &data->audiohook)) { |
| 157 |
ast_log(AST_LOG_WARNING, "Failed to remove SCRAMBLE audiohook from channel %s\n", ast_channel_name(chan)); |
| 158 |
return -1; |
| 159 |
} |
| 160 |
|
| 161 |
if (ast_channel_datastore_remove(chan, datastore)) { |
| 162 |
ast_log(AST_LOG_WARNING, "Failed to remove SCRAMBLE datastore from channel %s\n", |
| 163 |
ast_channel_name(chan)); |
| 164 |
return -1; |
| 165 |
} |
| 166 |
ast_datastore_free(datastore); |
| 167 |
|
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
static int scramble_write(struct ast_channel *chan, const char *cmd, char *data, const char *value) |
| 172 |
{ |
| 173 |
char *parse; |
| 174 |
struct ast_datastore *datastore = NULL; |
| 175 |
struct scramble_information *ni = NULL; |
| 176 |
int tx = 1, rx = 1; |
| 177 |
|
| 178 |
AST_DECLARE_APP_ARGS(args, |
| 179 |
AST_APP_ARG(direction); |
| 180 |
); |
| 181 |
|
| 182 |
if (!chan) { |
| 183 |
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd); |
| 184 |
return -1; |
| 185 |
} |
| 186 |
|
| 187 |
parse = ast_strdupa(value); |
| 188 |
AST_STANDARD_APP_ARGS(args, parse); |
| 189 |
|
| 190 |
if (!strcasecmp(args.direction, "remove")) { |
| 191 |
return remove_scrambler(chan); |
| 192 |
} |
| 193 |
if (!strcasecmp(args.direction, "tx")) { |
| 194 |
tx = 1; |
| 195 |
rx = 0; |
| 196 |
} else if (!strcasecmp(args.direction, "rx")) { |
| 197 |
rx = 0; |
| 198 |
tx = 1; |
| 199 |
} else if (strcasecmp(args.direction, "both")) { |
| 200 |
ast_log(LOG_ERROR, "Direction must be either RX, TX, both, or remove\n"); |
| 201 |
return -1; |
| 202 |
} |
| 203 |
ast_channel_lock(chan); |
| 204 |
if (!(datastore = ast_channel_datastore_find(chan, &scramble_datastore, NULL))) { |
| 205 |
|
| 206 |
if (!(datastore = ast_datastore_alloc(&scramble_datastore, NULL))) { |
| 207 |
return 0; |
| 208 |
} |
| 209 |
if (!(ni = ast_calloc(1, sizeof(*ni)))) { |
| 210 |
ast_datastore_free(datastore); |
| 211 |
return 0; |
| 212 |
} |
| 213 |
ast_audiohook_init(&ni->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Voice scrambler", AST_AUDIOHOOK_MANIPULATE_ALL_RATES); |
| 214 |
ni->audiohook.manipulate_callback = scramble_callback; |
| 215 |
datastore->data = ni; |
| 216 |
ast_channel_datastore_add(chan, datastore); |
| 217 |
ast_audiohook_attach(chan, &ni->audiohook); |
| 218 |
} else { |
| 219 |
ni = datastore->data; |
| 220 |
} |
| 221 |
ni->tx = tx; |
| 222 |
ni->rx = rx; |
| 223 |
ast_channel_unlock(chan); |
| 224 |
|
| 225 |
return 0; |
| 226 |
} |
| 227 |
|
| 228 |
static struct ast_custom_function scramble_function = { |
| 229 |
.name = "SCRAMBLE", |
| 230 |
.write = scramble_write, |
| 231 |
}; |
| 232 |
|
| 233 |
static int unload_module(void) |
| 234 |
{ |
| 235 |
return ast_custom_function_unregister(&scramble_function); |
| 236 |
} |
| 237 |
|
| 238 |
static int load_module(void) |
| 239 |
{ |
| 240 |
return ast_custom_function_register(&scramble_function); |
| 241 |
} |
| 242 |
|
| 243 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Frequency inverting voice scrambler"); |