sndio.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2007 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "alMain.h"
  25. #include "AL/al.h"
  26. #include "AL/alc.h"
  27. #include <sndio.h>
  28. static const ALCchar sndio_device[] = "SndIO Default";
  29. #ifdef HAVE_DYNLOAD
  30. static void *sndio_handle;
  31. #define MAKE_FUNC(x) static typeof(x) * p##x
  32. MAKE_FUNC(sio_initpar);
  33. MAKE_FUNC(sio_open);
  34. MAKE_FUNC(sio_close);
  35. MAKE_FUNC(sio_setpar);
  36. MAKE_FUNC(sio_getpar);
  37. MAKE_FUNC(sio_getcap);
  38. MAKE_FUNC(sio_onmove);
  39. MAKE_FUNC(sio_write);
  40. MAKE_FUNC(sio_read);
  41. MAKE_FUNC(sio_start);
  42. MAKE_FUNC(sio_stop);
  43. MAKE_FUNC(sio_nfds);
  44. MAKE_FUNC(sio_pollfd);
  45. MAKE_FUNC(sio_revents);
  46. MAKE_FUNC(sio_eof);
  47. MAKE_FUNC(sio_setvol);
  48. MAKE_FUNC(sio_onvol);
  49. #define sio_initpar psio_initpar
  50. #define sio_open psio_open
  51. #define sio_close psio_close
  52. #define sio_setpar psio_setpar
  53. #define sio_getpar psio_getpar
  54. #define sio_getcap psio_getcap
  55. #define sio_onmove psio_onmove
  56. #define sio_write psio_write
  57. #define sio_read psio_read
  58. #define sio_start psio_start
  59. #define sio_stop psio_stop
  60. #define sio_nfds psio_nfds
  61. #define sio_pollfd psio_pollfd
  62. #define sio_revents psio_revents
  63. #define sio_eof psio_eof
  64. #define sio_setvol psio_setvol
  65. #define sio_onvol psio_onvol
  66. #endif
  67. static ALCboolean sndio_load(void)
  68. {
  69. #ifdef HAVE_DYNLOAD
  70. if(!sndio_handle)
  71. {
  72. sndio_handle = LoadLib("libsndio.so");
  73. if(!sndio_handle)
  74. return ALC_FALSE;
  75. #define LOAD_FUNC(f) do { \
  76. p##f = GetSymbol(sndio_handle, #f); \
  77. if(p##f == NULL) { \
  78. CloseLib(sndio_handle); \
  79. sndio_handle = NULL; \
  80. return ALC_FALSE; \
  81. } \
  82. } while(0)
  83. LOAD_FUNC(sio_initpar);
  84. LOAD_FUNC(sio_open);
  85. LOAD_FUNC(sio_close);
  86. LOAD_FUNC(sio_setpar);
  87. LOAD_FUNC(sio_getpar);
  88. LOAD_FUNC(sio_getcap);
  89. LOAD_FUNC(sio_onmove);
  90. LOAD_FUNC(sio_write);
  91. LOAD_FUNC(sio_read);
  92. LOAD_FUNC(sio_start);
  93. LOAD_FUNC(sio_stop);
  94. LOAD_FUNC(sio_nfds);
  95. LOAD_FUNC(sio_pollfd);
  96. LOAD_FUNC(sio_revents);
  97. LOAD_FUNC(sio_eof);
  98. LOAD_FUNC(sio_setvol);
  99. LOAD_FUNC(sio_onvol);
  100. #undef LOAD_FUNC
  101. }
  102. #endif
  103. return ALC_TRUE;
  104. }
  105. typedef struct {
  106. struct sio_hdl *sndHandle;
  107. ALvoid *mix_data;
  108. ALsizei data_size;
  109. volatile int killNow;
  110. ALvoid *thread;
  111. } sndio_data;
  112. static ALuint sndio_proc(ALvoid *ptr)
  113. {
  114. ALCdevice *device = ptr;
  115. sndio_data *data = device->ExtraData;
  116. ALsizei frameSize;
  117. size_t wrote;
  118. SetRTPriority();
  119. frameSize = FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
  120. while(!data->killNow && device->Connected)
  121. {
  122. ALsizei len = data->data_size;
  123. ALubyte *WritePtr = data->mix_data;
  124. aluMixData(device, WritePtr, len/frameSize);
  125. while(len > 0 && !data->killNow)
  126. {
  127. wrote = sio_write(data->sndHandle, WritePtr, len);
  128. if(wrote == 0)
  129. {
  130. ERR("sio_write failed\n");
  131. aluHandleDisconnect(device);
  132. break;
  133. }
  134. len -= wrote;
  135. WritePtr += wrote;
  136. }
  137. }
  138. return 0;
  139. }
  140. static ALCenum sndio_open_playback(ALCdevice *device, const ALCchar *deviceName)
  141. {
  142. sndio_data *data;
  143. if(!deviceName)
  144. deviceName = sndio_device;
  145. else if(strcmp(deviceName, sndio_device) != 0)
  146. return ALC_INVALID_VALUE;
  147. data = calloc(1, sizeof(*data));
  148. data->killNow = 0;
  149. data->sndHandle = sio_open(NULL, SIO_PLAY, 0);
  150. if(data->sndHandle == NULL)
  151. {
  152. free(data);
  153. ERR("Could not open device\n");
  154. return ALC_INVALID_VALUE;
  155. }
  156. device->szDeviceName = strdup(deviceName);
  157. device->ExtraData = data;
  158. return ALC_NO_ERROR;
  159. }
  160. static void sndio_close_playback(ALCdevice *device)
  161. {
  162. sndio_data *data = device->ExtraData;
  163. sio_close(data->sndHandle);
  164. free(data);
  165. device->ExtraData = NULL;
  166. }
  167. static ALCboolean sndio_reset_playback(ALCdevice *device)
  168. {
  169. sndio_data *data = device->ExtraData;
  170. struct sio_par par;
  171. sio_initpar(&par);
  172. par.rate = device->Frequency;
  173. par.pchan = ((device->FmtChans != DevFmtMono) ? 2 : 1);
  174. switch(device->FmtType)
  175. {
  176. case DevFmtByte:
  177. par.bits = 8;
  178. par.sig = 1;
  179. break;
  180. case DevFmtUByte:
  181. par.bits = 8;
  182. par.sig = 0;
  183. break;
  184. case DevFmtFloat:
  185. case DevFmtShort:
  186. par.bits = 16;
  187. par.sig = 1;
  188. break;
  189. case DevFmtUShort:
  190. par.bits = 16;
  191. par.sig = 0;
  192. break;
  193. case DevFmtInt:
  194. par.bits = 32;
  195. par.sig = 1;
  196. break;
  197. case DevFmtUInt:
  198. par.bits = 32;
  199. par.sig = 0;
  200. break;
  201. }
  202. par.le = SIO_LE_NATIVE;
  203. par.round = device->UpdateSize;
  204. par.appbufsz = device->UpdateSize * (device->NumUpdates-1);
  205. if(!par.appbufsz) par.appbufsz = device->UpdateSize;
  206. if(!sio_setpar(data->sndHandle, &par) || !sio_getpar(data->sndHandle, &par))
  207. {
  208. ERR("Failed to set device parameters\n");
  209. return ALC_FALSE;
  210. }
  211. if(par.bits != par.bps*8)
  212. {
  213. ERR("Padded samples not supported (%u of %u bits)\n", par.bits, par.bps*8);
  214. return ALC_FALSE;
  215. }
  216. device->Frequency = par.rate;
  217. device->FmtChans = ((par.pchan==1) ? DevFmtMono : DevFmtStereo);
  218. if(par.bits == 8 && par.sig == 1)
  219. device->FmtType = DevFmtByte;
  220. else if(par.bits == 8 && par.sig == 0)
  221. device->FmtType = DevFmtUByte;
  222. else if(par.bits == 16 && par.sig == 1)
  223. device->FmtType = DevFmtShort;
  224. else if(par.bits == 16 && par.sig == 0)
  225. device->FmtType = DevFmtUShort;
  226. else if(par.bits == 32 && par.sig == 1)
  227. device->FmtType = DevFmtInt;
  228. else if(par.bits == 32 && par.sig == 0)
  229. device->FmtType = DevFmtUInt;
  230. else
  231. {
  232. ERR("Unhandled sample format: %s %u-bit\n", (par.sig?"signed":"unsigned"), par.bits);
  233. return ALC_FALSE;
  234. }
  235. device->UpdateSize = par.round;
  236. device->NumUpdates = (par.bufsz/par.round) + 1;
  237. SetDefaultChannelOrder(device);
  238. return ALC_TRUE;
  239. }
  240. static ALCboolean sndio_start_playback(ALCdevice *device)
  241. {
  242. sndio_data *data = device->ExtraData;
  243. if(!sio_start(data->sndHandle))
  244. {
  245. ERR("Error starting playback\n");
  246. return ALC_FALSE;
  247. }
  248. data->data_size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
  249. data->mix_data = calloc(1, data->data_size);
  250. data->thread = StartThread(sndio_proc, device);
  251. if(data->thread == NULL)
  252. {
  253. sio_stop(data->sndHandle);
  254. free(data->mix_data);
  255. data->mix_data = NULL;
  256. return ALC_FALSE;
  257. }
  258. return ALC_TRUE;
  259. }
  260. static void sndio_stop_playback(ALCdevice *device)
  261. {
  262. sndio_data *data = device->ExtraData;
  263. if(!data->thread)
  264. return;
  265. data->killNow = 1;
  266. StopThread(data->thread);
  267. data->thread = NULL;
  268. data->killNow = 0;
  269. if(!sio_stop(data->sndHandle))
  270. ERR("Error stopping device\n");
  271. free(data->mix_data);
  272. data->mix_data = NULL;
  273. }
  274. static const BackendFuncs sndio_funcs = {
  275. sndio_open_playback,
  276. sndio_close_playback,
  277. sndio_reset_playback,
  278. sndio_start_playback,
  279. sndio_stop_playback,
  280. NULL,
  281. NULL,
  282. NULL,
  283. NULL,
  284. NULL,
  285. NULL
  286. };
  287. ALCboolean alc_sndio_init(BackendFuncs *func_list)
  288. {
  289. if(!sndio_load())
  290. return ALC_FALSE;
  291. *func_list = sndio_funcs;
  292. return ALC_TRUE;
  293. }
  294. void alc_sndio_deinit(void)
  295. {
  296. #ifdef HAVE_DYNLOAD
  297. if(sndio_handle)
  298. CloseLib(sndio_handle);
  299. sndio_handle = NULL;
  300. #endif
  301. }
  302. void alc_sndio_probe(enum DevProbe type)
  303. {
  304. switch(type)
  305. {
  306. case ALL_DEVICE_PROBE:
  307. AppendAllDeviceList(sndio_device);
  308. break;
  309. case CAPTURE_DEVICE_PROBE:
  310. break;
  311. }
  312. }