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.

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