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.

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