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.

1010 lines
34KB

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