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.

429 lines
12KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Internal shared data and functions.
  4. If you edit this file, you should carefully consider changing the
  5. JACK_PROTOCOL_VERSION in configure.in.
  6. Copyright (C) 2001-2003 Paul Davis
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. $Id$
  19. */
  20. #ifndef __jack_internal_h__
  21. #define __jack_internal_h__
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <limits.h>
  25. #include <dlfcn.h>
  26. #include <pthread.h>
  27. #include <sys/types.h>
  28. #include <sys/time.h>
  29. /* Needed by <sysdeps/time.h> */
  30. extern void jack_error (const char *fmt, ...);
  31. #include <jack/jack.h>
  32. #include <jack/types.h>
  33. #include <jack/port.h>
  34. #include <jack/transport.h>
  35. #include <sysdeps/time.h>
  36. #include <sysdeps/atomicity.h>
  37. #ifdef JACK_USE_MACH_THREADS
  38. #include <sysdeps/mach_port.h>
  39. #endif
  40. #ifdef DEBUG_ENABLED
  41. #define DEBUG(format,args...) \
  42. fprintf (stderr, "jack:%5d:%" PRIu64 " %s:%s:%d: " format "\n", getpid(), jack_get_microseconds(), __FILE__, __FUNCTION__, __LINE__ , ## args)
  43. #else
  44. #if JACK_CPP_VARARGS_BROKEN
  45. #define DEBUG(format...)
  46. #else
  47. #define DEBUG(format,args...)
  48. #endif
  49. #endif
  50. /* Enable preemption checking for Linux Realtime Preemption kernels.
  51. *
  52. * This checks if any RT-safe code section does anything to cause CPU
  53. * preemption. Examples are sleep() or other system calls that block.
  54. * If a problem is detected, the kernel writes a syslog entry, and
  55. * sends SIGUSR2 to the client.
  56. */
  57. #ifdef DO_PREEMPTION_CHECKING
  58. #define CHECK_PREEMPTION(engine, onoff) \
  59. if ((engine)->real_time) gettimeofday (1, (onoff))
  60. #else
  61. #define CHECK_PREEMPTION(engine, onoff)
  62. #endif
  63. #ifndef FALSE
  64. #define FALSE (0)
  65. #endif
  66. #ifndef TRUE
  67. #define TRUE (1)
  68. #endif
  69. typedef struct _jack_engine jack_engine_t;
  70. typedef struct _jack_request jack_request_t;
  71. typedef void * dlhandle;
  72. typedef enum {
  73. TransportCommandNone = 0,
  74. TransportCommandStart = 1,
  75. TransportCommandStop = 2,
  76. } transport_command_t;
  77. typedef struct {
  78. volatile jack_time_t guard1;
  79. volatile jack_nframes_t frames;
  80. volatile jack_time_t stamp;
  81. volatile jack_time_t guard2;
  82. } jack_frame_timer_t;
  83. /* JACK engine shared memory data structure. */
  84. typedef struct {
  85. jack_transport_state_t transport_state;
  86. volatile transport_command_t transport_cmd;
  87. transport_command_t previous_cmd; /* previous transport_cmd */
  88. jack_position_t current_time; /* position for current cycle */
  89. jack_position_t pending_time; /* position for next cycle */
  90. jack_position_t request_time; /* latest requested position */
  91. jack_unique_t prev_request; /* previous request unique ID */
  92. volatile _Atomic_word seq_number; /* unique ID sequence number */
  93. int8_t new_pos; /* new position this cycle */
  94. int8_t pending_pos; /* new position request pending */
  95. jack_nframes_t pending_frame; /* pending frame number */
  96. int32_t sync_clients; /* number of active_slowsync clients */
  97. int32_t sync_remain; /* number of them with sync_poll */
  98. jack_time_t sync_timeout;
  99. jack_time_t sync_time_left;
  100. jack_frame_timer_t frame_timer;
  101. int32_t internal;
  102. jack_nframes_t frames_at_cycle_start;
  103. pid_t engine_pid;
  104. jack_nframes_t buffer_size;
  105. int8_t real_time;
  106. int8_t do_mlock;
  107. int8_t do_munlock;
  108. int32_t client_priority;
  109. int32_t has_capabilities;
  110. float cpu_load;
  111. float xrun_delayed_usecs;
  112. uint32_t port_max;
  113. int32_t engine_ok;
  114. jack_port_type_id_t n_port_types;
  115. jack_port_type_info_t port_types[JACK_MAX_PORT_TYPES];
  116. jack_port_shared_t ports[0];
  117. } jack_control_t;
  118. typedef enum {
  119. BufferSizeChange,
  120. SampleRateChange,
  121. AttachPortSegment,
  122. PortConnected,
  123. PortDisconnected,
  124. GraphReordered,
  125. PortRegistered,
  126. PortUnregistered,
  127. XRun,
  128. StartFreewheel,
  129. StopFreewheel
  130. } EventType;
  131. typedef struct {
  132. EventType type;
  133. union {
  134. uint32_t n;
  135. jack_port_id_t port_id;
  136. jack_port_id_t self_id;
  137. } x;
  138. union {
  139. uint32_t n;
  140. jack_port_type_id_t ptid;
  141. jack_port_id_t other_id;
  142. } y;
  143. } jack_event_t;
  144. typedef enum {
  145. ClientInternal, /* connect request just names .so */
  146. ClientDriver, /* code is loaded along with driver */
  147. ClientExternal /* client is in another process */
  148. } ClientType;
  149. typedef enum {
  150. NotTriggered,
  151. Triggered,
  152. Running,
  153. Finished
  154. } jack_client_state_t;
  155. /* JACK client shared memory data structure. */
  156. typedef volatile struct {
  157. volatile jack_client_id_t id; /* w: engine r: engine and client */
  158. volatile jack_nframes_t nframes; /* w: engine r: client */
  159. volatile jack_client_state_t state; /* w: engine and client r: engine */
  160. volatile char name[JACK_CLIENT_NAME_SIZE];
  161. volatile ClientType type; /* w: engine r: engine and client */
  162. volatile int8_t active; /* w: engine r: engine and client */
  163. volatile int8_t dead; /* r/w: engine */
  164. volatile int8_t timed_out; /* r/w: engine */
  165. volatile int8_t is_timebase; /* w: engine, r: engine and client */
  166. volatile int8_t timebase_new; /* w: engine and client, r: engine */
  167. volatile int8_t is_slowsync; /* w: engine, r: engine and client */
  168. volatile int8_t active_slowsync; /* w: engine, r: engine and client */
  169. volatile int8_t sync_poll; /* w: engine and client, r: engine */
  170. volatile int8_t sync_new; /* w: engine and client, r: engine */
  171. volatile pid_t pid; /* w: client r: engine; client pid */
  172. volatile pid_t pgrp; /* w: client r: engine; client pgrp */
  173. volatile uint64_t signalled_at;
  174. volatile uint64_t awake_at;
  175. volatile uint64_t finished_at;
  176. /* JOQ: all these pointers are trouble for 32/64 compatibility,
  177. * they should move to non-shared memory.
  178. */
  179. /* callbacks
  180. */
  181. JackProcessCallback process;
  182. void *process_arg;
  183. JackThreadInitCallback thread_init;
  184. void *thread_init_arg;
  185. JackBufferSizeCallback bufsize;
  186. void *bufsize_arg;
  187. JackSampleRateCallback srate;
  188. void *srate_arg;
  189. JackPortRegistrationCallback port_register;
  190. void *port_register_arg;
  191. JackGraphOrderCallback graph_order;
  192. void *graph_order_arg;
  193. JackXRunCallback xrun;
  194. void *xrun_arg;
  195. JackSyncCallback sync_cb;
  196. void *sync_arg;
  197. JackTimebaseCallback timebase_cb;
  198. void *timebase_arg;
  199. JackFreewheelCallback freewheel_cb;
  200. void *freewheel_arg;
  201. /* external clients: set by libjack
  202. * internal clients: set by engine */
  203. int (*deliver_request)(void*, jack_request_t*); /* JOQ: 64/32 bug! */
  204. void *deliver_arg;
  205. /* for engine use only */
  206. void *private_client;
  207. } jack_client_control_t;
  208. typedef struct {
  209. int32_t load;
  210. ClientType type;
  211. jack_options_t options;
  212. char name[JACK_CLIENT_NAME_SIZE];
  213. char object_path[PATH_MAX+1];
  214. char object_data[1024];
  215. } jack_client_connect_request_t;
  216. typedef struct {
  217. uint32_t protocol_v;
  218. jack_status_t status;
  219. jack_shm_info_t client_shm;
  220. jack_shm_info_t engine_shm;
  221. char fifo_prefix[PATH_MAX+1];
  222. int32_t realtime;
  223. int32_t realtime_priority;
  224. char name[JACK_CLIENT_NAME_SIZE]; /* unique name, if assigned */
  225. /* these two are valid only for internal clients */
  226. jack_client_control_t *client_control;
  227. jack_control_t *engine_control;
  228. #ifdef JACK_USE_MACH_THREADS
  229. /* specific resources for server/client real-time thread communication */
  230. int32_t portnum;
  231. #endif
  232. } jack_client_connect_result_t;
  233. typedef struct {
  234. jack_client_id_t client_id;
  235. } jack_client_connect_ack_request_t;
  236. typedef struct {
  237. int8_t status;
  238. } jack_client_connect_ack_result_t;
  239. typedef enum {
  240. RegisterPort = 1,
  241. UnRegisterPort = 2,
  242. ConnectPorts = 3,
  243. DisconnectPorts = 4,
  244. SetTimeBaseClient = 5,
  245. ActivateClient = 6,
  246. DeactivateClient = 7,
  247. DisconnectPort = 8,
  248. SetClientCapabilities = 9,
  249. GetPortConnections = 10,
  250. GetPortNConnections = 11,
  251. ResetTimeBaseClient = 12,
  252. SetSyncClient = 13,
  253. ResetSyncClient = 14,
  254. SetSyncTimeout = 15,
  255. SetBufferSize = 16,
  256. FreeWheel = 17,
  257. StopFreeWheel = 18,
  258. IntClientHandle = 19,
  259. IntClientLoad = 20,
  260. IntClientName = 21,
  261. IntClientUnload = 22
  262. } RequestType;
  263. struct _jack_request {
  264. RequestType type;
  265. union {
  266. struct {
  267. char name[JACK_PORT_NAME_SIZE];
  268. char type[JACK_PORT_TYPE_SIZE];
  269. uint32_t flags;
  270. jack_shmsize_t buffer_size;
  271. jack_port_id_t port_id;
  272. jack_client_id_t client_id;
  273. } port_info;
  274. struct {
  275. char source_port[JACK_PORT_NAME_SIZE];
  276. char destination_port[JACK_PORT_NAME_SIZE];
  277. } connect;
  278. struct {
  279. int32_t nports;
  280. const char **ports; /* JOQ: 32/64 problem? */
  281. } port_connections;
  282. struct {
  283. jack_client_id_t client_id;
  284. int32_t conditional;
  285. } timebase;
  286. struct {
  287. jack_options_t options;
  288. jack_client_id_t id;
  289. char name[JACK_CLIENT_NAME_SIZE];
  290. char path[PATH_MAX+1];
  291. char init[JACK_LOAD_INIT_LIMIT];
  292. } intclient;
  293. jack_client_id_t client_id;
  294. jack_nframes_t nframes;
  295. jack_time_t timeout;
  296. } x;
  297. int32_t status;
  298. };
  299. /* Per-client structure allocated in the server's address space.
  300. * It's here because its not part of the engine structure.
  301. */
  302. typedef struct _jack_client_internal {
  303. jack_client_control_t *control;
  304. int request_fd;
  305. int event_fd;
  306. int subgraph_start_fd;
  307. int subgraph_wait_fd;
  308. JSList *ports; /* protected by engine->client_lock */
  309. JSList *truefeeds; /* protected by engine->client_lock */
  310. JSList *sortfeeds; /* protected by engine->client_lock */
  311. int fedcount;
  312. int tfedcount;
  313. jack_shm_info_t control_shm;
  314. unsigned long execution_order;
  315. struct _jack_client_internal *next_client; /* not a linked list! */
  316. dlhandle handle;
  317. int (*initialize)(jack_client_t*, const char*); /* int. clients only */
  318. void (*finish)(void *); /* internal clients only */
  319. int error;
  320. #ifdef JACK_USE_MACH_THREADS
  321. /* specific resources for server/client real-time thread communication */
  322. mach_port_t serverport;
  323. trivial_message message;
  324. int running;
  325. int portnum;
  326. #endif /* JACK_USE_MACH_THREADS */
  327. } jack_client_internal_t;
  328. extern int jack_client_handle_port_connection (jack_client_t *client,
  329. jack_event_t *event);
  330. extern jack_client_t *jack_driver_client_new (jack_engine_t *,
  331. const char *client_name);
  332. extern jack_client_t *jack_client_alloc_internal (jack_client_control_t*,
  333. jack_engine_t*);
  334. /* internal clients call this. it's defined in jack/engine.c */
  335. void handle_internal_client_request (jack_control_t*, jack_request_t*);
  336. extern char *jack_tmpdir;
  337. extern char *jack_user_dir (void);
  338. extern char *jack_server_dir (const char *server_name);
  339. extern void *jack_zero_filled_buffer;
  340. extern jack_port_functions_t jack_builtin_audio_functions;
  341. extern jack_port_type_info_t jack_builtin_port_types[];
  342. extern void jack_client_invalidate_port_buffers (jack_client_t *client);
  343. extern void jack_transport_copy_position (jack_position_t *from,
  344. jack_position_t *to);
  345. extern void jack_call_sync_client (jack_client_t *client);
  346. extern void jack_call_timebase_master (jack_client_t *client);
  347. extern char *jack_default_server_name (void);
  348. void silent_jack_error_callback (const char *desc);
  349. /* needed for port management */
  350. jack_port_t *jack_port_by_id_int (const jack_client_t *client,
  351. jack_port_id_t id, int* free);
  352. jack_port_t *jack_port_by_name_int (jack_client_t *client,
  353. const char *port_name);
  354. #endif /* __jack_internal_h__ */