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.

470 lines
13KB

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