summaryrefslogtreecommitdiff
path: root/example-clients/netslave.c
blob: eb92339822b9bce5d0f60d6bd96ba4e7827b0ba9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
    Copyright (C) 2009 Grame

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include <math.h>
#include <signal.h>
#include <getopt.h>
#include <string.h>

#include <jack/net.h>

jack_net_slave_t* net;

static void signal_handler(int sig)
{
	jack_net_slave_close(net);
	fprintf(stderr, "signal received, exiting ...\n");
	exit(0);
}

static void
usage ()
{
	fprintf (stderr, "\n"
    "usage: jack_net_slave \n"
    "              [ -C capture channels (default = 2)]\n"
    "              [ -P playback channels (default = 2) ]\n"
    "              [ -a hostname (default = %s) ]\n"
    "              [ -p port (default = %d)]\n", DEFAULT_MULTICAST_IP, DEFAULT_PORT);
}

static void net_shutdown(void* data)
{
    printf("Restarting...\n");
}

static int net_process(jack_nframes_t buffer_size,
                        int audio_input,
                        float** audio_input_buffer,
                        int midi_input,
                        void** midi_input_buffer,
                        int audio_output,
                        float** audio_output_buffer,
                        int midi_output,
                        void** midi_output_buffer,
                        void* data)
{
    int i;

    // Copy input to output
    for (i = 0; i < audio_input; i++) {
        memcpy(audio_output_buffer[i], audio_input_buffer[i], buffer_size * sizeof(float));
    }
    return 0;
}

int
main (int argc, char *argv[])
{
    int audio_input = 2;
    int audio_output = 2;
    int udp_port = DEFAULT_PORT;
    const char* multicast_ip = DEFAULT_MULTICAST_IP;
    const char *options = "C:P:a:p:h";
    int option_index;
    int opt;

	struct option long_options[] =
	{
		{"audio input", 1, 0, 'C'},
		{"audio output", 1, 0, 'P'},
		{"hostname", 1, 0, 'a'},
		{"port", 1, 0, 'p'},
		{0, 0, 0, 0}
	};

	while ((opt = getopt_long (argc, argv, options, long_options, &option_index)) != EOF) {

		switch (opt) {

		case 'C':
			audio_input = atoi(optarg);
			break;

		case 'P':
			audio_output = atoi(optarg);
			break;

		case 'a':
            multicast_ip = strdup(optarg);
            break;

		case 'p':
			udp_port = atoi(optarg);
			break;

		case 'h':
			usage();
			return -1;
		}
	}

    jack_slave_t request = { audio_input, audio_output, 0, 0, DEFAULT_MTU, -1, JackFloatEncoder, 0, 2 };
    jack_master_t result;

    printf("Waiting for a master...\n");

    if ((net = jack_net_slave_open(multicast_ip, udp_port, "net_slave", &request, &result)) == 0) {
    	fprintf(stderr, "JACK server not running?\n");
		return 1;
	}

    printf("Master is found and running...\n");

    jack_set_net_slave_process_callback(net, net_process, NULL);
    jack_set_net_slave_shutdown_callback(net, net_shutdown, NULL);

    if (jack_net_slave_activate(net) != 0) {
    	fprintf(stderr, "Cannot activate slave client\n");
		return 1;
	}

    /* install a signal handler to properly quits jack client */
#ifdef WIN32
	signal(SIGINT, signal_handler);
    signal(SIGABRT, signal_handler);
	signal(SIGTERM, signal_handler);
#else
	signal(SIGQUIT, signal_handler);
	signal(SIGTERM, signal_handler);
	signal(SIGHUP, signal_handler);
	signal(SIGINT, signal_handler);
#endif

    /* run until interrupted */
	while (1) {
	#ifdef WIN32
		Sleep(1000);
	#else
		sleep(1);
	#endif
	};

    // Wait for application end
    jack_net_slave_deactivate(net);
    jack_net_slave_close(net);
    exit(0);
}