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.

2757 lines
83KB

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