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.

1306 lines
35KB

  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 <glib.h>
  23. #include <stdarg.h>
  24. #include <asm/msr.h>
  25. #include <jack/alsa_driver.h>
  26. #include <jack/types.h>
  27. #include <jack/internal.h>
  28. #include <jack/engine.h>
  29. #include <jack/hammerfall.h>
  30. #include <jack/generic.h>
  31. static void
  32. alsa_driver_release_channel_dependent_memory (alsa_driver_t *driver)
  33. {
  34. if (driver->playback_addr) {
  35. free (driver->playback_addr);
  36. driver->playback_addr = 0;
  37. }
  38. if (driver->capture_addr) {
  39. free (driver->capture_addr);
  40. driver->capture_addr = 0;
  41. }
  42. if (driver->silent) {
  43. free (driver->silent);
  44. driver->silent = 0;
  45. }
  46. }
  47. static int
  48. alsa_driver_check_capabilities (alsa_driver_t *driver)
  49. {
  50. return 0;
  51. }
  52. static int
  53. alsa_driver_check_card_type (alsa_driver_t *driver)
  54. {
  55. int err;
  56. snd_ctl_card_info_t *card_info;
  57. snd_ctl_card_info_alloca (&card_info);
  58. if ((err = snd_ctl_open (&driver->ctl_handle, driver->alsa_name, 0)) < 0) {
  59. jack_error ("control open \"%s\" (%s)", driver->alsa_name, snd_strerror(err));
  60. return -1;
  61. }
  62. if ((err = snd_ctl_card_info(driver->ctl_handle, card_info)) < 0) {
  63. jack_error ("control hardware info \"%s\" (%s)", driver->alsa_name, snd_strerror (err));
  64. snd_ctl_close (driver->ctl_handle);
  65. return -1;
  66. }
  67. driver->alsa_driver = strdup(snd_ctl_card_info_get_driver (card_info));
  68. return alsa_driver_check_capabilities (driver);
  69. }
  70. static int
  71. alsa_driver_hammerfall_hardware (alsa_driver_t *driver)
  72. {
  73. driver->hw = jack_alsa_hammerfall_hw_new (driver);
  74. return 0;
  75. }
  76. static int
  77. alsa_driver_generic_hardware (alsa_driver_t *driver)
  78. {
  79. driver->hw = jack_alsa_generic_hw_new (driver);
  80. return 0;
  81. }
  82. static int
  83. alsa_driver_hw_specific (alsa_driver_t *driver, int hw_monitoring)
  84. {
  85. int err;
  86. if (!strcmp(driver->alsa_driver, "RME9652")) {
  87. if ((err = alsa_driver_hammerfall_hardware (driver)) != 0) {
  88. return err;
  89. }
  90. } else {
  91. if ((err = alsa_driver_generic_hardware (driver)) != 0) {
  92. return err;
  93. }
  94. }
  95. if (driver->hw->capabilities & Cap_HardwareMonitoring) {
  96. driver->has_hw_monitoring = TRUE;
  97. /* XXX need to ensure that this is really FALSE or TRUE or whatever*/
  98. driver->hw_monitoring = hw_monitoring;
  99. } else {
  100. driver->has_hw_monitoring = FALSE;
  101. driver->hw_monitoring = FALSE;
  102. }
  103. if (driver->hw->capabilities & Cap_ClockLockReporting) {
  104. driver->has_clock_sync_reporting = TRUE;
  105. } else {
  106. driver->has_clock_sync_reporting = FALSE;
  107. }
  108. return 0;
  109. }
  110. static void
  111. alsa_driver_setup_io_function_pointers (alsa_driver_t *driver)
  112. {
  113. switch (driver->sample_bytes) {
  114. case 2:
  115. if (driver->interleaved) {
  116. driver->channel_copy = memcpy_interleave_d16_s16;
  117. } else {
  118. driver->channel_copy = memcpy_fake;
  119. }
  120. driver->write_via_copy = sample_move_d16_sS;
  121. driver->read_via_copy = sample_move_dS_s16;
  122. break;
  123. case 4:
  124. if (driver->interleaved) {
  125. driver->channel_copy = memcpy_interleave_d32_s32;
  126. } else {
  127. driver->channel_copy = memcpy_fake;
  128. }
  129. driver->write_via_copy = sample_move_d32u24_sS;
  130. driver->read_via_copy = sample_move_dS_s32u24;
  131. break;
  132. }
  133. }
  134. static int
  135. alsa_driver_configure_stream (alsa_driver_t *driver,
  136. const char *stream_name,
  137. snd_pcm_t *handle,
  138. snd_pcm_hw_params_t *hw_params,
  139. snd_pcm_sw_params_t *sw_params,
  140. unsigned long *nchns)
  141. {
  142. int err;
  143. if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0) {
  144. jack_error ("ALSA: no playback configurations available");
  145. return -1;
  146. }
  147. if ((err = snd_pcm_hw_params_set_periods_integer (handle, hw_params)) < 0) {
  148. jack_error ("ALSA: cannot restrict period size to integral value.");
  149. return -1;
  150. }
  151. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_NONINTERLEAVED)) < 0) {
  152. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
  153. jack_error ("ALSA: mmap-based access is not possible for the %s "
  154. "stream of this audio interface", stream_name);
  155. return -1;
  156. }
  157. }
  158. if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S32_LE)) < 0) {
  159. if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
  160. jack_error ("Sorry. The audio interface \"%s\""
  161. "doesn't support either of the two hardware sample formats that ardour can use.",
  162. driver->alsa_name);
  163. return -1;
  164. }
  165. }
  166. if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params, driver->frame_rate, 0)) < 0) {
  167. jack_error ("ALSA: cannot set sample/frame rate to %u for %s", driver->frame_rate, stream_name);
  168. return -1;
  169. }
  170. *nchns = snd_pcm_hw_params_get_channels_max (hw_params);
  171. if (*nchns > 1024) {
  172. /* the hapless user is an unwitting victim of the "default"
  173. ALSA PCM device, which can support up to 16 million
  174. channels. since they can't be bothered to set up
  175. a proper default device, limit the number of channels
  176. for them to a sane default.
  177. */
  178. jack_error ("You appear to be using the ALSA software \"plug\" layer, probably\n"
  179. "a result of using the \"default\" ALSA device. This is less\n"
  180. "efficient than it could be. Consider using a ~/.asoundrc file\n"
  181. "to define a hardware audio device rather than using the plug layer\n");
  182. *nchns = 2;
  183. }
  184. if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, *nchns)) < 0) {
  185. jack_error ("ALSA: cannot set channel count to %u for %s", *nchns, stream_name);
  186. return -1;
  187. }
  188. if ((err = snd_pcm_hw_params_set_period_size (handle, hw_params, driver->frames_per_cycle, 0)) < 0) {
  189. jack_error ("ALSA: cannot set period size to %u frames for %s", driver->frames_per_cycle, stream_name);
  190. return -1;
  191. }
  192. if ((err = snd_pcm_hw_params_set_periods (handle, hw_params, driver->user_nperiods, 0)) < 0) {
  193. jack_error ("ALSA: cannot set number of periods to %u for %s", driver->user_nperiods, stream_name);
  194. return -1;
  195. }
  196. if ((err = snd_pcm_hw_params_set_buffer_size (handle, hw_params, driver->user_nperiods * driver->frames_per_cycle)) < 0) {
  197. jack_error ("ALSA: cannot set buffer length to %u for %s", driver->user_nperiods * driver->frames_per_cycle, stream_name);
  198. return -1;
  199. }
  200. if ((err = snd_pcm_hw_params (handle, hw_params)) < 0) {
  201. jack_error ("ALSA: cannot set hardware parameters for %s", stream_name);
  202. return -1;
  203. }
  204. snd_pcm_sw_params_current (handle, sw_params);
  205. if ((err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, ~0U)) < 0) {
  206. jack_error ("ALSA: cannot set start mode for %s", stream_name);
  207. return -1;
  208. }
  209. if ((err = snd_pcm_sw_params_set_stop_threshold (handle, sw_params, ~0U)) < 0) {
  210. jack_error ("ALSA: cannot set stop mode for %s", stream_name);
  211. return -1;
  212. }
  213. if ((err = snd_pcm_sw_params_set_silence_threshold (handle, sw_params, 0)) < 0) {
  214. jack_error ("ALSA: cannot set silence threshold for %s", stream_name);
  215. return -1;
  216. }
  217. if ((err = snd_pcm_sw_params_set_silence_size (handle, sw_params, driver->frames_per_cycle * driver->nfragments)) < 0) {
  218. jack_error ("ALSA: cannot set silence size for %s", stream_name);
  219. return -1;
  220. }
  221. if ((err = snd_pcm_sw_params_set_avail_min (handle, sw_params, driver->frames_per_cycle)) < 0) {
  222. jack_error ("ALSA: cannot set avail min for %s", stream_name);
  223. return -1;
  224. }
  225. if ((err = snd_pcm_sw_params (handle, sw_params)) < 0) {
  226. jack_error ("ALSA: cannot set software parameters for %s", stream_name);
  227. return -1;
  228. }
  229. return 0;
  230. }
  231. static int
  232. alsa_driver_set_parameters (alsa_driver_t *driver, nframes_t frames_per_cycle, nframes_t user_nperiods, nframes_t rate)
  233. {
  234. int p_noninterleaved;
  235. int c_noninterleaved;
  236. snd_pcm_format_t c_format, p_format;
  237. int dir;
  238. unsigned int p_period_size, c_period_size;
  239. unsigned int p_nfragments, c_nfragments;
  240. channel_t chn;
  241. driver->frame_rate = rate;
  242. driver->frames_per_cycle = frames_per_cycle;
  243. driver->user_nperiods = user_nperiods;
  244. if (alsa_driver_configure_stream (driver, "capture",
  245. driver->capture_handle,
  246. driver->capture_hw_params,
  247. driver->capture_sw_params,
  248. &driver->capture_nchannels)) {
  249. jack_error ("ALSA: cannot configure capture channel");
  250. return -1;
  251. }
  252. if (alsa_driver_configure_stream (driver, "playback",
  253. driver->playback_handle,
  254. driver->playback_hw_params,
  255. driver->playback_sw_params,
  256. &driver->playback_nchannels)) {
  257. jack_error ("ALSA: cannot configure playback channel");
  258. return -1;
  259. }
  260. /* check the fragment size, since thats non-negotiable */
  261. p_period_size = snd_pcm_hw_params_get_period_size (driver->playback_hw_params, &dir);
  262. c_period_size = snd_pcm_hw_params_get_period_size (driver->capture_hw_params, &dir);
  263. if (c_period_size != driver->frames_per_cycle || p_period_size != driver->frames_per_cycle) {
  264. jack_error ("alsa_pcm: requested an interrupt every %u frames but got %uc%up frames",
  265. driver->frames_per_cycle, c_period_size, p_period_size);
  266. return -1;
  267. }
  268. p_nfragments = snd_pcm_hw_params_get_periods (driver->playback_hw_params, &dir);
  269. c_nfragments = snd_pcm_hw_params_get_periods (driver->capture_hw_params, &dir);
  270. if (p_nfragments != c_nfragments) {
  271. jack_error ("alsa_pcm: different period counts for playback and capture!");
  272. return -1;
  273. }
  274. driver->nfragments = c_nfragments;
  275. driver->buffer_frames = driver->frames_per_cycle * driver->nfragments;
  276. /* Check that we are using the same sample format on both streams */
  277. p_format = (snd_pcm_format_t) snd_pcm_hw_params_get_format (driver->playback_hw_params);
  278. c_format = (snd_pcm_format_t) snd_pcm_hw_params_get_format (driver->capture_hw_params);
  279. if (p_format != c_format) {
  280. jack_error ("Sorry. The audio interface \"%s\""
  281. "doesn't support the same sample format for capture and playback."
  282. "this JACK/ALSA driver cannot use this hardware.", driver->alsa_name);
  283. return -1;
  284. }
  285. driver->sample_format = p_format;
  286. driver->sample_bytes = snd_pcm_format_physical_width (driver->sample_format) / 8;
  287. driver->bytes_per_cycle = driver->sample_bytes * driver->frames_per_cycle;
  288. switch (driver->sample_format) {
  289. case SND_PCM_FORMAT_S32_LE:
  290. case SND_PCM_FORMAT_S16_LE:
  291. break;
  292. default:
  293. jack_error ("programming error: unhandled format type");
  294. exit (1);
  295. }
  296. /* check interleave setup */
  297. p_noninterleaved = (snd_pcm_hw_params_get_access (driver->playback_hw_params) == SND_PCM_ACCESS_MMAP_NONINTERLEAVED);
  298. c_noninterleaved = (snd_pcm_hw_params_get_access (driver->capture_hw_params) == SND_PCM_ACCESS_MMAP_NONINTERLEAVED);
  299. if (c_noninterleaved != p_noninterleaved) {
  300. jack_error ("ALSA: the playback and capture components of this audio interface differ "
  301. "in their use of channel interleaving. Ardour cannot use this h/w.");
  302. return -1;
  303. }
  304. driver->interleaved = !c_noninterleaved;
  305. if (driver->interleaved) {
  306. driver->interleave_unit = snd_pcm_format_physical_width (driver->sample_format) / 8;
  307. driver->playback_interleave_skip = driver->interleave_unit * driver->playback_nchannels;
  308. driver->capture_interleave_skip = driver->interleave_unit * driver->capture_nchannels;
  309. } else {
  310. driver->interleave_unit = 0; /* NOT USED */
  311. driver->playback_interleave_skip = snd_pcm_format_physical_width (driver->sample_format) / 8;
  312. driver->capture_interleave_skip = driver->playback_interleave_skip;
  313. }
  314. if (driver->playback_nchannels > driver->capture_nchannels) {
  315. driver->max_nchannels = driver->playback_nchannels;
  316. driver->user_nchannels = driver->capture_nchannels;
  317. } else {
  318. driver->max_nchannels = driver->capture_nchannels;
  319. driver->user_nchannels = driver->playback_nchannels;
  320. }
  321. alsa_driver_setup_io_function_pointers (driver);
  322. /* Allocate and initialize structures that rely on
  323. the channels counts.
  324. */
  325. driver->playback_addr = (char **) malloc (sizeof (char *) * driver->playback_nchannels);
  326. driver->capture_addr = (char **) malloc (sizeof (char *) * driver->capture_nchannels);
  327. memset (driver->playback_addr, 0, sizeof (char *) * driver->playback_nchannels);
  328. memset (driver->capture_addr, 0, sizeof (char *) * driver->capture_nchannels);
  329. driver->silent = (unsigned long *) malloc (sizeof (unsigned long) * driver->playback_nchannels);
  330. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  331. driver->silent[chn] = 0;
  332. }
  333. driver->clock_sync_data = (ClockSyncStatus *) malloc (sizeof (ClockSyncStatus) *
  334. driver->capture_nchannels > driver->playback_nchannels ?
  335. driver->capture_nchannels : driver->playback_nchannels);
  336. /* set up the bit pattern that is used to record which
  337. channels require action on every cycle. any bits that are
  338. not set after the engine's process() call indicate channels
  339. that potentially need to be silenced.
  340. XXX this is limited to <wordsize> channels. Use a bitset
  341. type instead.
  342. */
  343. driver->channel_done_bits = 0;
  344. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  345. driver->channel_done_bits |= (1<<chn);
  346. }
  347. driver->period_interval = (unsigned long) floor ((((float) driver->frames_per_cycle) / driver->frame_rate) * 1000.0);
  348. if (driver->engine) {
  349. driver->engine->set_buffer_size (driver->engine, driver->frames_per_cycle);
  350. }
  351. return 0;
  352. }
  353. static int
  354. alsa_driver_reset_parameters (alsa_driver_t *driver, nframes_t frames_per_cycle, nframes_t user_nperiods, nframes_t rate)
  355. {
  356. /* XXX unregister old ports ? */
  357. alsa_driver_release_channel_dependent_memory (driver);
  358. return alsa_driver_set_parameters (driver, frames_per_cycle, user_nperiods, rate);
  359. }
  360. static int
  361. alsa_driver_get_channel_addresses (alsa_driver_t *driver,
  362. snd_pcm_uframes_t *capture_avail,
  363. snd_pcm_uframes_t *playback_avail,
  364. snd_pcm_uframes_t *capture_offset,
  365. snd_pcm_uframes_t *playback_offset)
  366. {
  367. unsigned long err;
  368. channel_t chn;
  369. if (capture_avail) {
  370. if ((err = snd_pcm_mmap_begin (driver->capture_handle, &driver->capture_areas,
  371. (snd_pcm_uframes_t *) capture_offset,
  372. (snd_pcm_uframes_t *) capture_avail)) < 0) {
  373. jack_error ("ALSA-HW: %s: mmap areas info error", driver->alsa_name);
  374. return -1;
  375. }
  376. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  377. const snd_pcm_channel_area_t *a = &driver->capture_areas[chn];
  378. driver->capture_addr[chn] = (char *) a->addr + ((a->first + a->step * *capture_offset) / 8);
  379. }
  380. }
  381. if (playback_avail) {
  382. if ((err = snd_pcm_mmap_begin (driver->playback_handle, &driver->playback_areas,
  383. (snd_pcm_uframes_t *) playback_offset,
  384. (snd_pcm_uframes_t *) playback_avail)) < 0) {
  385. jack_error ("ALSA-HW: %s: mmap areas info error ", driver->alsa_name);
  386. return -1;
  387. }
  388. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  389. const snd_pcm_channel_area_t *a = &driver->playback_areas[chn];
  390. driver->playback_addr[chn] = (char *) a->addr + ((a->first + a->step * *playback_offset) / 8);
  391. }
  392. }
  393. return 0;
  394. }
  395. static int
  396. alsa_driver_audio_start (alsa_driver_t *driver)
  397. {
  398. int err;
  399. snd_pcm_uframes_t poffset, pavail;
  400. channel_t chn;
  401. if ((err = snd_pcm_prepare (driver->playback_handle)) < 0) {
  402. jack_error ("ALSA-HW: prepare error for playback on \"%s\" (%s)", driver->alsa_name, snd_strerror(err));
  403. return -1;
  404. }
  405. if (driver->capture_and_playback_not_synced) {
  406. if ((err = snd_pcm_prepare (driver->capture_handle)) < 0) {
  407. jack_error ("ALSA-HW: prepare error for capture on \"%s\" (%s)", driver->alsa_name, snd_strerror(err));
  408. return -1;
  409. }
  410. }
  411. if (driver->hw_monitoring) {
  412. driver->hw->set_input_monitor_mask (driver->hw, driver->input_monitor_mask);
  413. }
  414. /* fill playback buffer with zeroes, and mark
  415. all fragments as having data.
  416. */
  417. pavail = snd_pcm_avail_update (driver->playback_handle);
  418. if (pavail != driver->buffer_frames) {
  419. jack_error ("ALSA-HW: full buffer not available at start");
  420. return -1;
  421. }
  422. if (alsa_driver_get_channel_addresses (driver, 0, &pavail, 0, &poffset)) {
  423. return -1;
  424. }
  425. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  426. alsa_driver_silence_on_channel (driver, chn, driver->buffer_frames);
  427. }
  428. snd_pcm_mmap_commit (driver->playback_handle, poffset, driver->buffer_frames);
  429. if ((err = snd_pcm_start (driver->playback_handle)) < 0) {
  430. jack_error ("could not start playback (%s)", snd_strerror (err));
  431. return -1;
  432. }
  433. if (driver->capture_and_playback_not_synced) {
  434. if ((err = snd_pcm_start (driver->capture_handle)) < 0) {
  435. jack_error ("could not start capture (%s)", snd_strerror (err));
  436. return -1;
  437. }
  438. }
  439. if (driver->hw_monitoring && (driver->input_monitor_mask || driver->all_monitor_in)) {
  440. if (driver->all_monitor_in) {
  441. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  442. } else {
  443. driver->hw->set_input_monitor_mask (driver->hw, driver->input_monitor_mask);
  444. }
  445. }
  446. driver->playback_nfds = snd_pcm_poll_descriptors_count (driver->playback_handle);
  447. driver->capture_nfds = snd_pcm_poll_descriptors_count (driver->capture_handle);
  448. if (driver->pfd)
  449. free (driver->pfd);
  450. driver->pfd = (struct pollfd *) malloc (sizeof (struct pollfd) *
  451. (driver->playback_nfds + driver->capture_nfds + 1));
  452. return 0;
  453. }
  454. static int
  455. alsa_driver_audio_stop (alsa_driver_t *driver)
  456. {
  457. int err;
  458. if ((err = snd_pcm_drop (driver->playback_handle)) < 0) {
  459. jack_error ("alsa_pcm: channel flush for playback failed (%s)", snd_strerror (err));
  460. return -1;
  461. }
  462. if (driver->capture_and_playback_not_synced) {
  463. if ((err = snd_pcm_drop (driver->capture_handle)) < 0) {
  464. jack_error ("alsa_pcm: channel flush for capture failed (%s)", snd_strerror (err));
  465. return -1;
  466. }
  467. }
  468. driver->hw->set_input_monitor_mask (driver->hw, 0);
  469. return 0;
  470. }
  471. static int
  472. alsa_driver_xrun_recovery (alsa_driver_t *driver)
  473. {
  474. snd_pcm_sframes_t capture_delay;
  475. int err;
  476. if ((err = snd_pcm_delay (driver->capture_handle, &capture_delay))) {
  477. jack_error ("alsa_pcm: cannot determine capture delay (%s)", snd_strerror (err));
  478. exit (1);
  479. }
  480. fprintf (stderr, "alsa_pcm: xrun of %lu frames, (%.3f msecs)\n", capture_delay,
  481. ((float) capture_delay / (float) driver->frame_rate) * 1000.0);
  482. #if ENGINE
  483. if (!engine->xrun_recoverable ()) {
  484. /* don't report an error here, its distracting */
  485. return -1;
  486. }
  487. #endif
  488. if (alsa_driver_audio_stop (driver) || alsa_driver_audio_start (driver)) {
  489. return -1;
  490. }
  491. return 0;
  492. }
  493. static void
  494. alsa_driver_silence_untouched_channels (alsa_driver_t *driver, nframes_t nframes)
  495. {
  496. channel_t chn;
  497. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  498. if ((driver->channels_not_done & (1<<chn))) {
  499. if (driver->silent[chn] < driver->buffer_frames) {
  500. alsa_driver_silence_on_channel_no_mark (driver, chn, nframes);
  501. driver->silent[chn] += nframes;
  502. }
  503. }
  504. }
  505. }
  506. void
  507. alsa_driver_set_clock_sync_status (alsa_driver_t *driver, channel_t chn, ClockSyncStatus status)
  508. {
  509. driver->clock_sync_data[chn] = status;
  510. alsa_driver_clock_sync_notify (driver, chn, status);
  511. }
  512. static int under_gdb = FALSE;
  513. static int waitcnt = 0;
  514. static int
  515. alsa_driver_wait (alsa_driver_t *driver)
  516. {
  517. snd_pcm_sframes_t avail = 0;
  518. snd_pcm_sframes_t contiguous = 0;
  519. snd_pcm_sframes_t capture_avail = 0;
  520. snd_pcm_sframes_t playback_avail = 0;
  521. snd_pcm_uframes_t capture_offset = 0;
  522. snd_pcm_uframes_t playback_offset = 0;
  523. int xrun_detected;
  524. channel_t chn;
  525. GSList *node;
  526. int need_capture = 1;
  527. int need_playback = 1;
  528. jack_engine_t *engine = driver->engine;
  529. int i;
  530. static unsigned long long last = 0;
  531. unsigned long long start;
  532. again:
  533. while (need_playback || need_capture) {
  534. int p_timed_out, c_timed_out;
  535. int ci = 0;
  536. int nfds;
  537. nfds = 0;
  538. if (need_playback) {
  539. snd_pcm_poll_descriptors (driver->playback_handle, &driver->pfd[0], driver->playback_nfds);
  540. nfds += driver->playback_nfds;
  541. }
  542. if (need_capture) {
  543. snd_pcm_poll_descriptors (driver->capture_handle, &driver->pfd[nfds], driver->capture_nfds);
  544. ci = nfds;
  545. nfds += driver->capture_nfds;
  546. }
  547. /* ALSA doesn't set POLLERR in some versions of 0.9.X */
  548. for (i = 0; i < nfds; i++) {
  549. driver->pfd[nfds].events |= POLLERR;
  550. }
  551. if (poll (driver->pfd, nfds, 1000) < 0) {
  552. if (errno == EINTR) {
  553. printf ("poll interrupt\n");
  554. // this happens mostly when run
  555. // under gdb, or when exiting due to a signal
  556. if (under_gdb) {
  557. goto again;
  558. }
  559. return 1;
  560. }
  561. jack_error ("ALSA::Device: poll call failed (%s)", strerror (errno));
  562. return -1;
  563. }
  564. rdtscll (start);
  565. // fprintf (stderr, "engine cycle %.6f msecs\n", (((float) (start - last))/450000.0f));
  566. last = start;
  567. if (driver->engine) {
  568. struct timeval tv;
  569. gettimeofday (&tv, NULL);
  570. driver->engine->control->time.microseconds = tv.tv_sec * 1000000 + tv.tv_usec;
  571. }
  572. p_timed_out = 0;
  573. if (need_playback) {
  574. for (i = 0; i < driver->playback_nfds; i++) {
  575. if (driver->pfd[i].revents & POLLERR) {
  576. jack_error ("ALSA: poll reports error on playback stream.");
  577. return -1;
  578. }
  579. if (driver->pfd[i].revents == 0) {
  580. p_timed_out++;
  581. }
  582. }
  583. if (p_timed_out == 0) {
  584. need_playback = 0;
  585. }
  586. }
  587. c_timed_out = 0;
  588. if (need_capture) {
  589. for (i = ci; i < nfds; i++) {
  590. if (driver->pfd[i].revents & POLLERR) {
  591. jack_error ("ALSA: poll reports error on capture stream.");
  592. return -1;
  593. }
  594. if (driver->pfd[i].revents == 0) {
  595. c_timed_out++;
  596. }
  597. }
  598. if (c_timed_out == 0) {
  599. need_capture = 0;
  600. }
  601. }
  602. if (p_timed_out == driver->playback_nfds && c_timed_out == driver->capture_nfds) {
  603. jack_error ("ALSA: poll time out");
  604. return -1;
  605. }
  606. }
  607. xrun_detected = FALSE;
  608. if ((capture_avail = snd_pcm_avail_update (driver->capture_handle)) < 0) {
  609. if (capture_avail == -EPIPE) {
  610. xrun_detected = TRUE;
  611. } else {
  612. jack_error ("unknown ALSA avail_update return value (%u)", capture_avail);
  613. }
  614. }
  615. if ((playback_avail = snd_pcm_avail_update (driver->playback_handle)) < 0) {
  616. if (playback_avail == -EPIPE) {
  617. xrun_detected = TRUE;
  618. } else {
  619. jack_error ("unknown ALSA avail_update return value (%u)", playback_avail);
  620. }
  621. }
  622. if (xrun_detected) {
  623. return alsa_driver_xrun_recovery (driver);
  624. }
  625. avail = capture_avail < playback_avail ? capture_avail : playback_avail;
  626. /* constrain the available count to the nearest (round down) number of
  627. periods.
  628. */
  629. avail = avail - (avail % driver->frames_per_cycle);
  630. while (avail) {
  631. waitcnt++;
  632. capture_avail = (avail > driver->frames_per_cycle) ? driver->frames_per_cycle : avail;
  633. playback_avail = (avail > driver->frames_per_cycle) ? driver->frames_per_cycle : avail;
  634. if (alsa_driver_get_channel_addresses (driver,
  635. (snd_pcm_uframes_t *) &capture_avail,
  636. (snd_pcm_uframes_t *) &playback_avail,
  637. &capture_offset, &playback_offset) < 0) {
  638. return -1;
  639. }
  640. contiguous = capture_avail < playback_avail ? capture_avail : playback_avail;
  641. driver->channels_not_done = driver->channel_done_bits;
  642. if (engine->process_lock (engine) == 0) {
  643. int ret;
  644. GSList *prev;
  645. if ((ret = engine->process (engine, contiguous)) != 0) {
  646. engine->process_unlock (engine);
  647. alsa_driver_audio_stop (driver);
  648. if (ret > 0) {
  649. engine->post_process (engine);
  650. }
  651. return ret;
  652. }
  653. /* now move data from ports to channels */
  654. for (chn = 0, prev = 0, node = driver->playback_ports; node; prev = node, node = g_slist_next (node), chn++) {
  655. jack_port_t *port = (jack_port_t *) node->data;
  656. if (!jack_port_connected (port)) {
  657. continue;
  658. }
  659. alsa_driver_write_to_channel (driver, 1, jack_port_get_buffer (port, contiguous), contiguous);
  660. }
  661. engine->process_unlock (engine);
  662. }
  663. /* Now handle input monitoring */
  664. driver->input_monitor_mask = 0;
  665. for (chn = 0, node = driver->capture_ports; node; node = g_slist_next (node), chn++) {
  666. if (((jack_port_t *) node->data)->shared->monitor_requests) {
  667. driver->input_monitor_mask |= (1<<chn);
  668. }
  669. }
  670. if (!driver->hw_monitoring) {
  671. if (driver->all_monitor_in) {
  672. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  673. alsa_driver_copy_channel (driver, chn, chn, contiguous);
  674. }
  675. } else if (driver->input_monitor_mask) {
  676. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  677. if (driver->input_monitor_mask & (1<<chn)) {
  678. alsa_driver_copy_channel (driver, chn, chn, contiguous);
  679. }
  680. }
  681. }
  682. } else {
  683. if ((driver->hw->input_monitor_mask != driver->input_monitor_mask) && !driver->all_monitor_in) {
  684. driver->hw->set_input_monitor_mask (driver->hw, driver->input_monitor_mask);
  685. }
  686. }
  687. if (driver->channels_not_done) {
  688. alsa_driver_silence_untouched_channels (driver, contiguous);
  689. }
  690. snd_pcm_mmap_commit (driver->capture_handle, capture_offset, contiguous);
  691. snd_pcm_mmap_commit (driver->playback_handle, playback_offset, contiguous);
  692. avail -= contiguous;
  693. }
  694. engine->post_process (engine);
  695. return 0;
  696. }
  697. static int
  698. alsa_driver_process (nframes_t nframes, void *arg)
  699. {
  700. alsa_driver_t *driver = (alsa_driver_t *) arg;
  701. channel_t chn;
  702. jack_port_t *port;
  703. GSList *node;
  704. for (chn = 0, node = driver->capture_ports; node; node = g_slist_next (node), chn++) {
  705. port = (jack_port_t *) node->data;
  706. if (!jack_port_connected (port)) {
  707. continue;
  708. }
  709. alsa_driver_read_from_channel (driver, chn, jack_port_get_buffer (port, nframes), nframes);
  710. }
  711. return 0;
  712. }
  713. static void
  714. alsa_driver_attach (alsa_driver_t *driver, jack_engine_t *engine)
  715. {
  716. char buf[32];
  717. channel_t chn;
  718. jack_port_t *port;
  719. driver->engine = engine;
  720. driver->engine->set_buffer_size (engine, driver->frames_per_cycle);
  721. driver->engine->set_sample_rate (engine, driver->frame_rate);
  722. /* Now become a client of the engine */
  723. if ((driver->client = jack_driver_become_client ("alsa_pcm")) == NULL) {
  724. jack_error ("ALSA: cannot become client");
  725. return;
  726. }
  727. jack_set_process_callback (driver->client, alsa_driver_process, driver);
  728. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  729. snprintf (buf, sizeof(buf) - 1, "in_%lu", chn+1);
  730. port = jack_port_register (driver->client, buf,
  731. JACK_DEFAULT_AUDIO_TYPE,
  732. JackPortIsOutput|JackPortIsPhysical|JackPortCanMonitor, 0);
  733. if (port == 0) {
  734. jack_error ("ALSA: cannot register port for %s", buf);
  735. break;
  736. }
  737. /* XXX fix this so that it can handle: systemic (external) latency
  738. */
  739. jack_port_set_latency (port, driver->frames_per_cycle * driver->nfragments);
  740. driver->capture_ports = g_slist_append (driver->capture_ports, port);
  741. }
  742. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  743. snprintf (buf, sizeof(buf) - 1, "out_%lu", chn+1);
  744. port = jack_port_register (driver->client, buf,
  745. JACK_DEFAULT_AUDIO_TYPE,
  746. JackPortIsInput|JackPortIsPhysical, 0);
  747. if (port == 0) {
  748. jack_error ("ALSA: cannot register port for %s", buf);
  749. break;
  750. }
  751. /* XXX fix this so that it can handle: systemic (external) latency
  752. */
  753. jack_port_set_latency (port, driver->frames_per_cycle * driver->nfragments);
  754. driver->playback_ports = g_slist_append (driver->playback_ports, port);
  755. }
  756. printf ("ALSA: ports registered, starting driver\n");
  757. jack_activate (driver->client);
  758. }
  759. static void
  760. alsa_driver_detach (alsa_driver_t *driver, jack_engine_t *engine)
  761. {
  762. GSList *node;
  763. if (driver->engine == 0) {
  764. return;
  765. }
  766. for (node = driver->capture_ports; node; node = g_slist_next (node)) {
  767. jack_port_unregister (driver->client, ((jack_port_t *) node->data));
  768. }
  769. g_slist_free (driver->capture_ports);
  770. driver->capture_ports = 0;
  771. for (node = driver->playback_ports; node; node = g_slist_next (node)) {
  772. jack_port_unregister (driver->client, ((jack_port_t *) node->data));
  773. }
  774. g_slist_free (driver->playback_ports);
  775. driver->playback_ports = 0;
  776. driver->engine = 0;
  777. }
  778. static int
  779. alsa_driver_change_sample_clock (alsa_driver_t *driver, SampleClockMode mode)
  780. {
  781. return driver->hw->change_sample_clock (driver->hw, mode);
  782. }
  783. static void
  784. alsa_driver_request_all_monitor_input (alsa_driver_t *driver, int yn)
  785. {
  786. if (driver->hw_monitoring) {
  787. if (yn) {
  788. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  789. } else {
  790. driver->hw->set_input_monitor_mask (driver->hw, driver->input_monitor_mask);
  791. }
  792. }
  793. driver->all_monitor_in = yn;
  794. }
  795. static void
  796. alsa_driver_set_hw_monitoring (alsa_driver_t *driver, int yn)
  797. {
  798. if (yn) {
  799. driver->hw_monitoring = TRUE;
  800. if (driver->all_monitor_in) {
  801. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  802. } else {
  803. driver->hw->set_input_monitor_mask (driver->hw, driver->input_monitor_mask);
  804. }
  805. } else {
  806. driver->hw_monitoring = FALSE;
  807. driver->hw->set_input_monitor_mask (driver->hw, 0);
  808. }
  809. }
  810. static ClockSyncStatus
  811. alsa_driver_clock_sync_status (channel_t chn)
  812. {
  813. return Lock;
  814. }
  815. static void
  816. alsa_driver_delete (alsa_driver_t *driver)
  817. {
  818. GSList *node;
  819. for (node = driver->clock_sync_listeners; node; node = g_slist_next (node)) {
  820. free (node->data);
  821. }
  822. g_slist_free (driver->clock_sync_listeners);
  823. if (driver->capture_handle) {
  824. snd_pcm_close (driver->capture_handle);
  825. driver->capture_handle = 0;
  826. }
  827. if (driver->playback_handle) {
  828. snd_pcm_close (driver->playback_handle);
  829. driver->capture_handle = 0;
  830. }
  831. if (driver->capture_hw_params) {
  832. snd_pcm_hw_params_free (driver->capture_hw_params);
  833. driver->capture_hw_params = 0;
  834. }
  835. if (driver->playback_hw_params) {
  836. snd_pcm_hw_params_free (driver->playback_hw_params);
  837. driver->playback_hw_params = 0;
  838. }
  839. if (driver->capture_sw_params) {
  840. snd_pcm_sw_params_free (driver->capture_sw_params);
  841. driver->capture_sw_params = 0;
  842. }
  843. if (driver->playback_sw_params) {
  844. snd_pcm_sw_params_free (driver->playback_sw_params);
  845. driver->playback_sw_params = 0;
  846. }
  847. if (driver->pfd) {
  848. free (driver->pfd);
  849. }
  850. if (driver->hw) {
  851. driver->hw->release (driver->hw);
  852. driver->hw = 0;
  853. }
  854. free(driver->alsa_name);
  855. free(driver->alsa_driver);
  856. alsa_driver_release_channel_dependent_memory (driver);
  857. free (driver);
  858. }
  859. static jack_driver_t *
  860. alsa_driver_new (char *name, char *alsa_device,
  861. nframes_t frames_per_cycle,
  862. nframes_t user_nperiods,
  863. nframes_t rate,
  864. int hw_monitoring)
  865. {
  866. int err;
  867. alsa_driver_t *driver;
  868. printf ("creating alsa driver ... %s|%lu|%lu|%lu|%s\n",
  869. alsa_device, frames_per_cycle, user_nperiods, rate,
  870. hw_monitoring ? "hwmon":"swmon");
  871. driver = (alsa_driver_t *) calloc (1, sizeof (alsa_driver_t));
  872. jack_driver_init ((jack_driver_t *) driver);
  873. driver->attach = (JackDriverAttachFunction) alsa_driver_attach;
  874. driver->detach = (JackDriverDetachFunction) alsa_driver_detach;
  875. driver->wait = (JackDriverWaitFunction) alsa_driver_wait;
  876. driver->start = (JackDriverStartFunction) alsa_driver_audio_start;
  877. driver->stop = (JackDriverStopFunction) alsa_driver_audio_stop;
  878. driver->ctl_handle = 0;
  879. driver->hw = 0;
  880. driver->capture_and_playback_not_synced = FALSE;
  881. driver->nfragments = 0;
  882. driver->max_nchannels = 0;
  883. driver->user_nchannels = 0;
  884. driver->playback_nchannels = 0;
  885. driver->capture_nchannels = 0;
  886. driver->playback_addr = 0;
  887. driver->capture_addr = 0;
  888. driver->silent = 0;
  889. driver->all_monitor_in = FALSE;
  890. driver->clock_mode = ClockMaster; /* XXX is it? */
  891. driver->input_monitor_mask = 0; /* XXX is it? */
  892. driver->capture_ports = 0;
  893. driver->playback_ports = 0;
  894. driver->pfd = 0;
  895. driver->playback_nfds = 0;
  896. driver->capture_nfds = 0;
  897. pthread_mutex_init (&driver->clock_sync_lock, 0);
  898. driver->clock_sync_listeners = 0;
  899. if ((err = snd_pcm_open (&driver->playback_handle, alsa_device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
  900. jack_error ("ALSA: Cannot open PCM device %s/%s", name, alsa_device);
  901. free (driver);
  902. return 0;
  903. }
  904. driver->alsa_name = strdup (alsa_device);
  905. if ((err = snd_pcm_open (&driver->capture_handle, alsa_device, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
  906. snd_pcm_close (driver->playback_handle);
  907. jack_error ("ALSA: Cannot open PCM device %s", name);
  908. free (driver);
  909. return 0;
  910. }
  911. if (alsa_driver_check_card_type (driver)) {
  912. snd_pcm_close (driver->capture_handle);
  913. snd_pcm_close (driver->playback_handle);
  914. free (driver);
  915. return 0;
  916. }
  917. driver->playback_hw_params = 0;
  918. driver->capture_hw_params = 0;
  919. driver->playback_sw_params = 0;
  920. driver->capture_sw_params = 0;
  921. if ((err = snd_pcm_hw_params_malloc (&driver->playback_hw_params)) < 0) {
  922. jack_error ("ALSA: could no allocate playback hw params structure");
  923. alsa_driver_delete (driver);
  924. return 0;
  925. }
  926. if ((err = snd_pcm_hw_params_malloc (&driver->capture_hw_params)) < 0) {
  927. jack_error ("ALSA: could no allocate capture hw params structure");
  928. alsa_driver_delete (driver);
  929. return 0;
  930. }
  931. if ((err = snd_pcm_sw_params_malloc (&driver->playback_sw_params)) < 0) {
  932. jack_error ("ALSA: could no allocate playback sw params structure");
  933. alsa_driver_delete (driver);
  934. return 0;
  935. }
  936. if ((err = snd_pcm_sw_params_malloc (&driver->capture_sw_params)) < 0) {
  937. jack_error ("ALSA: could no allocate capture sw params structure");
  938. alsa_driver_delete (driver);
  939. return 0;
  940. }
  941. if (alsa_driver_set_parameters (driver, frames_per_cycle, user_nperiods, rate)) {
  942. alsa_driver_delete (driver);
  943. return 0;
  944. }
  945. if (snd_pcm_link (driver->capture_handle, driver->playback_handle) != 0) {
  946. driver->capture_and_playback_not_synced = TRUE;
  947. } else {
  948. driver->capture_and_playback_not_synced = FALSE;
  949. }
  950. alsa_driver_hw_specific (driver, hw_monitoring);
  951. return (jack_driver_t *) driver;
  952. }
  953. int
  954. alsa_driver_listen_for_clock_sync_status (alsa_driver_t *driver,
  955. ClockSyncListenerFunction func,
  956. void *arg)
  957. {
  958. ClockSyncListener *csl;
  959. csl = (ClockSyncListener *) malloc (sizeof (ClockSyncListener));
  960. csl->function = func;
  961. csl->arg = arg;
  962. csl->id = driver->next_clock_sync_listener_id++;
  963. pthread_mutex_lock (&driver->clock_sync_lock);
  964. driver->clock_sync_listeners = g_slist_prepend (driver->clock_sync_listeners, csl);
  965. pthread_mutex_unlock (&driver->clock_sync_lock);
  966. return csl->id;
  967. }
  968. int
  969. alsa_driver_stop_listening_to_clock_sync_status (alsa_driver_t *driver, int which)
  970. {
  971. GSList *node;
  972. int ret = -1;
  973. pthread_mutex_lock (&driver->clock_sync_lock);
  974. for (node = driver->clock_sync_listeners; node; node = g_slist_next (node)) {
  975. if (((ClockSyncListener *) node->data)->id == which) {
  976. driver->clock_sync_listeners = g_slist_remove_link (driver->clock_sync_listeners, node);
  977. free (node->data);
  978. g_slist_free_1 (node);
  979. ret = 0;
  980. break;
  981. }
  982. }
  983. pthread_mutex_unlock (&driver->clock_sync_lock);
  984. return ret;
  985. }
  986. void
  987. alsa_driver_clock_sync_notify (alsa_driver_t *driver, channel_t chn, ClockSyncStatus status)
  988. {
  989. GSList *node;
  990. pthread_mutex_lock (&driver->clock_sync_lock);
  991. for (node = driver->clock_sync_listeners; node; node = g_slist_next (node)) {
  992. ClockSyncListener *csl = (ClockSyncListener *) node->data;
  993. csl->function (chn, status, csl->arg);
  994. }
  995. pthread_mutex_unlock (&driver->clock_sync_lock);
  996. }
  997. /* PLUGIN INTERFACE */
  998. jack_driver_t *
  999. driver_initialize (va_list ap)
  1000. {
  1001. nframes_t srate;
  1002. nframes_t frames_per_interrupt;
  1003. unsigned long user_nperiods;
  1004. char *pcm_name;
  1005. int hw_monitoring;
  1006. pcm_name = va_arg (ap, char *);
  1007. frames_per_interrupt = va_arg (ap, nframes_t);
  1008. user_nperiods = va_arg(ap, unsigned long);
  1009. srate = va_arg (ap, nframes_t);
  1010. hw_monitoring = va_arg (ap, int);
  1011. return alsa_driver_new ("alsa_pcm", pcm_name, frames_per_interrupt,
  1012. user_nperiods, srate, hw_monitoring);
  1013. }
  1014. void
  1015. driver_finish (jack_driver_t *driver)
  1016. {
  1017. alsa_driver_delete ((alsa_driver_t *) driver);
  1018. }