mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-11-09 13:30:06 -05:00
move things around
This commit is contained in:
parent
847cef83b6
commit
d1655196c3
130 changed files with 363 additions and 335 deletions
143
src/modules/module-jack/defs.h
Normal file
143
src/modules/module-jack/defs.h
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
/* PipeWire
|
||||
* Copyright (C) 2017 Wim Taymans <wim.taymans@gmail.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include "pipewire/log.h"
|
||||
|
||||
#define USE_POSIX_SHM
|
||||
#undef JACK_MONITOR
|
||||
|
||||
#define JACK_DEFAULT_SERVER_NAME "default"
|
||||
#define JACK_SOCKET_DIR "/dev/shm"
|
||||
#define JACK_SHM_DIR "/dev/shm"
|
||||
#define JACK_SERVER_NAME_SIZE 256
|
||||
#define JACK_CLIENT_NAME_SIZE 64
|
||||
#define JACK_PORT_NAME_SIZE 256
|
||||
#define JACK_PORT_TYPE_SIZE 32
|
||||
#define JACK_PROTOCOL_VERSION 8
|
||||
|
||||
#define PORT_NUM_MAX 4096
|
||||
#define PORT_NUM_FOR_CLIENT 2048
|
||||
#define CONNECTION_NUM_FOR_PORT PORT_NUM_FOR_CLIENT
|
||||
|
||||
#define REAL_JACK_PORT_NAME_SIZE JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE
|
||||
|
||||
#define BUFFER_SIZE_MAX 8192
|
||||
|
||||
#define CLIENT_NUM 256
|
||||
|
||||
#define JACK_ENGINE_ROLLING_COUNT 32
|
||||
#define JACK_ENGINE_ROLLING_INTERVAL 1024
|
||||
|
||||
#define TIME_POINTS 100000
|
||||
#define FAILURE_TIME_POINTS 10000
|
||||
#define FAILURE_WINDOW 10
|
||||
#define MEASURED_CLIENTS 32
|
||||
|
||||
#define SYNC_MAX_NAME_SIZE 256
|
||||
|
||||
#define JACK_UUID_SIZE 36
|
||||
#define JACK_UUID_STRING_SIZE (JACK_UUID_SIZE+1)
|
||||
|
||||
#define JACK_SESSION_COMMAND_SIZE 256
|
||||
|
||||
#define NO_PORT 0xFFFE
|
||||
#define EMPTY 0xFFFD
|
||||
#define FREE 0xFFFC
|
||||
|
||||
|
||||
|
||||
typedef enum {
|
||||
JACK_TIMER_SYSTEM_CLOCK,
|
||||
JACK_TIMER_HPET,
|
||||
} jack_timer_type_t;
|
||||
|
||||
enum jack_request_type {
|
||||
jack_request_RegisterPort = 1,
|
||||
jack_request_UnRegisterPort = 2,
|
||||
jack_request_ConnectPorts = 3,
|
||||
jack_request_DisconnectPorts = 4,
|
||||
jack_request_SetTimeBaseClient = 5,
|
||||
jack_request_ActivateClient = 6,
|
||||
jack_request_DeactivateClient = 7,
|
||||
jack_request_DisconnectPort = 8,
|
||||
jack_request_SetClientCapabilities = 9,
|
||||
jack_request_GetPortConnections = 10,
|
||||
jack_request_GetPortNConnections = 11,
|
||||
jack_request_ReleaseTimebase = 12,
|
||||
jack_request_SetTimebaseCallback = 13,
|
||||
jack_request_SetBufferSize = 20,
|
||||
jack_request_SetFreeWheel = 21,
|
||||
jack_request_ClientCheck = 22,
|
||||
jack_request_ClientOpen = 23,
|
||||
jack_request_ClientClose = 24,
|
||||
jack_request_ConnectNamePorts = 25,
|
||||
jack_request_DisconnectNamePorts = 26,
|
||||
jack_request_GetInternalClientName = 27,
|
||||
jack_request_InternalClientHandle = 28,
|
||||
jack_request_InternalClientLoad = 29,
|
||||
jack_request_InternalClientUnload = 30,
|
||||
jack_request_PortRename = 31,
|
||||
jack_request_Notification = 32,
|
||||
jack_request_SessionNotify = 33,
|
||||
jack_request_SessionReply = 34,
|
||||
jack_request_GetClientByUUID = 35,
|
||||
jack_request_ReserveClientName = 36,
|
||||
jack_request_GetUUIDByClient = 37,
|
||||
jack_request_ClientHasSessionCallback = 38,
|
||||
jack_request_ComputeTotalLatencies = 39
|
||||
};
|
||||
|
||||
enum jack_notification_type {
|
||||
jack_notify_AddClient = 0,
|
||||
jack_notify_RemoveClient = 1,
|
||||
jack_notify_ActivateClient = 2,
|
||||
jack_notify_XRunCallback = 3,
|
||||
jack_notify_GraphOrderCallback = 4,
|
||||
jack_notify_BufferSizeCallback = 5,
|
||||
jack_notify_SampleRateCallback = 6,
|
||||
jack_notify_StartFreewheelCallback = 7,
|
||||
jack_notify_StopFreewheelCallback = 8,
|
||||
jack_notify_PortRegistrationOnCallback = 9,
|
||||
jack_notify_PortRegistrationOffCallback = 10,
|
||||
jack_notify_PortConnectCallback = 11,
|
||||
jack_notify_PortDisconnectCallback = 12,
|
||||
jack_notify_PortRenameCallback = 13,
|
||||
jack_notify_RealTimeCallback = 14,
|
||||
jack_notify_ShutDownCallback = 15,
|
||||
jack_notify_QUIT = 16,
|
||||
jack_notify_SessionCallback = 17,
|
||||
jack_notify_LatencyCallback = 18,
|
||||
jack_notify_max = 64 // To keep some room in JackClientControl fCallback table
|
||||
};
|
||||
|
||||
#define kActivateClient_size (2*sizeof(int))
|
||||
#define kDeactivateClient_size (sizeof(int))
|
||||
#define kRegisterPort_size (sizeof(int) + JACK_PORT_NAME_SIZE+1 + JACK_PORT_TYPE_SIZE+1 + 2*sizeof(unsigned int))
|
||||
#define kClientCheck_size (JACK_CLIENT_NAME_SIZE+1 + 4 * sizeof(int))
|
||||
#define kClientOpen_size (JACK_CLIENT_NAME_SIZE+1 + 2 * sizeof(int))
|
||||
#define kClientClose_size (sizeof(int))
|
||||
#define kConnectNamePorts_size (sizeof(int) + REAL_JACK_PORT_NAME_SIZE+1 + REAL_JACK_PORT_NAME_SIZE+1)
|
||||
#define kGetUUIDByClient_size (JACK_CLIENT_NAME_SIZE+1)
|
||||
|
||||
#define CheckRead(var,size) if(read(client->fd,var,size)!=size) {pw_log_error("read error"); return -1; }
|
||||
#define CheckWrite(var,size) if(write(client->fd,var,size)!=size) {pw_log_error("write error"); return -1; }
|
||||
#define CheckSize(expected) { int __size; CheckRead(&__size, sizeof(int)); if (__size != expected) { pw_log_error("CheckSize error size %d != %d", __size, (int)expected); return -1; } }
|
||||
|
||||
#define jack_error pw_log_error
|
||||
#define jack_log pw_log_info
|
||||
54
src/modules/module-jack/server.h
Normal file
54
src/modules/module-jack/server.h
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/* PipeWire
|
||||
* Copyright (C) 2017 Wim Taymans <wim.taymans@gmail.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
struct jack_client {
|
||||
int ref_num;
|
||||
struct client *owner;
|
||||
struct jack_client_control *control;
|
||||
struct pw_node *node;
|
||||
};
|
||||
|
||||
struct jack_server {
|
||||
pthread_mutex_t lock;
|
||||
|
||||
bool promiscuous;
|
||||
|
||||
struct jack_graph_manager *graph_manager;
|
||||
struct jack_engine_control *engine_control;
|
||||
|
||||
struct jack_client* client_table[CLIENT_NUM];
|
||||
struct jack_synchro synchro_table[CLIENT_NUM];
|
||||
};
|
||||
|
||||
static inline int
|
||||
jack_server_allocate_ref_num(struct jack_server *server)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < CLIENT_NUM; i++)
|
||||
if (server->client_table[i] == NULL)
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static inline void
|
||||
jack_server_free_ref_num(struct jack_server *server, int ref_num)
|
||||
{
|
||||
server->client_table[ref_num] = NULL;
|
||||
}
|
||||
601
src/modules/module-jack/shared.h
Normal file
601
src/modules/module-jack/shared.h
Normal file
|
|
@ -0,0 +1,601 @@
|
|||
/* PipeWire
|
||||
* Copyright (C) 2015 Wim Taymans <wim.taymans@gmail.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
|
||||
extern int segment_num;
|
||||
|
||||
static inline int jack_shm_alloc(size_t size, jack_shm_info_t *info, int num)
|
||||
{
|
||||
char name[64];
|
||||
|
||||
snprintf(name, sizeof(name), "/jack_shared%d", num);
|
||||
|
||||
if (jack_shmalloc(name, size, info)) {
|
||||
pw_log_error("Cannot create shared memory segment of size = %zd (%s)", size, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (jack_attach_shm(info)) {
|
||||
jack_error("Cannot attach shared memory segment name = %s err = %s", name, strerror(errno));
|
||||
jack_destroy_shm(info);
|
||||
return -1;
|
||||
}
|
||||
info->size = size;
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef uint16_t jack_int_t; // Internal type for ports and refnum
|
||||
|
||||
typedef enum {
|
||||
NotTriggered,
|
||||
Triggered,
|
||||
Running,
|
||||
Finished,
|
||||
} jack_client_state_t;
|
||||
|
||||
PRE_PACKED_STRUCTURE
|
||||
struct jack_client_timing {
|
||||
jack_time_t signaled_at;
|
||||
jack_time_t awake_at;
|
||||
jack_time_t finished_at;
|
||||
jack_client_state_t status;
|
||||
} POST_PACKED_STRUCTURE;
|
||||
|
||||
#define JACK_CLIENT_TIMING_INIT (struct jack_client_timing) { 0, 0, 0, NotTriggered }
|
||||
|
||||
PRE_PACKED_STRUCTURE
|
||||
struct jack_port {
|
||||
int type_id;
|
||||
enum JackPortFlags flags;
|
||||
char name[REAL_JACK_PORT_NAME_SIZE];
|
||||
char alias1[REAL_JACK_PORT_NAME_SIZE];
|
||||
char alias2[REAL_JACK_PORT_NAME_SIZE];
|
||||
int ref_num;
|
||||
|
||||
jack_nframes_t latency;
|
||||
jack_nframes_t total_latency;
|
||||
jack_latency_range_t playback_latency;
|
||||
jack_latency_range_t capture_latency;
|
||||
uint8_t monitor_requests;
|
||||
|
||||
bool in_use;
|
||||
jack_port_id_t tied;
|
||||
jack_default_audio_sample_t buffer[BUFFER_SIZE_MAX + 8];
|
||||
} POST_PACKED_STRUCTURE;
|
||||
|
||||
static inline void jack_port_init(struct jack_port *port, int ref_num,
|
||||
const char* port_name, int type_id, enum JackPortFlags flags)
|
||||
{
|
||||
port->type_id = type_id;
|
||||
port->flags = flags;
|
||||
strcpy(port->name, port_name);
|
||||
port->alias1[0] = '\0';
|
||||
port->alias2[0] = '\0';
|
||||
port->ref_num = ref_num;
|
||||
port->latency = 0;
|
||||
port->total_latency = 0;
|
||||
port->playback_latency.min = port->playback_latency.max = 0;
|
||||
port->capture_latency.min = port->capture_latency.max = 0;
|
||||
port->monitor_requests = 0;
|
||||
port->in_use = true;
|
||||
port->tied = NO_PORT;
|
||||
}
|
||||
|
||||
#define MAKE_FIXED_ARRAY(size) \
|
||||
PRE_PACKED_STRUCTURE \
|
||||
struct { \
|
||||
jack_int_t table[size]; \
|
||||
uint32_t counter; \
|
||||
} POST_PACKED_STRUCTURE
|
||||
|
||||
#define INIT_FIXED_ARRAY(arr) ({ \
|
||||
int i; \
|
||||
for (i = 0; i < SPA_N_ELEMENTS(arr.table); i++) \
|
||||
arr.table[i] = EMPTY; \
|
||||
arr.counter = 0; \
|
||||
})
|
||||
|
||||
#define ADD_FIXED_ARRAY(arr,item) ({ \
|
||||
int i,ret = -1; \
|
||||
for (i = 0; i < SPA_N_ELEMENTS(arr.table); i++) { \
|
||||
if (arr.table[i] == EMPTY) { \
|
||||
arr.table[i] = item; \
|
||||
arr.counter++; \
|
||||
ret = 0; \
|
||||
break; \
|
||||
} \
|
||||
} \
|
||||
ret; \
|
||||
})
|
||||
|
||||
#define MAKE_FIXED_ARRAY1(size) \
|
||||
PRE_PACKED_STRUCTURE \
|
||||
struct { \
|
||||
MAKE_FIXED_ARRAY(size) array; \
|
||||
bool used; \
|
||||
} POST_PACKED_STRUCTURE
|
||||
|
||||
#define INIT_FIXED_ARRAY1(arr) ({ \
|
||||
INIT_FIXED_ARRAY(arr.array); \
|
||||
arr.used = false; \
|
||||
})
|
||||
|
||||
#define ADD_FIXED_ARRAY1(arr,item) ADD_FIXED_ARRAY(arr.array,item)
|
||||
|
||||
#define MAKE_FIXED_MATRIX(size) \
|
||||
PRE_PACKED_STRUCTURE \
|
||||
struct { \
|
||||
jack_int_t table[size][size]; \
|
||||
} POST_PACKED_STRUCTURE
|
||||
|
||||
#define INIT_FIXED_MATRIX(mat,idx) ({ \
|
||||
int i; \
|
||||
for (i = 0; i < SPA_N_ELEMENTS(mat.table[0]); i++){ \
|
||||
mat.table[idx][i] = 0; \
|
||||
mat.table[i][idx] = 0; \
|
||||
} \
|
||||
})
|
||||
|
||||
PRE_PACKED_STRUCTURE
|
||||
struct jack_activation_count {
|
||||
int32_t value;
|
||||
int32_t count;
|
||||
} POST_PACKED_STRUCTURE;
|
||||
|
||||
static inline void jack_activation_count_set_value(struct jack_activation_count *cnt, int32_t val) {
|
||||
cnt->value = val;
|
||||
}
|
||||
|
||||
#define MAKE_LOOP_FEEDBACK(size) \
|
||||
PRE_PACKED_STRUCTURE \
|
||||
struct { \
|
||||
int table[size][3]; \
|
||||
} POST_PACKED_STRUCTURE
|
||||
|
||||
#define INIT_LOOP_FEEDBACK(arr,size) ({ \
|
||||
int i; \
|
||||
for (i = 0; i < size; i++) { \
|
||||
arr.table[i][0] = EMPTY; \
|
||||
arr.table[i][1] = EMPTY; \
|
||||
arr.table[i][2] = 0; \
|
||||
} \
|
||||
})
|
||||
|
||||
PRE_PACKED_STRUCTURE
|
||||
struct jack_connection_manager {
|
||||
MAKE_FIXED_ARRAY(CONNECTION_NUM_FOR_PORT) connection[PORT_NUM_MAX];
|
||||
MAKE_FIXED_ARRAY1(PORT_NUM_FOR_CLIENT) input_port[CLIENT_NUM];
|
||||
MAKE_FIXED_ARRAY(PORT_NUM_FOR_CLIENT) output_port[CLIENT_NUM];
|
||||
MAKE_FIXED_MATRIX(CLIENT_NUM) connection_ref;
|
||||
struct jack_activation_count input_counter[CLIENT_NUM];
|
||||
MAKE_LOOP_FEEDBACK(CONNECTION_NUM_FOR_PORT) loop_feedback;
|
||||
} POST_PACKED_STRUCTURE;
|
||||
|
||||
static inline void
|
||||
jack_connection_manager_init_ref_num(struct jack_connection_manager *conn, int ref_num)
|
||||
{
|
||||
INIT_FIXED_ARRAY1(conn->input_port[ref_num]);
|
||||
INIT_FIXED_ARRAY(conn->output_port[ref_num]);
|
||||
INIT_FIXED_MATRIX(conn->connection_ref, ref_num);
|
||||
jack_activation_count_set_value (&conn->input_counter[ref_num], 0);
|
||||
}
|
||||
|
||||
static inline void
|
||||
jack_connection_manager_init(struct jack_connection_manager *conn)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < PORT_NUM_MAX; i++)
|
||||
INIT_FIXED_ARRAY(conn->connection[i]);
|
||||
|
||||
INIT_LOOP_FEEDBACK(conn->loop_feedback, CONNECTION_NUM_FOR_PORT);
|
||||
|
||||
for (i = 0; i < CLIENT_NUM; i++)
|
||||
jack_connection_manager_init_ref_num(conn, i);
|
||||
}
|
||||
|
||||
static inline int
|
||||
jack_connection_manager_add_port(struct jack_connection_manager *conn, bool input,
|
||||
int ref_num, jack_port_id_t port_id)
|
||||
{
|
||||
if (input)
|
||||
return ADD_FIXED_ARRAY1(conn->input_port[ref_num], port_id);
|
||||
else
|
||||
return ADD_FIXED_ARRAY(conn->output_port[ref_num], port_id);
|
||||
}
|
||||
|
||||
PRE_PACKED_STRUCTURE
|
||||
struct jack_atomic_counter {
|
||||
union {
|
||||
struct {
|
||||
uint16_t short_val1; // Cur
|
||||
uint16_t short_val2; // Next
|
||||
} scounter;
|
||||
uint32_t long_val;
|
||||
} info;
|
||||
} POST_PACKED_STRUCTURE;
|
||||
|
||||
#define Counter(e) (e).info.long_val
|
||||
#define CurIndex(e) (e).info.scounter.short_val1
|
||||
#define NextIndex(e) (e).info.scounter.short_val2
|
||||
|
||||
#define CurArrayIndex(e) (CurIndex(e) & 0x0001)
|
||||
#define NextArrayIndex(e) ((CurIndex(e) + 1) & 0x0001)
|
||||
|
||||
#define MAKE_ATOMIC_STATE(type) \
|
||||
PRE_PACKED_STRUCTURE \
|
||||
struct { \
|
||||
type state[2]; \
|
||||
volatile struct jack_atomic_counter counter; \
|
||||
int32_t call_write_counter; \
|
||||
} POST_PACKED_STRUCTURE
|
||||
|
||||
PRE_PACKED_STRUCTURE
|
||||
struct jack_atomic_array_counter {
|
||||
union {
|
||||
struct {
|
||||
unsigned char byte_val[4];
|
||||
} scounter;
|
||||
uint32_t long_val;
|
||||
} info;
|
||||
} POST_PACKED_STRUCTURE;
|
||||
|
||||
#define MAKE_ATOMIC_ARRAY_STATE(type) \
|
||||
PRE_PACKED_STRUCTURE \
|
||||
struct { \
|
||||
type state[3]; \
|
||||
volatile struct jack_atomic_array_counter counter; \
|
||||
} POST_PACKED_STRUCTURE
|
||||
|
||||
PRE_PACKED_STRUCTURE
|
||||
struct jack_graph_manager {
|
||||
jack_shm_info_t info;
|
||||
MAKE_ATOMIC_STATE(struct jack_connection_manager) state;
|
||||
unsigned int port_max;
|
||||
struct jack_client_timing client_timing[CLIENT_NUM];
|
||||
struct jack_port port_array[0];
|
||||
} POST_PACKED_STRUCTURE;
|
||||
|
||||
static inline struct jack_graph_manager *
|
||||
jack_graph_manager_alloc(int port_max)
|
||||
{
|
||||
struct jack_graph_manager *mgr;
|
||||
jack_shm_info_t info;
|
||||
size_t i, size;
|
||||
|
||||
size = sizeof(struct jack_graph_manager) + port_max * sizeof(struct jack_port);
|
||||
|
||||
if (jack_shm_alloc(size, &info, segment_num++) < 0)
|
||||
return NULL;
|
||||
|
||||
mgr = (struct jack_graph_manager *)jack_shm_addr(&info);
|
||||
mgr->info = info;
|
||||
|
||||
jack_connection_manager_init(&mgr->state.state[0]);
|
||||
jack_connection_manager_init(&mgr->state.state[1]);
|
||||
mgr->port_max = port_max;
|
||||
|
||||
for (i = 0; i < port_max; i++) {
|
||||
mgr->port_array[i].in_use = false;
|
||||
mgr->port_array[i].ref_num = -1;
|
||||
}
|
||||
return mgr;
|
||||
}
|
||||
|
||||
static inline jack_port_id_t
|
||||
jack_graph_manager_allocate_port(struct jack_graph_manager *mgr,
|
||||
int ref_num, const char* port_name, int type_id,
|
||||
enum JackPortFlags flags)
|
||||
{
|
||||
int i;
|
||||
for (i = 1; i < mgr->port_max; i++) {
|
||||
if (!mgr->port_array[i].in_use) {
|
||||
jack_port_init(&mgr->port_array[i], ref_num, port_name, type_id, flags);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return NO_PORT;
|
||||
}
|
||||
|
||||
|
||||
static inline struct jack_connection_manager *
|
||||
jack_graph_manager_next_start(struct jack_graph_manager *manager)
|
||||
{
|
||||
uint32_t next_index;
|
||||
|
||||
if (manager->state.call_write_counter++ == 0) {
|
||||
struct jack_atomic_counter old_val;
|
||||
struct jack_atomic_counter new_val;
|
||||
uint32_t cur_index;
|
||||
bool need_copy;
|
||||
do {
|
||||
old_val = manager->state.counter;
|
||||
new_val = old_val;
|
||||
cur_index = CurArrayIndex(new_val);
|
||||
next_index = NextArrayIndex(new_val);
|
||||
need_copy = (CurIndex(new_val) == NextIndex(new_val));
|
||||
NextIndex(new_val) = CurIndex(new_val); // Invalidate next index
|
||||
}
|
||||
while (!__atomic_compare_exchange_n((uint32_t*)&manager->state.counter,
|
||||
(uint32_t*)&Counter(old_val),
|
||||
Counter(new_val),
|
||||
false,
|
||||
__ATOMIC_SEQ_CST,
|
||||
__ATOMIC_SEQ_CST));
|
||||
|
||||
if (need_copy)
|
||||
memcpy(&manager->state.state[next_index],
|
||||
&manager->state.state[cur_index],
|
||||
sizeof(struct jack_connection_manager));
|
||||
}
|
||||
else {
|
||||
next_index = NextArrayIndex(manager->state.counter);
|
||||
}
|
||||
return &manager->state.state[next_index];
|
||||
}
|
||||
|
||||
static inline void
|
||||
jack_graph_manager_next_stop(struct jack_graph_manager *manager)
|
||||
{
|
||||
if (--manager->state.call_write_counter == 0) {
|
||||
struct jack_atomic_counter old_val;
|
||||
struct jack_atomic_counter new_val;
|
||||
do {
|
||||
old_val = manager->state.counter;
|
||||
new_val = old_val;
|
||||
NextIndex(new_val)++; // Set next index
|
||||
}
|
||||
while (!__atomic_compare_exchange_n((uint32_t*)&manager->state.counter,
|
||||
(uint32_t*)&Counter(old_val),
|
||||
Counter(new_val),
|
||||
false,
|
||||
__ATOMIC_SEQ_CST,
|
||||
__ATOMIC_SEQ_CST));
|
||||
}
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
TransportCommandNone = 0,
|
||||
TransportCommandStart = 1,
|
||||
TransportCommandStop = 2,
|
||||
} transport_command_t;
|
||||
|
||||
PRE_PACKED_STRUCTURE
|
||||
struct jack_transport_engine {
|
||||
MAKE_ATOMIC_ARRAY_STATE(jack_position_t) state;
|
||||
jack_transport_state_t transport_state;
|
||||
volatile transport_command_t transport_cmd;
|
||||
transport_command_t previous_cmd; /* previous transport_cmd */
|
||||
jack_time_t sync_timeout;
|
||||
int sync_time_left;
|
||||
int time_base_master;
|
||||
bool pending_pos;
|
||||
bool network_sync;
|
||||
bool conditionnal;
|
||||
int32_t write_counter;
|
||||
} POST_PACKED_STRUCTURE;
|
||||
|
||||
PRE_PACKED_STRUCTURE
|
||||
struct jack_timer {
|
||||
jack_nframes_t frames;
|
||||
jack_time_t current_wakeup;
|
||||
jack_time_t current_callback;
|
||||
jack_time_t next_wakeup;
|
||||
float period_usecs;
|
||||
float filter_omega; /* set once, never altered */
|
||||
bool initialized;
|
||||
} POST_PACKED_STRUCTURE;
|
||||
|
||||
PRE_PACKED_STRUCTURE
|
||||
struct jack_frame_timer {
|
||||
MAKE_ATOMIC_STATE(struct jack_timer) state;
|
||||
bool first_wakeup;
|
||||
} POST_PACKED_STRUCTURE;
|
||||
|
||||
#ifdef JACK_MONITOR
|
||||
PRE_PACKED_STRUCTURE
|
||||
struct jack_timing_measure_client {
|
||||
int ref_num;
|
||||
jack_time_t signaled_at;
|
||||
jack_time_t awake_at;
|
||||
jack_time_t finished_at;
|
||||
jack_client_state_t status;
|
||||
} POST_PACKED_STRUCTURE;
|
||||
|
||||
PRE_PACKED_STRUCTURE
|
||||
struct jack_timing_client_interval {
|
||||
int ref_num;
|
||||
char name[JACK_CLIENT_NAME_SIZE+1];
|
||||
int begin_interval;
|
||||
int end_interval;
|
||||
} POST_PACKED_STRUCTURE;
|
||||
|
||||
PRE_PACKED_STRUCTURE
|
||||
struct jack_timing_measure {
|
||||
unsigned int audio_cycle;
|
||||
jack_time_t period_usecs;
|
||||
jack_time_t cur_cycle_begin;
|
||||
jack_time_t prev_cycle_end;
|
||||
struct jack_timing_measure_client client_table[CLIENT_NUM];
|
||||
} POST_PACKED_STRUCTURE;
|
||||
|
||||
PRE_PACKED_STRUCTURE
|
||||
struct jack_engine_profiling {
|
||||
struct jack_timing_measure profile_table[TIME_POINTS];
|
||||
struct jack_timing_client_interval interval_table[MEASURED_CLIENTS];
|
||||
|
||||
unsigned int audio_cycle;
|
||||
unsigned int measured_client;
|
||||
} POST_PACKED_STRUCTURE;
|
||||
#endif
|
||||
|
||||
PRE_PACKED_STRUCTURE
|
||||
struct jack_engine_control {
|
||||
jack_shm_info_t info;
|
||||
jack_nframes_t buffer_size;
|
||||
jack_nframes_t sample_rate;
|
||||
bool sync_mode;
|
||||
bool temporary;
|
||||
jack_time_t period_usecs;
|
||||
jack_time_t timeout_usecs;
|
||||
float max_delayed_usecs;
|
||||
float xrun_delayed_usecs;
|
||||
bool timeout;
|
||||
bool real_time;
|
||||
bool saved_real_time;
|
||||
int server_priority;
|
||||
int client_priority;
|
||||
int max_client_priority;
|
||||
char server_name[JACK_SERVER_NAME_SIZE];
|
||||
struct jack_transport_engine transport;
|
||||
jack_timer_type_t clock_source;
|
||||
int driver_num;
|
||||
bool verbose;
|
||||
|
||||
jack_time_t prev_cycle_time;
|
||||
jack_time_t cur_cycle_time;
|
||||
jack_time_t spare_usecs;
|
||||
jack_time_t max_usecs;
|
||||
jack_time_t rolling_client_usecs[JACK_ENGINE_ROLLING_COUNT];
|
||||
unsigned int rolling_client_usecs_cnt;
|
||||
int rolling_client_usecs_index;
|
||||
int rolling_interval;
|
||||
float CPU_load;
|
||||
|
||||
uint64_t period;
|
||||
uint64_t computation;
|
||||
uint64_t constraint;
|
||||
|
||||
struct jack_frame_timer frame_timer;
|
||||
|
||||
#ifdef JACK_MONITOR
|
||||
struct jack_engine_profiling profiler;
|
||||
#endif
|
||||
} POST_PACKED_STRUCTURE;
|
||||
|
||||
static inline void
|
||||
jack_engine_control_reset_rolling_usecs(struct jack_engine_control *ctrl)
|
||||
{
|
||||
memset(ctrl->rolling_client_usecs, 0, sizeof(ctrl->rolling_client_usecs));
|
||||
ctrl->rolling_client_usecs_index = 0;
|
||||
ctrl->rolling_client_usecs_cnt = 0;
|
||||
ctrl->spare_usecs = 0;
|
||||
ctrl->rolling_interval = floor((JACK_ENGINE_ROLLING_INTERVAL * 1000.f) / ctrl->period_usecs);
|
||||
}
|
||||
|
||||
static inline struct jack_engine_control *
|
||||
jack_engine_control_alloc(const char* name)
|
||||
{
|
||||
struct jack_engine_control *ctrl;
|
||||
jack_shm_info_t info;
|
||||
size_t size;
|
||||
|
||||
size = sizeof(struct jack_engine_control);
|
||||
if (jack_shm_alloc(size, &info, segment_num++) < 0)
|
||||
return NULL;
|
||||
|
||||
ctrl = (struct jack_engine_control *)jack_shm_addr(&info);
|
||||
ctrl->info = info;
|
||||
|
||||
ctrl->buffer_size = 512;
|
||||
ctrl->sample_rate = 48000;
|
||||
ctrl->sync_mode = false;
|
||||
ctrl->temporary = false;
|
||||
ctrl->period_usecs = 1000000.f / ctrl->sample_rate * ctrl->buffer_size;
|
||||
ctrl->timeout_usecs = 0;
|
||||
ctrl->max_delayed_usecs = 0.f;
|
||||
ctrl->xrun_delayed_usecs = 0.f;
|
||||
ctrl->timeout = false;
|
||||
ctrl->real_time = true;
|
||||
ctrl->saved_real_time = false;
|
||||
ctrl->server_priority = 20;
|
||||
ctrl->client_priority = 15;
|
||||
ctrl->max_client_priority = 19;
|
||||
strcpy(ctrl->server_name, name);
|
||||
ctrl->clock_source = 0;
|
||||
ctrl->driver_num = 0;
|
||||
ctrl->verbose = true;
|
||||
|
||||
ctrl->prev_cycle_time = 0;
|
||||
ctrl->cur_cycle_time = 0;
|
||||
ctrl->spare_usecs = 0;
|
||||
ctrl->max_usecs = 0;
|
||||
jack_engine_control_reset_rolling_usecs(ctrl);
|
||||
ctrl->CPU_load = 0.f;
|
||||
|
||||
ctrl->period = 0;
|
||||
ctrl->computation = 0;
|
||||
ctrl->constraint = 0;
|
||||
|
||||
return ctrl;
|
||||
}
|
||||
|
||||
PRE_PACKED_STRUCTURE
|
||||
struct jack_client_control {
|
||||
jack_shm_info_t info;
|
||||
char name[JACK_CLIENT_NAME_SIZE+1];
|
||||
bool callback[jack_notify_max];
|
||||
volatile jack_transport_state_t transport_state;
|
||||
volatile bool transport_sync;
|
||||
volatile bool transport_timebase;
|
||||
int ref_num;
|
||||
int PID;
|
||||
bool active;
|
||||
|
||||
int session_ID;
|
||||
char session_command[JACK_SESSION_COMMAND_SIZE];
|
||||
jack_session_flags_t session_flags;
|
||||
} POST_PACKED_STRUCTURE;
|
||||
|
||||
static inline struct jack_client_control *
|
||||
jack_client_control_alloc(const char* name, int pid, int ref_num, int uuid)
|
||||
{
|
||||
struct jack_client_control *ctrl;
|
||||
jack_shm_info_t info;
|
||||
size_t size;
|
||||
|
||||
size = sizeof(struct jack_client_control);
|
||||
if (jack_shm_alloc(size, &info, segment_num++) < 0)
|
||||
return NULL;
|
||||
|
||||
ctrl = (struct jack_client_control *)jack_shm_addr(&info);
|
||||
ctrl->info = info;
|
||||
|
||||
strcpy(ctrl->name, name);
|
||||
for (int i = 0; i < jack_notify_max; i++)
|
||||
ctrl->callback[i] = false;
|
||||
|
||||
// Always activated
|
||||
ctrl->callback[jack_notify_AddClient] = true;
|
||||
ctrl->callback[jack_notify_RemoveClient] = true;
|
||||
ctrl->callback[jack_notify_ActivateClient] = true;
|
||||
ctrl->callback[jack_notify_LatencyCallback] = true;
|
||||
// So that driver synchro are correctly setup in "flush" or "normal" mode
|
||||
ctrl->callback[jack_notify_StartFreewheelCallback] = true;
|
||||
ctrl->callback[jack_notify_StopFreewheelCallback] = true;
|
||||
ctrl->ref_num = ref_num;
|
||||
ctrl->PID = pid;
|
||||
ctrl->transport_state = JackTransportStopped;
|
||||
ctrl->transport_sync = false;
|
||||
ctrl->transport_timebase = false;
|
||||
ctrl->active = false;
|
||||
ctrl->session_ID = uuid;
|
||||
|
||||
return ctrl;
|
||||
}
|
||||
1303
src/modules/module-jack/shm.c
Normal file
1303
src/modules/module-jack/shm.c
Normal file
File diff suppressed because it is too large
Load diff
216
src/modules/module-jack/shm.h
Normal file
216
src/modules/module-jack/shm.h
Normal file
|
|
@ -0,0 +1,216 @@
|
|||
/* This module provides a set of abstract shared memory interfaces
|
||||
* with support using both System V and POSIX shared memory
|
||||
* implementations. The code is divided into three sections:
|
||||
*
|
||||
* - common (interface-independent) code
|
||||
* - POSIX implementation
|
||||
* - System V implementation
|
||||
* - Windows implementation
|
||||
*
|
||||
* The implementation used is determined by whether USE_POSIX_SHM was
|
||||
* set in the ./configure step.
|
||||
*/
|
||||
|
||||
/*
|
||||
Copyright (C) 2001-2003 Paul Davis
|
||||
Copyright (C) 2005-2012 Grame
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef __jack_shm_h__
|
||||
#define __jack_shm_h__
|
||||
|
||||
#include <limits.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <jack/types.h>
|
||||
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#define MAX_SERVERS 8 /* maximum concurrent servers */
|
||||
#define MAX_SHM_ID 256 /* generally about 16 per server */
|
||||
#define JACK_SHM_MAGIC 0x4a41434b /* shm magic number: "JACK" */
|
||||
#define JACK_SHM_NULL_INDEX -1 /* NULL SHM index */
|
||||
#define JACK_SHM_REGISTRY_INDEX -2 /* pseudo SHM index for registry */
|
||||
|
||||
|
||||
/* On Mac OS X, SHM_NAME_MAX is the maximum length of a shared memory
|
||||
* segment name (instead of NAME_MAX or PATH_MAX as defined by the
|
||||
* standard).
|
||||
*/
|
||||
#ifdef USE_POSIX_SHM
|
||||
|
||||
#ifndef NAME_MAX
|
||||
#define NAME_MAX 255
|
||||
#endif
|
||||
|
||||
#ifndef SHM_NAME_MAX
|
||||
#define SHM_NAME_MAX NAME_MAX
|
||||
#endif
|
||||
typedef char shm_name_t[SHM_NAME_MAX];
|
||||
typedef shm_name_t jack_shm_id_t;
|
||||
|
||||
#elif WIN32
|
||||
#define NAME_MAX 255
|
||||
#ifndef SHM_NAME_MAX
|
||||
#define SHM_NAME_MAX NAME_MAX
|
||||
#endif
|
||||
typedef char shm_name_t[SHM_NAME_MAX];
|
||||
typedef shm_name_t jack_shm_id_t;
|
||||
|
||||
#elif __ANDROID__
|
||||
|
||||
#ifndef NAME_MAX
|
||||
#define NAME_MAX 255
|
||||
#endif
|
||||
|
||||
#ifndef SHM_NAME_MAX
|
||||
#define SHM_NAME_MAX NAME_MAX
|
||||
#endif
|
||||
typedef char shm_name_t[SHM_NAME_MAX];
|
||||
typedef shm_name_t jack_shm_id_t;
|
||||
typedef int jack_shm_fd_t;
|
||||
|
||||
#else
|
||||
/* System V SHM */
|
||||
typedef int jack_shm_id_t;
|
||||
#endif /* SHM type */
|
||||
|
||||
/* shared memory type */
|
||||
typedef enum {
|
||||
shm_POSIX = 1, /* POSIX shared memory */
|
||||
shm_SYSV = 2, /* System V shared memory */
|
||||
shm_WIN32 = 3, /* Windows 32 shared memory */
|
||||
shm_ANDROID = 4 /* Android shared memory */
|
||||
} jack_shmtype_t;
|
||||
|
||||
typedef int16_t jack_shm_registry_index_t;
|
||||
|
||||
/**
|
||||
* A structure holding information about shared memory allocated by
|
||||
* JACK. this persists across invocations of JACK, and can be used by
|
||||
* multiple JACK servers. It contains no pointers and is valid across
|
||||
* address spaces.
|
||||
*
|
||||
* The registry consists of two parts: a header including an array of
|
||||
* server names, followed by an array of segment registry entries.
|
||||
*/
|
||||
typedef struct _jack_shm_server {
|
||||
#ifdef WIN32
|
||||
int pid; /* process ID */
|
||||
#else
|
||||
pid_t pid; /* process ID */
|
||||
#endif
|
||||
|
||||
char name[JACK_SERVER_NAME_SIZE];
|
||||
}
|
||||
jack_shm_server_t;
|
||||
|
||||
typedef struct _jack_shm_header {
|
||||
uint32_t magic; /* magic number */
|
||||
uint16_t protocol; /* JACK protocol version */
|
||||
jack_shmtype_t type; /* shm type */
|
||||
jack_shmsize_t size; /* total registry segment size */
|
||||
jack_shmsize_t hdr_len; /* size of header */
|
||||
jack_shmsize_t entry_len; /* size of registry entry */
|
||||
jack_shm_server_t server[MAX_SERVERS]; /* current server array */
|
||||
}
|
||||
jack_shm_header_t;
|
||||
|
||||
typedef struct _jack_shm_registry {
|
||||
jack_shm_registry_index_t index; /* offset into the registry */
|
||||
|
||||
#ifdef WIN32
|
||||
int allocator; /* PID that created shm segment */
|
||||
#else
|
||||
pid_t allocator; /* PID that created shm segment */
|
||||
#endif
|
||||
|
||||
jack_shmsize_t size; /* for POSIX unattach */
|
||||
jack_shm_id_t id; /* API specific, see above */
|
||||
#ifdef __ANDROID__
|
||||
jack_shm_fd_t fd;
|
||||
#endif
|
||||
}
|
||||
jack_shm_registry_t;
|
||||
|
||||
#define JACK_SHM_REGISTRY_SIZE (sizeof (jack_shm_header_t) \
|
||||
+ sizeof (jack_shm_registry_t) * MAX_SHM_ID)
|
||||
|
||||
/**
|
||||
* a structure holding information about shared memory
|
||||
* allocated by JACK. this version is valid only
|
||||
* for a given address space. It contains a pointer
|
||||
* indicating where the shared memory has been
|
||||
* attached to the address space.
|
||||
*/
|
||||
|
||||
PRE_PACKED_STRUCTURE
|
||||
struct _jack_shm_info {
|
||||
jack_shm_registry_index_t index; /* offset into the registry */
|
||||
uint32_t size;
|
||||
#ifdef __ANDROID__
|
||||
jack_shm_fd_t fd;
|
||||
#endif
|
||||
union {
|
||||
void *attached_at; /* address where attached */
|
||||
char ptr_size[8];
|
||||
} ptr; /* a "pointer" that has the same 8 bytes size when compling in 32 or 64 bits */
|
||||
} POST_PACKED_STRUCTURE;
|
||||
|
||||
typedef struct _jack_shm_info jack_shm_info_t;
|
||||
|
||||
/* utility functions used only within JACK */
|
||||
|
||||
void jack_shm_copy_from_registry (jack_shm_info_t*,
|
||||
jack_shm_registry_index_t);
|
||||
void jack_shm_copy_to_registry (jack_shm_info_t*,
|
||||
jack_shm_registry_index_t*);
|
||||
int jack_release_shm_info (jack_shm_registry_index_t);
|
||||
char* jack_shm_addr (jack_shm_info_t* si);
|
||||
|
||||
// here begin the API
|
||||
int jack_register_server (const char *server_name, int new_registry);
|
||||
int jack_unregister_server (const char *server_name);
|
||||
|
||||
int jack_initialize_shm (const char *server_name);
|
||||
int jack_initialize_shm_server (void);
|
||||
int jack_initialize_shm_client (void);
|
||||
int jack_cleanup_shm (void);
|
||||
|
||||
int jack_shmalloc (const char *shm_name, jack_shmsize_t size,
|
||||
jack_shm_info_t* result);
|
||||
void jack_release_shm (jack_shm_info_t*);
|
||||
void jack_release_lib_shm (jack_shm_info_t*);
|
||||
void jack_destroy_shm (jack_shm_info_t*);
|
||||
int jack_attach_shm (jack_shm_info_t*);
|
||||
int jack_attach_lib_shm (jack_shm_info_t*);
|
||||
int jack_attach_shm_read (jack_shm_info_t*);
|
||||
int jack_attach_lib_shm_read (jack_shm_info_t*);
|
||||
int jack_resize_shm (jack_shm_info_t*, jack_shmsize_t size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __jack_shm_h__ */
|
||||
48
src/modules/module-jack/synchro.h
Normal file
48
src/modules/module-jack/synchro.h
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
/* PipeWire
|
||||
* Copyright (C) 2017 Wim Taymans <wim.taymans@gmail.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
struct jack_synchro {
|
||||
char name[SYNC_MAX_NAME_SIZE];
|
||||
bool flush;
|
||||
sem_t *semaphore;
|
||||
};
|
||||
|
||||
#define JACK_SYNCHRO_INIT (struct jack_synchro) { { 0, }, false, NULL }
|
||||
|
||||
static inline int
|
||||
jack_synchro_init(struct jack_synchro *synchro,
|
||||
const char *client_name,
|
||||
const char *server_name,
|
||||
int value, bool internal,
|
||||
bool promiscuous)
|
||||
{
|
||||
if (promiscuous)
|
||||
snprintf(synchro->name, sizeof(synchro->name),
|
||||
"jack_sem.%s_%s", server_name, client_name);
|
||||
else
|
||||
snprintf(synchro->name, sizeof(synchro->name),
|
||||
"jack_sem.%d_%s_%s", getuid(), server_name, client_name);
|
||||
|
||||
synchro->flush = false;
|
||||
if ((synchro->semaphore = sem_open(synchro->name, O_CREAT | O_RDWR, 0777, value)) == (sem_t*)SEM_FAILED) {
|
||||
pw_log_error("can't check in named semaphore name = %s err = %s", synchro->name, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue