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.

JackFFADODriver.cpp 36KB

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