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.

394 lines
11KB

  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. #include <sysdeps/time.h>
  36. #include <sysdeps/atomicity.h>
  37. #ifdef JACK_USE_MACH_THREADS
  38. #include <sysdeps/mach_port.h>
  39. #endif
  40. #ifdef DEBUG_ENABLED
  41. #define DEBUG(format,args...) \
  42. fprintf (stderr, "jack:%5d:%" PRIu64 " %s:%s:%d: " format "\n", getpid(), jack_get_microseconds(), __FILE__, __FUNCTION__, __LINE__ , ## args)
  43. #else
  44. #if JACK_CPP_VARARGS_BROKEN
  45. #define DEBUG(format...)
  46. #else
  47. #define DEBUG(format,args...)
  48. #endif
  49. #endif
  50. #ifndef FALSE
  51. #define FALSE (0)
  52. #endif
  53. #ifndef TRUE
  54. #define TRUE (1)
  55. #endif
  56. typedef struct _jack_engine jack_engine_t;
  57. typedef struct _jack_request jack_request_t;
  58. typedef void * dlhandle;
  59. typedef enum {
  60. TransportCommandNone = 0,
  61. TransportCommandStart = 1,
  62. TransportCommandStop = 2,
  63. } transport_command_t;
  64. typedef struct {
  65. volatile jack_time_t guard1;
  66. volatile jack_nframes_t frames;
  67. volatile jack_time_t stamp;
  68. volatile jack_time_t guard2;
  69. } jack_frame_timer_t;
  70. /* JACK engine shared memory data structure. */
  71. typedef struct {
  72. jack_transport_state_t transport_state;
  73. volatile transport_command_t transport_cmd;
  74. transport_command_t previous_cmd; /* previous transport_cmd */
  75. jack_position_t current_time; /* position for current cycle */
  76. jack_position_t pending_time; /* position for next cycle */
  77. jack_position_t request_time; /* latest requested position */
  78. jack_unique_t prev_request; /* previous request unique ID */
  79. volatile _Atomic_word seq_number; /* unique ID sequence number */
  80. int8_t new_pos; /* new position this cycle */
  81. int8_t pending_pos; /* new position request pending */
  82. jack_nframes_t pending_frame; /* pending frame number */
  83. int32_t sync_clients; /* number of active_slowsync clients */
  84. int32_t sync_remain; /* number of them with sync_poll */
  85. jack_time_t sync_timeout;
  86. jack_time_t sync_time_left;
  87. jack_frame_timer_t frame_timer;
  88. int32_t internal;
  89. jack_nframes_t frames_at_cycle_start;
  90. pid_t engine_pid;
  91. jack_nframes_t buffer_size;
  92. int8_t real_time;
  93. int8_t do_mlock;
  94. int8_t do_munlock;
  95. int32_t client_priority;
  96. int32_t has_capabilities;
  97. float cpu_load;
  98. float xrun_delayed_usecs;
  99. uint32_t port_max;
  100. int32_t engine_ok;
  101. jack_port_type_id_t n_port_types;
  102. jack_port_type_info_t port_types[JACK_MAX_PORT_TYPES];
  103. jack_port_shared_t ports[0];
  104. } jack_control_t;
  105. typedef enum {
  106. BufferSizeChange,
  107. SampleRateChange,
  108. AttachPortSegment,
  109. PortConnected,
  110. PortDisconnected,
  111. GraphReordered,
  112. PortRegistered,
  113. PortUnregistered,
  114. XRun,
  115. StartFreewheel,
  116. StopFreewheel
  117. } EventType;
  118. typedef struct {
  119. EventType type;
  120. union {
  121. uint32_t n;
  122. jack_port_id_t port_id;
  123. jack_port_id_t self_id;
  124. } x;
  125. union {
  126. uint32_t n;
  127. jack_port_type_id_t ptid;
  128. jack_port_id_t other_id;
  129. } y;
  130. } jack_event_t;
  131. typedef enum {
  132. ClientInternal, /* connect request just names .so */
  133. ClientDriver, /* code is loaded along with driver */
  134. ClientExternal /* client is in another process */
  135. } ClientType;
  136. typedef enum {
  137. NotTriggered,
  138. Triggered,
  139. Running,
  140. Finished
  141. } jack_client_state_t;
  142. /* JACK client shared memory data structure. */
  143. typedef volatile struct {
  144. volatile jack_client_id_t id; /* w: engine r: engine and client */
  145. volatile jack_nframes_t nframes; /* w: engine r: client */
  146. volatile jack_client_state_t state; /* w: engine and client r: engine */
  147. volatile char name[JACK_CLIENT_NAME_SIZE];
  148. volatile ClientType type; /* w: engine r: engine and client */
  149. volatile int8_t active; /* w: engine r: engine and client */
  150. volatile int8_t dead; /* r/w: engine */
  151. volatile int8_t timed_out; /* r/w: engine */
  152. volatile int8_t is_timebase; /* w: engine, r: engine and client */
  153. volatile int8_t timebase_new; /* w: engine and client, r: engine */
  154. volatile int8_t is_slowsync; /* w: engine, r: engine and client */
  155. volatile int8_t active_slowsync; /* w: engine, r: engine and client */
  156. volatile int8_t sync_poll; /* w: engine and client, r: engine */
  157. volatile int8_t sync_new; /* w: engine and client, r: engine */
  158. volatile pid_t pid; /* w: client r: engine; client pid */
  159. volatile pid_t pgrp; /* w: client r: engine; client pgrp */
  160. volatile uint64_t signalled_at;
  161. volatile uint64_t awake_at;
  162. volatile uint64_t finished_at;
  163. /* JOQ: all these pointers are trouble for 32/64 compatibility,
  164. * they should move to non-shared memory.
  165. */
  166. /* callbacks
  167. */
  168. JackProcessCallback process;
  169. void *process_arg;
  170. JackThreadInitCallback thread_init;
  171. void *thread_init_arg;
  172. JackBufferSizeCallback bufsize;
  173. void *bufsize_arg;
  174. JackSampleRateCallback srate;
  175. void *srate_arg;
  176. JackPortRegistrationCallback port_register;
  177. void *port_register_arg;
  178. JackGraphOrderCallback graph_order;
  179. void *graph_order_arg;
  180. JackXRunCallback xrun;
  181. void *xrun_arg;
  182. JackSyncCallback sync_cb;
  183. void *sync_arg;
  184. JackTimebaseCallback timebase_cb;
  185. void *timebase_arg;
  186. JackFreewheelCallback freewheel_cb;
  187. void *freewheel_arg;
  188. /* external clients: set by libjack
  189. * internal clients: set by engine */
  190. int (*deliver_request)(void*, jack_request_t*);
  191. void *deliver_arg;
  192. /* for engine use only */
  193. void *private_client;
  194. } jack_client_control_t;
  195. typedef struct {
  196. int32_t load;
  197. ClientType type;
  198. jack_options_t options;
  199. char name[JACK_CLIENT_NAME_SIZE];
  200. char object_path[PATH_MAX+1];
  201. char object_data[1024];
  202. } jack_client_connect_request_t;
  203. typedef struct {
  204. int32_t status; /* messy name overloading */
  205. uint32_t protocol_v;
  206. jack_status_t open_status; /* used for open() */
  207. jack_shm_info_t client_shm;
  208. jack_shm_info_t engine_shm;
  209. char fifo_prefix[PATH_MAX+1];
  210. int32_t realtime;
  211. int32_t realtime_priority;
  212. char name[JACK_CLIENT_NAME_SIZE]; /* unique name, if assigned */
  213. /* these two are valid only if the connect request
  214. was for type == ClientDriver.
  215. */
  216. jack_client_control_t *client_control; /* JOQ: 64/32 problem */
  217. jack_control_t *engine_control; /* JOQ: 64/32 problem */
  218. #ifdef JACK_USE_MACH_THREADS
  219. /* specific resources for server/client real-time thread communication */
  220. int32_t portnum;
  221. #endif
  222. } jack_client_connect_result_t;
  223. typedef struct {
  224. jack_client_id_t client_id;
  225. } jack_client_connect_ack_request_t;
  226. typedef struct {
  227. int8_t status;
  228. } jack_client_connect_ack_result_t;
  229. typedef enum {
  230. RegisterPort = 1,
  231. UnRegisterPort = 2,
  232. ConnectPorts = 3,
  233. DisconnectPorts = 4,
  234. SetTimeBaseClient = 5,
  235. ActivateClient = 6,
  236. DeactivateClient = 7,
  237. DisconnectPort = 8,
  238. SetClientCapabilities = 9,
  239. GetPortConnections = 10,
  240. GetPortNConnections = 11,
  241. ResetTimeBaseClient = 12,
  242. SetSyncClient = 13,
  243. ResetSyncClient = 14,
  244. SetSyncTimeout = 15,
  245. SetBufferSize = 16,
  246. FreeWheel = 17,
  247. StopFreeWheel = 18,
  248. } RequestType;
  249. struct _jack_request {
  250. RequestType type;
  251. union {
  252. struct {
  253. char name[JACK_PORT_NAME_SIZE];
  254. char type[JACK_PORT_TYPE_SIZE];
  255. uint32_t flags;
  256. jack_shmsize_t buffer_size;
  257. jack_port_id_t port_id;
  258. jack_client_id_t client_id;
  259. } port_info;
  260. struct {
  261. char source_port[JACK_PORT_NAME_SIZE];
  262. char destination_port[JACK_PORT_NAME_SIZE];
  263. } connect;
  264. struct {
  265. int32_t nports;
  266. const char **ports; /* JOQ: 32/64 problem? */
  267. } port_connections;
  268. struct {
  269. jack_client_id_t client_id;
  270. int32_t conditional;
  271. } timebase;
  272. jack_client_id_t client_id;
  273. jack_nframes_t nframes;
  274. jack_time_t timeout;
  275. } x;
  276. int32_t status;
  277. };
  278. /* Per-client structure allocated in the server's address space.
  279. * It's here because its not part of the engine structure.
  280. */
  281. typedef struct _jack_client_internal {
  282. jack_client_control_t *control;
  283. int request_fd;
  284. int event_fd;
  285. int subgraph_start_fd;
  286. int subgraph_wait_fd;
  287. JSList *ports; /* protected by engine->client_lock */
  288. JSList *fed_by; /* protected by engine->client_lock */
  289. jack_shm_info_t control_shm;
  290. unsigned long execution_order;
  291. struct _jack_client_internal *next_client; /* not a linked list! */
  292. dlhandle handle;
  293. int (*initialize)(jack_client_t*, const char*); /* int. clients only */
  294. void (*finish)(void *); /* internal clients only */
  295. int error;
  296. #ifdef JACK_USE_MACH_THREADS
  297. /* specific resources for server/client real-time thread communication */
  298. mach_port_t serverport;
  299. trivial_message message;
  300. int running;
  301. int portnum;
  302. #endif /* JACK_USE_MACH_THREADS */
  303. } jack_client_internal_t;
  304. extern void jack_cleanup_files ();
  305. extern int jack_client_handle_port_connection (jack_client_t *client,
  306. jack_event_t *event);
  307. extern jack_client_t *jack_driver_client_new (jack_engine_t *,
  308. const char *client_name);
  309. extern jack_client_t *jack_client_alloc_internal (jack_client_control_t*,
  310. jack_engine_t*);
  311. /* internal clients call this. it's defined in jack/engine.c */
  312. void handle_internal_client_request (jack_control_t*, jack_request_t*);
  313. extern char *jack_server_dir;
  314. extern void *jack_zero_filled_buffer;
  315. extern jack_port_functions_t jack_builtin_audio_functions;
  316. extern jack_port_type_info_t jack_builtin_port_types[];
  317. extern void jack_client_invalidate_port_buffers (jack_client_t *client);
  318. extern void jack_transport_copy_position (jack_position_t *from,
  319. jack_position_t *to);
  320. extern void jack_call_sync_client (jack_client_t *client);
  321. extern void jack_call_timebase_master (jack_client_t *client);
  322. void silent_jack_error_callback (const char *desc);
  323. #endif /* __jack_internal_h__ */