Blame src/pcm/pcm_dsnoop.c

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