mshtml: Make get_pos_rect() static.
[wine] / dlls / quartz / dsoundrender.c
index a060ddc..f172d87 100644 (file)
 #include "pin.h"
 
 #include "uuids.h"
-#include "mmreg.h"
 #include "vfwmsgs.h"
-#include "fourcc.h"
 #include "windef.h"
 #include "winbase.h"
 #include "dshow.h"
 #include "evcode.h"
 #include "strmif.h"
 #include "dsound.h"
+#include "amaudio.h"
 
 #include "wine/unicode.h"
 #include "wine/debug.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
 
+/* NOTE: buffer can still be filled completely,
+ * but we start waiting until only this amount is buffered
+ */
+static const REFERENCE_TIME DSoundRenderer_Max_Fill = 150 * 10000;
+
 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
 
 static const IBaseFilterVtbl DSoundRender_Vtbl;
 static const IPinVtbl DSoundRender_InputPin_Vtbl;
-static const IMemInputPinVtbl MemInputPin_Vtbl; 
 static const IBasicAudioVtbl IBasicAudio_Vtbl;
+static const IReferenceClockVtbl IReferenceClock_Vtbl;
+static const IMediaSeekingVtbl IMediaSeeking_Vtbl;
+static const IAMDirectSoundVtbl IAMDirectSound_Vtbl;
+static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl;
+static const IQualityControlVtbl DSoundRender_QualityControl_Vtbl = {
+    QualityControlImpl_QueryInterface,
+    QualityControlImpl_AddRef,
+    QualityControlImpl_Release,
+    QualityControlImpl_Notify,
+    QualityControlImpl_SetSink
+};
 
 typedef struct DSoundRenderImpl
 {
-    const IBaseFilterVtbl * lpVtbl;
-    const IBasicAudioVtbl *IBasicAudio_vtbl;
+    BaseFilter filter;
 
-    LONG refCount;
-    CRITICAL_SECTION csFilter;
-    FILTER_STATE state;
-    REFERENCE_TIME rtStreamStart;
-    IReferenceClock * pClock;
-    FILTER_INFO filterInfo;
+    const IBasicAudioVtbl *IBasicAudio_vtbl;
+    const IReferenceClockVtbl *IReferenceClock_vtbl;
+    const IAMDirectSoundVtbl *IAMDirectSound_vtbl;
+    const IAMFilterMiscFlagsVtbl *IAMFilterMiscFlags_vtbl;
+    IUnknown *seekthru_unk;
 
-    InputPin * pInputPin;
-    IPin ** ppPins;
+    BaseInputPin * pInputPin;
+    QualityControlImpl qcimpl;
 
-    LPDIRECTSOUND dsound;
+    IDirectSound8 *dsound;
     LPDIRECTSOUNDBUFFER dsbuffer;
-    DWORD write_pos;
-    BOOL init;
-} DSoundRenderImpl;
+    DWORD buf_size;
+    DWORD in_loop;
+    DWORD last_playpos, writepos;
 
-static HRESULT DSoundRender_InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
-{
-    InputPin * pPinImpl;
+    REFERENCE_TIME play_time;
 
-    *ppPin = NULL;
+    HANDLE state_change, blocked;
 
-    if (pPinInfo->dir != PINDIR_INPUT)
-    {
-        ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
-        return E_INVALIDARG;
-    }
+    LONG volume;
+    LONG pan;
 
-    pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
+    DWORD threadid;
+    HANDLE advisethread, thread_wait;
+} DSoundRenderImpl;
 
-    if (!pPinImpl)
-        return E_OUTOFMEMORY;
+static REFERENCE_TIME time_from_pos(DSoundRenderImpl *This, DWORD pos) {
+    WAVEFORMATEX *wfx = (WAVEFORMATEX*)This->pInputPin->pin.mtCurrent.pbFormat;
+    REFERENCE_TIME ret = 10000000;
+    ret = ret * pos / wfx->nAvgBytesPerSec;
+    return ret;
+}
 
