Skip to content

Commit

Permalink
Allow portaudio driver on any system, use device reservation too
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <falktx@falktx.com>
  • Loading branch information
falkTX committed Dec 3, 2023
1 parent 7d08d7f commit 806da09
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 5 deletions.
86 changes: 85 additions & 1 deletion windows/portaudio/JackPortAudioDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,43 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include <iostream>
#include <assert.h>

#ifdef __linux__
#include "JackServerGlobals.h"
#endif

using namespace std;

namespace Jack
{

#ifdef __linux__
static volatile bool device_reservation_loop_running = false;

static void* on_device_reservation_loop(void*)
{
while (device_reservation_loop_running && JackServerGlobals::on_device_reservation_loop != NULL) {
JackServerGlobals::on_device_reservation_loop();
usleep(50*1000);
}

return NULL;
}

static bool name_to_num(const char* paDeviceName, char* entry)
{
if (const char* sep = strstr(paDeviceName, " (hw:"))
{
sep += 5;
while (*sep != '\0' && *sep != ',' && *sep != ')')
*entry++ = *sep++;
*entry = '\0';
return true;
}

return false;
}
#endif

int JackPortAudioDriver::Render(const void* inputBuffer, void* outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
Expand Down Expand Up @@ -231,6 +263,27 @@ int JackPortAudioDriver::Open(jack_nframes_t buffer_size,
fCaptureChannels = inchannels;
fPlaybackChannels = outchannels;

#ifdef __linux__
if (JackServerGlobals::on_device_acquire != NULL) {
char audio_name[32];
snprintf(audio_name, sizeof(audio_name), "Audio");

if (name_to_num(capture_driver_name, audio_name+5)) {
if (!JackServerGlobals::on_device_acquire(audio_name)) {
jack_error("Audio device %s cannot be acquired...", capture_driver_name);
return -1;
}
}

if (strcmp(capture_driver_name, playback_driver_name) && name_to_num(playback_driver_name, audio_name+5)) {
if (!JackServerGlobals::on_device_acquire(audio_name)) {
jack_error("Audio device %s cannot be acquired...", playback_driver_name);
return -1;
}
}
}
#endif

err = OpenStream(buffer_size);
if (err != paNoError) {
jack_error("Pa_OpenStream error = %s", Pa_GetErrorText(err));
Expand All @@ -243,6 +296,15 @@ int JackPortAudioDriver::Open(jack_nframes_t buffer_size,
fEngineControl->fConstraint = fEngineControl->fPeriodUsecs * 1000;
#endif

#ifdef __linux__
if (JackServerGlobals::on_device_reservation_loop != NULL) {
device_reservation_loop_running = true;
if (JackPosixThread::StartImp(&fReservationLoopThread, 0, 0, on_device_reservation_loop, NULL) != 0) {
device_reservation_loop_running = false;
}
}
#endif

return 0;

error:
Expand All @@ -260,6 +322,28 @@ int JackPortAudioDriver::Close()
if (err != paNoError) {
jack_error("Pa_CloseStream error = %s", Pa_GetErrorText(err));
}

#ifdef __linux__
if (device_reservation_loop_running) {
device_reservation_loop_running = false;
JackPosixThread::StopImp(fReservationLoopThread);
}

if (JackServerGlobals::on_device_release != NULL)
{
char audio_name[32];
snprintf(audio_name, sizeof(audio_name), "Audio");

if (name_to_num(fCaptureDriverName, audio_name+5)) {
JackServerGlobals::on_device_release(audio_name);
}

if (strcmp(fCaptureDriverName, fPlaybackDriverName) && name_to_num(fPlaybackDriverName, audio_name+5)) {
JackServerGlobals::on_device_release(audio_name);
}
}
#endif

delete fPaDevices;
fPaDevices = NULL;
return (err != paNoError) ? -1 : 0;
Expand All @@ -269,9 +353,9 @@ int JackPortAudioDriver::Attach()
{
if (JackAudioDriver::Attach() == 0) {

#if defined(HAVE_ASIO)
const char* alias;

#if defined(HAVE_ASIO)
if (fInputDevice != paNoDevice && fPaDevices->GetHostFromDevice(fInputDevice) == "ASIO") {
for (int i = 0; i < fCaptureChannels; i++) {
if (PaAsio_GetInputChannelName(fInputDevice, i, &alias) == paNoError) {
Expand Down
17 changes: 14 additions & 3 deletions windows/portaudio/JackPortAudioDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

#include "JackAudioDriver.h"
#include "JackPortAudioDevices.h"

#ifdef _WIN32
#include "JackMMCSS.h"
#endif

namespace Jack
{
Expand All @@ -31,7 +34,11 @@ namespace Jack
\brief The PortAudio driver.
*/

class JackPortAudioDriver : public JackMMCSS, public JackAudioDriver
class JackPortAudioDriver :
#ifdef _WIN32
public JackMMCSS,
#endif
public JackAudioDriver
{

private:
Expand All @@ -42,6 +49,7 @@ class JackPortAudioDriver : public JackMMCSS, public JackAudioDriver
PaDeviceIndex fInputDevice;
PaDeviceIndex fOutputDevice;
PortAudioDevices* fPaDevices;
jack_native_thread_t fReservationLoopThread;

static int Render(const void* inputBuffer, void* outputBuffer,
unsigned long framesPerBuffer,
Expand All @@ -55,8 +63,11 @@ class JackPortAudioDriver : public JackMMCSS, public JackAudioDriver

public:

JackPortAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table, PortAudioDevices* pa_devices)
: JackMMCSS(), JackAudioDriver(name, alias, engine, table), fStream(NULL), fInputBuffer(NULL), fOutputBuffer(NULL),
JackPortAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table, PortAudioDevices* pa_devices) :
#ifdef _WIN32
JackMMCSS(),
#endif
JackAudioDriver(name, alias, engine, table), fStream(NULL), fInputBuffer(NULL), fOutputBuffer(NULL),
fInputDevice(paNoDevice), fOutputDevice(paNoDevice), fPaDevices(pa_devices)
{}

Expand Down
1 change: 0 additions & 1 deletion wscript
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ def options(opt):
'portaudio',
help='Enable Portaudio driver',
conf_dest='BUILD_DRIVER_PORTAUDIO')
portaudio.check(header_name='windows.h') # only build portaudio on windows
portaudio.check_cfg(
package='portaudio-2.0 >= 19',
uselib_store='PORTAUDIO',
Expand Down

0 comments on commit 806da09

Please sign in to comment.