Synchronize with trunk revision 59636 (just before Alex's CreateProcess revamp).
[reactos.git] / dll / directx / quartz / dsoundrender.c
index b327c00..f7b0032 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
-#include "config.h"
+#include <config.h>
 
 #include "quartz_private.h"
-#include "control_private.h"
 #include "pin.h"
 
-#include "uuids.h"
-#include "vfwmsgs.h"
-#include "windef.h"
-#include "winbase.h"
-#include "dshow.h"
-#include "evcode.h"
-#include "strmif.h"
-#include "dsound.h"
-#include "amaudio.h"
+//#include "uuids.h"
+//#include "vfwmsgs.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"
+#include <wine/unicode.h>
+#include <wine/debug.h>
 
 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
 
-static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
+/* 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 IBaseFilterVtbl DSoundRender_Vtbl;
-static const IPinVtbl DSoundRender_InputPin_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;
 
 typedef struct DSoundRenderImpl
 {
-    const IBaseFilterVtbl * lpVtbl;
-    const IBasicAudioVtbl *IBasicAudio_vtbl;
-    const IReferenceClockVtbl *IReferenceClock_vtbl;
-    const IAMDirectSoundVtbl *IAMDirectSound_vtbl;
-
-    LONG refCount;
-    CRITICAL_SECTION csFilter;
-    FILTER_STATE state;
-    REFERENCE_TIME rtStreamStart, rtLastStop;
-    IReferenceClock * pClock;
-    FILTER_INFO filterInfo;
+    BaseRenderer renderer;
+    BasicAudio basicAudio;
 
-    InputPin * pInputPin;
+    IReferenceClock IReferenceClock_iface;
+    IAMDirectSound IAMDirectSound_iface;
+    IAMFilterMiscFlags IAMFilterMiscFlags_iface;
 
     IDirectSound8 *dsound;
     LPDIRECTSOUNDBUFFER dsbuffer;
     DWORD buf_size;
-    DWORD write_pos;
-    DWORD write_loops;
-
-    DWORD last_play_pos;
-    DWORD play_loops;
+    DWORD in_loop;
+    DWORD last_playpos, writepos;
 
     REFERENCE_TIME play_time;
-    MediaSeekingImpl mediaSeeking;
 
-    HANDLE state_change, blocked;
+    HANDLE blocked;
 
     LONG volume;
     LONG pan;
+
+    DWORD threadid;
+    HANDLE advisethread, thread_wait;
 } DSoundRenderImpl;
 
-/* Seeking is not needed for a renderer, rely on newsegment for the appropriate changes */
-static HRESULT sound_mod_stop(IBaseFilter *iface)
+static inline DSoundRenderImpl *impl_from_BaseRenderer(BaseRenderer *iface)
 {
-    TRACE("(%p)\n", iface);
-    return S_OK;
+    return CONTAINING_RECORD(iface, DSoundRenderImpl, renderer);
 }
 
-static HRESULT sound_mod_start(IBaseFilter *iface)
+static inline DSoundRenderImpl *impl_from_IBaseFilter(IBaseFilter *iface)
 {
-    TRACE("(%p)\n", iface);
-
-    return S_OK;
+    return CONTAINING_RECORD(iface, DSoundRenderImpl, renderer.filter.IBaseFilter_iface);
 }
 
-static HRESULT sound_mod_rate(IBaseFilter *iface)
+static inline DSoundRenderImpl *impl_from_IBasicAudio(IBasicAudio *iface)
 {
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
-
-    WAVEFORMATEX *format = (WAVEFORMATEX*)This->pInputPin->pin.mtCurrent.pbFormat;
-    DWORD freq = format->nSamplesPerSec;
-    double rate = This->mediaSeeking.dRate;
-
-    freq = (DWORD)((double)freq * rate);
-
-    TRACE("(%p)\n", iface);
-
-    if (freq > DSBFREQUENCY_MAX)
-        return VFW_E_UNSUPPORTED_AUDIO;
+    return CONTAINING_RECORD(iface, DSoundRenderImpl, basicAudio.IBasicAudio_iface);
+}
 
-    if (freq < DSBFREQUENCY_MIN)
-        return VFW_E_UNSUPPORTED_AUDIO;
+static inline DSoundRenderImpl *impl_from_IReferenceClock(IReferenceClock *iface)
+{
+    return CONTAINING_RECORD(iface, DSoundRenderImpl, IReferenceClock_iface);
+}
 
-    return S_OK;
+static inline DSoundRenderImpl *impl_from_IAMDirectSound(IAMDirectSound *iface)
+{
+    return CONTAINING_RECORD(iface, DSoundRenderImpl, IAMDirectSound_iface);
 }
 
-static inline HRESULT DSoundRender_GetPos(DSoundRenderImpl *This, DWORD *pPlayPos, REFERENCE_TIME *pRefTime)
+static inline DSoundRenderImpl *impl_from_IAMFilterMiscFlags(IAMFilterMiscFlags *iface)
 {
-    HRESULT hr;
+    return CONTAINING_RECORD(iface, DSoundRenderImpl, IAMFilterMiscFlags_iface);
+}
 
-    EnterCriticalSection(&This->csFilter);
-    {
-        DWORD state;
-        DWORD write_pos;
+static REFERENCE_TIME time_from_pos(DSoundRenderImpl *This, DWORD pos) {
+    WAVEFORMATEX *wfx = (WAVEFORMATEX*)This->renderer.pInputPin->pin.mtCurrent.pbFormat;
+    REFERENCE_TIME ret = 10000000;
+    ret = ret * pos / wfx->nAvgBytesPerSec;
+    return ret;
+}
 
-        hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
-        if (SUCCEEDED(hr) && !(state & DSBSTATUS_PLAYING) && This->state == State_Running)
-        {
-            TRACE("Not playing, kickstarting the engine\n");
+static DWORD pos_from_time(DSoundRenderImpl *This, REFERENCE_TIME time) {
+    WAVEFORMATEX *wfx = (WAVEFORMATEX*)This->renderer.pInputPin->pin.mtCurrent.pbFormat;
+    REFERENCE_TIME ret = time;
+    ret *= wfx->nSamplesPerSec;
+    ret /= 10000000;
+    ret *= wfx->nBlockAlign;
+    return ret;
+}
 
-            hr = IDirectSoundBuffer_Play(This->dsbuffer, 0, 0, DSBPLAY_LOOPING);
-            if (FAILED(hr))
-                ERR("Can't play sound buffer (%x)\n", hr);
+static void DSoundRender_UpdatePositions(DSoundRenderImpl *This, DWORD *seqwritepos, DWORD *minwritepos) {
+    WAVEFORMATEX *wfx = (WAVEFORMATEX*)This->renderer.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);
+    }
+    *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;
+}
 
-        if (SUCCEEDED(hr))
-            hr = IDirectSoundBuffer_GetCurrentPosition(This->dsbuffer, pPlayPos, &write_pos);
-        if (hr == S_OK)
-        {
-            DWORD play_pos = *pPlayPos;
-
-            if (play_pos < This->last_play_pos)
-                This->play_loops++;
-            This->last_play_pos = play_pos;
-
-            /* If we really fell behind, start at the next possible position
-             * Also happens when just starting playback for the first time,
-             * or when flushing
-             */
-            if ((This->play_loops*This->buf_size)+play_pos >=
-                (This->write_loops*This->buf_size)+This->write_pos)
-                This->write_pos = write_pos;
+static HRESULT DSoundRender_GetWritePos(DSoundRenderImpl *This, DWORD *ret_writepos, REFERENCE_TIME write_at, DWORD *pfree, DWORD *skip)
+{
+    WAVEFORMATEX *wfx = (WAVEFORMATEX*)This->renderer.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->renderer.filter.pClock == &This->IReferenceClock_iface) {
+        max_lag = min_lag;
+        cur = This->play_time + time_from_pos(This, playpos);
+        cur -= This->renderer.filter.rtStreamStart;
+    } else if (This->renderer.filter.pClock) {
+        IReferenceClock_GetTime(This->renderer.filter.pClock, &cur);
+        cur -= This->renderer.filter.rtStreamStart;
+    } else
+        write_at = -1;
+
+    if (writepos == min_writepos)
+        max_lag = 0;
+
+    *skip = 0;
+    if (write_at < 0) {
+        *ret_writepos = writepos;
+        goto end;
+    }
 
-            if (pRefTime)
-            {
-                REFERENCE_TIME play_time;
-                play_time = ((REFERENCE_TIME)This->play_loops*10000000) +
-                            ((REFERENCE_TIME)play_pos*10000000/This->buf_size);
+    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;
+}
 
-                /* Don't let time run backwards */
-                if(play_time-This->play_time > 0)
-                    This->play_time = play_time;
-                else
-                    hr = S_FALSE;
+static HRESULT DSoundRender_HandleEndOfStream(DSoundRenderImpl *This)
+{
+    while (1)
+    {
+        DWORD pos1, pos2;
+        DSoundRender_UpdatePositions(This, &pos1, &pos2);
+        if (pos1 == pos2)
+            break;
 
-                *pRefTime = This->play_time;
-            }
-        }
+        This->in_loop = 1;
+        LeaveCriticalSection(&This->renderer.filter.csFilter);
+        LeaveCriticalSection(&This->renderer.csRenderLock);
+        WaitForSingleObject(This->blocked, 10);
+        EnterCriticalSection(&This->renderer.filter.csFilter);
+        EnterCriticalSection(&This->renderer.csRenderLock);
+        This->in_loop = 0;
     }
-    LeaveCriticalSection(&This->csFilter);
 
-    return hr;
+    return S_OK;
 }
 
