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.

946 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. debugPrint(DEBUG_LEVEL_STARTUP, " Driver compiled on %s %s", __DATE__, __TIME__);
  287. debugPrint(DEBUG_LEVEL_STARTUP, " Created driver %s", name);
  288. debugPrint(DEBUG_LEVEL_STARTUP, " period_size: %d", driver->device_options.period_size);
  289. debugPrint(DEBUG_LEVEL_STARTUP, " period_usecs: %d", driver->period_usecs);
  290. debugPrint(DEBUG_LEVEL_STARTUP, " sample rate: %d", driver->device_options.sample_rate);
  291. debugPrint(DEBUG_LEVEL_STARTUP, " verbose level: %d", driver->device_options.verbose);
  292. return (ffado_driver_t *) driver;
  293. }
  294. void
  295. JackFFADODriver::ffado_driver_delete (ffado_driver_t *driver)
  296. {
  297. free (driver);
  298. }
  299. int JackFFADODriver::Attach()
  300. {
  301. JackPort* port;
  302. int port_index;
  303. char buf[JACK_PORT_NAME_SIZE];
  304. char portname[JACK_PORT_NAME_SIZE];
  305. ffado_driver_t* driver = (ffado_driver_t*)fDriver;
  306. jack_log("JackFFADODriver::Attach fBufferSize %ld fSampleRate %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  307. g_verbose = (fEngineControl->fVerbose ? 1 : 0);
  308. /* preallocate some buffers such that they don't have to be allocated
  309. in RT context (or from the stack)
  310. */
  311. /* the null buffer is a buffer that contains one period of silence */
  312. driver->nullbuffer = (ffado_sample_t *)calloc(driver->period_size, sizeof(ffado_sample_t));
  313. if (driver->nullbuffer == NULL) {
  314. printError("could not allocate memory for null buffer");
  315. return -1;
  316. }
  317. /* calloc should do this, but it can't hurt to be sure */
  318. memset(driver->nullbuffer, 0, driver->period_size*sizeof(ffado_sample_t));
  319. /* the scratch buffer is a buffer of one period that can be used as dummy memory */
  320. driver->scratchbuffer = (ffado_sample_t *)calloc(driver->period_size, sizeof(ffado_sample_t));
  321. if (driver->scratchbuffer == NULL) {
  322. printError("could not allocate memory for scratch buffer");
  323. return -1;
  324. }
  325. /* packetizer thread options */
  326. driver->device_options.realtime = (fEngineControl->fRealTime ? 1 : 0);
  327. driver->device_options.packetizer_priority = fEngineControl->fServerPriority +
  328. FFADO_RT_PRIORITY_PACKETIZER_RELATIVE;
  329. if (driver->device_options.packetizer_priority > 98) {
  330. driver->device_options.packetizer_priority = 98;
  331. }
  332. // initialize the thread
  333. driver->dev = ffado_streaming_init(driver->device_info, driver->device_options);
  334. if (!driver->dev) {
  335. printError("FFADO: Error creating virtual device");
  336. return -1;
  337. }
  338. if (driver->device_options.realtime) {
  339. printMessage("Streaming thread running with Realtime scheduling, priority %d",
  340. driver->device_options.packetizer_priority);
  341. } else {
  342. printMessage("Streaming thread running without Realtime scheduling");
  343. }
  344. ffado_streaming_set_audio_datatype(driver->dev, ffado_audio_datatype_float);
  345. /* ports */
  346. // capture
  347. driver->capture_nchannels = ffado_streaming_get_nb_capture_streams(driver->dev);
  348. driver->capture_channels = (ffado_capture_channel_t *)calloc(driver->capture_nchannels, sizeof(ffado_capture_channel_t));
  349. if (driver->capture_channels == NULL) {
  350. printError("could not allocate memory for capture channel list");
  351. return -1;
  352. }
  353. fCaptureChannels = 0;
  354. for (channel_t chn = 0; chn < driver->capture_nchannels; chn++) {
  355. ffado_streaming_get_capture_stream_name(driver->dev, chn, portname, sizeof(portname) - 1);
  356. driver->capture_channels[chn].stream_type = ffado_streaming_get_capture_stream_type(driver->dev, chn);
  357. if (driver->capture_channels[chn].stream_type == ffado_stream_type_audio) {
  358. snprintf(buf, sizeof(buf) - 1, "%s:%s", fClientControl.fName, portname);
  359. printMessage ("Registering audio capture port %s", buf);
  360. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, buf,
  361. JACK_DEFAULT_AUDIO_TYPE,
  362. CaptureDriverFlags,
  363. fEngineControl->fBufferSize)) == NO_PORT) {
  364. jack_error("driver: cannot register port for %s", buf);
  365. return -1;
  366. }
  367. // setup port parameters
  368. if (ffado_streaming_set_capture_stream_buffer(driver->dev, chn, NULL)) {
  369. printError(" cannot configure initial port buffer for %s", buf);
  370. }
  371. ffado_streaming_capture_stream_onoff(driver->dev, chn, 0);
  372. port = fGraphManager->GetPort(port_index);
  373. port->SetLatency(driver->period_size + driver->capture_frame_latency);
  374. // capture port aliases (jackd1 style port names)
  375. snprintf(buf, sizeof(buf) - 1, "%s:capture_%i", fClientControl.fName, (int) chn + 1);
  376. port->SetAlias(buf);
  377. fCapturePortList[chn] = port_index;
  378. jack_log("JackFFADODriver::Attach fCapturePortList[i] %ld ", port_index);
  379. fCaptureChannels++;
  380. } else if (driver->capture_channels[chn].stream_type == ffado_stream_type_midi) {
  381. snprintf(buf, sizeof(buf) - 1, "%s:%s", fClientControl.fName, portname);
  382. printMessage ("Registering midi capture port %s", buf);
  383. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, buf,
  384. JACK_DEFAULT_MIDI_TYPE,
  385. CaptureDriverFlags,
  386. fEngineControl->fBufferSize)) == NO_PORT) {
  387. jack_error("driver: cannot register port for %s", buf);
  388. return -1;
  389. }
  390. // setup port parameters
  391. if (ffado_streaming_set_capture_stream_buffer(driver->dev, chn, NULL)) {
  392. printError(" cannot configure initial port buffer for %s", buf);
  393. }
  394. if (ffado_streaming_capture_stream_onoff(driver->dev, chn, 0)) {
  395. printError(" cannot enable port %s", buf);
  396. }
  397. driver->capture_channels[chn].midi_input = new JackFFADOMidiInput();
  398. // setup the midi buffer
  399. driver->capture_channels[chn].midi_buffer = (uint32_t *)calloc(driver->period_size, sizeof(uint32_t));
  400. port = fGraphManager->GetPort(port_index);
  401. port->SetLatency(driver->period_size + driver->capture_frame_latency);
  402. fCapturePortList[chn] = port_index;
  403. jack_log("JackFFADODriver::Attach fCapturePortList[i] %ld ", port_index);
  404. fCaptureChannels++;
  405. } else {
  406. printMessage ("Don't register capture port %s", portname);
  407. }
  408. }
  409. // playback
  410. driver->playback_nchannels = ffado_streaming_get_nb_playback_streams(driver->dev);
  411. driver->playback_channels = (ffado_playback_channel_t *)calloc(driver->playback_nchannels, sizeof(ffado_playback_channel_t));
  412. if (driver->playback_channels == NULL) {
  413. printError("could not allocate memory for playback channel list");
  414. return -1;
  415. }
  416. fPlaybackChannels = 0;
  417. for (channel_t chn = 0; chn < driver->playback_nchannels; chn++) {
  418. ffado_streaming_get_playback_stream_name(driver->dev, chn, portname, sizeof(portname) - 1);
  419. driver->playback_channels[chn].stream_type = ffado_streaming_get_playback_stream_type(driver->dev, chn);
  420. if (driver->playback_channels[chn].stream_type == ffado_stream_type_audio) {
  421. snprintf(buf, sizeof(buf) - 1, "%s:%s", fClientControl.fName, portname);
  422. printMessage ("Registering audio playback port %s", buf);
  423. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, buf,
  424. JACK_DEFAULT_AUDIO_TYPE,
  425. PlaybackDriverFlags,
  426. fEngineControl->fBufferSize)) == NO_PORT) {
  427. jack_error("driver: cannot register port for %s", buf);
  428. return -1;
  429. }
  430. // setup port parameters
  431. if (ffado_streaming_set_playback_stream_buffer(driver->dev, chn, NULL)) {
  432. printError(" cannot configure initial port buffer for %s", buf);
  433. }
  434. if (ffado_streaming_playback_stream_onoff(driver->dev, chn, 0)) {
  435. printError(" cannot enable port %s", buf);
  436. }
  437. port = fGraphManager->GetPort(port_index);
  438. // Add one buffer more latency if "async" mode is used...
  439. port->SetLatency((driver->period_size * (driver->device_options.nb_buffers - 1)) + ((fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize) + driver->playback_frame_latency);
  440. // playback port aliases (jackd1 style port names)
  441. snprintf(buf, sizeof(buf) - 1, "%s:playback_%i", fClientControl.fName, (int) chn + 1);
  442. port->SetAlias(buf);
  443. fPlaybackPortList[chn] = port_index;
  444. jack_log("JackFFADODriver::Attach fPlaybackPortList[i] %ld ", port_index);
  445. fPlaybackChannels++;
  446. } else if (driver->playback_channels[chn].stream_type == ffado_stream_type_midi) {
  447. snprintf(buf, sizeof(buf) - 1, "%s:%s", fClientControl.fName, portname);
  448. printMessage ("Registering midi playback port %s", buf);
  449. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, buf,
  450. JACK_DEFAULT_MIDI_TYPE,
  451. PlaybackDriverFlags,
  452. fEngineControl->fBufferSize)) == NO_PORT) {
  453. jack_error("driver: cannot register port for %s", buf);
  454. return -1;
  455. }
  456. // setup port parameters
  457. if (ffado_streaming_set_playback_stream_buffer(driver->dev, chn, NULL)) {
  458. printError(" cannot configure initial port buffer for %s", buf);
  459. }
  460. if (ffado_streaming_playback_stream_onoff(driver->dev, chn, 0)) {
  461. printError(" cannot enable port %s", buf);
  462. }
  463. // setup the midi buffer
  464. // This constructor optionally accepts arguments for the
  465. // non-realtime buffer size and the realtime buffer size. Ideally,
  466. // these would become command-line options for the FFADO driver.
  467. driver->playback_channels[chn].midi_output = new JackFFADOMidiOutput();
  468. driver->playback_channels[chn].midi_buffer = (uint32_t *)calloc(driver->period_size, sizeof(uint32_t));
  469. port = fGraphManager->GetPort(port_index);
  470. port->SetLatency((driver->period_size * (driver->device_options.nb_buffers - 1)) + driver->playback_frame_latency);
  471. fPlaybackPortList[chn] = port_index;
  472. jack_log("JackFFADODriver::Attach fPlaybackPortList[i] %ld ", port_index);
  473. fPlaybackChannels++;
  474. } else {
  475. printMessage ("Don't register playback port %s", portname);
  476. }
  477. }
  478. assert(fCaptureChannels < DRIVER_PORT_NUM);
  479. assert(fPlaybackChannels < DRIVER_PORT_NUM);
  480. if (ffado_streaming_prepare(driver->dev)) {
  481. printError("Could not prepare streaming device!");
  482. return -1;
  483. }
  484. // this makes no sense...
  485. assert(fCaptureChannels + fPlaybackChannels > 0);
  486. return 0;
  487. }
  488. int JackFFADODriver::Detach()
  489. {
  490. channel_t chn;
  491. ffado_driver_t* driver = (ffado_driver_t*)fDriver;
  492. jack_log("JackFFADODriver::Detach");
  493. // finish the libfreebob streaming
  494. ffado_streaming_finish(driver->dev);
  495. driver->dev = NULL;
  496. // free all internal buffers
  497. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  498. if (driver->capture_channels[chn].midi_buffer)
  499. free(driver->capture_channels[chn].midi_buffer);
  500. if (driver->capture_channels[chn].midi_input)
  501. delete ((JackFFADOMidiInput *) (driver->capture_channels[chn].midi_input));
  502. }
  503. free(driver->capture_channels);
  504. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  505. if (driver->playback_channels[chn].midi_buffer)
  506. free(driver->playback_channels[chn].midi_buffer);
  507. if (driver->playback_channels[chn].midi_output)
  508. delete ((JackFFADOMidiOutput *) (driver->playback_channels[chn].midi_output));
  509. }
  510. free(driver->playback_channels);
  511. free(driver->nullbuffer);
  512. free(driver->scratchbuffer);
  513. return JackAudioDriver::Detach(); // Generic JackAudioDriver Detach
  514. }
  515. int JackFFADODriver::Open(ffado_jack_settings_t *params)
  516. {
  517. // Generic JackAudioDriver Open
  518. if (JackAudioDriver::Open(
  519. params->period_size, params->sample_rate,
  520. params->playback_ports, params->playback_ports,
  521. 0, 0, 0, "", "",
  522. params->capture_frame_latency, params->playback_frame_latency) != 0) {
  523. return -1;
  524. }
  525. fDriver = (jack_driver_t *)ffado_driver_new ("ffado_pcm", params);
  526. if (fDriver) {
  527. // FFADO driver may have changed the in/out values
  528. //fCaptureChannels = ((ffado_driver_t *)fDriver)->capture_nchannels_audio;
  529. //fPlaybackChannels = ((ffado_driver_t *)fDriver)->playback_nchannels_audio;
  530. return 0;
  531. } else {
  532. JackAudioDriver::Close();
  533. return -1;
  534. }
  535. }
  536. int JackFFADODriver::Close()
  537. {
  538. JackAudioDriver::Close();
  539. ffado_driver_delete((ffado_driver_t*)fDriver);
  540. return 0;
  541. }
  542. int JackFFADODriver::Start()
  543. {
  544. JackAudioDriver::Start();
  545. return ffado_driver_start((ffado_driver_t *)fDriver);
  546. }
  547. int JackFFADODriver::Stop()
  548. {
  549. return ffado_driver_stop((ffado_driver_t *)fDriver);
  550. }
  551. int JackFFADODriver::Read()
  552. {
  553. printEnter();
  554. /* Taken from ffado_driver_run_cycle */
  555. ffado_driver_t* driver = (ffado_driver_t*)fDriver;
  556. int wait_status = 0;
  557. fDelayedUsecs = 0.f;
  558. jack_nframes_t nframes = ffado_driver_wait(driver, -1, &wait_status,
  559. &fDelayedUsecs);
  560. if ((wait_status < 0)) {
  561. printError( "wait status < 0! (= %d)", wait_status);
  562. return -1;
  563. }
  564. if (nframes == 0) {
  565. /* we detected an xrun and restarted: notify
  566. * clients about the delay.
  567. */
  568. jack_log("FFADO XRun");
  569. NotifyXRun(fBeginDateUst, fDelayedUsecs);
  570. return -1;
  571. }
  572. if (nframes != fEngineControl->fBufferSize)
  573. jack_log("JackFFADODriver::Read nframes = %ld", nframes);
  574. // Has to be done before read
  575. JackDriver::CycleIncTime();
  576. printExit();
  577. return ffado_driver_read((ffado_driver_t *)fDriver, fEngineControl->fBufferSize);
  578. }
  579. int JackFFADODriver::Write()
  580. {
  581. printEnter();
  582. int res = ffado_driver_write((ffado_driver_t *)fDriver, fEngineControl->fBufferSize);
  583. printExit();
  584. return res;
  585. }
  586. void
  587. JackFFADODriver::jack_driver_init (jack_driver_t *driver)
  588. {
  589. memset (driver, 0, sizeof (*driver));
  590. driver->attach = 0;
  591. driver->detach = 0;
  592. driver->write = 0;
  593. driver->read = 0;
  594. driver->null_cycle = 0;
  595. driver->bufsize = 0;
  596. driver->start = 0;
  597. driver->stop = 0;
  598. }
  599. void
  600. JackFFADODriver::jack_driver_nt_init (jack_driver_nt_t * driver)
  601. {
  602. memset (driver, 0, sizeof (*driver));
  603. jack_driver_init ((jack_driver_t *) driver);
  604. driver->attach = 0;
  605. driver->detach = 0;
  606. driver->bufsize = 0;
  607. driver->stop = 0;
  608. driver->start = 0;
  609. driver->nt_bufsize = 0;
  610. driver->nt_start = 0;
  611. driver->nt_stop = 0;
  612. driver->nt_attach = 0;
  613. driver->nt_detach = 0;
  614. driver->nt_run_cycle = 0;
  615. }
  616. } // end of namespace
  617. #ifdef __cplusplus
  618. extern "C"
  619. {
  620. #endif
  621. SERVER_EXPORT const jack_driver_desc_t *
  622. driver_get_descriptor () {
  623. jack_driver_desc_t * desc;
  624. jack_driver_param_desc_t * params;
  625. unsigned int i;
  626. desc = (jack_driver_desc_t *)calloc (1, sizeof (jack_driver_desc_t));
  627. strcpy (desc->name, "firewire"); // size MUST be less then JACK_DRIVER_NAME_MAX + 1
  628. strcpy(desc->desc, "Linux FFADO API based audio backend"); // size MUST be less then JACK_DRIVER_PARAM_DESC + 1
  629. desc->nparams = 11;
  630. params = (jack_driver_param_desc_t *)calloc (desc->nparams, sizeof (jack_driver_param_desc_t));
  631. desc->params = params;
  632. i = 0;
  633. strcpy (params[i].name, "period");
  634. params[i].character = 'p';
  635. params[i].type = JackDriverParamUInt;
  636. params[i].value.ui = 1024;
  637. strcpy (params[i].short_desc, "Frames per period");
  638. strcpy (params[i].long_desc, params[i].short_desc);
  639. i++;
  640. strcpy (params[i].name, "nperiods");
  641. params[i].character = 'n';
  642. params[i].type = JackDriverParamUInt;
  643. params[i].value.ui = 3;
  644. strcpy (params[i].short_desc, "Number of periods of playback latency");
  645. strcpy (params[i].long_desc, params[i].short_desc);
  646. i++;
  647. strcpy (params[i].name, "rate");
  648. params[i].character = 'r';
  649. params[i].type = JackDriverParamUInt;
  650. params[i].value.ui = 48000U;
  651. strcpy (params[i].short_desc, "Sample rate");
  652. strcpy (params[i].long_desc, params[i].short_desc);
  653. i++;
  654. strcpy (params[i].name, "capture");
  655. params[i].character = 'C';
  656. params[i].type = JackDriverParamBool;
  657. params[i].value.i = 0;
  658. strcpy (params[i].short_desc, "Provide capture ports.");
  659. strcpy (params[i].long_desc, params[i].short_desc);
  660. i++;
  661. strcpy (params[i].name, "playback");
  662. params[i].character = 'P';
  663. params[i].type = JackDriverParamBool;
  664. params[i].value.i = 0;
  665. strcpy (params[i].short_desc, "Provide playback ports.");
  666. strcpy (params[i].long_desc, params[i].short_desc);
  667. i++;
  668. strcpy (params[i].name, "duplex");
  669. params[i].character = 'D';
  670. params[i].type = JackDriverParamBool;
  671. params[i].value.i = 1;
  672. strcpy (params[i].short_desc, "Provide both capture and playback ports.");
  673. strcpy (params[i].long_desc, params[i].short_desc);
  674. i++;
  675. strcpy (params[i].name, "input-latency");
  676. params[i].character = 'I';
  677. params[i].type = JackDriverParamUInt;
  678. params[i].value.ui = 0;
  679. strcpy (params[i].short_desc, "Extra input latency (frames)");
  680. strcpy (params[i].long_desc, params[i].short_desc);
  681. i++;
  682. strcpy (params[i].name, "output-latency");
  683. params[i].character = 'O';
  684. params[i].type = JackDriverParamUInt;
  685. params[i].value.ui = 0;
  686. strcpy (params[i].short_desc, "Extra output latency (frames)");
  687. strcpy (params[i].long_desc, params[i].short_desc);
  688. i++;
  689. strcpy (params[i].name, "inchannels");
  690. params[i].character = 'i';
  691. params[i].type = JackDriverParamUInt;
  692. params[i].value.ui = 0;
  693. strcpy (params[i].short_desc, "Number of input channels to provide (note: currently ignored)");
  694. strcpy (params[i].long_desc, params[i].short_desc);
  695. i++;
  696. strcpy (params[i].name, "outchannels");
  697. params[i].character = 'o';
  698. params[i].type = JackDriverParamUInt;
  699. params[i].value.ui = 0;
  700. strcpy (params[i].short_desc, "Number of output channels to provide (note: currently ignored)");
  701. strcpy (params[i].long_desc, params[i].short_desc);
  702. i++;
  703. strcpy (params[i].name, "verbose");
  704. params[i].character = 'v';
  705. params[i].type = JackDriverParamUInt;
  706. params[i].value.ui = 3;
  707. strcpy (params[i].short_desc, "libffado verbose level");
  708. strcpy (params[i].long_desc, params[i].short_desc);
  709. return desc;
  710. }
  711. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params) {
  712. const JSList * node;
  713. const jack_driver_param_t * param;
  714. ffado_jack_settings_t cmlparams;
  715. char *device_name="hw:0";
  716. cmlparams.period_size_set = 0;
  717. cmlparams.sample_rate_set = 0;
  718. cmlparams.buffer_size_set = 0;
  719. /* default values */
  720. cmlparams.period_size = 1024;
  721. cmlparams.sample_rate = 48000;
  722. cmlparams.buffer_size = 3;
  723. cmlparams.playback_ports = 0;
  724. cmlparams.capture_ports = 0;
  725. cmlparams.playback_frame_latency = 0;
  726. cmlparams.capture_frame_latency = 0;
  727. cmlparams.verbose_level = 0;
  728. cmlparams.slave_mode = 0;
  729. cmlparams.snoop_mode = 0;
  730. cmlparams.device_info = NULL;
  731. for (node = params; node; node = jack_slist_next (node)) {
  732. param = (jack_driver_param_t *) node->data;
  733. switch (param->character) {
  734. case 'd':
  735. device_name = strdup (param->value.str);
  736. break;
  737. case 'p':
  738. cmlparams.period_size = param->value.ui;
  739. cmlparams.period_size_set = 1;
  740. break;
  741. case 'n':
  742. cmlparams.buffer_size = param->value.ui;
  743. cmlparams.buffer_size_set = 1;
  744. break;
  745. case 'r':
  746. cmlparams.sample_rate = param->value.ui;
  747. cmlparams.sample_rate_set = 1;
  748. break;
  749. case 'i':
  750. cmlparams.capture_ports = param->value.ui;
  751. break;
  752. case 'o':
  753. cmlparams.playback_ports = param->value.ui;
  754. break;
  755. case 'I':
  756. cmlparams.capture_frame_latency = param->value.ui;
  757. break;
  758. case 'O':
  759. cmlparams.playback_frame_latency = param->value.ui;
  760. break;
  761. case 'x':
  762. cmlparams.slave_mode = param->value.ui;
  763. break;
  764. case 'X':
  765. cmlparams.snoop_mode = param->value.ui;
  766. break;
  767. case 'v':
  768. cmlparams.verbose_level = param->value.ui;
  769. }
  770. }
  771. /* duplex is the default */
  772. if (!cmlparams.playback_ports && !cmlparams.capture_ports) {
  773. cmlparams.playback_ports = 1;
  774. cmlparams.capture_ports = 1;
  775. }
  776. // temporary
  777. cmlparams.device_info = device_name;
  778. Jack::JackFFADODriver* ffado_driver = new Jack::JackFFADODriver("system", "firewire_pcm", engine, table);
  779. Jack::JackDriverClientInterface* threaded_driver = new Jack::JackThreadedDriver(ffado_driver);
  780. // Special open for FFADO driver...
  781. if (ffado_driver->Open(&cmlparams) == 0) {
  782. return threaded_driver;
  783. } else {
  784. delete threaded_driver; // Delete the decorated driver
  785. return NULL;
  786. }
  787. }
  788. #ifdef __cplusplus
  789. }
  790. #endif