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.

1011 lines
35KB

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