-    if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
-    {
-        pPinImpl->pin.lpVtbl = &DSoundRender_InputPin_Vtbl;
-        pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
-        
-        *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
-        return S_OK;
+static DWORD pos_from_time(DSoundRenderImpl *This, REFERENCE_TIME time) {
+    WAVEFORMATEX *wfx = (WAVEFORMATEX*)This->pInputPin->pin.mtCurrent.pbFormat;
+    REFERENCE_TIME ret = time;
+    ret *= wfx->nSamplesPerSec;
+    ret /= 10000000;
+    ret *= wfx->nBlockAlign;
+    return ret;
+}
+
+static void DSoundRender_UpdatePositions(DSoundRenderImpl *This, DWORD *seqwritepos, DWORD *minwritepos) {
+    WAVEFORMATEX *wfx = (WAVEFORMATEX*)This->pInputPin->pin.mtCurrent.pbFormat;
+    BYTE *buf1, *buf2;
+    DWORD size1, size2, playpos, writepos, old_writepos, old_playpos, adv;
+    BOOL writepos_set = This->writepos < This->buf_size;
+
+    /* Update position and zero */
+    old_writepos = This->writepos;
+    old_playpos = This->last_playpos;
+    if (old_writepos <= old_playpos)
+        old_writepos += This->buf_size;
+
+    IDirectSoundBuffer_GetCurrentPosition(This->dsbuffer, &playpos, &writepos);
+    if (old_playpos > playpos) {
+        adv = This->buf_size + playpos - old_playpos;
+        This->play_time += time_from_pos(This, This->buf_size);
+    } else
+        adv = playpos - old_playpos;
+    This->last_playpos = playpos;
+    if (adv) {
+        TRACE("Moving from %u to %u: clearing %u bytes\n", old_playpos, playpos, adv);
+        IDirectSoundBuffer_Lock(This->dsbuffer, old_playpos, adv, (void**)&buf1, &size1, (void**)&buf2, &size2, 0);
+        memset(buf1, wfx->wBitsPerSample == 8 ? 128  : 0, size1);
+        memset(buf2, wfx->wBitsPerSample == 8 ? 128  : 0, size2);
+        IDirectSoundBuffer_Unlock(This->dsbuffer, buf1, size1, buf2, size2);
     }
-    return E_FAIL;
+    *minwritepos = writepos;
+    if (!writepos_set || old_writepos < writepos) {
+        if (writepos_set) {
+            This->writepos = This->buf_size;
+            FIXME("Underrun of data occurred!\n");
+        }
+        *seqwritepos = writepos;
+    } else
+        *seqwritepos = This->writepos;
 }
 
+static HRESULT DSoundRender_GetWritePos(DSoundRenderImpl *This, DWORD *ret_writepos, REFERENCE_TIME write_at, DWORD *pfree, DWORD *skip)
+{
+    WAVEFORMATEX *wfx = (WAVEFORMATEX*)This->pInputPin->pin.mtCurrent.pbFormat;
+    DWORD writepos, min_writepos, playpos;
+    REFERENCE_TIME max_lag = 50 * 10000;
+    REFERENCE_TIME min_lag = 25 * 10000;
+    REFERENCE_TIME cur, writepos_t, delta_t;
+
+    DSoundRender_UpdatePositions(This, &writepos, &min_writepos);
+    playpos = This->last_playpos;
+    if (This->filter.pClock == (IReferenceClock*)&This->IReferenceClock_vtbl) {
+        max_lag = min_lag;
+        cur = This->play_time + time_from_pos(This, playpos);
+        cur -= This->filter.rtStreamStart;
+    } else if (This->filter.pClock) {
+        IReferenceClock_GetTime(This->filter.pClock, &cur);
+        cur -= This->filter.rtStreamStart;
+    } else
+        write_at = -1;
+
+    if (writepos == min_writepos)
+        max_lag = 0;
+
+    *skip = 0;
+    if (write_at < 0) {
+        *ret_writepos = writepos;
+        goto end;
+    }
 
-#define DSBUFFERSIZE 8192
+    if (writepos >= playpos)
+        writepos_t = cur + time_from_pos(This, writepos - playpos);
+    else
+        writepos_t = cur + time_from_pos(This, This->buf_size + writepos - playpos);
+
+    /* write_at: Starting time of sample */
+    /* cur: current time of play position */
+    /* writepos_t: current time of our pointer play position */
+    delta_t = write_at - writepos_t;
+    if (delta_t >= -max_lag && delta_t <= max_lag) {
+        TRACE("Continuing from old position\n");
+        *ret_writepos = writepos;
+    } else if (delta_t < 0) {
+        REFERENCE_TIME past, min_writepos_t;
+        WARN("Delta too big %i/%i, overwriting old data or even skipping\n", (int)delta_t / 10000, (int)max_lag / 10000);
+        if (min_writepos >= playpos)
+            min_writepos_t = cur + time_from_pos(This, min_writepos - playpos);
+        else
+            min_writepos_t = cur + time_from_pos(This, This->buf_size - playpos + min_writepos);
+        past = min_writepos_t - write_at;
+        if (past >= 0) {
+            DWORD skipbytes = pos_from_time(This, past);
+            WARN("Skipping %u bytes\n", skipbytes);
+            *skip = skipbytes;
+            *ret_writepos = min_writepos;
+        } else {
+            DWORD aheadbytes = pos_from_time(This, -past);
+            WARN("Advancing %u bytes\n", aheadbytes);
+            *ret_writepos = (min_writepos + aheadbytes) % This->buf_size;
+        }
+    } else /* delta_t > 0 */ {
+        DWORD aheadbytes;
+        WARN("Delta too big %i/%i, too far ahead\n", (int)delta_t / 10000, (int)max_lag / 10000);
+        aheadbytes = pos_from_time(This, delta_t);
+        WARN("Advancing %u bytes\n", aheadbytes);
+        if (delta_t >= DSoundRenderer_Max_Fill)
+            return S_FALSE;
+        *ret_writepos = (min_writepos + aheadbytes) % This->buf_size;
+    }
+end:
+    if (playpos > *ret_writepos)
+        *pfree = playpos - *ret_writepos;
+    else if (playpos == *ret_writepos)
+        *pfree = This->buf_size - wfx->nBlockAlign;
+    else
+        *pfree = This->buf_size + playpos - *ret_writepos;
+    if (time_from_pos(This, This->buf_size - *pfree) >= DSoundRenderer_Max_Fill) {
+        TRACE("Blocked: too full %i / %i\n", (int)(time_from_pos(This, This->buf_size - *pfree)/10000), (int)(DSoundRenderer_Max_Fill / 10000));
+        return S_FALSE;
+    }
+    return S_OK;
+}
 
-static HRESULT DSoundRender_CreateSoundBuffer(IBaseFilter * iface)
+static HRESULT DSoundRender_HandleEndOfStream(DSoundRenderImpl *This)
 {
     HRESULT hr;
-    WAVEFORMATEX wav_fmt;
-    AM_MEDIA_TYPE amt;
-    WAVEFORMATEX* format;
-    DSBUFFERDESC buf_desc;
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
-
-    hr = IPin_ConnectionMediaType(This->ppPins[0], &amt);
-    if (FAILED(hr)) {
-       ERR("Unable to retrieve media type\n");
-       return hr;
-    }
+    IMediaEventSink *pEventSink;
 
-    TRACE("MajorType %s\n", debugstr_guid(&amt.majortype));
-    TRACE("SubType %s\n", debugstr_guid(&amt.subtype));
-    TRACE("Format %s\n", debugstr_guid(&amt.formattype));
-    TRACE("Size %d\n", amt.cbFormat);
+    while (1)
+    {
+        DWORD pos1, pos2;
+        DSoundRender_UpdatePositions(This, &pos1, &pos2);
+        if (pos1 == pos2)
+            break;
 
-    dump_AM_MEDIA_TYPE(&amt);
-    
-    format = (WAVEFORMATEX*)amt.pbFormat;
-    TRACE("wFormatTag = %x %x\n", format->wFormatTag, WAVE_FORMAT_PCM);
-    TRACE("nChannels = %d\n", format->nChannels);
-    TRACE("nSamplesPerSec = %u\n", format->nSamplesPerSec);
-    TRACE("nAvgBytesPerSec = %u\n", format->nAvgBytesPerSec);
-    TRACE("nBlockAlign = %d\n", format->nBlockAlign);
-    TRACE("wBitsPerSample = %d\n", format->wBitsPerSample);
-    TRACE("cbSize = %d\n", format->cbSize);
-    
-    hr = DirectSoundCreate(NULL, &This->dsound, NULL);
-    if (FAILED(hr)) {
-       ERR("Cannot create Direct Sound object\n");
-       return hr;
+        This->in_loop = 1;
+        LeaveCriticalSection(&This->filter.csFilter);
+        WaitForSingleObject(This->blocked, 10);
+        EnterCriticalSection(&This->filter.csFilter);
+        This->in_loop = 0;
+        if (This->pInputPin->flushing ||
+            This->filter.state != State_Running) {
+            SetEvent(This->state_change);
+            return S_FALSE;
+        }
     }
 
-    wav_fmt = *format;
-    wav_fmt.cbSize = 0;
+    if (!This->filter.filterInfo.pGraph)
+        return S_OK;
 
-    memset(&buf_desc,0,sizeof(DSBUFFERDESC));
-    buf_desc.dwSize = sizeof(DSBUFFERDESC);
-    buf_desc.dwBufferBytes = DSBUFFERSIZE;
-    buf_desc.lpwfxFormat = &wav_fmt;
-    hr = IDirectSound_CreateSoundBuffer(This->dsound, &buf_desc, &This->dsbuffer, NULL);
-    if (FAILED(hr)) {
-        ERR("Can't create sound buffer !\n");
-        IDirectSound_Release(This->dsound);
-        return hr;
-    }
-    hr = IDirectSoundBuffer_Play(This->dsbuffer, 0, 0, DSBPLAY_LOOPING);
-    if (FAILED(hr)) {
-        ERR("Can't start sound buffer (%x)!\n", hr);
-        IDirectSoundBuffer_Release(This->dsbuffer);
-        IDirectSound_Release(This->dsound);
-        return hr;
+    hr = IFilterGraph_QueryInterface(This->filter.filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
+    if (SUCCEEDED(hr))
+    {
+        hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, (LONG_PTR)This);
+        IMediaEventSink_Release(pEventSink);
     }
-
-    This->write_pos = 0;
-    
     return hr;
 }
 
-static HRESULT DSoundRender_SendSampleData(DSoundRenderImpl* This, LPBYTE data, DWORD size)
+static HRESULT DSoundRender_SendSampleData(DSoundRenderImpl* This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, const BYTE *data, DWORD size)
 {
     HRESULT hr;
-    LPBYTE lpbuf1 = NULL;
-    LPBYTE lpbuf2 = NULL;
-    DWORD dwsize1 = 0;
-    DWORD dwsize2 = 0;
-    DWORD size2;
-    DWORD play_pos,buf_free;
-
-    do {
-        hr = IDirectSoundBuffer_GetCurrentPosition(This->dsbuffer, &play_pos, NULL);
-        if (hr != DS_OK)
-        {
-            ERR("Error GetCurrentPosition: %x\n", hr);
-            break;
-        }
-        if (This->write_pos < play_pos)
-             buf_free = play_pos-This->write_pos;
-        else
-             buf_free = DSBUFFERSIZE - This->write_pos + play_pos;
 
-        /* This situation is ambiguous; Assume full when playing */
-        if(buf_free == DSBUFFERSIZE)
-        {
-            Sleep(10);
+    while (size && This->filter.state != State_Stopped) {
+        DWORD writepos, skip = 0, free, size1, size2, ret;
+        BYTE *buf1, *buf2;
+
+        if (This->filter.state == State_Running)
+            hr = DSoundRender_GetWritePos(This, &writepos, tStart, &free, &skip);
+        else
+            hr = S_FALSE;
+
+        if (hr != S_OK) {
+            This->in_loop = 1;
+            LeaveCriticalSection(&This->filter.csFilter);
+            ret = WaitForSingleObject(This->blocked, 10);
+            EnterCriticalSection(&This->filter.csFilter);
+            This->in_loop = 0;
+            if (This->pInputPin->flushing ||
+                This->filter.state == State_Stopped) {
+                SetEvent(This->state_change);
+                return This->filter.state == State_Paused ? S_OK : VFW_E_WRONG_STATE;
+            }
+            if (ret != WAIT_TIMEOUT)
+                ERR("%x\n", ret);
             continue;
         }
+        tStart = -1;
+
+        if (skip)
+            FIXME("Sample dropped %u of %u bytes\n", skip, size);
+        if (skip >= size)
+            return S_OK;
+        data += skip;
+        size -= skip;
 
-        size2 = min(buf_free, size);
-        hr = IDirectSoundBuffer_Lock(This->dsbuffer, This->write_pos, size2, (LPVOID *)&lpbuf1, &dwsize1, (LPVOID *)&lpbuf2, &dwsize2, 0);
+        hr = IDirectSoundBuffer_Lock(This->dsbuffer, writepos, min(free, size), (void**)&buf1, &size1, (void**)&buf2, &size2, 0);
         if (hr != DS_OK) {
             ERR("Unable to lock sound buffer! (%x)\n", hr);
             break;
         }
-        /* TRACE("write_pos=%d, size=%d, sz1=%d, sz2=%d\n", This->write_pos, size2, dwsize1, dwsize2); */
-
-        memcpy(lpbuf1, data, dwsize1);
-        if (dwsize2)
-            memcpy(lpbuf2, data + dwsize1, dwsize2);
-
-        hr = IDirectSoundBuffer_Unlock(This->dsbuffer, lpbuf1, dwsize1, lpbuf2, dwsize2);
-        if (hr != DS_OK)
-            ERR("Unable to unlock sound buffer! (%x)\n", hr);
-
-        size -= dwsize1 + dwsize2;
-        data += dwsize1 + dwsize2;
-        This->write_pos = (This->write_pos + dwsize1 + dwsize2) % DSBUFFERSIZE;
-
-        if (!size)
-            break;
-        Sleep(10);
-    } while (This->state == State_Running);
-
-    return hr;
+        memcpy(buf1, data, size1);
+        if (size2)
+            memcpy(buf2, data+size1, size2);
+        IDirectSoundBuffer_Unlock(This->dsbuffer, buf1, size1, buf2, size2);
+        This->writepos = (writepos + size1 + size2) % This->buf_size;
+        TRACE("Wrote %u bytes at %u, next at %u - (%u/%u)\n", size1+size2, writepos, This->writepos, free, size);
+        data += size1 + size2;
+        size -= size1 + size2;
+    }
+    return S_OK;
 }
 
-static HRESULT DSoundRender_Sample(LPVOID iface, IMediaSample * pSample)
+static HRESULT WINAPI DSoundRender_Receive(BaseInputPin *pin, IMediaSample * pSample)
 {
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
+    DSoundRenderImpl *This = (DSoundRenderImpl*)pin->pin.pinInfo.pFilter;
     LPBYTE pbSrcStream = NULL;
-    long cbSrcStream = 0;
+    LONG cbSrcStream = 0;
     REFERENCE_TIME tStart, tStop;
     HRESULT hr;
+    AM_MEDIA_TYPE *amt;
+
+    TRACE("%p %p\n", pin, pSample);
 
-    TRACE("%p %p\n", iface, pSample);
+    /* Slightly incorrect, Pause completes when a frame is received so we should signal
+     * pause completion here, but for sound playing a single frame doesn't make sense
+     */
 
-    if (This->state != State_Running)
+    EnterCriticalSection(&This->filter.csFilter);
+
+    if (This->pInputPin->end_of_stream || This->pInputPin->flushing)
+    {
+        LeaveCriticalSection(&This->filter.csFilter);
+        return S_FALSE;
+    }
+
+    if (This->filter.state == State_Stopped)
+    {
+        LeaveCriticalSection(&This->filter.csFilter);
         return VFW_E_WRONG_STATE;
-    
+    }
+
+    if (IMediaSample_GetMediaType(pSample, &amt) == S_OK)
+    {
+        AM_MEDIA_TYPE *orig = &This->pInputPin->pin.mtCurrent;
+        WAVEFORMATEX *origfmt = (WAVEFORMATEX *)orig->pbFormat;
+        WAVEFORMATEX *newfmt = (WAVEFORMATEX *)amt->pbFormat;
+
+        if (origfmt->wFormatTag == newfmt->wFormatTag &&
+            origfmt->nChannels == newfmt->nChannels &&
+            origfmt->nBlockAlign == newfmt->nBlockAlign &&
+            origfmt->wBitsPerSample == newfmt->wBitsPerSample &&
+            origfmt->cbSize ==  newfmt->cbSize)
+        {
+            if (origfmt->nSamplesPerSec != newfmt->nSamplesPerSec)
+            {
+                hr = IDirectSoundBuffer_SetFrequency(This->dsbuffer,
+                                                     newfmt->nSamplesPerSec);
+                if (FAILED(hr))
+                {
+                    LeaveCriticalSection(&This->filter.csFilter);
+                    return VFW_E_TYPE_NOT_ACCEPTED;
+                }
+                FreeMediaType(orig);
+                CopyMediaType(orig, amt);
+                IMediaSample_SetMediaType(pSample, NULL);
+            }
+        }
+        else
+        {
+            LeaveCriticalSection(&This->filter.csFilter);
+            return VFW_E_TYPE_NOT_ACCEPTED;
+        }
+    }
+
     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
     if (FAILED(hr))
     {
         ERR("Cannot get pointer to sample data (%x)\n", hr);
-       return hr;
+        LeaveCriticalSection(&This->filter.csFilter);
+        return hr;
     }
 
+    if (IMediaSample_GetMediaTime(pSample, &tStart, &tStop) == S_OK)
+        MediaSeekingPassThru_RegisterMediaTime(This->seekthru_unk, tStart);
     hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
-    if (FAILED(hr))
+    if (FAILED(hr)) {
         ERR("Cannot get sample time (%x)\n", hr);
+        tStart = tStop = -1;
+    }
 
-    cbSrcStream = IMediaSample_GetActualDataLength(pSample);
-
-    TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, cbSrcStream);
+    IMediaSample_IsDiscontinuity(pSample);
 
-#if 0 /* For debugging purpose */
+    if (IMediaSample_IsPreroll(pSample) == S_OK)
     {
-        int i;
-        for(i = 0; i < cbSrcStream; i++)
-        {
-           if ((i!=0) && !(i%16))
-                TRACE("\n");
-            TRACE("%02x ", pbSrcStream[i]);
-        }
-        TRACE("\n");
-    }
-#endif
-  
-    if (!This->init)
-    {
-        hr = DSoundRender_CreateSoundBuffer(iface);
-        if (SUCCEEDED(hr))
-            This->init = TRUE;
-        else
-        {
-            ERR("Unable to create DSound buffer\n");
-            return hr;
-        }
+        TRACE("Preroll!\n");
+        LeaveCriticalSection(&This->filter.csFilter);
+        return S_OK;
     }
-    
-    hr = DSoundRender_SendSampleData(This, pbSrcStream, cbSrcStream);
 
+    cbSrcStream = IMediaSample_GetActualDataLength(pSample);
+    TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream, cbSrcStream);
+
+    SetEvent(This->state_change);
+    hr = DSoundRender_SendSampleData(This, tStart, tStop, pbSrcStream, cbSrcStream);
+    if (This->filter.state == State_Running && This->filter.pClock && tStart >= 0) {
+        REFERENCE_TIME jitter, now = 0;
+        Quality q;
+        IReferenceClock_GetTime(This->filter.pClock, &now);
+        jitter = now - This->filter.rtStreamStart - tStart;
+        if (jitter <= -DSoundRenderer_Max_Fill)
+            jitter += DSoundRenderer_Max_Fill;
+        else if (jitter < 0)
+            jitter = 0;
+        q.Type = (jitter > 0 ? Famine : Flood);
+        q.Proportion = 1.;
+        q.Late = jitter;
+        q.TimeStamp = tStart;
+        IQualityControl_Notify((IQualityControl *)&This->qcimpl, (IBaseFilter*)This, q);
+    }
+    LeaveCriticalSection(&This->filter.csFilter);
     return hr;
 }
 
-static HRESULT DSoundRender_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
+static HRESULT WINAPI DSoundRender_CheckMediaType(BasePin *iface, const AM_MEDIA_TYPE * pmt)
 {
-    WAVEFORMATEX* format = (WAVEFORMATEX*)pmt->pbFormat;
+    WAVEFORMATEX* format;
+
+    if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio))
+        return S_FALSE;
+
+    format =  (WAVEFORMATEX*)pmt->pbFormat;
+    TRACE("Format = %p\n", format);
     TRACE("wFormatTag = %x %x\n", format->wFormatTag, WAVE_FORMAT_PCM);
     TRACE("nChannels = %d\n", format->nChannels);
     TRACE("nSamplesPerSec = %d\n", format->nAvgBytesPerSec);
@@ -287,11 +443,46 @@ static HRESULT DSoundRender_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
     TRACE("nBlockAlign = %d\n", format->nBlockAlign);
     TRACE("wBitsPerSample = %d\n", format->wBitsPerSample);
 
-    if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio) && IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_PCM))
-        return S_OK;
-    return S_FALSE;
+    if (!IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_PCM))
+        return S_FALSE;
+
+    return S_OK;
+}
+
+static IPin* WINAPI DSoundRender_GetPin(BaseFilter *iface, int pos)
+{
+    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
+
+    if (pos >= 1 || pos < 0)
+        return NULL;
+
+    IPin_AddRef((IPin*)This->pInputPin);
+    return (IPin*)This->pInputPin;
 }
 
