oss.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 <sys/ioctl.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <memory.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. #include <math.h>
  31. #include "alMain.h"
  32. #include "AL/al.h"
  33. #include "AL/alc.h"
  34. #include <sys/soundcard.h>
  35. /*
  36. * The OSS documentation talks about SOUND_MIXER_READ, but the header
  37. * only contains MIXER_READ. Play safe. Same for WRITE.
  38. */
  39. #ifndef SOUND_MIXER_READ
  40. #define SOUND_MIXER_READ MIXER_READ
  41. #endif
  42. #ifndef SOUND_MIXER_WRITE
  43. #define SOUND_MIXER_WRITE MIXER_WRITE
  44. #endif
  45. static const ALCchar oss_device[] = "OSS Default";
  46. static const char *oss_driver = "/dev/dsp";
  47. static const char *oss_capture = "/dev/dsp";
  48. typedef struct {
  49. int fd;
  50. volatile int killNow;
  51. ALvoid *thread;
  52. ALubyte *mix_data;
  53. int data_size;
  54. RingBuffer *ring;
  55. int doCapture;
  56. } oss_data;
  57. static int log2i(ALCuint x)
  58. {
  59. int y = 0;
  60. while (x > 1)
  61. {
  62. x >>= 1;
  63. y++;
  64. }
  65. return y;
  66. }
  67. static ALuint OSSProc(ALvoid *ptr)
  68. {
  69. ALCdevice *pDevice = (ALCdevice*)ptr;
  70. oss_data *data = (oss_data*)pDevice->ExtraData;
  71. ALint frameSize;
  72. ssize_t wrote;
  73. SetRTPriority();
  74. frameSize = FrameSizeFromDevFmt(pDevice->FmtChans, pDevice->FmtType);
  75. while(!data->killNow && pDevice->Connected)
  76. {
  77. ALint len = data->data_size;
  78. ALubyte *WritePtr = data->mix_data;
  79. aluMixData(pDevice, WritePtr, len/frameSize);
  80. while(len > 0 && !data->killNow)
  81. {
  82. wrote = write(data->fd, WritePtr, len);
  83. if(wrote < 0)
  84. {
  85. if(errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
  86. {
  87. ERR("write failed: %s\n", strerror(errno));
  88. aluHandleDisconnect(pDevice);
  89. break;
  90. }
  91. Sleep(1);
  92. continue;
  93. }
  94. len -= wrote;
  95. WritePtr += wrote;
  96. }
  97. }
  98. return 0;
  99. }
  100. static ALuint OSSCaptureProc(ALvoid *ptr)
  101. {
  102. ALCdevice *pDevice = (ALCdevice*)ptr;
  103. oss_data *data = (oss_data*)pDevice->ExtraData;
  104. int frameSize;
  105. int amt;
  106. SetRTPriority();
  107. frameSize = FrameSizeFromDevFmt(pDevice->FmtChans, pDevice->FmtType);
  108. while(!data->killNow)
  109. {
  110. amt = read(data->fd, data->mix_data, data->data_size);
  111. if(amt < 0)
  112. {
  113. ERR("read failed: %s\n", strerror(errno));
  114. aluHandleDisconnect(pDevice);
  115. break;
  116. }
  117. if(amt == 0)
  118. {
  119. Sleep(1);
  120. continue;
  121. }
  122. if(data->doCapture)
  123. WriteRingBuffer(data->ring, data->mix_data, amt/frameSize);
  124. }
  125. return 0;
  126. }
  127. static ALCenum oss_open_playback(ALCdevice *device, const ALCchar *deviceName)
  128. {
  129. oss_data *data;
  130. if(!deviceName)
  131. deviceName = oss_device;
  132. else if(strcmp(deviceName, oss_device) != 0)
  133. return ALC_INVALID_VALUE;
  134. data = (oss_data*)calloc(1, sizeof(oss_data));
  135. data->killNow = 0;
  136. data->fd = open(oss_driver, O_WRONLY);
  137. if(data->fd == -1)
  138. {
  139. free(data);
  140. ERR("Could not open %s: %s\n", oss_driver, strerror(errno));
  141. return ALC_INVALID_VALUE;
  142. }
  143. device->szDeviceName = strdup(deviceName);
  144. device->ExtraData = data;
  145. return ALC_NO_ERROR;
  146. }
  147. static void oss_close_playback(ALCdevice *device)
  148. {
  149. oss_data *data = (oss_data*)device->ExtraData;
  150. close(data->fd);
  151. free(data);
  152. device->ExtraData = NULL;
  153. }
  154. static ALCboolean oss_reset_playback(ALCdevice *device)
  155. {
  156. oss_data *data = (oss_data*)device->ExtraData;
  157. int numFragmentsLogSize;
  158. int log2FragmentSize;
  159. unsigned int periods;
  160. audio_buf_info info;
  161. ALuint frameSize;
  162. int numChannels;
  163. int ossFormat;
  164. int ossSpeed;
  165. char *err;
  166. switch(device->FmtType)
  167. {
  168. case DevFmtByte:
  169. ossFormat = AFMT_S8;
  170. break;
  171. case DevFmtUByte:
  172. ossFormat = AFMT_U8;
  173. break;
  174. case DevFmtUShort:
  175. case DevFmtInt:
  176. case DevFmtUInt:
  177. case DevFmtFloat:
  178. device->FmtType = DevFmtShort;
  179. /* fall-through */
  180. case DevFmtShort:
  181. ossFormat = AFMT_S16_NE;
  182. break;
  183. }
  184. periods = device->NumUpdates;
  185. numChannels = ChannelsFromDevFmt(device->FmtChans);
  186. frameSize = numChannels * BytesFromDevFmt(device->FmtType);
  187. ossSpeed = device->Frequency;
  188. log2FragmentSize = log2i(device->UpdateSize * frameSize);
  189. /* according to the OSS spec, 16 bytes are the minimum */
  190. if (log2FragmentSize < 4)
  191. log2FragmentSize = 4;
  192. /* Subtract one period since the temp mixing buffer counts as one. Still
  193. * need at least two on the card, though. */
  194. if(periods > 2) periods--;
  195. numFragmentsLogSize = (periods << 16) | log2FragmentSize;
  196. #define CHECKERR(func) if((func) < 0) { \
  197. err = #func; \
  198. goto err; \
  199. }
  200. /* Don't fail if SETFRAGMENT fails. We can handle just about anything
  201. * that's reported back via GETOSPACE */
  202. ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize);
  203. CHECKERR(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat));
  204. CHECKERR(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels));
  205. CHECKERR(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed));
  206. CHECKERR(ioctl(data->fd, SNDCTL_DSP_GETOSPACE, &info));
  207. if(0)
  208. {
  209. err:
  210. ERR("%s failed: %s\n", err, strerror(errno));
  211. return ALC_FALSE;
  212. }
  213. #undef CHECKERR
  214. if((int)ChannelsFromDevFmt(device->FmtChans) != numChannels)
  215. {
  216. ERR("Failed to set %s, got %d channels instead\n", DevFmtChannelsString(device->FmtChans), numChannels);
  217. return ALC_FALSE;
  218. }
  219. if(!((ossFormat == AFMT_S8 && device->FmtType == DevFmtByte) ||
  220. (ossFormat == AFMT_U8 && device->FmtType == DevFmtUByte) ||
  221. (ossFormat == AFMT_S16_NE && device->FmtType == DevFmtShort)))
  222. {
  223. ERR("Failed to set %s samples, got OSS format %#x\n", DevFmtTypeString(device->FmtType), ossFormat);
  224. return ALC_FALSE;
  225. }
  226. device->Frequency = ossSpeed;
  227. device->UpdateSize = info.fragsize / frameSize;
  228. device->NumUpdates = info.fragments + 1;
  229. SetDefaultChannelOrder(device);
  230. return ALC_TRUE;
  231. }
  232. static ALCboolean oss_start_playback(ALCdevice *device)
  233. {
  234. oss_data *data = (oss_data*)device->ExtraData;
  235. data->data_size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
  236. data->mix_data = calloc(1, data->data_size);
  237. data->thread = StartThread(OSSProc, device);
  238. if(data->thread == NULL)
  239. {
  240. free(data->mix_data);
  241. data->mix_data = NULL;
  242. return ALC_FALSE;
  243. }
  244. return ALC_TRUE;
  245. }
  246. static void oss_stop_playback(ALCdevice *device)
  247. {
  248. oss_data *data = (oss_data*)device->ExtraData;
  249. if(!data->thread)
  250. return;
  251. data->killNow = 1;
  252. StopThread(data->thread);
  253. data->thread = NULL;
  254. data->killNow = 0;
  255. if(ioctl(data->fd, SNDCTL_DSP_RESET) != 0)
  256. ERR("Error resetting device: %s\n", strerror(errno));
  257. free(data->mix_data);
  258. data->mix_data = NULL;
  259. }
  260. static ALCenum oss_open_capture(ALCdevice *device, const ALCchar *deviceName)
  261. {
  262. int numFragmentsLogSize;
  263. int log2FragmentSize;
  264. unsigned int periods;
  265. audio_buf_info info;
  266. ALuint frameSize;
  267. int numChannels;
  268. oss_data *data;
  269. int ossFormat;
  270. int ossSpeed;
  271. char *err;
  272. if(!deviceName)
  273. deviceName = oss_device;
  274. else if(strcmp(deviceName, oss_device) != 0)
  275. return ALC_INVALID_VALUE;
  276. data = (oss_data*)calloc(1, sizeof(oss_data));
  277. data->killNow = 0;
  278. data->fd = open(oss_capture, O_RDONLY);
  279. if(data->fd == -1)
  280. {
  281. free(data);
  282. ERR("Could not open %s: %s\n", oss_capture, strerror(errno));
  283. return ALC_INVALID_VALUE;
  284. }
  285. switch(device->FmtType)
  286. {
  287. case DevFmtByte:
  288. ossFormat = AFMT_S8;
  289. break;
  290. case DevFmtUByte:
  291. ossFormat = AFMT_U8;
  292. break;
  293. case DevFmtShort:
  294. ossFormat = AFMT_S16_NE;
  295. break;
  296. case DevFmtUShort:
  297. case DevFmtInt:
  298. case DevFmtUInt:
  299. case DevFmtFloat:
  300. free(data);
  301. ERR("%s capture samples not supported\n", DevFmtTypeString(device->FmtType));
  302. return ALC_INVALID_VALUE;
  303. }
  304. periods = 4;
  305. numChannels = ChannelsFromDevFmt(device->FmtChans);
  306. frameSize = numChannels * BytesFromDevFmt(device->FmtType);
  307. ossSpeed = device->Frequency;
  308. log2FragmentSize = log2i(device->UpdateSize * device->NumUpdates *
  309. frameSize / periods);
  310. /* according to the OSS spec, 16 bytes are the minimum */
  311. if (log2FragmentSize < 4)
  312. log2FragmentSize = 4;
  313. numFragmentsLogSize = (periods << 16) | log2FragmentSize;
  314. #define CHECKERR(func) if((func) < 0) { \
  315. err = #func; \
  316. goto err; \
  317. }
  318. CHECKERR(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize));
  319. CHECKERR(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat));
  320. CHECKERR(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels));
  321. CHECKERR(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed));
  322. CHECKERR(ioctl(data->fd, SNDCTL_DSP_GETISPACE, &info));
  323. if(0)
  324. {
  325. err:
  326. ERR("%s failed: %s\n", err, strerror(errno));
  327. close(data->fd);
  328. free(data);
  329. return ALC_INVALID_VALUE;
  330. }
  331. #undef CHECKERR
  332. if((int)ChannelsFromDevFmt(device->FmtChans) != numChannels)
  333. {
  334. ERR("Failed to set %s, got %d channels instead\n", DevFmtChannelsString(device->FmtChans), numChannels);
  335. close(data->fd);
  336. free(data);
  337. return ALC_INVALID_VALUE;
  338. }
  339. if(!((ossFormat == AFMT_S8 && device->FmtType == DevFmtByte) ||
  340. (ossFormat == AFMT_U8 && device->FmtType == DevFmtUByte) ||
  341. (ossFormat == AFMT_S16_NE && device->FmtType == DevFmtShort)))
  342. {
  343. ERR("Failed to set %s samples, got OSS format %#x\n", DevFmtTypeString(device->FmtType), ossFormat);
  344. close(data->fd);
  345. free(data);
  346. return ALC_INVALID_VALUE;
  347. }
  348. data->ring = CreateRingBuffer(frameSize, device->UpdateSize * device->NumUpdates);
  349. if(!data->ring)
  350. {
  351. ERR("Ring buffer create failed\n");
  352. close(data->fd);
  353. free(data);
  354. return ALC_OUT_OF_MEMORY;
  355. }
  356. data->data_size = info.fragsize;
  357. data->mix_data = calloc(1, data->data_size);
  358. device->ExtraData = data;
  359. data->thread = StartThread(OSSCaptureProc, device);
  360. if(data->thread == NULL)
  361. {
  362. device->ExtraData = NULL;
  363. free(data->mix_data);
  364. free(data);
  365. return ALC_OUT_OF_MEMORY;
  366. }
  367. device->szDeviceName = strdup(deviceName);
  368. return ALC_NO_ERROR;
  369. }
  370. static void oss_close_capture(ALCdevice *device)
  371. {
  372. oss_data *data = (oss_data*)device->ExtraData;
  373. data->killNow = 1;
  374. StopThread(data->thread);
  375. close(data->fd);
  376. DestroyRingBuffer(data->ring);
  377. free(data->mix_data);
  378. free(data);
  379. device->ExtraData = NULL;
  380. }
  381. static void oss_start_capture(ALCdevice *pDevice)
  382. {
  383. oss_data *data = (oss_data*)pDevice->ExtraData;
  384. data->doCapture = 1;
  385. }
  386. static void oss_stop_capture(ALCdevice *pDevice)
  387. {
  388. oss_data *data = (oss_data*)pDevice->ExtraData;
  389. data->doCapture = 0;
  390. }
  391. static ALCenum oss_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
  392. {
  393. oss_data *data = (oss_data*)pDevice->ExtraData;
  394. ReadRingBuffer(data->ring, pBuffer, lSamples);
  395. return ALC_NO_ERROR;
  396. }
  397. static ALCuint oss_available_samples(ALCdevice *pDevice)
  398. {
  399. oss_data *data = (oss_data*)pDevice->ExtraData;
  400. return RingBufferSize(data->ring);
  401. }
  402. static const BackendFuncs oss_funcs = {
  403. oss_open_playback,
  404. oss_close_playback,
  405. oss_reset_playback,
  406. oss_start_playback,
  407. oss_stop_playback,
  408. oss_open_capture,
  409. oss_close_capture,
  410. oss_start_capture,
  411. oss_stop_capture,
  412. oss_capture_samples,
  413. oss_available_samples
  414. };
  415. ALCboolean alc_oss_init(BackendFuncs *func_list)
  416. {
  417. ConfigValueStr("oss", "device", &oss_driver);
  418. ConfigValueStr("oss", "capture", &oss_capture);
  419. *func_list = oss_funcs;
  420. return ALC_TRUE;
  421. }
  422. void alc_oss_deinit(void)
  423. {
  424. }
  425. void alc_oss_probe(enum DevProbe type)
  426. {
  427. switch(type)
  428. {
  429. case ALL_DEVICE_PROBE:
  430. {
  431. #ifdef HAVE_STAT
  432. struct stat buf;
  433. if(stat(oss_device, &buf) == 0)
  434. #endif
  435. AppendAllDeviceList(oss_device);
  436. }
  437. break;
  438. case CAPTURE_DEVICE_PROBE:
  439. {
  440. #ifdef HAVE_STAT
  441. struct stat buf;
  442. if(stat(oss_capture, &buf) == 0)
  443. #endif
  444. AppendCaptureDeviceList(oss_device);
  445. }
  446. break;
  447. }
  448. }