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.

888 lines
30KB

  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 int *s32src = (signed int*)src;
  65. s32src += channel;
  66. sample_move_dS_s24(dst, (char*)s32src, nframes, chcount<<2);
  67. break;
  68. }
  69. case 32: {
  70. signed int *s32src = (signed int*)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. if (ai_in.rate_source != ai_out.rate_source) {
  194. jack_info("Warning : input and output are not necessarily driven by the same clock!");
  195. }
  196. }
  197. int JackOSSDriver::OpenInput()
  198. {
  199. int flags = 0;
  200. int gFragFormat;
  201. int cur_capture_channels;
  202. int cur_sample_format;
  203. jack_nframes_t cur_sample_rate;
  204. if (fCaptureChannels == 0) fCaptureChannels = 2;
  205. if ((fInFD = open(fCaptureDriverName, O_RDONLY | ((fExcl) ? O_EXCL : 0))) < 0) {
  206. jack_error("JackOSSDriver::OpenInput failed to open device : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  207. return -1;
  208. }
  209. jack_log("JackOSSDriver::OpenInput input fInFD = %d", fInFD);
  210. if (fExcl) {
  211. if (ioctl(fInFD, SNDCTL_DSP_COOKEDMODE, &flags) == -1) {
  212. jack_error("JackOSSDriver::OpenInput failed to set cooked mode : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  213. goto error;
  214. }
  215. }
  216. gFragFormat = (2 << 16) + int2pow2(fEngineControl->fBufferSize * fSampleSize * fCaptureChannels);
  217. if (ioctl(fInFD, SNDCTL_DSP_SETFRAGMENT, &gFragFormat) == -1) {
  218. jack_error("JackOSSDriver::OpenInput failed to set fragments : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  219. goto error;
  220. }
  221. cur_sample_format = fSampleFormat;
  222. if (ioctl(fInFD, SNDCTL_DSP_SETFMT, &fSampleFormat) == -1) {
  223. jack_error("JackOSSDriver::OpenInput failed to set format : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  224. goto error;
  225. }
  226. if (cur_sample_format != fSampleFormat) {
  227. jack_info("JackOSSDriver::OpenInput driver forced the sample format %ld", fSampleFormat);
  228. }
  229. cur_capture_channels = fCaptureChannels;
  230. if (ioctl(fInFD, SNDCTL_DSP_CHANNELS, &fCaptureChannels) == -1) {
  231. jack_error("JackOSSDriver::OpenInput failed to set channels : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  232. goto error;
  233. }
  234. if (cur_capture_channels != fCaptureChannels) {
  235. jack_info("JackOSSDriver::OpenInput driver forced the number of capture channels %ld", fCaptureChannels);
  236. }
  237. cur_sample_rate = fEngineControl->fSampleRate;
  238. if (ioctl(fInFD, SNDCTL_DSP_SPEED, &fEngineControl->fSampleRate) == -1) {
  239. jack_error("JackOSSDriver::OpenInput failed to set sample rate : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  240. goto error;
  241. }
  242. if (cur_sample_rate != fEngineControl->fSampleRate) {
  243. jack_info("JackOSSDriver::OpenInput driver forced the sample rate %ld", fEngineControl->fSampleRate);
  244. }
  245. fInputBufferSize = 0;
  246. if (ioctl(fInFD, SNDCTL_DSP_GETBLKSIZE, &fInputBufferSize) == -1) {
  247. jack_error("JackOSSDriver::OpenInput failed to get fragments : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  248. goto error;
  249. }
  250. if (fInputBufferSize != fEngineControl->fBufferSize * fSampleSize * fCaptureChannels) {
  251. if (fIgnoreHW) {
  252. int new_buffer_size = fInputBufferSize / (fSampleSize * fCaptureChannels);
  253. jack_info("JackOSSDriver::OpenInput driver forced buffer size %ld", new_buffer_size);
  254. JackAudioDriver::SetBufferSize(new_buffer_size); // never fails
  255. } else {
  256. jack_error("JackOSSDriver::OpenInput wanted buffer size cannot be obtained");
  257. goto error;
  258. }
  259. }
  260. fInputBuffer = (void*)calloc(fInputBufferSize, 1);
  261. assert(fInputBuffer);
  262. return 0;
  263. error:
  264. ::close(fInFD);
  265. return -1;
  266. }
  267. int JackOSSDriver::OpenOutput()
  268. {
  269. int flags = 0;
  270. int gFragFormat;
  271. int cur_sample_format;
  272. int cur_playback_channels;
  273. jack_nframes_t cur_sample_rate;
  274. if (fPlaybackChannels == 0) fPlaybackChannels = 2;
  275. if ((fOutFD = open(fPlaybackDriverName, O_WRONLY | ((fExcl) ? O_EXCL : 0))) < 0) {
  276. jack_error("JackOSSDriver::OpenOutput failed to open device : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  277. return -1;
  278. }
  279. jack_log("JackOSSDriver::OpenOutput output fOutFD = %d", fOutFD);
  280. if (fExcl) {
  281. if (ioctl(fOutFD, SNDCTL_DSP_COOKEDMODE, &flags) == -1) {
  282. jack_error("JackOSSDriver::OpenOutput failed to set cooked mode : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  283. goto error;
  284. }
  285. }
  286. gFragFormat = (2 << 16) + int2pow2(fEngineControl->fBufferSize * fSampleSize * fPlaybackChannels);
  287. if (ioctl(fOutFD, SNDCTL_DSP_SETFRAGMENT, &gFragFormat) == -1) {
  288. jack_error("JackOSSDriver::OpenOutput failed to set fragments : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  289. goto error;
  290. }
  291. cur_sample_format = fSampleFormat;
  292. if (ioctl(fOutFD, SNDCTL_DSP_SETFMT, &fSampleFormat) == -1) {
  293. jack_error("JackOSSDriver::OpenOutput failed to set format : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  294. goto error;
  295. }
  296. if (cur_sample_format != fSampleFormat) {
  297. jack_info("JackOSSDriver::OpenOutput driver forced the sample format %ld", fSampleFormat);
  298. }
  299. cur_playback_channels = fPlaybackChannels;
  300. if (ioctl(fOutFD, SNDCTL_DSP_CHANNELS, &fPlaybackChannels) == -1) {
  301. jack_error("JackOSSDriver::OpenOutput failed to set channels : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  302. goto error;
  303. }
  304. if (cur_playback_channels != fPlaybackChannels) {
  305. jack_info("JackOSSDriver::OpenOutput driver forced the number of playback channels %ld", fPlaybackChannels);
  306. }
  307. cur_sample_rate = fEngineControl->fSampleRate;
  308. if (ioctl(fOutFD, SNDCTL_DSP_SPEED, &fEngineControl->fSampleRate) == -1) {
  309. jack_error("JackOSSDriver::OpenOutput failed to set sample rate : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  310. goto error;
  311. }
  312. if (cur_sample_rate != fEngineControl->fSampleRate) {
  313. jack_info("JackOSSDriver::OpenInput driver forced the sample rate %ld", fEngineControl->fSampleRate);
  314. }
  315. fOutputBufferSize = 0;
  316. if (ioctl(fOutFD, SNDCTL_DSP_GETBLKSIZE, &fOutputBufferSize) == -1) {
  317. jack_error("JackOSSDriver::OpenOutput failed to get fragments : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  318. goto error;
  319. }
  320. if (fOutputBufferSize != fEngineControl->fBufferSize * fSampleSize * fPlaybackChannels) {
  321. if (fIgnoreHW) {
  322. int new_buffer_size = fOutputBufferSize / (fSampleSize * fPlaybackChannels);
  323. jack_info("JackOSSDriver::OpenOutput driver forced buffer size %ld", new_buffer_size);
  324. JackAudioDriver::SetBufferSize(new_buffer_size); // never fails
  325. } else {
  326. jack_error("JackOSSDriver::OpenInput wanted buffer size cannot be obtained");
  327. goto error;
  328. }
  329. }
  330. fOutputBuffer = (void*)calloc(fOutputBufferSize, 1);
  331. fFirstCycle = true;
  332. assert(fOutputBuffer);
  333. return 0;
  334. error:
  335. ::close(fOutFD);
  336. return -1;
  337. }
  338. int JackOSSDriver::Open(jack_nframes_t nframes,
  339. int user_nperiods,
  340. jack_nframes_t samplerate,
  341. bool capturing,
  342. bool playing,
  343. int inchannels,
  344. int outchannels,
  345. bool excl,
  346. bool monitor,
  347. const char* capture_driver_uid,
  348. const char* playback_driver_uid,
  349. jack_nframes_t capture_latency,
  350. jack_nframes_t playback_latency,
  351. int bits,
  352. bool ignorehwbuf)
  353. {
  354. // Generic JackAudioDriver Open
  355. if (JackAudioDriver::Open(nframes, samplerate, capturing, playing, inchannels, outchannels, monitor,
  356. capture_driver_uid, playback_driver_uid, capture_latency, playback_latency) != 0) {
  357. return -1;
  358. } else {
  359. if (!fEngineControl->fSyncMode) {
  360. jack_error("Cannot run in asynchronous mode, use the -S parameter for jackd");
  361. return -1;
  362. }
  363. fRWMode |= ((capturing) ? kRead : 0);
  364. fRWMode |= ((playing) ? kWrite : 0);
  365. fBits = bits;
  366. fIgnoreHW = ignorehwbuf;
  367. fNperiods = user_nperiods;
  368. fExcl = excl;
  369. #ifdef JACK_MONITOR
  370. // Force memory page in
  371. memset(&gCycleTable, 0, sizeof(gCycleTable));
  372. #endif
  373. if (OpenAux() < 0) {
  374. Close();
  375. return -1;
  376. } else {
  377. return 0;
  378. }
  379. }
  380. }
  381. int JackOSSDriver::Close()
  382. {
  383. #ifdef JACK_MONITOR
  384. FILE* file = fopen("OSSProfiling.log", "w");
  385. if (file) {
  386. jack_info("Writing OSS driver timing data....");
  387. for (int i = 1; i < gCycleCount; i++) {
  388. int d1 = gCycleTable.fTable[i].fAfterRead - gCycleTable.fTable[i].fBeforeRead;
  389. int d2 = gCycleTable.fTable[i].fAfterReadConvert - gCycleTable.fTable[i].fAfterRead;
  390. int d3 = gCycleTable.fTable[i].fAfterWrite - gCycleTable.fTable[i].fBeforeWrite;
  391. int d4 = gCycleTable.fTable[i].fBeforeWrite - gCycleTable.fTable[i].fBeforeWriteConvert;
  392. fprintf(file, "%d \t %d \t %d \t %d \t \n", d1, d2, d3, d4);
  393. }
  394. fclose(file);
  395. } else {
  396. jack_error("JackOSSDriver::Close : cannot open OSSProfiling.log file");
  397. }
  398. file = fopen("TimingOSS.plot", "w");
  399. if (file == NULL) {
  400. jack_error("JackOSSDriver::Close cannot open TimingOSS.plot file");
  401. } else {
  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. fprintf(file, "set output 'TimingOSS.pdf\n");
  411. fprintf(file, "set terminal pdf\n");
  412. fprintf(file, "set grid\n");
  413. fprintf(file, "set title \"OSS audio driver timing\"\n");
  414. fprintf(file, "set xlabel \"audio cycles\"\n");
  415. fprintf(file, "set ylabel \"usec\"\n");
  416. fprintf(file, "plot \"OSSProfiling.log\" using 1 title \"Driver read wait\" with lines, \
  417. \"OSSProfiling.log\" using 2 title \"Driver read convert duration\" with lines, \
  418. \"OSSProfiling.log\" using 3 title \"Driver write wait\" with lines, \
  419. \"OSSProfiling.log\" using 4 title \"Driver write convert duration\" with lines\n");
  420. fclose(file);
  421. }
  422. #endif
  423. int res = JackAudioDriver::Close();
  424. CloseAux();
  425. return res;
  426. }
  427. int JackOSSDriver::OpenAux()
  428. {
  429. SetSampleFormat();
  430. if ((fRWMode & kRead) && (OpenInput() < 0)) {
  431. return -1;
  432. }
  433. if ((fRWMode & kWrite) && (OpenOutput() < 0)) {
  434. return -1;
  435. }
  436. // In duplex mode, check that input and output use the same buffer size
  437. /*
  438. 10/02/09 : desactivated for now, needs more check (only needed when *same* device is used for input and output ??)
  439. if ((fRWMode & kRead) && (fRWMode & kWrite) && (fInputBufferSize != fOutputBufferSize)) {
  440. jack_error("JackOSSDriver::OpenAux input and output buffer size are not the same!!");
  441. return -1;
  442. }
  443. */
  444. DisplayDeviceInfo();
  445. return 0;
  446. }
  447. void JackOSSDriver::CloseAux()
  448. {
  449. if (fRWMode & kRead && fInFD > 0) {
  450. close(fInFD);
  451. fInFD = -1;
  452. }
  453. if (fRWMode & kWrite && fOutFD > 0) {
  454. close(fOutFD);
  455. fOutFD = -1;
  456. }
  457. if (fInputBuffer)
  458. free(fInputBuffer);
  459. fInputBuffer = NULL;
  460. if (fOutputBuffer)
  461. free(fOutputBuffer);
  462. fOutputBuffer = NULL;
  463. }
  464. int JackOSSDriver::Read()
  465. {
  466. if (fInFD < 0) {
  467. // Keep begin cycle time
  468. JackDriver::CycleTakeBeginTime();
  469. return 0;
  470. }
  471. ssize_t count;
  472. #ifdef JACK_MONITOR
  473. gCycleTable.fTable[gCycleCount].fBeforeRead = GetMicroSeconds();
  474. #endif
  475. audio_errinfo ei_in;
  476. count = ::read(fInFD, fInputBuffer, fInputBufferSize);
  477. #ifdef JACK_MONITOR
  478. if (count > 0 && count != (int)fInputBufferSize)
  479. jack_log("JackOSSDriver::Read count = %ld", count / (fSampleSize * fCaptureChannels));
  480. gCycleTable.fTable[gCycleCount].fAfterRead = GetMicroSeconds();
  481. #endif
  482. // XRun detection
  483. if (ioctl(fInFD, SNDCTL_DSP_GETERROR, &ei_in) == 0) {
  484. if (ei_in.rec_overruns > 0 ) {
  485. jack_error("JackOSSDriver::Read overruns");
  486. jack_time_t cur_time = GetMicroSeconds();
  487. NotifyXRun(cur_time, float(cur_time - fBeginDateUst)); // Better this value than nothing...
  488. }
  489. if (ei_in.rec_errorcount > 0 && ei_in.rec_lasterror != 0) {
  490. jack_error("%d OSS rec event(s), last=%05d:%d", ei_in.rec_errorcount, ei_in.rec_lasterror, ei_in.rec_errorparm);
  491. }
  492. }
  493. if (count < 0) {
  494. jack_log("JackOSSDriver::Read error = %s", strerror(errno));
  495. return -1;
  496. } else if (count < (int)fInputBufferSize) {
  497. jack_error("JackOSSDriver::Read error bytes read = %ld", count);
  498. return -1;
  499. } else {
  500. // Keep begin cycle time
  501. JackDriver::CycleTakeBeginTime();
  502. for (int i = 0; i < fCaptureChannels; i++) {
  503. if (fGraphManager->GetConnectionsNum(fCapturePortList[i]) > 0) {
  504. CopyAndConvertIn(GetInputBuffer(i), fInputBuffer, fEngineControl->fBufferSize, i, fCaptureChannels, fBits);
  505. }
  506. }
  507. #ifdef JACK_MONITOR
  508. gCycleTable.fTable[gCycleCount].fAfterReadConvert = GetMicroSeconds();
  509. #endif
  510. return 0;
  511. }
  512. }
  513. int JackOSSDriver::Write()
  514. {
  515. if (fOutFD < 0) {
  516. // Keep end cycle time
  517. JackDriver::CycleTakeEndTime();
  518. return 0;
  519. }
  520. ssize_t count;
  521. audio_errinfo ei_out;
  522. // Maybe necessary to write an empty output buffer first time : see http://manuals.opensound.com/developer/fulldup.c.html
  523. if (fFirstCycle) {
  524. fFirstCycle = false;
  525. memset(fOutputBuffer, 0, fOutputBufferSize);
  526. // Prefill ouput buffer
  527. for (int i = 0; i < fNperiods; i++) {
  528. count = ::write(fOutFD, fOutputBuffer, fOutputBufferSize);
  529. if (count < (int)fOutputBufferSize) {
  530. jack_error("JackOSSDriver::Write error bytes written = %ld", count);
  531. return -1;
  532. }
  533. }
  534. int delay;
  535. if (ioctl(fOutFD, SNDCTL_DSP_GETODELAY, &delay) == -1) {
  536. jack_error("JackOSSDriver::Write error get out delay : %s@%i, errno = %d", __FILE__, __LINE__, errno);
  537. return -1;
  538. }
  539. delay /= fSampleSize * fPlaybackChannels;
  540. jack_info("JackOSSDriver::Write output latency frames = %ld", delay);
  541. }
  542. #ifdef JACK_MONITOR
  543. gCycleTable.fTable[gCycleCount].fBeforeWriteConvert = GetMicroSeconds();
  544. #endif
  545. memset(fOutputBuffer, 0, fOutputBufferSize);
  546. for (int i = 0; i < fPlaybackChannels; i++) {
  547. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
  548. CopyAndConvertOut(fOutputBuffer, GetOutputBuffer(i), fEngineControl->fBufferSize, i, fPlaybackChannels, fBits);
  549. }
  550. }
  551. #ifdef JACK_MONITOR
  552. gCycleTable.fTable[gCycleCount].fBeforeWrite = GetMicroSeconds();
  553. #endif
  554. // Keep end cycle time
  555. JackDriver::CycleTakeEndTime();
  556. count = ::write(fOutFD, fOutputBuffer, fOutputBufferSize);
  557. #ifdef JACK_MONITOR
  558. if (count > 0 && count != (int)fOutputBufferSize)
  559. jack_log("JackOSSDriver::Write count = %ld", count / (fSampleSize * fPlaybackChannels));
  560. gCycleTable.fTable[gCycleCount].fAfterWrite = GetMicroSeconds();
  561. gCycleCount = (gCycleCount == CYCLE_POINTS - 1) ? gCycleCount: gCycleCount + 1;
  562. #endif
  563. // XRun detection
  564. if (ioctl(fOutFD, SNDCTL_DSP_GETERROR, &ei_out) == 0) {
  565. if (ei_out.play_underruns > 0) {
  566. jack_error("JackOSSDriver::Write underruns");
  567. jack_time_t cur_time = GetMicroSeconds();
  568. NotifyXRun(cur_time, float(cur_time - fBeginDateUst)); // Better this value than nothing...
  569. }
  570. if (ei_out.play_errorcount > 0 && ei_out.play_lasterror != 0) {
  571. jack_error("%d OSS play event(s), last=%05d:%d",ei_out.play_errorcount, ei_out.play_lasterror, ei_out.play_errorparm);
  572. }
  573. }
  574. if (count < 0) {
  575. jack_log("JackOSSDriver::Write error = %s", strerror(errno));
  576. return -1;
  577. } else if (count < (int)fOutputBufferSize) {
  578. jack_error("JackOSSDriver::Write error bytes written = %ld", count);
  579. return -1;
  580. } else {
  581. return 0;
  582. }
  583. }
  584. int JackOSSDriver::SetBufferSize(jack_nframes_t buffer_size)
  585. {
  586. CloseAux();
  587. JackAudioDriver::SetBufferSize(buffer_size); // Generic change, never fails
  588. return OpenAux();
  589. }
  590. int JackOSSDriver::ProcessSync()
  591. {
  592. // Read input buffers for the current cycle
  593. if (Read() < 0) {
  594. jack_error("ProcessSync: read error, skip cycle");
  595. return 0; // Non fatal error here, skip cycle, but continue processing...
  596. }
  597. if (fIsMaster) {
  598. ProcessGraphSync();
  599. } else {
  600. fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
  601. }
  602. // Write output buffers for the current cycle
  603. if (Write() < 0) {
  604. jack_error("JackAudioDriver::ProcessSync: write error, skip cycle");
  605. return 0; // Non fatal error here, skip cycle, but continue processing...
  606. }
  607. return 0;
  608. }
  609. } // end of namespace
  610. #ifdef __cplusplus
  611. extern "C"
  612. {
  613. #endif
  614. SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor()
  615. {
  616. jack_driver_desc_t * desc;
  617. jack_driver_desc_filler_t filler;
  618. jack_driver_param_value_t value;
  619. desc = jack_driver_descriptor_construct("oss", "OSS API based audio backend", &filler);
  620. value.ui = OSS_DRIVER_DEF_FS;
  621. jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
  622. value.ui = OSS_DRIVER_DEF_BLKSIZE;
  623. jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames per period", NULL);
  624. value.ui = OSS_DRIVER_DEF_NPERIODS;
  625. jack_driver_descriptor_add_parameter(desc, &filler, "nperiods", 'n', JackDriverParamUInt, &value, NULL, "Number of periods to prefill output buffer", NULL);
  626. value.i = OSS_DRIVER_DEF_BITS;
  627. jack_driver_descriptor_add_parameter(desc, &filler, "wordlength", 'w', JackDriverParamInt, &value, NULL, "Word length", NULL);
  628. value.ui = OSS_DRIVER_DEF_INS;
  629. jack_driver_descriptor_add_parameter(desc, &filler, "inchannels", 'i', JackDriverParamUInt, &value, NULL, "Capture channels", NULL);
  630. value.ui = OSS_DRIVER_DEF_OUTS;
  631. jack_driver_descriptor_add_parameter(desc, &filler, "outchannels", 'o', JackDriverParamUInt, &value, NULL, "Playback channels", NULL);
  632. value.i = false;
  633. jack_driver_descriptor_add_parameter(desc, &filler, "excl", 'e', JackDriverParamBool, &value, NULL, "Exclusif (O_EXCL) access mode", NULL);
  634. strcpy(value.str, OSS_DRIVER_DEF_DEV);
  635. jack_driver_descriptor_add_parameter(desc, &filler, "capture", 'C', JackDriverParamString, &value, NULL, "Input device", NULL);
  636. jack_driver_descriptor_add_parameter(desc, &filler, "playback", 'P', JackDriverParamString, &value, NULL, "Output device", NULL);
  637. jack_driver_descriptor_add_parameter(desc, &filler, "device", 'd', JackDriverParamString, &value, NULL, "OSS device name", NULL);
  638. value.i = false;
  639. jack_driver_descriptor_add_parameter(desc, &filler, "ignorehwbuf", 'b', JackDriverParamBool, &value, NULL, "Ignore hardware period size", NULL);
  640. value.ui = 0;
  641. jack_driver_descriptor_add_parameter(desc, &filler, "input-latency", 'I', JackDriverParamUInt, &value, NULL, "Extra input latency", NULL);
  642. jack_driver_descriptor_add_parameter(desc, &filler, "output-latency", 'O', JackDriverParamUInt, &value, NULL, "Extra output latency", NULL);
  643. return desc;
  644. }
  645. EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  646. {
  647. int bits = OSS_DRIVER_DEF_BITS;
  648. jack_nframes_t srate = OSS_DRIVER_DEF_FS;
  649. jack_nframes_t frames_per_interrupt = OSS_DRIVER_DEF_BLKSIZE;
  650. const char* capture_pcm_name = OSS_DRIVER_DEF_DEV;
  651. const char* playback_pcm_name = OSS_DRIVER_DEF_DEV;
  652. bool capture = false;
  653. bool playback = false;
  654. int chan_in = 0;
  655. int chan_out = 0;
  656. bool monitor = false;
  657. bool excl = false;
  658. unsigned int nperiods = OSS_DRIVER_DEF_NPERIODS;
  659. const JSList *node;
  660. const jack_driver_param_t *param;
  661. bool ignorehwbuf = false;
  662. jack_nframes_t systemic_input_latency = 0;
  663. jack_nframes_t systemic_output_latency = 0;
  664. for (node = params; node; node = jack_slist_next(node)) {
  665. param = (const jack_driver_param_t *)node->data;
  666. switch (param->character) {
  667. case 'r':
  668. srate = param->value.ui;
  669. break;
  670. case 'p':
  671. frames_per_interrupt = (unsigned int)param->value.ui;
  672. break;
  673. case 'n':
  674. nperiods = (unsigned int)param->value.ui;
  675. break;
  676. case 'w':
  677. bits = param->value.i;
  678. break;
  679. case 'i':
  680. chan_in = (int)param->value.ui;
  681. break;
  682. case 'o':
  683. chan_out = (int)param->value.ui;
  684. break;
  685. case 'C':
  686. capture = true;
  687. if (strcmp(param->value.str, "none") != 0) {
  688. capture_pcm_name = param->value.str;
  689. }
  690. break;
  691. case 'P':
  692. playback = true;
  693. if (strcmp(param->value.str, "none") != 0) {
  694. playback_pcm_name = param->value.str;
  695. }
  696. break;
  697. case 'd':
  698. playback_pcm_name = param->value.str;
  699. capture_pcm_name = param->value.str;
  700. break;
  701. case 'b':
  702. ignorehwbuf = true;
  703. break;
  704. case 'e':
  705. excl = true;
  706. break;
  707. case 'I':
  708. systemic_input_latency = param->value.ui;
  709. break;
  710. case 'O':
  711. systemic_output_latency = param->value.ui;
  712. break;
  713. }
  714. }
  715. // duplex is the default
  716. if (!capture && !playback) {
  717. capture = true;
  718. playback = true;
  719. }
  720. Jack::JackOSSDriver* oss_driver = new Jack::JackOSSDriver("system", "oss", engine, table);
  721. Jack::JackDriverClientInterface* threaded_driver = new Jack::JackThreadedDriver(oss_driver);
  722. // Special open for OSS driver...
  723. if (oss_driver->Open(frames_per_interrupt, nperiods, srate, capture, playback, chan_in, chan_out,
  724. excl, monitor, capture_pcm_name, playback_pcm_name, systemic_input_latency, systemic_output_latency, bits, ignorehwbuf) == 0) {
  725. return threaded_driver;
  726. } else {
  727. delete threaded_driver; // Delete the decorated driver
  728. return NULL;
  729. }
  730. }
  731. #ifdef __cplusplus
  732. }
  733. #endif