+static LONG WINAPI DSoundRender_GetPinCount(BaseFilter *iface)
+{
+    /* Our pins are static */
+    return 1;
+}
+
+static const BaseFilterFuncTable BaseFuncTable = {
+    DSoundRender_GetPin,
+    DSoundRender_GetPinCount
+};
+
+static const  BasePinFuncTable input_BaseFuncTable = {
+    DSoundRender_CheckMediaType,
+    NULL,
+    BasePinImpl_GetMediaTypeVersion,
+    BasePinImpl_GetMediaType
+};
+
+static const BaseInputPinFuncTable input_BaseInputFuncTable = {
+    DSoundRender_Receive
+};
+
+
 HRESULT DSoundRender_create(IUnknown * pUnkOuter, LPVOID * ppv)
 {
     HRESULT hr;
@@ -310,38 +501,65 @@ HRESULT DSoundRender_create(IUnknown * pUnkOuter, LPVOID * ppv)
         return E_OUTOFMEMORY;
     ZeroMemory(pDSoundRender, sizeof(DSoundRenderImpl));
 
-    pDSoundRender->lpVtbl = &DSoundRender_Vtbl;
-    pDSoundRender->IBasicAudio_vtbl = &IBasicAudio_Vtbl;
-    pDSoundRender->refCount = 1;
-    InitializeCriticalSection(&pDSoundRender->csFilter);
-    pDSoundRender->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DSoundRenderImpl.csFilter");
-    pDSoundRender->state = State_Stopped;
+    BaseFilter_Init(&pDSoundRender->filter, &DSoundRender_Vtbl, &CLSID_DSoundRender, (DWORD_PTR)(__FILE__ ": DSoundRenderImpl.csFilter"), &BaseFuncTable);
 
-    pDSoundRender->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
-    if (!pDSoundRender->ppPins)
-    {
-        pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
-        DeleteCriticalSection(&pDSoundRender->csFilter);
-        CoTaskMemFree(pDSoundRender);
-        return E_OUTOFMEMORY;
-    }
+    pDSoundRender->IBasicAudio_vtbl = &IBasicAudio_Vtbl;
+    pDSoundRender->IReferenceClock_vtbl = &IReferenceClock_Vtbl;
+    pDSoundRender->IAMDirectSound_vtbl = &IAMDirectSound_Vtbl;
+    pDSoundRender->IAMFilterMiscFlags_vtbl = &IAMFilterMiscFlags_Vtbl;
 
     /* construct input pin */
     piInput.dir = PINDIR_INPUT;
     piInput.pFilter = (IBaseFilter *)pDSoundRender;
     lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
-    hr = DSoundRender_InputPin_Construct(&piInput, DSoundRender_Sample, (LPVOID)pDSoundRender, DSoundRender_QueryAccept, &pDSoundRender->csFilter, (IPin **)&pDSoundRender->pInputPin);
+    hr = BaseInputPin_Construct(&DSoundRender_InputPin_Vtbl, &piInput, &input_BaseFuncTable, &input_BaseInputFuncTable, &pDSoundRender->filter.csFilter, NULL, (IPin **)&pDSoundRender->pInputPin);
+
+    if (SUCCEEDED(hr))
+    {
+        hr = DirectSoundCreate8(NULL, &pDSoundRender->dsound, NULL);
+        if (FAILED(hr))
+            ERR("Cannot create Direct Sound object (%x)\n", hr);
+        else
+            hr = IDirectSound_SetCooperativeLevel(pDSoundRender->dsound, GetDesktopWindow(), DSSCL_PRIORITY);
+        if (SUCCEEDED(hr)) {
+            IDirectSoundBuffer *buf;
+            DSBUFFERDESC buf_desc;
+            memset(&buf_desc,0,sizeof(DSBUFFERDESC));
+            buf_desc.dwSize = sizeof(DSBUFFERDESC);
+            buf_desc.dwFlags = DSBCAPS_PRIMARYBUFFER;
+            hr = IDirectSound_CreateSoundBuffer(pDSoundRender->dsound, &buf_desc, &buf, NULL);
+            if (SUCCEEDED(hr)) {
+                IDirectSoundBuffer_Play(buf, 0, 0, DSBPLAY_LOOPING);
+                IUnknown_Release(buf);
+            }
+            hr = S_OK;
+        }
+    }
 
     if (SUCCEEDED(hr))
     {
-        pDSoundRender->ppPins[0] = (IPin *)pDSoundRender->pInputPin;
-        *ppv = (LPVOID)pDSoundRender;
+        ISeekingPassThru *passthru;
+        pDSoundRender->state_change = CreateEventW(NULL, TRUE, TRUE, NULL);
+        pDSoundRender->blocked = CreateEventW(NULL, TRUE, TRUE, NULL);
+        hr = CoCreateInstance(&CLSID_SeekingPassThru, (IUnknown*)pDSoundRender, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void**)&pDSoundRender->seekthru_unk);
+        if (!pDSoundRender->state_change || !pDSoundRender->blocked || FAILED(hr))
+        {
+            IUnknown_Release((IUnknown *)pDSoundRender);
+            return HRESULT_FROM_WIN32(GetLastError());
+        }
+
+        IUnknown_QueryInterface(pDSoundRender->seekthru_unk, &IID_ISeekingPassThru, (void**)&passthru);
+        ISeekingPassThru_Init(passthru, TRUE, (IPin*)pDSoundRender->pInputPin);
+        ISeekingPassThru_Release(passthru);
+        QualityControlImpl_init(&pDSoundRender->qcimpl, (IPin*)pDSoundRender->pInputPin, (IBaseFilter*)pDSoundRender);
+        pDSoundRender->qcimpl.lpVtbl = &DSoundRender_QualityControl_Vtbl;
+        *ppv = pDSoundRender;
     }
     else
     {
-        CoTaskMemFree(pDSoundRender->ppPins);
-        pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
-        DeleteCriticalSection(&pDSoundRender->csFilter);
+        if (pDSoundRender->pInputPin)
+            IPin_Release((IPin*)pDSoundRender->pInputPin);
+        BaseFilterImpl_Release((IBaseFilter*)pDSoundRender);
         CoTaskMemFree(pDSoundRender);
     }
 
