jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

892 lines
30KB

  1. /*
  2. Copyright (C) 2003-2007 Jussi Laako <jussi@sonarnerd.net>
  3. Copyright (C) 2008 Grame & RTL 2008
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include "driver_interface.h"
  17. #include "JackThreadedDriver.h"
  18. #include "JackDriverLoader.h"
  19. #include "JackOSSDriver.h"
  20. #include "JackEngineControl.h"
  21. #include "JackGraphManager.h"
  22. #include "JackError.h"
  23. #include "JackTime.h"
  24. #include "JackShmMem.h"
  25. #include "memops.h"
  26. #include <sys/ioctl.h>
  27. #include <sys/soundcard.h>
  28. #include <fcntl.h>
  29. #include <iostream>
  30. #include <assert.h>
  31. #include <stdio.h>
  32. using namespace std;
  33. namespace Jack
  34. {
  35. #ifdef JACK_MONITOR
  36. #define CYCLE_POINTS 500000
  37. struct OSSCycle {
  38. jack_time_t fBeforeRead;
  39. jack_time_t fAfterRead;
  40. jack_time_t fAfterReadConvert;
  41. jack_time_t fBeforeWrite;
  42. jack_time_t fAfterWrite;
  43. jack_time_t fBeforeWriteConvert;
  44. };
  45. struct OSSCycleTable {
  46. jack_time_t fBeforeFirstWrite;
  47. jack_time_t fAfterFirstWrite;
  48. OSSCycle fTable[CYCLE_POINTS];
  49. };
  50. OSSCycleTable gCycleTable;
  51. int gCycleCount = 0;
  52. #endif
  53. inline int int2pow2(int x) { int r = 0; while ((1 << r) < x) r++; return r; }
  54. static inline void CopyAndConvertIn(jack_sample_t *dst, void *src, size_t nframes, int channel, int chcount, int bits)
  55. {
  56. switch (bits) {
  57. case 16: {
  58. signed short *s16src = (signed short*)src;
  59. s16src += channel;
  60. sample_move_dS_s16(dst, (char*)s16src, nframes, chcount<<1);
  61. break;
  62. }
  63. case 24: {
  64. signed int *s32src = (signed int*)src;
  65. s32src += channel;
  66. sample_move_dS_s24(dst, (char*)s32src, nframes, chcount<<2);
  67. break;
  68. }
  69. case 32: {
  70. signed int *s32src = (signed int*)src;
  71. s32src += channel;
  72. sample_move_dS_s32u24(dst, (char*)s32src, nframes, chcount<<2);
  73. break;
  74. }
  75. }
  76. }
  77. static inline void CopyAndConvertOut(void *dst, jack_sample_t *src, size_t nframes, int channel, int chcount, int bits)
  78. {
  79. switch (bits) {
  80. case 16: {
  81. signed short *s16dst = (signed short*)dst;
  82. s16dst += channel;
  83. sample_move_d16_sS((char*)s16dst, src, nframes, chcount<<1, NULL); // No dithering for now...
  84. break;
  85. }
  86. case 24: {
  87. signed int *s32dst = (signed int*)dst;
  88. s32dst += channel;
  89. sample_move_d24_sS((char*)s32dst, src, nframes, chcount<<2, NULL); // No dithering for now...
  90. break;
  91. }
  92. case 32: {
  93. signed int *s32dst = (signed int*)dst;
  94. s32dst += channel;
  95. sample_move_d32u24_sS((char*)s32dst, src, nframes, chcount<<2, NULL);
  96. break;
  97. }
  98. }
  99. }
  100. void JackOSSDriver::SetSampleFormat()
  101. {
  102. switch (fBits) {
  103. case 24: /* native-endian LSB aligned 24-bits in 32-bits integer */
  104. fSampleFormat = AFMT_S24_NE;
  105. fSampleSize = sizeof(int);
  106. break;
  107. case 32: /* native-endian 32-bit integer */
  108. fSampleFormat = AFMT_S32_NE;
  109. fSampleSize = sizeof(int);
  110. break;
  111. case 16: /* native-endian 16-bit integer */
  112. default:
  113. fSampleFormat = AFMT_S16_NE;
  114. fSampleSize = sizeof(short);
  115. break;
  116. }
  117. }
  118. void JackOSSDriver::DisplayDeviceInfo()
  119. {
  120. audio_buf_info info;
  121. oss_audioinfo ai_in, ai_out;
  122. memset(&info, 0, sizeof(audio_buf_info));
  123. int cap = 0;
  124. // Duplex cards : http://manuals.opensound.com/developer/full_duplex.html
  125. jack_info("Audio Interface Description :");
  126. jack_info("Sampling Frequency : %d, Sample Format : %d, Mode : %d", fEngineControl->fSampleRate, fSampleFormat, fRWMode);
  127. if (fRWMode & kWrite) {
  128. #ifdef OSS_SYSINFO
  129. oss_sysinfo si;
  130. if (ioctl(fOutFD, OSS_SYSINFO, &si) == -1) {
  131. jack_error("JackOSSDriver::DisplayDeviceInfo OSS_SYSINFO failed : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  132. } else {
  133. jack_info("OSS product %s", si.product);
  134. jack_info("OSS version %s", si.version);
  135. jack_info("OSS version num %d", si.versionnum);
  136. jack_info("OSS numaudios %d", si.numaudios);
  137. jack_info("OSS numaudioengines %d", si.numaudioengines);
  138. jack_info("OSS numcards %d", si.numcards);
  139. }
  140. #endif
  141. jack_info("Output capabilities - %d channels : ", fPlaybackChannels);
  142. jack_info("Output block size = %d", fOutputBufferSize);
  143. if (ioctl(fOutFD, SNDCTL_DSP_GETOSPACE, &info) == -1) {
  144. jack_error("JackOSSDriver::DisplayDeviceInfo SNDCTL_DSP_GETOSPACE failed : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  145. } else {
  146. jack_info("output space info: fragments = %d, fragstotal = %d, fragsize = %d, bytes = %d",
  147. info.fragments, info.fragstotal, info.fragsize, info.bytes);
  148. }
  149. if (ioctl(fOutFD, SNDCTL_DSP_GETCAPS, &cap) == -1) {
  150. jack_error("JackOSSDriver::DisplayDeviceInfo SNDCTL_DSP_GETCAPS failed : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  151. } else {
  152. if (cap & DSP_CAP_DUPLEX) jack_info(" DSP_CAP_DUPLEX");
  153. if (cap & DSP_CAP_REALTIME) jack_info(" DSP_CAP_REALTIME");
  154. if (cap & DSP_CAP_BATCH) jack_info(" DSP_CAP_BATCH");
  155. if (cap & DSP_CAP_COPROC) jack_info(" DSP_CAP_COPROC");
  156. if (cap & DSP_CAP_TRIGGER) jack_info(" DSP_CAP_TRIGGER");
  157. if (cap & DSP_CAP_MMAP) jack_info(" DSP_CAP_MMAP");
  158. if (cap & DSP_CAP_MULTI) jack_info(" DSP_CAP_MULTI");
  159. if (cap & DSP_CAP_BIND) jack_info(" DSP_CAP_BIND");
  160. }
  161. }
  162. if (fRWMode & kRead) {
  163. #ifdef OSS_SYSINFO
  164. oss_sysinfo si;
  165. if (ioctl(fInFD, OSS_SYSINFO, &si) == -1) {
  166. jack_error("JackOSSDriver::DisplayDeviceInfo OSS_SYSINFO failed : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  167. } else {
  168. jack_info("OSS product %s", si.product);
  169. jack_info("OSS version %s", si.version);
  170. jack_info("OSS version num %d", si.versionnum);
  171. jack_info("OSS numaudios %d", si.numaudios);
  172. jack_info("OSS numaudioengines %d", si.numaudioengines);
  173. jack_info("OSS numcards %d", si.numcards);
  174. }
  175. #endif
  176. jack_info("Input capabilities - %d channels : ", fCaptureChannels);
  177. jack_info("Input block size = %d", fInputBufferSize);
  178. if (ioctl(fInFD, SNDCTL_DSP_GETISPACE, &info) == -1) {
  179. jack_error("JackOSSDriver::DisplayDeviceInfo SNDCTL_DSP_GETOSPACE failed : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  180. } else {
  181. jack_info("input space info: fragments = %d, fragstotal = %d, fragsize = %d, bytes = %d",
  182. info.fragments, info.fragstotal, info.fragsize, info.bytes);
  183. }
  184. if (ioctl(fInFD, SNDCTL_DSP_GETCAPS, &cap) == -1) {
  185. jack_error("JackOSSDriver::DisplayDeviceInfo SNDCTL_DSP_GETCAPS failed : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  186. } else {
  187. if (cap & DSP_CAP_DUPLEX) jack_info(" DSP_CAP_DUPLEX");
  188. if (cap & DSP_CAP_REALTIME) jack_info(" DSP_CAP_REALTIME");
  189. if (cap & DSP_CAP_BATCH) jack_info(" DSP_CAP_BATCH");
  190. if (cap & DSP_CAP_COPROC) jack_info(" DSP_CAP_COPROC");
  191. if (cap & DSP_CAP_TRIGGER) jack_info(" DSP_CAP_TRIGGER");
  192. if (cap & DSP_CAP_MMAP) jack_info(" DSP_CAP_MMAP");
  193. if (cap & DSP_CAP_MULTI) jack_info(" DSP_CAP_MULTI");
  194. if (cap & DSP_CAP_BIND) jack_info(" DSP_CAP_BIND");
  195. }
  196. }
  197. if (ai_in.rate_source != ai_out.rate_source) {
  198. jack_info("Warning : input and output are not necessarily driven by the same clock!");
  199. }
  200. }
  201. int JackOSSDriver::OpenInput()
  202. {
  203. int flags = 0;
  204. int gFragFormat;
  205. int cur_capture_channels;
  206. int cur_sample_format;
  207. jack_nframes_t cur_sample_rate;
  208. if (fCaptureChannels == 0) fCaptureChannels = 2;
  209. if ((fInFD = open(fCaptureDriverName, O_RDONLY | ((fExcl) ? O_EXCL : 0))) < 0) {
  210. jack_error("JackOSSDriver::OpenInput failed to open device : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  211. return -1;
  212. }
  213. jack_log("JackOSSDriver::OpenInput input fInFD = %d", fInFD);
  214. if (fExcl) {
  215. if (ioctl(fInFD, SNDCTL_DSP_COOKEDMODE, &flags) == -1) {
  216. jack_error("JackOSSDriver::OpenInput failed to set cooked mode : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  217. goto error;
  218. }
  219. }
  220. gFragFormat = (2 << 16) + int2pow2(fEngineControl->fBufferSize * fSampleSize * fCaptureChannels);
  221. if (ioctl(fInFD, SNDCTL_DSP_SETFRAGMENT, &gFragFormat) == -1) {
  222. jack_error("JackOSSDriver::OpenInput failed to set fragments : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  223. goto error;
  224. }
  225. cur_sample_format = fSampleFormat;
  226. if (ioctl(fInFD, SNDCTL_DSP_SETFMT, &fSampleFormat) == -1) {
  227. jack_error("JackOSSDriver::OpenInput failed to set format : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  228. goto error;
  229. }
  230. if (cur_sample_format != fSampleFormat) {
  231. jack_info("JackOSSDriver::OpenInput driver forced the sample format %ld", fSampleFormat);
  232. }
  233. cur_capture_channels = fCaptureChannels;
  234. if (ioctl(fInFD, SNDCTL_DSP_CHANNELS, &fCaptureChannels) == -1) {
  235. jack_error("JackOSSDriver::OpenInput failed to set channels : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  236. goto error;
  237. }
  238. if (cur_capture_channels != fCaptureChannels) {
  239. jack_info("JackOSSDriver::OpenInput driver forced the number of capture channels %ld", fCaptureChannels);
  240. }
  241. cur_sample_rate = fEngineControl->fSampleRate;
  242. if (ioctl(fInFD, SNDCTL_DSP_SPEED, &fEngineControl->fSampleRate) == -1) {
  243. jack_error("JackOSSDriver::OpenInput failed to set sample rate : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  244. goto error;
  245. }
  246. if (cur_sample_rate != fEngineControl->fSampleRate) {
  247. jack_info("JackOSSDriver::OpenInput driver forced the sample rate %ld", fEngineControl->fSampleRate);
  248. }
  249. fInputBufferSize = 0;
  250. if (ioctl(fInFD, SNDCTL_DSP_GETBLKSIZE, &fInputBufferSize) == -1) {
  251. jack_error("JackOSSDriver::OpenInput failed to get fragments : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  252. goto error;
  253. }
  254. if (fInputBufferSize != fEngineControl->fBufferSize * fSampleSize * fCaptureChannels) {
  255. if (fIgnoreHW) {
  256. int new_buffer_size = fInputBufferSize / (fSampleSize * fCaptureChannels);
  257. jack_info("JackOSSDriver::OpenInput driver forced buffer size %ld", new_buffer_size);
  258. JackAudioDriver::SetBufferSize(new_buffer_size); // never fails
  259. } else {
  260. jack_error("JackOSSDriver::OpenInput wanted buffer size cannot be obtained");
  261. goto error;
  262. }
  263. }
  264. fInputBuffer = (void*)calloc(fInputBufferSize, 1);
  265. assert(fInputBuffer);
  266. return 0;
  267. error:
  268. ::close(fInFD);
  269. return -1;
  270. }
  271. int JackOSSDriver::OpenOutput()
  272. {
  273. int flags = 0;
  274. int gFragFormat;
  275. int cur_sample_format;
  276. int cur_playback_channels;
  277. jack_nframes_t cur_sample_rate;
  278. if (fPlaybackChannels == 0) fPlaybackChannels = 2;
  279. if ((fOutFD = open(fPlaybackDriverName, O_WRONLY | ((fExcl) ? O_EXCL : 0))) < 0) {
  280. jack_error("JackOSSDriver::OpenOutput failed to open device : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  281. return -1;
  282. }
  283. jack_log("JackOSSDriver::OpenOutput output fOutFD = %d", fOutFD);
  284. if (fExcl) {
  285. if (ioctl(fOutFD, SNDCTL_DSP_COOKEDMODE, &flags) == -1) {
  286. jack_error("JackOSSDriver::OpenOutput failed to set cooked mode : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  287. goto error;
  288. }
  289. }
  290. gFragFormat = (2 << 16) + int2pow2(fEngineControl->fBufferSize * fSampleSize * fPlaybackChannels);
  291. if (ioctl(fOutFD, SNDCTL_DSP_SETFRAGMENT, &gFragFormat) == -1) {
  292. jack_error("JackOSSDriver::OpenOutput failed to set fragments : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  293. goto error;
  294. }
  295. cur_sample_format = fSampleFormat;
  296. if (ioctl(fOutFD, SNDCTL_DSP_SETFMT, &fSampleFormat) == -1) {
  297. jack_error("JackOSSDriver::OpenOutput failed to set format : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  298. goto error;
  299. }
  300. if (cur_sample_format != fSampleFormat) {
  301. jack_info("JackOSSDriver::OpenOutput driver forced the sample format %ld", fSampleFormat);
  302. }
  303. cur_playback_channels = fPlaybackChannels;
  304. if (ioctl(fOutFD, SNDCTL_DSP_CHANNELS, &fPlaybackChannels) == -1) {
  305. jack_error("JackOSSDriver::OpenOutput failed to set channels : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  306. goto error;
  307. }
  308. if (cur_playback_channels != fPlaybackChannels) {
  309. jack_info("JackOSSDriver::OpenOutput driver forced the number of playback channels %ld", fPlaybackChannels);
  310. }
  311. cur_sample_rate = fEngineControl->fSampleRate;
  312. if (ioctl(fOutFD, SNDCTL_DSP_SPEED, &fEngineControl->fSampleRate) == -1) {
  313. jack_error("JackOSSDriver::OpenOutput failed to set sample rate : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  314. goto error;
  315. }
  316. if (cur_sample_rate != fEngineControl->fSampleRate) {
  317. jack_info("JackOSSDriver::OpenInput driver forced the sample rate %ld", fEngineControl->fSampleRate);
  318. }
  319. fOutputBufferSize = 0;
  320. if (ioctl(fOutFD, SNDCTL_DSP_GETBLKSIZE, &fOutputBufferSize) == -1) {
  321. jack_error("JackOSSDriver::OpenOutput failed to get fragments : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  322. goto error;
  323. }
  324. if (fOutputBufferSize != fEngineControl->fBufferSize * fSampleSize * fPlaybackChannels) {
  325. if (fIgnoreHW) {
  326. int new_buffer_size = fOutputBufferSize / (fSampleSize * fPlaybackChannels);
  327. jack_info("JackOSSDriver::OpenOutput driver forced buffer size %ld", new_buffer_size);
  328. JackAudioDriver::SetBufferSize(new_buffer_size); // never fails
  329. } else {
  330. jack_error("JackOSSDriver::OpenInput wanted buffer size cannot be obtained");
  331. goto error;
  332. }
  333. }
  334. fOutputBuffer = (void*)calloc(fOutputBufferSize, 1);
  335. fFirstCycle = true;
  336. assert(fOutputBuffer);
  337. return 0;
  338. error:
  339. ::close(fOutFD);
  340. return -1;
  341. }
  342. int JackOSSDriver::Open(jack_nframes_t nframes,
  343. int user_nperiods,
  344. jack_nframes_t samplerate,
  345. bool capturing,
  346. bool playing,
  347. int inchannels,
  348. int outchannels,
  349. bool excl,
  350. bool monitor,
  351. const char* capture_driver_uid,
  352. const char* playback_driver_uid,
  353. jack_nframes_t capture_latency,
  354. jack_nframes_t playback_latency,
  355. int bits,
  356. bool ignorehwbuf)
  357. {
  358. // Generic JackAudioDriver Open
  359. if (JackAudioDriver::Open(nframes, samplerate, capturing, playing, inchannels, outchannels, monitor,
  360. capture_driver_uid, playback_driver_uid, capture_latency, playback_latency) != 0) {
  361. return -1;
  362. } else {
  363. if (!fEngineControl->fSyncMode) {
  364. jack_error("Cannot run in asynchronous mode, use the -S parameter for jackd");
  365. return -1;
  366. }
  367. fRWMode |= ((capturing) ? kRead : 0);
  368. fRWMode |= ((playing) ? kWrite : 0);
  369. fBits = bits;
  370. fIgnoreHW = ignorehwbuf;
  371. fNperiods = user_nperiods;
  372. fExcl = excl;
  373. #ifdef JACK_MONITOR
  374. // Force memory page in
  375. memset(&gCycleTable, 0, sizeof(gCycleTable));
  376. #endif
  377. if (OpenAux() < 0) {
  378. Close();
  379. return -1;
  380. } else {
  381. return 0;
  382. }
  383. }
  384. }
  385. int JackOSSDriver::Close()
  386. {
  387. #ifdef JACK_MONITOR
  388. FILE* file = fopen("OSSProfiling.log", "w");
  389. if (file) {
  390. jack_info("Writing OSS driver timing data....");
  391. for (int i = 1; i < gCycleCount; i++) {
  392. int d1 = gCycleTable.fTable[i].fAfterRead - gCycleTable.fTable[i].fBeforeRead;
  393. int d2 = gCycleTable.fTable[i].fAfterReadConvert - gCycleTable.fTable[i].fAfterRead;
  394. int d3 = gCycleTable.fTable[i].fAfterWrite - gCycleTable.fTable[i].fBeforeWrite;
  395. int d4 = gCycleTable.fTable[i].fBeforeWrite - gCycleTable.fTable[i].fBeforeWriteConvert;
  396. fprintf(file, "%d \t %d \t %d \t %d \t \n", d1, d2, d3, d4);
  397. }
  398. fclose(file);
  399. } else {
  400. jack_error("JackOSSDriver::Close : cannot open OSSProfiling.log file");
  401. }
  402. file = fopen("TimingOSS.plot", "w");
  403. if (file == NULL) {
  404. jack_error("JackOSSDriver::Close cannot open TimingOSS.plot file");
  405. } else {
  406. fprintf(file, "set grid\n");
  407. fprintf(file, "set title \"OSS audio driver timing\"\n");
  408. fprintf(file, "set xlabel \"audio cycles\"\n");
  409. fprintf(file, "set ylabel \"usec\"\n");
  410. fprintf(file, "plot \"OSSProfiling.log\" using 1 title \"Driver read wait\" with lines, \
  411. \"OSSProfiling.log\" using 2 title \"Driver read convert duration\" with lines, \
  412. \"OSSProfiling.log\" using 3 title \"Driver write wait\" with lines, \
  413. \"OSSProfiling.log\" using 4 title \"Driver write convert duration\" with lines\n");
  414. fprintf(file, "set output 'TimingOSS.pdf\n");
  415. fprintf(file, "set terminal pdf\n");
  416. fprintf(file, "set grid\n");
  417. fprintf(file, "set title \"OSS audio driver timing\"\n");
  418. fprintf(file, "set xlabel \"audio cycles\"\n");
  419. fprintf(file, "set ylabel \"usec\"\n");
  420. fprintf(file, "plot \"OSSProfiling.log\" using 1 title \"Driver read wait\" with lines, \
  421. \"OSSProfiling.log\" using 2 title \"Driver read convert duration\" with lines, \
  422. \"OSSProfiling.log\" using 3 title \"Driver write wait\" with lines, \
  423. \"OSSProfiling.log\" using 4 title \"Driver write convert duration\" with lines\n");
  424. fclose(file);
  425. }
  426. #endif
  427. int res = JackAudioDriver::Close();
  428. CloseAux();
  429. return res;
  430. }
  431. int JackOSSDriver::OpenAux()
  432. {
  433. SetSampleFormat();
  434. if ((fRWMode & kRead) && (OpenInput() < 0)) {
  435. return -1;
  436. }
  437. if ((fRWMode & kWrite) && (OpenOutput() < 0)) {
  438. return -1;
  439. }
  440. // In duplex mode, check that input and output use the same buffer size
  441. /*
  442. 10/02/09 : deactivated for now, needs more check (only needed when *same* device is used for input and output ??)
  443. if ((fRWMode & kRead) && (fRWMode & kWrite) && (fInputBufferSize != fOutputBufferSize)) {
  444. jack_error("JackOSSDriver::OpenAux input and output buffer size are not the same!!");
  445. return -1;
  446. }
  447. */
  448. DisplayDeviceInfo();
  449. return 0;
  450. }
  451. void JackOSSDriver::CloseAux()
  452. {
  453. if (fRWMode & kRead && fInFD > 0) {
  454. close(fInFD);
  455. fInFD = -1;
  456. }
  457. if (fRWMode & kWrite && fOutFD > 0) {
  458. close(fOutFD);
  459. fOutFD = -1;
  460. }
  461. if (fInputBuffer)
  462. free(fInputBuffer);
  463. fInputBuffer = NULL;
  464. if (fOutputBuffer)
  465. free(fOutputBuffer);
  466. fOutputBuffer = NULL;
  467. }
  468. int JackOSSDriver::Read()
  469. {
  470. if (fInFD < 0) {
  471. // Keep begin cycle time
  472. JackDriver::CycleTakeBeginTime();
  473. return 0;
  474. }
  475. ssize_t count;
  476. #ifdef JACK_MONITOR
  477. gCycleTable.fTable[gCycleCount].fBeforeRead = GetMicroSeconds();
  478. #endif
  479. audio_errinfo ei_in;
  480. count = ::read(fInFD, fInputBuffer, fInputBufferSize);
  481. #ifdef JACK_MONITOR
  482. if (count > 0 && count != (int)fInputBufferSize)
  483. jack_log("JackOSSDriver::Read count = %ld", count / (fSampleSize * fCaptureChannels));
  484. gCycleTable.fTable[gCycleCount].fAfterRead = GetMicroSeconds();
  485. #endif
  486. // XRun detection
  487. if (ioctl(fInFD, SNDCTL_DSP_GETERROR, &ei_in) == 0) {
  488. if (ei_in.rec_overruns > 0 ) {
  489. jack_error("JackOSSDriver::Read overruns");
  490. jack_time_t cur_time = GetMicroSeconds();
  491. NotifyXRun(cur_time, float(cur_time - fBeginDateUst)); // Better this value than nothing...
  492. }
  493. if (ei_in.rec_errorcount > 0 && ei_in.rec_lasterror != 0) {
  494. jack_error("%d OSS rec event(s), last=%05d:%d", ei_in.rec_errorcount, ei_in.rec_lasterror, ei_in.rec_errorparm);
  495. }
  496. }
  497. if (count < 0) {
  498. jack_log("JackOSSDriver::Read error = %s", strerror(errno));
  499. return -1;
  500. } else if (count < (int)fInputBufferSize) {
  501. jack_error("JackOSSDriver::Read error bytes read = %ld", count);
  502. return -1;
  503. } else {
  504. // Keep begin cycle time
  505. JackDriver::CycleTakeBeginTime();
  506. for (int i = 0; i < fCaptureChannels; i++) {
  507. if (fGraphManager->GetConnectionsNum(fCapturePortList[i]) > 0) {
  508. CopyAndConvertIn(GetInputBuffer(i), fInputBuffer, fEngineControl->fBufferSize, i, fCaptureChannels, fBits);
  509. }
  510. }
  511. #ifdef JACK_MONITOR
  512. gCycleTable.fTable[gCycleCount].fAfterReadConvert = GetMicroSeconds();
  513. #endif
  514. return 0;
  515. }
  516. }
  517. int JackOSSDriver::Write()
  518. {
  519. if (fOutFD < 0) {
  520. // Keep end cycle time
  521. JackDriver::CycleTakeEndTime();
  522. return 0;
  523. }
  524. ssize_t count;
  525. audio_errinfo ei_out;
  526. // Maybe necessary to write an empty output buffer first time : see http://manuals.opensound.com/developer/fulldup.c.html
  527. if (fFirstCycle) {
  528. fFirstCycle = false;
  529. memset(fOutputBuffer, 0, fOutputBufferSize);
  530. // Prefill output buffer
  531. for (int i = 0; i < fNperiods; i++) {
  532. count = ::write(fOutFD, fOutputBuffer, fOutputBufferSize);
  533. if (count < (int)fOutputBufferSize) {
  534. jack_error("JackOSSDriver::Write error bytes written = %ld", count);
  535. return -1;
  536. }
  537. }
  538. int delay;
  539. if (ioctl(fOutFD, SNDCTL_DSP_GETODELAY, &delay) == -1) {
  540. jack_error("JackOSSDriver::Write error get out delay : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  541. return -1;
  542. }
  543. delay /= fSampleSize * fPlaybackChannels;
  544. jack_info("JackOSSDriver::Write output latency frames = %ld", delay);
  545. }
  546. #ifdef JACK_MONITOR
  547. gCycleTable.fTable[gCycleCount].fBeforeWriteConvert = GetMicroSeconds();
  548. #endif
  549. memset(fOutputBuffer, 0, fOutputBufferSize);
  550. for (int i = 0; i < fPlaybackChannels; i++) {
  551. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
  552. CopyAndConvertOut(fOutputBuffer, GetOutputBuffer(i), fEngineControl->fBufferSize, i, fPlaybackChannels, fBits);
  553. }
  554. }
  555. #ifdef JACK_MONITOR
  556. gCycleTable.fTable[gCycleCount].fBeforeWrite = GetMicroSeconds();
  557. #endif
  558. // Keep end cycle time
  559. JackDriver::CycleTakeEndTime();
  560. count = ::write(fOutFD, fOutputBuffer, fOutputBufferSize);
  561. #ifdef JACK_MONITOR
  562. if (count > 0 && count != (int)fOutputBufferSize)
  563. jack_log("JackOSSDriver::Write count = %ld", count / (fSampleSize * fPlaybackChannels));
  564. gCycleTable.fTable[gCycleCount].fAfterWrite = GetMicroSeconds();
  565. gCycleCount = (gCycleCount == CYCLE_POINTS - 1) ? gCycleCount: gCycleCount + 1;
  566. #endif
  567. // XRun detection
  568. if (ioctl(fOutFD, SNDCTL_DSP_GETERROR, &ei_out) == 0) {
  569. if (ei_out.play_underruns > 0) {
  570. jack_error("JackOSSDriver::Write underruns");
  571. jack_time_t cur_time = GetMicroSeconds();
  572. NotifyXRun(cur_time, float(cur_time - fBeginDateUst)); // Better this value than nothing...
  573. }
  574. if (ei_out.play_errorcount > 0 && ei_out.play_lasterror != 0) {
  575. jack_error("%d OSS play event(s), last=%05d:%d",ei_out.play_errorcount, ei_out.play_lasterror, ei_out.play_errorparm);
  576. }
  577. }
  578. if (count < 0) {
  579. jack_log("JackOSSDriver::Write error = %s", strerror(errno));
  580. return -1;
  581. } else if (count < (int)fOutputBufferSize) {
  582. jack_error("JackOSSDriver::Write error bytes written = %ld", count);
  583. return -1;
  584. } else {
  585. return 0;
  586. }
  587. }
  588. int JackOSSDriver::SetBufferSize(jack_nframes_t buffer_size)
  589. {
  590. CloseAux();
  591. JackAudioDriver::SetBufferSize(buffer_size); // Generic change, never fails
  592. return OpenAux();
  593. }
  594. int JackOSSDriver::ProcessSync()
  595. {
  596. // Read input buffers for the current cycle
  597. if (Read() < 0) {
  598. jack_error("ProcessSync: read error, skip cycle");
  599. return 0; // Non fatal error here, skip cycle, but continue processing...
  600. }
  601. if (fIsMaster) {
  602. ProcessGraphSync();
  603. } else {
  604. ResumeRefNum();
  605. }
  606. // Write output buffers for the current cycle
  607. if (Write() < 0) {
  608. jack_error("JackAudioDriver::ProcessSync: write error, skip cycle");
  609. return 0; // Non fatal error here, skip cycle, but continue processing...
  610. }
  611. return 0;
  612. }
  613. } // end of namespace
  614. #ifdef __cplusplus
  615. extern "C"
  616. {
  617. #endif
  618. SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor()
  619. {
  620. jack_driver_desc_t * desc;
  621. jack_driver_desc_filler_t filler;
  622. jack_driver_param_value_t value;
  623. desc = jack_driver_descriptor_construct("oss", JackDriverMaster, "OSS API based audio backend", &filler);
  624. value.ui = OSS_DRIVER_DEF_FS;
  625. jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
  626. value.ui = OSS_DRIVER_DEF_BLKSIZE;
  627. jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames per period", NULL);
  628. value.ui = OSS_DRIVER_DEF_NPERIODS;
  629. jack_driver_descriptor_add_parameter(desc, &filler, "nperiods", 'n', JackDriverParamUInt, &value, NULL, "Number of periods to prefill output buffer", NULL);
  630. value.i = OSS_DRIVER_DEF_BITS;
  631. jack_driver_descriptor_add_parameter(desc, &filler, "wordlength", 'w', JackDriverParamInt, &value, NULL, "Word length", NULL);
  632. value.ui = OSS_DRIVER_DEF_INS;
  633. jack_driver_descriptor_add_parameter(desc, &filler, "inchannels", 'i', JackDriverParamUInt, &value, NULL, "Capture channels", NULL);
  634. value.ui = OSS_DRIVER_DEF_OUTS;
  635. jack_driver_descriptor_add_parameter(desc, &filler, "outchannels", 'o', JackDriverParamUInt, &value, NULL, "Playback channels", NULL);
  636. value.i = false;
  637. jack_driver_descriptor_add_parameter(desc, &filler, "excl", 'e', JackDriverParamBool, &value, NULL, "Exclusive and direct device access", NULL);
  638. strcpy(value.str, OSS_DRIVER_DEF_DEV);
  639. jack_driver_descriptor_add_parameter(desc, &filler, "capture", 'C', JackDriverParamString, &value, NULL, "Input device", NULL);
  640. jack_driver_descriptor_add_parameter(desc, &filler, "playback", 'P', JackDriverParamString, &value, NULL, "Output device", NULL);
  641. jack_driver_descriptor_add_parameter(desc, &filler, "device", 'd', JackDriverParamString, &value, NULL, "OSS device name", NULL);
  642. value.i = false;
  643. jack_driver_descriptor_add_parameter(desc, &filler, "ignorehwbuf", 'b', JackDriverParamBool, &value, NULL, "Ignore hardware period size", NULL);
  644. value.ui = 0;
  645. jack_driver_descriptor_add_parameter(desc, &filler, "input-latency", 'I', JackDriverParamUInt, &value, NULL, "Extra input latency", NULL);
  646. jack_driver_descriptor_add_parameter(desc, &filler, "output-latency", 'O', JackDriverParamUInt, &value, NULL, "Extra output latency", NULL);
  647. return desc;
  648. }
  649. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  650. {
  651. int bits = OSS_DRIVER_DEF_BITS;
  652. jack_nframes_t srate = OSS_DRIVER_DEF_FS;
  653. jack_nframes_t frames_per_interrupt = OSS_DRIVER_DEF_BLKSIZE;
  654. const char* capture_pcm_name = OSS_DRIVER_DEF_DEV;
  655. const char* playback_pcm_name = OSS_DRIVER_DEF_DEV;
  656. bool capture = false;
  657. bool playback = false;
  658. int chan_in = 0;
  659. int chan_out = 0;
  660. bool monitor = false;
  661. bool excl = false;
  662. unsigned int nperiods = OSS_DRIVER_DEF_NPERIODS;
  663. const JSList *node;
  664. const jack_driver_param_t *param;
  665. bool ignorehwbuf = false;
  666. jack_nframes_t systemic_input_latency = 0;
  667. jack_nframes_t systemic_output_latency = 0;
  668. for (node = params; node; node = jack_slist_next(node)) {
  669. param = (const jack_driver_param_t *)node->data;
  670. switch (param->character) {
  671. case 'r':
  672. srate = param->value.ui;
  673. break;
  674. case 'p':
  675. frames_per_interrupt = (unsigned int)param->value.ui;
  676. break;
  677. case 'n':
  678. nperiods = (unsigned int)param->value.ui;
  679. break;
  680. case 'w':
  681. bits = param->value.i;
  682. break;
  683. case 'i':
  684. chan_in = (int)param->value.ui;
  685. break;
  686. case 'o':
  687. chan_out = (int)param->value.ui;
  688. break;
  689. case 'C':
  690. capture = true;
  691. if (strcmp(param->value.str, "none") != 0) {
  692. capture_pcm_name = param->value.str;
  693. }
  694. break;
  695. case 'P':
  696. playback = true;
  697. if (strcmp(param->value.str, "none") != 0) {
  698. playback_pcm_name = param->value.str;
  699. }
  700. break;
  701. case 'd':
  702. playback_pcm_name = param->value.str;
  703. capture_pcm_name = param->value.str;
  704. break;
  705. case 'b':
  706. ignorehwbuf = true;
  707. break;
  708. case 'e':
  709. excl = true;
  710. break;
  711. case 'I':
  712. systemic_input_latency = param->value.ui;
  713. break;
  714. case 'O':
  715. systemic_output_latency = param->value.ui;
  716. break;
  717. }
  718. }
  719. // duplex is the default
  720. if (!capture && !playback) {
  721. capture = true;
  722. playback = true;
  723. }
  724. Jack::JackOSSDriver* oss_driver = new Jack::JackOSSDriver("system", "oss", engine, table);
  725. Jack::JackDriverClientInterface* threaded_driver = new Jack::JackThreadedDriver(oss_driver);
  726. // Special open for OSS driver...
  727. if (oss_driver->Open(frames_per_interrupt, nperiods, srate, capture, playback, chan_in, chan_out,
  728. excl, monitor, capture_pcm_name, playback_pcm_name, systemic_input_latency, systemic_output_latency, bits, ignorehwbuf) == 0) {
  729. return threaded_driver;
  730. } else {
  731. delete threaded_driver; // Delete the decorated driver
  732. return NULL;
  733. }
  734. }
  735. #ifdef __cplusplus
  736. }
  737. #endif