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"
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" },
174 return "Unknown Error";
182 for (i = 0; i < (
sizeof(
status) * 8); i++) {
198 double from_srate, to_srate, jack_srate;
200 double *resample_factor;
216 if (from_srate == to_srate) {
219 *resample_factor = 1.0;
223 *resample_factor = to_srate / from_srate;
229 *resample_factor, *resample_factor))) {
231 input ?
"input" :
"output");
247 short s_buf[nframes];
251 size_t write_len =
sizeof(s_buf);
254 int total_in_buf_used = 0;
255 int total_out_buf_used = 0;
256 float f_buf[nframes + 1];
258 memset(f_buf, 0,
sizeof(f_buf));
260 while (total_in_buf_used < nframes) {
266 &in_buf[total_in_buf_used], nframes - total_in_buf_used,
268 &f_buf[total_out_buf_used],
ARRAY_LEN(f_buf) - total_out_buf_used);
270 if (out_buf_used < 0)
273 total_out_buf_used += out_buf_used;
274 total_in_buf_used += in_buf_used;
276 if (total_out_buf_used ==
ARRAY_LEN(f_buf)) {
278 "nframes '%d', total_out_buf_used '%d'\n", nframes, total_out_buf_used);
283 for (i = 0; i < total_out_buf_used; i++)
284 s_buf[i] = f_buf[i] * (SHRT_MAX / 1.0);
286 write_len = total_out_buf_used *
sizeof(int16_t);
290 for (i = 0; i < nframes; i++)
291 s_buf[i] = in_buf[i] * (SHRT_MAX / 1.0);
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);
312 len = nframes *
sizeof(float);
317 ast_debug(2,
"Wanted %d bytes to send to the output port, "
318 "but only got %d\n", (
int)
len, (
int) res);
325 void *input_port_buf, *output_port_buf;
397 jack_options_t jack_options = JackNullOption;
399 unsigned int channel_rate;
401 unsigned int ringbuffer_size;
415 ast_debug(1,
"Audiohook parameters: slin-format:%s, rate:%d, frame-len:%d, ringbuffer_size: %d\n",
433 jack_options |= JackNoStartServer;
436 jack_options |= JackServerName;
450 JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput | JackPortIsTerminal, 0);
457 JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput | JackPortIsTerminal, 0);
464 ast_log(
LOG_ERROR,
"Failed to register process callback with jack client\n");
480 NULL, JackPortIsInput);
488 for (i = 0; ports[i]; i++) {
489 ast_debug(1,
"Found port '%s' that matched specified input port '%s'\n",
497 ast_debug(1,
"Connected '%s' to '%s'\n", ports[0],
511 NULL, JackPortIsOutput);
519 for (i = 0; ports[i]; i++) {
520 ast_debug(1,
"Found port '%s' that matched specified output port '%s'\n",
528 ast_debug(1,
"Connected '%s' to '%s'\n", ports[0],
543 size_t f_buf_used = 0;
548 memset(f_buf, 0,
sizeof(f_buf));
555 int total_in_buf_used = 0;
556 int total_out_buf_used = 0;
558 memset(in_buf, 0,
sizeof(in_buf));
560 for (i = 0; i < f->
samples; i++)
561 in_buf[i] = s_buf[i] * (1.0 / SHRT_MAX);
563 while (total_in_buf_used <
ARRAY_LEN(in_buf)) {
569 &in_buf[total_in_buf_used],
ARRAY_LEN(in_buf) - total_in_buf_used,
571 &f_buf[total_out_buf_used],
ARRAY_LEN(f_buf) - total_out_buf_used);
573 if (out_buf_used < 0)
576 total_out_buf_used += out_buf_used;
577 total_in_buf_used += in_buf_used;
579 if (total_out_buf_used ==
ARRAY_LEN(f_buf)) {
585 f_buf_used = total_out_buf_used;
591 for (i = 0; i < f->
samples; i++)
592 f_buf[i] = s_buf[i] * (1.0 / SHRT_MAX);
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);
631 .datalen =
sizeof(
buf),
636 size_t res, read_len;
639 read_len = out_frame ? out_frame->
datalen :
sizeof(
buf);
640 read_buf = out_frame ? out_frame->
data.
ptr :
buf;
644 if (res < read_len) {
647 ast_debug(1,
"Sending an empty frame for the JACK_HOOK\n");
655 if (res < read_len) {
656 ast_log(
LOG_ERROR,
"Error reading from ringbuffer, even though it said there was enough data\n");
895 ast_log(
LOG_ERROR,
"'%s' is not a supported mode. Only manipulate is supported.\n",
981 if (!strcasecmp(
value,
"on"))
983 else if (!strcasecmp(
value,
"off"))
995 .synopsis =
"Enable a jack hook on a channel",
996 .syntax =
"JACK_HOOK(<mode>,[options])",
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"
1004 " Valid modes are:\n"
1007 " spy - Create a read-only audio hook. Only an output jack port will\n"
1009 " whisper - Create a write-only audio hook. Only an input jack port will\n"
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"
1017 " Valid options are:\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"
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.
union ast_frame::@228 data
struct ast_frame_subclass subclass
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)