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.

385 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. */
  158. /* callbacks
  159. */
  160. JackProcessCallback process;
  161. void *process_arg;
  162. JackBufferSizeCallback bufsize;
  163. void *bufsize_arg;
  164. JackSampleRateCallback srate;
  165. void *srate_arg;
  166. JackPortRegistrationCallback port_register;
  167. void *port_register_arg;
  168. JackGraphOrderCallback graph_order;
  169. void *graph_order_arg;
  170. JackXRunCallback xrun;
  171. void *xrun_arg;
  172. JackSyncCallback sync_cb;
  173. void *sync_arg;
  174. JackTimebaseCallback timebase_cb;
  175. void *timebase_arg;
  176. JackFreewheelCallback freewheel_cb;
  177. void *freewheel_arg;
  178. /* external clients: set by libjack
  179. * internal clients: set by engine */
  180. int (*deliver_request)(void*, jack_request_t*);
  181. void *deliver_arg;
  182. /* for engine use only */
  183. void *private_client;
  184. } jack_client_control_t;
  185. typedef struct {
  186. int32_t load;
  187. ClientType type;
  188. char name[JACK_CLIENT_NAME_SIZE+1];
  189. char object_path[PATH_MAX+1];
  190. char object_data[1024];
  191. } jack_client_connect_request_t;
  192. typedef struct {
  193. int32_t status;
  194. uint32_t protocol_v;
  195. jack_shm_info_t client_shm;
  196. jack_shm_info_t engine_shm;
  197. char fifo_prefix[PATH_MAX+1];
  198. int32_t realtime;
  199. int32_t realtime_priority;
  200. /* these two are valid only if the connect request
  201. was for type == ClientDriver.
  202. */
  203. jack_client_control_t *client_control; /* JOQ: 64/32 problem */
  204. jack_control_t *engine_control; /* JOQ: 64/32 problem */
  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. FreeWheel = 17,
  234. StopFreeWheel = 18,
  235. } RequestType;
  236. struct _jack_request {
  237. RequestType type;
  238. union {
  239. struct {
  240. char name[JACK_PORT_NAME_SIZE+1];
  241. char type[JACK_PORT_TYPE_SIZE+1];
  242. uint32_t flags;
  243. jack_shmsize_t buffer_size;
  244. jack_port_id_t port_id;
  245. jack_client_id_t client_id;
  246. } port_info;
  247. struct {
  248. char source_port[JACK_PORT_NAME_SIZE+1];
  249. char destination_port[JACK_PORT_NAME_SIZE+1];
  250. } connect;
  251. struct {
  252. int32_t nports;
  253. const char **ports; /* JOQ: 32/64 problem? */
  254. } port_connections;
  255. struct {
  256. jack_client_id_t client_id;
  257. int32_t conditional;
  258. } timebase;
  259. jack_client_id_t client_id;
  260. jack_nframes_t nframes;
  261. jack_time_t timeout;
  262. } x;
  263. int32_t status;
  264. };
  265. /* per-client structure allocated in the server's address space
  266. * its here because its not part of the engine structure.
  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. jack_shm_info_t control_shm;
  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*); /* int. clients only */
  281. void (*finish)(void *); /* internal clients only */
  282. int error;
  283. #if defined(__APPLE__) && defined(__POWERPC__)
  284. /* specific resources 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,
  293. jack_event_t *event);
  294. extern jack_client_t *jack_driver_client_new (jack_engine_t *,
  295. const char *client_name);
  296. extern jack_client_t *jack_client_alloc_internal (jack_client_control_t*,
  297. jack_engine_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_zero_filled_buffer;
  302. extern void jack_error (const char *fmt, ...);
  303. extern jack_port_functions_t jack_builtin_audio_functions;
  304. extern jack_port_type_info_t jack_builtin_port_types[];
  305. extern void jack_client_invalidate_port_buffers (jack_client_t *client);
  306. extern void jack_transport_copy_position (jack_position_t *from,
  307. jack_position_t *to);
  308. extern void jack_call_sync_client (jack_client_t *client);
  309. extern void jack_call_timebase_master (jack_client_t *client);
  310. extern int jack_acquire_real_time_scheduling (pthread_t, int priority);
  311. extern int jack_drop_real_time_scheduling (pthread_t);
  312. void silent_jack_error_callback (const char *desc);
  313. #endif /* __jack_internal_h__ */