From 6decd24c12fc8718d60cd124f2e5c3a55adb9521 Mon Sep 17 00:00:00 2001 From: sletz Date: Fri, 25 Nov 2011 11:32:33 +0000 Subject: [PATCH] More robust dynamic port management in JACK/CoreMidi bridge(2). git-svn-id: http://subversion.jackaudio.org/jack/jack2/trunk/jackmp@4605 0c269be4-1314-0410-8aa9-9f06e86f4224 --- common/JackEngine.cpp | 4 +- common/JackMutex.h | 12 ++--- macosx/coremidi/JackCoreMidiDriver.cpp | 74 ++++++++++++++------------ macosx/coremidi/JackCoreMidiDriver.h | 4 +- 4 files changed, 51 insertions(+), 43 deletions(-) diff --git a/common/JackEngine.cpp b/common/JackEngine.cpp index de0dc38b..6ba48726 100644 --- a/common/JackEngine.cpp +++ b/common/JackEngine.cpp @@ -247,11 +247,11 @@ void JackEngine::NotifyClient(int refnum, int event, int sync, const char* messa /* Important for internal clients : unlock before calling the notification callbacks. */ - bool res = fMutex.Unlock(); + bool res = Unlock(); if (client->ClientNotify(refnum, client->GetClientControl()->fName, event, sync, message, value1, value2) < 0) jack_error("NotifyClient fails name = %s event = %ld val1 = %ld val2 = %ld", client->GetClientControl()->fName, event, value1, value2); if (res) - fMutex.Lock(); + Lock(); } else { jack_log("JackEngine::NotifyClient: no callback for event = %ld", event); diff --git a/common/JackMutex.h b/common/JackMutex.h index 0a2600d7..2bdea991 100644 --- a/common/JackMutex.h +++ b/common/JackMutex.h @@ -37,7 +37,7 @@ class JackLockAble { protected: - + JackMutex fMutex; JackLockAble() @@ -46,10 +46,10 @@ class JackLockAble {} public: - - void Lock() + + bool Lock() { - fMutex.Lock(); + return fMutex.Lock(); } bool Trylock() @@ -57,9 +57,9 @@ class JackLockAble return fMutex.Trylock(); } - void Unlock() + bool Unlock() { - fMutex.Unlock(); + return fMutex.Unlock(); } }; diff --git a/macosx/coremidi/JackCoreMidiDriver.cpp b/macosx/coremidi/JackCoreMidiDriver.cpp index 7e65adf3..c4479ed6 100644 --- a/macosx/coremidi/JackCoreMidiDriver.cpp +++ b/macosx/coremidi/JackCoreMidiDriver.cpp @@ -487,16 +487,18 @@ JackCoreMidiDriver::HandleNotification(const MIDINotification *message) switch (message->messageID) { case kMIDIMsgSetupChanged: - SaveConnections(); - Stop(); - // Wait at least one cycle... - JackSleep(int(fEngineControl->fPeriodUsecs * 2.f)); - Detach(); - CloseAux(); - OpenAux(); - Attach(); - Start(); - RestoreConnections(); + Lock(); + { + SaveConnections(); + Stop(); + Detach(); + CloseAux(); + OpenAux(); + Attach(); + Start(); + RestoreConnections(); + } + Unlock(); break; case kMIDIMsgObjectAdded: @@ -508,8 +510,6 @@ JackCoreMidiDriver::HandleNotification(const MIDINotification *message) } } -#define WAIT_COUNTER 100 - int JackCoreMidiDriver::Open(bool capturing_aux, bool playing_aux, int in_channels_aux, int out_channels_aux, bool monitor_aux, @@ -550,20 +550,6 @@ JackCoreMidiDriver::Open(bool capturing_aux, bool playing_aux, int in_channels_a return 0; } -int -JackCoreMidiDriver::Read() -{ - jack_nframes_t buffer_size = fEngineControl->fBufferSize; - for (int i = 0; i < num_physical_inputs; i++) { - physical_input_ports[i]->ProcessJack(GetInputBuffer(i), buffer_size); - } - for (int i = 0; i < num_virtual_inputs; i++) { - virtual_input_ports[i]-> - ProcessJack(GetInputBuffer(num_physical_inputs + i), buffer_size); - } - return 0; -} - int JackCoreMidiDriver::Start() { @@ -703,16 +689,36 @@ JackCoreMidiDriver::Stop() } int -JackCoreMidiDriver::Write() +JackCoreMidiDriver::Read() { - jack_nframes_t buffer_size = fEngineControl->fBufferSize; - for (int i = 0; i < num_physical_outputs; i++) { - physical_output_ports[i]->ProcessJack(GetOutputBuffer(i), buffer_size); + if (Trylock()) { + jack_nframes_t buffer_size = fEngineControl->fBufferSize; + for (int i = 0; i < num_physical_inputs; i++) { + physical_input_ports[i]->ProcessJack(GetInputBuffer(i), buffer_size); + } + for (int i = 0; i < num_virtual_inputs; i++) { + virtual_input_ports[i]-> + ProcessJack(GetInputBuffer(num_physical_inputs + i), buffer_size); + } + Unlock(); } - for (int i = 0; i < num_virtual_outputs; i++) { - virtual_output_ports[i]-> - ProcessJack(GetOutputBuffer(num_physical_outputs + i), - buffer_size); + return 0; +} + +int +JackCoreMidiDriver::Write() +{ + if (Trylock()) { + jack_nframes_t buffer_size = fEngineControl->fBufferSize; + for (int i = 0; i < num_physical_outputs; i++) { + physical_output_ports[i]->ProcessJack(GetOutputBuffer(i), buffer_size); + } + for (int i = 0; i < num_virtual_outputs; i++) { + virtual_output_ports[i]-> + ProcessJack(GetOutputBuffer(num_physical_outputs + i), + buffer_size); + } + Unlock(); } return 0; } diff --git a/macosx/coremidi/JackCoreMidiDriver.h b/macosx/coremidi/JackCoreMidiDriver.h index 13f9f904..3d665e8a 100644 --- a/macosx/coremidi/JackCoreMidiDriver.h +++ b/macosx/coremidi/JackCoreMidiDriver.h @@ -30,7 +30,7 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. namespace Jack { - class JackCoreMidiDriver: public JackMidiDriver, public JackRunnableInterface { + class JackCoreMidiDriver: public JackMidiDriver, public JackRunnableInterface, public JackLockAble { private: @@ -101,4 +101,6 @@ namespace Jack { } +#define WAIT_COUNTER 100 + #endif