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.

2753 lines
88KB

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