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.

970 lines
33KB

  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)
  225. fCaptureChannels = 2;
  226. if ((fInFD = open(fCaptureDriverName, O_RDONLY | ((fExcl) ? O_EXCL : 0))) < 0) {
  227. jack_error("JackBoomerDriver::OpenInput failed to open device : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  228. return -1;
  229. }
  230. jack_log("JackBoomerDriver::OpenInput input fInFD = %d", fInFD);
  231. if (fExcl) {
  232. if (ioctl(fInFD, SNDCTL_DSP_COOKEDMODE, &flags) == -1) {
  233. jack_error("JackBoomerDriver::OpenInput failed to set cooked mode : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  234. goto error;
  235. }
  236. }
  237. gFragFormat = (2 << 16) + int2pow2(fEngineControl->fBufferSize * fSampleSize * fCaptureChannels);
  238. if (ioctl(fInFD, SNDCTL_DSP_SETFRAGMENT, &gFragFormat) == -1) {
  239. jack_error("JackBoomerDriver::OpenInput failed to set fragments : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  240. goto error;
  241. }
  242. cur_sample_format = fSampleFormat;
  243. if (ioctl(fInFD, SNDCTL_DSP_SETFMT, &fSampleFormat) == -1) {
  244. jack_error("JackBoomerDriver::OpenInput failed to set format : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  245. goto error;
  246. }
  247. if (cur_sample_format != fSampleFormat) {
  248. jack_info("JackBoomerDriver::OpenInput driver forced the sample format %ld", fSampleFormat);
  249. }
  250. cur_capture_channels = fCaptureChannels;
  251. if (ioctl(fInFD, SNDCTL_DSP_CHANNELS, &fCaptureChannels) == -1) {
  252. jack_error("JackBoomerDriver::OpenInput failed to set channels : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  253. goto error;
  254. }
  255. if (cur_capture_channels != fCaptureChannels) {
  256. jack_info("JackBoomerDriver::OpenInput driver forced the number of capture channels %ld", fCaptureChannels);
  257. }
  258. cur_sample_rate = fEngineControl->fSampleRate;
  259. if (ioctl(fInFD, SNDCTL_DSP_SPEED, &fEngineControl->fSampleRate) == -1) {
  260. jack_error("JackBoomerDriver::OpenInput failed to set sample rate : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  261. goto error;
  262. }
  263. if (cur_sample_rate != fEngineControl->fSampleRate) {
  264. jack_info("JackBoomerDriver::OpenInput driver forced the sample rate %ld", fEngineControl->fSampleRate);
  265. }
  266. // Just set the read size to the value we want...
  267. fInputBufferSize = fEngineControl->fBufferSize * fSampleSize * fCaptureChannels;
  268. fInputBuffer = (void*)calloc(fInputBufferSize, 1);
  269. assert(fInputBuffer);
  270. return 0;
  271. error:
  272. ::close(fInFD);
  273. return -1;
  274. }
  275. int JackBoomerDriver::OpenOutput()
  276. {
  277. int flags = 0;
  278. int gFragFormat;
  279. int cur_sample_format;
  280. int cur_playback_channels;
  281. jack_nframes_t cur_sample_rate;
  282. if (fPlaybackChannels == 0)
  283. fPlaybackChannels = 2;
  284. if ((fOutFD = open(fPlaybackDriverName, O_WRONLY | ((fExcl) ? O_EXCL : 0))) < 0) {
  285. jack_error("JackBoomerDriver::OpenOutput failed to open device : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  286. return -1;
  287. }
  288. jack_log("JackBoomerDriver::OpenOutput output fOutFD = %d", fOutFD);
  289. if (fExcl) {
  290. if (ioctl(fOutFD, SNDCTL_DSP_COOKEDMODE, &flags) == -1) {
  291. jack_error("JackBoomerDriver::OpenOutput failed to set cooked mode : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  292. goto error;
  293. }
  294. }
  295. gFragFormat = (2 << 16) + int2pow2(fEngineControl->fBufferSize * fSampleSize * fPlaybackChannels);
  296. if (ioctl(fOutFD, SNDCTL_DSP_SETFRAGMENT, &gFragFormat) == -1) {
  297. jack_error("JackBoomerDriver::OpenOutput failed to set fragments : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  298. goto error;
  299. }
  300. cur_sample_format = fSampleFormat;
  301. if (ioctl(fOutFD, SNDCTL_DSP_SETFMT, &fSampleFormat) == -1) {
  302. jack_error("JackBoomerDriver::OpenOutput failed to set format : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  303. goto error;
  304. }
  305. if (cur_sample_format != fSampleFormat) {
  306. jack_info("JackBoomerDriver::OpenOutput driver forced the sample format %ld", fSampleFormat);
  307. }
  308. cur_playback_channels = fPlaybackChannels;
  309. if (ioctl(fOutFD, SNDCTL_DSP_CHANNELS, &fPlaybackChannels) == -1) {
  310. jack_error("JackBoomerDriver::OpenOutput failed to set channels : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  311. goto error;
  312. }
  313. if (cur_playback_channels != fPlaybackChannels) {
  314. jack_info("JackBoomerDriver::OpenOutput driver forced the number of playback channels %ld", fPlaybackChannels);
  315. }
  316. cur_sample_rate = fEngineControl->fSampleRate;
  317. if (ioctl(fOutFD, SNDCTL_DSP_SPEED, &fEngineControl->fSampleRate) == -1) {
  318. jack_error("JackBoomerDriver::OpenOutput failed to set sample rate : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  319. goto error;
  320. }
  321. if (cur_sample_rate != fEngineControl->fSampleRate) {
  322. jack_info("JackBoomerDriver::OpenInput driver forced the sample rate %ld", fEngineControl->fSampleRate);
  323. }
  324. // Just set the write size to the value we want...
  325. fOutputBufferSize = fEngineControl->fBufferSize * fSampleSize * fPlaybackChannels;
  326. fOutputBuffer = (void*)calloc(fOutputBufferSize, 1);
  327. assert(fOutputBuffer);
  328. return 0;
  329. error:
  330. ::close(fOutFD);
  331. return -1;
  332. }
  333. int JackBoomerDriver::Open(jack_nframes_t nframes,
  334. int user_nperiods,
  335. jack_nframes_t samplerate,
  336. bool capturing,
  337. bool playing,
  338. int inchannels,
  339. int outchannels,
  340. bool excl,
  341. bool monitor,
  342. const char* capture_driver_uid,
  343. const char* playback_driver_uid,
  344. jack_nframes_t capture_latency,
  345. jack_nframes_t playback_latency,
  346. int bits, bool syncio)
  347. {
  348. // Generic JackAudioDriver Open
  349. if (JackAudioDriver::Open(nframes, samplerate, capturing, playing, inchannels, outchannels, monitor,
  350. capture_driver_uid, playback_driver_uid, capture_latency, playback_latency) != 0) {
  351. return -1;
  352. } else {
  353. if (!fEngineControl->fSyncMode) {
  354. jack_error("Cannot run in asynchronous mode, use the -S parameter for jackd");
  355. return -1;
  356. }
  357. fRWMode |= ((capturing) ? kRead : 0);
  358. fRWMode |= ((playing) ? kWrite : 0);
  359. fBits = bits;
  360. fExcl = excl;
  361. fNperiods = (user_nperiods == 0) ? 1 : user_nperiods ;
  362. fSyncIO = syncio;
  363. #ifdef JACK_MONITOR
  364. // Force memory page in
  365. memset(&gCycleTable, 0, sizeof(gCycleTable));
  366. #endif
  367. if (OpenAux() < 0) {
  368. Close();
  369. return -1;
  370. } else {
  371. return 0;
  372. }
  373. }
  374. }
  375. int JackBoomerDriver::Close()
  376. {
  377. #ifdef JACK_MONITOR
  378. FILE* file = fopen("OSSProfiling.log", "w");
  379. if (file) {
  380. jack_info("Writing OSS driver timing data....");
  381. for (int i = 1; i < std::min(gCycleReadCount, gCycleWriteCount); i++) {
  382. int d1 = gCycleTable.fTable[i].fAfterRead - gCycleTable.fTable[i].fBeforeRead;
  383. int d2 = gCycleTable.fTable[i].fAfterReadConvert - gCycleTable.fTable[i].fAfterRead;
  384. int d3 = gCycleTable.fTable[i].fAfterWrite - gCycleTable.fTable[i].fBeforeWrite;
  385. int d4 = gCycleTable.fTable[i].fBeforeWrite - gCycleTable.fTable[i].fBeforeWriteConvert;
  386. fprintf(file, "%d \t %d \t %d \t %d \t \n", d1, d2, d3, d4);
  387. }
  388. fclose(file);
  389. } else {
  390. jack_error("JackBoomerDriver::Close : cannot open OSSProfiling.log file");
  391. }
  392. file = fopen("TimingOSS.plot", "w");
  393. if (file == NULL) {
  394. jack_error("JackBoomerDriver::Close cannot open TimingOSS.plot file");
  395. } else {
  396. fprintf(file, "set grid\n");
  397. fprintf(file, "set title \"OSS audio driver timing\"\n");
  398. fprintf(file, "set xlabel \"audio cycles\"\n");
  399. fprintf(file, "set ylabel \"usec\"\n");
  400. fprintf(file, "plot \"OSSProfiling.log\" using 1 title \"Driver read wait\" with lines, \
  401. \"OSSProfiling.log\" using 2 title \"Driver read convert duration\" with lines, \
  402. \"OSSProfiling.log\" using 3 title \"Driver write wait\" with lines, \
  403. \"OSSProfiling.log\" using 4 title \"Driver write convert duration\" with lines\n");
  404. fprintf(file, "set output 'TimingOSS.pdf\n");
  405. fprintf(file, "set terminal pdf\n");
  406. fprintf(file, "set grid\n");
  407. fprintf(file, "set title \"OSS audio driver timing\"\n");
  408. fprintf(file, "set xlabel \"audio cycles\"\n");
  409. fprintf(file, "set ylabel \"usec\"\n");
  410. fprintf(file, "plot \"OSSProfiling.log\" using 1 title \"Driver read wait\" with lines, \
  411. \"OSSProfiling.log\" using 2 title \"Driver read convert duration\" with lines, \
  412. \"OSSProfiling.log\" using 3 title \"Driver write wait\" with lines, \
  413. \"OSSProfiling.log\" using 4 title \"Driver write convert duration\" with lines\n");
  414. fclose(file);
  415. }
  416. #endif
  417. int res = JackAudioDriver::Close();
  418. CloseAux();
  419. return res;
  420. }
  421. int JackBoomerDriver::OpenAux()
  422. {
  423. SetSampleFormat();
  424. if ((fRWMode & kRead) && (OpenInput() < 0)) {
  425. return -1;
  426. }
  427. if ((fRWMode & kWrite) && (OpenOutput() < 0)) {
  428. return -1;
  429. }
  430. DisplayDeviceInfo();
  431. return 0;
  432. }
  433. void JackBoomerDriver::CloseAux()
  434. {
  435. if (fRWMode & kRead && fInFD >= 0) {
  436. close(fInFD);
  437. fInFD = -1;
  438. }
  439. if (fRWMode & kWrite && fOutFD >= 0) {
  440. close(fOutFD);
  441. fOutFD = -1;
  442. }
  443. if (fInputBuffer)
  444. free(fInputBuffer);
  445. fInputBuffer = NULL;
  446. if (fOutputBuffer)
  447. free(fOutputBuffer);
  448. fOutputBuffer = NULL;
  449. }
  450. int JackBoomerDriver::Start()
  451. {
  452. jack_log("JackBoomerDriver::Start");
  453. JackAudioDriver::Start();
  454. // Input/output synchronisation
  455. if (fInFD >= 0 && fOutFD >= 0 && fSyncIO) {
  456. jack_log("JackBoomerDriver::Start sync input/output");
  457. // Create and fill synch group
  458. int id;
  459. oss_syncgroup group;
  460. group.id = 0;
  461. group.mode = PCM_ENABLE_INPUT;
  462. if (ioctl(fInFD, SNDCTL_DSP_SYNCGROUP, &group) == -1)
  463. jack_error("JackBoomerDriver::Start failed to use SNDCTL_DSP_SYNCGROUP : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  464. group.mode = PCM_ENABLE_OUTPUT;
  465. if (ioctl(fOutFD, SNDCTL_DSP_SYNCGROUP, &group) == -1)
  466. jack_error("JackBoomerDriver::Start failed to use SNDCTL_DSP_SYNCGROUP : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  467. // Prefill output buffer : 2 fragments of silence as described in http://manuals.opensound.com/developer/synctest.c.html#LOC6
  468. char* silence_buf = (char*)malloc(fFragmentSize);
  469. memset(silence_buf, 0, fFragmentSize);
  470. jack_log ("JackBoomerDriver::Start prefill size = %d", fFragmentSize);
  471. for (int i = 0; i < 2; i++) {
  472. ssize_t count = ::write(fOutFD, silence_buf, fFragmentSize);
  473. if (count < (int)fFragmentSize) {
  474. jack_error("JackBoomerDriver::Start error bytes written = %ld", count);
  475. }
  476. }
  477. free(silence_buf);
  478. // Start input/output in sync
  479. id = group.id;
  480. if (ioctl(fInFD, SNDCTL_DSP_SYNCSTART, &id) == -1)
  481. jack_error("JackBoomerDriver::Start failed to use SNDCTL_DSP_SYNCSTART : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  482. } else if (fOutFD >= 0) {
  483. // Maybe necessary to write an empty output buffer first time : see http://manuals.opensound.com/developer/fulldup.c.html
  484. memset(fOutputBuffer, 0, fOutputBufferSize);
  485. // Prefill ouput buffer
  486. for (int i = 0; i < fNperiods; i++) {
  487. ssize_t count = ::write(fOutFD, fOutputBuffer, fOutputBufferSize);
  488. if (count < (int)fOutputBufferSize) {
  489. jack_error("JackBoomerDriver::Start error bytes written = %ld", count);
  490. }
  491. }
  492. }
  493. // Start input thread only when needed
  494. if (fInFD >= 0) {
  495. if (fInputThread.StartSync() < 0) {
  496. jack_error("Cannot start input thread");
  497. return -1;
  498. }
  499. }
  500. // Start output thread only when needed
  501. if (fOutFD >= 0) {
  502. if (fOutputThread.StartSync() < 0) {
  503. jack_error("Cannot start output thread");
  504. return -1;
  505. }
  506. }
  507. return 0;
  508. }
  509. int JackBoomerDriver::Stop()
  510. {
  511. // Stop input thread only when needed
  512. if (fInFD >= 0) {
  513. fInputThread.Kill();
  514. }
  515. // Stop output thread only when needed
  516. if (fOutFD >= 0) {
  517. fOutputThread.Kill();
  518. }
  519. return 0;
  520. }
  521. bool JackBoomerDriver::JackBoomerDriverInput::Init()
  522. {
  523. if (fDriver->IsRealTime()) {
  524. jack_log("JackBoomerDriverInput::Init IsRealTime");
  525. if (fDriver->fInputThread.AcquireRealTime(GetEngineControl()->fServerPriority) < 0) {
  526. jack_error("AcquireRealTime error");
  527. } else {
  528. set_threaded_log_function();
  529. }
  530. }
  531. return true;
  532. }
  533. // TODO : better error handling
  534. bool JackBoomerDriver::JackBoomerDriverInput::Execute()
  535. {
  536. #ifdef JACK_MONITOR
  537. gCycleTable.fTable[gCycleReadCount].fBeforeRead = GetMicroSeconds();
  538. #endif
  539. audio_errinfo ei_in;
  540. ssize_t count = ::read(fDriver->fInFD, fDriver->fInputBuffer, fDriver->fInputBufferSize);
  541. #ifdef JACK_MONITOR
  542. if (count > 0 && count != (int)fDriver->fInputBufferSize)
  543. jack_log("JackBoomerDriverInput::Execute count = %ld", count / (fDriver->fSampleSize * fDriver->fCaptureChannels));
  544. gCycleTable.fTable[gCycleReadCount].fAfterRead = GetMicroSeconds();
  545. #endif
  546. // XRun detection
  547. if (ioctl(fDriver->fInFD, SNDCTL_DSP_GETERROR, &ei_in) == 0) {
  548. if (ei_in.rec_overruns > 0 ) {
  549. jack_error("JackBoomerDriverInput::Execute overruns");
  550. jack_time_t cur_time = GetMicroSeconds();
  551. fDriver->NotifyXRun(cur_time, float(cur_time - fDriver->fBeginDateUst)); // Better this value than nothing...
  552. }
  553. if (ei_in.rec_errorcount > 0 && ei_in.rec_lasterror != 0) {
  554. jack_error("%d OSS rec event(s), last=%05d:%d", ei_in.rec_errorcount, ei_in.rec_lasterror, ei_in.rec_errorparm);
  555. }
  556. }
  557. if (count < 0) {
  558. jack_log("JackBoomerDriverInput::Execute error = %s", strerror(errno));
  559. } else if (count < (int)fDriver->fInputBufferSize) {
  560. jack_error("JackBoomerDriverInput::Execute error bytes read = %ld", count);
  561. } else {
  562. // Keep begin cycle time
  563. fDriver->CycleTakeBeginTime();
  564. for (int i = 0; i < fDriver->fCaptureChannels; i++) {
  565. if (fDriver->fGraphManager->GetConnectionsNum(fDriver->fCapturePortList[i]) > 0) {
  566. CopyAndConvertIn(fDriver->GetInputBuffer(i),
  567. fDriver->fInputBuffer,
  568. fDriver->fEngineControl->fBufferSize,
  569. i,
  570. fDriver->fCaptureChannels * fDriver->fSampleSize,
  571. fDriver->fBits);
  572. }
  573. }
  574. #ifdef JACK_MONITOR
  575. gCycleTable.fTable[gCycleReadCount].fAfterReadConvert = GetMicroSeconds();
  576. gCycleReadCount = (gCycleReadCount == CYCLE_POINTS - 1) ? gCycleReadCount: gCycleReadCount + 1;
  577. #endif
  578. }
  579. // Duplex : sync with write thread
  580. if (fDriver->fInFD >= 0 && fDriver->fOutFD >= 0) {
  581. fDriver->SynchronizeRead();
  582. } else {
  583. // Otherwise direct process
  584. fDriver->Process();
  585. }
  586. return true;
  587. }
  588. bool JackBoomerDriver::JackBoomerDriverOutput::Init()
  589. {
  590. if (fDriver->IsRealTime()) {
  591. jack_log("JackBoomerDriverOutput::Init IsRealTime");
  592. if (fDriver->fOutputThread.AcquireRealTime(GetEngineControl()->fServerPriority) < 0) {
  593. jack_error("AcquireRealTime error");
  594. } else {
  595. set_threaded_log_function();
  596. }
  597. }
  598. int delay;
  599. if (ioctl(fDriver->fOutFD, SNDCTL_DSP_GETODELAY, &delay) == -1) {
  600. jack_error("JackBoomerDriverOutput::Init error get out delay : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  601. }
  602. delay /= fDriver->fSampleSize * fDriver->fPlaybackChannels;
  603. jack_info("JackBoomerDriverOutput::Init output latency frames = %ld", delay);
  604. return true;
  605. }
  606. // TODO : better error handling
  607. bool JackBoomerDriver::JackBoomerDriverOutput::Execute()
  608. {
  609. memset(fDriver->fOutputBuffer, 0, fDriver->fOutputBufferSize);
  610. #ifdef JACK_MONITOR
  611. gCycleTable.fTable[gCycleWriteCount].fBeforeWriteConvert = GetMicroSeconds();
  612. #endif
  613. for (int i = 0; i < fDriver->fPlaybackChannels; i++) {
  614. if (fDriver->fGraphManager->GetConnectionsNum(fDriver->fPlaybackPortList[i]) > 0) {
  615. CopyAndConvertOut(fDriver->fOutputBuffer,
  616. fDriver->GetOutputBuffer(i),
  617. fDriver->fEngineControl->fBufferSize,
  618. i,
  619. fDriver->fPlaybackChannels * fDriver->fSampleSize,
  620. fDriver->fBits);
  621. }
  622. }
  623. #ifdef JACK_MONITOR
  624. gCycleTable.fTable[gCycleWriteCount].fBeforeWrite = GetMicroSeconds();
  625. #endif
  626. ssize_t count = ::write(fDriver->fOutFD, fDriver->fOutputBuffer, fDriver->fOutputBufferSize);
  627. #ifdef JACK_MONITOR
  628. if (count > 0 && count != (int)fDriver->fOutputBufferSize)
  629. jack_log("JackBoomerDriverOutput::Execute count = %ld", count / (fDriver->fSampleSize * fDriver->fPlaybackChannels));
  630. gCycleTable.fTable[gCycleWriteCount].fAfterWrite = GetMicroSeconds();
  631. gCycleWriteCount = (gCycleWriteCount == CYCLE_POINTS - 1) ? gCycleWriteCount: gCycleWriteCount + 1;
  632. #endif
  633. // XRun detection
  634. audio_errinfo ei_out;
  635. if (ioctl(fDriver->fOutFD, SNDCTL_DSP_GETERROR, &ei_out) == 0) {
  636. if (ei_out.play_underruns > 0) {
  637. jack_error("JackBoomerDriverOutput::Execute underruns");
  638. jack_time_t cur_time = GetMicroSeconds();
  639. fDriver->NotifyXRun(cur_time, float(cur_time - fDriver->fBeginDateUst)); // Better this value than nothing...
  640. }
  641. if (ei_out.play_errorcount > 0 && ei_out.play_lasterror != 0) {
  642. jack_error("%d OSS play event(s), last=%05d:%d",ei_out.play_errorcount, ei_out.play_lasterror, ei_out.play_errorparm);
  643. }
  644. }
  645. if (count < 0) {
  646. jack_log("JackBoomerDriverOutput::Execute error = %s", strerror(errno));
  647. } else if (count < (int)fDriver->fOutputBufferSize) {
  648. jack_error("JackBoomerDriverOutput::Execute error bytes written = %ld", count);
  649. }
  650. // Duplex : sync with read thread
  651. if (fDriver->fInFD >= 0 && fDriver->fOutFD >= 0) {
  652. fDriver->SynchronizeWrite();
  653. } else {
  654. // Otherwise direct process
  655. fDriver->CycleTakeBeginTime();
  656. fDriver->Process();
  657. }
  658. return true;
  659. }
  660. void JackBoomerDriver::SynchronizeRead()
  661. {
  662. sem_wait(&fWriteSema);
  663. Process();
  664. sem_post(&fReadSema);
  665. }
  666. void JackBoomerDriver::SynchronizeWrite()
  667. {
  668. sem_post(&fWriteSema);
  669. sem_wait(&fReadSema);
  670. }
  671. int JackBoomerDriver::SetBufferSize(jack_nframes_t buffer_size)
  672. {
  673. CloseAux();
  674. JackAudioDriver::SetBufferSize(buffer_size); // Generic change, never fails
  675. return OpenAux();
  676. }
  677. } // end of namespace
  678. #ifdef __cplusplus
  679. extern "C"
  680. {
  681. #endif
  682. SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor()
  683. {
  684. jack_driver_desc_t * desc;
  685. jack_driver_desc_filler_t filler;
  686. jack_driver_param_value_t value;
  687. desc = jack_driver_descriptor_construct("boomer", "Boomer/OSS API based audio backend", &filler);
  688. value.ui = OSS_DRIVER_DEF_FS;
  689. jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
  690. value.ui = OSS_DRIVER_DEF_BLKSIZE;
  691. jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames per period", NULL);
  692. value.ui = OSS_DRIVER_DEF_NPERIODS;
  693. jack_driver_descriptor_add_parameter(desc, &filler, "nperiods", 'n', JackDriverParamUInt, &value, NULL, "Number of periods to prefill output buffer", NULL);
  694. value.i = OSS_DRIVER_DEF_BITS;
  695. jack_driver_descriptor_add_parameter(desc, &filler, "wordlength", 'w', JackDriverParamInt, &value, NULL, "Word length", NULL);
  696. value.ui = OSS_DRIVER_DEF_INS;
  697. jack_driver_descriptor_add_parameter(desc, &filler, "inchannels", 'i', JackDriverParamUInt, &value, NULL, "Capture channels", NULL);
  698. value.ui = OSS_DRIVER_DEF_OUTS;
  699. jack_driver_descriptor_add_parameter(desc, &filler, "outchannels", 'o', JackDriverParamUInt, &value, NULL, "Playback channels", NULL);
  700. value.i = false;
  701. jack_driver_descriptor_add_parameter(desc, &filler, "excl", 'e', JackDriverParamBool, &value, NULL, "Exclusif (O_EXCL) access mode", NULL);
  702. strcpy(value.str, OSS_DRIVER_DEF_DEV);
  703. jack_driver_descriptor_add_parameter(desc, &filler, "capture", 'C', JackDriverParamString, &value, NULL, "Input device", NULL);
  704. jack_driver_descriptor_add_parameter(desc, &filler, "playback", 'P', JackDriverParamString, &value, NULL, "Output device", NULL);
  705. jack_driver_descriptor_add_parameter(desc, &filler, "device", 'd', JackDriverParamString, &value, NULL, "OSS device name", NULL);
  706. value.ui = 0;
  707. jack_driver_descriptor_add_parameter(desc, &filler, "input-latency", 'I', JackDriverParamUInt, &value, NULL, "Extra input latency", NULL);
  708. jack_driver_descriptor_add_parameter(desc, &filler, "output-latency", 'O', JackDriverParamUInt, &value, NULL, "Extra output latency", NULL);
  709. value.i = false;
  710. jack_driver_descriptor_add_parameter(desc, &filler, "sync-io", 'S', JackDriverParamBool, &value, NULL, "In duplex mode, synchronize input and output", NULL);
  711. return desc;
  712. }
  713. EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  714. {
  715. int bits = OSS_DRIVER_DEF_BITS;
  716. jack_nframes_t srate = OSS_DRIVER_DEF_FS;
  717. jack_nframes_t frames_per_interrupt = OSS_DRIVER_DEF_BLKSIZE;
  718. const char* capture_pcm_name = OSS_DRIVER_DEF_DEV;
  719. const char* playback_pcm_name = OSS_DRIVER_DEF_DEV;
  720. bool capture = false;
  721. bool playback = false;
  722. int chan_in = 0;
  723. int chan_out = 0;
  724. bool monitor = false;
  725. bool excl = false;
  726. bool syncio = false;
  727. unsigned int nperiods = OSS_DRIVER_DEF_NPERIODS;
  728. const JSList *node;
  729. const jack_driver_param_t *param;
  730. jack_nframes_t systemic_input_latency = 0;
  731. jack_nframes_t systemic_output_latency = 0;
  732. for (node = params; node; node = jack_slist_next(node)) {
  733. param = (const jack_driver_param_t *)node->data;
  734. switch (param->character) {
  735. case 'r':
  736. srate = param->value.ui;
  737. break;
  738. case 'p':
  739. frames_per_interrupt = (unsigned int)param->value.ui;
  740. break;
  741. case 'n':
  742. nperiods = (unsigned int)param->value.ui;
  743. break;
  744. case 'w':
  745. bits = param->value.i;
  746. break;
  747. case 'i':
  748. chan_in = (int)param->value.ui;
  749. break;
  750. case 'o':
  751. chan_out = (int)param->value.ui;
  752. break;
  753. case 'C':
  754. capture = true;
  755. if (strcmp(param->value.str, "none") != 0) {
  756. capture_pcm_name = param->value.str;
  757. }
  758. break;
  759. case 'P':
  760. playback = true;
  761. if (strcmp(param->value.str, "none") != 0) {
  762. playback_pcm_name = param->value.str;
  763. }
  764. break;
  765. case 'd':
  766. playback_pcm_name = param->value.str;
  767. capture_pcm_name = param->value.str;
  768. break;
  769. case 'e':
  770. excl = true;
  771. break;
  772. case 'I':
  773. systemic_input_latency = param->value.ui;
  774. break;
  775. case 'O':
  776. systemic_output_latency = param->value.ui;
  777. break;
  778. case 'S':
  779. syncio = true;
  780. break;
  781. }
  782. }
  783. // duplex is the default
  784. if (!capture && !playback) {
  785. capture = true;
  786. playback = true;
  787. }
  788. Jack::JackBoomerDriver* boomer_driver = new Jack::JackBoomerDriver("system", "boomer", engine, table);
  789. // Special open for Boomer driver...
  790. if (boomer_driver->Open(frames_per_interrupt, nperiods, srate, capture, playback, chan_in, chan_out, excl,
  791. monitor, capture_pcm_name, playback_pcm_name, systemic_input_latency, systemic_output_latency, bits, syncio) == 0) {
  792. return boomer_driver;
  793. } else {
  794. delete boomer_driver; // Delete the driver
  795. return NULL;
  796. }
  797. }
  798. #ifdef __cplusplus
  799. }
  800. #endif