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.

3095 lines
82KB

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