jack1 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1957 lines
51KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. $Id$
  15. */
  16. #include <math.h>
  17. #include <stdio.h>
  18. #include <memory.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <errno.h>
  22. #include <stdarg.h>
  23. #include <getopt.h>
  24. #include <jack/alsa_driver.h>
  25. #include <jack/types.h>
  26. #include <jack/internal.h>
  27. #include <jack/engine.h>
  28. #include <jack/hammerfall.h>
  29. #include <jack/hdsp.h>
  30. #include <jack/ice1712.h>
  31. #include <jack/generic.h>
  32. #include <jack/time.h>
  33. extern void store_work_time (int);
  34. extern void store_wait_time (int);
  35. extern void show_wait_times ();
  36. extern void show_work_times ();
  37. #undef DEBUG_WAKEUP
  38. /* Delay (in process calls) before jackd will report an xrun */
  39. #define XRUN_REPORT_DELAY 64
  40. static void
  41. alsa_driver_release_channel_dependent_memory (alsa_driver_t *driver)
  42. {
  43. if (driver->playback_addr) {
  44. free (driver->playback_addr);
  45. driver->playback_addr = 0;
  46. }
  47. if (driver->capture_addr) {
  48. free (driver->capture_addr);
  49. driver->capture_addr = 0;
  50. }
  51. if (driver->silent) {
  52. free (driver->silent);
  53. driver->silent = 0;
  54. }
  55. if (driver->dither_state) {
  56. free (driver->dither_state);
  57. driver->dither_state = 0;
  58. }
  59. }
  60. static int
  61. alsa_driver_check_capabilities (alsa_driver_t *driver)
  62. {
  63. return 0;
  64. }
  65. static int
  66. alsa_driver_check_card_type (alsa_driver_t *driver)
  67. {
  68. int err;
  69. snd_ctl_card_info_t *card_info;
  70. snd_ctl_card_info_alloca (&card_info);
  71. // XXX: I don't know the "right" way to do this. Which to use driver->alsa_name_playback or driver->alsa_name_capture.
  72. if ((err = snd_ctl_open (&driver->ctl_handle, driver->alsa_name_playback, 0)) < 0) {
  73. jack_error ("control open \"%s\" (%s)", driver->alsa_name_playback, snd_strerror(err));
  74. return -1;
  75. }
  76. if ((err = snd_ctl_card_info(driver->ctl_handle, card_info)) < 0) {
  77. jack_error ("control hardware info \"%s\" (%s)", driver->alsa_name_playback, snd_strerror (err));
  78. snd_ctl_close (driver->ctl_handle);
  79. return -1;
  80. }
  81. driver->alsa_driver = strdup(snd_ctl_card_info_get_driver (card_info));
  82. return alsa_driver_check_capabilities (driver);
  83. }
  84. static int
  85. alsa_driver_hammerfall_hardware (alsa_driver_t *driver)
  86. {
  87. driver->hw = jack_alsa_hammerfall_hw_new (driver);
  88. return 0;
  89. }
  90. static int
  91. alsa_driver_hdsp_hardware (alsa_driver_t *driver)
  92. {
  93. driver->hw = jack_alsa_hdsp_hw_new (driver);
  94. return 0;
  95. }
  96. static int
  97. alsa_driver_ice1712_hardware (alsa_driver_t *driver)
  98. {
  99. driver->hw = jack_alsa_ice1712_hw_new (driver);
  100. return 0;
  101. }
  102. static int
  103. alsa_driver_generic_hardware (alsa_driver_t *driver)
  104. {
  105. driver->hw = jack_alsa_generic_hw_new (driver);
  106. return 0;
  107. }
  108. static int
  109. alsa_driver_hw_specific (alsa_driver_t *driver, int hw_monitoring, int hw_metering)
  110. {
  111. int err;
  112. if (!strcmp(driver->alsa_driver, "RME9652")) {
  113. if ((err = alsa_driver_hammerfall_hardware (driver)) != 0) {
  114. return err;
  115. }
  116. } else if (!strcmp(driver->alsa_driver, "H-DSP")) {
  117. if ((err = alsa_driver_hdsp_hardware (driver)) !=0) {
  118. return err;
  119. }
  120. } else if (!strcmp(driver->alsa_driver, "ICE1712")) {
  121. if ((err = alsa_driver_ice1712_hardware (driver)) !=0) {
  122. return err;
  123. }
  124. } else {
  125. if ((err = alsa_driver_generic_hardware (driver)) != 0) {
  126. return err;
  127. }
  128. }
  129. if (driver->hw->capabilities & Cap_HardwareMonitoring) {
  130. driver->has_hw_monitoring = TRUE;
  131. /* XXX need to ensure that this is really FALSE or TRUE or whatever*/
  132. driver->hw_monitoring = hw_monitoring;
  133. } else {
  134. driver->has_hw_monitoring = FALSE;
  135. driver->hw_monitoring = FALSE;
  136. }
  137. if (driver->hw->capabilities & Cap_ClockLockReporting) {
  138. driver->has_clock_sync_reporting = TRUE;
  139. } else {
  140. driver->has_clock_sync_reporting = FALSE;
  141. }
  142. if (driver->hw->capabilities & Cap_HardwareMetering) {
  143. driver->has_hw_metering = TRUE;
  144. driver->hw_metering = hw_metering;
  145. } else {
  146. driver->has_hw_metering = FALSE;
  147. driver->hw_metering = FALSE;
  148. }
  149. return 0;
  150. }
  151. static void
  152. alsa_driver_setup_io_function_pointers (alsa_driver_t *driver)
  153. {
  154. switch (driver->playback_sample_bytes) {
  155. case 2:
  156. if (driver->playback_interleaved) {
  157. driver->channel_copy = memcpy_interleave_d16_s16;
  158. } else {
  159. driver->channel_copy = memcpy_fake;
  160. }
  161. switch (driver->dither) {
  162. case Rectangular:
  163. printf("Rectangular dithering at 16 bits\n");
  164. driver->write_via_copy = sample_move_dither_rect_d16_sS;
  165. break;
  166. case Triangular:
  167. printf("Triangular dithering at 16 bits\n");
  168. driver->write_via_copy = sample_move_dither_tri_d16_sS;
  169. break;
  170. case Shaped:
  171. printf("Noise-shaped dithering at 16 bits\n");
  172. driver->write_via_copy = sample_move_dither_shaped_d16_sS;
  173. break;
  174. default:
  175. driver->write_via_copy = sample_move_d16_sS;
  176. break;
  177. }
  178. break;
  179. case 4:
  180. if (driver->playback_interleaved) {
  181. driver->channel_copy = memcpy_interleave_d32_s32;
  182. } else {
  183. driver->channel_copy = memcpy_fake;
  184. }
  185. switch (driver->dither) {
  186. case Rectangular:
  187. printf("Rectangular dithering at 16 bits\n");
  188. driver->write_via_copy = sample_move_dither_rect_d32u24_sS;
  189. break;
  190. case Triangular:
  191. printf("Triangular dithering at 16 bits\n");
  192. driver->write_via_copy = sample_move_dither_tri_d32u24_sS;
  193. break;
  194. case Shaped:
  195. printf("Noise-shaped dithering at 16 bits\n");
  196. driver->write_via_copy = sample_move_dither_shaped_d32u24_sS;
  197. break;
  198. default:
  199. driver->write_via_copy = sample_move_d32u24_sS;
  200. break;
  201. }
  202. break;
  203. }
  204. switch (driver->capture_sample_bytes) {
  205. case 2:
  206. driver->read_via_copy = sample_move_dS_s16;
  207. break;
  208. case 4:
  209. driver->read_via_copy = sample_move_dS_s32u24;
  210. break;
  211. }
  212. }
  213. static int
  214. alsa_driver_configure_stream (alsa_driver_t *driver, char *device_name,
  215. const char *stream_name,
  216. snd_pcm_t *handle,
  217. snd_pcm_hw_params_t *hw_params,
  218. snd_pcm_sw_params_t *sw_params,
  219. unsigned long *nchns)
  220. {
  221. int err;
  222. if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0) {
  223. jack_error ("ALSA: no playback configurations available (%s)",
  224. snd_strerror (err));
  225. return -1;
  226. }
  227. if ((err = snd_pcm_hw_params_set_periods_integer (handle, hw_params)) < 0) {
  228. jack_error ("ALSA: cannot restrict period size to integral value.");
  229. return -1;
  230. }
  231. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_NONINTERLEAVED)) < 0) {
  232. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
  233. jack_error ("ALSA: mmap-based access is not possible for the %s "
  234. "stream of this audio interface", stream_name);
  235. return -1;
  236. }
  237. }
  238. if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S32)) < 0) {
  239. if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S16)) < 0) {
  240. jack_error ("Sorry. The audio interface \"%s\""
  241. "doesn't support either of the two hardware sample formats that jack can use.",
  242. device_name);
  243. return -1;
  244. }
  245. }
  246. if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params, driver->frame_rate, 0)) < 0) {
  247. jack_error ("ALSA: cannot set sample/frame rate to %u for %s", driver->frame_rate, stream_name);
  248. return -1;
  249. }
  250. *nchns = snd_pcm_hw_params_get_channels_max (hw_params);
  251. if (*nchns > 1024) {
  252. /* the hapless user is an unwitting victim of the "default"
  253. ALSA PCM device, which can support up to 16 million
  254. channels. since they can't be bothered to set up
  255. a proper default device, limit the number of channels
  256. for them to a sane default.
  257. */
  258. jack_error ("You appear to be using the ALSA software \"plug\" layer, probably\n"
  259. "a result of using the \"default\" ALSA device. This is less\n"
  260. "efficient than it could be. Consider using a ~/.asoundrc file\n"
  261. "to define a hardware audio device rather than using the plug layer\n");
  262. *nchns = 2;
  263. }
  264. if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, *nchns)) < 0) {
  265. jack_error ("ALSA: cannot set channel count to %u for %s", *nchns, stream_name);
  266. return -1;
  267. }
  268. if ((err = snd_pcm_hw_params_set_period_size (handle, hw_params, driver->frames_per_cycle, 0)) < 0) {
  269. jack_error ("ALSA: cannot set period size to %u frames for %s", driver->frames_per_cycle, stream_name);
  270. return -1;
  271. }
  272. if ((err = snd_pcm_hw_params_set_periods (handle, hw_params, driver->user_nperiods, 0)) < 0) {
  273. jack_error ("ALSA: cannot set number of periods to %u for %s", driver->user_nperiods, stream_name);
  274. return -1;
  275. }
  276. if ((err = snd_pcm_hw_params_set_buffer_size (handle, hw_params, driver->user_nperiods * driver->frames_per_cycle)) < 0) {
  277. jack_error ("ALSA: cannot set buffer length to %u for %s", driver->user_nperiods * driver->frames_per_cycle, stream_name);
  278. return -1;
  279. }
  280. if ((err = snd_pcm_hw_params (handle, hw_params)) < 0) {
  281. jack_error ("ALSA: cannot set hardware parameters for %s", stream_name);
  282. return -1;
  283. }
  284. snd_pcm_sw_params_current (handle, sw_params);
  285. if ((err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, 0U)) < 0) {
  286. jack_error ("ALSA: cannot set start mode for %s", stream_name);
  287. return -1;
  288. }
  289. {
  290. snd_pcm_uframes_t stop_th = driver->user_nperiods * driver->frames_per_cycle;
  291. if (driver->soft_mode) {
  292. stop_th = (snd_pcm_uframes_t)-1;
  293. }
  294. if ((err = snd_pcm_sw_params_set_stop_threshold (handle, sw_params, stop_th)) < 0) {
  295. jack_error ("ALSA: cannot set stop mode for %s", stream_name);
  296. return -1;
  297. }
  298. }
  299. if ((err = snd_pcm_sw_params_set_silence_threshold (handle, sw_params, 0)) < 0) {
  300. jack_error ("ALSA: cannot set silence threshold for %s", stream_name);
  301. return -1;
  302. }
  303. if ((err = snd_pcm_sw_params_set_silence_size (handle, sw_params, driver->frames_per_cycle * driver->nfragments)) < 0) {
  304. jack_error ("ALSA: cannot set silence size for %s", stream_name);
  305. return -1;
  306. }
  307. if ((err = snd_pcm_sw_params_set_avail_min (handle, sw_params, driver->frames_per_cycle)) < 0) {
  308. jack_error ("ALSA: cannot set avail min for %s", stream_name);
  309. return -1;
  310. }
  311. if ((err = snd_pcm_sw_params (handle, sw_params)) < 0) {
  312. jack_error ("ALSA: cannot set software parameters for %s", stream_name);
  313. return -1;
  314. }
  315. return 0;
  316. }
  317. static int
  318. alsa_driver_set_parameters (alsa_driver_t *driver, jack_nframes_t frames_per_cycle, jack_nframes_t user_nperiods, jack_nframes_t rate)
  319. {
  320. int dir;
  321. unsigned int p_period_size = 0;
  322. unsigned int c_period_size = 0;
  323. unsigned int p_nfragments = 0;
  324. unsigned int c_nfragments = 0;
  325. channel_t chn;
  326. driver->frame_rate = rate;
  327. driver->frames_per_cycle = frames_per_cycle;
  328. driver->user_nperiods = user_nperiods;
  329. if (driver->capture_handle) {
  330. if (alsa_driver_configure_stream (driver, driver->alsa_name_capture, "capture",
  331. driver->capture_handle,
  332. driver->capture_hw_params,
  333. driver->capture_sw_params,
  334. &driver->capture_nchannels)) {
  335. jack_error ("ALSA: cannot configure capture channel");
  336. return -1;
  337. }
  338. }
  339. if (driver->playback_handle) {
  340. if (alsa_driver_configure_stream (driver, driver->alsa_name_playback, "playback",
  341. driver->playback_handle,
  342. driver->playback_hw_params,
  343. driver->playback_sw_params,
  344. &driver->playback_nchannels)) {
  345. jack_error ("ALSA: cannot configure playback channel");
  346. return -1;
  347. }
  348. }
  349. /* check the fragment size, since thats non-negotiable */
  350. if (driver->playback_handle) {
  351. p_period_size = snd_pcm_hw_params_get_period_size (driver->playback_hw_params, &dir);
  352. p_nfragments = snd_pcm_hw_params_get_periods (driver->playback_hw_params, &dir);
  353. driver->playback_sample_format = (snd_pcm_format_t) snd_pcm_hw_params_get_format (driver->playback_hw_params);
  354. driver->playback_interleaved = (snd_pcm_hw_params_get_access (driver->playback_hw_params) == SND_PCM_ACCESS_MMAP_INTERLEAVED);
  355. if (p_period_size != driver->frames_per_cycle) {
  356. jack_error ("alsa_pcm: requested an interrupt every %u frames but got %uc frames for playback",
  357. driver->frames_per_cycle,p_period_size);
  358. return -1;
  359. }
  360. }
  361. if (driver->capture_handle) {
  362. c_period_size = snd_pcm_hw_params_get_period_size (driver->capture_hw_params, &dir);
  363. c_nfragments = snd_pcm_hw_params_get_periods (driver->capture_hw_params, &dir);
  364. driver->capture_sample_format = (snd_pcm_format_t) snd_pcm_hw_params_get_format (driver->capture_hw_params);
  365. driver->capture_interleaved = (snd_pcm_hw_params_get_access (driver->capture_hw_params) == SND_PCM_ACCESS_MMAP_INTERLEAVED);
  366. if (c_period_size != driver->frames_per_cycle) {
  367. jack_error ("alsa_pcm: requested an interrupt every %u frames but got %uc frames for capture",
  368. driver->frames_per_cycle,p_period_size);
  369. return -1;
  370. }
  371. }
  372. if (driver->capture_handle && driver->playback_handle) {
  373. if (p_nfragments != c_nfragments) {
  374. jack_error ("alsa_pcm: different period counts for playback and capture!");
  375. return -1;
  376. }
  377. driver->nfragments = c_nfragments;
  378. } else if (driver->capture_handle) {
  379. driver->nfragments = c_nfragments;
  380. } else {
  381. driver->nfragments = p_nfragments;
  382. }
  383. driver->buffer_frames = driver->frames_per_cycle * driver->nfragments;
  384. driver->playback_sample_bytes = snd_pcm_format_physical_width (driver->playback_sample_format) / 8;
  385. driver->capture_sample_bytes = snd_pcm_format_physical_width (driver->capture_sample_format) / 8;
  386. switch (driver->playback_sample_format) {
  387. case SND_PCM_FORMAT_S32_LE:
  388. case SND_PCM_FORMAT_S16_LE:
  389. case SND_PCM_FORMAT_S32_BE:
  390. case SND_PCM_FORMAT_S16_BE:
  391. break;
  392. default:
  393. jack_error ("programming error: unhandled format type for playback");
  394. exit (1);
  395. }
  396. switch (driver->capture_sample_format) {
  397. case SND_PCM_FORMAT_S32_LE:
  398. case SND_PCM_FORMAT_S16_LE:
  399. case SND_PCM_FORMAT_S32_BE:
  400. case SND_PCM_FORMAT_S16_BE:
  401. break;
  402. default:
  403. jack_error ("programming error: unhandled format type for capture");
  404. exit (1);
  405. }
  406. if (driver->playback_interleaved) {
  407. const snd_pcm_channel_area_t *my_areas;
  408. snd_pcm_uframes_t offset, frames;
  409. if (snd_pcm_mmap_begin(driver->playback_handle, &my_areas, &offset, &frames) < 0) {
  410. jack_error ("ALSA: %s: mmap areas info error", driver->alsa_name_playback);
  411. return -1;
  412. }
  413. driver->playback_interleave_skip = my_areas[0].step / 8;
  414. driver->interleave_unit = snd_pcm_format_physical_width (driver->playback_sample_format) / 8;
  415. } else {
  416. driver->interleave_unit = 0; /* NOT USED */
  417. driver->playback_interleave_skip = snd_pcm_format_physical_width (driver->playback_sample_format) / 8;
  418. }
  419. if (driver->capture_interleaved) {
  420. const snd_pcm_channel_area_t *my_areas;
  421. snd_pcm_uframes_t offset, frames;
  422. if (snd_pcm_mmap_begin(driver->capture_handle, &my_areas, &offset, &frames) < 0) {
  423. jack_error ("ALSA: %s: mmap areas info error", driver->alsa_name_capture);
  424. return -1;
  425. }
  426. driver->capture_interleave_skip = my_areas[0].step / 8;
  427. } else {
  428. driver->capture_interleave_skip = snd_pcm_format_physical_width (driver->capture_sample_format) / 8;
  429. }
  430. if (driver->playback_nchannels > driver->capture_nchannels) {
  431. driver->max_nchannels = driver->playback_nchannels;
  432. driver->user_nchannels = driver->capture_nchannels;
  433. } else {
  434. driver->max_nchannels = driver->capture_nchannels;
  435. driver->user_nchannels = driver->playback_nchannels;
  436. }
  437. alsa_driver_setup_io_function_pointers (driver);
  438. /* Allocate and initialize structures that rely on
  439. the channels counts.
  440. */
  441. /* set up the bit pattern that is used to record which
  442. channels require action on every cycle. any bits that are
  443. not set after the engine's process() call indicate channels
  444. that potentially need to be silenced.
  445. XXX this is limited to <wordsize> channels. Use a bitset
  446. type instead.
  447. */
  448. driver->channel_done_bits = 0;
  449. if (driver->playback_handle) {
  450. driver->playback_addr = (char **) malloc (sizeof (char *) * driver->playback_nchannels);
  451. memset (driver->playback_addr, 0, sizeof (char *) * driver->playback_nchannels);
  452. driver->silent = (unsigned long *) malloc (sizeof (unsigned long) * driver->playback_nchannels);
  453. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  454. driver->silent[chn] = 0;
  455. }
  456. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  457. driver->channel_done_bits |= (1<<chn);
  458. }
  459. driver->dither_state = (dither_state_t *) calloc ( driver->playback_nchannels, sizeof (dither_state_t));
  460. }
  461. if (driver->capture_handle) {
  462. driver->capture_addr = (char **) malloc (sizeof (char *) * driver->capture_nchannels);
  463. memset (driver->capture_addr, 0, sizeof (char *) * driver->capture_nchannels);
  464. }
  465. driver->clock_sync_data = (ClockSyncStatus *) malloc (sizeof (ClockSyncStatus) *
  466. driver->max_nchannels);
  467. driver->period_usecs = (jack_time_t) floor ((((float) driver->frames_per_cycle) / driver->frame_rate) * 1000000.0f);
  468. driver->poll_timeout = (int) floor (1.5f * driver->period_usecs);
  469. if (driver->engine) {
  470. driver->engine->set_buffer_size (driver->engine, driver->frames_per_cycle);
  471. }
  472. return 0;
  473. }
  474. #if 0
  475. static int /* UNUSED */
  476. alsa_driver_reset_parameters (alsa_driver_t *driver, jack_nframes_t frames_per_cycle, jack_nframes_t user_nperiods, jack_nframes_t rate)
  477. {
  478. /* XXX unregister old ports ? */
  479. alsa_driver_release_channel_dependent_memory (driver);
  480. return alsa_driver_set_parameters (driver, frames_per_cycle, user_nperiods, rate);
  481. }
  482. #endif
  483. static int
  484. alsa_driver_get_channel_addresses (alsa_driver_t *driver,
  485. snd_pcm_uframes_t *capture_avail,
  486. snd_pcm_uframes_t *playback_avail,
  487. snd_pcm_uframes_t *capture_offset,
  488. snd_pcm_uframes_t *playback_offset)
  489. {
  490. unsigned long err;
  491. channel_t chn;
  492. if (capture_avail) {
  493. if ((err = snd_pcm_mmap_begin (driver->capture_handle, &driver->capture_areas,
  494. (snd_pcm_uframes_t *) capture_offset,
  495. (snd_pcm_uframes_t *) capture_avail)) < 0) {
  496. jack_error ("ALSA: %s: mmap areas info error", driver->alsa_name_capture);
  497. return -1;
  498. }
  499. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  500. const snd_pcm_channel_area_t *a = &driver->capture_areas[chn];
  501. driver->capture_addr[chn] = (char *) a->addr + ((a->first + a->step * *capture_offset) / 8);
  502. }
  503. }
  504. if (playback_avail) {
  505. if ((err = snd_pcm_mmap_begin (driver->playback_handle, &driver->playback_areas,
  506. (snd_pcm_uframes_t *) playback_offset,
  507. (snd_pcm_uframes_t *) playback_avail)) < 0) {
  508. jack_error ("ALSA: %s: mmap areas info error ", driver->alsa_name_playback);
  509. return -1;
  510. }
  511. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  512. const snd_pcm_channel_area_t *a = &driver->playback_areas[chn];
  513. driver->playback_addr[chn] = (char *) a->addr + ((a->first + a->step * *playback_offset) / 8);
  514. }
  515. }
  516. return 0;
  517. }
  518. static int
  519. alsa_driver_audio_start (alsa_driver_t *driver)
  520. {
  521. int err;
  522. snd_pcm_uframes_t poffset, pavail;
  523. channel_t chn;
  524. driver->poll_last = 0;
  525. driver->poll_next = 0;
  526. if (driver->playback_handle) {
  527. if ((err = snd_pcm_prepare (driver->playback_handle)) < 0) {
  528. jack_error ("ALSA: prepare error for playback on \"%s\" (%s)", driver->alsa_name_playback, snd_strerror(err));
  529. return -1;
  530. }
  531. }
  532. if (driver->capture_handle && driver->capture_and_playback_not_synced) {
  533. if ((err = snd_pcm_prepare (driver->capture_handle)) < 0) {
  534. jack_error ("ALSA: prepare error for capture on \"%s\" (%s)", driver->alsa_name_capture, snd_strerror(err));
  535. return -1;
  536. }
  537. }
  538. if (driver->hw_monitoring) {
  539. driver->hw->set_input_monitor_mask (driver->hw, driver->input_monitor_mask);
  540. }
  541. if (driver->playback_handle) {
  542. /* fill playback buffer with zeroes, and mark
  543. all fragments as having data.
  544. */
  545. pavail = snd_pcm_avail_update (driver->playback_handle);
  546. if (pavail != driver->buffer_frames) {
  547. jack_error ("ALSA: full buffer not available at start");
  548. return -1;
  549. }
  550. if (alsa_driver_get_channel_addresses (driver, 0, &pavail, 0, &poffset)) {
  551. return -1;
  552. }
  553. /* XXX this is cheating. ALSA offers no guarantee that we can access
  554. the entire buffer at any one time. It works on most hardware
  555. tested so far, however, buts its a liability in the long run. I think
  556. that alsa-lib may have a better function for doing this here, where
  557. the goal is to silence the entire buffer.
  558. */
  559. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  560. alsa_driver_silence_on_channel (driver, chn, driver->buffer_frames);
  561. }
  562. snd_pcm_mmap_commit (driver->playback_handle, poffset, driver->buffer_frames);
  563. if ((err = snd_pcm_start (driver->playback_handle)) < 0) {
  564. jack_error ("could not start playback (%s)", snd_strerror (err));
  565. return -1;
  566. }
  567. }
  568. if (driver->capture_handle && driver->capture_and_playback_not_synced) {
  569. if ((err = snd_pcm_start (driver->capture_handle)) < 0) {
  570. jack_error ("could not start capture (%s)", snd_strerror (err));
  571. return -1;
  572. }
  573. }
  574. if (driver->hw_monitoring && (driver->input_monitor_mask || driver->all_monitor_in)) {
  575. if (driver->all_monitor_in) {
  576. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  577. } else {
  578. driver->hw->set_input_monitor_mask (driver->hw, driver->input_monitor_mask);
  579. }
  580. }
  581. if (driver->playback_handle) {
  582. driver->playback_nfds = snd_pcm_poll_descriptors_count (driver->playback_handle);
  583. } else {
  584. driver->playback_nfds = 0;
  585. }
  586. if (driver->capture_handle) {
  587. driver->capture_nfds = snd_pcm_poll_descriptors_count (driver->capture_handle);
  588. } else {
  589. driver->capture_nfds = 0;
  590. }
  591. if (driver->pfd)
  592. free (driver->pfd);
  593. driver->pfd = (struct pollfd *) malloc (sizeof (struct pollfd) *
  594. (driver->playback_nfds + driver->capture_nfds + 2));
  595. return 0;
  596. }
  597. static int
  598. alsa_driver_audio_stop (alsa_driver_t *driver)
  599. {
  600. int err;
  601. if (driver->playback_handle) {
  602. if ((err = snd_pcm_drop (driver->playback_handle)) < 0) {
  603. jack_error ("alsa_pcm: channel flush for playback failed (%s)", snd_strerror (err));
  604. return -1;
  605. }
  606. }
  607. if (!driver->playback_handle || driver->capture_and_playback_not_synced) {
  608. if (driver->capture_handle) {
  609. if ((err = snd_pcm_drop (driver->capture_handle)) < 0) {
  610. jack_error ("alsa_pcm: channel flush for capture failed (%s)", snd_strerror (err));
  611. return -1;
  612. }
  613. }
  614. }
  615. if (driver->hw_monitoring) {
  616. driver->hw->set_input_monitor_mask (driver->hw, 0);
  617. }
  618. return 0;
  619. }
  620. static int
  621. alsa_driver_xrun_recovery (alsa_driver_t *driver)
  622. {
  623. snd_pcm_status_t *status;
  624. int res;
  625. snd_pcm_status_alloca(&status);
  626. if (driver->capture_handle) {
  627. if ((res = snd_pcm_status(driver->capture_handle, status)) < 0) {
  628. jack_error("status error: %s", snd_strerror(res));
  629. }
  630. } else {
  631. if ((res = snd_pcm_status(driver->playback_handle, status)) < 0) {
  632. jack_error("status error: %s", snd_strerror(res));
  633. }
  634. }
  635. if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN && driver->xrun_count == 0 && driver->process_count > XRUN_REPORT_DELAY) {
  636. struct timeval now, diff, tstamp;
  637. driver->xrun_count++;
  638. gettimeofday(&now, 0);
  639. snd_pcm_status_get_trigger_tstamp(status, &tstamp);
  640. timersub(&now, &tstamp, &diff);
  641. fprintf(stderr, "\n\n**** alsa_pcm: xrun of at least %.3f msecs\n\n", diff.tv_sec * 1000 + diff.tv_usec / 1000.0);
  642. }
  643. if (alsa_driver_audio_stop (driver) || alsa_driver_audio_start (driver)) {
  644. return -1;
  645. }
  646. return 0;
  647. }
  648. static void
  649. alsa_driver_silence_untouched_channels (alsa_driver_t *driver, jack_nframes_t nframes)
  650. {
  651. channel_t chn;
  652. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  653. if ((driver->channels_not_done & (1<<chn))) {
  654. if (driver->silent[chn] < driver->buffer_frames) {
  655. alsa_driver_silence_on_channel_no_mark (driver, chn, nframes);
  656. driver->silent[chn] += nframes;
  657. }
  658. }
  659. }
  660. }
  661. void
  662. alsa_driver_set_clock_sync_status (alsa_driver_t *driver, channel_t chn, ClockSyncStatus status)
  663. {
  664. driver->clock_sync_data[chn] = status;
  665. alsa_driver_clock_sync_notify (driver, chn, status);
  666. }
  667. static int under_gdb = FALSE;
  668. static jack_nframes_t
  669. alsa_driver_wait (alsa_driver_t *driver, int extra_fd, int *status, float *delayed_usecs)
  670. {
  671. snd_pcm_sframes_t avail = 0;
  672. snd_pcm_sframes_t capture_avail = 0;
  673. snd_pcm_sframes_t playback_avail = 0;
  674. int xrun_detected = FALSE;
  675. int need_capture;
  676. int need_playback;
  677. unsigned int i;
  678. jack_time_t poll_enter;
  679. jack_time_t poll_ret = 0;
  680. *status = -1;
  681. *delayed_usecs = 0;
  682. need_capture = driver->capture_handle ? 1 : 0;
  683. if (extra_fd >= 0) {
  684. need_playback = 0;
  685. } else {
  686. need_playback = driver->playback_handle ? 1 : 0;
  687. }
  688. again:
  689. while (need_playback || need_capture) {
  690. unsigned int p_timed_out, c_timed_out;
  691. unsigned int ci = 0;
  692. unsigned int nfds;
  693. nfds = 0;
  694. if (need_playback) {
  695. snd_pcm_poll_descriptors (driver->playback_handle, &driver->pfd[0], driver->playback_nfds);
  696. nfds += driver->playback_nfds;
  697. }
  698. if (need_capture) {
  699. snd_pcm_poll_descriptors (driver->capture_handle, &driver->pfd[nfds], driver->capture_nfds);
  700. ci = nfds;
  701. nfds += driver->capture_nfds;
  702. }
  703. /* ALSA doesn't set POLLERR in some versions of 0.9.X */
  704. for (i = 0; i < nfds; i++) {
  705. driver->pfd[i].events |= POLLERR;
  706. }
  707. if (extra_fd >= 0) {
  708. driver->pfd[nfds].fd = extra_fd;
  709. driver->pfd[nfds].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  710. nfds++;
  711. }
  712. poll_enter = jack_get_microseconds ();
  713. if (poll (driver->pfd, nfds, driver->poll_timeout) < 0) {
  714. if (errno == EINTR) {
  715. printf ("poll interrupt\n");
  716. // this happens mostly when run
  717. // under gdb, or when exiting due to a signal
  718. if (under_gdb) {
  719. goto again;
  720. }
  721. *status = -2;
  722. return 0;
  723. }
  724. jack_error ("ALSA: poll call failed (%s)", strerror (errno));
  725. *status = -3;
  726. return 0;
  727. }
  728. poll_ret = jack_get_microseconds ();
  729. if (extra_fd < 0) {
  730. if (driver->poll_next && poll_ret > driver->poll_next) {
  731. *delayed_usecs = poll_ret - driver->poll_next;
  732. }
  733. driver->poll_last = poll_ret;
  734. driver->poll_next = poll_ret + driver->period_usecs;
  735. driver->engine->transport_cycle_start (driver->engine,
  736. poll_ret);
  737. }
  738. #ifdef DEBUG_WAKEUP
  739. fprintf (stderr, "%Lu: checked %d fds, %Lu usecs since poll entered\n",
  740. poll_ret,
  741. nfds,
  742. poll_ret - poll_enter);
  743. #endif
  744. /* check to see if it was the extra FD that caused us to return from poll
  745. */
  746. if (extra_fd >= 0) {
  747. if (driver->pfd[nfds-1].revents == 0) {
  748. /* we timed out on the extra fd */
  749. *status = -4;
  750. return -1;
  751. }
  752. /* if POLLIN was the only bit set, we're OK */
  753. *status = 0;
  754. return (driver->pfd[nfds-1].revents == POLLIN) ? 0 : -1;
  755. }
  756. p_timed_out = 0;
  757. if (need_playback) {
  758. for (i = 0; i < driver->playback_nfds; i++) {
  759. if (driver->pfd[i].revents & POLLERR) {
  760. xrun_detected = TRUE;
  761. }
  762. if (driver->pfd[i].revents == 0) {
  763. p_timed_out++;
  764. #ifdef DEBUG_WAKEUP
  765. fprintf (stderr, "%Lu playback stream timed out\n", poll_ret);
  766. #endif
  767. }
  768. }
  769. if (p_timed_out == 0) {
  770. need_playback = 0;
  771. #ifdef DEBUG_WAKEUP
  772. fprintf (stderr, "%Lu playback stream ready\n", poll_ret);
  773. #endif
  774. }
  775. }
  776. c_timed_out = 0;
  777. if (need_capture) {
  778. for (i = ci; i < nfds; i++) {
  779. if (driver->pfd[i].revents & POLLERR) {
  780. xrun_detected = TRUE;
  781. }
  782. if (driver->pfd[i].revents == 0) {
  783. c_timed_out++;
  784. #ifdef DEBUG_WAKEUP
  785. fprintf (stderr, "%Lu capture stream timed out\n", poll_ret);
  786. #endif
  787. }
  788. }
  789. if (c_timed_out == 0) {
  790. need_capture = 0;
  791. #ifdef DEBUG_WAKEUP
  792. fprintf (stderr, "%Lu capture stream ready\n", poll_ret);
  793. #endif
  794. }
  795. }
  796. if ((p_timed_out && (p_timed_out == driver->playback_nfds)) &&
  797. (c_timed_out && (c_timed_out == driver->capture_nfds))){
  798. jack_error ("ALSA: poll time out, polled for %Lu usecs", poll_ret - poll_enter);
  799. *status = -5;
  800. return 0;
  801. }
  802. }
  803. if (driver->capture_handle) {
  804. if ((capture_avail = snd_pcm_avail_update (driver->capture_handle)) < 0) {
  805. if (capture_avail == -EPIPE) {
  806. xrun_detected = TRUE;
  807. } else {
  808. jack_error ("unknown ALSA avail_update return value (%u)", capture_avail);
  809. }
  810. }
  811. } else {
  812. capture_avail = INT_MAX; /* odd, but see min() computation below */
  813. }
  814. if (driver->playback_handle) {
  815. if ((playback_avail = snd_pcm_avail_update (driver->playback_handle)) < 0) {
  816. if (playback_avail == -EPIPE) {
  817. xrun_detected = TRUE;
  818. } else {
  819. jack_error ("unknown ALSA avail_update return value (%u)", playback_avail);
  820. }
  821. }
  822. } else {
  823. playback_avail = INT_MAX; /* odd, but see min() computation below */
  824. }
  825. if (xrun_detected) {
  826. *status = alsa_driver_xrun_recovery (driver);
  827. return 0;
  828. }
  829. *status = 0;
  830. driver->last_wait_ust = poll_ret;
  831. avail = capture_avail < playback_avail ? capture_avail : playback_avail;
  832. #ifdef DEBUG_WAKEUP
  833. fprintf (stderr, "wakup complete, avail = %lu, pavail = %lu cavail = %lu\n",
  834. avail, playback_avail, capture_avail);
  835. #endif
  836. /* mark all channels not done for now. read/write will change this */
  837. driver->channels_not_done = driver->channel_done_bits;
  838. /* constrain the available count to the nearest (round down) number of
  839. periods.
  840. */
  841. return avail - (avail % driver->frames_per_cycle);
  842. }
  843. static int
  844. alsa_driver_null_cycle (alsa_driver_t* driver, jack_nframes_t nframes)
  845. {
  846. jack_nframes_t nf;
  847. snd_pcm_uframes_t offset;
  848. snd_pcm_uframes_t contiguous;
  849. int chn;
  850. if (driver->capture_handle) {
  851. nf = nframes;
  852. offset = 0;
  853. while (nf) {
  854. contiguous = (nf > driver->frames_per_cycle) ? driver->frames_per_cycle : nf;
  855. if (snd_pcm_mmap_begin (driver->capture_handle, &driver->capture_areas,
  856. (snd_pcm_uframes_t *) &offset,
  857. (snd_pcm_uframes_t *) &contiguous)) {
  858. return -1;
  859. }
  860. if (snd_pcm_mmap_commit (driver->capture_handle, offset, contiguous) < 0) {
  861. return -1;
  862. }
  863. nf -= contiguous;
  864. }
  865. }
  866. if (driver->playback_handle) {
  867. nf = nframes;
  868. offset = 0;
  869. while (nf) {
  870. contiguous = (nf > driver->frames_per_cycle) ? driver->frames_per_cycle : nf;
  871. if (snd_pcm_mmap_begin (driver->playback_handle, &driver->playback_areas,
  872. (snd_pcm_uframes_t *) &offset,
  873. (snd_pcm_uframes_t *) &contiguous)) {
  874. return -1;
  875. }
  876. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  877. alsa_driver_silence_on_channel (driver, chn, contiguous);
  878. }
  879. if (snd_pcm_mmap_commit (driver->playback_handle, offset, contiguous) < 0) {
  880. return -1;
  881. }
  882. nf -= contiguous;
  883. }
  884. }
  885. return 0;
  886. }
  887. static int
  888. alsa_driver_read (alsa_driver_t *driver, jack_nframes_t nframes)
  889. {
  890. snd_pcm_sframes_t contiguous;
  891. snd_pcm_sframes_t nread;
  892. snd_pcm_sframes_t offset;
  893. jack_default_audio_sample_t* buf;
  894. channel_t chn;
  895. JSList *node;
  896. jack_port_t* port;
  897. if (!driver->capture_handle) {
  898. return 0;
  899. }
  900. nread = 0;
  901. contiguous = 0;
  902. while (nframes) {
  903. contiguous = (nframes > driver->frames_per_cycle) ? driver->frames_per_cycle : nframes;
  904. if (alsa_driver_get_channel_addresses (driver,
  905. (snd_pcm_uframes_t *) &contiguous,
  906. (snd_pcm_uframes_t *) 0,
  907. &offset, 0) < 0) {
  908. return -1;
  909. }
  910. for (chn = 0, node = driver->capture_ports; node; node = jack_slist_next (node), chn++) {
  911. port = (jack_port_t *) node->data;
  912. if (!jack_port_connected (port)) {
  913. /* no-copy optimization */
  914. continue;
  915. }
  916. buf = jack_port_get_buffer (port, nframes);
  917. alsa_driver_read_from_channel (driver, chn, buf + nread, contiguous);
  918. }
  919. if (snd_pcm_mmap_commit (driver->capture_handle, offset, contiguous) < 0) {
  920. jack_error ("alsa_pcm: could not complete read of %lu frames\n", contiguous);
  921. return -1;
  922. }
  923. nframes -= contiguous;
  924. }
  925. return 0;
  926. }
  927. static int
  928. alsa_driver_write (alsa_driver_t* driver, jack_nframes_t nframes)
  929. {
  930. channel_t chn;
  931. JSList *node;
  932. jack_default_audio_sample_t* buf;
  933. snd_pcm_sframes_t nwritten;
  934. snd_pcm_sframes_t contiguous;
  935. snd_pcm_sframes_t offset;
  936. jack_port_t *port;
  937. int err;
  938. driver->process_count++;
  939. if (!driver->playback_handle) {
  940. return 0;
  941. }
  942. nwritten = 0;
  943. contiguous = 0;
  944. /* check current input monitor request status */
  945. driver->input_monitor_mask = 0;
  946. for (chn = 0, node = driver->capture_ports; node; node = jack_slist_next (node), chn++) {
  947. if (((jack_port_t *) node->data)->shared->monitor_requests) {
  948. driver->input_monitor_mask |= (1<<chn);
  949. }
  950. }
  951. if (driver->hw_monitoring) {
  952. if ((driver->hw->input_monitor_mask != driver->input_monitor_mask) && !driver->all_monitor_in) {
  953. driver->hw->set_input_monitor_mask (driver->hw, driver->input_monitor_mask);
  954. }
  955. }
  956. while (nframes) {
  957. contiguous = (nframes > driver->frames_per_cycle) ? driver->frames_per_cycle : nframes;
  958. if (alsa_driver_get_channel_addresses (driver,
  959. (snd_pcm_uframes_t *) 0,
  960. (snd_pcm_uframes_t *) &contiguous,
  961. 0, &offset) < 0) {
  962. return -1;
  963. }
  964. for (chn = 0, node = driver->playback_ports; node; node = jack_slist_next (node), chn++) {
  965. port = (jack_port_t *) node->data;
  966. if (!jack_port_connected (port)) {
  967. continue;
  968. }
  969. buf = jack_port_get_buffer (port, contiguous);
  970. alsa_driver_write_to_channel (driver, chn, buf + nwritten, contiguous);
  971. }
  972. if (driver->channels_not_done) {
  973. alsa_driver_silence_untouched_channels (driver, contiguous);
  974. }
  975. if ((err = snd_pcm_mmap_commit (driver->playback_handle, offset, contiguous)) < 0) {
  976. jack_error ("could not complete playback of %lu frames: error = %d", contiguous, err);
  977. if (err != -EPIPE && err != -ESTRPIPE)
  978. return -1;
  979. }
  980. nframes -= contiguous;
  981. }
  982. return 0;
  983. }
  984. static int
  985. alsa_driver_attach (alsa_driver_t *driver, jack_engine_t *engine)
  986. {
  987. char buf[32];
  988. channel_t chn;
  989. jack_port_t *port;
  990. int port_flags;
  991. driver->engine = engine;
  992. driver->engine->set_buffer_size (engine, driver->frames_per_cycle);
  993. driver->engine->set_sample_rate (engine, driver->frame_rate);
  994. port_flags = JackPortIsOutput|JackPortIsPhysical|JackPortIsTerminal;
  995. if (driver->has_hw_monitoring) {
  996. port_flags |= JackPortCanMonitor;
  997. }
  998. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  999. snprintf (buf, sizeof(buf) - 1, "capture_%lu", chn+1);
  1000. if ((port = jack_port_register (driver->client, buf, JACK_DEFAULT_AUDIO_TYPE, port_flags, 0)) == NULL) {
  1001. jack_error ("ALSA: cannot register port for %s", buf);
  1002. break;
  1003. }
  1004. if (driver->hw_metering) {
  1005. jack_port_set_peak_function(port, driver->hw->get_hardware_peak);
  1006. jack_port_set_power_function(port, driver->hw->get_hardware_power);
  1007. }
  1008. /* XXX fix this so that it can handle: systemic (external) latency
  1009. */
  1010. jack_port_set_latency (port, driver->frames_per_cycle);
  1011. driver->capture_ports = jack_slist_append (driver->capture_ports, port);
  1012. }
  1013. port_flags = JackPortIsInput|JackPortIsPhysical|JackPortIsTerminal;
  1014. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  1015. jack_port_t *monitor_port;
  1016. snprintf (buf, sizeof(buf) - 1, "playback_%lu", chn+1);
  1017. if ((port = jack_port_register (driver->client, buf, JACK_DEFAULT_AUDIO_TYPE, port_flags, 0)) == NULL) {
  1018. jack_error ("ALSA: cannot register port for %s", buf);
  1019. break;
  1020. }
  1021. if (driver->hw_metering) {
  1022. jack_port_set_peak_function(port, driver->hw->get_hardware_peak);
  1023. jack_port_set_power_function(port, driver->hw->get_hardware_power);
  1024. }
  1025. /* XXX fix this so that it can handle: systemic (external) latency
  1026. */
  1027. jack_port_set_latency (port, driver->frames_per_cycle * driver->nfragments);
  1028. driver->playback_ports = jack_slist_append (driver->playback_ports, port);
  1029. if (driver->with_monitor_ports) {
  1030. snprintf (buf, sizeof(buf) - 1, "monitor_%lu", chn+1);
  1031. if ((monitor_port = jack_port_register (driver->client, buf, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0)) == NULL) {
  1032. jack_error ("ALSA: cannot register monitor port for %s", buf);
  1033. } else {
  1034. jack_port_tie (port, monitor_port);
  1035. }
  1036. }
  1037. }
  1038. jack_activate (driver->client);
  1039. return 0;
  1040. }
  1041. static void
  1042. alsa_driver_detach (alsa_driver_t *driver, jack_engine_t *engine)
  1043. {
  1044. JSList *node;
  1045. if (driver->engine == 0) {
  1046. return;
  1047. }
  1048. for (node = driver->capture_ports; node; node = jack_slist_next (node)) {
  1049. jack_port_unregister (driver->client, ((jack_port_t *) node->data));
  1050. }
  1051. jack_slist_free (driver->capture_ports);
  1052. driver->capture_ports = 0;
  1053. for (node = driver->playback_ports; node; node = jack_slist_next (node)) {
  1054. jack_port_unregister (driver->client, ((jack_port_t *) node->data));
  1055. }
  1056. jack_slist_free (driver->playback_ports);
  1057. driver->playback_ports = 0;
  1058. driver->engine = 0;
  1059. }
  1060. #if 0
  1061. static int /* UNUSED */
  1062. alsa_driver_change_sample_clock (alsa_driver_t *driver, SampleClockMode mode)
  1063. {
  1064. return driver->hw->change_sample_clock (driver->hw, mode);
  1065. }
  1066. static void /* UNUSED */
  1067. alsa_driver_request_all_monitor_input (alsa_driver_t *driver, int yn)
  1068. {
  1069. if (driver->hw_monitoring) {
  1070. if (yn) {
  1071. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  1072. } else {
  1073. driver->hw->set_input_monitor_mask (driver->hw, driver->input_monitor_mask);
  1074. }
  1075. }
  1076. driver->all_monitor_in = yn;
  1077. }
  1078. static void /* UNUSED */
  1079. alsa_driver_set_hw_monitoring (alsa_driver_t *driver, int yn)
  1080. {
  1081. if (yn) {
  1082. driver->hw_monitoring = TRUE;
  1083. if (driver->all_monitor_in) {
  1084. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  1085. } else {
  1086. driver->hw->set_input_monitor_mask (driver->hw, driver->input_monitor_mask);
  1087. }
  1088. } else {
  1089. driver->hw_monitoring = FALSE;
  1090. driver->hw->set_input_monitor_mask (driver->hw, 0);
  1091. }
  1092. }
  1093. static ClockSyncStatus /* UNUSED */
  1094. alsa_driver_clock_sync_status (channel_t chn)
  1095. {
  1096. return Lock;
  1097. }
  1098. #endif
  1099. static void
  1100. alsa_driver_delete (alsa_driver_t *driver)
  1101. {
  1102. JSList *node;
  1103. for (node = driver->clock_sync_listeners; node; node = jack_slist_next (node)) {
  1104. free (node->data);
  1105. }
  1106. jack_slist_free (driver->clock_sync_listeners);
  1107. if (driver->capture_handle) {
  1108. snd_pcm_close (driver->capture_handle);
  1109. driver->capture_handle = 0;
  1110. }
  1111. if (driver->playback_handle) {
  1112. snd_pcm_close (driver->playback_handle);
  1113. driver->capture_handle = 0;
  1114. }
  1115. if (driver->capture_hw_params) {
  1116. snd_pcm_hw_params_free (driver->capture_hw_params);
  1117. driver->capture_hw_params = 0;
  1118. }
  1119. if (driver->playback_hw_params) {
  1120. snd_pcm_hw_params_free (driver->playback_hw_params);
  1121. driver->playback_hw_params = 0;
  1122. }
  1123. if (driver->capture_sw_params) {
  1124. snd_pcm_sw_params_free (driver->capture_sw_params);
  1125. driver->capture_sw_params = 0;
  1126. }
  1127. if (driver->playback_sw_params) {
  1128. snd_pcm_sw_params_free (driver->playback_sw_params);
  1129. driver->playback_sw_params = 0;
  1130. }
  1131. if (driver->pfd) {
  1132. free (driver->pfd);
  1133. }
  1134. if (driver->hw) {
  1135. driver->hw->release (driver->hw);
  1136. driver->hw = 0;
  1137. }
  1138. free(driver->alsa_name_playback);
  1139. free(driver->alsa_name_capture);
  1140. free(driver->alsa_driver);
  1141. alsa_driver_release_channel_dependent_memory (driver);
  1142. free (driver);
  1143. }
  1144. static jack_driver_t *
  1145. alsa_driver_new (char *name, char *playback_alsa_device, char *capture_alsa_device,
  1146. jack_client_t *client,
  1147. jack_nframes_t frames_per_cycle,
  1148. jack_nframes_t user_nperiods,
  1149. jack_nframes_t rate,
  1150. int hw_monitoring,
  1151. int hw_metering,
  1152. int capturing,
  1153. int playing,
  1154. DitherAlgorithm dither,
  1155. int soft_mode,
  1156. int monitor)
  1157. {
  1158. int err;
  1159. alsa_driver_t *driver;
  1160. printf ("creating alsa driver ... %s|%s|%lu|%lu|%lu|%s|%s|%s\n",
  1161. playback_alsa_device, capture_alsa_device, frames_per_cycle, user_nperiods, rate,
  1162. hw_monitoring ? "hwmon":"nomon", hw_metering ? "hwmeter":"swmeter", soft_mode ? "soft-mode":"rt");
  1163. driver = (alsa_driver_t *) calloc (1, sizeof (alsa_driver_t));
  1164. jack_driver_init ((jack_driver_t *) driver);
  1165. driver->attach = (JackDriverAttachFunction) alsa_driver_attach;
  1166. driver->detach = (JackDriverDetachFunction) alsa_driver_detach;
  1167. driver->wait = (JackDriverWaitFunction) alsa_driver_wait;
  1168. driver->read = (JackDriverReadFunction) alsa_driver_read;
  1169. driver->write = (JackDriverReadFunction) alsa_driver_write;
  1170. driver->null_cycle = (JackDriverNullCycleFunction) alsa_driver_null_cycle;
  1171. driver->start = (JackDriverStartFunction) alsa_driver_audio_start;
  1172. driver->stop = (JackDriverStopFunction) alsa_driver_audio_stop;
  1173. driver->playback_handle = NULL;
  1174. driver->capture_handle = NULL;
  1175. driver->ctl_handle = 0;
  1176. driver->hw = 0;
  1177. driver->capture_and_playback_not_synced = FALSE;
  1178. driver->nfragments = 0;
  1179. driver->max_nchannels = 0;
  1180. driver->user_nchannels = 0;
  1181. driver->playback_nchannels = 0;
  1182. driver->capture_nchannels = 0;
  1183. driver->playback_addr = 0;
  1184. driver->capture_addr = 0;
  1185. driver->silent = 0;
  1186. driver->all_monitor_in = FALSE;
  1187. driver->with_monitor_ports = monitor;
  1188. driver->clock_mode = ClockMaster; /* XXX is it? */
  1189. driver->input_monitor_mask = 0; /* XXX is it? */
  1190. driver->capture_ports = 0;
  1191. driver->playback_ports = 0;
  1192. driver->pfd = 0;
  1193. driver->playback_nfds = 0;
  1194. driver->capture_nfds = 0;
  1195. driver->dither = dither;
  1196. driver->soft_mode = soft_mode;
  1197. pthread_mutex_init (&driver->clock_sync_lock, 0);
  1198. driver->clock_sync_listeners = 0;
  1199. driver->xrun_count = 0;
  1200. driver->process_count = 0;
  1201. if (playing) {
  1202. if (snd_pcm_open (&driver->playback_handle, playback_alsa_device, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) {
  1203. if (errno == -EBUSY) {
  1204. jack_error ("the playback device \"%s\" is already in use. Please stop the application using it and "
  1205. "run JACK again", playback_alsa_device);
  1206. return NULL;
  1207. }
  1208. driver->playback_handle = NULL;
  1209. }
  1210. snd_pcm_nonblock (driver->playback_handle, 0);
  1211. }
  1212. if (capturing) {
  1213. if (snd_pcm_open (&driver->capture_handle, capture_alsa_device, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK) < 0) {
  1214. if (errno == -EBUSY) {
  1215. jack_error ("the capture device \"%s\" is already in use. Please stop the application using it and "
  1216. "run JACK again", capture_alsa_device);
  1217. return NULL;
  1218. }
  1219. driver->capture_handle = NULL;
  1220. }
  1221. snd_pcm_nonblock (driver->capture_handle, 0);
  1222. }
  1223. fprintf (stderr, "open\n");
  1224. if (driver->playback_handle == NULL) {
  1225. if (playing) {
  1226. /* they asked for playback, but we can't do it */
  1227. jack_error ("ALSA: Cannot open PCM device %s for playback. Falling back to capture-only mode",
  1228. name);
  1229. if (driver->capture_handle == NULL) {
  1230. /* can't do anything */
  1231. free (driver);
  1232. return 0;
  1233. }
  1234. playing = FALSE;
  1235. }
  1236. }
  1237. if (driver->capture_handle == NULL) {
  1238. if (capturing) {
  1239. /* they asked for capture, but we can't do it */
  1240. jack_error ("ALSA: Cannot open PCM device %s for capture. Falling back to playback-only mode",
  1241. name);
  1242. if (driver->playback_handle == NULL) {
  1243. /* can't do anything */
  1244. free (driver);
  1245. return 0;
  1246. }
  1247. capturing = FALSE;
  1248. }
  1249. }
  1250. driver->alsa_name_playback = strdup (playback_alsa_device);
  1251. driver->alsa_name_capture = strdup (capture_alsa_device);
  1252. if (alsa_driver_check_card_type (driver)) {
  1253. if (driver->capture_handle) {
  1254. snd_pcm_close (driver->capture_handle);
  1255. }
  1256. if (driver->playback_handle) {
  1257. snd_pcm_close (driver->playback_handle);
  1258. }
  1259. free (driver);
  1260. return 0;
  1261. }
  1262. driver->playback_hw_params = 0;
  1263. driver->capture_hw_params = 0;
  1264. driver->playback_sw_params = 0;
  1265. driver->capture_sw_params = 0;
  1266. if (driver->playback_handle) {
  1267. if ((err = snd_pcm_hw_params_malloc (&driver->playback_hw_params)) < 0) {
  1268. jack_error ("ALSA: could no allocate playback hw params structure");
  1269. alsa_driver_delete (driver);
  1270. return 0;
  1271. }
  1272. if ((err = snd_pcm_sw_params_malloc (&driver->playback_sw_params)) < 0) {
  1273. jack_error ("ALSA: could no allocate playback sw params structure");
  1274. alsa_driver_delete (driver);
  1275. return 0;
  1276. }
  1277. }
  1278. if (driver->capture_handle) {
  1279. if ((err = snd_pcm_hw_params_malloc (&driver->capture_hw_params)) < 0) {
  1280. jack_error ("ALSA: could no allocate capture hw params structure");
  1281. alsa_driver_delete (driver);
  1282. return 0;
  1283. }
  1284. if ((err = snd_pcm_sw_params_malloc (&driver->capture_sw_params)) < 0) {
  1285. jack_error ("ALSA: could no allocate capture sw params structure");
  1286. alsa_driver_delete (driver);
  1287. return 0;
  1288. }
  1289. }
  1290. if (alsa_driver_set_parameters (driver, frames_per_cycle, user_nperiods, rate)) {
  1291. alsa_driver_delete (driver);
  1292. return 0;
  1293. }
  1294. driver->capture_and_playback_not_synced = FALSE;
  1295. if (driver->capture_handle && driver->playback_handle) {
  1296. if (snd_pcm_link (driver->capture_handle, driver->playback_handle) != 0) {
  1297. driver->capture_and_playback_not_synced = TRUE;
  1298. }
  1299. }
  1300. alsa_driver_hw_specific (driver, hw_monitoring, hw_metering);
  1301. driver->client = client;
  1302. return (jack_driver_t *) driver;
  1303. }
  1304. int
  1305. alsa_driver_listen_for_clock_sync_status (alsa_driver_t *driver,
  1306. ClockSyncListenerFunction func,
  1307. void *arg)
  1308. {
  1309. ClockSyncListener *csl;
  1310. csl = (ClockSyncListener *) malloc (sizeof (ClockSyncListener));
  1311. csl->function = func;
  1312. csl->arg = arg;
  1313. csl->id = driver->next_clock_sync_listener_id++;
  1314. pthread_mutex_lock (&driver->clock_sync_lock);
  1315. driver->clock_sync_listeners = jack_slist_prepend (driver->clock_sync_listeners, csl);
  1316. pthread_mutex_unlock (&driver->clock_sync_lock);
  1317. return csl->id;
  1318. }
  1319. int
  1320. alsa_driver_stop_listening_to_clock_sync_status (alsa_driver_t *driver, unsigned int which)
  1321. {
  1322. JSList *node;
  1323. int ret = -1;
  1324. pthread_mutex_lock (&driver->clock_sync_lock);
  1325. for (node = driver->clock_sync_listeners; node; node = jack_slist_next (node)) {
  1326. if (((ClockSyncListener *) node->data)->id == which) {
  1327. driver->clock_sync_listeners = jack_slist_remove_link (driver->clock_sync_listeners, node);
  1328. free (node->data);
  1329. jack_slist_free_1 (node);
  1330. ret = 0;
  1331. break;
  1332. }
  1333. }
  1334. pthread_mutex_unlock (&driver->clock_sync_lock);
  1335. return ret;
  1336. }
  1337. void
  1338. alsa_driver_clock_sync_notify (alsa_driver_t *driver, channel_t chn, ClockSyncStatus status)
  1339. {
  1340. JSList *node;
  1341. pthread_mutex_lock (&driver->clock_sync_lock);
  1342. for (node = driver->clock_sync_listeners; node; node = jack_slist_next (node)) {
  1343. ClockSyncListener *csl = (ClockSyncListener *) node->data;
  1344. csl->function (chn, status, csl->arg);
  1345. }
  1346. pthread_mutex_unlock (&driver->clock_sync_lock);
  1347. }
  1348. static void
  1349. alsa_usage ()
  1350. {
  1351. fprintf (stderr, "\n"
  1352. "ALSA driver arguments:\n"
  1353. " -h,--help \tprint this message\n"
  1354. " -d,--device <name> \tALSA device name (default: \"default\")\n"
  1355. " -r,--rate <n> \tsample rate (default: 48000)\n"
  1356. " -p,--period <n> \tframes per period (default: 1024)\n"
  1357. " -n,--nperiods <n> \tnumber of periods in hardware buffer (default: 2)\n"
  1358. " -H,--hwmon \tuse hardware monitoring, if available (default: no)\n"
  1359. " -M,--hwmeter \tuse hardware metering, if available (default: no)\n"
  1360. " -D,--duplex \tduplex I/O (default: yes)\n"
  1361. " -C,--capture [name] \tcapture input and optionally set the capture device (default: duplex)\n"
  1362. " -P,--playback [name] \tplayback output and optionally set the playback device (default: duplex)\n"
  1363. " -s,--softmode\tsoft-mode, no xrun handling (default: off)\n"
  1364. " -m,--monitor \tprovide monitor ports for the output (default: off)\n"
  1365. " -z,--dither \tdithering mode:\n"
  1366. " -zn,--dither=none (off, the default)\n"
  1367. " -zr,--dither=rectangular\n"
  1368. " -zs,--dither=shaped\n"
  1369. " -zt,--dither=triangular\n"
  1370. "\n");
  1371. }
  1372. static void
  1373. alsa_error (char *type, char *value)
  1374. {
  1375. fprintf (stderr, "ALSA driver: unknown %s: `%s'\n", type, value);
  1376. alsa_usage();
  1377. }
  1378. static int
  1379. dither_opt (char c, DitherAlgorithm* dither)
  1380. {
  1381. switch (*optarg) {
  1382. case '-':
  1383. case 'n':
  1384. *dither = None;
  1385. break;
  1386. case 'r':
  1387. *dither = Rectangular;
  1388. break;
  1389. case 's':
  1390. *dither = Shaped;
  1391. break;
  1392. case 't':
  1393. *dither = Triangular;
  1394. break;
  1395. default:
  1396. alsa_error ("illegal dithering mode", "");
  1397. return -1;
  1398. }
  1399. return 0;
  1400. }
  1401. /* DRIVER "PLUGIN" INTERFACE */
  1402. const char driver_client_name[] = "alsa_pcm";
  1403. jack_driver_t *
  1404. driver_initialize (jack_client_t *client, int argc, char **argv)
  1405. {
  1406. jack_nframes_t srate = 48000;
  1407. jack_nframes_t frames_per_interrupt = 1024;
  1408. unsigned long user_nperiods = 2;
  1409. char *playback_pcm_name = "default";
  1410. char *capture_pcm_name = "default";
  1411. int hw_monitoring = FALSE;
  1412. int hw_metering = FALSE;
  1413. int capture = FALSE;
  1414. int playback = FALSE;
  1415. int soft_mode = FALSE;
  1416. int monitor = FALSE;
  1417. DitherAlgorithm dither = None;
  1418. int opt;
  1419. char *envvar;
  1420. char optstring[2]; /* string made from opt char */
  1421. struct option long_options[] =
  1422. {
  1423. { "capture", 2, NULL, 'C' },
  1424. { "duplex", 0, NULL, 'D' },
  1425. { "device", 1, NULL, 'd' },
  1426. { "hwmon", 0, NULL, 'H' },
  1427. { "hwmeter", 0, NULL, 'M' },
  1428. { "help", 0, NULL, 'h' },
  1429. { "playback", 2, NULL, 'P' },
  1430. { "period", 1, NULL, 'p' },
  1431. { "rate", 1, NULL, 'r' },
  1432. { "nperiods", 1, NULL, 'n' },
  1433. { "softmode", 1, NULL, 's' },
  1434. { "dither", 2, NULL, 'z' },
  1435. { "monitor", 0, NULL, 'm' },
  1436. { 0, 0, 0, 0 }
  1437. };
  1438. /* before we do anything else, see if there are environment variables
  1439. for each parameter.
  1440. */
  1441. if ((envvar = getenv ("JACK_ALSA_DEVICE")) != NULL) {
  1442. playback_pcm_name = envvar;
  1443. capture_pcm_name = envvar;
  1444. }
  1445. if ((envvar = getenv ("JACK_ALSA_HWMON")) != NULL) {
  1446. hw_monitoring = TRUE;
  1447. }
  1448. if ((envvar = getenv ("JACK_ALSA_SOFTMODE")) != NULL) {
  1449. soft_mode = TRUE;
  1450. }
  1451. if ((envvar = getenv ("JACK_ALSA_PERIOD_FRAMES")) != NULL) {
  1452. frames_per_interrupt = atoi (envvar);
  1453. }
  1454. if ((envvar = getenv ("JACK_ALSA_PERIODS")) != NULL) {
  1455. user_nperiods = atoi (envvar);
  1456. }
  1457. if ((envvar = getenv ("JACK_ALSA_SRATE")) != NULL) {
  1458. srate = atoi (envvar);
  1459. }
  1460. if ((envvar = getenv ("JACK_ALSA_DITHER")) != NULL) {
  1461. if (dither_opt (*envvar, &dither)) {
  1462. return NULL;
  1463. }
  1464. }
  1465. if ((envvar = getenv ("JACK_ALSA_CAPTURE")) != NULL) {
  1466. capture = atoi (envvar);
  1467. }
  1468. if ((envvar = getenv ("JACK_ALSA_PLAYBACK")) != NULL) {
  1469. playback = atoi (envvar);
  1470. }
  1471. if ((envvar = getenv ("JACK_ALSA_MONITOR")) != NULL) {
  1472. monitor = atoi (envvar);
  1473. }
  1474. /*
  1475. * Setting optind back to zero is a hack to reinitialize a new
  1476. * getopts() loop. See declaration in <getopt.h>.
  1477. */
  1478. optind = 0;
  1479. opterr = 0;
  1480. while ((opt = getopt_long(argc, argv, "-C::Dd:HMP::p:r:n:msz::",
  1481. long_options, NULL))
  1482. != EOF) {
  1483. switch (opt) {
  1484. case 'C':
  1485. capture = TRUE;
  1486. if (optarg != NULL) {
  1487. capture_pcm_name = optarg;
  1488. }
  1489. break;
  1490. case 'D':
  1491. capture = TRUE;
  1492. playback = TRUE;
  1493. break;
  1494. case 'd':
  1495. playback_pcm_name = optarg;
  1496. capture_pcm_name = optarg;
  1497. break;
  1498. case 'H':
  1499. hw_monitoring = TRUE;
  1500. break;
  1501. case 'h':
  1502. alsa_usage();
  1503. return NULL;
  1504. case 'm':
  1505. monitor = TRUE;
  1506. break;
  1507. case 'M':
  1508. hw_metering = TRUE;
  1509. break;
  1510. case 'P':
  1511. playback = TRUE;
  1512. if (optarg != NULL) {
  1513. playback_pcm_name = optarg;
  1514. }
  1515. break;
  1516. case 'p':
  1517. frames_per_interrupt = atoi(optarg);
  1518. break;
  1519. case 'r':
  1520. srate = atoi(optarg);
  1521. break;
  1522. case 'n':
  1523. user_nperiods = atoi(optarg);
  1524. break;
  1525. case 's':
  1526. soft_mode = TRUE;
  1527. break;
  1528. case 'z':
  1529. if (optarg == NULL) {
  1530. dither = None;
  1531. } else {
  1532. if (dither_opt (*optarg, &dither)) {
  1533. return NULL;
  1534. }
  1535. }
  1536. break;
  1537. /* the rest is error handling: */
  1538. case 1: /* not an option */
  1539. alsa_error("parameter", optarg);
  1540. return NULL;
  1541. default: /* unrecognized option */
  1542. optstring[0] = (char) optopt;
  1543. optstring[1] = '\0';
  1544. alsa_error("option", optstring);
  1545. return NULL;
  1546. }
  1547. }
  1548. /* duplex is the default */
  1549. if (!capture && !playback) {
  1550. capture = TRUE;
  1551. playback = TRUE;
  1552. }
  1553. return alsa_driver_new ("alsa_pcm", playback_pcm_name, capture_pcm_name, client, frames_per_interrupt,
  1554. user_nperiods, srate, hw_monitoring, hw_metering, capture,
  1555. playback, dither, soft_mode, monitor);
  1556. }
  1557. void
  1558. driver_finish (jack_driver_t *driver)
  1559. {
  1560. alsa_driver_delete ((alsa_driver_t *) driver);
  1561. }