-static HRESULT DSoundRender_SendSampleData(DSoundRenderImpl* This, const BYTE *data, DWORD size)
+static HRESULT DSoundRender_SendSampleData(DSoundRenderImpl* This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, const BYTE *data, DWORD size)
 {
-    HRESULT hr = S_OK;
-    LPBYTE lpbuf1 = NULL;
-    LPBYTE lpbuf2 = NULL;
-    DWORD dwsize1 = 0;
-    DWORD dwsize2 = 0;
-    DWORD size2;
-    DWORD play_pos,buf_free;
+    HRESULT hr;
 
-    do {
+    while (size && This->renderer.filter.state != State_Stopped) {
+        DWORD writepos, skip = 0, free, size1, size2, ret;
+        BYTE *buf1, *buf2;
 
-        hr = DSoundRender_GetPos(This, &play_pos, NULL);
-        if (hr != DS_OK)
-        {
-            ERR("GetPos returned error: %x\n", hr);
-            break;
-        }
-        if (This->write_pos <= play_pos)
-             buf_free = play_pos-This->write_pos;
+        if (This->renderer.filter.state == State_Running)
+            hr = DSoundRender_GetWritePos(This, &writepos, tStart, &free, &skip);
         else
-             buf_free = This->buf_size - This->write_pos + play_pos;
-
-        /* Wait for enough of the buffer to empty before filling it */
-        if(buf_free < This->buf_size/4)
-        {
-            Sleep(50);
+            hr = S_FALSE;
+
+        if (hr != S_OK) {
+            This->in_loop = 1;
+            LeaveCriticalSection(&This->renderer.csRenderLock);
+            ret = WaitForSingleObject(This->blocked, 10);
+            EnterCriticalSection(&This->renderer.csRenderLock);
+            This->in_loop = 0;
+            if (This->renderer.pInputPin->flushing ||
+                This->renderer.filter.state == State_Stopped) {
+                return This->renderer.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 += dwsize1 + dwsize2;
-        if (This->write_pos >= This->buf_size)
-        {
-            This->write_pos -= This->buf_size;
-            This->write_loops++;
-        }
-    } while (size && This->state == State_Running);
+        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;
+}
 
-    return hr;
+static HRESULT WINAPI DSoundRender_ShouldDrawSampleNow(BaseRenderer *This, IMediaSample *pMediaSample, REFERENCE_TIME *pStartTime, REFERENCE_TIME *pEndTime)
+{
+    /* We time ourselves do not use the base renderers timing */
+    return S_OK;
 }
 
-static HRESULT DSoundRender_Sample(LPVOID iface, IMediaSample * pSample)
+
+static HRESULT WINAPI DSoundRender_PrepareReceive(BaseRenderer *iface, IMediaSample *pSample)
 {
-    DSoundRenderImpl *This = iface;
-    LPBYTE pbSrcStream = NULL;
-    LONG cbSrcStream = 0;
-    REFERENCE_TIME tStart, tStop;
+    DSoundRenderImpl *This = impl_from_BaseRenderer(iface);
     HRESULT hr;
     AM_MEDIA_TYPE *amt;
 
-    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
-     */
-
-    EnterCriticalSection(&This->csFilter);
-
-    if (This->pInputPin->end_of_stream || This->pInputPin->flushing)
-    {
-        LeaveCriticalSection(&This->csFilter);
-        return S_FALSE;
-    }
-
-    if (This->state == State_Stopped)
-    {
-        LeaveCriticalSection(&This->csFilter);
-        return VFW_E_WRONG_STATE;
-    }
-
     if (IMediaSample_GetMediaType(pSample, &amt) == S_OK)
     {
-        AM_MEDIA_TYPE *orig = &This->pInputPin->pin.mtCurrent;
+        AM_MEDIA_TYPE *orig = &This->renderer.pInputPin->pin.mtCurrent;
         WAVEFORMATEX *origfmt = (WAVEFORMATEX *)orig->pbFormat;
         WAVEFORMATEX *newfmt = (WAVEFORMATEX *)amt->pbFormat;
 
@@ -280,91 +345,76 @@ static HRESULT DSoundRender_Sample(LPVOID iface, IMediaSample * pSample)
                 hr = IDirectSoundBuffer_SetFrequency(This->dsbuffer,
                                                      newfmt->nSamplesPerSec);
                 if (FAILED(hr))
-                {
-                    LeaveCriticalSection(&This->csFilter);
                     return VFW_E_TYPE_NOT_ACCEPTED;
-                }
                 FreeMediaType(orig);
                 CopyMediaType(orig, amt);
                 IMediaSample_SetMediaType(pSample, NULL);
             }
         }
         else
-        {
-            LeaveCriticalSection(&This->csFilter);
             return VFW_E_TYPE_NOT_ACCEPTED;
-        }
     }
+    return S_OK;
+}
+
+static HRESULT WINAPI DSoundRender_DoRenderSample(BaseRenderer *iface, IMediaSample * pSample)
+{
+    DSoundRenderImpl *This = impl_from_BaseRenderer(iface);
+    LPBYTE pbSrcStream = NULL;
+    LONG cbSrcStream = 0;
+    REFERENCE_TIME tStart, tStop;
+    HRESULT hr;
 
-    SetEvent(This->state_change);
+    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
+     */
 
     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
     if (FAILED(hr))
     {
         ERR("Cannot get pointer to sample data (%x)\n", hr);
-        LeaveCriticalSection(&This->csFilter);
         return hr;
     }
 
     hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
-    if (FAILED(hr))
+    if (FAILED(hr)) {
         ERR("Cannot get sample time (%x)\n", hr);
+        tStart = tStop = -1;
+    }
 
-    if (This->rtLastStop != tStart && (IMediaSample_IsDiscontinuity(pSample) == S_FALSE))
-        WARN("Unexpected discontinuity: Last: %u.%03u, tStart: %u.%03u\n",
-            (DWORD)(This->rtLastStop / 10000000), (DWORD)((This->rtLastStop / 10000)%1000),
-            (DWORD)(tStart / 10000000), (DWORD)((tStart / 10000)%1000));
-    This->rtLastStop = tStop;
+    IMediaSample_IsDiscontinuity(pSample);
 
     if (IMediaSample_IsPreroll(pSample) == S_OK)
     {
         TRACE("Preroll!\n");
-        LeaveCriticalSection(&This->csFilter);
         return S_OK;
     }
 
-    if (This->state == State_Paused)
-    {
-        LeaveCriticalSection(&This->csFilter);
-        WaitForSingleObject(This->blocked, INFINITE);
-        EnterCriticalSection(&This->csFilter);
-        if (This->state == State_Stopped)
-        {
-            LeaveCriticalSection(&This->csFilter);
-            return VFW_E_WRONG_STATE;
-        }
-
-        if (This->state == State_Paused)
-        {
-            /* Assuming we return because of flushing */
-            TRACE("Flushing\n");
-            LeaveCriticalSection(&This->csFilter);
-            return S_OK;
-        }
-    }
-
     cbSrcStream = IMediaSample_GetActualDataLength(pSample);
     TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream, cbSrcStream);
 
-#if 0 /* For debugging purpose */
-    {
-        int i;
-        for(i = 0; i < cbSrcStream; i++)
-        {
-           if ((i!=0) && !(i%16))
-                TRACE("\n");
-            TRACE("%02x ", pbSrcStream[i]);
-        }
-        TRACE("\n");
+    hr = DSoundRender_SendSampleData(This, tStart, tStop, pbSrcStream, cbSrcStream);
+    if (This->renderer.filter.state == State_Running && This->renderer.filter.pClock && tStart >= 0) {
+        REFERENCE_TIME jitter, now = 0;
+        Quality q;
+        IReferenceClock_GetTime(This->renderer.filter.pClock, &now);
+        jitter = now - This->renderer.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->renderer.qcimpl, (IBaseFilter*)This, q);
     }
-#endif
-
-    hr = DSoundRender_SendSampleData(This, pbSrcStream, cbSrcStream);
-    LeaveCriticalSection(&This->csFilter);
     return hr;
 }
 
-static HRESULT DSoundRender_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
+static HRESULT WINAPI DSoundRender_CheckMediaType(BaseRenderer *iface, const AM_MEDIA_TYPE * pmt)
 {
     WAVEFORMATEX* format;
 
@@ -386,735 +436,402 @@ static HRESULT DSoundRender_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
     return S_OK;
 }
 
-HRESULT DSoundRender_create(IUnknown * pUnkOuter, LPVOID * ppv)
-{
-    HRESULT hr;
-    PIN_INFO piInput;
-    DSoundRenderImpl * pDSoundRender;
-
-    TRACE("(%p, %p)\n", pUnkOuter, ppv);
-
-    *ppv = NULL;
-
-    if (pUnkOuter)
-        return CLASS_E_NOAGGREGATION;
-    
-    pDSoundRender = CoTaskMemAlloc(sizeof(DSoundRenderImpl));
-    if (!pDSoundRender)
-        return E_OUTOFMEMORY;
-    ZeroMemory(pDSoundRender, sizeof(DSoundRenderImpl));
-
-    pDSoundRender->lpVtbl = &DSoundRender_Vtbl;
-    pDSoundRender->IBasicAudio_vtbl = &IBasicAudio_Vtbl;
-    pDSoundRender->IReferenceClock_vtbl = &IReferenceClock_Vtbl;
-    pDSoundRender->IAMDirectSound_vtbl = &IAMDirectSound_Vtbl;
-    pDSoundRender->refCount = 1;
-    InitializeCriticalSection(&pDSoundRender->csFilter);
-    pDSoundRender->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DSoundRenderImpl.csFilter");
-    pDSoundRender->state = State_Stopped;
-
-    /* construct input pin */
-    piInput.dir = PINDIR_INPUT;
-    piInput.pFilter = (IBaseFilter *)pDSoundRender;
-    lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
-    hr = InputPin_Construct(&DSoundRender_InputPin_Vtbl, &piInput, DSoundRender_Sample, pDSoundRender, DSoundRender_QueryAccept, NULL, &pDSoundRender->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
-            IDirectSound_SetCooperativeLevel(pDSoundRender->dsound, GetDesktopWindow(), DSSCL_PRIORITY);
-    }
-
-    if (SUCCEEDED(hr))
-    {
-        MediaSeekingImpl_Init((IBaseFilter*)pDSoundRender, sound_mod_stop, sound_mod_start, sound_mod_rate, &pDSoundRender->mediaSeeking, &pDSoundRender->csFilter);
-        pDSoundRender->mediaSeeking.lpVtbl = &IMediaSeeking_Vtbl;
-
-        pDSoundRender->state_change = CreateEventW(NULL, TRUE, TRUE, NULL);
-        pDSoundRender->blocked = CreateEventW(NULL, FALSE, FALSE, NULL);
-
-        if (!pDSoundRender->state_change || !pDSoundRender->blocked)
-        {
-            IUnknown_Release((IUnknown *)pDSoundRender);
-            return HRESULT_FROM_WIN32(GetLastError());
-        }
-
-        *ppv = pDSoundRender;
-    }
-    else
-    {
-        if (pDSoundRender->pInputPin)
-            IPin_Release((IPin*)pDSoundRender->pInputPin);
-        pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
-        DeleteCriticalSection(&pDSoundRender->csFilter);
-        CoTaskMemFree(pDSoundRender);
-    }
-
-    return hr;
-}
-
-static HRESULT WINAPI DSoundRender_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
-{
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
-    TRACE("(%p, %p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
-
-    *ppv = NULL;
-
-    if (IsEqualIID(riid, &IID_IUnknown))
-        *ppv = This;
-    else if (IsEqualIID(riid, &IID_IPersist))
-        *ppv = This;
-    else if (IsEqualIID(riid, &IID_IMediaFilter))
-        *ppv = This;
-    else if (IsEqualIID(riid, &IID_IBaseFilter))
-        *ppv = This;
-    else if (IsEqualIID(riid, &IID_IBasicAudio))
-        *ppv = &This->IBasicAudio_vtbl;
-    else if (IsEqualIID(riid, &IID_IReferenceClock))
-        *ppv = &This->IReferenceClock_vtbl;
-    else if (IsEqualIID(riid, &IID_IMediaSeeking))
-        *ppv = &This->mediaSeeking.lpVtbl;
-    else if (IsEqualIID(riid, &IID_IAMDirectSound))
-        *ppv = &This->IAMDirectSound_vtbl;
-
-    if (*ppv)
-    {
-        IUnknown_AddRef((IUnknown *)(*ppv));
-        return S_OK;
-    }
-
-    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);
-
-    TRACE("(%p)->() Release from %d\n", This, refCount + 1);
-
-    if (!refCount)
-    {
-        IPin *pConnectedTo;
-
-        if (This->pClock)
-            IReferenceClock_Release(This->pClock);
-
-        if (This->dsbuffer)
-            IDirectSoundBuffer_Release(This->dsbuffer);
-        This->dsbuffer = NULL;
-        if (This->dsound)
-            IDirectSound_Release(This->dsound);
-        This->dsound = NULL;
-       
-        if (SUCCEEDED(IPin_ConnectedTo((IPin *)This->pInputPin, &pConnectedTo)))
-        {
-            IPin_Disconnect(pConnectedTo);
-            IPin_Release(pConnectedTo);
-        }
-        IPin_Disconnect((IPin *)This->pInputPin);
-
-        IPin_Release((IPin *)This->pInputPin);
-
-        This->lpVtbl = NULL;
-        This->IBasicAudio_vtbl = NULL;
-        
-        This->csFilter.DebugInfo->Spare[0] = 0;
-        DeleteCriticalSection(&This->csFilter);
-
-        CloseHandle(This->state_change);
-        CloseHandle(This->blocked);
-
-        TRACE("Destroying Audio Renderer\n");
-        CoTaskMemFree(This);
-        
-        return 0;
-    }
-    else
-        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)
+static VOID WINAPI DSoundRender_OnStopStreaming(BaseRenderer * iface)
 {
-    HRESULT hr = S_OK;
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
+    DSoundRenderImpl *This = impl_from_BaseRenderer(iface);
 
     TRACE("(%p/%p)->()\n", This, iface);
 
-    EnterCriticalSection(&This->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);
-            }
-        }
-        if (SUCCEEDED(hr))
-            This->state = State_Stopped;
-
-        /* Complete our transition */
-        SetEvent(This->state_change);
-        SetEvent(This->blocked);
-    }
-    LeaveCriticalSection(&This->csFilter);
-    
-    return hr;
+    IDirectSoundBuffer_Stop(This->dsbuffer);
+    This->writepos = This->buf_size;
+    SetEvent(This->blocked);
 }
 
