Commit 2778fd64 authored by Jean-Paul Saman's avatar Jean-Paul Saman

audio_output/amem.c: cleanup

Fold function for win32 and linux together
parent 195787d4
...@@ -106,11 +106,11 @@ typedef struct ...@@ -106,11 +106,11 @@ typedef struct
uint8_t i_channels; uint8_t i_channels;
#endif #endif
unsigned int i_read_samples; unsigned int i_read_samples; /* total samples read */
unsigned int i_write_samples; unsigned int i_write_samples; /* total samples written */
unsigned int i_max_samples; unsigned int i_max_samples; /* maximum samples in buffer */
unsigned int i_nb_samples; unsigned int i_nb_samples; /* number of samples in buffer */
void *samples; void *samples; /* buffer of samples */
} amem_shared_t; } amem_shared_t;
/* Private structure */ /* Private structure */
...@@ -216,11 +216,11 @@ static void amem_sem_destroy(vlc_sem_t *sem) ...@@ -216,11 +216,11 @@ static void amem_sem_destroy(vlc_sem_t *sem)
#endif #endif
} }
#if defined(_WIN32_WINNT) || defined(WIN32)
/* FIXME: not tested yet */
static int amem_shmem_create(aout_instance_t *aout, ssize_t len) static int amem_shmem_create(aout_instance_t *aout, ssize_t len)
{ {
aout_sys_t *sys = (aout_sys_t *)aout->output.p_sys; aout_sys_t *sys = (aout_sys_t *)aout->output.p_sys;
#if defined(_WIN32_WINNT) || defined(WIN32)
TCHAR szName[] = TEXT(sys->psz_name); TCHAR szName[] = TEXT(sys->psz_name);
sys->handle = CreateFileMapping( sys->handle = CreateFileMapping(
...@@ -230,7 +230,6 @@ static int amem_shmem_create(aout_instance_t *aout, ssize_t len) ...@@ -230,7 +230,6 @@ static int amem_shmem_create(aout_instance_t *aout, ssize_t len)
0, // maximum object size (high-order DWORD) 0, // maximum object size (high-order DWORD)
len, // maximum object size (low-order DWORD) len, // maximum object size (low-order DWORD)
szName); // name of mapping object szName); // name of mapping object
if (sys->handle == NULL) if (sys->handle == NULL)
{ {
msg_Err(aout, "could not create file mapping object (%d).", msg_Err(aout, "could not create file mapping object (%d).",
...@@ -239,12 +238,8 @@ static int amem_shmem_create(aout_instance_t *aout, ssize_t len) ...@@ -239,12 +238,8 @@ static int amem_shmem_create(aout_instance_t *aout, ssize_t len)
} }
/* Map shared buffer */ /* Map shared buffer */
sys->header = (amem_shared_t *) MapViewOfFile(sys->handle, // handle to map object sys->header = (amem_shared_t *) MapViewOfFile(sys->handle,
FILE_MAP_ALL_ACCESS, // read/write permission FILE_MAP_ALL_ACCESS, 0, 0, len);
0,
0,
len);
if (sys->header == NULL) if (sys->header == NULL)
{ {
msg_Err(aout, "could not map view of file (%d).", msg_Err(aout, "could not map view of file (%d).",
...@@ -253,109 +248,7 @@ static int amem_shmem_create(aout_instance_t *aout, ssize_t len) ...@@ -253,109 +248,7 @@ static int amem_shmem_create(aout_instance_t *aout, ssize_t len)
return VLC_ENGERIC; return VLC_ENGERIC;
} }
sys->header->samples = sys->header + sizeof(amem_shared_t);
amem_sem_init(&sys->header->sem, 1);
sys->b_own_semaphore = true;
return VLC_SUCCESS;
}
static int amem_shmem_open(aout_instance_t *aout, ssize_t len)
{
aout_sys_t *sys = (aout_sys_t *)aout->output.p_sys;
TCHAR szName[]=TEXT(sys->psz_name);
sys->handle = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
szName); // name of mapping object
if (sys->handle == NULL)
{
msg_Err(aout, "could not open file mapping object (%d).",
GetLastError());
return VLC_EGENERIC;
}
sys->header = (amem_shared_t *) MapViewOfFile(handle, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
len);
if (sys->header == NULL)
{
msg_Err(aout, "could not map view of file (%d).",
GetLastError());
CloseHandle(sys->handle);
return VLC_EGENERIC;
}
sys->header->samples = sys->header + sizeof(amem_shared_t);
return VLC_SUCCESS;
}
static void amem_shmem_close(aout_instance_t *aout)
{
aout_sys_t *sys = (aout_sys_t *)aout->output.p_sys;
if (sys->b_own_semaphore)
amem_sem_destroy(&sys->header->sem);
UnmapViewOfFile(sys->header);
CloseHandle(sys->handle);
}
static void amem_shmem_header(aout_instance_t *aout)
{
aout_sys_t *sys = (aout_sys_t *)aout->output.p_sys;
amem_sem_wait(&sys->header->sem);
/* Fill header struct */
uint16_t tag = 0;
fourcc_to_wf_tag(aout->output.output.i_format, &tag);
sys->header->wave.wFormatTag = tag;
sys->header->wave.nChannels = aout->output.output.i_channels;
sys->header->wave.nSamplesPerSec = aout->output.output.i_rate * aout->output.i_nb_samples;
//sys->header->wave.nAvgBytesPerSec = ;
sys->header->wave.nBlockAlign = aout->output.output.i_blockalign;
sys->header->wave.wBitsPerSample = aout->output.output.i_bitspersample;
sys->header->wave.cbSize = 0; /* No additional format metadata */
sys->header->i_max_samples = aout->output.i_nb_samples;
sys->header->i_nb_samples = 0;
msg_Info(aout, "samples %d, bits per sample %d",
aout->output.i_nb_samples, aout->output.output.i_bitspersample);
/* FIXME: do not assume entire buffer is available */
memset(sys->header->samples, 0, (aout->output.i_nb_samples *
aout->output.output.i_bitspersample));
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;
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);
amem_sem_post(&header->sem);
}
#else #else
static int amem_shmem_create(aout_instance_t *aout, ssize_t len)
{
aout_sys_t *sys = (aout_sys_t *)aout->output.p_sys;
sys->shm_fd = shm_open(sys->psz_name, (O_CREAT | O_EXCL | O_RDWR), (S_IREAD | S_IWRITE)); sys->shm_fd = shm_open(sys->psz_name, (O_CREAT | O_EXCL | O_RDWR), (S_IREAD | S_IWRITE));
if (sys->shm_fd < 0) if (sys->shm_fd < 0)
{ {
...@@ -378,6 +271,8 @@ static int amem_shmem_create(aout_instance_t *aout, ssize_t len) ...@@ -378,6 +271,8 @@ static int amem_shmem_create(aout_instance_t *aout, ssize_t len)
msg_Err(aout, "could not mmap shared memory object (%m)"); msg_Err(aout, "could not mmap shared memory object (%m)");
return VLC_EGENERIC; return VLC_EGENERIC;
} }
#endif
sys->header->samples = sys->header + sizeof(amem_shared_t); sys->header->samples = sys->header + sizeof(amem_shared_t);
/* initialize unamed semaphore */ /* initialize unamed semaphore */
...@@ -391,6 +286,30 @@ static int amem_shmem_open(aout_instance_t *aout, ssize_t len) ...@@ -391,6 +286,30 @@ static int amem_shmem_open(aout_instance_t *aout, ssize_t len)
{ {
aout_sys_t *sys = (aout_sys_t *)aout->output.p_sys; aout_sys_t *sys = (aout_sys_t *)aout->output.p_sys;
#if defined(_WIN32_WINNT) || defined(WIN32)
TCHAR szName[]=TEXT(sys->psz_name);
sys->handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, szName);
if (sys->handle == NULL)
{
msg_Err(aout, "could not open file mapping object (%d).",
GetLastError());
return VLC_EGENERIC;
}
sys->header = (amem_shared_t *) MapViewOfFile(sys->handle,
FILE_MAP_ALL_ACCESS, 0, 0, len);
if (sys->header == NULL)
{
msg_Err(aout, "could not map view of file (%d).",
GetLastError());
CloseHandle(sys->handle);
return VLC_EGENERIC;
}
#else
sys->shm_fd = shm_open(sys->psz_name, O_RDWR, (S_IREAD | S_IWRITE)); sys->shm_fd = shm_open(sys->psz_name, O_RDWR, (S_IREAD | S_IWRITE));
if (sys->shm_fd < 0) if (sys->shm_fd < 0)
{ {
...@@ -413,6 +332,7 @@ static int amem_shmem_open(aout_instance_t *aout, ssize_t len) ...@@ -413,6 +332,7 @@ static int amem_shmem_open(aout_instance_t *aout, ssize_t len)
msg_Err(aout, "could not mmap shared memory object (%m)"); msg_Err(aout, "could not mmap shared memory object (%m)");
return VLC_EGENERIC; return VLC_EGENERIC;
} }
#endif
sys->header->samples = sys->header + sizeof(amem_shared_t); sys->header->samples = sys->header + sizeof(amem_shared_t);
msg_Dbg(aout, "shared memory opened"); msg_Dbg(aout, "shared memory opened");
...@@ -426,6 +346,10 @@ static void amem_shmem_close(aout_instance_t *aout) ...@@ -426,6 +346,10 @@ static void amem_shmem_close(aout_instance_t *aout)
if (sys->b_own_semaphore) if (sys->b_own_semaphore)
amem_sem_destroy(&sys->header->sem); amem_sem_destroy(&sys->header->sem);
#if defined(_WIN32_WINNT) || defined(WIN32)
UnmapViewOfFile(sys->header);
CloseHandle(sys->handle);
#else
/* Determine shared memory size */ /* Determine shared memory size */
if (sys->header != MAP_FAILED) if (sys->header != MAP_FAILED)
{ {
...@@ -437,6 +361,7 @@ static void amem_shmem_close(aout_instance_t *aout) ...@@ -437,6 +361,7 @@ static void amem_shmem_close(aout_instance_t *aout)
} }
} }
close(sys->shm_fd); close(sys->shm_fd);
#endif
msg_Dbg(aout, "shared memory closed"); msg_Dbg(aout, "shared memory closed");
} }
...@@ -451,6 +376,17 @@ static void amem_shmem_header(aout_instance_t *aout) ...@@ -451,6 +376,17 @@ static void amem_shmem_header(aout_instance_t *aout)
amem_sem_wait(&header->sem); amem_sem_wait(&header->sem);
/* Fill header struct */ /* Fill header struct */
#if defined(_WIN32_WINNT) || defined(WIN32)
uint16_t tag = 0;
fourcc_to_wf_tag(aout->output.output.i_format, &tag);
sys->header->wave.wFormatTag = tag;
sys->header->wave.nChannels = aout->output.output.i_channels;
sys->header->wave.nSamplesPerSec = aout->output.output.i_rate * aout->output.i_nb_samples;
//sys->header->wave.nAvgBytesPerSec = ;
sys->header->wave.nBlockAlign = aout->output.output.i_blockalign;
sys->header->wave.wBitsPerSample = aout->output.output.i_bitspersample;
sys->header->wave.cbSize = 0; /* No additional format metadata */
#else
header->i_format = aout->output.output.i_format; header->i_format = aout->output.output.i_format;
header->i_bitspersample = aout->output.output.i_bitspersample; header->i_bitspersample = aout->output.output.i_bitspersample;
header->i_bytes_per_frame = aout->output.output.i_bytes_per_frame; header->i_bytes_per_frame = aout->output.output.i_bytes_per_frame;
...@@ -458,6 +394,7 @@ static void amem_shmem_header(aout_instance_t *aout) ...@@ -458,6 +394,7 @@ static void amem_shmem_header(aout_instance_t *aout)
header->i_frame_length = aout->output.output.i_frame_length; header->i_frame_length = aout->output.output.i_frame_length;
header->i_rate = aout->output.output.i_rate; header->i_rate = aout->output.output.i_rate;
header->i_blockalign = aout->output.output.i_blockalign; header->i_blockalign = aout->output.output.i_blockalign;
#endif
header->i_read_samples = 0; header->i_read_samples = 0;
header->i_write_samples = 0; header->i_write_samples = 0;
...@@ -479,7 +416,12 @@ static int amem_shmem_write(aout_instance_t *aout, aout_buffer_t *p_buffer) ...@@ -479,7 +416,12 @@ static int amem_shmem_write(aout_instance_t *aout, aout_buffer_t *p_buffer)
/* FIXME: do not assume we can fill the entire buffer */ /* FIXME: do not assume we can fill the entire buffer */
assert(p_buffer->i_nb_samples <= sys->header->i_max_samples); assert(p_buffer->i_nb_samples <= sys->header->i_max_samples);
#if defined(_WIN32_WINNT) || defined(WIN32)
CopyMemory((PVOID)sys->header->samples, data->p_buffer, data->i_buffer);
#else
memcpy(sys->header->samples, p_buffer->p_buffer, p_buffer->i_buffer); memcpy(sys->header->samples, p_buffer->p_buffer, p_buffer->i_buffer);
#endif
sys->header->i_nb_samples = p_buffer->i_nb_samples; sys->header->i_nb_samples = p_buffer->i_nb_samples;
sys->header->i_write_samples += p_buffer->i_nb_samples; sys->header->i_write_samples += p_buffer->i_nb_samples;
...@@ -488,8 +430,6 @@ static int amem_shmem_write(aout_instance_t *aout, aout_buffer_t *p_buffer) ...@@ -488,8 +430,6 @@ static int amem_shmem_write(aout_instance_t *aout, aout_buffer_t *p_buffer)
return VLC_SUCCESS; return VLC_SUCCESS;
} }
#endif
/***************************************************************************** /*****************************************************************************
* Module descriptor * Module descriptor
*****************************************************************************/ *****************************************************************************/
......
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