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.

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