pa_win_wasapi.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #ifndef PA_WIN_WASAPI_H
  2. #define PA_WIN_WASAPI_H
  3. /*
  4. * $Id: $
  5. * PortAudio Portable Real-Time Audio Library
  6. * DirectSound specific extensions
  7. *
  8. * Copyright (c) 1999-2007 Ross Bencina and Phil Burk
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining
  11. * a copy of this software and associated documentation files
  12. * (the "Software"), to deal in the Software without restriction,
  13. * including without limitation the rights to use, copy, modify, merge,
  14. * publish, distribute, sublicense, and/or sell copies of the Software,
  15. * and to permit persons to whom the Software is furnished to do so,
  16. * subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be
  19. * included in all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  24. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  25. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  26. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. */
  29. /*
  30. * The text above constitutes the entire PortAudio license; however,
  31. * the PortAudio community also makes the following non-binding requests:
  32. *
  33. * Any person wishing to distribute modifications to the Software is
  34. * requested to send the modifications to the original developer so that
  35. * they can be incorporated into the canonical version. It is also
  36. * requested that these non-binding requests be included along with the
  37. * license above.
  38. */
  39. /** @file
  40. @ingroup public_header
  41. @brief WASAPI-specific PortAudio API extension header file.
  42. */
  43. #include "portaudio.h"
  44. #include "pa_win_waveformat.h"
  45. #ifdef __cplusplus
  46. extern "C"
  47. {
  48. #endif /* __cplusplus */
  49. /* Setup flags */
  50. typedef enum PaWasapiFlags
  51. {
  52. /* puts WASAPI into exclusive mode */
  53. paWinWasapiExclusive = (1 << 0),
  54. /* allows to skip internal PA processing completely */
  55. paWinWasapiRedirectHostProcessor = (1 << 1),
  56. /* assigns custom channel mask */
  57. paWinWasapiUseChannelMask = (1 << 2),
  58. /* selects non-Event driven method of data read/write
  59. Note: WASAPI Event driven core is capable of 2ms latency!!!, but Polling
  60. method can only provide 15-20ms latency. */
  61. paWinWasapiPolling = (1 << 3),
  62. /* forces custom thread priority setting. must be used if PaWasapiStreamInfo::threadPriority
  63. is set to custom value. */
  64. paWinWasapiThreadPriority = (1 << 4)
  65. }
  66. PaWasapiFlags;
  67. #define paWinWasapiExclusive (paWinWasapiExclusive)
  68. #define paWinWasapiRedirectHostProcessor (paWinWasapiRedirectHostProcessor)
  69. #define paWinWasapiUseChannelMask (paWinWasapiUseChannelMask)
  70. #define paWinWasapiPolling (paWinWasapiPolling)
  71. #define paWinWasapiThreadPriority (paWinWasapiThreadPriority)
  72. /* Host processor. Allows to skip internal PA processing completely.
  73. You must set paWinWasapiRedirectHostProcessor flag to PaWasapiStreamInfo::flags member
  74. in order to have host processor redirected to your callback.
  75. Use with caution! inputFrames and outputFrames depend solely on final device setup.
  76. To query maximal values of inputFrames/outputFrames use PaWasapi_GetFramesPerHostBuffer.
  77. */
  78. typedef void (*PaWasapiHostProcessorCallback) (void *inputBuffer, long inputFrames,
  79. void *outputBuffer, long outputFrames,
  80. void *userData);
  81. /* Device role */
  82. typedef enum PaWasapiDeviceRole
  83. {
  84. eRoleRemoteNetworkDevice = 0,
  85. eRoleSpeakers,
  86. eRoleLineLevel,
  87. eRoleHeadphones,
  88. eRoleMicrophone,
  89. eRoleHeadset,
  90. eRoleHandset,
  91. eRoleUnknownDigitalPassthrough,
  92. eRoleSPDIF,
  93. eRoleHDMI,
  94. eRoleUnknownFormFactor
  95. }
  96. PaWasapiDeviceRole;
  97. /* Jack connection type */
  98. typedef enum PaWasapiJackConnectionType
  99. {
  100. eJackConnTypeUnknown,
  101. eJackConnType3Point5mm,
  102. eJackConnTypeQuarter,
  103. eJackConnTypeAtapiInternal,
  104. eJackConnTypeRCA,
  105. eJackConnTypeOptical,
  106. eJackConnTypeOtherDigital,
  107. eJackConnTypeOtherAnalog,
  108. eJackConnTypeMultichannelAnalogDIN,
  109. eJackConnTypeXlrProfessional,
  110. eJackConnTypeRJ11Modem,
  111. eJackConnTypeCombination
  112. }
  113. PaWasapiJackConnectionType;
  114. /* Jack geometric location */
  115. typedef enum PaWasapiJackGeoLocation
  116. {
  117. eJackGeoLocUnk = 0,
  118. eJackGeoLocRear = 0x1, /* matches EPcxGeoLocation::eGeoLocRear */
  119. eJackGeoLocFront,
  120. eJackGeoLocLeft,
  121. eJackGeoLocRight,
  122. eJackGeoLocTop,
  123. eJackGeoLocBottom,
  124. eJackGeoLocRearPanel,
  125. eJackGeoLocRiser,
  126. eJackGeoLocInsideMobileLid,
  127. eJackGeoLocDrivebay,
  128. eJackGeoLocHDMI,
  129. eJackGeoLocOutsideMobileLid,
  130. eJackGeoLocATAPI,
  131. eJackGeoLocReserved5,
  132. eJackGeoLocReserved6,
  133. }
  134. PaWasapiJackGeoLocation;
  135. /* Jack general location */
  136. typedef enum PaWasapiJackGenLocation
  137. {
  138. eJackGenLocPrimaryBox = 0,
  139. eJackGenLocInternal,
  140. eJackGenLocSeparate,
  141. eJackGenLocOther
  142. }
  143. PaWasapiJackGenLocation;
  144. /* Jack's type of port */
  145. typedef enum PaWasapiJackPortConnection
  146. {
  147. eJackPortConnJack = 0,
  148. eJackPortConnIntegratedDevice,
  149. eJackPortConnBothIntegratedAndJack,
  150. eJackPortConnUnknown
  151. }
  152. PaWasapiJackPortConnection;
  153. /* Thread priority */
  154. typedef enum PaWasapiThreadPriority
  155. {
  156. eThreadPriorityNone = 0,
  157. eThreadPriorityAudio, //!< Default for Shared mode.
  158. eThreadPriorityCapture,
  159. eThreadPriorityDistribution,
  160. eThreadPriorityGames,
  161. eThreadPriorityPlayback,
  162. eThreadPriorityProAudio, //!< Default for Exclusive mode.
  163. eThreadPriorityWindowManager
  164. }
  165. PaWasapiThreadPriority;
  166. /* Stream descriptor. */
  167. typedef struct PaWasapiJackDescription
  168. {
  169. unsigned long channelMapping;
  170. unsigned long color; /* derived from macro: #define RGB(r,g,b) ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16))) */
  171. PaWasapiJackConnectionType connectionType;
  172. PaWasapiJackGeoLocation geoLocation;
  173. PaWasapiJackGenLocation genLocation;
  174. PaWasapiJackPortConnection portConnection;
  175. unsigned int isConnected;
  176. }
  177. PaWasapiJackDescription;
  178. /* Stream descriptor. */
  179. typedef struct PaWasapiStreamInfo
  180. {
  181. unsigned long size; /**< sizeof(PaWasapiStreamInfo) */
  182. PaHostApiTypeId hostApiType; /**< paWASAPI */
  183. unsigned long version; /**< 1 */
  184. unsigned long flags; /**< collection of PaWasapiFlags */
  185. /* Support for WAVEFORMATEXTENSIBLE channel masks. If flags contains
  186. paWinWasapiUseChannelMask this allows you to specify which speakers
  187. to address in a multichannel stream. Constants for channelMask
  188. are specified in pa_win_waveformat.h. Will be used only if
  189. paWinWasapiUseChannelMask flag is specified.
  190. */
  191. PaWinWaveFormatChannelMask channelMask;
  192. /* Delivers raw data to callback obtained from GetBuffer() methods skipping
  193. internal PortAudio processing inventory completely. userData parameter will
  194. be the same that was passed to Pa_OpenStream method. Will be used only if
  195. paWinWasapiRedirectHostProcessor flag is specified.
  196. */
  197. PaWasapiHostProcessorCallback hostProcessorOutput;
  198. PaWasapiHostProcessorCallback hostProcessorInput;
  199. /* Specifies thread priority explicitly. Will be used only if paWinWasapiThreadPriority flag
  200. is specified.
  201. Please note, if Input/Output streams are opened simultaniously (Full-Duplex mode)
  202. you shall specify same value for threadPriority or othervise one of the values will be used
  203. to setup thread priority.
  204. */
  205. PaWasapiThreadPriority threadPriority;
  206. }
  207. PaWasapiStreamInfo;
  208. /** Returns default sound format for device. Format is represented by PaWinWaveFormat or
  209. WAVEFORMATEXTENSIBLE structure.
  210. @param pFormat Pointer to PaWinWaveFormat or WAVEFORMATEXTENSIBLE structure.
  211. @param nFormatSize Size of PaWinWaveFormat or WAVEFORMATEXTENSIBLE structure in bytes.
  212. @param nDevice Device index.
  213. @return Non-negative value indicating the number of bytes copied into format decriptor
  214. or, a PaErrorCode (which are always negative) if PortAudio is not initialized
  215. or an error is encountered.
  216. */
  217. int PaWasapi_GetDeviceDefaultFormat( void *pFormat, unsigned int nFormatSize, PaDeviceIndex nDevice );
  218. /** Returns device role (PaWasapiDeviceRole enum).
  219. @param nDevice device index.
  220. @return Non-negative value indicating device role or, a PaErrorCode (which are always negative)
  221. if PortAudio is not initialized or an error is encountered.
  222. */
  223. int/*PaWasapiDeviceRole*/ PaWasapi_GetDeviceRole( PaDeviceIndex nDevice );
  224. /** Boost thread priority of calling thread (MMCSS). Use it for Blocking Interface only for thread
  225. which makes calls to Pa_WriteStream/Pa_ReadStream.
  226. @param hTask Handle to pointer to priority task. Must be used with PaWasapi_RevertThreadPriority
  227. method to revert thread priority to initial state.
  228. @param nPriorityClass Id of thread priority of PaWasapiThreadPriority type. Specifying
  229. eThreadPriorityNone does nothing.
  230. @return Error code indicating success or failure.
  231. @see PaWasapi_RevertThreadPriority
  232. */
  233. PaError PaWasapi_ThreadPriorityBoost( void **hTask, PaWasapiThreadPriority nPriorityClass );
  234. /** Boost thread priority of calling thread (MMCSS). Use it for Blocking Interface only for thread
  235. which makes calls to Pa_WriteStream/Pa_ReadStream.
  236. @param hTask Task handle obtained by PaWasapi_BoostThreadPriority method.
  237. @return Error code indicating success or failure.
  238. @see PaWasapi_BoostThreadPriority
  239. */
  240. PaError PaWasapi_ThreadPriorityRevert( void *hTask );
  241. /** Get number of frames per host buffer. This is maximal value of frames of WASAPI buffer which
  242. can be locked for operations. Use this method as helper to findout maximal values of
  243. inputFrames/outputFrames of PaWasapiHostProcessorCallback.
  244. @param pStream Pointer to PaStream to query.
  245. @param nInput Pointer to variable to receive number of input frames. Can be NULL.
  246. @param nOutput Pointer to variable to receive number of output frames. Can be NULL.
  247. @return Error code indicating success or failure.
  248. @see PaWasapiHostProcessorCallback
  249. */
  250. PaError PaWasapi_GetFramesPerHostBuffer( PaStream *pStream, unsigned int *nInput, unsigned int *nOutput );
  251. /** Get number of jacks associated with a WASAPI device. Use this method to determine if
  252. there are any jacks associated with the provided WASAPI device. Not all audio devices
  253. will support this capability. This is valid for both input and output devices.
  254. @param nDevice device index.
  255. @param jcount Number of jacks is returned in this variable
  256. @return Error code indicating success or failure
  257. @see PaWasapi_GetJackDescription
  258. */
  259. PaError PaWasapi_GetJackCount(PaDeviceIndex nDevice, int *jcount);
  260. /** Get the jack description associated with a WASAPI device and jack number
  261. Before this function is called, use PaWasapi_GetJackCount to determine the
  262. number of jacks associated with device. If jcount is greater than zero, then
  263. each jack from 0 to jcount can be queried with this function to get the jack
  264. description.
  265. @param nDevice device index.
  266. @param jindex Which jack to return information
  267. @param KSJACK_DESCRIPTION This structure filled in on success.
  268. @return Error code indicating success or failure
  269. @see PaWasapi_GetJackCount
  270. */
  271. PaError PaWasapi_GetJackDescription(PaDeviceIndex nDevice, int jindex, PaWasapiJackDescription *pJackDescription);
  272. /*
  273. IMPORTANT:
  274. WASAPI is implemented for Callback and Blocking interfaces. It supports Shared and Exclusive
  275. share modes.
  276. Exclusive Mode:
  277. Exclusive mode allows to deliver audio data directly to hardware bypassing
  278. software mixing.
  279. Exclusive mode is specified by 'paWinWasapiExclusive' flag.
  280. Callback Interface:
  281. Provides best audio quality with low latency. Callback interface is implemented in
  282. two versions:
  283. 1) Event-Driven:
  284. This is the most powerful WASAPI implementation which provides glitch-free
  285. audio at around 3ms latency in Exclusive mode. Lowest possible latency for this mode is
  286. 3 ms for HD Audio class audio chips. For the Shared mode latency can not be
  287. lower than 20 ms.
  288. 2) Poll-Driven:
  289. Polling is another 2-nd method to operate with WASAPI. It is less efficient than Event-Driven
  290. and provides latency at around 10-13ms. Polling must be used to overcome a system bug
  291. under Windows Vista x64 when application is WOW64(32-bit) and Event-Driven method simply
  292. times out (event handle is never signalled on buffer completion). Please note, such WOW64 bug
  293. does not exist in Vista x86 or Windows 7.
  294. Polling can be setup by speciying 'paWinWasapiPolling' flag. Our WASAPI implementation detects
  295. WOW64 bug and sets 'paWinWasapiPolling' automatically.
  296. Thread priority:
  297. Normally thread priority is set automatically and does not require modification. Although
  298. if user wants some tweaking thread priority can be modified by setting 'paWinWasapiThreadPriority'
  299. flag and specifying 'PaWasapiStreamInfo::threadPriority' with value from PaWasapiThreadPriority
  300. enum.
  301. Blocking Interface:
  302. Blocking interface is implemented but due to above described Poll-Driven method can not
  303. deliver lowest possible latency. Specifying too low latency in Shared mode will result in
  304. distorted audio although Exclusive mode adds stability.
  305. Pa_IsFormatSupported:
  306. To check format with correct Share Mode (Exclusive/Shared) you must supply
  307. PaWasapiStreamInfo with flags paWinWasapiExclusive set through member of
  308. PaStreamParameters::hostApiSpecificStreamInfo structure.
  309. Pa_OpenStream:
  310. To set desired Share Mode (Exclusive/Shared) you must supply
  311. PaWasapiStreamInfo with flags paWinWasapiExclusive set through member of
  312. PaStreamParameters::hostApiSpecificStreamInfo structure.
  313. */
  314. #ifdef __cplusplus
  315. }
  316. #endif /* __cplusplus */
  317. #endif /* PA_WIN_WASAPI_H */