JACK API headers
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.

273 lines
9.2KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Copyright (C) 2001-2003 Paul Davis
  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. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #ifndef __jack_engine_h__
  17. #define __jack_engine_h__
  18. #include <jack/jack.h>
  19. #include <jack/internal.h>
  20. #include <jack/driver_interface.h>
  21. struct _jack_driver;
  22. struct _jack_client_internal;
  23. struct _jack_port_internal;
  24. /* Structures is allocated by the engine in local memory to keep track
  25. * of port buffers and connections.
  26. */
  27. typedef struct {
  28. jack_shm_info_t* shm_info;
  29. jack_shmsize_t offset;
  30. } jack_port_buffer_info_t;
  31. /* The engine keeps an array of these in its local memory. */
  32. typedef struct _jack_port_internal {
  33. struct _jack_port_shared *shared;
  34. JSList *connections;
  35. jack_port_buffer_info_t *buffer_info;
  36. } jack_port_internal_t;
  37. /* The engine's internal port type structure. */
  38. typedef struct _jack_port_buffer_list {
  39. pthread_mutex_t lock; /* only lock within server */
  40. JSList *freelist; /* list of free buffers */
  41. jack_port_buffer_info_t *info; /* jack_buffer_info_t array */
  42. } jack_port_buffer_list_t;
  43. typedef struct _jack_reserved_name {
  44. jack_client_id_t uuid;
  45. char name[JACK_CLIENT_NAME_SIZE];
  46. } jack_reserved_name_t;
  47. #define JACKD_WATCHDOG_TIMEOUT 10000
  48. #define JACKD_CLIENT_EVENT_TIMEOUT 2000
  49. /* The main engine structure in local memory. */
  50. struct _jack_engine {
  51. jack_control_t *control;
  52. JSList *drivers;
  53. struct _jack_driver *driver;
  54. jack_driver_desc_t *driver_desc;
  55. JSList *driver_params;
  56. JSList *slave_drivers;
  57. /* these are "callbacks" made by the driver backend */
  58. int (*set_buffer_size) (struct _jack_engine *, jack_nframes_t frames);
  59. int (*set_sample_rate) (struct _jack_engine *, jack_nframes_t frames);
  60. int (*run_cycle) (struct _jack_engine *, jack_nframes_t nframes,
  61. float delayed_usecs);
  62. void (*delay) (struct _jack_engine *, float delayed_usecs);
  63. void (*transport_cycle_start) (struct _jack_engine *, jack_time_t time);
  64. void (*driver_exit) (struct _jack_engine *);
  65. jack_time_t (*get_microseconds)(void);
  66. /* "private" sections starts here */
  67. /* engine serialization -- use precedence for deadlock avoidance */
  68. pthread_mutex_t request_lock; /* precedes client_lock */
  69. pthread_rwlock_t client_lock;
  70. pthread_mutex_t port_lock;
  71. pthread_mutex_t problem_lock; /* must hold write lock on client_lock */
  72. int process_errors;
  73. int period_msecs;
  74. /* Time to wait for clients in msecs. Used when jackd is run
  75. * without realtime priority enabled. */
  76. int client_timeout_msecs;
  77. /* info on the shm segment containing this->control */
  78. jack_shm_info_t control_shm;
  79. /* address-space local port buffer and segment info,
  80. indexed by the port type_id
  81. */
  82. jack_port_buffer_list_t port_buffers[JACK_MAX_PORT_TYPES];
  83. jack_shm_info_t port_segment[JACK_MAX_PORT_TYPES];
  84. unsigned int port_max;
  85. pthread_t server_thread;
  86. pthread_t watchdog_thread;
  87. int fds[2];
  88. int cleanup_fifo[2];
  89. jack_client_id_t next_client_id;
  90. size_t pfd_size;
  91. size_t pfd_max;
  92. struct pollfd *pfd;
  93. char fifo_prefix[PATH_MAX+1];
  94. int *fifo;
  95. unsigned long fifo_size;
  96. /* session handling */
  97. int session_reply_fd;
  98. int session_pending_replies;
  99. unsigned long external_client_cnt;
  100. int rtpriority;
  101. volatile char freewheeling;
  102. volatile char stop_freewheeling;
  103. jack_client_id_t fwclient;
  104. pthread_t freewheel_thread;
  105. char verbose;
  106. char do_munlock;
  107. const char *server_name;
  108. char temporary;
  109. int reordered;
  110. int watchdog_check;
  111. int feedbackcount;
  112. int removing_clients;
  113. pid_t wait_pid;
  114. int nozombies;
  115. int timeout_count_threshold;
  116. volatile int problems;
  117. volatile int timeout_count;
  118. volatile int new_clients_allowed;
  119. /* these lists are protected by `client_lock' */
  120. JSList *clients;
  121. JSList *clients_waiting;
  122. JSList *reserved_client_names;
  123. jack_port_internal_t *internal_ports;
  124. jack_client_internal_t *timebase_client;
  125. jack_port_buffer_info_t *silent_buffer;
  126. jack_client_internal_t *current_client;
  127. #define JACK_ENGINE_ROLLING_COUNT 32
  128. #define JACK_ENGINE_ROLLING_INTERVAL 1024
  129. jack_time_t rolling_client_usecs[JACK_ENGINE_ROLLING_COUNT];
  130. int rolling_client_usecs_cnt;
  131. int rolling_client_usecs_index;
  132. int rolling_interval;
  133. float max_usecs;
  134. float spare_usecs;
  135. int first_wakeup;
  136. #ifdef JACK_USE_MACH_THREADS
  137. /* specific resources for server/client real-time thread communication */
  138. mach_port_t servertask, bp;
  139. int portnum;
  140. #endif
  141. /* used for port names munging */
  142. int audio_out_cnt;
  143. int audio_in_cnt;
  144. int midi_out_cnt;
  145. int midi_in_cnt;
  146. };
  147. /* public functions */
  148. jack_engine_t *jack_engine_new (int real_time, int real_time_priority,
  149. int do_mlock, int do_unlock,
  150. const char *server_name, int temporary,
  151. int verbose, int client_timeout,
  152. unsigned int port_max,
  153. pid_t waitpid, jack_nframes_t frame_time_offset, int nozombies,
  154. int timeout_count_threshold,
  155. JSList *drivers);
  156. void jack_engine_delete (jack_engine_t *);
  157. int jack_run (jack_engine_t *engine);
  158. int jack_wait (jack_engine_t *engine);
  159. int jack_engine_load_driver (jack_engine_t *engine,
  160. jack_driver_desc_t * driver_desc,
  161. JSList * driver_params);
  162. int jack_engine_load_slave_driver (jack_engine_t *engine,
  163. jack_driver_desc_t * driver_desc,
  164. JSList * driver_params);
  165. void jack_dump_configuration(jack_engine_t *engine, int take_lock);
  166. /* private engine functions */
  167. void jack_engine_reset_rolling_usecs (jack_engine_t *engine);
  168. int internal_client_request (void* ptr, jack_request_t *request);
  169. int jack_get_fifo_fd (jack_engine_t *engine,
  170. unsigned int which_fifo);
  171. extern jack_timer_type_t clock_source;
  172. extern jack_client_internal_t *
  173. jack_client_internal_by_id (jack_engine_t *engine, jack_client_id_t id);
  174. #define jack_rdlock_graph(e) { DEBUG ("acquiring graph read lock"); if (pthread_rwlock_rdlock (&e->client_lock)) abort(); }
  175. #define jack_lock_graph(e) { DEBUG ("acquiring graph write lock"); if (pthread_rwlock_wrlock (&e->client_lock)) abort(); }
  176. #define jack_try_rdlock_graph(e) pthread_rwlock_tryrdlock (&e->client_lock)
  177. #define jack_unlock_graph(e) { DEBUG ("release graph lock"); if (pthread_rwlock_unlock (&e->client_lock)) abort(); }
  178. #define jack_trylock_problems(e) pthread_mutex_trylock (&e->problem_lock)
  179. #define jack_lock_problems(e) { DEBUG ("acquiring problem lock"); if (pthread_mutex_lock (&e->problem_lock)) abort(); }
  180. #define jack_unlock_problems(e) { DEBUG ("release problem lock"); if (pthread_mutex_unlock (&e->problem_lock)) abort(); }
  181. #if 0
  182. static inline void jack_rdlock_graph (jack_engine_t* engine) {
  183. DEBUG ("acquiring graph read lock");
  184. pthread_rwlock_rdlock (&engine->client_lock);
  185. }
  186. static inline void jack_lock_graph (jack_engine_t* engine) {
  187. DEBUG ("acquiring graph lock");
  188. pthread_rwlock_wrlock (&engine->client_lock);
  189. }
  190. static inline int jack_try_rdlock_graph (jack_engine_t *engine)
  191. {
  192. DEBUG ("TRYING to acquiring graph read lock");
  193. return pthread_rwlock_tryrdlock (&engine->client_lock);
  194. }
  195. static inline void jack_unlock_graph (jack_engine_t* engine)
  196. {
  197. DEBUG ("releasing graph lock");
  198. pthread_rwlock_unlock (&engine->client_lock);
  199. }
  200. #endif
  201. static inline unsigned int jack_power_of_two (unsigned int n)
  202. {
  203. return !(n & (n - 1));
  204. }
  205. /* Internal port handling interfaces for JACK engine. */
  206. void jack_port_clear_connections (jack_engine_t *engine,
  207. jack_port_internal_t *port);
  208. void jack_port_registration_notify (jack_engine_t *, jack_port_id_t, int);
  209. void jack_port_release (jack_engine_t *engine, jack_port_internal_t *);
  210. void jack_sort_graph (jack_engine_t *engine);
  211. int jack_stop_freewheeling (jack_engine_t* engine, int engine_exiting);
  212. jack_client_internal_t *
  213. jack_client_by_name (jack_engine_t *engine, const char *name);
  214. int jack_deliver_event (jack_engine_t *, jack_client_internal_t *, jack_event_t *);
  215. void jack_stop_watchdog (jack_engine_t * );
  216. void
  217. jack_engine_signal_problems (jack_engine_t* engine);
  218. int
  219. jack_use_driver (jack_engine_t *engine, struct _jack_driver *driver);
  220. int
  221. jack_drivers_start (jack_engine_t *engine);
  222. int
  223. jack_add_slave_driver (jack_engine_t *engine, struct _jack_driver *driver);
  224. #endif /* __jack_engine_h__ */