| 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 |
#include "asterisk.h" |
| 34 |
#include "errno.h" |
| 35 |
#include <uuid/uuid.h> |
| 36 |
|
| 37 |
#include "asterisk/file.h" |
| 38 |
#include "asterisk/module.h" |
| 39 |
#include "asterisk/channel.h" |
| 40 |
#include "asterisk/app.h" |
| 41 |
#include "asterisk/pbx.h" |
| 42 |
#include "asterisk/res_audiosocket.h" |
| 43 |
#include "asterisk/utils.h" |
| 44 |
#include "asterisk/format_cache.h" |
| 45 |
|
| 46 |
#define AST_MODULE "app_audiosocket" |
| 47 |
#define AUDIOSOCKET_CONFIG "audiosocket.conf" |
| 48 |
#define MAX_WAIT_TIMEOUT_MSEC 2000 |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
static const char app[] = "AudioSocket"; |
| 75 |
|
| 76 |
static int audiosocket_run(struct ast_channel *chan, const char *id, const int svc, const char *server); |
| 77 |
|
| 78 |
static int audiosocket_exec(struct ast_channel *chan, const char *data) |
| 79 |
{ |
| 80 |
char *parse; |
| 81 |
struct ast_format *readFormat, *writeFormat; |
| 82 |
const char *chanName; |
| 83 |
int res; |
| 84 |
|
| 85 |
AST_DECLARE_APP_ARGS(args, |
| 86 |
AST_APP_ARG(idStr); |
| 87 |
AST_APP_ARG(server); |
| 88 |
); |
| 89 |
|
| 90 |
int s = 0; |
| 91 |
uuid_t uu; |
| 92 |
|
| 93 |
|
| 94 |
chanName = ast_channel_name(chan); |
| 95 |
|
| 96 |
|
| 97 |
parse = ast_strdupa(data); |
| 98 |
AST_STANDARD_APP_ARGS(args, parse); |
| 99 |
if (ast_strlen_zero(args.idStr)) { |
| 100 |
ast_log(LOG_ERROR, "UUID is required\n"); |
| 101 |
return -1; |
| 102 |
} |
| 103 |
if (uuid_parse(args.idStr, uu)) { |
| 104 |
ast_log(LOG_ERROR, "Failed to parse UUID '%s'\n", args.idStr); |
| 105 |
return -1; |
| 106 |
} |
| 107 |
if ((s = ast_audiosocket_connect(args.server, chan)) < 0) { |
| 108 |
|
| 109 |
return -1; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
writeFormat = ao2_bump(ast_channel_writeformat(chan)); |
| 115 |
readFormat = ao2_bump(ast_channel_readformat(chan)); |
| 116 |
|
| 117 |
if (ast_set_write_format(chan, ast_format_slin)) { |
| 118 |
ast_log(LOG_ERROR, "Failed to set write format to SLINEAR for channel '%s'\n", chanName); |
| 119 |
res = -1; |
| 120 |
goto end; |
| 121 |
} |
| 122 |
|
| 123 |
if (ast_set_read_format(chan, ast_format_slin)) { |
| 124 |
ast_log(LOG_ERROR, "Failed to set read format to SLINEAR for channel '%s'\n", chanName); |
| 125 |
res = -1; |
| 126 |
goto end; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
res = audiosocket_run(chan, args.idStr, s, args.server); |
| 134 |
|
| 135 |
end: |
| 136 |
|
| 137 |
|
| 138 |
if (ast_set_write_format(chan, writeFormat)) { |
| 139 |
ast_log(LOG_ERROR, "Failed to restore write format for channel '%s'\n", chanName); |
| 140 |
} |
| 141 |
if (ast_set_read_format(chan, readFormat)) { |
| 142 |
ast_log(LOG_ERROR, "Failed to restore read format for channel '%s'\n", chanName); |
| 143 |
} |
| 144 |
ao2_ref(writeFormat, -1); |
| 145 |
ao2_ref(readFormat, -1); |
| 146 |
close(s); |
| 147 |
|
| 148 |
return res; |
| 149 |
} |
| 150 |
|
| 151 |
static int audiosocket_run(struct ast_channel *chan, const char *id, int svc, const char *server) |
| 152 |
{ |
| 153 |
const char *chanName; |
| 154 |
struct ast_channel *targetChan; |
| 155 |
int ms = MAX_WAIT_TIMEOUT_MSEC; |
| 156 |
int outfd = -1; |
| 157 |
struct ast_frame *f; |
| 158 |
int hangup; |
| 159 |
|
| 160 |
if (!chan || ast_channel_state(chan) != AST_STATE_UP) { |
| 161 |
ast_log(LOG_ERROR, "Channel is %s\n", chan ? "not answered" : "missing"); |
| 162 |
return -1; |
| 163 |
} |
| 164 |
|
| 165 |
if (ast_audiosocket_init(svc, id)) { |
| 166 |
ast_log(LOG_ERROR, "Failed to initialize AudioSocket\n"); |
| 167 |
return -1; |
| 168 |
} |
| 169 |
|
| 170 |
chanName = ast_channel_name(chan); |
| 171 |
|
| 172 |
while (1) { |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
targetChan = ast_waitfor_nandfds(&chan, 1, &svc, 1, NULL, &outfd, &ms); |
| 177 |
ms = MAX_WAIT_TIMEOUT_MSEC; |
| 178 |
|
| 179 |
if (targetChan) { |
| 180 |
|
| 181 |
f = ast_read(chan); |
| 182 |
if (!f) { |
| 183 |
ast_log(LOG_WARNING, "Failed to receive frame from channel '%s' connected to AudioSocket server '%s'", chanName, server); |
| 184 |
return -1; |
| 185 |
} |
| 186 |
|
| 187 |
if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_DTMF) { |
| 188 |
|
| 189 |
if (ast_audiosocket_send_frame(svc, f)) { |
| 190 |
ast_log(LOG_WARNING, "Failed to forward frame from channel '%s' to AudioSocket server '%s'\n", |
| 191 |
chanName, server); |
| 192 |
ast_frfree(f); |
| 193 |
return -1; |
| 194 |
} |
| 195 |
} |
| 196 |
ast_frfree(f); |
| 197 |
|
| 198 |
} else if (outfd >= 0) { |
| 199 |
|
| 200 |
f = ast_audiosocket_receive_frame_with_hangup(svc, &hangup); |
| 201 |
if (hangup) { |
| 202 |
|
| 203 |
return 0; |
| 204 |
} |
| 205 |
if (!f) { |
| 206 |
ast_log(LOG_WARNING, "Failed to receive frame from AudioSocket server '%s'" |
| 207 |
" connected to channel '%s'\n", server, chanName); |
| 208 |
return -1; |
| 209 |
} |
| 210 |
|
| 211 |
if (ast_write(chan, f)) { |
| 212 |
ast_log(LOG_WARNING, "Failed to forward frame from AudioSocket server '%s' to channel '%s'\n", server, chanName); |
| 213 |
ast_frfree(f); |
| 214 |
return -1; |
| 215 |
} |
| 216 |
ast_frfree(f); |
| 217 |
|
| 218 |
} else { |
| 219 |
|
| 220 |
|
| 221 |
ast_log(LOG_ERROR, "Reached timeout after %d ms of no activity on AudioSocket connection between '%s' and '%s'\n", MAX_WAIT_TIMEOUT_MSEC, chanName, server); |
| 222 |
return -1; |
| 223 |
} |
| 224 |
} |
| 225 |
return 0; |
| 226 |
} |
| 227 |
|
| 228 |
static int unload_module(void) |
| 229 |
{ |
| 230 |
return ast_unregister_application(app); |
| 231 |
} |
| 232 |
|
| 233 |
static int load_module(void) |
| 234 |
{ |
| 235 |
return ast_register_application_xml(app, audiosocket_exec); |
| 236 |
} |
| 237 |
|
| 238 |
AST_MODULE_INFO( |
| 239 |
ASTERISK_GPL_KEY, |
| 240 |
AST_MODFLAG_LOAD_ORDER, |
| 241 |
"AudioSocket Application", |
| 242 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 243 |
.load = load_module, |
| 244 |
.unload = unload_module, |
| 245 |
.load_pri = AST_MODPRI_CHANNEL_DRIVER, |
| 246 |
.requires = "res_audiosocket", |
| 247 |
); |