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.

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