-static HRESULT WINAPI DSoundRender_Pause(IBaseFilter * iface)
+static VOID WINAPI DSoundRender_OnStartStreaming(BaseRenderer * iface)
 {
-    HRESULT hr = S_OK;
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
-    
-    TRACE("(%p/%p)->()\n", This, iface);
+    DSoundRenderImpl *This = impl_from_BaseRenderer(iface);
 
-    EnterCriticalSection(&This->csFilter);
-    if (This->state != State_Paused)
-    {
-        DWORD state = 0;
-        if (This->state == State_Stopped)
-        {
-            This->pInputPin->end_of_stream = 0;
-        }
+    TRACE("(%p)\n", This);
 
-        if (This->dsbuffer)
+    if (This->renderer.pInputPin->pin.pConnectedTo)
+    {
+        if (This->renderer.filter.state == State_Paused)
         {
-            hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
-            if (SUCCEEDED(hr))
-            {
-                if (state & DSBSTATUS_PLAYING)
-                    hr = IDirectSoundBuffer_Stop(This->dsbuffer);
-            }
+            /* Unblock our thread, state changing from paused to running doesn't need a reset for state change */
+            SetEvent(This->blocked);
         }
-        if (SUCCEEDED(hr))
-            This->state = State_Paused;
-
+        IDirectSoundBuffer_Play(This->dsbuffer, 0, 0, DSBPLAY_LOOPING);
         ResetEvent(This->blocked);
-        ResetEvent(This->state_change);
     }
-    LeaveCriticalSection(&This->csFilter);
-
-    return hr;
 }
 
-static HRESULT WINAPI DSoundRender_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
+static HRESULT WINAPI DSoundRender_CompleteConnect(BaseRenderer * iface, IPin * pReceivePin)
 {
+    DSoundRenderImpl *This = impl_from_BaseRenderer(iface);
+    const AM_MEDIA_TYPE * pmt = &This->renderer.pInputPin->pin.mtCurrent;
     HRESULT hr = S_OK;
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
+    WAVEFORMATEX *format;
+    DSBUFFERDESC buf_desc;
 
-    TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
+    TRACE("(%p)->(%p)\n", This, pReceivePin);
+    dump_AM_MEDIA_TYPE(pmt);
 
-    EnterCriticalSection(&This->csFilter);
-    {
-        This->rtStreamStart = tStart;
-        if (This->state == State_Paused)
-        {
-            /* Unblock our thread, state changing from paused to running doesn't need a reset for state change */
-            SetEvent(This->blocked);
-        }
-        else if (This->state == State_Stopped)
-        {
-            ResetEvent(This->state_change);
-            This->pInputPin->end_of_stream = 0;
-        }
+    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);
 
-        This->state = State_Running;
-    }
-    LeaveCriticalSection(&This->csFilter);
+    format = (WAVEFORMATEX*)pmt->pbFormat;
 
-    return hr;
-}
+    This->buf_size = format->nAvgBytesPerSec;
 
