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.

306 lines
9.8KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #ifndef __jack_driver_h__
  16. #define __jack_driver_h__
  17. #include <pthread.h>
  18. #include <jack/types.h>
  19. #include "port.h"
  20. #include "driver_interface.h"
  21. typedef float gain_t;
  22. typedef unsigned long channel_t;
  23. typedef enum {
  24. Lock = 0x1,
  25. NoLock = 0x2,
  26. Sync = 0x4,
  27. NoSync = 0x8
  28. } ClockSyncStatus;
  29. typedef void (*ClockSyncListenerFunction)(channel_t, ClockSyncStatus, void*);
  30. typedef struct {
  31. unsigned long id;
  32. ClockSyncListenerFunction function;
  33. void *arg;
  34. } ClockSyncListener;
  35. struct _jack_engine;
  36. struct _jack_driver;
  37. typedef int (*JackDriverAttachFunction)(struct _jack_driver *,
  38. struct _jack_engine *);
  39. typedef int (*JackDriverDetachFunction)(struct _jack_driver *,
  40. struct _jack_engine *);
  41. typedef int (*JackDriverReadFunction)(struct _jack_driver *,
  42. jack_nframes_t nframes);
  43. typedef int (*JackDriverWriteFunction)(struct _jack_driver *,
  44. jack_nframes_t nframes);
  45. typedef int (*JackDriverNullCycleFunction)(struct _jack_driver *,
  46. jack_nframes_t nframes);
  47. typedef int (*JackDriverStopFunction)(struct _jack_driver *);
  48. typedef int (*JackDriverStartFunction)(struct _jack_driver *);
  49. typedef int (*JackDriverBufSizeFunction)(struct _jack_driver *,
  50. jack_nframes_t nframes);
  51. /*
  52. Call sequence summary:
  53. 1) engine loads driver via runtime dynamic linking
  54. - calls jack_driver_load
  55. - we call dlsym for "driver_initialize" and execute it
  56. 2) engine attaches to driver
  57. 3) engine starts driver
  58. 4) driver runs its own thread, calling
  59. while () {
  60. driver->wait ();
  61. driver->engine->run_cycle ()
  62. }
  63. 5) engine stops driver
  64. 6) engine detaches from driver
  65. 7) engine calls driver `finish' routine
  66. Note that stop/start may be called multiple times in the event of an
  67. error return from the `wait' function.
  68. */
  69. typedef struct _jack_driver {
  70. /* The _jack_driver structure fields are included at the beginning of
  71. each driver-specific structure using the JACK_DRIVER_DECL macro,
  72. which is defined below. The comments that follow describe each
  73. common field.
  74. The driver should set this to be the interval it expects to elapse
  75. between returning from the `wait' function. if set to zero, it
  76. implies that the driver does not expect regular periodic wakeups.
  77. jack_time_t period_usecs;
  78. The driver should set this within its "wait" function to indicate
  79. the UST of the most recent determination that the engine cycle
  80. should run. it should not be set if the "extra_fd" argument of
  81. the wait function is set to a non-zero value.
  82. jack_time_t last_wait_ust;
  83. These are not used by the driver. They should not be written to or
  84. modified in any way
  85. void *handle;
  86. struct _jack_internal_client *internal_client;
  87. This should perform any cleanup associated with the driver. it will
  88. be called when jack server process decides to get rid of the
  89. driver. in some systems, it may not be called at all, so the driver
  90. should never rely on a call to this. it can set it to NULL if
  91. it has nothing do do.
  92. void (*finish)(struct _jack_driver *);
  93. The JACK engine will call this when it wishes to attach itself to
  94. the driver. the engine will pass a pointer to itself, which the driver
  95. may use in anyway it wishes to. the driver may assume that this
  96. is the same engine object that will make `wait' calls until a
  97. `detach' call is made.
  98. JackDriverAttachFunction attach;
  99. The JACK engine will call this when it is finished using a driver.
  100. JackDriverDetachFunction detach;
  101. The JACK engine will call this when it wants to wait until the
  102. driver decides that its time to process some data. the driver returns
  103. a count of the number of audioframes that can be processed.
  104. it should set the variable pointed to by `status' as follows:
  105. zero: the wait completed normally, processing may begin
  106. negative: the wait failed, and recovery is not possible
  107. positive: the wait failed, and the driver stopped itself.
  108. a call to `start' will return the driver to
  109. a correct and known state.
  110. the driver should also fill out the `delayed_usecs' variable to
  111. indicate any delay in its expected periodic execution. for example,
  112. if it discovers that its return from poll(2) is later than it
  113. expects it to be, it would place an estimate of the delay
  114. in this variable. the engine will use this to decide if it
  115. plans to continue execution.
  116. JackDriverWaitFunction wait;
  117. The JACK engine will call this to ask the driver to move
  118. data from its inputs to its output port buffers. it should
  119. return 0 to indicate successful completion, negative otherwise.
  120. This function will always be called after the wait function (above).
  121. JackDriverReadFunction read;
  122. The JACK engine will call this to ask the driver to move
  123. data from its input port buffers to its outputs. it should
  124. return 0 to indicate successful completion, negative otherwise.
  125. this function will always be called after the read function (above).
  126. JackDriverWriteFunction write;
  127. The JACK engine will call this after the wait function (above) has
  128. been called, but for some reason the engine is unable to execute
  129. a full "cycle". the driver should do whatever is necessary to
  130. keep itself running correctly, but cannot reference ports
  131. or other JACK data structures in any way.
  132. JackDriverNullCycleFunction null_cycle;
  133. The engine will call this when it plans to stop calling the `wait'
  134. function for some period of time. the driver should take
  135. appropriate steps to handle this (possibly no steps at all).
  136. NOTE: the driver must silence its capture buffers (if any)
  137. from within this function or the function that actually
  138. implements the change in state.
  139. JackDriverStopFunction stop;
  140. The engine will call this to let the driver know that it plans
  141. to start calling the `wait' function on a regular basis. the driver
  142. should take any appropriate steps to handle this (possibly no steps
  143. at all). NOTE: The driver may wish to silence its playback buffers
  144. (if any) from within this function or the function that actually
  145. implements the change in state.
  146. JackDriverStartFunction start;
  147. The engine will call this to let the driver know that some client
  148. has requested a new buffer size. The stop function will be called
  149. prior to this, and the start function after this one has returned.
  150. JackDriverBufSizeFunction bufsize;
  151. */
  152. /* define the fields here... */
  153. #define JACK_DRIVER_DECL \
  154. jack_time_t period_usecs; \
  155. jack_time_t last_wait_ust; \
  156. void *handle; \
  157. struct _jack_client_internal * internal_client; \
  158. void (*finish)(struct _jack_driver *); \
  159. JackDriverAttachFunction attach; \
  160. JackDriverDetachFunction detach; \
  161. JackDriverReadFunction read; \
  162. JackDriverWriteFunction write; \
  163. JackDriverNullCycleFunction null_cycle; \
  164. JackDriverStopFunction stop; \
  165. JackDriverStartFunction start; \
  166. JackDriverBufSizeFunction bufsize;
  167. JACK_DRIVER_DECL /* expand the macro */
  168. } jack_driver_t;
  169. typedef jack_driver_desc_t * (*JackDriverDescFunction)();
  170. void jack_driver_init(jack_driver_t *);
  171. void jack_driver_release(jack_driver_t *);
  172. jack_driver_t *jack_driver_load(int argc, char **argv);
  173. void jack_driver_unload(jack_driver_t *);
  174. /****************************
  175. *** Non-Threaded Drivers ***
  176. ****************************/
  177. /*
  178. Call sequence summary:
  179. 1) engine loads driver via runtime dynamic linking
  180. - calls jack_driver_load
  181. - we call dlsym for "driver_initialize" and execute it
  182. - driver_initialize calls jack_driver_nt_init
  183. 2) nt layer attaches to driver
  184. 3) nt layer starts driver
  185. 4) nt layer runs a thread, calling
  186. while () {
  187. driver->nt_run_ctcle();
  188. }
  189. 5) nt layer stops driver
  190. 6) nt layer detaches driver
  191. 7) engine calls driver `finish' routine which calls jack_driver_nt_finish
  192. Note that stop/start may be called multiple times in the event of an
  193. error return from the `wait' function.
  194. */
  195. struct _jack_driver_nt;
  196. typedef int (*JackDriverNTAttachFunction)(struct _jack_driver_nt *);
  197. typedef int (*JackDriverNTDetachFunction)(struct _jack_driver_nt *);
  198. typedef int (*JackDriverNTStopFunction)(struct _jack_driver_nt *);
  199. typedef int (*JackDriverNTStartFunction)(struct _jack_driver_nt *);
  200. typedef int (*JackDriverNTBufSizeFunction)(struct _jack_driver_nt *,
  201. jack_nframes_t nframes);
  202. typedef int (*JackDriverNTRunCycleFunction)(struct _jack_driver_nt *);
  203. typedef struct _jack_driver_nt {
  204. #define JACK_DRIVER_NT_DECL \
  205. JACK_DRIVER_DECL \
  206. struct _jack_engine * engine; \
  207. volatile int nt_run; \
  208. pthread_t nt_thread; \
  209. pthread_mutex_t nt_run_lock; \
  210. JackDriverNTAttachFunction nt_attach; \
  211. JackDriverNTDetachFunction nt_detach; \
  212. JackDriverNTStopFunction nt_stop; \
  213. JackDriverNTStartFunction nt_start; \
  214. JackDriverNTBufSizeFunction nt_bufsize; \
  215. JackDriverNTRunCycleFunction nt_run_cycle;
  216. #define nt_read read
  217. #define nt_write write
  218. #define nt_null_cycle null_cycle
  219. JACK_DRIVER_NT_DECL
  220. } jack_driver_nt_t;
  221. void jack_driver_nt_init(jack_driver_nt_t * driver);
  222. void jack_driver_nt_finish(jack_driver_nt_t * driver);
  223. #endif /* __jack_driver_h__ */