2007-01-29 16:26:23 +01:00
|
|
|
/*
|
|
|
|
* Sample Wine Driver for Advanced Linux Sound System (ALSA)
|
|
|
|
* Based on version <final> of the ALSA API
|
|
|
|
*
|
|
|
|
* Copyright 2002 Eric Pouech
|
|
|
|
* 2002 Marco Pietrobono
|
|
|
|
* 2003 Christian Costa : WaveIn support
|
|
|
|
* 2006-2007 Maarten Lankhorst
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*======================================================================*
|
|
|
|
* Low level dsound output implementation *
|
|
|
|
*======================================================================*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2007-10-30 15:06:51 +01:00
|
|
|
#include <assert.h>
|
2007-01-29 16:26:23 +01:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
|
|
|
# include <sys/mman.h>
|
|
|
|
#endif
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "wingdi.h"
|
|
|
|
#include "winerror.h"
|
|
|
|
#include "winuser.h"
|
|
|
|
#include "mmddk.h"
|
|
|
|
|
|
|
|
#include "alsa.h"
|
|
|
|
#include "wine/library.h"
|
|
|
|
#include "wine/unicode.h"
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
2007-06-21 20:30:46 +02:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(dsalsa);
|
2007-01-29 16:26:23 +01:00
|
|
|
|
|
|
|
typedef struct IDsDriverImpl IDsDriverImpl;
|
|
|
|
typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
|
|
|
|
|
|
|
|
struct IDsDriverImpl
|
|
|
|
{
|
|
|
|
/* IUnknown fields */
|
2010-12-08 01:09:45 +01:00
|
|
|
IDsDriver IDsDriver_iface;
|
2007-06-02 17:03:24 +02:00
|
|
|
LONG ref;
|
|
|
|
|
2007-01-29 16:26:23 +01:00
|
|
|
/* IDsDriverImpl fields */
|
2007-06-02 17:03:24 +02:00
|
|
|
IDsDriverBufferImpl* primary;
|
|
|
|
UINT wDevID;
|
2007-01-29 16:26:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct IDsDriverBufferImpl
|
|
|
|
{
|
2010-12-08 01:09:45 +01:00
|
|
|
IDsDriverBuffer IDsDriverBuffer_iface;
|
2007-06-21 20:30:46 +02:00
|
|
|
LONG ref;
|
|
|
|
IDsDriverImpl* drv;
|
|
|
|
|
|
|
|
CRITICAL_SECTION pcm_crst;
|
2009-07-12 20:11:55 +02:00
|
|
|
BYTE *mmap_buffer;
|
2007-06-21 20:30:46 +02:00
|
|
|
DWORD mmap_buflen_bytes;
|
2009-07-12 20:11:55 +02:00
|
|
|
BOOL mmap;
|
2007-01-29 16:26:23 +01:00
|
|
|
|
2007-06-02 17:03:24 +02:00
|
|
|
snd_pcm_t *pcm;
|
|
|
|
snd_pcm_hw_params_t *hw_params;
|
|
|
|
snd_pcm_sw_params_t *sw_params;
|
2007-07-29 16:17:22 +02:00
|
|
|
snd_pcm_uframes_t mmap_buflen_frames, mmap_pos, mmap_commitahead;
|
2007-01-29 16:26:23 +01:00
|
|
|
};
|
|
|
|
|
2010-12-08 01:09:45 +01:00
|
|
|
static inline IDsDriverImpl *impl_from_IDsDriver(IDsDriver *iface)
|
|
|
|
{
|
|
|
|
return CONTAINING_RECORD(iface, IDsDriverImpl, IDsDriver_iface);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline IDsDriverBufferImpl *impl_from_IDsDriverBuffer(IDsDriverBuffer *iface)
|
|
|
|
{
|
|
|
|
return CONTAINING_RECORD(iface, IDsDriverBufferImpl, IDsDriverBuffer_iface);
|
|
|
|
}
|
|
|
|
|
2007-06-21 20:30:46 +02:00
|
|
|
/** Fill buffers, for starting and stopping
|
|
|
|
* Alsa won't start playing until everything is filled up
|
|
|
|
* This also updates mmap_pos
|
2007-06-27 23:37:26 +02:00
|
|
|
*
|
|
|
|
* Returns: Amount of periods in use so snd_pcm_avail_update
|
|
|
|
* doesn't have to be called up to 4x in GetPosition()
|
2007-06-21 20:30:46 +02:00
|
|
|
*/
|
2007-06-27 23:37:26 +02:00
|
|
|
static snd_pcm_uframes_t CommitAll(IDsDriverBufferImpl *This)
|
2007-01-29 16:26:23 +01:00
|
|
|
{
|
2007-06-21 20:30:46 +02:00
|
|
|
const snd_pcm_channel_area_t *areas;
|
2009-07-12 20:11:55 +02:00
|
|
|
snd_pcm_sframes_t used;
|
2007-06-27 23:37:26 +02:00
|
|
|
const snd_pcm_uframes_t commitahead = This->mmap_commitahead;
|
2007-06-21 20:30:46 +02:00
|
|
|
|
|
|
|
used = This->mmap_buflen_frames - snd_pcm_avail_update(This->pcm);
|
2009-07-12 20:11:55 +02:00
|
|
|
if (used < 0) used = 0;
|
|
|
|
TRACE("%p needs to commit to %lu, used: %ld\n", This, commitahead, used);
|
2007-06-27 23:37:26 +02:00
|
|
|
if (used < commitahead)
|
2007-06-21 20:30:46 +02:00
|
|
|
{
|
2009-09-22 19:35:57 +02:00
|
|
|
snd_pcm_sframes_t done;
|
|
|
|
snd_pcm_uframes_t putin = commitahead - used;
|
2009-07-12 20:11:55 +02:00
|
|
|
if (This->mmap)
|
|
|
|
{
|
|
|
|
snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
|
|
|
|
done = snd_pcm_mmap_commit(This->pcm, This->mmap_pos, putin);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (putin + This->mmap_pos > This->mmap_buflen_frames)
|
|
|
|
putin = This->mmap_buflen_frames - This->mmap_pos;
|
2009-09-22 19:35:57 +02:00
|
|
|
done = snd_pcm_writei(This->pcm, This->mmap_buffer + snd_pcm_frames_to_bytes(This->pcm, This->mmap_pos), putin);
|
|
|
|
if (done < putin) WARN("Short write %ld/%ld\n", putin, done);
|
2009-07-12 20:11:55 +02:00
|
|
|
}
|
2009-09-22 19:35:57 +02:00
|
|
|
if (done < 0) done = 0;
|
2007-06-27 23:37:26 +02:00
|
|
|
This->mmap_pos += done;
|
|
|
|
used += done;
|
|
|
|
putin = commitahead - used;
|
2007-06-21 20:30:46 +02:00
|
|
|
|
2007-06-27 23:37:26 +02:00
|
|
|
if (This->mmap_pos == This->mmap_buflen_frames && (snd_pcm_sframes_t)putin > 0)
|
2007-06-21 20:30:46 +02:00
|
|
|
{
|
2009-07-12 20:11:55 +02:00
|
|
|
if (This->mmap)
|
|
|
|
{
|
|
|
|
snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
|
|
|
|
done = snd_pcm_mmap_commit(This->pcm, This->mmap_pos, putin);
|
|
|
|
This->mmap_pos += done;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-22 19:35:57 +02:00
|
|
|
done = snd_pcm_writei(This->pcm, This->mmap_buffer, putin);
|
|
|
|
if (done < putin) WARN("Short write %ld/%ld\n", putin, done);
|
|
|
|
if (done < 0) done = 0;
|
|
|
|
This->mmap_pos = done;
|
2009-07-12 20:11:55 +02:00
|
|
|
}
|
2007-06-27 23:37:26 +02:00
|
|
|
used += done;
|
2007-06-21 20:30:46 +02:00
|
|
|
}
|
|
|
|
}
|
2007-06-27 23:37:26 +02:00
|
|
|
|
|
|
|
if (This->mmap_pos == This->mmap_buflen_frames)
|
|
|
|
This->mmap_pos = 0;
|
|
|
|
|
|
|
|
return used;
|
2007-06-21 20:30:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void CheckXRUN(IDsDriverBufferImpl* This)
|
|
|
|
{
|
|
|
|
snd_pcm_state_t state = snd_pcm_state(This->pcm);
|
|
|
|
int err;
|
|
|
|
|
2007-01-29 16:26:23 +01:00
|
|
|
if ( state == SND_PCM_STATE_XRUN )
|
|
|
|
{
|
2007-06-21 20:30:46 +02:00
|
|
|
err = snd_pcm_prepare(This->pcm);
|
|
|
|
CommitAll(This);
|
|
|
|
snd_pcm_start(This->pcm);
|
|
|
|
WARN("xrun occurred\n");
|
|
|
|
if ( err < 0 )
|
2007-01-29 16:26:23 +01:00
|
|
|
ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err));
|
|
|
|
}
|
|
|
|
else if ( state == SND_PCM_STATE_SUSPENDED )
|
|
|
|
{
|
2007-06-21 20:30:46 +02:00
|
|
|
int err = snd_pcm_resume(This->pcm);
|
|
|
|
TRACE("recovery from suspension occurred\n");
|
2007-01-29 16:26:23 +01:00
|
|
|
if (err < 0 && err != -EAGAIN){
|
2007-06-21 20:30:46 +02:00
|
|
|
err = snd_pcm_prepare(This->pcm);
|
2007-01-29 16:26:23 +01:00
|
|
|
if (err < 0)
|
|
|
|
ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err));
|
|
|
|
}
|
2007-01-29 16:33:24 +01:00
|
|
|
} else if ( state != SND_PCM_STATE_RUNNING ) {
|
2007-07-19 00:58:23 +02:00
|
|
|
FIXME("Unhandled state: %d\n", state);
|
2007-01-29 16:26:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate the memory-mapped buffer for direct sound, and set up the
|
|
|
|
* callback.
|
|
|
|
*/
|
|
|
|
static int DSDB_CreateMMAP(IDsDriverBufferImpl* pdbi)
|
|
|
|
{
|
2007-06-02 17:03:24 +02:00
|
|
|
snd_pcm_t *pcm = pdbi->pcm;
|
|
|
|
snd_pcm_format_t format;
|
2007-06-21 20:30:46 +02:00
|
|
|
snd_pcm_uframes_t frames, ofs, avail, psize, boundary;
|
2007-06-02 17:03:24 +02:00
|
|
|
unsigned int channels, bits_per_sample, bits_per_frame;
|
|
|
|
int err, mmap_mode;
|
2007-06-21 20:30:46 +02:00
|
|
|
const snd_pcm_channel_area_t *areas;
|
2007-06-02 17:03:24 +02:00
|
|
|
snd_pcm_hw_params_t *hw_params = pdbi->hw_params;
|
|
|
|
snd_pcm_sw_params_t *sw_params = pdbi->sw_params;
|
2009-07-12 20:11:55 +02:00
|
|
|
void *buf;
|
2007-06-02 17:03:24 +02:00
|
|
|
|
|
|
|
mmap_mode = snd_pcm_type(pcm);
|
2007-01-29 16:33:24 +01:00
|
|
|
|
2007-06-02 17:03:24 +02:00
|
|
|
if (mmap_mode == SND_PCM_TYPE_HW)
|
2007-06-27 23:37:26 +02:00
|
|
|
TRACE("mmap'd buffer is a direct hardware buffer.\n");
|
|
|
|
else if (mmap_mode == SND_PCM_TYPE_DMIX)
|
|
|
|
TRACE("mmap'd buffer is an ALSA dmix buffer\n");
|
2007-06-02 17:03:24 +02:00
|
|
|
else
|
2007-06-27 23:37:26 +02:00
|
|
|
TRACE("mmap'd buffer is an ALSA type %d buffer\n", mmap_mode);
|
2007-01-29 16:26:23 +01:00
|
|
|
|
2007-06-02 17:03:24 +02:00
|
|
|
err = snd_pcm_hw_params_get_period_size(hw_params, &psize, NULL);
|
|
|
|
|
|
|
|
err = snd_pcm_hw_params_get_format(hw_params, &format);
|
|
|
|
err = snd_pcm_hw_params_get_buffer_size(hw_params, &frames);
|
|
|
|
err = snd_pcm_hw_params_get_channels(hw_params, &channels);
|
2007-01-29 16:33:24 +01:00
|
|
|
bits_per_sample = snd_pcm_format_physical_width(format);
|
|
|
|
bits_per_frame = bits_per_sample * channels;
|
|
|
|
|
2007-06-21 20:30:46 +02:00
|
|
|
if (TRACE_ON(dsalsa))
|
2007-06-02 17:03:24 +02:00
|
|
|
ALSA_TraceParameters(hw_params, NULL, FALSE);
|
2007-01-29 16:26:23 +01:00
|
|
|
|
|
|
|
TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n",
|
|
|
|
snd_pcm_format_name(format), frames, channels, bits_per_sample, bits_per_frame);
|
|
|
|
|
|
|
|
pdbi->mmap_buflen_frames = frames;
|
2007-06-02 17:03:24 +02:00
|
|
|
pdbi->mmap_buflen_bytes = snd_pcm_frames_to_bytes( pcm, frames );
|
2007-01-29 16:26:23 +01:00
|
|
|
|
2007-06-21 20:30:46 +02:00
|
|
|
snd_pcm_sw_params_current(pcm, sw_params);
|
|
|
|
snd_pcm_sw_params_set_start_threshold(pcm, sw_params, 0);
|
|
|
|
snd_pcm_sw_params_get_boundary(sw_params, &boundary);
|
|
|
|
snd_pcm_sw_params_set_stop_threshold(pcm, sw_params, boundary);
|
2008-08-18 20:37:14 +02:00
|
|
|
snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, boundary);
|
2007-06-21 20:30:46 +02:00
|
|
|
snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0);
|
|
|
|
snd_pcm_sw_params_set_avail_min(pcm, sw_params, 0);
|
|
|
|
err = snd_pcm_sw_params(pcm, sw_params);
|
|
|
|
|
2007-06-02 17:03:24 +02:00
|
|
|
avail = snd_pcm_avail_update(pcm);
|
2008-08-18 20:37:14 +02:00
|
|
|
if ((snd_pcm_sframes_t)avail < 0)
|
2007-01-29 16:26:23 +01:00
|
|
|
{
|
2007-06-21 20:30:46 +02:00
|
|
|
ERR("No buffer is available: %s.\n", snd_strerror(avail));
|
|
|
|
return DSERR_GENERIC;
|
2007-01-29 16:26:23 +01:00
|
|
|
}
|
2009-07-12 20:11:55 +02:00
|
|
|
|
|
|
|
if (!pdbi->mmap)
|
2007-01-29 16:26:23 +01:00
|
|
|
{
|
2009-07-12 20:11:55 +02:00
|
|
|
buf = pdbi->mmap_buffer = HeapAlloc(GetProcessHeap(), 0, pdbi->mmap_buflen_bytes);
|
|
|
|
if (!buf)
|
|
|
|
return DSERR_OUTOFMEMORY;
|
|
|
|
|
|
|
|
snd_pcm_format_set_silence(format, buf, pdbi->mmap_buflen_frames);
|
2009-09-13 07:23:49 +02:00
|
|
|
pdbi->mmap_pos = 0;
|
2009-07-12 20:11:55 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = snd_pcm_mmap_begin(pcm, &areas, &ofs, &avail);
|
|
|
|
if ( err < 0 )
|
|
|
|
{
|
|
|
|
ERR("Can't map sound device for direct access: %s/%d\n", snd_strerror(err), err);
|
|
|
|
return DSERR_GENERIC;
|
|
|
|
}
|
|
|
|
snd_pcm_format_set_silence(format, areas->addr, pdbi->mmap_buflen_frames);
|
|
|
|
pdbi->mmap_pos = ofs + snd_pcm_mmap_commit(pcm, ofs, 0);
|
|
|
|
pdbi->mmap_buffer = areas->addr;
|
2007-01-29 16:26:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("created mmap buffer of %ld frames (%d bytes) at %p\n",
|
|
|
|
frames, pdbi->mmap_buflen_bytes, pdbi->mmap_buffer);
|
|
|
|
|
|
|
|
return DS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
|
|
|
|
{
|
|
|
|
/* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
|
|
|
|
FIXME("(): stub!\n");
|
|
|
|
return DSERR_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
|
|
|
|
{
|
2010-12-08 01:09:45 +01:00
|
|
|
IDsDriverBufferImpl *This = impl_from_IDsDriverBuffer(iface);
|
2007-01-29 16:26:23 +01:00
|
|
|
ULONG refCount = InterlockedIncrement(&This->ref);
|
|
|
|
|
|
|
|
TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
|
|
|
|
|
|
|
|
return refCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
|
|
|
|
{
|
2010-12-08 01:09:45 +01:00
|
|
|
IDsDriverBufferImpl *This = impl_from_IDsDriverBuffer(iface);
|
2007-01-29 16:26:23 +01:00
|
|
|
ULONG refCount = InterlockedDecrement(&This->ref);
|
|
|
|
|
|
|
|
TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
|
|
|
|
|
|
|
|
if (refCount)
|
2007-06-21 20:30:46 +02:00
|
|
|
return refCount;
|
2007-01-29 16:33:24 +01:00
|
|
|
|
|
|
|
TRACE("mmap buffer %p destroyed\n", This->mmap_buffer);
|
|
|
|
|
2007-01-29 16:26:23 +01:00
|
|
|
if (This == This->drv->primary)
|
2007-06-02 17:03:24 +02:00
|
|
|
This->drv->primary = NULL;
|
|
|
|
|
2007-06-21 20:30:46 +02:00
|
|
|
This->pcm_crst.DebugInfo->Spare[0] = 0;
|
|
|
|
DeleteCriticalSection(&This->pcm_crst);
|
|
|
|
|
|
|
|
snd_pcm_drop(This->pcm);
|
2007-06-02 17:03:24 +02:00
|
|
|
snd_pcm_close(This->pcm);
|
|
|
|
This->pcm = NULL;
|
|
|
|
HeapFree(GetProcessHeap(), 0, This->sw_params);
|
|
|
|
HeapFree(GetProcessHeap(), 0, This->hw_params);
|
2009-07-12 20:11:55 +02:00
|
|
|
if (!This->mmap)
|
|
|
|
HeapFree(GetProcessHeap(), 0, This->mmap_buffer);
|
2007-01-29 16:26:23 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, This);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
|
|
|
|
LPVOID*ppvAudio1,LPDWORD pdwLen1,
|
|
|
|
LPVOID*ppvAudio2,LPDWORD pdwLen2,
|
|
|
|
DWORD dwWritePosition,DWORD dwWriteLen,
|
|
|
|
DWORD dwFlags)
|
|
|
|
{
|
2010-12-08 01:09:45 +01:00
|
|
|
IDsDriverBufferImpl *This = impl_from_IDsDriverBuffer(iface);
|
2007-07-29 16:17:22 +02:00
|
|
|
snd_pcm_uframes_t writepos;
|
|
|
|
|
|
|
|
TRACE("%d bytes from %d\n", dwWriteLen, dwWritePosition);
|
|
|
|
|
|
|
|
/* **** */
|
|
|
|
EnterCriticalSection(&This->pcm_crst);
|
|
|
|
|
2007-08-27 14:28:52 +02:00
|
|
|
if (dwFlags & DSBLOCK_ENTIREBUFFER)
|
|
|
|
dwWriteLen = This->mmap_buflen_bytes;
|
|
|
|
|
2007-07-29 16:17:22 +02:00
|
|
|
if (dwWriteLen > This->mmap_buflen_bytes || dwWritePosition >= This->mmap_buflen_bytes)
|
|
|
|
{
|
|
|
|
/* **** */
|
|
|
|
LeaveCriticalSection(&This->pcm_crst);
|
|
|
|
return DSERR_INVALIDPARAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ppvAudio2) *ppvAudio2 = NULL;
|
|
|
|
if (pdwLen2) *pdwLen2 = 0;
|
|
|
|
|
2009-07-12 20:11:55 +02:00
|
|
|
*ppvAudio1 = This->mmap_buffer + dwWritePosition;
|
2007-07-29 16:17:22 +02:00
|
|
|
*pdwLen1 = dwWriteLen;
|
|
|
|
|
|
|
|
if (dwWritePosition+dwWriteLen > This->mmap_buflen_bytes)
|
|
|
|
{
|
|
|
|
DWORD remainder = This->mmap_buflen_bytes - dwWritePosition;
|
|
|
|
*pdwLen1 = remainder;
|
|
|
|
|
|
|
|
if (ppvAudio2 && pdwLen2)
|
|
|
|
{
|
|
|
|
*ppvAudio2 = This->mmap_buffer;
|
|
|
|
*pdwLen2 = dwWriteLen - remainder;
|
|
|
|
}
|
|
|
|
else dwWriteLen = remainder;
|
|
|
|
}
|
|
|
|
|
|
|
|
writepos = snd_pcm_bytes_to_frames(This->pcm, dwWritePosition);
|
2007-08-02 14:19:05 +02:00
|
|
|
if (writepos == This->mmap_pos)
|
2007-07-29 16:17:22 +02:00
|
|
|
{
|
|
|
|
const snd_pcm_channel_area_t *areas;
|
|
|
|
snd_pcm_uframes_t writelen = snd_pcm_bytes_to_frames(This->pcm, dwWriteLen), putin = writelen;
|
|
|
|
TRACE("Hit mmap_pos, locking data!\n");
|
2009-07-12 20:11:55 +02:00
|
|
|
if (This->mmap)
|
|
|
|
snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
|
2007-07-29 16:17:22 +02:00
|
|
|
}
|
2008-09-04 12:07:37 +02:00
|
|
|
else
|
|
|
|
WARN("mmap_pos (%lu) != writepos (%lu) not locking data!\n", This->mmap_pos, writepos);
|
2007-07-29 16:17:22 +02:00
|
|
|
|
|
|
|
LeaveCriticalSection(&This->pcm_crst);
|
|
|
|
/* **** */
|
|
|
|
return DS_OK;
|
2007-01-29 16:26:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
|
|
|
|
LPVOID pvAudio1,DWORD dwLen1,
|
|
|
|
LPVOID pvAudio2,DWORD dwLen2)
|
|
|
|
{
|
2010-12-08 01:09:45 +01:00
|
|
|
IDsDriverBufferImpl *This = impl_from_IDsDriverBuffer(iface);
|
2007-07-29 16:17:22 +02:00
|
|
|
snd_pcm_uframes_t writepos;
|
|
|
|
|
|
|
|
if (!dwLen1)
|
|
|
|
return DS_OK;
|
|
|
|
|
|
|
|
/* **** */
|
|
|
|
EnterCriticalSection(&This->pcm_crst);
|
|
|
|
|
|
|
|
writepos = snd_pcm_bytes_to_frames(This->pcm, (DWORD_PTR)pvAudio1 - (DWORD_PTR)This->mmap_buffer);
|
|
|
|
if (writepos == This->mmap_pos)
|
|
|
|
{
|
|
|
|
const snd_pcm_channel_area_t *areas;
|
|
|
|
snd_pcm_uframes_t writelen = snd_pcm_bytes_to_frames(This->pcm, dwLen1);
|
|
|
|
TRACE("Committing data\n");
|
2009-07-12 20:11:55 +02:00
|
|
|
if (This->mmap)
|
|
|
|
This->mmap_pos += snd_pcm_mmap_commit(This->pcm, This->mmap_pos, writelen);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = snd_pcm_writei(This->pcm, pvAudio1, writelen);
|
|
|
|
if (ret == -EPIPE)
|
|
|
|
{
|
2009-08-11 09:03:12 +02:00
|
|
|
WARN("Underrun occurred\n");
|
2010-06-11 10:54:24 +02:00
|
|
|
wine_snd_pcm_recover(This->pcm, -EPIPE, 1);
|
2009-07-12 20:11:55 +02:00
|
|
|
ret = snd_pcm_writei(This->pcm, pvAudio1, writelen);
|
2009-09-22 19:35:57 +02:00
|
|
|
|
|
|
|
/* Advance mmap pointer a little to make dsound notice the underrun and respond to it */
|
|
|
|
if (ret < writelen) WARN("Short write %ld/%d\n", writelen, ret);
|
|
|
|
This->mmap_pos += This->mmap_commitahead + ret;
|
|
|
|
This->mmap_pos %= This->mmap_buflen_frames;
|
2009-07-12 20:11:55 +02:00
|
|
|
}
|
2009-09-22 19:35:57 +02:00
|
|
|
else if (ret > 0)
|
|
|
|
This->mmap_pos += ret;
|
2009-07-12 20:11:55 +02:00
|
|
|
if (ret < 0)
|
|
|
|
WARN("Committing data: %d / %s (%p %ld)\n", ret, snd_strerror(ret), pvAudio1, writelen);
|
|
|
|
}
|
|
|
|
|
2007-07-29 16:17:22 +02:00
|
|
|
if (This->mmap_pos == This->mmap_buflen_frames)
|
|
|
|
This->mmap_pos = 0;
|
2009-09-22 19:35:57 +02:00
|
|
|
if (dwLen2)
|
2007-07-29 16:17:22 +02:00
|
|
|
{
|
|
|
|
writelen = snd_pcm_bytes_to_frames(This->pcm, dwLen2);
|
2009-07-12 20:11:55 +02:00
|
|
|
if (This->mmap)
|
|
|
|
{
|
|
|
|
snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &writelen);
|
|
|
|
This->mmap_pos += snd_pcm_mmap_commit(This->pcm, This->mmap_pos, writelen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = snd_pcm_writei(This->pcm, pvAudio2, writelen);
|
2009-09-22 19:35:57 +02:00
|
|
|
if (ret < writelen) WARN("Short write %ld/%d\n", writelen, ret);
|
|
|
|
This->mmap_pos = ret > 0 ? ret : 0;
|
2009-07-12 20:11:55 +02:00
|
|
|
}
|
2007-07-29 16:17:22 +02:00
|
|
|
assert(This->mmap_pos < This->mmap_buflen_frames);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LeaveCriticalSection(&This->pcm_crst);
|
|
|
|
/* **** */
|
|
|
|
|
|
|
|
return DS_OK;
|
2007-01-29 16:26:23 +01:00
|
|
|
}
|
|
|
|
|
2007-08-27 14:28:52 +02:00
|
|
|
static HRESULT SetFormat(IDsDriverBufferImpl *This, LPWAVEFORMATEX pwfx)
|
2007-01-29 16:26:23 +01:00
|
|
|
{
|
2007-06-21 20:30:46 +02:00
|
|
|
snd_pcm_t *pcm = NULL;
|
2007-06-02 17:03:24 +02:00
|
|
|
snd_pcm_hw_params_t *hw_params = This->hw_params;
|
|
|
|
unsigned int buffer_time = 500000;
|
|
|
|
snd_pcm_format_t format = -1;
|
|
|
|
snd_pcm_uframes_t psize;
|
|
|
|
DWORD rate = pwfx->nSamplesPerSec;
|
|
|
|
int err=0;
|
|
|
|
|
|
|
|
switch (pwfx->wBitsPerSample)
|
|
|
|
{
|
|
|
|
case 8: format = SND_PCM_FORMAT_U8; break;
|
|
|
|
case 16: format = SND_PCM_FORMAT_S16_LE; break;
|
2007-10-12 19:35:05 +02:00
|
|
|
case 24: format = SND_PCM_FORMAT_S24_3LE; break;
|
2007-06-02 17:03:24 +02:00
|
|
|
case 32: format = SND_PCM_FORMAT_S32_LE; break;
|
2007-06-21 20:30:46 +02:00
|
|
|
default: FIXME("Unsupported bpp: %d\n", pwfx->wBitsPerSample); return DSERR_GENERIC;
|
2007-06-02 17:03:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = snd_pcm_open(&pcm, WOutDev[This->drv->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
|
|
|
|
if (err < 0)
|
|
|
|
{
|
2007-06-21 20:30:46 +02:00
|
|
|
if (errno != EBUSY || !This->pcm)
|
|
|
|
{
|
2007-07-05 16:10:30 +02:00
|
|
|
WARN("Cannot open sound device: %s\n", snd_strerror(err));
|
2007-06-21 20:30:46 +02:00
|
|
|
return DSERR_GENERIC;
|
|
|
|
}
|
|
|
|
snd_pcm_drop(This->pcm);
|
|
|
|
snd_pcm_close(This->pcm);
|
|
|
|
This->pcm = NULL;
|
|
|
|
err = snd_pcm_open(&pcm, WOutDev[This->drv->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
|
|
|
|
if (err < 0)
|
|
|
|
{
|
2007-07-05 16:10:30 +02:00
|
|
|
WARN("Cannot open sound device: %s\n", snd_strerror(err));
|
2007-06-21 20:30:46 +02:00
|
|
|
return DSERR_BUFFERLOST;
|
|
|
|
}
|
2007-06-02 17:03:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set some defaults */
|
|
|
|
snd_pcm_hw_params_any(pcm, hw_params);
|
|
|
|
err = snd_pcm_hw_params_set_channels(pcm, hw_params, pwfx->nChannels);
|
|
|
|
if (err < 0) { WARN("Could not set channels to %d\n", pwfx->nChannels); goto err; }
|
|
|
|
|
|
|
|
err = snd_pcm_hw_params_set_format(pcm, hw_params, format);
|
|
|
|
if (err < 0) { WARN("Could not set format to %d bpp\n", pwfx->wBitsPerSample); goto err; }
|
|
|
|
|
|
|
|
/* Alsa's rate resampling is only used if the application specifically requests
|
2007-07-05 16:10:30 +02:00
|
|
|
* a buffer at a certain frequency, else it is better to disable it due to unwanted
|
2007-06-02 17:03:24 +02:00
|
|
|
* side effects, which may include: Less granular pointer, changing buffer sizes, etc
|
|
|
|
*/
|
|
|
|
#if SND_LIB_VERSION >= 0x010009
|
2007-08-27 14:28:52 +02:00
|
|
|
snd_pcm_hw_params_set_rate_resample(pcm, hw_params, 0);
|
2007-06-02 17:03:24 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
err = snd_pcm_hw_params_set_rate_near(pcm, hw_params, &rate, NULL);
|
|
|
|
if (err < 0) { rate = pwfx->nSamplesPerSec; WARN("Could not set rate\n"); goto err; }
|
|
|
|
|
2007-09-05 10:56:59 +02:00
|
|
|
if (!ALSA_NearMatch(rate, pwfx->nSamplesPerSec))
|
2007-06-02 17:03:24 +02:00
|
|
|
{
|
|
|
|
WARN("Could not set sound rate to %d, but instead to %d\n", pwfx->nSamplesPerSec, rate);
|
|
|
|
pwfx->nSamplesPerSec = rate;
|
|
|
|
pwfx->nAvgBytesPerSec = rate * pwfx->nBlockAlign;
|
|
|
|
/* Let DirectSound detect this */
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_pcm_hw_params_set_periods_integer(pcm, hw_params);
|
|
|
|
snd_pcm_hw_params_set_buffer_time_near(pcm, hw_params, &buffer_time, NULL);
|
|
|
|
buffer_time = 10000;
|
|
|
|
snd_pcm_hw_params_set_period_time_near(pcm, hw_params, &buffer_time, NULL);
|
2009-07-12 20:11:55 +02:00
|
|
|
|
|
|
|
err = snd_pcm_hw_params_get_period_size(hw_params, &psize, NULL);
|
|
|
|
buffer_time = 16;
|
|
|
|
snd_pcm_hw_params_set_periods_near(pcm, hw_params, &buffer_time, NULL);
|
|
|
|
|
|
|
|
if (!This->mmap)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, This->mmap_buffer);
|
|
|
|
This->mmap_buffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
|
|
|
|
if (err >= 0)
|
|
|
|
This->mmap = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
This->mmap = 0;
|
|
|
|
err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
|
|
|
|
}
|
|
|
|
|
2007-06-02 17:03:24 +02:00
|
|
|
err = snd_pcm_hw_params(pcm, hw_params);
|
|
|
|
|
2007-07-31 18:57:34 +02:00
|
|
|
/* ALSA needs at least 3 buffers to work successfully */
|
2007-07-29 12:39:49 +02:00
|
|
|
This->mmap_commitahead = 3 * psize;
|
|
|
|
while (This->mmap_commitahead <= 512)
|
|
|
|
This->mmap_commitahead += psize;
|
2007-06-02 17:03:24 +02:00
|
|
|
|
|
|
|
if (This->pcm)
|
|
|
|
{
|
2007-06-21 20:30:46 +02:00
|
|
|
snd_pcm_drop(This->pcm);
|
2007-06-02 17:03:24 +02:00
|
|
|
snd_pcm_close(This->pcm);
|
|
|
|
}
|
|
|
|
This->pcm = pcm;
|
2007-06-21 20:30:46 +02:00
|
|
|
snd_pcm_prepare(This->pcm);
|
2007-06-02 17:03:24 +02:00
|
|
|
DSDB_CreateMMAP(This);
|
|
|
|
return S_OK;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (err < 0)
|
|
|
|
WARN("Failed to apply changes: %s\n", snd_strerror(err));
|
|
|
|
|
2007-06-27 23:37:26 +02:00
|
|
|
if (!This->pcm)
|
|
|
|
This->pcm = pcm;
|
|
|
|
else
|
|
|
|
snd_pcm_close(pcm);
|
|
|
|
|
2007-06-02 17:03:24 +02:00
|
|
|
if (This->pcm)
|
|
|
|
snd_pcm_hw_params_current(This->pcm, This->hw_params);
|
|
|
|
|
|
|
|
return DSERR_BADFORMAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface, LPWAVEFORMATEX pwfx)
|
|
|
|
{
|
2010-12-08 01:09:45 +01:00
|
|
|
IDsDriverBufferImpl *This = impl_from_IDsDriverBuffer(iface);
|
2007-06-21 20:30:46 +02:00
|
|
|
HRESULT hr = S_OK;
|
2007-06-02 17:03:24 +02:00
|
|
|
|
|
|
|
TRACE("(%p, %p)\n", iface, pwfx);
|
|
|
|
|
2007-08-27 14:28:52 +02:00
|
|
|
/* **** */
|
|
|
|
EnterCriticalSection(&This->pcm_crst);
|
|
|
|
hr = SetFormat(This, pwfx);
|
|
|
|
/* **** */
|
|
|
|
LeaveCriticalSection(&This->pcm_crst);
|
2007-06-02 17:03:24 +02:00
|
|
|
|
2007-08-27 14:28:52 +02:00
|
|
|
if (hr == DS_OK)
|
|
|
|
return S_FALSE;
|
2007-06-02 17:03:24 +02:00
|
|
|
return hr;
|
2007-01-29 16:26:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
|
|
|
|
{
|
|
|
|
/* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
|
2007-06-02 17:03:24 +02:00
|
|
|
FIXME("(%p,%d): stub\n",iface,dwFreq);
|
|
|
|
return S_OK;
|
2007-01-29 16:26:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
|
|
|
|
{
|
2010-12-08 01:09:45 +01:00
|
|
|
IDsDriverBufferImpl *This = impl_from_IDsDriverBuffer(iface);
|
2007-06-02 17:03:24 +02:00
|
|
|
FIXME("(%p,%p): stub\n",This,pVolPan);
|
|
|
|
/* TODO: Bring volume control back */
|
2007-01-29 16:26:23 +01:00
|
|
|
return DS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
|
|
|
|
{
|
2007-06-02 17:03:24 +02:00
|
|
|
/* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
|
2007-06-21 20:30:46 +02:00
|
|
|
/* I don't even think alsa allows this */
|
2007-01-29 16:33:24 +01:00
|
|
|
FIXME("(%p,%d): stub\n",iface,dwNewPos);
|
2007-01-29 16:26:23 +01:00
|
|
|
return DSERR_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
|
|
|
|
LPDWORD lpdwPlay, LPDWORD lpdwWrite)
|
|
|
|
{
|
2010-12-08 01:09:45 +01:00
|
|
|
IDsDriverBufferImpl *This = impl_from_IDsDriverBuffer(iface);
|
2007-08-02 14:16:31 +02:00
|
|
|
snd_pcm_uframes_t hw_pptr, hw_wptr;
|
2007-06-02 17:03:24 +02:00
|
|
|
snd_pcm_state_t state;
|
2007-01-29 16:26:23 +01:00
|
|
|
|
2007-07-29 16:17:22 +02:00
|
|
|
/* **** */
|
2007-06-21 20:30:46 +02:00
|
|
|
EnterCriticalSection(&This->pcm_crst);
|
|
|
|
|
|
|
|
if (!This->pcm)
|
2007-06-02 17:03:24 +02:00
|
|
|
{
|
2007-06-21 20:30:46 +02:00
|
|
|
FIXME("Bad pointer for pcm: %p\n", This->pcm);
|
2007-06-22 23:30:57 +02:00
|
|
|
LeaveCriticalSection(&This->pcm_crst);
|
2007-06-02 17:03:24 +02:00
|
|
|
return DSERR_GENERIC;
|
|
|
|
}
|
2007-01-29 16:26:23 +01:00
|
|
|
|
2007-07-29 16:17:22 +02:00
|
|
|
if (!lpdwPlay && !lpdwWrite)
|
|
|
|
CommitAll(This);
|
|
|
|
|
2007-06-21 20:30:46 +02:00
|
|
|
state = snd_pcm_state(This->pcm);
|
2007-06-27 23:37:26 +02:00
|
|
|
|
2007-07-19 00:58:23 +02:00
|
|
|
if (state != SND_PCM_STATE_PREPARED && state != SND_PCM_STATE_RUNNING)
|
|
|
|
{
|
2007-06-27 23:37:26 +02:00
|
|
|
CheckXRUN(This);
|
2007-07-19 00:58:23 +02:00
|
|
|
state = snd_pcm_state(This->pcm);
|
|
|
|
}
|
|
|
|
if (state == SND_PCM_STATE_RUNNING)
|
2007-01-29 16:33:24 +01:00
|
|
|
{
|
2008-08-08 20:50:02 +02:00
|
|
|
snd_pcm_sframes_t used = This->mmap_buflen_frames - snd_pcm_avail_update(This->pcm);
|
|
|
|
|
|
|
|
if (used < 0)
|
|
|
|
{
|
2009-07-12 20:11:55 +02:00
|
|
|
WARN("Underrun: %ld / %ld\n", used, snd_pcm_avail_update(This->pcm));
|
|
|
|
if (This->mmap)
|
|
|
|
{
|
|
|
|
snd_pcm_forward(This->pcm, -used);
|
|
|
|
This->mmap_pos += -used;
|
|
|
|
This->mmap_pos %= This->mmap_buflen_frames;
|
|
|
|
}
|
2008-08-08 20:50:02 +02:00
|
|
|
used = 0;
|
|
|
|
}
|
2007-06-27 23:37:26 +02:00
|
|
|
|
|
|
|
if (This->mmap_pos > used)
|
|
|
|
hw_pptr = This->mmap_pos - used;
|
|
|
|
else
|
2007-07-29 16:17:22 +02:00
|
|
|
hw_pptr = This->mmap_buflen_frames + This->mmap_pos - used;
|
2007-06-27 23:37:26 +02:00
|
|
|
hw_pptr %= This->mmap_buflen_frames;
|
2007-06-21 20:30:46 +02:00
|
|
|
|
2007-06-27 23:37:26 +02:00
|
|
|
TRACE("At position: %ld (%ld) - Used %ld\n", hw_pptr, This->mmap_pos, used);
|
|
|
|
}
|
2007-08-02 14:16:31 +02:00
|
|
|
else hw_pptr = This->mmap_pos;
|
|
|
|
hw_wptr = This->mmap_pos;
|
2007-06-21 20:30:46 +02:00
|
|
|
|
|
|
|
LeaveCriticalSection(&This->pcm_crst);
|
2007-07-29 16:17:22 +02:00
|
|
|
/* **** */
|
2007-01-29 16:26:23 +01:00
|
|
|
|
|
|
|
if (lpdwPlay)
|
2007-06-21 20:30:46 +02:00
|
|
|
*lpdwPlay = snd_pcm_frames_to_bytes(This->pcm, hw_pptr);
|
2007-01-29 16:26:23 +01:00
|
|
|
if (lpdwWrite)
|
2007-06-21 20:30:46 +02:00
|
|
|
*lpdwWrite = snd_pcm_frames_to_bytes(This->pcm, hw_wptr);
|
2007-01-29 16:26:23 +01:00
|
|
|
|
2007-06-21 20:30:46 +02:00
|
|
|
TRACE("hw_pptr=0x%08x, hw_wptr=0x%08x playpos=%d, writepos=%d\n", (unsigned int)hw_pptr, (unsigned int)hw_wptr, lpdwPlay?*lpdwPlay:-1, lpdwWrite?*lpdwWrite:-1);
|
2007-01-29 16:26:23 +01:00
|
|
|
return DS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
|
|
|
|
{
|
2010-12-08 01:09:45 +01:00
|
|
|
IDsDriverBufferImpl *This = impl_from_IDsDriverBuffer(iface);
|
2007-01-29 16:26:23 +01:00
|
|
|
TRACE("(%p,%x,%x,%x)\n",iface,dwRes1,dwRes2,dwFlags);
|
|
|
|
|
2007-06-21 20:30:46 +02:00
|
|
|
/* **** */
|
|
|
|
EnterCriticalSection(&This->pcm_crst);
|
2007-07-29 16:17:22 +02:00
|
|
|
snd_pcm_start(This->pcm);
|
2007-06-21 20:30:46 +02:00
|
|
|
/* **** */
|
|
|
|
LeaveCriticalSection(&This->pcm_crst);
|
2007-01-29 16:26:23 +01:00
|
|
|
return DS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
|
|
|
|
{
|
2007-07-29 16:17:22 +02:00
|
|
|
const snd_pcm_channel_area_t *areas;
|
|
|
|
snd_pcm_uframes_t avail;
|
|
|
|
snd_pcm_format_t format;
|
2010-12-08 01:09:45 +01:00
|
|
|
IDsDriverBufferImpl *This = impl_from_IDsDriverBuffer(iface);
|
2007-01-29 16:26:23 +01:00
|
|
|
TRACE("(%p)\n",iface);
|
|
|
|
|
2007-06-21 20:30:46 +02:00
|
|
|
/* **** */
|
|
|
|
EnterCriticalSection(&This->pcm_crst);
|
2007-07-29 16:17:22 +02:00
|
|
|
avail = This->mmap_buflen_frames;
|
2007-06-21 20:30:46 +02:00
|
|
|
snd_pcm_drop(This->pcm);
|
|
|
|
snd_pcm_prepare(This->pcm);
|
2007-08-02 14:21:17 +02:00
|
|
|
avail = snd_pcm_avail_update(This->pcm);
|
2007-07-29 16:17:22 +02:00
|
|
|
snd_pcm_hw_params_get_format(This->hw_params, &format);
|
2009-07-12 20:11:55 +02:00
|
|
|
if (This->mmap)
|
|
|
|
{
|
|
|
|
snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &avail);
|
|
|
|
snd_pcm_format_set_silence(format, areas->addr, This->mmap_buflen_frames);
|
|
|
|
snd_pcm_mmap_commit(This->pcm, This->mmap_pos, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snd_pcm_format_set_silence(format, This->mmap_buffer, This->mmap_buflen_frames);
|
|
|
|
snd_pcm_writei(This->pcm, This->mmap_buffer, This->mmap_buflen_frames);
|
|
|
|
This->mmap_pos = 0;
|
|
|
|
}
|
2007-07-29 16:17:22 +02:00
|
|
|
|
2007-06-21 20:30:46 +02:00
|
|
|
/* **** */
|
|
|
|
LeaveCriticalSection(&This->pcm_crst);
|
2007-01-29 16:26:23 +01:00
|
|
|
return DS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const IDsDriverBufferVtbl dsdbvt =
|
|
|
|
{
|
|
|
|
IDsDriverBufferImpl_QueryInterface,
|
|
|
|
IDsDriverBufferImpl_AddRef,
|
|
|
|
IDsDriverBufferImpl_Release,
|
|
|
|
IDsDriverBufferImpl_Lock,
|
|
|
|
IDsDriverBufferImpl_Unlock,
|
|
|
|
IDsDriverBufferImpl_SetFormat,
|
|
|
|
IDsDriverBufferImpl_SetFrequency,
|
|
|
|
IDsDriverBufferImpl_SetVolumePan,
|
|
|
|
IDsDriverBufferImpl_SetPosition,
|
|
|
|
IDsDriverBufferImpl_GetPosition,
|
|
|
|
IDsDriverBufferImpl_Play,
|
|
|
|
IDsDriverBufferImpl_Stop
|
|
|
|
};
|
|
|
|
|
|
|
|
static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
|
|
|
|
{
|
|
|
|
/* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
|
|
|
|
FIXME("(%p): stub!\n",iface);
|
|
|
|
return DSERR_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
|
|
|
|
{
|
2010-12-08 01:09:45 +01:00
|
|
|
IDsDriverImpl *This = impl_from_IDsDriver(iface);
|
2007-01-29 16:26:23 +01:00
|
|
|
ULONG refCount = InterlockedIncrement(&This->ref);
|
|
|
|
|
|
|
|
TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
|
|
|
|
|
|
|
|
return refCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
|
|
|
|
{
|
2010-12-08 01:09:45 +01:00
|
|
|
IDsDriverImpl *This = impl_from_IDsDriver(iface);
|
2007-01-29 16:26:23 +01:00
|
|
|
ULONG refCount = InterlockedDecrement(&This->ref);
|
|
|
|
|
|
|
|
TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
|
|
|
|
|
|
|
|
if (refCount)
|
2007-06-02 17:03:24 +02:00
|
|
|
return refCount;
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, This);
|
2007-01-29 16:26:23 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
|
|
|
|
{
|
2010-12-08 01:09:45 +01:00
|
|
|
IDsDriverImpl *This = impl_from_IDsDriver(iface);
|
2007-01-29 16:26:23 +01:00
|
|
|
TRACE("(%p,%p)\n",iface,pDesc);
|
2008-03-24 22:37:37 +01:00
|
|
|
*pDesc = WOutDev[This->wDevID].ds_desc;
|
2007-07-29 16:17:22 +02:00
|
|
|
pDesc->dwFlags = DSDDESC_DONTNEEDSECONDARYLOCK | DSDDESC_DONTNEEDWRITELEAD;
|
2007-01-29 16:26:23 +01:00
|
|
|
pDesc->dnDevNode = WOutDev[This->wDevID].waveDesc.dnDevNode;
|
|
|
|
pDesc->wVxdId = 0;
|
|
|
|
pDesc->wReserved = 0;
|
|
|
|
pDesc->ulDeviceNum = This->wDevID;
|
|
|
|
pDesc->dwHeapType = DSDHEAP_NOHEAP;
|
|
|
|
pDesc->pvDirectDrawHeap = NULL;
|
2007-06-02 17:03:24 +02:00
|
|
|
pDesc->dwMemStartAddress = 0xDEAD0000;
|
|
|
|
pDesc->dwMemEndAddress = 0xDEAF0000;
|
2007-01-29 16:26:23 +01:00
|
|
|
pDesc->dwMemAllocExtra = 0;
|
|
|
|
pDesc->pvReserved1 = NULL;
|
|
|
|
pDesc->pvReserved2 = NULL;
|
|
|
|
return DS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
|
|
|
|
{
|
2007-06-02 17:03:24 +02:00
|
|
|
HRESULT hr = S_OK;
|
2010-12-08 01:09:45 +01:00
|
|
|
IDsDriverImpl *This = impl_from_IDsDriver(iface);
|
2007-06-02 17:03:24 +02:00
|
|
|
int err=0;
|
|
|
|
snd_pcm_t *pcm = NULL;
|
|
|
|
snd_pcm_hw_params_t *hw_params;
|
|
|
|
|
|
|
|
/* While this is not really needed, it is a good idea to do this,
|
|
|
|
* to see if sound can be initialized */
|
|
|
|
|
|
|
|
hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
|
|
|
|
|
|
|
|
if (!hw_params)
|
|
|
|
{
|
|
|
|
hr = DSERR_OUTOFMEMORY;
|
|
|
|
goto unalloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = snd_pcm_open(&pcm, WOutDev[This->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
|
|
|
|
if (err < 0) goto err;
|
|
|
|
err = snd_pcm_hw_params_any(pcm, hw_params);
|
|
|
|
if (err < 0) goto err;
|
|
|
|
err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
|
2009-07-12 20:11:55 +02:00
|
|
|
if (err < 0)
|
|
|
|
err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
|
2007-06-02 17:03:24 +02:00
|
|
|
if (err < 0) goto err;
|
|
|
|
|
|
|
|
TRACE("Success\n");
|
|
|
|
snd_pcm_close(pcm);
|
|
|
|
goto unalloc;
|
|
|
|
|
|
|
|
err:
|
|
|
|
hr = DSERR_GENERIC;
|
|
|
|
FIXME("Failed to open device: %s\n", snd_strerror(err));
|
|
|
|
if (pcm)
|
|
|
|
snd_pcm_close(pcm);
|
|
|
|
unalloc:
|
|
|
|
HeapFree(GetProcessHeap(), 0, hw_params);
|
|
|
|
if (hr != S_OK)
|
|
|
|
WARN("--> %08x\n", hr);
|
|
|
|
return hr;
|
2007-01-29 16:26:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
|
|
|
|
{
|
2010-12-08 01:09:45 +01:00
|
|
|
IDsDriverImpl *This = impl_from_IDsDriver(iface);
|
2007-06-02 17:03:24 +02:00
|
|
|
TRACE("(%p) stub, harmless\n",This);
|
2007-01-29 16:26:23 +01:00
|
|
|
return DS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
|
|
|
|
{
|
2010-12-08 01:09:45 +01:00
|
|
|
IDsDriverImpl *This = impl_from_IDsDriver(iface);
|
2007-01-29 16:26:23 +01:00
|
|
|
TRACE("(%p,%p)\n",iface,pCaps);
|
2008-03-24 22:37:37 +01:00
|
|
|
*pCaps = WOutDev[This->wDevID].ds_caps;
|
2007-01-29 16:26:23 +01:00
|
|
|
return DS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
|
|
|
|
LPWAVEFORMATEX pwfx,
|
|
|
|
DWORD dwFlags, DWORD dwCardAddress,
|
|
|
|
LPDWORD pdwcbBufferSize,
|
|
|
|
LPBYTE *ppbBuffer,
|
|
|
|
LPVOID *ppvObj)
|
|
|
|
{
|
2010-12-08 01:09:45 +01:00
|
|
|
IDsDriverImpl *This = impl_from_IDsDriver(iface);
|
2007-01-29 16:26:23 +01:00
|
|
|
IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
|
2007-06-02 17:03:24 +02:00
|
|
|
HRESULT err;
|
2007-01-29 16:26:23 +01:00
|
|
|
|
|
|
|
TRACE("(%p,%p,%x,%x)\n",iface,pwfx,dwFlags,dwCardAddress);
|
2007-06-02 17:03:24 +02:00
|
|
|
/* we only support primary buffers... for now */
|
2007-01-29 16:26:23 +01:00
|
|
|
if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
|
2007-06-02 17:03:24 +02:00
|
|
|
return DSERR_UNSUPPORTED;
|
2007-01-29 16:26:23 +01:00
|
|
|
if (This->primary)
|
2007-06-02 17:03:24 +02:00
|
|
|
return DSERR_ALLOCATED;
|
2007-01-29 16:26:23 +01:00
|
|
|
|
2007-06-02 17:03:24 +02:00
|
|
|
*ippdsdb = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDsDriverBufferImpl));
|
2007-01-29 16:26:23 +01:00
|
|
|
if (*ippdsdb == NULL)
|
2007-06-02 17:03:24 +02:00
|
|
|
return DSERR_OUTOFMEMORY;
|
|
|
|
|
|
|
|
(*ippdsdb)->hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
|
|
|
|
(*ippdsdb)->sw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_sw_params_sizeof());
|
|
|
|
if (!(*ippdsdb)->hw_params || !(*ippdsdb)->sw_params)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
|
|
|
|
HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
|
|
|
|
return DSERR_OUTOFMEMORY;
|
|
|
|
}
|
2010-12-08 01:09:45 +01:00
|
|
|
(*ippdsdb)->IDsDriverBuffer_iface.lpVtbl = &dsdbvt;
|
2007-01-29 16:26:23 +01:00
|
|
|
(*ippdsdb)->ref = 1;
|
|
|
|
(*ippdsdb)->drv = This;
|
2007-06-21 20:30:46 +02:00
|
|
|
InitializeCriticalSection(&(*ippdsdb)->pcm_crst);
|
|
|
|
(*ippdsdb)->pcm_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ALSA_DSOUTPUT.pcm_crst");
|
2007-01-29 16:26:23 +01:00
|
|
|
|
2007-06-02 17:03:24 +02:00
|
|
|
/* SetFormat has to re-initialize pcm here anyway */
|
2007-08-27 14:28:52 +02:00
|
|
|
err = SetFormat(*ippdsdb, pwfx);
|
2007-06-02 17:03:24 +02:00
|
|
|
if (FAILED(err))
|
|
|
|
{
|
2007-07-05 16:10:30 +02:00
|
|
|
WARN("Error occurred: %08x\n", err);
|
2007-06-02 17:03:24 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2007-01-29 16:26:23 +01:00
|
|
|
|
2007-06-02 17:03:24 +02:00
|
|
|
if (dwFlags & DSBCAPS_PRIMARYBUFFER)
|
|
|
|
This->primary = *ippdsdb;
|
2007-01-29 16:26:23 +01:00
|
|
|
|
2007-06-21 20:30:46 +02:00
|
|
|
*pdwcbBufferSize = (*ippdsdb)->mmap_buflen_bytes;
|
|
|
|
*ppbBuffer = (*ippdsdb)->mmap_buffer;
|
|
|
|
|
2007-01-29 16:26:23 +01:00
|
|
|
/* buffer is ready to go */
|
|
|
|
TRACE("buffer created at %p\n", *ippdsdb);
|
2007-06-02 17:03:24 +02:00
|
|
|
return err;
|
|
|
|
|
|
|
|
err:
|
|
|
|
HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
|
|
|
|
HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
|
|
|
|
HeapFree(GetProcessHeap(), 0, *ippdsdb);
|
|
|
|
*ippdsdb = NULL;
|
|
|
|
return err;
|
2007-01-29 16:26:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
|
|
|
|
PIDSDRIVERBUFFER pBuffer,
|
|
|
|
LPVOID *ppvObj)
|
|
|
|
{
|
2010-12-08 01:09:45 +01:00
|
|
|
IDsDriverImpl *This = impl_from_IDsDriver(iface);
|
2007-06-02 17:03:24 +02:00
|
|
|
FIXME("(%p,%p): stub\n",This,pBuffer);
|
2007-01-29 16:26:23 +01:00
|
|
|
return DSERR_INVALIDCALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const IDsDriverVtbl dsdvt =
|
|
|
|
{
|
|
|
|
IDsDriverImpl_QueryInterface,
|
|
|
|
IDsDriverImpl_AddRef,
|
|
|
|
IDsDriverImpl_Release,
|
|
|
|
IDsDriverImpl_GetDriverDesc,
|
|
|
|
IDsDriverImpl_Open,
|
|
|
|
IDsDriverImpl_Close,
|
|
|
|
IDsDriverImpl_GetCaps,
|
|
|
|
IDsDriverImpl_CreateSoundBuffer,
|
|
|
|
IDsDriverImpl_DuplicateSoundBuffer
|
|
|
|
};
|
|
|
|
|
|
|
|
DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
|
|
|
|
{
|
|
|
|
IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
|
|
|
|
|
|
|
|
TRACE("driver created\n");
|
|
|
|
|
|
|
|
*idrv = HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl));
|
|
|
|
if (!*idrv)
|
2007-06-27 23:37:26 +02:00
|
|
|
return MMSYSERR_NOMEM;
|
2010-12-08 01:09:45 +01:00
|
|
|
(*idrv)->IDsDriver_iface.lpVtbl = &dsdvt;
|
2007-01-29 16:26:23 +01:00
|
|
|
(*idrv)->ref = 1;
|
|
|
|
|
|
|
|
(*idrv)->wDevID = wDevID;
|
|
|
|
(*idrv)->primary = NULL;
|
|
|
|
return MMSYSERR_NOERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
|
|
|
|
{
|
2008-03-24 22:37:37 +01:00
|
|
|
*desc = WOutDev[wDevID].ds_desc;
|
2007-01-29 16:26:23 +01:00
|
|
|
return MMSYSERR_NOERROR;
|
|
|
|
}
|