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
10.0KB

  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. These are not used by the driver. They should not be written to or
  85. modified in any way
  86. void *handle;
  87. struct _jack_internal_client *internal_client;
  88. This should perform any cleanup associated with the driver. it will
  89. be called when jack server process decides to get rid of the
  90. driver. in some systems, it may not be called at all, so the driver
  91. should never rely on a call to this. it can set it to NULL if
  92. it has nothing do do.
  93. void (*finish)(struct _jack_driver *);
  94. The JACK engine will call this when it wishes to attach itself to
  95. the driver. the engine will pass a pointer to itself, which the driver
  96. may use in anyway it wishes to. the driver may assume that this
  97. is the same engine object that will make `wait' calls until a
  98. `detach' call is made.
  99. JackDriverAttachFunction attach;
  100. The JACK engine will call this when it is finished using a driver.
  101. JackDriverDetachFunction detach;
  102. The JACK engine will call this when it wants to wait until the
  103. driver decides that its time to process some data. the driver returns
  104. a count of the number of audioframes that can be processed.
  105. it should set the variable pointed to by `status' as follows:
  106. zero: the wait completed normally, processing may begin
  107. negative: the wait failed, and recovery is not possible
  108. positive: the wait failed, and the driver stopped itself.
  109. a call to `start' will return the driver to
  110. a correct and known state.
  111. the driver should also fill out the `delayed_usecs' variable to
  112. indicate any delay in its expected periodic execution. for example,
  113. if it discovers that its return from poll(2) is later than it
  114. expects it to be, it would place an estimate of the delay
  115. in this variable. the engine will use this to decide if it
  116. plans to continue execution.
  117. JackDriverWaitFunction wait;
  118. The JACK engine will call this to ask the driver to move
  119. data from its inputs to its output port buffers. it should
  120. return 0 to indicate successful completion, negative otherwise.
  121. This function will always be called after the wait function (above).
  122. JackDriverReadFunction read;
  123. The JACK engine will call this to ask the driver to move
  124. data from its input port buffers to its outputs. it should
  125. return 0 to indicate successful completion, negative otherwise.
  126. this function will always be called after the read function (above).
  127. JackDriverWriteFunction write;
  128. The JACK engine will call this after the wait function (above) has
  129. been called, but for some reason the engine is unable to execute
  130. a full "cycle". the driver should do whatever is necessary to
  131. keep itself running correctly, but cannot reference ports
  132. or other JACK data structures in any way.
  133. JackDriverNullCycleFunction null_cycle;
  134. The engine will call this when it plans to stop calling the `wait'
  135. function for some period of time. the driver should take
  136. appropriate steps to handle this (possibly no steps at all).
  137. NOTE: the driver must silence its capture buffers (if any)
  138. from within this function or the function that actually
  139. implements the change in state.
  140. JackDriverStopFunction stop;
  141. The engine will call this to let the driver know that it plans
  142. to start calling the `wait' function on a regular basis. the driver
  143. should take any appropriate steps to handle this (possibly no steps
  144. at all). NOTE: The driver may wish to silence its playback buffers
  145. (if any) from within this function or the function that actually
  146. implements the change in state.
  147. JackDriverStartFunction start;
  148. The engine will call this to let the driver know that some client
  149. has requested a new buffer size. The stop function will be called
  150. prior to this, and the start function after this one has returned.
  151. JackDriverBufSizeFunction bufsize;
  152. */
  153. /* define the fields here... */
  154. #define JACK_DRIVER_DECL \
  155. jack_time_t period_usecs; \
  156. jack_time_t last_wait_ust; \
  157. void *handle; \
  158. struct _jack_client_internal * internal_client; \
  159. void (*finish)(struct _jack_driver *);\
  160. JackDriverAttachFunction attach; \
  161. JackDriverDetachFunction detach; \
  162. JackDriverReadFunction read; \
  163. JackDriverWriteFunction write; \
  164. JackDriverNullCycleFunction null_cycle; \
  165. JackDriverStopFunction stop; \
  166. JackDriverStartFunction start; \
  167. JackDriverBufSizeFunction bufsize;
  168. JACK_DRIVER_DECL /* expand the macro */
  169. } jack_driver_t;
  170. typedef jack_driver_desc_t * (*JackDriverDescFunction) ();
  171. void jack_driver_init (jack_driver_t *);
  172. void jack_driver_release (jack_driver_t *);
  173. jack_driver_t *jack_driver_load (int argc, char **argv);
  174. void jack_driver_unload (jack_driver_t *);
  175. /****************************
  176. *** Non-Threaded Drivers ***
  177. ****************************/
  178. /*
  179. Call sequence summary:
  180. 1) engine loads driver via runtime dynamic linking
  181. - calls jack_driver_load
  182. - we call dlsym for "driver_initialize" and execute it
  183. - driver_initialize calls jack_driver_nt_init
  184. 2) nt layer attaches to driver
  185. 3) nt layer starts driver
  186. 4) nt layer runs a thread, calling
  187. while () {
  188. driver->nt_run_ctcle();
  189. }
  190. 5) nt layer stops driver
  191. 6) nt layer detaches driver
  192. 7) engine calls driver `finish' routine which calls jack_driver_nt_finish
  193. Note that stop/start may be called multiple times in the event of an
  194. error return from the `wait' function.
  195. */
  196. struct _jack_driver_nt;
  197. typedef int (*JackDriverNTAttachFunction)(struct _jack_driver_nt *);
  198. typedef int (*JackDriverNTDetachFunction)(struct _jack_driver_nt *);
  199. typedef int (*JackDriverNTStopFunction)(struct _jack_driver_nt *);
  200. typedef int (*JackDriverNTStartFunction)(struct _jack_driver_nt *);
  201. typedef int (*JackDriverNTBufSizeFunction)(struct _jack_driver_nt *,
  202. jack_nframes_t nframes);
  203. typedef int (*JackDriverNTRunCycleFunction)(struct _jack_driver_nt *);
  204. typedef struct _jack_driver_nt {
  205. #define JACK_DRIVER_NT_DECL \
  206. JACK_DRIVER_DECL \
  207. struct _jack_engine * engine; \
  208. volatile int nt_run; \
  209. pthread_t nt_thread; \
  210. pthread_mutex_t nt_run_lock; \
  211. JackDriverNTAttachFunction nt_attach; \
  212. JackDriverNTDetachFunction nt_detach; \
  213. JackDriverNTStopFunction nt_stop; \
  214. JackDriverNTStartFunction nt_start; \
  215. JackDriverNTBufSizeFunction nt_bufsize; \
  216. JackDriverNTRunCycleFunction nt_run_cycle;
  217. #define nt_read read
  218. #define nt_write write
  219. #define nt_null_cycle null_cycle
  220. JACK_DRIVER_NT_DECL
  221. } jack_driver_nt_t;
  222. void jack_driver_nt_init (jack_driver_nt_t * driver);
  223. void jack_driver_nt_finish (jack_driver_nt_t * driver);
  224. #endif /* __jack_driver_h__ */