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.

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