@@ -356,15 +574,25 @@ static HRESULT WINAPI DSoundRender_QueryInterface(IBaseFilter * iface, REFIID ri
     *ppv = NULL;
 
     if (IsEqualIID(riid, &IID_IUnknown))
-        *ppv = (LPVOID)This;
+        *ppv = This;
     else if (IsEqualIID(riid, &IID_IPersist))
-        *ppv = (LPVOID)This;
+        *ppv = This;
     else if (IsEqualIID(riid, &IID_IMediaFilter))
-        *ppv = (LPVOID)This;
+        *ppv = This;
     else if (IsEqualIID(riid, &IID_IBaseFilter))
-        *ppv = (LPVOID)This;
+        *ppv = This;
     else if (IsEqualIID(riid, &IID_IBasicAudio))
-        *ppv = (LPVOID)&(This->IBasicAudio_vtbl);
+        *ppv = &This->IBasicAudio_vtbl;
+    else if (IsEqualIID(riid, &IID_IReferenceClock))
+        *ppv = &This->IReferenceClock_vtbl;
+    else if (IsEqualIID(riid, &IID_IMediaSeeking))
+        return IUnknown_QueryInterface(This->seekthru_unk, riid, ppv);
+    else if (IsEqualIID(riid, &IID_IAMDirectSound))
+        *ppv = &This->IAMDirectSound_vtbl;
+    else if (IsEqualIID(riid, &IID_IAMFilterMiscFlags))
+        *ppv = &This->IAMFilterMiscFlags_vtbl;
+    else if (IsEqualIID(riid, &IID_IQualityControl))
+        *ppv = &This->qcimpl;
 
     if (*ppv)
     {
@@ -372,25 +600,16 @@ static HRESULT WINAPI DSoundRender_QueryInterface(IBaseFilter * iface, REFIID ri
         return S_OK;
     }
 
-    FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
+    if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
+        FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
 
     return E_NOINTERFACE;
 }
 
-static ULONG WINAPI DSoundRender_AddRef(IBaseFilter * iface)
-{
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
-    ULONG refCount = InterlockedIncrement(&This->refCount);
-
-    TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
-
-    return refCount;
-}
-
 static ULONG WINAPI DSoundRender_Release(IBaseFilter * iface)
 {
     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
-    ULONG refCount = InterlockedDecrement(&This->refCount);
+    ULONG refCount = BaseFilterImpl_Release(iface);
 
     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
 
@@ -398,8 +617,11 @@ static ULONG WINAPI DSoundRender_Release(IBaseFilter * iface)
     {
         IPin *pConnectedTo;
 
-        if (This->pClock)
-            IReferenceClock_Release(This->pClock);
+        if (This->threadid) {
+            PostThreadMessageW(This->threadid, WM_APP, 0, 0);
+            WaitForSingleObject(This->advisethread, INFINITE);
+            CloseHandle(This->advisethread);
+        }
 
         if (This->dsbuffer)
             IDirectSoundBuffer_Release(This->dsbuffer);
@@ -407,22 +629,21 @@ static ULONG WINAPI DSoundRender_Release(IBaseFilter * iface)
         if (This->dsound)
             IDirectSound_Release(This->dsound);
         This->dsound = NULL;
-       
-        if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[0], &pConnectedTo)))
+        if (SUCCEEDED(IPin_ConnectedTo((IPin *)This->pInputPin, &pConnectedTo)))
         {
             IPin_Disconnect(pConnectedTo);
             IPin_Release(pConnectedTo);
         }
-        IPin_Disconnect(This->ppPins[0]);
+        IPin_Disconnect((IPin *)This->pInputPin);
+
+        IPin_Release((IPin *)This->pInputPin);
 
-        IPin_Release(This->ppPins[0]);
-        
-        CoTaskMemFree(This->ppPins);
-        This->lpVtbl = NULL;
         This->IBasicAudio_vtbl = NULL;
-        
-        This->csFilter.DebugInfo->Spare[0] = 0;
-        DeleteCriticalSection(&This->csFilter);
+        if (This->seekthru_unk)
+            IUnknown_Release(This->seekthru_unk);
+
+        CloseHandle(This->state_change);
+        CloseHandle(This->blocked);
 
         TRACE("Destroying Audio Renderer\n");
         CoTaskMemFree(This);
@@ -433,18 +654,6 @@ static ULONG WINAPI DSoundRender_Release(IBaseFilter * iface)
         return refCount;
 }
 
-/** IPersist methods **/
-
-static HRESULT WINAPI DSoundRender_GetClassID(IBaseFilter * iface, CLSID * pClsid)
-{
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
-    TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
-
-    *pClsid = CLSID_DSoundRender;
-
-    return S_OK;
-}
-
 /** IMediaFilter methods **/
 
 static HRESULT WINAPI DSoundRender_Stop(IBaseFilter * iface)
@@ -454,22 +663,19 @@ static HRESULT WINAPI DSoundRender_Stop(IBaseFilter * iface)
 
     TRACE("(%p/%p)->()\n", This, iface);
 
-    EnterCriticalSection(&This->csFilter);
+    EnterCriticalSection(&This->filter.csFilter);
     {
-        DWORD state = 0;
-        if (This->dsbuffer)
-        {
-            hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
-            if (SUCCEEDED(hr))
-            {
-                if (state & DSBSTATUS_PLAYING)
-                    hr = IDirectSoundBuffer_Stop(This->dsbuffer);
-            }
-        }
+        hr = IDirectSoundBuffer_Stop(This->dsbuffer);
         if (SUCCEEDED(hr))
-            This->state = State_Stopped;
+            This->filter.state = State_Stopped;
+
+        /* Complete our transition */
+        This->writepos = This->buf_size;
+        SetEvent(This->state_change);
+        SetEvent(This->blocked);
+        MediaSeekingPassThru_ResetMediaTime(This->seekthru_unk);
     }
-    LeaveCriticalSection(&This->csFilter);
+    LeaveCriticalSection(&This->filter.csFilter);
     
     return hr;
 }
@@ -481,22 +687,22 @@ static HRESULT WINAPI DSoundRender_Pause(IBaseFilter * iface)
     
     TRACE("(%p/%p)->()\n", This, iface);
 
-    EnterCriticalSection(&This->csFilter);
+    EnterCriticalSection(&This->filter.csFilter);
+    if (This->filter.state != State_Paused)
     {
-        DWORD state = 0;
-        if (This->dsbuffer)
+        if (This->filter.state == State_Stopped)
         {
-            hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
-            if (SUCCEEDED(hr))
-            {
-                if (state & DSBSTATUS_PLAYING)
-                    hr = IDirectSoundBuffer_Stop(This->dsbuffer);
-            }
+            This->pInputPin->end_of_stream = 0;
+            ResetEvent(This->state_change);
         }
+
+        hr = IDirectSoundBuffer_Stop(This->dsbuffer);
         if (SUCCEEDED(hr))
-            This->state = State_Paused;
+            This->filter.state = State_Paused;
+
+        ResetEvent(This->blocked);
     }
-    LeaveCriticalSection(&This->csFilter);
+    LeaveCriticalSection(&This->filter.csFilter);
 
     return hr;
 }
@@ -508,215 +714,297 @@ static HRESULT WINAPI DSoundRender_Run(IBaseFilter * iface, REFERENCE_TIME tStar
 
     TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
 
-    EnterCriticalSection(&This->csFilter);
+    EnterCriticalSection(&This->filter.csFilter);
+    if (This->pInputPin->pin.pConnectedTo)
     {
-        /* It's okay if there's no buffer yet. It'll start when it's created */
-        if (This->dsbuffer)
+        This->filter.rtStreamStart = tStart;
+        QualityControlRender_Start(&This->qcimpl, tStart);
+        if (This->filter.state == State_Paused)
         {
-            hr = IDirectSoundBuffer_Play(This->dsbuffer, 0, 0, DSBPLAY_LOOPING);
-            if (FAILED(hr))
-                ERR("Can't start playing! (%x)\n", hr);
+            /* Unblock our thread, state changing from paused to running doesn't need a reset for state change */
+            SetEvent(This->blocked);
         }
+        else if (This->filter.state == State_Stopped)
+        {
+            ResetEvent(This->state_change);
+            This->pInputPin->end_of_stream = 0;
+        }
+        IDirectSoundBuffer_Play(This->dsbuffer, 0, 0, DSBPLAY_LOOPING);
+        ResetEvent(This->blocked);
+    } else if (This->filter.filterInfo.pGraph) {
+        IMediaEventSink *pEventSink;
+        hr = IFilterGraph_QueryInterface(This->filter.filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
         if (SUCCEEDED(hr))
         {
-            This->rtStreamStart = tStart;
-            This->state = State_Running;
+            hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, (LONG_PTR)This);
+            IMediaEventSink_Release(pEventSink);
         }
+        hr = S_OK;
     }
-    LeaveCriticalSection(&This->csFilter);
+    if (SUCCEEDED(hr))
+        This->filter.state = State_Running;
+    LeaveCriticalSection(&This->filter.csFilter);
 
     return hr;
 }
 
 static HRESULT WINAPI DSoundRender_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
 {
+    HRESULT hr;
     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
 
     TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
 
-    EnterCriticalSection(&This->csFilter);
-    {
-        *pState = This->state;
-    }
-    LeaveCriticalSection(&This->csFilter);
+    if (WaitForSingleObject(This->state_change, dwMilliSecsTimeout) == WAIT_TIMEOUT)
+        hr = VFW_S_STATE_INTERMEDIATE;
+    else
+        hr = S_OK;
 
-    return S_OK;
+    BaseFilterImpl_GetState(iface, dwMilliSecsTimeout, pState);
+
+    return hr;
 }
 
-static HRESULT WINAPI DSoundRender_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
+/** IBaseFilter implementation **/
+
+static HRESULT WINAPI DSoundRender_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
 {
     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
 
-    TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
+    TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_w(Id), ppPin);
+    
+    FIXME("DSoundRender::FindPin(...)\n");
 
-    EnterCriticalSection(&This->csFilter);
-    {
-        if (This->pClock)
-            IReferenceClock_Release(This->pClock);
-        This->pClock = pClock;
-        if (This->pClock)
-            IReferenceClock_AddRef(This->pClock);
-    }
-    LeaveCriticalSection(&This->csFilter);
+    /* FIXME: critical section */
 
-    return S_OK;
+    return E_NOTIMPL;
 }
 
-static HRESULT WINAPI DSoundRender_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
+static const IBaseFilterVtbl DSoundRender_Vtbl =
+{
+    DSoundRender_QueryInterface,
+    BaseFilterImpl_AddRef,
+    DSoundRender_Release,
+    BaseFilterImpl_GetClassID,
+    DSoundRender_Stop,
+    DSoundRender_Pause,
+    DSoundRender_Run,
+    DSoundRender_GetState,
+    BaseFilterImpl_SetSyncSource,
+    BaseFilterImpl_GetSyncSource,
+    BaseFilterImpl_EnumPins,
+    DSoundRender_FindPin,
+    BaseFilterImpl_QueryFilterInfo,
+    BaseFilterImpl_JoinFilterGraph,
+    BaseFilterImpl_QueryVendorInfo
+};
+
+static HRESULT WINAPI DSoundRender_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
 {
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
+    BaseInputPin *This = (BaseInputPin *)iface;
+    PIN_DIRECTION pindirReceive;
+    DSoundRenderImpl *DSImpl;
+    HRESULT hr = S_OK;
 
-    TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
+    TRACE("(%p)->(%p, %p)\n", This, pReceivePin, pmt);
+    dump_AM_MEDIA_TYPE(pmt);
 
-    EnterCriticalSection(&This->csFilter);
+    EnterCriticalSection(This->pin.pCritSec);
     {
-        *ppClock = This->pClock;
-        IReferenceClock_AddRef(This->pClock);
-    }
-    LeaveCriticalSection(&This->csFilter);
-    
-    return S_OK;
-}
+        DSImpl = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
 
-/** IBaseFilter implementation **/
+        if (This->pin.pConnectedTo)
+            hr = VFW_E_ALREADY_CONNECTED;
 
-static HRESULT WINAPI DSoundRender_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
-{
-    ENUMPINDETAILS epd;
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
+        if (SUCCEEDED(hr) && This->pin.pFuncsTable->pfnCheckMediaType((BasePin*)This, pmt) != S_OK)
+            hr = VFW_E_TYPE_NOT_ACCEPTED;
 
-    TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
+        if (SUCCEEDED(hr))
+        {
+            IPin_QueryDirection(pReceivePin, &pindirReceive);
 
-    epd.cPins = 1; /* input pin */
-    epd.ppPins = This->ppPins;
-    return IEnumPinsImpl_Construct(&epd, ppEnum);
-}
+            if (pindirReceive != PINDIR_OUTPUT)
+            {
+                ERR("Can't connect from non-output pin\n");
+                hr = VFW_E_INVALID_DIRECTION;
+            }
+        }
 
-static HRESULT WINAPI DSoundRender_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
-{
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
+        if (SUCCEEDED(hr))
+        {
+            WAVEFORMATEX *format;
+            DSBUFFERDESC buf_desc;
+
+            TRACE("MajorType %s\n", debugstr_guid(&pmt->majortype));
+            TRACE("SubType %s\n", debugstr_guid(&pmt->subtype));
+            TRACE("Format %s\n", debugstr_guid(&pmt->formattype));
+            TRACE("Size %d\n", pmt->cbFormat);
+
+            format = (WAVEFORMATEX*)pmt->pbFormat;
+
+            DSImpl->buf_size = format->nAvgBytesPerSec;
+
+            memset(&buf_desc,0,sizeof(DSBUFFERDESC));
+            buf_desc.dwSize = sizeof(DSBUFFERDESC);
+            buf_desc.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN |
+                               DSBCAPS_CTRLFREQUENCY | DSBCAPS_GLOBALFOCUS |
+                               DSBCAPS_GETCURRENTPOSITION2;
+            buf_desc.dwBufferBytes = DSImpl->buf_size;
+            buf_desc.lpwfxFormat = format;
+            hr = IDirectSound_CreateSoundBuffer(DSImpl->dsound, &buf_desc, &DSImpl->dsbuffer, NULL);
+            DSImpl->writepos = DSImpl->buf_size;
+            if (FAILED(hr))
+                ERR("Can't create sound buffer (%x)\n", hr);
+        }
 
-    TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_w(Id), ppPin);
-    
-    FIXME("DSoundRender::FindPin(...)\n");
+        if (SUCCEEDED(hr))
+        {
+            hr = IDirectSoundBuffer_SetVolume(DSImpl->dsbuffer, DSImpl->volume);
+            if (FAILED(hr))
+                ERR("Can't set volume to %d (%x)\n", DSImpl->volume, hr);
 
-    /* FIXME: critical section */
+            hr = IDirectSoundBuffer_SetPan(DSImpl->dsbuffer, DSImpl->pan);
+            if (FAILED(hr))
+                ERR("Can't set pan to %d (%x)\n", DSImpl->pan, hr);
+            hr = S_OK;
+        }
 
