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.

3157 lines
83KB

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