-static HRESULT WINAPI DSoundRender_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
-{
-    HRESULT hr;
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
+    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 = This->buf_size;
+    buf_desc.lpwfxFormat = format;
+    hr = IDirectSound_CreateSoundBuffer(This->dsound, &buf_desc, &This->dsbuffer, NULL);
+    This->writepos = This->buf_size;
+    if (FAILED(hr))
+        ERR("Can't create sound buffer (%x)\n", hr);
 
-    TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
+    if (SUCCEEDED(hr))
+    {
+        hr = IDirectSoundBuffer_SetVolume(This->dsbuffer, This->volume);
+        if (FAILED(hr))
+            ERR("Can't set volume to %d (%x)\n", This->volume, hr);
 
-    if (WaitForSingleObject(This->state_change, dwMilliSecsTimeout) == WAIT_TIMEOUT)
-        hr = VFW_S_STATE_INTERMEDIATE;
-    else
+        hr = IDirectSoundBuffer_SetPan(This->dsbuffer, This->pan);
+        if (FAILED(hr))
+            ERR("Can't set pan to %d (%x)\n", This->pan, hr);
         hr = S_OK;
-
-    EnterCriticalSection(&This->csFilter);
-    {
-        *pState = This->state;
     }
-    LeaveCriticalSection(&This->csFilter);
-
-    return hr;
-}
-
-static HRESULT WINAPI DSoundRender_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
-{
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
-
-    TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
 
-    EnterCriticalSection(&This->csFilter);
+    if (FAILED(hr) && hr != VFW_E_ALREADY_CONNECTED)
     {
-        if (This->pClock)
-            IReferenceClock_Release(This->pClock);
-        This->pClock = pClock;
-        if (This->pClock)
-            IReferenceClock_AddRef(This->pClock);
+        if (This->dsbuffer)
+            IDirectSoundBuffer_Release(This->dsbuffer);
+        This->dsbuffer = NULL;
     }
-    LeaveCriticalSection(&This->csFilter);
 
-    return S_OK;
+    return hr;
 }
 
-static HRESULT WINAPI DSoundRender_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
+static HRESULT WINAPI DSoundRender_BreakConnect(BaseRenderer* iface)
 {
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
+    DSoundRenderImpl *This = impl_from_BaseRenderer(iface);
 
-    TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
+    TRACE("(%p)->()\n", iface);
 
-    EnterCriticalSection(&This->csFilter);
-    {
-        *ppClock = This->pClock;
-        if (This->pClock)
-            IReferenceClock_AddRef(This->pClock);
+    if (This->threadid) {
+        PostThreadMessageW(This->threadid, WM_APP, 0, 0);
+        LeaveCriticalSection(This->renderer.pInputPin->pin.pCritSec);
+        WaitForSingleObject(This->advisethread, INFINITE);
+        EnterCriticalSection(This->renderer.pInputPin->pin.pCritSec);
+        CloseHandle(This->advisethread);
     }
-    LeaveCriticalSection(&This->csFilter);
-    
-    return S_OK;
-}
-
-/** IBaseFilter implementation **/
-
-static HRESULT DSoundRender_GetPin(IBaseFilter *iface, ULONG pos, IPin **pin, DWORD *lastsynctick)
-{
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
-
-    /* Our pins are static, not changing so setting static tick count is ok */
-    *lastsynctick = 0;
-
-    if (pos >= 1)
-        return S_FALSE;
+    if (This->dsbuffer)
+        IDirectSoundBuffer_Release(This->dsbuffer);
+    This->dsbuffer = NULL;
 
-    *pin = (IPin *)This->pInputPin;
-    IPin_AddRef(*pin);
     return S_OK;
 }
 
-static HRESULT WINAPI DSoundRender_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
+static HRESULT WINAPI DSoundRender_EndOfStream(BaseRenderer* iface)
 {
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
-
-    TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
-
-    return IEnumPinsImpl_Construct(ppEnum, DSoundRender_GetPin, iface);
-}
+    DSoundRenderImpl *This = impl_from_BaseRenderer(iface);
+    HRESULT hr;
 
-static HRESULT WINAPI DSoundRender_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
-{
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
+    TRACE("(%p)->()\n",iface);
 
-    TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_w(Id), ppPin);
-    
-    FIXME("DSoundRender::FindPin(...)\n");
+    hr = BaseRendererImpl_EndOfStream(iface);
+    if (hr != S_OK)
+    {
+        ERR("%08x\n", hr);
+        return hr;
+    }
 
-    /* FIXME: critical section */
+    hr = DSoundRender_HandleEndOfStream(This);
 
-    return E_NOTIMPL;
+    return hr;
 }
 
-static HRESULT WINAPI DSoundRender_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
+static HRESULT WINAPI DSoundRender_BeginFlush(BaseRenderer* iface)
 {
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
+    DSoundRenderImpl *This = impl_from_BaseRenderer(iface);
 
-    TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
-
-    strcpyW(pInfo->achName, This->filterInfo.achName);
-    pInfo->pGraph = This->filterInfo.pGraph;
+    TRACE("\n");
+    BaseRendererImpl_BeginFlush(iface);
+    SetEvent(This->blocked);
 
-    if (pInfo->pGraph)
-        IFilterGraph_AddRef(pInfo->pGraph);
-    
     return S_OK;
 }
 
-static HRESULT WINAPI DSoundRender_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
+static HRESULT WINAPI DSoundRender_EndFlush(BaseRenderer* iface)
 {
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
+    DSoundRenderImpl *This = impl_from_BaseRenderer(iface);
+
+    TRACE("\n");
 
-    TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
+    BaseRendererImpl_EndFlush(iface);
+    if (This->renderer.filter.state != State_Stopped)
+        ResetEvent(This->blocked);
 
-    EnterCriticalSection(&This->csFilter);
+    if (This->dsbuffer)
     {
-        if (pName)
-            strcpyW(This->filterInfo.achName, pName);
-        else
-            *This->filterInfo.achName = '\0';
-        This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
+        LPBYTE buffer;
+        DWORD size;
+
+        /* Force a reset */
+        IDirectSoundBuffer_Lock(This->dsbuffer, 0, 0, (LPVOID *)&buffer, &size, NULL, NULL, DSBLOCK_ENTIREBUFFER);
+        memset(buffer, 0, size);
+        IDirectSoundBuffer_Unlock(This->dsbuffer, buffer, size, NULL, 0);
+        This->writepos = This->buf_size;
     }
-    LeaveCriticalSection(&This->csFilter);
 
     return S_OK;
 }
 
-static HRESULT WINAPI DSoundRender_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
-{
-    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
-    TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
-    return E_NOTIMPL;
-}
-
-static const IBaseFilterVtbl DSoundRender_Vtbl =
-{
-    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
+static const BaseRendererFuncTable BaseFuncTable = {
+    DSoundRender_CheckMediaType,
+    DSoundRender_DoRenderSample,
+    /**/
+    NULL,
+    NULL,
+    NULL,
+    DSoundRender_OnStartStreaming,
+    DSoundRender_OnStopStreaming,
+    NULL,
+    NULL,
+    NULL,
+    DSoundRender_ShouldDrawSampleNow,
+    DSoundRender_PrepareReceive,
+    /**/
+    DSoundRender_CompleteConnect,
+    DSoundRender_BreakConnect,
+    DSoundRender_EndOfStream,
+    DSoundRender_BeginFlush,
+    DSoundRender_EndFlush,
 };
 
-static HRESULT WINAPI DSoundRender_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
+HRESULT DSoundRender_create(IUnknown * pUnkOuter, LPVOID * ppv)
 {
-    InputPin *This = (InputPin *)iface;
-    PIN_DIRECTION pindirReceive;
-    DSoundRenderImpl *DSImpl;
-    HRESULT hr = S_OK;
+    HRESULT hr;
+    DSoundRenderImpl * pDSoundRender;
 
-    TRACE("(%p)->(%p, %p)\n", This, pReceivePin, pmt);
-    dump_AM_MEDIA_TYPE(pmt);
+    TRACE("(%p, %p)\n", pUnkOuter, ppv);
 
-    EnterCriticalSection(This->pin.pCritSec);
-    {
-        DSImpl = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
-        DSImpl->rtLastStop = -1;
+    *ppv = NULL;
 
-        if (This->pin.pConnectedTo)
-            hr = VFW_E_ALREADY_CONNECTED;
+    if (pUnkOuter)
+        return CLASS_E_NOAGGREGATION;
 
-        if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
-            hr = VFW_E_TYPE_NOT_ACCEPTED;
+    pDSoundRender = CoTaskMemAlloc(sizeof(DSoundRenderImpl));
+    if (!pDSoundRender)
+        return E_OUTOFMEMORY;
+    ZeroMemory(pDSoundRender, sizeof(DSoundRenderImpl));
 
-        if (SUCCEEDED(hr))
-        {
-            IPin_QueryDirection(pReceivePin, &pindirReceive);
+    hr = BaseRenderer_Init(&pDSoundRender->renderer, &DSoundRender_Vtbl, (IUnknown*)pDSoundRender, &CLSID_DSoundRender, (DWORD_PTR)(__FILE__ ": DSoundRenderImpl.csFilter"), &BaseFuncTable);
 
-            if (pindirReceive != PINDIR_OUTPUT)
-            {
-                ERR("Can't connect from non-output pin\n");
-                hr = VFW_E_INVALID_DIRECTION;
-            }
-        }
+    BasicAudio_Init(&pDSoundRender->basicAudio,&IBasicAudio_Vtbl);
+    pDSoundRender->IReferenceClock_iface.lpVtbl = &IReferenceClock_Vtbl;
+    pDSoundRender->IAMDirectSound_iface.lpVtbl = &IAMDirectSound_Vtbl;
+    pDSoundRender->IAMFilterMiscFlags_iface.lpVtbl = &IAMFilterMiscFlags_Vtbl;
 
-        if (SUCCEEDED(hr))
-        {
-            WAVEFORMATEX *format;
+    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;
-
-            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_GETCURRENTPOSITION2;
-            buf_desc.dwBufferBytes = DSImpl->buf_size;
-            buf_desc.lpwfxFormat = format;
-            hr = IDirectSound_CreateSoundBuffer(DSImpl->dsound, &buf_desc, &DSImpl->dsbuffer, NULL);
-            if (FAILED(hr))
-                ERR("Can't create sound buffer (%x)\n", hr);
-        }
-
-        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);
-
-            hr = IDirectSoundBuffer_SetPan(DSImpl->dsbuffer, DSImpl->pan);
-            if (FAILED(hr))
-                ERR("Can't set pan to %d (%x)\n", DSImpl->pan, hr);
-
-            DSImpl->write_pos = 0;
+            buf_desc.dwFlags = DSBCAPS_PRIMARYBUFFER;
+            hr = IDirectSound_CreateSoundBuffer(pDSoundRender->dsound, &buf_desc, &buf, NULL);
+            if (SUCCEEDED(hr)) {
+                IDirectSoundBuffer_Play(buf, 0, 0, DSBPLAY_LOOPING);
+                IDirectSoundBuffer_Release(buf);
+            }
             hr = S_OK;
         }
+    }
 
-        if (SUCCEEDED(hr))
-        {
-            CopyMediaType(&This->pin.mtCurrent, pmt);
-            This->pin.pConnectedTo = pReceivePin;
-            IPin_AddRef(pReceivePin);
-        }
-        else if (hr != VFW_E_ALREADY_CONNECTED)
+    if (SUCCEEDED(hr))
+    {
+        pDSoundRender->blocked = CreateEventW(NULL, TRUE, TRUE, NULL);
+
+        if (!pDSoundRender->blocked || FAILED(hr))
         {
-            if (DSImpl->dsbuffer)
-                IDirectSoundBuffer_Release(DSImpl->dsbuffer);
-            DSImpl->dsbuffer = NULL;
+            IUnknown_Release((IUnknown *)pDSoundRender);
+            return HRESULT_FROM_WIN32(GetLastError());
         }
+
+        *ppv = pDSoundRender;
+    }
+    else
+    {
+        BaseRendererImpl_Release(&pDSoundRender->renderer.filter.IBaseFilter_iface);
+        CoTaskMemFree(pDSoundRender);
     }
-    LeaveCriticalSection(This->pin.pCritSec);
 
     return hr;
 }
 
