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 #include "strmbase_private.h"
23 enum {SAMPLE_PACKET
, EOS_PACKET
};
25 typedef struct tagQueuedEvent
{
29 IMediaSample
*pSample
;
32 static DWORD WINAPI
OutputQueue_InitialThreadProc(LPVOID data
)
34 OutputQueue
*This
= (OutputQueue
*)data
;
35 return This
->pFuncsTable
->pfnThreadProc(This
);
38 static void OutputQueue_FreeSamples(OutputQueue
*pOutputQueue
)
40 struct list
*cursor
, *cursor2
;
41 LIST_FOR_EACH_SAFE(cursor
, cursor2
, pOutputQueue
->SampleList
)
43 QueuedEvent
*qev
= LIST_ENTRY(cursor
, QueuedEvent
, entry
);
45 HeapFree(GetProcessHeap(),0,qev
);
49 HRESULT WINAPI
OutputQueue_Construct(
50 BaseOutputPin
*pInputPin
,
56 const OutputQueueFuncTable
* pFuncsTable
,
57 OutputQueue
**ppOutputQueue
)
61 BOOL threaded
= FALSE
;
66 if (!pInputPin
|| !pFuncsTable
|| !ppOutputQueue
)
69 *ppOutputQueue
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(OutputQueue
));
73 This
= *ppOutputQueue
;
74 This
->pFuncsTable
= pFuncsTable
;
75 This
->lBatchSize
= lBatchSize
;
76 This
->bBatchExact
= bBatchExact
;
77 InitializeCriticalSection(&This
->csQueue
);
78 This
->csQueue
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": OutputQueue.csQueue");
79 This
->SampleList
= HeapAlloc(GetProcessHeap(),0,sizeof(struct list
));
80 if (!This
->SampleList
)
82 OutputQueue_Destroy(This
);
83 *ppOutputQueue
= NULL
;
86 list_init(This
->SampleList
);
88 This
->pInputPin
= pInputPin
;
89 IPin_AddRef(&pInputPin
->pin
.IPin_iface
);
91 EnterCriticalSection(&This
->csQueue
);
92 if (bAuto
&& pInputPin
->pMemInputPin
)
93 threaded
= IMemInputPin_ReceiveCanBlock(pInputPin
->pMemInputPin
);
99 This
->hThread
= CreateThread(NULL
, 0, OutputQueue_InitialThreadProc
, This
, 0, &tid
);
102 SetThreadPriority(This
->hThread
, dwPriority
);
103 This
->hProcessQueue
= CreateEventW(NULL
, 0, 0, NULL
);
106 LeaveCriticalSection(&This
->csQueue
);
111 HRESULT WINAPI
OutputQueue_Destroy(OutputQueue
*pOutputQueue
)
113 EnterCriticalSection(&pOutputQueue
->csQueue
);
114 OutputQueue_FreeSamples(pOutputQueue
);
115 pOutputQueue
->bTerminate
= TRUE
;
116 SetEvent(pOutputQueue
->hProcessQueue
);
117 LeaveCriticalSection(&pOutputQueue
->csQueue
);
119 pOutputQueue
->csQueue
.DebugInfo
->Spare
[0] = 0;
120 DeleteCriticalSection(&pOutputQueue
->csQueue
);
121 CloseHandle(pOutputQueue
->hProcessQueue
);
123 HeapFree(GetProcessHeap(),0,pOutputQueue
->SampleList
);
125 IPin_Release(&pOutputQueue
->pInputPin
->pin
.IPin_iface
);
126 HeapFree(GetProcessHeap(),0,pOutputQueue
);
130 HRESULT WINAPI
OutputQueue_ReceiveMultiple(OutputQueue
*pOutputQueue
, IMediaSample
**ppSamples
, LONG nSamples
, LONG
*nSamplesProcessed
)
135 if (!pOutputQueue
->pInputPin
->pin
.pConnectedTo
|| !pOutputQueue
->pInputPin
->pMemInputPin
)
136 return VFW_E_NOT_CONNECTED
;
138 if (!pOutputQueue
->hThread
)
140 IMemInputPin_AddRef(pOutputQueue
->pInputPin
->pMemInputPin
);
141 hr
= IMemInputPin_ReceiveMultiple(pOutputQueue
->pInputPin
->pMemInputPin
,ppSamples
, nSamples
, nSamplesProcessed
);
142 IMemInputPin_Release(pOutputQueue
->pInputPin
->pMemInputPin
);
146 EnterCriticalSection(&pOutputQueue
->csQueue
);
147 *nSamplesProcessed
= 0;
149 for (i
= 0; i
< nSamples
; i
++)
151 QueuedEvent
*qev
= HeapAlloc(GetProcessHeap(),0,sizeof(QueuedEvent
));
154 ERR("Out of Memory\n");
158 qev
->type
= SAMPLE_PACKET
;
159 qev
->pSample
= ppSamples
[i
];
160 IMediaSample_AddRef(ppSamples
[i
]);
161 list_add_tail(pOutputQueue
->SampleList
, &qev
->entry
);
162 (*nSamplesProcessed
)++;
165 if (!pOutputQueue
->bBatchExact
|| list_count(pOutputQueue
->SampleList
) >= pOutputQueue
->lBatchSize
)
166 SetEvent(pOutputQueue
->hProcessQueue
);
167 LeaveCriticalSection(&pOutputQueue
->csQueue
);
172 HRESULT WINAPI
OutputQueue_Receive(OutputQueue
*pOutputQueue
, IMediaSample
*pSample
)
175 return OutputQueue_ReceiveMultiple(pOutputQueue
,&pSample
,1,&processed
);
178 VOID WINAPI
OutputQueue_SendAnyway(OutputQueue
*pOutputQueue
)
180 if (pOutputQueue
->hThread
)
182 EnterCriticalSection(&pOutputQueue
->csQueue
);
183 if (!list_empty(pOutputQueue
->SampleList
))
185 pOutputQueue
->bSendAnyway
= TRUE
;
186 SetEvent(pOutputQueue
->hProcessQueue
);
188 LeaveCriticalSection(&pOutputQueue
->csQueue
);
192 VOID WINAPI
OutputQueue_EOS(OutputQueue
*pOutputQueue
)
194 EnterCriticalSection(&pOutputQueue
->csQueue
);
195 if (pOutputQueue
->hThread
)
197 QueuedEvent
*qev
= HeapAlloc(GetProcessHeap(),0,sizeof(QueuedEvent
));
200 ERR("Out of Memory\n");
201 LeaveCriticalSection(&pOutputQueue
->csQueue
);
204 qev
->type
= EOS_PACKET
;
206 list_add_tail(pOutputQueue
->SampleList
, &qev
->entry
);
211 IPin_ConnectedTo(&pOutputQueue
->pInputPin
->pin
.IPin_iface
, &ppin
);
214 IPin_EndOfStream(ppin
);
218 LeaveCriticalSection(&pOutputQueue
->csQueue
);
219 /* Covers sending the Event to the worker Thread */
220 OutputQueue_SendAnyway(pOutputQueue
);
223 DWORD WINAPI
OutputQueueImpl_ThreadProc(OutputQueue
*pOutputQueue
)
227 EnterCriticalSection(&pOutputQueue
->csQueue
);
228 if (!list_empty(pOutputQueue
->SampleList
) &&
229 (!pOutputQueue
->bBatchExact
||
230 list_count(pOutputQueue
->SampleList
) >= pOutputQueue
->lBatchSize
||
231 pOutputQueue
->bSendAnyway
235 while (!list_empty(pOutputQueue
->SampleList
))
237 IMediaSample
**ppSamples
;
239 LONG nSamplesProcessed
;
240 struct list
*cursor
, *cursor2
;
243 /* First Pass Process Samples */
244 i
= list_count(pOutputQueue
->SampleList
);
245 ppSamples
= HeapAlloc(GetProcessHeap(),0,sizeof(IMediaSample
*) * i
);
247 LIST_FOR_EACH_SAFE(cursor
, cursor2
, pOutputQueue
->SampleList
)
249 QueuedEvent
*qev
= LIST_ENTRY(cursor
, QueuedEvent
, entry
);
250 if (qev
->type
== SAMPLE_PACKET
)
251 ppSamples
[nSamples
++] = qev
->pSample
;
255 HeapFree(GetProcessHeap(),0,qev
);
258 if (pOutputQueue
->pInputPin
->pin
.pConnectedTo
&& pOutputQueue
->pInputPin
->pMemInputPin
)
260 IMemInputPin_AddRef(pOutputQueue
->pInputPin
->pMemInputPin
);
261 LeaveCriticalSection(&pOutputQueue
->csQueue
);
262 IMemInputPin_ReceiveMultiple(pOutputQueue
->pInputPin
->pMemInputPin
, ppSamples
, nSamples
, &nSamplesProcessed
);
263 EnterCriticalSection(&pOutputQueue
->csQueue
);
264 IMemInputPin_Release(pOutputQueue
->pInputPin
->pMemInputPin
);
266 for (i
= 0; i
< nSamples
; i
++)
267 IMediaSample_Release(ppSamples
[i
]);
268 HeapFree(GetProcessHeap(),0,ppSamples
);
270 /* Process Non-Samples */
271 LIST_FOR_EACH_SAFE(cursor
, cursor2
, pOutputQueue
->SampleList
)
273 QueuedEvent
*qev
= LIST_ENTRY(cursor
, QueuedEvent
, entry
);
274 if (qev
->type
== EOS_PACKET
)
277 IPin_ConnectedTo(&pOutputQueue
->pInputPin
->pin
.IPin_iface
, &ppin
);
280 IPin_EndOfStream(ppin
);
284 else if (qev
->type
== SAMPLE_PACKET
)
287 FIXME("Unhandled Event type %i\n",qev
->type
);
289 HeapFree(GetProcessHeap(),0,qev
);
292 pOutputQueue
->bSendAnyway
= FALSE
;
294 LeaveCriticalSection(&pOutputQueue
->csQueue
);
295 WaitForSingleObject(pOutputQueue
->hProcessQueue
, INFINITE
);
297 while (!pOutputQueue
->bTerminate
);