47 #include <jack/jack.h> 48 #include <jack/ringbuffer.h> 50 #include <libresample.h> 61 #define RESAMPLE_QUALITY 1 64 #define RINGBUFFER_FRAME_CAPACITY 100 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" \ 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" 145 static const struct {
149 { JackFailure,
"Failure" },
150 { JackInvalidOption,
"Invalid Option" },
151 { JackNameNotUnique,
"Name Not Unique" },
152 { JackServerStarted,
"Server Started" },
153 { JackServerFailed,
"Server Failed" },
154 { JackServerError,
"Server Error" },
155 { JackNoSuchClient,
"No Such Client" },
156 { JackLoadFailure,
"Load Failure" },
157 { JackInitFailure,
"Init Failure" },
158 { JackShmFailure,
"Shared Memory Access Failure" },
159 { JackVersionError,
"Version Mismatch" },
171 return "Unknown Error";
179 for (i = 0; i < (
sizeof(
status) * 8); i++) {
180 if (!(status & (1 << i)))
195 double from_srate, to_srate, jack_srate;
197 double *resample_factor;
205 jack_srate = jack_get_sample_rate(jack_data->
client);
213 if (from_srate == to_srate) {
216 *resample_factor = 1.0;
220 *resample_factor = to_srate / from_srate;
226 *resample_factor, *resample_factor))) {
228 input ?
"input" :
"output");
244 short s_buf[nframes];
248 size_t write_len =
sizeof(s_buf);
251 int total_in_buf_used = 0;
252 int total_out_buf_used = 0;
253 float f_buf[nframes + 1];
255 memset(f_buf, 0,
sizeof(f_buf));
257 while (total_in_buf_used < nframes) {
263 &in_buf[total_in_buf_used], nframes - total_in_buf_used,
265 &f_buf[total_out_buf_used],
ARRAY_LEN(f_buf) - total_out_buf_used);
267 if (out_buf_used < 0)
270 total_out_buf_used += out_buf_used;
271 total_in_buf_used += in_buf_used;
273 if (total_out_buf_used ==
ARRAY_LEN(f_buf)) {
275 "nframes '%d', total_out_buf_used '%d'\n", nframes, total_out_buf_used);
280 for (i = 0; i < total_out_buf_used; i++)
281 s_buf[i] = f_buf[i] * (SHRT_MAX / 1.0);
283 write_len = total_out_buf_used *
sizeof(
int16_t);
287 for (i = 0; i < nframes; i++)
288 s_buf[i] = in_buf[i] * (SHRT_MAX / 1.0);
291 res = jack_ringbuffer_write(jack_data->
input_rb, (
const char *) s_buf, write_len);
292 if (res != write_len) {
293 ast_log(
LOG_WARNING,
"Tried to write %d bytes to the ringbuffer, but only wrote %d\n",
294 (
int)
sizeof(s_buf), (
int) res);
309 len = nframes *
sizeof(float);
311 res = jack_ringbuffer_read(jack_data->
output_rb, buf, len);
314 ast_debug(2,
"Wanted %d bytes to send to the output port, " 315 "but only got %d\n", (
int) len, (
int) res);
322 void *input_port_buf, *output_port_buf;
327 input_port_buf = jack_port_get_buffer(jack_data->
input_port, nframes);
330 output_port_buf = jack_port_get_buffer(jack_data->
output_port, nframes);
356 jack_client_close(jack_data->
client);
361 jack_ringbuffer_free(jack_data->
input_rb);
366 jack_ringbuffer_free(jack_data->
output_rb);
394 jack_options_t jack_options = JackNullOption;
396 unsigned int channel_rate;
398 unsigned int ringbuffer_size;
412 ast_debug(1,
"Audiohook parameters: slin-format:%s, rate:%d, frame-len:%d, ringbuffer_size: %d\n",
423 if (!(jack_data->
output_rb = jack_ringbuffer_create(ringbuffer_size)))
426 if (!(jack_data->
input_rb = jack_ringbuffer_create(ringbuffer_size)))
430 jack_options |= JackNoStartServer;
433 jack_options |= JackServerName;
434 jack_data->
client = jack_client_open(client_name, jack_options, &status,
437 jack_data->
client = jack_client_open(client_name, jack_options, &status);
447 JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput | JackPortIsTerminal, 0);
454 JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput | JackPortIsTerminal, 0);
461 ast_log(
LOG_ERROR,
"Failed to register process callback with jack client\n");
467 if (jack_activate(jack_data->
client)) {
477 NULL, JackPortIsInput);
485 for (i = 0; ports[i]; i++) {
486 ast_debug(1,
"Found port '%s' that matched specified input port '%s'\n",
490 if (jack_connect(jack_data->
client, jack_port_name(jack_data->
output_port), ports[0])) {
494 ast_debug(1,
"Connected '%s' to '%s'\n", ports[0],
508 NULL, JackPortIsOutput);
516 for (i = 0; ports[i]; i++) {
517 ast_debug(1,
"Found port '%s' that matched specified output port '%s'\n",
521 if (jack_connect(jack_data->
client, ports[0], jack_port_name(jack_data->
input_port))) {
525 ast_debug(1,
"Connected '%s' to '%s'\n", ports[0],
540 size_t f_buf_used = 0;
545 memset(f_buf, 0,
sizeof(f_buf));
552 int total_in_buf_used = 0;
553 int total_out_buf_used = 0;
555 memset(in_buf, 0,
sizeof(in_buf));
557 for (i = 0; i < f->
samples; i++)
558 in_buf[i] = s_buf[i] * (1.0 / SHRT_MAX);
560 while (total_in_buf_used <
ARRAY_LEN(in_buf)) {
566 &in_buf[total_in_buf_used],
ARRAY_LEN(in_buf) - total_in_buf_used,
568 &f_buf[total_out_buf_used],
ARRAY_LEN(f_buf) - total_out_buf_used);
570 if (out_buf_used < 0)
573 total_out_buf_used += out_buf_used;
574 total_in_buf_used += in_buf_used;
576 if (total_out_buf_used ==
ARRAY_LEN(f_buf)) {
582 f_buf_used = total_out_buf_used;
588 for (i = 0; i < f->
samples; i++)
589 f_buf[i] = s_buf[i] * (1.0 / SHRT_MAX);
594 res = jack_ringbuffer_write(jack_data->
output_rb, (
const char *) f_buf, f_buf_used *
sizeof(
float));
595 if (res != (f_buf_used *
sizeof(
float))) {
596 ast_log(
LOG_WARNING,
"Tried to write %d bytes to the ringbuffer, but only wrote %d\n",
597 (
int) (f_buf_used *
sizeof(
float)), (
int) res);
630 .datalen =
sizeof(
buf),
635 size_t res, read_len;
638 read_len = out_frame ? out_frame->
datalen :
sizeof(
buf);
639 read_buf = out_frame ? out_frame->
data.
ptr :
buf;
641 res = jack_ringbuffer_read_space(jack_data->
input_rb);
643 if (res < read_len) {
646 ast_debug(1,
"Sending an empty frame for the JACK_HOOK\n");
652 res = jack_ringbuffer_read(jack_data->
input_rb, (
char *) read_buf, read_len);
654 if (res < read_len) {
655 ast_log(
LOG_ERROR,
"Error reading from ringbuffer, even though it said there was enough data\n");
787 while (!jack_data->
stop) {
856 jack_data = datastore->
data;
894 ast_log(
LOG_ERROR,
"'%s' is not a supported mode. Only manipulate is supported.\n",
915 datastore->
data = jack_data;
957 jack_data = datastore->
data;
980 if (!strcasecmp(value,
"on"))
982 else if (!strcasecmp(value,
"off"))
994 .synopsis =
"Enable a jack hook on a channel",
995 .syntax =
"JACK_HOOK(<mode>,[options])",
997 " The JACK_HOOK allows turning on or off jack connectivity to this channel.\n" 998 "When the JACK_HOOK is turned on, jack ports will get created that allow\n" 999 "access to the audio stream for this channel. The mode specifies which mode\n" 1000 "this hook should run in. A mode must be specified when turning the JACK_HOOK.\n" 1001 "on. However, all arguments are optional when turning it off.\n" 1003 " Valid modes are:\n" 1006 " spy - Create a read-only audio hook. Only an output jack port will\n" 1008 " whisper - Create a write-only audio hook. Only an input jack port will\n" 1011 " manipulate - Create a read/write audio hook. Both an input and an output\n" 1012 " jack port will get created. Audio from the channel will be\n" 1013 " sent out the output port and will be replaced by the audio\n" 1014 " coming in on the input port as it gets passed on.\n" 1016 " Valid options are:\n" 1020 " To turn on the JACK_HOOK,\n" 1021 " Set(JACK_HOOK(manipulate,i(pure_data_0:input0)o(pure_data_0:output0))=on)\n" 1022 " To turn off the JACK_HOOK,\n" 1023 " Set(JACK_HOOK()=off)\n" static int unload_module(void)
const ast_string_field connect_input_port
#define ast_channel_lock(chan)
Main Channel structure associated with a channel.
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
unsigned int has_audiohook
static void log_jack_status(const char *prefix, jack_status_t status)
String manipulation functions.
static const char jack_app[]
#define ast_test_flag(p, flag)
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
unsigned int no_start_server
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
static void handle_input(void *buf, jack_nframes_t nframes, struct jack_data *jack_data)
Handle jack input port.
static int handle_options(struct jack_data *jack_data, const char *__options_str)
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
static void jack_shutdown(void *arg)
Structure for a data store type.
static int init_jack_data(struct ast_channel *chan, struct jack_data *jack_data)
#define ast_calloc_with_stringfields(n, type, size)
Allocate a structure with embedded stringfields in a single allocation.
int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
Attach audiohook to channel.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
static const char * jack_status_to_str(jack_status_t status)
#define AST_DECLARE_STRING_FIELDS(field_list)
Declare the fields needed in a structure.
#define ast_str_alloca(init_len)
Structure for a data store object.
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
static int input(yyscan_t yyscanner)
int ast_unregister_application(const char *app)
Unregister an application.
int ast_audiohook_destroy(struct ast_audiohook *audiohook)
Destroys an audiohook structure.
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
struct ast_frame_subclass subclass
static void handle_output(void *buf, jack_nframes_t nframes, struct jack_data *jack_data)
Handle jack output port.
jack_port_t * output_port
#define ast_strlen_zero(foo)
struct ast_format * ast_channel_readformat(struct ast_channel *chan)
#define AST_APP_OPTIONS(holder, options...)
Declares an array of options for an application.
ast_audiohook_manipulate_callback manipulate_callback
int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source, enum ast_audiohook_init_flags flags)
Initialize an audiohook structure.
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
#define COMMON_OPTIONS
Common options between the Jack() app and JACK_HOOK() function.
static int jack_exec(struct ast_channel *chan, const char *data)
#define ast_debug(level,...)
Log a DEBUG message.
static const struct ast_app_option jack_exec_options[128]
General Asterisk PBX channel definitions.
static struct ast_custom_function jack_hook_function
int ast_set_read_format(struct ast_channel *chan, struct ast_format *format)
Sets read format on channel chan.
Data structure associated with a custom dialplan function.
#define AST_STRING_FIELD(name)
Declare a string field.
#define ast_strdupa(s)
duplicate a string in memory from the stack
static int jack_hook_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
int ast_set_write_format(struct ast_channel *chan, struct ast_format *format)
Sets write format on channel chan.
jack_ringbuffer_t * input_rb
static void handle_jack_audio(struct ast_channel *chan, struct jack_data *jack_data, struct ast_frame *out_frame)
handle jack audio
static int alloc_resampler(struct jack_data *jack_data, int input)
int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr)
Parses a string containing application options and sets flags/arguments.
Core PBX routines and definitions.
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "JACK Interface")
jack_ringbuffer_t * output_rb
const ast_string_field server_name
int ast_audiohook_detach(struct ast_audiohook *audiohook)
Detach audiohook from channel.
static struct jack_data * destroy_jack_data(struct jack_data *jack_data)
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
#define AST_APP_OPTION_ARG(option, flagno, argno)
Declares an application option that accepts an argument.
const ast_string_field client_name
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
struct sla_ringing_trunk * first
#define ast_channel_unlock(chan)
static int jack_hook_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
double input_resample_factor
static int jack_process(jack_nframes_t nframes, void *arg)
#define RINGBUFFER_FRAME_CAPACITY
int ast_write(struct ast_channel *chan, struct ast_frame *frame)
Write a frame to a channel This function writes the given frame to the indicated channel.
double output_resample_factor
Module has failed to load, may be in an inconsistent state.
static int disable_jack_hook(struct ast_channel *chan)
Structure used to handle boolean flags.
static int enable_jack_hook(struct ast_channel *chan, char *data)
static int load_module(void)
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
static const struct @29 jack_status_table[]
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
static void jack_hook_ds_destroy(void *data)
const char * ast_channel_name(const struct ast_channel *chan)
unsigned int audiohook_rate
Data structure associated with a single frame of data.
unsigned int frame_datalen
static struct jack_data * jack_data_alloc(void)
static const struct ast_datastore_info jack_hook_ds_info
static int queue_voice_frame(struct jack_data *jack_data, struct ast_frame *f)
enum ast_audiohook_status status
#define ast_datastore_alloc(info, uid)
union ast_frame::@263 data
enum ast_frame_type frametype
struct ast_audiohook audiohook
#define AST_APP_OPTION(option, flagno)
Declares an application option that does not accept an argument.
struct ast_format * format
const ast_string_field connect_output_port
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
struct ast_format * audiohook_format
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
Remove a datastore from a channel.
#define ast_custom_function_register(acf)
Register a custom function.
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
static char prefix[MAX_PREFIX]
#define AST_APP_ARG(name)
Define an application argument.
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.