-static HRESULT WINAPI DSoundRender_InputPin_Disconnect(IPin * iface)
-{
-    IPinImpl *This = (IPinImpl*)iface;
-    DSoundRenderImpl *DSImpl;
-
-    TRACE("(%p)->()\n", iface);
-
-    DSImpl = (DSoundRenderImpl*)This->pinInfo.pFilter;
-    if (DSImpl->dsbuffer)
-        IDirectSoundBuffer_Release(DSImpl->dsbuffer);
-    DSImpl->dsbuffer = NULL;
-
-    return IPinImpl_Disconnect(iface);
-}
-
-static HRESULT WINAPI DSoundRender_InputPin_EndOfStream(IPin * iface)
+static HRESULT WINAPI DSoundRender_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
 {
-    InputPin* This = (InputPin*)iface;
-    DSoundRenderImpl *me = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
-    IMediaEventSink* pEventSink;
-    HRESULT hr;
+    DSoundRenderImpl *This = impl_from_IBaseFilter(iface);
+    TRACE("(%p, %p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
 
-    EnterCriticalSection(This->pin.pCritSec);
+    *ppv = NULL;
 
-    TRACE("(%p/%p)->()\n", This, iface);
-    hr = InputPin_EndOfStream(iface);
-    if (hr != S_OK)
+    if (IsEqualIID(riid, &IID_IBasicAudio))
+        *ppv = &This->basicAudio.IBasicAudio_iface;
+    else if (IsEqualIID(riid, &IID_IReferenceClock))
+        *ppv = &This->IReferenceClock_iface;
+    else if (IsEqualIID(riid, &IID_IAMDirectSound))
+        *ppv = &This->IAMDirectSound_iface;
+    else if (IsEqualIID(riid, &IID_IAMFilterMiscFlags))
+        *ppv = &This->IAMFilterMiscFlags_iface;
+    else
     {
-        ERR("%08x\n", hr);
-        LeaveCriticalSection(This->pin.pCritSec);
-        return hr;
+        HRESULT hr;
+        hr = BaseRendererImpl_QueryInterface(iface, riid, ppv);
+        if (SUCCEEDED(hr))
+            return hr;
     }
 
-    hr = IFilterGraph_QueryInterface(me->filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
-    if (SUCCEEDED(hr))
+    if (*ppv)
     {
-        BYTE * silence;
-
-        silence = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, me->buf_size);
-        if (silence)
-        {
-            memset(silence, 0, me->buf_size);
-            DSoundRender_SendSampleData((DSoundRenderImpl*)This->pin.pinInfo.pFilter, silence, me->buf_size);
-            HeapFree(GetProcessHeap(), 0, silence);
-        }
-
-        hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, 0);
-        IMediaEventSink_Release(pEventSink);
+        IUnknown_AddRef((IUnknown *)(*ppv));
+        return S_OK;
     }
-    LeaveCriticalSection(This->pin.pCritSec);
 
-    return hr;
+    if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
+        FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
+
+    return E_NOINTERFACE;
 }
 
