mirror of
https://github.com/outbackdingo/UltraGrid.git
synced 2026-03-21 17:40:23 +00:00
using bound checking variants Remained last one instance in utils/text.c, that does the checking by itself and vsnprintf compat using vsprintf, that is not used, anyways.
263 lines
10 KiB
C
263 lines
10 KiB
C
/**
|
|
* @file jack_common.h
|
|
* @author Martin Piatka <piatka@cesnet.cz>
|
|
* Martin Pulec <pulec@cesnet.cz>
|
|
*/
|
|
/*
|
|
* Copyright (c) 2019-2023 CESNET, z. s. p. o.
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, is permitted provided that the following conditions
|
|
* are met:
|
|
*
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
*
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
*
|
|
* 3. Neither the name of CESNET nor the names of its contributors may be
|
|
* used to endorse or promote products derived from this software without
|
|
* specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
|
|
* "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
|
|
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
* EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
#ifndef JACK_COMMON_H
|
|
#define JACK_COMMON_H
|
|
|
|
#ifndef __cplusplus
|
|
#include <stdbool.h>
|
|
#endif // ! defined __cplusplus
|
|
|
|
#include <jack/jack.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "compat/dlfunc.h"
|
|
#include "debug.h"
|
|
#include "types.h"
|
|
|
|
#if defined (__linux__) && defined (_GNU_SOURCE)
|
|
#include "utils/fs.h"
|
|
#define HAVE_DLINFO 1
|
|
#endif
|
|
|
|
typedef int (*jack_activate_t)(jack_client_t *client) JACK_OPTIONAL_WEAK_EXPORT;
|
|
typedef int (*jack_client_close_t)(jack_client_t *client) JACK_OPTIONAL_WEAK_EXPORT;
|
|
typedef jack_client_t *(*jack_client_open_t)(const char *client_name,
|
|
jack_options_t options,
|
|
jack_status_t *status, ...) JACK_OPTIONAL_WEAK_EXPORT;
|
|
typedef int (*jack_connect_t)(jack_client_t *client,
|
|
const char *source_port,
|
|
const char *destination_port) JACK_OPTIONAL_WEAK_EXPORT;
|
|
typedef int (*jack_deactivate_t)(jack_client_t *client) JACK_OPTIONAL_WEAK_EXPORT;
|
|
typedef int (*jack_disconnect_t)(jack_client_t *client,
|
|
const char *source_port,
|
|
const char *destination_port) JACK_OPTIONAL_WEAK_EXPORT;
|
|
typedef void (*jack_free_t)(void* ptr) JACK_OPTIONAL_WEAK_EXPORT;
|
|
typedef const char **(*jack_get_ports_t)(jack_client_t *client,
|
|
const char *port_name_pattern,
|
|
const char *type_name_pattern,
|
|
unsigned long flags) JACK_OPTIONAL_WEAK_EXPORT;
|
|
typedef jack_nframes_t (*jack_get_sample_rate_t)(jack_client_t *) JACK_OPTIONAL_WEAK_EXPORT;
|
|
typedef void * (*jack_port_get_buffer_t)(jack_port_t *port, jack_nframes_t) JACK_OPTIONAL_WEAK_EXPORT;
|
|
typedef const char * (*jack_port_name_t)(const jack_port_t *port) JACK_OPTIONAL_WEAK_EXPORT;
|
|
typedef jack_port_t * (*jack_port_register_t)(jack_client_t *client,
|
|
const char *port_name,
|
|
const char *port_type,
|
|
unsigned long flags,
|
|
unsigned long buffer_size) JACK_OPTIONAL_WEAK_EXPORT;
|
|
typedef int (*jack_set_process_callback_t)(jack_client_t *client,
|
|
JackProcessCallback process_callback,
|
|
void *arg) JACK_OPTIONAL_WEAK_EXPORT;
|
|
typedef int (*jack_set_sample_rate_callback_t)(jack_client_t *client,
|
|
JackSampleRateCallback srate_callback,
|
|
void *arg) JACK_OPTIONAL_WEAK_EXPORT;
|
|
|
|
struct libjack_connection {
|
|
LIB_HANDLE libjack; ///< lib connection
|
|
|
|
jack_activate_t activate;
|
|
jack_client_close_t client_close;
|
|
jack_client_open_t client_open;
|
|
jack_connect_t connect;
|
|
jack_deactivate_t deactivate;
|
|
jack_disconnect_t disconnect;
|
|
jack_free_t free;
|
|
jack_get_ports_t get_ports;
|
|
jack_get_sample_rate_t get_sample_rate;
|
|
jack_port_get_buffer_t port_get_buffer;
|
|
jack_port_name_t port_name;
|
|
jack_port_register_t port_register;
|
|
jack_set_process_callback_t set_process_callback;
|
|
jack_set_sample_rate_callback_t set_sample_rate_callback;
|
|
};
|
|
|
|
static void close_libjack(struct libjack_connection *s)
|
|
{
|
|
if (s == NULL) {
|
|
return;
|
|
}
|
|
#ifdef RTLD_DEFAULT
|
|
if (s->libjack != RTLD_DEFAULT) {
|
|
dlclose(s->libjack);
|
|
}
|
|
#else
|
|
dlclose(s->libjack);
|
|
#endif // defined RTLD_DEFAULT
|
|
free(s);
|
|
}
|
|
|
|
#define JACK_DLSYM(sym) s->sym = (void *) dlsym(s->libjack, "jack_" #sym); if (s->sym == NULL) { log_msg(LOG_LEVEL_ERROR, "JACK symbol %s not found: %s\n", "jack_" #sym, dlerror()); return false; }
|
|
|
|
static bool load_jack_symbols(struct libjack_connection *s)
|
|
{
|
|
JACK_DLSYM(activate);
|
|
JACK_DLSYM(client_close)
|
|
JACK_DLSYM(client_open)
|
|
JACK_DLSYM(connect);
|
|
JACK_DLSYM(deactivate);
|
|
JACK_DLSYM(disconnect);
|
|
JACK_DLSYM(free)
|
|
JACK_DLSYM(get_ports)
|
|
JACK_DLSYM(get_sample_rate);
|
|
JACK_DLSYM(set_sample_rate_callback);
|
|
JACK_DLSYM(port_get_buffer);
|
|
JACK_DLSYM(port_name);
|
|
JACK_DLSYM(port_register);
|
|
JACK_DLSYM(set_process_callback);
|
|
return true;
|
|
}
|
|
|
|
static struct libjack_connection *open_libjack(void)
|
|
{
|
|
struct libjack_connection *s = calloc(1, sizeof(struct libjack_connection));
|
|
|
|
#ifdef RTLD_DEFAULT
|
|
// try to load from already loaded library (eg. by LD_PRELOAD), RTLD_DEFAULT is POSIX-only
|
|
if (dlsym(RTLD_DEFAULT, "jack_activate") != NULL) {
|
|
s->libjack = RTLD_DEFAULT;
|
|
if (load_jack_symbols(s)) {
|
|
log_msg(LOG_LEVEL_VERBOSE, "Using preloaded JACK library.\n");
|
|
return s;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
const char *shlib =
|
|
#ifdef _WIN32
|
|
"C:/Windows/libjack64.dll";
|
|
#elif defined (__APPLE__)
|
|
"libjack.dylib";
|
|
#elif defined (__linux__)
|
|
"libjack.so";
|
|
#else
|
|
"";
|
|
#endif
|
|
s->libjack = dlopen(shlib, RTLD_NOW);
|
|
if (s->libjack == NULL) {
|
|
log_msg(LOG_LEVEL_ERROR, "JACK library \"%s\" opening failed: %s\n", shlib, dlerror());
|
|
free(s);
|
|
return NULL;
|
|
}
|
|
if (!load_jack_symbols(s)) {
|
|
close_libjack(s);
|
|
return NULL;
|
|
}
|
|
#ifdef HAVE_DLINFO
|
|
char path[MAX_PATH_SIZE] = "(unknown)";
|
|
dlinfo(s->libjack, RTLD_DI_ORIGIN, path); // nonstandard GNU extension
|
|
log_msg(LOG_LEVEL_VERBOSE, "Loaded JACK library from %s.\n", path);
|
|
#endif // defined HAVE_DLINFO
|
|
return s;
|
|
}
|
|
|
|
static inline struct device_info *audio_jack_probe(const char *client_name,
|
|
unsigned long port_flags,
|
|
int *count)
|
|
{
|
|
jack_client_t *client;
|
|
jack_status_t status;
|
|
char *last_name = NULL;
|
|
int i;
|
|
const char **ports;
|
|
int port_count = 0;
|
|
struct libjack_connection *libjack = open_libjack();
|
|
if (!libjack) {
|
|
return NULL;
|
|
}
|
|
|
|
*count = 0;
|
|
client = libjack->client_open(client_name, JackNoStartServer, &status);
|
|
if(status & JackFailure) {
|
|
log_msg(LOG_LEVEL_ERROR, "Opening JACK client failed.\n");
|
|
close_libjack(libjack);
|
|
return NULL;
|
|
}
|
|
|
|
ports = libjack->get_ports(client, NULL, NULL, port_flags);
|
|
if(ports == NULL) {
|
|
log_msg(LOG_LEVEL_ERROR, "Unable to enumerate JACK ports.\n");
|
|
close_libjack(libjack);
|
|
return NULL;
|
|
}
|
|
|
|
for(port_count = 0; ports[port_count] != NULL; port_count++);
|
|
|
|
struct device_info *available_devices = port_count > 0 ? calloc(port_count, sizeof(struct device_info)) : NULL;
|
|
|
|
int channel_count = 0;
|
|
for(i = 0; ports[i] != NULL; i++) {
|
|
char *item = strdup(ports[i]);
|
|
assert(item != NULL);
|
|
char *save_ptr = NULL;
|
|
char *name;
|
|
|
|
++channel_count;
|
|
name = strtok_r(item, "_", &save_ptr);
|
|
if (name == NULL) { // shouldn't happen
|
|
log_msg(LOG_LEVEL_ERROR, "Incorrect JACK name: %s!\n", ports[i]);
|
|
free(item);
|
|
continue;
|
|
}
|
|
if(last_name && strcmp(last_name, name) != 0) {
|
|
snprintf(available_devices[*count].name, sizeof available_devices[*count].name, "jack:%s (%d channels)", last_name, channel_count);
|
|
snprintf(available_devices[*count].dev, sizeof available_devices[*count].dev, ":\"%s\"", last_name);
|
|
channel_count = 0;
|
|
(*count)++;
|
|
}
|
|
free(last_name);
|
|
last_name = strdup(name);
|
|
free(item);
|
|
}
|
|
if(last_name) {
|
|
snprintf(available_devices[*count].name, sizeof available_devices[*count].name, "jack:%s (%d channels)", last_name, channel_count);
|
|
snprintf(available_devices[*count].dev, sizeof available_devices[*count].dev, ":\"%s\"", last_name);
|
|
(*count)++;
|
|
}
|
|
free(last_name);
|
|
libjack->free(ports);
|
|
libjack->client_close(client);
|
|
close_libjack(libjack);
|
|
|
|
return available_devices;
|
|
}
|
|
|
|
|
|
#endif //JACK_COMMON_H
|
|
|