Commit e44ab5b9 authored by sletz's avatar sletz
Browse files

Merge from control branch: missing files

git-svn-id: http://subversion.jackaudio.org/jack/jack2/trunk/jackmp@2340 0c269be4-1314-0410-8aa9-9f06e86f4224
parent c0f0fd7c
This diff is collapsed.
/*
Copyright (C) 2001 Paul Davis
Copyright (C) 2004-2008 Grame
Copyright (C) 2008 Nedko Arnaudov
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdarg.h>
#include <stdio.h>
#include "JackError.h"
#include "JackGlobals.h"
#include "JackMessageBuffer.h"
int jack_verbose = 0;
void change_thread_log_function(jack_log_function_t log_function)
{
if (!jack_tls_set(g_key_log_function, (void*)log_function))
{
jack_error("failed to set thread log function");
}
}
EXPORT void set_threaded_log_function()
{
change_thread_log_function(Jack::JackMessageBufferAdd);
}
void jack_log_function(int level, const char *message)
{
void (* log_callback)(const char *);
switch (level)
{
case LOG_LEVEL_INFO:
log_callback = jack_info_callback;
break;
case LOG_LEVEL_ERROR:
log_callback = jack_error_callback;
break;
default:
return;
}
log_callback(message);
}
static
void
jack_format_and_log(int level, const char *prefix, const char *fmt, va_list ap)
{
char buffer[300];
size_t len;
jack_log_function_t log_function;
if (prefix != NULL) {
len = strlen(prefix);
memcpy(buffer, prefix, len);
} else {
len = 0;
}
vsnprintf(buffer + len, sizeof(buffer) - len, fmt, ap);
log_function = (jack_log_function_t)jack_tls_get(g_key_log_function);
/* if log function is not overriden for thread, use default one */
if (log_function == NULL)
{
log_function = jack_log_function;
//log_function(LOG_LEVEL_INFO, "------ Using default log function");
}
else
{
//log_function(LOG_LEVEL_INFO, "++++++ Using thread-specific log function");
}
log_function(level, buffer);
}
EXPORT void jack_error(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
jack_format_and_log(LOG_LEVEL_ERROR, NULL, fmt, ap);
va_end(ap);
}
EXPORT void jack_info(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
jack_format_and_log(LOG_LEVEL_INFO, NULL, fmt, ap);
va_end(ap);
}
EXPORT void jack_log(const char *fmt,...)
{
if (jack_verbose) {
va_list ap;
va_start(ap, fmt);
jack_format_and_log(LOG_LEVEL_INFO, "Jack: ", fmt, ap);
va_end(ap);
}
}
static void default_jack_error_callback(const char *desc)
{
fprintf(stderr, "%s\n", desc);
fflush(stderr);
}
static void default_jack_info_callback (const char *desc)
{
fprintf(stdout, "%s\n", desc);
fflush(stdout);
}
void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
void (*jack_info_callback)(const char *desc) = &default_jack_info_callback;
/*
* messagebuffer.h -- realtime-safe message interface for jackd.
*
* This function is included in libjack so backend drivers can use
* it, *not* for external client processes. The VERBOSE() and
* MESSAGE() macros are realtime-safe.
*/
/*
* Copyright (C) 2004 Rui Nuno Capela, Steve Harris
* Copyright (C) 2008 Nedko Arnaudov
* Copyright (C) 2008 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.
*
*/
#include "JackMessageBuffer.h"
#include "JackGlobals.h"
#include "JackError.h"
namespace Jack
{
JackMessageBuffer* JackMessageBuffer::fInstance = NULL;
JackMessageBuffer::JackMessageBuffer():fInBuffer(0),fOutBuffer(0),fOverruns(0)
{
fThread = JackGlobals::MakeThread(this);
fSignal = JackGlobals::MakeInterProcessSync();
fMutex = new JackMutex();
fThread->Start();
}
JackMessageBuffer::~JackMessageBuffer()
{
if (fOverruns > 0) {
jack_error("WARNING: %d message buffer overruns!", fOverruns);
} else {
jack_info("no message buffer overruns");
}
fThread->SetStatus(JackThread::kIdle);
fSignal->SignalAll();
fThread->Stop();
Flush();
delete fThread;
delete fMutex;
delete fSignal;
}
void JackMessageBuffer::Flush()
{
while (fOutBuffer != fInBuffer) {
jack_log_function(fBuffers[fOutBuffer].level, fBuffers[fOutBuffer].message);
fOutBuffer = MB_NEXT(fOutBuffer);
}
}
void JackMessageBuffer::AddMessage(int level, const char *message)
{
if (fMutex->Trylock()) {
fBuffers[fInBuffer].level = level;
strncpy(fBuffers[fInBuffer].message, message, MB_BUFFERSIZE);
fInBuffer = MB_NEXT(fInBuffer);
fSignal->SignalAll();
fMutex->Unlock();
} else { /* lock collision */
INC_ATOMIC(&fOverruns);
}
}
bool JackMessageBuffer::Execute()
{
fSignal->Wait();
fMutex->Lock();
Flush();
fMutex->Unlock();
return true;
}
void JackMessageBuffer::Create()
{
if (fInstance == NULL) {
fInstance = new JackMessageBuffer();
}
}
void JackMessageBuffer::Destroy()
{
if (fInstance != NULL) {
delete fInstance;
fInstance = NULL;
}
}
void JackMessageBufferAdd(int level, const char *message)
{
if (Jack::JackMessageBuffer::fInstance == NULL) {
/* Unable to print message with realtime safety.
* Complain and print it anyway. */
jack_log_function(LOG_LEVEL_ERROR, "messagebuffer not initialized, skip message");
} else {
Jack::JackMessageBuffer::fInstance->AddMessage(level, message);
}
}
};
/*
* messagebuffer.h -- realtime-safe message interface for jackd.
*
* This function is included in libjack so backend drivers can use
* it, *not* for external client processes. The VERBOSE() and
* MESSAGE() macros are realtime-safe.
*/
/*
* Copyright (C) 2004 Rui Nuno Capela, Steve Harris
* Copyright (C) 2008 Nedko Arnaudov
* Copyright (C) 2008 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 __JackMessageBuffer__
#define __JackMessageBuffer__
#include "JackThread.h"
#include "JackMutex.h"
#include "JackAtomic.h"
#include "JackSyncInterface.h"
namespace Jack
{
/* MB_NEXT() relies on the fact that MB_BUFFERS is a power of two */
#define MB_BUFFERS 128
#define MB_NEXT(index) ((index+1) & (MB_BUFFERS-1))
#define MB_BUFFERSIZE 256 /* message length limit */
struct JackMessage
{
int level;
char message[MB_BUFFERSIZE];
};
class JackMessageBuffer : public JackRunnableInterface
{
private:
JackMessage fBuffers[MB_BUFFERS];
JackMutex* fMutex;
JackThread* fThread;
JackSyncInterface* fSignal;
volatile unsigned int fInBuffer;
volatile unsigned int fOutBuffer;
SInt32 fOverruns;
void Flush();
public:
JackMessageBuffer();
~JackMessageBuffer();
// JackRunnableInterface interface
bool Execute();
void static Create();
void static Destroy();
void AddMessage(int level, const char *message);
static JackMessageBuffer* fInstance;
};
#ifdef __cplusplus
extern "C"
{
#endif
void JackMessageBufferAdd(int level, const char *message);
#ifdef __cplusplus
}
#endif
};
#endif
/*
* messagebuffer.h -- realtime-safe message interface for jackd.
*
* This function is included in libjack so backend drivers can use
* it, *not* for external client processes. The VERBOSE() and
* MESSAGE() macros are realtime-safe.
*/
/*
* Copyright (C) 2004 Rui Nuno Capela, Steve Harris
* Copyright (C) 2008 Nedko Arnaudov
* Copyright (C) 2008 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 __JackMessageBuffer__
#define __JackMessageBuffer__
#include "JackThread.h"
#include "JackMutex.h"
#include "JackAtomic.h"
#include "JackSyncInterface.h"
namespace Jack
{
/* MB_NEXT() relies on the fact that MB_BUFFERS is a power of two */
#define MB_BUFFERS 128
#define MB_NEXT(index) ((index+1) & (MB_BUFFERS-1))
#define MB_BUFFERSIZE 256 /* message length limit */
struct JackMessage
{
int level;
char message[MB_BUFFERSIZE];
};
class JackMessageBuffer : public JackRunnableInterface
{
private:
JackMessage fBuffers[MB_BUFFERS];
JackMutex* fMutex;
JackThread* fThread;
JackSyncInterface* fSignal;
volatile unsigned int fInBuffer;
volatile unsigned int fOutBuffer;
SInt32 fOverruns;
void Flush();
public:
JackMessageBuffer();
~JackMessageBuffer();
// JackRunnableInterface interface
bool Execute();
void static Create();
void static Destroy();
void AddMessage(int level, const char *message);
static JackMessageBuffer* fInstance;
};
#ifdef __cplusplus
extern "C"
{
#endif
void JackMessageBufferAdd(int level, const char *message);
#ifdef __cplusplus
}
#endif
};
#endif
/*
Copyright (C) 2004-2006 Grame
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "JackProcessSync.h"
#include "JackError.h"
namespace Jack
{
bool JackProcessSync::TimedWait(long usec)
{
struct timeval T0, T1;
timespec time;
struct timeval now;
int res;
pthread_mutex_lock(&fLock);
jack_log("JackProcessSync::TimedWait time out = %ld", usec);
gettimeofday(&T0, 0);
gettimeofday(&now, 0);
unsigned int next_date_usec = now.tv_usec + usec;
time.tv_sec = now.tv_sec + (next_date_usec / 1000000);
time.tv_nsec = (next_date_usec % 1000000) * 1000;
res = pthread_cond_timedwait(&fCond, &fLock, &time);
if (res != 0)
jack_error("pthread_cond_timedwait error usec = %ld err = %s", usec, strerror(res));
gettimeofday(&T1, 0);
pthread_mutex_unlock(&fLock);
jack_log("JackProcessSync::TimedWait finished delta = %5.1lf",
(1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
return (res == 0);
}
void JackProcessSync::Wait()
{
int res;
pthread_mutex_lock(&fLock);
//jack_log("JackProcessSync::Wait...");
if ((res = pthread_cond_wait(&fCond, &fLock)) != 0)
jack_error("pthread_cond_wait error err = %s", strerror(errno));
pthread_mutex_unlock(&fLock);
//jack_log("JackProcessSync::Wait finished");
}
bool JackInterProcessSync::TimedWait(long usec)
{
struct timeval T0, T1;
//jack_log("JackInterProcessSync::TimedWait...");
gettimeofday(&T0, 0);
bool res = fSynchro->TimedWait(usec);
gettimeofday(&T1, 0);
//jack_log("JackInterProcessSync::TimedWait finished delta = %5.1lf", (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
return res;
}
} // end of namespace
/*
JACK control API
Copyright (C) 2008 Nedko Arnaudov
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __control_types__
#define __control_types__
#include "jslist.h"
#include "JackExports.h"
#ifdef WIN32
typedef HANDLE sigset_t;
#endif
/** Parameter types, intentionally similar to jack_driver_param_type_t */
typedef enum
{
JackParamInt = 1, /**< @brief value type is a signed integer */
JackParamUInt, /**< @brief value type is an unsigned integer */
JackParamChar, /**< @brief value type is a char */
JackParamString, /**< @brief value type is a string with max size of ::JACK_PARAM_STRING_MAX+1 chars */
JackParamBool, /**< @brief value type is a boolean */
} jackctl_param_type_t;
/** @brief Max value that jackctl_param_type_t type can have */
#define JACK_PARAM_MAX (JackParamBool + 1)
/** @brief Max length of string parameter value, excluding terminating nul char */
#define JACK_PARAM_STRING_MAX 63
/** @brief Type for parameter value */
/* intentionally similar to jack_driver_param_value_t */
union jackctl_parameter_value
{
uint32_t ui; /**< @brief member used for ::JackParamUInt */
int32_t i; /**< @brief member used for ::JackParamInt */
char c; /**< @brief member used for ::JackParamChar */
char str[JACK_PARAM_STRING_MAX + 1]; /**< @brief member used for ::JackParamString */
bool b; /**< @brief member used for ::JackParamBool */
};
/** opaque type for server object */
typedef struct jackctl_server jackctl_server_t;
/** opaque type for driver object */
typedef struct jackctl_driver jackctl_driver_t;
/** opaque type for parameter object */
typedef struct jackctl_parameter jackctl_parameter_t;
#ifdef __cplusplus
extern "C" {
#endif
#if 0
} /* Adjust editor indent */
#endif
EXPORT sigset_t
jackctl_setup_signals(
unsigned int flags);
EXPORT void
jackctl_wait_signals(
sigset_t signals);
EXPORT jackctl_server_t *
jackctl_server_create();
EXPORT void
jackctl_server_destroy(
jackctl_server_t * server);
EXPORT const JSList *
jackctl_server_get_drivers_list(
jackctl_server_t * server);
EXPORT bool
jackctl_server_start(
jackctl_server_t * server,
jackctl_driver_t * driver);
EXPORT bool
jackctl_server_stop(
jackctl_server_t * server);
EXPORT const JSList *
jackctl_server_get_parameters(
jackctl_server_t * server);