oss.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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, ALint64, getLatency)
  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. ALubyte *read_data;
  409. int data_size;
  410. RingBuffer *ring;
  411. int doCapture;
  412. volatile int killNow;
  413. althrd_t thread;
  414. } ALCcaptureOSS;
  415. static int ALCcaptureOSS_recordProc(void *ptr);
  416. static void ALCcaptureOSS_Construct(ALCcaptureOSS *self, ALCdevice *device);
  417. static DECLARE_FORWARD(ALCcaptureOSS, ALCbackend, void, Destruct)
  418. static ALCenum ALCcaptureOSS_open(ALCcaptureOSS *self, const ALCchar *name);
  419. static void ALCcaptureOSS_close(ALCcaptureOSS *self);
  420. static DECLARE_FORWARD(ALCcaptureOSS, ALCbackend, ALCboolean, reset)
  421. static ALCboolean ALCcaptureOSS_start(ALCcaptureOSS *self);
  422. static void ALCcaptureOSS_stop(ALCcaptureOSS *self);
  423. static ALCenum ALCcaptureOSS_captureSamples(ALCcaptureOSS *self, ALCvoid *buffer, ALCuint samples);
  424. static ALCuint ALCcaptureOSS_availableSamples(ALCcaptureOSS *self);
  425. static DECLARE_FORWARD(ALCcaptureOSS, ALCbackend, ALint64, getLatency)
  426. static DECLARE_FORWARD(ALCcaptureOSS, ALCbackend, void, lock)
  427. static DECLARE_FORWARD(ALCcaptureOSS, ALCbackend, void, unlock)
  428. DECLARE_DEFAULT_ALLOCATORS(ALCcaptureOSS)
  429. DEFINE_ALCBACKEND_VTABLE(ALCcaptureOSS);
  430. static int ALCcaptureOSS_recordProc(void *ptr)
  431. {
  432. ALCcaptureOSS *self = (ALCcaptureOSS*)ptr;
  433. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  434. int frameSize;
  435. int amt;
  436. SetRTPriority();
  437. althrd_setname(althrd_current(), RECORD_THREAD_NAME);
  438. frameSize = FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
  439. while(!self->killNow)
  440. {
  441. amt = read(self->fd, self->read_data, self->data_size);
  442. if(amt < 0)
  443. {
  444. ERR("read failed: %s\n", strerror(errno));
  445. ALCcaptureOSS_lock(self);
  446. aluHandleDisconnect(device);
  447. ALCcaptureOSS_unlock(self);
  448. break;
  449. }
  450. if(amt == 0)
  451. {
  452. al_nssleep(1000000);
  453. continue;
  454. }
  455. if(self->doCapture)
  456. WriteRingBuffer(self->ring, self->read_data, amt/frameSize);
  457. }
  458. return 0;
  459. }
  460. static void ALCcaptureOSS_Construct(ALCcaptureOSS *self, ALCdevice *device)
  461. {
  462. ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
  463. SET_VTABLE2(ALCcaptureOSS, ALCbackend, self);
  464. }
  465. static ALCenum ALCcaptureOSS_open(ALCcaptureOSS *self, const ALCchar *name)
  466. {
  467. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  468. struct oss_device *dev = &oss_capture;
  469. int numFragmentsLogSize;
  470. int log2FragmentSize;
  471. unsigned int periods;
  472. audio_buf_info info;
  473. ALuint frameSize;
  474. int numChannels;
  475. int ossFormat;
  476. int ossSpeed;
  477. char *err;
  478. if(!name)
  479. name = dev->handle;
  480. else
  481. {
  482. while (dev != NULL)
  483. {
  484. if (strcmp(dev->handle, name) == 0)
  485. break;
  486. dev = dev->next;
  487. }
  488. if (dev == NULL)
  489. return ALC_INVALID_VALUE;
  490. }
  491. self->fd = open(dev->path, O_RDONLY);
  492. if(self->fd == -1)
  493. {
  494. ERR("Could not open %s: %s\n", dev->path, strerror(errno));
  495. return ALC_INVALID_VALUE;
  496. }
  497. switch(device->FmtType)
  498. {
  499. case DevFmtByte:
  500. ossFormat = AFMT_S8;
  501. break;
  502. case DevFmtUByte:
  503. ossFormat = AFMT_U8;
  504. break;
  505. case DevFmtShort:
  506. ossFormat = AFMT_S16_NE;
  507. break;
  508. case DevFmtUShort:
  509. case DevFmtInt:
  510. case DevFmtUInt:
  511. case DevFmtFloat:
  512. ERR("%s capture samples not supported\n", DevFmtTypeString(device->FmtType));
  513. return ALC_INVALID_VALUE;
  514. }
  515. periods = 4;
  516. numChannels = ChannelsFromDevFmt(device->FmtChans);
  517. frameSize = numChannels * BytesFromDevFmt(device->FmtType);
  518. ossSpeed = device->Frequency;
  519. log2FragmentSize = log2i(device->UpdateSize * device->NumUpdates *
  520. frameSize / periods);
  521. /* according to the OSS spec, 16 bytes are the minimum */
  522. if (log2FragmentSize < 4)
  523. log2FragmentSize = 4;
  524. numFragmentsLogSize = (periods << 16) | log2FragmentSize;
  525. #define CHECKERR(func) if((func) < 0) { \
  526. err = #func; \
  527. goto err; \
  528. }
  529. CHECKERR(ioctl(self->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize));
  530. CHECKERR(ioctl(self->fd, SNDCTL_DSP_SETFMT, &ossFormat));
  531. CHECKERR(ioctl(self->fd, SNDCTL_DSP_CHANNELS, &numChannels));
  532. CHECKERR(ioctl(self->fd, SNDCTL_DSP_SPEED, &ossSpeed));
  533. CHECKERR(ioctl(self->fd, SNDCTL_DSP_GETISPACE, &info));
  534. if(0)
  535. {
  536. err:
  537. ERR("%s failed: %s\n", err, strerror(errno));
  538. close(self->fd);
  539. self->fd = -1;
  540. return ALC_INVALID_VALUE;
  541. }
  542. #undef CHECKERR
  543. if((int)ChannelsFromDevFmt(device->FmtChans) != numChannels)
  544. {
  545. ERR("Failed to set %s, got %d channels instead\n", DevFmtChannelsString(device->FmtChans), numChannels);
  546. close(self->fd);
  547. self->fd = -1;
  548. return ALC_INVALID_VALUE;
  549. }
  550. if(!((ossFormat == AFMT_S8 && device->FmtType == DevFmtByte) ||
  551. (ossFormat == AFMT_U8 && device->FmtType == DevFmtUByte) ||
  552. (ossFormat == AFMT_S16_NE && device->FmtType == DevFmtShort)))
  553. {
  554. ERR("Failed to set %s samples, got OSS format %#x\n", DevFmtTypeString(device->FmtType), ossFormat);
  555. close(self->fd);
  556. self->fd = -1;
  557. return ALC_INVALID_VALUE;
  558. }
  559. self->ring = CreateRingBuffer(frameSize, device->UpdateSize * device->NumUpdates);
  560. if(!self->ring)
  561. {
  562. ERR("Ring buffer create failed\n");
  563. close(self->fd);
  564. self->fd = -1;
  565. return ALC_OUT_OF_MEMORY;
  566. }
  567. self->data_size = info.fragsize;
  568. self->read_data = calloc(1, self->data_size);
  569. self->killNow = 0;
  570. if(althrd_create(&self->thread, ALCcaptureOSS_recordProc, self) != althrd_success)
  571. {
  572. device->ExtraData = NULL;
  573. close(self->fd);
  574. self->fd = -1;
  575. return ALC_OUT_OF_MEMORY;
  576. }
  577. al_string_copy_cstr(&device->DeviceName, name);
  578. return ALC_NO_ERROR;
  579. }
  580. static void ALCcaptureOSS_close(ALCcaptureOSS *self)
  581. {
  582. int res;
  583. self->killNow = 1;
  584. althrd_join(self->thread, &res);
  585. close(self->fd);
  586. self->fd = -1;
  587. DestroyRingBuffer(self->ring);
  588. self->ring = NULL;
  589. free(self->read_data);
  590. self->read_data = NULL;
  591. }
  592. static ALCboolean ALCcaptureOSS_start(ALCcaptureOSS *self)
  593. {
  594. self->doCapture = 1;
  595. return ALC_TRUE;
  596. }
  597. static void ALCcaptureOSS_stop(ALCcaptureOSS *self)
  598. {
  599. self->doCapture = 0;
  600. }
  601. static ALCenum ALCcaptureOSS_captureSamples(ALCcaptureOSS *self, ALCvoid *buffer, ALCuint samples)
  602. {
  603. ReadRingBuffer(self->ring, buffer, samples);
  604. return ALC_NO_ERROR;
  605. }
  606. static ALCuint ALCcaptureOSS_availableSamples(ALCcaptureOSS *self)
  607. {
  608. return RingBufferSize(self->ring);
  609. }
  610. typedef struct ALCossBackendFactory {
  611. DERIVE_FROM_TYPE(ALCbackendFactory);
  612. } ALCossBackendFactory;
  613. #define ALCOSSBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCossBackendFactory, ALCbackendFactory) } }
  614. ALCbackendFactory *ALCossBackendFactory_getFactory(void);
  615. static ALCboolean ALCossBackendFactory_init(ALCossBackendFactory *self);
  616. static void ALCossBackendFactory_deinit(ALCossBackendFactory *self);
  617. static ALCboolean ALCossBackendFactory_querySupport(ALCossBackendFactory *self, ALCbackend_Type type);
  618. static void ALCossBackendFactory_probe(ALCossBackendFactory *self, enum DevProbe type);
  619. static ALCbackend* ALCossBackendFactory_createBackend(ALCossBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
  620. DEFINE_ALCBACKENDFACTORY_VTABLE(ALCossBackendFactory);
  621. ALCbackendFactory *ALCossBackendFactory_getFactory(void)
  622. {
  623. static ALCossBackendFactory factory = ALCOSSBACKENDFACTORY_INITIALIZER;
  624. return STATIC_CAST(ALCbackendFactory, &factory);
  625. }
  626. ALCboolean ALCossBackendFactory_init(ALCossBackendFactory* UNUSED(self))
  627. {
  628. ConfigValueStr(NULL, "oss", "device", &oss_playback.path);
  629. ConfigValueStr(NULL, "oss", "capture", &oss_capture.path);
  630. return ALC_TRUE;
  631. }
  632. void ALCossBackendFactory_deinit(ALCossBackendFactory* UNUSED(self))
  633. {
  634. ALCossListFree(&oss_playback);
  635. ALCossListFree(&oss_capture);
  636. }
  637. ALCboolean ALCossBackendFactory_querySupport(ALCossBackendFactory* UNUSED(self), ALCbackend_Type type)
  638. {
  639. if(type == ALCbackend_Playback || type == ALCbackend_Capture)
  640. return ALC_TRUE;
  641. return ALC_FALSE;
  642. }
  643. void ALCossBackendFactory_probe(ALCossBackendFactory* UNUSED(self), enum DevProbe type)
  644. {
  645. switch(type)
  646. {
  647. case ALL_DEVICE_PROBE:
  648. {
  649. struct oss_device *cur = &oss_playback;
  650. ALCossListFree(cur);
  651. ALCossListPopulate(cur, NULL);
  652. while (cur != NULL)
  653. {
  654. AppendAllDevicesList(cur->handle);
  655. cur = cur->next;
  656. }
  657. }
  658. break;
  659. case CAPTURE_DEVICE_PROBE:
  660. {
  661. struct oss_device *cur = &oss_capture;
  662. ALCossListFree(cur);
  663. ALCossListPopulate(NULL, cur);
  664. while (cur != NULL)
  665. {
  666. AppendCaptureDeviceList(cur->handle);
  667. cur = cur->next;
  668. }
  669. }
  670. break;
  671. }
  672. }
  673. ALCbackend* ALCossBackendFactory_createBackend(ALCossBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
  674. {
  675. if(type == ALCbackend_Playback)
  676. {
  677. ALCplaybackOSS *backend;
  678. NEW_OBJ(backend, ALCplaybackOSS)(device);
  679. if(!backend) return NULL;
  680. return STATIC_CAST(ALCbackend, backend);
  681. }
  682. if(type == ALCbackend_Capture)
  683. {
  684. ALCcaptureOSS *backend;
  685. NEW_OBJ(backend, ALCcaptureOSS)(device);
  686. if(!backend) return NULL;
  687. return STATIC_CAST(ALCbackend, backend);
  688. }
  689. return NULL;
  690. }