winex11: add owned windows to taskbar if owner is not mapped
[wine] / dlls / quartz / dsoundrender.c
1 /*
2  * Direct Sound Audio Renderer
3  *
4  * Copyright 2004 Christian Costa
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22
23 #include "quartz_private.h"
24 #include "control_private.h"
25 #include "pin.h"
26
27 #include "uuids.h"
28 #include "vfwmsgs.h"
29 #include "windef.h"
30 #include "winbase.h"
31 #include "dshow.h"
32 #include "evcode.h"
33 #include "strmif.h"
34 #include "dsound.h"
35
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
40
41 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
42
43 static const IBaseFilterVtbl DSoundRender_Vtbl;
44 static const IPinVtbl DSoundRender_InputPin_Vtbl;
45 static const IMemInputPinVtbl MemInputPin_Vtbl; 
46 static const IBasicAudioVtbl IBasicAudio_Vtbl;
47 static const IReferenceClockVtbl IReferenceClock_Vtbl;
48 static const IMediaSeekingVtbl IMediaSeeking_Vtbl;
49
50 typedef struct DSoundRenderImpl
51 {
52     const IBaseFilterVtbl * lpVtbl;
53     const IBasicAudioVtbl *IBasicAudio_vtbl;
54     const IReferenceClockVtbl *IReferenceClock_vtbl;
55
56     LONG refCount;
57     CRITICAL_SECTION csFilter;
58     FILTER_STATE state;
59     REFERENCE_TIME rtStreamStart, rtLastStop;
60     IReferenceClock * pClock;
61     FILTER_INFO filterInfo;
62
63     InputPin * pInputPin;
64     IPin ** ppPins;
65
66     LPDIRECTSOUND dsound;
67     LPDIRECTSOUNDBUFFER dsbuffer;
68     DWORD buf_size;
69     DWORD write_pos;
70     DWORD write_loops;
71
72     DWORD last_play_pos;
73     DWORD play_loops;
74
75     REFERENCE_TIME play_time;
76     MediaSeekingImpl mediaSeeking;
77
78     long volume;
79     long pan;
80 } DSoundRenderImpl;
81
82 /* Seeking is not needed for a renderer, rely on newsegment for the appropiate changes */
83 static HRESULT sound_mod_stop(IBaseFilter *iface)
84 {
85     TRACE("(%p)\n", iface);
86     return S_OK;
87 }
88
89 static HRESULT sound_mod_start(IBaseFilter *iface)
90 {
91     TRACE("(%p)\n", iface);
92
93     return S_OK;
94 }
95
96 static HRESULT sound_mod_rate(IBaseFilter *iface)
97 {
98     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
99
100     WAVEFORMATEX *format = (WAVEFORMATEX*)This->pInputPin->pin.mtCurrent.pbFormat;
101     DWORD freq = format->nSamplesPerSec;
102     double rate = This->mediaSeeking.dRate;
103
104     freq = (DWORD)((double)freq * rate);
105
106     TRACE("(%p)\n", iface);
107
108     if (freq > DSBFREQUENCY_MAX)
109         return VFW_E_UNSUPPORTED_AUDIO;
110
111     if (freq < DSBFREQUENCY_MIN)
112         return VFW_E_UNSUPPORTED_AUDIO;
113
114     return S_OK;
115 }
116
117 static inline HRESULT DSoundRender_GetPos(DSoundRenderImpl *This, DWORD *pPlayPos, REFERENCE_TIME *pRefTime)
118 {
119     HRESULT hr;
120
121     EnterCriticalSection(&This->csFilter);
122     {
123         DWORD state;
124         DWORD write_pos;
125
126         hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
127         if (SUCCEEDED(hr) && !(state & DSBSTATUS_PLAYING) && This->state == State_Running)
128         {
129             TRACE("Not playing, kickstarting the engine\n");
130
131             hr = IDirectSoundBuffer_Play(This->dsbuffer, 0, 0, DSBPLAY_LOOPING);
132             if (FAILED(hr))
133                 ERR("Can't play sound buffer (%x)\n", hr);
134         }
135
136         if (SUCCEEDED(hr))
137             hr = IDirectSoundBuffer_GetCurrentPosition(This->dsbuffer, pPlayPos, &write_pos);
138         if (hr == S_OK)
139         {
140             DWORD play_pos = *pPlayPos;
141
142             if (play_pos < This->last_play_pos)
143                 This->play_loops++;
144             This->last_play_pos = play_pos;
145
146             /* If we really fell behind, start at the next possible position
147              * Also happens when just starting playback for the first time,
148              * or when flushing
149              */
150             if ((This->play_loops*This->buf_size)+play_pos >=
151                 (This->write_loops*This->buf_size)+This->write_pos)
152                 This->write_pos = write_pos;
153
154             if (pRefTime)
155             {
156                 REFERENCE_TIME play_time;
157                 play_time = ((REFERENCE_TIME)This->play_loops*10000000) +
158                             ((REFERENCE_TIME)play_pos*10000000/This->buf_size);
159
160                 /* Don't let time run backwards */
161                 if(play_time-This->play_time > 0)
162                     This->play_time = play_time;
163                 else
164                     hr = S_FALSE;
165
166                 *pRefTime = This->play_time;
167             }
168         }
169     }
170     LeaveCriticalSection(&This->csFilter);
171
172     return hr;
173 }
174
175 static HRESULT DSoundRender_SendSampleData(DSoundRenderImpl* This, const BYTE *data, DWORD size)
176 {
177     HRESULT hr = S_OK;
178     LPBYTE lpbuf1 = NULL;
179     LPBYTE lpbuf2 = NULL;
180     DWORD dwsize1 = 0;
181     DWORD dwsize2 = 0;
182     DWORD size2;
183     DWORD play_pos,buf_free;
184
185     do {
186
187         hr = DSoundRender_GetPos(This, &play_pos, NULL);
188         if (hr != DS_OK)
189         {
190             ERR("GetPos returned error: %x\n", hr);
191             break;
192         }
193         if (This->write_pos <= play_pos)
194              buf_free = play_pos-This->write_pos;
195         else
196              buf_free = This->buf_size - This->write_pos + play_pos;
197
198         /* Wait for enough of the buffer to empty before filling it */
199         if(buf_free < This->buf_size/4)
200         {
201             Sleep(50);
202             continue;
203         }
204
205         size2 = min(buf_free, size);
206         hr = IDirectSoundBuffer_Lock(This->dsbuffer, This->write_pos, size2, (LPVOID *)&lpbuf1, &dwsize1, (LPVOID *)&lpbuf2, &dwsize2, 0);
207         if (hr != DS_OK) {
208             ERR("Unable to lock sound buffer! (%x)\n", hr);
209             break;
210         }
211         /* TRACE("write_pos=%d, size=%d, sz1=%d, sz2=%d\n", This->write_pos, size2, dwsize1, dwsize2); */
212
213         memcpy(lpbuf1, data, dwsize1);
214         if (dwsize2)
215             memcpy(lpbuf2, data + dwsize1, dwsize2);
216
217         hr = IDirectSoundBuffer_Unlock(This->dsbuffer, lpbuf1, dwsize1, lpbuf2, dwsize2);
218         if (hr != DS_OK)
219             ERR("Unable to unlock sound buffer! (%x)\n", hr);
220
221         size -= dwsize1 + dwsize2;
222         data += dwsize1 + dwsize2;
223         This->write_pos += dwsize1 + dwsize2;
224         if (This->write_pos >= This->buf_size)
225         {
226             This->write_pos -= This->buf_size;
227             This->write_loops++;
228         }
229     } while (size && This->state == State_Running);
230
231     return hr;
232 }
233
234 static HRESULT DSoundRender_Sample(LPVOID iface, IMediaSample * pSample)
235 {
236     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
237     LPBYTE pbSrcStream = NULL;
238     long cbSrcStream = 0;
239     REFERENCE_TIME tStart, tStop;
240     HRESULT hr;
241
242     TRACE("%p %p\n", iface, pSample);
243
244     /* Slightly incorrect, Pause completes when a frame is received so we should signal
245      * pause completion here, but for sound receiving a single frame doesn't make sense
246      */
247     if (This->state == State_Paused)
248         return S_FALSE;
249
250     if (This->state == State_Stopped)
251         return S_FALSE;
252
253     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
254     if (FAILED(hr))
255     {
256         ERR("Cannot get pointer to sample data (%x)\n", hr);
257         return hr;
258     }
259
260     hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
261     if (FAILED(hr))
262         ERR("Cannot get sample time (%x)\n", hr);
263
264     if (This->rtLastStop != tStart && (IMediaSample_IsDiscontinuity(pSample) == S_FALSE))
265         FIXME("Unexpected discontinuity: Last: %lld, tStart: %lld\n", This->rtLastStop, tStart);
266     This->rtLastStop = tStop;
267
268     cbSrcStream = IMediaSample_GetActualDataLength(pSample);
269     TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, cbSrcStream);
270
271 #if 0 /* For debugging purpose */
272     {
273         int i;
274         for(i = 0; i < cbSrcStream; i++)
275         {
276             if ((i!=0) && !(i%16))
277                 TRACE("\n");
278             TRACE("%02x ", pbSrcStream[i]);
279         }
280         TRACE("\n");
281     }
282 #endif
283
284     return DSoundRender_SendSampleData(This, pbSrcStream, cbSrcStream);
285 }
286
287 static HRESULT DSoundRender_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
288 {
289     WAVEFORMATEX* format = (WAVEFORMATEX*)pmt->pbFormat;
290     TRACE("wFormatTag = %x %x\n", format->wFormatTag, WAVE_FORMAT_PCM);
291     TRACE("nChannels = %d\n", format->nChannels);
292     TRACE("nSamplesPerSec = %d\n", format->nAvgBytesPerSec);
293     TRACE("nAvgBytesPerSec = %d\n", format->nAvgBytesPerSec);
294     TRACE("nBlockAlign = %d\n", format->nBlockAlign);
295     TRACE("wBitsPerSample = %d\n", format->wBitsPerSample);
296
297     if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio) && IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_PCM))
298         return S_OK;
299     return S_FALSE;
300 }
301
302 HRESULT DSoundRender_create(IUnknown * pUnkOuter, LPVOID * ppv)
303 {
304     HRESULT hr;
305     PIN_INFO piInput;
306     DSoundRenderImpl * pDSoundRender;
307
308     TRACE("(%p, %p)\n", pUnkOuter, ppv);
309
310     *ppv = NULL;
311
312     if (pUnkOuter)
313         return CLASS_E_NOAGGREGATION;
314     
315     pDSoundRender = CoTaskMemAlloc(sizeof(DSoundRenderImpl));
316     if (!pDSoundRender)
317         return E_OUTOFMEMORY;
318     ZeroMemory(pDSoundRender, sizeof(DSoundRenderImpl));
319
320     pDSoundRender->lpVtbl = &DSoundRender_Vtbl;
321     pDSoundRender->IBasicAudio_vtbl = &IBasicAudio_Vtbl;
322     pDSoundRender->IReferenceClock_vtbl = &IReferenceClock_Vtbl;
323     pDSoundRender->refCount = 1;
324     InitializeCriticalSection(&pDSoundRender->csFilter);
325     pDSoundRender->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DSoundRenderImpl.csFilter");
326     pDSoundRender->state = State_Stopped;
327
328     pDSoundRender->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
329     if (!pDSoundRender->ppPins)
330     {
331         pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
332         DeleteCriticalSection(&pDSoundRender->csFilter);
333         CoTaskMemFree(pDSoundRender);
334         return E_OUTOFMEMORY;
335     }
336
337     /* construct input pin */
338     piInput.dir = PINDIR_INPUT;
339     piInput.pFilter = (IBaseFilter *)pDSoundRender;
340     lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
341     hr = InputPin_Construct(&DSoundRender_InputPin_Vtbl, &piInput, DSoundRender_Sample, pDSoundRender, DSoundRender_QueryAccept, NULL, &pDSoundRender->csFilter, (IPin **)&pDSoundRender->pInputPin);
342
343     if (SUCCEEDED(hr))
344     {
345         hr = DirectSoundCreate(NULL, &pDSoundRender->dsound, NULL);
346         if (FAILED(hr))
347             ERR("Cannot create Direct Sound object (%x)\n", hr);
348     }
349
350     if (SUCCEEDED(hr))
351     {
352         MediaSeekingImpl_Init((IBaseFilter*)pDSoundRender, sound_mod_stop, sound_mod_start, sound_mod_rate, &pDSoundRender->mediaSeeking, &pDSoundRender->csFilter);
353         pDSoundRender->mediaSeeking.lpVtbl = &IMediaSeeking_Vtbl;
354
355         pDSoundRender->ppPins[0] = (IPin *)pDSoundRender->pInputPin;
356         *ppv = (LPVOID)pDSoundRender;
357     }
358     else
359     {
360         if (pDSoundRender->pInputPin)
361             IPin_Release((IPin*)pDSoundRender->pInputPin);
362         CoTaskMemFree(pDSoundRender->ppPins);
363         pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
364         DeleteCriticalSection(&pDSoundRender->csFilter);
365         CoTaskMemFree(pDSoundRender);
366     }
367
368     return hr;
369 }
370
371 static HRESULT WINAPI DSoundRender_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
372 {
373     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
374     TRACE("(%p, %p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
375
376     *ppv = NULL;
377
378     if (IsEqualIID(riid, &IID_IUnknown))
379         *ppv = (LPVOID)This;
380     else if (IsEqualIID(riid, &IID_IPersist))
381         *ppv = (LPVOID)This;
382     else if (IsEqualIID(riid, &IID_IMediaFilter))
383         *ppv = (LPVOID)This;
384     else if (IsEqualIID(riid, &IID_IBaseFilter))
385         *ppv = (LPVOID)This;
386     else if (IsEqualIID(riid, &IID_IBasicAudio))
387         *ppv = (LPVOID)&(This->IBasicAudio_vtbl);
388     else if (IsEqualIID(riid, &IID_IReferenceClock))
389         *ppv = (LPVOID)&(This->IReferenceClock_vtbl);
390     else if (IsEqualIID(riid, &IID_IMediaSeeking))
391         *ppv = &This->mediaSeeking.lpVtbl;
392
393     if (*ppv)
394     {
395         IUnknown_AddRef((IUnknown *)(*ppv));
396         return S_OK;
397     }
398
399     if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
400         FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
401
402     return E_NOINTERFACE;
403 }
404
405 static ULONG WINAPI DSoundRender_AddRef(IBaseFilter * iface)
406 {
407     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
408     ULONG refCount = InterlockedIncrement(&This->refCount);
409
410     TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
411
412     return refCount;
413 }
414
415 static ULONG WINAPI DSoundRender_Release(IBaseFilter * iface)
416 {
417     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
418     ULONG refCount = InterlockedDecrement(&This->refCount);
419
420     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
421
422     if (!refCount)
423     {
424         IPin *pConnectedTo;
425
426         if (This->pClock)
427             IReferenceClock_Release(This->pClock);
428
429         if (This->dsbuffer)
430             IDirectSoundBuffer_Release(This->dsbuffer);
431         This->dsbuffer = NULL;
432         if (This->dsound)
433             IDirectSound_Release(This->dsound);
434         This->dsound = NULL;
435        
436         if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[0], &pConnectedTo)))
437         {
438             IPin_Disconnect(pConnectedTo);
439             IPin_Release(pConnectedTo);
440         }
441         IPin_Disconnect(This->ppPins[0]);
442
443         IPin_Release(This->ppPins[0]);
444         
445         CoTaskMemFree(This->ppPins);
446         This->lpVtbl = NULL;
447         This->IBasicAudio_vtbl = NULL;
448         
449         This->csFilter.DebugInfo->Spare[0] = 0;
450         DeleteCriticalSection(&This->csFilter);
451
452         TRACE("Destroying Audio Renderer\n");
453         CoTaskMemFree(This);
454         
455         return 0;
456     }
457     else
458         return refCount;
459 }
460
461 /** IPersist methods **/
462
463 static HRESULT WINAPI DSoundRender_GetClassID(IBaseFilter * iface, CLSID * pClsid)
464 {
465     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
466     TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
467
468     *pClsid = CLSID_DSoundRender;
469
470     return S_OK;
471 }
472
473 /** IMediaFilter methods **/
474
475 static HRESULT WINAPI DSoundRender_Stop(IBaseFilter * iface)
476 {
477     HRESULT hr = S_OK;
478     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
479
480     TRACE("(%p/%p)->()\n", This, iface);
481
482     EnterCriticalSection(&This->csFilter);
483     {
484         DWORD state = 0;
485         if (This->dsbuffer)
486         {
487             hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
488             if (SUCCEEDED(hr))
489             {
490                 if (state & DSBSTATUS_PLAYING)
491                     hr = IDirectSoundBuffer_Stop(This->dsbuffer);
492             }
493         }
494         if (SUCCEEDED(hr))
495             This->state = State_Stopped;
496     }
497     LeaveCriticalSection(&This->csFilter);
498     
499     return hr;
500 }
501
502 static HRESULT WINAPI DSoundRender_Pause(IBaseFilter * iface)
503 {
504     HRESULT hr = S_OK;
505     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
506     
507     TRACE("(%p/%p)->()\n", This, iface);
508
509     EnterCriticalSection(&This->csFilter);
510     {
511         DWORD state = 0;
512         if (This->state == State_Stopped)
513             This->pInputPin->end_of_stream = 0;
514
515         if (This->dsbuffer)
516         {
517             hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
518             if (SUCCEEDED(hr))
519             {
520                 if (state & DSBSTATUS_PLAYING)
521                     hr = IDirectSoundBuffer_Stop(This->dsbuffer);
522             }
523         }
524         if (SUCCEEDED(hr))
525             This->state = State_Paused;
526     }
527     LeaveCriticalSection(&This->csFilter);
528
529     return hr;
530 }
531
532 static HRESULT WINAPI DSoundRender_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
533 {
534     HRESULT hr = S_OK;
535     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
536
537     TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
538
539     EnterCriticalSection(&This->csFilter);
540     {
541         This->rtStreamStart = tStart;
542         This->state = State_Running;
543         This->pInputPin->end_of_stream = 0;
544     }
545     LeaveCriticalSection(&This->csFilter);
546
547     return hr;
548 }
549
550 static HRESULT WINAPI DSoundRender_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
551 {
552     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
553
554     TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
555
556     EnterCriticalSection(&This->csFilter);
557     {
558         *pState = This->state;
559     }
560     LeaveCriticalSection(&This->csFilter);
561
562     return S_OK;
563 }
564
565 static HRESULT WINAPI DSoundRender_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
566 {
567     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
568
569     TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
570
571     EnterCriticalSection(&This->csFilter);
572     {
573         if (This->pClock)
574             IReferenceClock_Release(This->pClock);
575         This->pClock = pClock;
576         if (This->pClock)
577             IReferenceClock_AddRef(This->pClock);
578     }
579     LeaveCriticalSection(&This->csFilter);
580
581     return S_OK;
582 }
583
584 static HRESULT WINAPI DSoundRender_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
585 {
586     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
587
588     TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
589
590     EnterCriticalSection(&This->csFilter);
591     {
592         *ppClock = This->pClock;
593         IReferenceClock_AddRef(This->pClock);
594     }
595     LeaveCriticalSection(&This->csFilter);
596     
597     return S_OK;
598 }
599
600 /** IBaseFilter implementation **/
601
602 static HRESULT WINAPI DSoundRender_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
603 {
604     ENUMPINDETAILS epd;
605     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
606
607     TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
608
609     epd.cPins = 1; /* input pin */
610     epd.ppPins = This->ppPins;
611     return IEnumPinsImpl_Construct(&epd, ppEnum);
612 }
613
614 static HRESULT WINAPI DSoundRender_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
615 {
616     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
617
618     TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_w(Id), ppPin);
619     
620     FIXME("DSoundRender::FindPin(...)\n");
621
622     /* FIXME: critical section */
623
624     return E_NOTIMPL;
625 }
626
627 static HRESULT WINAPI DSoundRender_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
628 {
629     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
630
631     TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
632
633     strcpyW(pInfo->achName, This->filterInfo.achName);
634     pInfo->pGraph = This->filterInfo.pGraph;
635
636     if (pInfo->pGraph)
637         IFilterGraph_AddRef(pInfo->pGraph);
638     
639     return S_OK;
640 }
641
642 static HRESULT WINAPI DSoundRender_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
643 {
644     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
645
646     TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
647
648     EnterCriticalSection(&This->csFilter);
649     {
650         if (pName)
651             strcpyW(This->filterInfo.achName, pName);
652         else
653             *This->filterInfo.achName = '\0';
654         This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
655     }
656     LeaveCriticalSection(&This->csFilter);
657
658     return S_OK;
659 }
660
661 static HRESULT WINAPI DSoundRender_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
662 {
663     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
664     TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
665     return E_NOTIMPL;
666 }
667
668 static const IBaseFilterVtbl DSoundRender_Vtbl =
669 {
670     DSoundRender_QueryInterface,
671     DSoundRender_AddRef,
672     DSoundRender_Release,
673     DSoundRender_GetClassID,
674     DSoundRender_Stop,
675     DSoundRender_Pause,
676     DSoundRender_Run,
677     DSoundRender_GetState,
678     DSoundRender_SetSyncSource,
679     DSoundRender_GetSyncSource,
680     DSoundRender_EnumPins,
681     DSoundRender_FindPin,
682     DSoundRender_QueryFilterInfo,
683     DSoundRender_JoinFilterGraph,
684     DSoundRender_QueryVendorInfo
685 };
686
687 static HRESULT WINAPI DSoundRender_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
688 {
689     InputPin *This = (InputPin *)iface;
690     PIN_DIRECTION pindirReceive;
691     DSoundRenderImpl *DSImpl;
692     HRESULT hr = S_OK;
693
694     TRACE("(%p)->(%p, %p)\n", This, pReceivePin, pmt);
695     dump_AM_MEDIA_TYPE(pmt);
696
697     EnterCriticalSection(This->pin.pCritSec);
698     {
699         DSImpl = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
700         DSImpl->rtLastStop = -1;
701
702         if (This->pin.pConnectedTo)
703             hr = VFW_E_ALREADY_CONNECTED;
704
705         if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
706             hr = VFW_E_TYPE_NOT_ACCEPTED;
707
708         if (SUCCEEDED(hr))
709         {
710             IPin_QueryDirection(pReceivePin, &pindirReceive);
711
712             if (pindirReceive != PINDIR_OUTPUT)
713             {
714                 ERR("Can't connect from non-output pin\n");
715                 hr = VFW_E_INVALID_DIRECTION;
716             }
717         }
718
719         if (SUCCEEDED(hr))
720         {
721             WAVEFORMATEX *format;
722             DSBUFFERDESC buf_desc;
723
724             TRACE("MajorType %s\n", debugstr_guid(&pmt->majortype));
725             TRACE("SubType %s\n", debugstr_guid(&pmt->subtype));
726             TRACE("Format %s\n", debugstr_guid(&pmt->formattype));
727             TRACE("Size %d\n", pmt->cbFormat);
728
729             format = (WAVEFORMATEX*)pmt->pbFormat;
730
731             DSImpl->buf_size = format->nAvgBytesPerSec;
732
733             memset(&buf_desc,0,sizeof(DSBUFFERDESC));
734             buf_desc.dwSize = sizeof(DSBUFFERDESC);
735             buf_desc.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN |
736                                DSBCAPS_CTRLFREQUENCY |
737                                DSBCAPS_GETCURRENTPOSITION2;
738             buf_desc.dwBufferBytes = DSImpl->buf_size;
739             buf_desc.lpwfxFormat = format;
740             hr = IDirectSound_CreateSoundBuffer(DSImpl->dsound, &buf_desc, &DSImpl->dsbuffer, NULL);
741             if (FAILED(hr))
742                 ERR("Can't create sound buffer (%x)\n", hr);
743         }
744
745         if (SUCCEEDED(hr))
746         {
747             hr = IDirectSoundBuffer_SetVolume(DSImpl->dsbuffer, DSImpl->volume);
748             if (FAILED(hr))
749                 ERR("Can't set volume to %ld (%x)\n", DSImpl->volume, hr);
750
751             hr = IDirectSoundBuffer_SetPan(DSImpl->dsbuffer, DSImpl->pan);
752             if (FAILED(hr))
753                 ERR("Can't set pan to %ld (%x)\n", DSImpl->pan, hr);
754
755             DSImpl->write_pos = 0;
756             hr = S_OK;
757         }
758
759         if (SUCCEEDED(hr))
760         {
761             CopyMediaType(&This->pin.mtCurrent, pmt);
762             This->pin.pConnectedTo = pReceivePin;
763             IPin_AddRef(pReceivePin);
764         }
765         else
766         {
767             if (DSImpl->dsbuffer)
768                 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
769             DSImpl->dsbuffer = NULL;
770         }
771     }
772     LeaveCriticalSection(This->pin.pCritSec);
773
774     return hr;
775 }
776
777 static HRESULT WINAPI DSoundRender_InputPin_Disconnect(IPin * iface)
778 {
779     IPinImpl *This = (IPinImpl*)iface;
780     DSoundRenderImpl *DSImpl;
781
782     TRACE("(%p)->()\n", iface);
783
784     DSImpl = (DSoundRenderImpl*)This->pinInfo.pFilter;
785     if (DSImpl->dsbuffer)
786         IDirectSoundBuffer_Release(DSImpl->dsbuffer);
787     DSImpl->dsbuffer = NULL;
788
789     return IPinImpl_Disconnect(iface);
790 }
791
792 static HRESULT WINAPI DSoundRender_InputPin_EndOfStream(IPin * iface)
793 {
794     InputPin* This = (InputPin*)iface;
795     DSoundRenderImpl *me = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
796     IMediaEventSink* pEventSink;
797     HRESULT hr;
798
799     EnterCriticalSection(This->pin.pCritSec);
800
801     TRACE("(%p/%p)->()\n", This, iface);
802     InputPin_EndOfStream(iface);
803
804     hr = IFilterGraph_QueryInterface(me->filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
805     if (SUCCEEDED(hr))
806     {
807         BYTE * silence;
808
809         silence = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, me->buf_size);
810         if (silence)
811         {
812             memset(silence, 0, me->buf_size);
813             DSoundRender_SendSampleData((DSoundRenderImpl*)This->pin.pinInfo.pFilter, silence, me->buf_size);
814             HeapFree(GetProcessHeap(), 0, silence);
815         }
816
817         hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, 0);
818         IMediaEventSink_Release(pEventSink);
819     }
820     LeaveCriticalSection(This->pin.pCritSec);
821
822     return hr;
823 }
824
825 static HRESULT WINAPI DSoundRender_InputPin_BeginFlush(IPin * iface)
826 {
827     InputPin *This = (InputPin *)iface;
828     DSoundRenderImpl *pFilter = (DSoundRenderImpl *)This->pin.pinInfo.pFilter;
829     HRESULT hr;
830     LPBYTE buffer;
831     DWORD size;
832
833     TRACE("\n");
834
835     EnterCriticalSection(This->pin.pCritSec);
836     hr = InputPin_BeginFlush(iface);
837
838     if (pFilter->dsbuffer)
839     {
840         IDirectSoundBuffer_Stop(pFilter->dsbuffer);
841
842         /* Force a reset */
843         IDirectSoundBuffer_SetCurrentPosition(pFilter->dsbuffer, 0);
844         pFilter->write_pos = pFilter->last_play_pos = 0;
845         ++pFilter->play_loops;
846         pFilter->write_loops = pFilter->play_loops;
847
848         IDirectSoundBuffer_Lock(pFilter->dsbuffer, 0, 0, (LPVOID *)&buffer, &size, NULL, NULL, DSBLOCK_ENTIREBUFFER);
849         memset(buffer, 0, size);
850         IDirectSoundBuffer_Unlock(pFilter->dsbuffer, buffer, size, NULL, 0);
851     }
852     LeaveCriticalSection(This->pin.pCritSec);
853
854     return hr;
855 }
856
857 static const IPinVtbl DSoundRender_InputPin_Vtbl =
858 {
859     InputPin_QueryInterface,
860     IPinImpl_AddRef,
861     InputPin_Release,
862     InputPin_Connect,
863     DSoundRender_InputPin_ReceiveConnection,
864     DSoundRender_InputPin_Disconnect,
865     IPinImpl_ConnectedTo,
866     IPinImpl_ConnectionMediaType,
867     IPinImpl_QueryPinInfo,
868     IPinImpl_QueryDirection,
869     IPinImpl_QueryId,
870     IPinImpl_QueryAccept,
871     IPinImpl_EnumMediaTypes,
872     IPinImpl_QueryInternalConnections,
873     DSoundRender_InputPin_EndOfStream,
874     DSoundRender_InputPin_BeginFlush,
875     InputPin_EndFlush,
876     InputPin_NewSegment
877 };
878
879 static const IMemInputPinVtbl MemInputPin_Vtbl = 
880 {
881     MemInputPin_QueryInterface,
882     MemInputPin_AddRef,
883     MemInputPin_Release,
884     MemInputPin_GetAllocator,
885     MemInputPin_NotifyAllocator,
886     MemInputPin_GetAllocatorRequirements,
887     MemInputPin_Receive,
888     MemInputPin_ReceiveMultiple,
889     MemInputPin_ReceiveCanBlock
890 };
891
892 /*** IUnknown methods ***/
893 static HRESULT WINAPI Basicaudio_QueryInterface(IBasicAudio *iface,
894                                                 REFIID riid,
895                                                 LPVOID*ppvObj) {
896     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
897
898     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
899
900     return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
901 }
902
903 static ULONG WINAPI Basicaudio_AddRef(IBasicAudio *iface) {
904     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
905
906     TRACE("(%p/%p)->()\n", This, iface);
907
908     return DSoundRender_AddRef((IBaseFilter*)This);
909 }
910
911 static ULONG WINAPI Basicaudio_Release(IBasicAudio *iface) {
912     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
913
914     TRACE("(%p/%p)->()\n", This, iface);
915
916     return DSoundRender_Release((IBaseFilter*)This);
917 }
918
919 /*** IDispatch methods ***/
920 static HRESULT WINAPI Basicaudio_GetTypeInfoCount(IBasicAudio *iface,
921                                                   UINT*pctinfo) {
922     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
923
924     TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, pctinfo);
925
926     return S_OK;
927 }
928
929 static HRESULT WINAPI Basicaudio_GetTypeInfo(IBasicAudio *iface,
930                                              UINT iTInfo,
931                                              LCID lcid,
932                                              ITypeInfo**ppTInfo) {
933     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
934
935     TRACE("(%p/%p)->(%d, %d, %p): stub !!!\n", This, iface, iTInfo, lcid, ppTInfo);
936
937     return S_OK;
938 }
939
940 static HRESULT WINAPI Basicaudio_GetIDsOfNames(IBasicAudio *iface,
941                                                REFIID riid,
942                                                LPOLESTR*rgszNames,
943                                                UINT cNames,
944                                                LCID lcid,
945                                                DISPID*rgDispId) {
946     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
947
948     TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
949
950     return S_OK;
951 }
952
953 static HRESULT WINAPI Basicaudio_Invoke(IBasicAudio *iface,
954                                         DISPID dispIdMember,
955                                         REFIID riid,
956                                         LCID lcid,
957                                         WORD wFlags,
958                                         DISPPARAMS*pDispParams,
959                                         VARIANT*pVarResult,
960                                         EXCEPINFO*pExepInfo,
961                                         UINT*puArgErr) {
962     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
963
964     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);
965
966     return S_OK;
967 }
968
969 /*** IBasicAudio methods ***/
970 static HRESULT WINAPI Basicaudio_put_Volume(IBasicAudio *iface,
971                                             long lVolume) {
972     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
973
974     TRACE("(%p/%p)->(%ld)\n", This, iface, lVolume);
975
976     if (lVolume > DSBVOLUME_MAX || lVolume < DSBVOLUME_MIN)
977         return E_INVALIDARG;
978
979     if (This->dsbuffer) {
980         if (FAILED(IDirectSoundBuffer_SetVolume(This->dsbuffer, lVolume)))
981             return E_FAIL;
982     }
983
984     This->volume = lVolume;
985     return S_OK;
986 }
987
988 static HRESULT WINAPI Basicaudio_get_Volume(IBasicAudio *iface,
989                                             long *plVolume) {
990     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
991
992     TRACE("(%p/%p)->(%p)\n", This, iface, plVolume);
993
994     if (!plVolume)
995         return E_POINTER;
996
997     *plVolume = This->volume;
998     return S_OK;
999 }
1000
1001 static HRESULT WINAPI Basicaudio_put_Balance(IBasicAudio *iface,
1002                                              long lBalance) {
1003     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1004
1005     TRACE("(%p/%p)->(%ld)\n", This, iface, lBalance);
1006
1007     if (lBalance < DSBPAN_LEFT || lBalance > DSBPAN_RIGHT)
1008         return E_INVALIDARG;
1009
1010     if (This->dsbuffer) {
1011         if (FAILED(IDirectSoundBuffer_SetPan(This->dsbuffer, lBalance)))
1012             return E_FAIL;
1013     }
1014
1015     This->pan = lBalance;
1016     return S_OK;
1017 }
1018
1019 static HRESULT WINAPI Basicaudio_get_Balance(IBasicAudio *iface,
1020                                              long *plBalance) {
1021     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1022
1023     TRACE("(%p/%p)->(%p)\n", This, iface, plBalance);
1024
1025     if (!plBalance)
1026         return E_POINTER;
1027
1028     *plBalance = This->pan;
1029     return S_OK;
1030 }
1031
1032 static const IBasicAudioVtbl IBasicAudio_Vtbl =
1033 {
1034     Basicaudio_QueryInterface,
1035     Basicaudio_AddRef,
1036     Basicaudio_Release,
1037     Basicaudio_GetTypeInfoCount,
1038     Basicaudio_GetTypeInfo,
1039     Basicaudio_GetIDsOfNames,
1040     Basicaudio_Invoke,
1041     Basicaudio_put_Volume,
1042     Basicaudio_get_Volume,
1043     Basicaudio_put_Balance,
1044     Basicaudio_get_Balance
1045 };
1046
1047
1048 /*** IUnknown methods ***/
1049 static HRESULT WINAPI ReferenceClock_QueryInterface(IReferenceClock *iface,
1050                                                 REFIID riid,
1051                                                 LPVOID*ppvObj)
1052 {
1053     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1054
1055     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
1056
1057     return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
1058 }
1059
1060 static ULONG WINAPI ReferenceClock_AddRef(IReferenceClock *iface)
1061 {
1062     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1063
1064     TRACE("(%p/%p)->()\n", This, iface);
1065
1066     return DSoundRender_AddRef((IBaseFilter*)This);
1067 }
1068
1069 static ULONG WINAPI ReferenceClock_Release(IReferenceClock *iface)
1070 {
1071     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1072
1073     TRACE("(%p/%p)->()\n", This, iface);
1074
1075     return DSoundRender_Release((IBaseFilter*)This);
1076 }
1077
1078 /*** IReferenceClock methods ***/
1079 static HRESULT WINAPI ReferenceClock_GetTime(IReferenceClock *iface,
1080                                              REFERENCE_TIME *pTime)
1081 {
1082     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1083     HRESULT hr = E_FAIL;
1084     DWORD play_pos;
1085
1086     TRACE("(%p/%p)->(%p)\n", This, iface, pTime);
1087
1088     if (This->dsbuffer)
1089         hr = DSoundRender_GetPos(This, &play_pos, pTime);
1090     if (FAILED(hr))
1091         ERR("Could not get reference time (%x)!\n", hr);
1092
1093     return hr;
1094 }
1095
1096 static HRESULT WINAPI ReferenceClock_AdviseTime(IReferenceClock *iface,
1097                                                 REFERENCE_TIME rtBaseTime,
1098                                                 REFERENCE_TIME rtStreamTime,
1099                                                 HEVENT hEvent,
1100                                                 DWORD_PTR *pdwAdviseCookie)
1101 {
1102     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1103
1104     FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hEvent, pdwAdviseCookie);
1105
1106     return E_NOTIMPL;
1107 }
1108
1109 static HRESULT WINAPI ReferenceClock_AdvisePeriodic(IReferenceClock *iface,
1110                                                     REFERENCE_TIME rtBaseTime,
1111                                                     REFERENCE_TIME rtStreamTime,
1112                                                     HSEMAPHORE hSemaphore,
1113                                                     DWORD_PTR *pdwAdviseCookie)
1114 {
1115     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1116
1117     FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hSemaphore, pdwAdviseCookie);
1118
1119     return E_NOTIMPL;
1120 }
1121
1122 static HRESULT WINAPI ReferenceClock_Unadvise(IReferenceClock *iface,
1123                                               DWORD_PTR dwAdviseCookie)
1124 {
1125     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1126
1127     FIXME("(%p/%p)->(%p): stub!\n", This, iface, (void*)dwAdviseCookie);
1128
1129     return S_FALSE;
1130 }
1131
1132 static const IReferenceClockVtbl IReferenceClock_Vtbl =
1133 {
1134     ReferenceClock_QueryInterface,
1135     ReferenceClock_AddRef,
1136     ReferenceClock_Release,
1137     ReferenceClock_GetTime,
1138     ReferenceClock_AdviseTime,
1139     ReferenceClock_AdvisePeriodic,
1140     ReferenceClock_Unadvise
1141 };
1142
1143 static inline DSoundRenderImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
1144 {
1145     return (DSoundRenderImpl *)((char*)iface - FIELD_OFFSET(DSoundRenderImpl, mediaSeeking.lpVtbl));
1146 }
1147
1148 static HRESULT WINAPI sound_seek_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
1149 {
1150     DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1151
1152     return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
1153 }
1154
1155 static ULONG WINAPI sound_seek_AddRef(IMediaSeeking * iface)
1156 {
1157     DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1158
1159     return IUnknown_AddRef((IUnknown *)This);
1160 }
1161
1162 static ULONG WINAPI sound_seek_Release(IMediaSeeking * iface)
1163 {
1164     DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1165
1166     return IUnknown_Release((IUnknown *)This);
1167 }
1168
1169 static const IMediaSeekingVtbl IMediaSeeking_Vtbl =
1170 {
1171     sound_seek_QueryInterface,
1172     sound_seek_AddRef,
1173     sound_seek_Release,
1174     MediaSeekingImpl_GetCapabilities,
1175     MediaSeekingImpl_CheckCapabilities,
1176     MediaSeekingImpl_IsFormatSupported,
1177     MediaSeekingImpl_QueryPreferredFormat,
1178     MediaSeekingImpl_GetTimeFormat,
1179     MediaSeekingImpl_IsUsingTimeFormat,
1180     MediaSeekingImpl_SetTimeFormat,
1181     MediaSeekingImpl_GetDuration,
1182     MediaSeekingImpl_GetStopPosition,
1183     MediaSeekingImpl_GetCurrentPosition,
1184     MediaSeekingImpl_ConvertTimeFormat,
1185     MediaSeekingImpl_SetPositions,
1186     MediaSeekingImpl_GetPositions,
1187     MediaSeekingImpl_GetAvailable,
1188     MediaSeekingImpl_SetRate,
1189     MediaSeekingImpl_GetRate,
1190     MediaSeekingImpl_GetPreroll
1191 };