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"
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++) {
195 double from_srate, to_srate, jack_srate;
197 double *resample_factor;
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);
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;
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",
430 jack_options |= JackNoStartServer;
433 jack_options |= JackServerName;
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");
477 NULL, JackPortIsInput);
485 for (i = 0; ports[i]; i++) {
486 ast_debug(1,
"Found port '%s' that matched specified input port '%s'\n",
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",
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);
628 .datalen =
sizeof(
buf),
633 size_t res, read_len;
636 read_len = out_frame ? out_frame->
datalen :
sizeof(
buf);
637 read_buf = out_frame ? out_frame->
data.
ptr :
buf;
641 if (res < read_len) {
644 ast_debug(1,
"Sending an empty frame for the JACK_HOOK\n");
652 if (res < read_len) {
653 ast_log(
LOG_ERROR,
"Error reading from ringbuffer, even though it said there was enough data\n");
892 ast_log(
LOG_ERROR,
"'%s' is not a supported mode. Only manipulate is supported.\n",
978 if (!strcasecmp(
value,
"on"))
980 else if (!strcasecmp(
value,
"off"))
992 .synopsis =
"Enable a jack hook on a channel",
993 .syntax =
"JACK_HOOK(<mode>,[options])",
995 " The JACK_HOOK allows turning on or off jack connectivity to this channel.\n"
996 "When the JACK_HOOK is turned on, jack ports will get created that allow\n"
997 "access to the audio stream for this channel. The mode specifies which mode\n"
998 "this hook should run in. A mode must be specified when turning the JACK_HOOK.\n"
999 "on. However, all arguments are optional when turning it off.\n"
1001 " Valid modes are:\n"
1004 " spy - Create a read-only audio hook. Only an output jack port will\n"
1006 " whisper - Create a write-only audio hook. Only an input jack port will\n"
1009 " manipulate - Create a read/write audio hook. Both an input and an output\n"
1010 " jack port will get created. Audio from the channel will be\n"
1011 " sent out the output port and will be replaced by the audio\n"
1012 " coming in on the input port as it gets passed on.\n"
1014 " Valid options are:\n"
1018 " To turn on the JACK_HOOK,\n"
1019 " Set(JACK_HOOK(manipulate,i(pure_data_0:input0)o(pure_data_0:output0))=on)\n"
1020 " To turn off the JACK_HOOK,\n"
1021 " Set(JACK_HOOK()=off)\n"
static struct jack_data * destroy_jack_data(struct jack_data *jack_data)
static int queue_voice_frame(struct jack_data *jack_data, struct ast_frame *f)
static int jack_process(jack_nframes_t nframes, void *arg)
static const struct @27 jack_status_table[]
static void handle_output(void *buf, jack_nframes_t nframes, struct jack_data *jack_data)
Handle jack output port.
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "JACK Interface")
#define COMMON_OPTIONS
Common options between the Jack() app and JACK_HOOK() function.
static const struct ast_datastore_info jack_hook_ds_info
static const char * jack_status_to_str(jack_status_t status)
static int disable_jack_hook(struct ast_channel *chan)
static void handle_input(void *buf, jack_nframes_t nframes, struct jack_data *jack_data)
Handle jack input port.
static int enable_jack_hook(struct ast_channel *chan, char *data)
static void jack_hook_ds_destroy(void *data)
static int handle_options(struct jack_data *jack_data, const char *__options_str)
static struct ast_custom_function jack_hook_function
static int init_jack_data(struct ast_channel *chan, struct jack_data *jack_data)
static struct jack_data * jack_data_alloc(void)
static int jack_exec(struct ast_channel *chan, const char *data)
static int jack_hook_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
#define RINGBUFFER_FRAME_CAPACITY
static int jack_hook_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
static void handle_jack_audio(struct ast_channel *chan, struct jack_data *jack_data, struct ast_frame *out_frame)
handle jack audio
static void jack_shutdown(void *arg)
static void log_jack_status(const char *prefix, jack_status_t status)
static int load_module(void)
static const struct ast_app_option jack_exec_options[128]
static int unload_module(void)
static const char jack_app[]
static int alloc_resampler(struct jack_data *jack_data, int input)
struct sla_ringing_trunk * first
static int input(yyscan_t yyscanner)
Asterisk main include file. File version handling, generic pbx functions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
@ AST_AUDIOHOOK_MANIPULATE_ALL_RATES
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.
@ AST_AUDIOHOOK_DIRECTION_READ
int ast_audiohook_detach(struct ast_audiohook *audiohook)
Detach audiohook from channel.
int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
Attach audiohook to channel.
int ast_audiohook_destroy(struct ast_audiohook *audiohook)
Destroys an audiohook structure.
@ AST_AUDIOHOOK_TYPE_MANIPULATE
@ AST_AUDIOHOOK_STATUS_DONE
General Asterisk PBX channel definitions.
const char * ast_channel_name(const struct ast_channel *chan)
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
Remove a datastore from a channel.
#define ast_channel_lock(chan)
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
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.
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
int ast_set_read_format(struct ast_channel *chan, struct ast_format *format)
Sets read format on channel chan.
int ast_set_write_format(struct ast_channel *chan, struct ast_format *format)
Sets write format on channel chan.
#define ast_channel_unlock(chan)
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.
struct ast_format * ast_channel_readformat(struct ast_channel *chan)
#define ast_datastore_alloc(info, uid)
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static char prefix[MAX_PREFIX]
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_APP_OPTIONS(holder, options...)
Declares an array of options for an application.
#define AST_APP_OPTION_ARG(option, flagno, argno)
Declares an application option that accepts an argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define AST_APP_OPTION(option, flagno)
Declares an application option that does not accept an argument.
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.
#define ast_debug(level,...)
Log a DEBUG message.
Asterisk locking-related definitions:
Asterisk module definitions.
#define ASTERISK_GPL_KEY
The text the key() function should return.
int ast_unregister_application(const char *app)
Unregister an application.
@ AST_MODULE_LOAD_SUCCESS
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Core PBX routines and definitions.
#define ast_custom_function_register(acf)
Register a custom function.
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
#define AST_DECLARE_STRING_FIELDS(field_list)
Declare the fields needed in a structure.
#define ast_calloc_with_stringfields(n, type, size)
Allocate a structure with embedded stringfields in a single allocation.
#define AST_STRING_FIELD(name)
Declare a string field.
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
String manipulation functions.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
#define ast_str_alloca(init_len)
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
ast_audiohook_manipulate_callback manipulate_callback
enum ast_audiohook_status status
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Structure for a data store type.
Structure for a data store object.
Structure used to handle boolean flags.
struct ast_format * format
Data structure associated with a single frame of data.
struct ast_frame_subclass subclass
union ast_frame::@226 data
enum ast_frame_type frametype
Support for dynamic strings.
jack_ringbuffer_t * input_rb
jack_port_t * output_port
double output_resample_factor
struct ast_format * audiohook_format
const ast_string_field client_name
const ast_string_field connect_input_port
double input_resample_factor
jack_ringbuffer_t * output_rb
unsigned int frame_datalen
const ast_string_field connect_output_port
unsigned int no_start_server
struct ast_audiohook audiohook
const ast_string_field server_name
unsigned int has_audiohook
unsigned int audiohook_rate
#define ast_test_flag(p, flag)