-    return E_NOTIMPL;
+        if (SUCCEEDED(hr))
+        {
+            CopyMediaType(&This->pin.mtCurrent, pmt);
+            This->pin.pConnectedTo = pReceivePin;
+            IPin_AddRef(pReceivePin);
+        }
+        else if (hr != VFW_E_ALREADY_CONNECTED)
+        {
+            if (DSImpl->dsbuffer)
+                IDirectSoundBuffer_Release(DSImpl->dsbuffer);
+            DSImpl->dsbuffer = NULL;
+        }
+    }
+    LeaveCriticalSection(This->pin.pCritSec);
+
+    return hr;
 }
 
-static HRESULT WINAPI DSoundRender_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
+static HRESULT WINAPI DSoundRender_InputPin_Disconnect(IPin * iface)
 {
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
+    BasePin *This = (BasePin*)iface;
+    DSoundRenderImpl *DSImpl;
 
-    TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
+    TRACE("(%p)->()\n", iface);
 
-    strcpyW(pInfo->achName, This->filterInfo.achName);
-    pInfo->pGraph = This->filterInfo.pGraph;
+    DSImpl = (DSoundRenderImpl*)This->pinInfo.pFilter;
+    if (DSImpl->threadid) {
+        PostThreadMessageW(DSImpl->threadid, WM_APP, 0, 0);
+        WaitForSingleObject(DSImpl->advisethread, INFINITE);
+        CloseHandle(DSImpl->advisethread);
+    }
+    if (DSImpl->dsbuffer)
+        IDirectSoundBuffer_Release(DSImpl->dsbuffer);
+    DSImpl->dsbuffer = NULL;
 
-    if (pInfo->pGraph)
-        IFilterGraph_AddRef(pInfo->pGraph);
-    
-    return S_OK;
+    return BasePinImpl_Disconnect(iface);
 }
 
-static HRESULT WINAPI DSoundRender_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
+static HRESULT WINAPI DSoundRender_InputPin_EndOfStream(IPin * iface)
 {
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
+    BaseInputPin* This = (BaseInputPin*)iface;
+    DSoundRenderImpl *me = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
+    HRESULT hr;
 
-    TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
+    EnterCriticalSection(This->pin.pCritSec);
 
-    EnterCriticalSection(&This->csFilter);
+    TRACE("(%p/%p)->()\n", This, iface);
+    hr = BaseInputPinImpl_EndOfStream(iface);
+    if (hr != S_OK)
     {
-        if (pName)
-            strcpyW(This->filterInfo.achName, pName);
-        else
-            *This->filterInfo.achName = '\0';
-        This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
+        ERR("%08x\n", hr);
+        LeaveCriticalSection(This->pin.pCritSec);
+        return hr;
     }
-    LeaveCriticalSection(&This->csFilter);
 
-    return S_OK;
-}
+    hr = DSoundRender_HandleEndOfStream(me);
+    MediaSeekingPassThru_EOS(me->seekthru_unk);
+    SetEvent(me->state_change);
+    LeaveCriticalSection(This->pin.pCritSec);
 
-static HRESULT WINAPI DSoundRender_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
-{
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
-    TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
-    return E_NOTIMPL;
+    return hr;
 }
 
-static const IBaseFilterVtbl DSoundRender_Vtbl =
+static HRESULT WINAPI DSoundRender_InputPin_BeginFlush(IPin * iface)
 {
-    DSoundRender_QueryInterface,
-    DSoundRender_AddRef,
-    DSoundRender_Release,
-    DSoundRender_GetClassID,
-    DSoundRender_Stop,
-    DSoundRender_Pause,
-    DSoundRender_Run,
-    DSoundRender_GetState,
-    DSoundRender_SetSyncSource,
-    DSoundRender_GetSyncSource,
-    DSoundRender_EnumPins,
-    DSoundRender_FindPin,
-    DSoundRender_QueryFilterInfo,
-    DSoundRender_JoinFilterGraph,
-    DSoundRender_QueryVendorInfo
-};
+    BaseInputPin *This = (BaseInputPin *)iface;
+    DSoundRenderImpl *pFilter = (DSoundRenderImpl *)This->pin.pinInfo.pFilter;
+    HRESULT hr;
 
-static HRESULT WINAPI DSoundRender_InputPin_EndOfStream(IPin * iface)
+    TRACE("\n");
+
+    EnterCriticalSection(This->pin.pCritSec);
+    hr = BaseInputPinImpl_BeginFlush(iface);
+    SetEvent(pFilter->blocked);
+    LeaveCriticalSection(This->pin.pCritSec);
+
+    return hr;
+}
+
+static HRESULT WINAPI DSoundRender_InputPin_EndFlush(IPin * iface)
 {
-    InputPin* This = (InputPin*)iface;
-    IMediaEventSink* pEventSink;
+    BaseInputPin *This = (BaseInputPin *)iface;
+    DSoundRenderImpl *pFilter = (DSoundRenderImpl *)This->pin.pinInfo.pFilter;
     HRESULT hr;
 
-    TRACE("(%p/%p)->()\n", This, iface);
+    TRACE("\n");
 
-    hr = IFilterGraph_QueryInterface(((DSoundRenderImpl*)This->pin.pinInfo.pFilter)->filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
-    if (SUCCEEDED(hr))
+    EnterCriticalSection(This->pin.pCritSec);
+    if (pFilter->in_loop) {
+        ResetEvent(pFilter->state_change);
+        LeaveCriticalSection(This->pin.pCritSec);
+        WaitForSingleObject(pFilter->state_change, -1);
+        EnterCriticalSection(This->pin.pCritSec);
+    }
+    if (pFilter->filter.state != State_Stopped)
+        ResetEvent(pFilter->blocked);
+
+    if (pFilter->dsbuffer)
     {
-        /* FIXME: We should wait that all audio data has been played */
-        hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, 0);
-        IMediaEventSink_Release(pEventSink);
+        LPBYTE buffer;
+        DWORD size;
+
+        /* Force a reset */
+        IDirectSoundBuffer_Lock(pFilter->dsbuffer, 0, 0, (LPVOID *)&buffer, &size, NULL, NULL, DSBLOCK_ENTIREBUFFER);
+        memset(buffer, 0, size);
+        IDirectSoundBuffer_Unlock(pFilter->dsbuffer, buffer, size, NULL, 0);
+        pFilter->writepos = pFilter->buf_size;
     }
+    QualityControlRender_Start(&pFilter->qcimpl, pFilter->filter.rtStreamStart);
+    hr = BaseInputPinImpl_EndFlush(iface);
+    LeaveCriticalSection(This->pin.pCritSec);
+    MediaSeekingPassThru_ResetMediaTime(pFilter->seekthru_unk);
 
     return hr;
 }
 
