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 if (ast_waitfor(chan, -1) < 0) {
781 switch (f->frametype) {
782 case AST_FRAME_CONTROL:
783 if (f->subclass.integer == AST_CONTROL_HANGUP)
786 case AST_FRAME_VOICE:
787 queue_voice_frame(jack_data, f);
794 handle_jack_audio(chan, jack_data, NULL);
797 jack_data = destroy_jack_data(jack_data);
802 static void jack_hook_ds_destroy(void *data)
804 struct jack_data *jack_data = data;
806 destroy_jack_data(jack_data);
809 static const struct ast_datastore_info jack_hook_ds_info = {
811 .destroy = jack_hook_ds_destroy,
814 static int jack_hook_callback(struct ast_audiohook *audiohook, struct ast_channel *chan,
815 struct ast_frame *frame, enum ast_audiohook_direction direction)
817 struct ast_datastore *datastore;
818 struct jack_data *jack_data;
820 if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
823 if (direction != AST_AUDIOHOOK_DIRECTION_READ)
826 if (frame->frametype != AST_FRAME_VOICE)
829 if (frame->subclass.format.id != AST_FORMAT_SLINEAR) {
830 ast_log(LOG_WARNING, "Expected frame in SLINEAR for the audiohook, but got format %s\n",
831 ast_getformatname(&frame->subclass.format));
835 ast_channel_lock(chan);
837 if (!(datastore = ast_channel_datastore_find(chan, &jack_hook_ds_info, NULL))) {
838 ast_log(LOG_ERROR, "JACK_HOOK datastore not found for '%s'\n", ast_channel_name(chan));
839 ast_channel_unlock(chan);
843 jack_data = datastore->data;
845 queue_voice_frame(jack_data, frame);
847 handle_jack_audio(chan, jack_data, frame);
849 ast_channel_unlock(chan);
854 static int enable_jack_hook(struct ast_channel *chan, char *data)
856 struct ast_datastore *datastore;
857 struct jack_data *jack_data = NULL;
858 AST_DECLARE_APP_ARGS(args,
860 AST_APP_ARG(options);
863 AST_STANDARD_APP_ARGS(args, data);
865 ast_channel_lock(chan);
867 if ((datastore = ast_channel_datastore_find(chan, &jack_hook_ds_info, NULL))) {
868 ast_log(LOG_ERROR, "JACK_HOOK already enabled for '%s'\n", ast_channel_name(chan));
872 if (ast_strlen_zero(args.mode) || strcasecmp(args.mode, "manipulate")) {
873 ast_log(LOG_ERROR, "'%s' is not a supported mode. Only manipulate is supported.\n",
874 S_OR(args.mode, "<none>"));
878 if (!(jack_data = jack_data_alloc()))
881 if (!ast_strlen_zero(args.options) && handle_options(jack_data, args.options))
884 if (init_jack_data(chan, jack_data))
887 if (!(datastore = ast_datastore_alloc(&jack_hook_ds_info, NULL)))
890 jack_data->has_audiohook = 1;
891 ast_audiohook_init(&jack_data->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "JACK_HOOK", 0);
892 jack_data->audiohook.manipulate_callback = jack_hook_callback;
894 datastore->data = jack_data;
896 if (ast_audiohook_attach(chan, &jack_data->audiohook))
899 if (ast_channel_datastore_add(chan, datastore))
902 ast_channel_unlock(chan);
907 ast_channel_unlock(chan);
910 destroy_jack_data(jack_data);
914 datastore->data = NULL;
915 ast_datastore_free(datastore);
921 static int disable_jack_hook(struct ast_channel *chan)
923 struct ast_datastore *datastore;
924 struct jack_data *jack_data;
926 ast_channel_lock(chan);
928 if (!(datastore = ast_channel_datastore_find(chan, &jack_hook_ds_info, NULL))) {
929 ast_channel_unlock(chan);
930 ast_log(LOG_WARNING, "No JACK_HOOK found to disable\n");
934 ast_channel_datastore_remove(chan, datastore);
936 jack_data = datastore->data;
937 ast_audiohook_detach(&jack_data->audiohook);
939 /* Keep the channel locked while we destroy the datastore, so that we can
940 * ensure that all of the jack stuff is stopped just in case another frame
941 * tries to come through the audiohook callback. */
942 ast_datastore_free(datastore);
944 ast_channel_unlock(chan);
949 static int jack_hook_write(struct ast_channel *chan, const char *cmd, char *data,
955 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
959 if (!strcasecmp(value, "on"))
960 res = enable_jack_hook(chan, data);
961 else if (!strcasecmp(value, "off"))
962 res = disable_jack_hook(chan);
964 ast_log(LOG_ERROR, "'%s' is not a valid value for JACK_HOOK()\n", value);
971 static struct ast_custom_function jack_hook_function = {
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"
1004 .write = jack_hook_write,
1007 static int unload_module(void)
1011 res = ast_unregister_application(jack_app);
1012 res |= ast_custom_function_unregister(&jack_hook_function);
1017 static int load_module(void)
1019 if (ast_register_application_xml(jack_app, jack_exec)) {
1020 return AST_MODULE_LOAD_DECLINE;
1023 if (ast_custom_function_register(&jack_hook_function)) {
1024 ast_unregister_application(jack_app);
1025 return AST_MODULE_LOAD_DECLINE;
1028 return AST_MODULE_LOAD_SUCCESS;
1031 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "JACK Interface");