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.

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