-static const IPinVtbl DSoundRender_InputPin_Vtbl = 
-{
-    InputPin_QueryInterface,
-    IPinImpl_AddRef,
-    InputPin_Release,
-    InputPin_Connect,
-    InputPin_ReceiveConnection,
-    IPinImpl_Disconnect,
-    IPinImpl_ConnectedTo,
-    IPinImpl_ConnectionMediaType,
-    IPinImpl_QueryPinInfo,
-    IPinImpl_QueryDirection,
-    IPinImpl_QueryId,
-    IPinImpl_QueryAccept,
-    IPinImpl_EnumMediaTypes,
-    IPinImpl_QueryInternalConnections,
+static const IPinVtbl DSoundRender_InputPin_Vtbl =
+{
+    BaseInputPinImpl_QueryInterface,
+    BasePinImpl_AddRef,
+    BaseInputPinImpl_Release,
+    BaseInputPinImpl_Connect,
+    DSoundRender_InputPin_ReceiveConnection,
+    DSoundRender_InputPin_Disconnect,
+    BasePinImpl_ConnectedTo,
+    BasePinImpl_ConnectionMediaType,
+    BasePinImpl_QueryPinInfo,
+    BasePinImpl_QueryDirection,
+    BasePinImpl_QueryId,
+    BaseInputPinImpl_QueryAccept,
+    BasePinImpl_EnumMediaTypes,
+    BasePinImpl_QueryInternalConnections,
     DSoundRender_InputPin_EndOfStream,
-    InputPin_BeginFlush,
-    InputPin_EndFlush,
-    InputPin_NewSegment
-};
-
-static const IMemInputPinVtbl MemInputPin_Vtbl = 
-{
-    MemInputPin_QueryInterface,
-    MemInputPin_AddRef,
-    MemInputPin_Release,
-    MemInputPin_GetAllocator,
-    MemInputPin_NotifyAllocator,
-    MemInputPin_GetAllocatorRequirements,
-    MemInputPin_Receive,
-    MemInputPin_ReceiveMultiple,
-    MemInputPin_ReceiveCanBlock
+    DSoundRender_InputPin_BeginFlush,
+    DSoundRender_InputPin_EndFlush,
+    BaseInputPinImpl_NewSegment
 };
 
 /*** IUnknown methods ***/
@@ -735,7 +1023,7 @@ static ULONG WINAPI Basicaudio_AddRef(IBasicAudio *iface) {
 
     TRACE("(%p/%p)->()\n", This, iface);
 
-    return DSoundRender_AddRef((IBaseFilter*)This);
+    return BaseFilterImpl_AddRef((IBaseFilter*)This);
 }
 
 static ULONG WINAPI Basicaudio_Release(IBasicAudio *iface) {
@@ -798,38 +1086,64 @@ static HRESULT WINAPI Basicaudio_Invoke(IBasicAudio *iface,
 
 /*** IBasicAudio methods ***/
 static HRESULT WINAPI Basicaudio_put_Volume(IBasicAudio *iface,
-                                           long lVolume) {
+                                            LONG lVolume) {
     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
 
-    TRACE("(%p/%p)->(%ld): stub !!!\n", This, iface, lVolume);
+    TRACE("(%p/%p)->(%d)\n", This, iface, lVolume);
+
+    if (lVolume > DSBVOLUME_MAX || lVolume < DSBVOLUME_MIN)
+        return E_INVALIDARG;
+
+    if (This->dsbuffer) {
+        if (FAILED(IDirectSoundBuffer_SetVolume(This->dsbuffer, lVolume)))
+            return E_FAIL;
+    }
 
+    This->volume = lVolume;
     return S_OK;
 }
 
 static HRESULT WINAPI Basicaudio_get_Volume(IBasicAudio *iface,
-                                           long *plVolume) {
+                                            LONG *plVolume) {
     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
 
-    TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, plVolume);
+    TRACE("(%p/%p)->(%p)\n", This, iface, plVolume);
+
+    if (!plVolume)
+        return E_POINTER;
 
+    *plVolume = This->volume;
     return S_OK;
 }
 
 static HRESULT WINAPI Basicaudio_put_Balance(IBasicAudio *iface,
-                                            long lBalance) {
+                                             LONG lBalance) {
     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
 
-    TRACE("(%p/%p)->(%ld): stub !!!\n", This, iface, lBalance);
+    TRACE("(%p/%p)->(%d)\n", This, iface, lBalance);
 
+    if (lBalance < DSBPAN_LEFT || lBalance > DSBPAN_RIGHT)
+        return E_INVALIDARG;
+
+    if (This->dsbuffer) {
+        if (FAILED(IDirectSoundBuffer_SetPan(This->dsbuffer, lBalance)))
+            return E_FAIL;
+    }
+
+    This->pan = lBalance;
     return S_OK;
 }
 
 static HRESULT WINAPI Basicaudio_get_Balance(IBasicAudio *iface,
-                                            long *plBalance) {
+                                             LONG *plBalance) {
     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
 
-    TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, plBalance);
+    TRACE("(%p/%p)->(%p)\n", This, iface, plBalance);
 
+    if (!plBalance)
+        return E_POINTER;
+
+    *plBalance = This->pan;
     return S_OK;
 }
 
@@ -847,3 +1161,401 @@ static const IBasicAudioVtbl IBasicAudio_Vtbl =
     Basicaudio_put_Balance,
     Basicaudio_get_Balance
 };
