Skip to content

Commit

Permalink
Added new thread to use a queue for sending data after parsing MIDI b…
Browse files Browse the repository at this point in the history
…uffers

Multiple changes to reduce the amount of thread locking
  • Loading branch information
ravelox committed Sep 24, 2020
1 parent fa6d7e0 commit ff4c044
Show file tree
Hide file tree
Showing 38 changed files with 745 additions and 365 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ logging.log_file
logging.log_level
Threshold for log events. Acceptable values are debug,info,normal,warning and error.
Default is normal.
logging.hex_dump
Set to yes to write hex dump of data buffers to log file. This can slow down processing for large buffers if enabled.
Default is no
security.check
If set to yes, it is not possible to write the daemon pid to a file with executable permissions.
Default is yes.
Expand All @@ -207,6 +210,8 @@ file_mode
Default is 0640.
sync.interval
Interval in seconds between SYNC commands for timing purposes. Default is 10s.
journal.write
Set to yes to enable MIDI recovery journal. Default is no.
```

If ALSA is detected, the following options are also available:
Expand All @@ -221,9 +226,6 @@ alsa.input_device
alsa.input_buffer_size
Size of the buffer to use for reading data from the input device.
Default is 4096. Maximum is 65535.
alsa.writeback
If a MIDI command is received from an inbound ALSA device, this option controls whether that event is written to an ALSA output device if it has the same card number.
This is a yes/no option. Default is no.
alsa.writeback
If a MIDI command is received from an inbound ALSA device, this option controls whether that event is written to an ALSA output device if it has the same level number.
See also **alsa.writeback.level**.
Expand Down
2 changes: 1 addition & 1 deletion raveloxmidi/configure.ac
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
m4_define([VERSION_NUMBER],[0.9.2])
m4_define([VERSION_NUMBER],[0.10.0])
m4_define([BUILD_NUMBER],m4_esyscmd_s(pkgscripts/build_number))
m4_define([FULL_VERSION], [VERSION_NUMBER.BUILD_NUMBER])
AC_INIT([raveloxmidi],[FULL_VERSION])
Expand Down
8 changes: 7 additions & 1 deletion raveloxmidi/include/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ typedef struct {

#ifdef INSIDE_LOGGING
int logging_threshold = 3;
int logging_hex_dump = 0;
int logging_enabled = 0;
#else
extern int logging_threshold;
#define DEBUG_ONLY if(logging_threshold!=LOGGING_DEBUG) return;
extern int logging_hex_dump;
extern int logging_enabled;
#define DEBUG_ONLY if( (logging_enabled==0) || (logging_threshold!=LOGGING_DEBUG)) return;
#define INFO_ONLY if( (logging_enabled==0) || (logging_threshold>LOGGING_INFO)) return;
#define HEX_DUMP_ENABLED if(logging_hex_dump==0) return;
#endif

int logging_name_to_value(name_map_t *map, const char *name);
Expand Down
3 changes: 2 additions & 1 deletion raveloxmidi/include/midi_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "data_table.h"
#include "ring_buffer.h"
#include "dbuffer.h"
#include "data_context.h"

typedef enum midi_state_status_t {
MIDI_STATE_INIT,
Expand Down Expand Up @@ -62,6 +63,6 @@ void midi_state_reset( midi_state_t *state );

void midi_state_dump( midi_state_t *state );

void midi_state_to_commands( midi_state_t *state, data_table_t **table, char get_delta );
void midi_state_send( midi_state_t *state, data_context_t *context, char get_delta );

#endif
2 changes: 2 additions & 0 deletions raveloxmidi/include/net_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#ifndef NET_CONNECTION_H
#define NET_CONNECTION_H

#include <arpa/inet.h>

#include <pthread.h>

#include "midi_note.h"
Expand Down
27 changes: 26 additions & 1 deletion raveloxmidi/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ void put_uint64( unsigned char **dest, uint64_t src, size_t *len );
void get_uint32( void *dest, unsigned char **src, size_t *len );
void get_uint64( void *dest, unsigned char **src, size_t *len );
void hex_dump( unsigned char *buffer, size_t len );
void FREENULL( const char *description, void **ptr );
int check_file_security( const char *filepath );
int is_yes( const char *value );
int is_no( const char *value );
Expand All @@ -51,6 +50,32 @@ void utils_unlock( void );
void utils_init( void );
void utils_teardown( void );

// Memory tracking utilities
void utils_mem_tracking_init( void );
void utils_mem_tracking_teardown( void );

void *utils_malloc( size_t size, const char *code_file_name, unsigned int line_number );
void utils_free( void *ptr , const char *code_file_name, unsigned int line_number);
void *utils_realloc( void *orig_ptr, size_t new_size, const char *code_file_name, unsigned int line_number );
char *utils_strdup( const char *s , const char *code_file_name, unsigned int line_number);
void utils_freenull( const char *description, void **ptr, const char *code_file_name, unsigned int line_number );

#define X_MALLOC(a) utils_malloc( a, __FILE__, __LINE__ )
#define X_FREE(a) utils_free( a, __FILE__, __LINE__ )
#define X_REALLOC(a,b) utils_realloc( a,b, __FILE__, __LINE__ )
#define X_STRDUP(a) utils_strdup( a, __FILE__, __LINE__ )
#define X_FREENULL(a,b) utils_freenull( a, b, __FILE__, __LINE__ )

// Mutex tracking utilities
void utils_pthread_tracking_init( void );
void utils_pthread_tracking_teardown( void );
void utils_pthread_mutex_lock( pthread_mutex_t *mutex, const char *code_file_name, unsigned int line_number);
void utils_pthread_mutex_unlock( pthread_mutex_t *mutex, const char *code_file_name, unsigned int line_number);

#define X_MUTEX_LOCK(a) utils_pthread_mutex_lock( a, __FILE__, __LINE__ )
#define X_MUTEX_UNLOCK(a) utils_pthread_mutex_unlock( a, __FILE__, __LINE__ )

// Generic macros
#define MAX(a,b) ( (a) > (b) ? (a) : (b) )
#define MIN(a,b) ( (a) < (b) ? (a) : (b) )

Expand Down
14 changes: 14 additions & 0 deletions raveloxmidi/man/raveloxmidi.1.in
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ Threshold for log events. Acceptable values are \fBdebug\fP , \fBinfo\fP , \fBno
Default is normal.
.TP
.B
logging.hex_dump
Set to yes to write hex dump of data buffers to log file. This can slow down processing for large buffers if enabled.
.br
Default is no
.TP
.B
security.check
If set to yes, it is not possible to write the daemon pid to a file with executable permissions.
.br
Expand Down Expand Up @@ -184,6 +190,12 @@ See
https://developer.apple.com/library/archive/documentation/Audio/Conceptual/MIDINetworkDriverProtocol/MIDI/MIDI.html
for more information
.TP
.B
journal.write
Set to yes to enable MIDI recovery journal.
.br
Default is no.
.TP
If ALSA is detected, the following options are also available:
.TP
.B alsa.output_device
Expand Down Expand Up @@ -215,6 +227,8 @@ Possible values are \fBcard\fP (hw:X,*,*) or \fBdevice\fP (hw:X,Y,*)
.br
Default is card.
.fi
.SH DEBUGGING
For debugging, additional logging can be generated for each memory allocation and release. An environment variable \fBRAVELOXMIDI_MEM_FILE\fP can be set to the name of a file to log the extra information into. This should only be enabled on request.
.SH AUTHOR
.B raveloxmidi
is developed by Dave Kelly (c) @COPYRIGHT_YEAR@
7 changes: 5 additions & 2 deletions raveloxmidi/src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ raveloxmidi_SOURCES = \
utils.c \
raveloxmidi_alsa.c \
kv_table.c \
net_distribute.c \
data_table.c \
dbuffer.c \
dstring.c
dstring.c \
data_queue.c \
data_context.c \
midi_sender.c

raveloxmidi_LDADD = @PTHREAD_LIBS@ @AVAHI_LIBS@ @ALSA_LIBS@
raveloxmidi_CFLAGS = @PTHREAD_CFLAGS@ @AVAHI_CFLAGS@ @ALSA_CFLAGS@

EXTRA_DIST =

AM_CPPFLAGS = -I ../include

4 changes: 3 additions & 1 deletion raveloxmidi/src/applemidi_feedback.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ extern int errno;

#include "logging.h"

#include "utils.h"

void applemidi_feedback_responder( void *data )
{
net_applemidi_feedback *feedback;
Expand Down Expand Up @@ -81,7 +83,7 @@ net_response_t *applemidi_feedback_create( uint32_t ssrc, uint16_t rtp_seq )

if( ! cmd )
{
free( feedback );
X_FREE( feedback );
return NULL;
}

Expand Down
8 changes: 5 additions & 3 deletions raveloxmidi/src/applemidi_inv.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ extern int errno;
#include "raveloxmidi_config.h"
#include "logging.h"

#include "utils.h"

net_response_t * applemidi_inv_responder( char *ip_address, uint16_t port, void *data )
{
net_applemidi_command *cmd = NULL;
Expand Down Expand Up @@ -79,7 +81,7 @@ net_response_t * applemidi_inv_responder( char *ip_address, uint16_t port, void

if( ! accept_inv ) {
logging_printf( LOGGING_ERROR, "applemidi_inv_responder: Unable to allocate memory for accept_inv command data\n");
free( cmd );
X_FREE( cmd );
net_ctx_reset( ctx );
return NULL;
}
Expand All @@ -90,9 +92,9 @@ net_response_t * applemidi_inv_responder( char *ip_address, uint16_t port, void
service_name = config_string_get("service.name");
if( service_name )
{
accept_inv->name = (char *)strdup( service_name );
accept_inv->name = (char *)X_STRDUP( service_name );
} else {
accept_inv->name = (char *)strdup( "RaveloxMIDI" );
accept_inv->name = (char *)X_STRDUP( "RaveloxMIDI" );
}

cmd->data = accept_inv;
Expand Down
16 changes: 8 additions & 8 deletions raveloxmidi/src/applemidi_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ net_response_t * applemidi_sync_responder( void *data )
net_applemidi_sync *sync_resp = NULL;
net_ctx_t *ctx = NULL;
net_response_t *response = NULL;
long delta = 0;
long current_time = 0;
unsigned long local_timestamp = 0;
unsigned long current_time = 0;

if( ! data ) return NULL;

Expand Down Expand Up @@ -69,7 +69,7 @@ net_response_t * applemidi_sync_responder( void *data )

if( ! sync_resp ) {
logging_printf( LOGGING_ERROR, "applemidi_sync_responder: Unable to allocate memory for sync_resp command data\n");
free( cmd );
X_FREE( cmd );
return NULL;
}

Expand All @@ -84,20 +84,20 @@ net_response_t * applemidi_sync_responder( void *data )
memcpy( sync_resp->padding, sync->padding, 3 );

current_time = time_in_microseconds();
delta = current_time - ctx->start;
local_timestamp = current_time - ctx->start;

logging_printf( LOGGING_DEBUG, "applemidi_sync_responder: now=%ld start=%ld delta=%ld\n", current_time, ctx->start, delta );
logging_printf( LOGGING_DEBUG, "applemidi_sync_responder: now=%lu start=%lu local_timestamp=%lu\n", current_time, ctx->start, local_timestamp );

switch( sync_resp->count )
{
case 2:
sync_resp->timestamp3 = delta;
sync_resp->timestamp3 = local_timestamp;
break;
case 1:
sync_resp->timestamp2 = delta;
sync_resp->timestamp2 = local_timestamp;
break;
case 0:
sync_resp->timestamp1 = delta;
sync_resp->timestamp1 = local_timestamp;
break;
}

Expand Down
10 changes: 5 additions & 5 deletions raveloxmidi/src/chapter_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ chapter_c_t *chapter_c_create( void )
uint8_t index = 0;
chapter_c_t *new_chapter_c = NULL;

new_chapter_c = (chapter_c_t *)malloc( sizeof( chapter_c_t ) );
new_chapter_c = (chapter_c_t *)X_MALLOC( sizeof( chapter_c_t ) );
if( ! new_chapter_c )
{
logging_printf(LOGGING_ERROR, "chapter_c_create: Unable to allocate memory to create chapter_c log\n");
Expand Down Expand Up @@ -138,7 +138,7 @@ void chapter_c_pack( chapter_c_t *chapter_c, unsigned char **packed, size_t *siz
if( chapter_c->len == 0 ) return;

*size = PACKED_CHAPTER_C_HEADER_SIZE + ( (chapter_c->len) * PACKED_CONTROLLER_LOG_SIZE );
*packed = (unsigned char *)malloc( *size );
*packed = (unsigned char *)X_MALLOC( *size );

if(! *packed )
{
Expand Down Expand Up @@ -194,7 +194,7 @@ void chapter_c_destroy( chapter_c_t **chapter_c )
if( ! chapter_c ) return;
if( ! *chapter_c ) return;

FREENULL( "chapter_c",(void **) &(*chapter_c) );
X_FREENULL( "chapter_c",(void **) &(*chapter_c) );
}

void chapter_c_reset( chapter_c_t *chapter_c )
Expand Down Expand Up @@ -232,7 +232,7 @@ controller_log_t *controller_log_create( void )
{
controller_log_t *new_controller_log = NULL;

new_controller_log = (controller_log_t *)malloc( sizeof(controller_log_t ) );
new_controller_log = (controller_log_t *)X_MALLOC( sizeof(controller_log_t ) );
if( ! new_controller_log )
{
logging_printf( LOGGING_ERROR,"controller_log_create: Unable to allocate memory for new controller_log_t\n");
Expand All @@ -251,7 +251,7 @@ void controller_log_destroy( controller_log_t **controller_log )
if( ! controller_log ) return;
if( ! *controller_log) return;

FREENULL( "controller_log",(void **)controller_log);
X_FREENULL( "controller_log",(void **)controller_log);
}
void controller_log_reset( controller_log_t *controller_log )
{
Expand Down
Loading

0 comments on commit ff4c044

Please sign in to comment.