jack1 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.

974 lines
30KB

  1. /*
  2. * FireWire Backend for Jack
  3. * using FFADO
  4. * FFADO = Firewire (pro-)audio for linux
  5. *
  6. * http://www.ffado.org
  7. * http://www.jackaudio.org
  8. *
  9. * Copyright (C) 2005-2007 Pieter Palmers
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. /*
  26. * Main Jack driver entry routines
  27. *
  28. */
  29. #include <math.h>
  30. #include <stdio.h>
  31. #include <memory.h>
  32. #include <unistd.h>
  33. #include <stdlib.h>
  34. #include <errno.h>
  35. #include <stdarg.h>
  36. #include <sys/mman.h>
  37. #include <jack/types.h>
  38. #include <jack/internal.h>
  39. #include <jack/engine.h>
  40. #include <sysdeps/time.h>
  41. #include <assert.h>
  42. #include "ffado_driver.h"
  43. #define SAMPLE_MAX_24BIT 8388608.0f
  44. #define SAMPLE_MAX_16BIT 32768.0f
  45. static int ffado_driver_stop (ffado_driver_t *driver);
  46. #define FIREWIRE_REQUIRED_FFADO_API_VERSION 8
  47. // enable verbose messages
  48. static int g_verbose=0;
  49. static int
  50. ffado_driver_attach (ffado_driver_t *driver)
  51. {
  52. char buf[64];
  53. char buf2[64];
  54. channel_t chn;
  55. jack_port_t *port=NULL;
  56. int port_flags;
  57. g_verbose=driver->engine->verbose;
  58. driver->engine->set_buffer_size (driver->engine, driver->period_size);
  59. driver->engine->set_sample_rate (driver->engine, driver->sample_rate);
  60. /* preallocate some buffers such that they don't have to be allocated
  61. in RT context (or from the stack)
  62. */
  63. /* the null buffer is a buffer that contains one period of silence */
  64. driver->nullbuffer = calloc(driver->period_size, sizeof(ffado_sample_t));
  65. if(driver->nullbuffer == NULL) {
  66. printError("could not allocate memory for null buffer");
  67. return -1;
  68. }
  69. /* calloc should do this, but it can't hurt to be sure */
  70. memset(driver->nullbuffer, 0, driver->period_size*sizeof(ffado_sample_t));
  71. /* the scratch buffer is a buffer of one period that can be used as dummy memory */
  72. driver->scratchbuffer = calloc(driver->period_size, sizeof(ffado_sample_t));
  73. if(driver->scratchbuffer == NULL) {
  74. printError("could not allocate memory for scratch buffer");
  75. return -1;
  76. }
  77. /* packetizer thread options */
  78. driver->device_options.realtime=(driver->engine->control->real_time? 1 : 0);
  79. driver->device_options.packetizer_priority = driver->engine->rtpriority;
  80. if (driver->device_options.packetizer_priority > 98) {
  81. driver->device_options.packetizer_priority = 98;
  82. }
  83. if (driver->device_options.packetizer_priority < 1) {
  84. driver->device_options.packetizer_priority = 1;
  85. }
  86. driver->dev = ffado_streaming_init(driver->device_info, driver->device_options);
  87. if(!driver->dev) {
  88. printError("Error creating FFADO streaming device");
  89. return -1;
  90. }
  91. if (driver->device_options.realtime) {
  92. printMessage("Streaming thread running with Realtime scheduling, priority %d",
  93. driver->device_options.packetizer_priority);
  94. } else {
  95. printMessage("Streaming thread running without Realtime scheduling");
  96. }
  97. ffado_streaming_set_audio_datatype(driver->dev, ffado_audio_datatype_float);
  98. /* ports */
  99. port_flags = JackPortIsOutput|JackPortIsPhysical|JackPortIsTerminal;
  100. driver->capture_nchannels=ffado_streaming_get_nb_capture_streams(driver->dev);
  101. driver->capture_channels=calloc(driver->capture_nchannels, sizeof(ffado_capture_channel_t));
  102. if(driver->capture_channels==NULL) {
  103. printError("could not allocate memory for capture channel list");
  104. return -1;
  105. }
  106. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  107. ffado_streaming_get_capture_stream_name(driver->dev, chn, buf, sizeof(buf) - 1);
  108. driver->capture_channels[chn].stream_type=ffado_streaming_get_capture_stream_type(driver->dev, chn);
  109. if(driver->capture_channels[chn].stream_type == ffado_stream_type_audio) {
  110. snprintf(buf2, 64, "C%d_%s",(int)chn,buf); // needed to avoid duplicate names
  111. printMessage ("Registering audio capture port %s", buf2);
  112. if ((port = jack_port_register (driver->client, buf2,
  113. JACK_DEFAULT_AUDIO_TYPE,
  114. port_flags, 0)) == NULL) {
  115. printError (" cannot register port for %s", buf2);
  116. break;
  117. }
  118. driver->capture_ports =
  119. jack_slist_append (driver->capture_ports, port);
  120. // setup port parameters
  121. if (ffado_streaming_set_capture_stream_buffer(driver->dev, chn, NULL)) {
  122. printError(" cannot configure initial port buffer for %s", buf2);
  123. }
  124. if(ffado_streaming_capture_stream_onoff(driver->dev, chn, 1)) {
  125. printError(" cannot enable port %s", buf2);
  126. }
  127. } else if(driver->capture_channels[chn].stream_type == ffado_stream_type_midi) {
  128. snprintf(buf2, 64, "C%d_%s",(int)chn,buf); // needed to avoid duplicate names
  129. printMessage ("Registering midi capture port %s", buf2);
  130. if ((port = jack_port_register (driver->client, buf2,
  131. JACK_DEFAULT_MIDI_TYPE,
  132. port_flags, 0)) == NULL) {
  133. printError (" cannot register port for %s", buf2);
  134. break;
  135. }
  136. driver->capture_ports =
  137. jack_slist_append (driver->capture_ports, port);
  138. // setup port parameters
  139. if (ffado_streaming_set_capture_stream_buffer(driver->dev, chn, NULL)) {
  140. printError(" cannot configure initial port buffer for %s", buf2);
  141. }
  142. if(ffado_streaming_capture_stream_onoff(driver->dev, chn, 1)) {
  143. printError(" cannot enable port %s", buf2);
  144. }
  145. // setup midi unpacker
  146. midi_unpack_init(&driver->capture_channels[chn].midi_unpack);
  147. midi_unpack_reset(&driver->capture_channels[chn].midi_unpack);
  148. // setup the midi buffer
  149. driver->capture_channels[chn].midi_buffer = calloc(driver->period_size, sizeof(uint32_t));
  150. } else {
  151. printMessage ("Don't register capture port %s", buf);
  152. // we have to add a NULL entry in the list to be able to loop over the channels in the read/write routines
  153. driver->capture_ports =
  154. jack_slist_append (driver->capture_ports, NULL);
  155. }
  156. jack_port_set_latency (port, driver->period_size + driver->capture_frame_latency);
  157. }
  158. port_flags = JackPortIsInput|JackPortIsPhysical|JackPortIsTerminal;
  159. driver->playback_nchannels=ffado_streaming_get_nb_playback_streams(driver->dev);
  160. driver->playback_channels=calloc(driver->playback_nchannels, sizeof(ffado_playback_channel_t));
  161. if(driver->playback_channels==NULL) {
  162. printError("could not allocate memory for playback channel list");
  163. return -1;
  164. }
  165. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  166. ffado_streaming_get_playback_stream_name(driver->dev, chn, buf, sizeof(buf) - 1);
  167. driver->playback_channels[chn].stream_type=ffado_streaming_get_playback_stream_type(driver->dev, chn);
  168. if(driver->playback_channels[chn].stream_type == ffado_stream_type_audio) {
  169. snprintf(buf2, 64, "P%d_%s",(int)chn,buf); // needed to avoid duplicate names
  170. printMessage ("Registering audio playback port %s", buf2);
  171. if ((port = jack_port_register (driver->client, buf2,
  172. JACK_DEFAULT_AUDIO_TYPE,
  173. port_flags, 0)) == NULL) {
  174. printError(" cannot register port for %s", buf2);
  175. break;
  176. }
  177. driver->playback_ports =
  178. jack_slist_append (driver->playback_ports, port);
  179. // setup port parameters
  180. if (ffado_streaming_set_playback_stream_buffer(driver->dev, chn, NULL)) {
  181. printError(" cannot configure initial port buffer for %s", buf2);
  182. }
  183. if(ffado_streaming_playback_stream_onoff(driver->dev, chn, 1)) {
  184. printError(" cannot enable port %s", buf2);
  185. }
  186. } else if(driver->playback_channels[chn].stream_type == ffado_stream_type_midi) {
  187. snprintf(buf2, 64, "P%d_%s",(int)chn,buf); // needed to avoid duplicate names
  188. printMessage ("Registering midi playback port %s", buf2);
  189. if ((port = jack_port_register (driver->client, buf2,
  190. JACK_DEFAULT_MIDI_TYPE,
  191. port_flags, 0)) == NULL) {
  192. printError(" cannot register port for %s", buf2);
  193. break;
  194. }
  195. driver->playback_ports =
  196. jack_slist_append (driver->playback_ports, port);
  197. // setup port parameters
  198. if (ffado_streaming_set_playback_stream_buffer(driver->dev, chn, NULL)) {
  199. printError(" cannot configure initial port buffer for %s", buf2);
  200. }
  201. if(ffado_streaming_playback_stream_onoff(driver->dev, chn, 1)) {
  202. printError(" cannot enable port %s", buf2);
  203. }
  204. // setup midi packer
  205. midi_pack_reset(&driver->playback_channels[chn].midi_pack);
  206. // setup the midi buffer
  207. driver->playback_channels[chn].midi_buffer = calloc(driver->period_size, sizeof(uint32_t));
  208. } else {
  209. printMessage ("Don't register playback port %s", buf);
  210. // we have to add a NULL entry in the list to be able to loop over the channels in the read/write routines
  211. driver->playback_ports =
  212. jack_slist_append (driver->playback_ports, NULL);
  213. }
  214. jack_port_set_latency (port, (driver->period_size * (driver->device_options.nb_buffers - 1)) + driver->playback_frame_latency);
  215. }
  216. if(ffado_streaming_prepare(driver->dev)) {
  217. printError("Could not prepare streaming device!");
  218. return -1;
  219. }
  220. return jack_activate (driver->client);
  221. }
  222. static int
  223. ffado_driver_detach (ffado_driver_t *driver)
  224. {
  225. JSList *node;
  226. unsigned int chn;
  227. if (driver->engine == NULL) {
  228. return 0;
  229. }
  230. for (node = driver->capture_ports; node;
  231. node = jack_slist_next (node)) {
  232. // Don't try to unregister NULL entries added for non-audio
  233. // ffado ports by ffado_driver_attach().
  234. if (node->data != NULL) {
  235. jack_port_unregister (driver->client,
  236. ((jack_port_t *) node->data));
  237. }
  238. }
  239. jack_slist_free (driver->capture_ports);
  240. driver->capture_ports = 0;
  241. for (node = driver->playback_ports; node;
  242. node = jack_slist_next (node)) {
  243. if (node->data != NULL) {
  244. jack_port_unregister (driver->client,
  245. ((jack_port_t *) node->data));
  246. }
  247. }
  248. jack_slist_free (driver->playback_ports);
  249. driver->playback_ports = 0;
  250. ffado_streaming_finish(driver->dev);
  251. driver->dev=NULL;
  252. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  253. if(driver->capture_channels[chn].midi_buffer)
  254. free(driver->capture_channels[chn].midi_buffer);
  255. }
  256. free(driver->capture_channels);
  257. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  258. if(driver->playback_channels[chn].midi_buffer)
  259. free(driver->playback_channels[chn].midi_buffer);
  260. }
  261. free(driver->playback_channels);
  262. free(driver->nullbuffer);
  263. free(driver->scratchbuffer);
  264. return 0;
  265. }
  266. static int
  267. ffado_driver_read (ffado_driver_t * driver, jack_nframes_t nframes)
  268. {
  269. channel_t chn;
  270. int nb_connections;
  271. JSList *node;
  272. jack_port_t* port;
  273. printEnter();
  274. for (chn = 0, node = driver->capture_ports; node; node = jack_slist_next (node), chn++) {
  275. if(driver->capture_channels[chn].stream_type == ffado_stream_type_audio) {
  276. port = (jack_port_t *) node->data;
  277. nb_connections = jack_port_connected (port);
  278. /* if there are no connections, use the dummy buffer and disable the stream */
  279. if(nb_connections) {
  280. ffado_streaming_capture_stream_onoff(driver->dev, chn, 1);
  281. ffado_streaming_set_capture_stream_buffer(driver->dev, chn, (char *)(jack_port_get_buffer (port, nframes)));
  282. } else {
  283. ffado_streaming_capture_stream_onoff(driver->dev, chn, 0);
  284. ffado_streaming_set_capture_stream_buffer(driver->dev, chn, (char *)(driver->scratchbuffer));
  285. }
  286. } else if (driver->capture_channels[chn].stream_type == ffado_stream_type_midi) {
  287. port = (jack_port_t *) node->data;
  288. nb_connections = jack_port_connected (port);
  289. if(nb_connections) {
  290. ffado_streaming_capture_stream_onoff(driver->dev, chn, 1);
  291. } else {
  292. ffado_streaming_capture_stream_onoff(driver->dev, chn, 0);
  293. }
  294. /* always set a buffer */
  295. ffado_streaming_set_capture_stream_buffer(driver->dev, chn,
  296. (char *)(driver->capture_channels[chn].midi_buffer));
  297. } else { /* ensure a valid buffer */
  298. ffado_streaming_set_capture_stream_buffer(driver->dev, chn, (char *)(driver->scratchbuffer));
  299. ffado_streaming_capture_stream_onoff(driver->dev, chn, 0);
  300. }
  301. }
  302. /* now transfer the buffers */
  303. ffado_streaming_transfer_capture_buffers(driver->dev);
  304. /* process the midi data */
  305. for (chn = 0, node = driver->capture_ports; node; node = jack_slist_next (node), chn++) {
  306. if (driver->capture_channels[chn].stream_type == ffado_stream_type_midi) {
  307. jack_default_audio_sample_t* buf;
  308. int i;
  309. int done;
  310. uint32_t *midi_buffer = driver->capture_channels[chn].midi_buffer;
  311. midi_unpack_t *midi_unpack = &driver->capture_channels[chn].midi_unpack;
  312. port = (jack_port_t *) node->data;
  313. nb_connections = jack_port_connected (port);
  314. buf = jack_port_get_buffer (port, nframes);
  315. /* if the returned buffer is invalid, discard the midi data */
  316. jack_midi_clear_buffer(buf);
  317. /* no connections means no processing */
  318. if(nb_connections == 0) continue;
  319. /* else unpack
  320. note that libffado guarantees that midi bytes are on 8-byte aligned indexes */
  321. for(i = 0; i < nframes; i += 8) {
  322. if(midi_buffer[i] & 0xFF000000) {
  323. done = midi_unpack_buf(midi_unpack, (unsigned char *)(midi_buffer+i), 1, buf, i);
  324. if (done != 1) {
  325. printError("MIDI buffer overflow in channel %d\n", chn);
  326. break;
  327. }
  328. }
  329. }
  330. }
  331. }
  332. printExit();
  333. return 0;
  334. }
  335. static int
  336. ffado_driver_write (ffado_driver_t * driver, jack_nframes_t nframes)
  337. {
  338. channel_t chn;
  339. int nb_connections;
  340. JSList *node;
  341. jack_port_t *port;
  342. printEnter();
  343. driver->process_count++;
  344. if (driver->engine->freewheeling) {
  345. return 0;
  346. }
  347. for (chn = 0, node = driver->playback_ports; node; node = jack_slist_next (node), chn++) {
  348. if(driver->playback_channels[chn].stream_type == ffado_stream_type_audio) {
  349. port = (jack_port_t *) node->data;
  350. nb_connections = jack_port_connected (port);
  351. /* use the silent buffer + disable if there are no connections */
  352. if(nb_connections) {
  353. ffado_streaming_playback_stream_onoff(driver->dev, chn, 1);
  354. ffado_streaming_set_playback_stream_buffer(driver->dev, chn, (char *)(jack_port_get_buffer (port, nframes)));
  355. } else {
  356. ffado_streaming_playback_stream_onoff(driver->dev, chn, 0);
  357. ffado_streaming_set_playback_stream_buffer(driver->dev, chn, (char *)(driver->nullbuffer));
  358. }
  359. } else if (driver->playback_channels[chn].stream_type == ffado_stream_type_midi) {
  360. jack_default_audio_sample_t* buf;
  361. int nevents;
  362. int i;
  363. midi_pack_t *midi_pack = &driver->playback_channels[chn].midi_pack;
  364. uint32_t *midi_buffer = driver->playback_channels[chn].midi_buffer;
  365. int min_next_pos = 0;
  366. port = (jack_port_t *) node->data;
  367. nb_connections = jack_port_connected (port);
  368. /* skip if no connections */
  369. if(nb_connections == 0) {
  370. ffado_streaming_set_playback_stream_buffer(driver->dev, chn, (char *)(driver->nullbuffer));
  371. ffado_streaming_playback_stream_onoff(driver->dev, chn, 0);
  372. continue;
  373. }
  374. memset(midi_buffer, 0, nframes * sizeof(uint32_t));
  375. ffado_streaming_set_playback_stream_buffer(driver->dev, chn, (char *)(midi_buffer));
  376. ffado_streaming_playback_stream_onoff(driver->dev, chn, 1);
  377. /* check if we still have to process bytes from the previous period */
  378. /*
  379. if(driver->playback_channels[chn].nb_overflow_bytes) {
  380. printMessage("have to process %d bytes from previous period", driver->playback_channels[chn].nb_overflow_bytes);
  381. }
  382. */
  383. for (i=0; i<driver->playback_channels[chn].nb_overflow_bytes; ++i) {
  384. midi_buffer[min_next_pos] = 0x01000000 | (driver->playback_channels[chn].overflow_buffer[i] & 0xFF);
  385. min_next_pos += 8;
  386. }
  387. driver->playback_channels[chn].nb_overflow_bytes=0;
  388. /* process the events in this period */
  389. buf = jack_port_get_buffer (port, nframes);
  390. nevents = jack_midi_get_event_count(buf);
  391. for (i=0; i<nevents; ++i) {
  392. int j;
  393. jack_midi_event_t event;
  394. jack_midi_event_get(&event, buf, i);
  395. midi_pack_event(midi_pack, &event);
  396. /* floor the initial position to be a multiple of 8 */
  397. int pos = event.time & 0xFFFFFFF8;
  398. for(j = 0; j < event.size; j++) {
  399. /* make sure we don't overwrite a previous byte */
  400. while(pos < min_next_pos && pos < nframes) {
  401. /* printMessage("have to correct pos to %d", pos); */
  402. pos += 8;
  403. }
  404. if(pos >= nframes) {
  405. int f;
  406. /* printMessage("midi message crosses period boundary"); */
  407. driver->playback_channels[chn].nb_overflow_bytes = event.size - j;
  408. if(driver->playback_channels[chn].nb_overflow_bytes > MIDI_OVERFLOW_BUFFER_SIZE) {
  409. printError("too much midi bytes cross period boundary");
  410. driver->playback_channels[chn].nb_overflow_bytes = MIDI_OVERFLOW_BUFFER_SIZE;
  411. }
  412. /* save the bytes that still have to be transmitted in the next period */
  413. for(f = 0; f < driver->playback_channels[chn].nb_overflow_bytes; f++) {
  414. driver->playback_channels[chn].overflow_buffer[f] = event.buffer[j+f];
  415. }
  416. /* exit since we can't transmit anything anymore.
  417. the rate should be controlled */
  418. if(i < nevents-1) {
  419. printError("%d midi events lost due to period crossing", nevents-i-1);
  420. }
  421. break;
  422. } else {
  423. midi_buffer[pos] = 0x01000000 | (event.buffer[j] & 0xFF);
  424. pos += 8;
  425. min_next_pos = pos;
  426. }
  427. }
  428. }
  429. } else { /* ensure a valid buffer */
  430. ffado_streaming_set_playback_stream_buffer(driver->dev, chn, (char *)(driver->nullbuffer));
  431. ffado_streaming_playback_stream_onoff(driver->dev, chn, 0);
  432. }
  433. }
  434. ffado_streaming_transfer_playback_buffers(driver->dev);
  435. printExit();
  436. return 0;
  437. }
  438. //static inline jack_nframes_t
  439. static jack_nframes_t
  440. ffado_driver_wait (ffado_driver_t *driver, int extra_fd, int *status,
  441. float *delayed_usecs)
  442. {
  443. jack_time_t wait_enter;
  444. jack_time_t wait_ret;
  445. ffado_wait_response response;
  446. printEnter();
  447. wait_enter = jack_get_microseconds ();
  448. if (wait_enter > driver->wait_next) {
  449. /*
  450. * This processing cycle was delayed past the
  451. * next due interrupt! Do not account this as
  452. * a wakeup delay:
  453. */
  454. driver->wait_next = 0;
  455. driver->wait_late++;
  456. }
  457. // *status = -2; interrupt
  458. // *status = -3; timeout
  459. // *status = -4; extra FD
  460. response = ffado_streaming_wait(driver->dev);
  461. wait_ret = jack_get_microseconds ();
  462. if (driver->wait_next && wait_ret > driver->wait_next) {
  463. *delayed_usecs = wait_ret - driver->wait_next;
  464. }
  465. driver->wait_last = wait_ret;
  466. driver->wait_next = wait_ret + driver->period_usecs;
  467. driver->engine->transport_cycle_start (driver->engine, wait_ret);
  468. if (response == ffado_wait_ok) {
  469. // all good
  470. *status=0;
  471. } else if (response == ffado_wait_xrun) {
  472. // xrun happened, but it's handled
  473. *status=0;
  474. return 0;
  475. } else if (response == ffado_wait_error) {
  476. // an error happened (unhandled xrun)
  477. // this should be fatal
  478. *status=-1;
  479. return 0;
  480. } else if (response == ffado_wait_shutdown) {
  481. // we are to shutdown the ffado system
  482. // this should be fatal
  483. *status=-1;
  484. return 0;
  485. } else {
  486. // we don't know about this response code
  487. printError("unknown wait response (%d) from ffado", response);
  488. // this should be fatal
  489. *status=-1;
  490. return 0;
  491. }
  492. driver->last_wait_ust = wait_ret;
  493. // FIXME: this should do something more useful
  494. *delayed_usecs = 0;
  495. printExit();
  496. return driver->period_size;
  497. }
  498. static int
  499. ffado_driver_run_cycle (ffado_driver_t *driver)
  500. {
  501. jack_engine_t *engine = driver->engine;
  502. int wait_status=0;
  503. float delayed_usecs=0.0;
  504. jack_nframes_t nframes = ffado_driver_wait (driver, -1,
  505. &wait_status, &delayed_usecs);
  506. if ((wait_status < 0)) {
  507. printError( "wait status < 0! (= %d)",wait_status);
  508. return -1;
  509. }
  510. if ((nframes == 0)) {
  511. /* we detected an xrun and restarted: notify
  512. * clients about the delay. */
  513. printMessage("xrun detected");
  514. engine->delay (engine, delayed_usecs);
  515. return 0;
  516. }
  517. return engine->run_cycle (engine, nframes, delayed_usecs);
  518. }
  519. /*
  520. * in a null cycle we should discard the input and write silence to the outputs
  521. */
  522. static int
  523. ffado_driver_null_cycle (ffado_driver_t* driver, jack_nframes_t nframes)
  524. {
  525. channel_t chn;
  526. JSList *node;
  527. ffado_streaming_stream_type stream_type;
  528. printEnter();
  529. if (driver->engine->freewheeling) {
  530. return 0;
  531. }
  532. // write silence to buffer
  533. for (chn = 0, node = driver->playback_ports; node; node = jack_slist_next (node), chn++) {
  534. stream_type=ffado_streaming_get_playback_stream_type(driver->dev, chn);
  535. if(stream_type == ffado_stream_type_audio) {
  536. ffado_streaming_set_playback_stream_buffer(driver->dev, chn, (char *)(driver->nullbuffer));
  537. }
  538. }
  539. ffado_streaming_transfer_playback_buffers(driver->dev);
  540. // read & discard from input ports
  541. for (chn = 0, node = driver->capture_ports; node; node = jack_slist_next (node), chn++) {
  542. stream_type=ffado_streaming_get_capture_stream_type(driver->dev, chn);
  543. if(stream_type == ffado_stream_type_audio) {
  544. ffado_streaming_set_capture_stream_buffer(driver->dev, chn, (char *)(driver->scratchbuffer));
  545. }
  546. }
  547. ffado_streaming_transfer_capture_buffers(driver->dev);
  548. printExit();
  549. return 0;
  550. }
  551. static int
  552. ffado_driver_start (ffado_driver_t *driver)
  553. {
  554. int retval=0;
  555. if((retval=ffado_streaming_start(driver->dev))) {
  556. printError("Could not start streaming threads: %d", retval);
  557. return retval;
  558. }
  559. return 0;
  560. }
  561. static int
  562. ffado_driver_stop (ffado_driver_t *driver)
  563. {
  564. int retval=0;
  565. if((retval=ffado_streaming_stop(driver->dev))) {
  566. printError("Could not stop streaming threads");
  567. return retval;
  568. }
  569. return 0;
  570. }
  571. static int
  572. ffado_driver_bufsize (ffado_driver_t* driver, jack_nframes_t nframes)
  573. {
  574. printError("Buffer size change requested but not supported!!!");
  575. /*
  576. driver->period_size = nframes;
  577. driver->period_usecs =
  578. (jack_time_t) floor ((((float) nframes) / driver->sample_rate)
  579. * 1000000.0f);
  580. */
  581. /* tell the engine to change its buffer size */
  582. //driver->engine->set_buffer_size (driver->engine, nframes);
  583. return -1; // unsupported
  584. }
  585. typedef void (*JackDriverFinishFunction) (jack_driver_t *);
  586. ffado_driver_t *
  587. ffado_driver_new (jack_client_t * client,
  588. char *name,
  589. ffado_jack_settings_t *params)
  590. {
  591. ffado_driver_t *driver;
  592. if(ffado_get_api_version() != FIREWIRE_REQUIRED_FFADO_API_VERSION) {
  593. printError("Incompatible libffado version! (%s)", ffado_get_version());
  594. return NULL;
  595. }
  596. printMessage("Starting firewire backend (%s)", ffado_get_version());
  597. driver = calloc (1, sizeof (ffado_driver_t));
  598. /* Setup the jack interfaces */
  599. jack_driver_nt_init ((jack_driver_nt_t *) driver);
  600. driver->nt_attach = (JackDriverNTAttachFunction) ffado_driver_attach;
  601. driver->nt_detach = (JackDriverNTDetachFunction) ffado_driver_detach;
  602. driver->nt_start = (JackDriverNTStartFunction) ffado_driver_start;
  603. driver->nt_stop = (JackDriverNTStopFunction) ffado_driver_stop;
  604. driver->nt_run_cycle = (JackDriverNTRunCycleFunction) ffado_driver_run_cycle;
  605. driver->null_cycle = (JackDriverNullCycleFunction) ffado_driver_null_cycle;
  606. driver->write = (JackDriverReadFunction) ffado_driver_write;
  607. driver->read = (JackDriverReadFunction) ffado_driver_read;
  608. driver->nt_bufsize = (JackDriverNTBufSizeFunction) ffado_driver_bufsize;
  609. /* copy command line parameter contents to the driver structure */
  610. memcpy(&driver->settings,params,sizeof(ffado_jack_settings_t));
  611. /* prepare all parameters */
  612. driver->sample_rate = params->sample_rate;
  613. driver->period_size = params->period_size;
  614. driver->last_wait_ust = 0;
  615. driver->period_usecs =
  616. (jack_time_t) floor ((((float) driver->period_size) * 1000000.0f) / driver->sample_rate);
  617. driver->client = client;
  618. driver->engine = NULL;
  619. memset(&driver->device_options,0,sizeof(driver->device_options));
  620. driver->device_options.sample_rate=params->sample_rate;
  621. driver->device_options.period_size=params->period_size;
  622. driver->device_options.nb_buffers=params->buffer_size;
  623. driver->capture_frame_latency = params->capture_frame_latency;
  624. driver->playback_frame_latency = params->playback_frame_latency;
  625. driver->device_options.slave_mode=params->slave_mode;
  626. driver->device_options.snoop_mode=params->snoop_mode;
  627. driver->device_options.verbose=params->verbose_level;
  628. driver->device_info.nb_device_spec_strings=1;
  629. driver->device_info.device_spec_strings=calloc(1, sizeof(char *));
  630. driver->device_info.device_spec_strings[0]=strdup(params->device_info);
  631. debugPrint(DEBUG_LEVEL_STARTUP, " Driver compiled on %s %s for FFADO %s (API version %d)",
  632. __DATE__, __TIME__, ffado_get_version(), ffado_get_api_version());
  633. debugPrint(DEBUG_LEVEL_STARTUP, " Created driver %s", name);
  634. debugPrint(DEBUG_LEVEL_STARTUP, " period_size: %d", driver->period_size);
  635. debugPrint(DEBUG_LEVEL_STARTUP, " period_usecs: %d", driver->period_usecs);
  636. debugPrint(DEBUG_LEVEL_STARTUP, " sample rate: %d", driver->sample_rate);
  637. return (ffado_driver_t *) driver;
  638. }
  639. static void
  640. ffado_driver_delete (ffado_driver_t *driver)
  641. {
  642. unsigned int i;
  643. jack_driver_nt_finish ((jack_driver_nt_t *) driver);
  644. for (i=0; i < driver->device_info.nb_device_spec_strings; i++) {
  645. free (driver->device_info.device_spec_strings[i]);
  646. }
  647. free (driver->device_info.device_spec_strings);
  648. free (driver);
  649. }
  650. /*
  651. * dlopen plugin stuff
  652. */
  653. const char driver_client_name[] = "firewire_pcm";
  654. const jack_driver_desc_t *
  655. driver_get_descriptor ()
  656. {
  657. jack_driver_desc_t * desc;
  658. jack_driver_param_desc_t * params;
  659. unsigned int i;
  660. desc = calloc (1, sizeof (jack_driver_desc_t));
  661. strcpy (desc->name, "firewire");
  662. desc->nparams = 11;
  663. params = calloc (desc->nparams, sizeof (jack_driver_param_desc_t));
  664. desc->params = params;
  665. i = 0;
  666. strcpy (params[i].name, "device");
  667. params[i].character = 'd';
  668. params[i].type = JackDriverParamString;
  669. strcpy (params[i].value.str, "hw:0");
  670. strcpy (params[i].short_desc, "The FireWire device to use.");
  671. strcpy (params[i].long_desc, "The FireWire device to use. Please consult the FFADO documentation for more info.");
  672. i++;
  673. strcpy (params[i].name, "period");
  674. params[i].character = 'p';
  675. params[i].type = JackDriverParamUInt;
  676. params[i].value.ui = 1024;
  677. strcpy (params[i].short_desc, "Frames per period");
  678. strcpy (params[i].long_desc, params[i].short_desc);
  679. i++;
  680. strcpy (params[i].name, "nperiods");
  681. params[i].character = 'n';
  682. params[i].type = JackDriverParamUInt;
  683. params[i].value.ui = 3;
  684. strcpy (params[i].short_desc, "Number of periods of playback latency");
  685. strcpy (params[i].long_desc, params[i].short_desc);
  686. i++;
  687. strcpy (params[i].name, "rate");
  688. params[i].character = 'r';
  689. params[i].type = JackDriverParamUInt;
  690. params[i].value.ui = 48000U;
  691. strcpy (params[i].short_desc, "Sample rate");
  692. strcpy (params[i].long_desc, params[i].short_desc);
  693. i++;
  694. strcpy (params[i].name, "capture");
  695. params[i].character = 'i';
  696. params[i].type = JackDriverParamUInt;
  697. params[i].value.ui = 1U;
  698. strcpy (params[i].short_desc, "Provide capture ports.");
  699. strcpy (params[i].long_desc, params[i].short_desc);
  700. i++;
  701. strcpy (params[i].name, "playback");
  702. params[i].character = 'o';
  703. params[i].type = JackDriverParamUInt;
  704. params[i].value.ui = 1U;
  705. strcpy (params[i].short_desc, "Provide playback ports.");
  706. strcpy (params[i].long_desc, params[i].short_desc);
  707. i++;
  708. strcpy (params[i].name, "input-latency");
  709. params[i].character = 'I';
  710. params[i].type = JackDriverParamUInt;
  711. params[i].value.ui = 0;
  712. strcpy (params[i].short_desc, "Extra input latency (frames)");
  713. strcpy (params[i].long_desc, params[i].short_desc);
  714. i++;
  715. strcpy (params[i].name, "output-latency");
  716. params[i].character = 'O';
  717. params[i].type = JackDriverParamUInt;
  718. params[i].value.ui = 0;
  719. strcpy (params[i].short_desc, "Extra output latency (frames)");
  720. strcpy (params[i].long_desc, params[i].short_desc);
  721. i++;
  722. strcpy (params[i].name, "slave");
  723. params[i].character = 'x';
  724. params[i].type = JackDriverParamUInt;
  725. params[i].value.ui = 0U;
  726. strcpy (params[i].short_desc, "Act as a BounceDevice slave");
  727. strcpy (params[i].long_desc, params[i].short_desc);
  728. i++;
  729. strcpy (params[i].name, "slave");
  730. params[i].character = 'X';
  731. params[i].type = JackDriverParamUInt;
  732. params[i].value.ui = 0U;
  733. strcpy (params[i].short_desc, "Operate in snoop mode");
  734. strcpy (params[i].long_desc, params[i].short_desc);
  735. i++;
  736. strcpy (params[i].name, "verbose");
  737. params[i].character = 'v';
  738. params[i].type = JackDriverParamUInt;
  739. params[i].value.ui = 0U;
  740. strcpy (params[i].short_desc, "Verbose level for the firewire backend");
  741. strcpy (params[i].long_desc, params[i].short_desc);
  742. return desc;
  743. }
  744. jack_driver_t *
  745. driver_initialize (jack_client_t *client, JSList * params)
  746. {
  747. jack_driver_t *driver;
  748. const JSList * node;
  749. const jack_driver_param_t * param;
  750. ffado_jack_settings_t cmlparams;
  751. char *device_name="hw:0";
  752. cmlparams.period_size_set=0;
  753. cmlparams.sample_rate_set=0;
  754. cmlparams.buffer_size_set=0;
  755. /* default values */
  756. cmlparams.period_size=1024;
  757. cmlparams.sample_rate=48000;
  758. cmlparams.buffer_size=3;
  759. cmlparams.playback_ports=1;
  760. cmlparams.capture_ports=1;
  761. cmlparams.playback_frame_latency=0;
  762. cmlparams.capture_frame_latency=0;
  763. cmlparams.slave_mode=0;
  764. cmlparams.snoop_mode=0;
  765. cmlparams.verbose_level=0;
  766. for (node = params; node; node = jack_slist_next (node))
  767. {
  768. param = (jack_driver_param_t *) node->data;
  769. switch (param->character)
  770. {
  771. case 'd':
  772. device_name = strdup (param->value.str);
  773. break;
  774. case 'p':
  775. cmlparams.period_size = param->value.ui;
  776. cmlparams.period_size_set = 1;
  777. break;
  778. case 'n':
  779. cmlparams.buffer_size = param->value.ui;
  780. cmlparams.buffer_size_set = 1;
  781. break;
  782. case 'r':
  783. cmlparams.sample_rate = param->value.ui;
  784. cmlparams.sample_rate_set = 1;
  785. break;
  786. case 'i':
  787. cmlparams.capture_ports = param->value.ui;
  788. break;
  789. case 'o':
  790. cmlparams.playback_ports = param->value.ui;
  791. break;
  792. case 'I':
  793. cmlparams.capture_frame_latency = param->value.ui;
  794. break;
  795. case 'O':
  796. cmlparams.playback_frame_latency = param->value.ui;
  797. break;
  798. case 'x':
  799. cmlparams.slave_mode = param->value.ui;
  800. break;
  801. case 'X':
  802. cmlparams.snoop_mode = param->value.ui;
  803. break;
  804. case 'v':
  805. cmlparams.verbose_level = param->value.ui;
  806. break;
  807. }
  808. }
  809. // temporary
  810. cmlparams.device_info = device_name;
  811. driver=(jack_driver_t *)ffado_driver_new (client, "ffado_pcm", &cmlparams);
  812. return driver;
  813. }
  814. void
  815. driver_finish (jack_driver_t *driver)
  816. {
  817. ffado_driver_t *drv=(ffado_driver_t *) driver;
  818. // If jack hasn't called the detach method, do it now. As of jack 0.101.1
  819. // the detach method was not being called explicitly on closedown, and
  820. // we need it to at least deallocate the iso resources.
  821. if (drv->dev != NULL)
  822. ffado_driver_detach(drv);
  823. ffado_driver_delete (drv);
  824. }