| 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 |
#include "asterisk.h" |
| 44 |
|
| 45 |
#include <limits.h> |
| 46 |
|
| 47 |
#include <jack/jack.h> |
| 48 |
#include <jack/ringbuffer.h> |
| 49 |
|
| 50 |
#include <libresample.h> |
| 51 |
|
| 52 |
#include "asterisk/module.h" |
| 53 |
#include "asterisk/channel.h" |
| 54 |
#include "asterisk/strings.h" |
| 55 |
#include "asterisk/lock.h" |
| 56 |
#include "asterisk/app.h" |
| 57 |
#include "asterisk/pbx.h" |
| 58 |
#include "asterisk/audiohook.h" |
| 59 |
#include "asterisk/format_cache.h" |
| 60 |
|
| 61 |
#define RESAMPLE_QUALITY 1 |
| 62 |
|
| 63 |
|
| 64 |
#define RINGBUFFER_FRAME_CAPACITY 100 |
| 65 |
|
| 66 |
|
| 67 |
#define COMMON_OPTIONS \ |
| 68 |
" s(<name>) - Connect to the specified jack server name.\n" \ |
| 69 |
" i(<name>) - Connect the output port that gets created to the specified\n" \ |
| 70 |
" jack input port.\n" \ |
| 71 |
" o(<name>) - Connect the input port that gets created to the specified\n" \ |
| 72 |
" jack output port.\n" \ |
| 73 |
" n - Do not automatically start the JACK server if it is not already\n" \ |
| 74 |
" running.\n" \ |
| 75 |
" c(<name>) - By default, Asterisk will use the channel name for the jack client\n" \ |
| 76 |
" name. Use this option to specify a custom client name.\n" |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
static const char jack_app[] = "JACK"; |
| 121 |
|
| 122 |
struct jack_data { |
| 123 |
AST_DECLARE_STRING_FIELDS( |
| 124 |
AST_STRING_FIELD(server_name); |
| 125 |
AST_STRING_FIELD(client_name); |
| 126 |
AST_STRING_FIELD(connect_input_port); |
| 127 |
AST_STRING_FIELD(connect_output_port); |
| 128 |
); |
| 129 |
jack_client_t *client; |
| 130 |
jack_port_t *input_port; |
| 131 |
jack_port_t *output_port; |
| 132 |
jack_ringbuffer_t *input_rb; |
| 133 |
jack_ringbuffer_t *output_rb; |
| 134 |
struct ast_format *audiohook_format; |
| 135 |
unsigned int audiohook_rate; |
| 136 |
unsigned int frame_datalen; |
| 137 |
void *output_resampler; |
| 138 |
double output_resample_factor; |
| 139 |
void *input_resampler; |
| 140 |
double input_resample_factor; |
| 141 |
unsigned int stop:1; |
| 142 |
unsigned int has_audiohook:1; |
| 143 |
unsigned int no_start_server:1; |
| 144 |
|
| 145 |
struct ast_audiohook audiohook; |
| 146 |
}; |
| 147 |
|
| 148 |
static const struct { |
| 149 |
jack_status_t status; |
| 150 |
const char *str; |
| 151 |
} jack_status_table[] = { |
| 152 |
{ JackFailure, "Failure" }, |
| 153 |
{ JackInvalidOption, "Invalid Option" }, |
| 154 |
{ JackNameNotUnique, "Name Not Unique" }, |
| 155 |
{ JackServerStarted, "Server Started" }, |
| 156 |
{ JackServerFailed, "Server Failed" }, |
| 157 |
{ JackServerError, "Server Error" }, |
| 158 |
{ JackNoSuchClient, "No Such Client" }, |
| 159 |
{ JackLoadFailure, "Load Failure" }, |
| 160 |
{ JackInitFailure, "Init Failure" }, |
| 161 |
{ JackShmFailure, "Shared Memory Access Failure" }, |
| 162 |
{ JackVersionError, "Version Mismatch" }, |
| 163 |
}; |
| 164 |
|
| 165 |
static const char *jack_status_to_str(jack_status_t status) |
| 166 |
{ |
| 167 |
int i; |
| 168 |
|
| 169 |
for (i = 0; i < ARRAY_LEN(jack_status_table); i++) { |
| 170 |
if (jack_status_table[i].status == status) |
| 171 |
return jack_status_table[i].str; |
| 172 |
} |
| 173 |
|
| 174 |
return "Unknown Error"; |
| 175 |
} |
| 176 |
|
| 177 |
static void log_jack_status(const char *prefix, jack_status_t status) |
| 178 |
{ |
| 179 |
struct ast_str *str = ast_str_alloca(512); |
| 180 |
int i, first = 0; |
| 181 |
|
| 182 |
for (i = 0; i < (sizeof(status) * 8); i++) { |
| 183 |
if (!(status & (1 << i))) |
| 184 |
continue; |
| 185 |
|
| 186 |
if (!first) { |
| 187 |
ast_str_set(&str, 0, "%s", jack_status_to_str((1 << i))); |
| 188 |
first = 1; |
| 189 |
} else |
| 190 |
ast_str_append(&str, 0, ", %s", jack_status_to_str((1 << i))); |
| 191 |
} |
| 192 |
|
| 193 |
ast_log(LOG_NOTICE, "%s: %s\n", prefix, ast_str_buffer(str)); |
| 194 |
} |
| 195 |
|
| 196 |
static int alloc_resampler(struct jack_data *jack_data, int input) |
| 197 |
{ |
| 198 |
double from_srate, to_srate, jack_srate; |
| 199 |
void **resampler; |
| 200 |
double *resample_factor; |
| 201 |
|
| 202 |
if (input && jack_data->input_resampler) |
| 203 |
return 0; |
| 204 |
|
| 205 |
if (!input && jack_data->output_resampler) |
| 206 |
return 0; |
| 207 |
|
| 208 |
jack_srate = jack_get_sample_rate(jack_data->client); |
| 209 |
|
| 210 |
to_srate = input ? jack_data->audiohook_rate : jack_srate; |
| 211 |
from_srate = input ? jack_srate : jack_data->audiohook_rate; |
| 212 |
|
| 213 |
resample_factor = input ? &jack_data->input_resample_factor : |
| 214 |
&jack_data->output_resample_factor; |
| 215 |
|
| 216 |
if (from_srate == to_srate) { |
| 217 |
|
| 218 |
|
| 219 |
*resample_factor = 1.0; |
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
*resample_factor = to_srate / from_srate; |
| 224 |
|
| 225 |
resampler = input ? &jack_data->input_resampler : |
| 226 |
&jack_data->output_resampler; |
| 227 |
|
| 228 |
if (!(*resampler = resample_open(RESAMPLE_QUALITY, |
| 229 |
*resample_factor, *resample_factor))) { |
| 230 |
ast_log(LOG_ERROR, "Failed to open %s resampler\n", |
| 231 |
input ? "input" : "output"); |
| 232 |
return -1; |
| 233 |
} |
| 234 |
|
| 235 |
return 0; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
static void handle_input(void *buf, jack_nframes_t nframes, |
| 245 |
struct jack_data *jack_data) |
| 246 |
{ |
| 247 |
short s_buf[nframes]; |
| 248 |
float *in_buf = buf; |
| 249 |
size_t res; |
| 250 |
int i; |
| 251 |
size_t write_len = sizeof(s_buf); |
| 252 |
|
| 253 |
if (jack_data->input_resampler) { |
| 254 |
int total_in_buf_used = 0; |
| 255 |
int total_out_buf_used = 0; |
| 256 |
float f_buf[nframes + 1]; |
| 257 |
|
| 258 |
memset(f_buf, 0, sizeof(f_buf)); |
| 259 |
|
| 260 |
while (total_in_buf_used < nframes) { |
| 261 |
int in_buf_used; |
| 262 |
int out_buf_used; |
| 263 |
|
| 264 |
out_buf_used = resample_process(jack_data->input_resampler, |
| 265 |
jack_data->input_resample_factor, |
| 266 |
&in_buf[total_in_buf_used], nframes - total_in_buf_used, |
| 267 |
0, &in_buf_used, |
| 268 |
&f_buf[total_out_buf_used], ARRAY_LEN(f_buf) - total_out_buf_used); |
| 269 |
|
| 270 |
if (out_buf_used < 0) |
| 271 |
break; |
| 272 |
|
| 273 |
total_out_buf_used += out_buf_used; |
| 274 |
total_in_buf_used += in_buf_used; |
| 275 |
|
| 276 |
if (total_out_buf_used == ARRAY_LEN(f_buf)) { |
| 277 |
ast_log(LOG_ERROR, "Output buffer filled ... need to increase its size, " |
| 278 |
"nframes '%d', total_out_buf_used '%d'\n", nframes, total_out_buf_used); |
| 279 |
break; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
for (i = 0; i < total_out_buf_used; i++) |
| 284 |
s_buf[i] = f_buf[i] * (SHRT_MAX / 1.0); |
| 285 |
|
| 286 |
write_len = total_out_buf_used * sizeof(int16_t); |
| 287 |
} else { |
| 288 |
|
| 289 |
|
| 290 |
for (i = 0; i < nframes; i++) |
| 291 |
s_buf[i] = in_buf[i] * (SHRT_MAX / 1.0); |
| 292 |
} |
| 293 |
|
| 294 |
res = jack_ringbuffer_write(jack_data->input_rb, (const char *) s_buf, write_len); |
| 295 |
if (res != write_len) { |
| 296 |
ast_log(LOG_WARNING, "Tried to write %d bytes to the ringbuffer, but only wrote %d\n", |
| 297 |
(int) sizeof(s_buf), (int) res); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
static void handle_output(void *buf, jack_nframes_t nframes, |
| 308 |
struct jack_data *jack_data) |
| 309 |
{ |
| 310 |
size_t res, len; |
| 311 |
|
| 312 |
len = nframes * sizeof(float); |
| 313 |
|
| 314 |
res = jack_ringbuffer_read(jack_data->output_rb, buf, len); |
| 315 |
|
| 316 |
if (len != res) { |
| 317 |
ast_debug(2, "Wanted %d bytes to send to the output port, " |
| 318 |
"but only got %d\n", (int) len, (int) res); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
static int jack_process(jack_nframes_t nframes, void *arg) |
| 323 |
{ |
| 324 |
struct jack_data *jack_data = arg; |
| 325 |
void *input_port_buf, *output_port_buf; |
| 326 |
|
| 327 |
if (!jack_data->input_resample_factor) |
| 328 |
alloc_resampler(jack_data, 1); |
| 329 |
|
| 330 |
input_port_buf = jack_port_get_buffer(jack_data->input_port, nframes); |
| 331 |
handle_input(input_port_buf, nframes, jack_data); |
| 332 |
|
| 333 |
output_port_buf = jack_port_get_buffer(jack_data->output_port, nframes); |
| 334 |
handle_output(output_port_buf, nframes, jack_data); |
| 335 |
|
| 336 |
return 0; |
| 337 |
} |
| 338 |
|
| 339 |
static void jack_shutdown(void *arg) |
| 340 |
{ |
| 341 |
struct jack_data *jack_data = arg; |
| 342 |
|
| 343 |
jack_data->stop = 1; |
| 344 |
} |
| 345 |
|
| 346 |
static struct jack_data *destroy_jack_data(struct jack_data *jack_data) |
| 347 |
{ |
| 348 |
if (jack_data->input_port) { |
| 349 |
jack_port_unregister(jack_data->client, jack_data->input_port); |
| 350 |
jack_data->input_port = NULL; |
| 351 |
} |
| 352 |
|
| 353 |
if (jack_data->output_port) { |
| 354 |
jack_port_unregister(jack_data->client, jack_data->output_port); |
| 355 |
jack_data->output_port = NULL; |
| 356 |
} |
| 357 |
|
| 358 |
if (jack_data->client) { |
| 359 |
jack_client_close(jack_data->client); |
| 360 |
jack_data->client = NULL; |
| 361 |
} |
| 362 |
|
| 363 |
if (jack_data->input_rb) { |
| 364 |
jack_ringbuffer_free(jack_data->input_rb); |
| 365 |
jack_data->input_rb = NULL; |
| 366 |
} |
| 367 |
|
| 368 |
if (jack_data->output_rb) { |
| 369 |
jack_ringbuffer_free(jack_data->output_rb); |
| 370 |
jack_data->output_rb = NULL; |
| 371 |
} |
| 372 |
|
| 373 |
if (jack_data->output_resampler) { |
| 374 |
resample_close(jack_data->output_resampler); |
| 375 |
jack_data->output_resampler = NULL; |
| 376 |
} |
| 377 |
|
| 378 |
if (jack_data->input_resampler) { |
| 379 |
resample_close(jack_data->input_resampler); |
| 380 |
jack_data->input_resampler = NULL; |
| 381 |
} |
| 382 |
|
| 383 |
if (jack_data->has_audiohook) |
| 384 |
ast_audiohook_destroy(&jack_data->audiohook); |
| 385 |
|
| 386 |
ast_string_field_free_memory(jack_data); |
| 387 |
|
| 388 |
ast_free(jack_data); |
| 389 |
|
| 390 |
return NULL; |
| 391 |
} |
| 392 |
|
| 393 |
static int init_jack_data(struct ast_channel *chan, struct jack_data *jack_data) |
| 394 |
{ |
| 395 |
const char *client_name; |
| 396 |
jack_status_t status = 0; |
| 397 |
jack_options_t jack_options = JackNullOption; |
| 398 |
|
| 399 |
unsigned int channel_rate; |
| 400 |
|
| 401 |
unsigned int ringbuffer_size; |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
channel_rate = ast_format_get_sample_rate(ast_channel_readformat(chan)); |
| 407 |
jack_data->audiohook_format = ast_format_cache_get_slin_by_rate(channel_rate); |
| 408 |
jack_data->audiohook_rate = ast_format_get_sample_rate(jack_data->audiohook_format); |
| 409 |
|
| 410 |
|
| 411 |
jack_data->frame_datalen = jack_data->audiohook_rate / 50; |
| 412 |
|
| 413 |
ringbuffer_size = jack_data->frame_datalen * RINGBUFFER_FRAME_CAPACITY; |
| 414 |
|
| 415 |
ast_debug(1, "Audiohook parameters: slin-format:%s, rate:%d, frame-len:%d, ringbuffer_size: %d\n", |
| 416 |
ast_format_get_name(jack_data->audiohook_format), jack_data->audiohook_rate, jack_data->frame_datalen, ringbuffer_size); |
| 417 |
|
| 418 |
if (!ast_strlen_zero(jack_data->client_name)) { |
| 419 |
client_name = jack_data->client_name; |
| 420 |
} else { |
| 421 |
ast_channel_lock(chan); |
| 422 |
client_name = ast_strdupa(ast_channel_name(chan)); |
| 423 |
ast_channel_unlock(chan); |
| 424 |
} |
| 425 |
|
| 426 |
if (!(jack_data->output_rb = jack_ringbuffer_create(ringbuffer_size))) |
| 427 |
return -1; |
| 428 |
|
| 429 |
if (!(jack_data->input_rb = jack_ringbuffer_create(ringbuffer_size))) |
| 430 |
return -1; |
| 431 |
|
| 432 |
if (jack_data->no_start_server) |
| 433 |
jack_options |= JackNoStartServer; |
| 434 |
|
| 435 |
if (!ast_strlen_zero(jack_data->server_name)) { |
| 436 |
jack_options |= JackServerName; |
| 437 |
jack_data->client = jack_client_open(client_name, jack_options, &status, |
| 438 |
jack_data->server_name); |
| 439 |
} else { |
| 440 |
jack_data->client = jack_client_open(client_name, jack_options, &status); |
| 441 |
} |
| 442 |
|
| 443 |
if (status) |
| 444 |
log_jack_status("Client Open Status", status); |
| 445 |
|
| 446 |
if (!jack_data->client) |
| 447 |
return -1; |
| 448 |
|
| 449 |
jack_data->input_port = jack_port_register(jack_data->client, "input", |
| 450 |
JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput | JackPortIsTerminal, 0); |
| 451 |
if (!jack_data->input_port) { |
| 452 |
ast_log(LOG_ERROR, "Failed to create input port for jack port\n"); |
| 453 |
return -1; |
| 454 |
} |
| 455 |
|
| 456 |
jack_data->output_port = jack_port_register(jack_data->client, "output", |
| 457 |
JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput | JackPortIsTerminal, 0); |
| 458 |
if (!jack_data->output_port) { |
| 459 |
ast_log(LOG_ERROR, "Failed to create output port for jack port\n"); |
| 460 |
return -1; |
| 461 |
} |
| 462 |
|
| 463 |
if (jack_set_process_callback(jack_data->client, jack_process, jack_data)) { |
| 464 |
ast_log(LOG_ERROR, "Failed to register process callback with jack client\n"); |
| 465 |
return -1; |
| 466 |
} |
| 467 |
|
| 468 |
jack_on_shutdown(jack_data->client, jack_shutdown, jack_data); |
| 469 |
|
| 470 |
if (jack_activate(jack_data->client)) { |
| 471 |
ast_log(LOG_ERROR, "Unable to activate jack client\n"); |
| 472 |
return -1; |
| 473 |
} |
| 474 |
|
| 475 |
while (!ast_strlen_zero(jack_data->connect_input_port)) { |
| 476 |
const char **ports; |
| 477 |
int i; |
| 478 |
|
| 479 |
ports = jack_get_ports(jack_data->client, jack_data->connect_input_port, |
| 480 |
NULL, JackPortIsInput); |
| 481 |
|
| 482 |
if (!ports) { |
| 483 |
ast_log(LOG_ERROR, "No input port matching '%s' was found\n", |
| 484 |
jack_data->connect_input_port); |
| 485 |
break; |
| 486 |
} |
| 487 |
|
| 488 |
for (i = 0; ports[i]; i++) { |
| 489 |
ast_debug(1, "Found port '%s' that matched specified input port '%s'\n", |
| 490 |
ports[i], jack_data->connect_input_port); |
| 491 |
} |
| 492 |
|
| 493 |
if (jack_connect(jack_data->client, jack_port_name(jack_data->output_port), ports[0])) { |
| 494 |
ast_log(LOG_ERROR, "Failed to connect '%s' to '%s'\n", ports[0], |
| 495 |
jack_port_name(jack_data->output_port)); |
| 496 |
} else { |
| 497 |
ast_debug(1, "Connected '%s' to '%s'\n", ports[0], |
| 498 |
jack_port_name(jack_data->output_port)); |
| 499 |
} |
| 500 |
|
| 501 |
jack_free(ports); |
| 502 |
|
| 503 |
break; |
| 504 |
} |
| 505 |
|
| 506 |
while (!ast_strlen_zero(jack_data->connect_output_port)) { |
| 507 |
const char **ports; |
| 508 |
int i; |
| 509 |
|
| 510 |
ports = jack_get_ports(jack_data->client, jack_data->connect_output_port, |
| 511 |
NULL, JackPortIsOutput); |
| 512 |
|
| 513 |
if (!ports) { |
| 514 |
ast_log(LOG_ERROR, "No output port matching '%s' was found\n", |
| 515 |
jack_data->connect_output_port); |
| 516 |
break; |
| 517 |
} |
| 518 |
|
| 519 |
for (i = 0; ports[i]; i++) { |
| 520 |
ast_debug(1, "Found port '%s' that matched specified output port '%s'\n", |
| 521 |
ports[i], jack_data->connect_output_port); |
| 522 |
} |
| 523 |
|
| 524 |
if (jack_connect(jack_data->client, ports[0], jack_port_name(jack_data->input_port))) { |
| 525 |
ast_log(LOG_ERROR, "Failed to connect '%s' to '%s'\n", ports[0], |
| 526 |
jack_port_name(jack_data->input_port)); |
| 527 |
} else { |
| 528 |
ast_debug(1, "Connected '%s' to '%s'\n", ports[0], |
| 529 |
jack_port_name(jack_data->input_port)); |
| 530 |
} |
| 531 |
|
| 532 |
jack_free(ports); |
| 533 |
|
| 534 |
break; |
| 535 |
} |
| 536 |
|
| 537 |
return 0; |
| 538 |
} |
| 539 |
|
| 540 |
static int queue_voice_frame(struct jack_data *jack_data, struct ast_frame *f) |
| 541 |
{ |
| 542 |
float f_buf[f->samples * 8]; |
| 543 |
size_t f_buf_used = 0; |
| 544 |
int i; |
| 545 |
int16_t *s_buf = f->data.ptr; |
| 546 |
size_t res; |
| 547 |
|
| 548 |
memset(f_buf, 0, sizeof(f_buf)); |
| 549 |
|
| 550 |
if (!jack_data->output_resample_factor) |
| 551 |
alloc_resampler(jack_data, 0); |
| 552 |
|
| 553 |
if (jack_data->output_resampler) { |
| 554 |
float in_buf[f->samples]; |
| 555 |
int total_in_buf_used = 0; |
| 556 |
int total_out_buf_used = 0; |
| 557 |
|
| 558 |
memset(in_buf, 0, sizeof(in_buf)); |
| 559 |
|
| 560 |
for (i = 0; i < f->samples; i++) |
| 561 |
in_buf[i] = s_buf[i] * (1.0 / SHRT_MAX); |
| 562 |
|
| 563 |
while (total_in_buf_used < ARRAY_LEN(in_buf)) { |
| 564 |
int in_buf_used; |
| 565 |
int out_buf_used; |
| 566 |
|
| 567 |
out_buf_used = resample_process(jack_data->output_resampler, |
| 568 |
jack_data->output_resample_factor, |
| 569 |
&in_buf[total_in_buf_used], ARRAY_LEN(in_buf) - total_in_buf_used, |
| 570 |
0, &in_buf_used, |
| 571 |
&f_buf[total_out_buf_used], ARRAY_LEN(f_buf) - total_out_buf_used); |
| 572 |
|
| 573 |
if (out_buf_used < 0) |
| 574 |
break; |
| 575 |
|
| 576 |
total_out_buf_used += out_buf_used; |
| 577 |
total_in_buf_used += in_buf_used; |
| 578 |
|
| 579 |
if (total_out_buf_used == ARRAY_LEN(f_buf)) { |
| 580 |
ast_log(LOG_ERROR, "Output buffer filled ... need to increase its size\n"); |
| 581 |
break; |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
f_buf_used = total_out_buf_used; |
| 586 |
if (f_buf_used > ARRAY_LEN(f_buf)) |
| 587 |
f_buf_used = ARRAY_LEN(f_buf); |
| 588 |
} else { |
| 589 |
|
| 590 |
|
| 591 |
for (i = 0; i < f->samples; i++) |
| 592 |
f_buf[i] = s_buf[i] * (1.0 / SHRT_MAX); |
| 593 |
|
| 594 |
f_buf_used = f->samples; |
| 595 |
} |
| 596 |
|
| 597 |
res = jack_ringbuffer_write(jack_data->output_rb, (const char *) f_buf, f_buf_used * sizeof(float)); |
| 598 |
if (res != (f_buf_used * sizeof(float))) { |
| 599 |
ast_log(LOG_WARNING, "Tried to write %d bytes to the ringbuffer, but only wrote %d\n", |
| 600 |
(int) (f_buf_used * sizeof(float)), (int) res); |
| 601 |
} |
| 602 |
return 0; |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
static void handle_jack_audio(struct ast_channel *chan, struct jack_data *jack_data, |
| 623 |
struct ast_frame *out_frame) |
| 624 |
{ |
| 625 |
short buf[jack_data->frame_datalen]; |
| 626 |
struct ast_frame f = { |
| 627 |
.frametype = AST_FRAME_VOICE, |
| 628 |
.subclass.format = jack_data->audiohook_format, |
| 629 |
.src = "JACK", |
| 630 |
.data.ptr = buf, |
| 631 |
.datalen = sizeof(buf), |
| 632 |
.samples = ARRAY_LEN(buf), |
| 633 |
}; |
| 634 |
|
| 635 |
for (;;) { |
| 636 |
size_t res, read_len; |
| 637 |
char *read_buf; |
| 638 |
|
| 639 |
read_len = out_frame ? out_frame->datalen : sizeof(buf); |
| 640 |
read_buf = out_frame ? out_frame->data.ptr : buf; |
| 641 |
|
| 642 |
res = jack_ringbuffer_read_space(jack_data->input_rb); |
| 643 |
|
| 644 |
if (res < read_len) { |
| 645 |
|
| 646 |
if (out_frame) { |
| 647 |
ast_debug(1, "Sending an empty frame for the JACK_HOOK\n"); |
| 648 |
memset(out_frame->data.ptr, 0, out_frame->datalen); |
| 649 |
} |
| 650 |
break; |
| 651 |
} |
| 652 |
|
| 653 |
res = jack_ringbuffer_read(jack_data->input_rb, (char *) read_buf, read_len); |
| 654 |
|
| 655 |
if (res < read_len) { |
| 656 |
ast_log(LOG_ERROR, "Error reading from ringbuffer, even though it said there was enough data\n"); |
| 657 |
break; |
| 658 |
} |
| 659 |
|
| 660 |
if (out_frame) { |
| 661 |
|
| 662 |
|
| 663 |
break; |
| 664 |
} |
| 665 |
|
| 666 |
ast_write(chan, &f); |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
enum { |
| 671 |
OPT_SERVER_NAME = (1 << 0), |
| 672 |
OPT_INPUT_PORT = (1 << 1), |
| 673 |
OPT_OUTPUT_PORT = (1 << 2), |
| 674 |
OPT_NOSTART_SERVER = (1 << 3), |
| 675 |
OPT_CLIENT_NAME = (1 << 4), |
| 676 |
}; |
| 677 |
|
| 678 |
enum { |
| 679 |
OPT_ARG_SERVER_NAME, |
| 680 |
OPT_ARG_INPUT_PORT, |
| 681 |
OPT_ARG_OUTPUT_PORT, |
| 682 |
OPT_ARG_CLIENT_NAME, |
| 683 |
|
| 684 |
|
| 685 |
OPT_ARG_ARRAY_SIZE, |
| 686 |
}; |
| 687 |
|
| 688 |
AST_APP_OPTIONS(jack_exec_options, BEGIN_OPTIONS |
| 689 |
AST_APP_OPTION_ARG('s', OPT_SERVER_NAME, OPT_ARG_SERVER_NAME), |
| 690 |
AST_APP_OPTION_ARG('i', OPT_INPUT_PORT, OPT_ARG_INPUT_PORT), |
| 691 |
AST_APP_OPTION_ARG('o', OPT_OUTPUT_PORT, OPT_ARG_OUTPUT_PORT), |
| 692 |
AST_APP_OPTION('n', OPT_NOSTART_SERVER), |
| 693 |
AST_APP_OPTION_ARG('c', OPT_CLIENT_NAME, OPT_ARG_CLIENT_NAME), |
| 694 |
END_OPTIONS ); |
| 695 |
|
| 696 |
static struct jack_data *jack_data_alloc(void) |
| 697 |
{ |
| 698 |
struct jack_data *jack_data; |
| 699 |
|
| 700 |
if (!(jack_data = ast_calloc_with_stringfields(1, struct jack_data, 32))) { |
| 701 |
return NULL; |
| 702 |
} |
| 703 |
|
| 704 |
return jack_data; |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
static int handle_options(struct jack_data *jack_data, const char *__options_str) |
| 711 |
{ |
| 712 |
struct ast_flags options = { 0, }; |
| 713 |
char *option_args[OPT_ARG_ARRAY_SIZE]; |
| 714 |
char *options_str; |
| 715 |
|
| 716 |
options_str = ast_strdupa(__options_str); |
| 717 |
|
| 718 |
ast_app_parse_options(jack_exec_options, &options, option_args, options_str); |
| 719 |
|
| 720 |
if (ast_test_flag(&options, OPT_SERVER_NAME)) { |
| 721 |
if (!ast_strlen_zero(option_args[OPT_ARG_SERVER_NAME])) |
| 722 |
ast_string_field_set(jack_data, server_name, option_args[OPT_ARG_SERVER_NAME]); |
| 723 |
else { |
| 724 |
ast_log(LOG_ERROR, "A server name must be provided with the s() option\n"); |
| 725 |
return -1; |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
if (ast_test_flag(&options, OPT_CLIENT_NAME)) { |
| 730 |
if (!ast_strlen_zero(option_args[OPT_ARG_CLIENT_NAME])) |
| 731 |
ast_string_field_set(jack_data, client_name, option_args[OPT_ARG_CLIENT_NAME]); |
| 732 |
else { |
| 733 |
ast_log(LOG_ERROR, "A client name must be provided with the c() option\n"); |
| 734 |
return -1; |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
if (ast_test_flag(&options, OPT_INPUT_PORT)) { |
| 739 |
if (!ast_strlen_zero(option_args[OPT_ARG_INPUT_PORT])) |
| 740 |
ast_string_field_set(jack_data, connect_input_port, option_args[OPT_ARG_INPUT_PORT]); |
| 741 |
else { |
| 742 |
ast_log(LOG_ERROR, "A name must be provided with the i() option\n"); |
| 743 |
return -1; |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
if (ast_test_flag(&options, OPT_OUTPUT_PORT)) { |
| 748 |
if (!ast_strlen_zero(option_args[OPT_ARG_OUTPUT_PORT])) |
| 749 |
ast_string_field_set(jack_data, connect_output_port, option_args[OPT_ARG_OUTPUT_PORT]); |
| 750 |
else { |
| 751 |
ast_log(LOG_ERROR, "A name must be provided with the o() option\n"); |
| 752 |
return -1; |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
jack_data->no_start_server = ast_test_flag(&options, OPT_NOSTART_SERVER) ? 1 : 0; |
| 757 |
|
| 758 |
return 0; |
| 759 |
} |
| 760 |
|
| 761 |
static int jack_exec(struct ast_channel *chan, const char *data) |
| 762 |
{ |
| 763 |
struct jack_data *jack_data; |
| 764 |
|
| 765 |
if (!(jack_data = jack_data_alloc())) |
| 766 |
return -1; |
| 767 |
|
| 768 |
if (!ast_strlen_zero(data) && handle_options(jack_data, data)) { |
| 769 |
destroy_jack_data(jack_data); |
| 770 |
return -1; |
| 771 |
} |
| 772 |
|
| 773 |
if (init_jack_data(chan, jack_data)) { |
| 774 |
destroy_jack_data(jack_data); |
| 775 |
return -1; |
| 776 |
} |
| 777 |
|
| 778 |
if (ast_set_read_format(chan, jack_data->audiohook_format)) { |
| 779 |
destroy_jack_data(jack_data); |
| 780 |
return -1; |
| 781 |
} |
| 782 |
|
| 783 |
if (ast_set_write_format(chan, jack_data->audiohook_format)) { |
| 784 |
destroy_jack_data(jack_data); |
| 785 |
return -1; |
| 786 |
} |
| 787 |
|
| 788 |
while (!jack_data->stop) { |
| 789 |
struct ast_frame *f; |
| 790 |
|
| 791 |
if (ast_waitfor(chan, -1) < 0) { |
| 792 |
break; |
| 793 |
} |
| 794 |
|
| 795 |
f = ast_read(chan); |
| 796 |
if (!f) { |
| 797 |
jack_data->stop = 1; |
| 798 |
continue; |
| 799 |
} |
| 800 |
|
| 801 |
switch (f->frametype) { |
| 802 |
case AST_FRAME_CONTROL: |
| 803 |
if (f->subclass.integer == AST_CONTROL_HANGUP) |
| 804 |
jack_data->stop = 1; |
| 805 |
break; |
| 806 |
case AST_FRAME_VOICE: |
| 807 |
queue_voice_frame(jack_data, f); |
| 808 |
default: |
| 809 |
break; |
| 810 |
} |
| 811 |
|
| 812 |
ast_frfree(f); |
| 813 |
|
| 814 |
handle_jack_audio(chan, jack_data, NULL); |
| 815 |
} |
| 816 |
|
| 817 |
jack_data = destroy_jack_data(jack_data); |
| 818 |
|
| 819 |
return 0; |
| 820 |
} |
| 821 |
|
| 822 |
static void jack_hook_ds_destroy(void *data) |
| 823 |
{ |
| 824 |
struct jack_data *jack_data = data; |
| 825 |
|
| 826 |
destroy_jack_data(jack_data); |
| 827 |
} |
| 828 |
|
| 829 |
static const struct ast_datastore_info jack_hook_ds_info = { |
| 830 |
.type = "JACK_HOOK", |
| 831 |
.destroy = jack_hook_ds_destroy, |
| 832 |
}; |
| 833 |
|
| 834 |
static int jack_hook_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, |
| 835 |
struct ast_frame *frame, enum ast_audiohook_direction direction) |
| 836 |
{ |
| 837 |
struct ast_datastore *datastore; |
| 838 |
struct jack_data *jack_data; |
| 839 |
|
| 840 |
if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) |
| 841 |
return 0; |
| 842 |
|
| 843 |
if (direction != AST_AUDIOHOOK_DIRECTION_READ) |
| 844 |
return 0; |
| 845 |
|
| 846 |
if (frame->frametype != AST_FRAME_VOICE) |
| 847 |
return 0; |
| 848 |
|
| 849 |
ast_channel_lock(chan); |
| 850 |
|
| 851 |
if (!(datastore = ast_channel_datastore_find(chan, &jack_hook_ds_info, NULL))) { |
| 852 |
ast_log(LOG_ERROR, "JACK_HOOK datastore not found for '%s'\n", ast_channel_name(chan)); |
| 853 |
ast_channel_unlock(chan); |
| 854 |
return -1; |
| 855 |
} |
| 856 |
|
| 857 |
jack_data = datastore->data; |
| 858 |
|
| 859 |
if (ast_format_cmp(frame->subclass.format, jack_data->audiohook_format) == AST_FORMAT_CMP_NOT_EQUAL) { |
| 860 |
ast_log(LOG_WARNING, "Expected frame in %s for the audiohook, but got format %s\n", |
| 861 |
ast_format_get_name(jack_data->audiohook_format), |
| 862 |
ast_format_get_name(frame->subclass.format)); |
| 863 |
ast_channel_unlock(chan); |
| 864 |
return 0; |
| 865 |
} |
| 866 |
|
| 867 |
queue_voice_frame(jack_data, frame); |
| 868 |
|
| 869 |
handle_jack_audio(chan, jack_data, frame); |
| 870 |
|
| 871 |
ast_channel_unlock(chan); |
| 872 |
|
| 873 |
return 0; |
| 874 |
} |
| 875 |
|
| 876 |
static int enable_jack_hook(struct ast_channel *chan, char *data) |
| 877 |
{ |
| 878 |
struct ast_datastore *datastore; |
| 879 |
struct jack_data *jack_data = NULL; |
| 880 |
AST_DECLARE_APP_ARGS(args, |
| 881 |
AST_APP_ARG(mode); |
| 882 |
AST_APP_ARG(options); |
| 883 |
); |
| 884 |
|
| 885 |
AST_STANDARD_APP_ARGS(args, data); |
| 886 |
|
| 887 |
ast_channel_lock(chan); |
| 888 |
|
| 889 |
if ((datastore = ast_channel_datastore_find(chan, &jack_hook_ds_info, NULL))) { |
| 890 |
ast_log(LOG_ERROR, "JACK_HOOK already enabled for '%s'\n", ast_channel_name(chan)); |
| 891 |
goto return_error; |
| 892 |
} |
| 893 |
|
| 894 |
if (ast_strlen_zero(args.mode) || strcasecmp(args.mode, "manipulate")) { |
| 895 |
ast_log(LOG_ERROR, "'%s' is not a supported mode. Only manipulate is supported.\n", |
| 896 |
S_OR(args.mode, "<none>")); |
| 897 |
goto return_error; |
| 898 |
} |
| 899 |
|
| 900 |
if (!(jack_data = jack_data_alloc())) |
| 901 |
goto return_error; |
| 902 |
|
| 903 |
if (!ast_strlen_zero(args.options) && handle_options(jack_data, args.options)) |
| 904 |
goto return_error; |
| 905 |
|
| 906 |
if (init_jack_data(chan, jack_data)) |
| 907 |
goto return_error; |
| 908 |
|
| 909 |
if (!(datastore = ast_datastore_alloc(&jack_hook_ds_info, NULL))) |
| 910 |
goto return_error; |
| 911 |
|
| 912 |
jack_data->has_audiohook = 1; |
| 913 |
ast_audiohook_init(&jack_data->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "JACK_HOOK", AST_AUDIOHOOK_MANIPULATE_ALL_RATES); |
| 914 |
jack_data->audiohook.manipulate_callback = jack_hook_callback; |
| 915 |
|
| 916 |
datastore->data = jack_data; |
| 917 |
|
| 918 |
if (ast_audiohook_attach(chan, &jack_data->audiohook)) |
| 919 |
goto return_error; |
| 920 |
|
| 921 |
if (ast_channel_datastore_add(chan, datastore)) |
| 922 |
goto return_error; |
| 923 |
|
| 924 |
ast_channel_unlock(chan); |
| 925 |
|
| 926 |
return 0; |
| 927 |
|
| 928 |
return_error: |
| 929 |
ast_channel_unlock(chan); |
| 930 |
|
| 931 |
if (jack_data) { |
| 932 |
destroy_jack_data(jack_data); |
| 933 |
} |
| 934 |
|
| 935 |
if (datastore) { |
| 936 |
datastore->data = NULL; |
| 937 |
ast_datastore_free(datastore); |
| 938 |
} |
| 939 |
|
| 940 |
return -1; |
| 941 |
} |
| 942 |
|
| 943 |
static int disable_jack_hook(struct ast_channel *chan) |
| 944 |
{ |
| 945 |
struct ast_datastore *datastore; |
| 946 |
struct jack_data *jack_data; |
| 947 |
|
| 948 |
ast_channel_lock(chan); |
| 949 |
|
| 950 |
if (!(datastore = ast_channel_datastore_find(chan, &jack_hook_ds_info, NULL))) { |
| 951 |
ast_channel_unlock(chan); |
| 952 |
ast_log(LOG_WARNING, "No JACK_HOOK found to disable\n"); |
| 953 |
return -1; |
| 954 |
} |
| 955 |
|
| 956 |
ast_channel_datastore_remove(chan, datastore); |
| 957 |
|
| 958 |
jack_data = datastore->data; |
| 959 |
ast_audiohook_detach(&jack_data->audiohook); |
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
ast_datastore_free(datastore); |
| 965 |
|
| 966 |
ast_channel_unlock(chan); |
| 967 |
|
| 968 |
return 0; |
| 969 |
} |
| 970 |
|
| 971 |
static int jack_hook_write(struct ast_channel *chan, const char *cmd, char *data, |
| 972 |
const char *value) |
| 973 |
{ |
| 974 |
int res; |
| 975 |
|
| 976 |
if (!chan) { |
| 977 |
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd); |
| 978 |
return -1; |
| 979 |
} |
| 980 |
|
| 981 |
if (!strcasecmp(value, "on")) |
| 982 |
res = enable_jack_hook(chan, data); |
| 983 |
else if (!strcasecmp(value, "off")) |
| 984 |
res = disable_jack_hook(chan); |
| 985 |
else { |
| 986 |
ast_log(LOG_ERROR, "'%s' is not a valid value for JACK_HOOK()\n", value); |
| 987 |
res = -1; |
| 988 |
} |
| 989 |
|
| 990 |
return res; |
| 991 |
} |
| 992 |
|
| 993 |
static struct ast_custom_function jack_hook_function = { |
| 994 |
.name = "JACK_HOOK", |
| 995 |
.synopsis = "Enable a jack hook on a channel", |
| 996 |
.syntax = "JACK_HOOK(<mode>,[options])", |
| 997 |
.desc = |
| 998 |
" The JACK_HOOK allows turning on or off jack connectivity to this channel.\n" |
| 999 |
"When the JACK_HOOK is turned on, jack ports will get created that allow\n" |
| 1000 |
"access to the audio stream for this channel. The mode specifies which mode\n" |
| 1001 |
"this hook should run in. A mode must be specified when turning the JACK_HOOK.\n" |
| 1002 |
"on. However, all arguments are optional when turning it off.\n" |
| 1003 |
"\n" |
| 1004 |
" Valid modes are:\n" |
| 1005 |
#if 0 |
| 1006 |
|
| 1007 |
" spy - Create a read-only audio hook. Only an output jack port will\n" |
| 1008 |
" get created.\n" |
| 1009 |
" whisper - Create a write-only audio hook. Only an input jack port will\n" |
| 1010 |
" get created.\n" |
| 1011 |
#endif |
| 1012 |
" manipulate - Create a read/write audio hook. Both an input and an output\n" |
| 1013 |
" jack port will get created. Audio from the channel will be\n" |
| 1014 |
" sent out the output port and will be replaced by the audio\n" |
| 1015 |
" coming in on the input port as it gets passed on.\n" |
| 1016 |
"\n" |
| 1017 |
" Valid options are:\n" |
| 1018 |
COMMON_OPTIONS |
| 1019 |
"\n" |
| 1020 |
" Examples:\n" |
| 1021 |
" To turn on the JACK_HOOK,\n" |
| 1022 |
" Set(JACK_HOOK(manipulate,i(pure_data_0:input0)o(pure_data_0:output0))=on)\n" |
| 1023 |
" To turn off the JACK_HOOK,\n" |
| 1024 |
" Set(JACK_HOOK()=off)\n" |
| 1025 |
"", |
| 1026 |
.write = jack_hook_write, |
| 1027 |
}; |
| 1028 |
|
| 1029 |
static int unload_module(void) |
| 1030 |
{ |
| 1031 |
int res; |
| 1032 |
|
| 1033 |
res = ast_unregister_application(jack_app); |
| 1034 |
res |= ast_custom_function_unregister(&jack_hook_function); |
| 1035 |
|
| 1036 |
return res; |
| 1037 |
} |
| 1038 |
|
| 1039 |
static int load_module(void) |
| 1040 |
{ |
| 1041 |
if (ast_register_application_xml(jack_app, jack_exec)) { |
| 1042 |
return AST_MODULE_LOAD_DECLINE; |
| 1043 |
} |
| 1044 |
|
| 1045 |
if (ast_custom_function_register(&jack_hook_function)) { |
| 1046 |
ast_unregister_application(jack_app); |
| 1047 |
return AST_MODULE_LOAD_DECLINE; |
| 1048 |
} |
| 1049 |
|
| 1050 |
return AST_MODULE_LOAD_SUCCESS; |
| 1051 |
} |
| 1052 |
|
| 1053 |
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "JACK Interface"); |