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