Commit cbcba506 authored by Jean-Paul Saman's avatar Jean-Paul Saman

audio_output/amem.c: Fold amem_shmem_create() into amem_shmem_open()

The functions amem_shmem_create() and amem_shmem_open() were almost identical.
parent fb619a0c
...@@ -235,94 +235,32 @@ static amem_buffer_t *amem_buffer_get(aout_instance_t *aout, unsigned int index) ...@@ -235,94 +235,32 @@ static amem_buffer_t *amem_buffer_get(aout_instance_t *aout, unsigned int index)
return buffer; return buffer;
} }
static int amem_shmem_create(aout_instance_t *aout, unsigned int i_buffers, ssize_t len) static int amem_shmem_open(aout_instance_t *aout, unsigned int i_buffers, ssize_t len, bool b_create)
{ {
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) #if defined(_WIN32_WINNT) || defined(WIN32)
TCHAR szName[] = TEXT(sys->psz_name); TCHAR szName[]=TEXT(sys->psz_name);
sys->handle = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
len, // maximum object size (low-order DWORD)
szName); // name of mapping object
if (sys->handle == NULL)
{
msg_Err(aout, "could not create file mapping object (%d).",
GetLastError());
return VLC_EGENERIC;
}
/* Map shared buffer */
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_ENGERIC;
}
#else
sys->shm_fd = shm_open(sys->psz_name, (O_CREAT | O_EXCL | O_RDWR), (S_IREAD | S_IWRITE));
if (sys->shm_fd < 0)
{
msg_Err(aout, "could not open shared memory object (%m)");
return VLC_EGENERIC;
}
/* set correct maximum size */
if (ftruncate(sys->shm_fd, len) < 0)
{
msg_Err(aout, "failed limiting shared memory size (%m)");
return VLC_EGENERIC;
}
/* mmap shared segment */ if (b_create)
sys->header = mmap(0, len, (PROT_READ | PROT_WRITE),
MAP_SHARED, sys->shm_fd, 0);
if (sys->header == MAP_FAILED)
{ {
msg_Err(aout, "could not mmap shared memory object (%m)"); sys->handle = CreateFileMapping(
return VLC_EGENERIC; INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
len, // maximum object size (low-order DWORD)
szName); // name of mapping object
} }
#endif else
/* setup pointers in shared structures */
sys->header->i_buffers = i_buffers;
for (unsigned int i = 0; i < sys->header->i_buffers; i++)
{ {
amem_buffer_t *buffer = amem_buffer_get(aout, i); sys->handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, szName);
if (buffer)
{
buffer->i_nb_samples = 0;
memset(buffer->samples, 0, (sys->header->i_max_samples * sys->header->i_bitspersample));
}
} }
/* initialize unamed semaphore */
amem_sem_init(&sys->header->sem, 1);
sys->b_own_semaphore = true;
msg_Dbg(aout, "shared memory created");
return VLC_SUCCESS;
}
static int amem_shmem_open(aout_instance_t *aout, unsigned int i_buffers, ssize_t len)
{
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) if (sys->handle == NULL)
{ {
msg_Err(aout, "could not open file mapping object (%d).", msg_Err(aout, "could not %s file mapping object (%d).",
GetLastError()); b_create ? "create": "open", GetLastError());
return VLC_EGENERIC; return VLC_EGENERIC;
} }
...@@ -330,26 +268,26 @@ static int amem_shmem_open(aout_instance_t *aout, unsigned int i_buffers, ssize_ ...@@ -330,26 +268,26 @@ static int amem_shmem_open(aout_instance_t *aout, unsigned int i_buffers, ssize_
FILE_MAP_ALL_ACCESS, 0, 0, len); FILE_MAP_ALL_ACCESS, 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).", GetLastError());
GetLastError());
CloseHandle(sys->handle); CloseHandle(sys->handle);
return VLC_EGENERIC; return VLC_EGENERIC;
} }
#else #else
sys->shm_fd = shm_open(sys->psz_name, O_RDWR, (S_IREAD | S_IWRITE)); int flags = b_create ? (O_CREAT | O_EXCL | O_RDWR) : O_RDWR;
sys->shm_fd = shm_open(sys->psz_name, flags, (S_IREAD | S_IWRITE));
if (sys->shm_fd < 0) if (sys->shm_fd < 0)
{ {
msg_Err(aout, "could not open shared memory object (%m)"); msg_Err(aout, "could not %s shared memory object (%m)",
b_create ? "create" : "open");
return VLC_EGENERIC; return VLC_EGENERIC;
} }
/* set correct maximum size */ /* set correct maximum size */
if (ftruncate(sys->shm_fd, len) < 0) if (ftruncate(sys->shm_fd, len) < 0)
{ {
msg_Err(aout, "failed limiting shared memory size (%m)"); msg_Err(aout, "failed setting shared memory size (%m)");
return VLC_EGENERIC; return VLC_EGENERIC;
} }
...@@ -369,10 +307,21 @@ static int amem_shmem_open(aout_instance_t *aout, unsigned int i_buffers, ssize_ ...@@ -369,10 +307,21 @@ static int amem_shmem_open(aout_instance_t *aout, unsigned int i_buffers, ssize_
{ {
amem_buffer_t *buffer = amem_buffer_get(aout, i); amem_buffer_t *buffer = amem_buffer_get(aout, i);
if (buffer) if (buffer)
{
buffer->i_nb_samples = 0; buffer->i_nb_samples = 0;
if (b_create)
memset(buffer->samples, 0, (sys->header->i_max_samples * sys->header->i_bitspersample));
}
}
/* initialize unamed semaphore */
if (b_create)
{
amem_sem_init(&sys->header->sem, 1);
sys->b_own_semaphore = true;
} }
msg_Dbg(aout, "shared memory opened"); msg_Dbg(aout, "shared memory %s", b_create ? "created" : "opened");
return VLC_SUCCESS; return VLC_SUCCESS;
} }
...@@ -692,7 +641,7 @@ static int SetupSharedMem(aout_instance_t *aout, unsigned int i_buffers) ...@@ -692,7 +641,7 @@ static int SetupSharedMem(aout_instance_t *aout, unsigned int i_buffers)
return VLC_EGENERIC; return VLC_EGENERIC;
} }
if (amem_shmem_open(aout, i_buffers, len) != VLC_SUCCESS) if (amem_shmem_open(aout, i_buffers, len, false) != VLC_SUCCESS)
return VLC_EGENERIC; return VLC_EGENERIC;
/* Fill header struct */ /* Fill header struct */
......
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