Merge pull request #23 from jackaudio/device_reservation_fixes
[jack2.git] / example-clients / netslave.c
blobeb92339822b9bce5d0f60d6bd96ba4e7827b0ba9
1 /*
2 Copyright (C) 2009 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <errno.h>
22 #ifndef WIN32
23 #include <unistd.h>
24 #endif
25 #include <math.h>
26 #include <signal.h>
27 #include <getopt.h>
28 #include <string.h>
30 #include <jack/net.h>
32 jack_net_slave_t* net;
34 static void signal_handler(int sig)
36 jack_net_slave_close(net);
37 fprintf(stderr, "signal received, exiting ...\n");
38 exit(0);
41 static void
42 usage ()
44 fprintf (stderr, "\n"
45 "usage: jack_net_slave \n"
46 " [ -C capture channels (default = 2)]\n"
47 " [ -P playback channels (default = 2) ]\n"
48 " [ -a hostname (default = %s) ]\n"
49 " [ -p port (default = %d)]\n", DEFAULT_MULTICAST_IP, DEFAULT_PORT);
52 static void net_shutdown(void* data)
54 printf("Restarting...\n");
57 static int net_process(jack_nframes_t buffer_size,
58 int audio_input,
59 float** audio_input_buffer,
60 int midi_input,
61 void** midi_input_buffer,
62 int audio_output,
63 float** audio_output_buffer,
64 int midi_output,
65 void** midi_output_buffer,
66 void* data)
68 int i;
70 // Copy input to output
71 for (i = 0; i < audio_input; i++) {
72 memcpy(audio_output_buffer[i], audio_input_buffer[i], buffer_size * sizeof(float));
74 return 0;
77 int
78 main (int argc, char *argv[])
80 int audio_input = 2;
81 int audio_output = 2;
82 int udp_port = DEFAULT_PORT;
83 const char* multicast_ip = DEFAULT_MULTICAST_IP;
84 const char *options = "C:P:a:p:h";
85 int option_index;
86 int opt;
88 struct option long_options[] =
90 {"audio input", 1, 0, 'C'},
91 {"audio output", 1, 0, 'P'},
92 {"hostname", 1, 0, 'a'},
93 {"port", 1, 0, 'p'},
94 {0, 0, 0, 0}
97 while ((opt = getopt_long (argc, argv, options, long_options, &option_index)) != EOF) {
99 switch (opt) {
101 case 'C':
102 audio_input = atoi(optarg);
103 break;
105 case 'P':
106 audio_output = atoi(optarg);
107 break;
109 case 'a':
110 multicast_ip = strdup(optarg);
111 break;
113 case 'p':
114 udp_port = atoi(optarg);
115 break;
117 case 'h':
118 usage();
119 return -1;
123 jack_slave_t request = { audio_input, audio_output, 0, 0, DEFAULT_MTU, -1, JackFloatEncoder, 0, 2 };
124 jack_master_t result;
126 printf("Waiting for a master...\n");
128 if ((net = jack_net_slave_open(multicast_ip, udp_port, "net_slave", &request, &result)) == 0) {
129 fprintf(stderr, "JACK server not running?\n");
130 return 1;
133 printf("Master is found and running...\n");
135 jack_set_net_slave_process_callback(net, net_process, NULL);
136 jack_set_net_slave_shutdown_callback(net, net_shutdown, NULL);
138 if (jack_net_slave_activate(net) != 0) {
139 fprintf(stderr, "Cannot activate slave client\n");
140 return 1;
143 /* install a signal handler to properly quits jack client */
144 #ifdef WIN32
145 signal(SIGINT, signal_handler);
146 signal(SIGABRT, signal_handler);
147 signal(SIGTERM, signal_handler);
148 #else
149 signal(SIGQUIT, signal_handler);
150 signal(SIGTERM, signal_handler);
151 signal(SIGHUP, signal_handler);
152 signal(SIGINT, signal_handler);
153 #endif
155 /* run until interrupted */
156 while (1) {
157 #ifdef WIN32
158 Sleep(1000);
159 #else
160 sleep(1);
161 #endif
164 // Wait for application end
165 jack_net_slave_deactivate(net);
166 jack_net_slave_close(net);
167 exit(0);