2003-03-18 20:37:42 +00:00
|
|
|
/**
|
2003-07-01 09:23:38 +00:00
|
|
|
* \file pcm/pcm_dsnoop.c
|
2003-03-18 20:37:42 +00:00
|
|
|
* \ingroup PCM_Plugins
|
|
|
|
|
* \brief PCM Capture Stream Snooping (dsnoop) Plugin Interface
|
2007-10-15 10:24:55 +02:00
|
|
|
* \author Jaroslav Kysela <perex@perex.cz>
|
2003-03-18 20:37:42 +00:00
|
|
|
* \date 2003
|
|
|
|
|
*/
|
|
|
|
|
/*
|
|
|
|
|
* PCM - Capture Stream Snooping
|
2007-10-15 10:24:55 +02:00
|
|
|
* Copyright (c) 2003 by Jaroslav Kysela <perex@perex.cz>
|
2003-03-18 20:37:42 +00:00
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* This library 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 library; if not, write to the Free Software
|
2017-11-14 14:29:26 +01:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2003-03-18 20:37:42 +00:00
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <fcntl.h>
|
2003-12-07 09:30:47 +00:00
|
|
|
#include <ctype.h>
|
2005-05-30 10:13:54 +00:00
|
|
|
#include <grp.h>
|
2003-03-18 20:37:42 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#include <sys/shm.h>
|
|
|
|
|
#include <sys/sem.h>
|
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#include "pcm_direct.h"
|
|
|
|
|
|
|
|
|
|
#ifndef PIC
|
|
|
|
|
/* entry for static linking */
|
|
|
|
|
const char *_snd_module_pcm_dsnoop = "";
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2010-10-29 17:36:41 +02:00
|
|
|
static int snoop_timestamp(snd_pcm_t *pcm)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
|
|
|
snd_pcm_uframes_t ptr1 = -2LL /* invalid value */, ptr2;
|
|
|
|
|
|
|
|
|
|
/* loop is required to sync hw.ptr with timestamp */
|
|
|
|
|
while (1) {
|
|
|
|
|
ptr2 = *dsnoop->spcm->hw.ptr;
|
|
|
|
|
if (ptr1 == ptr2)
|
|
|
|
|
break;
|
|
|
|
|
ptr1 = ptr2;
|
|
|
|
|
dsnoop->update_tstamp = snd_pcm_hw_fast_tstamp(dsnoop->spcm);
|
|
|
|
|
}
|
|
|
|
|
dsnoop->slave_hw_ptr = ptr1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
static void snoop_areas(snd_pcm_direct_t *dsnoop,
|
2003-03-18 20:37:42 +00:00
|
|
|
const snd_pcm_channel_area_t *src_areas,
|
|
|
|
|
const snd_pcm_channel_area_t *dst_areas,
|
|
|
|
|
snd_pcm_uframes_t src_ofs,
|
|
|
|
|
snd_pcm_uframes_t dst_ofs,
|
|
|
|
|
snd_pcm_uframes_t size)
|
|
|
|
|
{
|
2003-03-19 16:35:35 +00:00
|
|
|
unsigned int chn, schn, channels;
|
|
|
|
|
snd_pcm_format_t format;
|
|
|
|
|
|
|
|
|
|
channels = dsnoop->channels;
|
|
|
|
|
format = dsnoop->shmptr->s.format;
|
|
|
|
|
if (dsnoop->interleaved) {
|
|
|
|
|
unsigned int fbytes = snd_pcm_format_physical_width(format) / 8;
|
|
|
|
|
memcpy(((char *)dst_areas[0].addr) + (dst_ofs * channels * fbytes),
|
|
|
|
|
((char *)src_areas[0].addr) + (src_ofs * channels * fbytes),
|
|
|
|
|
size * channels * fbytes);
|
2003-03-18 20:37:42 +00:00
|
|
|
} else {
|
2003-03-19 16:35:35 +00:00
|
|
|
for (chn = 0; chn < channels; chn++) {
|
|
|
|
|
schn = dsnoop->bindings ? dsnoop->bindings[chn] : chn;
|
|
|
|
|
snd_pcm_area_copy(&dst_areas[chn], dst_ofs, &src_areas[schn], src_ofs, size, format);
|
|
|
|
|
}
|
2003-03-18 20:37:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* synchronize shm ring buffer with hardware
|
|
|
|
|
*/
|
2003-03-19 16:35:35 +00:00
|
|
|
static void snd_pcm_dsnoop_sync_area(snd_pcm_t *pcm, snd_pcm_uframes_t slave_hw_ptr, snd_pcm_uframes_t size)
|
2003-03-18 20:37:42 +00:00
|
|
|
{
|
2003-03-19 16:35:35 +00:00
|
|
|
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
|
|
|
snd_pcm_uframes_t hw_ptr = dsnoop->hw_ptr;
|
|
|
|
|
snd_pcm_uframes_t transfer;
|
2003-03-18 20:37:42 +00:00
|
|
|
const snd_pcm_channel_area_t *src_areas, *dst_areas;
|
|
|
|
|
|
|
|
|
|
/* add sample areas here */
|
2003-03-19 16:35:35 +00:00
|
|
|
dst_areas = snd_pcm_mmap_areas(pcm);
|
|
|
|
|
src_areas = snd_pcm_mmap_areas(dsnoop->spcm);
|
|
|
|
|
hw_ptr %= pcm->buffer_size;
|
2005-08-12 15:53:02 +00:00
|
|
|
slave_hw_ptr %= dsnoop->slave_buffer_size;
|
2003-03-18 20:37:42 +00:00
|
|
|
while (size > 0) {
|
2003-03-19 16:35:35 +00:00
|
|
|
transfer = hw_ptr + size > pcm->buffer_size ? pcm->buffer_size - hw_ptr : size;
|
2005-08-12 15:53:02 +00:00
|
|
|
transfer = slave_hw_ptr + transfer > dsnoop->slave_buffer_size ?
|
|
|
|
|
dsnoop->slave_buffer_size - slave_hw_ptr : transfer;
|
2003-03-18 20:37:42 +00:00
|
|
|
size -= transfer;
|
2003-03-19 16:35:35 +00:00
|
|
|
snoop_areas(dsnoop, src_areas, dst_areas, slave_hw_ptr, hw_ptr, transfer);
|
|
|
|
|
slave_hw_ptr += transfer;
|
2005-08-12 15:53:02 +00:00
|
|
|
slave_hw_ptr %= dsnoop->slave_buffer_size;
|
2003-03-19 16:35:35 +00:00
|
|
|
hw_ptr += transfer;
|
|
|
|
|
hw_ptr %= pcm->buffer_size;
|
2003-03-18 20:37:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* synchronize hardware pointer (hw_ptr) with ours
|
|
|
|
|
*/
|
2004-01-23 13:04:43 +00:00
|
|
|
static int snd_pcm_dsnoop_sync_ptr(snd_pcm_t *pcm)
|
2003-03-18 20:37:42 +00:00
|
|
|
{
|
2003-03-19 16:35:35 +00:00
|
|
|
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
2003-03-18 20:37:42 +00:00
|
|
|
snd_pcm_uframes_t slave_hw_ptr, old_slave_hw_ptr, avail;
|
|
|
|
|
snd_pcm_sframes_t diff;
|
2017-01-10 12:03:36 +05:30
|
|
|
int err;
|
|
|
|
|
|
2004-01-19 19:48:27 +00:00
|
|
|
switch (snd_pcm_state(dsnoop->spcm)) {
|
|
|
|
|
case SND_PCM_STATE_DISCONNECTED:
|
|
|
|
|
dsnoop->state = SNDRV_PCM_STATE_DISCONNECTED;
|
2006-03-03 14:08:58 +00:00
|
|
|
return -ENODEV;
|
2017-01-10 12:03:36 +05:30
|
|
|
case SND_PCM_STATE_XRUN:
|
|
|
|
|
if ((err = snd_pcm_direct_slave_recover(dsnoop)) < 0)
|
|
|
|
|
return err;
|
|
|
|
|
break;
|
2004-01-19 19:48:27 +00:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-01-10 12:03:36 +05:30
|
|
|
if (snd_pcm_direct_client_chk_xrun(dsnoop, pcm))
|
|
|
|
|
return -EPIPE;
|
2004-01-20 15:29:39 +00:00
|
|
|
if (dsnoop->slowptr)
|
|
|
|
|
snd_pcm_hwsync(dsnoop->spcm);
|
2003-03-19 16:35:35 +00:00
|
|
|
old_slave_hw_ptr = dsnoop->slave_hw_ptr;
|
2010-10-29 17:36:41 +02:00
|
|
|
snoop_timestamp(pcm);
|
|
|
|
|
slave_hw_ptr = dsnoop->slave_hw_ptr;
|
2003-03-18 20:37:42 +00:00
|
|
|
diff = slave_hw_ptr - old_slave_hw_ptr;
|
|
|
|
|
if (diff == 0) /* fast path */
|
|
|
|
|
return 0;
|
|
|
|
|
if (diff < 0) {
|
2005-08-12 15:53:02 +00:00
|
|
|
slave_hw_ptr += dsnoop->slave_boundary;
|
2003-03-18 20:37:42 +00:00
|
|
|
diff = slave_hw_ptr - old_slave_hw_ptr;
|
|
|
|
|
}
|
2003-03-19 16:35:35 +00:00
|
|
|
snd_pcm_dsnoop_sync_area(pcm, old_slave_hw_ptr, diff);
|
|
|
|
|
dsnoop->hw_ptr += diff;
|
|
|
|
|
dsnoop->hw_ptr %= pcm->boundary;
|
2003-03-18 20:37:42 +00:00
|
|
|
// printf("sync ptr diff = %li\n", diff);
|
|
|
|
|
if (pcm->stop_threshold >= pcm->boundary) /* don't care */
|
|
|
|
|
return 0;
|
2020-06-22 14:15:12 +01:00
|
|
|
if ((avail = snd_pcm_mmap_capture_avail(pcm)) >= pcm->stop_threshold) {
|
2014-07-10 14:37:49 +02:00
|
|
|
gettimestamp(&dsnoop->trigger_tstamp, pcm->tstamp_type);
|
2003-03-19 16:35:35 +00:00
|
|
|
dsnoop->state = SND_PCM_STATE_XRUN;
|
|
|
|
|
dsnoop->avail_max = avail;
|
2003-03-18 20:37:42 +00:00
|
|
|
return -EPIPE;
|
|
|
|
|
}
|
2003-03-19 16:35:35 +00:00
|
|
|
if (avail > dsnoop->avail_max)
|
|
|
|
|
dsnoop->avail_max = avail;
|
2004-01-23 13:04:43 +00:00
|
|
|
return 0;
|
2003-03-18 20:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* plugin implementation
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static int snd_pcm_dsnoop_status(snd_pcm_t *pcm, snd_pcm_status_t * status)
|
|
|
|
|
{
|
2003-03-19 16:35:35 +00:00
|
|
|
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
2004-01-19 19:48:27 +00:00
|
|
|
snd_pcm_state_t state;
|
2003-03-18 20:37:42 +00:00
|
|
|
|
2003-04-19 13:12:59 +00:00
|
|
|
switch(dsnoop->state) {
|
|
|
|
|
case SNDRV_PCM_STATE_DRAINING:
|
|
|
|
|
case SNDRV_PCM_STATE_RUNNING:
|
|
|
|
|
snd_pcm_dsnoop_sync_ptr(pcm);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2003-03-18 20:37:42 +00:00
|
|
|
memset(status, 0, sizeof(*status));
|
2015-06-02 16:47:50 +02:00
|
|
|
snd_pcm_status(dsnoop->spcm, status);
|
2004-01-19 19:48:27 +00:00
|
|
|
state = snd_pcm_state(dsnoop->spcm);
|
|
|
|
|
status->state = state == SND_PCM_STATE_RUNNING ? dsnoop->state : state;
|
2003-03-19 16:35:35 +00:00
|
|
|
status->trigger_tstamp = dsnoop->trigger_tstamp;
|
|
|
|
|
status->avail = snd_pcm_mmap_capture_avail(pcm);
|
|
|
|
|
status->avail_max = status->avail > dsnoop->avail_max ? status->avail : dsnoop->avail_max;
|
|
|
|
|
dsnoop->avail_max = 0;
|
2015-06-02 16:47:50 +02:00
|
|
|
status->delay = snd_pcm_mmap_capture_delay(pcm);
|
2003-03-18 20:37:42 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static snd_pcm_state_t snd_pcm_dsnoop_state(snd_pcm_t *pcm)
|
|
|
|
|
{
|
2003-03-19 16:35:35 +00:00
|
|
|
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
2017-01-10 12:03:54 +05:30
|
|
|
int err;
|
2015-10-30 17:13:50 +01:00
|
|
|
snd_pcm_state_t state;
|
|
|
|
|
state = snd_pcm_state(dsnoop->spcm);
|
|
|
|
|
switch (state) {
|
2004-01-19 19:48:27 +00:00
|
|
|
case SND_PCM_STATE_SUSPENDED:
|
|
|
|
|
case SND_PCM_STATE_DISCONNECTED:
|
2015-10-30 17:13:50 +01:00
|
|
|
dsnoop->state = state;
|
|
|
|
|
return state;
|
2017-01-10 12:03:54 +05:30
|
|
|
case SND_PCM_STATE_XRUN:
|
|
|
|
|
if ((err = snd_pcm_direct_slave_recover(dsnoop)) < 0)
|
|
|
|
|
return err;
|
|
|
|
|
break;
|
2004-01-19 19:48:27 +00:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-01-10 12:03:54 +05:30
|
|
|
snd_pcm_direct_client_chk_xrun(dsnoop, pcm);
|
2003-03-19 16:35:35 +00:00
|
|
|
return dsnoop->state;
|
2003-03-18 20:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int snd_pcm_dsnoop_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delayp)
|
|
|
|
|
{
|
2003-03-19 16:35:35 +00:00
|
|
|
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
2003-03-18 20:37:42 +00:00
|
|
|
int err;
|
|
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
switch(dsnoop->state) {
|
2003-03-18 20:37:42 +00:00
|
|
|
case SNDRV_PCM_STATE_DRAINING:
|
|
|
|
|
case SNDRV_PCM_STATE_RUNNING:
|
|
|
|
|
err = snd_pcm_dsnoop_sync_ptr(pcm);
|
|
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
2012-05-14 23:56:34 +08:00
|
|
|
/* Fall through */
|
2003-03-18 20:37:42 +00:00
|
|
|
case SNDRV_PCM_STATE_PREPARED:
|
|
|
|
|
case SNDRV_PCM_STATE_SUSPENDED:
|
2020-06-22 14:15:13 +01:00
|
|
|
*delayp = snd_pcm_mmap_capture_avail(pcm);
|
2003-03-18 20:37:42 +00:00
|
|
|
return 0;
|
|
|
|
|
case SNDRV_PCM_STATE_XRUN:
|
|
|
|
|
return -EPIPE;
|
2004-01-19 19:48:27 +00:00
|
|
|
case SNDRV_PCM_STATE_DISCONNECTED:
|
2006-03-03 14:08:58 +00:00
|
|
|
return -ENODEV;
|
2003-03-18 20:37:42 +00:00
|
|
|
default:
|
|
|
|
|
return -EBADFD;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int snd_pcm_dsnoop_hwsync(snd_pcm_t *pcm)
|
|
|
|
|
{
|
2003-03-19 16:35:35 +00:00
|
|
|
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
2003-03-18 20:37:42 +00:00
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
switch(dsnoop->state) {
|
2003-03-18 20:37:42 +00:00
|
|
|
case SNDRV_PCM_STATE_DRAINING:
|
|
|
|
|
case SNDRV_PCM_STATE_RUNNING:
|
|
|
|
|
return snd_pcm_dsnoop_sync_ptr(pcm);
|
|
|
|
|
case SNDRV_PCM_STATE_PREPARED:
|
|
|
|
|
case SNDRV_PCM_STATE_SUSPENDED:
|
|
|
|
|
return 0;
|
|
|
|
|
case SNDRV_PCM_STATE_XRUN:
|
|
|
|
|
return -EPIPE;
|
2004-01-19 19:48:27 +00:00
|
|
|
case SNDRV_PCM_STATE_DISCONNECTED:
|
2006-03-03 14:08:58 +00:00
|
|
|
return -ENODEV;
|
2003-03-18 20:37:42 +00:00
|
|
|
default:
|
|
|
|
|
return -EBADFD;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int snd_pcm_dsnoop_reset(snd_pcm_t *pcm)
|
|
|
|
|
{
|
2003-03-19 16:35:35 +00:00
|
|
|
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
|
|
|
dsnoop->hw_ptr %= pcm->period_size;
|
|
|
|
|
dsnoop->appl_ptr = dsnoop->hw_ptr;
|
2010-10-29 17:36:41 +02:00
|
|
|
dsnoop->slave_appl_ptr = dsnoop->slave_hw_ptr;
|
2019-05-15 11:56:34 +05:30
|
|
|
snd_pcm_direct_reset_slave_ptr(pcm, dsnoop);
|
2003-03-18 20:37:42 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int snd_pcm_dsnoop_start(snd_pcm_t *pcm)
|
|
|
|
|
{
|
2003-03-19 16:35:35 +00:00
|
|
|
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
2003-03-18 20:37:42 +00:00
|
|
|
int err;
|
2019-05-15 11:56:34 +05:30
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
if (dsnoop->state != SND_PCM_STATE_PREPARED)
|
2003-03-18 20:37:42 +00:00
|
|
|
return -EBADFD;
|
2005-05-17 09:28:29 +00:00
|
|
|
snd_pcm_hwsync(dsnoop->spcm);
|
2010-10-29 17:36:41 +02:00
|
|
|
snoop_timestamp(pcm);
|
|
|
|
|
dsnoop->slave_appl_ptr = dsnoop->slave_hw_ptr;
|
2019-05-15 11:56:34 +05:30
|
|
|
snd_pcm_direct_reset_slave_ptr(pcm, dsnoop);
|
2003-03-19 16:35:35 +00:00
|
|
|
err = snd_timer_start(dsnoop->timer);
|
2003-03-18 20:37:42 +00:00
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
2003-03-19 16:35:35 +00:00
|
|
|
dsnoop->state = SND_PCM_STATE_RUNNING;
|
2010-10-29 17:36:41 +02:00
|
|
|
dsnoop->trigger_tstamp = dsnoop->update_tstamp;
|
2003-03-18 20:37:42 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int snd_pcm_dsnoop_drop(snd_pcm_t *pcm)
|
|
|
|
|
{
|
2003-03-19 16:35:35 +00:00
|
|
|
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
|
|
|
if (dsnoop->state == SND_PCM_STATE_OPEN)
|
2003-03-18 20:37:42 +00:00
|
|
|
return -EBADFD;
|
2003-03-19 16:35:35 +00:00
|
|
|
dsnoop->state = SND_PCM_STATE_SETUP;
|
2010-03-03 10:58:53 +01:00
|
|
|
snd_timer_stop(dsnoop->timer);
|
2003-03-18 20:37:42 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
pcm: Add thread-safety to PCM API
Traditionally, many of ALSA library functions are supposed to be
thread-unsafe, and applications are required to take care of thread
safety by themselves. However, people never be careful enough, and
almost all applications fail in this regard.
This patch is an attempt to harden the thread safety in exported PCM
functions in a simplistic way: just wrap some of exported functions
with the pthread mutex of each PCM object. Not all API functions are
wrapped by the mutex since it doesn't make sense. Instead, the
patchset covers only the functions that may be likely called
concurrently. The supposedly thread-safe API functions are marked in
the document.
For achieving the feature, two new fields are added snd_pcm_t when the
option is enabled: thread_safe and lock. The former indicates that
the plugin is thread-safe that doesn't need this workaround and the
latter is the pthread mutex. Currently only hw plugin have
thread_safe=1. So, the most of real-time sensitive apps won't be
influenced by this patchset.
Although the patch covers most of PCM ops, a few snd_pcm_fast_ops are
left without the extra mutex locking: namely, the ones that may have
blocking behavior, i.e. resume, drain, readi, writei, readn and
writen. These are supposed to handle own locking in the callbacks.
Also, if anyone wants to disable this new thread-safe API feature, it
can be still turned off via --disable-thread-safety configure option.
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2016-06-30 15:32:40 +02:00
|
|
|
/* locked version */
|
|
|
|
|
static int __snd_pcm_dsnoop_drain(snd_pcm_t *pcm)
|
2003-03-18 20:37:42 +00:00
|
|
|
{
|
2003-03-19 16:35:35 +00:00
|
|
|
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
2003-03-18 20:37:42 +00:00
|
|
|
snd_pcm_uframes_t stop_threshold;
|
|
|
|
|
int err;
|
|
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
if (dsnoop->state == SND_PCM_STATE_OPEN)
|
2003-03-18 20:37:42 +00:00
|
|
|
return -EBADFD;
|
|
|
|
|
stop_threshold = pcm->stop_threshold;
|
|
|
|
|
if (pcm->stop_threshold > pcm->buffer_size)
|
|
|
|
|
pcm->stop_threshold = pcm->buffer_size;
|
2003-03-19 16:35:35 +00:00
|
|
|
while (dsnoop->state == SND_PCM_STATE_RUNNING) {
|
2003-03-18 20:37:42 +00:00
|
|
|
err = snd_pcm_dsnoop_sync_ptr(pcm);
|
|
|
|
|
if (err < 0)
|
|
|
|
|
break;
|
|
|
|
|
if (pcm->mode & SND_PCM_NONBLOCK)
|
|
|
|
|
return -EAGAIN;
|
pcm: Add thread-safety to PCM API
Traditionally, many of ALSA library functions are supposed to be
thread-unsafe, and applications are required to take care of thread
safety by themselves. However, people never be careful enough, and
almost all applications fail in this regard.
This patch is an attempt to harden the thread safety in exported PCM
functions in a simplistic way: just wrap some of exported functions
with the pthread mutex of each PCM object. Not all API functions are
wrapped by the mutex since it doesn't make sense. Instead, the
patchset covers only the functions that may be likely called
concurrently. The supposedly thread-safe API functions are marked in
the document.
For achieving the feature, two new fields are added snd_pcm_t when the
option is enabled: thread_safe and lock. The former indicates that
the plugin is thread-safe that doesn't need this workaround and the
latter is the pthread mutex. Currently only hw plugin have
thread_safe=1. So, the most of real-time sensitive apps won't be
influenced by this patchset.
Although the patch covers most of PCM ops, a few snd_pcm_fast_ops are
left without the extra mutex locking: namely, the ones that may have
blocking behavior, i.e. resume, drain, readi, writei, readn and
writen. These are supposed to handle own locking in the callbacks.
Also, if anyone wants to disable this new thread-safe API feature, it
can be still turned off via --disable-thread-safety configure option.
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2016-06-30 15:32:40 +02:00
|
|
|
__snd_pcm_wait_in_lock(pcm, -1);
|
2003-03-18 20:37:42 +00:00
|
|
|
}
|
|
|
|
|
pcm->stop_threshold = stop_threshold;
|
|
|
|
|
return snd_pcm_dsnoop_drop(pcm);
|
|
|
|
|
}
|
|
|
|
|
|
pcm: Add thread-safety to PCM API
Traditionally, many of ALSA library functions are supposed to be
thread-unsafe, and applications are required to take care of thread
safety by themselves. However, people never be careful enough, and
almost all applications fail in this regard.
This patch is an attempt to harden the thread safety in exported PCM
functions in a simplistic way: just wrap some of exported functions
with the pthread mutex of each PCM object. Not all API functions are
wrapped by the mutex since it doesn't make sense. Instead, the
patchset covers only the functions that may be likely called
concurrently. The supposedly thread-safe API functions are marked in
the document.
For achieving the feature, two new fields are added snd_pcm_t when the
option is enabled: thread_safe and lock. The former indicates that
the plugin is thread-safe that doesn't need this workaround and the
latter is the pthread mutex. Currently only hw plugin have
thread_safe=1. So, the most of real-time sensitive apps won't be
influenced by this patchset.
Although the patch covers most of PCM ops, a few snd_pcm_fast_ops are
left without the extra mutex locking: namely, the ones that may have
blocking behavior, i.e. resume, drain, readi, writei, readn and
writen. These are supposed to handle own locking in the callbacks.
Also, if anyone wants to disable this new thread-safe API feature, it
can be still turned off via --disable-thread-safety configure option.
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2016-06-30 15:32:40 +02:00
|
|
|
static int snd_pcm_dsnoop_drain(snd_pcm_t *pcm)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
snd_pcm_lock(pcm);
|
|
|
|
|
err = __snd_pcm_dsnoop_drain(pcm);
|
|
|
|
|
snd_pcm_unlock(pcm);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2005-05-17 09:28:29 +00:00
|
|
|
static int snd_pcm_dsnoop_pause(snd_pcm_t *pcm ATTRIBUTE_UNUSED, int enable ATTRIBUTE_UNUSED)
|
2003-03-18 20:37:42 +00:00
|
|
|
{
|
2005-05-17 09:28:29 +00:00
|
|
|
return -EIO;
|
2003-03-18 20:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
2008-04-21 12:46:50 +02:00
|
|
|
static snd_pcm_sframes_t snd_pcm_dsnoop_rewindable(snd_pcm_t *pcm)
|
|
|
|
|
{
|
2014-09-14 00:30:19 +06:00
|
|
|
return snd_pcm_mmap_capture_hw_avail(pcm);
|
2008-04-21 12:46:50 +02:00
|
|
|
}
|
|
|
|
|
|
2003-03-18 20:37:42 +00:00
|
|
|
static snd_pcm_sframes_t snd_pcm_dsnoop_rewind(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
|
|
|
|
|
{
|
2008-04-21 12:46:50 +02:00
|
|
|
snd_pcm_sframes_t avail;
|
|
|
|
|
|
2014-09-14 00:30:14 +06:00
|
|
|
avail = snd_pcm_dsnoop_rewindable(pcm);
|
2008-04-21 12:46:50 +02:00
|
|
|
if (frames > (snd_pcm_uframes_t)avail)
|
|
|
|
|
frames = avail;
|
2003-03-18 20:37:42 +00:00
|
|
|
snd_pcm_mmap_appl_backward(pcm, frames);
|
|
|
|
|
return frames;
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-21 12:46:50 +02:00
|
|
|
static snd_pcm_sframes_t snd_pcm_dsnoop_forwardable(snd_pcm_t *pcm)
|
|
|
|
|
{
|
2014-09-14 00:30:19 +06:00
|
|
|
return snd_pcm_mmap_capture_avail(pcm);
|
2008-04-21 12:46:50 +02:00
|
|
|
}
|
|
|
|
|
|
2003-03-18 20:37:42 +00:00
|
|
|
static snd_pcm_sframes_t snd_pcm_dsnoop_forward(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_sframes_t avail;
|
|
|
|
|
|
2014-09-14 00:30:14 +06:00
|
|
|
avail = snd_pcm_dsnoop_forwardable(pcm);
|
2003-03-18 20:37:42 +00:00
|
|
|
if (frames > (snd_pcm_uframes_t)avail)
|
|
|
|
|
frames = avail;
|
|
|
|
|
snd_pcm_mmap_appl_forward(pcm, frames);
|
|
|
|
|
return frames;
|
|
|
|
|
}
|
|
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
static snd_pcm_sframes_t snd_pcm_dsnoop_writei(snd_pcm_t *pcm ATTRIBUTE_UNUSED, const void *buffer ATTRIBUTE_UNUSED, snd_pcm_uframes_t size ATTRIBUTE_UNUSED)
|
2003-03-18 20:37:42 +00:00
|
|
|
{
|
|
|
|
|
return -ENODEV;
|
|
|
|
|
}
|
|
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
static snd_pcm_sframes_t snd_pcm_dsnoop_writen(snd_pcm_t *pcm ATTRIBUTE_UNUSED, void **bufs ATTRIBUTE_UNUSED, snd_pcm_uframes_t size ATTRIBUTE_UNUSED)
|
2003-03-18 20:37:42 +00:00
|
|
|
{
|
|
|
|
|
return -ENODEV;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int snd_pcm_dsnoop_close(snd_pcm_t *pcm)
|
|
|
|
|
{
|
2003-03-19 16:35:35 +00:00
|
|
|
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
|
|
|
|
|
|
|
|
if (dsnoop->timer)
|
|
|
|
|
snd_timer_close(dsnoop->timer);
|
|
|
|
|
snd_pcm_direct_semaphore_down(dsnoop, DIRECT_IPC_SEM_CLIENT);
|
|
|
|
|
snd_pcm_close(dsnoop->spcm);
|
|
|
|
|
if (dsnoop->server)
|
|
|
|
|
snd_pcm_direct_server_discard(dsnoop);
|
|
|
|
|
if (dsnoop->client)
|
|
|
|
|
snd_pcm_direct_client_discard(dsnoop);
|
2013-04-09 14:31:20 +02:00
|
|
|
if (snd_pcm_direct_shm_discard(dsnoop)) {
|
|
|
|
|
if (snd_pcm_direct_semaphore_discard(dsnoop))
|
|
|
|
|
snd_pcm_direct_semaphore_final(dsnoop, DIRECT_IPC_SEM_CLIENT);
|
|
|
|
|
} else
|
|
|
|
|
snd_pcm_direct_semaphore_final(dsnoop, DIRECT_IPC_SEM_CLIENT);
|
2006-02-27 09:58:32 +00:00
|
|
|
free(dsnoop->bindings);
|
2003-03-18 20:37:42 +00:00
|
|
|
pcm->private_data = NULL;
|
2003-03-19 16:35:35 +00:00
|
|
|
free(dsnoop);
|
2003-03-18 20:37:42 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static snd_pcm_sframes_t snd_pcm_dsnoop_mmap_commit(snd_pcm_t *pcm,
|
2003-03-19 16:35:35 +00:00
|
|
|
snd_pcm_uframes_t offset ATTRIBUTE_UNUSED,
|
|
|
|
|
snd_pcm_uframes_t size)
|
2003-03-18 20:37:42 +00:00
|
|
|
{
|
2003-03-19 16:35:35 +00:00
|
|
|
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
2003-03-18 20:37:42 +00:00
|
|
|
int err;
|
|
|
|
|
|
2005-03-15 11:39:51 +00:00
|
|
|
switch (snd_pcm_state(dsnoop->spcm)) {
|
|
|
|
|
case SND_PCM_STATE_XRUN:
|
2017-01-10 12:03:36 +05:30
|
|
|
if ((err = snd_pcm_direct_slave_recover(dsnoop)) < 0)
|
|
|
|
|
return err;
|
|
|
|
|
break;
|
2005-03-15 11:39:51 +00:00
|
|
|
case SND_PCM_STATE_SUSPENDED:
|
|
|
|
|
return -ESTRPIPE;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-01-10 12:03:36 +05:30
|
|
|
if (snd_pcm_direct_client_chk_xrun(dsnoop, pcm))
|
|
|
|
|
return -EPIPE;
|
2003-03-19 16:35:35 +00:00
|
|
|
if (dsnoop->state == SND_PCM_STATE_RUNNING) {
|
2003-03-18 20:37:42 +00:00
|
|
|
err = snd_pcm_dsnoop_sync_ptr(pcm);
|
|
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
|
|
|
|
}
|
2003-03-19 16:35:35 +00:00
|
|
|
snd_pcm_mmap_appl_forward(pcm, size);
|
2005-05-23 08:58:04 +00:00
|
|
|
/* clear timer queue to avoid a bogus return from poll */
|
|
|
|
|
if (snd_pcm_mmap_capture_avail(pcm) < pcm->avail_min)
|
|
|
|
|
snd_pcm_direct_clear_timer_queue(dsnoop);
|
2003-03-18 20:37:42 +00:00
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-20 19:33:53 +00:00
|
|
|
static snd_pcm_sframes_t snd_pcm_dsnoop_avail_update(snd_pcm_t *pcm)
|
2003-03-18 20:37:42 +00:00
|
|
|
{
|
2003-03-19 16:35:35 +00:00
|
|
|
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
2003-03-18 20:37:42 +00:00
|
|
|
int err;
|
|
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
if (dsnoop->state == SND_PCM_STATE_RUNNING) {
|
2003-03-18 20:37:42 +00:00
|
|
|
err = snd_pcm_dsnoop_sync_ptr(pcm);
|
|
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
|
|
|
|
}
|
2017-01-10 12:04:09 +05:30
|
|
|
if (dsnoop->state == SND_PCM_STATE_XRUN)
|
|
|
|
|
return -EPIPE;
|
|
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
return snd_pcm_mmap_capture_avail(pcm);
|
2003-03-18 20:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
2008-01-09 13:50:45 +01:00
|
|
|
static int snd_pcm_dsnoop_htimestamp(snd_pcm_t *pcm,
|
|
|
|
|
snd_pcm_uframes_t *avail,
|
|
|
|
|
snd_htimestamp_t *tstamp)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
|
|
|
snd_pcm_uframes_t avail1;
|
|
|
|
|
int ok = 0;
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
if (dsnoop->state == SND_PCM_STATE_RUNNING ||
|
|
|
|
|
dsnoop->state == SND_PCM_STATE_DRAINING)
|
|
|
|
|
snd_pcm_dsnoop_sync_ptr(pcm);
|
|
|
|
|
avail1 = snd_pcm_mmap_capture_avail(pcm);
|
|
|
|
|
if (ok && *avail == avail1)
|
|
|
|
|
break;
|
|
|
|
|
*avail = avail1;
|
2010-10-29 17:36:41 +02:00
|
|
|
*tstamp = snd_pcm_hw_fast_tstamp(dsnoop->spcm);
|
2012-09-21 17:59:42 +02:00
|
|
|
ok = 1;
|
2008-01-09 13:50:45 +01:00
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2003-03-18 20:37:42 +00:00
|
|
|
static void snd_pcm_dsnoop_dump(snd_pcm_t *pcm, snd_output_t *out)
|
|
|
|
|
{
|
2003-03-19 16:35:35 +00:00
|
|
|
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
2003-03-18 20:37:42 +00:00
|
|
|
|
2005-11-01 13:35:52 +00:00
|
|
|
snd_output_printf(out, "Direct Snoop PCM\n");
|
2003-03-18 20:37:42 +00:00
|
|
|
if (pcm->setup) {
|
2005-12-11 08:24:13 +00:00
|
|
|
snd_output_printf(out, "Its setup is:\n");
|
2003-03-18 20:37:42 +00:00
|
|
|
snd_pcm_dump_setup(pcm, out);
|
|
|
|
|
}
|
2003-03-19 16:35:35 +00:00
|
|
|
if (dsnoop->spcm)
|
|
|
|
|
snd_pcm_dump(dsnoop->spcm, out);
|
2003-03-18 20:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
2008-11-21 20:38:09 +01:00
|
|
|
static const snd_pcm_ops_t snd_pcm_dsnoop_ops = {
|
2003-07-25 17:02:00 +00:00
|
|
|
.close = snd_pcm_dsnoop_close,
|
2003-09-02 10:32:33 +00:00
|
|
|
.info = snd_pcm_direct_info,
|
|
|
|
|
.hw_refine = snd_pcm_direct_hw_refine,
|
|
|
|
|
.hw_params = snd_pcm_direct_hw_params,
|
|
|
|
|
.hw_free = snd_pcm_direct_hw_free,
|
|
|
|
|
.sw_params = snd_pcm_direct_sw_params,
|
|
|
|
|
.channel_info = snd_pcm_direct_channel_info,
|
2003-07-25 17:02:00 +00:00
|
|
|
.dump = snd_pcm_dsnoop_dump,
|
2003-09-02 10:32:33 +00:00
|
|
|
.nonblock = snd_pcm_direct_nonblock,
|
|
|
|
|
.async = snd_pcm_direct_async,
|
|
|
|
|
.mmap = snd_pcm_direct_mmap,
|
|
|
|
|
.munmap = snd_pcm_direct_munmap,
|
2012-07-25 15:05:15 +02:00
|
|
|
.query_chmaps = snd_pcm_direct_query_chmaps,
|
|
|
|
|
.get_chmap = snd_pcm_direct_get_chmap,
|
|
|
|
|
.set_chmap = snd_pcm_direct_set_chmap,
|
2003-03-18 20:37:42 +00:00
|
|
|
};
|
|
|
|
|
|
2008-11-21 20:38:09 +01:00
|
|
|
static const snd_pcm_fast_ops_t snd_pcm_dsnoop_fast_ops = {
|
2003-07-25 17:02:00 +00:00
|
|
|
.status = snd_pcm_dsnoop_status,
|
|
|
|
|
.state = snd_pcm_dsnoop_state,
|
|
|
|
|
.hwsync = snd_pcm_dsnoop_hwsync,
|
|
|
|
|
.delay = snd_pcm_dsnoop_delay,
|
2015-10-30 17:13:50 +01:00
|
|
|
.prepare = snd_pcm_direct_prepare,
|
2003-07-25 17:02:00 +00:00
|
|
|
.reset = snd_pcm_dsnoop_reset,
|
|
|
|
|
.start = snd_pcm_dsnoop_start,
|
|
|
|
|
.drop = snd_pcm_dsnoop_drop,
|
|
|
|
|
.drain = snd_pcm_dsnoop_drain,
|
|
|
|
|
.pause = snd_pcm_dsnoop_pause,
|
2008-04-21 12:46:50 +02:00
|
|
|
.rewindable = snd_pcm_dsnoop_rewindable,
|
2003-07-25 17:02:00 +00:00
|
|
|
.rewind = snd_pcm_dsnoop_rewind,
|
2008-04-21 12:46:50 +02:00
|
|
|
.forwardable = snd_pcm_dsnoop_forwardable,
|
2003-07-25 17:02:00 +00:00
|
|
|
.forward = snd_pcm_dsnoop_forward,
|
2005-08-16 12:19:15 +00:00
|
|
|
.resume = snd_pcm_direct_resume,
|
2005-01-20 15:07:51 +00:00
|
|
|
.link = NULL,
|
2007-03-13 02:52:33 +01:00
|
|
|
.link_slaves = NULL,
|
2005-01-20 15:07:51 +00:00
|
|
|
.unlink = NULL,
|
2003-07-25 17:02:00 +00:00
|
|
|
.writei = snd_pcm_dsnoop_writei,
|
|
|
|
|
.writen = snd_pcm_dsnoop_writen,
|
|
|
|
|
.readi = snd_pcm_mmap_readi,
|
|
|
|
|
.readn = snd_pcm_mmap_readn,
|
|
|
|
|
.avail_update = snd_pcm_dsnoop_avail_update,
|
|
|
|
|
.mmap_commit = snd_pcm_dsnoop_mmap_commit,
|
2008-01-09 13:50:45 +01:00
|
|
|
.htimestamp = snd_pcm_dsnoop_htimestamp,
|
2017-01-10 12:03:54 +05:30
|
|
|
.poll_descriptors = snd_pcm_direct_poll_descriptors,
|
2005-05-23 09:03:16 +00:00
|
|
|
.poll_descriptors_count = NULL,
|
|
|
|
|
.poll_revents = snd_pcm_direct_poll_revents,
|
2003-03-18 20:37:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2003-03-19 16:35:35 +00:00
|
|
|
* \brief Creates a new dsnoop PCM
|
2003-03-18 20:37:42 +00:00
|
|
|
* \param pcmp Returns created PCM handle
|
|
|
|
|
* \param name Name of PCM
|
2006-03-21 10:27:35 +00:00
|
|
|
* \param opts Direct PCM configurations
|
2003-03-18 20:37:42 +00:00
|
|
|
* \param params Parameters for slave
|
|
|
|
|
* \param root Configuration root
|
|
|
|
|
* \param sconf Slave configuration
|
|
|
|
|
* \param stream PCM Direction (stream)
|
|
|
|
|
* \param mode PCM Mode
|
|
|
|
|
* \retval zero on success otherwise a negative error code
|
|
|
|
|
* \warning Using of this function might be dangerous in the sense
|
|
|
|
|
* of compatibility reasons. The prototype might be freely
|
|
|
|
|
* changed in future.
|
|
|
|
|
*/
|
|
|
|
|
int snd_pcm_dsnoop_open(snd_pcm_t **pcmp, const char *name,
|
2006-03-21 10:27:35 +00:00
|
|
|
struct snd_pcm_direct_open_conf *opts,
|
2003-12-07 09:30:47 +00:00
|
|
|
struct slave_params *params,
|
|
|
|
|
snd_config_t *root, snd_config_t *sconf,
|
|
|
|
|
snd_pcm_stream_t stream, int mode)
|
2003-03-18 20:37:42 +00:00
|
|
|
{
|
|
|
|
|
snd_pcm_t *pcm = NULL, *spcm = NULL;
|
2003-03-19 16:35:35 +00:00
|
|
|
snd_pcm_direct_t *dsnoop = NULL;
|
2003-07-15 19:15:10 +00:00
|
|
|
int ret, first_instance, fail_sem_loop = 10;
|
2003-03-18 20:37:42 +00:00
|
|
|
|
|
|
|
|
assert(pcmp);
|
|
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
if (stream != SND_PCM_STREAM_CAPTURE) {
|
|
|
|
|
SNDERR("The dsnoop plugin supports only capture stream");
|
2003-03-18 20:37:42 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
dsnoop = calloc(1, sizeof(snd_pcm_direct_t));
|
|
|
|
|
if (!dsnoop) {
|
2003-03-18 20:37:42 +00:00
|
|
|
ret = -ENOMEM;
|
2005-05-23 08:56:48 +00:00
|
|
|
goto _err_nosem;
|
2003-03-18 20:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
2006-04-26 18:30:44 +02:00
|
|
|
ret = snd_pcm_direct_parse_bindings(dsnoop, params, opts->bindings);
|
2003-03-18 20:37:42 +00:00
|
|
|
if (ret < 0)
|
2005-05-23 08:56:48 +00:00
|
|
|
goto _err_nosem;
|
2003-03-18 20:37:42 +00:00
|
|
|
|
2006-03-21 10:27:35 +00:00
|
|
|
dsnoop->ipc_key = opts->ipc_key;
|
|
|
|
|
dsnoop->ipc_perm = opts->ipc_perm;
|
|
|
|
|
dsnoop->ipc_gid = opts->ipc_gid;
|
2020-04-15 00:44:39 +00:00
|
|
|
dsnoop->tstamp_type = opts->tstamp_type;
|
2003-03-19 16:35:35 +00:00
|
|
|
dsnoop->semid = -1;
|
|
|
|
|
dsnoop->shmid = -1;
|
2003-03-18 20:37:42 +00:00
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
ret = snd_pcm_new(&pcm, dsnoop->type = SND_PCM_TYPE_DSNOOP, name, stream, mode);
|
2003-03-18 20:37:42 +00:00
|
|
|
if (ret < 0)
|
2005-05-23 08:56:48 +00:00
|
|
|
goto _err_nosem;
|
2003-03-18 20:37:42 +00:00
|
|
|
|
2003-07-15 19:15:10 +00:00
|
|
|
while (1) {
|
|
|
|
|
ret = snd_pcm_direct_semaphore_create_or_connect(dsnoop);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
SNDERR("unable to create IPC semaphore");
|
2005-05-23 08:56:48 +00:00
|
|
|
goto _err_nosem;
|
2003-07-15 19:15:10 +00:00
|
|
|
}
|
2003-03-18 20:37:42 +00:00
|
|
|
|
2003-07-15 19:15:10 +00:00
|
|
|
ret = snd_pcm_direct_semaphore_down(dsnoop, DIRECT_IPC_SEM_CLIENT);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
snd_pcm_direct_semaphore_discard(dsnoop);
|
|
|
|
|
if (--fail_sem_loop <= 0)
|
2005-05-23 08:56:48 +00:00
|
|
|
goto _err_nosem;
|
2003-07-15 19:15:10 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2003-03-18 20:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
first_instance = ret = snd_pcm_direct_shm_create_or_connect(dsnoop);
|
2003-03-18 20:37:42 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
|
SNDERR("unable to create IPC shm instance");
|
|
|
|
|
goto _err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pcm->ops = &snd_pcm_dsnoop_ops;
|
|
|
|
|
pcm->fast_ops = &snd_pcm_dsnoop_fast_ops;
|
2003-03-19 16:35:35 +00:00
|
|
|
pcm->private_data = dsnoop;
|
|
|
|
|
dsnoop->state = SND_PCM_STATE_OPEN;
|
2006-03-21 10:27:35 +00:00
|
|
|
dsnoop->slowptr = opts->slowptr;
|
2006-04-06 18:49:11 +02:00
|
|
|
dsnoop->max_periods = opts->max_periods;
|
2016-12-30 11:56:15 +05:30
|
|
|
dsnoop->var_periodsize = opts->var_periodsize;
|
2004-01-23 13:04:43 +00:00
|
|
|
dsnoop->sync_ptr = snd_pcm_dsnoop_sync_ptr;
|
2019-05-15 11:56:34 +05:30
|
|
|
dsnoop->hw_ptr_alignment = opts->hw_ptr_alignment;
|
2003-03-18 20:37:42 +00:00
|
|
|
|
2016-05-28 10:37:26 +02:00
|
|
|
retry:
|
2003-03-18 20:37:42 +00:00
|
|
|
if (first_instance) {
|
2006-01-30 14:41:51 +00:00
|
|
|
/* recursion is already checked in
|
|
|
|
|
snd_pcm_direct_get_slave_ipc_offset() */
|
|
|
|
|
ret = snd_pcm_open_slave(&spcm, root, sconf, stream,
|
|
|
|
|
mode | SND_PCM_NONBLOCK, NULL);
|
2003-03-18 20:37:42 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
|
SNDERR("unable to open slave");
|
|
|
|
|
goto _err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (snd_pcm_type(spcm) != SND_PCM_TYPE_HW) {
|
2003-03-19 16:35:35 +00:00
|
|
|
SNDERR("dsnoop plugin can be only connected to hw plugin");
|
2003-03-18 20:37:42 +00:00
|
|
|
goto _err;
|
|
|
|
|
}
|
|
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
ret = snd_pcm_direct_initialize_slave(dsnoop, spcm, params);
|
2003-03-18 20:37:42 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
|
SNDERR("unable to initialize slave");
|
|
|
|
|
goto _err;
|
|
|
|
|
}
|
|
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
dsnoop->spcm = spcm;
|
2003-03-18 20:37:42 +00:00
|
|
|
|
2006-04-28 15:55:32 +02:00
|
|
|
if (dsnoop->shmptr->use_server) {
|
|
|
|
|
ret = snd_pcm_direct_server_create(dsnoop);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
SNDERR("unable to create server");
|
|
|
|
|
goto _err;
|
|
|
|
|
}
|
2003-03-18 20:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
dsnoop->shmptr->type = spcm->type;
|
2003-03-18 20:37:42 +00:00
|
|
|
} else {
|
2006-04-28 15:55:32 +02:00
|
|
|
if (dsnoop->shmptr->use_server) {
|
|
|
|
|
/* up semaphore to avoid deadlock */
|
|
|
|
|
snd_pcm_direct_semaphore_up(dsnoop, DIRECT_IPC_SEM_CLIENT);
|
|
|
|
|
ret = snd_pcm_direct_client_connect(dsnoop);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
SNDERR("unable to connect client");
|
|
|
|
|
goto _err_nosem;
|
|
|
|
|
}
|
2003-03-18 20:37:42 +00:00
|
|
|
|
2006-04-28 15:55:32 +02:00
|
|
|
snd_pcm_direct_semaphore_down(dsnoop, DIRECT_IPC_SEM_CLIENT);
|
|
|
|
|
|
|
|
|
|
ret = snd_pcm_direct_open_secondary_client(&spcm, dsnoop, "dsnoop_client");
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
goto _err;
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
ret = snd_pcm_open_slave(&spcm, root, sconf, stream,
|
|
|
|
|
mode | SND_PCM_NONBLOCK |
|
|
|
|
|
SND_PCM_APPEND,
|
|
|
|
|
NULL);
|
|
|
|
|
if (ret < 0) {
|
2016-04-14 17:33:03 +02:00
|
|
|
/* all other streams have been closed;
|
|
|
|
|
* retry as the first instance
|
|
|
|
|
*/
|
|
|
|
|
if (ret == -EBADFD) {
|
|
|
|
|
first_instance = 1;
|
|
|
|
|
goto retry;
|
|
|
|
|
}
|
2006-04-28 15:55:32 +02:00
|
|
|
SNDERR("unable to open slave");
|
|
|
|
|
goto _err;
|
|
|
|
|
}
|
|
|
|
|
if (snd_pcm_type(spcm) != SND_PCM_TYPE_HW) {
|
|
|
|
|
SNDERR("dsnoop plugin can be only connected to hw plugin");
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
|
goto _err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = snd_pcm_direct_initialize_secondary_slave(dsnoop, spcm, params);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
SNDERR("unable to initialize slave");
|
|
|
|
|
goto _err;
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-08-12 15:53:02 +00:00
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
dsnoop->spcm = spcm;
|
2003-03-18 20:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
ret = snd_pcm_direct_initialize_poll_fd(dsnoop);
|
2003-03-18 20:37:42 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
|
SNDERR("unable to initialize poll_fd");
|
|
|
|
|
goto _err;
|
|
|
|
|
}
|
|
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
pcm->poll_fd = dsnoop->poll_fd;
|
2003-03-18 20:37:42 +00:00
|
|
|
pcm->poll_events = POLLIN; /* it's different than other plugins */
|
2014-07-10 14:37:49 +02:00
|
|
|
pcm->tstamp_type = spcm->tstamp_type;
|
2003-03-18 20:37:42 +00:00
|
|
|
pcm->mmap_rw = 1;
|
2003-03-19 16:35:35 +00:00
|
|
|
snd_pcm_set_hw_ptr(pcm, &dsnoop->hw_ptr, -1, 0);
|
|
|
|
|
snd_pcm_set_appl_ptr(pcm, &dsnoop->appl_ptr, -1, 0);
|
2003-03-18 20:37:42 +00:00
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
if (dsnoop->channels == UINT_MAX)
|
|
|
|
|
dsnoop->channels = dsnoop->shmptr->s.channels;
|
2003-03-18 20:37:42 +00:00
|
|
|
|
2003-03-19 16:35:35 +00:00
|
|
|
snd_pcm_direct_semaphore_up(dsnoop, DIRECT_IPC_SEM_CLIENT);
|
2003-03-18 20:37:42 +00:00
|
|
|
|
|
|
|
|
*pcmp = pcm;
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
_err:
|
2005-05-23 08:56:48 +00:00
|
|
|
if (dsnoop->timer)
|
|
|
|
|
snd_timer_close(dsnoop->timer);
|
|
|
|
|
if (dsnoop->server)
|
|
|
|
|
snd_pcm_direct_server_discard(dsnoop);
|
|
|
|
|
if (dsnoop->client)
|
|
|
|
|
snd_pcm_direct_client_discard(dsnoop);
|
|
|
|
|
if (spcm)
|
|
|
|
|
snd_pcm_close(spcm);
|
2016-11-25 15:43:40 +05:30
|
|
|
if ((dsnoop->shmid >= 0) && (snd_pcm_direct_shm_discard(dsnoop))) {
|
|
|
|
|
if (snd_pcm_direct_semaphore_discard(dsnoop))
|
|
|
|
|
snd_pcm_direct_semaphore_final(dsnoop, DIRECT_IPC_SEM_CLIENT);
|
|
|
|
|
} else
|
2005-05-23 08:56:48 +00:00
|
|
|
snd_pcm_direct_semaphore_up(dsnoop, DIRECT_IPC_SEM_CLIENT);
|
2016-11-25 15:43:40 +05:30
|
|
|
|
2005-05-23 08:56:48 +00:00
|
|
|
_err_nosem:
|
2003-03-19 16:35:35 +00:00
|
|
|
if (dsnoop) {
|
2006-02-27 09:58:32 +00:00
|
|
|
free(dsnoop->bindings);
|
2003-03-19 16:35:35 +00:00
|
|
|
free(dsnoop);
|
2003-03-18 20:37:42 +00:00
|
|
|
}
|
|
|
|
|
if (pcm)
|
|
|
|
|
snd_pcm_free(pcm);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*! \page pcm_plugins
|
|
|
|
|
|
2004-03-02 17:13:49 +00:00
|
|
|
\section pcm_plugins_dsnoop Plugin: dsnoop
|
2003-03-18 20:37:42 +00:00
|
|
|
|
|
|
|
|
This plugin splits one capture stream to more.
|
2003-10-23 14:42:47 +00:00
|
|
|
It works the reverse way of \ref pcm_plugins_dmix "dmix plugin",
|
|
|
|
|
reading the shared capture buffer from many clients concurrently.
|
|
|
|
|
The meaning of parameters below are almost identical with
|
|
|
|
|
dmix plugin.
|
2003-03-18 20:37:42 +00:00
|
|
|
|
|
|
|
|
\code
|
|
|
|
|
pcm.name {
|
|
|
|
|
type dsnoop # Direct snoop
|
|
|
|
|
ipc_key INT # unique IPC key
|
|
|
|
|
ipc_key_add_uid BOOL # add current uid to unique IPC key
|
2004-04-26 07:40:12 +00:00
|
|
|
ipc_perm INT # IPC permissions (octal, default 0600)
|
2019-05-15 11:56:34 +05:30
|
|
|
hw_ptr_alignment STR # Slave application and hw pointer alignment type
|
|
|
|
|
# STR can be one of the below strings :
|
|
|
|
|
# no
|
|
|
|
|
# roundup
|
|
|
|
|
# rounddown
|
|
|
|
|
# auto (default)
|
2020-04-15 00:44:39 +00:00
|
|
|
tstamp_type STR # timestamp type
|
|
|
|
|
# STR can be one of the below strings :
|
|
|
|
|
# default, gettimeofday, monotonic, monotonic_raw
|
2003-03-18 20:37:42 +00:00
|
|
|
slave STR
|
|
|
|
|
# or
|
|
|
|
|
slave { # Slave definition
|
|
|
|
|
pcm STR # slave PCM name
|
|
|
|
|
# or
|
|
|
|
|
pcm { } # slave PCM definition
|
|
|
|
|
format STR # format definition
|
|
|
|
|
rate INT # rate definition
|
|
|
|
|
channels INT
|
|
|
|
|
period_time INT # in usec
|
|
|
|
|
# or
|
2019-10-14 09:34:35 +02:00
|
|
|
period_size INT # in frames
|
2003-03-18 20:37:42 +00:00
|
|
|
buffer_time INT # in usec
|
|
|
|
|
# or
|
2019-10-14 09:34:35 +02:00
|
|
|
buffer_size INT # in frames
|
2003-03-18 20:37:42 +00:00
|
|
|
periods INT # when buffer_size or buffer_time is not specified
|
|
|
|
|
}
|
|
|
|
|
bindings { # note: this is client independent!!!
|
|
|
|
|
N INT # maps slave channel to client channel N
|
|
|
|
|
}
|
2004-04-26 07:40:12 +00:00
|
|
|
slowptr BOOL # slow but more precise pointer updates
|
2003-03-18 20:37:42 +00:00
|
|
|
}
|
|
|
|
|
\endcode
|
|
|
|
|
|
2019-05-15 11:56:34 +05:30
|
|
|
<code>hw_ptr_alignment</code> specifies slave application and hw
|
|
|
|
|
pointer alignment type. By default hw_ptr_alignment is auto. Below are
|
|
|
|
|
the possible configurations:
|
|
|
|
|
- no: minimal latency with minimal frames dropped at startup. But
|
|
|
|
|
wakeup of application (return from snd_pcm_wait() or poll()) can
|
|
|
|
|
take up to 2 * period.
|
|
|
|
|
- roundup: It is guaranteed that all frames will be played at
|
|
|
|
|
startup. But the latency will increase upto period-1 frames.
|
|
|
|
|
- rounddown: It is guaranteed that a wakeup will happen for each
|
|
|
|
|
period and frames can be written from application. But on startup
|
|
|
|
|
upto period-1 frames will be dropped.
|
|
|
|
|
- auto: Selects the best approach depending on the used period and
|
|
|
|
|
buffer size.
|
|
|
|
|
If the application buffer size is < 2 * application period,
|
|
|
|
|
"roundup" will be selected to avoid over runs. If the slave_period
|
|
|
|
|
is < 10ms we could expect that there are low latency
|
|
|
|
|
requirements. Therefore "rounddown" will be chosen to avoid long
|
|
|
|
|
wakeup times. Else "no" will be chosen.
|
|
|
|
|
|
2003-07-01 09:23:38 +00:00
|
|
|
\subsection pcm_plugins_dsnoop_funcref Function reference
|
2003-03-18 20:37:42 +00:00
|
|
|
|
|
|
|
|
<UL>
|
|
|
|
|
<LI>snd_pcm_dsnoop_open()
|
|
|
|
|
<LI>_snd_pcm_dsnoop_open()
|
|
|
|
|
</UL>
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
2003-03-19 16:35:35 +00:00
|
|
|
* \brief Creates a new dsnoop PCM
|
2003-03-18 20:37:42 +00:00
|
|
|
* \param pcmp Returns created PCM handle
|
|
|
|
|
* \param name Name of PCM
|
|
|
|
|
* \param root Root configuration node
|
2003-03-19 16:35:35 +00:00
|
|
|
* \param conf Configuration node with dsnoop PCM description
|
2003-03-18 20:37:42 +00:00
|
|
|
* \param stream PCM Stream
|
|
|
|
|
* \param mode PCM Mode
|
|
|
|
|
* \warning Using of this function might be dangerous in the sense
|
|
|
|
|
* of compatibility reasons. The prototype might be freely
|
|
|
|
|
* changed in future.
|
|
|
|
|
*/
|
|
|
|
|
int _snd_pcm_dsnoop_open(snd_pcm_t **pcmp, const char *name,
|
|
|
|
|
snd_config_t *root, snd_config_t *conf,
|
|
|
|
|
snd_pcm_stream_t stream, int mode)
|
|
|
|
|
{
|
2006-01-16 13:15:32 +00:00
|
|
|
snd_config_t *sconf;
|
2003-03-18 20:37:42 +00:00
|
|
|
struct slave_params params;
|
2006-01-16 13:15:32 +00:00
|
|
|
struct snd_pcm_direct_open_conf dopen;
|
|
|
|
|
int bsize, psize;
|
2003-03-18 20:37:42 +00:00
|
|
|
int err;
|
2003-12-07 09:30:47 +00:00
|
|
|
|
2006-04-06 18:49:11 +02:00
|
|
|
err = snd_pcm_direct_parse_open_conf(root, conf, stream, &dopen);
|
2006-01-16 13:15:32 +00:00
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
|
|
|
|
|
2003-03-18 20:37:42 +00:00
|
|
|
/* the default settings, it might be invalid for some hardware */
|
|
|
|
|
params.format = SND_PCM_FORMAT_S16;
|
|
|
|
|
params.rate = 48000;
|
|
|
|
|
params.channels = 2;
|
2004-10-29 10:56:13 +00:00
|
|
|
params.period_time = -1;
|
2003-03-18 20:37:42 +00:00
|
|
|
params.buffer_time = -1;
|
|
|
|
|
bsize = psize = -1;
|
|
|
|
|
params.periods = 3;
|
2006-01-16 13:15:32 +00:00
|
|
|
err = snd_pcm_slave_conf(root, dopen.slave, &sconf, 8,
|
2008-01-23 12:26:22 +01:00
|
|
|
SND_PCM_HW_PARAM_FORMAT, SCONF_UNCHANGED, ¶ms.format,
|
2003-03-18 20:37:42 +00:00
|
|
|
SND_PCM_HW_PARAM_RATE, 0, ¶ms.rate,
|
|
|
|
|
SND_PCM_HW_PARAM_CHANNELS, 0, ¶ms.channels,
|
|
|
|
|
SND_PCM_HW_PARAM_PERIOD_TIME, 0, ¶ms.period_time,
|
|
|
|
|
SND_PCM_HW_PARAM_BUFFER_TIME, 0, ¶ms.buffer_time,
|
2003-04-23 12:41:55 +00:00
|
|
|
SND_PCM_HW_PARAM_PERIOD_SIZE, 0, &psize,
|
|
|
|
|
SND_PCM_HW_PARAM_BUFFER_SIZE, 0, &bsize,
|
2003-03-18 20:37:42 +00:00
|
|
|
SND_PCM_HW_PARAM_PERIODS, 0, ¶ms.periods);
|
|
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
|
|
|
|
|
2004-10-29 10:56:13 +00:00
|
|
|
/* set a reasonable default */
|
|
|
|
|
if (psize == -1 && params.period_time == -1)
|
|
|
|
|
params.period_time = 125000; /* 0.125 seconds */
|
|
|
|
|
|
2008-01-23 12:26:22 +01:00
|
|
|
if (params.format == -2)
|
|
|
|
|
params.format = SND_PCM_FORMAT_UNKNOWN;
|
|
|
|
|
|
2003-03-18 20:37:42 +00:00
|
|
|
params.period_size = psize;
|
|
|
|
|
params.buffer_size = bsize;
|
2006-01-16 13:15:32 +00:00
|
|
|
|
2006-03-21 10:27:35 +00:00
|
|
|
err = snd_pcm_dsnoop_open(pcmp, name, &dopen, ¶ms,
|
2006-01-16 13:15:32 +00:00
|
|
|
root, sconf, stream, mode);
|
2006-10-12 14:42:45 +02:00
|
|
|
snd_config_delete(sconf);
|
2003-03-18 20:37:42 +00:00
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
#ifndef DOC_HIDDEN
|
|
|
|
|
SND_DLSYM_BUILD_VERSION(_snd_pcm_dsnoop_open, SND_PCM_DLSYM_VERSION);
|
|
|
|
|
#endif
|