From 00280570af529e4a54009bd5c6aeeab05286efcf Mon Sep 17 00:00:00 2001 From: Adrian Knoth Date: Thu, 8 Mar 2012 21:54:22 +0100 Subject: [PATCH 1/4] Fix segfault in jack_connect When jack_connect is called without any argument, it causes a segfault in snprintf( portA, sizeof(portA), "%s", argv[argc-1] ); snprintf( portB, sizeof(portB), "%s", argv[argc-2] ); Reported in http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=662199 --- example-clients/connect.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/example-clients/connect.c b/example-clients/connect.c index 5ed112b0..0f4f8be5 100644 --- a/example-clients/connect.c +++ b/example-clients/connect.c @@ -128,7 +128,10 @@ main (int argc, char *argv[]) return 1; } - if (argc < 3) show_usage(my_name); + if (argc < 3) { + show_usage(my_name); + return 1; + } /* try to become a client of the JACK server */ From 1a8d715c2e3e6fcc18c8f8f36278d716caddd1c5 Mon Sep 17 00:00:00 2001 From: Adrian Knoth Date: Sat, 17 Mar 2012 22:34:24 +0100 Subject: [PATCH 2/4] [firewire] Introduce UpdateLatencies() in FFADO backend In preparation for the upcoming FFADO buffersize functionality, add code that updates the latency afterwards. --- linux/firewire/JackFFADODriver.cpp | 25 +++++++++++++++++++++++++ linux/firewire/JackFFADODriver.h | 2 ++ 2 files changed, 27 insertions(+) diff --git a/linux/firewire/JackFFADODriver.cpp b/linux/firewire/JackFFADODriver.cpp index 742005ef..b33e1cd4 100644 --- a/linux/firewire/JackFFADODriver.cpp +++ b/linux/firewire/JackFFADODriver.cpp @@ -253,6 +253,31 @@ JackFFADODriver::ffado_driver_restart (ffado_driver_t *driver) return Start(); } +void +JackFFADODriver::UpdateLatencies(void) +{ + jack_latency_range_t range; + ffado_driver_t* driver = (ffado_driver_t*)fDriver; + + for (int i = 0; i < fCaptureChannels; i++) { + range.min = range.max = driver->period_size + driver->capture_frame_latency; + fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &range); + } + + for (int i = 0; i < fPlaybackChannels; i++) { + // Add one buffer more latency if "async" mode is used... + range.min = range.max = (driver->period_size * + (driver->device_options.nb_buffers - 1)) + + ((fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize) + driver->playback_frame_latency; + fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &range); + // Monitor port + if (fWithMonitorPorts) { + range.min = range.max =driver->period_size; + fGraphManager->GetPort(fMonitorPortList[i])->SetLatencyRange(JackCaptureLatency, &range); + } + } +} + int JackFFADODriver::SetBufferSize (jack_nframes_t nframes) { diff --git a/linux/firewire/JackFFADODriver.h b/linux/firewire/JackFFADODriver.h index 1dd401a6..cb2a45d4 100644 --- a/linux/firewire/JackFFADODriver.h +++ b/linux/firewire/JackFFADODriver.h @@ -2,6 +2,7 @@ Copyright (C) 2001 Paul Davis Copyright (C) 2004 Grame Copyright (C) 2007 Pieter Palmers +Copyright (C) 2012 Adrian Knoth This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -59,6 +60,7 @@ class JackFFADODriver : public JackAudioDriver void jack_driver_init (jack_driver_t *driver); void jack_driver_nt_init (jack_driver_nt_t * driver); + void UpdateLatencies(); public: From 96e0251234a29a1360c05d5d7dc98b83436b8183 Mon Sep 17 00:00:00 2001 From: Adrian Knoth Date: Sat, 17 Mar 2012 22:36:30 +0100 Subject: [PATCH 3/4] [firewire] Allow FFADO backend to change the buffer size This is a port of Jonathan Woithe's patch from jackd1. With sufficiently recent versions of FFADO, it allows to change the buffersize at runtime. --- linux/firewire/JackFFADODriver.cpp | 65 +++++++++++++++++++++++++++--- linux/firewire/JackFFADODriver.h | 6 +++ 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/linux/firewire/JackFFADODriver.cpp b/linux/firewire/JackFFADODriver.cpp index b33e1cd4..085b78a2 100644 --- a/linux/firewire/JackFFADODriver.cpp +++ b/linux/firewire/JackFFADODriver.cpp @@ -3,6 +3,7 @@ Copyright (C) 2001 Paul Davis Copyright (C) 2004 Grame Copyright (C) 2007 Pieter Palmers Copyright (C) 2009 Devin Anderson +Copyright (C) 2012 Jonathan Woithe, Adrian Knoth This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -48,7 +49,10 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. namespace Jack { +// Basic functionality requires API version 8. If version 9 or later +// is present the buffers can be resized at runtime. #define FIREWIRE_REQUIRED_FFADO_API_VERSION 8 +#define FIREWIRE_REQUIRED_FFADO_API_VERSION_FOR_SETBUFSIZE 9 #define jack_get_microseconds GetMicroSeconds @@ -281,19 +285,68 @@ JackFFADODriver::UpdateLatencies(void) int JackFFADODriver::SetBufferSize (jack_nframes_t nframes) { - printError("Buffer size change requested but not supported!!!"); + ffado_driver_t* driver = (ffado_driver_t*)fDriver; + signed int chn; + + // The speed of this function isn't critical; we can afford the + // time to check the FFADO API version. + if (ffado_get_api_version() < FIREWIRE_REQUIRED_FFADO_API_VERSION_FOR_SETBUFSIZE || + ffado_streaming_set_period_size == NULL) { + printError("unsupported on current version of FFADO; please upgrade FFADO"); + return -1; + } - /* driver->period_size = nframes; driver->period_usecs = (jack_time_t) floor ((((float) nframes) / driver->sample_rate) * 1000000.0f); - */ + + + // Reallocate the null and scratch buffers. + driver->nullbuffer = (ffado_sample_t*) calloc(driver->period_size, sizeof(ffado_sample_t)); + if(driver->nullbuffer == NULL) { + printError("could not allocate memory for null buffer"); + return -1; + } + driver->scratchbuffer = (ffado_sample_t*) calloc(driver->period_size, sizeof(ffado_sample_t)); + if(driver->scratchbuffer == NULL) { + printError("could not allocate memory for scratch buffer"); + return -1; + } + + // MIDI buffers need reallocating + for (chn = 0; chn < driver->capture_nchannels; chn++) { + if(driver->capture_channels[chn].stream_type == ffado_stream_type_midi) { + // setup the midi buffer + if (driver->capture_channels[chn].midi_buffer != NULL) + free(driver->capture_channels[chn].midi_buffer); + driver->capture_channels[chn].midi_buffer = (ffado_sample_t*) calloc(driver->period_size, sizeof(uint32_t)); + } + } + for (chn = 0; chn < driver->playback_nchannels; chn++) { + if(driver->playback_channels[chn].stream_type == ffado_stream_type_midi) { + if (driver->playback_channels[chn].midi_buffer != NULL) + free(driver->playback_channels[chn].midi_buffer); + driver->playback_channels[chn].midi_buffer = (ffado_sample_t*) calloc(driver->period_size, sizeof(uint32_t)); + } + } + + // Notify FFADO of the period size change + if (ffado_streaming_set_period_size(driver->dev, nframes) != 0) { + printError("could not alter FFADO device period size"); + return -1; + } + + // This is needed to give the shadow variables a chance to + // properly update to the changes. + sleep(1); /* tell the engine to change its buffer size */ - //driver->engine->set_buffer_size (driver->engine, nframes); + JackAudioDriver::SetBufferSize(nframes); // Generic change, never fails - return -1; // unsupported + UpdateLatencies(); + + return 0; } typedef void (*JackDriverFinishFunction) (jack_driver_t *); @@ -306,7 +359,7 @@ JackFFADODriver::ffado_driver_new (const char *name, assert(params); - if (ffado_get_api_version() != FIREWIRE_REQUIRED_FFADO_API_VERSION) { + if (ffado_get_api_version() < FIREWIRE_REQUIRED_FFADO_API_VERSION) { printError("Incompatible libffado version! (%s)", ffado_get_version()); return NULL; } diff --git a/linux/firewire/JackFFADODriver.h b/linux/firewire/JackFFADODriver.h index cb2a45d4..790f4dd0 100644 --- a/linux/firewire/JackFFADODriver.h +++ b/linux/firewire/JackFFADODriver.h @@ -82,6 +82,12 @@ class JackFFADODriver : public JackAudioDriver int Read(); int Write(); + // BufferSize can be changed + bool IsFixedBufferSize() + { + return false; + } + int SetBufferSize(jack_nframes_t nframes); }; From 8603dd812743cda893f650444b22a02405685d26 Mon Sep 17 00:00:00 2001 From: Adrian Knoth Date: Sat, 17 Mar 2012 22:45:52 +0100 Subject: [PATCH 4/4] [firewire] Use UpdateLatencies() in Attach(). Cleanup of obsolete code, the functionality is now provided by UpdateLatencies(). --- linux/firewire/JackFFADODriver.cpp | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/linux/firewire/JackFFADODriver.cpp b/linux/firewire/JackFFADODriver.cpp index 085b78a2..fda4fc38 100644 --- a/linux/firewire/JackFFADODriver.cpp +++ b/linux/firewire/JackFFADODriver.cpp @@ -427,7 +427,6 @@ int JackFFADODriver::Attach() jack_port_id_t port_index; char buf[REAL_JACK_PORT_NAME_SIZE]; char portname[REAL_JACK_PORT_NAME_SIZE]; - jack_latency_range_t range; ffado_driver_t* driver = (ffado_driver_t*)fDriver; @@ -513,8 +512,6 @@ int JackFFADODriver::Attach() ffado_streaming_capture_stream_onoff(driver->dev, chn, 0); port = fGraphManager->GetPort(port_index); - range.min = range.max = driver->period_size + driver->capture_frame_latency; - port->SetLatencyRange(JackCaptureLatency, &range); // capture port aliases (jackd1 style port names) snprintf(buf, sizeof(buf), "%s:capture_%i", fClientControl.fName, (int) chn + 1); port->SetAlias(buf); @@ -544,9 +541,6 @@ int JackFFADODriver::Attach() // setup the midi buffer driver->capture_channels[chn].midi_buffer = (uint32_t *)calloc(driver->period_size, sizeof(uint32_t)); - port = fGraphManager->GetPort(port_index); - range.min = range.max = driver->period_size + driver->capture_frame_latency; - port->SetLatencyRange(JackCaptureLatency, &range); fCapturePortList[chn] = port_index; jack_log("JackFFADODriver::Attach fCapturePortList[i] %ld ", port_index); fCaptureChannels++; @@ -590,8 +584,6 @@ int JackFFADODriver::Attach() port = fGraphManager->GetPort(port_index); // Add one buffer more latency if "async" mode is used... - range.min = range.max = (driver->period_size * (driver->device_options.nb_buffers - 1)) + ((fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize) + driver->playback_frame_latency; - port->SetLatencyRange(JackPlaybackLatency, &range); // playback port aliases (jackd1 style port names) snprintf(buf, sizeof(buf), "%s:playback_%i", fClientControl.fName, (int) chn + 1); port->SetAlias(buf); @@ -626,9 +618,6 @@ int JackFFADODriver::Attach() driver->playback_channels[chn].midi_buffer = (uint32_t *)calloc(driver->period_size, sizeof(uint32_t)); - port = fGraphManager->GetPort(port_index); - range.min = range.max = (driver->period_size * (driver->device_options.nb_buffers - 1)) + ((fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize) + driver->playback_frame_latency; - port->SetLatencyRange(JackPlaybackLatency, &range); fPlaybackPortList[chn] = port_index; jack_log("JackFFADODriver::Attach fPlaybackPortList[i] %ld ", port_index); fPlaybackChannels++; @@ -637,6 +626,8 @@ int JackFFADODriver::Attach() } } + UpdateLatencies(); + assert(fCaptureChannels < DRIVER_PORT_NUM); assert(fPlaybackChannels < DRIVER_PORT_NUM);