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.

379 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. volatile long seq_number; /* unique ID sequence number */
  85. char new_pos; /* new position this cycle */
  86. char pending_pos; /* new position request pending */
  87. jack_nframes_t pending_frame; /* pending frame number */
  88. unsigned long sync_clients; /* number of is_slowsync clients */
  89. unsigned long sync_remain; /* number of them with sync_poll */
  90. jack_time_t sync_timeout;
  91. jack_time_t sync_time_left;
  92. jack_frame_timer_t frame_timer;
  93. int internal;
  94. jack_nframes_t frames_at_cycle_start;
  95. pid_t engine_pid;
  96. unsigned long buffer_size;
  97. char real_time;
  98. int client_priority;
  99. int has_capabilities;
  100. float cpu_load;
  101. unsigned long port_max;
  102. int engine_ok;
  103. unsigned long n_port_types;
  104. jack_port_type_info_t port_types[JACK_MAX_PORT_TYPES];
  105. jack_port_shared_t ports[0];
  106. } jack_control_t;
  107. typedef enum {
  108. BufferSizeChange,
  109. SampleRateChange,
  110. NewPortType,
  111. PortConnected,
  112. PortDisconnected,
  113. GraphReordered,
  114. PortRegistered,
  115. PortUnregistered,
  116. XRun,
  117. } EventType;
  118. typedef struct {
  119. EventType type;
  120. union {
  121. unsigned long n;
  122. jack_port_id_t port_id;
  123. jack_port_id_t self_id;
  124. shm_name_t shm_name;
  125. } x;
  126. union {
  127. unsigned long n;
  128. jack_port_id_t other_id;
  129. void* addr;
  130. } y;
  131. union {
  132. size_t size;
  133. } z;
  134. } jack_event_t;
  135. typedef enum {
  136. ClientInternal, /* connect request just names .so */
  137. ClientDriver, /* code is loaded along with driver */
  138. ClientExternal /* client is in another process */
  139. } ClientType;
  140. typedef enum {
  141. NotTriggered,
  142. Triggered,
  143. Running,
  144. Finished
  145. } jack_client_state_t;
  146. /* JACK client shared memory data structure. */
  147. typedef volatile struct {
  148. volatile jack_client_id_t id; /* w: engine r: engine and client */
  149. volatile jack_nframes_t nframes; /* w: engine r: client */
  150. volatile jack_client_state_t state; /* w: engine and client r: engine */
  151. volatile char name[JACK_CLIENT_NAME_SIZE+1];
  152. volatile ClientType type; /* w: engine r: engine and client */
  153. volatile char active : 1; /* w: engine r: engine and client */
  154. volatile char dead : 1; /* r/w: engine */
  155. volatile char timed_out : 1; /* r/w: engine */
  156. volatile char is_timebase : 1; /* w: engine, r: engine and client */
  157. volatile char is_slowsync : 1; /* w: engine, r: engine and client */
  158. volatile char sync_poll : 1; /* w: engine and client, r: engine */
  159. volatile char sync_new : 1; /* w: engine and client, r: engine */
  160. volatile pid_t pid; /* w: client r: engine; client pid */
  161. volatile unsigned long long signalled_at;
  162. volatile unsigned long long awake_at;
  163. volatile unsigned long long finished_at;
  164. /* callbacks */
  165. JackProcessCallback process;
  166. void *process_arg;
  167. JackBufferSizeCallback bufsize;
  168. void *bufsize_arg;
  169. JackSampleRateCallback srate;
  170. void *srate_arg;
  171. JackPortRegistrationCallback port_register;
  172. void *port_register_arg;
  173. JackGraphOrderCallback graph_order;
  174. void *graph_order_arg;
  175. JackXRunCallback xrun;
  176. void *xrun_arg;
  177. JackSyncCallback sync_cb;
  178. void *sync_arg;
  179. JackTimebaseCallback timebase_cb;
  180. void *timebase_arg;
  181. /* external clients: set by libjack
  182. * internal clients: set by engine */
  183. int (*deliver_request)(void*, jack_request_t*);
  184. void *deliver_arg;
  185. /* for engine use only */
  186. void *private_client;
  187. } jack_client_control_t;
  188. typedef struct {
  189. int load;
  190. ClientType type;
  191. char name[JACK_CLIENT_NAME_SIZE+1];
  192. char object_path[PATH_MAX+1];
  193. char object_data[1024];
  194. } jack_client_connect_request_t;
  195. typedef struct {
  196. int status;
  197. unsigned int protocol_v;
  198. shm_name_t client_shm_name;
  199. shm_name_t control_shm_name;
  200. char fifo_prefix[PATH_MAX+1];
  201. int realtime;
  202. int realtime_priority;
  203. /* these two are valid only if the connect request
  204. was for type == ClientDriver.
  205. */
  206. jack_client_control_t *client_control;
  207. jack_control_t *engine_control;
  208. size_t control_size;
  209. /* when we write this response, we deliver n_port_types
  210. of jack_port_type_info_t after it.
  211. */
  212. unsigned long n_port_types;
  213. #if defined(__APPLE__) && defined(__POWERPC__)
  214. /* specific ressources for server/client real-time thread communication */
  215. int 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. char 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. } RequestType;
  241. struct _jack_request {
  242. RequestType type;
  243. union {
  244. struct {
  245. char name[JACK_PORT_NAME_SIZE+1];
  246. char type[JACK_PORT_TYPE_SIZE+1];
  247. unsigned long flags;
  248. unsigned long buffer_size;
  249. jack_port_id_t port_id;
  250. jack_client_id_t client_id;
  251. } port_info;
  252. struct {
  253. char source_port[JACK_PORT_NAME_SIZE+1];
  254. char destination_port[JACK_PORT_NAME_SIZE+1];
  255. } connect;
  256. struct {
  257. unsigned int nports;
  258. const char **ports;
  259. } port_connections;
  260. struct {
  261. jack_client_id_t client_id;
  262. int conditional;
  263. } timebase;
  264. jack_client_id_t client_id;
  265. jack_nframes_t nframes;
  266. jack_time_t timeout;
  267. } x;
  268. int status;
  269. };
  270. typedef struct _jack_client_internal {
  271. jack_client_control_t *control;
  272. int request_fd;
  273. int event_fd;
  274. int subgraph_start_fd;
  275. int subgraph_wait_fd;
  276. JSList *ports; /* protected by engine->client_lock */
  277. JSList *fed_by; /* protected by engine->client_lock */
  278. shm_name_t shm_name;
  279. unsigned long execution_order;
  280. struct _jack_client_internal *next_client; /* not a linked list! */
  281. dlhandle handle;
  282. int (*initialize)(jack_client_t*, const char*); /* for internal clients only */
  283. void (*finish)(void *); /* for internal clients only */
  284. int error;
  285. #if defined(__APPLE__) && defined(__POWERPC__)
  286. /* specific ressources for server/client real-time thread communication */
  287. mach_port_t serverport;
  288. trivial_message message;
  289. int running;
  290. int portnum;
  291. #endif
  292. } jack_client_internal_t;
  293. extern void jack_cleanup_files ();
  294. extern int jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event);
  295. extern void jack_client_handle_new_port_type (jack_client_t *client, shm_name_t, size_t, void* addr);
  296. extern jack_client_t *jack_driver_client_new (jack_engine_t *, const char *client_name);
  297. jack_client_t *jack_client_alloc_internal (jack_client_control_t*, jack_control_t*);
  298. /* internal clients call this. its 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_type_info_t jack_builtin_port_types[];
  303. extern void jack_client_invalidate_port_buffers (jack_client_t *client);
  304. extern void jack_transport_copy_position (jack_position_t *from,
  305. jack_position_t *to);
  306. #endif /* __jack_internal_h__ */