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.

2960 lines
79KB

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