jack1 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
3.4KB

  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include <getopt.h>
  4. #include <jack/engine.h>
  5. #include <jack/internal.h>
  6. #include <jack/driver.h>
  7. static sigset_t signals;
  8. static jack_engine_t *engine;
  9. static void *
  10. signal_thread (void *arg)
  11. {
  12. int sig;
  13. int err;
  14. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  15. err = sigwait (&signals, &sig);
  16. fprintf (stderr, "exiting due to signal %d\n", sig);
  17. jack_engine_delete (engine);
  18. exit (err);
  19. /*NOTREACHED*/
  20. return 0;
  21. }
  22. int
  23. catch_signals (void)
  24. {
  25. pthread_t thread_id;
  26. sigemptyset (&signals);
  27. sigaddset(&signals, SIGHUP);
  28. sigaddset(&signals, SIGINT);
  29. sigaddset(&signals, SIGQUIT);
  30. sigaddset(&signals, SIGILL);
  31. sigaddset(&signals, SIGTRAP);
  32. sigaddset(&signals, SIGABRT);
  33. sigaddset(&signals, SIGIOT);
  34. sigaddset(&signals, SIGFPE);
  35. sigaddset(&signals, SIGKILL);
  36. sigaddset(&signals, SIGPIPE);
  37. sigaddset(&signals, SIGTERM);
  38. sigaddset(&signals, SIGCHLD);
  39. sigaddset(&signals, SIGCONT);
  40. sigaddset(&signals, SIGSTOP);
  41. sigaddset(&signals, SIGTSTP);
  42. sigaddset(&signals, SIGTTIN);
  43. sigaddset(&signals, SIGTTOU);
  44. /* this can make debugging a pain, but it also makes
  45. segv-exits cleanup after themselves rather than
  46. leaving the audio thread active. i still
  47. find it truly wierd that _exit() or whatever is done
  48. by the default SIGSEGV handler does not
  49. cancel all threads in a process, but what
  50. else can we do?
  51. */
  52. sigaddset(&signals, SIGSEGV);
  53. /* all child threads will inherit this mask */
  54. pthread_sigmask (SIG_BLOCK, &signals, 0);
  55. /* start a thread to wait for signals */
  56. if (pthread_create (&thread_id, 0, signal_thread, 0)) {
  57. fprintf (stderr, "cannot create signal catching thread");
  58. return -1;
  59. }
  60. pthread_detach (thread_id);
  61. return 0;
  62. }
  63. static char *alsa_pcm_name = "default";
  64. static nframes_t frames_per_interrupt = 64;
  65. static nframes_t srate = 48000;
  66. static int realtime = 0;
  67. static int realtime_priority = 10;
  68. static void usage ()
  69. {
  70. fprintf (stderr, "usage: engine [ -d ALSA PCM device ] [ -r sample-rate ] [ -p frames_per_interrupt ] [ -R [ -P priority ] ]\n");
  71. }
  72. int
  73. main (int argc, char *argv[])
  74. {
  75. jack_driver_t *driver;
  76. const char *options = "hd:r:p:RP:";
  77. struct option long_options[] =
  78. {
  79. { "device", 1, 0, 'd' },
  80. { "srate", 1, 0, 'r' },
  81. { "frames-per-interrupt", 1, 0, 'p' },
  82. { "help", 0, 0, 'h' },
  83. { "realtime", 0, 0, 'R' },
  84. { "realtime-priority", 1, 0, 'P' },
  85. { 0, 0, 0, 0 }
  86. };
  87. int option_index;
  88. int opt;
  89. catch_signals ();
  90. opterr = 0;
  91. while ((opt = getopt_long (argc, argv, options, long_options, &option_index)) != EOF) {
  92. switch (opt) {
  93. case 'd':
  94. alsa_pcm_name = optarg;
  95. break;
  96. case 'r':
  97. srate = atoi (optarg);
  98. break;
  99. case 'p':
  100. frames_per_interrupt = atoi (optarg);
  101. break;
  102. case 'R':
  103. realtime = 1;
  104. break;
  105. case 'P':
  106. realtime_priority = atoi (optarg);
  107. break;
  108. case 'h':
  109. default:
  110. fprintf (stderr, "unknown option character %c\n", opt);
  111. usage();
  112. return -1;
  113. }
  114. }
  115. if ((engine = jack_engine_new (realtime, realtime_priority)) == 0) {
  116. fprintf (stderr, "cannot create engine\n");
  117. return 1;
  118. }
  119. if ((driver = jack_driver_load (ADDON_DIR "/jack_alsa.so", alsa_pcm_name, frames_per_interrupt, srate)) == 0) {
  120. fprintf (stderr, "cannot load ALSA driver module\n");
  121. return 1;
  122. }
  123. jack_use_driver (engine, driver);
  124. printf ("start engine ...\n");
  125. jack_run (engine);
  126. jack_wait (engine);
  127. return 0;
  128. }