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.

305 lines
9.9KB

  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. $Id$
  15. */
  16. #ifndef __jack_driver_h__
  17. #define __jack_driver_h__
  18. #include <pthread.h>
  19. #include <jack/types.h>
  20. #include <jack/port.h>
  21. #include <jack/driver_interface.h>
  22. typedef float gain_t;
  23. typedef long channel_t;
  24. typedef enum {
  25. Lock = 0x1,
  26. NoLock = 0x2,
  27. Sync = 0x4,
  28. NoSync = 0x8
  29. } ClockSyncStatus;
  30. typedef void (*ClockSyncListenerFunction)(channel_t,ClockSyncStatus,void*);
  31. typedef struct {
  32. unsigned long id;
  33. ClockSyncListenerFunction function;
  34. void *arg;
  35. } ClockSyncListener;
  36. struct _jack_engine;
  37. struct _jack_driver;
  38. typedef int (*JackDriverAttachFunction)(struct _jack_driver *,
  39. struct _jack_engine *);
  40. typedef int (*JackDriverDetachFunction)(struct _jack_driver *,
  41. struct _jack_engine *);
  42. typedef int (*JackDriverReadFunction)(struct _jack_driver *,
  43. jack_nframes_t nframes);
  44. typedef int (*JackDriverWriteFunction)(struct _jack_driver *,
  45. jack_nframes_t nframes);
  46. typedef int (*JackDriverNullCycleFunction)(struct _jack_driver *,
  47. jack_nframes_t nframes);
  48. typedef int (*JackDriverStopFunction)(struct _jack_driver *);
  49. typedef int (*JackDriverStartFunction)(struct _jack_driver *);
  50. typedef int (*JackDriverBufSizeFunction)(struct _jack_driver *,
  51. jack_nframes_t nframes);
  52. /*
  53. Call sequence summary:
  54. 1) engine loads driver via runtime dynamic linking
  55. - calls jack_driver_load
  56. - we call dlsym for "driver_initialize" and execute it
  57. 2) engine attaches to driver
  58. 3) engine starts driver
  59. 4) driver runs its own thread, calling
  60. while () {
  61. driver->wait ();
  62. driver->engine->run_cycle ()
  63. }
  64. 5) engine stops driver
  65. 6) engine detaches from driver
  66. 7) engine calls driver `finish' routine
  67. Note that stop/start may be called multiple times in the event of an
  68. error return from the `wait' function.
  69. */
  70. typedef struct _jack_driver {
  71. /* The _jack_driver structure fields are included at the beginning of
  72. each driver-specific structure using the JACK_DRIVER_DECL macro,
  73. which is defined below. The comments that follow describe each
  74. common field.
  75. The driver should set this to be the interval it expects to elapse
  76. between returning from the `wait' function. if set to zero, it
  77. implies that the driver does not expect regular periodic wakeups.
  78. jack_time_t period_usecs;
  79. The driver should set this within its "wait" function to indicate
  80. the UST of the most recent determination that the engine cycle
  81. should run. it should not be set if the "extra_fd" argument of
  82. the wait function is set to a non-zero value.
  83. jack_time_t last_wait_ust;
  84. This is not used by the driver. it should not be written to or
  85. modified in any way
  86. void *handle;
  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. void (*finish)(struct _jack_driver *);\
  158. JackDriverAttachFunction attach; \
  159. JackDriverDetachFunction detach; \
  160. JackDriverReadFunction read; \
  161. JackDriverWriteFunction write; \
  162. JackDriverNullCycleFunction null_cycle; \
  163. JackDriverStopFunction stop; \
  164. JackDriverStartFunction start; \
  165. JackDriverBufSizeFunction bufsize;
  166. JACK_DRIVER_DECL /* expand the macro */
  167. } jack_driver_t;
  168. typedef jack_driver_desc_t * (*JackDriverDescFunction) ();
  169. void jack_driver_init (jack_driver_t *);
  170. void jack_driver_release (jack_driver_t *);
  171. jack_driver_t *jack_driver_load (int argc, char **argv);
  172. void jack_driver_unload (jack_driver_t *);
  173. /****************************
  174. *** Non-Threaded Drivers ***
  175. ****************************/
  176. /*
  177. Call sequence summary:
  178. 1) engine loads driver via runtime dynamic linking
  179. - calls jack_driver_load
  180. - we call dlsym for "driver_initialize" and execute it
  181. - driver_initialize calls jack_driver_nt_init
  182. 2) nt layer attaches to driver
  183. 3) nt layer starts driver
  184. 4) nt layer runs a thread, calling
  185. while () {
  186. driver->nt_run_ctcle();
  187. }
  188. 5) nt layer stops driver
  189. 6) nt layer detaches driver
  190. 7) engine calls driver `finish' routine which calls jack_driver_nt_finish
  191. Note that stop/start may be called multiple times in the event of an
  192. error return from the `wait' function.
  193. */
  194. struct _jack_driver_nt;
  195. typedef int (*JackDriverNTAttachFunction)(struct _jack_driver_nt *);
  196. typedef int (*JackDriverNTDetachFunction)(struct _jack_driver_nt *);
  197. typedef int (*JackDriverNTStopFunction)(struct _jack_driver_nt *);
  198. typedef int (*JackDriverNTStartFunction)(struct _jack_driver_nt *);
  199. typedef int (*JackDriverNTBufSizeFunction)(struct _jack_driver_nt *,
  200. jack_nframes_t nframes);
  201. typedef int (*JackDriverNTRunCycleFunction)(struct _jack_driver_nt *);
  202. typedef struct _jack_driver_nt {
  203. #define JACK_DRIVER_NT_DECL \
  204. JACK_DRIVER_DECL \
  205. struct _jack_engine * engine; \
  206. volatile int nt_run; \
  207. pthread_t nt_thread; \
  208. pthread_mutex_t nt_run_lock; \
  209. JackDriverNTAttachFunction nt_attach; \
  210. JackDriverNTDetachFunction nt_detach; \
  211. JackDriverNTStopFunction nt_stop; \
  212. JackDriverNTStartFunction nt_start; \
  213. JackDriverNTBufSizeFunction nt_bufsize; \
  214. JackDriverNTRunCycleFunction nt_run_cycle;
  215. #define nt_read read
  216. #define nt_write write
  217. #define nt_null_cycle null_cycle
  218. JACK_DRIVER_NT_DECL
  219. } jack_driver_nt_t;
  220. void jack_driver_nt_init (jack_driver_nt_t * driver);
  221. void jack_driver_nt_finish (jack_driver_nt_t * driver);
  222. #endif /* __jack_driver_h__ */