Blame src/pcm/pcm_dshare.c

Packit 4a16fb
/**
Packit 4a16fb
 * \file pcm/pcm_dshare.c
Packit 4a16fb
 * \ingroup PCM_Plugins
Packit 4a16fb
 * \brief PCM Direct Sharing of Channels Plugin Interface
Packit 4a16fb
 * \author Jaroslav Kysela <perex@perex.cz>
Packit 4a16fb
 * \date 2003
Packit 4a16fb
 */
Packit 4a16fb
/*
Packit 4a16fb
 *  PCM - Direct Sharing of Channels
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_dshare = "";
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
/* start is pending - this state happens when rate plugin does a delayed commit */
Packit 4a16fb
#define STATE_RUN_PENDING	1024
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
static void do_silence(snd_pcm_t *pcm)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_direct_t *dshare = pcm->private_data;
Packit 4a16fb
	const snd_pcm_channel_area_t *dst_areas;
Packit 4a16fb
	unsigned int chn, dchn, channels;
Packit 4a16fb
	snd_pcm_format_t format;
Packit 4a16fb
Packit 4a16fb
	dst_areas = snd_pcm_mmap_areas(dshare->spcm);
Packit 4a16fb
	channels = dshare->channels;
Packit 4a16fb
	format = dshare->shmptr->s.format;
