Commit 195787d4 authored by Jean-Paul Saman's avatar Jean-Paul Saman

audio_output/amem.c: Abstract vlc_sem_* calls into amem_sem_* calls

Abstract vlc_sem_* calls into amem_sem_* calls.
parent e73d940f
......@@ -123,6 +123,7 @@ typedef sem_t vlc_sem_t;
#elif defined( WIN32 )
#if !defined( UNDER_CE )
typedef HANDLE vlc_thread_t;
typedef HANDLE vlc_sem_t;
#else
typedef struct
{
......
......@@ -93,10 +93,10 @@ static const int format_int[] = { VLC_CODEC_U8,
/* Shared memory header structure */
typedef struct
{
vlc_sem_t sem; /* handle to semaphore */
#if defined(_WIN32_WINNT) || defined(WIN32)
WAVEFORMATEX wave;
WAVEFORMATEX wave;
#else
vlc_sem_t sem;
vlc_fourcc_t i_format;
unsigned int i_rate;
unsigned int i_bytes_per_frame;
......@@ -116,7 +116,6 @@ typedef struct
/* Private structure */
struct aout_sys_t
{
char *psz_name;
int i_delay;
#if defined(_WIN32_WINNT) || defined(WIN32)
......@@ -126,6 +125,9 @@ struct aout_sys_t
#endif
amem_shared_t *header;
char *psz_name;
bool b_own_semaphore;
};
static int Open (vlc_object_t *);
......@@ -138,6 +140,82 @@ static void ExitSharedMem(aout_instance_t *);
/*
* Shared Memory helpers
*/
static void amem_sem_init(vlc_sem_t *sem, unsigned value)
{
#if defined(_WIN32_WINNT) || defined(WIN32)
sem = CreateSemaphore(NULL /*LPSECURITY_ATTRIBUTES*/, 1, 1, NULL);
if (sem == NULL)
abort();
#else
if (unlikely(sem_init(sem, 1 /* share between process*/, value)) == -1)
abort();
#endif
}
static int amem_sem_trywait(vlc_sem_t *sem)
{
int again = 0;
do
{
#if defined(_WIN32_WINNT) || defined(WIN32)
DWORD wait = WaitForSingleObject(sem, 0L);
switch(wait)
{
case WAIT_OBJECT_0:
return VLC_SUCCESS;
case WAIT_TIMEOUT:
again++;
break;
default:
return VLC_EGENERIC;
}
#else
if (sem_trywait(sem) == 0)
break;
if (errno == EAGAIN)
again++;
#endif
if (again == 3)
return VLC_EGENERIC;
} while (again < 3);
return VLC_SUCCESS;
}
static void amem_sem_wait(vlc_sem_t *sem)
{
#if defined(_WIN32_WINNT) || defined(WIN32)
DWORD wait = WaitForSingleObject(sem, 0L);
assert(wait == WAIT_OBJECT_0);
#else
vlc_sem_wait(sem);
#endif
}
static void amem_sem_post(vlc_sem_t *sem)
{
#if defined(_WIN32_WINNT) || defined(WIN32)
BOOL ret = ReleaseSemaphore(sem, 1, NULL);
#else
int ret = vlc_sem_post(sem);
#endif
assert(ret >= 0);
}
static void amem_sem_destroy(vlc_sem_t *sem)
{
#if defined(_WIN32_WINNT) || defined(WIN32)
BOOL ret = CloseHandle(sem);
assert(ret >= 0);
#else
vlc_sem_destroy(sem);
#endif
}
#if defined(_WIN32_WINNT) || defined(WIN32)
/* FIXME: not tested yet */
static int amem_shmem_create(aout_instance_t *aout, ssize_t len)
......@@ -176,6 +254,8 @@ static int amem_shmem_create(aout_instance_t *aout, ssize_t len)
}
sys->header->samples = sys->header + sizeof(amem_shared_t);
amem_sem_init(&sys->header->sem, 1);
sys->b_own_semaphore = true;
return VLC_SUCCESS;
}
......@@ -214,8 +294,6 @@ static int amem_shmem_open(aout_instance_t *aout, ssize_t len)
sys->header->samples = sys->header + sizeof(amem_shared_t);
/* FIXME: initialize semaphore */
return VLC_SUCCESS;
}
......@@ -223,7 +301,8 @@ static void amem_shmem_close(aout_instance_t *aout)
{
aout_sys_t *sys = (aout_sys_t *)aout->output.p_sys;
/* FIXME: release semaphore */
if (sys->b_own_semaphore)
amem_sem_destroy(&sys->header->sem);
UnmapViewOfFile(sys->header);
CloseHandle(sys->handle);
......@@ -233,7 +312,7 @@ static void amem_shmem_header(aout_instance_t *aout)
{
aout_sys_t *sys = (aout_sys_t *)aout->output.p_sys;
vlc_sem_wait(&sys->header->sem);
amem_sem_wait(&sys->header->sem);
/* Fill header struct */
uint16_t tag = 0;
......@@ -255,31 +334,24 @@ static void amem_shmem_header(aout_instance_t *aout)
memset(sys->header->samples, 0, (aout->output.i_nb_samples *
aout->output.output.i_bitspersample));
if (vlc_sem_post(&sys->header->sem) < 0)
msg_Err(aout, "failed unlocking semaphore (%m)");
amem_sem_post(&sys->header->sem);
}
static void amem_shmem_write(aout_instance_t *aout, aout_buffer_t *data)
{
aout_sys_t *sys = (aout_sys_t *)aout->output.p_sys;
/* FIXME: lock Semaphore */
amem_sem_wait(&header->sem);
/* FIXME: Do not assume we can fill entire buffer */
CopyMemory((PVOID)sys->header->samples, data->p_buffer, data->i_buffer);
sys->header->i_nb_samples = p_buffer->i_buffer / (sys->header->i_bitspersample / 8);
/* FIXME: unlock Semaphore */
amem_sem_post(&header->sem);
}
#else
static void amem_sem_init(vlc_sem_t *sem, unsigned value)
{
if (unlikely(sem_init(sem, 1 /* share between process*/, value)) == -1)
abort();
}
static int amem_shmem_create(aout_instance_t *aout, ssize_t len)
{
aout_sys_t *sys = (aout_sys_t *)aout->output.p_sys;
......@@ -310,7 +382,7 @@ static int amem_shmem_create(aout_instance_t *aout, ssize_t len)
/* initialize unamed semaphore */
amem_sem_init(&sys->header->sem, 1);
sys->b_own_semaphore = true;
msg_Dbg(aout, "shared memory created");
return VLC_SUCCESS;
}
......@@ -351,7 +423,8 @@ static void amem_shmem_close(aout_instance_t *aout)
{
aout_sys_t *sys = (aout_sys_t *)aout->output.p_sys;
vlc_sem_destroy(&sys->header->sem);
if (sys->b_own_semaphore)
amem_sem_destroy(&sys->header->sem);
/* Determine shared memory size */
if (sys->header != MAP_FAILED)
......@@ -375,7 +448,7 @@ static void amem_shmem_header(aout_instance_t *aout)
msg_Dbg(aout, "syncing shared header");
vlc_sem_wait(&header->sem);
amem_sem_wait(&header->sem);
/* Fill header struct */
header->i_format = aout->output.output.i_format;
......@@ -394,34 +467,14 @@ static void amem_shmem_header(aout_instance_t *aout)
memset(header->samples, 0, (aout->output.i_nb_samples *
aout->output.output.i_bitspersample));
if (vlc_sem_post(&header->sem) < 0)
msg_Err(aout, "failed unlocking semaphore (%m)");
amem_sem_post(&header->sem);
}
static int amem_shmem_write(aout_instance_t *aout, aout_buffer_t *p_buffer)
{
aout_sys_t *sys = (aout_sys_t *)aout->output.p_sys;
#if 0
int again = 0;
do
{
if (sem_trywait(&sys->header->sem) == 0)
break;
if (errno == EAGAIN)
again++;
if (again == 3)
{
msg_Err(aout, "Failed locking semaphore for writing");
return VLC_EGENERIC;
}
} while (again < 3);
#endif
vlc_sem_wait(&sys->header->sem);
amem_sem_wait(&sys->header->sem);
/* FIXME: do not assume we can fill the entire buffer */
assert(p_buffer->i_nb_samples <= sys->header->i_max_samples);
......@@ -430,8 +483,7 @@ static int amem_shmem_write(aout_instance_t *aout, aout_buffer_t *p_buffer)
sys->header->i_nb_samples = p_buffer->i_nb_samples;
sys->header->i_write_samples += p_buffer->i_nb_samples;
if (vlc_sem_post(&sys->header->sem) < 0)
msg_Err(aout, "failed unlocking semaphore (%m)");
amem_sem_post(&sys->header->sem);
return VLC_SUCCESS;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment