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.

377 lines
10KB

  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:%Lu %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 struct {
  56. const char *shm_name;
  57. size_t offset;
  58. } jack_port_buffer_info_t;
  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. /* the relatively low value of this constant
  71. * reflects the fact that JACK currently only
  72. * knows about *1* port type. (March 2003)
  73. */
  74. #define JACK_MAX_PORT_TYPES 4
  75. /* JACK engine shared memory data structure. */
  76. typedef struct {
  77. jack_transport_state_t transport_state;
  78. volatile transport_command_t transport_cmd;
  79. transport_command_t previous_cmd; /* previous transport_cmd */
  80. jack_position_t current_time; /* position for current cycle */
  81. jack_position_t pending_time; /* position for next cycle */
  82. jack_position_t request_time; /* latest requested position */
  83. jack_unique_t prev_request; /* previous request unique ID */
  84. int new_pos; /* new position this cycle */
  85. unsigned long sync_clients; /* number of is_slowsync clients */
  86. unsigned long sync_remain; /* number of them with sync_poll */
  87. jack_time_t sync_timeout;
  88. jack_time_t sync_time_left;
  89. jack_frame_timer_t frame_timer;
  90. int internal;
  91. jack_nframes_t frames_at_cycle_start;
  92. pid_t engine_pid;
  93. unsigned long buffer_size;
  94. char real_time;
  95. int client_priority;
  96. int has_capabilities;
  97. float cpu_load;
  98. unsigned long port_max;
  99. int engine_ok;
  100. jack_engine_t *engine;
  101. unsigned long 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. NewPortType,
  109. PortConnected,
  110. PortDisconnected,
  111. GraphReordered,
  112. PortRegistered,
  113. PortUnregistered,
  114. XRun,
  115. } EventType;
  116. typedef struct {
  117. EventType type;
  118. union {
  119. unsigned long n;
  120. jack_port_id_t port_id;
  121. jack_port_id_t self_id;
  122. shm_name_t shm_name;
  123. } x;
  124. union {
  125. unsigned long n;
  126. jack_port_id_t other_id;
  127. void* addr;
  128. } y;
  129. union {
  130. size_t size;
  131. } z;
  132. } jack_event_t;
  133. typedef enum {
  134. ClientInternal, /* connect request just names .so */
  135. ClientDriver, /* code is loaded along with driver */
  136. ClientExternal /* client is in another process */
  137. } ClientType;
  138. typedef enum {
  139. NotTriggered,
  140. Triggered,
  141. Running,
  142. Finished
  143. } jack_client_state_t;
  144. /* JACK client shared memory data structure. */
  145. typedef volatile struct {
  146. volatile jack_client_id_t id; /* w: engine r: engine and client */
  147. volatile jack_nframes_t nframes; /* w: engine r: client */
  148. volatile jack_client_state_t state; /* w: engine and client r: engine */
  149. volatile char name[JACK_CLIENT_NAME_SIZE+1];
  150. volatile ClientType type; /* w: engine r: engine and client */
  151. volatile char active : 1; /* w: engine r: engine and client */
  152. volatile char dead : 1; /* r/w: engine */
  153. volatile char timed_out : 1; /* r/w: engine */
  154. volatile char is_timebase : 1; /* w: engine, r: engine and client */
  155. volatile char is_slowsync : 1; /* w: engine, r: engine and client */
  156. volatile char sync_poll : 1; /* w: engine and client, r: engine */
  157. volatile char sync_new : 1; /* w: engine and client, r: engine */
  158. volatile pid_t pid; /* w: client r: engine; client pid */
  159. volatile unsigned long long signalled_at;
  160. volatile unsigned long long awake_at;
  161. volatile unsigned long long finished_at;
  162. /* callbacks */
  163. JackProcessCallback process;
  164. void *process_arg;
  165. JackBufferSizeCallback bufsize;
  166. void *bufsize_arg;
  167. JackSampleRateCallback srate;
  168. void *srate_arg;
  169. JackPortRegistrationCallback port_register;
  170. void *port_register_arg;
  171. JackGraphOrderCallback graph_order;
  172. void *graph_order_arg;
  173. JackXRunCallback xrun;
  174. void *xrun_arg;
  175. JackSyncCallback sync_cb;
  176. void *sync_arg;
  177. JackTimebaseCallback timebase_cb;
  178. void *timebase_arg;
  179. /* external clients: set by libjack
  180. * internal clients: set by engine */
  181. int (*deliver_request)(void*, jack_request_t*);
  182. void *deliver_arg;
  183. /* for engine use only */
  184. void *private_client;
  185. } jack_client_control_t;
  186. typedef struct {
  187. int load;
  188. ClientType type;
  189. char name[JACK_CLIENT_NAME_SIZE+1];
  190. char object_path[PATH_MAX+1];
  191. char object_data[1024];
  192. } jack_client_connect_request_t;
  193. typedef struct {
  194. int status;
  195. unsigned int protocol_v;
  196. shm_name_t client_shm_name;
  197. shm_name_t control_shm_name;
  198. char fifo_prefix[PATH_MAX+1];
  199. int realtime;
  200. int realtime_priority;
  201. /* these two are valid only if the connect request
  202. was for type == ClientDriver.
  203. */
  204. jack_client_control_t *client_control;
  205. jack_control_t *engine_control;
  206. size_t control_size;
  207. /* when we write this response, we deliver n_port_types
  208. of jack_port_type_info_t after it.
  209. */
  210. unsigned long n_port_types;
  211. #if defined(__APPLE__) && defined(__POWERPC__)
  212. /* specific ressources for server/client real-time thread communication */
  213. int portnum;
  214. #endif
  215. } jack_client_connect_result_t;
  216. typedef struct {
  217. jack_client_id_t client_id;
  218. } jack_client_connect_ack_request_t;
  219. typedef struct {
  220. char status;
  221. } jack_client_connect_ack_result_t;
  222. typedef enum {
  223. RegisterPort = 1,
  224. UnRegisterPort = 2,
  225. ConnectPorts = 3,
  226. DisconnectPorts = 4,
  227. SetTimeBaseClient = 5,
  228. ActivateClient = 6,
  229. DeactivateClient = 7,
  230. DisconnectPort = 8,
  231. SetClientCapabilities = 9,
  232. GetPortConnections = 10,
  233. GetPortNConnections = 11,
  234. ResetTimeBaseClient = 12,
  235. SetSyncClient = 13,
  236. ResetSyncClient = 14,
  237. SetSyncTimeout = 15,
  238. } RequestType;
  239. struct _jack_request {
  240. RequestType type;
  241. union {
  242. struct {
  243. char name[JACK_PORT_NAME_SIZE+1];
  244. char type[JACK_PORT_TYPE_SIZE+1];
  245. unsigned long flags;
  246. unsigned long buffer_size;
  247. jack_port_id_t port_id;
  248. jack_client_id_t client_id;
  249. } port_info;
  250. struct {
  251. char source_port[JACK_PORT_NAME_SIZE+1];
  252. char destination_port[JACK_PORT_NAME_SIZE+1];
  253. } connect;
  254. struct {
  255. unsigned int nports;
  256. const char **ports;
  257. } port_connections;
  258. struct {
  259. jack_client_id_t client_id;
  260. int conditional;
  261. } timebase;
  262. jack_client_id_t client_id;
  263. jack_nframes_t nframes;
  264. jack_time_t timeout;
  265. } x;
  266. int status;
  267. };
  268. typedef struct _jack_client_internal {
  269. jack_client_control_t *control;
  270. int request_fd;
  271. int event_fd;
  272. int subgraph_start_fd;
  273. int subgraph_wait_fd;
  274. JSList *ports; /* protected by engine->client_lock */
  275. JSList *fed_by; /* protected by engine->client_lock */
  276. shm_name_t shm_name;
  277. unsigned long execution_order;
  278. struct _jack_client_internal *next_client; /* not a linked list! */
  279. dlhandle handle;
  280. int (*initialize)(jack_client_t*, const char*); /* for internal clients only */
  281. void (*finish)(void *); /* for internal clients only */
  282. int error;
  283. #if defined(__APPLE__) && defined(__POWERPC__)
  284. /* specific ressources for server/client real-time thread communication */
  285. mach_port_t serverport;
  286. trivial_message message;
  287. int running;
  288. int portnum;
  289. #endif
  290. } jack_client_internal_t;
  291. extern void jack_cleanup_files ();
  292. extern int jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event);
  293. extern void jack_client_handle_new_port_type (jack_client_t *client, shm_name_t, size_t, void* addr);
  294. extern jack_client_t *jack_driver_client_new (jack_engine_t *, const char *client_name);
  295. jack_client_t *jack_client_alloc_internal (jack_client_control_t*, jack_control_t*);
  296. /* internal clients call this. its defined in jack/engine.c */
  297. void handle_internal_client_request (jack_control_t*, jack_request_t*);
  298. extern char *jack_server_dir;
  299. extern void jack_error (const char *fmt, ...);
  300. extern jack_port_type_info_t jack_builtin_port_types[];
  301. extern void jack_client_invalidate_port_buffers (jack_client_t *client);
  302. extern void jack_transport_copy_position (jack_position_t *from,
  303. jack_position_t *to);
  304. #endif /* __jack_internal_h__ */