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.

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