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.

481 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. /* external clients: set by libjack
  225. * internal clients: set by engine */
  226. int (*deliver_request)(void*, jack_request_t*); /* JOQ: 64/32 bug! */
  227. void *deliver_arg;
  228. /* for engine use only */
  229. void *private_client;
  230. } jack_client_control_t;
  231. typedef struct {
  232. uint32_t protocol_v; /* protocol version, must go first */
  233. int32_t load;
  234. ClientType type;
  235. jack_options_t options;
  236. char name[JACK_CLIENT_NAME_SIZE];
  237. char object_path[PATH_MAX+1];
  238. char object_data[1024];
  239. } jack_client_connect_request_t;
  240. typedef struct {
  241. jack_status_t status;
  242. jack_shm_info_t client_shm;
  243. jack_shm_info_t engine_shm;
  244. char fifo_prefix[PATH_MAX+1];
  245. int32_t realtime;
  246. int32_t realtime_priority;
  247. char name[JACK_CLIENT_NAME_SIZE]; /* unique name, if assigned */
  248. /* these two are valid only for internal clients */
  249. jack_client_control_t *client_control;
  250. jack_control_t *engine_control;
  251. #ifdef JACK_USE_MACH_THREADS
  252. /* specific resources for server/client real-time thread communication */
  253. int32_t portnum;
  254. #endif
  255. } jack_client_connect_result_t;
  256. typedef struct {
  257. jack_client_id_t client_id;
  258. } jack_client_connect_ack_request_t;
  259. typedef struct {
  260. int8_t status;
  261. } jack_client_connect_ack_result_t;
  262. typedef enum {
  263. RegisterPort = 1,
  264. UnRegisterPort = 2,
  265. ConnectPorts = 3,
  266. DisconnectPorts = 4,
  267. SetTimeBaseClient = 5,
  268. ActivateClient = 6,
  269. DeactivateClient = 7,
  270. DisconnectPort = 8,
  271. SetClientCapabilities = 9,
  272. GetPortConnections = 10,
  273. GetPortNConnections = 11,
  274. ResetTimeBaseClient = 12,
  275. SetSyncClient = 13,
  276. ResetSyncClient = 14,
  277. SetSyncTimeout = 15,
  278. SetBufferSize = 16,
  279. FreeWheel = 17,
  280. StopFreeWheel = 18,
  281. IntClientHandle = 19,
  282. IntClientLoad = 20,
  283. IntClientName = 21,
  284. IntClientUnload = 22,
  285. RecomputeTotalLatencies = 23,
  286. RecomputeTotalLatency = 24
  287. } RequestType;
  288. struct _jack_request {
  289. RequestType type;
  290. union {
  291. struct {
  292. char name[JACK_PORT_NAME_SIZE];
  293. char type[JACK_PORT_TYPE_SIZE];
  294. uint32_t flags;
  295. jack_shmsize_t buffer_size;
  296. jack_port_id_t port_id;
  297. jack_client_id_t client_id;
  298. } port_info;
  299. struct {
  300. char source_port[JACK_PORT_NAME_SIZE];
  301. char destination_port[JACK_PORT_NAME_SIZE];
  302. } connect;
  303. struct {
  304. int32_t nports;
  305. const char **ports; /* JOQ: 32/64 problem? */
  306. } port_connections;
  307. struct {
  308. jack_client_id_t client_id;
  309. int32_t conditional;
  310. } timebase;
  311. struct {
  312. jack_options_t options;
  313. jack_client_id_t id;
  314. char name[JACK_CLIENT_NAME_SIZE];
  315. char path[PATH_MAX+1];
  316. char init[JACK_LOAD_INIT_LIMIT];
  317. } intclient;
  318. jack_client_id_t client_id;
  319. jack_nframes_t nframes;
  320. jack_time_t timeout;
  321. pid_t cap_pid;
  322. } x;
  323. int32_t status;
  324. };
  325. /* Per-client structure allocated in the server's address space.
  326. * It's here because its not part of the engine structure.
  327. */
  328. typedef struct _jack_client_internal {
  329. jack_client_control_t *control;
  330. int request_fd;
  331. int event_fd;
  332. int subgraph_start_fd;
  333. int subgraph_wait_fd;
  334. JSList *ports; /* protected by engine->client_lock */
  335. JSList *truefeeds; /* protected by engine->client_lock */
  336. JSList *sortfeeds; /* protected by engine->client_lock */
  337. int fedcount;
  338. int tfedcount;
  339. jack_shm_info_t control_shm;
  340. unsigned long execution_order;
  341. struct _jack_client_internal *next_client; /* not a linked list! */
  342. dlhandle handle;
  343. int (*initialize)(jack_client_t*, const char*); /* int. clients only */
  344. void (*finish)(void *); /* internal clients only */
  345. int error;
  346. #ifdef JACK_USE_MACH_THREADS
  347. /* specific resources for server/client real-time thread communication */
  348. mach_port_t serverport;
  349. trivial_message message;
  350. int running;
  351. int portnum;
  352. #endif /* JACK_USE_MACH_THREADS */
  353. } jack_client_internal_t;
  354. typedef struct _jack_thread_arg {
  355. jack_client_t* client;
  356. void* (*work_function)(void*);
  357. int priority;
  358. int realtime;
  359. void* arg;
  360. pid_t cap_pid;
  361. } jack_thread_arg_t;
  362. extern int jack_client_handle_port_connection (jack_client_t *client,
  363. jack_event_t *event);
  364. extern jack_client_t *jack_driver_client_new (jack_engine_t *,
  365. const char *client_name);
  366. extern jack_client_t *jack_client_alloc_internal (jack_client_control_t*,
  367. jack_engine_t*);
  368. /* internal clients call this. it's defined in jack/engine.c */
  369. void handle_internal_client_request (jack_control_t*, jack_request_t*);
  370. extern char *jack_tmpdir;
  371. extern char *jack_user_dir (void);
  372. extern char *jack_server_dir (const char *server_name, char *server_dir);
  373. extern void *jack_zero_filled_buffer;
  374. extern jack_port_functions_t jack_builtin_audio_functions;
  375. extern jack_port_type_info_t jack_builtin_port_types[];
  376. extern void jack_client_invalidate_port_buffers (jack_client_t *client);
  377. extern void jack_transport_copy_position (jack_position_t *from,
  378. jack_position_t *to);
  379. extern void jack_call_sync_client (jack_client_t *client);
  380. extern void jack_call_timebase_master (jack_client_t *client);
  381. extern char *jack_default_server_name (void);
  382. void silent_jack_error_callback (const char *desc);
  383. /* needed for port management */
  384. jack_port_t *jack_port_by_id_int (const jack_client_t *client,
  385. jack_port_id_t id, int* free);
  386. jack_port_t *jack_port_by_name_int (jack_client_t *client,
  387. const char *port_name);
  388. int jack_port_name_equals (jack_port_shared_t* port, const char* target);
  389. #ifdef __GNUC__
  390. # define likely(x) __builtin_expect((x),1)
  391. # define unlikely(x) __builtin_expect((x),0)
  392. #else
  393. # define likely(x) (x)
  394. # define unlikely(x) (x)
  395. #endif
  396. #endif /* __jack_internal_h__ */