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.

2327 lines
60KB

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