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.

3102 lines
82KB

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