49 #include <jack/jack.h>
50 #include <jack/ringbuffer.h>
52 #include <libresample.h>
62 #define RESAMPLE_QUALITY 1
64 #define RINGBUFFER_SIZE 16384
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"
142 static const struct {
146 { JackFailure,
"Failure" },
147 { JackInvalidOption,
"Invalid Option" },
148 { JackNameNotUnique,
"Name Not Unique" },
149 { JackServerStarted,
"Server Started" },
150 { JackServerFailed,
"Server Failed" },
151 { JackServerError,
"Server Error" },
152 { JackNoSuchClient,
"No Such Client" },
153 { JackLoadFailure,
"Load Failure" },
154 { JackInitFailure,
"Init Failure" },
155 { JackShmFailure,
"Shared Memory Access Failure" },
156 { JackVersionError,
"Version Mismatch" },
168 return "Unknown Error";
176 for (i = 0; i < (
sizeof(
status) * 8); i++) {
177 if (!(status & (1 << i)))
192 double from_srate, to_srate, jack_srate;
194 double *resample_factor;
202 jack_srate = jack_get_sample_rate(jack_data->
client);
206 to_srate = input ? 8000.0 : jack_srate;
207 from_srate = input ? jack_srate : 8000.0;
212 if (from_srate == to_srate) {
215 *resample_factor = 1.0;
219 *resample_factor = to_srate / from_srate;
225 *resample_factor, *resample_factor))) {
227 input ?
"input" :
"output");
243 short s_buf[nframes];
247 size_t write_len =
sizeof(s_buf);
250 int total_in_buf_used = 0;
251 int total_out_buf_used = 0;
252 float f_buf[nframes + 1];
254 memset(f_buf, 0,
sizeof(f_buf));
256 while (total_in_buf_used < nframes) {
262 &in_buf[total_in_buf_used], nframes - total_in_buf_used,
264 &f_buf[total_out_buf_used],
ARRAY_LEN(f_buf) - total_out_buf_used);
266 if (out_buf_used < 0)
269 total_out_buf_used += out_buf_used;
270 total_in_buf_used += in_buf_used;
272 if (total_out_buf_used ==
ARRAY_LEN(f_buf)) {
274 "nframes '%d', total_out_buf_used '%d'\n", nframes, total_out_buf_used);
279 for (i = 0; i < total_out_buf_used; i++)
280 s_buf[i] = f_buf[i] * (SHRT_MAX / 1.0);
282 write_len = total_out_buf_used *
sizeof(int16_t);
286 for (i = 0; i < nframes; i++)
287 s_buf[i] = in_buf[i] * (SHRT_MAX / 1.0);
290 res = jack_ringbuffer_write(jack_data->
input_rb, (
const char *) s_buf, write_len);
291 if (res != write_len) {
292 ast_debug(2,
"Tried to write %d bytes to the ringbuffer, but only wrote %d\n",
293 (
int)
sizeof(s_buf), (
int) res);
308 len = nframes *
sizeof(float);
310 res = jack_ringbuffer_read(jack_data->
output_rb, buf, len);
313 ast_debug(2,
"Wanted %d bytes to send to the output port, "
314 "but only got %d\n", (
int) len, (
int) res);
321 void *input_port_buf, *output_port_buf;
326 input_port_buf = jack_port_get_buffer(jack_data->
input_port, nframes);
329 output_port_buf = jack_port_get_buffer(jack_data->
output_port, nframes);
355 jack_client_close(jack_data->
client);
360 jack_ringbuffer_free(jack_data->
input_rb);
365 jack_ringbuffer_free(jack_data->
output_rb);
393 jack_options_t jack_options = JackNullOption;
410 jack_options |= JackNoStartServer;
413 jack_options |= JackServerName;
414 jack_data->
client = jack_client_open(client_name, jack_options, &status,
417 jack_data->
client = jack_client_open(client_name, jack_options, &status);
427 JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput | JackPortIsTerminal, 0);
434 JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput | JackPortIsTerminal, 0);
441 ast_log(
LOG_ERROR,
"Failed to register process callback with jack client\n");
447 if (jack_activate(jack_data->
client)) {
457 NULL, JackPortIsInput);
465 for (i = 0; ports[i]; i++) {
466 ast_debug(1,
"Found port '%s' that matched specified input port '%s'\n",
470 if (jack_connect(jack_data->
client, jack_port_name(jack_data->
output_port), ports[0])) {
474 ast_debug(1,
"Connected '%s' to '%s'\n", ports[0],
478 free((
void *) ports);
488 NULL, JackPortIsOutput);
496 for (i = 0; ports[i]; i++) {
497 ast_debug(1,
"Found port '%s' that matched specified output port '%s'\n",
501 if (jack_connect(jack_data->
client, ports[0], jack_port_name(jack_data->
input_port))) {
505 ast_debug(1,
"Connected '%s' to '%s'\n", ports[0],
509 free((
void *) ports);
520 size_t f_buf_used = 0;
525 memset(f_buf, 0,
sizeof(f_buf));
532 int total_in_buf_used = 0;
533 int total_out_buf_used = 0;
535 memset(in_buf, 0,
sizeof(in_buf));
537 for (i = 0; i < f->
samples; i++)
538 in_buf[i] = s_buf[i] * (1.0 / SHRT_MAX);
540 while (total_in_buf_used <
ARRAY_LEN(in_buf)) {
546 &in_buf[total_in_buf_used],
ARRAY_LEN(in_buf) - total_in_buf_used,
548 &f_buf[total_out_buf_used],
ARRAY_LEN(f_buf) - total_out_buf_used);
550 if (out_buf_used < 0)
553 total_out_buf_used += out_buf_used;
554 total_in_buf_used += in_buf_used;
556 if (total_out_buf_used ==
ARRAY_LEN(f_buf)) {
562 f_buf_used = total_out_buf_used;
568 for (i = 0; i < f->
samples; i++)
569 f_buf[i] = s_buf[i] * (1.0 / SHRT_MAX);
574 res = jack_ringbuffer_write(jack_data->
output_rb, (
const char *) f_buf, f_buf_used *
sizeof(
float));
575 if (res != (f_buf_used *
sizeof(
float))) {
576 ast_debug(2,
"Tried to write %d bytes to the ringbuffer, but only wrote %d\n",
577 (
int) (f_buf_used *
sizeof(
float)), (
int) res);
611 .datalen =
sizeof(buf),
616 size_t res, read_len;
619 read_len = out_frame ? out_frame->
datalen :
sizeof(buf);
620 read_buf = out_frame ? out_frame->
data.
ptr : buf;
622 res = jack_ringbuffer_read_space(jack_data->
input_rb);
624 if (res < read_len) {
627 ast_debug(1,
"Sending an empty frame for the JACK_HOOK\n");
633 res = jack_ringbuffer_read(jack_data->
input_rb, (
char *) read_buf, read_len);
635 if (res < read_len) {
636 ast_log(
LOG_ERROR,
"Error reading from ringbuffer, even though it said there was enough data\n");
768 while (!jack_data->
stop) {
830 ast_log(
LOG_WARNING,
"Expected frame in SLINEAR for the audiohook, but got format %s\n",
843 jack_data = datastore->
data;
873 ast_log(
LOG_ERROR,
"'%s' is not a supported mode. Only manipulate is supported.\n",
894 datastore->
data = jack_data;
914 datastore->
data = NULL;
936 jack_data = datastore->
data;
959 if (!strcasecmp(value,
"on"))
961 else if (!strcasecmp(value,
"off"))
973 .synopsis =
"Enable a jack hook on a channel",
974 .syntax =
"JACK_HOOK(<mode>,[options])",
976 " The JACK_HOOK allows turning on or off jack connectivity to this channel.\n"
977 "When the JACK_HOOK is turned on, jack ports will get created that allow\n"
978 "access to the audio stream for this channel. The mode specifies which mode\n"
979 "this hook should run in. A mode must be specified when turning the JACK_HOOK.\n"
980 "on. However, all arguments are optional when turning it off.\n"
982 " Valid modes are:\n"
985 " spy - Create a read-only audio hook. Only an output jack port will\n"
987 " whisper - Create a write-only audio hook. Only an input jack port will\n"
990 " manipulate - Create a read/write audio hook. Both an input and an output\n"
991 " jack port will get created. Audio from the channel will be\n"
992 " sent out the output port and will be replaced by the audio\n"
993 " coming in on the input port as it gets passed on.\n"
995 " Valid options are:\n"
999 " To turn on the JACK_HOOK,\n"
1000 " Set(JACK_HOOK(manipulate,i(pure_data_0:input0)o(pure_data_0:output0))=on)\n"
1001 " To turn off the JACK_HOOK,\n"
1002 " Set(JACK_HOOK()=off)\n"
static int unload_module(void)
union ast_frame_subclass subclass
const ast_string_field connect_input_port
#define ast_channel_lock(chan)
Main Channel structure associated with a channel.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
#define AST_APP_OPTION_ARG(option, flagno, argno)
Declares an application option that accepts an argument.
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
unsigned int has_audiohook
#define AST_APP_OPTIONS(holder, options...)
Declares an array of options for an application.
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)
unsigned int no_start_server
int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source)
Initialize an audiohook structure.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
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_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
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.
static void handle_output(void *buf, jack_nframes_t nframes, struct jack_data *jack_data)
Handle jack output port.
jack_port_t * output_port
ast_audiohook_manipulate_callback manipulate_callback
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
int ast_set_write_format(struct ast_channel *chan, format_t format)
Sets write format on channel chan Set write format for channel to whichever component of "format" is ...
#define COMMON_OPTIONS
Common options between the Jack() app and JACK_HOOK() function.
static struct ast_app_option jack_exec_options[128]
static int jack_exec(struct ast_channel *chan, const char *data)
int ast_set_read_format(struct ast_channel *chan, format_t format)
Sets read format on channel chan Set read format for channel to whichever component of "format" is be...
#define ast_debug(level,...)
Log a DEBUG message.
General Asterisk PBX channel definitions.
static struct ast_custom_function jack_hook_function
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Data structure associated with a custom dialplan function.
#define AST_STRING_FIELD(name)
Declare a string field.
static int jack_hook_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
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)
Core PBX routines and definitions.
jack_ringbuffer_t * output_rb
static struct ast_datastore_info jack_hook_ds_info
#define ast_strdupa(s)
duplicate a string in memory from the stack
const ast_string_field server_name
int ast_audiohook_detach(struct ast_audiohook *audiohook)
Detach audiohook from channel.
char * ast_getformatname(format_t format)
Get the name of a format.
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 ...
const ast_string_field client_name
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
const ast_string_field name
struct sla_ringing_trunk * first
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
struct ast_datastore * ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
#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)
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
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)
#define AST_FORMAT_SLINEAR
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
#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)
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
Data structure associated with a single frame of data.
static struct jack_data * jack_data_alloc(void)
static int queue_voice_frame(struct jack_data *jack_data, struct ast_frame *f)
enum ast_audiohook_status status
#define AST_APP_ARG(name)
Define an application argument.
enum ast_frame_type frametype
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
struct ast_audiohook audiohook
const ast_string_field connect_output_port
static struct @22 jack_status_table[]
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
union ast_frame::@172 data
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
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.
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
#define AST_APP_OPTION(option, flagno)
Declares an application option that does not accept an argument.
static char prefix[MAX_PREFIX]
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.