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.

963 lines
35KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004 Grame
  4. Copyright (C) 2007 Pieter Palmers
  5. Copyright (C) 2009 Devin Anderson
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <iostream>
  19. #include <unistd.h>
  20. #include <math.h>
  21. #include <stdio.h>
  22. #include <memory.h>
  23. #include <unistd.h>
  24. #include <stdlib.h>
  25. #include <errno.h>
  26. #include <stdarg.h>
  27. #include <signal.h>
  28. #include <sys/types.h>
  29. #include <sys/time.h>
  30. #include <regex.h>
  31. #include <string.h>
  32. #include "JackFFADODriver.h"
  33. #include "JackFFADOMidiInput.h"
  34. #include "JackFFADOMidiOutput.h"
  35. #include "JackEngineControl.h"
  36. #include "JackClientControl.h"
  37. #include "JackPort.h"
  38. #include "JackGraphManager.h"
  39. #include "JackCompilerDeps.h"
  40. namespace Jack
  41. {
  42. #define FIREWIRE_REQUIRED_FFADO_API_VERSION 8
  43. #define jack_get_microseconds GetMicroSeconds
  44. int
  45. JackFFADODriver::ffado_driver_read (ffado_driver_t * driver, jack_nframes_t nframes)
  46. {
  47. channel_t chn;
  48. jack_default_audio_sample_t* buf = NULL;
  49. printEnter();
  50. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  51. // if nothing connected, don't process
  52. if (fGraphManager->GetConnectionsNum(fCapturePortList[chn]) == 0) {
  53. buf = (jack_default_audio_sample_t*)driver->scratchbuffer;
  54. // we always have to specify a valid buffer
  55. ffado_streaming_set_capture_stream_buffer(driver->dev, chn, (char *)(buf));
  56. // notify the streaming system that it can (but doesn't have to) skip
  57. // this channel
  58. ffado_streaming_capture_stream_onoff(driver->dev, chn, 0);
  59. } else {
  60. if (driver->capture_channels[chn].stream_type == ffado_stream_type_audio) {
  61. buf = (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[chn], nframes);
  62. /* if the returned buffer is invalid, use the dummy buffer */
  63. if (!buf) buf = (jack_default_audio_sample_t*)driver->scratchbuffer;
  64. ffado_streaming_set_capture_stream_buffer(driver->dev, chn, (char *)(buf));
  65. ffado_streaming_capture_stream_onoff(driver->dev, chn, 1);
  66. } else if (driver->capture_channels[chn].stream_type == ffado_stream_type_midi) {
  67. ffado_streaming_set_capture_stream_buffer(driver->dev, chn,
  68. (char *)(driver->capture_channels[chn].midi_buffer));
  69. ffado_streaming_capture_stream_onoff(driver->dev, chn, 1);
  70. } else { // always have a valid buffer
  71. ffado_streaming_set_capture_stream_buffer(driver->dev, chn, (char *)(driver->scratchbuffer));
  72. // don't process what we don't use
  73. ffado_streaming_capture_stream_onoff(driver->dev, chn, 0);
  74. }
  75. }
  76. }
  77. /* now transfer the buffers */
  78. ffado_streaming_transfer_capture_buffers(driver->dev);
  79. /* process the midi data */
  80. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  81. if (driver->capture_channels[chn].stream_type == ffado_stream_type_midi) {
  82. JackFFADOMidiInput *midi_input = (JackFFADOMidiInput *) driver->capture_channels[chn].midi_input;
  83. JackMidiBuffer *buffer = (JackMidiBuffer *) fGraphManager->GetBuffer(fCapturePortList[chn], nframes);
  84. if (! buffer) {
  85. continue;
  86. }
  87. midi_input->SetInputBuffer(driver->capture_channels[chn].midi_buffer);
  88. midi_input->SetPortBuffer(buffer);
  89. midi_input->Process(nframes);
  90. }
  91. }
  92. printExit();
  93. return 0;
  94. }
  95. int
  96. JackFFADODriver::ffado_driver_write (ffado_driver_t * driver, jack_nframes_t nframes)
  97. {
  98. channel_t chn;
  99. jack_default_audio_sample_t* buf;
  100. printEnter();
  101. driver->process_count++;
  102. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  103. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[chn]) == 0) {
  104. buf = (jack_default_audio_sample_t*)driver->nullbuffer;
  105. // we always have to specify a valid buffer
  106. ffado_streaming_set_playback_stream_buffer(driver->dev, chn, (char *)(buf));
  107. // notify the streaming system that it can (but doesn't have to) skip
  108. // this channel
  109. ffado_streaming_playback_stream_onoff(driver->dev, chn, 0);
  110. } else {
  111. if (driver->playback_channels[chn].stream_type == ffado_stream_type_audio) {
  112. buf = (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[chn], nframes);
  113. /* use the silent buffer if there is no valid jack buffer */
  114. if (!buf) buf = (jack_default_audio_sample_t*)driver->nullbuffer;
  115. ffado_streaming_set_playback_stream_buffer(driver->dev, chn, (char *)(buf));
  116. ffado_streaming_playback_stream_onoff(driver->dev, chn, 1);
  117. } else if (driver->playback_channels[chn].stream_type == ffado_stream_type_midi) {
  118. uint32_t *midi_buffer = driver->playback_channels[chn].midi_buffer;
  119. memset(midi_buffer, 0, nframes * sizeof(uint32_t));
  120. buf = (jack_default_audio_sample_t *) fGraphManager->GetBuffer(fPlaybackPortList[chn], nframes);
  121. ffado_streaming_set_playback_stream_buffer(driver->dev, chn, (char *)(midi_buffer));
  122. /* if the returned buffer is invalid, continue */
  123. if (!buf) {
  124. ffado_streaming_playback_stream_onoff(driver->dev, chn, 0);
  125. continue;
  126. }
  127. ffado_streaming_playback_stream_onoff(driver->dev, chn, 1);
  128. JackFFADOMidiOutput *midi_output = (JackFFADOMidiOutput *) driver->playback_channels[chn].midi_output;
  129. midi_output->SetPortBuffer((JackMidiBuffer *) buf);
  130. midi_output->SetOutputBuffer(midi_buffer);
  131. midi_output->Process(nframes);
  132. } else { // always have a valid buffer
  133. ffado_streaming_set_playback_stream_buffer(driver->dev, chn, (char *)(driver->nullbuffer));
  134. ffado_streaming_playback_stream_onoff(driver->dev, chn, 0);
  135. }
  136. }
  137. }
  138. ffado_streaming_transfer_playback_buffers(driver->dev);
  139. printExit();
  140. return 0;
  141. }
  142. jack_nframes_t
  143. JackFFADODriver::ffado_driver_wait (ffado_driver_t *driver, int extra_fd, int *status,
  144. float *delayed_usecs)
  145. {
  146. jack_time_t wait_enter;
  147. jack_time_t wait_ret;
  148. ffado_wait_response response;
  149. printEnter();
  150. wait_enter = jack_get_microseconds ();
  151. if (wait_enter > driver->wait_next) {
  152. /*
  153. * This processing cycle was delayed past the
  154. * next due interrupt! Do not account this as
  155. * a wakeup delay:
  156. */
  157. driver->wait_next = 0;
  158. driver->wait_late++;
  159. }
  160. // *status = -2; interrupt
  161. // *status = -3; timeout
  162. // *status = -4; extra FD
  163. response = ffado_streaming_wait(driver->dev);
  164. wait_ret = jack_get_microseconds ();
  165. if (driver->wait_next && wait_ret > driver->wait_next) {
  166. *delayed_usecs = wait_ret - driver->wait_next;
  167. }
  168. driver->wait_last = wait_ret;
  169. driver->wait_next = wait_ret + driver->period_usecs;
  170. // driver->engine->transport_cycle_start (driver->engine, wait_ret);
  171. if(response == ffado_wait_ok) {
  172. // all good
  173. *status = 0;
  174. } else if (response == ffado_wait_xrun) {
  175. // xrun happened, but it's handled
  176. *status = 0;
  177. return 0;
  178. } else if (response == ffado_wait_error) {
  179. // an error happened (unhandled xrun)
  180. // this should be fatal
  181. jack_error("JackFFADODriver::ffado_driver_wait - unhandled xrun");
  182. *status = -1;
  183. return 0;
  184. } else if (response == ffado_wait_shutdown) {
  185. // ffado requested shutdown (e.g. device unplugged)
  186. // this should be fatal
  187. jack_error("JackFFADODriver::ffado_driver_wait - shutdown requested "
  188. "(device unplugged?)");
  189. *status = -1;
  190. return 0;
  191. } else {
  192. // unknown response code. should be fatal
  193. // this should be fatal
  194. jack_error("JackFFADODriver::ffado_driver_wait - unexpected error "
  195. "code '%d' returned from 'ffado_streaming_wait'", response);
  196. *status = -1;
  197. return 0;
  198. }
  199. fBeginDateUst = wait_ret;
  200. printExit();
  201. return driver->period_size;
  202. }
  203. int
  204. JackFFADODriver::ffado_driver_start (ffado_driver_t *driver)
  205. {
  206. int retval = 0;
  207. if ((retval = ffado_streaming_start(driver->dev))) {
  208. printError("Could not start streaming threads");
  209. return retval;
  210. }
  211. return 0;
  212. }
  213. int
  214. JackFFADODriver::ffado_driver_stop (ffado_driver_t *driver)
  215. {
  216. int retval = 0;
  217. if ((retval = ffado_streaming_stop(driver->dev))) {
  218. printError("Could not stop streaming threads");
  219. return retval;
  220. }
  221. return 0;
  222. }
  223. int
  224. JackFFADODriver::ffado_driver_restart (ffado_driver_t *driver)
  225. {
  226. if (Stop())
  227. return -1;
  228. return Start();
  229. }
  230. int
  231. JackFFADODriver::SetBufferSize (jack_nframes_t nframes)
  232. {
  233. printError("Buffer size change requested but not supported!!!");
  234. /*
  235. driver->period_size = nframes;
  236. driver->period_usecs =
  237. (jack_time_t) floor ((((float) nframes) / driver->sample_rate)
  238. * 1000000.0f);
  239. */
  240. /* tell the engine to change its buffer size */
  241. //driver->engine->set_buffer_size (driver->engine, nframes);
  242. return -1; // unsupported
  243. }
  244. typedef void (*JackDriverFinishFunction) (jack_driver_t *);
  245. ffado_driver_t *
  246. JackFFADODriver::ffado_driver_new (const char *name,
  247. ffado_jack_settings_t *params)
  248. {
  249. ffado_driver_t *driver;
  250. assert(params);
  251. if (ffado_get_api_version() != FIREWIRE_REQUIRED_FFADO_API_VERSION) {
  252. printError("Incompatible libffado version! (%s)", ffado_get_version());
  253. return NULL;
  254. }
  255. printMessage("Starting FFADO backend (%s)", ffado_get_version());
  256. driver = (ffado_driver_t*)calloc (1, sizeof (ffado_driver_t));
  257. /* Setup the jack interfaces */
  258. jack_driver_nt_init ((jack_driver_nt_t *) driver);
  259. /* driver->nt_attach = (JackDriverNTAttachFunction) ffado_driver_attach;
  260. driver->nt_detach = (JackDriverNTDetachFunction) ffado_driver_detach;
  261. driver->nt_start = (JackDriverNTStartFunction) ffado_driver_start;
  262. driver->nt_stop = (JackDriverNTStopFunction) ffado_driver_stop;
  263. driver->nt_run_cycle = (JackDriverNTRunCycleFunction) ffado_driver_run_cycle;
  264. driver->null_cycle = (JackDriverNullCycleFunction) ffado_driver_null_cycle;
  265. driver->write = (JackDriverReadFunction) ffado_driver_write;
  266. driver->read = (JackDriverReadFunction) ffado_driver_read;
  267. driver->nt_bufsize = (JackDriverNTBufSizeFunction) ffado_driver_bufsize;
  268. */
  269. /* copy command line parameter contents to the driver structure */
  270. memcpy(&driver->settings, params, sizeof(ffado_jack_settings_t));
  271. /* prepare all parameters */
  272. driver->sample_rate = params->sample_rate;
  273. driver->period_size = params->period_size;
  274. fBeginDateUst = 0;
  275. driver->period_usecs =
  276. (jack_time_t) floor ((((float) driver->period_size) * 1000000.0f) / driver->sample_rate);
  277. // driver->client = client;
  278. driver->engine = NULL;
  279. memset(&driver->device_options, 0, sizeof(driver->device_options));
  280. driver->device_options.sample_rate = params->sample_rate;
  281. driver->device_options.period_size = params->period_size;
  282. driver->device_options.nb_buffers = params->buffer_size;
  283. driver->device_options.verbose = params->verbose_level;
  284. driver->capture_frame_latency = params->capture_frame_latency;
  285. driver->playback_frame_latency = params->playback_frame_latency;
  286. driver->device_options.snoop_mode = params->snoop_mode;
  287. debugPrint(DEBUG_LEVEL_STARTUP, " Driver compiled on %s %s", __DATE__, __TIME__);
  288. debugPrint(DEBUG_LEVEL_STARTUP, " Created driver %s", name);
  289. debugPrint(DEBUG_LEVEL_STARTUP, " period_size: %d", driver->device_options.period_size);
  290. debugPrint(DEBUG_LEVEL_STARTUP, " period_usecs: %d", driver->period_usecs);
  291. debugPrint(DEBUG_LEVEL_STARTUP, " sample rate: %d", driver->device_options.sample_rate);
  292. debugPrint(DEBUG_LEVEL_STARTUP, " verbose level: %d", driver->device_options.verbose);
  293. return (ffado_driver_t *) driver;
  294. }
  295. void
  296. JackFFADODriver::ffado_driver_delete (ffado_driver_t *driver)
  297. {
  298. free (driver);
  299. }
  300. int JackFFADODriver::Attach()
  301. {
  302. JackPort* port;
  303. int port_index;
  304. char buf[JACK_PORT_NAME_SIZE];
  305. char portname[JACK_PORT_NAME_SIZE];
  306. ffado_driver_t* driver = (ffado_driver_t*)fDriver;
  307. jack_log("JackFFADODriver::Attach fBufferSize %ld fSampleRate %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  308. g_verbose = (fEngineControl->fVerbose ? 1 : 0);
  309. /* preallocate some buffers such that they don't have to be allocated
  310. in RT context (or from the stack)
  311. */
  312. /* the null buffer is a buffer that contains one period of silence */
  313. driver->nullbuffer = (ffado_sample_t *)calloc(driver->period_size, sizeof(ffado_sample_t));
  314. if (driver->nullbuffer == NULL) {
  315. printError("could not allocate memory for null buffer");
  316. return -1;
  317. }
  318. /* calloc should do this, but it can't hurt to be sure */
  319. memset(driver->nullbuffer, 0, driver->period_size*sizeof(ffado_sample_t));
  320. /* the scratch buffer is a buffer of one period that can be used as dummy memory */
  321. driver->scratchbuffer = (ffado_sample_t *)calloc(driver->period_size, sizeof(ffado_sample_t));
  322. if (driver->scratchbuffer == NULL) {
  323. printError("could not allocate memory for scratch buffer");
  324. return -1;
  325. }
  326. /* packetizer thread options */
  327. driver->device_options.realtime = (fEngineControl->fRealTime ? 1 : 0);
  328. driver->device_options.packetizer_priority = fEngineControl->fServerPriority +
  329. FFADO_RT_PRIORITY_PACKETIZER_RELATIVE;
  330. if (driver->device_options.packetizer_priority > 98) {
  331. driver->device_options.packetizer_priority = 98;
  332. }
  333. // initialize the thread
  334. driver->dev = ffado_streaming_init(driver->device_info, driver->device_options);
  335. if (!driver->dev) {
  336. printError("FFADO: Error creating virtual device");
  337. return -1;
  338. }
  339. if (driver->device_options.realtime) {
  340. printMessage("Streaming thread running with Realtime scheduling, priority %d",
  341. driver->device_options.packetizer_priority);
  342. } else {
  343. printMessage("Streaming thread running without Realtime scheduling");
  344. }
  345. ffado_streaming_set_audio_datatype(driver->dev, ffado_audio_datatype_float);
  346. /* ports */
  347. // capture
  348. driver->capture_nchannels = ffado_streaming_get_nb_capture_streams(driver->dev);
  349. driver->capture_channels = (ffado_capture_channel_t *)calloc(driver->capture_nchannels, sizeof(ffado_capture_channel_t));
  350. if (driver->capture_channels == NULL) {
  351. printError("could not allocate memory for capture channel list");
  352. return -1;
  353. }
  354. fCaptureChannels = 0;
  355. for (channel_t chn = 0; chn < driver->capture_nchannels; chn++) {
  356. ffado_streaming_get_capture_stream_name(driver->dev, chn, portname, sizeof(portname) - 1);
  357. driver->capture_channels[chn].stream_type = ffado_streaming_get_capture_stream_type(driver->dev, chn);
  358. if (driver->capture_channels[chn].stream_type == ffado_stream_type_audio) {
  359. snprintf(buf, sizeof(buf) - 1, "firewire_pcm:%s_in", portname);
  360. printMessage ("Registering audio capture port %s", buf);
  361. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, buf,
  362. JACK_DEFAULT_AUDIO_TYPE,
  363. CaptureDriverFlags,
  364. fEngineControl->fBufferSize)) == NO_PORT) {
  365. jack_error("driver: cannot register port for %s", buf);
  366. return -1;
  367. }
  368. // setup port parameters
  369. if (ffado_streaming_set_capture_stream_buffer(driver->dev, chn, NULL)) {
  370. printError(" cannot configure initial port buffer for %s", buf);
  371. }
  372. ffado_streaming_capture_stream_onoff(driver->dev, chn, 0);
  373. port = fGraphManager->GetPort(port_index);
  374. port->SetLatency(driver->period_size + driver->capture_frame_latency);
  375. // capture port aliases (jackd1 style port names)
  376. snprintf(buf, sizeof(buf) - 1, "%s:capture_%i", fClientControl.fName, (int) chn + 1);
  377. port->SetAlias(buf);
  378. fCapturePortList[chn] = port_index;
  379. jack_log("JackFFADODriver::Attach fCapturePortList[i] %ld ", port_index);
  380. fCaptureChannels++;
  381. } else if (driver->capture_channels[chn].stream_type == ffado_stream_type_midi) {
  382. snprintf(buf, sizeof(buf) - 1, "firewire_pcm:%s_in", portname);
  383. printMessage ("Registering midi capture port %s", buf);
  384. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, buf,
  385. JACK_DEFAULT_MIDI_TYPE,
  386. CaptureDriverFlags,
  387. fEngineControl->fBufferSize)) == NO_PORT) {
  388. jack_error("driver: cannot register port for %s", buf);
  389. return -1;
  390. }
  391. // setup port parameters
  392. if (ffado_streaming_set_capture_stream_buffer(driver->dev, chn, NULL)) {
  393. printError(" cannot configure initial port buffer for %s", buf);
  394. }
  395. if (ffado_streaming_capture_stream_onoff(driver->dev, chn, 0)) {
  396. printError(" cannot enable port %s", buf);
  397. }
  398. driver->capture_channels[chn].midi_input = new JackFFADOMidiInput();
  399. // setup the midi buffer
  400. driver->capture_channels[chn].midi_buffer = (uint32_t *)calloc(driver->period_size, sizeof(uint32_t));
  401. port = fGraphManager->GetPort(port_index);
  402. port->SetLatency(driver->period_size + driver->capture_frame_latency);
  403. fCapturePortList[chn] = port_index;
  404. jack_log("JackFFADODriver::Attach fCapturePortList[i] %ld ", port_index);
  405. fCaptureChannels++;
  406. } else {
  407. printMessage ("Don't register capture port %s", portname);
  408. }
  409. }
  410. // playback
  411. driver->playback_nchannels = ffado_streaming_get_nb_playback_streams(driver->dev);
  412. driver->playback_channels = (ffado_playback_channel_t *)calloc(driver->playback_nchannels, sizeof(ffado_playback_channel_t));
  413. if (driver->playback_channels == NULL) {
  414. printError("could not allocate memory for playback channel list");
  415. return -1;
  416. }
  417. fPlaybackChannels = 0;
  418. for (channel_t chn = 0; chn < driver->playback_nchannels; chn++) {
  419. ffado_streaming_get_playback_stream_name(driver->dev, chn, portname, sizeof(portname) - 1);
  420. driver->playback_channels[chn].stream_type = ffado_streaming_get_playback_stream_type(driver->dev, chn);
  421. if (driver->playback_channels[chn].stream_type == ffado_stream_type_audio) {
  422. snprintf(buf, sizeof(buf) - 1, "firewire_pcm:%s_out", portname);
  423. printMessage ("Registering audio playback port %s", buf);
  424. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, buf,
  425. JACK_DEFAULT_AUDIO_TYPE,
  426. PlaybackDriverFlags,
  427. fEngineControl->fBufferSize)) == NO_PORT) {
  428. jack_error("driver: cannot register port for %s", buf);
  429. return -1;
  430. }
  431. // setup port parameters
  432. if (ffado_streaming_set_playback_stream_buffer(driver->dev, chn, NULL)) {
  433. printError(" cannot configure initial port buffer for %s", buf);
  434. }
  435. if (ffado_streaming_playback_stream_onoff(driver->dev, chn, 0)) {
  436. printError(" cannot enable port %s", buf);
  437. }
  438. port = fGraphManager->GetPort(port_index);
  439. // Add one buffer more latency if "async" mode is used...
  440. port->SetLatency((driver->period_size * (driver->device_options.nb_buffers - 1)) + ((fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize) + driver->playback_frame_latency);
  441. // playback port aliases (jackd1 style port names)
  442. snprintf(buf, sizeof(buf) - 1, "%s:playback_%i", fClientControl.fName, (int) chn + 1);
  443. port->SetAlias(buf);
  444. fPlaybackPortList[chn] = port_index;
  445. jack_log("JackFFADODriver::Attach fPlaybackPortList[i] %ld ", port_index);
  446. fPlaybackChannels++;
  447. } else if (driver->playback_channels[chn].stream_type == ffado_stream_type_midi) {
  448. snprintf(buf, sizeof(buf) - 1, "firewire_pcm:%s_out", portname);
  449. printMessage ("Registering midi playback port %s", buf);
  450. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, buf,
  451. JACK_DEFAULT_MIDI_TYPE,
  452. PlaybackDriverFlags,
  453. fEngineControl->fBufferSize)) == NO_PORT) {
  454. jack_error("driver: cannot register port for %s", buf);
  455. return -1;
  456. }
  457. // setup port parameters
  458. if (ffado_streaming_set_playback_stream_buffer(driver->dev, chn, NULL)) {
  459. printError(" cannot configure initial port buffer for %s", buf);
  460. }
  461. if (ffado_streaming_playback_stream_onoff(driver->dev, chn, 0)) {
  462. printError(" cannot enable port %s", buf);
  463. }
  464. // setup the midi buffer
  465. // This constructor optionally accepts arguments for the
  466. // non-realtime buffer size and the realtime buffer size. Ideally,
  467. // these would become command-line options for the FFADO driver.
  468. driver->playback_channels[chn].midi_output = new JackFFADOMidiOutput();
  469. driver->playback_channels[chn].midi_buffer = (uint32_t *)calloc(driver->period_size, sizeof(uint32_t));
  470. port = fGraphManager->GetPort(port_index);
  471. port->SetLatency((driver->period_size * (driver->device_options.nb_buffers - 1)) + driver->playback_frame_latency);
  472. fPlaybackPortList[chn] = port_index;
  473. jack_log("JackFFADODriver::Attach fPlaybackPortList[i] %ld ", port_index);
  474. fPlaybackChannels++;
  475. } else {
  476. printMessage ("Don't register playback port %s", portname);
  477. }
  478. }
  479. assert(fCaptureChannels < DRIVER_PORT_NUM);
  480. assert(fPlaybackChannels < DRIVER_PORT_NUM);
  481. if (ffado_streaming_prepare(driver->dev)) {
  482. printError("Could not prepare streaming device!");
  483. return -1;
  484. }
  485. // this makes no sense...
  486. assert(fCaptureChannels + fPlaybackChannels > 0);
  487. return 0;
  488. }
  489. int JackFFADODriver::Detach()
  490. {
  491. channel_t chn;
  492. ffado_driver_t* driver = (ffado_driver_t*)fDriver;
  493. jack_log("JackFFADODriver::Detach");
  494. // finish the libfreebob streaming
  495. ffado_streaming_finish(driver->dev);
  496. driver->dev = NULL;
  497. // free all internal buffers
  498. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  499. if (driver->capture_channels[chn].midi_buffer)
  500. free(driver->capture_channels[chn].midi_buffer);
  501. if (driver->capture_channels[chn].midi_input)
  502. delete ((JackFFADOMidiInput *) (driver->capture_channels[chn].midi_input));
  503. }
  504. free(driver->capture_channels);
  505. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  506. if (driver->playback_channels[chn].midi_buffer)
  507. free(driver->playback_channels[chn].midi_buffer);
  508. if (driver->playback_channels[chn].midi_output)
  509. delete ((JackFFADOMidiOutput *) (driver->playback_channels[chn].midi_output));
  510. }
  511. free(driver->playback_channels);
  512. free(driver->nullbuffer);
  513. free(driver->scratchbuffer);
  514. return JackAudioDriver::Detach(); // Generic JackAudioDriver Detach
  515. }
  516. int JackFFADODriver::Open(ffado_jack_settings_t *params)
  517. {
  518. // Generic JackAudioDriver Open
  519. if (JackAudioDriver::Open(
  520. params->period_size, params->sample_rate,
  521. params->playback_ports, params->playback_ports,
  522. 0, 0, 0, "", "",
  523. params->capture_frame_latency, params->playback_frame_latency) != 0) {
  524. return -1;
  525. }
  526. fDriver = (jack_driver_t *)ffado_driver_new ("ffado_pcm", params);
  527. if (fDriver) {
  528. // FFADO driver may have changed the in/out values
  529. //fCaptureChannels = ((ffado_driver_t *)fDriver)->capture_nchannels_audio;
  530. //fPlaybackChannels = ((ffado_driver_t *)fDriver)->playback_nchannels_audio;
  531. return 0;
  532. } else {
  533. JackAudioDriver::Close();
  534. return -1;
  535. }
  536. }
  537. int JackFFADODriver::Close()
  538. {
  539. JackAudioDriver::Close();
  540. ffado_driver_delete((ffado_driver_t*)fDriver);
  541. return 0;
  542. }
  543. int JackFFADODriver::Start()
  544. {
  545. JackAudioDriver::Start();
  546. return ffado_driver_start((ffado_driver_t *)fDriver);
  547. }
  548. int JackFFADODriver::Stop()
  549. {
  550. return ffado_driver_stop((ffado_driver_t *)fDriver);
  551. }
  552. int JackFFADODriver::Read()
  553. {
  554. printEnter();
  555. /* Taken from ffado_driver_run_cycle */
  556. ffado_driver_t* driver = (ffado_driver_t*)fDriver;
  557. int wait_status = 0;
  558. fDelayedUsecs = 0.f;
  559. jack_nframes_t nframes = ffado_driver_wait(driver, -1, &wait_status,
  560. &fDelayedUsecs);
  561. if ((wait_status < 0)) {
  562. printError( "wait status < 0! (= %d)", wait_status);
  563. return -1;
  564. }
  565. if (nframes == 0) {
  566. /* we detected an xrun and restarted: notify
  567. * clients about the delay.
  568. */
  569. jack_log("FFADO XRun");
  570. NotifyXRun(fBeginDateUst, fDelayedUsecs);
  571. return -1;
  572. }
  573. if (nframes != fEngineControl->fBufferSize)
  574. jack_log("JackFFADODriver::Read warning nframes = %ld", nframes);
  575. // Has to be done before read
  576. JackDriver::CycleIncTime();
  577. printExit();
  578. return ffado_driver_read((ffado_driver_t *)fDriver, fEngineControl->fBufferSize);
  579. }
  580. int JackFFADODriver::Write()
  581. {
  582. printEnter();
  583. int res = ffado_driver_write((ffado_driver_t *)fDriver, fEngineControl->fBufferSize);
  584. printExit();
  585. return res;
  586. }
  587. void
  588. JackFFADODriver::jack_driver_init (jack_driver_t *driver)
  589. {
  590. memset (driver, 0, sizeof (*driver));
  591. driver->attach = 0;
  592. driver->detach = 0;
  593. driver->write = 0;
  594. driver->read = 0;
  595. driver->null_cycle = 0;
  596. driver->bufsize = 0;
  597. driver->start = 0;
  598. driver->stop = 0;
  599. }
  600. void
  601. JackFFADODriver::jack_driver_nt_init (jack_driver_nt_t * driver)
  602. {
  603. memset (driver, 0, sizeof (*driver));
  604. jack_driver_init ((jack_driver_t *) driver);
  605. driver->attach = 0;
  606. driver->detach = 0;
  607. driver->bufsize = 0;
  608. driver->stop = 0;
  609. driver->start = 0;
  610. driver->nt_bufsize = 0;
  611. driver->nt_start = 0;
  612. driver->nt_stop = 0;
  613. driver->nt_attach = 0;
  614. driver->nt_detach = 0;
  615. driver->nt_run_cycle = 0;
  616. }
  617. } // end of namespace
  618. #ifdef __cplusplus
  619. extern "C"
  620. {
  621. #endif
  622. SERVER_EXPORT const jack_driver_desc_t *
  623. driver_get_descriptor () {
  624. jack_driver_desc_t * desc;
  625. jack_driver_param_desc_t * params;
  626. unsigned int i;
  627. desc = (jack_driver_desc_t *)calloc (1, sizeof (jack_driver_desc_t));
  628. strcpy (desc->name, "firewire"); // size MUST be less then JACK_DRIVER_NAME_MAX + 1
  629. strcpy(desc->desc, "Linux FFADO API based audio backend"); // size MUST be less then JACK_DRIVER_PARAM_DESC + 1
  630. desc->nparams = 13;
  631. params = (jack_driver_param_desc_t *)calloc (desc->nparams, sizeof (jack_driver_param_desc_t));
  632. desc->params = params;
  633. i = 0;
  634. strcpy (params[i].name, "device");
  635. params[i].character = 'd';
  636. params[i].type = JackDriverParamString;
  637. strcpy (params[i].value.str, "hw:0");
  638. strcpy (params[i].short_desc, "The FireWire device to use.");
  639. strcpy (params[i].long_desc, "The FireWire device to use. Please consult the FFADO documentation for more info.");
  640. i++;
  641. strcpy (params[i].name, "period");
  642. params[i].character = 'p';
  643. params[i].type = JackDriverParamUInt;
  644. params[i].value.ui = 1024;
  645. strcpy (params[i].short_desc, "Frames per period");
  646. strcpy (params[i].long_desc, params[i].short_desc);
  647. i++;
  648. strcpy (params[i].name, "nperiods");
  649. params[i].character = 'n';
  650. params[i].type = JackDriverParamUInt;
  651. params[i].value.ui = 3;
  652. strcpy (params[i].short_desc, "Number of periods of playback latency");
  653. strcpy (params[i].long_desc, params[i].short_desc);
  654. i++;
  655. strcpy (params[i].name, "rate");
  656. params[i].character = 'r';
  657. params[i].type = JackDriverParamUInt;
  658. params[i].value.ui = 48000U;
  659. strcpy (params[i].short_desc, "Sample rate");
  660. strcpy (params[i].long_desc, params[i].short_desc);
  661. i++;
  662. strcpy (params[i].name, "capture");
  663. params[i].character = 'C';
  664. params[i].type = JackDriverParamBool;
  665. params[i].value.i = 0;
  666. strcpy (params[i].short_desc, "Provide capture ports.");
  667. strcpy (params[i].long_desc, params[i].short_desc);
  668. i++;
  669. strcpy (params[i].name, "playback");
  670. params[i].character = 'P';
  671. params[i].type = JackDriverParamBool;
  672. params[i].value.i = 0;
  673. strcpy (params[i].short_desc, "Provide playback ports.");
  674. strcpy (params[i].long_desc, params[i].short_desc);
  675. i++;
  676. strcpy (params[i].name, "duplex");
  677. params[i].character = 'D';
  678. params[i].type = JackDriverParamBool;
  679. params[i].value.i = 1;
  680. strcpy (params[i].short_desc, "Provide both capture and playback ports.");
  681. strcpy (params[i].long_desc, params[i].short_desc);
  682. i++;
  683. strcpy (params[i].name, "input-latency");
  684. params[i].character = 'I';
  685. params[i].type = JackDriverParamUInt;
  686. params[i].value.ui = 0;
  687. strcpy (params[i].short_desc, "Extra input latency (frames)");
  688. strcpy (params[i].long_desc, params[i].short_desc);
  689. i++;
  690. strcpy (params[i].name, "output-latency");
  691. params[i].character = 'O';
  692. params[i].type = JackDriverParamUInt;
  693. params[i].value.ui = 0;
  694. strcpy (params[i].short_desc, "Extra output latency (frames)");
  695. strcpy (params[i].long_desc, params[i].short_desc);
  696. i++;
  697. strcpy (params[i].name, "inchannels");
  698. params[i].character = 'i';
  699. params[i].type = JackDriverParamUInt;
  700. params[i].value.ui = 0;
  701. strcpy (params[i].short_desc, "Number of input channels to provide (note: currently ignored)");
  702. strcpy (params[i].long_desc, params[i].short_desc);
  703. i++;
  704. strcpy (params[i].name, "outchannels");
  705. params[i].character = 'o';
  706. params[i].type = JackDriverParamUInt;
  707. params[i].value.ui = 0;
  708. strcpy (params[i].short_desc, "Number of output channels to provide (note: currently ignored)");
  709. strcpy (params[i].long_desc, params[i].short_desc);
  710. i++;
  711. strcpy (params[i].name, "verbose");
  712. params[i].character = 'v';
  713. params[i].type = JackDriverParamUInt;
  714. params[i].value.ui = 3;
  715. strcpy (params[i].short_desc, "libffado verbose level");
  716. strcpy (params[i].long_desc, params[i].short_desc);
  717. i++;
  718. strcpy (params[i].name, "snoop");
  719. params[i].character = 'X';
  720. params[i].type = JackDriverParamBool;
  721. params[i].value.i = 0;
  722. strcpy (params[i].short_desc, "Snoop firewire traffic");
  723. strcpy (params[i].long_desc, params[i].short_desc);
  724. return desc;
  725. }
  726. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params) {
  727. const JSList * node;
  728. const jack_driver_param_t * param;
  729. ffado_jack_settings_t cmlparams;
  730. char *device_name=(char*)"hw:0";
  731. cmlparams.period_size_set = 0;
  732. cmlparams.sample_rate_set = 0;
  733. cmlparams.buffer_size_set = 0;
  734. /* default values */
  735. cmlparams.period_size = 1024;
  736. cmlparams.sample_rate = 48000;
  737. cmlparams.buffer_size = 3;
  738. cmlparams.playback_ports = 0;
  739. cmlparams.capture_ports = 0;
  740. cmlparams.playback_frame_latency = 0;
  741. cmlparams.capture_frame_latency = 0;
  742. cmlparams.verbose_level = 0;
  743. cmlparams.slave_mode = 0;
  744. cmlparams.snoop_mode = 0;
  745. cmlparams.device_info = NULL;
  746. for (node = params; node; node = jack_slist_next (node)) {
  747. param = (jack_driver_param_t *) node->data;
  748. switch (param->character) {
  749. case 'd':
  750. device_name = const_cast<char*>(param->value.str);
  751. break;
  752. case 'p':
  753. cmlparams.period_size = param->value.ui;
  754. cmlparams.period_size_set = 1;
  755. break;
  756. case 'n':
  757. cmlparams.buffer_size = param->value.ui;
  758. cmlparams.buffer_size_set = 1;
  759. break;
  760. case 'r':
  761. cmlparams.sample_rate = param->value.ui;
  762. cmlparams.sample_rate_set = 1;
  763. break;
  764. case 'i':
  765. cmlparams.capture_ports = param->value.ui;
  766. break;
  767. case 'o':
  768. cmlparams.playback_ports = param->value.ui;
  769. break;
  770. case 'I':
  771. cmlparams.capture_frame_latency = param->value.ui;
  772. break;
  773. case 'O':
  774. cmlparams.playback_frame_latency = param->value.ui;
  775. break;
  776. case 'x':
  777. cmlparams.slave_mode = param->value.ui;
  778. break;
  779. case 'X':
  780. cmlparams.snoop_mode = param->value.i;
  781. break;
  782. case 'v':
  783. cmlparams.verbose_level = param->value.ui;
  784. }
  785. }
  786. /* duplex is the default */
  787. if (!cmlparams.playback_ports && !cmlparams.capture_ports) {
  788. cmlparams.playback_ports = 1;
  789. cmlparams.capture_ports = 1;
  790. }
  791. // temporary
  792. cmlparams.device_info = device_name;
  793. Jack::JackFFADODriver* ffado_driver = new Jack::JackFFADODriver("system", "firewire_pcm", engine, table);
  794. Jack::JackDriverClientInterface* threaded_driver = new Jack::JackThreadedDriver(ffado_driver);
  795. // Special open for FFADO driver...
  796. if (ffado_driver->Open(&cmlparams) == 0) {
  797. return threaded_driver;
  798. } else {
  799. delete threaded_driver; // Delete the decorated driver
  800. return NULL;
  801. }
  802. }
  803. #ifdef __cplusplus
  804. }
  805. #endif