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.

1043 lines
36KB

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