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.

1016 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. /*
  463. // 10/02/09 : desactivated for now, needs more check (only needed when *same* device is used for input and output ??)
  464. if ((fRWMode & kRead) && (fRWMode & kWrite) && (fInputBufferSize != fOutputBufferSize)) {
  465. jack_error("JackOSSDriver::OpenAux input and output buffer size are not the same!!");
  466. return -1;
  467. }
  468. */
  469. DisplayDeviceInfo();
  470. return 0;
  471. }
  472. void JackOSSDriver::CloseAux()
  473. {
  474. if (fRWMode & kRead && fInFD > 0) {
  475. close(fInFD);
  476. fInFD = -1;
  477. }
  478. if (fRWMode & kWrite && fOutFD > 0) {
  479. close(fOutFD);
  480. fOutFD = -1;
  481. }
  482. if (fInputBuffer)
  483. free(fInputBuffer);
  484. fInputBuffer = NULL;
  485. if (fOutputBuffer)
  486. free(fOutputBuffer);
  487. fOutputBuffer = NULL;
  488. }
  489. int JackOSSDriver::Read()
  490. {
  491. if (fInFD < 0) {
  492. // Keep begin cycle time
  493. JackDriver::CycleTakeBeginTime();
  494. return 0;
  495. }
  496. ssize_t count;
  497. /*
  498. // Maybe necessary to write an empty output buffer first time : see http://manuals.opensound.com/developer/fulldup.c.html
  499. if (fFirstCycle) {
  500. fFirstCycle = false;
  501. memset(fOutputBuffer, 0, fOutputBufferSize);
  502. // Prefill ouput buffer
  503. for (int i = 0; i < fNperiods; i++) {
  504. count = ::write(fOutFD, fOutputBuffer, fOutputBufferSize);
  505. if (count < fOutputBufferSize) {
  506. jack_error("JackOSSDriver::Write error bytes written = %ld", count);
  507. return -1;
  508. }
  509. }
  510. int delay;
  511. if (ioctl(fOutFD, SNDCTL_DSP_GETODELAY, &delay) == -1) {
  512. jack_error("JackOSSDriver::Write error get out delay : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  513. return -1;
  514. }
  515. delay /= fSampleSize * fPlaybackChannels;
  516. jack_info("JackOSSDriver::Write output latency frames = %ld", delay);
  517. }
  518. */
  519. #ifdef JACK_MONITOR
  520. gCycleTable.fTable[gCycleCount].fBeforeRead = GetMicroSeconds();
  521. #endif
  522. audio_errinfo ei_in;
  523. count = ::read(fInFD, fInputBuffer, fInputBufferSize);
  524. #ifdef JACK_MONITOR
  525. if (count > 0 && count != (int)fInputBufferSize)
  526. jack_log("JackOSSDriver::Read count = %ld", count / (fSampleSize * fCaptureChannels));
  527. gCycleTable.fTable[gCycleCount].fAfterRead = GetMicroSeconds();
  528. #endif
  529. // XRun detection
  530. if (ioctl(fInFD, SNDCTL_DSP_GETERROR, &ei_in) == 0) {
  531. if (ei_in.rec_overruns > 0 ) {
  532. jack_error("JackOSSDriver::Read overruns");
  533. jack_time_t cur_time = GetMicroSeconds();
  534. NotifyXRun(cur_time, float(cur_time - fBeginDateUst)); // Better this value than nothing...
  535. }
  536. if (ei_in.rec_errorcount > 0 && ei_in.rec_lasterror != 0) {
  537. jack_error("%d OSS rec event(s), last=%05d:%d", ei_in.rec_errorcount, ei_in.rec_lasterror, ei_in.rec_errorparm);
  538. }
  539. }
  540. if (count < 0) {
  541. jack_log("JackOSSDriver::Read error = %s", strerror(errno));
  542. return -1;
  543. } else if (count < (int)fInputBufferSize) {
  544. jack_error("JackOSSDriver::Read error bytes read = %ld", count);
  545. return -1;
  546. } else {
  547. // Keep begin cycle time
  548. JackDriver::CycleTakeBeginTime();
  549. for (int i = 0; i < fCaptureChannels; i++) {
  550. if (fGraphManager->GetConnectionsNum(fCapturePortList[i]) > 0) {
  551. CopyAndConvertIn(GetInputBuffer(i), fInputBuffer, fEngineControl->fBufferSize, i, fCaptureChannels, fBits);
  552. }
  553. }
  554. #ifdef JACK_MONITOR
  555. gCycleTable.fTable[gCycleCount].fAfterReadConvert = GetMicroSeconds();
  556. #endif
  557. return 0;
  558. }
  559. }
  560. int JackOSSDriver::Write()
  561. {
  562. if (fOutFD < 0) {
  563. // Keep end cycle time
  564. JackDriver::CycleTakeEndTime();
  565. return 0;
  566. }
  567. ssize_t count;
  568. audio_errinfo ei_out;
  569. // Maybe necessary to write an empty output buffer first time : see http://manuals.opensound.com/developer/fulldup.c.html
  570. if (fFirstCycle) {
  571. fFirstCycle = false;
  572. memset(fOutputBuffer, 0, fOutputBufferSize);
  573. // Prefill ouput buffer
  574. for (int i = 0; i < fNperiods; i++) {
  575. count = ::write(fOutFD, fOutputBuffer, fOutputBufferSize);
  576. if (count < (int)fOutputBufferSize) {
  577. jack_error("JackOSSDriver::Write error bytes written = %ld", count);
  578. return -1;
  579. }
  580. }
  581. int delay;
  582. if (ioctl(fOutFD, SNDCTL_DSP_GETODELAY, &delay) == -1) {
  583. jack_error("JackOSSDriver::Write error get out delay : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  584. return -1;
  585. }
  586. delay /= fSampleSize * fPlaybackChannels;
  587. jack_info("JackOSSDriver::Write output latency frames = %ld", delay);
  588. }
  589. #ifdef JACK_MONITOR
  590. gCycleTable.fTable[gCycleCount].fBeforeWriteConvert = GetMicroSeconds();
  591. #endif
  592. memset(fOutputBuffer, 0, fOutputBufferSize);
  593. for (int i = 0; i < fPlaybackChannels; i++) {
  594. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
  595. CopyAndConvertOut(fOutputBuffer, GetOutputBuffer(i), fEngineControl->fBufferSize, i, fPlaybackChannels, fBits);
  596. }
  597. }
  598. #ifdef JACK_MONITOR
  599. gCycleTable.fTable[gCycleCount].fBeforeWrite = GetMicroSeconds();
  600. #endif
  601. // Keep end cycle time
  602. JackDriver::CycleTakeEndTime();
  603. count = ::write(fOutFD, fOutputBuffer, fOutputBufferSize);
  604. #ifdef JACK_MONITOR
  605. if (count > 0 && count != (int)fOutputBufferSize)
  606. jack_log("JackOSSDriver::Write count = %ld", count / (fSampleSize * fPlaybackChannels));
  607. gCycleTable.fTable[gCycleCount].fAfterWrite = GetMicroSeconds();
  608. gCycleCount = (gCycleCount == CYCLE_POINTS - 1) ? gCycleCount: gCycleCount + 1;
  609. #endif
  610. // XRun detection
  611. if (ioctl(fOutFD, SNDCTL_DSP_GETERROR, &ei_out) == 0) {
  612. if (ei_out.play_underruns > 0) {
  613. jack_error("JackOSSDriver::Write underruns");
  614. jack_time_t cur_time = GetMicroSeconds();
  615. NotifyXRun(cur_time, float(cur_time - 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("JackOSSDriver::Write error = %s", strerror(errno));
  623. return -1;
  624. } else if (count < (int)fOutputBufferSize) {
  625. jack_error("JackOSSDriver::Write error bytes written = %ld", count);
  626. return -1;
  627. } else {
  628. return 0;
  629. }
  630. }
  631. int JackOSSDriver::SetBufferSize(jack_nframes_t buffer_size)
  632. {
  633. CloseAux();
  634. JackAudioDriver::SetBufferSize(buffer_size); // never fails
  635. return OpenAux();
  636. }
  637. int JackOSSDriver::ProcessSync()
  638. {
  639. // Read input buffers for the current cycle
  640. if (Read() < 0) {
  641. jack_error("ProcessSync: read error, skip cycle");
  642. return 0; // Skip cycle, but continue processing...
  643. }
  644. if (fIsMaster) {
  645. ProcessGraphSync();
  646. } else {
  647. fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
  648. }
  649. // Write output buffers for the current cycle
  650. if (Write() < 0) {
  651. jack_error("JackAudioDriver::ProcessSync: write error, skip cycle");
  652. return 0; // Skip cycle, but continue processing...
  653. }
  654. return 0;
  655. }
  656. } // end of namespace
  657. #ifdef __cplusplus
  658. extern "C"
  659. {
  660. #endif
  661. SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor()
  662. {
  663. jack_driver_desc_t *desc;
  664. unsigned int i;
  665. desc = (jack_driver_desc_t*)calloc(1, sizeof(jack_driver_desc_t));
  666. strcpy(desc->name, "oss"); // size MUST be less then JACK_DRIVER_NAME_MAX + 1
  667. strcpy(desc->desc, "OSS API based audio backend"); // size MUST be less then JACK_DRIVER_PARAM_DESC + 1
  668. desc->nparams = OSS_DRIVER_N_PARAMS;
  669. desc->params = (jack_driver_param_desc_t*)calloc(desc->nparams, sizeof(jack_driver_param_desc_t));
  670. i = 0;
  671. strcpy(desc->params[i].name, "rate");
  672. desc->params[i].character = 'r';
  673. desc->params[i].type = JackDriverParamUInt;
  674. desc->params[i].value.ui = OSS_DRIVER_DEF_FS;
  675. strcpy(desc->params[i].short_desc, "Sample rate");
  676. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  677. i++;
  678. strcpy(desc->params[i].name, "period");
  679. desc->params[i].character = 'p';
  680. desc->params[i].type = JackDriverParamUInt;
  681. desc->params[i].value.ui = OSS_DRIVER_DEF_BLKSIZE;
  682. strcpy(desc->params[i].short_desc, "Frames per period");
  683. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  684. i++;
  685. strcpy(desc->params[i].name, "nperiods");
  686. desc->params[i].character = 'n';
  687. desc->params[i].type = JackDriverParamUInt;
  688. desc->params[i].value.ui = OSS_DRIVER_DEF_NPERIODS;
  689. strcpy(desc->params[i].short_desc, "Number of periods to prefill output buffer");
  690. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  691. i++;
  692. strcpy(desc->params[i].name, "wordlength");
  693. desc->params[i].character = 'w';
  694. desc->params[i].type = JackDriverParamInt;
  695. desc->params[i].value.i = OSS_DRIVER_DEF_BITS;
  696. strcpy(desc->params[i].short_desc, "Word length");
  697. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  698. i++;
  699. strcpy(desc->params[i].name, "inchannels");
  700. desc->params[i].character = 'i';
  701. desc->params[i].type = JackDriverParamUInt;
  702. desc->params[i].value.ui = OSS_DRIVER_DEF_INS;
  703. strcpy(desc->params[i].short_desc, "Capture channels");
  704. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  705. i++;
  706. strcpy(desc->params[i].name, "outchannels");
  707. desc->params[i].character = 'o';
  708. desc->params[i].type = JackDriverParamUInt;
  709. desc->params[i].value.ui = OSS_DRIVER_DEF_OUTS;
  710. strcpy(desc->params[i].short_desc, "Playback channels");
  711. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  712. i++;
  713. strcpy(desc->params[i].name, "excl");
  714. desc->params[i].character = 'e';
  715. desc->params[i].type = JackDriverParamBool;
  716. desc->params[i].value.i = false;
  717. strcpy(desc->params[i].short_desc, "Exclusif (O_EXCL) access mode");
  718. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  719. i++;
  720. strcpy(desc->params[i].name, "capture");
  721. desc->params[i].character = 'C';
  722. desc->params[i].type = JackDriverParamString;
  723. strcpy(desc->params[i].value.str, OSS_DRIVER_DEF_DEV);
  724. strcpy(desc->params[i].short_desc, "Input device");
  725. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  726. i++;
  727. strcpy(desc->params[i].name, "playback");
  728. desc->params[i].character = 'P';
  729. desc->params[i].type = JackDriverParamString;
  730. strcpy(desc->params[i].value.str, OSS_DRIVER_DEF_DEV);
  731. strcpy(desc->params[i].short_desc, "Output device");
  732. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  733. i++;
  734. strcpy (desc->params[i].name, "device");
  735. desc->params[i].character = 'd';
  736. desc->params[i].type = JackDriverParamString;
  737. strcpy(desc->params[i].value.str, OSS_DRIVER_DEF_DEV);
  738. strcpy(desc->params[i].short_desc, "OSS device name");
  739. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  740. i++;
  741. strcpy(desc->params[i].name, "ignorehwbuf");
  742. desc->params[i].character = 'b';
  743. desc->params[i].type = JackDriverParamBool;
  744. desc->params[i].value.i = TRUE;
  745. strcpy(desc->params[i].short_desc, "Ignore hardware period size");
  746. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  747. i++;
  748. strcpy(desc->params[i].name, "input-latency");
  749. desc->params[i].character = 'I';
  750. desc->params[i].type = JackDriverParamUInt;
  751. desc->params[i].value.i = 0;
  752. strcpy(desc->params[i].short_desc, "Extra input latency");
  753. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  754. i++;
  755. strcpy(desc->params[i].name, "output-latency");
  756. desc->params[i].character = 'O';
  757. desc->params[i].type = JackDriverParamUInt;
  758. desc->params[i].value.i = 0;
  759. strcpy(desc->params[i].short_desc, "Extra output latency");
  760. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  761. return desc;
  762. }
  763. EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  764. {
  765. int bits = OSS_DRIVER_DEF_BITS;
  766. jack_nframes_t srate = OSS_DRIVER_DEF_FS;
  767. jack_nframes_t frames_per_interrupt = OSS_DRIVER_DEF_BLKSIZE;
  768. const char* capture_pcm_name = OSS_DRIVER_DEF_DEV;
  769. const char* playback_pcm_name = OSS_DRIVER_DEF_DEV;
  770. bool capture = false;
  771. bool playback = false;
  772. int chan_in = 0;
  773. int chan_out = 0;
  774. bool monitor = false;
  775. bool excl = false;
  776. unsigned int nperiods = OSS_DRIVER_DEF_NPERIODS;
  777. const JSList *node;
  778. const jack_driver_param_t *param;
  779. bool ignorehwbuf = false;
  780. jack_nframes_t systemic_input_latency = 0;
  781. jack_nframes_t systemic_output_latency = 0;
  782. for (node = params; node; node = jack_slist_next(node)) {
  783. param = (const jack_driver_param_t *)node->data;
  784. switch (param->character) {
  785. case 'r':
  786. srate = param->value.ui;
  787. break;
  788. case 'p':
  789. frames_per_interrupt = (unsigned int)param->value.ui;
  790. break;
  791. case 'n':
  792. nperiods = (unsigned int)param->value.ui;
  793. break;
  794. case 'w':
  795. bits = param->value.i;
  796. break;
  797. case 'i':
  798. chan_in = (int)param->value.ui;
  799. break;
  800. case 'o':
  801. chan_out = (int)param->value.ui;
  802. break;
  803. case 'C':
  804. capture = true;
  805. if (strcmp(param->value.str, "none") != 0) {
  806. capture_pcm_name = strdup(param->value.str);
  807. }
  808. break;
  809. case 'P':
  810. playback = true;
  811. if (strcmp(param->value.str, "none") != 0) {
  812. playback_pcm_name = strdup(param->value.str);
  813. }
  814. break;
  815. case 'd':
  816. playback_pcm_name = strdup (param->value.str);
  817. capture_pcm_name = strdup (param->value.str);
  818. break;
  819. case 'b':
  820. ignorehwbuf = true;
  821. break;
  822. case 'e':
  823. excl = true;
  824. break;
  825. case 'I':
  826. systemic_input_latency = param->value.ui;
  827. break;
  828. case 'O':
  829. systemic_output_latency = param->value.ui;
  830. break;
  831. }
  832. }
  833. // duplex is the default
  834. if (!capture && !playback) {
  835. capture = true;
  836. playback = true;
  837. }
  838. Jack::JackOSSDriver* oss_driver = new Jack::JackOSSDriver("system", "oss", engine, table);
  839. Jack::JackDriverClientInterface* threaded_driver = new Jack::JackThreadedDriver(oss_driver);
  840. // Special open for OSS driver...
  841. if (oss_driver->Open(frames_per_interrupt, nperiods, srate, capture, playback, chan_in, chan_out,
  842. excl, monitor, capture_pcm_name, playback_pcm_name, systemic_input_latency, systemic_output_latency, bits, ignorehwbuf) == 0) {
  843. return threaded_driver;
  844. } else {
  845. delete threaded_driver; // Delete the decorated driver
  846. return NULL;
  847. }
  848. }
  849. #ifdef __cplusplus
  850. }
  851. #endif