Jack2  1.9.9
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  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 ret;
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  ret = fscanf(fp, "%s", buffer);
179  while (ret != 0 && ret != EOF) {
180  argv[argc] = (char*)malloc(64);
181  strcpy(argv[argc], buffer);
182  ret = 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 'v':
239  verbose_aux = 1;
240  break;
241 
242  case 'S':
243  sync = 1;
244  break;
245 
246  case 'n':
247  server_name = optarg;
248  break;
249 
250  case 'P':
251  realtime_priority = atoi(optarg);
252  break;
253 
254  case 'r':
255  realtime = 0;
256  break;
257 
258  case 'R':
259  realtime = 1;
260  break;
261 
262  case 'T':
263  temporary = 1;
264  break;
265 
266  case 't':
267  client_timeout = atoi(optarg);
268  break;
269 
270  default:
271  jack_error("unknown option character %c", optopt);
272  break;
273  }
274  }
275 
276  drivers = jack_drivers_load(drivers);
277  if (!drivers) {
278  jack_error("jackdmp: no drivers found; exiting");
279  goto error;
280  }
281 
282  driver_desc = jack_find_driver_descriptor(drivers, master_driver_name);
283  if (!driver_desc) {
284  jack_error("jackdmp: unknown master driver '%s'", master_driver_name);
285  goto error;
286  }
287 
288  if (optind < argc) {
289  driver_nargs = 1 + argc - optind;
290  } else {
291  driver_nargs = 1;
292  }
293 
294  if (driver_nargs == 0) {
295  jack_error("No driver specified ... hmm. JACK won't do"
296  " anything when run like this.");
297  goto error;
298  }
299 
300  master_driver_args = (char**)malloc(sizeof(char*) * driver_nargs);
301  master_driver_args[0] = master_driver_name;
302 
303  for (i = 1; i < driver_nargs; i++) {
304  master_driver_args[i] = argv[optind++];
305  }
306 
307  if (jack_parse_driver_params(driver_desc, driver_nargs, master_driver_args, &master_driver_params)) {
308  goto error;
309  }
310 
311 #ifndef WIN32
312  if (server_name == NULL)
313  server_name = (char*)JackTools::DefaultServerName();
314 #endif
315 
316  rc = jack_register_server(server_name, false);
317  switch (rc) {
318  case EEXIST:
319  jack_error("`%s' server already active", server_name);
320  goto error;
321  case ENOSPC:
322  jack_error("too many servers already active");
323  goto error;
324  case ENOMEM:
325  jack_error("no access to shm registry");
326  goto error;
327  default:
328  jack_info("server `%s' registered", server_name);
329  }
330 
331  /* clean up shared memory and files from any previous instance of this server name */
332  jack_cleanup_shm();
333  JackTools::CleanupFiles(server_name);
334 
335  if (!realtime && client_timeout == 0)
336  client_timeout = 500; /* 0.5 sec; usable when non realtime. */
337 
338  for (i = 0; i < argc; i++) {
339  free(argv[i]);
340  }
341 
342  int res = Start(server_name, driver_desc, master_driver_params, sync, temporary, client_timeout, realtime, realtime_priority, port_max, verbose_aux, clock_source);
343  if (res < 0) {
344  jack_error("Cannot start server... exit");
345  Delete();
346  jack_cleanup_shm();
347  JackTools::CleanupFiles(server_name);
348  jack_unregister_server(server_name);
349  goto error;
350  }
351 
352  // Slave drivers
353  std::map<std::string, JackDriverInfo*>::iterator it1;
354  for (it1 = fSlavesList.begin(); it1 != fSlavesList.end(); it1++) {
355  const char* name = ((*it1).first).c_str();
356  driver_desc = jack_find_driver_descriptor(drivers, name);
357  if (!driver_desc) {
358  jack_error("jackdmp: unknown slave driver '%s'", name);
359  } else {
360  (*it1).second = fInstance->AddSlave(driver_desc, NULL);
361  }
362  }
363 
364  // Loopback driver
365  if (loopback > 0) {
366  driver_desc = jack_find_driver_descriptor(drivers, "loopback");
367  if (!driver_desc) {
368  jack_error("jackdmp: unknown driver '%s'", "loopback");
369  } else {
370  fSlavesList["loopback"] = fInstance->AddSlave(driver_desc, NULL);
371  }
372  }
373 
374  // Internal clients
375  std::map<std::string, int>::iterator it2;
376  for (it2 = fInternalsList.begin(); it2 != fInternalsList.end(); it2++) {
377  int status, refnum;
378  const char* name = ((*it2).first).c_str();
379  fInstance->InternalClientLoad2(name, name, NULL, JackNullOption, &refnum, -1, &status);
380  (*it2).second = refnum;
381  }
382  }
383 
384  if (master_driver_params)
385  jack_free_driver_params(master_driver_params);
386  return true;
387 
388 error:
389  jack_log("JackServerGlobals Init error");
390  if (master_driver_params)
391  jack_free_driver_params(master_driver_params);
392  Destroy();
393  return false;
394 }
395 
396 void JackServerGlobals::Destroy()
397 {
398  if (--fUserCount == 0) {
399  jack_log("JackServerGlobals Destroy");
400  Stop();
401  Delete();
402  jack_cleanup_shm();
403  JackTools::CleanupFiles(server_name);
404  jack_unregister_server(server_name);
405  }
406 }
407 
408 } // end of namespace
409 
410 
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:91
SERVER_EXPORT void jack_info(const char *fmt,...)
Definition: JackError.cpp:99
Definition: getopt.h:84
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:107