jack2 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.

303 lines
11KB

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