+
+struct dsoundrender_timer {
+    struct dsoundrender_timer *next;
+    REFERENCE_TIME start;
+    REFERENCE_TIME periodicity;
+    HANDLE handle;
+    DWORD cookie;
+};
+static LONG cookie_counter = 1;
+
+static DWORD WINAPI DSoundAdviseThread(LPVOID lpParam) {
+    DSoundRenderImpl *This = lpParam;
+    struct dsoundrender_timer head = { };
+    MSG msg;
+
+    TRACE("(%p): Main Loop\n", This);
+
+    PeekMessageW(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
+    SetEvent(This->thread_wait);
+
+    while (1)
+    {
+        HRESULT hr;
+        REFERENCE_TIME curtime = 0;
+        BOOL ret;
+        struct dsoundrender_timer *prev = &head, *cur;
+
+        hr = IReferenceClock_GetTime((IReferenceClock*) &This->IReferenceClock_vtbl, &curtime);
+        if (FAILED(hr)) {
+            FIXME("Could not get time: %08x\n", hr);
+            continue;
+        }
+        TRACE("Time: %s\n", wine_dbgstr_longlong(curtime));
+        while (prev->next) {
+            cur = prev->next;
+            if (cur->start > curtime) {
+                TRACE("Skipping %p\n", cur);
+                prev = cur;
+            } else if (cur->periodicity) {
+                while (cur->start <= curtime) {
+                    cur->start += cur->periodicity;
+                    ReleaseSemaphore(cur->handle, 1, NULL);
+                }
+                prev = cur;
+            } else {
+                struct dsoundrender_timer *next = cur->next;
+                TRACE("Firing %p %s < %s\n", cur, wine_dbgstr_longlong(cur->start), wine_dbgstr_longlong(curtime));
+                SetEvent(cur->handle);
+                HeapFree(GetProcessHeap(), 0, cur);
+                prev->next = next;
+            }
+        }
+        if (!head.next)
+            ret = GetMessageW(&msg, INVALID_HANDLE_VALUE, WM_APP, WM_APP + 4);
+        else
+            ret = PeekMessageW(&msg, INVALID_HANDLE_VALUE, WM_APP, WM_APP + 4, PM_REMOVE);
+        while (ret) {
+            switch (LOWORD(msg.message) - WM_APP) {
+                case 0: TRACE("Exiting\n"); return 0;
+                case 1:
+                case 2: {
+                    struct dsoundrender_timer *t = (struct dsoundrender_timer *)msg.wParam;
+                    if (LOWORD(msg.message) - WM_APP == 1)
+                        TRACE("Adding one-shot timer %p\n", t);
+                    else
+                        TRACE("Adding periodic timer %p\n", t);
+                    t->next = head.next;
+                    head.next = t;
+                    break;
+                }
+                case 3:
+                    prev = &head;
+                    while (prev->next) {
+                        cur = prev->next;
+                        if (cur->cookie == msg.wParam) {
+                            struct dsoundrender_timer *next = cur->next;
+                            HeapFree(GetProcessHeap(), 0, cur);
+                            prev->next = next;
+                            break;
+                        }
+                        prev = cur;
+                    }
+                    break;
+            }
+            ret = PeekMessageW(&msg, INVALID_HANDLE_VALUE, WM_APP, WM_APP + 4, PM_REMOVE);
+        }
+        MsgWaitForMultipleObjects(0, NULL, 5, QS_POSTMESSAGE, 0);
+    }
+    return 0;
+}
+
+/*** IUnknown methods ***/
+static HRESULT WINAPI ReferenceClock_QueryInterface(IReferenceClock *iface,
+                                               REFIID riid,
+                                               LPVOID*ppvObj)
+{
+    ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
+
+    TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
+
+    return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
+}
+
+static ULONG WINAPI ReferenceClock_AddRef(IReferenceClock *iface)
+{
+    ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
+
+    TRACE("(%p/%p)->()\n", This, iface);
+
+    return BaseFilterImpl_AddRef((IBaseFilter*)This);
+}
+
+static ULONG WINAPI ReferenceClock_Release(IReferenceClock *iface)
+{
+    ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
+
+    TRACE("(%p/%p)->()\n", This, iface);
+
+    return DSoundRender_Release((IBaseFilter*)This);
+}
+
+/*** IReferenceClock methods ***/
+static HRESULT WINAPI ReferenceClock_GetTime(IReferenceClock *iface,
+                                             REFERENCE_TIME *pTime)
+{
+    ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
+    HRESULT hr = E_FAIL;
+
+    TRACE("(%p/%p)->(%p)\n", This, iface, pTime);
+    if (!pTime)
+        return E_POINTER;
+
+    if (This->dsbuffer) {
+        DWORD writepos1, writepos2;
+        EnterCriticalSection(&This->filter.csFilter);
+        DSoundRender_UpdatePositions(This, &writepos1, &writepos2);
+        *pTime = This->play_time + time_from_pos(This, This->last_playpos);
+        LeaveCriticalSection(&This->filter.csFilter);
+        hr = S_OK;
+    }
+    if (FAILED(hr))
+        WARN("Could not get reference time (%x)!\n", hr);
+
+    return hr;
+}
+
+static HRESULT WINAPI ReferenceClock_AdviseTime(IReferenceClock *iface,
+                                                REFERENCE_TIME rtBaseTime,
+                                                REFERENCE_TIME rtStreamTime,
+                                                HEVENT hEvent,
+                                                DWORD_PTR *pdwAdviseCookie)
+{
+    ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
+    REFERENCE_TIME when = rtBaseTime + rtStreamTime;
+    REFERENCE_TIME future;
+    TRACE("(%p/%p)->(%s, %s, %p, %p)\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hEvent, pdwAdviseCookie);
+
+    if (when <= 0)
+        return E_INVALIDARG;
+
+    if (!pdwAdviseCookie)
+        return E_POINTER;
+
+    EnterCriticalSection(&This->filter.csFilter);
+    future = when - This->play_time;
+    if (!This->threadid && This->dsbuffer) {
+        This->thread_wait = CreateEventW(0, 0, 0, 0);
+        This->advisethread = CreateThread(NULL, 0, DSoundAdviseThread, This, 0, &This->threadid);
+        WaitForSingleObject(This->thread_wait, INFINITE);
+        CloseHandle(This->thread_wait);
+    }
+    LeaveCriticalSection(&This->filter.csFilter);
+    /* If it's in the past or the next millisecond, trigger immediately  */
+    if (future <= 10000) {
+        SetEvent((HANDLE)hEvent);
+        *pdwAdviseCookie = 0;
+    } else {
+        struct dsoundrender_timer *t = HeapAlloc(GetProcessHeap(), 0, sizeof(*t));
+        t->next = NULL;
+        t->start = when;
+        t->periodicity = 0;
+        t->handle = (HANDLE)hEvent;
+        t->cookie = InterlockedIncrement(&cookie_counter);
+        PostThreadMessageW(This->threadid, WM_APP+1, (WPARAM)t, 0);
+        *pdwAdviseCookie = t->cookie;
+    }
+
+    return S_OK;
+}
+
+static HRESULT WINAPI ReferenceClock_AdvisePeriodic(IReferenceClock *iface,
+                                                    REFERENCE_TIME rtStartTime,
+                                                    REFERENCE_TIME rtPeriodTime,
+                                                    HSEMAPHORE hSemaphore,
+                                                    DWORD_PTR *pdwAdviseCookie)
+{
+    ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
+    struct dsoundrender_timer *t;
+
+    TRACE("(%p/%p)->(%s, %s, %p, %p)\n", This, iface, wine_dbgstr_longlong(rtStartTime), wine_dbgstr_longlong(rtPeriodTime), (void*)hSemaphore, pdwAdviseCookie);
+
+    if (rtStartTime <= 0 || rtPeriodTime <= 0)
+        return E_INVALIDARG;
+
+    if (!pdwAdviseCookie)
+        return E_POINTER;
+
+    EnterCriticalSection(&This->filter.csFilter);
+    if (!This->threadid && This->dsbuffer) {
+        This->thread_wait = CreateEventW(0, 0, 0, 0);
+        This->advisethread = CreateThread(NULL, 0, DSoundAdviseThread, This, 0, &This->threadid);
+        WaitForSingleObject(This->thread_wait, INFINITE);
+        CloseHandle(This->thread_wait);
+    }
+    LeaveCriticalSection(&This->filter.csFilter);
+
+    t = HeapAlloc(GetProcessHeap(), 0, sizeof(*t));
+    t->next = NULL;
+    t->start = rtStartTime;
+    t->periodicity = rtPeriodTime;
+    t->handle = (HANDLE)hSemaphore;
+    t->cookie = InterlockedIncrement(&cookie_counter);
+    PostThreadMessageW(This->threadid, WM_APP+1, (WPARAM)t, 0);
+    *pdwAdviseCookie = t->cookie;
+
+    return S_OK;
+}
+
+static HRESULT WINAPI ReferenceClock_Unadvise(IReferenceClock *iface,
+                                              DWORD_PTR dwAdviseCookie)
+{
+    ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
+
+    TRACE("(%p/%p)->(%p)\n", This, iface, (void*)dwAdviseCookie);
+    if (!This->advisethread || !dwAdviseCookie)
+        return S_FALSE;
+    PostThreadMessageW(This->threadid, WM_APP+3, dwAdviseCookie, 0);
+    return S_OK;
+}
+
+static const IReferenceClockVtbl IReferenceClock_Vtbl =
+{
+    ReferenceClock_QueryInterface,
+    ReferenceClock_AddRef,
+    ReferenceClock_Release,
+    ReferenceClock_GetTime,
+    ReferenceClock_AdviseTime,
+    ReferenceClock_AdvisePeriodic,
+    ReferenceClock_Unadvise
+};
+
+/*** IUnknown methods ***/
+static HRESULT WINAPI AMDirectSound_QueryInterface(IAMDirectSound *iface,
+                                               REFIID riid,
+                                               LPVOID*ppvObj)
+{
+    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+
+    TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
+
+    return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
+}
+
+static ULONG WINAPI AMDirectSound_AddRef(IAMDirectSound *iface)
+{
+    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+
+    TRACE("(%p/%p)->()\n", This, iface);
+
+    return BaseFilterImpl_AddRef((IBaseFilter*)This);
+}
+
+static ULONG WINAPI AMDirectSound_Release(IAMDirectSound *iface)
+{
+    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+
+    TRACE("(%p/%p)->()\n", This, iface);
+
+    return DSoundRender_Release((IBaseFilter*)This);
+}
+
+/*** IAMDirectSound methods ***/
+static HRESULT WINAPI AMDirectSound_GetDirectSoundInterface(IAMDirectSound *iface,  IDirectSound **ds)
+{
+    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+
+    FIXME("(%p/%p)->(%p): stub\n", This, iface, ds);
+
+    return E_NOTIMPL;
+}
+
+static HRESULT WINAPI AMDirectSound_GetPrimaryBufferInterface(IAMDirectSound *iface, IDirectSoundBuffer **buf)
+{
+    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+
+    FIXME("(%p/%p)->(%p): stub\n", This, iface, buf);
+
+    return E_NOTIMPL;
+}
+
+static HRESULT WINAPI AMDirectSound_GetSecondaryBufferInterface(IAMDirectSound *iface, IDirectSoundBuffer **buf)
+{
+    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+
+    FIXME("(%p/%p)->(%p): stub\n", This, iface, buf);
+
+    return E_NOTIMPL;
+}
+
+static HRESULT WINAPI AMDirectSound_ReleaseDirectSoundInterface(IAMDirectSound *iface, IDirectSound *ds)
+{
+    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+
+    FIXME("(%p/%p)->(%p): stub\n", This, iface, ds);
+
+    return E_NOTIMPL;
+}
+
+static HRESULT WINAPI AMDirectSound_ReleasePrimaryBufferInterface(IAMDirectSound *iface, IDirectSoundBuffer *buf)
+{
+    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+
+    FIXME("(%p/%p)->(%p): stub\n", This, iface, buf);
+
+    return E_NOTIMPL;
+}
+
+static HRESULT WINAPI AMDirectSound_ReleaseSecondaryBufferInterface(IAMDirectSound *iface, IDirectSoundBuffer *buf)
+{
+    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+
+    FIXME("(%p/%p)->(%p): stub\n", This, iface, buf);
+
+    return E_NOTIMPL;
+}
+
+static HRESULT WINAPI AMDirectSound_SetFocusWindow(IAMDirectSound *iface, HWND hwnd, BOOL bgsilent)
+{
+    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+
+    FIXME("(%p/%p)->(%p,%d): stub\n", This, iface, hwnd, bgsilent);
+
+    return E_NOTIMPL;
+}
+
+static HRESULT WINAPI AMDirectSound_GetFocusWindow(IAMDirectSound *iface, HWND hwnd)
+{
+    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+
+    FIXME("(%p/%p)->(%p): stub\n", This, iface, hwnd);
+
+    return E_NOTIMPL;
+}
+
+static const IAMDirectSoundVtbl IAMDirectSound_Vtbl =
+{
+    AMDirectSound_QueryInterface,
+    AMDirectSound_AddRef,
+    AMDirectSound_Release,
+    AMDirectSound_GetDirectSoundInterface,
+    AMDirectSound_GetPrimaryBufferInterface,
+    AMDirectSound_GetSecondaryBufferInterface,
+    AMDirectSound_ReleaseDirectSoundInterface,
+    AMDirectSound_ReleasePrimaryBufferInterface,
+    AMDirectSound_ReleaseSecondaryBufferInterface,
+    AMDirectSound_SetFocusWindow,
+    AMDirectSound_GetFocusWindow
+};
+
+static DSoundRenderImpl *from_IAMFilterMiscFlags(IAMFilterMiscFlags *iface) {
+    return (DSoundRenderImpl*)((char*)iface - offsetof(DSoundRenderImpl, IAMFilterMiscFlags_vtbl));
+}
+
+static HRESULT WINAPI AMFilterMiscFlags_QueryInterface(IAMFilterMiscFlags *iface, REFIID riid, void **ppv) {
+    DSoundRenderImpl *This = from_IAMFilterMiscFlags(iface);
+    return IUnknown_QueryInterface((IUnknown*)This, riid, ppv);
+}
+
+static ULONG WINAPI AMFilterMiscFlags_AddRef(IAMFilterMiscFlags *iface) {
+    DSoundRenderImpl *This = from_IAMFilterMiscFlags(iface);
+    return IUnknown_AddRef((IUnknown*)This);
+}
+
+static ULONG WINAPI AMFilterMiscFlags_Release(IAMFilterMiscFlags *iface) {
+    DSoundRenderImpl *This = from_IAMFilterMiscFlags(iface);
+    return IUnknown_Release((IUnknown*)This);
+}
+
+static ULONG WINAPI AMFilterMiscFlags_GetMiscFlags(IAMFilterMiscFlags *iface) {
+    return AM_FILTER_MISC_FLAGS_IS_RENDERER;
+}
+
+static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl = {
+    AMFilterMiscFlags_QueryInterface,
+    AMFilterMiscFlags_AddRef,
+    AMFilterMiscFlags_Release,
+    AMFilterMiscFlags_GetMiscFlags
+};