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.

1045 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. // TODO : better error handling
  532. bool JackBoomerDriver::JackBoomerDriverInput::Execute()
  533. {
  534. #ifdef JACK_MONITOR
  535. gCycleTable.fTable[gCycleReadCount].fBeforeRead = GetMicroSeconds();
  536. #endif
  537. audio_errinfo ei_in;
  538. ssize_t count = ::read(fDriver->fInFD, fDriver->fInputBuffer, fDriver->fInputBufferSize);
  539. #ifdef JACK_MONITOR
  540. if (count > 0 && count != (int)fDriver->fInputBufferSize)
  541. jack_log("JackBoomerDriverInput::Execute count = %ld", count / (fDriver->fSampleSize * fDriver->fCaptureChannels));
  542. gCycleTable.fTable[gCycleReadCount].fAfterRead = GetMicroSeconds();
  543. #endif
  544. // XRun detection
  545. if (ioctl(fDriver->fInFD, SNDCTL_DSP_GETERROR, &ei_in) == 0) {
  546. if (ei_in.rec_overruns > 0 ) {
  547. jack_error("JackBoomerDriverInput::Execute overruns");
  548. jack_time_t cur_time = GetMicroSeconds();
  549. fDriver->NotifyXRun(cur_time, float(cur_time - fDriver->fBeginDateUst)); // Better this value than nothing...
  550. }
  551. if (ei_in.rec_errorcount > 0 && ei_in.rec_lasterror != 0) {
  552. jack_error("%d OSS rec event(s), last=%05d:%d", ei_in.rec_errorcount, ei_in.rec_lasterror, ei_in.rec_errorparm);
  553. }
  554. }
  555. if (count < 0) {
  556. jack_log("JackBoomerDriverInput::Execute error = %s", strerror(errno));
  557. } else if (count < (int)fDriver->fInputBufferSize) {
  558. jack_error("JackBoomerDriverInput::Execute error bytes read = %ld", count);
  559. } else {
  560. // Keep begin cycle time
  561. fDriver->CycleTakeBeginTime();
  562. for (int i = 0; i < fDriver->fCaptureChannels; i++) {
  563. if (fDriver->fGraphManager->GetConnectionsNum(fDriver->fCapturePortList[i]) > 0) {
  564. CopyAndConvertIn(fDriver->GetInputBuffer(i),
  565. fDriver->fInputBuffer,
  566. fDriver->fEngineControl->fBufferSize,
  567. i,
  568. fDriver->fCaptureChannels * fDriver->fSampleSize,
  569. fDriver->fBits);
  570. }
  571. }
  572. #ifdef JACK_MONITOR
  573. gCycleTable.fTable[gCycleReadCount].fAfterReadConvert = GetMicroSeconds();
  574. gCycleReadCount = (gCycleReadCount == CYCLE_POINTS - 1) ? gCycleReadCount: gCycleReadCount + 1;
  575. #endif
  576. }
  577. // Duplex : sync with write thread
  578. if (fDriver->fInFD >= 0 && fDriver->fOutFD >= 0) {
  579. fDriver->SynchronizeRead();
  580. } else {
  581. // Otherwise direct process
  582. fDriver->Process();
  583. }
  584. return true;
  585. }
  586. bool JackBoomerDriver::JackBoomerDriverOutput::Init()
  587. {
  588. if (fDriver->IsRealTime()) {
  589. jack_log("JackBoomerDriverOutput::Init IsRealTime");
  590. if (fDriver->fOutputThread.AcquireRealTime(GetEngineControl()->fServerPriority) < 0) {
  591. jack_error("AcquireRealTime error");
  592. } else {
  593. set_threaded_log_function();
  594. }
  595. }
  596. int delay;
  597. if (ioctl(fDriver->fOutFD, SNDCTL_DSP_GETODELAY, &delay) == -1) {
  598. jack_error("JackBoomerDriverOutput::Init error get out delay : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  599. }
  600. delay /= fDriver->fSampleSize * fDriver->fPlaybackChannels;
  601. jack_info("JackBoomerDriverOutput::Init output latency frames = %ld", delay);
  602. return true;
  603. }
  604. // TODO : better error handling
  605. bool JackBoomerDriver::JackBoomerDriverOutput::Execute()
  606. {
  607. memset(fDriver->fOutputBuffer, 0, fDriver->fOutputBufferSize);
  608. #ifdef JACK_MONITOR
  609. gCycleTable.fTable[gCycleWriteCount].fBeforeWriteConvert = GetMicroSeconds();
  610. #endif
  611. for (int i = 0; i < fDriver->fPlaybackChannels; i++) {
  612. if (fDriver->fGraphManager->GetConnectionsNum(fDriver->fPlaybackPortList[i]) > 0) {
  613. CopyAndConvertOut(fDriver->fOutputBuffer,
  614. fDriver->GetOutputBuffer(i),
  615. fDriver->fEngineControl->fBufferSize,
  616. i,
  617. fDriver->fPlaybackChannels * fDriver->fSampleSize,
  618. fDriver->fBits);
  619. }
  620. }
  621. #ifdef JACK_MONITOR
  622. gCycleTable.fTable[gCycleWriteCount].fBeforeWrite = GetMicroSeconds();
  623. #endif
  624. ssize_t count = ::write(fDriver->fOutFD, fDriver->fOutputBuffer, fDriver->fOutputBufferSize);
  625. #ifdef JACK_MONITOR
  626. if (count > 0 && count != (int)fDriver->fOutputBufferSize)
  627. jack_log("JackBoomerDriverOutput::Execute count = %ld", count / (fDriver->fSampleSize * fDriver->fPlaybackChannels));
  628. gCycleTable.fTable[gCycleWriteCount].fAfterWrite = GetMicroSeconds();
  629. gCycleWriteCount = (gCycleWriteCount == CYCLE_POINTS - 1) ? gCycleWriteCount: gCycleWriteCount + 1;
  630. #endif
  631. // XRun detection
  632. audio_errinfo ei_out;
  633. if (ioctl(fDriver->fOutFD, SNDCTL_DSP_GETERROR, &ei_out) == 0) {
  634. if (ei_out.play_underruns > 0) {
  635. jack_error("JackBoomerDriverOutput::Execute underruns");
  636. jack_time_t cur_time = GetMicroSeconds();
  637. fDriver->NotifyXRun(cur_time, float(cur_time - fDriver->fBeginDateUst)); // Better this value than nothing...
  638. }
  639. if (ei_out.play_errorcount > 0 && ei_out.play_lasterror != 0) {
  640. jack_error("%d OSS play event(s), last=%05d:%d",ei_out.play_errorcount, ei_out.play_lasterror, ei_out.play_errorparm);
  641. }
  642. }
  643. if (count < 0) {
  644. jack_log("JackBoomerDriverOutput::Execute error = %s", strerror(errno));
  645. } else if (count < (int)fDriver->fOutputBufferSize) {
  646. jack_error("JackBoomerDriverOutput::Execute error bytes written = %ld", count);
  647. }
  648. // Duplex : sync with read thread
  649. if (fDriver->fInFD >= 0 && fDriver->fOutFD >= 0) {
  650. fDriver->SynchronizeWrite();
  651. } else {
  652. // Otherwise direct process
  653. fDriver->CycleTakeBeginTime();
  654. fDriver->Process();
  655. }
  656. return true;
  657. }
  658. void JackBoomerDriver::SynchronizeRead()
  659. {
  660. sem_wait(&fWriteSema);
  661. Process();
  662. sem_post(&fReadSema);
  663. }
  664. void JackBoomerDriver::SynchronizeWrite()
  665. {
  666. sem_post(&fWriteSema);
  667. sem_wait(&fReadSema);
  668. }
  669. int JackBoomerDriver::SetBufferSize(jack_nframes_t buffer_size)
  670. {
  671. CloseAux();
  672. JackAudioDriver::SetBufferSize(buffer_size); // never fails
  673. return OpenAux();
  674. }
  675. } // end of namespace
  676. #ifdef __cplusplus
  677. extern "C"
  678. {
  679. #endif
  680. SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor()
  681. {
  682. jack_driver_desc_t *desc;
  683. unsigned int i;
  684. desc = (jack_driver_desc_t*)calloc(1, sizeof(jack_driver_desc_t));
  685. strcpy(desc->name, "boomer"); // size MUST be less then JACK_DRIVER_NAME_MAX + 1
  686. strcpy(desc->desc, "Boomer/OSS API based audio backend"); // size MUST be less then JACK_DRIVER_PARAM_DESC + 1
  687. desc->nparams = OSS_DRIVER_N_PARAMS;
  688. desc->params = (jack_driver_param_desc_t*)calloc(desc->nparams, sizeof(jack_driver_param_desc_t));
  689. i = 0;
  690. strcpy(desc->params[i].name, "rate");
  691. desc->params[i].character = 'r';
  692. desc->params[i].type = JackDriverParamUInt;
  693. desc->params[i].value.ui = OSS_DRIVER_DEF_FS;
  694. strcpy(desc->params[i].short_desc, "Sample rate");
  695. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  696. i++;
  697. strcpy(desc->params[i].name, "period");
  698. desc->params[i].character = 'p';
  699. desc->params[i].type = JackDriverParamUInt;
  700. desc->params[i].value.ui = OSS_DRIVER_DEF_BLKSIZE;
  701. strcpy(desc->params[i].short_desc, "Frames per period");
  702. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  703. i++;
  704. strcpy(desc->params[i].name, "nperiods");
  705. desc->params[i].character = 'n';
  706. desc->params[i].type = JackDriverParamUInt;
  707. desc->params[i].value.ui = OSS_DRIVER_DEF_NPERIODS;
  708. strcpy(desc->params[i].short_desc, "Number of periods to prefill output buffer");
  709. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  710. i++;
  711. strcpy(desc->params[i].name, "wordlength");
  712. desc->params[i].character = 'w';
  713. desc->params[i].type = JackDriverParamInt;
  714. desc->params[i].value.i = OSS_DRIVER_DEF_BITS;
  715. strcpy(desc->params[i].short_desc, "Word length");
  716. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  717. i++;
  718. strcpy(desc->params[i].name, "inchannels");
  719. desc->params[i].character = 'i';
  720. desc->params[i].type = JackDriverParamUInt;
  721. desc->params[i].value.ui = OSS_DRIVER_DEF_INS;
  722. strcpy(desc->params[i].short_desc, "Capture channels");
  723. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  724. i++;
  725. strcpy(desc->params[i].name, "outchannels");
  726. desc->params[i].character = 'o';
  727. desc->params[i].type = JackDriverParamUInt;
  728. desc->params[i].value.ui = OSS_DRIVER_DEF_OUTS;
  729. strcpy(desc->params[i].short_desc, "Playback channels");
  730. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  731. i++;
  732. strcpy(desc->params[i].name, "excl");
  733. desc->params[i].character = 'e';
  734. desc->params[i].type = JackDriverParamBool;
  735. desc->params[i].value.i = false;
  736. strcpy(desc->params[i].short_desc, "Exclusif (O_EXCL) access mode");
  737. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  738. i++;
  739. strcpy(desc->params[i].name, "capture");
  740. desc->params[i].character = 'C';
  741. desc->params[i].type = JackDriverParamString;
  742. strcpy(desc->params[i].value.str, OSS_DRIVER_DEF_DEV);
  743. strcpy(desc->params[i].short_desc, "Input device");
  744. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  745. i++;
  746. strcpy(desc->params[i].name, "playback");
  747. desc->params[i].character = 'P';
  748. desc->params[i].type = JackDriverParamString;
  749. strcpy(desc->params[i].value.str, OSS_DRIVER_DEF_DEV);
  750. strcpy(desc->params[i].short_desc, "Output device");
  751. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  752. i++;
  753. strcpy (desc->params[i].name, "device");
  754. desc->params[i].character = 'd';
  755. desc->params[i].type = JackDriverParamString;
  756. strcpy(desc->params[i].value.str, OSS_DRIVER_DEF_DEV);
  757. strcpy(desc->params[i].short_desc, "OSS device name");
  758. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  759. i++;
  760. strcpy(desc->params[i].name, "input-latency");
  761. desc->params[i].character = 'I';
  762. desc->params[i].type = JackDriverParamUInt;
  763. desc->params[i].value.i = 0;
  764. strcpy(desc->params[i].short_desc, "Extra input latency");
  765. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  766. i++;
  767. strcpy(desc->params[i].name, "output-latency");
  768. desc->params[i].character = 'O';
  769. desc->params[i].type = JackDriverParamUInt;
  770. desc->params[i].value.i = 0;
  771. strcpy(desc->params[i].short_desc, "Extra output latency");
  772. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  773. i++;
  774. strcpy(desc->params[i].name, "sync-io");
  775. desc->params[i].character = 'S';
  776. desc->params[i].type = JackDriverParamBool;
  777. desc->params[i].value.i = false;
  778. strcpy(desc->params[i].short_desc, "In duplex mode, synchronize input and output");
  779. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  780. return desc;
  781. }
  782. EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  783. {
  784. int bits = OSS_DRIVER_DEF_BITS;
  785. jack_nframes_t srate = OSS_DRIVER_DEF_FS;
  786. jack_nframes_t frames_per_interrupt = OSS_DRIVER_DEF_BLKSIZE;
  787. const char* capture_pcm_name = OSS_DRIVER_DEF_DEV;
  788. const char* playback_pcm_name = OSS_DRIVER_DEF_DEV;
  789. bool capture = false;
  790. bool playback = false;
  791. int chan_in = 0;
  792. int chan_out = 0;
  793. bool monitor = false;
  794. bool excl = false;
  795. bool syncio = false;
  796. unsigned int nperiods = OSS_DRIVER_DEF_NPERIODS;
  797. const JSList *node;
  798. const jack_driver_param_t *param;
  799. jack_nframes_t systemic_input_latency = 0;
  800. jack_nframes_t systemic_output_latency = 0;
  801. for (node = params; node; node = jack_slist_next(node)) {
  802. param = (const jack_driver_param_t *)node->data;
  803. switch (param->character) {
  804. case 'r':
  805. srate = param->value.ui;
  806. break;
  807. case 'p':
  808. frames_per_interrupt = (unsigned int)param->value.ui;
  809. break;
  810. case 'n':
  811. nperiods = (unsigned int)param->value.ui;
  812. break;
  813. case 'w':
  814. bits = param->value.i;
  815. break;
  816. case 'i':
  817. chan_in = (int)param->value.ui;
  818. break;
  819. case 'o':
  820. chan_out = (int)param->value.ui;
  821. break;
  822. case 'C':
  823. capture = true;
  824. if (strcmp(param->value.str, "none") != 0) {
  825. capture_pcm_name = param->value.str;
  826. }
  827. break;
  828. case 'P':
  829. playback = true;
  830. if (strcmp(param->value.str, "none") != 0) {
  831. playback_pcm_name = param->value.str;
  832. }
  833. break;
  834. case 'd':
  835. playback_pcm_name = param->value.str;
  836. capture_pcm_name = param->value.str;
  837. break;
  838. case 'e':
  839. excl = true;
  840. break;
  841. case 'I':
  842. systemic_input_latency = param->value.ui;
  843. break;
  844. case 'O':
  845. systemic_output_latency = param->value.ui;
  846. break;
  847. case 'S':
  848. syncio = true;
  849. break;
  850. }
  851. }
  852. // duplex is the default
  853. if (!capture && !playback) {
  854. capture = true;
  855. playback = true;
  856. }
  857. Jack::JackBoomerDriver* boomer_driver = new Jack::JackBoomerDriver("system", "boomer", engine, table);
  858. // Special open for Boomer driver...
  859. if (boomer_driver->Open(frames_per_interrupt, nperiods, srate, capture, playback, chan_in, chan_out, excl,
  860. monitor, capture_pcm_name, playback_pcm_name, systemic_input_latency, systemic_output_latency, bits, syncio) == 0) {
  861. return boomer_driver;
  862. } else {
  863. delete boomer_driver; // Delete the driver
  864. return NULL;
  865. }
  866. }
  867. #ifdef __cplusplus
  868. }
  869. #endif