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.

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