Packit 4a16fb
	for (chn = 0; chn < channels; chn++) {
Packit 4a16fb
		dchn = dshare->bindings ? dshare->bindings[chn] : chn;
Packit 4a16fb
		if (dchn != UINT_MAX)
Packit 4a16fb
			snd_pcm_area_silence(&dst_areas[dchn], 0,
Packit 4a16fb
					     dshare->shmptr->s.buffer_size, format);
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void share_areas(snd_pcm_direct_t *dshare,
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, dchn, channels;
Packit 4a16fb
	snd_pcm_format_t format;
Packit 4a16fb
Packit 4a16fb
	channels = dshare->channels;
Packit 4a16fb
	format = dshare->shmptr->s.format;
Packit 4a16fb
	if (dshare->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
			dchn = dshare->bindings ? dshare->bindings[chn] : chn;
Packit 4a16fb
			if (dchn != UINT_MAX)
Packit 4a16fb
				snd_pcm_area_copy(&dst_areas[dchn], dst_ofs,
Packit 4a16fb
						  &src_areas[chn], src_ofs, size, format);
Packit 4a16fb
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_dshare_sync_area(snd_pcm_t *pcm)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_direct_t *dshare = pcm->private_data;
Packit 4a16fb
	snd_pcm_uframes_t slave_hw_ptr, slave_appl_ptr, slave_size;
Packit 4a16fb
	snd_pcm_uframes_t appl_ptr, size;
Packit 4a16fb
	const snd_pcm_channel_area_t *src_areas, *dst_areas;
Packit 4a16fb
	
Packit 4a16fb
	/* calculate the size to transfer */
Packit 4a16fb
	size = dshare->appl_ptr - dshare->last_appl_ptr;
Packit 4a16fb
	if (! size)
Packit 4a16fb
		return;
Packit 4a16fb
	slave_hw_ptr = dshare->slave_hw_ptr;
Packit 4a16fb
	/* don't write on the last active period - this area may be cleared
Packit 4a16fb
	 * by the driver during write operation...
Packit 4a16fb
	 */
Packit 4a16fb
	slave_hw_ptr -= slave_hw_ptr % dshare->slave_period_size;
Packit 4a16fb
	slave_hw_ptr += dshare->slave_buffer_size;
Packit 4a16fb
	if (slave_hw_ptr >= dshare->slave_boundary)
Packit 4a16fb
		slave_hw_ptr -= dshare->slave_boundary;
Packit 4a16fb
	if (slave_hw_ptr < dshare->slave_appl_ptr)
Packit 4a16fb
		slave_size = slave_hw_ptr + (dshare->slave_boundary - dshare->slave_appl_ptr);
Packit 4a16fb
	else
Packit 4a16fb
		slave_size = slave_hw_ptr - dshare->slave_appl_ptr;
Packit 4a16fb
	if (slave_size < size)
Packit 4a16fb
		size = slave_size;
Packit 4a16fb
	if (! size)
Packit 4a16fb
		return;
Packit 4a16fb
Packit 4a16fb
	/* add sample areas here */
Packit 4a16fb
	src_areas = snd_pcm_mmap_areas(pcm);
Packit 4a16fb
	dst_areas = snd_pcm_mmap_areas(dshare->spcm);
Packit 4a16fb
	appl_ptr = dshare->last_appl_ptr % pcm->buffer_size;
Packit 4a16fb
	dshare->last_appl_ptr += size;
Packit 4a16fb
	dshare->last_appl_ptr %= pcm->boundary;
Packit 4a16fb
	slave_appl_ptr = dshare->slave_appl_ptr % dshare->slave_buffer_size;
Packit 4a16fb
	dshare->slave_appl_ptr += size;
Packit 4a16fb
	dshare->slave_appl_ptr %= dshare->slave_boundary;
Packit 4a16fb
	for (;;) {
Packit 4a16fb
		snd_pcm_uframes_t transfer = size;
Packit 4a16fb
		if (appl_ptr + transfer > pcm->buffer_size)
Packit 4a16fb
			transfer = pcm->buffer_size - appl_ptr;
Packit 4a16fb
		if (slave_appl_ptr + transfer > dshare->slave_buffer_size)
Packit 4a16fb
			transfer = dshare->slave_buffer_size - slave_appl_ptr;
Packit 4a16fb
		share_areas(dshare, src_areas, dst_areas, appl_ptr, slave_appl_ptr, transfer);
Packit 4a16fb
		size -= transfer;
Packit 4a16fb
		if (! size)
Packit 4a16fb
			break;
Packit 4a16fb
		slave_appl_ptr += transfer;
Packit 4a16fb
		slave_appl_ptr %= dshare->slave_buffer_size;
Packit 4a16fb
		appl_ptr += transfer;
Packit 4a16fb
		appl_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_dshare_sync_ptr0(snd_pcm_t *pcm, snd_pcm_uframes_t slave_hw_ptr)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_direct_t *dshare = pcm->private_data;
Packit 4a16fb
	snd_pcm_uframes_t old_slave_hw_ptr, avail;
Packit 4a16fb
	snd_pcm_sframes_t diff;
Packit 4a16fb
Packit 4a16fb
	old_slave_hw_ptr = dshare->slave_hw_ptr;
Packit 4a16fb
	dshare->slave_hw_ptr = 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 (dshare->state != SND_PCM_STATE_RUNNING &&
Packit 4a16fb
	    dshare->state != SND_PCM_STATE_DRAINING)
Packit 4a16fb
		/* not really started yet - don't update hw_ptr */
Packit 4a16fb
		return 0;
Packit 4a16fb
	if (diff < 0) {
Packit 4a16fb
		slave_hw_ptr += dshare->slave_boundary;
Packit 4a16fb
		diff = slave_hw_ptr - old_slave_hw_ptr;
Packit 4a16fb
	}
Packit 4a16fb
	dshare->hw_ptr += diff;
Packit 4a16fb
	dshare->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
	avail = snd_pcm_mmap_playback_avail(pcm);
Packit 4a16fb
	if (avail > dshare->avail_max)
Packit 4a16fb
		dshare->avail_max = avail;
Packit 4a16fb
	if (avail >= pcm->stop_threshold) {
Packit 4a16fb
		snd_timer_stop(dshare->timer);
Packit 4a16fb
		do_silence(pcm);
Packit 4a16fb
		gettimestamp(&dshare->trigger_tstamp, pcm->tstamp_type);
Packit 4a16fb
		if (dshare->state == SND_PCM_STATE_RUNNING) {
Packit 4a16fb
			dshare->state = SND_PCM_STATE_XRUN;
Packit 4a16fb
			return -EPIPE;
Packit 4a16fb
		}
Packit 4a16fb
		dshare->state = SND_PCM_STATE_SETUP;
Packit 4a16fb
		/* clear queue to remove pending poll events */
Packit 4a16fb
		snd_pcm_direct_clear_timer_queue(dshare);
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_pcm_dshare_sync_ptr(snd_pcm_t *pcm)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_direct_t *dshare = pcm->private_data;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	switch (snd_pcm_state(dshare->spcm)) {
Packit 4a16fb
	case SND_PCM_STATE_DISCONNECTED:
Packit 4a16fb
		dshare->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(dshare)) < 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(dshare, pcm))
Packit 4a16fb
		return -EPIPE;
Packit 4a16fb
	if (dshare->slowptr)
Packit 4a16fb
		snd_pcm_hwsync(dshare->spcm);
Packit 4a16fb
Packit 4a16fb
	return snd_pcm_dshare_sync_ptr0(pcm, *dshare->spcm->hw.ptr);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 *  plugin implementation
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
static snd_pcm_state_t snd_pcm_dshare_state(snd_pcm_t *pcm);
Packit 4a16fb
Packit 4a16fb
static int snd_pcm_dshare_status(snd_pcm_t *pcm, snd_pcm_status_t * status)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_direct_t *dshare = pcm->private_data;
Packit 4a16fb
Packit 4a16fb
	memset(status, 0, sizeof(*status));
Packit 4a16fb
	snd_pcm_status(dshare->spcm, status);
Packit 4a16fb
Packit 4a16fb
	switch (dshare->state) {
Packit 4a16fb
	case SNDRV_PCM_STATE_DRAINING:
Packit 4a16fb
	case SNDRV_PCM_STATE_RUNNING:
Packit 4a16fb
		snd_pcm_dshare_sync_ptr0(pcm, status->hw_ptr);
Packit 4a16fb
		status->delay += snd_pcm_mmap_playback_delay(pcm)
Packit 4a16fb
				+ status->avail - dshare->spcm->buffer_size;
Packit 4a16fb
		break;
Packit 4a16fb
	default:
Packit 4a16fb
		break;
Packit 4a16fb
	}
Packit 4a16fb
	status->state = snd_pcm_dshare_state(pcm);
Packit 4a16fb
	status->trigger_tstamp = dshare->trigger_tstamp;
Packit 4a16fb
	status->avail = snd_pcm_mmap_playback_avail(pcm);
Packit 4a16fb
	status->avail_max = status->avail > dshare->avail_max ? status->avail : dshare->avail_max;
Packit 4a16fb
	dshare->avail_max = 0;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static snd_pcm_state_t snd_pcm_dshare_state(snd_pcm_t *pcm)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_direct_t *dshare = pcm->private_data;
Packit 4a16fb
	int err;
Packit 4a16fb
	snd_pcm_state_t state;
Packit 4a16fb
	state = snd_pcm_state(dshare->spcm);
Packit 4a16fb
	switch (state) {
Packit 4a16fb
	case SND_PCM_STATE_SUSPENDED:
Packit 4a16fb
	case SND_PCM_STATE_DISCONNECTED:
Packit 4a16fb
		dshare->state = state;
Packit 4a16fb
		return state;
Packit 4a16fb
	case SND_PCM_STATE_XRUN:
Packit 4a16fb
		if ((err = snd_pcm_direct_slave_recover(dshare)) < 0)
Packit 4a16fb
			return err;
Packit 4a16fb
		break;
Packit 4a16fb
	default:
Packit 4a16fb
		break;
Packit 4a16fb
	}
Packit 4a16fb
	snd_pcm_direct_client_chk_xrun(dshare, pcm);
Packit 4a16fb
	if (dshare->state == STATE_RUN_PENDING)
Packit 4a16fb
		return SNDRV_PCM_STATE_RUNNING;
Packit 4a16fb
	return dshare->state;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_pcm_dshare_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delayp)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_direct_t *dshare = pcm->private_data;
Packit 4a16fb
	int err;
Packit 4a16fb
	
Packit 4a16fb
	switch (dshare->state) {
Packit 4a16fb
	case SNDRV_PCM_STATE_DRAINING:
Packit 4a16fb
	case SNDRV_PCM_STATE_RUNNING:
Packit 4a16fb
		err = snd_pcm_dshare_sync_ptr(pcm);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			return err;
Packit 4a16fb
		/* fallthru */
Packit 4a16fb
	case SNDRV_PCM_STATE_PREPARED:
Packit 4a16fb
	case SNDRV_PCM_STATE_SUSPENDED:
Packit 4a16fb
	case STATE_RUN_PENDING:
Packit 4a16fb
		*delayp = snd_pcm_mmap_playback_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_dshare_hwsync(snd_pcm_t *pcm)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_direct_t *dshare = pcm->private_data;
Packit 4a16fb
Packit 4a16fb
	switch(dshare->state) {
Packit 4a16fb
	case SNDRV_PCM_STATE_DRAINING:
Packit 4a16fb
	case SNDRV_PCM_STATE_RUNNING:
Packit 4a16fb
		return snd_pcm_dshare_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_dshare_reset(snd_pcm_t *pcm)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_direct_t *dshare = pcm->private_data;
Packit 4a16fb
	dshare->hw_ptr %= pcm->period_size;
Packit 4a16fb
	dshare->appl_ptr = dshare->last_appl_ptr = dshare->hw_ptr;
Packit 4a16fb
	dshare->slave_appl_ptr = dshare->slave_hw_ptr = *dshare->spcm->hw.ptr;
Packit 4a16fb
	snd_pcm_direct_reset_slave_ptr(pcm, dshare);
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_pcm_dshare_start_timer(snd_pcm_t *pcm, snd_pcm_direct_t *dshare)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	snd_pcm_hwsync(dshare->spcm);
Packit 4a16fb
	dshare->slave_appl_ptr = dshare->slave_hw_ptr = *dshare->spcm->hw.ptr;
Packit 4a16fb
	snd_pcm_direct_reset_slave_ptr(pcm, dshare);
Packit 4a16fb
	err = snd_timer_start(dshare->timer);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	dshare->state = SND_PCM_STATE_RUNNING;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_pcm_dshare_start(snd_pcm_t *pcm)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_direct_t *dshare = pcm->private_data;
Packit 4a16fb
	snd_pcm_sframes_t avail;
Packit 4a16fb
	int err;
Packit 4a16fb
	
Packit 4a16fb
	if (dshare->state != SND_PCM_STATE_PREPARED)
Packit 4a16fb
		return -EBADFD;
Packit 4a16fb
	avail = snd_pcm_mmap_playback_hw_avail(pcm);
Packit 4a16fb
	if (avail == 0)
Packit 4a16fb
		dshare->state = STATE_RUN_PENDING;
Packit 4a16fb
	else if (avail < 0)
Packit 4a16fb
		return 0;
Packit 4a16fb
	else {
Packit 4a16fb
		err = snd_pcm_dshare_start_timer(pcm, dshare);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			return err;
Packit 4a16fb
		snd_pcm_dshare_sync_area(pcm);
Packit 4a16fb
	}
Packit 4a16fb
	gettimestamp(&dshare->trigger_tstamp, pcm->tstamp_type);
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_pcm_dshare_drop(snd_pcm_t *pcm)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_direct_t *dshare = pcm->private_data;
Packit 4a16fb
	if (dshare->state == SND_PCM_STATE_OPEN)
Packit 4a16fb
		return -EBADFD;
Packit 4a16fb
	dshare->state = SND_PCM_STATE_SETUP;
Packit 4a16fb
	snd_pcm_direct_timer_stop(dshare);
Packit 4a16fb
	do_silence(pcm);
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* locked version */
Packit 4a16fb
static int __snd_pcm_dshare_drain(snd_pcm_t *pcm)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_direct_t *dshare = pcm->private_data;
Packit 4a16fb
	snd_pcm_uframes_t stop_threshold;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	switch (snd_pcm_state(dshare->spcm)) {
Packit 4a16fb
	case SND_PCM_STATE_SUSPENDED:
Packit 4a16fb
		return -ESTRPIPE;
Packit 4a16fb
	default:
Packit 4a16fb
		break;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	if (dshare->state == SND_PCM_STATE_OPEN)
Packit 4a16fb
		return -EBADFD;
Packit 4a16fb
	if (pcm->mode & SND_PCM_NONBLOCK)
Packit 4a16fb
		return -EAGAIN;
Packit 4a16fb
	if (dshare->state == SND_PCM_STATE_PREPARED) {
Packit 4a16fb
		if (snd_pcm_mmap_playback_hw_avail(pcm) > 0)
Packit 4a16fb
			snd_pcm_dshare_start(pcm);
Packit 4a16fb
		else {
Packit 4a16fb
			snd_pcm_dshare_drop(pcm);
Packit 4a16fb
			return 0;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	if (dshare->state == SND_PCM_STATE_XRUN) {
Packit 4a16fb
		snd_pcm_dshare_drop(pcm);
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
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
	dshare->state = SND_PCM_STATE_DRAINING;
Packit 4a16fb
	do {
Packit 4a16fb
		err = snd_pcm_dshare_sync_ptr(pcm);
Packit 4a16fb
		if (err < 0) {
Packit 4a16fb
			snd_pcm_dshare_drop(pcm);
Packit 4a16fb
			break;
Packit 4a16fb
		}
Packit 4a16fb
		if (dshare->state == SND_PCM_STATE_DRAINING) {
Packit 4a16fb
			snd_pcm_dshare_sync_area(pcm);
Packit 4a16fb
			snd_pcm_wait_nocheck(pcm, -1);
Packit 4a16fb
			snd_pcm_direct_clear_timer_queue(dshare); /* force poll to wait */
Packit 4a16fb
Packit 4a16fb
			switch (snd_pcm_state(dshare->spcm)) {
Packit 4a16fb
			case SND_PCM_STATE_SUSPENDED:
Packit 4a16fb
				return -ESTRPIPE;
Packit 4a16fb
			default:
Packit 4a16fb
				break;
Packit 4a16fb
			}
Packit 4a16fb
		}
Packit 4a16fb
	} while (dshare->state == SND_PCM_STATE_DRAINING);
Packit 4a16fb
	pcm->stop_threshold = stop_threshold;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_pcm_dshare_drain(snd_pcm_t *pcm)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	snd_pcm_lock(pcm);
Packit 4a16fb
	err = __snd_pcm_dshare_drain(pcm);
Packit 4a16fb
	snd_pcm_unlock(pcm);
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_pcm_dshare_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_dshare_rewindable(snd_pcm_t *pcm)
Packit 4a16fb
{
Packit 4a16fb
	return snd_pcm_mmap_playback_hw_rewindable(pcm);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static snd_pcm_sframes_t snd_pcm_dshare_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_dshare_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_dshare_forwardable(snd_pcm_t *pcm)
Packit 4a16fb
{
Packit 4a16fb
	return snd_pcm_mmap_playback_avail(pcm);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static snd_pcm_sframes_t snd_pcm_dshare_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_dshare_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_dshare_readi(snd_pcm_t *pcm ATTRIBUTE_UNUSED, 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_dshare_readn(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_dshare_close(snd_pcm_t *pcm)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_direct_t *dshare = pcm->private_data;
Packit 4a16fb
Packit 4a16fb
	if (dshare->timer)
Packit 4a16fb
		snd_timer_close(dshare->timer);
Packit 4a16fb
	if (dshare->bindings)
Packit 4a16fb
		do_silence(pcm);
Packit 4a16fb
	snd_pcm_direct_semaphore_down(dshare, DIRECT_IPC_SEM_CLIENT);
Packit 4a16fb
	dshare->shmptr->u.dshare.chn_mask &= ~dshare->u.dshare.chn_mask;
Packit 4a16fb
	snd_pcm_close(dshare->spcm);
Packit 4a16fb
 	if (dshare->server)
Packit 4a16fb
 		snd_pcm_direct_server_discard(dshare);
Packit 4a16fb
 	if (dshare->client)
Packit 4a16fb
 		snd_pcm_direct_client_discard(dshare);
Packit 4a16fb
	if (snd_pcm_direct_shm_discard(dshare)) {
Packit 4a16fb
		if (snd_pcm_direct_semaphore_discard(dshare))
Packit 4a16fb
			snd_pcm_direct_semaphore_final(dshare, DIRECT_IPC_SEM_CLIENT);
Packit 4a16fb
	} else
Packit 4a16fb
		snd_pcm_direct_semaphore_final(dshare, DIRECT_IPC_SEM_CLIENT);
Packit 4a16fb
	free(dshare->bindings);
Packit 4a16fb
	pcm->private_data = NULL;
Packit 4a16fb
	free(dshare);
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static snd_pcm_sframes_t snd_pcm_dshare_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 *dshare = pcm->private_data;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	switch (snd_pcm_state(dshare->spcm)) {
Packit 4a16fb
	case SND_PCM_STATE_XRUN:
Packit 4a16fb
		if ((err = snd_pcm_direct_slave_recover(dshare)) < 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(dshare, pcm))
Packit 4a16fb
		return -EPIPE;
Packit 4a16fb
	if (! size)
Packit 4a16fb
		return 0;
Packit 4a16fb
	snd_pcm_mmap_appl_forward(pcm, size);
Packit 4a16fb
	if (dshare->state == STATE_RUN_PENDING) {
Packit 4a16fb
		err = snd_pcm_dshare_start_timer(pcm, dshare);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			return err;
Packit 4a16fb
	} else if (dshare->state == SND_PCM_STATE_RUNNING ||
Packit 4a16fb
		   dshare->state == SND_PCM_STATE_DRAINING) {
Packit 4a16fb
		if ((err = snd_pcm_dshare_sync_ptr(pcm)) < 0)
Packit 4a16fb
			return err;
Packit 4a16fb
	}
Packit 4a16fb
	if (dshare->state == SND_PCM_STATE_RUNNING ||
Packit 4a16fb
	    dshare->state == SND_PCM_STATE_DRAINING) {
Packit 4a16fb
		/* ok, we commit the changes after the validation of area */
Packit 4a16fb
		/* it's intended, although the result might be crappy */
Packit 4a16fb
		snd_pcm_dshare_sync_area(pcm);
Packit 4a16fb
		/* clear timer queue to avoid a bogus return from poll */
Packit 4a16fb
		if (snd_pcm_mmap_playback_avail(pcm) < pcm->avail_min)
Packit 4a16fb
			snd_pcm_direct_clear_timer_queue(dshare);
Packit 4a16fb
	}
Packit 4a16fb
	return size;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static snd_pcm_sframes_t snd_pcm_dshare_avail_update(snd_pcm_t *pcm)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_direct_t *dshare = pcm->private_data;
Packit 4a16fb
	int err;
Packit 4a16fb
	
Packit 4a16fb
	if (dshare->state == SND_PCM_STATE_RUNNING ||
Packit 4a16fb
	    dshare->state == SND_PCM_STATE_DRAINING) {
Packit 4a16fb
		if ((err = snd_pcm_dshare_sync_ptr(pcm)) < 0)
Packit 4a16fb
			return err;
Packit 4a16fb
	}
Packit 4a16fb
	if (dshare->state == SND_PCM_STATE_XRUN)
Packit 4a16fb
		return -EPIPE;
Packit 4a16fb
Packit 4a16fb
	return snd_pcm_mmap_playback_avail(pcm);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_pcm_dshare_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 *dshare = pcm->private_data;
Packit 4a16fb
	snd_pcm_uframes_t avail1;
Packit 4a16fb
	int ok = 0;
Packit 4a16fb
	
Packit 4a16fb
	while (1) {
Packit 4a16fb
		if (dshare->state == SND_PCM_STATE_RUNNING ||
Packit 4a16fb
		    dshare->state == SND_PCM_STATE_DRAINING)
Packit 4a16fb
			snd_pcm_dshare_sync_ptr(pcm);
Packit 4a16fb
		avail1 = snd_pcm_mmap_playback_avail(pcm);
Packit 4a16fb
		if (ok && *avail == avail1)
Packit 4a16fb
			break;
Packit 4a16fb
		*avail = avail1;
Packit 4a16fb
		*tstamp = snd_pcm_hw_fast_tstamp(dshare->spcm);
Packit 4a16fb
		ok = 1;
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void snd_pcm_dshare_dump(snd_pcm_t *pcm, snd_output_t *out)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_direct_t *dshare = pcm->private_data;
Packit 4a16fb
Packit 4a16fb
	snd_output_printf(out, "Direct Share 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 (dshare->spcm)
Packit 4a16fb
		snd_pcm_dump(dshare->spcm, out);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static const snd_pcm_ops_t snd_pcm_dshare_dummy_ops = {
Packit 4a16fb
	.close = snd_pcm_dshare_close,
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
static const snd_pcm_fast_ops_t snd_pcm_dshare_fast_dummy_ops;
Packit 4a16fb
Packit 4a16fb
static const snd_pcm_ops_t snd_pcm_dshare_ops = {
Packit 4a16fb
	.close = snd_pcm_dshare_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_dshare_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
	.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_dshare_fast_ops = {
Packit 4a16fb
	.status = snd_pcm_dshare_status,
Packit 4a16fb
	.state = snd_pcm_dshare_state,
Packit 4a16fb
	.hwsync = snd_pcm_dshare_hwsync,
Packit 4a16fb
	.delay = snd_pcm_dshare_delay,
Packit 4a16fb
	.prepare = snd_pcm_direct_prepare,
Packit 4a16fb
	.reset = snd_pcm_dshare_reset,
Packit 4a16fb
	.start = snd_pcm_dshare_start,
Packit 4a16fb
	.drop = snd_pcm_dshare_drop,
Packit 4a16fb
	.drain = snd_pcm_dshare_drain,
Packit 4a16fb
	.pause = snd_pcm_dshare_pause,
Packit 4a16fb
	.rewindable = snd_pcm_dshare_rewindable,
Packit 4a16fb
	.rewind = snd_pcm_dshare_rewind,
Packit 4a16fb
	.forwardable = snd_pcm_dshare_forwardable,
Packit 4a16fb
	.forward = snd_pcm_dshare_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_mmap_writei,
Packit 4a16fb
	.writen = snd_pcm_mmap_writen,
Packit 4a16fb
	.readi = snd_pcm_dshare_readi,
Packit 4a16fb
	.readn = snd_pcm_dshare_readn,
Packit 4a16fb
	.avail_update = snd_pcm_dshare_avail_update,
Packit 4a16fb
	.mmap_commit = snd_pcm_dshare_mmap_commit,
Packit 4a16fb
	.htimestamp = snd_pcm_dshare_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 dshare 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_dshare_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 *dshare = NULL;
Packit 4a16fb
	int ret, first_instance;
Packit 4a16fb
	unsigned int chn;
Packit 4a16fb
	int fail_sem_loop = 10;
Packit 4a16fb
Packit 4a16fb
	assert(pcmp);
Packit 4a16fb
Packit 4a16fb
	if (stream != SND_PCM_STREAM_PLAYBACK) {
Packit 4a16fb
		SNDERR("The dshare plugin supports only playback stream");
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	dshare = calloc(1, sizeof(snd_pcm_direct_t));
Packit 4a16fb
	if (!dshare) {
Packit 4a16fb
		ret = -ENOMEM;
Packit 4a16fb
		goto _err_nosem;
Packit 4a16fb
	}
Packit 4a16fb
	
Packit 4a16fb
	ret = snd_pcm_direct_parse_bindings(dshare, params, opts->bindings);
Packit 4a16fb
	if (ret < 0)
Packit 4a16fb
		goto _err_nosem;
Packit 4a16fb
Packit 4a16fb
	dshare->ipc_key = opts->ipc_key;
Packit 4a16fb
	dshare->ipc_perm = opts->ipc_perm;
Packit 4a16fb
	dshare->ipc_gid = opts->ipc_gid;
Packit Service f36a15
	dshare->tstamp_type = opts->tstamp_type;
Packit 4a16fb
	dshare->semid = -1;
Packit 4a16fb
	dshare->shmid = -1;
Packit 4a16fb
Packit 4a16fb
	ret = snd_pcm_new(&pcm, dshare->type = SND_PCM_TYPE_DSHARE, 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(dshare);
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(dshare, DIRECT_IPC_SEM_CLIENT);
Packit 4a16fb
		if (ret < 0) {
Packit 4a16fb
			snd_pcm_direct_semaphore_discard(dshare);
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(dshare);
Packit 4a16fb
	if (ret < 0) {
Packit 4a16fb
		SNDERR("unable to create IPC shm instance");
Packit 4a16fb
		goto _err;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	if (!dshare->bindings) {
Packit 4a16fb
		pcm->ops = &snd_pcm_dshare_dummy_ops;
Packit 4a16fb
		pcm->fast_ops = &snd_pcm_dshare_fast_dummy_ops;
Packit 4a16fb
	} else {
Packit 4a16fb
		pcm->ops = &snd_pcm_dshare_ops;
Packit 4a16fb
		pcm->fast_ops = &snd_pcm_dshare_fast_ops;
Packit 4a16fb
	}
Packit 4a16fb
	pcm->private_data = dshare;
Packit 4a16fb
	dshare->state = SND_PCM_STATE_OPEN;
Packit 4a16fb
	dshare->slowptr = opts->slowptr;
Packit 4a16fb
	dshare->max_periods = opts->max_periods;
Packit 4a16fb
	dshare->var_periodsize = opts->var_periodsize;
Packit 4a16fb
	dshare->hw_ptr_alignment = opts->hw_ptr_alignment;
Packit 4a16fb
	dshare->sync_ptr = snd_pcm_dshare_sync_ptr;
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("dshare plugin can be only connected to hw plugin");
Packit 4a16fb
			goto _err;
Packit 4a16fb
		}
Packit 4a16fb
		
Packit 4a16fb
		ret = snd_pcm_direct_initialize_slave(dshare, spcm, params);
Packit 4a16fb
		if (ret < 0) {
Packit 4a16fb
			SNDERR("unable to initialize slave");
Packit 4a16fb
			goto _err;
Packit 4a16fb
		}
Packit 4a16fb
Packit 4a16fb
		dshare->spcm = spcm;
Packit 4a16fb
		
Packit 4a16fb
		if (dshare->shmptr->use_server) {
Packit 4a16fb
			ret = snd_pcm_direct_server_create(dshare);
Packit 4a16fb
			if (ret < 0) {
Packit 4a16fb
				SNDERR("unable to create server");
Packit 4a16fb
				goto _err;
Packit 4a16fb
			}
Packit 4a16fb
		}
Packit 4a16fb
Packit 4a16fb
		dshare->shmptr->type = spcm->type;
Packit 4a16fb
	} else {
Packit 4a16fb
		if (dshare->shmptr->use_server) {
Packit 4a16fb
			/* up semaphore to avoid deadlock */
Packit 4a16fb
			snd_pcm_direct_semaphore_up(dshare, DIRECT_IPC_SEM_CLIENT);
Packit 4a16fb
			ret = snd_pcm_direct_client_connect(dshare);
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(dshare, DIRECT_IPC_SEM_CLIENT);
Packit 4a16fb
			ret = snd_pcm_direct_open_secondary_client(&spcm, dshare, "dshare_client");
Packit 4a16fb
			if (ret < 0)
Packit 4a16fb
				goto _err;
Packit 4a16fb
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("dshare 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(dshare, 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
		dshare->spcm = spcm;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	for (chn = 0; dshare->bindings && (chn < dshare->channels); chn++) {
Packit 4a16fb
		unsigned int dchn = dshare->bindings ? dshare->bindings[chn] : chn;
Packit 4a16fb
		if (dchn != UINT_MAX)
Packit 4a16fb
			dshare->u.dshare.chn_mask |= (1ULL << dchn);
Packit 4a16fb
	}
Packit 4a16fb
	if (dshare->shmptr->u.dshare.chn_mask & dshare->u.dshare.chn_mask) {
Packit 4a16fb
		SNDERR("destination channel specified in bindings is already used");
Packit 4a16fb
		dshare->u.dshare.chn_mask = 0;
Packit 4a16fb
		ret = -EINVAL;
Packit 4a16fb
		goto _err;
Packit 4a16fb
	}
Packit 4a16fb
	dshare->shmptr->u.dshare.chn_mask |= dshare->u.dshare.chn_mask;
Packit 4a16fb
		
Packit 4a16fb
	ret = snd_pcm_direct_initialize_poll_fd(dshare);
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 = dshare->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, &dshare->hw_ptr, -1, 0);
Packit 4a16fb
	snd_pcm_set_appl_ptr(pcm, &dshare->appl_ptr, -1, 0);
Packit 4a16fb
	
Packit 4a16fb
	snd_pcm_direct_semaphore_up(dshare, DIRECT_IPC_SEM_CLIENT);
Packit 4a16fb
Packit 4a16fb
	*pcmp = pcm;
Packit 4a16fb
	return 0;
Packit 4a16fb
	
Packit 4a16fb
 _err:
Packit 4a16fb
	if (dshare->shmptr)
Packit 4a16fb
		dshare->shmptr->u.dshare.chn_mask &= ~dshare->u.dshare.chn_mask;
Packit 4a16fb
	if (dshare->timer)
Packit 4a16fb
		snd_timer_close(dshare->timer);
Packit 4a16fb
	if (dshare->server)
Packit 4a16fb
		snd_pcm_direct_server_discard(dshare);
Packit 4a16fb
	if (dshare->client)
Packit 4a16fb
		snd_pcm_direct_client_discard(dshare);
Packit 4a16fb
	if (spcm)
Packit 4a16fb
		snd_pcm_close(spcm);
Packit 4a16fb
	if ((dshare->shmid >= 0) && (snd_pcm_direct_shm_discard(dshare))) {
Packit 4a16fb
		if (snd_pcm_direct_semaphore_discard(dshare))
Packit 4a16fb
			snd_pcm_direct_semaphore_final(dshare, DIRECT_IPC_SEM_CLIENT);
Packit 4a16fb
	} else
Packit 4a16fb
		snd_pcm_direct_semaphore_up(dshare, DIRECT_IPC_SEM_CLIENT);
Packit 4a16fb
 _err_nosem:
Packit 4a16fb
	if (dshare) {
Packit 4a16fb
		free(dshare->bindings);
Packit 4a16fb
		free(dshare);
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_dshare Plugin: dshare
Packit 4a16fb
Packit 4a16fb
This plugin provides sharing channels.
Packit 4a16fb
Unlike \ref pcm_plugins_share "share plugin", this plugin doesn't need
Packit 4a16fb
the explicit server program but accesses the shared buffer concurrently
Packit 4a16fb
from each client as well as \ref pcm_plugins_dmix "dmix" and
Packit 4a16fb
\ref pcm_plugins_dsnoop "dsnoop" plugins do.
Packit 4a16fb
The parameters below are almost identical with these plugins.
Packit 4a16fb
Packit 4a16fb
\code
Packit 4a16fb
pcm.name {
Packit 4a16fb
	type dshare		# Direct sharing
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 Service f36a15
	tstamp_type STR		# timestamp type
Packit Service f36a15
				# STR can be one of the below strings :
Packit Service f36a15
				# default, gettimeofday, monotonic, monotonic_raw
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 under 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. Such wakeup delay could otherwise end up with Xruns in
Packit 4a16fb
  case of a dependency to another sound device (e.g. forwarding of
Packit 4a16fb
  microphone to speaker). Else "no" will be chosen.
Packit 4a16fb
Packit 4a16fb
\subsection pcm_plugins_dshare_funcref Function reference
Packit 4a16fb
Packit 4a16fb
    Packit 4a16fb
      
  • snd_pcm_dshare_open()
  • Packit 4a16fb
      
  • _snd_pcm_dshare_open()
  • Packit 4a16fb
    Packit 4a16fb
    Packit 4a16fb
    */
    Packit 4a16fb
    Packit 4a16fb
    /**
    Packit 4a16fb
     * \brief Creates a new dshare 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 dshare 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_dshare_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_dshare_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_dshare_open, SND_PCM_DLSYM_VERSION);
    Packit 4a16fb
    #endif