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.

378 lines
11KB

  1. /*
  2. Internal shared data and functions.
  3. If you edit this file, you should carefully consider changing the
  4. JACK_PROTOCOL_VERSION in configure.in.
  5. Copyright (C) 2001-2003 Paul Davis
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. $Id$
  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 <sys/types.h>
  26. #include <sys/time.h>
  27. #include <pthread.h>
  28. #if defined(__APPLE__) && defined(__POWERPC__)
  29. #include "mach_port.h"
  30. #endif
  31. #include <jack/jack.h>
  32. #include <jack/types.h>
  33. #include <jack/port.h>
  34. #include <jack/transport.h>
  35. #include <jack/time.h>
  36. #ifdef DEBUG_ENABLED
  37. #define DEBUG(format,args...) \
  38. printf ("jack:%5d:%" PRIu64 " %s:%s:%d: " format "\n", getpid(), jack_get_microseconds(), __FILE__, __FUNCTION__, __LINE__ , ## args)
  39. #else
  40. #if defined(__APPLE__) && defined(__POWERPC__)
  41. #define DEBUG(format...)
  42. #else
  43. #define DEBUG(format,args...)
  44. #endif
  45. #endif
  46. #ifndef FALSE
  47. #define FALSE (0)
  48. #endif
  49. #ifndef TRUE
  50. #define TRUE (!FALSE)
  51. #endif
  52. typedef struct _jack_engine jack_engine_t;
  53. typedef struct _jack_request jack_request_t;
  54. typedef void * dlhandle;
  55. typedef enum {
  56. TransportCommandNone = 0,
  57. TransportCommandStart = 1,
  58. TransportCommandStop = 2,
  59. } transport_command_t;
  60. typedef struct {
  61. volatile jack_time_t guard1;
  62. volatile jack_nframes_t frames;
  63. volatile jack_time_t stamp;
  64. volatile jack_time_t guard2;
  65. } jack_frame_timer_t;
  66. /* JACK engine shared memory data structure. */
  67. typedef struct {
  68. jack_transport_state_t transport_state;
  69. volatile transport_command_t transport_cmd;
  70. transport_command_t previous_cmd; /* previous transport_cmd */
  71. jack_position_t current_time; /* position for current cycle */
  72. jack_position_t pending_time; /* position for next cycle */
  73. jack_position_t request_time; /* latest requested position */
  74. jack_unique_t prev_request; /* previous request unique ID */
  75. volatile uint32_t seq_number; /* unique ID sequence number */
  76. int8_t new_pos; /* new position this cycle */
  77. int8_t pending_pos; /* new position request pending */
  78. jack_nframes_t pending_frame; /* pending frame number */
  79. uint32_t sync_clients; /* number of is_slowsync clients */
  80. uint32_t sync_remain; /* number of them with sync_poll */
  81. jack_time_t sync_timeout;
  82. jack_time_t sync_time_left;
  83. jack_frame_timer_t frame_timer;
  84. int32_t internal;
  85. jack_nframes_t frames_at_cycle_start;
  86. pid_t engine_pid;
  87. jack_nframes_t buffer_size;
  88. int8_t real_time;
  89. int32_t client_priority;
  90. int32_t has_capabilities;
  91. float cpu_load;
  92. uint32_t port_max;
  93. int32_t engine_ok;
  94. uint32_t n_port_types;
  95. jack_port_type_info_t port_types[JACK_MAX_PORT_TYPES];
  96. jack_port_shared_t ports[0];
  97. } jack_control_t;
  98. typedef enum {
  99. BufferSizeChange,
  100. SampleRateChange,
  101. AttachPortSegment,
  102. PortConnected,
  103. PortDisconnected,
  104. GraphReordered,
  105. PortRegistered,
  106. PortUnregistered,
  107. XRun,
  108. } EventType;
  109. typedef struct {
  110. EventType type;
  111. union {
  112. uint32_t n;
  113. jack_port_id_t port_id;
  114. jack_port_id_t self_id;
  115. shm_name_t shm_name;
  116. } x;
  117. union {
  118. uint32_t n;
  119. jack_port_type_id_t ptid;
  120. jack_port_id_t other_id;
  121. } y;
  122. union {
  123. jack_shmsize_t size;
  124. } z;
  125. } jack_event_t;
  126. typedef enum {
  127. ClientInternal, /* connect request just names .so */
  128. ClientDriver, /* code is loaded along with driver */
  129. ClientExternal /* client is in another process */
  130. } ClientType;
  131. typedef enum {
  132. NotTriggered,
  133. Triggered,
  134. Running,
  135. Finished
  136. } jack_client_state_t;
  137. /* JACK client shared memory data structure. */
  138. typedef volatile struct {
  139. volatile jack_client_id_t id; /* w: engine r: engine and client */
  140. volatile jack_nframes_t nframes; /* w: engine r: client */
  141. volatile jack_client_state_t state; /* w: engine and client r: engine */
  142. volatile int8_t name[JACK_CLIENT_NAME_SIZE+1];
  143. volatile ClientType type; /* w: engine r: engine and client */
  144. volatile int8_t active : 1; /* w: engine r: engine and client */
  145. volatile int8_t dead : 1; /* r/w: engine */
  146. volatile int8_t timed_out : 1; /* r/w: engine */
  147. volatile int8_t is_timebase : 1; /* w: engine, r: engine and client */
  148. volatile int8_t is_slowsync : 1; /* w: engine, r: engine and client */
  149. volatile int8_t sync_poll : 1; /* w: engine and client, r: engine */
  150. volatile int8_t sync_new : 1; /* w: engine and client, r: engine */
  151. volatile pid_t pid; /* w: client r: engine; client pid */
  152. volatile pid_t pgrp; /* w: client r: engine; client pgrp */
  153. volatile uint64_t signalled_at;
  154. volatile uint64_t awake_at;
  155. volatile uint64_t finished_at;
  156. /* JOQ: all these pointers are trouble for 32/64 compatibility,
  157. * they should move to non-shared memory. */
  158. /* callbacks */
  159. JackProcessCallback process;
  160. void *process_arg;
  161. JackBufferSizeCallback bufsize;
  162. void *bufsize_arg;
  163. JackSampleRateCallback srate;
  164. void *srate_arg;
  165. JackPortRegistrationCallback port_register;
  166. void *port_register_arg;
  167. JackGraphOrderCallback graph_order;
  168. void *graph_order_arg;
  169. JackXRunCallback xrun;
  170. void *xrun_arg;
  171. JackSyncCallback sync_cb;
  172. void *sync_arg;
  173. JackTimebaseCallback timebase_cb;
  174. void *timebase_arg;
  175. /* external clients: set by libjack
  176. * internal clients: set by engine */
  177. int (*deliver_request)(void*, jack_request_t*);
  178. void *deliver_arg;
  179. /* for engine use only */
  180. void *private_client;
  181. } jack_client_control_t;
  182. typedef struct {
  183. int32_t load;
  184. ClientType type;
  185. char name[JACK_CLIENT_NAME_SIZE+1];
  186. char object_path[PATH_MAX+1];
  187. char object_data[1024];
  188. } jack_client_connect_request_t;
  189. typedef struct {
  190. int32_t status;
  191. uint32_t protocol_v;
  192. shm_name_t client_shm_name;
  193. shm_name_t control_shm_name;
  194. char fifo_prefix[PATH_MAX+1];
  195. int32_t realtime;
  196. int32_t realtime_priority;
  197. /* these two are valid only if the connect request
  198. was for type == ClientDriver. */
  199. jack_client_control_t *client_control; /* JOQ: 64/32 problem */
  200. jack_control_t *engine_control; /* JOQ: 64/32 problem */
  201. jack_shmsize_t control_size;
  202. /* when we write this response, we deliver n_port_types
  203. of jack_port_type_info_t after it. */
  204. uint32_t n_port_types;
  205. #if defined(__APPLE__) && defined(__POWERPC__)
  206. /* specific resources for server/client real-time thread communication */
  207. int32_t portnum;
  208. #endif
  209. } jack_client_connect_result_t;
  210. typedef struct {
  211. jack_client_id_t client_id;
  212. } jack_client_connect_ack_request_t;
  213. typedef struct {
  214. int8_t status;
  215. } jack_client_connect_ack_result_t;
  216. typedef enum {
  217. RegisterPort = 1,
  218. UnRegisterPort = 2,
  219. ConnectPorts = 3,
  220. DisconnectPorts = 4,
  221. SetTimeBaseClient = 5,
  222. ActivateClient = 6,
  223. DeactivateClient = 7,
  224. DisconnectPort = 8,
  225. SetClientCapabilities = 9,
  226. GetPortConnections = 10,
  227. GetPortNConnections = 11,
  228. ResetTimeBaseClient = 12,
  229. SetSyncClient = 13,
  230. ResetSyncClient = 14,
  231. SetSyncTimeout = 15,
  232. SetBufferSize = 16,
  233. } RequestType;
  234. struct _jack_request {
  235. RequestType type;
  236. union {
  237. struct {
  238. char name[JACK_PORT_NAME_SIZE+1];
  239. char type[JACK_PORT_TYPE_SIZE+1];
  240. uint32_t flags;
  241. jack_shmsize_t buffer_size;
  242. jack_port_id_t port_id;
  243. jack_client_id_t client_id;
  244. } port_info;
  245. struct {
  246. char source_port[JACK_PORT_NAME_SIZE+1];
  247. char destination_port[JACK_PORT_NAME_SIZE+1];
  248. } connect;
  249. struct {
  250. int32_t nports;
  251. const char **ports; /* JOQ: 32/64 problem? */
  252. } port_connections;
  253. struct {
  254. jack_client_id_t client_id;
  255. int32_t conditional;
  256. } timebase;
  257. jack_client_id_t client_id;
  258. jack_nframes_t nframes;
  259. jack_time_t timeout;
  260. } x;
  261. int32_t status;
  262. };
  263. /* per-client structure allocated in the server's address space
  264. * JOQ: then why isn't this in engine.h? */
  265. typedef struct _jack_client_internal {
  266. jack_client_control_t *control;
  267. int request_fd;
  268. int event_fd;
  269. int subgraph_start_fd;
  270. int subgraph_wait_fd;
  271. JSList *ports; /* protected by engine->client_lock */
  272. JSList *fed_by; /* protected by engine->client_lock */
  273. shm_name_t shm_name;
  274. unsigned long execution_order;
  275. struct _jack_client_internal *next_client; /* not a linked list! */
  276. dlhandle handle;
  277. int (*initialize)(jack_client_t*, const char*); /* int. clients only */
  278. void (*finish)(void *); /* internal clients only */
  279. int error;
  280. #if defined(__APPLE__) && defined(__POWERPC__)
  281. /* specific resources for server/client real-time thread communication */
  282. mach_port_t serverport;
  283. trivial_message message;
  284. int running;
  285. int portnum;
  286. #endif
  287. } jack_client_internal_t;
  288. extern void jack_cleanup_files ();
  289. extern int jack_client_handle_port_connection (jack_client_t *client,
  290. jack_event_t *event);
  291. extern void jack_client_set_port_segment (jack_client_t *client, shm_name_t,
  292. jack_port_type_id_t ptid,
  293. jack_shmsize_t, void *addr);
  294. extern jack_client_t *jack_driver_client_new (jack_engine_t *,
  295. const char *client_name);
  296. jack_client_t *jack_client_alloc_internal (jack_client_control_t*,
  297. jack_control_t*);
  298. /* internal clients call this. it's defined in jack/engine.c */
  299. void handle_internal_client_request (jack_control_t*, jack_request_t*);
  300. extern char *jack_server_dir;
  301. extern void jack_error (const char *fmt, ...);
  302. extern jack_port_functions_t jack_builtin_audio_functions;
  303. extern jack_port_type_info_t jack_builtin_port_types[];
  304. extern void jack_client_invalidate_port_buffers (jack_client_t *client);
  305. extern void jack_transport_copy_position (jack_position_t *from,
  306. jack_position_t *to);
  307. extern void jack_call_sync_client (jack_client_t *client);
  308. extern void jack_call_timebase_master (jack_client_t *client);
  309. #endif /* __jack_internal_h__ */