kernel32: Close the stdio handles when creating a detached process.
[wine] / dlls / quartz / transform.c
1 /*
2  * Transform Filter (Base for decoders, etc...)
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 "config.h"
22
23 #include "quartz_private.h"
24 #include "control_private.h"
25 #include "pin.h"
26
27 #include "amvideo.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "dshow.h"
31 #include "strmif.h"
32 #include "vfw.h"
33
34 #include <assert.h>
35
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
38
39 #include "transform.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
42
43 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
44 static const WCHAR wcsOutputPinName[] = {'o','u','t','p','u','t',' ','p','i','n',0};
45
46 static const IBaseFilterVtbl TransformFilter_Vtbl;
47 static const IPinVtbl TransformFilter_InputPin_Vtbl;
48 static const IMemInputPinVtbl MemInputPin_Vtbl; 
49 static const IPinVtbl TransformFilter_OutputPin_Vtbl;
50
51 static HRESULT TransformFilter_Sample(LPVOID iface, IMediaSample * pSample)
52 {
53     TransformFilterImpl *This = (TransformFilterImpl *)iface;
54
55     TRACE("%p %p\n", iface, pSample);
56
57     return This->pFuncsTable->pfnProcessSampleData(This, pSample);
58 }
59
60 static HRESULT TransformFilter_Input_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
61 {
62     TransformFilterImpl* This = (TransformFilterImpl*)iface;
63     TRACE("%p\n", iface);
64     dump_AM_MEDIA_TYPE(pmt);
65
66     if (This->pFuncsTable->pfnQueryConnect)
67         return This->pFuncsTable->pfnQueryConnect(This, pmt);
68     /* Assume OK if there's no query method (the connection will fail if
69        needed) */
70     return S_OK;
71 }
72
73
74 static HRESULT TransformFilter_Output_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
75 {
76     TransformFilterImpl* pTransformFilter = (TransformFilterImpl*)iface;
77     AM_MEDIA_TYPE* outpmt = &((OutputPin*)pTransformFilter->ppPins[1])->pin.mtCurrent;
78     TRACE("%p\n", iface);
79
80     if (IsEqualIID(&pmt->majortype, &outpmt->majortype) && IsEqualIID(&pmt->subtype, &outpmt->subtype))
81         return S_OK;
82     return S_FALSE;
83 }
84
85 static HRESULT TransformFilter_InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
86 {
87     InputPin * pPinImpl;
88
89     *ppPin = NULL;
90
91     if (pPinInfo->dir != PINDIR_INPUT)
92     {
93         ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
94         return E_INVALIDARG;
95     }
96
97     pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
98
99     if (!pPinImpl)
100         return E_OUTOFMEMORY;
101
102     if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
103     {
104         pPinImpl->pin.lpVtbl = &TransformFilter_InputPin_Vtbl;
105         pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
106
107         *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
108         return S_OK;
109     }
110
111     CoTaskMemFree(pPinImpl);
112     return E_FAIL;
113 }
114
115 static HRESULT TransformFilter_OutputPin_Construct(const PIN_INFO * pPinInfo, const ALLOCATOR_PROPERTIES *props,
116                                                    LPVOID pUserData, QUERYACCEPTPROC pQueryAccept,
117                                                    LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
118 {
119     OutputPin * pPinImpl;
120
121     *ppPin = NULL;
122
123     if (pPinInfo->dir != PINDIR_OUTPUT)
124     {
125         ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
126         return E_INVALIDARG;
127     }
128
129     pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
130
131     if (!pPinImpl)
132         return E_OUTOFMEMORY;
133
134     if (SUCCEEDED(OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
135     {
136         pPinImpl->pin.lpVtbl = &TransformFilter_OutputPin_Vtbl;
137
138         *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
139         return S_OK;
140     }
141
142     CoTaskMemFree(pPinImpl);
143     return E_FAIL;
144 }
145
146 HRESULT TransformFilter_Create(TransformFilterImpl* pTransformFilter, const CLSID* pClsid, const TransformFuncsTable* pFuncsTable)
147 {
148     HRESULT hr;
149     PIN_INFO piInput;
150     PIN_INFO piOutput;
151
152     /* pTransformFilter is already allocated */
153     pTransformFilter->clsid = *pClsid;
154     pTransformFilter->pFuncsTable = pFuncsTable;
155
156     pTransformFilter->lpVtbl = &TransformFilter_Vtbl;
157
158     pTransformFilter->refCount = 1;
159     InitializeCriticalSection(&pTransformFilter->csFilter);
160     pTransformFilter->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TransformFilterImpl.csFilter");
161     pTransformFilter->state = State_Stopped;
162     pTransformFilter->pClock = NULL;
163     ZeroMemory(&pTransformFilter->filterInfo, sizeof(FILTER_INFO));
164
165     pTransformFilter->ppPins = CoTaskMemAlloc(2 * sizeof(IPin *));
166
167     /* construct input pin */
168     piInput.dir = PINDIR_INPUT;
169     piInput.pFilter = (IBaseFilter *)pTransformFilter;
170     lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
171     piOutput.dir = PINDIR_OUTPUT;
172     piOutput.pFilter = (IBaseFilter *)pTransformFilter;
173     lstrcpynW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
174
175     hr = TransformFilter_InputPin_Construct(&piInput, TransformFilter_Sample, pTransformFilter, TransformFilter_Input_QueryAccept, &pTransformFilter->csFilter, &pTransformFilter->ppPins[0]);
176
177     if (SUCCEEDED(hr))
178     {
179         ALLOCATOR_PROPERTIES props;
180         props.cbAlign = 1;
181         props.cbPrefix = 0;
182         props.cbBuffer = 0; /* Will be updated at connection time */
183         props.cBuffers = 2;
184
185         hr = TransformFilter_OutputPin_Construct(&piOutput, &props, pTransformFilter, TransformFilter_Output_QueryAccept, &pTransformFilter->csFilter, &pTransformFilter->ppPins[1]);
186
187         if (FAILED(hr))
188             ERR("Cannot create output pin (%x)\n", hr);
189     }
190     else
191     {
192         CoTaskMemFree(pTransformFilter->ppPins);
193         pTransformFilter->csFilter.DebugInfo->Spare[0] = 0;
194         DeleteCriticalSection(&pTransformFilter->csFilter);
195         CoTaskMemFree(pTransformFilter);
196     }
197
198     return hr;
199 }
200
201 static HRESULT WINAPI TransformFilter_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
202 {
203     TransformFilterImpl *This = (TransformFilterImpl *)iface;
204     TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
205
206     *ppv = NULL;
207
208     if (IsEqualIID(riid, &IID_IUnknown))
209         *ppv = (LPVOID)This;
210     else if (IsEqualIID(riid, &IID_IPersist))
211         *ppv = (LPVOID)This;
212     else if (IsEqualIID(riid, &IID_IMediaFilter))
213         *ppv = (LPVOID)This;
214     else if (IsEqualIID(riid, &IID_IBaseFilter))
215         *ppv = (LPVOID)This;
216
217     if (*ppv)
218     {
219         IUnknown_AddRef((IUnknown *)(*ppv));
220         return S_OK;
221     }
222
223     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
224
225     return E_NOINTERFACE;
226 }
227
228 static ULONG WINAPI TransformFilter_AddRef(IBaseFilter * iface)
229 {
230     TransformFilterImpl *This = (TransformFilterImpl *)iface;
231     ULONG refCount = InterlockedIncrement(&This->refCount);
232
233     TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
234
235     return refCount;
236 }
237
238 static ULONG WINAPI TransformFilter_Release(IBaseFilter * iface)
239 {
240     TransformFilterImpl *This = (TransformFilterImpl *)iface;
241     ULONG refCount = InterlockedDecrement(&This->refCount);
242
243     TRACE("(%p/%p)->() Release from %d\n", This, iface, refCount + 1);
244
245     if (!refCount)
246     {
247         ULONG i;
248
249         if (This->pClock)
250             IReferenceClock_Release(This->pClock);
251
252         for (i = 0; i < 2; i++)
253         {
254             IPin *pConnectedTo;
255
256             if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[i], &pConnectedTo)))
257             {
258                 IPin_Disconnect(pConnectedTo);
259                 IPin_Release(pConnectedTo);
260             }
261             IPin_Disconnect(This->ppPins[i]);
262
263             IPin_Release(This->ppPins[i]);
264         }
265
266         CoTaskMemFree(This->ppPins);
267         This->lpVtbl = NULL;
268
269         This->csFilter.DebugInfo->Spare[0] = 0;
270         DeleteCriticalSection(&This->csFilter);
271
272         TRACE("Destroying transform filter\n");
273         CoTaskMemFree(This);
274
275         return 0;
276     }
277     else
278         return refCount;
279 }
280
281 /** IPersist methods **/
282
283 static HRESULT WINAPI TransformFilter_GetClassID(IBaseFilter * iface, CLSID * pClsid)
284 {
285     TransformFilterImpl *This = (TransformFilterImpl *)iface;
286
287     TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
288
289     *pClsid = This->clsid;
290
291     return S_OK;
292 }
293
294 /** IMediaFilter methods **/
295
296 static HRESULT WINAPI TransformFilter_Stop(IBaseFilter * iface)
297 {
298     TransformFilterImpl *This = (TransformFilterImpl *)iface;
299
300     TRACE("(%p/%p)\n", This, iface);
301
302     EnterCriticalSection(&This->csFilter);
303     {
304         This->state = State_Stopped;
305         if (This->pFuncsTable->pfnProcessEnd)
306             This->pFuncsTable->pfnProcessEnd(This);
307     }
308     LeaveCriticalSection(&This->csFilter);
309
310     return S_OK;
311 }
312
313 static HRESULT WINAPI TransformFilter_Pause(IBaseFilter * iface)
314 {
315     TransformFilterImpl *This = (TransformFilterImpl *)iface;
316
317     TRACE("(%p/%p)->()\n", This, iface);
318
319     EnterCriticalSection(&This->csFilter);
320     {
321         This->state = State_Paused;
322     }
323     LeaveCriticalSection(&This->csFilter);
324
325     return S_OK;
326 }
327
328 static HRESULT WINAPI TransformFilter_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
329 {
330     HRESULT hr = S_OK;
331     TransformFilterImpl *This = (TransformFilterImpl *)iface;
332
333     TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
334
335     EnterCriticalSection(&This->csFilter);
336     {
337         This->rtStreamStart = tStart;
338         This->state = State_Running;
339         OutputPin_CommitAllocator((OutputPin *)This->ppPins[1]);
340         if (This->pFuncsTable->pfnProcessBegin)
341             This->pFuncsTable->pfnProcessBegin(This);
342     }
343     LeaveCriticalSection(&This->csFilter);
344
345     return hr;
346 }
347
348 static HRESULT WINAPI TransformFilter_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
349 {
350     TransformFilterImpl *This = (TransformFilterImpl *)iface;
351
352     TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
353
354     EnterCriticalSection(&This->csFilter);
355     {
356         *pState = This->state;
357     }
358     LeaveCriticalSection(&This->csFilter);
359
360     return S_OK;
361 }
362
363 static HRESULT WINAPI TransformFilter_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
364 {
365     TransformFilterImpl *This = (TransformFilterImpl *)iface;
366
367     TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
368
369     EnterCriticalSection(&This->csFilter);
370     {
371         if (This->pClock)
372             IReferenceClock_Release(This->pClock);
373         This->pClock = pClock;
374         if (This->pClock)
375             IReferenceClock_AddRef(This->pClock);
376     }
377     LeaveCriticalSection(&This->csFilter);
378
379     return S_OK;
380 }
381
382 static HRESULT WINAPI TransformFilter_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
383 {
384     TransformFilterImpl *This = (TransformFilterImpl *)iface;
385
386     TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
387
388     EnterCriticalSection(&This->csFilter);
389     {
390         *ppClock = This->pClock;
391         IReferenceClock_AddRef(This->pClock);
392     }
393     LeaveCriticalSection(&This->csFilter);
394
395     return S_OK;
396 }
397
398 /** IBaseFilter implementation **/
399
400 static HRESULT WINAPI TransformFilter_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
401 {
402     ENUMPINDETAILS epd;
403     TransformFilterImpl *This = (TransformFilterImpl *)iface;
404
405     TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
406
407     epd.cPins = 2; /* input and output pins */
408     epd.ppPins = This->ppPins;
409     return IEnumPinsImpl_Construct(&epd, ppEnum);
410 }
411
412 static HRESULT WINAPI TransformFilter_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
413 {
414     TransformFilterImpl *This = (TransformFilterImpl *)iface;
415
416     TRACE("(%p/%p)->(%p,%p)\n", This, iface, debugstr_w(Id), ppPin);
417
418     return E_NOTIMPL;
419 }
420
421 static HRESULT WINAPI TransformFilter_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
422 {
423     TransformFilterImpl *This = (TransformFilterImpl *)iface;
424
425     TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
426
427     strcpyW(pInfo->achName, This->filterInfo.achName);
428     pInfo->pGraph = This->filterInfo.pGraph;
429
430     if (pInfo->pGraph)
431         IFilterGraph_AddRef(pInfo->pGraph);
432
433     return S_OK;
434 }
435
436 static HRESULT WINAPI TransformFilter_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
437 {
438     HRESULT hr = S_OK;
439     TransformFilterImpl *This = (TransformFilterImpl *)iface;
440
441     TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
442
443     EnterCriticalSection(&This->csFilter);
444     {
445         if (pName)
446             strcpyW(This->filterInfo.achName, pName);
447         else
448             *This->filterInfo.achName = '\0';
449         This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
450     }
451     LeaveCriticalSection(&This->csFilter);
452
453     return hr;
454 }
455
456 static HRESULT WINAPI TransformFilter_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
457 {
458     TransformFilterImpl *This = (TransformFilterImpl *)iface;
459     TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
460     return E_NOTIMPL;
461 }
462
463 static const IBaseFilterVtbl TransformFilter_Vtbl =
464 {
465     TransformFilter_QueryInterface,
466     TransformFilter_AddRef,
467     TransformFilter_Release,
468     TransformFilter_GetClassID,
469     TransformFilter_Stop,
470     TransformFilter_Pause,
471     TransformFilter_Run,
472     TransformFilter_GetState,
473     TransformFilter_SetSyncSource,
474     TransformFilter_GetSyncSource,
475     TransformFilter_EnumPins,
476     TransformFilter_FindPin,
477     TransformFilter_QueryFilterInfo,
478     TransformFilter_JoinFilterGraph,
479     TransformFilter_QueryVendorInfo
480 };
481
482 static HRESULT WINAPI TransformFilter_InputPin_EndOfStream(IPin * iface)
483 {
484     InputPin* This = (InputPin*) iface;
485     TransformFilterImpl* pTransform;
486     IPin* ppin;
487     HRESULT hr;
488     
489     TRACE("(%p)->()\n", iface);
490
491     /* Since we process samples synchronously, just forward notification downstream */
492     pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
493     if (!pTransform)
494         hr = E_FAIL;
495     else
496         hr = IPin_ConnectedTo(pTransform->ppPins[1], &ppin);
497     if (SUCCEEDED(hr))
498     {
499         hr = IPin_EndOfStream(ppin);
500         IPin_Release(ppin);
501     }
502
503     if (FAILED(hr))
504         ERR("%x\n", hr);
505     return hr;
506 }
507
508 static HRESULT WINAPI TransformFilter_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
509 {
510     InputPin* This = (InputPin*) iface;
511     TransformFilterImpl* pTransform;
512     HRESULT hr;
513
514     TRACE("(%p)->(%p, %p)\n", iface, pReceivePin, pmt);
515
516     pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
517
518     hr = pTransform->pFuncsTable->pfnConnectInput(pTransform, pmt);
519     if (SUCCEEDED(hr))
520     {
521         hr = InputPin_ReceiveConnection(iface, pReceivePin, pmt);
522         if (FAILED(hr))
523             pTransform->pFuncsTable->pfnCleanup(pTransform);
524     }
525
526     return hr;
527 }
528
529 static HRESULT WINAPI TransformFilter_InputPin_Disconnect(IPin * iface)
530 {
531     InputPin* This = (InputPin*) iface;
532     TransformFilterImpl* pTransform;
533
534     TRACE("(%p)->()\n", iface);
535
536     pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
537     pTransform->pFuncsTable->pfnCleanup(pTransform);
538
539     return IPinImpl_Disconnect(iface);
540 }
541
542 static const IPinVtbl TransformFilter_InputPin_Vtbl = 
543 {
544     InputPin_QueryInterface,
545     IPinImpl_AddRef,
546     InputPin_Release,
547     InputPin_Connect,
548     TransformFilter_InputPin_ReceiveConnection,
549     TransformFilter_InputPin_Disconnect,
550     IPinImpl_ConnectedTo,
551     IPinImpl_ConnectionMediaType,
552     IPinImpl_QueryPinInfo,
553     IPinImpl_QueryDirection,
554     IPinImpl_QueryId,
555     IPinImpl_QueryAccept,
556     IPinImpl_EnumMediaTypes,
557     IPinImpl_QueryInternalConnections,
558     TransformFilter_InputPin_EndOfStream,
559     InputPin_BeginFlush,
560     InputPin_EndFlush,
561     InputPin_NewSegment
562 };
563
564 static HRESULT WINAPI TransformFilter_Output_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
565 {
566     IPinImpl *This = (IPinImpl *)iface;
567     ENUMMEDIADETAILS emd;
568
569     TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
570
571     emd.cMediaTypes = 1;
572     emd.pMediaTypes = &This->mtCurrent;
573
574     return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
575 }
576
577 static const IPinVtbl TransformFilter_OutputPin_Vtbl =
578 {
579     OutputPin_QueryInterface,
580     IPinImpl_AddRef,
581     OutputPin_Release,
582     OutputPin_Connect,
583     OutputPin_ReceiveConnection,
584     OutputPin_Disconnect,
585     IPinImpl_ConnectedTo,
586     IPinImpl_ConnectionMediaType,
587     IPinImpl_QueryPinInfo,
588     IPinImpl_QueryDirection,
589     IPinImpl_QueryId,
590     IPinImpl_QueryAccept,
591     TransformFilter_Output_EnumMediaTypes,
592     IPinImpl_QueryInternalConnections,
593     OutputPin_EndOfStream,
594     OutputPin_BeginFlush,
595     OutputPin_EndFlush,
596     OutputPin_NewSegment
597 };
598
599 static const IMemInputPinVtbl MemInputPin_Vtbl = 
600 {
601     MemInputPin_QueryInterface,
602     MemInputPin_AddRef,
603     MemInputPin_Release,
604     MemInputPin_GetAllocator,
605     MemInputPin_NotifyAllocator,
606     MemInputPin_GetAllocatorRequirements,
607     MemInputPin_Receive,
608     MemInputPin_ReceiveMultiple,
609     MemInputPin_ReceiveCanBlock
610 };