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.

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