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.

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