2 * Generic Implementation of COutputQueue
4 * Copyright 2011 Aric Stewart, CodeWeavers
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.
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.
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
24 #include "wine/debug.h"
25 #include "wine/unicode.h"
26 #include "wine/list.h"
27 #include "wine/strmbase.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(strmbase
);
34 enum {SAMPLE_PACKET
, EOS_PACKET
};
36 typedef struct tagQueuedEvent
{
40 IMediaSample
*pSample
;
43 static DWORD WINAPI
OutputQueue_InitialThreadProc(LPVOID data
)
45 OutputQueue
*This
= (OutputQueue
*)data
;
46 return This
->pFuncsTable
->pfnThreadProc(This
);
49 static void OutputQueue_FreeSamples(OutputQueue
*pOutputQueue
)
51 struct list
*cursor
, *cursor2
;
52 LIST_FOR_EACH_SAFE(cursor
, cursor2
, &pOutputQueue
->SampleList
)
54 QueuedEvent
*qev
= LIST_ENTRY(cursor
, QueuedEvent
, entry
);
56 HeapFree(GetProcessHeap(),0,qev
);
60 HRESULT WINAPI
OutputQueue_Construct(
61 BaseOutputPin
*pInputPin
,
67 const OutputQueueFuncTable
* pFuncsTable
,
68 OutputQueue
**ppOutputQueue
)
71 BOOL threaded
= FALSE
;
76 if (!pInputPin
|| !pFuncsTable
|| !ppOutputQueue
)
79 *ppOutputQueue
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(OutputQueue
));
83 This
= *ppOutputQueue
;
84 This
->pFuncsTable
= pFuncsTable
;
85 This
->lBatchSize
= lBatchSize
;
86 This
->bBatchExact
= bBatchExact
;
87 InitializeCriticalSection(&This
->csQueue
);
88 This
->csQueue
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": OutputQueue.csQueue");
89 list_init(&This
->SampleList
);
91 This
->pInputPin
= pInputPin
;
92 IPin_AddRef(&pInputPin
->pin
.IPin_iface
);
94 EnterCriticalSection(&This
->csQueue
);
95 if (bAuto
&& pInputPin
->pMemInputPin
)
96 threaded
= IMemInputPin_ReceiveCanBlock(pInputPin
->pMemInputPin
) == S_OK
;
102 This
->hThread
= CreateThread(NULL
, 0, OutputQueue_InitialThreadProc
, This
, 0, &tid
);
105 SetThreadPriority(This
->hThread
, dwPriority
);
106 This
->hProcessQueue
= CreateEventW(NULL
, 0, 0, NULL
);
109 LeaveCriticalSection(&This
->csQueue
);
114 HRESULT WINAPI
OutputQueue_Destroy(OutputQueue
*pOutputQueue
)
116 EnterCriticalSection(&pOutputQueue
->csQueue
);
117 OutputQueue_FreeSamples(pOutputQueue
);
118 pOutputQueue
->bTerminate
= TRUE
;
119 SetEvent(pOutputQueue
->hProcessQueue
);
120 LeaveCriticalSection(&pOutputQueue
->csQueue
);
122 pOutputQueue
->csQueue
.DebugInfo
->Spare
[0] = 0;
123 DeleteCriticalSection(&pOutputQueue
->csQueue
);
124 CloseHandle(pOutputQueue
->hProcessQueue
);
126 IPin_Release(&pOutputQueue
->pInputPin
->pin
.IPin_iface
);
127 HeapFree(GetProcessHeap(),0,pOutputQueue
);
131 HRESULT WINAPI
OutputQueue_ReceiveMultiple(OutputQueue
*pOutputQueue
, IMediaSample
**ppSamples
, LONG nSamples
, LONG
*nSamplesProcessed
)
136 if (!pOutputQueue
->pInputPin
->pin
.pConnectedTo
|| !pOutputQueue
->pInputPin
->pMemInputPin
)
137 return VFW_E_NOT_CONNECTED
;
139 if (!pOutputQueue
->hThread
)
141 IMemInputPin_AddRef(pOutputQueue
->pInputPin
->pMemInputPin
);
142 hr
= IMemInputPin_ReceiveMultiple(pOutputQueue
->pInputPin
->pMemInputPin
,ppSamples
, nSamples
, nSamplesProcessed
);
143 IMemInputPin_Release(pOutputQueue
->pInputPin
->pMemInputPin
);
147 EnterCriticalSection(&pOutputQueue
->csQueue
);
148 *nSamplesProcessed
= 0;
150 for (i
= 0; i
< nSamples
; i
++)
152 QueuedEvent
*qev
= HeapAlloc(GetProcessHeap(),0,sizeof(QueuedEvent
));
155 ERR("Out of Memory\n");
159 qev
->type
= SAMPLE_PACKET
;
160 qev
->pSample
= ppSamples
[i
];
161 IMediaSample_AddRef(ppSamples
[i
]);
162 list_add_tail(&pOutputQueue
->SampleList
, &qev
->entry
);
163 (*nSamplesProcessed
)++;
166 if (!pOutputQueue
->bBatchExact
|| list_count(&pOutputQueue
->SampleList
) >= pOutputQueue
->lBatchSize
)
167 SetEvent(pOutputQueue
->hProcessQueue
);
168 LeaveCriticalSection(&pOutputQueue
->csQueue
);
173 HRESULT WINAPI
OutputQueue_Receive(OutputQueue
*pOutputQueue
, IMediaSample
*pSample
)
176 return OutputQueue_ReceiveMultiple(pOutputQueue
,&pSample
,1,&processed
);
179 VOID WINAPI
OutputQueue_SendAnyway(OutputQueue
*pOutputQueue
)
181 if (pOutputQueue
->hThread
)
183 EnterCriticalSection(&pOutputQueue
->csQueue
);
184 if (!list_empty(&pOutputQueue
->SampleList
))
186 pOutputQueue
->bSendAnyway
= TRUE
;
187 SetEvent(pOutputQueue
->hProcessQueue
);
189 LeaveCriticalSection(&pOutputQueue
->csQueue
);
193 VOID WINAPI
OutputQueue_EOS(OutputQueue
*pOutputQueue
)
195 EnterCriticalSection(&pOutputQueue
->csQueue
);
196 if (pOutputQueue
->hThread
)
198 QueuedEvent
*qev
= HeapAlloc(GetProcessHeap(),0,sizeof(QueuedEvent
));
201 ERR("Out of Memory\n");
202 LeaveCriticalSection(&pOutputQueue
->csQueue
);
205 qev
->type
= EOS_PACKET
;
207 list_add_tail(&pOutputQueue
->SampleList
, &qev
->entry
);
212 IPin_ConnectedTo(&pOutputQueue
->pInputPin
->pin
.IPin_iface
, &ppin
);
215 IPin_EndOfStream(ppin
);
219 LeaveCriticalSection(&pOutputQueue
->csQueue
);
220 /* Covers sending the Event to the worker Thread */
221 OutputQueue_SendAnyway(pOutputQueue
);
224 DWORD WINAPI
OutputQueueImpl_ThreadProc(OutputQueue
*pOutputQueue
)
228 EnterCriticalSection(&pOutputQueue
->csQueue
);
229 if (!list_empty(&pOutputQueue
->SampleList
) &&
230 (!pOutputQueue
->bBatchExact
||
231 list_count(&pOutputQueue
->SampleList
) >= pOutputQueue
->lBatchSize
||
232 pOutputQueue
->bSendAnyway
236 while (!list_empty(&pOutputQueue
->SampleList
))
238 IMediaSample
**ppSamples
;
240 LONG nSamplesProcessed
;
241 struct list
*cursor
, *cursor2
;
244 /* First Pass Process Samples */
245 i
= list_count(&pOutputQueue
->SampleList
);
246 ppSamples
= HeapAlloc(GetProcessHeap(),0,sizeof(IMediaSample
*) * i
);
248 LIST_FOR_EACH_SAFE(cursor
, cursor2
, &pOutputQueue
->SampleList
)
250 QueuedEvent
*qev
= LIST_ENTRY(cursor
, QueuedEvent
, entry
);
251 if (qev
->type
== SAMPLE_PACKET
)
252 ppSamples
[nSamples
++] = qev
->pSample
;
256 HeapFree(GetProcessHeap(),0,qev
);
259 if (pOutputQueue
->pInputPin
->pin
.pConnectedTo
&& pOutputQueue
->pInputPin
->pMemInputPin
)
261 IMemInputPin_AddRef(pOutputQueue
->pInputPin
->pMemInputPin
);
262 LeaveCriticalSection(&pOutputQueue
->csQueue
);
263 IMemInputPin_ReceiveMultiple(pOutputQueue
->pInputPin
->pMemInputPin
, ppSamples
, nSamples
, &nSamplesProcessed
);
264 EnterCriticalSection(&pOutputQueue
->csQueue
);
265 IMemInputPin_Release(pOutputQueue
->pInputPin
->pMemInputPin
);
267 for (i
= 0; i
< nSamples
; i
++)
268 IMediaSample_Release(ppSamples
[i
]);
269 HeapFree(GetProcessHeap(),0,ppSamples
);
271 /* Process Non-Samples */
272 LIST_FOR_EACH_SAFE(cursor
, cursor2
, &pOutputQueue
->SampleList
)
274 QueuedEvent
*qev
= LIST_ENTRY(cursor
, QueuedEvent
, entry
);
275 if (qev
->type
== EOS_PACKET
)
278 IPin_ConnectedTo(&pOutputQueue
->pInputPin
->pin
.IPin_iface
, &ppin
);
281 IPin_EndOfStream(ppin
);
285 else if (qev
->type
== SAMPLE_PACKET
)
288 FIXME("Unhandled Event type %i\n",qev
->type
);
290 HeapFree(GetProcessHeap(),0,qev
);
293 pOutputQueue
->bSendAnyway
= FALSE
;
295 LeaveCriticalSection(&pOutputQueue
->csQueue
);
296 WaitForSingleObject(pOutputQueue
->hProcessQueue
, INFINITE
);
298 while (!pOutputQueue
->bTerminate
);