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.

2306 lines
59KB

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