Jack2  1.9.8
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 int JackServerGlobals::fRTNotificationSocket;
35 std::map<std::string, JackDriverInfo*> JackServerGlobals::fSlavesList;
36 std::map<std::string, int> JackServerGlobals::fInternalsList;
37 
38 bool (* JackServerGlobals::on_device_acquire)(const char * device_name) = NULL;
39 void (* JackServerGlobals::on_device_release)(const char * device_name) = NULL;
40 
41 int JackServerGlobals::Start(const char* server_name,
42  jack_driver_desc_t* driver_desc,
43  JSList* driver_params,
44  int sync,
45  int temporary,
46  int time_out_ms,
47  int rt,
48  int priority,
49  int port_max,
50  int verbose,
51  jack_timer_type_t clock)
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, 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  int do_mlock = 1;
104  unsigned int port_max = 128;
105  int do_unlock = 0;
106  int temporary = 0;
107 
108  int opt = 0;
109  int option_index = 0;
110  char *master_driver_name = NULL;
111  char **master_driver_args = NULL;
112  JSList* master_driver_params = NULL;
113  jack_driver_desc_t* driver_desc;
114  jack_timer_type_t clock_source = JACK_TIMER_SYSTEM_CLOCK;
115  int driver_nargs = 1;
116  JSList* drivers = NULL;
117  int loopback = 0;
118  int sync = 0;
119  int rc, i;
120  int ret;
121  int replace_registry = 0;
122 
123  FILE* fp = 0;
124  char filename[255];
125  char buffer[255];
126  int argc = 0;
127  char* argv[32];
128 
129  // First user starts the server
130  if (fUserCount++ == 0) {
131 
132  jack_log("JackServerGlobals Init");
133 
134  const char *options = "-d:X:I:P:uvshVrRL:STFl:t:mn:p:"
135  #ifdef __linux__
136  "c:"
137  #endif
138  ;
139 
140  struct option long_options[] = {
141  #ifdef __linux__
142  { "clock-source", 1, 0, 'c' },
143  #endif
144  { "loopback-driver", 1, 0, 'L' },
145  { "audio-driver", 1, 0, 'd' },
146  { "midi-driver", 1, 0, 'X' },
147  { "internal-client", 1, 0, 'I' },
148  { "verbose", 0, 0, 'v' },
149  { "help", 0, 0, 'h' },
150  { "port-max", 1, 0, 'p' },
151  { "no-mlock", 0, 0, 'm' },
152  { "name", 1, 0, 'n' },
153  { "unlock", 0, 0, 'u' },
154  { "realtime", 0, 0, 'R' },
155  { "no-realtime", 0, 0, 'r' },
156  { "replace-registry", 0, &replace_registry, 0 },
157  { "loopback", 0, 0, 'L' },
158  { "realtime-priority", 1, 0, 'P' },
159  { "timeout", 1, 0, 't' },
160  { "temporary", 0, 0, 'T' },
161  { "version", 0, 0, 'V' },
162  { "silent", 0, 0, 's' },
163  { "sync", 0, 0, 'S' },
164  { 0, 0, 0, 0 }
165  };
166 
167  snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
168  fp = fopen(filename, "r");
169 
170  if (!fp) {
171  fp = fopen("/etc/jackdrc", "r");
172  }
173  // if still not found, check old config name for backwards compatability
174  if (!fp) {
175  fp = fopen("/etc/jackd.conf", "r");
176  }
177 
178  argc = 0;
179  if (fp) {
180  ret = fscanf(fp, "%s", buffer);
181  while (ret != 0 && ret != EOF) {
182  argv[argc] = (char*)malloc(64);
183  strcpy(argv[argc], buffer);
184  ret = fscanf(fp, "%s", buffer);
185  argc++;
186  }
187  fclose(fp);
188  }
189 
190  /*
191  For testing
192  int argc = 15;
193  char* argv[] = {"jackdmp", "-R", "-v", "-d", "coreaudio", "-p", "512", "-d", "~:Aggregate:0", "-r", "48000", "-i", "2", "-o", "2" };
194  */
195 
196  opterr = 0;
197  optind = 1; // Important : to reset argv parsing
198 
199  while (!master_driver_name &&
200  (opt = getopt_long(argc, argv, options, long_options, &option_index)) != EOF) {
201 
202  switch (opt) {
203 
204  case 'c':
205  if (tolower (optarg[0]) == 'h') {
206  clock_source = JACK_TIMER_HPET;
207  } else if (tolower (optarg[0]) == 'c') {
208  clock_source = JACK_TIMER_CYCLE_COUNTER;
209  } else if (tolower (optarg[0]) == 's') {
210  clock_source = JACK_TIMER_SYSTEM_CLOCK;
211  } else {
212  jack_error("unknown option character %c", optopt);
213  }
214  break;
215 
216  case 'd':
217  master_driver_name = optarg;
218  break;
219 
220  case 'L':
221  loopback = atoi(optarg);
222  break;
223 
224  case 'X':
225  fSlavesList[optarg] = NULL;
226  break;
227 
228  case 'I':
229  fInternalsList[optarg] = -1;
230  break;
231 
232  case 'p':
233  port_max = (unsigned int)atol(optarg);
234  break;
235 
236  case 'm':
237  do_mlock = 0;
238  break;
239 
240  case 'u':
241  do_unlock = 1;
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 #endif
321 
322  rc = jack_register_server(server_name, false);
323  switch (rc) {
324  case EEXIST:
325  jack_error("`%s' server already active", server_name);
326  goto error;
327  case ENOSPC:
328  jack_error("too many servers already active");
329  goto error;
330  case ENOMEM:
331  jack_error("no access to shm registry");
332  goto error;
333  default:
334  jack_info("server `%s' registered", server_name);
335  }
336 
337  /* clean up shared memory and files from any previous instance of this server name */
338  jack_cleanup_shm();
339  JackTools::CleanupFiles(server_name);
340 
341  if (!realtime && client_timeout == 0)
342  client_timeout = 500; /* 0.5 sec; usable when non realtime. */
343 
344  for (i = 0; i < argc; i++) {
345  free(argv[i]);
346  }
347 
348  int res = Start(server_name, driver_desc, master_driver_params, sync, temporary, client_timeout, realtime, realtime_priority, port_max, verbose_aux, clock_source);
349  if (res < 0) {
350  jack_error("Cannot start server... exit");
351  Delete();
352  jack_cleanup_shm();
353  JackTools::CleanupFiles(server_name);
354  jack_unregister_server(server_name);
355  goto error;
356  }
357 
358  // Slave drivers
359  std::map<std::string, JackDriverInfo*>::iterator it1;
360  for (it1 = fSlavesList.begin(); it1 != fSlavesList.end(); it1++) {
361  const char* name = ((*it1).first).c_str();
362  driver_desc = jack_find_driver_descriptor(drivers, name);
363  if (!driver_desc) {
364  jack_error("jackdmp: unknown slave driver '%s'", name);
365  } else {
366  (*it1).second = fInstance->AddSlave(driver_desc, NULL);
367  }
368  }
369 
370  // Loopback driver
371  if (loopback > 0) {
372  driver_desc = jack_find_driver_descriptor(drivers, "loopback");
373  if (!driver_desc) {
374  jack_error("jackdmp: unknown driver '%s'", "loopback");
375  } else {
376  fSlavesList["loopback"] = fInstance->AddSlave(driver_desc, NULL);
377  }
378  }
379 
380  // Internal clients
381  std::map<std::string, int>::iterator it2;
382  for (it2 = fInternalsList.begin(); it2 != fInternalsList.end(); it2++) {
383  int status, refnum;
384  const char* name = ((*it2).first).c_str();
385  fInstance->InternalClientLoad2(name, name, NULL, JackNullOption, &refnum, -1, &status);
386  (*it2).second = refnum;
387  }
388  }
389 
390  if (master_driver_params)
391  jack_free_driver_params(master_driver_params);
392  return true;
393 
394 error:
395  jack_log("JackServerGlobals Init error");
396  if (master_driver_params)
397  jack_free_driver_params(master_driver_params);
398  Destroy();
399  return false;
400 }
401 
402 void JackServerGlobals::Destroy()
403 {
404  if (--fUserCount == 0) {
405  jack_log("JackServerGlobals Destroy");
406  Stop();
407  Delete();
408  jack_cleanup_shm();
409  JackTools::CleanupFiles(server_name);
410  jack_unregister_server(server_name);
411  }
412 }
413 
414 } // end of namespace
415 
416