oss.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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/time.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <memory.h>
  30. #include <unistd.h>
  31. #include <errno.h>
  32. #include <math.h>
  33. #include "alMain.h"
  34. #include "alu.h"
  35. #include "threads.h"
  36. #include "compat.h"
  37. #include "backends/base.h"
  38. #include <sys/soundcard.h>
  39. /*
  40. * The OSS documentation talks about SOUND_MIXER_READ, but the header
  41. * only contains MIXER_READ. Play safe. Same for WRITE.
  42. */
  43. #ifndef SOUND_MIXER_READ
  44. #define SOUND_MIXER_READ MIXER_READ
  45. #endif
  46. #ifndef SOUND_MIXER_WRITE
  47. #define SOUND_MIXER_WRITE MIXER_WRITE
  48. #endif
  49. #if defined(SOUND_VERSION) && (SOUND_VERSION < 0x040000)
  50. #define ALC_OSS_COMPAT
  51. #endif
  52. #ifndef SNDCTL_AUDIOINFO
  53. #define ALC_OSS_COMPAT
  54. #endif
  55. /*
  56. * FreeBSD strongly discourages the use of specific devices,
  57. * such as those returned in oss_audioinfo.devnode
  58. */
  59. #ifdef __FreeBSD__
  60. #define ALC_OSS_DEVNODE_TRUC
  61. #endif
  62. struct oss_device {
  63. const ALCchar *handle;
  64. const char *path;
  65. struct oss_device *next;
  66. };
  67. static struct oss_device oss_playback = {
  68. "OSS Default",
  69. "/dev/dsp",
  70. NULL
  71. };
  72. static struct oss_device oss_capture = {
  73. "OSS Default",
  74. "/dev/dsp",
  75. NULL
  76. };
  77. #ifdef ALC_OSS_COMPAT
  78. static void ALCossListPopulate(struct oss_device *UNUSED(playback), struct oss_device *UNUSED(capture))
  79. {
  80. }
  81. #else
  82. #ifndef HAVE_STRNLEN
  83. static size_t strnlen(const char *str, size_t maxlen)
  84. {
  85. const char *end = memchr(str, 0, maxlen);
  86. if(!end) return maxlen;
  87. return end - str;
  88. }
  89. #endif
  90. static void ALCossListAppend(struct oss_device *list, const char *handle, size_t hlen, const char *path, size_t plen)
  91. {
  92. struct oss_device *next;
  93. struct oss_device *last;
  94. size_t i;
  95. /* skip the first item "OSS Default" */
  96. last = list;
  97. next = list->next;
  98. #ifdef ALC_OSS_DEVNODE_TRUC
  99. for(i = 0;i < plen;i++)
  100. {
  101. if(path[i] == '.')
  102. {
  103. if(strncmp(path + i, handle + hlen + i - plen, plen - i) == 0)
  104. hlen = hlen + i - plen;
  105. plen = i;
  106. }
  107. }
  108. #else
  109. (void)i;
  110. #endif
  111. if(handle[0] == '\0')
  112. {
  113. handle = path;
  114. hlen = plen;
  115. }
  116. while(next != NULL)
  117. {
  118. if(strncmp(next->path, path, plen) == 0)
  119. return;
  120. last = next;
  121. next = next->next;
  122. }
  123. next = (struct oss_device*)malloc(sizeof(struct oss_device) + hlen + plen + 2);
  124. next->handle = (char*)(next + 1);
  125. next->path = next->handle + hlen + 1;
  126. next->next = NULL;
  127. last->next = next;
  128. strncpy((char*)next->handle, handle, hlen);
  129. ((char*)next->handle)[hlen] = '\0';
  130. strncpy((char*)next->path, path, plen);
  131. ((char*)next->path)[plen] = '\0';
  132. TRACE("Got device \"%s\", \"%s\"\n", next->handle, next->path);
  133. }
  134. static void ALCossListPopulate(struct oss_device *playback, struct oss_device *capture)
  135. {
  136. struct oss_sysinfo si;
  137. struct oss_audioinfo ai;
  138. int fd, i;
  139. if((fd=open("/dev/mixer", O_RDONLY)) < 0)
  140. {
  141. ERR("Could not open /dev/mixer\n");
  142. return;
  143. }
  144. if(ioctl(fd, SNDCTL_SYSINFO, &si) == -1)
  145. {
  146. ERR("SNDCTL_SYSINFO failed: %s\n", strerror(errno));
  147. goto done;
  148. }
  149. for(i = 0;i < si.numaudios;i++)
  150. {
  151. const char *handle;
  152. size_t len;
  153. ai.dev = i;
  154. if(ioctl(fd, SNDCTL_AUDIOINFO, &ai) == -1)
  155. {
  156. ERR("SNDCTL_AUDIOINFO (%d) failed: %s\n", i, strerror(errno));
  157. continue;
  158. }
  159. if(ai.devnode[0] == '\0')
  160. continue;
  161. if(ai.handle[0] != '\0')
  162. {
  163. len = strnlen(ai.handle, sizeof(ai.handle));
  164. handle = ai.handle;
  165. }
  166. else
  167. {
  168. len = strnlen(ai.name, sizeof(ai.name));
  169. handle = ai.name;
  170. }
  171. if((ai.caps&DSP_CAP_INPUT) && capture != NULL)
  172. ALCossListAppend(capture, handle, len, ai.devnode, strnlen(ai.devnode, sizeof(ai.devnode)));
  173. if((ai.caps&DSP_CAP_OUTPUT) && playback != NULL)
  174. ALCossListAppend(playback, handle, len, ai.devnode, strnlen(ai.devnode, sizeof(ai.devnode)));
  175. }
  176. done:
  177. close(fd);
  178. }
  179. #endif
  180. static void ALCossListFree(struct oss_device *list)
  181. {
  182. struct oss_device *cur;
  183. if(list == NULL)
  184. return;
  185. /* skip the first item "OSS Default" */
  186. cur = list->next;
  187. list->next = NULL;
  188. while(cur != NULL)
  189. {
  190. struct oss_device *next = cur->next;
  191. free(cur);
  192. cur = next;
  193. }
  194. }
  195. static int log2i(ALCuint x)
  196. {
  197. int y = 0;
  198. while (x > 1)
  199. {
  200. x >>= 1;
  201. y++;
  202. }
  203. return y;
  204. }
  205. typedef struct ALCplaybackOSS {
  206. DERIVE_FROM_TYPE(ALCbackend);
  207. int fd;
  208. ALubyte *mix_data;
  209. int data_size;
  210. volatile int killNow;
  211. althrd_t thread;
  212. } ALCplaybackOSS;
  213. static int ALCplaybackOSS_mixerProc(void *ptr);
  214. static void ALCplaybackOSS_Construct(ALCplaybackOSS *self, ALCdevice *device);
  215. static DECLARE_FORWARD(ALCplaybackOSS, ALCbackend, void, Destruct)
  216. static ALCenum ALCplaybackOSS_open(ALCplaybackOSS *self, const ALCchar *name);
  217. static void ALCplaybackOSS_close(ALCplaybackOSS *self);
  218. static ALCboolean ALCplaybackOSS_reset(ALCplaybackOSS *self);
  219. static ALCboolean ALCplaybackOSS_start(ALCplaybackOSS *self);
  220. static void ALCplaybackOSS_stop(ALCplaybackOSS *self);
  221. static DECLARE_FORWARD2(ALCplaybackOSS, ALCbackend, ALCenum, captureSamples, ALCvoid*, ALCuint)
  222. static DECLARE_FORWARD(ALCplaybackOSS, ALCbackend, ALCuint, availableSamples)
  223. static DECLARE_FORWARD(ALCplaybackOSS, ALCbackend, ClockLatency, getClockLatency)
  224. static DECLARE_FORWARD(ALCplaybackOSS, ALCbackend, void, lock)
  225. static DECLARE_FORWARD(ALCplaybackOSS, ALCbackend, void, unlock)
  226. DECLARE_DEFAULT_ALLOCATORS(ALCplaybackOSS)
  227. DEFINE_ALCBACKEND_VTABLE(ALCplaybackOSS);
  228. static int ALCplaybackOSS_mixerProc(void *ptr)
  229. {
  230. ALCplaybackOSS *self = (ALCplaybackOSS*)ptr;
  231. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  232. ALint frameSize;
  233. ssize_t wrote;
  234. SetRTPriority();
  235. althrd_setname(althrd_current(), MIXER_THREAD_NAME);
  236. frameSize = FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
  237. while(!self->killNow && device->Connected)
  238. {
  239. ALint len = self->data_size;
  240. ALubyte *WritePtr = self->mix_data;
  241. aluMixData(device, WritePtr, len/frameSize);
  242. while(len > 0 && !self->killNow)
  243. {
  244. wrote = write(self->fd, WritePtr, len);
  245. if(wrote < 0)
  246. {
  247. if(errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
  248. {
  249. ERR("write failed: %s\n", strerror(errno));
  250. ALCplaybackOSS_lock(self);
  251. aluHandleDisconnect(device);
  252. ALCplaybackOSS_unlock(self);
  253. break;
  254. }
  255. al_nssleep(1000000);
  256. continue;
  257. }
  258. len -= wrote;
  259. WritePtr += wrote;
  260. }
  261. }
  262. return 0;
  263. }
  264. static void ALCplaybackOSS_Construct(ALCplaybackOSS *self, ALCdevice *device)
  265. {
  266. ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
  267. SET_VTABLE2(ALCplaybackOSS, ALCbackend, self);
  268. }
  269. static ALCenum ALCplaybackOSS_open(ALCplaybackOSS *self, const ALCchar *name)
  270. {
  271. struct oss_device *dev = &oss_playback;
  272. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  273. if(!name)
  274. name = dev->handle;
  275. else
  276. {
  277. while (dev != NULL)
  278. {
  279. if (strcmp(dev->handle, name) == 0)
  280. break;
  281. dev = dev->next;
  282. }
  283. if (dev == NULL)
  284. return ALC_INVALID_VALUE;
  285. }
  286. self->killNow = 0;
  287. self->fd = open(dev->path, O_WRONLY);
  288. if(self->fd == -1)
  289. {
  290. ERR("Could not open %s: %s\n", dev->path, strerror(errno));
  291. return ALC_INVALID_VALUE;
  292. }
  293. al_string_copy_cstr(&device->DeviceName, name);
  294. return ALC_NO_ERROR;
  295. }
  296. static void ALCplaybackOSS_close(ALCplaybackOSS *self)
  297. {
  298. close(self->fd);
  299. self->fd = -1;
  300. }
  301. static ALCboolean ALCplaybackOSS_reset(ALCplaybackOSS *self)
  302. {
  303. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  304. int numFragmentsLogSize;
  305. int log2FragmentSize;
  306. unsigned int periods;
  307. audio_buf_info info;
  308. ALuint frameSize;
  309. int numChannels;
  310. int ossFormat;
  311. int ossSpeed;
  312. char *err;
  313. switch(device->FmtType)
  314. {
  315. case DevFmtByte:
  316. ossFormat = AFMT_S8;
  317. break;
  318. case DevFmtUByte:
  319. ossFormat = AFMT_U8;
  320. break;
  321. case DevFmtUShort:
  322. case DevFmtInt:
  323. case DevFmtUInt:
  324. case DevFmtFloat:
  325. device->FmtType = DevFmtShort;
  326. /* fall-through */
  327. case DevFmtShort:
  328. ossFormat = AFMT_S16_NE;
  329. break;
  330. }
  331. periods = device->NumUpdates;
  332. numChannels = ChannelsFromDevFmt(device->FmtChans);
  333. frameSize = numChannels * BytesFromDevFmt(device->FmtType);
  334. ossSpeed = device->Frequency;
  335. log2FragmentSize = log2i(device->UpdateSize * frameSize);
  336. /* according to the OSS spec, 16 bytes are the minimum */
  337. if (log2FragmentSize < 4)
  338. log2FragmentSize = 4;
  339. /* Subtract one period since the temp mixing buffer counts as one. Still
  340. * need at least two on the card, though. */
  341. if(periods > 2) periods--;
  342. numFragmentsLogSize = (periods << 16) | log2FragmentSize;
  343. #define CHECKERR(func) if((func) < 0) { \
  344. err = #func; \
  345. goto err; \
  346. }
  347. /* Don't fail if SETFRAGMENT fails. We can handle just about anything
  348. * that's reported back via GETOSPACE */
  349. ioctl(self->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize);
  350. CHECKERR(ioctl(self->fd, SNDCTL_DSP_SETFMT, &ossFormat));
  351. CHECKERR(ioctl(self->fd, SNDCTL_DSP_CHANNELS, &numChannels));
  352. CHECKERR(ioctl(self->fd, SNDCTL_DSP_SPEED, &ossSpeed));
  353. CHECKERR(ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &info));
  354. if(0)
  355. {
  356. err:
  357. ERR("%s failed: %s\n", err, strerror(errno));
  358. return ALC_FALSE;
  359. }
  360. #undef CHECKERR
  361. if((int)ChannelsFromDevFmt(device->FmtChans) != numChannels)
  362. {
  363. ERR("Failed to set %s, got %d channels instead\n", DevFmtChannelsString(device->FmtChans), numChannels);
  364. return ALC_FALSE;
  365. }
  366. if(!((ossFormat == AFMT_S8 && device->FmtType == DevFmtByte) ||
  367. (ossFormat == AFMT_U8 && device->FmtType == DevFmtUByte) ||
  368. (ossFormat == AFMT_S16_NE && device->FmtType == DevFmtShort)))
  369. {
  370. ERR("Failed to set %s samples, got OSS format %#x\n", DevFmtTypeString(device->FmtType), ossFormat);
  371. return ALC_FALSE;
  372. }
  373. device->Frequency = ossSpeed;
  374. device->UpdateSize = info.fragsize / frameSize;
  375. device->NumUpdates = info.fragments + 1;
  376. SetDefaultChannelOrder(device);
  377. return ALC_TRUE;
  378. }
  379. static ALCboolean ALCplaybackOSS_start(ALCplaybackOSS *self)
  380. {
  381. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  382. self->data_size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
  383. self->mix_data = calloc(1, self->data_size);
  384. self->killNow = 0;
  385. if(althrd_create(&self->thread, ALCplaybackOSS_mixerProc, self) != althrd_success)
  386. {
  387. free(self->mix_data);
  388. self->mix_data = NULL;
  389. return ALC_FALSE;
  390. }
  391. return ALC_TRUE;
  392. }
  393. static void ALCplaybackOSS_stop(ALCplaybackOSS *self)
  394. {
  395. int res;
  396. if(self->killNow)
  397. return;
  398. self->killNow = 1;
  399. althrd_join(self->thread, &res);
  400. if(ioctl(self->fd, SNDCTL_DSP_RESET) != 0)
  401. ERR("Error resetting device: %s\n", strerror(errno));
  402. free(self->mix_data);
  403. self->mix_data = NULL;
  404. }
  405. typedef struct ALCcaptureOSS {
  406. DERIVE_FROM_TYPE(ALCbackend);
  407. int fd;
  408. ll_ringbuffer_t *ring;
  409. int doCapture;
  410. volatile int killNow;
  411. althrd_t thread;
  412. } ALCcaptureOSS;
  413. static int ALCcaptureOSS_recordProc(void *ptr);
  414. static void ALCcaptureOSS_Construct(ALCcaptureOSS *self, ALCdevice *device);
  415. static DECLARE_FORWARD(ALCcaptureOSS, ALCbackend, void, Destruct)
  416. static ALCenum ALCcaptureOSS_open(ALCcaptureOSS *self, const ALCchar *name);
  417. static void ALCcaptureOSS_close(ALCcaptureOSS *self);
  418. static DECLARE_FORWARD(ALCcaptureOSS, ALCbackend, ALCboolean, reset)
  419. static ALCboolean ALCcaptureOSS_start(ALCcaptureOSS *self);
  420. static void ALCcaptureOSS_stop(ALCcaptureOSS *self);
  421. static ALCenum ALCcaptureOSS_captureSamples(ALCcaptureOSS *self, ALCvoid *buffer, ALCuint samples);
  422. static ALCuint ALCcaptureOSS_availableSamples(ALCcaptureOSS *self);
  423. static DECLARE_FORWARD(ALCcaptureOSS, ALCbackend, ClockLatency, getClockLatency)
  424. static DECLARE_FORWARD(ALCcaptureOSS, ALCbackend, void, lock)
  425. static DECLARE_FORWARD(ALCcaptureOSS, ALCbackend, void, unlock)
  426. DECLARE_DEFAULT_ALLOCATORS(ALCcaptureOSS)
  427. DEFINE_ALCBACKEND_VTABLE(ALCcaptureOSS);
  428. static int ALCcaptureOSS_recordProc(void *ptr)
  429. {
  430. ALCcaptureOSS *self = (ALCcaptureOSS*)ptr;
  431. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  432. int frameSize;
  433. ssize_t amt;
  434. SetRTPriority();
  435. althrd_setname(althrd_current(), RECORD_THREAD_NAME);
  436. frameSize = FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
  437. while(!self->killNow)
  438. {
  439. ll_ringbuffer_data_t vec[2];
  440. amt = 0;
  441. if(self->doCapture)
  442. {
  443. ll_ringbuffer_get_write_vector(self->ring, vec);
  444. if(vec[0].len > 0)
  445. {
  446. amt = read(self->fd, vec[0].buf, vec[0].len*frameSize);
  447. if(amt < 0)
  448. {
  449. ERR("read failed: %s\n", strerror(errno));
  450. ALCcaptureOSS_lock(self);
  451. aluHandleDisconnect(device);
  452. ALCcaptureOSS_unlock(self);
  453. break;
  454. }
  455. ll_ringbuffer_write_advance(self->ring, amt/frameSize);
  456. }
  457. }
  458. if(amt == 0)
  459. {
  460. al_nssleep(1000000);
  461. continue;
  462. }
  463. }
  464. return 0;
  465. }
  466. static void ALCcaptureOSS_Construct(ALCcaptureOSS *self, ALCdevice *device)
  467. {
  468. ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
  469. SET_VTABLE2(ALCcaptureOSS, ALCbackend, self);
  470. }
  471. static ALCenum ALCcaptureOSS_open(ALCcaptureOSS *self, const ALCchar *name)
  472. {
  473. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  474. struct oss_device *dev = &oss_capture;
  475. int numFragmentsLogSize;
  476. int log2FragmentSize;
  477. unsigned int periods;
  478. audio_buf_info info;
  479. ALuint frameSize;
  480. int numChannels;
  481. int ossFormat;
  482. int ossSpeed;
  483. char *err;
  484. if(!name)
  485. name = dev->handle;
  486. else
  487. {
  488. while (dev != NULL)
  489. {
  490. if (strcmp(dev->handle, name) == 0)
  491. break;
  492. dev = dev->next;
  493. }
  494. if (dev == NULL)
  495. return ALC_INVALID_VALUE;
  496. }
  497. self->fd = open(dev->path, O_RDONLY);
  498. if(self->fd == -1)
  499. {
  500. ERR("Could not open %s: %s\n", dev->path, strerror(errno));
  501. return ALC_INVALID_VALUE;
  502. }
  503. switch(device->FmtType)
  504. {
  505. case DevFmtByte:
  506. ossFormat = AFMT_S8;
  507. break;
  508. case DevFmtUByte:
  509. ossFormat = AFMT_U8;
  510. break;
  511. case DevFmtShort:
  512. ossFormat = AFMT_S16_NE;
  513. break;
  514. case DevFmtUShort:
  515. case DevFmtInt:
  516. case DevFmtUInt:
  517. case DevFmtFloat:
  518. ERR("%s capture samples not supported\n", DevFmtTypeString(device->FmtType));
  519. return ALC_INVALID_VALUE;
  520. }
  521. periods = 4;
  522. numChannels = ChannelsFromDevFmt(device->FmtChans);
  523. frameSize = numChannels * BytesFromDevFmt(device->FmtType);
  524. ossSpeed = device->Frequency;
  525. log2FragmentSize = log2i(device->UpdateSize * device->NumUpdates *
  526. frameSize / periods);
  527. /* according to the OSS spec, 16 bytes are the minimum */
  528. if (log2FragmentSize < 4)
  529. log2FragmentSize = 4;
  530. numFragmentsLogSize = (periods << 16) | log2FragmentSize;
  531. #define CHECKERR(func) if((func) < 0) { \
  532. err = #func; \
  533. goto err; \
  534. }
  535. CHECKERR(ioctl(self->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize));
  536. CHECKERR(ioctl(self->fd, SNDCTL_DSP_SETFMT, &ossFormat));
  537. CHECKERR(ioctl(self->fd, SNDCTL_DSP_CHANNELS, &numChannels));
  538. CHECKERR(ioctl(self->fd, SNDCTL_DSP_SPEED, &ossSpeed));
  539. CHECKERR(ioctl(self->fd, SNDCTL_DSP_GETISPACE, &info));
  540. if(0)
  541. {
  542. err:
  543. ERR("%s failed: %s\n", err, strerror(errno));
  544. close(self->fd);
  545. self->fd = -1;
  546. return ALC_INVALID_VALUE;
  547. }
  548. #undef CHECKERR
  549. if((int)ChannelsFromDevFmt(device->FmtChans) != numChannels)
  550. {
  551. ERR("Failed to set %s, got %d channels instead\n", DevFmtChannelsString(device->FmtChans), numChannels);
  552. close(self->fd);
  553. self->fd = -1;
  554. return ALC_INVALID_VALUE;
  555. }
  556. if(!((ossFormat == AFMT_S8 && device->FmtType == DevFmtByte) ||
  557. (ossFormat == AFMT_U8 && device->FmtType == DevFmtUByte) ||
  558. (ossFormat == AFMT_S16_NE && device->FmtType == DevFmtShort)))
  559. {
  560. ERR("Failed to set %s samples, got OSS format %#x\n", DevFmtTypeString(device->FmtType), ossFormat);
  561. close(self->fd);
  562. self->fd = -1;
  563. return ALC_INVALID_VALUE;
  564. }
  565. self->ring = ll_ringbuffer_create(device->UpdateSize*device->NumUpdates + 1, frameSize);
  566. if(!self->ring)
  567. {
  568. ERR("Ring buffer create failed\n");
  569. close(self->fd);
  570. self->fd = -1;
  571. return ALC_OUT_OF_MEMORY;
  572. }
  573. self->killNow = 0;
  574. if(althrd_create(&self->thread, ALCcaptureOSS_recordProc, self) != althrd_success)
  575. {
  576. ll_ringbuffer_free(self->ring);
  577. self->ring = NULL;
  578. close(self->fd);
  579. self->fd = -1;
  580. return ALC_OUT_OF_MEMORY;
  581. }
  582. al_string_copy_cstr(&device->DeviceName, name);
  583. return ALC_NO_ERROR;
  584. }
  585. static void ALCcaptureOSS_close(ALCcaptureOSS *self)
  586. {
  587. int res;
  588. self->killNow = 1;
  589. althrd_join(self->thread, &res);
  590. close(self->fd);
  591. self->fd = -1;
  592. ll_ringbuffer_free(self->ring);
  593. self->ring = NULL;
  594. }
  595. static ALCboolean ALCcaptureOSS_start(ALCcaptureOSS *self)
  596. {
  597. self->doCapture = 1;
  598. return ALC_TRUE;
  599. }
  600. static void ALCcaptureOSS_stop(ALCcaptureOSS *self)
  601. {
  602. self->doCapture = 0;
  603. }
  604. static ALCenum ALCcaptureOSS_captureSamples(ALCcaptureOSS *self, ALCvoid *buffer, ALCuint samples)
  605. {
  606. ll_ringbuffer_read(self->ring, buffer, samples);
  607. return ALC_NO_ERROR;
  608. }
  609. static ALCuint ALCcaptureOSS_availableSamples(ALCcaptureOSS *self)
  610. {
  611. return ll_ringbuffer_read_space(self->ring);
  612. }
  613. typedef struct ALCossBackendFactory {
  614. DERIVE_FROM_TYPE(ALCbackendFactory);
  615. } ALCossBackendFactory;
  616. #define ALCOSSBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCossBackendFactory, ALCbackendFactory) } }
  617. ALCbackendFactory *ALCossBackendFactory_getFactory(void);
  618. static ALCboolean ALCossBackendFactory_init(ALCossBackendFactory *self);
  619. static void ALCossBackendFactory_deinit(ALCossBackendFactory *self);
  620. static ALCboolean ALCossBackendFactory_querySupport(ALCossBackendFactory *self, ALCbackend_Type type);
  621. static void ALCossBackendFactory_probe(ALCossBackendFactory *self, enum DevProbe type);
  622. static ALCbackend* ALCossBackendFactory_createBackend(ALCossBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
  623. DEFINE_ALCBACKENDFACTORY_VTABLE(ALCossBackendFactory);
  624. ALCbackendFactory *ALCossBackendFactory_getFactory(void)
  625. {
  626. static ALCossBackendFactory factory = ALCOSSBACKENDFACTORY_INITIALIZER;
  627. return STATIC_CAST(ALCbackendFactory, &factory);
  628. }
  629. ALCboolean ALCossBackendFactory_init(ALCossBackendFactory* UNUSED(self))
  630. {
  631. ConfigValueStr(NULL, "oss", "device", &oss_playback.path);
  632. ConfigValueStr(NULL, "oss", "capture", &oss_capture.path);
  633. return ALC_TRUE;
  634. }
  635. void ALCossBackendFactory_deinit(ALCossBackendFactory* UNUSED(self))
  636. {
  637. ALCossListFree(&oss_playback);
  638. ALCossListFree(&oss_capture);
  639. }
  640. ALCboolean ALCossBackendFactory_querySupport(ALCossBackendFactory* UNUSED(self), ALCbackend_Type type)
  641. {
  642. if(type == ALCbackend_Playback || type == ALCbackend_Capture)
  643. return ALC_TRUE;
  644. return ALC_FALSE;
  645. }
  646. void ALCossBackendFactory_probe(ALCossBackendFactory* UNUSED(self), enum DevProbe type)
  647. {
  648. switch(type)
  649. {
  650. case ALL_DEVICE_PROBE:
  651. {
  652. struct oss_device *cur = &oss_playback;
  653. ALCossListFree(cur);
  654. ALCossListPopulate(cur, NULL);
  655. while (cur != NULL)
  656. {
  657. AppendAllDevicesList(cur->handle);
  658. cur = cur->next;
  659. }
  660. }
  661. break;
  662. case CAPTURE_DEVICE_PROBE:
  663. {
  664. struct oss_device *cur = &oss_capture;
  665. ALCossListFree(cur);
  666. ALCossListPopulate(NULL, cur);
  667. while (cur != NULL)
  668. {
  669. AppendCaptureDeviceList(cur->handle);
  670. cur = cur->next;
  671. }
  672. }
  673. break;
  674. }
  675. }
  676. ALCbackend* ALCossBackendFactory_createBackend(ALCossBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
  677. {
  678. if(type == ALCbackend_Playback)
  679. {
  680. ALCplaybackOSS *backend;
  681. NEW_OBJ(backend, ALCplaybackOSS)(device);
  682. if(!backend) return NULL;
  683. return STATIC_CAST(ALCbackend, backend);
  684. }
  685. if(type == ALCbackend_Capture)
  686. {
  687. ALCcaptureOSS *backend;
  688. NEW_OBJ(backend, ALCcaptureOSS)(device);
  689. if(!backend) return NULL;
  690. return STATIC_CAST(ALCbackend, backend);
  691. }
  692. return NULL;
  693. }