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.

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