-static HRESULT WINAPI DSoundRender_InputPin_BeginFlush(IPin * iface)
+static ULONG WINAPI DSoundRender_Release(IBaseFilter * iface)
 {
-    InputPin *This = (InputPin *)iface;
-    DSoundRenderImpl *pFilter = (DSoundRenderImpl *)This->pin.pinInfo.pFilter;
-    HRESULT hr;
-    LPBYTE buffer;
-    DWORD size;
+    DSoundRenderImpl *This = impl_from_IBaseFilter(iface);
+    ULONG refCount = BaseRendererImpl_Release(iface);
 
-    TRACE("\n");
-
-    EnterCriticalSection(This->pin.pCritSec);
-    hr = InputPin_BeginFlush(iface);
+    TRACE("(%p)->() Release from %d\n", This, refCount + 1);
 
-    if (pFilter->dsbuffer)
+    if (!refCount)
     {
-        IDirectSoundBuffer_Stop(pFilter->dsbuffer);
+        if (This->threadid) {
+            PostThreadMessageW(This->threadid, WM_APP, 0, 0);
+            WaitForSingleObject(This->advisethread, INFINITE);
+            CloseHandle(This->advisethread);
+        }
 
-        /* Force a reset */
-        IDirectSoundBuffer_SetCurrentPosition(pFilter->dsbuffer, 0);
-        pFilter->write_pos = pFilter->last_play_pos = 0;
-        ++pFilter->play_loops;
-        pFilter->write_loops = pFilter->play_loops;
+        if (This->dsbuffer)
+            IDirectSoundBuffer_Release(This->dsbuffer);
+        This->dsbuffer = NULL;
+        if (This->dsound)
+            IDirectSound_Release(This->dsound);
+        This->dsound = NULL;
 
-        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);
-    }
+        BasicAudio_Destroy(&This->basicAudio);
+        CloseHandle(This->blocked);
 
-    if (pFilter->state == State_Paused)
-        SetEvent(pFilter->blocked);
-    LeaveCriticalSection(This->pin.pCritSec);
+        TRACE("Destroying Audio Renderer\n");
+        CoTaskMemFree(This);
 
-    return hr;
+        return 0;
+    }
+    else
+        return refCount;
 }
 
-static HRESULT WINAPI DSoundRender_InputPin_EndFlush(IPin * iface)
+static HRESULT WINAPI DSoundRender_Pause(IBaseFilter * iface)
 {
-    InputPin *This = (InputPin *)iface;
-    DSoundRenderImpl *pFilter = (DSoundRenderImpl *)This->pin.pinInfo.pFilter;
-    HRESULT hr;
+    HRESULT hr = S_OK;
+    DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
 
-    TRACE("\n");
+    TRACE("(%p/%p)->()\n", This, iface);
 
-    EnterCriticalSection(This->pin.pCritSec);
-    hr = InputPin_EndFlush(iface);
+    EnterCriticalSection(&This->renderer.csRenderLock);
+    if (This->renderer.filter.state != State_Paused)
+    {
+        if (This->renderer.filter.state == State_Stopped)
+        {
+            if (This->renderer.pInputPin->pin.pConnectedTo)
+                ResetEvent(This->renderer.evComplete);
+            This->renderer.pInputPin->end_of_stream = 0;
+        }
+
+        hr = IDirectSoundBuffer_Stop(This->dsbuffer);
+        if (SUCCEEDED(hr))
+            This->renderer.filter.state = State_Paused;
 
-    if (pFilter->state == State_Paused)
-        SetEvent(pFilter->blocked);
-    LeaveCriticalSection(This->pin.pCritSec);
+        ResetEvent(This->blocked);
+        ResetEvent(This->renderer.RenderEvent);
+    }
+    ResetEvent(This->renderer.ThreadSignal);
+    LeaveCriticalSection(&This->renderer.csRenderLock);
 
     return hr;
 }
 
