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.

568 lines
16KB

  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. */
  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 <pthread.h>
  26. #include <sys/types.h>
  27. #include <sys/time.h>
  28. /* Needed by <sysdeps/time.h> */
  29. extern void jack_error(const char *fmt, ...);
  30. extern void jack_info(const char *fmt, ...);
  31. #include <jack/jack.h>
  32. #include <jack/types.h>
  33. #include <jack/transport.h>
  34. #include <jack/session.h>
  35. #include <jack/thread.h>
  36. #include <jack/metadata.h>
  37. #include "port.h"
  38. extern jack_thread_creator_t jack_thread_creator;
  39. typedef enum {
  40. JACK_TIMER_SYSTEM_CLOCK,
  41. JACK_TIMER_HPET,
  42. } jack_timer_type_t;
  43. void jack_init_time();
  44. void jack_set_clock_source (jack_timer_type_t);
  45. const char* jack_clock_source_name (jack_timer_type_t);
  46. #include <sysdeps/time.h>
  47. #include "atomicity.h"
  48. #ifdef JACK_USE_MACH_THREADS
  49. #include <sysdeps/mach_port.h>
  50. #endif
  51. #include "messagebuffer.h"
  52. #ifndef PATH_MAX
  53. #ifdef MAXPATHLEN
  54. #define PATH_MAX MAXPATHLEN
  55. #else
  56. #define PATH_MAX 1024
  57. #endif /* MAXPATHLEN */
  58. #endif /* !PATH_MAX */
  59. #ifdef DEBUG_ENABLED
  60. /* grab thread id instead of PID on linux */
  61. #if defined(__gnu_linux__)
  62. #ifdef gettid /* glibc has a version */
  63. #define GETTID() gettid ()
  64. #else /* use our own version */
  65. #include <sys/syscall.h>
  66. #define GETTID() syscall (__NR_gettid)
  67. #endif
  68. #else
  69. #define GETTID() getpid ()
  70. #endif
  71. #define DEBUG(format, args ...) \
  72. MESSAGE ("jack:%5d:%" PRIu64 " %s:%s:%d: " format "", GETTID (), jack_get_microseconds (), __FILE__, __FUNCTION__, __LINE__, ## args)
  73. #else
  74. #if JACK_CPP_VARARGS_BROKEN
  75. #define DEBUG(format ...)
  76. #else
  77. #define DEBUG(format, args ...)
  78. #endif
  79. #endif
  80. /* Enable preemption checking for Linux Realtime Preemption kernels.
  81. *
  82. * This checks if any RT-safe code section does anything to cause CPU
  83. * preemption. Examples are sleep() or other system calls that block.
  84. * If a problem is detected, the kernel writes a syslog entry, and
  85. * sends SIGUSR2 to the client.
  86. */
  87. #ifdef DO_PREEMPTION_CHECKING
  88. #define CHECK_PREEMPTION(engine, onoff) \
  89. if ((engine)->real_time) gettimeofday (1, (onoff))
  90. #else
  91. #define CHECK_PREEMPTION(engine, onoff)
  92. #endif
  93. #ifndef FALSE
  94. #define FALSE (0)
  95. #endif
  96. #ifndef TRUE
  97. #define TRUE (1)
  98. #endif
  99. typedef struct _jack_engine jack_engine_t;
  100. typedef struct _jack_request jack_request_t;
  101. typedef void * dlhandle;
  102. typedef enum {
  103. TransportCommandNone = 0,
  104. TransportCommandStart = 1,
  105. TransportCommandStop = 2,
  106. } transport_command_t;
  107. typedef struct {
  108. volatile uint32_t guard1;
  109. volatile jack_nframes_t frames;
  110. volatile jack_time_t current_wakeup;
  111. volatile jack_time_t next_wakeup;
  112. volatile float period_usecs;
  113. volatile int32_t initialized;
  114. volatile uint32_t guard2;
  115. /* not accessed by clients */
  116. int32_t reset_pending; /* xrun happened, deal with it */
  117. float filter_omega; /* set once, never altered */
  118. } POST_PACKED_STRUCTURE jack_frame_timer_t;
  119. /* JACK engine shared memory data structure. */
  120. typedef struct {
  121. jack_transport_state_t transport_state;
  122. volatile transport_command_t transport_cmd;
  123. transport_command_t previous_cmd; /* previous transport_cmd */
  124. jack_position_t current_time; /* position for current cycle */
  125. jack_position_t pending_time; /* position for next cycle */
  126. jack_position_t request_time; /* latest requested position */
  127. jack_unique_t prev_request; /* previous request unique ID */
  128. volatile _Atomic_word seq_number; /* unique ID sequence number */
  129. int8_t new_pos; /* new position this cycle */
  130. int8_t pending_pos; /* new position request pending */
  131. jack_nframes_t pending_frame; /* pending frame number */
  132. int32_t sync_clients; /* number of active_slowsync clients */
  133. int32_t sync_remain; /* number of them with sync_poll */
  134. jack_time_t sync_timeout;
  135. jack_time_t sync_time_left;
  136. jack_frame_timer_t frame_timer;
  137. int32_t internal;
  138. jack_timer_type_t clock_source;
  139. pid_t engine_pid;
  140. jack_nframes_t buffer_size;
  141. int8_t real_time;
  142. int8_t do_mlock;
  143. int8_t do_munlock;
  144. int32_t client_priority;
  145. int32_t max_client_priority;
  146. int32_t has_capabilities;
  147. float cpu_load;
  148. float xrun_delayed_usecs;
  149. float max_delayed_usecs;
  150. uint32_t port_max;
  151. int32_t engine_ok;
  152. jack_port_type_id_t n_port_types;
  153. jack_port_type_info_t port_types[JACK_MAX_PORT_TYPES];
  154. jack_port_shared_t ports[0];
  155. } POST_PACKED_STRUCTURE jack_control_t;
  156. typedef enum {
  157. BufferSizeChange,
  158. SampleRateChange,
  159. AttachPortSegment,
  160. PortConnected,
  161. PortDisconnected,
  162. GraphReordered,
  163. PortRegistered,
  164. PortUnregistered,
  165. XRun,
  166. StartFreewheel,
  167. StopFreewheel,
  168. ClientRegistered,
  169. ClientUnregistered,
  170. SaveSession,
  171. LatencyCallback,
  172. PropertyChange,
  173. PortRename
  174. } JackEventType;
  175. const char* jack_event_type_name (JackEventType);
  176. typedef struct {
  177. JackEventType type;
  178. union {
  179. uint32_t n;
  180. char name[JACK_PORT_NAME_SIZE];
  181. jack_port_id_t port_id;
  182. jack_port_id_t self_id;
  183. jack_uuid_t uuid;
  184. } x;
  185. union {
  186. uint32_t n;
  187. jack_port_type_id_t ptid;
  188. jack_port_id_t other_id;
  189. uint32_t key_size; /* key data will follow the event structure */
  190. } y;
  191. union {
  192. char other_name[JACK_PORT_NAME_SIZE];
  193. jack_property_change_t property_change;
  194. } z;
  195. } POST_PACKED_STRUCTURE jack_event_t;
  196. typedef enum {
  197. ClientInternal, /* connect request just names .so */
  198. ClientDriver, /* code is loaded along with driver */
  199. ClientExternal /* client is in another process */
  200. } ClientType;
  201. typedef enum {
  202. NotTriggered,
  203. Triggered,
  204. Running,
  205. Finished
  206. } jack_client_state_t;
  207. /* JACK client shared memory data structure. */
  208. typedef volatile struct {
  209. jack_uuid_t uuid; /* w: engine r: engine and client */
  210. volatile jack_client_state_t state; /* w: engine and client r: engine */
  211. volatile char name[JACK_CLIENT_NAME_SIZE];
  212. volatile char session_command[JACK_PORT_NAME_SIZE];
  213. volatile jack_session_flags_t session_flags;
  214. volatile ClientType type; /* w: engine r: engine and client */
  215. volatile int8_t active; /* w: engine r: engine and client */
  216. volatile int8_t dead; /* r/w: engine */
  217. volatile int8_t timed_out; /* r/w: engine */
  218. volatile int8_t is_timebase; /* w: engine, r: engine and client */
  219. volatile int8_t timebase_new; /* w: engine and client, r: engine */
  220. volatile int8_t is_slowsync; /* w: engine, r: engine and client */
  221. volatile int8_t active_slowsync; /* w: engine, r: engine and client */
  222. volatile int8_t sync_poll; /* w: engine and client, r: engine */
  223. volatile int8_t sync_new; /* w: engine and client, r: engine */
  224. volatile pid_t pid; /* w: client r: engine; client pid */
  225. volatile pid_t pgrp; /* w: client r: engine; client pgrp */
  226. volatile uint64_t signalled_at;
  227. volatile uint64_t awake_at;
  228. volatile uint64_t finished_at;
  229. volatile int32_t last_status; /* w: client, r: engine and client */
  230. /* indicators for whether callbacks have been set for this client.
  231. We do not include ptrs to the callbacks here (or their arguments)
  232. so that we can avoid 32/64 bit pointer size mismatches between
  233. the jack server and a client. The pointers are in the client-
  234. local structure which is part of the libjack compiled for
  235. either 32 bit or 64 bit clients.
  236. */
  237. volatile uint8_t process_cbset;
  238. volatile uint8_t thread_init_cbset;
  239. volatile uint8_t bufsize_cbset;
  240. volatile uint8_t srate_cbset;
  241. volatile uint8_t port_register_cbset;
  242. volatile uint8_t port_connect_cbset;
  243. volatile uint8_t graph_order_cbset;
  244. volatile uint8_t xrun_cbset;
  245. volatile uint8_t sync_cb_cbset;
  246. volatile uint8_t timebase_cb_cbset;
  247. volatile uint8_t freewheel_cb_cbset;
  248. volatile uint8_t client_register_cbset;
  249. volatile uint8_t thread_cb_cbset;
  250. volatile uint8_t session_cbset;
  251. volatile uint8_t latency_cbset;
  252. volatile uint8_t property_cbset;
  253. volatile uint8_t port_rename_cbset;
  254. } POST_PACKED_STRUCTURE jack_client_control_t;
  255. typedef struct {
  256. uint32_t protocol_v; /* protocol version, must go first */
  257. int32_t load;
  258. ClientType type;
  259. jack_options_t options;
  260. jack_uuid_t uuid;
  261. char name[JACK_CLIENT_NAME_SIZE];
  262. char object_path[PATH_MAX + 1];
  263. char object_data[1024];
  264. } POST_PACKED_STRUCTURE jack_client_connect_request_t;
  265. typedef struct {
  266. jack_status_t status;
  267. jack_shm_registry_index_t client_shm_index;
  268. jack_shm_registry_index_t engine_shm_index;
  269. char fifo_prefix[PATH_MAX + 1];
  270. int32_t realtime;
  271. int32_t realtime_priority;
  272. char name[JACK_CLIENT_NAME_SIZE]; /* unique name, if assigned */
  273. /* these are actually pointers, but they must
  274. be the same size regardless of whether the
  275. server and/or client are 64 bit or 32 bit.
  276. force them to be 64 bit.
  277. */
  278. uint64_t client_control;
  279. uint64_t engine_control;
  280. #ifdef JACK_USE_MACH_THREADS
  281. /* specific resources for server/client real-time thread communication */
  282. int32_t portnum;
  283. #endif
  284. } POST_PACKED_STRUCTURE jack_client_connect_result_t;
  285. typedef struct {
  286. jack_uuid_t client_id;
  287. } POST_PACKED_STRUCTURE jack_client_connect_ack_request_t;
  288. typedef struct {
  289. int8_t status;
  290. } POST_PACKED_STRUCTURE jack_client_connect_ack_result_t;
  291. typedef enum {
  292. RegisterPort = 1,
  293. UnRegisterPort = 2,
  294. ConnectPorts = 3,
  295. DisconnectPorts = 4,
  296. SetTimeBaseClient = 5,
  297. ActivateClient = 6,
  298. DeactivateClient = 7,
  299. DisconnectPort = 8,
  300. SetClientCapabilities = 9,
  301. GetPortConnections = 10,
  302. GetPortNConnections = 11,
  303. ResetTimeBaseClient = 12,
  304. SetSyncClient = 13,
  305. ResetSyncClient = 14,
  306. SetSyncTimeout = 15,
  307. SetBufferSize = 16,
  308. FreeWheel = 17,
  309. StopFreeWheel = 18,
  310. IntClientHandle = 19,
  311. IntClientLoad = 20,
  312. IntClientName = 21,
  313. IntClientUnload = 22,
  314. RecomputeTotalLatencies = 23,
  315. RecomputeTotalLatency = 24,
  316. SessionNotify = 25,
  317. GetClientByUUID = 26,
  318. GetUUIDByClientName = 27,
  319. ReserveName = 30,
  320. SessionReply = 31,
  321. SessionHasCallback = 32,
  322. PropertyChangeNotify = 33,
  323. PortNameChanged = 34
  324. } RequestType;
  325. struct _jack_request {
  326. //RequestType type;
  327. uint32_t type;
  328. union {
  329. struct {
  330. char name[JACK_PORT_NAME_SIZE];
  331. char type[JACK_PORT_TYPE_SIZE];
  332. uint32_t flags;
  333. jack_shmsize_t buffer_size;
  334. jack_port_id_t port_id;
  335. jack_uuid_t client_id;
  336. } POST_PACKED_STRUCTURE port_info;
  337. struct {
  338. char source_port[JACK_PORT_NAME_SIZE];
  339. char destination_port[JACK_PORT_NAME_SIZE];
  340. } POST_PACKED_STRUCTURE connect;
  341. struct {
  342. char path[JACK_PORT_NAME_SIZE];
  343. jack_session_event_type_t type;
  344. char target[JACK_CLIENT_NAME_SIZE];
  345. } POST_PACKED_STRUCTURE session;
  346. struct {
  347. int32_t nports;
  348. const char **ports; /* this is only exposed to internal clients, so there
  349. is no 64/32 issue. external clients read the ports
  350. one by one from the server, and allocate their
  351. own "ports" array in their own address space.
  352. we are lucky, because this is part of a union
  353. whose other components are bigger than this one.
  354. otherwise it would change structure size when
  355. comparing the 64 and 32 bit versions.
  356. */
  357. } POST_PACKED_STRUCTURE port_connections;
  358. struct {
  359. jack_uuid_t client_id;
  360. int32_t conditional;
  361. } POST_PACKED_STRUCTURE timebase;
  362. struct {
  363. char name[JACK_CLIENT_NAME_SIZE];
  364. jack_uuid_t uuid;
  365. } POST_PACKED_STRUCTURE reservename;
  366. struct {
  367. //jack_options_t options;
  368. uint32_t options;
  369. jack_uuid_t uuid;
  370. char name[JACK_CLIENT_NAME_SIZE];
  371. char path[PATH_MAX + 1];
  372. char init[JACK_LOAD_INIT_LIMIT];
  373. } POST_PACKED_STRUCTURE intclient;
  374. struct {
  375. jack_property_change_t change;
  376. jack_uuid_t uuid;
  377. size_t keylen;
  378. const char* key; /* not delivered inline to server, see oop_client_deliver_request() */
  379. } POST_PACKED_STRUCTURE property;
  380. jack_uuid_t client_id;
  381. jack_nframes_t nframes;
  382. jack_time_t timeout;
  383. pid_t cap_pid;
  384. char name[JACK_CLIENT_NAME_SIZE];
  385. } POST_PACKED_STRUCTURE x;
  386. int32_t status;
  387. } POST_PACKED_STRUCTURE;
  388. /* Per-client structure allocated in the server's address space.
  389. * It's here because its not part of the engine structure.
  390. */
  391. typedef struct _jack_client_internal {
  392. jack_client_control_t *control;
  393. int request_fd;
  394. int event_fd;
  395. int subgraph_start_fd;
  396. int subgraph_wait_fd;
  397. JSList *ports; /* protected by engine->client_lock */
  398. JSList *truefeeds; /* protected by engine->client_lock */
  399. JSList *sortfeeds; /* protected by engine->client_lock */
  400. int fedcount;
  401. int tfedcount;
  402. jack_shm_info_t control_shm;
  403. unsigned long execution_order;
  404. struct _jack_client_internal *next_client; /* not a linked list! */
  405. dlhandle handle;
  406. int (*initialize)(jack_client_t*, const char*); /* int. clients only */
  407. void (*finish)(void *); /* internal clients only */
  408. int error;
  409. int session_reply_pending;
  410. #ifdef JACK_USE_MACH_THREADS
  411. /* specific resources for server/client real-time thread communication */
  412. mach_port_t serverport;
  413. trivial_message message;
  414. int running;
  415. int portnum;
  416. #endif /* JACK_USE_MACH_THREADS */
  417. jack_client_t *private_client;
  418. } jack_client_internal_t;
  419. typedef struct _jack_thread_arg {
  420. jack_client_t* client;
  421. void* (*work_function)(void*);
  422. int priority;
  423. int realtime;
  424. void* arg;
  425. pid_t cap_pid;
  426. } jack_thread_arg_t;
  427. extern int jack_client_handle_port_connection(jack_client_t *client,
  428. jack_event_t *event);
  429. extern jack_client_t *jack_driver_client_new(jack_engine_t *,
  430. const char *client_name);
  431. extern jack_client_t *jack_client_alloc_internal(jack_client_control_t*,
  432. jack_engine_t*);
  433. /* internal clients call this. it's defined in jack/engine.c */
  434. void handle_internal_client_request(jack_control_t*, jack_request_t*);
  435. extern const char *jack_get_tmpdir(void);
  436. extern char *jack_user_dir(void);
  437. extern char *jack_server_dir(const char *server_name, char *server_dir);
  438. extern void *jack_zero_filled_buffer;
  439. extern jack_port_functions_t jack_builtin_audio_functions;
  440. extern jack_port_type_info_t jack_builtin_port_types[];
  441. extern void jack_client_fix_port_buffers(jack_client_t *client);
  442. extern void jack_transport_copy_position(jack_position_t *from,
  443. jack_position_t *to);
  444. extern void jack_call_sync_client(jack_client_t *client);
  445. extern void jack_call_timebase_master(jack_client_t *client);
  446. extern char *jack_default_server_name(void);
  447. void silent_jack_error_callback(const char *desc);
  448. /* needed for port management */
  449. extern jack_port_t *jack_port_by_id_int(const jack_client_t *client,
  450. jack_port_id_t id, int* free);
  451. extern jack_port_t *jack_port_by_name_int(jack_client_t *client,
  452. const char *port_name, int* free);
  453. extern int jack_port_name_equals(jack_port_shared_t* port, const char* target);
  454. /** Get the size (in bytes) of the data structure used to store
  455. * MIDI events internally.
  456. */
  457. extern size_t jack_midi_internal_event_size();
  458. extern int jack_client_handle_latency_callback(jack_client_t *client, jack_event_t *event, int is_driver);
  459. #ifdef __GNUC__
  460. # define likely(x) __builtin_expect ((x), 1)
  461. # define unlikely(x) __builtin_expect ((x), 0)
  462. #else
  463. # define likely(x) (x)
  464. # define unlikely(x) (x)
  465. #endif
  466. #ifdef VALGRIND_CLEAN
  467. #include <string.h>
  468. #define VALGRIND_MEMSET(ptr, val, size) memset ((ptr), (val), (size))
  469. #else
  470. #define VALGRIND_MEMSET(ptr, val, size)
  471. #endif
  472. #endif /* __jack_internal_h__ */