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.

3259 lines
87KB

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