Add mac audio capture
- Add CoreAudio device input capture for mac audio capturing. The code should cover just about everything for capturing mac input device audio. Because of the way mac audio is designed, users may have no choice but to obtain the open source soundflower software to capture their mac's desktop audio. It may be necessary for us to distribute it with the program as well. - Hide event backend - Use win32 events for windows - Allow timed waits for events - Fix a few warnings
This commit is contained in:
149
libobs/util/threading-posix.c
Normal file
149
libobs/util/threading-posix.c
Normal file
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
* Copyright (c) 2014 Hugh Bailey <obs.jim@gmail.com>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
#include "bmem.h"
|
||||
#include "threading.h"
|
||||
|
||||
struct event_data {
|
||||
pthread_mutex_t mutex;
|
||||
pthread_cond_t cond;
|
||||
volatile bool signalled;
|
||||
bool manual;
|
||||
};
|
||||
|
||||
int event_init(event_t *event, enum event_type type)
|
||||
{
|
||||
int code = 0;
|
||||
|
||||
struct event_data *data = bzalloc(sizeof(struct event_data));
|
||||
|
||||
if ((code = pthread_mutex_init(&data->mutex, NULL)) < 0) {
|
||||
bfree(data);
|
||||
return code;
|
||||
}
|
||||
|
||||
if ((code = pthread_cond_init(&data->cond, NULL)) < 0) {
|
||||
pthread_mutex_destroy(&data->mutex);
|
||||
bfree(data);
|
||||
return code;
|
||||
}
|
||||
|
||||
data->manual = (type == EVENT_TYPE_MANUAL);
|
||||
data->signalled = false;
|
||||
*event = data;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void event_destroy(event_t event)
|
||||
{
|
||||
if (event) {
|
||||
pthread_mutex_destroy(&event->mutex);
|
||||
pthread_cond_destroy(&event->cond);
|
||||
bfree(event);
|
||||
}
|
||||
}
|
||||
|
||||
int event_wait(event_t event)
|
||||
{
|
||||
int code = 0;
|
||||
pthread_mutex_lock(&event->mutex);
|
||||
if (!event->signalled)
|
||||
code = pthread_cond_wait(&event->cond, &event->mutex);
|
||||
|
||||
if (code == 0) {
|
||||
if (!event->manual)
|
||||
event->signalled = false;
|
||||
pthread_mutex_unlock(&event->mutex);
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
static inline void add_ms_to_ts(struct timespec *ts,
|
||||
unsigned long milliseconds)
|
||||
{
|
||||
ts->tv_sec += milliseconds/1000;
|
||||
ts->tv_nsec += (milliseconds%1000)*1000000;
|
||||
if (ts->tv_nsec > 1000000000) {
|
||||
ts->tv_sec += 1;
|
||||
ts->tv_nsec -= 1000000000;
|
||||
}
|
||||
}
|
||||
|
||||
int event_timedwait(event_t event, unsigned long milliseconds)
|
||||
{
|
||||
int code = 0;
|
||||
pthread_mutex_lock(&event->mutex);
|
||||
if (!event->signalled) {
|
||||
struct timespec ts;
|
||||
#ifdef __APPLE__
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
ts.tv_sec = tv.tv_sec;
|
||||
ts.tv_nsec = tv.tv_usec * 1000;
|
||||
#else
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
#endif
|
||||
add_ms_to_ts(&ts, milliseconds);
|
||||
code = pthread_cond_timedwait(&event->cond, &event->mutex, &ts);
|
||||
}
|
||||
|
||||
if (code == 0) {
|
||||
if (!event->manual)
|
||||
event->signalled = false;
|
||||
pthread_mutex_unlock(&event->mutex);
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
int event_try(event_t event)
|
||||
{
|
||||
pthread_mutex_lock(&event->mutex);
|
||||
if (event->signalled) {
|
||||
if (!event->manual)
|
||||
event->signalled = false;
|
||||
pthread_mutex_unlock(&event->mutex);
|
||||
return 0;
|
||||
}
|
||||
pthread_mutex_unlock(&event->mutex);
|
||||
|
||||
return EAGAIN;
|
||||
}
|
||||
|
||||
int event_signal(event_t event)
|
||||
{
|
||||
int code = 0;
|
||||
|
||||
pthread_mutex_lock(&event->mutex);
|
||||
code = pthread_cond_signal(&event->cond);
|
||||
event->signalled = true;
|
||||
pthread_mutex_unlock(&event->mutex);
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
void event_reset(event_t event)
|
||||
{
|
||||
pthread_mutex_lock(&event->mutex);
|
||||
event->signalled = false;
|
||||
pthread_mutex_unlock(&event->mutex);
|
||||
}
|
114
libobs/util/threading-windows.c
Normal file
114
libobs/util/threading-windows.c
Normal file
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright (c) 2014 Hugh Bailey <obs.jim@gmail.com>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "bmem.h"
|
||||
#include "threading.h"
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
struct event_data {
|
||||
HANDLE handle;
|
||||
};
|
||||
|
||||
int event_init(event_t *event, enum event_type type)
|
||||
{
|
||||
HANDLE handle;
|
||||
struct event_data *data;
|
||||
|
||||
handle = CreateEvent(NULL, (type == EVENT_TYPE_MANUAL), FALSE, NULL);
|
||||
if (!handle)
|
||||
return -1;
|
||||
|
||||
data = bmalloc(sizeof(struct event_data));
|
||||
data->handle = handle;
|
||||
|
||||
*event = data;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void event_destroy(event_t event)
|
||||
{
|
||||
if (event) {
|
||||
CloseHandle(event->handle);
|
||||
bfree(event);
|
||||
}
|
||||
}
|
||||
|
||||
int event_wait(event_t event)
|
||||
{
|
||||
DWORD code;
|
||||
|
||||
if (!event)
|
||||
return EINVAL;
|
||||
|
||||
code = WaitForSingleObject(event->handle, INFINITE);
|
||||
if (code != WAIT_OBJECT_0)
|
||||
return EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int event_timedwait(event_t event, unsigned long milliseconds)
|
||||
{
|
||||
DWORD code;
|
||||
|
||||
if (!event)
|
||||
return EINVAL;
|
||||
|
||||
code = WaitForSingleObject(event->handle, milliseconds);
|
||||
if (code == WAIT_TIMEOUT)
|
||||
return ETIMEDOUT;
|
||||
else if (code != WAIT_OBJECT_0)
|
||||
return EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int event_try(event_t event)
|
||||
{
|
||||
DWORD code;
|
||||
|
||||
if (!event)
|
||||
return EINVAL;
|
||||
|
||||
code = WaitForSingleObject(event->handle, 0);
|
||||
if (code == WAIT_TIMEOUT)
|
||||
return EAGAIN;
|
||||
else if (code != WAIT_OBJECT_0)
|
||||
return EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int event_signal(event_t event)
|
||||
{
|
||||
if (!event)
|
||||
return EINVAL;
|
||||
|
||||
if (!SetEvent(event->handle))
|
||||
return EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void event_reset(event_t event)
|
||||
{
|
||||
if (!event)
|
||||
return;
|
||||
|
||||
ResetEvent(event->handle);
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
|
||||
* Copyright (c) 2013-2014 Hugh Bailey <obs.jim@gmail.com>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
@@ -46,90 +46,22 @@ static inline void pthread_mutex_init_value(pthread_mutex_t *mutex)
|
||||
*mutex = init_val;
|
||||
}
|
||||
|
||||
struct event_data {
|
||||
pthread_mutex_t mutex;
|
||||
pthread_cond_t cond;
|
||||
bool signalled;
|
||||
bool manual;
|
||||
};
|
||||
|
||||
enum event_type {
|
||||
EVENT_TYPE_AUTO,
|
||||
EVENT_TYPE_MANUAL
|
||||
};
|
||||
|
||||
typedef struct event_data event_t;
|
||||
struct event_data;
|
||||
typedef struct event_data *event_t;
|
||||
|
||||
static inline int event_init(event_t *event, enum event_type type)
|
||||
{
|
||||
int code = 0;
|
||||
EXPORT int event_init(event_t *event, enum event_type type);
|
||||
EXPORT void event_destroy(event_t event);
|
||||
EXPORT int event_wait(event_t event);
|
||||
EXPORT int event_timedwait(event_t event, unsigned long milliseconds);
|
||||
EXPORT int event_try(event_t event);
|
||||
EXPORT int event_signal(event_t event);
|
||||
EXPORT void event_reset(event_t event);
|
||||
|
||||
if ((code = pthread_mutex_init(&event->mutex, NULL)) < 0)
|
||||
return code;
|
||||
|
||||
if ((code = pthread_cond_init(&event->cond, NULL)) < 0)
|
||||
pthread_mutex_destroy(&event->mutex);
|
||||
|
||||
event->manual = (type == EVENT_TYPE_MANUAL);
|
||||
event->signalled = false;
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
static inline void event_destroy(event_t *event)
|
||||
{
|
||||
if (event) {
|
||||
pthread_mutex_destroy(&event->mutex);
|
||||
pthread_cond_destroy(&event->cond);
|
||||
}
|
||||
}
|
||||
|
||||
static inline int event_wait(event_t *event)
|
||||
{
|
||||
int code = 0;
|
||||
pthread_mutex_lock(&event->mutex);
|
||||
if (event->signalled ||
|
||||
(code = pthread_cond_wait(&event->cond, &event->mutex)) == 0) {
|
||||
if (!event->manual)
|
||||
event->signalled = false;
|
||||
pthread_mutex_unlock(&event->mutex);
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
static inline int event_try(event_t *event)
|
||||
{
|
||||
pthread_mutex_lock(&event->mutex);
|
||||
if (event->signalled) {
|
||||
if (!event->manual)
|
||||
event->signalled = false;
|
||||
pthread_mutex_unlock(&event->mutex);
|
||||
return 0;
|
||||
}
|
||||
pthread_mutex_unlock(&event->mutex);
|
||||
|
||||
return EAGAIN;
|
||||
}
|
||||
|
||||
static inline int event_signal(event_t *event)
|
||||
{
|
||||
int code = 0;
|
||||
|
||||
pthread_mutex_lock(&event->mutex);
|
||||
code = pthread_cond_signal(&event->cond);
|
||||
event->signalled = true;
|
||||
pthread_mutex_unlock(&event->mutex);
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
static inline void event_reset(event_t *event)
|
||||
{
|
||||
pthread_mutex_lock(&event->mutex);
|
||||
event->signalled = false;
|
||||
pthread_mutex_unlock(&event->mutex);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
Reference in New Issue
Block a user