jack1 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.

2808 lines
75KB

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