Jack2: JackServerGlobals.cpp Source File

Jack2 1.9.12

JackServerGlobals.cpp
1 /*
2 Copyright (C) 2005 Grame
3
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.
8
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.
13
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.
17
18 */
19
20 #include "JackServerGlobals.h"
21 #include "JackLockedEngine.h"
22 #include "JackTools.h"
23 #include "shm.h"
24 #include <getopt.h>
25 #include <errno.h>
26
27 static char* server_name = NULL;
28
29 namespace Jack
30 {
31
32 JackServer* JackServerGlobals::fInstance;
33 unsigned int JackServerGlobals::fUserCount;
34 std::map<std::string, JackDriverInfo*> JackServerGlobals::fSlavesList;
35 std::map<std::string, int> JackServerGlobals::fInternalsList;
36
37 bool (* JackServerGlobals::on_device_acquire)(const char * device_name) = NULL;
38 void (* JackServerGlobals::on_device_release)(const char * device_name) = NULL;
39
40 int JackServerGlobals::Start(const char* server_name,
41 jack_driver_desc_t* driver_desc,
42 JSList* driver_params,
43 int sync,
44 int temporary,
45 int time_out_ms,
46 int rt,
47 int priority,
48 int port_max,
49 int verbose,
50 jack_timer_type_t clock,
51 char self_connect_mode)
52 {
53 jack_log("Jackdmp: sync = %ld timeout = %ld rt = %ld priority = %ld verbose = %ld ", sync, time_out_ms, rt, priority, verbose);
54 new JackServer(sync, temporary, time_out_ms, rt, priority, port_max, verbose, clock, self_connect_mode, server_name); // Will setup fInstance and fUserCount globals
55 int res = fInstance->Open(driver_desc, driver_params);
56 return (res < 0) ? res : fInstance->Start();
57 }
58
59 void JackServerGlobals::Stop()
60 {
61 jack_log("Jackdmp: server close");
62 fInstance->Stop();
63 fInstance->Close();
64 }
65
66 void JackServerGlobals::Delete()
67 {
68 jack_log("Jackdmp: delete server");
69
70 // Slave drivers
71 std::map<std::string, JackDriverInfo*>::iterator it1;
72 for (it1 = fSlavesList.begin(); it1 != fSlavesList.end(); it1++) {
73 JackDriverInfo* info = (*it1).second;
74 if (info) {
75 fInstance->RemoveSlave((info));
76 delete (info);
77 }
78 }
79 fSlavesList.clear();
80
81 // Internal clients
82 std::map<std::string, int> ::iterator it2;
83 for (it2 = fInternalsList.begin(); it2 != fInternalsList.end(); it2++) {
84 int status;
85 int refnum = (*it2).second;
86 if (refnum > 0) {
87 // Client object is internally kept in JackEngine, and will be deallocated in InternalClientUnload
88 fInstance->GetEngine()->InternalClientUnload(refnum, &status);
89 }
90 }
91 fInternalsList.clear();
92
93 delete fInstance;
94 fInstance = NULL;
95 }
96
97 bool JackServerGlobals::Init()
98 {
99 int realtime = 0;
100 int client_timeout = 0; /* msecs; if zero, use period size. */
101 int realtime_priority = 10;
102 int verbose_aux = 0;
103 unsigned int port_max = 128;
104 int temporary = 0;
105
106 int opt = 0;
107 int option_index = 0;
108 char *master_driver_name = NULL;
109 char **master_driver_args = NULL;
110 JSList* master_driver_params = NULL;
111 jack_driver_desc_t* driver_desc;
112 jack_timer_type_t clock_source = JACK_TIMER_SYSTEM_CLOCK;
113 int driver_nargs = 1;
114 JSList* drivers = NULL;
115 int loopback = 0;
116 int sync = 0;
117 int rc, i;
118 int res;
119 int replace_registry = 0;
120
121 FILE* fp = 0;
122 char filename[255];
123 char buffer[255];
124 int argc = 0;
125 char* argv[32];
126
127 // First user starts the server
128 if (fUserCount++ == 0) {
129
130 jack_log("JackServerGlobals Init");
131
132 const char *options = "-d:X:I:P:uvshVrRL:STFl:t:mn:p:"
133 #ifdef __linux__
134 "c:"
135 #endif
136 ;
137
138 struct option long_options[] = {
139 #ifdef __linux__
140 { "clock-source", 1, 0, 'c' },
141 #endif
142 { "loopback-driver", 1, 0, 'L' },
143 { "audio-driver", 1, 0, 'd' },
144 { "midi-driver", 1, 0, 'X' },
145 { "internal-client", 1, 0, 'I' },
146 { "verbose", 0, 0, 'v' },
147 { "help", 0, 0, 'h' },
148 { "port-max", 1, 0, 'p' },
149 { "no-mlock", 0, 0, 'm' },
150 { "name", 1, 0, 'n' },
151 { "unlock", 0, 0, 'u' },
152 { "realtime", 0, 0, 'R' },
153 { "no-realtime", 0, 0, 'r' },
154 { "replace-registry", 0, &replace_registry, 0 },
155 { "loopback", 0, 0, 'L' },
156 { "realtime-priority", 1, 0, 'P' },
157 { "timeout", 1, 0, 't' },
158 { "temporary", 0, 0, 'T' },
159 { "version", 0, 0, 'V' },
160 { "silent", 0, 0, 's' },
161 { "sync", 0, 0, 'S' },
162 { 0, 0, 0, 0 }
163 };
164
165 snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
166 fp = fopen(filename, "r");
167
168 if (!fp) {
169 fp = fopen("/etc/jackdrc", "r");
170 }
171 // if still not found, check old config name for backwards compatability
172 if (!fp) {
173 fp = fopen("/etc/jackd.conf", "r");
174 }
175
176 argc = 0;
177 if (fp) {
178 res = fscanf(fp, "%s", buffer);
179 while (res != 0 && res != EOF) {
180 argv[argc] = (char*)malloc(64);
181 strcpy(argv[argc], buffer);
182 res = fscanf(fp, "%s", buffer);
183 argc++;
184 }
185 fclose(fp);
186 }
187
188 /*
189 For testing
190 int argc = 15;
191 char* argv[] = {"jackdmp", "-R", "-v", "-d", "coreaudio", "-p", "512", "-d", "~:Aggregate:0", "-r", "48000", "-i", "2", "-o", "2" };
192 */
193
194 opterr = 0;
195 optind = 1; // Important : to reset argv parsing
196
197 while (!master_driver_name &&
198 (opt = getopt_long(argc, argv, options, long_options, &option_index)) != EOF) {
199
200 switch (opt) {
201
202 case 'c':
203 if (tolower (optarg[0]) == 'h') {
204 clock_source = JACK_TIMER_HPET;
205 } else if (tolower (optarg[0]) == 'c') {
206 /* For backwards compatibility with scripts, allow
207 * the user to request the cycle clock on the
208 * command line, but use the system clock instead
209 */
210 clock_source = JACK_TIMER_SYSTEM_CLOCK;
211 } else if (tolower (optarg[0]) == 's') {
212 clock_source = JACK_TIMER_SYSTEM_CLOCK;
213 } else {
214 jack_error("unknown option character %c", optopt);
215 }
216 break;
217
218 case 'd':
219 master_driver_name = optarg;
220 break;
221
222 case 'L':
223 loopback = atoi(optarg);
224 break;
225
226 case 'X':
227 fSlavesList[optarg] = NULL;
228 break;
229
230 case 'I':
231 fInternalsList[optarg] = -1;
232 break;
233
234 case 'p':
235 port_max = (unsigned int)atol(optarg);
236 break;
237
238 case 'm':
239 break;
240
241 case 'u':
242 break;
243
244 case 'v':
245 verbose_aux = 1;
246 break;
247
248 case 'S':
249 sync = 1;
250 break;
251
252 case 'n':
253 server_name = optarg;
254 break;
255
256 case 'P':
257 realtime_priority = atoi(optarg);
258 break;
259
260 case 'r':
261 realtime = 0;
262 break;
263
264 case 'R':
265 realtime = 1;
266 break;
267
268 case 'T':
269 temporary = 1;
270 break;
271
272 case 't':
273 client_timeout = atoi(optarg);
274 break;
275
276 default:
277 jack_error("unknown option character %c", optopt);
278 break;
279 }
280 }
281
282 drivers = jack_drivers_load(drivers);
283 if (!drivers) {
284 jack_error("jackdmp: no drivers found; exiting");
285 goto error;
286 }
287
288 driver_desc = jack_find_driver_descriptor(drivers, master_driver_name);
289 if (!driver_desc) {
290 jack_error("jackdmp: unknown master driver '%s'", master_driver_name);
291 goto error;
292 }
293
294 if (optind < argc) {
295 driver_nargs = 1 + argc - optind;
296 } else {
297 driver_nargs = 1;
298 }
299
300 if (driver_nargs == 0) {
301 jack_error("No driver specified ... hmm. JACK won't do"
302 " anything when run like this.");
303 goto error;
304 }
305
306 master_driver_args = (char**)malloc(sizeof(char*) * driver_nargs);
307 master_driver_args[0] = master_driver_name;
308
309 for (i = 1; i < driver_nargs; i++) {
310 master_driver_args[i] = argv[optind++];
311 }
312
313 if (jack_parse_driver_params(driver_desc, driver_nargs, master_driver_args, &master_driver_params)) {
314 goto error;
315 }
316
317 #ifndef WIN32
318 if (server_name == NULL) {
319 server_name = (char*)JackTools::DefaultServerName();
320 }
321 #endif
322
323 rc = jack_register_server(server_name, false);
324 switch (rc) {
325 case EEXIST:
326 jack_error("`%s' server already active", server_name);
327 goto error;
328 case ENOSPC:
329 jack_error("too many servers already active");
330 goto error;
331 case ENOMEM:
332 jack_error("no access to shm registry");
333 goto error;
334 default:
335 jack_info("server `%s' registered", server_name);
336 }
337
338 /* clean up shared memory and files from any previous instance of this server name */
339 jack_cleanup_shm();
340 JackTools::CleanupFiles(server_name);
341
342 if (!realtime && client_timeout == 0) {
343 client_timeout = 500; /* 0.5 sec; usable when non realtime. */
344 }
345
346 for (i = 0; i < argc; i++) {
347 free(argv[i]);
348 }
349
350 int res = Start(server_name, driver_desc, master_driver_params, sync, temporary, client_timeout, realtime, realtime_priority, port_max, verbose_aux, clock_source, JACK_DEFAULT_SELF_CONNECT_MODE);
351 if (res < 0) {
352 jack_error("Cannot start server... exit");
353 Delete();
354 jack_cleanup_shm();
355 JackTools::CleanupFiles(server_name);
356 jack_unregister_server(server_name);
357 goto error;
358 }
359
360 // Slave drivers
361 std::map<std::string, JackDriverInfo*>::iterator it1;
362 for (it1 = fSlavesList.begin(); it1 != fSlavesList.end(); it1++) {
363 const char* name = ((*it1).first).c_str();
364 driver_desc = jack_find_driver_descriptor(drivers, name);
365 if (!driver_desc) {
366 jack_error("jackdmp: unknown slave driver '%s'", name);
367 } else {
368 (*it1).second = fInstance->AddSlave(driver_desc, NULL);
369 }
370 }
371
372 // Loopback driver
373 if (loopback > 0) {
374 driver_desc = jack_find_driver_descriptor(drivers, "loopback");
375 if (!driver_desc) {
376 jack_error("jackdmp: unknown driver '%s'", "loopback");
377 } else {
378 fSlavesList["loopback"] = fInstance->AddSlave(driver_desc, NULL);
379 }
380 }
381
382 // Internal clients
383 std::map<std::string, int>::iterator it2;
384 for (it2 = fInternalsList.begin(); it2 != fInternalsList.end(); it2++) {
385 int status, refnum;
386 const char* name = ((*it2).first).c_str();
387 fInstance->InternalClientLoad2(name, name, NULL, JackNullOption, &refnum, -1, &status);
388 (*it2).second = refnum;
389 }
390 }
391
392 if (master_driver_params) {
393 jack_free_driver_params(master_driver_params);
394 }
395 return true;
396
397 error:
398 jack_log("JackServerGlobals Init error");
399 if (master_driver_params) {
400 jack_free_driver_params(master_driver_params);
401 }
402 Destroy();
403 return false;
404 }
405
406 void JackServerGlobals::Destroy()
407 {
408 if (--fUserCount == 0) {
409 jack_log("JackServerGlobals Destroy");
410 Stop();
411 Delete();
412 jack_cleanup_shm();
413 JackTools::CleanupFiles(server_name);
414 jack_unregister_server(server_name);
415 }
416 }
417
418 } // end of namespace
419
420
jack_log
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108
JackDriverInfo
Definition: JackDriverInfo.h:30
jack_error
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
option
Definition: getopt.h:84
jack_driver_desc_t
Definition: driver_interface.h:132
_JSList
Definition: linux/alsa/jslist.h:35
jack_info
SERVER_EXPORT void jack_info(const char *fmt,...)
Definition: JackError.cpp:100