2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007 - 2008, Russell Bryant
6 * Russell Bryant <russell@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Jack Application
23 * \author Russell Bryant <russell@digium.com>
25 * This is an application to connect an Asterisk channel to an input
26 * and output jack port so that the audio can be processed through
27 * another application, or to play audio from another application.
29 * \extref http://www.jackaudio.org/
31 * \note To install libresample, check it out of the following repository:
32 * <code>$ svn co http://svn.digium.com/svn/thirdparty/libresample/trunk</code>
34 * \ingroup applications
39 <depend>resample</depend>
40 <support_level>extended</support_level>
45 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
49 #include <jack/jack.h>
50 #include <jack/ringbuffer.h>
52 #include <libresample.h>
54 #include "asterisk/module.h"
55 #include "asterisk/channel.h"
56 #include "asterisk/strings.h"
57 #include "asterisk/lock.h"
58 #include "asterisk/app.h"
59 #include "asterisk/pbx.h"
60 #include "asterisk/audiohook.h"
62 #define RESAMPLE_QUALITY 1
64 #define RINGBUFFER_SIZE 16384
66 /*! \brief Common options between the Jack() app and JACK_HOOK() function */
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"
78 <application name="JACK" language="en_US">
80 Jack Audio Connection Kit
83 <parameter name="options" required="false">
86 <argument name="name" required="true">
87 <para>Connect to the specified jack server name</para>
91 <argument name="name" required="true">
92 <para>Connect the output port that gets created to the specified jack input port</para>
96 <argument name="name" required="true">
97 <para>Connect the input port that gets created to the specified jack output port</para>
101 <argument name="name" required="true">
102 <para>By default, Asterisk will use the channel name for the jack client name.</para>
103 <para>Use this option to specify a custom client name.</para>
110 <para>When executing this application, two jack ports will be created;
111 one input and one output. Other applications can be hooked up to
112 these ports to access audio coming from, or being send to the channel.</para>
117 static const char jack_app[] = "JACK";
120 AST_DECLARE_STRING_FIELDS(
121 AST_STRING_FIELD(server_name);
122 AST_STRING_FIELD(client_name);
123 AST_STRING_FIELD(connect_input_port);
124 AST_STRING_FIELD(connect_output_port);
126 jack_client_t *client;
127 jack_port_t *input_port;
128 jack_port_t *output_port;
129 jack_ringbuffer_t *input_rb;
130 jack_ringbuffer_t *output_rb;
131 void *output_resampler;
132 double output_resample_factor;
133 void *input_resampler;
134 double input_resample_factor;
136 unsigned int has_audiohook:1;
137 unsigned int no_start_server:1;
138 /*! Only used with JACK_HOOK */
139 struct ast_audiohook audiohook;
142 static const struct {
143 jack_status_t status;
145 } jack_status_table[] = {
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" },
159 static const char *jack_status_to_str(jack_status_t status)
163 for (i = 0; i < ARRAY_LEN(jack_status_table); i++) {
164 if (jack_status_table[i].status == status)
165 return jack_status_table[i].str;
168 return "Unknown Error";
171 static void log_jack_status(const char *prefix, jack_status_t status)
173 struct ast_str *str = ast_str_alloca(512);
176 for (i = 0; i < (sizeof(status) * 8); i++) {
177 if (!(status & (1 << i)))
181 ast_str_set(&str, 0, "%s", jack_status_to_str((1 << i)));
184 ast_str_append(&str, 0, ", %s", jack_status_to_str((1 << i)));
187 ast_log(LOG_NOTICE, "%s: %s\n", prefix, ast_str_buffer(str));
190 static int alloc_resampler(struct jack_data *jack_data, int input)
192 double from_srate, to_srate, jack_srate;
194 double *resample_factor;
196 if (input && jack_data->input_resampler)
199 if (!input && jack_data->output_resampler)
202 jack_srate = jack_get_sample_rate(jack_data->client);
204 /* XXX Hard coded 8 kHz */
206 to_srate = input ? 8000.0 : jack_srate;
207 from_srate = input ? jack_srate : 8000.0;
209 resample_factor = input ? &jack_data->input_resample_factor :
210 &jack_data->output_resample_factor;
212 if (from_srate == to_srate) {
213 /* Awesome! The jack sample rate is the same as ours.
214 * Resampling isn't needed. */
215 *resample_factor = 1.0;
219 *resample_factor = to_srate / from_srate;
221 resampler = input ? &jack_data->input_resampler :
222 &jack_data->output_resampler;
224 if (!(*resampler = resample_open(RESAMPLE_QUALITY,
225 *resample_factor, *resample_factor))) {
226 ast_log(LOG_ERROR, "Failed to open %s resampler\n",
227 input ? "input" : "output");
235 * \brief Handle jack input port
237 * Read nframes number of samples from the input buffer, resample it
238 * if necessary, and write it into the appropriate ringbuffer.
240 static void handle_input(void *buf, jack_nframes_t nframes,
241 struct jack_data *jack_data)
243 short s_buf[nframes];
247 size_t write_len = sizeof(s_buf);
249 if (jack_data->input_resampler) {
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) {
260 out_buf_used = resample_process(jack_data->input_resampler,
261 jack_data->input_resample_factor,
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)) {
273 ast_log(LOG_ERROR, "Output buffer filled ... need to increase its size, "
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);
284 /* No resampling needed */
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);
298 * \brief Handle jack output port
300 * Read nframes number of samples from the ringbuffer and write it out to the
301 * output port buffer.
303 static void handle_output(void *buf, jack_nframes_t nframes,
304 struct jack_data *jack_data)
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);
318 static int jack_process(jack_nframes_t nframes, void *arg)
320 struct jack_data *jack_data = arg;
321 void *input_port_buf, *output_port_buf;
323 if (!jack_data->input_resample_factor)
324 alloc_resampler(jack_data, 1);
326 input_port_buf = jack_port_get_buffer(jack_data->input_port, nframes);
327 handle_input(input_port_buf, nframes, jack_data);
329 output_port_buf = jack_port_get_buffer(jack_data->output_port, nframes);
330 handle_output(output_port_buf, nframes, jack_data);
335 static void jack_shutdown(void *arg)
337 struct jack_data *jack_data = arg;
342 static struct jack_data *destroy_jack_data(struct jack_data *jack_data)
344 if (jack_data->input_port) {
345 jack_port_unregister(jack_data->client, jack_data->input_port);
346 jack_data->input_port = NULL;
349 if (jack_data->output_port) {
350 jack_port_unregister(jack_data->client, jack_data->output_port);
351 jack_data->output_port = NULL;
354 if (jack_data->client) {
355 jack_client_close(jack_data->client);
356 jack_data->client = NULL;
359 if (jack_data->input_rb) {
360 jack_ringbuffer_free(jack_data->input_rb);
361 jack_data->input_rb = NULL;
364 if (jack_data->output_rb) {
365 jack_ringbuffer_free(jack_data->output_rb);
366 jack_data->output_rb = NULL;
369 if (jack_data->output_resampler) {
370 resample_close(jack_data->output_resampler);
371 jack_data->output_resampler = NULL;
374 if (jack_data->input_resampler) {
375 resample_close(jack_data->input_resampler);
376 jack_data->input_resampler = NULL;
379 if (jack_data->has_audiohook)
380 ast_audiohook_destroy(&jack_data->audiohook);
382 ast_string_field_free_memory(jack_data);
389 static int init_jack_data(struct ast_channel *chan, struct jack_data *jack_data)
391 const char *client_name;
392 jack_status_t status = 0;
393 jack_options_t jack_options = JackNullOption;
395 if (!ast_strlen_zero(jack_data->client_name)) {
396 client_name = jack_data->client_name;
398 ast_channel_lock(chan);
399 client_name = ast_strdupa(ast_channel_name(chan));
400 ast_channel_unlock(chan);
403 if (!(jack_data->output_rb = jack_ringbuffer_create(RINGBUFFER_SIZE)))
406 if (!(jack_data->input_rb = jack_ringbuffer_create(RINGBUFFER_SIZE)))
409 if (jack_data->no_start_server)
410 jack_options |= JackNoStartServer;
412 if (!ast_strlen_zero(jack_data->server_name)) {
413 jack_options |= JackServerName;
414 jack_data->client = jack_client_open(client_name, jack_options, &status,
415 jack_data->server_name);
417 jack_data->client = jack_client_open(client_name, jack_options, &status);
421 log_jack_status("Client Open Status", status);
423 if (!jack_data->client)
426 jack_data->input_port = jack_port_register(jack_data->client, "input",
427 JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput | JackPortIsTerminal, 0);
428 if (!jack_data->input_port) {
429 ast_log(LOG_ERROR, "Failed to create input port for jack port\n");
433 jack_data->output_port = jack_port_register(jack_data->client, "output",
434 JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput | JackPortIsTerminal, 0);
435 if (!jack_data->output_port) {
436 ast_log(LOG_ERROR, "Failed to create output port for jack port\n");
440 if (jack_set_process_callback(jack_data->client, jack_process, jack_data)) {
441 ast_log(LOG_ERROR, "Failed to register process callback with jack client\n");
445 jack_on_shutdown(jack_data->client, jack_shutdown, jack_data);
447 if (jack_activate(jack_data->client)) {
448 ast_log(LOG_ERROR, "Unable to activate jack client\n");
452 while (!ast_strlen_zero(jack_data->connect_input_port)) {
456 ports = jack_get_ports(jack_data->client, jack_data->connect_input_port,
457 NULL, JackPortIsInput);
460 ast_log(LOG_ERROR, "No input port matching '%s' was found\n",
461 jack_data->connect_input_port);
465 for (i = 0; ports[i]; i++) {
466 ast_debug(1, "Found port '%s' that matched specified input port '%s'\n",
467 ports[i], jack_data->connect_input_port);
470 if (jack_connect(jack_data->client, jack_port_name(jack_data->output_port), ports[0])) {
471 ast_log(LOG_ERROR, "Failed to connect '%s' to '%s'\n", ports[0],
472 jack_port_name(jack_data->output_port));
474 ast_debug(1, "Connected '%s' to '%s'\n", ports[0],
475 jack_port_name(jack_data->output_port));
478 free((void *) ports);
483 while (!ast_strlen_zero(jack_data->connect_output_port)) {
487 ports = jack_get_ports(jack_data->client, jack_data->connect_output_port,
488 NULL, JackPortIsOutput);
491 ast_log(LOG_ERROR, "No output port matching '%s' was found\n",
492 jack_data->connect_output_port);
496 for (i = 0; ports[i]; i++) {
497 ast_debug(1, "Found port '%s' that matched specified output port '%s'\n",
498 ports[i], jack_data->connect_output_port);
501 if (jack_connect(jack_data->client, ports[0], jack_port_name(jack_data->input_port))) {
502 ast_log(LOG_ERROR, "Failed to connect '%s' to '%s'\n", ports[0],
503 jack_port_name(jack_data->input_port));
505 ast_debug(1, "Connected '%s' to '%s'\n", ports[0],
506 jack_port_name(jack_data->input_port));
509 free((void *) ports);
517 static int queue_voice_frame(struct jack_data *jack_data, struct ast_frame *f)
519 float f_buf[f->samples * 8];
520 size_t f_buf_used = 0;
522 int16_t *s_buf = f->data.ptr;
525 memset(f_buf, 0, sizeof(f_buf));
527 if (!jack_data->output_resample_factor)
528 alloc_resampler(jack_data, 0);
530 if (jack_data->output_resampler) {
531 float in_buf[f->samples];
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)) {
544 out_buf_used = resample_process(jack_data->output_resampler,
545 jack_data->output_resample_factor,
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)) {
557 ast_log(LOG_ERROR, "Output buffer filled ... need to increase its size\n");
562 f_buf_used = total_out_buf_used;
563 if (f_buf_used > ARRAY_LEN(f_buf))
564 f_buf_used = ARRAY_LEN(f_buf);
566 /* No resampling needed */
568 for (i = 0; i < f->samples; i++)
569 f_buf[i] = s_buf[i] * (1.0 / SHRT_MAX);
571 f_buf_used = f->samples;
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);
584 * \brief handle jack audio
586 * \param[in] chan The Asterisk channel to write the frames to if no output frame
588 * \param[in] jack_data This is the jack_data struct that contains the input
589 * ringbuffer that audio will be read from.
590 * \param[out] out_frame If this argument is non-NULL, then assuming there is
591 * enough data avilable in the ringbuffer, the audio in this frame
592 * will get replaced with audio from the input buffer. If there is
593 * not enough data available to read at this time, then the frame
594 * data gets zeroed out.
596 * Read data from the input ringbuffer, which is the properly resampled audio
597 * that was read from the jack input port. Write it to the channel in 20 ms frames,
598 * or fill up an output frame instead if one is provided.
602 static void handle_jack_audio(struct ast_channel *chan, struct jack_data *jack_data,
603 struct ast_frame *out_frame)
606 struct ast_frame f = {
607 .frametype = AST_FRAME_VOICE,
610 .datalen = sizeof(buf),
611 .samples = ARRAY_LEN(buf),
613 ast_format_set(&f.subclass.format, AST_FORMAT_SLINEAR, 0);
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) {
625 /* Not enough data ready for another frame, move on ... */
627 ast_debug(1, "Sending an empty frame for the JACK_HOOK\n");
628 memset(out_frame->data.ptr, 0, out_frame->datalen);
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");
641 /* If an output frame was provided, then we just want to fill up the
642 * buffer in that frame and return. */
651 OPT_SERVER_NAME = (1 << 0),
652 OPT_INPUT_PORT = (1 << 1),
653 OPT_OUTPUT_PORT = (1 << 2),
654 OPT_NOSTART_SERVER = (1 << 3),
655 OPT_CLIENT_NAME = (1 << 4),
664 /* Must be the last element */
668 AST_APP_OPTIONS(jack_exec_options, BEGIN_OPTIONS
669 AST_APP_OPTION_ARG('s', OPT_SERVER_NAME, OPT_ARG_SERVER_NAME),
670 AST_APP_OPTION_ARG('i', OPT_INPUT_PORT, OPT_ARG_INPUT_PORT),
671 AST_APP_OPTION_ARG('o', OPT_OUTPUT_PORT, OPT_ARG_OUTPUT_PORT),
672 AST_APP_OPTION('n', OPT_NOSTART_SERVER),
673 AST_APP_OPTION_ARG('c', OPT_CLIENT_NAME, OPT_ARG_CLIENT_NAME),
676 static struct jack_data *jack_data_alloc(void)
678 struct jack_data *jack_data;
680 if (!(jack_data = ast_calloc_with_stringfields(1, struct jack_data, 32))) {
688 * \note This must be done before calling init_jack_data().
690 static int handle_options(struct jack_data *jack_data, const char *__options_str)
692 struct ast_flags options = { 0, };
693 char *option_args[OPT_ARG_ARRAY_SIZE];
696 options_str = ast_strdupa(__options_str);
698 ast_app_parse_options(jack_exec_options, &options, option_args, options_str);
700 if (ast_test_flag(&options, OPT_SERVER_NAME)) {
701 if (!ast_strlen_zero(option_args[OPT_ARG_SERVER_NAME]))
702 ast_string_field_set(jack_data, server_name, option_args[OPT_ARG_SERVER_NAME]);
704 ast_log(LOG_ERROR, "A server name must be provided with the s() option\n");
709 if (ast_test_flag(&options, OPT_CLIENT_NAME)) {
710 if (!ast_strlen_zero(option_args[OPT_ARG_CLIENT_NAME]))
711 ast_string_field_set(jack_data, client_name, option_args[OPT_ARG_CLIENT_NAME]);
713 ast_log(LOG_ERROR, "A client name must be provided with the c() option\n");
718 if (ast_test_flag(&options, OPT_INPUT_PORT)) {
719 if (!ast_strlen_zero(option_args[OPT_ARG_INPUT_PORT]))
720 ast_string_field_set(jack_data, connect_input_port, option_args[OPT_ARG_INPUT_PORT]);
722 ast_log(LOG_ERROR, "A name must be provided with the i() option\n");
727 if (ast_test_flag(&options, OPT_OUTPUT_PORT)) {
728 if (!ast_strlen_zero(option_args[OPT_ARG_OUTPUT_PORT]))
729 ast_string_field_set(jack_data, connect_output_port, option_args[OPT_ARG_OUTPUT_PORT]);
731 ast_log(LOG_ERROR, "A name must be provided with the o() option\n");
736 jack_data->no_start_server = ast_test_flag(&options, OPT_NOSTART_SERVER) ? 1 : 0;
741 static int jack_exec(struct ast_channel *chan, const char *data)
743 struct jack_data *jack_data;
745 if (!(jack_data = jack_data_alloc()))
748 if (!ast_strlen_zero(data) && handle_options(jack_data, data)) {
749 destroy_jack_data(jack_data);
753 if (init_jack_data(chan, jack_data)) {
754 destroy_jack_data(jack_data);
758 if (ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR)) {
759 destroy_jack_data(jack_data);
763 if (ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR)) {
764 destroy_jack_data(jack_data);
768 while (!jack_data->stop) {
771 ast_waitfor(chan, -1);
779 switch (f->frametype) {
780 case AST_FRAME_CONTROL:
781 if (f->subclass.integer == AST_CONTROL_HANGUP)
784 case AST_FRAME_VOICE:
785 queue_voice_frame(jack_data, f);
792 handle_jack_audio(chan, jack_data, NULL);
795 jack_data = destroy_jack_data(jack_data);
800 static void jack_hook_ds_destroy(void *data)
802 struct jack_data *jack_data = data;
804 destroy_jack_data(jack_data);
807 static const struct ast_datastore_info jack_hook_ds_info = {
809 .destroy = jack_hook_ds_destroy,
812 static int jack_hook_callback(struct ast_audiohook *audiohook, struct ast_channel *chan,
813 struct ast_frame *frame, enum ast_audiohook_direction direction)
815 struct ast_datastore *datastore;
816 struct jack_data *jack_data;
818 if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
821 if (direction != AST_AUDIOHOOK_DIRECTION_READ)
824 if (frame->frametype != AST_FRAME_VOICE)
827 if (frame->subclass.format.id != AST_FORMAT_SLINEAR) {
828 ast_log(LOG_WARNING, "Expected frame in SLINEAR for the audiohook, but got format %s\n",
829 ast_getformatname(&frame->subclass.format));
833 ast_channel_lock(chan);
835 if (!(datastore = ast_channel_datastore_find(chan, &jack_hook_ds_info, NULL))) {
836 ast_log(LOG_ERROR, "JACK_HOOK datastore not found for '%s'\n", ast_channel_name(chan));
837 ast_channel_unlock(chan);
841 jack_data = datastore->data;
843 queue_voice_frame(jack_data, frame);
845 handle_jack_audio(chan, jack_data, frame);
847 ast_channel_unlock(chan);
852 static int enable_jack_hook(struct ast_channel *chan, char *data)
854 struct ast_datastore *datastore;
855 struct jack_data *jack_data = NULL;
856 AST_DECLARE_APP_ARGS(args,
858 AST_APP_ARG(options);
861 AST_STANDARD_APP_ARGS(args, data);
863 ast_channel_lock(chan);
865 if ((datastore = ast_channel_datastore_find(chan, &jack_hook_ds_info, NULL))) {
866 ast_log(LOG_ERROR, "JACK_HOOK already enabled for '%s'\n", ast_channel_name(chan));
870 if (ast_strlen_zero(args.mode) || strcasecmp(args.mode, "manipulate")) {
871 ast_log(LOG_ERROR, "'%s' is not a supported mode. Only manipulate is supported.\n",
872 S_OR(args.mode, "<none>"));
876 if (!(jack_data = jack_data_alloc()))
879 if (!ast_strlen_zero(args.options) && handle_options(jack_data, args.options))
882 if (init_jack_data(chan, jack_data))
885 if (!(datastore = ast_datastore_alloc(&jack_hook_ds_info, NULL)))
888 jack_data->has_audiohook = 1;
889 ast_audiohook_init(&jack_data->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "JACK_HOOK", 0);
890 jack_data->audiohook.manipulate_callback = jack_hook_callback;
892 datastore->data = jack_data;
894 if (ast_audiohook_attach(chan, &jack_data->audiohook))
897 if (ast_channel_datastore_add(chan, datastore))
900 ast_channel_unlock(chan);
905 ast_channel_unlock(chan);
908 destroy_jack_data(jack_data);
913 static int disable_jack_hook(struct ast_channel *chan)
915 struct ast_datastore *datastore;
916 struct jack_data *jack_data;
918 ast_channel_lock(chan);
920 if (!(datastore = ast_channel_datastore_find(chan, &jack_hook_ds_info, NULL))) {
921 ast_channel_unlock(chan);
922 ast_log(LOG_WARNING, "No JACK_HOOK found to disable\n");
926 ast_channel_datastore_remove(chan, datastore);
928 jack_data = datastore->data;
929 ast_audiohook_detach(&jack_data->audiohook);
931 /* Keep the channel locked while we destroy the datastore, so that we can
932 * ensure that all of the jack stuff is stopped just in case another frame
933 * tries to come through the audiohook callback. */
934 ast_datastore_free(datastore);
936 ast_channel_unlock(chan);
941 static int jack_hook_write(struct ast_channel *chan, const char *cmd, char *data,
946 if (!strcasecmp(value, "on"))
947 res = enable_jack_hook(chan, data);
948 else if (!strcasecmp(value, "off"))
949 res = disable_jack_hook(chan);
951 ast_log(LOG_ERROR, "'%s' is not a valid value for JACK_HOOK()\n", value);
958 static struct ast_custom_function jack_hook_function = {
960 .synopsis = "Enable a jack hook on a channel",
961 .syntax = "JACK_HOOK(<mode>,[options])",
963 " The JACK_HOOK allows turning on or off jack connectivity to this channel.\n"
964 "When the JACK_HOOK is turned on, jack ports will get created that allow\n"
965 "access to the audio stream for this channel. The mode specifies which mode\n"
966 "this hook should run in. A mode must be specified when turning the JACK_HOOK.\n"
967 "on. However, all arguments are optional when turning it off.\n"
969 " Valid modes are:\n"
972 " spy - Create a read-only audio hook. Only an output jack port will\n"
974 " whisper - Create a write-only audio hook. Only an input jack port will\n"
977 " manipulate - Create a read/write audio hook. Both an input and an output\n"
978 " jack port will get created. Audio from the channel will be\n"
979 " sent out the output port and will be replaced by the audio\n"
980 " coming in on the input port as it gets passed on.\n"
982 " Valid options are:\n"
986 " To turn on the JACK_HOOK,\n"
987 " Set(JACK_HOOK(manipulate,i(pure_data_0:input0)o(pure_data_0:output0))=on)\n"
988 " To turn off the JACK_HOOK,\n"
989 " Set(JACK_HOOK()=off)\n"
991 .write = jack_hook_write,
994 static int unload_module(void)
998 res = ast_unregister_application(jack_app);
999 res |= ast_custom_function_unregister(&jack_hook_function);
1004 static int load_module(void)
1006 if (ast_register_application_xml(jack_app, jack_exec)) {
1007 return AST_MODULE_LOAD_DECLINE;
1010 if (ast_custom_function_register(&jack_hook_function)) {
1011 ast_unregister_application(jack_app);
1012 return AST_MODULE_LOAD_DECLINE;
1015 return AST_MODULE_LOAD_SUCCESS;
1018 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "JACK Interface");