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.

995 lines
34KB

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