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.

483 lines
14KB

  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. extern void jack_info (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. ClientRegistered,
  146. ClientUnregistered
  147. } JackEventType;
  148. typedef struct {
  149. JackEventType type;
  150. union {
  151. uint32_t n;
  152. char name[JACK_CLIENT_NAME_SIZE];
  153. jack_port_id_t port_id;
  154. jack_port_id_t self_id;
  155. } x;
  156. union {
  157. uint32_t n;
  158. jack_port_type_id_t ptid;
  159. jack_port_id_t other_id;
  160. } y;
  161. } jack_event_t;
  162. typedef enum {
  163. ClientInternal, /* connect request just names .so */
  164. ClientDriver, /* code is loaded along with driver */
  165. ClientExternal /* client is in another process */
  166. } ClientType;
  167. typedef enum {
  168. NotTriggered,
  169. Triggered,
  170. Running,
  171. Finished
  172. } jack_client_state_t;
  173. /* JACK client shared memory data structure. */
  174. typedef volatile struct {
  175. volatile jack_client_id_t id; /* w: engine r: engine and client */
  176. volatile jack_nframes_t nframes; /* w: engine r: client */
  177. volatile jack_client_state_t state; /* w: engine and client r: engine */
  178. volatile char name[JACK_CLIENT_NAME_SIZE];
  179. volatile ClientType type; /* w: engine r: engine and client */
  180. volatile int8_t active; /* w: engine r: engine and client */
  181. volatile int8_t dead; /* r/w: engine */
  182. volatile int8_t timed_out; /* r/w: engine */
  183. volatile int8_t is_timebase; /* w: engine, r: engine and client */
  184. volatile int8_t timebase_new; /* w: engine and client, r: engine */
  185. volatile int8_t is_slowsync; /* w: engine, r: engine and client */
  186. volatile int8_t active_slowsync; /* w: engine, r: engine and client */
  187. volatile int8_t sync_poll; /* w: engine and client, r: engine */
  188. volatile int8_t sync_new; /* w: engine and client, r: engine */
  189. volatile pid_t pid; /* w: client r: engine; client pid */
  190. volatile pid_t pgrp; /* w: client r: engine; client pgrp */
  191. volatile uint64_t signalled_at;
  192. volatile uint64_t awake_at;
  193. volatile uint64_t finished_at;
  194. volatile int32_t last_status; /* w: client, r: engine and client */
  195. /* JOQ: all these pointers are trouble for 32/64 compatibility,
  196. * they should move to non-shared memory.
  197. */
  198. /* callbacks
  199. */
  200. JackProcessCallback process;
  201. void *process_arg;
  202. JackThreadInitCallback thread_init;
  203. void *thread_init_arg;
  204. JackBufferSizeCallback bufsize;
  205. void *bufsize_arg;
  206. JackSampleRateCallback srate;
  207. void *srate_arg;
  208. JackPortRegistrationCallback port_register;
  209. void *port_register_arg;
  210. JackPortConnectCallback port_connect;
  211. void *port_connect_arg;
  212. JackGraphOrderCallback graph_order;
  213. void *graph_order_arg;
  214. JackXRunCallback xrun;
  215. void *xrun_arg;
  216. JackSyncCallback sync_cb;
  217. void *sync_arg;
  218. JackTimebaseCallback timebase_cb;
  219. void *timebase_arg;
  220. JackFreewheelCallback freewheel_cb;
  221. void *freewheel_arg;
  222. JackClientRegistrationCallback client_register;
  223. void *client_register_arg;
  224. JackThreadCallback thread_cb;
  225. void *thread_cb_arg;
  226. /* external clients: set by libjack
  227. * internal clients: set by engine */
  228. int (*deliver_request)(void*, jack_request_t*); /* JOQ: 64/32 bug! */
  229. void *deliver_arg;
  230. /* for engine use only */
  231. void *private_client;
  232. } jack_client_control_t;
  233. typedef struct {
  234. uint32_t protocol_v; /* protocol version, must go first */
  235. int32_t load;
  236. ClientType type;
  237. jack_options_t options;
  238. char name[JACK_CLIENT_NAME_SIZE];
  239. char object_path[PATH_MAX+1];
  240. char object_data[1024];
  241. } jack_client_connect_request_t;
  242. typedef struct {
  243. jack_status_t status;
  244. jack_shm_info_t client_shm;
  245. jack_shm_info_t engine_shm;
  246. char fifo_prefix[PATH_MAX+1];
  247. int32_t realtime;
  248. int32_t realtime_priority;
  249. char name[JACK_CLIENT_NAME_SIZE]; /* unique name, if assigned */
  250. /* these two are valid only for internal clients */
  251. jack_client_control_t *client_control;
  252. jack_control_t *engine_control;
  253. #ifdef JACK_USE_MACH_THREADS
  254. /* specific resources for server/client real-time thread communication */
  255. int32_t portnum;
  256. #endif
  257. } jack_client_connect_result_t;
  258. typedef struct {
  259. jack_client_id_t client_id;
  260. } jack_client_connect_ack_request_t;
  261. typedef struct {
  262. int8_t status;
  263. } jack_client_connect_ack_result_t;
  264. typedef enum {
  265. RegisterPort = 1,
  266. UnRegisterPort = 2,
  267. ConnectPorts = 3,
  268. DisconnectPorts = 4,
  269. SetTimeBaseClient = 5,
  270. ActivateClient = 6,
  271. DeactivateClient = 7,
  272. DisconnectPort = 8,
  273. SetClientCapabilities = 9,
  274. GetPortConnections = 10,
  275. GetPortNConnections = 11,
  276. ResetTimeBaseClient = 12,
  277. SetSyncClient = 13,
  278. ResetSyncClient = 14,
  279. SetSyncTimeout = 15,
  280. SetBufferSize = 16,
  281. FreeWheel = 17,
  282. StopFreeWheel = 18,
  283. IntClientHandle = 19,
  284. IntClientLoad = 20,
  285. IntClientName = 21,
  286. IntClientUnload = 22,
  287. RecomputeTotalLatencies = 23,
  288. RecomputeTotalLatency = 24
  289. } RequestType;
  290. struct _jack_request {
  291. RequestType type;
  292. union {
  293. struct {
  294. char name[JACK_PORT_NAME_SIZE];
  295. char type[JACK_PORT_TYPE_SIZE];
  296. uint32_t flags;
  297. jack_shmsize_t buffer_size;
  298. jack_port_id_t port_id;
  299. jack_client_id_t client_id;
  300. } port_info;
  301. struct {
  302. char source_port[JACK_PORT_NAME_SIZE];
  303. char destination_port[JACK_PORT_NAME_SIZE];
  304. } connect;
  305. struct {
  306. int32_t nports;
  307. const char **ports; /* JOQ: 32/64 problem? */
  308. } port_connections;
  309. struct {
  310. jack_client_id_t client_id;
  311. int32_t conditional;
  312. } timebase;
  313. struct {
  314. jack_options_t options;
  315. jack_client_id_t id;
  316. char name[JACK_CLIENT_NAME_SIZE];
  317. char path[PATH_MAX+1];
  318. char init[JACK_LOAD_INIT_LIMIT];
  319. } intclient;
  320. jack_client_id_t client_id;
  321. jack_nframes_t nframes;
  322. jack_time_t timeout;
  323. pid_t cap_pid;
  324. } x;
  325. int32_t status;
  326. };
  327. /* Per-client structure allocated in the server's address space.
  328. * It's here because its not part of the engine structure.
  329. */
  330. typedef struct _jack_client_internal {
  331. jack_client_control_t *control;
  332. int request_fd;
  333. int event_fd;
  334. int subgraph_start_fd;
  335. int subgraph_wait_fd;
  336. JSList *ports; /* protected by engine->client_lock */
  337. JSList *truefeeds; /* protected by engine->client_lock */
  338. JSList *sortfeeds; /* protected by engine->client_lock */
  339. int fedcount;
  340. int tfedcount;
  341. jack_shm_info_t control_shm;
  342. unsigned long execution_order;
  343. struct _jack_client_internal *next_client; /* not a linked list! */
  344. dlhandle handle;
  345. int (*initialize)(jack_client_t*, const char*); /* int. clients only */
  346. void (*finish)(void *); /* internal clients only */
  347. int error;
  348. #ifdef JACK_USE_MACH_THREADS
  349. /* specific resources for server/client real-time thread communication */
  350. mach_port_t serverport;
  351. trivial_message message;
  352. int running;
  353. int portnum;
  354. #endif /* JACK_USE_MACH_THREADS */
  355. } jack_client_internal_t;
  356. typedef struct _jack_thread_arg {
  357. jack_client_t* client;
  358. void* (*work_function)(void*);
  359. int priority;
  360. int realtime;
  361. void* arg;
  362. pid_t cap_pid;
  363. } jack_thread_arg_t;
  364. extern int jack_client_handle_port_connection (jack_client_t *client,
  365. jack_event_t *event);
  366. extern jack_client_t *jack_driver_client_new (jack_engine_t *,
  367. const char *client_name);
  368. extern jack_client_t *jack_client_alloc_internal (jack_client_control_t*,
  369. jack_engine_t*);
  370. /* internal clients call this. it's defined in jack/engine.c */
  371. void handle_internal_client_request (jack_control_t*, jack_request_t*);
  372. extern char *jack_tmpdir;
  373. extern char *jack_user_dir (void);
  374. extern char *jack_server_dir (const char *server_name, char *server_dir);
  375. extern void *jack_zero_filled_buffer;
  376. extern jack_port_functions_t jack_builtin_audio_functions;
  377. extern jack_port_type_info_t jack_builtin_port_types[];
  378. extern void jack_client_invalidate_port_buffers (jack_client_t *client);
  379. extern void jack_transport_copy_position (jack_position_t *from,
  380. jack_position_t *to);
  381. extern void jack_call_sync_client (jack_client_t *client);
  382. extern void jack_call_timebase_master (jack_client_t *client);
  383. extern char *jack_default_server_name (void);
  384. void silent_jack_error_callback (const char *desc);
  385. /* needed for port management */
  386. jack_port_t *jack_port_by_id_int (const jack_client_t *client,
  387. jack_port_id_t id, int* free);
  388. jack_port_t *jack_port_by_name_int (jack_client_t *client,
  389. const char *port_name);
  390. int jack_port_name_equals (jack_port_shared_t* port, const char* target);
  391. #ifdef __GNUC__
  392. # define likely(x) __builtin_expect((x),1)
  393. # define unlikely(x) __builtin_expect((x),0)
  394. #else
  395. # define likely(x) (x)
  396. # define unlikely(x) (x)
  397. #endif
  398. #endif /* __jack_internal_h__ */