advapi32/tests: Improve RegDeleteTree tests.
[wine.git] / dlls / strmbase / outputqueue.c
blob842a9f0fa0755da5ef1ab0990762db10485e116f
1 /*
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
21 #define COBJMACROS
23 #include "dshow.h"
24 #include "wine/debug.h"
25 #include "wine/unicode.h"
26 #include "wine/list.h"
27 #include "wine/strmbase.h"
28 #include "uuids.h"
29 #include "vfwmsgs.h"
30 #include <assert.h>
32 WINE_DEFAULT_DEBUG_CHANNEL(strmbase);
34 enum {SAMPLE_PACKET, EOS_PACKET};
36 typedef struct tagQueuedEvent {
37 int type;
38 struct list entry;
40 IMediaSample *pSample;
41 } QueuedEvent;
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);
55 list_remove(cursor);
56 HeapFree(GetProcessHeap(),0,qev);
60 HRESULT WINAPI OutputQueue_Construct(
61 BaseOutputPin *pInputPin,
62 BOOL bAuto,
63 BOOL bQueue,
64 LONG lBatchSize,
65 BOOL bBatchExact,
66 DWORD dwPriority,
67 const OutputQueueFuncTable* pFuncsTable,
68 OutputQueue **ppOutputQueue )
71 BOOL threaded = FALSE;
72 DWORD tid;
74 OutputQueue *This;
76 if (!pInputPin || !pFuncsTable || !ppOutputQueue)
77 return E_INVALIDARG;
79 *ppOutputQueue = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(OutputQueue));
80 if (!*ppOutputQueue)
81 return E_OUTOFMEMORY;
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 This->SampleList = HeapAlloc(GetProcessHeap(),0,sizeof(struct list));
90 if (!This->SampleList)
92 OutputQueue_Destroy(This);
93 *ppOutputQueue = NULL;
94 return E_OUTOFMEMORY;
96 list_init(This->SampleList);
98 This->pInputPin = pInputPin;
99 IPin_AddRef(&pInputPin->pin.IPin_iface);
101 EnterCriticalSection(&This->csQueue);
102 if (bAuto && pInputPin->pMemInputPin)
103 threaded = IMemInputPin_ReceiveCanBlock(pInputPin->pMemInputPin) == S_OK;
104 else
105 threaded = bQueue;
107 if (threaded)
109 This->hThread = CreateThread(NULL, 0, OutputQueue_InitialThreadProc, This, 0, &tid);
110 if (This->hThread)
112 SetThreadPriority(This->hThread, dwPriority);
113 This->hProcessQueue = CreateEventW(NULL, 0, 0, NULL);
116 LeaveCriticalSection(&This->csQueue);
118 return S_OK;
121 HRESULT WINAPI OutputQueue_Destroy(OutputQueue *pOutputQueue)
123 EnterCriticalSection(&pOutputQueue->csQueue);
124 OutputQueue_FreeSamples(pOutputQueue);
125 pOutputQueue->bTerminate = TRUE;
126 SetEvent(pOutputQueue->hProcessQueue);
127 LeaveCriticalSection(&pOutputQueue->csQueue);
129 pOutputQueue->csQueue.DebugInfo->Spare[0] = 0;
130 DeleteCriticalSection(&pOutputQueue->csQueue);
131 CloseHandle(pOutputQueue->hProcessQueue);
133 HeapFree(GetProcessHeap(),0,pOutputQueue->SampleList);
135 IPin_Release(&pOutputQueue->pInputPin->pin.IPin_iface);
136 HeapFree(GetProcessHeap(),0,pOutputQueue);
137 return S_OK;
140 HRESULT WINAPI OutputQueue_ReceiveMultiple(OutputQueue *pOutputQueue, IMediaSample **ppSamples, LONG nSamples, LONG *nSamplesProcessed)
142 HRESULT hr = S_OK;
143 int i;
145 if (!pOutputQueue->pInputPin->pin.pConnectedTo || !pOutputQueue->pInputPin->pMemInputPin)
146 return VFW_E_NOT_CONNECTED;
148 if (!pOutputQueue->hThread)
150 IMemInputPin_AddRef(pOutputQueue->pInputPin->pMemInputPin);
151 hr = IMemInputPin_ReceiveMultiple(pOutputQueue->pInputPin->pMemInputPin,ppSamples, nSamples, nSamplesProcessed);
152 IMemInputPin_Release(pOutputQueue->pInputPin->pMemInputPin);
154 else
156 EnterCriticalSection(&pOutputQueue->csQueue);
157 *nSamplesProcessed = 0;
159 for (i = 0; i < nSamples; i++)
161 QueuedEvent *qev = HeapAlloc(GetProcessHeap(),0,sizeof(QueuedEvent));
162 if (!qev)
164 ERR("Out of Memory\n");
165 hr = E_OUTOFMEMORY;
166 break;
168 qev->type = SAMPLE_PACKET;
169 qev->pSample = ppSamples[i];
170 IMediaSample_AddRef(ppSamples[i]);
171 list_add_tail(pOutputQueue->SampleList, &qev->entry);
172 (*nSamplesProcessed)++;
175 if (!pOutputQueue->bBatchExact || list_count(pOutputQueue->SampleList) >= pOutputQueue->lBatchSize)
176 SetEvent(pOutputQueue->hProcessQueue);
177 LeaveCriticalSection(&pOutputQueue->csQueue);
179 return hr;
182 HRESULT WINAPI OutputQueue_Receive(OutputQueue *pOutputQueue, IMediaSample *pSample)
184 LONG processed;
185 return OutputQueue_ReceiveMultiple(pOutputQueue,&pSample,1,&processed);
188 VOID WINAPI OutputQueue_SendAnyway(OutputQueue *pOutputQueue)
190 if (pOutputQueue->hThread)
192 EnterCriticalSection(&pOutputQueue->csQueue);
193 if (!list_empty(pOutputQueue->SampleList))
195 pOutputQueue->bSendAnyway = TRUE;
196 SetEvent(pOutputQueue->hProcessQueue);
198 LeaveCriticalSection(&pOutputQueue->csQueue);
202 VOID WINAPI OutputQueue_EOS(OutputQueue *pOutputQueue)
204 EnterCriticalSection(&pOutputQueue->csQueue);
205 if (pOutputQueue->hThread)
207 QueuedEvent *qev = HeapAlloc(GetProcessHeap(),0,sizeof(QueuedEvent));
208 if (!qev)
210 ERR("Out of Memory\n");
211 LeaveCriticalSection(&pOutputQueue->csQueue);
212 return;
214 qev->type = EOS_PACKET;
215 qev->pSample = NULL;
216 list_add_tail(pOutputQueue->SampleList, &qev->entry);
218 else
220 IPin* ppin = NULL;
221 IPin_ConnectedTo(&pOutputQueue->pInputPin->pin.IPin_iface, &ppin);
222 if (ppin)
224 IPin_EndOfStream(ppin);
225 IPin_Release(ppin);
228 LeaveCriticalSection(&pOutputQueue->csQueue);
229 /* Covers sending the Event to the worker Thread */
230 OutputQueue_SendAnyway(pOutputQueue);
233 DWORD WINAPI OutputQueueImpl_ThreadProc(OutputQueue *pOutputQueue)
237 EnterCriticalSection(&pOutputQueue->csQueue);
238 if (!list_empty(pOutputQueue->SampleList) &&
239 (!pOutputQueue->bBatchExact ||
240 list_count(pOutputQueue->SampleList) >= pOutputQueue->lBatchSize ||
241 pOutputQueue->bSendAnyway
245 while (!list_empty(pOutputQueue->SampleList))
247 IMediaSample **ppSamples;
248 LONG nSamples;
249 LONG nSamplesProcessed;
250 struct list *cursor, *cursor2;
251 int i = 0;
253 /* First Pass Process Samples */
254 i = list_count(pOutputQueue->SampleList);
255 ppSamples = HeapAlloc(GetProcessHeap(),0,sizeof(IMediaSample*) * i);
256 nSamples = 0;
257 LIST_FOR_EACH_SAFE(cursor, cursor2, pOutputQueue->SampleList)
259 QueuedEvent *qev = LIST_ENTRY(cursor, QueuedEvent, entry);
260 if (qev->type == SAMPLE_PACKET)
261 ppSamples[nSamples++] = qev->pSample;
262 else
263 break;
264 list_remove(cursor);
265 HeapFree(GetProcessHeap(),0,qev);
268 if (pOutputQueue->pInputPin->pin.pConnectedTo && pOutputQueue->pInputPin->pMemInputPin)
270 IMemInputPin_AddRef(pOutputQueue->pInputPin->pMemInputPin);
271 LeaveCriticalSection(&pOutputQueue->csQueue);
272 IMemInputPin_ReceiveMultiple(pOutputQueue->pInputPin->pMemInputPin, ppSamples, nSamples, &nSamplesProcessed);
273 EnterCriticalSection(&pOutputQueue->csQueue);
274 IMemInputPin_Release(pOutputQueue->pInputPin->pMemInputPin);
276 for (i = 0; i < nSamples; i++)
277 IMediaSample_Release(ppSamples[i]);
278 HeapFree(GetProcessHeap(),0,ppSamples);
280 /* Process Non-Samples */
281 LIST_FOR_EACH_SAFE(cursor, cursor2, pOutputQueue->SampleList)
283 QueuedEvent *qev = LIST_ENTRY(cursor, QueuedEvent, entry);
284 if (qev->type == EOS_PACKET)
286 IPin* ppin = NULL;
287 IPin_ConnectedTo(&pOutputQueue->pInputPin->pin.IPin_iface, &ppin);
288 if (ppin)
290 IPin_EndOfStream(ppin);
291 IPin_Release(ppin);
294 else if (qev->type == SAMPLE_PACKET)
295 break;
296 else
297 FIXME("Unhandled Event type %i\n",qev->type);
298 list_remove(cursor);
299 HeapFree(GetProcessHeap(),0,qev);
302 pOutputQueue->bSendAnyway = FALSE;
304 LeaveCriticalSection(&pOutputQueue->csQueue);
305 WaitForSingleObject(pOutputQueue->hProcessQueue, INFINITE);
307 while (!pOutputQueue->bTerminate);
308 return S_OK;