winex11: add owned windows to taskbar if owner is not mapped
[wine] / dlls / quartz / waveparser.c
1 /*
2  * WAVE Parser Filter
3  *
4  * Copyright 2005 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 "quartz_private.h"
22 #include "control_private.h"
23 #include "pin.h"
24
25 #include "uuids.h"
26 #include "aviriff.h"
27 #include "vfwmsgs.h"
28 #include "mmsystem.h"
29
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
32
33 #include <math.h>
34 #include <assert.h>
35
36 #include "parser.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
39
40 static const WCHAR wcsOutputPinName[] = {'o','u','t','p','u','t',' ','p','i','n',0};
41
42 typedef struct WAVEParserImpl
43 {
44     ParserImpl Parser;
45     IMediaSample * pCurrentSample;
46     LONGLONG StartOfFile; /* in media time */
47     LONGLONG EndOfFile;
48     DWORD dwSampleSize;
49     DWORD nSamplesPerSec;
50     DWORD dwLength;
51 } WAVEParserImpl;
52
53 static LONGLONG bytepos_to_duration(WAVEParserImpl *This, LONGLONG bytepos)
54 {
55     LONGLONG duration = BYTES_FROM_MEDIATIME(bytepos - This->StartOfFile);
56     duration *= 10000000;
57     duration /= (This->dwSampleSize * This->nSamplesPerSec);
58
59     return duration;
60 }
61
62 static LONGLONG duration_to_bytepos(WAVEParserImpl *This, LONGLONG duration)
63 {
64     LONGLONG bytepos;
65
66     bytepos = (This->dwSampleSize * This->nSamplesPerSec);
67     bytepos *= duration;
68     bytepos /= 10000000;
69     bytepos += BYTES_FROM_MEDIATIME(This->StartOfFile);
70     bytepos -= bytepos % This->dwSampleSize;
71
72     return MEDIATIME_FROM_BYTES(bytepos);
73 }
74
75 static HRESULT WAVEParser_Sample(LPVOID iface, IMediaSample * pSample)
76 {
77     WAVEParserImpl *This = (WAVEParserImpl *)iface;
78     LPBYTE pbSrcStream = NULL;
79     long cbSrcStream = 0;
80     REFERENCE_TIME tStart, tStop;
81     HRESULT hr;
82     BOOL bMoreData = TRUE;
83     Parser_OutputPin * pOutputPin;
84     BYTE * pbDstStream;
85     long cbDstStream;
86     long chunk_remaining_bytes = 0;
87     long offset_src = 0;
88
89     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
90
91     hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
92
93     cbSrcStream = IMediaSample_GetActualDataLength(pSample);
94
95     assert(BYTES_FROM_MEDIATIME(tStop - tStart) == cbSrcStream);
96
97     pOutputPin = (Parser_OutputPin *)This->Parser.ppPins[1];
98
99     /* Try to get rid of the current sample in case we had a S_FALSE last time */
100     if (This->pCurrentSample && (IMediaSample_GetActualDataLength(This->pCurrentSample) == IMediaSample_GetSize(This->pCurrentSample)))
101     {
102         HRESULT hr;
103
104         /* Unset advancement */
105         This->Parser.pInputPin->rtCurrent -= MEDIATIME_FROM_BYTES(cbSrcStream);
106
107         hr = OutputPin_SendSample(&pOutputPin->pin, This->pCurrentSample);
108
109         if (hr != S_OK)
110             return hr;
111
112         IMediaSample_Release(This->pCurrentSample);
113         This->pCurrentSample = NULL;
114
115         This->Parser.pInputPin->rtCurrent += MEDIATIME_FROM_BYTES(cbSrcStream);
116     }
117
118     if (tStop < This->StartOfFile)
119         return S_OK;
120
121     if (tStart < This->StartOfFile)
122         offset_src = BYTES_FROM_MEDIATIME(This->StartOfFile - tStart);
123
124     while (bMoreData)
125     {
126         if (!This->pCurrentSample)
127         {
128             /* cache media sample until it is ready to be dispatched
129              * (i.e. we reach the end of the chunk) */
130             hr = OutputPin_GetDeliveryBuffer(&pOutputPin->pin, &This->pCurrentSample, NULL, NULL, 0);
131
132             if (SUCCEEDED(hr))
133             {
134                 hr = IMediaSample_SetActualDataLength(This->pCurrentSample, 0);
135                 assert(hr == S_OK);
136             }
137             else
138             {
139                 TRACE("Skipping sending sample due to error (%x)\n", hr);
140                 This->pCurrentSample = NULL;
141                 break;
142             }
143         }
144
145         hr = IMediaSample_GetPointer(This->pCurrentSample, &pbDstStream);
146
147         if (SUCCEEDED(hr))
148         {
149             cbDstStream = IMediaSample_GetSize(This->pCurrentSample);
150
151             chunk_remaining_bytes = cbDstStream - IMediaSample_GetActualDataLength(This->pCurrentSample);
152         
153             assert(chunk_remaining_bytes >= 0);
154             assert(chunk_remaining_bytes <= cbDstStream - IMediaSample_GetActualDataLength(This->pCurrentSample));
155         }
156
157         if (chunk_remaining_bytes <= cbSrcStream - offset_src)
158         {
159             if (SUCCEEDED(hr))
160             {
161                 memcpy(pbDstStream + IMediaSample_GetActualDataLength(This->pCurrentSample), pbSrcStream + offset_src, chunk_remaining_bytes);
162                 hr = IMediaSample_SetActualDataLength(This->pCurrentSample, chunk_remaining_bytes + IMediaSample_GetActualDataLength(This->pCurrentSample));
163                 assert(hr == S_OK);
164             }
165
166             if (SUCCEEDED(hr))
167             {
168                 REFERENCE_TIME tAviStart, tAviStop, tOffset;
169
170                 IMediaSample_SetDiscontinuity(This->pCurrentSample, pOutputPin->dwSamplesProcessed == 0);
171
172                 IMediaSample_SetSyncPoint(This->pCurrentSample, TRUE);
173                 pOutputPin->dwSamplesProcessed++;
174
175                 tOffset = MEDIATIME_FROM_BYTES(offset_src + chunk_remaining_bytes - IMediaSample_GetActualDataLength(This->pCurrentSample));
176                 tAviStart = bytepos_to_duration(This, tStart + tOffset);
177
178                 tOffset += MEDIATIME_FROM_BYTES(IMediaSample_GetActualDataLength(This->pCurrentSample));
179                 tAviStop = bytepos_to_duration(This, tStart + tOffset);
180
181                 IMediaSample_SetTime(This->pCurrentSample, &tAviStart, &tAviStop);
182
183                 hr = OutputPin_SendSample(&pOutputPin->pin, This->pCurrentSample);
184                 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED && hr != S_FALSE)
185                     ERR("Error sending sample (%x)\n", hr);
186             }
187
188             if (This->pCurrentSample && hr != S_FALSE)
189             {
190                 IMediaSample_Release(This->pCurrentSample);
191                 This->pCurrentSample = NULL;
192             }
193             if (hr == S_FALSE)
194             {
195                 /* Break out */
196                 This->Parser.pInputPin->rtCurrent -= MEDIATIME_FROM_BYTES(cbSrcStream - offset_src - chunk_remaining_bytes);
197                 hr = S_OK;
198                 break;
199             }
200
201         }
202         else
203         {
204             if (SUCCEEDED(hr))
205             {
206                 memcpy(pbDstStream + IMediaSample_GetActualDataLength(This->pCurrentSample), pbSrcStream + offset_src, cbSrcStream - offset_src);
207                 IMediaSample_SetActualDataLength(This->pCurrentSample, cbSrcStream - offset_src + IMediaSample_GetActualDataLength(This->pCurrentSample));
208             }
209             bMoreData = FALSE;
210         }
211         offset_src += chunk_remaining_bytes;
212     }
213
214     if (tStop >= This->EndOfFile || (bytepos_to_duration(This, tStop) >= This->Parser.mediaSeeking.llStop))
215     {
216         int i;
217
218         TRACE("End of file reached\n");
219
220         for (i = 0; i < This->Parser.cStreams; i++)
221         {
222             IPin* ppin;
223             HRESULT hr;
224
225             TRACE("Send End Of Stream to output pin %d\n", i);
226
227             hr = IPin_ConnectedTo(This->Parser.ppPins[i+1], &ppin);
228             if (SUCCEEDED(hr))
229             {
230                 hr = IPin_EndOfStream(ppin);
231                 IPin_Release(ppin);
232             }
233             if (FAILED(hr))
234             {
235                 ERR("%x\n", hr);
236                 break;
237             }
238         }
239
240         /* Force the pullpin thread to stop */
241         hr = S_FALSE;
242     }
243
244     return hr;
245 }
246
247 static HRESULT WAVEParser_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
248 {
249     if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
250         return S_FALSE;
251     if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_WAVE))
252         return S_OK;
253     if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_AU) || IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_AIFF))
254         FIXME("AU and AIFF files not supported yet!\n");
255     return S_FALSE;
256 }
257
258 static HRESULT WAVEParserImpl_seek(IBaseFilter *iface)
259 {
260     WAVEParserImpl *This = (WAVEParserImpl *)iface;
261     PullPin *pPin = This->Parser.pInputPin;
262     IPin *victim = NULL;
263     LONGLONG newpos, curpos, endpos, bytepos;
264
265     newpos = This->Parser.mediaSeeking.llCurrent;
266     curpos = bytepos_to_duration(This, pPin->rtCurrent);
267     endpos = bytepos_to_duration(This, This->EndOfFile);
268     bytepos = duration_to_bytepos(This, newpos);
269
270     if (newpos > endpos)
271     {
272         WARN("Requesting position %x%08x beyond end of stream %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(endpos>>32), (DWORD)endpos);
273         return E_INVALIDARG;
274     }
275
276     if (curpos/1000000 == newpos/1000000)
277     {
278         TRACE("Requesting position %x%08x same as current position %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(curpos>>32), (DWORD)curpos);
279         return S_OK;
280     }
281
282     TRACE("Moving sound to %08u bytes!\n", (DWORD)BYTES_FROM_MEDIATIME(bytepos));
283
284     EnterCriticalSection(&pPin->thread_lock);
285     IPin_BeginFlush((IPin *)pPin);
286
287     /* Make sure this is done while stopped, BeginFlush takes care of this */
288     EnterCriticalSection(&This->Parser.csFilter);
289     IPin_ConnectedTo(This->Parser.ppPins[1], &victim);
290     if (victim)
291     {
292         IPin_NewSegment(victim, newpos, endpos, pPin->dRate);
293         IPin_Release(victim);
294     }
295
296     pPin->rtStart = pPin->rtCurrent = bytepos;
297     ((Parser_OutputPin *)This->Parser.ppPins[1])->dwSamplesProcessed = 0;
298     LeaveCriticalSection(&This->Parser.csFilter);
299
300     TRACE("Done flushing\n");
301     IPin_EndFlush((IPin *)pPin);
302     LeaveCriticalSection(&pPin->thread_lock);
303
304     return S_OK;
305 }
306
307 static HRESULT WAVEParser_InputPin_PreConnect(IPin * iface, IPin * pConnectPin)
308 {
309     PullPin *This = (PullPin *)iface;
310     HRESULT hr;
311     RIFFLIST list;
312     RIFFCHUNK chunk;
313     LONGLONG pos = 0; /* in bytes */
314     PIN_INFO piOutput;
315     ALLOCATOR_PROPERTIES props;
316     AM_MEDIA_TYPE amt;
317     WAVEParserImpl * pWAVEParser = (WAVEParserImpl *)This->pin.pinInfo.pFilter;
318     LONGLONG length, avail;
319
320     piOutput.dir = PINDIR_OUTPUT;
321     piOutput.pFilter = (IBaseFilter *)This;
322     lstrcpynW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
323     
324     hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
325     pos += sizeof(list);
326
327    if (list.fcc != FOURCC_RIFF)
328     {
329         ERR("Input stream not a RIFF file\n");
330         return E_FAIL;
331     }
332     if (list.cb > 1 * 1024 * 1024 * 1024) /* cannot be more than 1Gb in size */
333     {
334         ERR("Input stream violates RIFF spec\n");
335         return E_FAIL;
336     }
337     if (list.fccListType != mmioFOURCC('W','A','V','E'))
338     {
339         ERR("Input stream not an WAVE RIFF file\n");
340         return E_FAIL;
341     }
342
343     hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(chunk), (BYTE *)&chunk);
344     pos += sizeof(chunk);
345     if (chunk.fcc != mmioFOURCC('f','m','t',' '))
346     {
347         ERR("Expected 'fmt ' chunk, but got %.04s\n", (LPSTR)&chunk.fcc);
348         return E_FAIL;
349     }
350
351     amt.majortype = MEDIATYPE_Audio;
352     amt.formattype = FORMAT_WaveFormatEx;
353     amt.cbFormat = chunk.cb;
354     amt.pbFormat = CoTaskMemAlloc(amt.cbFormat);
355     amt.pUnk = NULL;
356     hr = IAsyncReader_SyncRead(This->pReader, pos, amt.cbFormat, amt.pbFormat);
357     amt.subtype = MEDIATYPE_Audio;
358     amt.subtype.Data1 = ((WAVEFORMATEX*)amt.pbFormat)->wFormatTag;
359
360     pos += chunk.cb;
361     hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(chunk), (BYTE *)&chunk);
362     if (chunk.fcc == mmioFOURCC('f','a','c','t'))
363     {
364         FIXME("'fact' chunk not supported yet\n");
365         pos += sizeof(chunk) + chunk.cb;
366         hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(chunk), (BYTE *)&chunk);
367     }
368     if (chunk.fcc != mmioFOURCC('d','a','t','a'))
369     {
370         ERR("Expected 'data' chunk, but got %.04s\n", (LPSTR)&chunk.fcc);
371         return E_FAIL;
372     }
373
374     if (hr == S_OK)
375     {
376         pWAVEParser->StartOfFile = MEDIATIME_FROM_BYTES(pos + sizeof(RIFFCHUNK));
377         pWAVEParser->EndOfFile = MEDIATIME_FROM_BYTES(pos + chunk.cb + sizeof(RIFFCHUNK));
378     }
379
380     if (hr != S_OK)
381         return E_FAIL;
382
383     props.cbAlign = ((WAVEFORMATEX*)amt.pbFormat)->nBlockAlign;
384     props.cbPrefix = 0;
385     props.cbBuffer = 4096;
386     props.cBuffers = 2;
387     pWAVEParser->dwSampleSize = ((WAVEFORMATEX*)amt.pbFormat)->nBlockAlign;
388     IAsyncReader_Length(This->pReader, &length, &avail);
389     pWAVEParser->dwLength = length / (ULONGLONG)pWAVEParser->dwSampleSize;
390     pWAVEParser->nSamplesPerSec = ((WAVEFORMATEX*)amt.pbFormat)->nSamplesPerSec;
391     hr = Parser_AddPin(&(pWAVEParser->Parser), &piOutput, &props, &amt);
392     CoTaskMemFree(amt.pbFormat);
393
394     pWAVEParser->Parser.mediaSeeking.llCurrent = 0;
395     pWAVEParser->Parser.mediaSeeking.llStop = pWAVEParser->Parser.mediaSeeking.llDuration = bytepos_to_duration(pWAVEParser, pWAVEParser->EndOfFile);
396     TRACE("Duration: %lld seconds\n", pWAVEParser->Parser.mediaSeeking.llDuration / (LONGLONG)10000000);
397
398     This->rtStop = pWAVEParser->EndOfFile;
399     This->rtStart = pWAVEParser->StartOfFile;
400
401     TRACE("WAVE File ok\n");
402
403     return hr;
404 }
405
406 static HRESULT WAVEParser_Cleanup(LPVOID iface)
407 {
408     WAVEParserImpl *This = (WAVEParserImpl*)iface;
409
410     TRACE("(%p)->()\n", This);
411
412     if (This->pCurrentSample)
413         IMediaSample_Release(This->pCurrentSample);
414     This->pCurrentSample = NULL;
415
416     return S_OK;
417 }
418
419 static HRESULT WAVEParser_disconnect(LPVOID iface)
420 {
421     /* TODO: Find and plug memory leaks */
422     return S_OK;
423 }
424
425 HRESULT WAVEParser_create(IUnknown * pUnkOuter, LPVOID * ppv)
426 {
427     HRESULT hr;
428     WAVEParserImpl * This;
429
430     TRACE("(%p, %p)\n", pUnkOuter, ppv);
431
432     *ppv = NULL;
433
434     if (pUnkOuter)
435         return CLASS_E_NOAGGREGATION;
436
437     /* Note: This memory is managed by the transform filter once created */
438     This = CoTaskMemAlloc(sizeof(WAVEParserImpl));
439
440     This->pCurrentSample = NULL;
441
442     hr = Parser_Create(&(This->Parser), &CLSID_WAVEParser, WAVEParser_Sample, WAVEParser_QueryAccept, WAVEParser_InputPin_PreConnect, WAVEParser_Cleanup, WAVEParser_disconnect, NULL, WAVEParserImpl_seek, NULL);
443
444     if (FAILED(hr))
445         return hr;
446
447     *ppv = (LPVOID)This;
448
449     return hr;
450 }