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

2804 lines
77KB

  1. /* -*- mode: c; c-file-style: "linux"; -*- */
  2. /*
  3. Copyright (C) 2001 Paul Davis
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #define __STDC_FORMAT_MACROS // For inttypes.h to work in C++
  17. #define _GNU_SOURCE /* for strcasestr() from string.h */
  18. #include <math.h>
  19. #include <stdio.h>
  20. #include <memory.h>
  21. #include <unistd.h>
  22. #include <stdlib.h>
  23. #include <errno.h>
  24. #include <stdarg.h>
  25. #include <signal.h>
  26. #include <sys/types.h>
  27. #include <sys/time.h>
  28. #include <string.h>
  29. #include "alsa_driver.h"
  30. #include "hammerfall.h"
  31. #include "hdsp.h"
  32. #include "ice1712.h"
  33. #include "usx2y.h"
  34. #include "generic.h"
  35. #include "memops.h"
  36. #include "JackError.h"
  37. #include "alsa_midi_impl.h"
  38. extern void store_work_time (int);
  39. extern void store_wait_time (int);
  40. extern void show_wait_times ();
  41. extern void show_work_times ();
  42. #undef DEBUG_WAKEUP
  43. char* strcasestr(const char* haystack, const char* needle);
  44. /* Delay (in process calls) before jackd will report an xrun */
  45. #define XRUN_REPORT_DELAY 0
  46. /* Max re-try count for Alsa poll timeout handling */
  47. #define MAX_RETRY_COUNT 5
  48. void
  49. jack_driver_init (jack_driver_t *driver)
  50. {
  51. memset (driver, 0, sizeof (*driver));
  52. driver->attach = 0;
  53. driver->detach = 0;
  54. driver->write = 0;
  55. driver->read = 0;
  56. driver->null_cycle = 0;
  57. driver->bufsize = 0;
  58. driver->start = 0;
  59. driver->stop = 0;
  60. }
  61. void
  62. jack_driver_nt_init (jack_driver_nt_t * driver)
  63. {
  64. memset (driver, 0, sizeof (*driver));
  65. jack_driver_init ((jack_driver_t *) driver);
  66. driver->attach = 0;
  67. driver->detach = 0;
  68. driver->bufsize = 0;
  69. driver->stop = 0;
  70. driver->start = 0;
  71. driver->nt_bufsize = 0;
  72. driver->nt_start = 0;
  73. driver->nt_stop = 0;
  74. driver->nt_attach = 0;
  75. driver->nt_detach = 0;
  76. driver->nt_run_cycle = 0;
  77. }
  78. static int
  79. alsa_driver_prepare (snd_pcm_t *handle, int is_capture)
  80. {
  81. int res = 0;
  82. #ifndef __QNXNTO__
  83. res = snd_pcm_prepare (handle);
  84. #else
  85. res = snd_pcm_plugin_prepare(handle, is_capture);
  86. #endif
  87. if (res < 0) {
  88. jack_error("error preparing: %s", snd_strerror(res));
  89. }
  90. return res;
  91. }
  92. static void
  93. alsa_driver_release_channel_dependent_memory (alsa_driver_t *driver, alsa_device_t *device)
  94. {
  95. bitset_destroy (&device->channels_done);
  96. bitset_destroy (&device->channels_not_done);
  97. device->capture_channel_offset = 0;
  98. device->playback_channel_offset = 0;
  99. /* if we have only 1 device reuse user requested channels, otherwise 0 will attemp to allocate max channels on next setup pass */
  100. if (driver->devices_count != 1) {
  101. driver->capture_nchannels = 0;
  102. driver->playback_nchannels = 0;
  103. device->capture_nchannels = 0;
  104. device->playback_nchannels = 0;
  105. }
  106. if (device->playback_addr) {
  107. free (device->playback_addr);
  108. device->playback_addr = 0;
  109. }
  110. if (device->capture_addr) {
  111. free (device->capture_addr);
  112. device->capture_addr = 0;
  113. }
  114. #ifdef __QNXNTO__
  115. if (device->playback_areas_ptr) {
  116. free(device->playback_areas_ptr);
  117. device->playback_areas_ptr = NULL;
  118. }
  119. if (device->capture_areas_ptr) {
  120. free(device->capture_areas_ptr);
  121. device->capture_areas_ptr = NULL;
  122. }
  123. #endif
  124. if (device->playback_interleave_skip) {
  125. free (device->playback_interleave_skip);
  126. device->playback_interleave_skip = NULL;
  127. }
  128. if (device->capture_interleave_skip) {
  129. free (device->capture_interleave_skip);
  130. device->capture_interleave_skip = NULL;
  131. }
  132. if (device->silent) {
  133. free (device->silent);
  134. device->silent = 0;
  135. }
  136. if (driver->dither_state) {
  137. free (driver->dither_state);
  138. driver->dither_state = 0;
  139. }
  140. }
  141. #ifndef __QNXNTO__
  142. static int
  143. alsa_driver_check_capabilities (alsa_driver_t *driver, alsa_device_t *device)
  144. {
  145. return 0;
  146. }
  147. char* get_control_device_name(const char * device_name);
  148. static int
  149. alsa_driver_check_card_type (alsa_driver_t *driver, alsa_device_t *device)
  150. {
  151. int err;
  152. snd_ctl_card_info_t *card_info;
  153. char * ctl_name;
  154. snd_ctl_card_info_alloca (&card_info);
  155. ctl_name = get_control_device_name(device->playback_name);
  156. // XXX: I don't know the "right" way to do this. Which to use
  157. // driver->alsa_name_playback or driver->alsa_name_capture.
  158. if ((err = snd_ctl_open (&driver->ctl_handle, ctl_name, 0)) < 0) {
  159. jack_error ("control open \"%s\" (%s)", ctl_name,
  160. snd_strerror(err));
  161. } else if ((err = snd_ctl_card_info(driver->ctl_handle, card_info)) < 0) {
  162. jack_error ("control hardware info \"%s\" (%s)",
  163. device->playback_name, snd_strerror (err));
  164. snd_ctl_close (driver->ctl_handle);
  165. }
  166. device->alsa_driver = strdup(snd_ctl_card_info_get_driver (card_info));
  167. free(ctl_name);
  168. return alsa_driver_check_capabilities (driver, device);
  169. }
  170. static int
  171. alsa_driver_hammerfall_hardware (alsa_driver_t *driver, alsa_device_t *device)
  172. {
  173. device->hw = jack_alsa_hammerfall_hw_new (driver);
  174. return 0;
  175. }
  176. static int
  177. alsa_driver_hdsp_hardware (alsa_driver_t *driver, alsa_device_t *device)
  178. {
  179. device->hw = jack_alsa_hdsp_hw_new (driver);
  180. return 0;
  181. }
  182. static int
  183. alsa_driver_ice1712_hardware (alsa_driver_t *driver, alsa_device_t *device)
  184. {
  185. device->hw = jack_alsa_ice1712_hw_new (driver);
  186. return 0;
  187. }
  188. // JACK2
  189. /*
  190. static int
  191. alsa_driver_usx2y_hardware (alsa_driver_t *driver)
  192. {
  193. driver->hw = jack_alsa_usx2y_hw_new (driver);
  194. return 0;
  195. }
  196. */
  197. static int
  198. alsa_driver_generic_hardware (alsa_driver_t *driver, alsa_device_t *device)
  199. {
  200. device->hw = jack_alsa_generic_hw_new (driver);
  201. return 0;
  202. }
  203. static int
  204. alsa_driver_hw_specific (alsa_driver_t *driver, alsa_device_t *device, int hw_monitoring,
  205. int hw_metering)
  206. {
  207. int err;
  208. if (!strcmp(device->alsa_driver, "RME9652")) {
  209. if ((err = alsa_driver_hammerfall_hardware (driver, device)) != 0) {
  210. return err;
  211. }
  212. } else if (!strcmp(device->alsa_driver, "H-DSP")) {
  213. if ((err = alsa_driver_hdsp_hardware (driver, device)) !=0) {
  214. return err;
  215. }
  216. } else if (!strcmp(device->alsa_driver, "ICE1712")) {
  217. if ((err = alsa_driver_ice1712_hardware (driver, device)) !=0) {
  218. return err;
  219. }
  220. }
  221. // JACK2
  222. /*
  223. else if (!strcmp(device->alsa_driver, "USB US-X2Y")) {
  224. if ((err = alsa_driver_usx2y_hardware (driver, device)) !=0) {
  225. return err;
  226. }
  227. }
  228. */
  229. else {
  230. if ((err = alsa_driver_generic_hardware (driver, device)) != 0) {
  231. return err;
  232. }
  233. }
  234. if (device->hw->capabilities & Cap_HardwareMonitoring) {
  235. driver->has_hw_monitoring = TRUE;
  236. /* XXX need to ensure that this is really FALSE or
  237. * TRUE or whatever*/
  238. driver->hw_monitoring = hw_monitoring;
  239. } else {
  240. driver->has_hw_monitoring = FALSE;
  241. driver->hw_monitoring = FALSE;
  242. }
  243. if (device->hw->capabilities & Cap_ClockLockReporting) {
  244. driver->has_clock_sync_reporting = TRUE;
  245. } else {
  246. driver->has_clock_sync_reporting = FALSE;
  247. }
  248. if (device->hw->capabilities & Cap_HardwareMetering) {
  249. driver->has_hw_metering = TRUE;
  250. driver->hw_metering = hw_metering;
  251. } else {
  252. driver->has_hw_metering = FALSE;
  253. driver->hw_metering = FALSE;
  254. }
  255. return 0;
  256. }
  257. #endif
  258. static void
  259. alsa_driver_setup_io_function_pointers (alsa_driver_t *driver, alsa_device_t *device)
  260. {
  261. if (device->playback_handle) {
  262. if (SND_PCM_FORMAT_FLOAT_LE == device->playback_sample_format) {
  263. device->write_via_copy = sample_move_dS_floatLE;
  264. } else {
  265. switch (device->playback_sample_bytes) {
  266. case 2:
  267. switch (driver->dither) {
  268. case Rectangular:
  269. jack_info("Rectangular dithering at 16 bits");
  270. device->write_via_copy = device->quirk_bswap?
  271. sample_move_dither_rect_d16_sSs:
  272. sample_move_dither_rect_d16_sS;
  273. break;
  274. case Triangular:
  275. jack_info("Triangular dithering at 16 bits");
  276. device->write_via_copy = device->quirk_bswap?
  277. sample_move_dither_tri_d16_sSs:
  278. sample_move_dither_tri_d16_sS;
  279. break;
  280. case Shaped:
  281. jack_info("Noise-shaped dithering at 16 bits");
  282. device->write_via_copy = device->quirk_bswap?
  283. sample_move_dither_shaped_d16_sSs:
  284. sample_move_dither_shaped_d16_sS;
  285. break;
  286. default:
  287. device->write_via_copy = device->quirk_bswap?
  288. sample_move_d16_sSs :
  289. sample_move_d16_sS;
  290. break;
  291. }
  292. break;
  293. case 3: /* NO DITHER */
  294. device->write_via_copy = device->quirk_bswap?
  295. sample_move_d24_sSs:
  296. sample_move_d24_sS;
  297. break;
  298. case 4: /* NO DITHER */
  299. device->write_via_copy = device->quirk_bswap?
  300. sample_move_d32u24_sSs:
  301. sample_move_d32u24_sS;
  302. break;
  303. default:
  304. jack_error ("impossible sample width (%d) discovered!",
  305. device->playback_sample_bytes);
  306. exit (1);
  307. }
  308. }
  309. }
  310. if (device->capture_handle) {
  311. if (SND_PCM_FORMAT_FLOAT_LE == device->capture_sample_format) {
  312. device->read_via_copy = sample_move_floatLE_sSs;
  313. } else {
  314. switch (device->capture_sample_bytes) {
  315. case 2:
  316. device->read_via_copy = device->quirk_bswap?
  317. sample_move_dS_s16s:
  318. sample_move_dS_s16;
  319. break;
  320. case 3:
  321. device->read_via_copy = device->quirk_bswap?
  322. sample_move_dS_s24s:
  323. sample_move_dS_s24;
  324. break;
  325. case 4:
  326. device->read_via_copy = device->quirk_bswap?
  327. sample_move_dS_s32u24s:
  328. sample_move_dS_s32u24;
  329. break;
  330. }
  331. }
  332. }
  333. }
  334. #ifdef __QNXNTO__
  335. static int
  336. alsa_driver_allocate_buffer(alsa_driver_t *driver, alsa_device_t *device, int frames, int channels, bool is_capture)
  337. {
  338. const long ALIGNMENT = 32;
  339. // TODO driver->playback_sample_bytes
  340. char* const fBuffer = malloc(channels * ((sizeof(alsa_driver_default_format_t)) * frames) + ALIGNMENT);
  341. if(fBuffer) {
  342. /* Provide an 32 byte aligned buffer */
  343. char* const aligned_buffer = (char*)((uintptr_t)fBuffer & ~(ALIGNMENT-1)) + ALIGNMENT;
  344. if(is_capture) {
  345. device->capture_areas_ptr = fBuffer;
  346. device->capture_areas = aligned_buffer;
  347. } else {
  348. device->playback_areas_ptr = fBuffer;
  349. device->playback_areas = aligned_buffer;
  350. }
  351. return 0;
  352. }
  353. jack_error ("ALSA: could not allocate audio buffer");
  354. return -1;
  355. }
  356. static int
  357. alsa_driver_get_setup (alsa_driver_t *driver, alsa_device_t *device, snd_pcm_channel_setup_t *setup, bool is_capture)
  358. {
  359. int err = 0;
  360. memset(setup, 0, sizeof(*setup));
  361. setup->channel = is_capture;
  362. if(is_capture) {
  363. err = snd_pcm_plugin_setup(device->capture_handle, setup);
  364. } else {
  365. err = snd_pcm_plugin_setup(device->playback_handle, setup);
  366. }
  367. if (err < 0) {
  368. jack_error("couldn't get channel setup for %s, err = %s ",
  369. is_capture ? device->capture_name : device->playback_name,
  370. strerror(err));
  371. return -1;
  372. }
  373. return 0;
  374. }
  375. static int
  376. alsa_driver_configure_stream (alsa_driver_t *driver, alsa_device_t *device, char *device_name,
  377. const char *stream_name,
  378. snd_pcm_t *handle,
  379. unsigned int *nperiodsp,
  380. channel_t *nchns,
  381. unsigned long sample_width,
  382. bool is_capture)
  383. {
  384. int err = 0;
  385. snd_pcm_channel_info_t ch_info;
  386. snd_pcm_channel_params_t ch_params;
  387. const unsigned long sample_size = is_capture ? device->capture_sample_bytes
  388. : device->playback_sample_bytes;
  389. memset(&ch_info, 0, sizeof(ch_info));
  390. /*A pointer to a snd_pcm_channel_info_t structure that snd_pcm_plugin_info() fills in with information about the PCM channel.
  391. * Before calling snd_pcm_plugin_info(), set the info structure's channel member to specify the direction.
  392. * This function sets all the other members.*/
  393. ch_info.channel = is_capture;
  394. if ((err = snd_pcm_plugin_info(handle, &ch_info)) < 0) {
  395. jack_error("couldn't get channel info for %s, %s, err = (%s)", stream_name, device_name, snd_strerror(err));
  396. alsa_driver_delete(driver);
  397. return -1;
  398. }
  399. if (!*nchns) {
  400. *nchns = ch_info.max_voices;
  401. }
  402. ch_params.mode = SND_PCM_MODE_BLOCK;
  403. ch_params.start_mode = SND_PCM_START_GO;
  404. ch_params.stop_mode = SND_PCM_STOP_STOP;
  405. ch_params.buf.block.frag_size = driver->frames_per_cycle * *nchns * sample_size;
  406. *nperiodsp = driver->user_nperiods;
  407. ch_params.buf.block.frags_min = 1;
  408. /* the maximal available periods (-1 due to one period is always processed
  409. * by DMA and therefore not free)
  410. */
  411. ch_params.buf.block.frags_max = *nperiodsp - 1;
  412. ch_params.format.interleave = 1;
  413. ch_params.format.rate = driver->frame_rate;
  414. ch_params.format.voices = *nchns;
  415. ch_params.channel = is_capture;
  416. ch_params.format.format = (sample_width == 4) ? SND_PCM_SFMT_S32_LE : SND_PCM_SFMT_S16_LE;
  417. /*Set the configurable parameters for a PCM channel*/
  418. if ((err = snd_pcm_plugin_params(handle, &ch_params)) < 0) {
  419. jack_error("snd_pcm_plugin_params failed for %s %s with err = (%s)", snd_strerror(err), stream_name, device_name);
  420. alsa_driver_delete(driver);
  421. return -1;
  422. }
  423. /*
  424. * The buffer has to be able to hold a full HW audio buffer
  425. * (periods * period_size) because the silence prefill will fill the
  426. * complete buffer
  427. */
  428. return alsa_driver_allocate_buffer(driver, device, driver->frames_per_cycle * *nperiodsp, *nchns, is_capture);
  429. }
  430. #else
  431. static int
  432. alsa_driver_configure_stream (alsa_driver_t *driver, alsa_device_t *device, char *device_name,
  433. const char *stream_name,
  434. snd_pcm_t *handle,
  435. snd_pcm_hw_params_t *hw_params,
  436. snd_pcm_sw_params_t *sw_params,
  437. unsigned int *nperiodsp,
  438. channel_t *nchns,
  439. unsigned long sample_width)
  440. {
  441. int err, format;
  442. unsigned int frame_rate;
  443. snd_pcm_uframes_t stop_th;
  444. static struct {
  445. char Name[40];
  446. snd_pcm_format_t format;
  447. int swapped;
  448. } formats[] = {
  449. {"32bit float little-endian", SND_PCM_FORMAT_FLOAT_LE, IS_LE},
  450. {"32bit integer little-endian", SND_PCM_FORMAT_S32_LE, IS_LE},
  451. {"32bit integer big-endian", SND_PCM_FORMAT_S32_BE, IS_BE},
  452. {"24bit little-endian in 3bytes format", SND_PCM_FORMAT_S24_3LE, IS_LE},
  453. {"24bit big-endian in 3bytes format", SND_PCM_FORMAT_S24_3BE, IS_BE},
  454. {"24bit little-endian", SND_PCM_FORMAT_S24_LE, IS_LE},
  455. {"24bit big-endian", SND_PCM_FORMAT_S24_BE, IS_BE},
  456. {"16bit little-endian", SND_PCM_FORMAT_S16_LE, IS_LE},
  457. {"16bit big-endian", SND_PCM_FORMAT_S16_BE, IS_BE},
  458. };
  459. #define NUMFORMATS (sizeof(formats)/sizeof(formats[0]))
  460. #define FIRST_16BIT_FORMAT 5
  461. if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0) {
  462. jack_error ("ALSA: no playback configurations available (%s)",
  463. snd_strerror (err));
  464. return -1;
  465. }
  466. if ((err = snd_pcm_hw_params_set_periods_integer (handle, hw_params))
  467. < 0) {
  468. jack_error ("ALSA: cannot restrict period size to integral"
  469. " value.");
  470. return -1;
  471. }
  472. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_NONINTERLEAVED)) < 0) {
  473. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
  474. if ((err = snd_pcm_hw_params_set_access (
  475. handle, hw_params,
  476. SND_PCM_ACCESS_MMAP_COMPLEX)) < 0) {
  477. jack_error ("ALSA: mmap-based access is not possible"
  478. " for the %s "
  479. "stream of this audio interface",
  480. stream_name);
  481. return -1;
  482. }
  483. }
  484. }
  485. format = (sample_width == 4) ? 0 : NUMFORMATS - 1;
  486. while (1) {
  487. if ((err = snd_pcm_hw_params_set_format (
  488. handle, hw_params, formats[format].format)) < 0) {
  489. if ((sample_width == 4
  490. ? format++ >= NUMFORMATS - 1
  491. : format-- <= 0)) {
  492. jack_error ("Sorry. The audio interface \"%s\""
  493. " doesn't support any of the"
  494. " hardware sample formats that"
  495. " JACK's alsa-driver can use.",
  496. device_name);
  497. return -1;
  498. }
  499. } else {
  500. if (formats[format].swapped) {
  501. device->quirk_bswap = 1;
  502. } else {
  503. device->quirk_bswap = 0;
  504. }
  505. jack_info ("ALSA: final selected sample format for %s: %s", stream_name, formats[format].Name);
  506. break;
  507. }
  508. }
  509. frame_rate = driver->frame_rate ;
  510. err = snd_pcm_hw_params_set_rate_near (handle, hw_params,
  511. &frame_rate, NULL) ;
  512. driver->frame_rate = frame_rate ;
  513. if (err < 0) {
  514. jack_error ("ALSA: cannot set sample/frame rate to %"
  515. PRIu32 " for %s", driver->frame_rate,
  516. stream_name);
  517. return -1;
  518. }
  519. if (!*nchns) {
  520. /*if not user-specified, try to find the maximum
  521. * number of channels */
  522. unsigned int channels_max ;
  523. err = snd_pcm_hw_params_get_channels_max (hw_params,
  524. &channels_max);
  525. *nchns = channels_max ;
  526. if (*nchns > 1024) {
  527. /* the hapless user is an unwitting victim of
  528. the "default" ALSA PCM device, which can
  529. support up to 16 million channels. since
  530. they can't be bothered to set up a proper
  531. default device, limit the number of
  532. channels for them to a sane default.
  533. */
  534. jack_error (
  535. "You appear to be using the ALSA software \"plug\" layer, probably\n"
  536. "a result of using the \"default\" ALSA device. This is less\n"
  537. "efficient than it could be. Consider using a hardware device\n"
  538. "instead rather than using the plug layer. Usually the name of the\n"
  539. "hardware device that corresponds to the first sound card is hw:0\n"
  540. );
  541. *nchns = 2;
  542. }
  543. }
  544. if ((err = snd_pcm_hw_params_set_channels (handle, hw_params,
  545. *nchns)) < 0) {
  546. jack_error ("ALSA: cannot set channel count to %u for %s",
  547. *nchns, stream_name);
  548. return -1;
  549. }
  550. if ((err = snd_pcm_hw_params_set_period_size (handle, hw_params,
  551. driver->frames_per_cycle,
  552. 0))
  553. < 0) {
  554. jack_error ("ALSA: cannot set period size to %" PRIu32
  555. " frames for %s", driver->frames_per_cycle,
  556. stream_name);
  557. return -1;
  558. }
  559. *nperiodsp = driver->user_nperiods;
  560. snd_pcm_hw_params_set_periods_min (handle, hw_params, nperiodsp, NULL);
  561. if (*nperiodsp < driver->user_nperiods)
  562. *nperiodsp = driver->user_nperiods;
  563. if (snd_pcm_hw_params_set_periods_near (handle, hw_params,
  564. nperiodsp, NULL) < 0) {
  565. jack_error ("ALSA: cannot set number of periods to %u for %s",
  566. *nperiodsp, stream_name);
  567. return -1;
  568. }
  569. if (*nperiodsp < driver->user_nperiods) {
  570. jack_error ("ALSA: got smaller periods %u than %u for %s",
  571. *nperiodsp, (unsigned int) driver->user_nperiods,
  572. stream_name);
  573. return -1;
  574. }
  575. jack_info ("ALSA: use %d periods for %s", *nperiodsp, stream_name);
  576. #if 0
  577. if (!jack_power_of_two(driver->frames_per_cycle)) {
  578. jack_error("JACK: frames must be a power of two "
  579. "(64, 512, 1024, ...)\n");
  580. return -1;
  581. }
  582. #endif
  583. if ((err = snd_pcm_hw_params_set_buffer_size (handle, hw_params,
  584. *nperiodsp *
  585. driver->frames_per_cycle))
  586. < 0) {
  587. jack_error ("ALSA: cannot set buffer length to %" PRIu32
  588. " for %s",
  589. *nperiodsp * driver->frames_per_cycle,
  590. stream_name);
  591. return -1;
  592. }
  593. if ((err = snd_pcm_hw_params (handle, hw_params)) < 0) {
  594. jack_error ("ALSA: cannot set hardware parameters for %s",
  595. stream_name);
  596. return -1;
  597. }
  598. snd_pcm_sw_params_current (handle, sw_params);
  599. if ((err = snd_pcm_sw_params_set_start_threshold (handle, sw_params,
  600. 0U)) < 0) {
  601. jack_error ("ALSA: cannot set start mode for %s", stream_name);
  602. return -1;
  603. }
  604. stop_th = *nperiodsp * driver->frames_per_cycle;
  605. if (driver->soft_mode) {
  606. stop_th = (snd_pcm_uframes_t)-1;
  607. }
  608. if ((err = snd_pcm_sw_params_set_stop_threshold (
  609. handle, sw_params, stop_th)) < 0) {
  610. jack_error ("ALSA: cannot set stop mode for %s",
  611. stream_name);
  612. return -1;
  613. }
  614. if ((err = snd_pcm_sw_params_set_silence_threshold (
  615. handle, sw_params, 0)) < 0) {
  616. jack_error ("ALSA: cannot set silence threshold for %s",
  617. stream_name);
  618. return -1;
  619. }
  620. #if 0
  621. jack_info ("set silence size to %lu * %lu = %lu",
  622. driver->frames_per_cycle, *nperiodsp,
  623. driver->frames_per_cycle * *nperiodsp);
  624. if ((err = snd_pcm_sw_params_set_silence_size (
  625. handle, sw_params,
  626. driver->frames_per_cycle * *nperiodsp)) < 0) {
  627. jack_error ("ALSA: cannot set silence size for %s",
  628. stream_name);
  629. return -1;
  630. }
  631. #endif
  632. if (handle == device->playback_handle)
  633. err = snd_pcm_sw_params_set_avail_min (
  634. handle, sw_params,
  635. driver->frames_per_cycle
  636. * (*nperiodsp - driver->user_nperiods + 1));
  637. else
  638. err = snd_pcm_sw_params_set_avail_min (
  639. handle, sw_params, driver->frames_per_cycle);
  640. if (err < 0) {
  641. jack_error ("ALSA: cannot set avail min for %s", stream_name);
  642. return -1;
  643. }
  644. err = snd_pcm_sw_params_set_tstamp_mode(handle, sw_params, SND_PCM_TSTAMP_ENABLE);
  645. if (err < 0) {
  646. jack_info("Could not enable ALSA time stamp mode for %s (err %d)",
  647. stream_name, err);
  648. }
  649. #if SND_LIB_MAJOR >= 1 && SND_LIB_MINOR >= 1
  650. err = snd_pcm_sw_params_set_tstamp_type(handle, sw_params, SND_PCM_TSTAMP_TYPE_MONOTONIC);
  651. if (err < 0) {
  652. jack_info("Could not use monotonic ALSA time stamps for %s (err %d)",
  653. stream_name, err);
  654. }
  655. #endif
  656. if ((err = snd_pcm_sw_params (handle, sw_params)) < 0) {
  657. jack_error ("ALSA: cannot set software parameters for %s\n",
  658. stream_name);
  659. return -1;
  660. }
  661. return 0;
  662. }
  663. #endif
  664. #ifdef __QNXNTO__
  665. static int
  666. alsa_driver_check_format (unsigned int format)
  667. {
  668. #else
  669. static int
  670. alsa_driver_check_format (snd_pcm_format_t format)
  671. {
  672. #endif
  673. switch (format) {
  674. #ifndef __QNXNTO__
  675. case SND_PCM_FORMAT_FLOAT_LE:
  676. case SND_PCM_FORMAT_S24_3LE:
  677. case SND_PCM_FORMAT_S24_3BE:
  678. case SND_PCM_FORMAT_S24_LE:
  679. case SND_PCM_FORMAT_S24_BE:
  680. case SND_PCM_FORMAT_S32_BE:
  681. case SND_PCM_FORMAT_S16_BE:
  682. #endif
  683. case SND_PCM_FORMAT_S16_LE:
  684. case SND_PCM_FORMAT_S32_LE:
  685. break;
  686. default:
  687. jack_error ("format not supported %d", format);
  688. return -1;
  689. }
  690. return 0;
  691. }
  692. static void
  693. alsa_driver_set_sample_bytes (alsa_driver_t *driver, alsa_device_t *device)
  694. {
  695. #ifdef __QNXNTO__
  696. device->playback_sample_bytes =
  697. snd_pcm_format_width (device->playback_sample_format)
  698. / 8;
  699. device->capture_sample_bytes =
  700. snd_pcm_format_width (device->capture_sample_format)
  701. / 8;
  702. #else
  703. device->playback_sample_bytes =
  704. snd_pcm_format_physical_width (device->playback_sample_format)
  705. / 8;
  706. device->capture_sample_bytes =
  707. snd_pcm_format_physical_width (device->capture_sample_format)
  708. / 8;
  709. #endif
  710. }
  711. static int
  712. alsa_driver_set_parameters (alsa_driver_t *driver,
  713. alsa_device_t *device,
  714. jack_nframes_t frames_per_cycle,
  715. jack_nframes_t user_nperiods,
  716. jack_nframes_t rate)
  717. {
  718. #ifdef __QNXNTO__
  719. snd_pcm_channel_setup_t c_setup;
  720. snd_pcm_channel_setup_t p_setup;
  721. jack_nframes_t p_periods = 0;
  722. jack_nframes_t c_periods = 0;
  723. #else
  724. int dir;
  725. #endif
  726. snd_pcm_uframes_t p_period_size = 0;
  727. snd_pcm_uframes_t c_period_size = 0;
  728. channel_t chn;
  729. unsigned int pr = 0;
  730. unsigned int cr = 0;
  731. int err;
  732. driver->frame_rate = rate;
  733. driver->frames_per_cycle = frames_per_cycle;
  734. driver->user_nperiods = user_nperiods;
  735. jack_info ("configuring C: '%s' P: '%s' %" PRIu32 "Hz, period = %"
  736. PRIu32 " frames (%.1f ms), buffer = %" PRIu32 " periods",
  737. device->capture_name != NULL ? device->capture_name : "-", device->playback_name != NULL ? device->playback_name : "-",
  738. rate, frames_per_cycle, (((float)frames_per_cycle / (float) rate) * 1000.0f), user_nperiods);
  739. if (device->capture_handle) {
  740. #ifdef __QNXNTO__
  741. err = alsa_driver_configure_stream (
  742. driver,
  743. device,
  744. device->capture_name,
  745. "capture",
  746. device->capture_handle,
  747. &driver->capture_nperiods,
  748. &device->capture_nchannels,
  749. device->capture_sample_bytes,
  750. SND_PCM_CHANNEL_CAPTURE);
  751. #else
  752. err = alsa_driver_configure_stream (
  753. driver,
  754. device,
  755. device->capture_name,
  756. "capture",
  757. device->capture_handle,
  758. driver->capture_hw_params,
  759. driver->capture_sw_params,
  760. &driver->capture_nperiods,
  761. &device->capture_nchannels,
  762. device->capture_sample_bytes);
  763. #endif
  764. if (err) {
  765. jack_error ("ALSA: cannot configure capture channel");
  766. return -1;
  767. }
  768. }
  769. if (device->playback_handle) {
  770. #ifdef __QNXNTO__
  771. err = alsa_driver_configure_stream (
  772. driver,
  773. device,
  774. device->playback_name,
  775. "playback",
  776. device->playback_handle,
  777. &driver->playback_nperiods,
  778. &device->playback_nchannels,
  779. device->playback_sample_bytes,
  780. SND_PCM_CHANNEL_PLAYBACK);
  781. #else
  782. err = alsa_driver_configure_stream (
  783. driver,
  784. device,
  785. device->playback_name,
  786. "playback",
  787. device->playback_handle,
  788. driver->playback_hw_params,
  789. driver->playback_sw_params,
  790. &driver->playback_nperiods,
  791. &device->playback_nchannels,
  792. device->playback_sample_bytes);
  793. #endif
  794. if (err) {
  795. jack_error ("ALSA: cannot configure playback channel");
  796. return -1;
  797. }
  798. }
  799. #ifdef __QNXNTO__
  800. if (device->capture_handle) {
  801. err = alsa_driver_get_setup(driver, device, &c_setup, SND_PCM_CHANNEL_CAPTURE);
  802. if(err < 0) {
  803. return -1;
  804. }
  805. cr = c_setup.format.rate;
  806. c_period_size = c_setup.buf.block.frag_size / device->capture_nchannels
  807. / device->capture_sample_bytes;
  808. c_periods = c_setup.buf.block.frags;
  809. device->capture_sample_format = c_setup.format.format;
  810. device->capture_interleaved = c_setup.format.interleave;
  811. }
  812. if (device->playback_handle) {
  813. err = alsa_driver_get_setup(driver, device, &p_setup, SND_PCM_CHANNEL_PLAYBACK);
  814. if(err < 0) {
  815. return -1;
  816. }
  817. pr = p_setup.format.rate;
  818. p_period_size = p_setup.buf.block.frag_size / device->playback_nchannels
  819. / device->playback_sample_bytes;
  820. p_periods = p_setup.buf.block.frags;
  821. device->playback_sample_format = p_setup.format.format;
  822. device->playback_interleaved = p_setup.format.interleave;
  823. }
  824. #else
  825. /* check the rate, since that's rather important */
  826. if (device->playback_handle) {
  827. snd_pcm_hw_params_get_rate (driver->playback_hw_params,
  828. &pr, &dir);
  829. }
  830. if (device->capture_handle) {
  831. snd_pcm_hw_params_get_rate (driver->capture_hw_params,
  832. &cr, &dir);
  833. }
  834. #endif
  835. if (device->capture_handle && device->playback_handle) {
  836. if (cr != pr) {
  837. jack_error ("playback and capture sample rates do "
  838. "not match (%d vs. %d)", pr, cr);
  839. }
  840. /* only change if *both* capture and playback rates
  841. * don't match requested certain hardware actually
  842. * still works properly in full-duplex with slightly
  843. * different rate values between adc and dac
  844. */
  845. if (cr != driver->frame_rate && pr != driver->frame_rate) {
  846. jack_error ("sample rate in use (%d Hz) does not "
  847. "match requested rate (%d Hz)",
  848. cr, driver->frame_rate);
  849. driver->frame_rate = cr;
  850. }
  851. }
  852. else if (device->capture_handle && cr != driver->frame_rate) {
  853. jack_error ("capture sample rate in use (%d Hz) does not "
  854. "match requested rate (%d Hz)",
  855. cr, driver->frame_rate);
  856. driver->frame_rate = cr;
  857. }
  858. else if (device->playback_handle && pr != driver->frame_rate) {
  859. jack_error ("playback sample rate in use (%d Hz) does not "
  860. "match requested rate (%d Hz)",
  861. pr, driver->frame_rate);
  862. driver->frame_rate = pr;
  863. }
  864. /* check the fragment size, since that's non-negotiable */
  865. if (device->playback_handle) {
  866. #ifndef __QNXNTO__
  867. snd_pcm_access_t access;
  868. err = snd_pcm_hw_params_get_period_size (
  869. driver->playback_hw_params, &p_period_size, &dir);
  870. err = snd_pcm_hw_params_get_format (
  871. driver->playback_hw_params,
  872. &(device->playback_sample_format));
  873. err = snd_pcm_hw_params_get_access (driver->playback_hw_params,
  874. &access);
  875. device->playback_interleaved =
  876. (access == SND_PCM_ACCESS_MMAP_INTERLEAVED)
  877. || (access == SND_PCM_ACCESS_MMAP_COMPLEX);
  878. #endif
  879. if (p_period_size != driver->frames_per_cycle) {
  880. jack_error ("alsa_pcm: requested an interrupt every %"
  881. PRIu32
  882. " frames but got %u frames for playback",
  883. driver->frames_per_cycle, p_period_size);
  884. return -1;
  885. }
  886. #ifdef __QNXNTO__
  887. if (p_periods != driver->user_nperiods) {
  888. jack_error ("alsa_pcm: requested %"
  889. PRIu32
  890. " periods but got %"
  891. PRIu32
  892. " periods for playback",
  893. driver->user_nperiods, p_periods);
  894. return -1;
  895. }
  896. #endif
  897. }
  898. if (device->capture_handle) {
  899. #ifndef __QNXNTO__
  900. snd_pcm_access_t access;
  901. err = snd_pcm_hw_params_get_period_size (
  902. driver->capture_hw_params, &c_period_size, &dir);
  903. err = snd_pcm_hw_params_get_format (
  904. driver->capture_hw_params,
  905. &(device->capture_sample_format));
  906. err = snd_pcm_hw_params_get_access (driver->capture_hw_params,
  907. &access);
  908. device->capture_interleaved =
  909. (access == SND_PCM_ACCESS_MMAP_INTERLEAVED)
  910. || (access == SND_PCM_ACCESS_MMAP_COMPLEX);
  911. #endif
  912. if (c_period_size != driver->frames_per_cycle) {
  913. jack_error ("alsa_pcm: requested an interrupt every %"
  914. PRIu32
  915. " frames but got %u frames for capture",
  916. driver->frames_per_cycle, c_period_size);
  917. return -1;
  918. }
  919. #ifdef __QNXNTO__
  920. /* capture buffers can be configured bigger but it should fail
  921. * if they are smaller as expected
  922. */
  923. if (c_periods < driver->user_nperiods) {
  924. jack_error ("alsa_pcm: requested %"
  925. PRIu32
  926. " periods but got %"
  927. PRIu32
  928. " periods for capture",
  929. driver->user_nperiods, c_periods);
  930. return -1;
  931. }
  932. #endif
  933. }
  934. alsa_driver_set_sample_bytes(driver, device);
  935. if (device->playback_handle) {
  936. err = alsa_driver_check_format(device->playback_sample_format);
  937. if(err < 0) {
  938. jack_error ("programming error: unhandled format "
  939. "type for playback");
  940. return -1;
  941. }
  942. }
  943. if (device->capture_handle) {
  944. err = alsa_driver_check_format(device->capture_sample_format);
  945. if(err < 0) {
  946. jack_error ("programming error: unhandled format "
  947. "type for capture");
  948. return -1;
  949. }
  950. }
  951. if (device->playback_interleaved) {
  952. #ifndef __QNXNTO__
  953. const snd_pcm_channel_area_t *my_areas;
  954. snd_pcm_uframes_t offset, frames;
  955. if (snd_pcm_mmap_begin(device->playback_handle,
  956. &my_areas, &offset, &frames) < 0) {
  957. jack_error ("ALSA: %s: mmap areas info error",
  958. device->playback_name);
  959. return -1;
  960. }
  961. // TODO does not work for capture only
  962. device->interleave_unit =
  963. snd_pcm_format_physical_width (
  964. device->playback_sample_format) / 8;
  965. #else
  966. device->interleave_unit = snd_pcm_format_width(
  967. device->playback_sample_format) / 8;
  968. #endif
  969. } else {
  970. device->interleave_unit = 0; /* NOT USED */
  971. }
  972. #ifndef __QNXNTO__
  973. if (device->capture_interleaved) {
  974. const snd_pcm_channel_area_t *my_areas;
  975. snd_pcm_uframes_t offset, frames;
  976. if (snd_pcm_mmap_begin(device->capture_handle,
  977. &my_areas, &offset, &frames) < 0) {
  978. jack_error ("ALSA: %s: mmap areas info error",
  979. device->capture_name);
  980. return -1;
  981. }
  982. }
  983. #endif
  984. if (device->playback_nchannels > device->capture_nchannels) {
  985. device->max_nchannels = device->playback_nchannels;
  986. device->user_nchannels = device->capture_nchannels;
  987. } else {
  988. device->max_nchannels = device->capture_nchannels;
  989. device->user_nchannels = device->playback_nchannels;
  990. }
  991. /* device local channel offset to offsets in driver, used by Jack2 */
  992. device->capture_channel_offset = driver->capture_nchannels;
  993. device->playback_channel_offset = driver->playback_nchannels;
  994. driver->capture_nchannels += device->capture_nchannels;
  995. driver->playback_nchannels += device->playback_nchannels;
  996. alsa_driver_setup_io_function_pointers (driver, device);
  997. /* Allocate and initialize structures that rely on the
  998. channels counts.
  999. Set up the bit pattern that is used to record which
  1000. channels require action on every cycle. any bits that are
  1001. not set after the engine's process() call indicate channels
  1002. that potentially need to be silenced.
  1003. */
  1004. bitset_create (&device->channels_done, device->max_nchannels);
  1005. bitset_create (&device->channels_not_done, device->max_nchannels);
  1006. if (device->playback_handle) {
  1007. device->playback_addr = (char **)
  1008. malloc (sizeof (char *) * device->playback_nchannels);
  1009. memset (device->playback_addr, 0,
  1010. sizeof (char *) * device->playback_nchannels);
  1011. device->playback_interleave_skip = (unsigned long *)
  1012. malloc (sizeof (unsigned long *) * device->playback_nchannels);
  1013. memset (device->playback_interleave_skip, 0,
  1014. sizeof (unsigned long *) * device->playback_nchannels);
  1015. device->silent = (unsigned long *)
  1016. malloc (sizeof (unsigned long)
  1017. * device->playback_nchannels);
  1018. for (chn = 0; chn < device->playback_nchannels; chn++) {
  1019. device->silent[chn] = 0;
  1020. }
  1021. for (chn = 0; chn < device->playback_nchannels; chn++) {
  1022. bitset_add (device->channels_done, chn);
  1023. }
  1024. driver->dither_state = (dither_state_t *) calloc (device->playback_nchannels, sizeof (dither_state_t));
  1025. }
  1026. if (device->capture_handle) {
  1027. device->capture_addr = (char **)
  1028. malloc (sizeof (char *) * device->capture_nchannels);
  1029. memset (device->capture_addr, 0,
  1030. sizeof (char *) * device->capture_nchannels);
  1031. device->capture_interleave_skip = (unsigned long *)
  1032. malloc (sizeof (unsigned long *) * device->capture_nchannels);
  1033. memset (device->capture_interleave_skip, 0,
  1034. sizeof (unsigned long *) * device->capture_nchannels);
  1035. }
  1036. driver->period_usecs =
  1037. (jack_time_t) floor ((((float) driver->frames_per_cycle) /
  1038. driver->frame_rate) * 1000000.0f);
  1039. driver->poll_timeout_ms = (int) floor (1.5f * (driver->period_usecs / 1000.0f));
  1040. // JACK2
  1041. /*
  1042. if (driver->engine) {
  1043. if (driver->engine->set_buffer_size (driver->engine,
  1044. driver->frames_per_cycle)) {
  1045. jack_error ("ALSA: Cannot set engine buffer size to %d (check MIDI)", driver->frames_per_cycle);
  1046. return -1;
  1047. }
  1048. }
  1049. */
  1050. return 0;
  1051. // may be unused
  1052. (void)err;
  1053. }
  1054. int
  1055. alsa_driver_reset_parameters (alsa_driver_t *driver,
  1056. jack_nframes_t frames_per_cycle,
  1057. jack_nframes_t user_nperiods,
  1058. jack_nframes_t rate)
  1059. {
  1060. int err = 0;
  1061. /* XXX unregister old ports ? */
  1062. for (int i = 0; i < driver->devices_count; ++i) {
  1063. alsa_device_t *device = &driver->devices[i];
  1064. alsa_driver_release_channel_dependent_memory (driver, device);
  1065. if ((err = alsa_driver_set_parameters (driver, device, frames_per_cycle, user_nperiods, rate)) != 0) {
  1066. return err;
  1067. }
  1068. }
  1069. return err;
  1070. }
  1071. #ifdef __QNXNTO__
  1072. static int
  1073. snd_pcm_poll_descriptors_count(snd_pcm_t *pcm)
  1074. {
  1075. return 1;
  1076. }
  1077. static int
  1078. snd_pcm_poll_descriptors_revents(snd_pcm_t *pcm, struct pollfd *pfds,
  1079. unsigned int nfds, unsigned short *revents)
  1080. {
  1081. *revents = pfds->revents;
  1082. return 0;
  1083. }
  1084. #endif
  1085. static int
  1086. alsa_driver_get_channel_addresses (alsa_driver_t *driver,
  1087. alsa_device_t *device,
  1088. snd_pcm_uframes_t *capture_avail,
  1089. snd_pcm_uframes_t *playback_avail,
  1090. snd_pcm_uframes_t *capture_offset,
  1091. snd_pcm_uframes_t *playback_offset)
  1092. {
  1093. channel_t chn;
  1094. if (capture_avail) {
  1095. #ifndef __QNXNTO__
  1096. int err;
  1097. if ((err = snd_pcm_mmap_begin (
  1098. device->capture_handle, &device->capture_areas,
  1099. (snd_pcm_uframes_t *) capture_offset,
  1100. (snd_pcm_uframes_t *) capture_avail)) < 0) {
  1101. jack_error ("ALSA: %s: mmap areas info error",
  1102. device->capture_name);
  1103. return -1;
  1104. }
  1105. for (chn = 0; chn < device->capture_nchannels; chn++) {
  1106. const snd_pcm_channel_area_t *a =
  1107. &device->capture_areas[chn];
  1108. device->capture_addr[chn] = (char *) a->addr
  1109. + ((a->first + a->step * *capture_offset) / 8);
  1110. device->capture_interleave_skip[chn] = (unsigned long ) (a->step / 8);
  1111. }
  1112. #else
  1113. for (chn = 0; chn < device->capture_nchannels; chn++) {
  1114. char* const a = device->capture_areas;
  1115. if (device->capture_interleaved) {
  1116. device->capture_addr[chn] = &a[chn * device->capture_sample_bytes];
  1117. device->capture_interleave_skip[chn] = device->capture_nchannels *
  1118. device->capture_sample_bytes;
  1119. } else {
  1120. device->capture_addr[chn] = &a[chn *
  1121. device->capture_sample_bytes * driver->frames_per_cycle];
  1122. device->capture_interleave_skip[chn] = device->capture_sample_bytes;
  1123. }
  1124. }
  1125. #endif
  1126. }
  1127. if (playback_avail) {
  1128. #ifndef __QNXNTO__
  1129. int err;
  1130. if ((err = snd_pcm_mmap_begin (
  1131. device->playback_handle, &device->playback_areas,
  1132. (snd_pcm_uframes_t *) playback_offset,
  1133. (snd_pcm_uframes_t *) playback_avail)) < 0) {
  1134. jack_error ("ALSA: %s: mmap areas info error ",
  1135. device->playback_name);
  1136. return -1;
  1137. }
  1138. for (chn = 0; chn < device->playback_nchannels; chn++) {
  1139. const snd_pcm_channel_area_t *a =
  1140. &device->playback_areas[chn];
  1141. device->playback_addr[chn] = (char *) a->addr
  1142. + ((a->first + a->step * *playback_offset) / 8);
  1143. device->playback_interleave_skip[chn] = (unsigned long ) (a->step / 8);
  1144. }
  1145. #else
  1146. for (chn = 0; chn < device->playback_nchannels; chn++) {
  1147. char* const a = device->playback_areas;
  1148. if (device->playback_interleaved) {
  1149. device->playback_addr[chn] = &a[chn * device->playback_sample_bytes];
  1150. device->playback_interleave_skip[chn] = device->playback_nchannels *
  1151. device->playback_sample_bytes;
  1152. } else {
  1153. device->playback_addr[chn] = &a[chn *
  1154. device->playback_sample_bytes * driver->frames_per_cycle];
  1155. device->playback_interleave_skip[chn] = device->playback_sample_bytes;
  1156. }
  1157. }
  1158. #endif
  1159. }
  1160. return 0;
  1161. }
  1162. #ifdef __QNXNTO__
  1163. static int
  1164. alsa_driver_stream_start(snd_pcm_t *pcm, bool is_capture)
  1165. {
  1166. return snd_pcm_channel_go(pcm, is_capture);
  1167. }
  1168. #else
  1169. static int
  1170. alsa_driver_stream_start(snd_pcm_t *pcm, bool is_capture)
  1171. {
  1172. return snd_pcm_start(pcm);
  1173. }
  1174. #endif
  1175. int
  1176. alsa_driver_start (alsa_driver_t *driver)
  1177. {
  1178. int err;
  1179. snd_pcm_uframes_t poffset, pavail;
  1180. channel_t chn;
  1181. driver->poll_last = 0;
  1182. driver->poll_next = 0;
  1183. for (int i = 0; i < driver->devices_c_count; ++i) {
  1184. alsa_device_t *device = &driver->devices[i];
  1185. if (device->capture_handle && !device->capture_linked) {
  1186. jack_log("prepare device %s", device->capture_name);
  1187. if ((err = alsa_driver_prepare (device->capture_handle, SND_PCM_STREAM_CAPTURE)) < 0) {
  1188. jack_error ("ALSA: failed to prepare device '%s' (%s)", device->capture_name, snd_strerror(err));
  1189. return -1;
  1190. }
  1191. }
  1192. }
  1193. for (int i = 0; i < driver->devices_p_count; ++i) {
  1194. alsa_device_t *device = &driver->devices[i];
  1195. if (device->playback_handle && !device->playback_linked) {
  1196. jack_log("prepare device %s", device->playback_name);
  1197. if ((err = alsa_driver_prepare (device->playback_handle, SND_PCM_STREAM_PLAYBACK)) < 0) {
  1198. jack_error ("ALSA: failed to prepare device '%s' (%s)", device->playback_name, snd_strerror(err));
  1199. return -1;
  1200. }
  1201. }
  1202. }
  1203. // TODO amiartus
  1204. // if (driver->hw_monitoring) {
  1205. // if (driver->input_monitor_mask || driver->all_monitor_in) {
  1206. // if (driver->all_monitor_in) {
  1207. // driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  1208. // } else {
  1209. // driver->hw->set_input_monitor_mask (
  1210. // driver->hw, driver->input_monitor_mask);
  1211. // }
  1212. // } else {
  1213. // driver->hw->set_input_monitor_mask (driver->hw,
  1214. // driver->input_monitor_mask);
  1215. // }
  1216. // }
  1217. driver->capture_nfds = 0;
  1218. driver->playback_nfds = 0;
  1219. for (int i = 0; i < driver->devices_count; ++i) {
  1220. alsa_device_t *device = &driver->devices[i];
  1221. if (device->capture_handle) {
  1222. driver->capture_nfds += snd_pcm_poll_descriptors_count (device->capture_handle);
  1223. }
  1224. if (device->playback_handle) {
  1225. driver->playback_nfds += snd_pcm_poll_descriptors_count (device->playback_handle);
  1226. }
  1227. }
  1228. if (driver->pfd) {
  1229. free (driver->pfd);
  1230. }
  1231. driver->pfd = (struct pollfd *)
  1232. malloc (sizeof (struct pollfd) *
  1233. (driver->playback_nfds + driver->capture_nfds + 2));
  1234. if (driver->midi && !driver->xrun_recovery)
  1235. (driver->midi->start)(driver->midi);
  1236. for (int i = 0; i < driver->devices_p_count; ++i) {
  1237. alsa_device_t *device = &driver->devices[i];
  1238. if (!device->playback_handle) {
  1239. continue;
  1240. }
  1241. const jack_nframes_t silence_frames = driver->frames_per_cycle *
  1242. driver->playback_nperiods;
  1243. /* fill playback buffer with zeroes, and mark
  1244. all fragments as having data.
  1245. */
  1246. #ifndef __QNXNTO__
  1247. pavail = snd_pcm_avail_update (device->playback_handle);
  1248. if (pavail != silence_frames) {
  1249. jack_error ("ALSA: full buffer not available at start");
  1250. return -1;
  1251. }
  1252. #endif
  1253. if (alsa_driver_get_channel_addresses (driver, device,
  1254. 0, &pavail, 0, &poffset)) {
  1255. jack_error("silence failed, get channel addresses");
  1256. return -1;
  1257. }
  1258. /* XXX this is cheating. ALSA offers no guarantee that
  1259. we can access the entire buffer at any one time. It
  1260. works on most hardware tested so far, however, buts
  1261. its a liability in the long run. I think that
  1262. alsa-lib may have a better function for doing this
  1263. here, where the goal is to silence the entire
  1264. buffer.
  1265. */
  1266. for (chn = 0; chn < device->playback_nchannels; chn++) {
  1267. alsa_driver_silence_on_channel (
  1268. driver, device, chn, silence_frames);
  1269. }
  1270. #ifdef __QNXNTO__
  1271. const size_t bytes = silence_frames * device->playback_nchannels *
  1272. device->playback_sample_bytes;
  1273. if ((err = snd_pcm_plugin_write(device->playback_handle,
  1274. device->playback_areas, bytes)) < bytes) {
  1275. jack_error ("ALSA: could not complete write of %"
  1276. PRIu32 " frames: error = %d", silence_frames, err);
  1277. return -1;
  1278. }
  1279. #else
  1280. snd_pcm_mmap_commit (device->playback_handle, poffset, silence_frames);
  1281. #endif
  1282. }
  1283. for (int i = 0; i < driver->devices_c_count; ++i) {
  1284. alsa_device_t *device = &driver->devices[i];
  1285. if (device->capture_handle && !device->capture_linked) {
  1286. jack_log("start device %s", device->capture_name);
  1287. if ((err = alsa_driver_stream_start (device->capture_handle, SND_PCM_STREAM_CAPTURE)) < 0) {
  1288. jack_error ("ALSA: failed to start device C: '%s' (%s)", device->capture_name,
  1289. snd_strerror(err));
  1290. return -1;
  1291. }
  1292. }
  1293. }
  1294. for (int i = 0; i < driver->devices_p_count; ++i) {
  1295. alsa_device_t *device = &driver->devices[i];
  1296. if (device->playback_handle && !device->playback_linked) {
  1297. jack_log("start device %s", device->playback_name);
  1298. if ((err = alsa_driver_stream_start (device->playback_handle, SND_PCM_STREAM_PLAYBACK)) < 0) {
  1299. jack_error ("ALSA: failed to start device P: '%s' (%s)", device->playback_name,
  1300. snd_strerror(err));
  1301. return -1;
  1302. }
  1303. }
  1304. }
  1305. return 0;
  1306. }
  1307. int
  1308. alsa_driver_stop (alsa_driver_t *driver)
  1309. {
  1310. int err;
  1311. // JSList* node;
  1312. // int chn;
  1313. /* silence all capture port buffers, because we might
  1314. be entering offline mode.
  1315. */
  1316. // JACK2
  1317. /*
  1318. for (chn = 0, node = driver->capture_ports; node;
  1319. node = jack_slist_next (node), chn++) {
  1320. jack_port_t* port;
  1321. char* buf;
  1322. jack_nframes_t nframes = driver->engine->control->buffer_size;
  1323. port = (jack_port_t *) node->data;
  1324. buf = jack_port_get_buffer (port, nframes);
  1325. memset (buf, 0, sizeof (jack_default_audio_sample_t) * nframes);
  1326. }
  1327. */
  1328. // JACK2
  1329. ClearOutput();
  1330. for (int i = 0; i < driver->devices_count; ++i) {
  1331. alsa_device_t *device = &driver->devices[i];
  1332. if (device->playback_handle && !device->playback_linked) {
  1333. #ifdef __QNXNTO__
  1334. /* In case of playback: Drain discards the frames */
  1335. err = snd_pcm_plugin_playback_drain(device->playback_handle);
  1336. #else
  1337. jack_info("flush device %s", device->playback_name);
  1338. err = snd_pcm_drop (device->playback_handle);
  1339. #endif
  1340. if (err < 0) {
  1341. jack_error ("ALSA: failed to flush device (%s)", snd_strerror (err));
  1342. return -1;
  1343. }
  1344. }
  1345. if (device->capture_handle && !device->capture_linked) {
  1346. #ifdef __QNXNTO__
  1347. /* In case of capture: Flush discards the frames */
  1348. err = snd_pcm_plugin_flush(device->capture_handle, SND_PCM_CHANNEL_CAPTURE);
  1349. #else
  1350. jack_info("flush device %s", device->capture_name);
  1351. err = snd_pcm_drop (device->capture_handle);
  1352. #endif
  1353. if (err < 0) {
  1354. jack_error ("ALSA: failed to flush device (%s)", snd_strerror (err));
  1355. return -1;
  1356. }
  1357. }
  1358. }
  1359. // TODO: amiartus
  1360. // if (driver->hw_monitoring) {
  1361. // driver->hw->set_input_monitor_mask (driver->hw, 0);
  1362. // }
  1363. // if (driver->midi && !driver->xrun_recovery)
  1364. // (driver->midi->stop)(driver->midi);
  1365. return 0;
  1366. }
  1367. static int
  1368. alsa_driver_restart (alsa_driver_t *driver)
  1369. {
  1370. int res;
  1371. driver->xrun_recovery = 1;
  1372. // JACK2
  1373. /*
  1374. if ((res = driver->nt_stop((struct _jack_driver_nt *) driver))==0)
  1375. res = driver->nt_start((struct _jack_driver_nt *) driver);
  1376. */
  1377. res = Restart();
  1378. driver->xrun_recovery = 0;
  1379. if (res && driver->midi)
  1380. (driver->midi->stop)(driver->midi);
  1381. return res;
  1382. }
  1383. static int
  1384. alsa_driver_get_state (snd_pcm_t *handle, int is_capture)
  1385. {
  1386. #ifdef __QNXNTO__
  1387. int res;
  1388. snd_pcm_channel_status_t status;
  1389. memset (&status, 0, sizeof (status));
  1390. status.channel = is_capture;
  1391. res = snd_pcm_plugin_status(handle, &status);
  1392. if (res < 0) {
  1393. jack_error("status error: %s", snd_strerror(res));
  1394. return -1;
  1395. }
  1396. return status.status;
  1397. #else
  1398. return snd_pcm_state(handle);
  1399. #endif
  1400. }
  1401. static int
  1402. alsa_driver_xrun_recovery (alsa_driver_t *driver, float *delayed_usecs)
  1403. {
  1404. int state;
  1405. for (int i = 0; i < driver->devices_count; ++i) {
  1406. alsa_device_t *device = &driver->devices[i];
  1407. if (device->capture_handle) {
  1408. state = alsa_driver_get_state(device->capture_handle, SND_PCM_STREAM_CAPTURE);
  1409. // TODO overrun
  1410. if (state == SND_PCM_STATE_XRUN) {
  1411. driver->xrun_count++;
  1412. #ifdef __QNXNTO__
  1413. /* Timestamp api's are not available as per QNX Documentation */
  1414. *delayed_usecs = 0;
  1415. #else
  1416. snd_pcm_status_t *status;
  1417. snd_pcm_status_alloca(&status);
  1418. snd_pcm_status(device->capture_handle, status);
  1419. struct timeval now, diff, tstamp;
  1420. snd_pcm_status_get_tstamp(status,&now);
  1421. snd_pcm_status_get_trigger_tstamp(status, &tstamp);
  1422. timersub(&now, &tstamp, &diff);
  1423. *delayed_usecs = diff.tv_sec * 1000000.0 + diff.tv_usec;
  1424. #endif
  1425. jack_log("**** alsa_pcm: xrun of at least %.3f msecs",*delayed_usecs / 1000.0);
  1426. }
  1427. }
  1428. if (device->playback_handle) {
  1429. state = alsa_driver_get_state(device->playback_handle, SND_PCM_STREAM_PLAYBACK);
  1430. // TODO overrun
  1431. if (state == SND_PCM_STATE_XRUN) {
  1432. driver->xrun_count++;
  1433. #ifdef __QNXNTO__
  1434. /* Timestamp api's are not available as per QNX Documentation */
  1435. *delayed_usecs = 0;
  1436. #else
  1437. snd_pcm_status_t *status;
  1438. snd_pcm_status_alloca(&status);
  1439. snd_pcm_status(device->playback_handle, status);
  1440. struct timeval now, diff, tstamp;
  1441. snd_pcm_status_get_tstamp(status,&now);
  1442. snd_pcm_status_get_trigger_tstamp(status, &tstamp);
  1443. timersub(&now, &tstamp, &diff);
  1444. *delayed_usecs = diff.tv_sec * 1000000.0 + diff.tv_usec;
  1445. #endif
  1446. jack_log("**** alsa_pcm: xrun of at least %.3f msecs",*delayed_usecs / 1000.0);
  1447. }
  1448. }
  1449. }
  1450. if (alsa_driver_restart (driver)) {
  1451. jack_error("xrun recovery failed to restart driver");
  1452. return -1;
  1453. }
  1454. return 0;
  1455. }
  1456. static void
  1457. alsa_driver_silence_untouched_channels (alsa_driver_t *driver, alsa_device_t *device,
  1458. jack_nframes_t nframes)
  1459. {
  1460. channel_t chn;
  1461. jack_nframes_t buffer_frames =
  1462. driver->frames_per_cycle * driver->playback_nperiods;
  1463. for (chn = 0; chn < device->playback_nchannels; chn++) {
  1464. if (bitset_contains (device->channels_not_done, chn)) {
  1465. if (device->silent[chn] < buffer_frames) {
  1466. alsa_driver_silence_on_channel_no_mark (
  1467. driver, device, chn, nframes);
  1468. device->silent[chn] += nframes;
  1469. }
  1470. }
  1471. }
  1472. }
  1473. #ifdef __QNXNTO__
  1474. static int
  1475. alsa_driver_poll_descriptors(snd_pcm_t *pcm, struct pollfd *pfds, unsigned int space, bool is_capture)
  1476. {
  1477. pfds->fd = snd_pcm_file_descriptor (pcm, is_capture);
  1478. pfds->events = POLLHUP|POLLNVAL;
  1479. pfds->events |= (is_capture == SND_PCM_STREAM_PLAYBACK) ? POLLOUT : POLLIN;
  1480. return snd_pcm_poll_descriptors_count(pcm);
  1481. }
  1482. static snd_pcm_sframes_t
  1483. alsa_driver_avail(alsa_driver_t *driver, snd_pcm_t *pcm, bool is_capture)
  1484. {
  1485. /* QNX guarantees that after poll() event at least one perido is available */
  1486. return driver->frames_per_cycle;
  1487. }
  1488. #else
  1489. static int
  1490. alsa_driver_poll_descriptors(snd_pcm_t *pcm, struct pollfd *pfds, unsigned int space, bool is_capture)
  1491. {
  1492. return snd_pcm_poll_descriptors(pcm, pfds, space);
  1493. }
  1494. static snd_pcm_sframes_t
  1495. alsa_driver_avail(alsa_driver_t *driver, snd_pcm_t *pcm, bool is_capture)
  1496. {
  1497. return snd_pcm_avail_update(pcm);
  1498. }
  1499. #endif
  1500. static int under_gdb = FALSE;
  1501. jack_nframes_t
  1502. alsa_driver_wait (alsa_driver_t *driver, int extra_fd, int *status, float
  1503. *delayed_usecs)
  1504. {
  1505. snd_pcm_sframes_t avail = 0;
  1506. snd_pcm_sframes_t capture_avail = 0;
  1507. snd_pcm_sframes_t playback_avail = 0;
  1508. int retry_cnt = 0;
  1509. jack_time_t poll_enter;
  1510. jack_time_t poll_ret = 0;
  1511. *status = -1;
  1512. *delayed_usecs = 0;
  1513. int cap_revents[driver->devices_c_count];
  1514. memset(cap_revents, 0, sizeof(cap_revents));
  1515. int play_revents[driver->devices_p_count];
  1516. memset(play_revents, 0, sizeof(play_revents));
  1517. int pfd_cap_count[driver->devices_c_count];
  1518. int pfd_play_count[driver->devices_p_count];
  1519. /* In case if extra_fd is positive number then should be added to pfd_count
  1520. * since at present extra_fd is always negative this is not changed now.
  1521. */
  1522. int pfd_count = driver->capture_nfds + driver->playback_nfds;
  1523. do {
  1524. int poll_result;
  1525. int pfd_index = 0;
  1526. /* collect capture poll descriptors */
  1527. for (int i = 0; i < driver->devices_c_count; ++i) {
  1528. /* this device already triggered poll event before */
  1529. if (cap_revents[i]) {
  1530. continue;
  1531. }
  1532. alsa_device_t *device = &driver->devices[i];
  1533. pfd_cap_count[i] = alsa_driver_poll_descriptors (device->capture_handle,
  1534. &driver->pfd[pfd_index],
  1535. pfd_count - pfd_index,
  1536. SND_PCM_STREAM_CAPTURE);
  1537. if (pfd_cap_count[i] < 0) {
  1538. jack_log ("alsa_driver_poll_descriptors failed pfd_cap_count[%d]=%d", i ,pfd_cap_count[i] );
  1539. /* In case of xrun -EPIPE is returned perform xrun recovery*/
  1540. if (pfd_cap_count[i] == -EPIPE) {
  1541. goto xrun;
  1542. }
  1543. /* for any other error return negative wait status to caller */
  1544. *status = ALSA_DRIVER_WAIT_ERROR;
  1545. return 0;
  1546. } else {
  1547. pfd_index += pfd_cap_count[i];
  1548. }
  1549. }
  1550. /* collect playback poll descriptors */
  1551. for (int i = 0; i < driver->devices_p_count; ++i) {
  1552. /* this device already triggered poll event before */
  1553. if (play_revents[i]) {
  1554. continue;
  1555. }
  1556. alsa_device_t *device = &driver->devices[i];
  1557. pfd_play_count[i] = alsa_driver_poll_descriptors (device->playback_handle,
  1558. &driver->pfd[pfd_index],
  1559. pfd_count - pfd_index,
  1560. SND_PCM_STREAM_PLAYBACK);
  1561. if (pfd_play_count[i] < 0) {
  1562. jack_log ("alsa_driver_poll_descriptors failed pfd_play_count[%d]=%d", i ,pfd_play_count[i] );
  1563. /* In case of xrun -EPIPE is returned perform xrun recovery*/
  1564. if (pfd_cap_count[i] == -EPIPE) {
  1565. goto xrun;
  1566. }
  1567. /* for any other error return negative wait status to caller */
  1568. *status = ALSA_DRIVER_WAIT_ERROR;
  1569. return 0;
  1570. } else {
  1571. pfd_index += pfd_play_count[i];
  1572. }
  1573. }
  1574. if (extra_fd >= 0) {
  1575. driver->pfd[pfd_index].fd = extra_fd;
  1576. driver->pfd[pfd_index].events =
  1577. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  1578. pfd_index++;
  1579. }
  1580. poll_enter = jack_get_microseconds ();
  1581. if (poll_enter > driver->poll_next) {
  1582. /*
  1583. * This processing cycle was delayed past the
  1584. * next due interrupt! Do not account this as
  1585. * a wakeup delay:
  1586. */
  1587. driver->poll_next = 0;
  1588. driver->poll_late++;
  1589. }
  1590. #ifdef __ANDROID__
  1591. poll_result = poll (driver->pfd, nfds, -1); //fix for sleep issue
  1592. #else
  1593. poll_result = poll (driver->pfd,(unsigned int) pfd_count, driver->poll_timeout_ms);
  1594. #endif
  1595. if (poll_result < 0) {
  1596. if (errno == EINTR) {
  1597. const char poll_log[] = "ALSA: poll interrupt";
  1598. // this happens mostly when run
  1599. // under gdb, or when exiting due to a signal
  1600. if (under_gdb) {
  1601. jack_info(poll_log);
  1602. continue;
  1603. }
  1604. jack_error(poll_log);
  1605. *status = -2;
  1606. return 0;
  1607. }
  1608. jack_error ("ALSA: poll call failed (%s)",
  1609. strerror (errno));
  1610. *status = -3;
  1611. return 0;
  1612. }
  1613. poll_ret = jack_get_microseconds ();
  1614. if (poll_result == 0) {
  1615. retry_cnt++;
  1616. if(retry_cnt > MAX_RETRY_COUNT) {
  1617. jack_error ("ALSA: poll time out, polled for %" PRIu64
  1618. " usecs, Reached max retry cnt = %d, Exiting",
  1619. poll_ret - poll_enter, MAX_RETRY_COUNT);
  1620. *status = -5;
  1621. return 0;
  1622. }
  1623. jack_error ("ALSA: poll time out, polled for %" PRIu64
  1624. " usecs, Retrying with a recovery, retry cnt = %d",
  1625. poll_ret - poll_enter, retry_cnt);
  1626. *status = alsa_driver_xrun_recovery (driver, delayed_usecs);
  1627. return 0;
  1628. }
  1629. // JACK2
  1630. SetTime(poll_ret);
  1631. if (extra_fd < 0) {
  1632. if (driver->poll_next && poll_ret > driver->poll_next) {
  1633. *delayed_usecs = poll_ret - driver->poll_next;
  1634. }
  1635. driver->poll_last = poll_ret;
  1636. driver->poll_next = poll_ret + driver->period_usecs;
  1637. } else {
  1638. /* check to see if it was the extra FD that caused us
  1639. * to return from poll */
  1640. if (driver->pfd[pfd_index-1].revents == 0) {
  1641. /* we timed out on the extra fd */
  1642. *status = -4;
  1643. return -1;
  1644. }
  1645. /* if POLLIN was the only bit set, we're OK */
  1646. *status = 0;
  1647. return (driver->pfd[pfd_index-1].revents == POLLIN) ? 0 : -1;
  1648. }
  1649. #ifdef DEBUG_WAKEUP
  1650. fprintf (stderr, "%" PRIu64 ": checked %d fds, started at %" PRIu64 " %" PRIu64 " usecs since poll entered\n",
  1651. poll_ret, desc_count, poll_enter, poll_ret - poll_enter);
  1652. #endif
  1653. pfd_index = 0;
  1654. for (int i = 0; i < driver->devices_c_count; ++i) {
  1655. /* this device already triggered poll event before */
  1656. if (cap_revents[i]) {
  1657. continue;
  1658. }
  1659. alsa_device_t *device = &driver->devices[i];
  1660. unsigned short collect_revs = 0;
  1661. if (snd_pcm_poll_descriptors_revents (device->capture_handle, &driver->pfd[pfd_index],
  1662. pfd_cap_count[i], &collect_revs) != 0) {
  1663. jack_error ("ALSA: capture revents failed");
  1664. *status = -6;
  1665. return 0;
  1666. }
  1667. pfd_index += pfd_cap_count[i];
  1668. if (collect_revs & (POLLERR | POLLIN)) {
  1669. if (collect_revs & POLLERR) {
  1670. /* optimization, no point in polling more if we already have xrun on one device */
  1671. jack_error ("ALSA: device reported xrun C: '%s'", device->capture_name);
  1672. *status = alsa_driver_xrun_recovery (driver, delayed_usecs);
  1673. return 0;
  1674. }
  1675. if (collect_revs & POLLIN) {
  1676. }
  1677. /* on next poll round skip fds from this device */
  1678. cap_revents[i] = collect_revs;
  1679. pfd_count -= pfd_cap_count[i];
  1680. }
  1681. }
  1682. for (int i = 0; i < driver->devices_p_count; ++i) {
  1683. /* this device already triggered poll event before */
  1684. if (play_revents[i]) {
  1685. continue;
  1686. }
  1687. alsa_device_t *device = &driver->devices[i];
  1688. unsigned short collect_revs = 0;
  1689. if (snd_pcm_poll_descriptors_revents (device->playback_handle, &driver->pfd[pfd_index],
  1690. pfd_play_count[i], &collect_revs) != 0) {
  1691. jack_error ("ALSA: playback revents failed");
  1692. *status = -6;
  1693. return 0;
  1694. }
  1695. pfd_index += pfd_play_count[i];
  1696. if (collect_revs & (POLLERR | POLLOUT)) {
  1697. if (collect_revs & POLLERR) {
  1698. /* optimization, no point in polling more if we already have xrun on one device */
  1699. jack_error ("ALSA: device reported xrun P: '%s'", device->playback_name);
  1700. *status = alsa_driver_xrun_recovery (driver, delayed_usecs);
  1701. return 0;
  1702. }
  1703. if (collect_revs & POLLNVAL) {
  1704. jack_error ("ALSA: playback device disconnected");
  1705. *status = -7;
  1706. return 0;
  1707. }
  1708. if (collect_revs & POLLOUT) {
  1709. }
  1710. /* on next poll round skip fds from this device */
  1711. play_revents[i] = collect_revs;
  1712. pfd_count -= pfd_play_count[i];
  1713. }
  1714. }
  1715. } while (pfd_count > 0);
  1716. /* TODO: amiartus; I assume all devices are snd_pcm_link-ed and running on the same clock source,
  1717. * therefore should have the same avail frames, however in practice, this might have to be reworked,
  1718. * since we should check carefully for avail frames on each device, make sure it matches and handle corner cases
  1719. */
  1720. capture_avail = INT_MAX;
  1721. for (int i = 0; i < driver->devices_c_count; ++i) {
  1722. alsa_device_t *device = &driver->devices[i];
  1723. snd_pcm_sframes_t avail = 0;
  1724. if ((avail = alsa_driver_avail (driver, device->capture_handle, SND_PCM_STREAM_CAPTURE)) < 0) {
  1725. if (avail == -EPIPE) {
  1726. jack_error ("ALSA: avail_update xrun on capture dev '%s'", device->capture_name);
  1727. *status = alsa_driver_xrun_recovery (driver, delayed_usecs);
  1728. return 0;
  1729. } else {
  1730. jack_error ("unknown ALSA avail_update return value (%u)", capture_avail);
  1731. }
  1732. }
  1733. jack_log("ALSA: avail frames C: %s %ld", device->capture_name, avail);
  1734. capture_avail = capture_avail < avail ? capture_avail : avail;
  1735. }
  1736. playback_avail = INT_MAX;
  1737. for (int i = 0; i < driver->devices_p_count; ++i) {
  1738. alsa_device_t *device = &driver->devices[i];
  1739. snd_pcm_sframes_t avail = 0;
  1740. if ((avail = alsa_driver_avail (driver, device->playback_handle, SND_PCM_STREAM_PLAYBACK)) < 0) {
  1741. if (avail == -EPIPE) {
  1742. jack_error ("ALSA: avail_update xrun on playback dev '%s'", device->playback_name);
  1743. *status = alsa_driver_xrun_recovery (driver, delayed_usecs);
  1744. return 0;
  1745. } else {
  1746. jack_error ("unknown ALSA avail_update return value (%u)", playback_avail);
  1747. }
  1748. }
  1749. jack_log("ALSA: avail frames P: %s %ld", device->playback_name, avail);
  1750. playback_avail = playback_avail < avail ? playback_avail : avail;
  1751. }
  1752. *status = 0;
  1753. driver->last_wait_ust = poll_ret;
  1754. avail = capture_avail < playback_avail ? capture_avail : playback_avail;
  1755. #ifdef DEBUG_WAKEUP
  1756. fprintf (stderr, "wakeup complete, avail = %lu, pavail = %lu "
  1757. "cavail = %lu\n",
  1758. avail, playback_avail, capture_avail);
  1759. #endif
  1760. /* mark all channels not done for now. read/write will change this */
  1761. for (int i = 0; i < driver->devices_p_count; ++i) {
  1762. alsa_device_t *device = &driver->devices[i];
  1763. bitset_copy (device->channels_not_done, device->channels_done);
  1764. }
  1765. /* constrain the available count to the nearest (round down) number of
  1766. periods.
  1767. */
  1768. return avail - (avail % driver->frames_per_cycle);
  1769. }
  1770. int
  1771. alsa_driver_read (alsa_driver_t *driver, jack_nframes_t nframes)
  1772. {
  1773. snd_pcm_sframes_t contiguous;
  1774. snd_pcm_sframes_t nread;
  1775. snd_pcm_uframes_t offset;
  1776. int err;
  1777. if (nframes > driver->frames_per_cycle) {
  1778. return -1;
  1779. }
  1780. for (size_t i = 0; i < driver->devices_c_count; ++i) {
  1781. alsa_device_t *device = &driver->devices[i];
  1782. nread = 0;
  1783. contiguous = 0;
  1784. jack_nframes_t frames_remain = nframes;
  1785. while (frames_remain) {
  1786. contiguous = frames_remain;
  1787. if (alsa_driver_get_channel_addresses (
  1788. driver,
  1789. device,
  1790. (snd_pcm_uframes_t *) &contiguous,
  1791. (snd_pcm_uframes_t *) 0,
  1792. &offset, 0) < 0) {
  1793. return -1;
  1794. }
  1795. #ifdef __QNXNTO__
  1796. const size_t bytes = contiguous * device->capture_nchannels * device->capture_sample_bytes;
  1797. if ((err = snd_pcm_plugin_read(device->capture_handle,
  1798. device->capture_areas, bytes)) < bytes) {
  1799. jack_error ("ALSA: could not complete read of %"
  1800. PRIu32 " frames: error = %d", contiguous, err);
  1801. return -1;
  1802. }
  1803. #endif
  1804. // JACK2
  1805. /*
  1806. for (chn = 0, node = driver->capture_ports; node;
  1807. node = jack_slist_next (node), chn++) {
  1808. port = (jack_port_t *) node->data;
  1809. if (!jack_port_connected (port)) {
  1810. // no-copy optimization
  1811. continue;
  1812. }
  1813. buf = jack_port_get_buffer (port, orig_nframes);
  1814. alsa_driver_read_from_channel (driver, chn,
  1815. buf + nread, contiguous);
  1816. }
  1817. */
  1818. ReadInput(device, nframes, contiguous, nread);
  1819. #ifndef __QNXNTO__
  1820. if ((err = snd_pcm_mmap_commit (device->capture_handle,
  1821. offset, contiguous)) < 0) {
  1822. jack_error ("ALSA: could not complete read of %"
  1823. PRIu32 " frames: error = %d", contiguous, err);
  1824. return -1;
  1825. }
  1826. #endif
  1827. frames_remain -= contiguous;
  1828. nread += contiguous;
  1829. }
  1830. }
  1831. return 0;
  1832. }
  1833. int
  1834. alsa_driver_write (alsa_driver_t* driver, jack_nframes_t nframes)
  1835. {
  1836. snd_pcm_sframes_t contiguous;
  1837. snd_pcm_sframes_t nwritten;
  1838. snd_pcm_uframes_t offset;
  1839. int err;
  1840. driver->process_count++;
  1841. if (nframes > driver->frames_per_cycle) {
  1842. return -1;
  1843. }
  1844. for (size_t i = 0; i < driver->devices_p_count; ++i) {
  1845. alsa_device_t *device = &driver->devices[i];
  1846. if (driver->midi)
  1847. (driver->midi->write)(driver->midi, nframes);
  1848. nwritten = 0;
  1849. contiguous = 0;
  1850. jack_nframes_t frames_remain = nframes;
  1851. /* check current input monitor request status */
  1852. driver->input_monitor_mask = 0;
  1853. MonitorInput();
  1854. if (driver->hw_monitoring) {
  1855. if ((device->hw->input_monitor_mask
  1856. != driver->input_monitor_mask)
  1857. && !driver->all_monitor_in) {
  1858. device->hw->set_input_monitor_mask (
  1859. device->hw, driver->input_monitor_mask);
  1860. }
  1861. }
  1862. while (frames_remain) {
  1863. contiguous = frames_remain;
  1864. if (alsa_driver_get_channel_addresses (
  1865. driver,
  1866. device,
  1867. (snd_pcm_uframes_t *) 0,
  1868. (snd_pcm_uframes_t *) &contiguous,
  1869. 0, &offset) < 0) {
  1870. return -1;
  1871. }
  1872. // JACK2
  1873. /*
  1874. for (chn = 0, node = driver->playback_ports, mon_node=driver->monitor_ports;
  1875. node;
  1876. node = jack_slist_next (node), chn++) {
  1877. port = (jack_port_t *) node->data;
  1878. if (!jack_port_connected (port)) {
  1879. continue;
  1880. }
  1881. buf = jack_port_get_buffer (port, orig_nframes);
  1882. alsa_driver_write_to_channel (driver, chn,
  1883. buf + nwritten, contiguous);
  1884. if (mon_node) {
  1885. port = (jack_port_t *) mon_node->data;
  1886. if (!jack_port_connected (port)) {
  1887. continue;
  1888. }
  1889. monbuf = jack_port_get_buffer (port, orig_nframes);
  1890. memcpy (monbuf + nwritten, buf + nwritten, contiguous * sizeof(jack_default_audio_sample_t));
  1891. mon_node = jack_slist_next (mon_node);
  1892. }
  1893. }
  1894. */
  1895. // JACK2
  1896. WriteOutput(device, nframes, contiguous, nwritten);
  1897. if (!bitset_empty (device->channels_not_done)) {
  1898. alsa_driver_silence_untouched_channels (driver, device, contiguous);
  1899. }
  1900. #ifdef __QNXNTO__
  1901. const size_t bytes = contiguous * device->playback_nchannels * device->playback_sample_bytes;
  1902. if ((err = snd_pcm_plugin_write(device->playback_handle,
  1903. device->playback_areas, bytes)) < bytes) {
  1904. jack_error ("ALSA: could not complete write of %"
  1905. PRIu32 " frames: error = %d", contiguous, err);
  1906. return -1;
  1907. }
  1908. #else
  1909. if ((err = snd_pcm_mmap_commit (device->playback_handle,
  1910. offset, contiguous)) < 0) {
  1911. jack_error ("ALSA: could not complete playback of %"
  1912. PRIu32 " frames: error = %d", contiguous, err);
  1913. if (err != -EPIPE && err != -ESTRPIPE)
  1914. return -1;
  1915. }
  1916. #endif
  1917. frames_remain -= contiguous;
  1918. nwritten += contiguous;
  1919. }
  1920. }
  1921. return 0;
  1922. }
  1923. #if 0
  1924. static int /* UNUSED */
  1925. alsa_driver_change_sample_clock (alsa_driver_t *driver, SampleClockMode mode)
  1926. {
  1927. return driver->hw->change_sample_clock (driver->hw, mode);
  1928. }
  1929. static void /* UNUSED */
  1930. alsa_driver_request_all_monitor_input (alsa_driver_t *driver, int yn)
  1931. {
  1932. if (driver->hw_monitoring) {
  1933. if (yn) {
  1934. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  1935. } else {
  1936. driver->hw->set_input_monitor_mask (
  1937. driver->hw, driver->input_monitor_mask);
  1938. }
  1939. }
  1940. driver->all_monitor_in = yn;
  1941. }
  1942. static void /* UNUSED */
  1943. alsa_driver_set_hw_monitoring (alsa_driver_t *driver, int yn)
  1944. {
  1945. if (yn) {
  1946. driver->hw_monitoring = TRUE;
  1947. if (driver->all_monitor_in) {
  1948. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  1949. } else {
  1950. driver->hw->set_input_monitor_mask (
  1951. driver->hw, driver->input_monitor_mask);
  1952. }
  1953. } else {
  1954. driver->hw_monitoring = FALSE;
  1955. driver->hw->set_input_monitor_mask (driver->hw, 0);
  1956. }
  1957. }
  1958. static ClockSyncStatus /* UNUSED */
  1959. alsa_driver_clock_sync_status (channel_t chn)
  1960. {
  1961. return Lock;
  1962. }
  1963. #endif
  1964. void
  1965. alsa_driver_delete (alsa_driver_t *driver)
  1966. {
  1967. JSList *node;
  1968. if (driver->midi)
  1969. (driver->midi->destroy)(driver->midi);
  1970. for (node = driver->clock_sync_listeners; node;
  1971. node = jack_slist_next (node)) {
  1972. free (node->data);
  1973. }
  1974. jack_slist_free (driver->clock_sync_listeners);
  1975. if (driver->ctl_handle) {
  1976. snd_ctl_close (driver->ctl_handle);
  1977. driver->ctl_handle = 0;
  1978. }
  1979. for (int i = 0; i < driver->devices_count; ++i) {
  1980. if (driver->devices[i].capture_handle) {
  1981. snd_pcm_close (driver->devices[i].capture_handle);
  1982. driver->devices[i].capture_handle = 0;
  1983. }
  1984. if (driver->devices[i].playback_handle) {
  1985. snd_pcm_close (driver->devices[i].playback_handle);
  1986. driver->devices[i].playback_handle = 0;
  1987. }
  1988. free(driver->devices[i].capture_name);
  1989. free(driver->devices[i].playback_name);
  1990. free(driver->devices[i].alsa_driver);
  1991. alsa_driver_release_channel_dependent_memory (driver, &driver->devices[i]);
  1992. if (driver->devices[i].hw) {
  1993. driver->devices[i].hw->release (driver->devices[i].hw);
  1994. driver->devices[i].hw = 0;
  1995. }
  1996. }
  1997. #ifndef __QNXNTO__
  1998. if (driver->capture_hw_params) {
  1999. snd_pcm_hw_params_free (driver->capture_hw_params);
  2000. driver->capture_hw_params = 0;
  2001. }
  2002. if (driver->playback_hw_params) {
  2003. snd_pcm_hw_params_free (driver->playback_hw_params);
  2004. driver->playback_hw_params = 0;
  2005. }
  2006. if (driver->capture_sw_params) {
  2007. snd_pcm_sw_params_free (driver->capture_sw_params);
  2008. driver->capture_sw_params = 0;
  2009. }
  2010. if (driver->playback_sw_params) {
  2011. snd_pcm_sw_params_free (driver->playback_sw_params);
  2012. driver->playback_sw_params = 0;
  2013. }
  2014. #endif
  2015. if (driver->pfd) {
  2016. free (driver->pfd);
  2017. }
  2018. //JACK2
  2019. //jack_driver_nt_finish ((jack_driver_nt_t *) driver);
  2020. free (driver);
  2021. }
  2022. static char*
  2023. discover_alsa_using_apps ()
  2024. {
  2025. char found[2048];
  2026. char command[5192];
  2027. char* path = getenv ("PATH");
  2028. char* dir;
  2029. size_t flen = 0;
  2030. int card;
  2031. int device;
  2032. size_t cmdlen = 0;
  2033. if (!path) {
  2034. return NULL;
  2035. }
  2036. /* look for lsof and give up if its not in PATH */
  2037. path = strdup (path);
  2038. dir = strtok (path, ":");
  2039. while (dir) {
  2040. char maybe[PATH_MAX+1];
  2041. snprintf (maybe, sizeof(maybe), "%s/lsof", dir);
  2042. if (access (maybe, X_OK) == 0) {
  2043. break;
  2044. }
  2045. dir = strtok (NULL, ":");
  2046. }
  2047. free (path);
  2048. if (!dir) {
  2049. return NULL;
  2050. }
  2051. snprintf (command, sizeof (command), "lsof -Fc0 ");
  2052. cmdlen = strlen (command);
  2053. for (card = 0; card < 8; ++card) {
  2054. for (device = 0; device < 8; ++device) {
  2055. char buf[32];
  2056. snprintf (buf, sizeof (buf), "/dev/snd/pcmC%dD%dp", card, device);
  2057. if (access (buf, F_OK) == 0) {
  2058. snprintf (command+cmdlen, sizeof(command)-cmdlen, "%s ", buf);
  2059. }
  2060. cmdlen = strlen (command);
  2061. snprintf (buf, sizeof (buf), "/dev/snd/pcmC%dD%dc", card, device);
  2062. if (access (buf, F_OK) == 0) {
  2063. snprintf (command+cmdlen, sizeof(command)-cmdlen, "%s ", buf);
  2064. }
  2065. cmdlen = strlen (command);
  2066. }
  2067. }
  2068. FILE* f = popen (command, "r");
  2069. if (!f) {
  2070. return NULL;
  2071. }
  2072. while (!feof (f)) {
  2073. char buf[1024]; /* lsof doesn't output much */
  2074. if (!fgets (buf, sizeof (buf), f)) {
  2075. break;
  2076. }
  2077. if (*buf != 'p') {
  2078. return NULL;
  2079. }
  2080. /* buf contains NULL as a separator between the process field and the command field */
  2081. char *pid = buf;
  2082. ++pid; /* skip leading 'p' */
  2083. char *cmd = pid;
  2084. /* skip to NULL */
  2085. while (*cmd) {
  2086. ++cmd;
  2087. }
  2088. ++cmd; /* skip to 'c' */
  2089. ++cmd; /* skip to first character of command */
  2090. snprintf (found+flen, sizeof (found)-flen, "%s (process ID %s)\n", cmd, pid);
  2091. flen = strlen (found);
  2092. if (flen >= sizeof (found)) {
  2093. break;
  2094. }
  2095. }
  2096. pclose (f);
  2097. if (flen) {
  2098. return strdup (found);
  2099. } else {
  2100. return NULL;
  2101. }
  2102. }
  2103. static int
  2104. alsa_driver_open (alsa_driver_t *driver, alsa_device_t *device, bool is_capture)
  2105. {
  2106. int err = 0;
  2107. char* current_apps;
  2108. if(is_capture) {
  2109. #ifdef __QNXNTO__
  2110. err = snd_pcm_open_name (&device->capture_handle,
  2111. device->capture_name,
  2112. SND_PCM_OPEN_CAPTURE | SND_PCM_OPEN_NONBLOCK);
  2113. #else
  2114. err = snd_pcm_open (&device->capture_handle,
  2115. device->capture_name,
  2116. SND_PCM_STREAM_CAPTURE,
  2117. SND_PCM_NONBLOCK);
  2118. #endif
  2119. } else {
  2120. #ifdef __QNXNTO__
  2121. err = snd_pcm_open_name (&device->playback_handle,
  2122. device->playback_name,
  2123. SND_PCM_OPEN_PLAYBACK | SND_PCM_OPEN_NONBLOCK);
  2124. #else
  2125. err = snd_pcm_open (&device->playback_handle,
  2126. device->playback_name,
  2127. SND_PCM_STREAM_PLAYBACK,
  2128. SND_PCM_NONBLOCK);
  2129. #endif
  2130. }
  2131. if (err < 0) {
  2132. switch (errno) {
  2133. case EBUSY:
  2134. #ifdef __ANDROID__
  2135. jack_error ("\n\nATTENTION: The device \"%s\" is "
  2136. "already in use. Please stop the"
  2137. " application using it and "
  2138. "run JACK again",
  2139. is_capture ? device->alsa_name_capture : device->alsa_name_playback);
  2140. #else
  2141. current_apps = discover_alsa_using_apps ();
  2142. if (current_apps) {
  2143. jack_error ("\n\nATTENTION: The device \"%s\" is "
  2144. "already in use. The following applications "
  2145. " are using your soundcard(s) so you should "
  2146. " check them and stop them as necessary before "
  2147. " trying to start JACK again:\n\n%s",
  2148. is_capture ? device->capture_name : device->playback_name,
  2149. current_apps);
  2150. free (current_apps);
  2151. } else {
  2152. jack_error ("\n\nATTENTION: The device \"%s\" is "
  2153. "already in use. Please stop the"
  2154. " application using it and "
  2155. "run JACK again",
  2156. is_capture ? device->capture_name : device->playback_name);
  2157. }
  2158. #endif
  2159. break;
  2160. case EPERM:
  2161. jack_error ("you do not have permission to open "
  2162. "the audio device \"%s\" for playback",
  2163. is_capture ? device->capture_name : device->playback_name);
  2164. break;
  2165. case EINVAL:
  2166. jack_error ("the state of handle or the mode is invalid "
  2167. "or invalid state change occured \"%s\" for %s",
  2168. is_capture ? device->capture_name : device->playback_name,
  2169. is_capture ? "capture" : "playback");
  2170. break;
  2171. case ENOENT:
  2172. jack_error ("device \"%s\" does not exist for %s",
  2173. is_capture ? device->capture_name : device->playback_name,
  2174. is_capture ? "capture" : "playback");
  2175. break;
  2176. case ENOMEM:
  2177. jack_error ("Not enough memory available for allocation for \"%s\" for %s",
  2178. is_capture ? device->capture_name : device->playback_name,
  2179. is_capture ? "capture" : "playback");
  2180. break;
  2181. case SND_ERROR_INCOMPATIBLE_VERSION:
  2182. jack_error ("Version mismatch \"%s\" for %s",
  2183. is_capture ? device->capture_name : device->playback_name,
  2184. is_capture ? "capture" : "playback");
  2185. break;
  2186. }
  2187. if(is_capture) {
  2188. device->capture_handle = NULL;
  2189. } else {
  2190. device->playback_handle = NULL;
  2191. }
  2192. }
  2193. if (is_capture && device->capture_handle) {
  2194. #ifdef __QNXNTO__
  2195. snd_pcm_nonblock_mode (device->capture_handle, 0);
  2196. #else
  2197. snd_pcm_nonblock (device->capture_handle, 0);
  2198. #endif
  2199. } else if(!is_capture && device->playback_handle) {
  2200. #ifdef __QNXNTO__
  2201. snd_pcm_nonblock_mode (device->playback_handle, 0);
  2202. #else
  2203. snd_pcm_nonblock (device->playback_handle, 0);
  2204. #endif
  2205. }
  2206. return err;
  2207. }
  2208. jack_driver_t *
  2209. alsa_driver_new (char *name, char **capture_alsa_devices,
  2210. char **playback_alsa_devices,
  2211. const char *capture_names,
  2212. const char *playback_names,
  2213. jack_client_t *client,
  2214. jack_nframes_t frames_per_cycle,
  2215. jack_nframes_t user_nperiods,
  2216. jack_nframes_t rate,
  2217. int hw_monitoring,
  2218. int hw_metering,
  2219. int capturing_count,
  2220. int playing_count,
  2221. DitherAlgorithm dither,
  2222. int soft_mode,
  2223. int monitor,
  2224. int user_capture_nchnls,
  2225. int user_playback_nchnls,
  2226. int shorts_first,
  2227. jack_nframes_t capture_latency,
  2228. jack_nframes_t playback_latency,
  2229. alsa_midi_t *midi_driver
  2230. )
  2231. {
  2232. int err;
  2233. alsa_driver_t *driver;
  2234. jack_info ("creating alsa driver ... %s|%" PRIu32 "|%s|%" PRIu32 "|%" PRIu32 "|%" PRIu32
  2235. "|%" PRIu32"|%" PRIu32"|%" PRIu32 "|%s|%s|%s|%s",
  2236. capturing_count > 0 ? capture_names : "-",
  2237. capturing_count,
  2238. playing_count > 0 ? playback_names : "-",
  2239. playing_count,
  2240. frames_per_cycle, user_nperiods, rate,
  2241. user_capture_nchnls,user_playback_nchnls,
  2242. hw_monitoring ? "hwmon": "nomon",
  2243. hw_metering ? "hwmeter":"swmeter",
  2244. soft_mode ? "soft-mode":"-",
  2245. shorts_first ? "16bit":"32bit");
  2246. driver = (alsa_driver_t *) calloc (1, sizeof (alsa_driver_t));
  2247. jack_driver_nt_init ((jack_driver_nt_t *) driver);
  2248. // JACK2
  2249. /*
  2250. driver->nt_attach = (JackDriverNTAttachFunction) alsa_driver_attach;
  2251. driver->nt_detach = (JackDriverNTDetachFunction) alsa_driver_detach;
  2252. driver->read = (JackDriverReadFunction) alsa_driver_read;
  2253. driver->write = (JackDriverReadFunction) alsa_driver_write;
  2254. driver->null_cycle = (JackDriverNullCycleFunction) alsa_driver_null_cycle;
  2255. driver->nt_bufsize = (JackDriverNTBufSizeFunction) alsa_driver_bufsize;
  2256. driver->nt_start = (JackDriverNTStartFunction) alsa_driver_start;
  2257. driver->nt_stop = (JackDriverNTStopFunction) alsa_driver_stop;
  2258. driver->nt_run_cycle = (JackDriverNTRunCycleFunction) alsa_driver_run_cycle;
  2259. */
  2260. driver->ctl_handle = 0;
  2261. driver->capture_frame_latency = capture_latency;
  2262. driver->playback_frame_latency = playback_latency;
  2263. driver->all_monitor_in = FALSE;
  2264. driver->with_monitor_ports = monitor;
  2265. driver->clock_mode = ClockMaster; /* XXX is it? */
  2266. driver->input_monitor_mask = 0; /* XXX is it? */
  2267. driver->pfd = 0;
  2268. driver->playback_nfds = 0;
  2269. driver->capture_nfds = 0;
  2270. driver->dither = dither;
  2271. driver->soft_mode = soft_mode;
  2272. pthread_mutex_init (&driver->clock_sync_lock, 0);
  2273. driver->clock_sync_listeners = 0;
  2274. driver->poll_late = 0;
  2275. driver->xrun_count = 0;
  2276. driver->process_count = 0;
  2277. driver->midi = midi_driver;
  2278. driver->xrun_recovery = 0;
  2279. driver->devices_c_count = capturing_count;
  2280. driver->devices_p_count = playing_count;
  2281. driver->devices_count = capturing_count > playing_count ? capturing_count : playing_count;
  2282. driver->devices = (alsa_device_t*) calloc(driver->devices_count, sizeof(*driver->devices));
  2283. if (driver->devices_count == 1) {
  2284. driver->devices[0].capture_nchannels = user_capture_nchnls;
  2285. driver->devices[0].playback_nchannels = user_playback_nchnls;
  2286. }
  2287. for (int i = 0; i < driver->devices_c_count; ++i) {
  2288. driver->devices[i].capture_sample_bytes = (shorts_first ? 2:4);
  2289. driver->devices[i].capture_name = strdup(capture_alsa_devices[i]);
  2290. err = alsa_driver_open(driver, &driver->devices[i], SND_PCM_STREAM_CAPTURE);
  2291. if (err < 0) {
  2292. jack_error ("\n\nATTENTION: Opening of the capture device \"%s\" failed.",
  2293. driver->devices[i].capture_name);
  2294. alsa_driver_delete (driver);
  2295. return NULL;
  2296. }
  2297. }
  2298. for (int i = 0; i < driver->devices_p_count; ++i) {
  2299. driver->devices[i].playback_sample_bytes = (shorts_first ? 2:4);
  2300. driver->devices[i].playback_name = strdup(playback_alsa_devices[i]);
  2301. #ifndef __QNXNTO__
  2302. if (alsa_driver_check_card_type (driver, &driver->devices[i])) {
  2303. alsa_driver_delete (driver);
  2304. return NULL;
  2305. }
  2306. alsa_driver_hw_specific (driver, &driver->devices[i], hw_monitoring, hw_metering);
  2307. #endif
  2308. err = alsa_driver_open(driver, &driver->devices[i], SND_PCM_STREAM_PLAYBACK);
  2309. if (err < 0) {
  2310. jack_error ("\n\nATTENTION: Opening of the playback device \"%s\" failed.",
  2311. driver->devices[i].playback_name);
  2312. alsa_driver_delete (driver);
  2313. return NULL;
  2314. }
  2315. }
  2316. #ifndef __QNXNTO__
  2317. driver->playback_hw_params = 0;
  2318. driver->capture_hw_params = 0;
  2319. driver->playback_sw_params = 0;
  2320. driver->capture_sw_params = 0;
  2321. if (driver->devices[0].playback_handle) {
  2322. if ((err = snd_pcm_hw_params_malloc (
  2323. &driver->playback_hw_params)) < 0) {
  2324. jack_error ("ALSA: could not allocate playback hw"
  2325. " params structure");
  2326. alsa_driver_delete (driver);
  2327. return NULL;
  2328. }
  2329. if ((err = snd_pcm_sw_params_malloc (
  2330. &driver->playback_sw_params)) < 0) {
  2331. jack_error ("ALSA: could not allocate playback sw"
  2332. " params structure");
  2333. alsa_driver_delete (driver);
  2334. return NULL;
  2335. }
  2336. }
  2337. if (driver->devices[0].capture_handle) {
  2338. if ((err = snd_pcm_hw_params_malloc (
  2339. &driver->capture_hw_params)) < 0) {
  2340. jack_error ("ALSA: could not allocate capture hw"
  2341. " params structure");
  2342. alsa_driver_delete (driver);
  2343. return NULL;
  2344. }
  2345. if ((err = snd_pcm_sw_params_malloc (
  2346. &driver->capture_sw_params)) < 0) {
  2347. jack_error ("ALSA: could not allocate capture sw"
  2348. " params structure");
  2349. alsa_driver_delete (driver);
  2350. return NULL;
  2351. }
  2352. }
  2353. #endif
  2354. for (int i = 0; i < driver->devices_count; ++i) {
  2355. alsa_device_t *device = &driver->devices[i];
  2356. if (alsa_driver_set_parameters (driver, device, frames_per_cycle, user_nperiods, rate)) {
  2357. jack_error ("ALSA: failed to set parameters");
  2358. alsa_driver_delete (driver);
  2359. return NULL;
  2360. }
  2361. }
  2362. for (int i = 0; i < driver->devices_c_count; ++i) {
  2363. alsa_device_t *device = &driver->devices[i];
  2364. device->capture_linked = 0;
  2365. if (i == 0) {
  2366. continue;
  2367. }
  2368. if (snd_pcm_link (driver->devices[0].capture_handle, device->capture_handle) != 0) {
  2369. jack_error ("failed to add device to link group C: '%s'", device->capture_name);
  2370. continue;
  2371. }
  2372. device->capture_linked = 1;
  2373. }
  2374. for (int i = 0; i < driver->devices_p_count; ++i) {
  2375. alsa_device_t *device = &driver->devices[i];
  2376. device->playback_linked = 0;
  2377. snd_pcm_t *handle = driver->devices[0].capture_handle;
  2378. if (!handle) {
  2379. if (i == 0) {
  2380. continue;
  2381. }
  2382. handle = driver->devices[0].playback_handle;
  2383. }
  2384. if (snd_pcm_link (handle, device->playback_handle) != 0) {
  2385. jack_error ("failed to add device to link group P: '%s'", device->playback_name);
  2386. continue;
  2387. }
  2388. device->playback_linked = 1;
  2389. }
  2390. driver->client = client;
  2391. return (jack_driver_t *) driver;
  2392. }
  2393. int
  2394. alsa_driver_stop_listening_to_clock_sync_status (alsa_driver_t *driver,
  2395. unsigned int which)
  2396. {
  2397. JSList *node;
  2398. int ret = -1;
  2399. pthread_mutex_lock (&driver->clock_sync_lock);
  2400. for (node = driver->clock_sync_listeners; node;
  2401. node = jack_slist_next (node)) {
  2402. if (((ClockSyncListener *) node->data)->id == which) {
  2403. driver->clock_sync_listeners =
  2404. jack_slist_remove_link (
  2405. driver->clock_sync_listeners, node);
  2406. free (node->data);
  2407. jack_slist_free_1 (node);
  2408. ret = 0;
  2409. break;
  2410. }
  2411. }
  2412. pthread_mutex_unlock (&driver->clock_sync_lock);
  2413. return ret;
  2414. }
  2415. /* DRIVER "PLUGIN" INTERFACE */
  2416. const char driver_client_name[] = "alsa_pcm";
  2417. void
  2418. driver_finish (jack_driver_t *driver)
  2419. {
  2420. alsa_driver_delete ((alsa_driver_t *) driver);
  2421. }