-static const IPinVtbl DSoundRender_InputPin_Vtbl =
+static const IBaseFilterVtbl DSoundRender_Vtbl =
 {
-    InputPin_QueryInterface,
-    IPinImpl_AddRef,
-    InputPin_Release,
-    InputPin_Connect,
-    DSoundRender_InputPin_ReceiveConnection,
-    DSoundRender_InputPin_Disconnect,
-    IPinImpl_ConnectedTo,
-    IPinImpl_ConnectionMediaType,
-    IPinImpl_QueryPinInfo,
-    IPinImpl_QueryDirection,
-    IPinImpl_QueryId,
-    IPinImpl_QueryAccept,
-    IPinImpl_EnumMediaTypes,
-    IPinImpl_QueryInternalConnections,
-    DSoundRender_InputPin_EndOfStream,
-    DSoundRender_InputPin_BeginFlush,
-    DSoundRender_InputPin_EndFlush,
-    InputPin_NewSegment
+    DSoundRender_QueryInterface,
+    BaseFilterImpl_AddRef,
+    DSoundRender_Release,
+    BaseFilterImpl_GetClassID,
+    BaseRendererImpl_Stop,
+    DSoundRender_Pause,
+    BaseRendererImpl_Run,
+    BaseRendererImpl_GetState,
+    BaseRendererImpl_SetSyncSource,
+    BaseFilterImpl_GetSyncSource,
+    BaseFilterImpl_EnumPins,
+    BaseRendererImpl_FindPin,
+    BaseFilterImpl_QueryFilterInfo,
+    BaseFilterImpl_JoinFilterGraph,
+    BaseFilterImpl_QueryVendorInfo
 };
 
 /*** IUnknown methods ***/
 static HRESULT WINAPI Basicaudio_QueryInterface(IBasicAudio *iface,
                                                REFIID riid,
                                                LPVOID*ppvObj) {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IBasicAudio(iface);
 
     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
 
-    return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
+    return DSoundRender_QueryInterface(&This->renderer.filter.IBaseFilter_iface, riid, ppvObj);
 }
 
 static ULONG WINAPI Basicaudio_AddRef(IBasicAudio *iface) {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IBasicAudio(iface);
 
     TRACE("(%p/%p)->()\n", This, iface);
 
-    return DSoundRender_AddRef((IBaseFilter*)This);
+    return BaseFilterImpl_AddRef(&This->renderer.filter.IBaseFilter_iface);
 }
 
 static ULONG WINAPI Basicaudio_Release(IBasicAudio *iface) {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IBasicAudio(iface);
 
     TRACE("(%p/%p)->()\n", This, iface);
 
-    return DSoundRender_Release((IBaseFilter*)This);
-}
-
-/*** IDispatch methods ***/
-static HRESULT WINAPI Basicaudio_GetTypeInfoCount(IBasicAudio *iface,
-                                                 UINT*pctinfo) {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
-
-    TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, pctinfo);
-
-    return S_OK;
-}
-
-static HRESULT WINAPI Basicaudio_GetTypeInfo(IBasicAudio *iface,
-                                            UINT iTInfo,
-                                            LCID lcid,
-                                            ITypeInfo**ppTInfo) {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
-
-    TRACE("(%p/%p)->(%d, %d, %p): stub !!!\n", This, iface, iTInfo, lcid, ppTInfo);
-
-    return S_OK;
-}
-
-static HRESULT WINAPI Basicaudio_GetIDsOfNames(IBasicAudio *iface,
-                                              REFIID riid,
-                                              LPOLESTR*rgszNames,
-                                              UINT cNames,
-                                              LCID lcid,
-                                              DISPID*rgDispId) {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
-
-    TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
-
-    return S_OK;
-}
-
-static HRESULT WINAPI Basicaudio_Invoke(IBasicAudio *iface,
-                                       DISPID dispIdMember,
-                                       REFIID riid,
-                                       LCID lcid,
-                                       WORD wFlags,
-                                       DISPPARAMS*pDispParams,
-                                       VARIANT*pVarResult,
-                                       EXCEPINFO*pExepInfo,
-                                       UINT*puArgErr) {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
-
-    TRACE("(%p/%p)->(%d, %s (%p), %d, %04x, %p, %p, %p, %p): stub !!!\n", This, iface, dispIdMember, debugstr_guid(riid), riid, lcid, wFlags, pDispParams, pVarResult, pExepInfo, puArgErr);
-
-    return S_OK;
+    return DSoundRender_Release(&This->renderer.filter.IBaseFilter_iface);
 }
 
 /*** IBasicAudio methods ***/
 static HRESULT WINAPI Basicaudio_put_Volume(IBasicAudio *iface,
                                             LONG lVolume) {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IBasicAudio(iface);
 
     TRACE("(%p/%p)->(%d)\n", This, iface, lVolume);
 
@@ -1132,7 +849,7 @@ static HRESULT WINAPI Basicaudio_put_Volume(IBasicAudio *iface,
 
 static HRESULT WINAPI Basicaudio_get_Volume(IBasicAudio *iface,
                                             LONG *plVolume) {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IBasicAudio(iface);
 
     TRACE("(%p/%p)->(%p)\n", This, iface, plVolume);
 
@@ -1145,7 +862,7 @@ static HRESULT WINAPI Basicaudio_get_Volume(IBasicAudio *iface,
 
 static HRESULT WINAPI Basicaudio_put_Balance(IBasicAudio *iface,
                                              LONG lBalance) {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IBasicAudio(iface);
 
     TRACE("(%p/%p)->(%d)\n", This, iface, lBalance);
 
@@ -1163,7 +880,7 @@ static HRESULT WINAPI Basicaudio_put_Balance(IBasicAudio *iface,
 
 static HRESULT WINAPI Basicaudio_get_Balance(IBasicAudio *iface,
                                              LONG *plBalance) {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IBasicAudio(iface);
 
     TRACE("(%p/%p)->(%p)\n", This, iface, plBalance);
 
@@ -1179,61 +896,163 @@ static const IBasicAudioVtbl IBasicAudio_Vtbl =
     Basicaudio_QueryInterface,
     Basicaudio_AddRef,
     Basicaudio_Release,
-    Basicaudio_GetTypeInfoCount,
-    Basicaudio_GetTypeInfo,
-    Basicaudio_GetIDsOfNames,
-    Basicaudio_Invoke,
+    BasicAudioImpl_GetTypeInfoCount,
+    BasicAudioImpl_GetTypeInfo,
+    BasicAudioImpl_GetIDsOfNames,
+    BasicAudioImpl_Invoke,
     Basicaudio_put_Volume,
     Basicaudio_get_Volume,
     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 = {0};
+    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(&This->IReferenceClock_iface, &curtime);
+        if (SUCCEEDED(hr)) {
+            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);
+    DSoundRenderImpl *This = impl_from_IReferenceClock(iface);
 
     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
 
-    return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
+    return DSoundRender_QueryInterface(&This->renderer.filter.IBaseFilter_iface, riid, ppvObj);
 }
 
 static ULONG WINAPI ReferenceClock_AddRef(IReferenceClock *iface)
 {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IReferenceClock(iface);
 
     TRACE("(%p/%p)->()\n", This, iface);
 
-    return DSoundRender_AddRef((IBaseFilter*)This);
+    return BaseFilterImpl_AddRef(&This->renderer.filter.IBaseFilter_iface);
 }
 
 static ULONG WINAPI ReferenceClock_Release(IReferenceClock *iface)
 {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IReferenceClock(iface);
 
     TRACE("(%p/%p)->()\n", This, iface);
 
-    return DSoundRender_Release((IBaseFilter*)This);
+    return DSoundRender_Release(&This->renderer.filter.IBaseFilter_iface);
 }
 
 /*** IReferenceClock methods ***/
 static HRESULT WINAPI ReferenceClock_GetTime(IReferenceClock *iface,
                                              REFERENCE_TIME *pTime)
 {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IReferenceClock(iface);
     HRESULT hr = E_FAIL;
-    DWORD play_pos;
 
     TRACE("(%p/%p)->(%p)\n", This, iface, pTime);
+    if (!pTime)
+        return E_POINTER;
 
-    if (This->dsbuffer)
-        hr = DSoundRender_GetPos(This, &play_pos, pTime);
+    if (This->dsbuffer) {
+        DWORD writepos1, writepos2;
+        EnterCriticalSection(&This->renderer.filter.csFilter);
+        DSoundRender_UpdatePositions(This, &writepos1, &writepos2);
+        if (This->renderer.pInputPin && This->renderer.pInputPin->pin.mtCurrent.pbFormat)
+        {
+            *pTime = This->play_time + time_from_pos(This, This->last_playpos);
+            hr = S_OK;
+        }
+        else
+        {
+            ERR("pInputPin Disconncted\n");
+            hr = E_FAIL;
+        }
+        LeaveCriticalSection(&This->renderer.filter.csFilter);
+    }
     if (FAILED(hr))
-        ERR("Could not get reference time (%x)!\n", hr);
+        WARN("Could not get reference time (%x)!\n", hr);
 
     return hr;
 }
@@ -1244,34 +1063,92 @@ static HRESULT WINAPI ReferenceClock_AdviseTime(IReferenceClock *iface,
                                                 HEVENT hEvent,
                                                 DWORD_PTR *pdwAdviseCookie)
 {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IReferenceClock(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);
 
-    FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hEvent, pdwAdviseCookie);
+    if (when <= 0)
+        return E_INVALIDARG;
 
-    return E_NOTIMPL;
+    if (!pdwAdviseCookie)
+        return E_POINTER;
+
+    EnterCriticalSection(&This->renderer.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->renderer.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 rtBaseTime,
-                                                    REFERENCE_TIME rtStreamTime,
+                                                    REFERENCE_TIME rtStartTime,
+                                                    REFERENCE_TIME rtPeriodTime,
                                                     HSEMAPHORE hSemaphore,
                                                     DWORD_PTR *pdwAdviseCookie)
 {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IReferenceClock(iface);
+    struct dsoundrender_timer *t;
 
-    FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hSemaphore, pdwAdviseCookie);
+    TRACE("(%p/%p)->(%s, %s, %p, %p)\n", This, iface, wine_dbgstr_longlong(rtStartTime), wine_dbgstr_longlong(rtPeriodTime), (void*)hSemaphore, pdwAdviseCookie);
 
-    return E_NOTIMPL;
+    if (rtStartTime <= 0 || rtPeriodTime <= 0)
+        return E_INVALIDARG;
+
+    if (!pdwAdviseCookie)
+        return E_POINTER;
+
+    EnterCriticalSection(&This->renderer.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->renderer.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);
-
-    FIXME("(%p/%p)->(%p): stub!\n", This, iface, (void*)dwAdviseCookie);
+    DSoundRenderImpl *This = impl_from_IReferenceClock(iface);
 
-    return S_FALSE;
+    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 =
@@ -1285,90 +1162,40 @@ static const IReferenceClockVtbl IReferenceClock_Vtbl =
     ReferenceClock_Unadvise
 };
 
-static inline DSoundRenderImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
-{
-    return (DSoundRenderImpl *)((char*)iface - FIELD_OFFSET(DSoundRenderImpl, mediaSeeking.lpVtbl));
-}
-
-static HRESULT WINAPI sound_seek_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
-{
-    DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
-
-    return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
-}
-
-static ULONG WINAPI sound_seek_AddRef(IMediaSeeking * iface)
-{
-    DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
-
-    return IUnknown_AddRef((IUnknown *)This);
-}
-
-static ULONG WINAPI sound_seek_Release(IMediaSeeking * iface)
-{
-    DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
-
-    return IUnknown_Release((IUnknown *)This);
-}
-
-static const IMediaSeekingVtbl IMediaSeeking_Vtbl =
-{
-    sound_seek_QueryInterface,
-    sound_seek_AddRef,
-    sound_seek_Release,
-    MediaSeekingImpl_GetCapabilities,
-    MediaSeekingImpl_CheckCapabilities,
-    MediaSeekingImpl_IsFormatSupported,
-    MediaSeekingImpl_QueryPreferredFormat,
-    MediaSeekingImpl_GetTimeFormat,
-    MediaSeekingImpl_IsUsingTimeFormat,
-    MediaSeekingImpl_SetTimeFormat,
-    MediaSeekingImpl_GetDuration,
-    MediaSeekingImpl_GetStopPosition,
-    MediaSeekingImpl_GetCurrentPosition,
-    MediaSeekingImpl_ConvertTimeFormat,
-    MediaSeekingImpl_SetPositions,
-    MediaSeekingImpl_GetPositions,
-    MediaSeekingImpl_GetAvailable,
-    MediaSeekingImpl_SetRate,
-    MediaSeekingImpl_GetRate,
-    MediaSeekingImpl_GetPreroll
-};
-
 /*** IUnknown methods ***/
 static HRESULT WINAPI AMDirectSound_QueryInterface(IAMDirectSound *iface,
                                                REFIID riid,
                                                LPVOID*ppvObj)
 {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
 
     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
 
-    return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
+    return DSoundRender_QueryInterface(&This->renderer.filter.IBaseFilter_iface, riid, ppvObj);
 }
 
 static ULONG WINAPI AMDirectSound_AddRef(IAMDirectSound *iface)
 {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
 
     TRACE("(%p/%p)->()\n", This, iface);
 
-    return DSoundRender_AddRef((IBaseFilter*)This);
+    return BaseFilterImpl_AddRef(&This->renderer.filter.IBaseFilter_iface);
 }
 
 static ULONG WINAPI AMDirectSound_Release(IAMDirectSound *iface)
 {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
 
     TRACE("(%p/%p)->()\n", This, iface);
 
-    return DSoundRender_Release((IBaseFilter*)This);
+    return DSoundRender_Release(&This->renderer.filter.IBaseFilter_iface);
 }
 
 /*** IAMDirectSound methods ***/
 static HRESULT WINAPI AMDirectSound_GetDirectSoundInterface(IAMDirectSound *iface,  IDirectSound **ds)
 {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
 
     FIXME("(%p/%p)->(%p): stub\n", This, iface, ds);
 
@@ -1377,7 +1204,7 @@ static HRESULT WINAPI AMDirectSound_GetDirectSoundInterface(IAMDirectSound *ifac
 
 static HRESULT WINAPI AMDirectSound_GetPrimaryBufferInterface(IAMDirectSound *iface, IDirectSoundBuffer **buf)
 {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
 
     FIXME("(%p/%p)->(%p): stub\n", This, iface, buf);
 
@@ -1386,7 +1213,7 @@ static HRESULT WINAPI AMDirectSound_GetPrimaryBufferInterface(IAMDirectSound *if
 
 static HRESULT WINAPI AMDirectSound_GetSecondaryBufferInterface(IAMDirectSound *iface, IDirectSoundBuffer **buf)
 {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
 
     FIXME("(%p/%p)->(%p): stub\n", This, iface, buf);
 
@@ -1395,7 +1222,7 @@ static HRESULT WINAPI AMDirectSound_GetSecondaryBufferInterface(IAMDirectSound *
 
 static HRESULT WINAPI AMDirectSound_ReleaseDirectSoundInterface(IAMDirectSound *iface, IDirectSound *ds)
 {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
 
     FIXME("(%p/%p)->(%p): stub\n", This, iface, ds);
 
@@ -1404,7 +1231,7 @@ static HRESULT WINAPI AMDirectSound_ReleaseDirectSoundInterface(IAMDirectSound *
 
 static HRESULT WINAPI AMDirectSound_ReleasePrimaryBufferInterface(IAMDirectSound *iface, IDirectSoundBuffer *buf)
 {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
 
     FIXME("(%p/%p)->(%p): stub\n", This, iface, buf);
 
@@ -1413,27 +1240,27 @@ static HRESULT WINAPI AMDirectSound_ReleasePrimaryBufferInterface(IAMDirectSound
 
 static HRESULT WINAPI AMDirectSound_ReleaseSecondaryBufferInterface(IAMDirectSound *iface, IDirectSoundBuffer *buf)
 {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
 
     FIXME("(%p/%p)->(%p): stub\n", This, iface, buf);
 
     return E_NOTIMPL;
 }
 
-static HRESULT WINAPI AMDirectSound_SetFocusWindow(IAMDirectSound *iface, HWND hwnd, BOOL bgsilent)
+static HRESULT WINAPI AMDirectSound_SetFocusWindow(IAMDirectSound *iface, HWND hwnd, BOOL bgaudible)
 {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
 
-    FIXME("(%p/%p)->(%p,%d): stub\n", This, iface, hwnd, bgsilent);
+    FIXME("(%p/%p)->(%p,%d): stub\n", This, iface, hwnd, bgaudible);
 
     return E_NOTIMPL;
 }
 
-static HRESULT WINAPI AMDirectSound_GetFocusWindow(IAMDirectSound *iface, HWND hwnd)
+static HRESULT WINAPI AMDirectSound_GetFocusWindow(IAMDirectSound *iface, HWND *hwnd, BOOL *bgaudible)
 {
-    ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
+    DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
 
-    FIXME("(%p/%p)->(%p): stub\n", This, iface, hwnd);
+    FIXME("(%p/%p)->(%p,%p): stub\n", This, iface, hwnd, bgaudible);
 
     return E_NOTIMPL;
 }
@@ -1452,3 +1279,29 @@ static const IAMDirectSoundVtbl IAMDirectSound_Vtbl =
     AMDirectSound_SetFocusWindow,
     AMDirectSound_GetFocusWindow
 };
+
+static HRESULT WINAPI AMFilterMiscFlags_QueryInterface(IAMFilterMiscFlags *iface, REFIID riid, void **ppv) {
+    DSoundRenderImpl *This = impl_from_IAMFilterMiscFlags(iface);
+    return IUnknown_QueryInterface((IUnknown*)This, riid, ppv);
+}
+
+static ULONG WINAPI AMFilterMiscFlags_AddRef(IAMFilterMiscFlags *iface) {
+    DSoundRenderImpl *This = impl_from_IAMFilterMiscFlags(iface);
+    return IUnknown_AddRef((IUnknown*)This);
+}
+
+static ULONG WINAPI AMFilterMiscFlags_Release(IAMFilterMiscFlags *iface) {
+    DSoundRenderImpl *This = impl_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
+};