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.

2755 lines
89KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004 Grame
  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. #if defined(HAVE_CONFIG_H)
  17. #include "config.h"
  18. #endif
  19. #include <iostream>
  20. #include <unistd.h>
  21. #include <math.h>
  22. #include <stdio.h>
  23. #include <memory.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <errno.h>
  27. #include <stdarg.h>
  28. #include <signal.h>
  29. #include <sys/types.h>
  30. #include <sys/time.h>
  31. #include <regex.h>
  32. #include <string.h>
  33. #include "JackAlsaDriver.h"
  34. #include "JackEngineControl.h"
  35. #include "JackClientControl.h"
  36. #include "JackPort.h"
  37. #include "JackGraphManager.h"
  38. #include "JackLockedEngine.h"
  39. #include "hammerfall.h"
  40. #include "hdsp.h"
  41. #include "ice1712.h"
  42. #include "usx2y.h"
  43. #include "generic.h"
  44. #include "memops.h"
  45. #include "JackPosixThread.h"
  46. #include "JackExports.h"
  47. namespace Jack
  48. {
  49. typedef long AudioDeviceID;
  50. #define jack_get_microseconds GetMicroSeconds
  51. /* Delay (in process calls) before jackd will report an xrun */
  52. #define XRUN_REPORT_DELAY 0
  53. void
  54. JackAlsaDriver::alsa_driver_release_channel_dependent_memory (alsa_driver_t *driver)
  55. {
  56. bitset_destroy (&driver->channels_done);
  57. bitset_destroy (&driver->channels_not_done);
  58. if (driver->playback_addr) {
  59. free (driver->playback_addr);
  60. driver->playback_addr = 0;
  61. }
  62. if (driver->capture_addr) {
  63. free (driver->capture_addr);
  64. driver->capture_addr = 0;
  65. }
  66. if (driver->playback_interleave_skip) {
  67. free (driver->playback_interleave_skip);
  68. driver->playback_interleave_skip = NULL;
  69. }
  70. if (driver->capture_interleave_skip) {
  71. free (driver->capture_interleave_skip);
  72. driver->capture_interleave_skip = NULL;
  73. }
  74. if (driver->silent) {
  75. free (driver->silent);
  76. driver->silent = 0;
  77. }
  78. if (driver->dither_state) {
  79. free (driver->dither_state);
  80. driver->dither_state = 0;
  81. }
  82. }
  83. int
  84. JackAlsaDriver::alsa_driver_check_capabilities (alsa_driver_t *driver)
  85. {
  86. return 0;
  87. }
  88. int
  89. JackAlsaDriver::alsa_driver_check_card_type (alsa_driver_t *driver)
  90. {
  91. int err;
  92. snd_ctl_card_info_t *card_info;
  93. char * ctl_name;
  94. regex_t expression;
  95. snd_ctl_card_info_alloca (&card_info);
  96. regcomp(&expression, "(plug)?hw:[0-9](,[0-9])?", REG_ICASE | REG_EXTENDED);
  97. if (!regexec(&expression, driver->alsa_name_playback, 0, NULL, 0)) {
  98. /* the user wants a hw or plughw device, the ctl name
  99. * should be hw:x where x is the card number */
  100. char tmp[5];
  101. strncpy(tmp, strstr(driver->alsa_name_playback, "hw"), 4);
  102. tmp[4] = '\0';
  103. jack_log("control device %s", tmp);
  104. ctl_name = strdup(tmp);
  105. } else {
  106. ctl_name = strdup(driver->alsa_name_playback);
  107. }
  108. // XXX: I don't know the "right" way to do this. Which to use
  109. // driver->alsa_name_playback or driver->alsa_name_capture.
  110. if ((err = snd_ctl_open (&driver->ctl_handle, ctl_name, 0)) < 0) {
  111. jack_error ("control open \"%s\" (%s)", ctl_name,
  112. snd_strerror(err));
  113. return -1;
  114. }
  115. if ((err = snd_ctl_card_info(driver->ctl_handle, card_info)) < 0) {
  116. jack_error ("control hardware info \"%s\" (%s)",
  117. driver->alsa_name_playback, snd_strerror (err));
  118. snd_ctl_close (driver->ctl_handle);
  119. return -1;
  120. }
  121. driver->alsa_driver = strdup(snd_ctl_card_info_get_driver (card_info));
  122. regfree(&expression);
  123. free(ctl_name);
  124. return alsa_driver_check_capabilities (driver);
  125. }
  126. int
  127. JackAlsaDriver::alsa_driver_hammerfall_hardware (alsa_driver_t *driver)
  128. {
  129. driver->hw = jack_alsa_hammerfall_hw_new (driver);
  130. return 0;
  131. }
  132. int
  133. JackAlsaDriver::alsa_driver_hdsp_hardware (alsa_driver_t *driver)
  134. {
  135. driver->hw = jack_alsa_hdsp_hw_new (driver);
  136. return 0;
  137. }
  138. int
  139. JackAlsaDriver::alsa_driver_ice1712_hardware (alsa_driver_t *driver)
  140. {
  141. driver->hw = jack_alsa_ice1712_hw_new (driver);
  142. return 0;
  143. }
  144. int
  145. JackAlsaDriver::alsa_driver_usx2y_hardware (alsa_driver_t *driver)
  146. {
  147. // TODO : will need so deeped redesign
  148. // driver->hw = jack_alsa_usx2y_hw_new (driver);
  149. return 0;
  150. }
  151. int
  152. JackAlsaDriver::alsa_driver_generic_hardware (alsa_driver_t *driver)
  153. {
  154. driver->hw = jack_alsa_generic_hw_new (driver);
  155. return 0;
  156. }
  157. int
  158. JackAlsaDriver::alsa_driver_hw_specific (alsa_driver_t *driver, int hw_monitoring,
  159. int hw_metering)
  160. {
  161. int err;
  162. if (!strcmp(driver->alsa_driver, "RME9652")) {
  163. if ((err = alsa_driver_hammerfall_hardware (driver)) != 0) {
  164. return err;
  165. }
  166. } else if (!strcmp(driver->alsa_driver, "H-DSP")) {
  167. if ((err = alsa_driver_hdsp_hardware (driver)) != 0) {
  168. return err;
  169. }
  170. } else if (!strcmp(driver->alsa_driver, "ICE1712")) {
  171. if ((err = alsa_driver_ice1712_hardware (driver)) != 0) {
  172. return err;
  173. }
  174. } else if (!strcmp(driver->alsa_driver, "USB US-X2Y")) {
  175. if ((err = alsa_driver_usx2y_hardware (driver)) != 0) {
  176. return err;
  177. }
  178. } else {
  179. if ((err = alsa_driver_generic_hardware (driver)) != 0) {
  180. return err;
  181. }
  182. }
  183. if (driver->hw->capabilities & Cap_HardwareMonitoring) {
  184. driver->has_hw_monitoring = TRUE;
  185. /* XXX need to ensure that this is really FALSE or
  186. * TRUE or whatever*/
  187. driver->hw_monitoring = hw_monitoring;
  188. } else {
  189. driver->has_hw_monitoring = FALSE;
  190. driver->hw_monitoring = FALSE;
  191. }
  192. if (driver->hw->capabilities & Cap_ClockLockReporting) {
  193. driver->has_clock_sync_reporting = TRUE;
  194. } else {
  195. driver->has_clock_sync_reporting = FALSE;
  196. }
  197. if (driver->hw->capabilities & Cap_HardwareMetering) {
  198. driver->has_hw_metering = TRUE;
  199. driver->hw_metering = hw_metering;
  200. } else {
  201. driver->has_hw_metering = FALSE;
  202. driver->hw_metering = FALSE;
  203. }
  204. return 0;
  205. }
  206. void
  207. JackAlsaDriver::alsa_driver_setup_io_function_pointers (alsa_driver_t *driver)
  208. {
  209. switch (driver->playback_sample_bytes) {
  210. case 2:
  211. if (driver->playback_interleaved) {
  212. driver->channel_copy = memcpy_interleave_d16_s16;
  213. } else {
  214. driver->channel_copy = memcpy_fake;
  215. }
  216. switch (driver->dither) {
  217. case Rectangular:
  218. jack_log("Rectangular dithering at 16 bits");
  219. driver->write_via_copy = driver->quirk_bswap ?
  220. sample_move_dither_rect_d16_sSs :
  221. sample_move_dither_rect_d16_sS;
  222. break;
  223. case Triangular:
  224. jack_log("Triangular dithering at 16 bits");
  225. driver->write_via_copy = driver->quirk_bswap ?
  226. sample_move_dither_tri_d16_sSs :
  227. sample_move_dither_tri_d16_sS;
  228. break;
  229. case Shaped:
  230. jack_log("Noise-shaped dithering at 16 bits");
  231. driver->write_via_copy = driver->quirk_bswap ?
  232. sample_move_dither_shaped_d16_sSs :
  233. sample_move_dither_shaped_d16_sS;
  234. break;
  235. default:
  236. driver->write_via_copy = driver->quirk_bswap ?
  237. sample_move_d16_sSs : sample_move_d16_sS;
  238. break;
  239. }
  240. break;
  241. case 3:
  242. if (driver->playback_interleaved) {
  243. driver->channel_copy = memcpy_interleave_d24_s24;
  244. } else {
  245. driver->channel_copy = memcpy_fake;
  246. }
  247. switch (driver->dither) {
  248. case Rectangular:
  249. jack_log("Rectangular dithering at 16 bits");
  250. driver->write_via_copy = driver->quirk_bswap ?
  251. sample_move_dither_rect_d24_sSs :
  252. sample_move_dither_rect_d24_sS;
  253. break;
  254. case Triangular:
  255. jack_log("Triangular dithering at 16 bits");
  256. driver->write_via_copy = driver->quirk_bswap ?
  257. sample_move_dither_tri_d24_sSs :
  258. sample_move_dither_tri_d24_sS;
  259. break;
  260. case Shaped:
  261. jack_log("Noise-shaped dithering at 16 bits");
  262. driver->write_via_copy = driver->quirk_bswap ?
  263. sample_move_dither_shaped_d24_sSs :
  264. sample_move_dither_shaped_d24_sS;
  265. break;
  266. default:
  267. driver->write_via_copy = driver->quirk_bswap ?
  268. sample_move_d24_sSs : sample_move_d24_sS;
  269. break;
  270. }
  271. break;
  272. case 4:
  273. if (driver->playback_interleaved) {
  274. driver->channel_copy = memcpy_interleave_d32_s32;
  275. } else {
  276. driver->channel_copy = memcpy_fake;
  277. }
  278. switch (driver->dither) {
  279. case Rectangular:
  280. jack_log("Rectangular dithering at 16 bits");
  281. driver->write_via_copy = driver->quirk_bswap ?
  282. sample_move_dither_rect_d32u24_sSs :
  283. sample_move_dither_rect_d32u24_sS;
  284. break;
  285. case Triangular:
  286. jack_log("Triangular dithering at 16 bits");
  287. driver->write_via_copy = driver->quirk_bswap ?
  288. sample_move_dither_tri_d32u24_sSs :
  289. sample_move_dither_tri_d32u24_sS;
  290. break;
  291. case Shaped:
  292. jack_log("Noise-shaped dithering at 16 bits");
  293. driver->write_via_copy = driver->quirk_bswap ?
  294. sample_move_dither_shaped_d32u24_sSs :
  295. sample_move_dither_shaped_d32u24_sS;
  296. break;
  297. default:
  298. driver->write_via_copy = driver->quirk_bswap ?
  299. sample_move_d32u24_sSs : sample_move_d32u24_sS;
  300. break;
  301. }
  302. break;
  303. }
  304. switch (driver->capture_sample_bytes) {
  305. case 2:
  306. driver->read_via_copy = driver->quirk_bswap ?
  307. sample_move_dS_s16s : sample_move_dS_s16;
  308. break;
  309. case 3:
  310. driver->read_via_copy = driver->quirk_bswap ?
  311. sample_move_dS_s24s : sample_move_dS_s24;
  312. break;
  313. case 4:
  314. driver->read_via_copy = driver->quirk_bswap ?
  315. sample_move_dS_s32u24s : sample_move_dS_s32u24;
  316. break;
  317. }
  318. }
  319. int
  320. JackAlsaDriver::alsa_driver_configure_stream (alsa_driver_t *driver, char *device_name,
  321. const char *stream_name,
  322. snd_pcm_t *handle,
  323. snd_pcm_hw_params_t *hw_params,
  324. snd_pcm_sw_params_t *sw_params,
  325. unsigned int *nperiodsp,
  326. unsigned long *nchns,
  327. unsigned long sample_width)
  328. {
  329. int err, format;
  330. unsigned int frame_rate;
  331. snd_pcm_uframes_t stop_th;
  332. static struct {
  333. char Name[32];
  334. snd_pcm_format_t format;
  335. int swapped;
  336. }
  337. formats[] = {
  338. {"32bit little-endian", SND_PCM_FORMAT_S32_LE, IS_LE},
  339. {"32bit big-endian", SND_PCM_FORMAT_S32_BE, IS_BE},
  340. {"24bit little-endian", SND_PCM_FORMAT_S24_3LE, IS_LE},
  341. {"24bit big-endian", SND_PCM_FORMAT_S24_3BE, IS_BE},
  342. {"16bit little-endian", SND_PCM_FORMAT_S16_LE, IS_LE},
  343. {"16bit big-endian", SND_PCM_FORMAT_S16_BE, IS_BE},
  344. };
  345. #define NUMFORMATS (sizeof(formats)/sizeof(formats[0]))
  346. #define FIRST_16BIT_FORMAT 4
  347. if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0) {
  348. jack_error ("ALSA: no playback configurations available (%s)",
  349. snd_strerror (err));
  350. return -1;
  351. }
  352. if ((err = snd_pcm_hw_params_set_periods_integer (handle, hw_params))
  353. < 0) {
  354. jack_error ("ALSA: cannot restrict period size to integral"
  355. " value.");
  356. return -1;
  357. }
  358. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_NONINTERLEAVED)) < 0) {
  359. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
  360. if ((err = snd_pcm_hw_params_set_access (
  361. handle, hw_params,
  362. SND_PCM_ACCESS_MMAP_COMPLEX)) < 0) {
  363. jack_error ("ALSA: mmap-based access is not possible"
  364. " for the %s "
  365. "stream of this audio interface",
  366. stream_name);
  367. return -1;
  368. }
  369. }
  370. }
  371. format = (sample_width == 4) ? 0 : NUMFORMATS - 1;
  372. while (1) {
  373. if ((err = snd_pcm_hw_params_set_format (
  374. handle, hw_params, formats[format].format)) < 0) {
  375. if ((sample_width == 4
  376. ? format++ >= (int)NUMFORMATS - 1
  377. : format-- <= 0)) {
  378. jack_error ("Sorry. The audio interface \"%s\""
  379. " doesn't support any of the"
  380. " hardware sample formats that"
  381. " JACK's alsa-driver can use.",
  382. device_name);
  383. return -1;
  384. }
  385. } else {
  386. if (formats[format].swapped) {
  387. driver->quirk_bswap = 1;
  388. } else {
  389. driver->quirk_bswap = 0;
  390. }
  391. jack_info ("ALSA: final selected sample format for %s: %s", stream_name, formats[format].Name);
  392. break;
  393. }
  394. }
  395. frame_rate = driver->frame_rate ;
  396. err = snd_pcm_hw_params_set_rate_near (handle, hw_params,
  397. &frame_rate, NULL) ;
  398. driver->frame_rate = frame_rate ;
  399. if (err < 0) {
  400. jack_error ("ALSA: cannot set sample/frame rate to %ld for %s", driver->frame_rate, stream_name);
  401. return -1;
  402. }
  403. if (!*nchns) {
  404. /*if not user-specified, try to find the maximum
  405. * number of channels */
  406. unsigned int channels_max ;
  407. err = snd_pcm_hw_params_get_channels_max (hw_params,
  408. &channels_max);
  409. *nchns = channels_max ;
  410. if (*nchns > 1024) {
  411. /* the hapless user is an unwitting victim of
  412. the "default" ALSA PCM device, which can
  413. support up to 16 million channels. since
  414. they can't be bothered to set up a proper
  415. default device, limit the number of
  416. channels for them to a sane default.
  417. */
  418. jack_error (
  419. "You appear to be using the ALSA software \"plug\" layer, probably\n"
  420. "a result of using the \"default\" ALSA device. This is less\n"
  421. "efficient than it could be. Consider using a hardware device\n"
  422. "instead rather than using the plug layer. Usually the name of the\n"
  423. "hardware device that corresponds to the first sound card is hw:0\n"
  424. );
  425. *nchns = 2;
  426. }
  427. }
  428. if ((err = snd_pcm_hw_params_set_channels (handle, hw_params,
  429. *nchns)) < 0) {
  430. jack_error ("ALSA: cannot set channel count to %u for %s",
  431. *nchns, stream_name);
  432. return -1;
  433. }
  434. if ((err = snd_pcm_hw_params_set_period_size (handle, hw_params,
  435. driver->frames_per_cycle,
  436. 0))
  437. < 0) {
  438. jack_error ("ALSA: cannot set period size to %ld frames for %s", driver->frames_per_cycle, stream_name);
  439. return -1;
  440. }
  441. *nperiodsp = driver->user_nperiods;
  442. snd_pcm_hw_params_set_periods_min (handle, hw_params, nperiodsp, NULL);
  443. if (*nperiodsp < driver->user_nperiods)
  444. *nperiodsp = driver->user_nperiods;
  445. if (snd_pcm_hw_params_set_periods_near (handle, hw_params,
  446. nperiodsp, NULL) < 0) {
  447. jack_error ("ALSA: cannot set number of periods to %u for %s",
  448. *nperiodsp, stream_name);
  449. return -1;
  450. }
  451. if (*nperiodsp < driver->user_nperiods) {
  452. jack_error ("ALSA: got smaller periods %u than %u for %s",
  453. *nperiodsp, (unsigned int) driver->user_nperiods,
  454. stream_name);
  455. return -1;
  456. }
  457. jack_info ("ALSA: use %d periods for %s", *nperiodsp, stream_name);
  458. /*
  459. if (!jack_power_of_two(driver->frames_per_cycle)) {
  460. jack_error("JACK: frames must be a power of two "
  461. "(64, 512, 1024, ...)\n");
  462. return -1;
  463. }
  464. */
  465. if ((err = snd_pcm_hw_params_set_buffer_size (handle, hw_params,
  466. *nperiodsp *
  467. driver->frames_per_cycle))
  468. < 0) {
  469. jack_error ("ALSA: cannot set buffer length to %ld for %s", *nperiodsp * driver->frames_per_cycle,
  470. stream_name);
  471. return -1;
  472. }
  473. if ((err = snd_pcm_hw_params (handle, hw_params)) < 0) {
  474. jack_error ("ALSA: cannot set hardware parameters for %s",
  475. stream_name);
  476. return -1;
  477. }
  478. snd_pcm_sw_params_current (handle, sw_params);
  479. if ((err = snd_pcm_sw_params_set_start_threshold (handle, sw_params,
  480. 0U)) < 0) {
  481. jack_error ("ALSA: cannot set start mode for %s", stream_name);
  482. return -1;
  483. }
  484. stop_th = *nperiodsp * driver->frames_per_cycle;
  485. if (driver->soft_mode) {
  486. stop_th = (snd_pcm_uframes_t) - 1;
  487. }
  488. if ((err = snd_pcm_sw_params_set_stop_threshold (
  489. handle, sw_params, stop_th)) < 0) {
  490. jack_error ("ALSA: cannot set stop mode for %s",
  491. stream_name);
  492. return -1;
  493. }
  494. if ((err = snd_pcm_sw_params_set_silence_threshold (
  495. handle, sw_params, 0)) < 0) {
  496. jack_error ("ALSA: cannot set silence threshold for %s",
  497. stream_name);
  498. return -1;
  499. }
  500. #if 0
  501. fprintf (stderr, "set silence size to %lu * %lu = %lu\n",
  502. driver->frames_per_cycle, *nperiodsp,
  503. driver->frames_per_cycle * *nperiodsp);
  504. if ((err = snd_pcm_sw_params_set_silence_size (
  505. handle, sw_params,
  506. driver->frames_per_cycle * *nperiodsp)) < 0) {
  507. jack_error ("ALSA: cannot set silence size for %s",
  508. stream_name);
  509. return -1;
  510. }
  511. #endif
  512. if (handle == driver->playback_handle)
  513. err = snd_pcm_sw_params_set_avail_min (
  514. handle, sw_params,
  515. driver->frames_per_cycle
  516. * (*nperiodsp - driver->user_nperiods + 1));
  517. else
  518. err = snd_pcm_sw_params_set_avail_min (
  519. handle, sw_params, driver->frames_per_cycle);
  520. if (err < 0) {
  521. jack_error ("ALSA: cannot set avail min for %s", stream_name);
  522. return -1;
  523. }
  524. if ((err = snd_pcm_sw_params (handle, sw_params)) < 0) {
  525. jack_error ("ALSA: cannot set software parameters for %s\n",
  526. stream_name);
  527. return -1;
  528. }
  529. return 0;
  530. }
  531. int
  532. JackAlsaDriver::alsa_driver_set_parameters (alsa_driver_t *driver,
  533. jack_nframes_t frames_per_cycle,
  534. jack_nframes_t user_nperiods,
  535. jack_nframes_t rate)
  536. {
  537. int dir;
  538. snd_pcm_uframes_t p_period_size = 0;
  539. snd_pcm_uframes_t c_period_size = 0;
  540. channel_t chn;
  541. unsigned int pr = 0;
  542. unsigned int cr = 0;
  543. int err;
  544. driver->frame_rate = rate;
  545. driver->frames_per_cycle = frames_per_cycle;
  546. driver->user_nperiods = user_nperiods;
  547. /* // steph
  548. fprintf (stderr, "configuring for %" PRIu32 "Hz, period = %"
  549. PRIu32 " frames, buffer = %" PRIu32 " periods\n",
  550. rate, frames_per_cycle, user_nperiods);
  551. */
  552. if (driver->capture_handle) {
  553. if (alsa_driver_configure_stream (
  554. driver,
  555. driver->alsa_name_capture,
  556. "capture",
  557. driver->capture_handle,
  558. driver->capture_hw_params,
  559. driver->capture_sw_params,
  560. &driver->capture_nperiods,
  561. (long unsigned int*)&driver->capture_nchannels,
  562. driver->capture_sample_bytes)) {
  563. jack_error ("ALSA: cannot configure capture channel");
  564. return -1;
  565. }
  566. }
  567. if (driver->playback_handle) {
  568. if (alsa_driver_configure_stream (
  569. driver,
  570. driver->alsa_name_playback,
  571. "playback",
  572. driver->playback_handle,
  573. driver->playback_hw_params,
  574. driver->playback_sw_params,
  575. &driver->playback_nperiods,
  576. (long unsigned int*)&driver->playback_nchannels,
  577. driver->playback_sample_bytes)) {
  578. jack_error ("ALSA: cannot configure playback channel");
  579. return -1;
  580. }
  581. }
  582. /* check the rate, since thats rather important */
  583. if (driver->playback_handle) {
  584. snd_pcm_hw_params_get_rate (driver->playback_hw_params,
  585. &pr, &dir);
  586. }
  587. if (driver->capture_handle) {
  588. snd_pcm_hw_params_get_rate (driver->capture_hw_params,
  589. &cr, &dir);
  590. }
  591. if (driver->capture_handle && driver->playback_handle) {
  592. if (cr != pr) {
  593. jack_error ("playback and capture sample rates do "
  594. "not match (%d vs. %d)", pr, cr);
  595. }
  596. /* only change if *both* capture and playback rates
  597. * don't match requested certain hardware actually
  598. * still works properly in full-duplex with slightly
  599. * different rate values between adc and dac
  600. */
  601. if (cr != driver->frame_rate && pr != driver->frame_rate) {
  602. jack_error ("sample rate in use (%d Hz) does not "
  603. "match requested rate (%d Hz)",
  604. cr, driver->frame_rate);
  605. driver->frame_rate = cr;
  606. }
  607. } else if (driver->capture_handle && cr != driver->frame_rate) {
  608. jack_error ("capture sample rate in use (%d Hz) does not "
  609. "match requested rate (%d Hz)",
  610. cr, driver->frame_rate);
  611. driver->frame_rate = cr;
  612. } else if (driver->playback_handle && pr != driver->frame_rate) {
  613. jack_error ("playback sample rate in use (%d Hz) does not "
  614. "match requested rate (%d Hz)",
  615. pr, driver->frame_rate);
  616. driver->frame_rate = pr;
  617. }
  618. /* check the fragment size, since thats non-negotiable */
  619. if (driver->playback_handle) {
  620. snd_pcm_access_t access;
  621. err = snd_pcm_hw_params_get_period_size (
  622. driver->playback_hw_params, &p_period_size, &dir);
  623. err = snd_pcm_hw_params_get_format (
  624. driver->playback_hw_params,
  625. &(driver->playback_sample_format));
  626. err = snd_pcm_hw_params_get_access (driver->playback_hw_params,
  627. &access);
  628. driver->playback_interleaved =
  629. (access == SND_PCM_ACCESS_MMAP_INTERLEAVED)
  630. || (access == SND_PCM_ACCESS_MMAP_COMPLEX);
  631. if (p_period_size != driver->frames_per_cycle) {
  632. /*
  633. jack_error ("alsa_pcm: requested an interrupt every %"
  634. PRIu32
  635. " frames but got %u frames for playback",
  636. driver->frames_per_cycle, p_period_size);
  637. */
  638. return -1;
  639. }
  640. }
  641. if (driver->capture_handle) {
  642. snd_pcm_access_t access;
  643. err = snd_pcm_hw_params_get_period_size (
  644. driver->capture_hw_params, &c_period_size, &dir);
  645. err = snd_pcm_hw_params_get_format (
  646. driver->capture_hw_params,
  647. &(driver->capture_sample_format));
  648. err = snd_pcm_hw_params_get_access (driver->capture_hw_params,
  649. &access);
  650. driver->capture_interleaved =
  651. (access == SND_PCM_ACCESS_MMAP_INTERLEAVED)
  652. || (access == SND_PCM_ACCESS_MMAP_COMPLEX);
  653. if (c_period_size != driver->frames_per_cycle) {
  654. /* // steph
  655. jack_error ("alsa_pcm: requested an interrupt every %"
  656. PRIu32
  657. " frames but got %uc frames for capture",
  658. driver->frames_per_cycle, p_period_size);
  659. */
  660. return -1;
  661. }
  662. }
  663. driver->playback_sample_bytes =
  664. snd_pcm_format_physical_width (driver->playback_sample_format)
  665. / 8;
  666. driver->capture_sample_bytes =
  667. snd_pcm_format_physical_width (driver->capture_sample_format)
  668. / 8;
  669. if (driver->playback_handle) {
  670. switch (driver->playback_sample_format) {
  671. case SND_PCM_FORMAT_S32_LE:
  672. case SND_PCM_FORMAT_S24_3LE:
  673. case SND_PCM_FORMAT_S24_3BE:
  674. case SND_PCM_FORMAT_S16_LE:
  675. case SND_PCM_FORMAT_S32_BE:
  676. case SND_PCM_FORMAT_S16_BE:
  677. break;
  678. default:
  679. jack_error ("programming error: unhandled format "
  680. "type for playback");
  681. exit (1);
  682. }
  683. }
  684. if (driver->capture_handle) {
  685. switch (driver->capture_sample_format) {
  686. case SND_PCM_FORMAT_S32_LE:
  687. case SND_PCM_FORMAT_S24_3LE:
  688. case SND_PCM_FORMAT_S24_3BE:
  689. case SND_PCM_FORMAT_S16_LE:
  690. case SND_PCM_FORMAT_S32_BE:
  691. case SND_PCM_FORMAT_S16_BE:
  692. break;
  693. default:
  694. jack_error ("programming error: unhandled format "
  695. "type for capture");
  696. exit (1);
  697. }
  698. }
  699. if (driver->playback_interleaved) {
  700. const snd_pcm_channel_area_t *my_areas;
  701. snd_pcm_uframes_t offset, frames;
  702. if (snd_pcm_mmap_begin(driver->playback_handle,
  703. &my_areas, &offset, &frames) < 0) {
  704. jack_error ("ALSA: %s: mmap areas info error",
  705. driver->alsa_name_playback);
  706. return -1;
  707. }
  708. driver->interleave_unit =
  709. snd_pcm_format_physical_width (
  710. driver->playback_sample_format) / 8;
  711. } else {
  712. driver->interleave_unit = 0; /* NOT USED */
  713. }
  714. if (driver->capture_interleaved) {
  715. const snd_pcm_channel_area_t *my_areas;
  716. snd_pcm_uframes_t offset, frames;
  717. if (snd_pcm_mmap_begin(driver->capture_handle,
  718. &my_areas, &offset, &frames) < 0) {
  719. jack_error ("ALSA: %s: mmap areas info error",
  720. driver->alsa_name_capture);
  721. return -1;
  722. }
  723. }
  724. if (driver->playback_nchannels > driver->capture_nchannels) {
  725. driver->max_nchannels = driver->playback_nchannels;
  726. driver->user_nchannels = driver->capture_nchannels;
  727. } else {
  728. driver->max_nchannels = driver->capture_nchannels;
  729. driver->user_nchannels = driver->playback_nchannels;
  730. }
  731. alsa_driver_setup_io_function_pointers (driver);
  732. /* Allocate and initialize structures that rely on the
  733. channels counts.
  734. Set up the bit pattern that is used to record which
  735. channels require action on every cycle. any bits that are
  736. not set after the engine's process() call indicate channels
  737. that potentially need to be silenced.
  738. */
  739. bitset_create (&driver->channels_done, driver->max_nchannels);
  740. bitset_create (&driver->channels_not_done, driver->max_nchannels);
  741. if (driver->playback_handle) {
  742. driver->playback_addr = (char **)
  743. malloc (sizeof (char *) * driver->playback_nchannels);
  744. memset (driver->playback_addr, 0,
  745. sizeof (char *) * driver->playback_nchannels);
  746. driver->playback_interleave_skip = (unsigned long *)
  747. malloc (sizeof (unsigned long *) * driver->playback_nchannels);
  748. memset (driver->playback_interleave_skip, 0,
  749. sizeof (unsigned long *) * driver->playback_nchannels);
  750. driver->silent = (unsigned long *)
  751. malloc (sizeof (unsigned long)
  752. * driver->playback_nchannels);
  753. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  754. driver->silent[chn] = 0;
  755. }
  756. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  757. bitset_add (driver->channels_done, chn);
  758. }
  759. driver->dither_state = (dither_state_t *)
  760. calloc ( driver->playback_nchannels,
  761. sizeof (dither_state_t));
  762. }
  763. if (driver->capture_handle) {
  764. driver->capture_addr = (char **)
  765. malloc (sizeof (char *) * driver->capture_nchannels);
  766. memset (driver->capture_addr, 0,
  767. sizeof (char *) * driver->capture_nchannels);
  768. driver->capture_interleave_skip = (unsigned long *)
  769. malloc (sizeof (unsigned long *) * driver->capture_nchannels);
  770. memset (driver->capture_interleave_skip, 0,
  771. sizeof (unsigned long *) * driver->capture_nchannels);
  772. }
  773. driver->clock_sync_data = (ClockSyncStatus *)
  774. malloc (sizeof (ClockSyncStatus) * driver->max_nchannels);
  775. driver->period_usecs =
  776. (jack_time_t) floor ((((float) driver->frames_per_cycle) /
  777. driver->frame_rate) * 1000000.0f);
  778. driver->poll_timeout = (int) floor (1.5f * driver->period_usecs);
  779. // steph
  780. /*
  781. if (driver->engine) {
  782. driver->engine->set_buffer_size (driver->engine,
  783. driver->frames_per_cycle);
  784. }
  785. */
  786. return 0;
  787. }
  788. int
  789. JackAlsaDriver::alsa_driver_reset_parameters (alsa_driver_t *driver,
  790. jack_nframes_t frames_per_cycle,
  791. jack_nframes_t user_nperiods,
  792. jack_nframes_t rate)
  793. {
  794. /* XXX unregister old ports ? */
  795. alsa_driver_release_channel_dependent_memory (driver);
  796. return alsa_driver_set_parameters (driver,
  797. frames_per_cycle,
  798. user_nperiods, rate);
  799. }
  800. int
  801. JackAlsaDriver::alsa_driver_get_channel_addresses (alsa_driver_t *driver,
  802. snd_pcm_uframes_t *capture_avail,
  803. snd_pcm_uframes_t *playback_avail,
  804. snd_pcm_uframes_t *capture_offset,
  805. snd_pcm_uframes_t *playback_offset)
  806. {
  807. unsigned long err;
  808. channel_t chn;
  809. if (capture_avail) {
  810. if ((err = snd_pcm_mmap_begin (
  811. driver->capture_handle, &driver->capture_areas,
  812. (snd_pcm_uframes_t *) capture_offset,
  813. (snd_pcm_uframes_t *) capture_avail)) < 0) {
  814. jack_error ("ALSA: %s: mmap areas info error",
  815. driver->alsa_name_capture);
  816. return -1;
  817. }
  818. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  819. const snd_pcm_channel_area_t *a =
  820. &driver->capture_areas[chn];
  821. driver->capture_addr[chn] = (char *) a->addr
  822. + ((a->first + a->step * *capture_offset) / 8);
  823. driver->capture_interleave_skip[chn] = (unsigned long ) (a->step / 8);
  824. }
  825. }
  826. if (playback_avail) {
  827. if ((err = snd_pcm_mmap_begin (
  828. driver->playback_handle, &driver->playback_areas,
  829. (snd_pcm_uframes_t *) playback_offset,
  830. (snd_pcm_uframes_t *) playback_avail)) < 0) {
  831. jack_error ("ALSA: %s: mmap areas info error ",
  832. driver->alsa_name_playback);
  833. return -1;
  834. }
  835. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  836. const snd_pcm_channel_area_t *a =
  837. &driver->playback_areas[chn];
  838. driver->playback_addr[chn] = (char *) a->addr
  839. + ((a->first + a->step * *playback_offset) / 8);
  840. driver->playback_interleave_skip[chn] = (unsigned long ) (a->step / 8);
  841. }
  842. }
  843. return 0;
  844. }
  845. int
  846. JackAlsaDriver::alsa_driver_start (alsa_driver_t *driver)
  847. {
  848. int err;
  849. snd_pcm_uframes_t poffset, pavail;
  850. channel_t chn;
  851. driver->poll_last = 0;
  852. driver->poll_next = 0;
  853. if (driver->playback_handle) {
  854. if ((err = snd_pcm_prepare (driver->playback_handle)) < 0) {
  855. jack_error ("ALSA: prepare error for playback on "
  856. "\"%s\" (%s)", driver->alsa_name_playback,
  857. snd_strerror(err));
  858. return -1;
  859. }
  860. }
  861. if ((driver->capture_handle && driver->capture_and_playback_not_synced)
  862. || !driver->playback_handle) {
  863. if ((err = snd_pcm_prepare (driver->capture_handle)) < 0) {
  864. jack_error ("ALSA: prepare error for capture on \"%s\""
  865. " (%s)", driver->alsa_name_capture,
  866. snd_strerror(err));
  867. return -1;
  868. }
  869. }
  870. if (driver->hw_monitoring) {
  871. if (driver->input_monitor_mask || driver->all_monitor_in) {
  872. if (driver->all_monitor_in) {
  873. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  874. } else {
  875. driver->hw->set_input_monitor_mask (
  876. driver->hw, driver->input_monitor_mask);
  877. }
  878. } else {
  879. driver->hw->set_input_monitor_mask (driver->hw,
  880. driver->input_monitor_mask);
  881. }
  882. }
  883. if (driver->playback_handle) {
  884. driver->playback_nfds =
  885. snd_pcm_poll_descriptors_count (driver->playback_handle);
  886. } else {
  887. driver->playback_nfds = 0;
  888. }
  889. if (driver->capture_handle) {
  890. driver->capture_nfds =
  891. snd_pcm_poll_descriptors_count (driver->capture_handle);
  892. } else {
  893. driver->capture_nfds = 0;
  894. }
  895. if (driver->pfd) {
  896. free (driver->pfd);
  897. }
  898. driver->pfd = (struct pollfd *)
  899. malloc (sizeof (struct pollfd) *
  900. (driver->playback_nfds + driver->capture_nfds + 2));
  901. if (driver->midi && !driver->xrun_recovery)
  902. (driver->midi->start)(driver->midi);
  903. if (driver->playback_handle) {
  904. /* fill playback buffer with zeroes, and mark
  905. all fragments as having data.
  906. */
  907. pavail = snd_pcm_avail_update (driver->playback_handle);
  908. if (pavail !=
  909. driver->frames_per_cycle * driver->playback_nperiods) {
  910. jack_error ("ALSA: full buffer not available at start");
  911. return -1;
  912. }
  913. if (alsa_driver_get_channel_addresses (driver,
  914. 0, &pavail, 0, &poffset)) {
  915. return -1;
  916. }
  917. /* XXX this is cheating. ALSA offers no guarantee that
  918. we can access the entire buffer at any one time. It
  919. works on most hardware tested so far, however, buts
  920. its a liability in the long run. I think that
  921. alsa-lib may have a better function for doing this
  922. here, where the goal is to silence the entire
  923. buffer.
  924. */
  925. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  926. alsa_driver_silence_on_channel (
  927. driver, chn,
  928. driver->user_nperiods
  929. * driver->frames_per_cycle);
  930. }
  931. snd_pcm_mmap_commit (driver->playback_handle, poffset,
  932. driver->user_nperiods
  933. * driver->frames_per_cycle);
  934. if ((err = snd_pcm_start (driver->playback_handle)) < 0) {
  935. jack_error ("ALSA: could not start playback (%s)",
  936. snd_strerror (err));
  937. return -1;
  938. }
  939. }
  940. if ((driver->capture_handle && driver->capture_and_playback_not_synced)
  941. || !driver->playback_handle) {
  942. if ((err = snd_pcm_start (driver->capture_handle)) < 0) {
  943. jack_error ("ALSA: could not start capture (%s)",
  944. snd_strerror (err));
  945. return -1;
  946. }
  947. }
  948. return 0;
  949. }
  950. int
  951. JackAlsaDriver::alsa_driver_stop (alsa_driver_t *driver)
  952. {
  953. int err;
  954. //JSList* node;
  955. //int chn;
  956. /* silence all capture port buffers, because we might
  957. be entering offline mode.
  958. */
  959. // steph
  960. /*
  961. for (chn = 0, node = driver->capture_ports; node;
  962. node = jack_slist_next (node), chn++) {
  963. jack_port_t* port;
  964. char* buf;
  965. jack_nframes_t nframes = driver->engine->control->buffer_size;
  966. port = (jack_port_t *) node->data;
  967. buf = jack_port_get_buffer (port, nframes);
  968. memset (buf, 0, sizeof (jack_default_audio_sample_t) * nframes);
  969. }
  970. */
  971. for (int i = 0; i < fPlaybackChannels; i++) {
  972. jack_default_audio_sample_t* buf =
  973. (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[i], fEngineControl->fBufferSize);
  974. memset (buf, 0, sizeof (jack_default_audio_sample_t) * fEngineControl->fBufferSize);
  975. }
  976. if (driver->playback_handle) {
  977. if ((err = snd_pcm_drop (driver->playback_handle)) < 0) {
  978. jack_error ("ALSA: channel flush for playback "
  979. "failed (%s)", snd_strerror (err));
  980. return -1;
  981. }
  982. }
  983. if (!driver->playback_handle
  984. || driver->capture_and_playback_not_synced) {
  985. if (driver->capture_handle) {
  986. if ((err = snd_pcm_drop (driver->capture_handle)) < 0) {
  987. jack_error ("ALSA: channel flush for "
  988. "capture failed (%s)",
  989. snd_strerror (err));
  990. return -1;
  991. }
  992. }
  993. }
  994. if (driver->hw_monitoring) {
  995. driver->hw->set_input_monitor_mask (driver->hw, 0);
  996. }
  997. if (driver->midi && !driver->xrun_recovery)
  998. (driver->midi->stop)(driver->midi);
  999. return 0;
  1000. }
  1001. int
  1002. JackAlsaDriver::alsa_driver_restart (alsa_driver_t *driver)
  1003. {
  1004. // steph
  1005. /*
  1006. if (driver->nt_stop((struct _jack_driver_nt *) driver))
  1007. return -1;
  1008. return driver->nt_start((struct _jack_driver_nt *) driver);
  1009. */
  1010. driver->xrun_recovery = 1;
  1011. int res = Stop();
  1012. if (!res)
  1013. res = Start();
  1014. driver->xrun_recovery = 0;
  1015. return res;
  1016. }
  1017. int
  1018. JackAlsaDriver::alsa_driver_xrun_recovery (alsa_driver_t *driver, float *delayed_usecs)
  1019. {
  1020. snd_pcm_status_t *status;
  1021. int res;
  1022. jack_error("alsa_driver_xrun_recovery");
  1023. snd_pcm_status_alloca(&status);
  1024. if (driver->capture_handle) {
  1025. if ((res = snd_pcm_status(driver->capture_handle, status))
  1026. < 0) {
  1027. jack_error("status error: %s", snd_strerror(res));
  1028. }
  1029. } else {
  1030. if ((res = snd_pcm_status(driver->playback_handle, status))
  1031. < 0) {
  1032. jack_error("status error: %s", snd_strerror(res));
  1033. }
  1034. }
  1035. if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN
  1036. && driver->process_count > XRUN_REPORT_DELAY) {
  1037. struct timeval now, diff, tstamp;
  1038. driver->xrun_count++;
  1039. snd_pcm_status_get_tstamp(status,&now);
  1040. snd_pcm_status_get_trigger_tstamp(status, &tstamp);
  1041. timersub(&now, &tstamp, &diff);
  1042. *delayed_usecs = diff.tv_sec * 1000000.0 + diff.tv_usec;
  1043. // steph: TODO use a RT version
  1044. jack_error("\n\n**** alsa_pcm: xrun of at least %.3f msecs\n\n", *delayed_usecs / 1000.0);
  1045. }
  1046. if (alsa_driver_restart (driver)) {
  1047. return -1;
  1048. }
  1049. return 0;
  1050. }
  1051. void
  1052. JackAlsaDriver::alsa_driver_silence_untouched_channels (alsa_driver_t *driver,
  1053. jack_nframes_t nframes)
  1054. {
  1055. channel_t chn;
  1056. jack_nframes_t buffer_frames =
  1057. driver->frames_per_cycle * driver->playback_nperiods;
  1058. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  1059. if (bitset_contains (driver->channels_not_done, chn)) {
  1060. if (driver->silent[chn] < buffer_frames) {
  1061. alsa_driver_silence_on_channel_no_mark (
  1062. driver, chn, nframes);
  1063. driver->silent[chn] += nframes;
  1064. }
  1065. }
  1066. }
  1067. }
  1068. jack_nframes_t
  1069. JackAlsaDriver::alsa_driver_wait (alsa_driver_t *driver, int extra_fd, int *status, float
  1070. *delayed_usecs)
  1071. {
  1072. snd_pcm_sframes_t avail = 0;
  1073. snd_pcm_sframes_t capture_avail = 0;
  1074. snd_pcm_sframes_t playback_avail = 0;
  1075. int xrun_detected = FALSE;
  1076. int need_capture;
  1077. int need_playback;
  1078. unsigned int i;
  1079. jack_time_t poll_enter;
  1080. jack_time_t poll_ret = 0;
  1081. *status = -1;
  1082. *delayed_usecs = 0;
  1083. need_capture = driver->capture_handle ? 1 : 0;
  1084. if (extra_fd >= 0) {
  1085. need_playback = 0;
  1086. } else {
  1087. need_playback = driver->playback_handle ? 1 : 0;
  1088. }
  1089. // steph
  1090. // again:
  1091. while (need_playback || need_capture) {
  1092. unsigned int p_timed_out, c_timed_out;
  1093. unsigned int ci = 0;
  1094. unsigned int nfds;
  1095. nfds = 0;
  1096. if (need_playback) {
  1097. snd_pcm_poll_descriptors (driver->playback_handle,
  1098. &driver->pfd[0],
  1099. driver->playback_nfds);
  1100. nfds += driver->playback_nfds;
  1101. }
  1102. if (need_capture) {
  1103. snd_pcm_poll_descriptors (driver->capture_handle,
  1104. &driver->pfd[nfds],
  1105. driver->capture_nfds);
  1106. ci = nfds;
  1107. nfds += driver->capture_nfds;
  1108. }
  1109. /* ALSA doesn't set POLLERR in some versions of 0.9.X */
  1110. for (i = 0; i < nfds; i++) {
  1111. driver->pfd[i].events |= POLLERR;
  1112. }
  1113. if (extra_fd >= 0) {
  1114. driver->pfd[nfds].fd = extra_fd;
  1115. driver->pfd[nfds].events =
  1116. POLLIN | POLLERR | POLLHUP | POLLNVAL;
  1117. nfds++;
  1118. }
  1119. poll_enter = jack_get_microseconds ();
  1120. if (poll_enter > driver->poll_next) {
  1121. /*
  1122. * This processing cycle was delayed past the
  1123. * next due interrupt! Do not account this as
  1124. * a wakeup delay:
  1125. */
  1126. driver->poll_next = 0;
  1127. driver->poll_late++;
  1128. }
  1129. if (poll (driver->pfd, nfds, driver->poll_timeout) < 0) {
  1130. if (errno == EINTR) {
  1131. jack_log("poll interrupt");
  1132. // this happens mostly when run
  1133. // under gdb, or when exiting due to a signal
  1134. // steph
  1135. /*
  1136. if (under_gdb) {
  1137. goto again;
  1138. }
  1139. */
  1140. *status = -2;
  1141. return 0;
  1142. }
  1143. jack_error ("ALSA: poll call failed (%s)",
  1144. strerror (errno));
  1145. *status = -3;
  1146. return 0;
  1147. }
  1148. poll_ret = jack_get_microseconds ();
  1149. // steph
  1150. fBeginDateUst = poll_ret;
  1151. if (extra_fd < 0) {
  1152. if (driver->poll_next && poll_ret > driver->poll_next) {
  1153. *delayed_usecs = poll_ret - driver->poll_next;
  1154. }
  1155. driver->poll_last = poll_ret;
  1156. driver->poll_next = poll_ret + driver->period_usecs;
  1157. // steph
  1158. /*
  1159. driver->engine->transport_cycle_start (driver->engine,
  1160. poll_ret);
  1161. */
  1162. }
  1163. #ifdef DEBUG_WAKEUP
  1164. fprintf (stderr, "%" PRIu64 ": checked %d fds, %" PRIu64
  1165. " usecs since poll entered\n", poll_ret, nfds,
  1166. poll_ret - poll_enter);
  1167. #endif
  1168. /* check to see if it was the extra FD that caused us
  1169. * to return from poll */
  1170. if (extra_fd >= 0) {
  1171. if (driver->pfd[nfds - 1].revents == 0) {
  1172. /* we timed out on the extra fd */
  1173. *status = -4;
  1174. // steph (cannot return negative value....)
  1175. // return -1;
  1176. return 0;
  1177. }
  1178. /* if POLLIN was the only bit set, we're OK */
  1179. *status = 0;
  1180. return (driver->pfd[nfds - 1].revents == POLLIN) ? 0 : -1;
  1181. }
  1182. p_timed_out = 0;
  1183. if (need_playback) {
  1184. for (i = 0; i < driver->playback_nfds; i++) {
  1185. if (driver->pfd[i].revents & POLLERR) {
  1186. xrun_detected = TRUE;
  1187. }
  1188. if (driver->pfd[i].revents == 0) {
  1189. p_timed_out++;
  1190. #ifdef DEBUG_WAKEUP
  1191. fprintf (stderr, "%" PRIu64
  1192. " playback stream timed out\n",
  1193. poll_ret);
  1194. #endif
  1195. }
  1196. }
  1197. if (p_timed_out == 0) {
  1198. need_playback = 0;
  1199. #ifdef DEBUG_WAKEUP
  1200. fprintf (stderr, "%" PRIu64
  1201. " playback stream ready\n",
  1202. poll_ret);
  1203. #endif
  1204. }
  1205. }
  1206. c_timed_out = 0;
  1207. if (need_capture) {
  1208. for (i = ci; i < nfds; i++) {
  1209. if (driver->pfd[i].revents & POLLERR) {
  1210. xrun_detected = TRUE;
  1211. }
  1212. if (driver->pfd[i].revents == 0) {
  1213. c_timed_out++;
  1214. #ifdef DEBUG_WAKEUP
  1215. fprintf (stderr, "%" PRIu64
  1216. " capture stream timed out\n",
  1217. poll_ret);
  1218. #endif
  1219. }
  1220. }
  1221. if (c_timed_out == 0) {
  1222. need_capture = 0;
  1223. #ifdef DEBUG_WAKEUP
  1224. fprintf (stderr, "%" PRIu64
  1225. " capture stream ready\n",
  1226. poll_ret);
  1227. #endif
  1228. }
  1229. }
  1230. if ((p_timed_out && (p_timed_out == driver->playback_nfds)) &&
  1231. (c_timed_out && (c_timed_out == driver->capture_nfds))) {
  1232. // steph
  1233. /*
  1234. jack_error ("ALSA: poll time out, polled for %" PRIu64
  1235. " usecs",
  1236. poll_ret - poll_enter);
  1237. */
  1238. *status = -5;
  1239. return 0;
  1240. }
  1241. }
  1242. if (driver->capture_handle) {
  1243. if ((capture_avail = snd_pcm_avail_update (
  1244. driver->capture_handle)) < 0) {
  1245. if (capture_avail == -EPIPE) {
  1246. xrun_detected = TRUE;
  1247. } else {
  1248. jack_error ("unknown ALSA avail_update return"
  1249. " value (%u)", capture_avail);
  1250. }
  1251. }
  1252. } else {
  1253. /* odd, but see min() computation below */
  1254. capture_avail = INT_MAX;
  1255. }
  1256. if (driver->playback_handle) {
  1257. if ((playback_avail = snd_pcm_avail_update (
  1258. driver->playback_handle)) < 0) {
  1259. if (playback_avail == -EPIPE) {
  1260. xrun_detected = TRUE;
  1261. } else {
  1262. jack_error ("unknown ALSA avail_update return"
  1263. " value (%u)", playback_avail);
  1264. }
  1265. }
  1266. } else {
  1267. /* odd, but see min() computation below */
  1268. playback_avail = INT_MAX;
  1269. }
  1270. if (xrun_detected) {
  1271. *status = alsa_driver_xrun_recovery (driver, delayed_usecs);
  1272. return 0;
  1273. }
  1274. *status = 0;
  1275. driver->last_wait_ust = poll_ret;
  1276. avail = capture_avail < playback_avail ? capture_avail : playback_avail;
  1277. #ifdef DEBUG_WAKEUP
  1278. fprintf (stderr, "wakeup complete, avail = %lu, pavail = %lu "
  1279. "cavail = %lu\n",
  1280. avail, playback_avail, capture_avail);
  1281. #endif
  1282. /* mark all channels not done for now. read/write will change this */
  1283. bitset_copy (driver->channels_not_done, driver->channels_done);
  1284. /* constrain the available count to the nearest (round down) number of
  1285. periods.
  1286. */
  1287. return avail - (avail % driver->frames_per_cycle);
  1288. }
  1289. int JackAlsaDriver::SetBufferSize(jack_nframes_t buffer_size)
  1290. {
  1291. jack_log("JackAlsaDriver::SetBufferSize %ld", buffer_size);
  1292. int res = alsa_driver_reset_parameters((alsa_driver_t *)fDriver, buffer_size,
  1293. ((alsa_driver_t *)fDriver)->user_nperiods,
  1294. ((alsa_driver_t *)fDriver)->frame_rate);
  1295. if (res == 0) { // update fEngineControl and fGraphManager
  1296. JackAudioDriver::SetBufferSize(buffer_size); // never fails
  1297. } else {
  1298. alsa_driver_reset_parameters((alsa_driver_t *)fDriver, fEngineControl->fBufferSize,
  1299. ((alsa_driver_t *)fDriver)->user_nperiods,
  1300. ((alsa_driver_t *)fDriver)->frame_rate);
  1301. }
  1302. return res;
  1303. }
  1304. int
  1305. JackAlsaDriver::alsa_driver_read (alsa_driver_t *driver, jack_nframes_t nframes)
  1306. {
  1307. snd_pcm_sframes_t contiguous;
  1308. snd_pcm_sframes_t nread;
  1309. snd_pcm_sframes_t offset;
  1310. jack_nframes_t orig_nframes;
  1311. jack_default_audio_sample_t* buf;
  1312. //channel_t chn;
  1313. //JSList *node;
  1314. //jack_port_t* port;
  1315. int err;
  1316. // steph
  1317. /*
  1318. if (!driver->capture_handle || driver->engine->freewheeling) {
  1319. return 0;
  1320. }
  1321. */
  1322. if (!driver->capture_handle) {
  1323. return 0;
  1324. }
  1325. if (nframes > driver->frames_per_cycle) {
  1326. return -1;
  1327. }
  1328. if (driver->midi)
  1329. (driver->midi->read)(driver->midi, nframes);
  1330. nread = 0;
  1331. contiguous = 0;
  1332. orig_nframes = nframes;
  1333. while (nframes) {
  1334. contiguous = nframes;
  1335. if (alsa_driver_get_channel_addresses (
  1336. driver,
  1337. (snd_pcm_uframes_t *) &contiguous,
  1338. (snd_pcm_uframes_t *) 0,
  1339. (snd_pcm_uframes_t *)&offset, 0) < 0) {
  1340. return -1;
  1341. }
  1342. // steph
  1343. for (int i = 0; i < fCaptureChannels; i++) {
  1344. if (fGraphManager->GetConnectionsNum(fCapturePortList[i]) > 0) {
  1345. buf = (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[i], orig_nframes);
  1346. alsa_driver_read_from_channel (driver, i, buf + nread, contiguous);
  1347. }
  1348. }
  1349. /* // steph
  1350. for (chn = 0, node = driver->capture_ports; node;
  1351. node = jack_slist_next (node), chn++) {
  1352. port = (jack_port_t *) node->data;
  1353. if (!jack_port_connected (port)) {
  1354. // no-copy optimization
  1355. continue;
  1356. }
  1357. buf = jack_port_get_buffer (port, orig_nframes);
  1358. alsa_driver_read_from_channel (driver, chn,
  1359. buf + nread, contiguous);
  1360. }
  1361. */
  1362. if ((err = snd_pcm_mmap_commit (driver->capture_handle,
  1363. offset, contiguous)) < 0) {
  1364. // steph
  1365. // jack_error ("ALSA: could not complete read of %"
  1366. // PRIu32 " frames: error = %d\n", contiguous, err);
  1367. jack_error ("ALSA: could not complete read of %d frames: error = %d", contiguous, err);
  1368. return -1;
  1369. }
  1370. nframes -= contiguous;
  1371. nread += contiguous;
  1372. }
  1373. return 0;
  1374. }
  1375. int
  1376. JackAlsaDriver::alsa_driver_write (alsa_driver_t* driver, jack_nframes_t nframes)
  1377. {
  1378. //channel_t chn;
  1379. //JSList *node;
  1380. //JSList *mon_node;
  1381. jack_default_audio_sample_t* buf;
  1382. jack_default_audio_sample_t* monbuf;
  1383. jack_nframes_t orig_nframes;
  1384. snd_pcm_sframes_t nwritten;
  1385. snd_pcm_sframes_t contiguous;
  1386. snd_pcm_sframes_t offset;
  1387. JackPort* port;
  1388. //jack_port_t *port;
  1389. int err;
  1390. driver->process_count++;
  1391. // steph
  1392. /*
  1393. if (!driver->playback_handle || driver->engine->freewheeling) {
  1394. return 0;
  1395. }
  1396. */
  1397. if (!driver->playback_handle) {
  1398. return 0;
  1399. }
  1400. if (nframes > driver->frames_per_cycle) {
  1401. return -1;
  1402. }
  1403. if (driver->midi)
  1404. (driver->midi->write)(driver->midi, nframes);
  1405. nwritten = 0;
  1406. contiguous = 0;
  1407. orig_nframes = nframes;
  1408. /* check current input monitor request status */
  1409. driver->input_monitor_mask = 0;
  1410. // steph
  1411. /*
  1412. for (chn = 0, node = driver->capture_ports; node;
  1413. node = jack_slist_next (node), chn++) {
  1414. if (((jack_port_t *) node->data)->shared->monitor_requests) {
  1415. driver->input_monitor_mask |= (1<<chn);
  1416. }
  1417. }
  1418. */
  1419. for (int i = 0; i < fCaptureChannels; i++) {
  1420. port = fGraphManager->GetPort(fCapturePortList[i]);
  1421. if (port->MonitoringInput()) {
  1422. driver->input_monitor_mask |= (1 << i);
  1423. }
  1424. }
  1425. if (driver->hw_monitoring) {
  1426. if ((driver->hw->input_monitor_mask
  1427. != driver->input_monitor_mask)
  1428. && !driver->all_monitor_in) {
  1429. driver->hw->set_input_monitor_mask (
  1430. driver->hw, driver->input_monitor_mask);
  1431. }
  1432. }
  1433. while (nframes) {
  1434. contiguous = nframes;
  1435. if (alsa_driver_get_channel_addresses (
  1436. driver,
  1437. (snd_pcm_uframes_t *) 0,
  1438. (snd_pcm_uframes_t *) &contiguous,
  1439. 0, (snd_pcm_uframes_t *)&offset) < 0) {
  1440. return -1;
  1441. }
  1442. // steph
  1443. for (int i = 0; i < fPlaybackChannels; i++) {
  1444. // Ouput ports
  1445. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
  1446. buf = (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[i], orig_nframes);
  1447. alsa_driver_write_to_channel (driver, i, buf + nwritten, contiguous);
  1448. // Monitor ports
  1449. if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0) {
  1450. monbuf = (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[i], orig_nframes);
  1451. memcpy(monbuf + nwritten, buf + nwritten, contiguous * sizeof(jack_default_audio_sample_t));
  1452. }
  1453. }
  1454. }
  1455. /*
  1456. for (chn = 0, node = driver->playback_ports, mon_node=driver->monitor_ports;
  1457. node;
  1458. node = jack_slist_next (node), chn++) {
  1459. port = (jack_port_t *) node->data;
  1460. if (!jack_port_connected (port)) {
  1461. continue;
  1462. }
  1463. buf = jack_port_get_buffer (port, orig_nframes);
  1464. alsa_driver_write_to_channel (driver, chn,
  1465. buf + nwritten, contiguous);
  1466. if (mon_node) {
  1467. port = (jack_port_t *) mon_node->data;
  1468. if (!jack_port_connected (port)) {
  1469. continue;
  1470. }
  1471. monbuf = jack_port_get_buffer (port, orig_nframes);
  1472. memcpy (monbuf + nwritten, buf + nwritten, contiguous * sizeof(jack_default_audio_sample_t));
  1473. mon_node = jack_slist_next (mon_node);
  1474. }
  1475. }
  1476. */
  1477. if (!bitset_empty (driver->channels_not_done)) {
  1478. alsa_driver_silence_untouched_channels (driver,
  1479. contiguous);
  1480. }
  1481. if ((err = snd_pcm_mmap_commit (driver->playback_handle,
  1482. offset, contiguous)) < 0) {
  1483. // steph
  1484. // jack_error ("ALSA: could not complete playback of %"
  1485. // PRIu32 " frames: error = %d", contiguous, err);
  1486. jack_error ("ALSA: could not complete playback of %d frames: error = %d", contiguous, err);
  1487. if (err != EPIPE && err != ESTRPIPE)
  1488. return -1;
  1489. }
  1490. nframes -= contiguous;
  1491. nwritten += contiguous;
  1492. }
  1493. return 0;
  1494. }
  1495. void
  1496. JackAlsaDriver::alsa_driver_delete (alsa_driver_t *driver)
  1497. {
  1498. JSList *node;
  1499. if (driver->midi)
  1500. (driver->midi->destroy)(driver->midi);
  1501. for (node = driver->clock_sync_listeners; node;
  1502. node = jack_slist_next (node)) {
  1503. free (node->data);
  1504. }
  1505. jack_slist_free (driver->clock_sync_listeners);
  1506. if (driver->ctl_handle) {
  1507. snd_ctl_close (driver->ctl_handle);
  1508. driver->ctl_handle = 0;
  1509. }
  1510. if (driver->ctl_handle) {
  1511. snd_ctl_close (driver->ctl_handle);
  1512. driver->ctl_handle = 0;
  1513. }
  1514. if (driver->capture_handle) {
  1515. snd_pcm_close (driver->capture_handle);
  1516. driver->capture_handle = 0;
  1517. }
  1518. if (driver->playback_handle) {
  1519. snd_pcm_close (driver->playback_handle);
  1520. driver->capture_handle = 0;
  1521. }
  1522. if (driver->capture_hw_params) {
  1523. snd_pcm_hw_params_free (driver->capture_hw_params);
  1524. driver->capture_hw_params = 0;
  1525. }
  1526. if (driver->playback_hw_params) {
  1527. snd_pcm_hw_params_free (driver->playback_hw_params);
  1528. driver->playback_hw_params = 0;
  1529. }
  1530. if (driver->capture_sw_params) {
  1531. snd_pcm_sw_params_free (driver->capture_sw_params);
  1532. driver->capture_sw_params = 0;
  1533. }
  1534. if (driver->playback_sw_params) {
  1535. snd_pcm_sw_params_free (driver->playback_sw_params);
  1536. driver->playback_sw_params = 0;
  1537. }
  1538. if (driver->pfd) {
  1539. free (driver->pfd);
  1540. }
  1541. if (driver->hw) {
  1542. driver->hw->release (driver->hw);
  1543. driver->hw = 0;
  1544. }
  1545. free(driver->alsa_name_playback);
  1546. free(driver->alsa_name_capture);
  1547. free(driver->alsa_driver);
  1548. alsa_driver_release_channel_dependent_memory (driver);
  1549. free (driver);
  1550. }
  1551. jack_driver_t *
  1552. JackAlsaDriver::alsa_driver_new (const char *name, char *playback_alsa_device,
  1553. char *capture_alsa_device,
  1554. jack_client_t *client,
  1555. jack_nframes_t frames_per_cycle,
  1556. jack_nframes_t user_nperiods,
  1557. jack_nframes_t rate,
  1558. int hw_monitoring,
  1559. int hw_metering,
  1560. int capturing,
  1561. int playing,
  1562. DitherAlgorithm dither,
  1563. int soft_mode,
  1564. int monitor,
  1565. int user_capture_nchnls,
  1566. int user_playback_nchnls,
  1567. int shorts_first,
  1568. jack_nframes_t capture_latency,
  1569. jack_nframes_t playback_latency,
  1570. alsa_midi_t *midi)
  1571. {
  1572. int err;
  1573. alsa_driver_t *driver;
  1574. /*
  1575. printf ("creating alsa driver ... %s|%s|%" PRIu32 "|%" PRIu32
  1576. "|%" PRIu32"|%" PRIu32"|%" PRIu32 "|%s|%s|%s|%s\n",
  1577. playing ? playback_alsa_device : "-",
  1578. capturing ? capture_alsa_device : "-",
  1579. frames_per_cycle, user_nperiods, rate,
  1580. user_capture_nchnls,user_playback_nchnls,
  1581. hw_monitoring ? "hwmon": "nomon",
  1582. hw_metering ? "hwmeter":"swmeter",
  1583. soft_mode ? "soft-mode":"-",
  1584. shorts_first ? "16bit":"32bit");
  1585. */
  1586. driver = (alsa_driver_t *) calloc (1, sizeof (alsa_driver_t));
  1587. jack_driver_nt_init ((jack_driver_nt_t *) driver);
  1588. driver->midi = midi;
  1589. driver->xrun_recovery = 0;
  1590. //driver->nt_attach = (JackDriverNTAttachFunction) alsa_driver_attach;
  1591. //driver->nt_detach = (JackDriverNTDetachFunction) alsa_driver_detach;
  1592. //driver->read = (JackDriverReadFunction) alsa_driver_read;
  1593. //driver->write = (JackDriverReadFunction) alsa_driver_write;
  1594. //driver->null_cycle = (JackDriverNullCycleFunction) alsa_driver_null_cycle;
  1595. //driver->nt_bufsize = (JackDriverNTBufSizeFunction) alsa_driver_bufsize;
  1596. //driver->nt_start = (JackDriverNTStartFunction) alsa_driver_start;
  1597. //driver->nt_stop = (JackDriverNTStopFunction) alsa_driver_stop;
  1598. //driver->nt_run_cycle = (JackDriverNTRunCycleFunction) alsa_driver_run_cycle;
  1599. driver->playback_handle = NULL;
  1600. driver->capture_handle = NULL;
  1601. driver->ctl_handle = 0;
  1602. driver->hw = 0;
  1603. driver->capture_and_playback_not_synced = FALSE;
  1604. driver->max_nchannels = 0;
  1605. driver->user_nchannels = 0;
  1606. driver->playback_nchannels = user_playback_nchnls;
  1607. driver->capture_nchannels = user_capture_nchnls;
  1608. driver->playback_sample_bytes = (shorts_first ? 2 : 4);
  1609. driver->capture_sample_bytes = (shorts_first ? 2 : 4);
  1610. driver->capture_frame_latency = capture_latency;
  1611. driver->playback_frame_latency = playback_latency;
  1612. driver->playback_addr = 0;
  1613. driver->capture_addr = 0;
  1614. driver->playback_interleave_skip = NULL; // steph
  1615. driver->capture_interleave_skip = NULL; // steph
  1616. driver->silent = 0;
  1617. driver->all_monitor_in = FALSE;
  1618. driver->with_monitor_ports = monitor;
  1619. driver->clock_mode = ClockMaster; /* XXX is it? */
  1620. driver->input_monitor_mask = 0; /* XXX is it? */
  1621. driver->capture_ports = 0;
  1622. driver->playback_ports = 0;
  1623. driver->monitor_ports = 0;
  1624. driver->pfd = 0;
  1625. driver->playback_nfds = 0;
  1626. driver->capture_nfds = 0;
  1627. driver->dither = dither;
  1628. driver->soft_mode = soft_mode;
  1629. pthread_mutex_init (&driver->clock_sync_lock, 0);
  1630. driver->clock_sync_listeners = 0;
  1631. driver->poll_late = 0;
  1632. driver->xrun_count = 0;
  1633. driver->process_count = 0;
  1634. driver->alsa_name_playback = strdup (playback_alsa_device);
  1635. driver->alsa_name_capture = strdup (capture_alsa_device);
  1636. if (alsa_driver_check_card_type (driver)) {
  1637. alsa_driver_delete (driver);
  1638. return NULL;
  1639. }
  1640. alsa_driver_hw_specific (driver, hw_monitoring, hw_metering);
  1641. if (playing) {
  1642. if (snd_pcm_open (&driver->playback_handle,
  1643. playback_alsa_device,
  1644. SND_PCM_STREAM_PLAYBACK,
  1645. SND_PCM_NONBLOCK) < 0) {
  1646. switch (errno) {
  1647. case EBUSY:
  1648. jack_error ("the playback device \"%s\" is "
  1649. "already in use. Please stop the"
  1650. " application using it and "
  1651. "run JACK again",
  1652. playback_alsa_device);
  1653. alsa_driver_delete (driver);
  1654. return NULL;
  1655. break;
  1656. case EPERM:
  1657. jack_error ("you do not have permission to open "
  1658. "the audio device \"%s\" for playback",
  1659. playback_alsa_device);
  1660. alsa_driver_delete (driver);
  1661. return NULL;
  1662. break;
  1663. }
  1664. driver->playback_handle = NULL;
  1665. }
  1666. if (driver->playback_handle) {
  1667. snd_pcm_nonblock (driver->playback_handle, 0);
  1668. }
  1669. }
  1670. if (capturing) {
  1671. if (snd_pcm_open (&driver->capture_handle,
  1672. capture_alsa_device,
  1673. SND_PCM_STREAM_CAPTURE,
  1674. SND_PCM_NONBLOCK) < 0) {
  1675. switch (errno) {
  1676. case EBUSY:
  1677. jack_error ("the capture device \"%s\" is "
  1678. "already in use. Please stop the"
  1679. " application using it and "
  1680. "run JACK again",
  1681. capture_alsa_device);
  1682. alsa_driver_delete (driver);
  1683. return NULL;
  1684. break;
  1685. case EPERM:
  1686. jack_error ("you do not have permission to open "
  1687. "the audio device \"%s\" for capture",
  1688. capture_alsa_device);
  1689. alsa_driver_delete (driver);
  1690. return NULL;
  1691. break;
  1692. }
  1693. driver->capture_handle = NULL;
  1694. }
  1695. if (driver->capture_handle) {
  1696. snd_pcm_nonblock (driver->capture_handle, 0);
  1697. }
  1698. }
  1699. if (driver->playback_handle == NULL) {
  1700. if (playing) {
  1701. /* they asked for playback, but we can't do it */
  1702. jack_error ("ALSA: Cannot open PCM device %s for "
  1703. "playback. Falling back to capture-only"
  1704. " mode", name);
  1705. if (driver->capture_handle == NULL) {
  1706. /* can't do anything */
  1707. alsa_driver_delete (driver);
  1708. return NULL;
  1709. }
  1710. playing = FALSE;
  1711. }
  1712. }
  1713. if (driver->capture_handle == NULL) {
  1714. if (capturing) {
  1715. /* they asked for capture, but we can't do it */
  1716. jack_error ("ALSA: Cannot open PCM device %s for "
  1717. "capture. Falling back to playback-only"
  1718. " mode", name);
  1719. if (driver->playback_handle == NULL) {
  1720. /* can't do anything */
  1721. alsa_driver_delete (driver);
  1722. return NULL;
  1723. }
  1724. capturing = FALSE;
  1725. }
  1726. }
  1727. driver->playback_hw_params = 0;
  1728. driver->capture_hw_params = 0;
  1729. driver->playback_sw_params = 0;
  1730. driver->capture_sw_params = 0;
  1731. if (driver->playback_handle) {
  1732. if ((err = snd_pcm_hw_params_malloc (
  1733. &driver->playback_hw_params)) < 0) {
  1734. jack_error ("ALSA: could not allocate playback hw"
  1735. " params structure");
  1736. alsa_driver_delete (driver);
  1737. return NULL;
  1738. }
  1739. if ((err = snd_pcm_sw_params_malloc (
  1740. &driver->playback_sw_params)) < 0) {
  1741. jack_error ("ALSA: could not allocate playback sw"
  1742. " params structure");
  1743. alsa_driver_delete (driver);
  1744. return NULL;
  1745. }
  1746. }
  1747. if (driver->capture_handle) {
  1748. if ((err = snd_pcm_hw_params_malloc (
  1749. &driver->capture_hw_params)) < 0) {
  1750. jack_error ("ALSA: could not allocate capture hw"
  1751. " params structure");
  1752. alsa_driver_delete (driver);
  1753. return NULL;
  1754. }
  1755. if ((err = snd_pcm_sw_params_malloc (
  1756. &driver->capture_sw_params)) < 0) {
  1757. jack_error ("ALSA: could not allocate capture sw"
  1758. " params structure");
  1759. alsa_driver_delete (driver);
  1760. return NULL;
  1761. }
  1762. }
  1763. if (alsa_driver_set_parameters (driver, frames_per_cycle,
  1764. user_nperiods, rate)) {
  1765. alsa_driver_delete (driver);
  1766. return NULL;
  1767. }
  1768. driver->capture_and_playback_not_synced = FALSE;
  1769. if (driver->capture_handle && driver->playback_handle) {
  1770. if (snd_pcm_link (driver->capture_handle,
  1771. driver->playback_handle) != 0) {
  1772. driver->capture_and_playback_not_synced = TRUE;
  1773. }
  1774. }
  1775. driver->client = client;
  1776. return (jack_driver_t *) driver;
  1777. }
  1778. int JackAlsaDriver::Attach()
  1779. {
  1780. JackPort* port;
  1781. int port_index;
  1782. unsigned long port_flags;
  1783. char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  1784. char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  1785. assert(fCaptureChannels < PORT_NUM);
  1786. assert(fPlaybackChannels < PORT_NUM);
  1787. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  1788. alsa_driver_t* alsa_driver = (alsa_driver_t*)fDriver;
  1789. if (alsa_driver->has_hw_monitoring)
  1790. port_flags |= JackPortCanMonitor;
  1791. // ALSA driver may have changed the values
  1792. JackAudioDriver::SetBufferSize(alsa_driver->frames_per_cycle);
  1793. JackAudioDriver::SetSampleRate(alsa_driver->frame_rate);
  1794. jack_log("JackAudioDriver::Attach fBufferSize %ld fSampleRate %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  1795. for (int i = 0; i < fCaptureChannels; i++) {
  1796. snprintf(alias, sizeof(alias) - 1, "%s:capture_%u", fAliasName, i + 1);
  1797. snprintf(name, sizeof(name) - 1, "%s:capture_%d", fClientControl->fName, i + 1);
  1798. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize)) == NO_PORT) {
  1799. jack_error("driver: cannot register port for %s", name);
  1800. return -1;
  1801. }
  1802. port = fGraphManager->GetPort(port_index);
  1803. port->SetAlias(alias);
  1804. port->SetLatency(alsa_driver->frames_per_cycle + alsa_driver->capture_frame_latency);
  1805. fCapturePortList[i] = port_index;
  1806. jack_log("JackAudioDriver::Attach fCapturePortList[i] %ld ", port_index);
  1807. }
  1808. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  1809. for (int i = 0; i < fPlaybackChannels; i++) {
  1810. snprintf(alias, sizeof(alias) - 1, "%s:playback_%u", fAliasName, i + 1);
  1811. snprintf(name, sizeof(name) - 1, "%s:playback_%d", fClientControl->fName, i + 1);
  1812. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize)) == NO_PORT) {
  1813. jack_error("driver: cannot register port for %s", name);
  1814. return -1;
  1815. }
  1816. port = fGraphManager->GetPort(port_index);
  1817. port->SetAlias(alias);
  1818. // Add one buffer more latency if "async" mode is used...
  1819. port->SetLatency((alsa_driver->frames_per_cycle * (alsa_driver->user_nperiods - 1)) +
  1820. ((fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize) + alsa_driver->playback_frame_latency);
  1821. fPlaybackPortList[i] = port_index;
  1822. jack_log("JackAudioDriver::Attach fPlaybackPortList[i] %ld ", port_index);
  1823. // Monitor ports
  1824. if (fWithMonitorPorts) {
  1825. jack_log("Create monitor port ");
  1826. snprintf(name, sizeof(name) - 1, "%s:monitor_%d", fClientControl->fName, i + 1);
  1827. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, fEngineControl->fBufferSize)) == NO_PORT) {
  1828. jack_error ("ALSA: cannot register monitor port for %s", name);
  1829. } else {
  1830. port = fGraphManager->GetPort(port_index);
  1831. port->SetLatency(alsa_driver->frames_per_cycle);
  1832. fMonitorPortList[i] = port_index;
  1833. }
  1834. }
  1835. }
  1836. if (alsa_driver->midi) {
  1837. int err = (alsa_driver->midi->attach)(alsa_driver->midi);
  1838. if (err)
  1839. jack_error ("ALSA: cannot attach MIDI: %d", err);
  1840. }
  1841. return 0;
  1842. }
  1843. int JackAlsaDriver::Detach()
  1844. {
  1845. alsa_driver_t* alsa_driver = (alsa_driver_t*)fDriver;
  1846. if (alsa_driver->midi)
  1847. (alsa_driver->midi->detach)(alsa_driver->midi);
  1848. return JackAudioDriver::Detach();
  1849. }
  1850. int JackAlsaDriver::Open(jack_nframes_t nframes,
  1851. jack_nframes_t user_nperiods,
  1852. jack_nframes_t samplerate,
  1853. bool hw_monitoring,
  1854. bool hw_metering,
  1855. bool capturing,
  1856. bool playing,
  1857. DitherAlgorithm dither,
  1858. bool soft_mode,
  1859. bool monitor,
  1860. int inchannels,
  1861. int outchannels,
  1862. bool shorts_first,
  1863. const char* capture_driver_name,
  1864. const char* playback_driver_name,
  1865. jack_nframes_t capture_latency,
  1866. jack_nframes_t playback_latency,
  1867. const char* midi_driver_name)
  1868. {
  1869. // Generic JackAudioDriver Open
  1870. if (JackAudioDriver::Open(nframes, samplerate, capturing, playing,
  1871. inchannels, outchannels, monitor, capture_driver_name, playback_driver_name,
  1872. capture_latency, playback_latency) != 0) {
  1873. return -1;
  1874. }
  1875. alsa_midi_t *midi = 0;
  1876. if (strcmp(midi_driver_name, "seq") == 0)
  1877. midi = alsa_seqmidi_new((jack_client_t*)this, 0);
  1878. else if (strcmp(midi_driver_name, "raw") == 0)
  1879. midi = alsa_rawmidi_new((jack_client_t*)this);
  1880. fDriver = alsa_driver_new ("alsa_pcm", (char*)playback_driver_name, (char*)capture_driver_name,
  1881. NULL,
  1882. nframes,
  1883. user_nperiods,
  1884. samplerate,
  1885. hw_monitoring,
  1886. hw_metering,
  1887. capturing,
  1888. playing,
  1889. dither,
  1890. soft_mode,
  1891. monitor,
  1892. inchannels,
  1893. outchannels,
  1894. shorts_first,
  1895. capture_latency,
  1896. playback_latency,
  1897. midi);
  1898. if (fDriver) {
  1899. // ALSA driver may have changed the in/out values
  1900. fCaptureChannels = ((alsa_driver_t *)fDriver)->capture_nchannels;
  1901. fPlaybackChannels = ((alsa_driver_t *)fDriver)->playback_nchannels;
  1902. return 0;
  1903. } else {
  1904. return -1;
  1905. }
  1906. }
  1907. int JackAlsaDriver::Close()
  1908. {
  1909. JackAudioDriver::Close();
  1910. alsa_driver_delete((alsa_driver_t*)fDriver);
  1911. return 0;
  1912. }
  1913. int JackAlsaDriver::Start()
  1914. {
  1915. return alsa_driver_start((alsa_driver_t *)fDriver);
  1916. }
  1917. int JackAlsaDriver::Stop()
  1918. {
  1919. return alsa_driver_stop((alsa_driver_t *)fDriver);
  1920. }
  1921. int JackAlsaDriver::Read()
  1922. {
  1923. /* Taken from alsa_driver_run_cycle */
  1924. int wait_status;
  1925. jack_nframes_t nframes;
  1926. fDelayedUsecs = 0.f;
  1927. nframes = alsa_driver_wait((alsa_driver_t *)fDriver, -1, &wait_status, &fDelayedUsecs);
  1928. if (wait_status < 0)
  1929. return -1; /* driver failed */
  1930. if (nframes == 0) {
  1931. /* we detected an xrun and restarted: notify
  1932. * clients about the delay.
  1933. */
  1934. jack_log("ALSA XRun");
  1935. NotifyXRun(fBeginDateUst, fDelayedUsecs);
  1936. return -1;
  1937. }
  1938. if (nframes != fEngineControl->fBufferSize)
  1939. jack_log("JackAlsaDriver::Read nframes = %ld", nframes);
  1940. // Has to be done before read
  1941. JackDriver::CycleIncTime();
  1942. return alsa_driver_read((alsa_driver_t *)fDriver, fEngineControl->fBufferSize);
  1943. }
  1944. int JackAlsaDriver::Write()
  1945. {
  1946. return alsa_driver_write((alsa_driver_t *)fDriver, fEngineControl->fBufferSize);
  1947. }
  1948. void
  1949. JackAlsaDriver::jack_driver_init (jack_driver_t *driver)
  1950. {
  1951. memset (driver, 0, sizeof (*driver));
  1952. driver->attach = 0;
  1953. driver->detach = 0;
  1954. driver->write = 0;
  1955. driver->read = 0;
  1956. driver->null_cycle = 0;
  1957. driver->bufsize = 0;
  1958. driver->start = 0;
  1959. driver->stop = 0;
  1960. }
  1961. void
  1962. JackAlsaDriver::jack_driver_nt_init (jack_driver_nt_t * driver)
  1963. {
  1964. memset (driver, 0, sizeof (*driver));
  1965. jack_driver_init ((jack_driver_t *) driver);
  1966. driver->attach = 0;
  1967. driver->detach = 0;
  1968. driver->bufsize = 0;
  1969. driver->stop = 0;
  1970. driver->start = 0;
  1971. driver->nt_bufsize = 0;
  1972. driver->nt_start = 0;
  1973. driver->nt_stop = 0;
  1974. driver->nt_attach = 0;
  1975. driver->nt_detach = 0;
  1976. driver->nt_run_cycle = 0;
  1977. }
  1978. int JackAlsaDriver::is_realtime() const
  1979. {
  1980. return fEngineControl->fRealTime;
  1981. }
  1982. int JackAlsaDriver::create_thread(pthread_t *thread, int priority, int realtime, void *(*start_routine)(void*), void *arg)
  1983. {
  1984. return JackPosixThread::StartImp(thread, priority, realtime, start_routine, arg);
  1985. }
  1986. jack_port_id_t JackAlsaDriver::port_register(const char *port_name, const char *port_type, unsigned long flags, unsigned long buffer_size)
  1987. {
  1988. unsigned int port_index;
  1989. int res = fEngine->PortRegister(fClientControl->fRefNum, port_name, port_type, flags, buffer_size, &port_index);
  1990. return (res == 0) ? port_index : 0;
  1991. }
  1992. int JackAlsaDriver::port_unregister(jack_port_id_t port_index)
  1993. {
  1994. return fEngine->PortUnRegister(fClientControl->fRefNum, port_index);
  1995. }
  1996. void* JackAlsaDriver::port_get_buffer(int port, jack_nframes_t nframes)
  1997. {
  1998. return fGraphManager->GetBuffer(port, nframes);
  1999. }
  2000. int JackAlsaDriver::port_set_alias(int port, const char* name)
  2001. {
  2002. return fGraphManager->GetPort(port)->SetAlias(name);
  2003. }
  2004. jack_nframes_t JackAlsaDriver::get_sample_rate() const
  2005. {
  2006. return fEngineControl->fSampleRate;
  2007. }
  2008. jack_nframes_t JackAlsaDriver::frame_time() const
  2009. {
  2010. JackTimer timer;
  2011. fEngineControl->ReadFrameTime(&timer);
  2012. if (timer.fInitialized) {
  2013. return timer.fFrames +
  2014. (long) rint(((double) ((GetMicroSeconds() - timer.fCurrentWakeup)) /
  2015. ((jack_time_t)(timer.fNextWakeUp - timer.fCurrentWakeup))) * fEngineControl->fBufferSize);
  2016. } else
  2017. return 0;
  2018. }
  2019. jack_nframes_t JackAlsaDriver::last_frame_time() const
  2020. {
  2021. JackTimer timer;
  2022. fEngineControl->ReadFrameTime(&timer);
  2023. return timer.fFrames;
  2024. }
  2025. } // end of namespace
  2026. #ifdef __cplusplus
  2027. extern "C"
  2028. {
  2029. #endif
  2030. static int
  2031. dither_opt (char c, DitherAlgorithm* dither) {
  2032. switch (c) {
  2033. case '-':
  2034. case 'n':
  2035. *dither = None;
  2036. break;
  2037. case 'r':
  2038. *dither = Rectangular;
  2039. break;
  2040. case 's':
  2041. *dither = Shaped;
  2042. break;
  2043. case 't':
  2044. *dither = Triangular;
  2045. break;
  2046. default:
  2047. fprintf (stderr, "ALSA driver: illegal dithering mode %c\n", c);
  2048. return -1;
  2049. }
  2050. return 0;
  2051. }
  2052. EXPORT const jack_driver_desc_t* driver_get_descriptor () {
  2053. jack_driver_desc_t * desc;
  2054. jack_driver_param_desc_t * params;
  2055. unsigned int i;
  2056. desc = (jack_driver_desc_t*)calloc (1, sizeof (jack_driver_desc_t));
  2057. strcpy (desc->name, "alsa");
  2058. desc->nparams = 18;
  2059. params = (jack_driver_param_desc_t*)calloc (desc->nparams, sizeof (jack_driver_param_desc_t));
  2060. i = 0;
  2061. strcpy (params[i].name, "capture");
  2062. params[i].character = 'C';
  2063. params[i].type = JackDriverParamString;
  2064. strcpy (params[i].value.str, "none");
  2065. strcpy (params[i].short_desc,
  2066. "Provide capture ports. Optionally set device");
  2067. strcpy (params[i].long_desc, params[i].short_desc);
  2068. i++;
  2069. strcpy (params[i].name, "playback");
  2070. params[i].character = 'P';
  2071. params[i].type = JackDriverParamString;
  2072. strcpy (params[i].value.str, "none");
  2073. strcpy (params[i].short_desc,
  2074. "Provide playback ports. Optionally set device");
  2075. strcpy (params[i].long_desc, params[i].short_desc);
  2076. i++;
  2077. strcpy (params[i].name, "device");
  2078. params[i].character = 'd';
  2079. params[i].type = JackDriverParamString;
  2080. strcpy (params[i].value.str, "hw:0");
  2081. strcpy (params[i].short_desc, "ALSA device name");
  2082. strcpy (params[i].long_desc, params[i].short_desc);
  2083. i++;
  2084. strcpy (params[i].name, "rate");
  2085. params[i].character = 'r';
  2086. params[i].type = JackDriverParamUInt;
  2087. params[i].value.ui = 48000U;
  2088. strcpy (params[i].short_desc, "Sample rate");
  2089. strcpy (params[i].long_desc, params[i].short_desc);
  2090. i++;
  2091. strcpy (params[i].name, "period");
  2092. params[i].character = 'p';
  2093. params[i].type = JackDriverParamUInt;
  2094. params[i].value.ui = 1024U;
  2095. strcpy (params[i].short_desc, "Frames per period");
  2096. strcpy (params[i].long_desc, params[i].short_desc);
  2097. i++;
  2098. strcpy (params[i].name, "nperiods");
  2099. params[i].character = 'n';
  2100. params[i].type = JackDriverParamUInt;
  2101. params[i].value.ui = 2U;
  2102. strcpy (params[i].short_desc, "Number of periods of playback latency");
  2103. strcpy (params[i].long_desc, params[i].short_desc);
  2104. i++;
  2105. strcpy (params[i].name, "hwmon");
  2106. params[i].character = 'H';
  2107. params[i].type = JackDriverParamBool;
  2108. params[i].value.i = 0;
  2109. strcpy (params[i].short_desc, "Hardware monitoring, if available");
  2110. strcpy (params[i].long_desc, params[i].short_desc);
  2111. i++;
  2112. strcpy (params[i].name, "hwmeter");
  2113. params[i].character = 'M';
  2114. params[i].type = JackDriverParamBool;
  2115. params[i].value.i = 0;
  2116. strcpy (params[i].short_desc, "Hardware metering, if available");
  2117. strcpy (params[i].long_desc, params[i].short_desc);
  2118. i++;
  2119. strcpy (params[i].name, "duplex");
  2120. params[i].character = 'D';
  2121. params[i].type = JackDriverParamBool;
  2122. params[i].value.i = 1;
  2123. strcpy (params[i].short_desc,
  2124. "Provide both capture and playback ports");
  2125. strcpy (params[i].long_desc, params[i].short_desc);
  2126. i++;
  2127. strcpy (params[i].name, "softmode");
  2128. params[i].character = 's';
  2129. params[i].type = JackDriverParamBool;
  2130. params[i].value.i = 0;
  2131. strcpy (params[i].short_desc, "Soft-mode, no xrun handling");
  2132. strcpy (params[i].long_desc, params[i].short_desc);
  2133. i++;
  2134. strcpy (params[i].name, "monitor");
  2135. params[i].character = 'm';
  2136. params[i].type = JackDriverParamBool;
  2137. params[i].value.i = 0;
  2138. strcpy (params[i].short_desc, "Provide monitor ports for the output");
  2139. strcpy (params[i].long_desc, params[i].short_desc);
  2140. i++;
  2141. strcpy (params[i].name, "dither");
  2142. params[i].character = 'z';
  2143. params[i].type = JackDriverParamChar;
  2144. params[i].value.c = 'n';
  2145. strcpy (params[i].short_desc, "Dithering mode");
  2146. strcpy (params[i].long_desc,
  2147. "Dithering mode:\n"
  2148. " n - none\n"
  2149. " r - rectangular\n"
  2150. " s - shaped\n"
  2151. " t - triangular");
  2152. i++;
  2153. strcpy (params[i].name, "inchannels");
  2154. params[i].character = 'i';
  2155. params[i].type = JackDriverParamUInt;
  2156. params[i].value.i = 0;
  2157. strcpy (params[i].short_desc,
  2158. "Number of capture channels (defaults to hardware max)");
  2159. strcpy (params[i].long_desc, params[i].short_desc);
  2160. i++;
  2161. strcpy (params[i].name, "outchannels");
  2162. params[i].character = 'o';
  2163. params[i].type = JackDriverParamUInt;
  2164. params[i].value.i = 0;
  2165. strcpy (params[i].short_desc,
  2166. "Number of playback channels (defaults to hardware max)");
  2167. strcpy (params[i].long_desc, params[i].short_desc);
  2168. i++;
  2169. strcpy (params[i].name, "shorts");
  2170. params[i].character = 'S';
  2171. params[i].type = JackDriverParamBool;
  2172. params[i].value.i = FALSE;
  2173. strcpy (params[i].short_desc, "Try 16-bit samples before 32-bit");
  2174. strcpy (params[i].long_desc, params[i].short_desc);
  2175. i++;
  2176. strcpy (params[i].name, "input-latency");
  2177. params[i].character = 'I';
  2178. params[i].type = JackDriverParamUInt;
  2179. params[i].value.i = 0;
  2180. strcpy (params[i].short_desc, "Extra input latency (frames)");
  2181. strcpy (params[i].long_desc, params[i].short_desc);
  2182. i++;
  2183. strcpy (params[i].name, "output-latency");
  2184. params[i].character = 'O';
  2185. params[i].type = JackDriverParamUInt;
  2186. params[i].value.i = 0;
  2187. strcpy (params[i].short_desc, "Extra output latency (frames)");
  2188. strcpy (params[i].long_desc, params[i].short_desc);
  2189. i++;
  2190. strcpy (params[i].name, "midi-driver");
  2191. params[i].character = 'X';
  2192. params[i].type = JackDriverParamString;
  2193. strcpy (params[i].value.str, "none");
  2194. strcpy (params[i].short_desc, "ALSA MIDI driver name (seq|raw)");
  2195. strcpy (params[i].long_desc,
  2196. "ALSA MIDI driver:\n"
  2197. " none - no MIDI driver\n"
  2198. " seq - ALSA Sequencer driver\n"
  2199. " raw - ALSA RawMIDI driver\n");
  2200. desc->params = params;
  2201. return desc;
  2202. }
  2203. EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params) {
  2204. jack_nframes_t srate = 48000;
  2205. jack_nframes_t frames_per_interrupt = 1024;
  2206. unsigned long user_nperiods = 2;
  2207. const char *playback_pcm_name = "hw:0";
  2208. const char *capture_pcm_name = "hw:0";
  2209. int hw_monitoring = FALSE;
  2210. int hw_metering = FALSE;
  2211. int capture = FALSE;
  2212. int playback = FALSE;
  2213. int soft_mode = FALSE;
  2214. int monitor = FALSE;
  2215. DitherAlgorithm dither = None;
  2216. int user_capture_nchnls = 0;
  2217. int user_playback_nchnls = 0;
  2218. int shorts_first = FALSE;
  2219. jack_nframes_t systemic_input_latency = 0;
  2220. jack_nframes_t systemic_output_latency = 0;
  2221. const JSList * node;
  2222. const jack_driver_param_t * param;
  2223. const char *midi_driver = "none";
  2224. for (node = params; node; node = jack_slist_next (node)) {
  2225. param = (const jack_driver_param_t *) node->data;
  2226. switch (param->character) {
  2227. case 'C':
  2228. capture = TRUE;
  2229. if (strcmp (param->value.str, "none") != 0) {
  2230. capture_pcm_name = strdup (param->value.str);
  2231. jack_log("capture device %s", capture_pcm_name);
  2232. }
  2233. break;
  2234. case 'P':
  2235. playback = TRUE;
  2236. if (strcmp (param->value.str, "none") != 0) {
  2237. playback_pcm_name = strdup (param->value.str);
  2238. jack_log("playback device %s", playback_pcm_name);
  2239. }
  2240. break;
  2241. case 'D':
  2242. playback = TRUE;
  2243. capture = TRUE;
  2244. break;
  2245. case 'd':
  2246. playback_pcm_name = strdup (param->value.str);
  2247. capture_pcm_name = strdup (param->value.str);
  2248. jack_log("playback device %s", playback_pcm_name);
  2249. jack_log("capture device %s", capture_pcm_name);
  2250. break;
  2251. case 'H':
  2252. hw_monitoring = param->value.i;
  2253. break;
  2254. case 'm':
  2255. monitor = param->value.i;
  2256. break;
  2257. case 'M':
  2258. hw_metering = param->value.i;
  2259. break;
  2260. case 'r':
  2261. srate = param->value.ui;
  2262. jack_log("apparent rate = %d", srate);
  2263. break;
  2264. case 'p':
  2265. frames_per_interrupt = param->value.ui;
  2266. jack_log("frames per period = %d", frames_per_interrupt);
  2267. break;
  2268. case 'n':
  2269. user_nperiods = param->value.ui;
  2270. if (user_nperiods < 2) /* enforce minimum value */
  2271. user_nperiods = 2;
  2272. break;
  2273. case 's':
  2274. soft_mode = param->value.i;
  2275. break;
  2276. case 'z':
  2277. if (dither_opt (param->value.c, &dither)) {
  2278. return NULL;
  2279. }
  2280. break;
  2281. case 'i':
  2282. user_capture_nchnls = param->value.ui;
  2283. break;
  2284. case 'o':
  2285. user_playback_nchnls = param->value.ui;
  2286. break;
  2287. case 'S':
  2288. shorts_first = param->value.i;
  2289. break;
  2290. case 'I':
  2291. systemic_input_latency = param->value.ui;
  2292. break;
  2293. case 'O':
  2294. systemic_output_latency = param->value.ui;
  2295. break;
  2296. case 'X':
  2297. midi_driver = strdup(param->value.str);
  2298. break;
  2299. }
  2300. }
  2301. /* duplex is the default */
  2302. if (!capture && !playback) {
  2303. capture = TRUE;
  2304. playback = TRUE;
  2305. }
  2306. Jack::JackAlsaDriver* alsa_driver = new Jack::JackAlsaDriver("system", "alsa_pcm", engine, table);
  2307. Jack::JackDriverClientInterface* threaded_driver = new Jack::JackThreadedDriver(alsa_driver);
  2308. // Special open for ALSA driver...
  2309. if (alsa_driver->Open(frames_per_interrupt, user_nperiods, srate, hw_monitoring, hw_metering, capture, playback, dither, soft_mode, monitor,
  2310. user_capture_nchnls, user_playback_nchnls, shorts_first, capture_pcm_name, playback_pcm_name,
  2311. systemic_input_latency, systemic_output_latency, midi_driver) == 0) {
  2312. return threaded_driver;
  2313. } else {
  2314. delete threaded_driver; // Delete the decorated driver
  2315. return NULL;
  2316. }
  2317. }
  2318. #ifdef __cplusplus
  2319. }
  2320. #endif