|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* \file pcm/pcm_dsnoop.c
|
|
Packit Service |
db8eaa |
* \ingroup PCM_Plugins
|
|
Packit Service |
db8eaa |
* \brief PCM Capture Stream Snooping (dsnoop) Plugin Interface
|
|
Packit Service |
db8eaa |
* \author Jaroslav Kysela <perex@perex.cz>
|
|
Packit Service |
db8eaa |
* \date 2003
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
/*
|
|
Packit Service |
db8eaa |
* PCM - Capture Stream Snooping
|
|
Packit Service |
db8eaa |
* Copyright (c) 2003 by Jaroslav Kysela <perex@perex.cz>
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* This library is free software; you can redistribute it and/or modify
|
|
Packit Service |
db8eaa |
* it under the terms of the GNU Lesser General Public License as
|
|
Packit Service |
db8eaa |
* published by the Free Software Foundation; either version 2.1 of
|
|
Packit Service |
db8eaa |
* the License, or (at your option) any later version.
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
db8eaa |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
db8eaa |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
db8eaa |
* GNU Lesser General Public License for more details.
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
db8eaa |
* License along with this library; if not, write to the Free Software
|
|
Packit Service |
db8eaa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
#include <stdio.h>
|
|
Packit Service |
db8eaa |
#include <stdlib.h>
|
|
Packit Service |
db8eaa |
#include <stddef.h>
|
|
Packit Service |
db8eaa |
#include <unistd.h>
|
|
Packit Service |
db8eaa |
#include <signal.h>
|
|
Packit Service |
db8eaa |
#include <string.h>
|
|
Packit Service |
db8eaa |
#include <fcntl.h>
|
|
Packit Service |
db8eaa |
#include <ctype.h>
|
|
Packit Service |
db8eaa |
#include <grp.h>
|
|
Packit Service |
db8eaa |
#include <sys/ioctl.h>
|
|
Packit Service |
db8eaa |
#include <sys/mman.h>
|
|
Packit Service |
db8eaa |
#include <sys/shm.h>
|
|
Packit Service |
db8eaa |
#include <sys/sem.h>
|
|
Packit Service |
db8eaa |
#include <sys/wait.h>
|
|
Packit Service |
db8eaa |
#include <sys/socket.h>
|
|
Packit Service |
db8eaa |
#include <sys/un.h>
|
|
Packit Service |
db8eaa |
#include <sys/mman.h>
|
|
Packit Service |
db8eaa |
#include "pcm_direct.h"
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
#ifndef PIC
|
|
Packit Service |
db8eaa |
/* entry for static linking */
|
|
Packit Service |
db8eaa |
const char *_snd_module_pcm_dsnoop = "";
|
|
Packit Service |
db8eaa |
#endif
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/*
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static int snoop_timestamp(snd_pcm_t *pcm)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
Packit Service |
db8eaa |
snd_pcm_uframes_t ptr1 = -2LL /* invalid value */, ptr2;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/* loop is required to sync hw.ptr with timestamp */
|
|
Packit Service |
db8eaa |
while (1) {
|
|
Packit Service |
db8eaa |
ptr2 = *dsnoop->spcm->hw.ptr;
|
|
Packit Service |
db8eaa |
if (ptr1 == ptr2)
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
ptr1 = ptr2;
|
|
Packit Service |
db8eaa |
dsnoop->update_tstamp = snd_pcm_hw_fast_tstamp(dsnoop->spcm);
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
dsnoop->slave_hw_ptr = ptr1;
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static void snoop_areas(snd_pcm_direct_t *dsnoop,
|
|
Packit Service |
db8eaa |
const snd_pcm_channel_area_t *src_areas,
|
|
Packit Service |
db8eaa |
const snd_pcm_channel_area_t *dst_areas,
|
|
Packit Service |
db8eaa |
snd_pcm_uframes_t src_ofs,
|
|
Packit Service |
db8eaa |
snd_pcm_uframes_t dst_ofs,
|
|
Packit Service |
db8eaa |
snd_pcm_uframes_t size)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
unsigned int chn, schn, channels;
|
|
Packit Service |
db8eaa |
snd_pcm_format_t format;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
channels = dsnoop->channels;
|
|
Packit Service |
db8eaa |
format = dsnoop->shmptr->s.format;
|
|
Packit Service |
db8eaa |
if (dsnoop->interleaved) {
|
|
Packit Service |
db8eaa |
unsigned int fbytes = snd_pcm_format_physical_width(format) / 8;
|
|
Packit Service |
db8eaa |
memcpy(((char *)dst_areas[0].addr) + (dst_ofs * channels * fbytes),
|
|
Packit Service |
db8eaa |
((char *)src_areas[0].addr) + (src_ofs * channels * fbytes),
|
|
Packit Service |
db8eaa |
size * channels * fbytes);
|
|
Packit Service |
db8eaa |
} else {
|
|
Packit Service |
db8eaa |
for (chn = 0; chn < channels; chn++) {
|
|
Packit Service |
db8eaa |
schn = dsnoop->bindings ? dsnoop->bindings[chn] : chn;
|
|
Packit Service |
db8eaa |
snd_pcm_area_copy(&dst_areas[chn], dst_ofs, &src_areas[schn], src_ofs, size, format);
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/*
|
|
Packit Service |
db8eaa |
* synchronize shm ring buffer with hardware
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
static void snd_pcm_dsnoop_sync_area(snd_pcm_t *pcm, snd_pcm_uframes_t slave_hw_ptr, snd_pcm_uframes_t size)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
Packit Service |
db8eaa |
snd_pcm_uframes_t hw_ptr = dsnoop->hw_ptr;
|
|
Packit Service |
db8eaa |
snd_pcm_uframes_t transfer;
|
|
Packit Service |
db8eaa |
const snd_pcm_channel_area_t *src_areas, *dst_areas;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/* add sample areas here */
|
|
Packit Service |
db8eaa |
dst_areas = snd_pcm_mmap_areas(pcm);
|
|
Packit Service |
db8eaa |
src_areas = snd_pcm_mmap_areas(dsnoop->spcm);
|
|
Packit Service |
db8eaa |
hw_ptr %= pcm->buffer_size;
|
|
Packit Service |
db8eaa |
slave_hw_ptr %= dsnoop->slave_buffer_size;
|
|
Packit Service |
db8eaa |
while (size > 0) {
|
|
Packit Service |
db8eaa |
transfer = hw_ptr + size > pcm->buffer_size ? pcm->buffer_size - hw_ptr : size;
|
|
Packit Service |
db8eaa |
transfer = slave_hw_ptr + transfer > dsnoop->slave_buffer_size ?
|
|
Packit Service |
db8eaa |
dsnoop->slave_buffer_size - slave_hw_ptr : transfer;
|
|
Packit Service |
db8eaa |
size -= transfer;
|
|
Packit Service |
db8eaa |
snoop_areas(dsnoop, src_areas, dst_areas, slave_hw_ptr, hw_ptr, transfer);
|
|
Packit Service |
db8eaa |
slave_hw_ptr += transfer;
|
|
Packit Service |
db8eaa |
slave_hw_ptr %= dsnoop->slave_buffer_size;
|
|
Packit Service |
db8eaa |
hw_ptr += transfer;
|
|
Packit Service |
db8eaa |
hw_ptr %= pcm->buffer_size;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/*
|
|
Packit Service |
db8eaa |
* synchronize hardware pointer (hw_ptr) with ours
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
static int snd_pcm_dsnoop_sync_ptr(snd_pcm_t *pcm)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
Packit Service |
db8eaa |
snd_pcm_uframes_t slave_hw_ptr, old_slave_hw_ptr, avail;
|
|
Packit Service |
db8eaa |
snd_pcm_sframes_t diff;
|
|
Packit Service |
db8eaa |
int err;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
switch (snd_pcm_state(dsnoop->spcm)) {
|
|
Packit Service |
db8eaa |
case SND_PCM_STATE_DISCONNECTED:
|
|
Packit Service |
db8eaa |
dsnoop->state = SNDRV_PCM_STATE_DISCONNECTED;
|
|
Packit Service |
db8eaa |
return -ENODEV;
|
|
Packit Service |
db8eaa |
case SND_PCM_STATE_XRUN:
|
|
Packit Service |
db8eaa |
if ((err = snd_pcm_direct_slave_recover(dsnoop)) < 0)
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
default:
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (snd_pcm_direct_client_chk_xrun(dsnoop, pcm))
|
|
Packit Service |
db8eaa |
return -EPIPE;
|
|
Packit Service |
db8eaa |
if (dsnoop->slowptr)
|
|
Packit Service |
db8eaa |
snd_pcm_hwsync(dsnoop->spcm);
|
|
Packit Service |
db8eaa |
old_slave_hw_ptr = dsnoop->slave_hw_ptr;
|
|
Packit Service |
db8eaa |
snoop_timestamp(pcm);
|
|
Packit Service |
db8eaa |
slave_hw_ptr = dsnoop->slave_hw_ptr;
|
|
Packit Service |
db8eaa |
diff = pcm_frame_diff(slave_hw_ptr, old_slave_hw_ptr, dsnoop->slave_boundary);
|
|
Packit Service |
db8eaa |
if (diff == 0) /* fast path */
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
snd_pcm_dsnoop_sync_area(pcm, old_slave_hw_ptr, diff);
|
|
Packit Service |
db8eaa |
dsnoop->hw_ptr += diff;
|
|
Packit Service |
db8eaa |
dsnoop->hw_ptr %= pcm->boundary;
|
|
Packit Service |
db8eaa |
// printf("sync ptr diff = %li\n", diff);
|
|
Packit Service |
db8eaa |
if (pcm->stop_threshold >= pcm->boundary) /* don't care */
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
if ((avail = snd_pcm_mmap_capture_avail(pcm)) >= pcm->stop_threshold) {
|
|
Packit Service |
db8eaa |
gettimestamp(&dsnoop->trigger_tstamp, pcm->tstamp_type);
|
|
Packit Service |
db8eaa |
dsnoop->state = SND_PCM_STATE_XRUN;
|
|
Packit Service |
db8eaa |
dsnoop->avail_max = avail;
|
|
Packit Service |
db8eaa |
return -EPIPE;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (avail > dsnoop->avail_max)
|
|
Packit Service |
db8eaa |
dsnoop->avail_max = avail;
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/*
|
|
Packit Service |
db8eaa |
* plugin implementation
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static int snd_pcm_dsnoop_status(snd_pcm_t *pcm, snd_pcm_status_t * status)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
Packit Service |
db8eaa |
snd_pcm_state_t state;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
switch(dsnoop->state) {
|
|
Packit Service |
db8eaa |
case SNDRV_PCM_STATE_DRAINING:
|
|
Packit Service |
db8eaa |
case SNDRV_PCM_STATE_RUNNING:
|
|
Packit Service |
db8eaa |
snd_pcm_dsnoop_sync_ptr(pcm);
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
default:
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
memset(status, 0, sizeof(*status));
|
|
Packit Service |
db8eaa |
snd_pcm_status(dsnoop->spcm, status);
|
|
Packit Service |
db8eaa |
state = snd_pcm_state(dsnoop->spcm);
|
|
Packit Service |
db8eaa |
status->state = state == SND_PCM_STATE_RUNNING ? dsnoop->state : state;
|
|
Packit Service |
db8eaa |
status->trigger_tstamp = dsnoop->trigger_tstamp;
|
|
Packit Service |
db8eaa |
status->avail = snd_pcm_mmap_capture_avail(pcm);
|
|
Packit Service |
db8eaa |
status->avail_max = status->avail > dsnoop->avail_max ? status->avail : dsnoop->avail_max;
|
|
Packit Service |
db8eaa |
dsnoop->avail_max = 0;
|
|
Packit Service |
db8eaa |
status->delay = snd_pcm_mmap_capture_delay(pcm);
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static snd_pcm_state_t snd_pcm_dsnoop_state(snd_pcm_t *pcm)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
Packit Service |
db8eaa |
int err;
|
|
Packit Service |
db8eaa |
snd_pcm_state_t state;
|
|
Packit Service |
db8eaa |
state = snd_pcm_state(dsnoop->spcm);
|
|
Packit Service |
db8eaa |
switch (state) {
|
|
Packit Service |
db8eaa |
case SND_PCM_STATE_SUSPENDED:
|
|
Packit Service |
db8eaa |
case SND_PCM_STATE_DISCONNECTED:
|
|
Packit Service |
db8eaa |
dsnoop->state = state;
|
|
Packit Service |
db8eaa |
return state;
|
|
Packit Service |
db8eaa |
case SND_PCM_STATE_XRUN:
|
|
Packit Service |
db8eaa |
if ((err = snd_pcm_direct_slave_recover(dsnoop)) < 0)
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
default:
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
snd_pcm_direct_client_chk_xrun(dsnoop, pcm);
|
|
Packit Service |
db8eaa |
return dsnoop->state;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static int snd_pcm_dsnoop_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delayp)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
Packit Service |
db8eaa |
int err;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
switch(dsnoop->state) {
|
|
Packit Service |
db8eaa |
case SNDRV_PCM_STATE_DRAINING:
|
|
Packit Service |
db8eaa |
case SNDRV_PCM_STATE_RUNNING:
|
|
Packit Service |
db8eaa |
err = snd_pcm_dsnoop_sync_ptr(pcm);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
/* Fall through */
|
|
Packit Service |
db8eaa |
case SNDRV_PCM_STATE_PREPARED:
|
|
Packit Service |
db8eaa |
case SNDRV_PCM_STATE_SUSPENDED:
|
|
Packit Service |
db8eaa |
*delayp = snd_pcm_mmap_capture_avail(pcm);
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
case SNDRV_PCM_STATE_XRUN:
|
|
Packit Service |
db8eaa |
return -EPIPE;
|
|
Packit Service |
db8eaa |
case SNDRV_PCM_STATE_DISCONNECTED:
|
|
Packit Service |
db8eaa |
return -ENODEV;
|
|
Packit Service |
db8eaa |
default:
|
|
Packit Service |
db8eaa |
return -EBADFD;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static int snd_pcm_dsnoop_hwsync(snd_pcm_t *pcm)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
switch(dsnoop->state) {
|
|
Packit Service |
db8eaa |
case SNDRV_PCM_STATE_DRAINING:
|
|
Packit Service |
db8eaa |
case SNDRV_PCM_STATE_RUNNING:
|
|
Packit Service |
db8eaa |
return snd_pcm_dsnoop_sync_ptr(pcm);
|
|
Packit Service |
db8eaa |
case SNDRV_PCM_STATE_PREPARED:
|
|
Packit Service |
db8eaa |
case SNDRV_PCM_STATE_SUSPENDED:
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
case SNDRV_PCM_STATE_XRUN:
|
|
Packit Service |
db8eaa |
return -EPIPE;
|
|
Packit Service |
db8eaa |
case SNDRV_PCM_STATE_DISCONNECTED:
|
|
Packit Service |
db8eaa |
return -ENODEV;
|
|
Packit Service |
db8eaa |
default:
|
|
Packit Service |
db8eaa |
return -EBADFD;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static int snd_pcm_dsnoop_reset(snd_pcm_t *pcm)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
Packit Service |
db8eaa |
dsnoop->hw_ptr %= pcm->period_size;
|
|
Packit Service |
db8eaa |
dsnoop->appl_ptr = dsnoop->hw_ptr;
|
|
Packit Service |
db8eaa |
dsnoop->slave_appl_ptr = dsnoop->slave_hw_ptr;
|
|
Packit Service |
db8eaa |
snd_pcm_direct_reset_slave_ptr(pcm, dsnoop);
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static int snd_pcm_dsnoop_start(snd_pcm_t *pcm)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
Packit Service |
db8eaa |
int err;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
if (dsnoop->state != SND_PCM_STATE_PREPARED)
|
|
Packit Service |
db8eaa |
return -EBADFD;
|
|
Packit Service |
db8eaa |
snd_pcm_hwsync(dsnoop->spcm);
|
|
Packit Service |
db8eaa |
snoop_timestamp(pcm);
|
|
Packit Service |
db8eaa |
dsnoop->slave_appl_ptr = dsnoop->slave_hw_ptr;
|
|
Packit Service |
db8eaa |
snd_pcm_direct_reset_slave_ptr(pcm, dsnoop);
|
|
Packit Service |
db8eaa |
err = snd_timer_start(dsnoop->timer);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
dsnoop->state = SND_PCM_STATE_RUNNING;
|
|
Packit Service |
db8eaa |
dsnoop->trigger_tstamp = dsnoop->update_tstamp;
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static int snd_pcm_dsnoop_drop(snd_pcm_t *pcm)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
Packit Service |
db8eaa |
if (dsnoop->state == SND_PCM_STATE_OPEN)
|
|
Packit Service |
db8eaa |
return -EBADFD;
|
|
Packit Service |
db8eaa |
dsnoop->state = SND_PCM_STATE_SETUP;
|
|
Packit Service |
db8eaa |
snd_timer_stop(dsnoop->timer);
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/* locked version */
|
|
Packit Service |
db8eaa |
static int __snd_pcm_dsnoop_drain(snd_pcm_t *pcm)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
Packit Service |
db8eaa |
snd_pcm_uframes_t stop_threshold;
|
|
Packit Service |
db8eaa |
int err;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
if (dsnoop->state == SND_PCM_STATE_OPEN)
|
|
Packit Service |
db8eaa |
return -EBADFD;
|
|
Packit Service |
db8eaa |
stop_threshold = pcm->stop_threshold;
|
|
Packit Service |
db8eaa |
if (pcm->stop_threshold > pcm->buffer_size)
|
|
Packit Service |
db8eaa |
pcm->stop_threshold = pcm->buffer_size;
|
|
Packit Service |
db8eaa |
while (dsnoop->state == SND_PCM_STATE_RUNNING) {
|
|
Packit Service |
db8eaa |
err = snd_pcm_dsnoop_sync_ptr(pcm);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
if (pcm->mode & SND_PCM_NONBLOCK)
|
|
Packit Service |
db8eaa |
return -EAGAIN;
|
|
Packit Service |
db8eaa |
__snd_pcm_wait_in_lock(pcm, -1);
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
pcm->stop_threshold = stop_threshold;
|
|
Packit Service |
db8eaa |
return snd_pcm_dsnoop_drop(pcm);
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static int snd_pcm_dsnoop_drain(snd_pcm_t *pcm)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
int err;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
snd_pcm_lock(pcm);
|
|
Packit Service |
db8eaa |
err = __snd_pcm_dsnoop_drain(pcm);
|
|
Packit Service |
db8eaa |
snd_pcm_unlock(pcm);
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static int snd_pcm_dsnoop_pause(snd_pcm_t *pcm ATTRIBUTE_UNUSED, int enable ATTRIBUTE_UNUSED)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
return -EIO;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static snd_pcm_sframes_t snd_pcm_dsnoop_rewindable(snd_pcm_t *pcm)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
return snd_pcm_mmap_capture_hw_rewindable(pcm);
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static snd_pcm_sframes_t snd_pcm_dsnoop_rewind(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_pcm_sframes_t avail;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
avail = snd_pcm_dsnoop_rewindable(pcm);
|
|
Packit Service |
db8eaa |
if (frames > (snd_pcm_uframes_t)avail)
|
|
Packit Service |
db8eaa |
frames = avail;
|
|
Packit Service |
db8eaa |
snd_pcm_mmap_appl_backward(pcm, frames);
|
|
Packit Service |
db8eaa |
return frames;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static snd_pcm_sframes_t snd_pcm_dsnoop_forwardable(snd_pcm_t *pcm)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
return snd_pcm_mmap_capture_avail(pcm);
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static snd_pcm_sframes_t snd_pcm_dsnoop_forward(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_pcm_sframes_t avail;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
avail = snd_pcm_dsnoop_forwardable(pcm);
|
|
Packit Service |
db8eaa |
if (frames > (snd_pcm_uframes_t)avail)
|
|
Packit Service |
db8eaa |
frames = avail;
|
|
Packit Service |
db8eaa |
snd_pcm_mmap_appl_forward(pcm, frames);
|
|
Packit Service |
db8eaa |
return frames;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
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)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
return -ENODEV;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
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)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
return -ENODEV;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static int snd_pcm_dsnoop_close(snd_pcm_t *pcm)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
if (dsnoop->timer)
|
|
Packit Service |
db8eaa |
snd_timer_close(dsnoop->timer);
|
|
Packit Service |
db8eaa |
snd_pcm_direct_semaphore_down(dsnoop, DIRECT_IPC_SEM_CLIENT);
|
|
Packit Service |
db8eaa |
snd_pcm_close(dsnoop->spcm);
|
|
Packit Service |
db8eaa |
if (dsnoop->server)
|
|
Packit Service |
db8eaa |
snd_pcm_direct_server_discard(dsnoop);
|
|
Packit Service |
db8eaa |
if (dsnoop->client)
|
|
Packit Service |
db8eaa |
snd_pcm_direct_client_discard(dsnoop);
|
|
Packit Service |
db8eaa |
if (snd_pcm_direct_shm_discard(dsnoop)) {
|
|
Packit Service |
db8eaa |
if (snd_pcm_direct_semaphore_discard(dsnoop))
|
|
Packit Service |
db8eaa |
snd_pcm_direct_semaphore_final(dsnoop, DIRECT_IPC_SEM_CLIENT);
|
|
Packit Service |
db8eaa |
} else
|
|
Packit Service |
db8eaa |
snd_pcm_direct_semaphore_final(dsnoop, DIRECT_IPC_SEM_CLIENT);
|
|
Packit Service |
db8eaa |
free(dsnoop->bindings);
|
|
Packit Service |
db8eaa |
pcm->private_data = NULL;
|
|
Packit Service |
db8eaa |
free(dsnoop);
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static snd_pcm_sframes_t snd_pcm_dsnoop_mmap_commit(snd_pcm_t *pcm,
|
|
Packit Service |
db8eaa |
snd_pcm_uframes_t offset ATTRIBUTE_UNUSED,
|
|
Packit Service |
db8eaa |
snd_pcm_uframes_t size)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
Packit Service |
db8eaa |
int err;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
switch (snd_pcm_state(dsnoop->spcm)) {
|
|
Packit Service |
db8eaa |
case SND_PCM_STATE_XRUN:
|
|
Packit Service |
db8eaa |
if ((err = snd_pcm_direct_slave_recover(dsnoop)) < 0)
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
case SND_PCM_STATE_SUSPENDED:
|
|
Packit Service |
db8eaa |
return -ESTRPIPE;
|
|
Packit Service |
db8eaa |
default:
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (snd_pcm_direct_client_chk_xrun(dsnoop, pcm))
|
|
Packit Service |
db8eaa |
return -EPIPE;
|
|
Packit Service |
db8eaa |
if (dsnoop->state == SND_PCM_STATE_RUNNING) {
|
|
Packit Service |
db8eaa |
err = snd_pcm_dsnoop_sync_ptr(pcm);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
snd_pcm_mmap_appl_forward(pcm, size);
|
|
Packit Service |
db8eaa |
/* clear timer queue to avoid a bogus return from poll */
|
|
Packit Service |
db8eaa |
if (snd_pcm_mmap_capture_avail(pcm) < pcm->avail_min)
|
|
Packit Service |
db8eaa |
snd_pcm_direct_clear_timer_queue(dsnoop);
|
|
Packit Service |
db8eaa |
return size;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static snd_pcm_sframes_t snd_pcm_dsnoop_avail_update(snd_pcm_t *pcm)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
Packit Service |
db8eaa |
int err;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
if (dsnoop->state == SND_PCM_STATE_RUNNING) {
|
|
Packit Service |
db8eaa |
err = snd_pcm_dsnoop_sync_ptr(pcm);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (dsnoop->state == SND_PCM_STATE_XRUN)
|
|
Packit Service |
db8eaa |
return -EPIPE;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
return snd_pcm_mmap_capture_avail(pcm);
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static int snd_pcm_dsnoop_htimestamp(snd_pcm_t *pcm,
|
|
Packit Service |
db8eaa |
snd_pcm_uframes_t *avail,
|
|
Packit Service |
db8eaa |
snd_htimestamp_t *tstamp)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
Packit Service |
db8eaa |
snd_pcm_uframes_t avail1;
|
|
Packit Service |
db8eaa |
int ok = 0;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
while (1) {
|
|
Packit Service |
db8eaa |
if (dsnoop->state == SND_PCM_STATE_RUNNING ||
|
|
Packit Service |
db8eaa |
dsnoop->state == SND_PCM_STATE_DRAINING)
|
|
Packit Service |
db8eaa |
snd_pcm_dsnoop_sync_ptr(pcm);
|
|
Packit Service |
db8eaa |
avail1 = snd_pcm_mmap_capture_avail(pcm);
|
|
Packit Service |
db8eaa |
if (ok && *avail == avail1)
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
*avail = avail1;
|
|
Packit Service |
db8eaa |
*tstamp = snd_pcm_hw_fast_tstamp(dsnoop->spcm);
|
|
Packit Service |
db8eaa |
ok = 1;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static void snd_pcm_dsnoop_dump(snd_pcm_t *pcm, snd_output_t *out)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_pcm_direct_t *dsnoop = pcm->private_data;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
snd_output_printf(out, "Direct Snoop PCM\n");
|
|
Packit Service |
db8eaa |
if (pcm->setup) {
|
|
Packit Service |
db8eaa |
snd_output_printf(out, "Its setup is:\n");
|
|
Packit Service |
db8eaa |
snd_pcm_dump_setup(pcm, out);
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (dsnoop->spcm)
|
|
Packit Service |
db8eaa |
snd_pcm_dump(dsnoop->spcm, out);
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static const snd_pcm_ops_t snd_pcm_dsnoop_ops = {
|
|
Packit Service |
db8eaa |
.close = snd_pcm_dsnoop_close,
|
|
Packit Service |
db8eaa |
.info = snd_pcm_direct_info,
|
|
Packit Service |
db8eaa |
.hw_refine = snd_pcm_direct_hw_refine,
|
|
Packit Service |
db8eaa |
.hw_params = snd_pcm_direct_hw_params,
|
|
Packit Service |
db8eaa |
.hw_free = snd_pcm_direct_hw_free,
|
|
Packit Service |
db8eaa |
.sw_params = snd_pcm_direct_sw_params,
|
|
Packit Service |
db8eaa |
.channel_info = snd_pcm_direct_channel_info,
|
|
Packit Service |
db8eaa |
.dump = snd_pcm_dsnoop_dump,
|
|
Packit Service |
db8eaa |
.nonblock = snd_pcm_direct_nonblock,
|
|
Packit Service |
db8eaa |
.async = snd_pcm_direct_async,
|
|
Packit Service |
db8eaa |
.mmap = snd_pcm_direct_mmap,
|
|
Packit Service |
db8eaa |
.munmap = snd_pcm_direct_munmap,
|
|
Packit Service |
db8eaa |
.query_chmaps = snd_pcm_direct_query_chmaps,
|
|
Packit Service |
db8eaa |
.get_chmap = snd_pcm_direct_get_chmap,
|
|
Packit Service |
db8eaa |
.set_chmap = snd_pcm_direct_set_chmap,
|
|
Packit Service |
db8eaa |
};
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static const snd_pcm_fast_ops_t snd_pcm_dsnoop_fast_ops = {
|
|
Packit Service |
db8eaa |
.status = snd_pcm_dsnoop_status,
|
|
Packit Service |
db8eaa |
.state = snd_pcm_dsnoop_state,
|
|
Packit Service |
db8eaa |
.hwsync = snd_pcm_dsnoop_hwsync,
|
|
Packit Service |
db8eaa |
.delay = snd_pcm_dsnoop_delay,
|
|
Packit Service |
db8eaa |
.prepare = snd_pcm_direct_prepare,
|
|
Packit Service |
db8eaa |
.reset = snd_pcm_dsnoop_reset,
|
|
Packit Service |
db8eaa |
.start = snd_pcm_dsnoop_start,
|
|
Packit Service |
db8eaa |
.drop = snd_pcm_dsnoop_drop,
|
|
Packit Service |
db8eaa |
.drain = snd_pcm_dsnoop_drain,
|
|
Packit Service |
db8eaa |
.pause = snd_pcm_dsnoop_pause,
|
|
Packit Service |
db8eaa |
.rewindable = snd_pcm_dsnoop_rewindable,
|
|
Packit Service |
db8eaa |
.rewind = snd_pcm_dsnoop_rewind,
|
|
Packit Service |
db8eaa |
.forwardable = snd_pcm_dsnoop_forwardable,
|
|
Packit Service |
db8eaa |
.forward = snd_pcm_dsnoop_forward,
|
|
Packit Service |
db8eaa |
.resume = snd_pcm_direct_resume,
|
|
Packit Service |
db8eaa |
.link = NULL,
|
|
Packit Service |
db8eaa |
.link_slaves = NULL,
|
|
Packit Service |
db8eaa |
.unlink = NULL,
|
|
Packit Service |
db8eaa |
.writei = snd_pcm_dsnoop_writei,
|
|
Packit Service |
db8eaa |
.writen = snd_pcm_dsnoop_writen,
|
|
Packit Service |
db8eaa |
.readi = snd_pcm_mmap_readi,
|
|
Packit Service |
db8eaa |
.readn = snd_pcm_mmap_readn,
|
|
Packit Service |
db8eaa |
.avail_update = snd_pcm_dsnoop_avail_update,
|
|
Packit Service |
db8eaa |
.mmap_commit = snd_pcm_dsnoop_mmap_commit,
|
|
Packit Service |
db8eaa |
.htimestamp = snd_pcm_dsnoop_htimestamp,
|
|
Packit Service |
db8eaa |
.poll_descriptors = snd_pcm_direct_poll_descriptors,
|
|
Packit Service |
db8eaa |
.poll_descriptors_count = NULL,
|
|
Packit Service |
db8eaa |
.poll_revents = snd_pcm_direct_poll_revents,
|
|
Packit Service |
db8eaa |
};
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* \brief Creates a new dsnoop PCM
|
|
Packit Service |
db8eaa |
* \param pcmp Returns created PCM handle
|
|
Packit Service |
db8eaa |
* \param name Name of PCM
|
|
Packit Service |
db8eaa |
* \param opts Direct PCM configurations
|
|
Packit Service |
db8eaa |
* \param params Parameters for slave
|
|
Packit Service |
db8eaa |
* \param root Configuration root
|
|
Packit Service |
db8eaa |
* \param sconf Slave configuration
|
|
Packit Service |
db8eaa |
* \param stream PCM Direction (stream)
|
|
Packit Service |
db8eaa |
* \param mode PCM Mode
|
|
Packit Service |
db8eaa |
* \retval zero on success otherwise a negative error code
|
|
Packit Service |
db8eaa |
* \warning Using of this function might be dangerous in the sense
|
|
Packit Service |
db8eaa |
* of compatibility reasons. The prototype might be freely
|
|
Packit Service |
db8eaa |
* changed in future.
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
int snd_pcm_dsnoop_open(snd_pcm_t **pcmp, const char *name,
|
|
Packit Service |
db8eaa |
struct snd_pcm_direct_open_conf *opts,
|
|
Packit Service |
db8eaa |
struct slave_params *params,
|
|
Packit Service |
db8eaa |
snd_config_t *root, snd_config_t *sconf,
|
|
Packit Service |
db8eaa |
snd_pcm_stream_t stream, int mode)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_pcm_t *pcm = NULL, *spcm = NULL;
|
|
Packit Service |
db8eaa |
snd_pcm_direct_t *dsnoop = NULL;
|
|
Packit Service |
db8eaa |
int ret, first_instance, fail_sem_loop = 10;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
assert(pcmp);
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
if (stream != SND_PCM_STREAM_CAPTURE) {
|
|
Packit Service |
db8eaa |
SNDERR("The dsnoop plugin supports only capture stream");
|
|
Packit Service |
db8eaa |
return -EINVAL;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
dsnoop = calloc(1, sizeof(snd_pcm_direct_t));
|
|
Packit Service |
db8eaa |
if (!dsnoop) {
|
|
Packit Service |
db8eaa |
ret = -ENOMEM;
|
|
Packit Service |
db8eaa |
goto _err_nosem;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
ret = snd_pcm_direct_parse_bindings(dsnoop, params, opts->bindings);
|
|
Packit Service |
db8eaa |
if (ret < 0)
|
|
Packit Service |
db8eaa |
goto _err_nosem;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
dsnoop->ipc_key = opts->ipc_key;
|
|
Packit Service |
db8eaa |
dsnoop->ipc_perm = opts->ipc_perm;
|
|
Packit Service |
db8eaa |
dsnoop->ipc_gid = opts->ipc_gid;
|
|
Packit Service |
db8eaa |
dsnoop->tstamp_type = opts->tstamp_type;
|
|
Packit Service |
db8eaa |
dsnoop->semid = -1;
|
|
Packit Service |
db8eaa |
dsnoop->shmid = -1;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
ret = snd_pcm_new(&pcm, dsnoop->type = SND_PCM_TYPE_DSNOOP, name, stream, mode);
|
|
Packit Service |
db8eaa |
if (ret < 0)
|
|
Packit Service |
db8eaa |
goto _err_nosem;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
while (1) {
|
|
Packit Service |
db8eaa |
ret = snd_pcm_direct_semaphore_create_or_connect(dsnoop);
|
|
Packit Service |
db8eaa |
if (ret < 0) {
|
|
Packit Service |
db8eaa |
SNDERR("unable to create IPC semaphore");
|
|
Packit Service |
db8eaa |
goto _err_nosem;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
ret = snd_pcm_direct_semaphore_down(dsnoop, DIRECT_IPC_SEM_CLIENT);
|
|
Packit Service |
db8eaa |
if (ret < 0) {
|
|
Packit Service |
db8eaa |
snd_pcm_direct_semaphore_discard(dsnoop);
|
|
Packit Service |
db8eaa |
if (--fail_sem_loop <= 0)
|
|
Packit Service |
db8eaa |
goto _err_nosem;
|
|
Packit Service |
db8eaa |
continue;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
first_instance = ret = snd_pcm_direct_shm_create_or_connect(dsnoop);
|
|
Packit Service |
db8eaa |
if (ret < 0) {
|
|
Packit Service |
db8eaa |
SNDERR("unable to create IPC shm instance");
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
pcm->ops = &snd_pcm_dsnoop_ops;
|
|
Packit Service |
db8eaa |
pcm->fast_ops = &snd_pcm_dsnoop_fast_ops;
|
|
Packit Service |
db8eaa |
pcm->private_data = dsnoop;
|
|
Packit Service |
db8eaa |
dsnoop->state = SND_PCM_STATE_OPEN;
|
|
Packit Service |
db8eaa |
dsnoop->slowptr = opts->slowptr;
|
|
Packit Service |
db8eaa |
dsnoop->max_periods = opts->max_periods;
|
|
Packit Service |
db8eaa |
dsnoop->var_periodsize = opts->var_periodsize;
|
|
Packit Service |
db8eaa |
dsnoop->sync_ptr = snd_pcm_dsnoop_sync_ptr;
|
|
Packit Service |
db8eaa |
dsnoop->hw_ptr_alignment = opts->hw_ptr_alignment;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
retry:
|
|
Packit Service |
db8eaa |
if (first_instance) {
|
|
Packit Service |
db8eaa |
/* recursion is already checked in
|
|
Packit Service |
db8eaa |
snd_pcm_direct_get_slave_ipc_offset() */
|
|
Packit Service |
db8eaa |
ret = snd_pcm_open_slave(&spcm, root, sconf, stream,
|
|
Packit Service |
db8eaa |
mode | SND_PCM_NONBLOCK, NULL);
|
|
Packit Service |
db8eaa |
if (ret < 0) {
|
|
Packit Service |
db8eaa |
SNDERR("unable to open slave");
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
if (snd_pcm_type(spcm) != SND_PCM_TYPE_HW) {
|
|
Packit Service |
db8eaa |
SNDERR("dsnoop plugin can be only connected to hw plugin");
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
ret = snd_pcm_direct_initialize_slave(dsnoop, spcm, params);
|
|
Packit Service |
db8eaa |
if (ret < 0) {
|
|
Packit Service |
db8eaa |
SNDERR("unable to initialize slave");
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
dsnoop->spcm = spcm;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
if (dsnoop->shmptr->use_server) {
|
|
Packit Service |
db8eaa |
ret = snd_pcm_direct_server_create(dsnoop);
|
|
Packit Service |
db8eaa |
if (ret < 0) {
|
|
Packit Service |
db8eaa |
SNDERR("unable to create server");
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
dsnoop->shmptr->type = spcm->type;
|
|
Packit Service |
db8eaa |
} else {
|
|
Packit Service |
db8eaa |
if (dsnoop->shmptr->use_server) {
|
|
Packit Service |
db8eaa |
/* up semaphore to avoid deadlock */
|
|
Packit Service |
db8eaa |
snd_pcm_direct_semaphore_up(dsnoop, DIRECT_IPC_SEM_CLIENT);
|
|
Packit Service |
db8eaa |
ret = snd_pcm_direct_client_connect(dsnoop);
|
|
Packit Service |
db8eaa |
if (ret < 0) {
|
|
Packit Service |
db8eaa |
SNDERR("unable to connect client");
|
|
Packit Service |
db8eaa |
goto _err_nosem;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
snd_pcm_direct_semaphore_down(dsnoop, DIRECT_IPC_SEM_CLIENT);
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
ret = snd_pcm_direct_open_secondary_client(&spcm, dsnoop, "dsnoop_client");
|
|
Packit Service |
db8eaa |
if (ret < 0)
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
} else {
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
ret = snd_pcm_open_slave(&spcm, root, sconf, stream,
|
|
Packit Service |
db8eaa |
mode | SND_PCM_NONBLOCK |
|
|
Packit Service |
db8eaa |
SND_PCM_APPEND,
|
|
Packit Service |
db8eaa |
NULL);
|
|
Packit Service |
db8eaa |
if (ret < 0) {
|
|
Packit Service |
db8eaa |
/* all other streams have been closed;
|
|
Packit Service |
db8eaa |
* retry as the first instance
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
if (ret == -EBADFD) {
|
|
Packit Service |
db8eaa |
first_instance = 1;
|
|
Packit Service |
db8eaa |
goto retry;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
SNDERR("unable to open slave");
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (snd_pcm_type(spcm) != SND_PCM_TYPE_HW) {
|
|
Packit Service |
db8eaa |
SNDERR("dsnoop plugin can be only connected to hw plugin");
|
|
Packit Service |
db8eaa |
ret = -EINVAL;
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
ret = snd_pcm_direct_initialize_secondary_slave(dsnoop, spcm, params);
|
|
Packit Service |
db8eaa |
if (ret < 0) {
|
|
Packit Service |
db8eaa |
SNDERR("unable to initialize slave");
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
dsnoop->spcm = spcm;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
ret = snd_pcm_direct_initialize_poll_fd(dsnoop);
|
|
Packit Service |
db8eaa |
if (ret < 0) {
|
|
Packit Service |
db8eaa |
SNDERR("unable to initialize poll_fd");
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
pcm->poll_fd = dsnoop->poll_fd;
|
|
Packit Service |
db8eaa |
pcm->poll_events = POLLIN; /* it's different than other plugins */
|
|
Packit Service |
db8eaa |
pcm->tstamp_type = spcm->tstamp_type;
|
|
Packit Service |
db8eaa |
pcm->mmap_rw = 1;
|
|
Packit Service |
db8eaa |
snd_pcm_set_hw_ptr(pcm, &dsnoop->hw_ptr, -1, 0);
|
|
Packit Service |
db8eaa |
snd_pcm_set_appl_ptr(pcm, &dsnoop->appl_ptr, -1, 0);
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
if (dsnoop->channels == UINT_MAX)
|
|
Packit Service |
db8eaa |
dsnoop->channels = dsnoop->shmptr->s.channels;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
snd_pcm_direct_semaphore_up(dsnoop, DIRECT_IPC_SEM_CLIENT);
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
*pcmp = pcm;
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
_err:
|
|
Packit Service |
db8eaa |
if (dsnoop->timer)
|
|
Packit Service |
db8eaa |
snd_timer_close(dsnoop->timer);
|
|
Packit Service |
db8eaa |
if (dsnoop->server)
|
|
Packit Service |
db8eaa |
snd_pcm_direct_server_discard(dsnoop);
|
|
Packit Service |
db8eaa |
if (dsnoop->client)
|
|
Packit Service |
db8eaa |
snd_pcm_direct_client_discard(dsnoop);
|
|
Packit Service |
db8eaa |
if (spcm)
|
|
Packit Service |
db8eaa |
snd_pcm_close(spcm);
|
|
Packit Service |
db8eaa |
if ((dsnoop->shmid >= 0) && (snd_pcm_direct_shm_discard(dsnoop))) {
|
|
Packit Service |
db8eaa |
if (snd_pcm_direct_semaphore_discard(dsnoop))
|
|
Packit Service |
db8eaa |
snd_pcm_direct_semaphore_final(dsnoop, DIRECT_IPC_SEM_CLIENT);
|
|
Packit Service |
db8eaa |
} else
|
|
Packit Service |
db8eaa |
snd_pcm_direct_semaphore_up(dsnoop, DIRECT_IPC_SEM_CLIENT);
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
_err_nosem:
|
|
Packit Service |
db8eaa |
if (dsnoop) {
|
|
Packit Service |
db8eaa |
free(dsnoop->bindings);
|
|
Packit Service |
db8eaa |
free(dsnoop);
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (pcm)
|
|
Packit Service |
db8eaa |
snd_pcm_free(pcm);
|
|
Packit Service |
db8eaa |
return ret;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/*! \page pcm_plugins
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
\section pcm_plugins_dsnoop Plugin: dsnoop
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
This plugin splits one capture stream to more.
|
|
Packit Service |
db8eaa |
It works the reverse way of \ref pcm_plugins_dmix "dmix plugin",
|
|
Packit Service |
db8eaa |
reading the shared capture buffer from many clients concurrently.
|
|
Packit Service |
db8eaa |
The meaning of parameters below are almost identical with
|
|
Packit Service |
db8eaa |
dmix plugin.
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
\code
|
|
Packit Service |
db8eaa |
pcm.name {
|
|
Packit Service |
db8eaa |
type dsnoop # Direct snoop
|
|
Packit Service |
db8eaa |
ipc_key INT # unique IPC key
|
|
Packit Service |
db8eaa |
ipc_key_add_uid BOOL # add current uid to unique IPC key
|
|
Packit Service |
db8eaa |
ipc_perm INT # IPC permissions (octal, default 0600)
|
|
Packit Service |
db8eaa |
hw_ptr_alignment STR # Slave application and hw pointer alignment type
|
|
Packit Service |
db8eaa |
# STR can be one of the below strings :
|
|
Packit Service |
db8eaa |
# no
|
|
Packit Service |
db8eaa |
# roundup
|
|
Packit Service |
db8eaa |
# rounddown
|
|
Packit Service |
db8eaa |
# auto (default)
|
|
Packit Service |
db8eaa |
tstamp_type STR # timestamp type
|
|
Packit Service |
db8eaa |
# STR can be one of the below strings :
|
|
Packit Service |
db8eaa |
# default, gettimeofday, monotonic, monotonic_raw
|
|
Packit Service |
db8eaa |
slave STR
|
|
Packit Service |
db8eaa |
# or
|
|
Packit Service |
db8eaa |
slave { # Slave definition
|
|
Packit Service |
db8eaa |
pcm STR # slave PCM name
|
|
Packit Service |
db8eaa |
# or
|
|
Packit Service |
db8eaa |
pcm { } # slave PCM definition
|
|
Packit Service |
db8eaa |
format STR # format definition
|
|
Packit Service |
db8eaa |
rate INT # rate definition
|
|
Packit Service |
db8eaa |
channels INT
|
|
Packit Service |
db8eaa |
period_time INT # in usec
|
|
Packit Service |
db8eaa |
# or
|
|
Packit Service |
db8eaa |
period_size INT # in frames
|
|
Packit Service |
db8eaa |
buffer_time INT # in usec
|
|
Packit Service |
db8eaa |
# or
|
|
Packit Service |
db8eaa |
buffer_size INT # in frames
|
|
Packit Service |
db8eaa |
periods INT # when buffer_size or buffer_time is not specified
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
bindings { # note: this is client independent!!!
|
|
Packit Service |
db8eaa |
N INT # maps slave channel to client channel N
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
slowptr BOOL # slow but more precise pointer updates
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
\endcode
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
hw_ptr_alignment specifies slave application and hw
|
|
Packit Service |
db8eaa |
pointer alignment type. By default hw_ptr_alignment is auto. Below are
|
|
Packit Service |
db8eaa |
the possible configurations:
|
|
Packit Service |
db8eaa |
- no: minimal latency with minimal frames dropped at startup. But
|
|
Packit Service |
db8eaa |
wakeup of application (return from snd_pcm_wait() or poll()) can
|
|
Packit Service |
db8eaa |
take up to 2 * period.
|
|
Packit Service |
db8eaa |
- roundup: It is guaranteed that all frames will be played at
|
|
Packit Service |
db8eaa |
startup. But the latency will increase upto period-1 frames.
|
|
Packit Service |
db8eaa |
- rounddown: It is guaranteed that a wakeup will happen for each
|
|
Packit Service |
db8eaa |
period and frames can be written from application. But on startup
|
|
Packit Service |
db8eaa |
upto period-1 frames will be dropped.
|
|
Packit Service |
db8eaa |
- auto: Selects the best approach depending on the used period and
|
|
Packit Service |
db8eaa |
buffer size.
|
|
Packit Service |
db8eaa |
If the application buffer size is < 2 * application period,
|
|
Packit Service |
db8eaa |
"roundup" will be selected to avoid over runs. If the slave_period
|
|
Packit Service |
db8eaa |
is < 10ms we could expect that there are low latency
|
|
Packit Service |
db8eaa |
requirements. Therefore "rounddown" will be chosen to avoid long
|
|
Packit Service |
db8eaa |
wakeup times. Else "no" will be chosen.
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
\subsection pcm_plugins_dsnoop_funcref Function reference
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
snd_pcm_dsnoop_open()
|
|
Packit Service |
db8eaa |
_snd_pcm_dsnoop_open()
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* \brief Creates a new dsnoop PCM
|
|
Packit Service |
db8eaa |
* \param pcmp Returns created PCM handle
|
|
Packit Service |
db8eaa |
* \param name Name of PCM
|
|
Packit Service |
db8eaa |
* \param root Root configuration node
|
|
Packit Service |
db8eaa |
* \param conf Configuration node with dsnoop PCM description
|
|
Packit Service |
db8eaa |
* \param stream PCM Stream
|
|
Packit Service |
db8eaa |
* \param mode PCM Mode
|
|
Packit Service |
db8eaa |
* \warning Using of this function might be dangerous in the sense
|
|
Packit Service |
db8eaa |
* of compatibility reasons. The prototype might be freely
|
|
Packit Service |
db8eaa |
* changed in future.
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
int _snd_pcm_dsnoop_open(snd_pcm_t **pcmp, const char *name,
|
|
Packit Service |
db8eaa |
snd_config_t *root, snd_config_t *conf,
|
|
Packit Service |
db8eaa |
snd_pcm_stream_t stream, int mode)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_config_t *sconf;
|
|
Packit Service |
db8eaa |
struct slave_params params;
|
|
Packit Service |
db8eaa |
struct snd_pcm_direct_open_conf dopen;
|
|
Packit Service |
db8eaa |
int bsize, psize;
|
|
Packit Service |
db8eaa |
int err;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
err = snd_pcm_direct_parse_open_conf(root, conf, stream, &dopen);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/* the default settings, it might be invalid for some hardware */
|
|
Packit Service |
db8eaa |
params.format = SND_PCM_FORMAT_S16;
|
|
Packit Service |
db8eaa |
params.rate = 48000;
|
|
Packit Service |
db8eaa |
params.channels = 2;
|
|
Packit Service |
db8eaa |
params.period_time = -1;
|
|
Packit Service |
db8eaa |
params.buffer_time = -1;
|
|
Packit Service |
db8eaa |
bsize = psize = -1;
|
|
Packit Service |
db8eaa |
params.periods = 3;
|
|
Packit Service |
db8eaa |
err = snd_pcm_slave_conf(root, dopen.slave, &sconf, 8,
|
|
Packit Service |
db8eaa |
SND_PCM_HW_PARAM_FORMAT, SCONF_UNCHANGED, ¶ms.format,
|
|
Packit Service |
db8eaa |
SND_PCM_HW_PARAM_RATE, 0, ¶ms.rate,
|
|
Packit Service |
db8eaa |
SND_PCM_HW_PARAM_CHANNELS, 0, ¶ms.channels,
|
|
Packit Service |
db8eaa |
SND_PCM_HW_PARAM_PERIOD_TIME, 0, ¶ms.period_time,
|
|
Packit Service |
db8eaa |
SND_PCM_HW_PARAM_BUFFER_TIME, 0, ¶ms.buffer_time,
|
|
Packit Service |
db8eaa |
SND_PCM_HW_PARAM_PERIOD_SIZE, 0, &psize,
|
|
Packit Service |
db8eaa |
SND_PCM_HW_PARAM_BUFFER_SIZE, 0, &bsize,
|
|
Packit Service |
db8eaa |
SND_PCM_HW_PARAM_PERIODS, 0, ¶ms.periods);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/* set a reasonable default */
|
|
Packit Service |
db8eaa |
if (psize == -1 && params.period_time == -1)
|
|
Packit Service |
db8eaa |
params.period_time = 125000; /* 0.125 seconds */
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
if (params.format == -2)
|
|
Packit Service |
db8eaa |
params.format = SND_PCM_FORMAT_UNKNOWN;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
params.period_size = psize;
|
|
Packit Service |
db8eaa |
params.buffer_size = bsize;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
err = snd_pcm_dsnoop_open(pcmp, name, &dopen, ¶ms,
|
|
Packit Service |
db8eaa |
root, sconf, stream, mode);
|
|
Packit Service |
db8eaa |
snd_config_delete(sconf);
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
#ifndef DOC_HIDDEN
|
|
Packit Service |
db8eaa |
SND_DLSYM_BUILD_VERSION(_snd_pcm_dsnoop_open, SND_PCM_DLSYM_VERSION);
|
|
Packit Service |
db8eaa |
#endif
|