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 WINE_DEFAULT_DEBUG_CHANNEL(strmbase
);
25 enum {SAMPLE_PACKET
, EOS_PACKET
};
27 typedef struct tagQueuedEvent
{
31 IMediaSample
*pSample
;
34 static DWORD WINAPI
OutputQueue_InitialThreadProc(LPVOID data
)
36 OutputQueue
*This
= (OutputQueue
*)data
;
37 return This
->pFuncsTable
->pfnThreadProc(This
);
40 static void OutputQueue_FreeSamples(OutputQueue
*pOutputQueue
)
42 struct list
*cursor
, *cursor2
;
43 LIST_FOR_EACH_SAFE(cursor
, cursor2
, &pOutputQueue
->SampleList
)
45 QueuedEvent
*qev
= LIST_ENTRY(cursor
, QueuedEvent
, entry
);
47 HeapFree(GetProcessHeap(),0,qev
);
51 HRESULT WINAPI
OutputQueue_Construct(
52 BaseOutputPin
*pInputPin
,
58 const OutputQueueFuncTable
* pFuncsTable
,
59 OutputQueue
**ppOutputQueue
)
62 BOOL threaded
= FALSE
;
67 if (!pInputPin
|| !pFuncsTable
|| !ppOutputQueue
)
70 *ppOutputQueue
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(OutputQueue
));
74 This
= *ppOutputQueue
;
75 This
->pFuncsTable
= pFuncsTable
;
76 This
->lBatchSize
= lBatchSize
;
77 This
->bBatchExact
= bBatchExact
;
78 InitializeCriticalSection(&This
->csQueue
);
79 This
->csQueue
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": OutputQueue.csQueue");
80 list_init(&This
->SampleList
);
82 This
->pInputPin
= pInputPin
;
83 IPin_AddRef(&pInputPin
->pin
.IPin_iface
);
85 EnterCriticalSection(&This
->csQueue
);
86 if (bAuto
&& pInputPin
->pMemInputPin
)
87 threaded
= IMemInputPin_ReceiveCanBlock(pInputPin
->pMemInputPin
) == S_OK
;
93 This
->hThread
= CreateThread(NULL
, 0, OutputQueue_InitialThreadProc
, This
, 0, &tid
);
96 SetThreadPriority(This
->hThread
, dwPriority
);
97 This
->hProcessQueue
= CreateEventW(NULL
, 0, 0, NULL
);
100 LeaveCriticalSection(&This
->csQueue
);
105 HRESULT WINAPI
OutputQueue_Destroy(OutputQueue
*pOutputQueue
)
107 EnterCriticalSection(&pOutputQueue
->csQueue
);
108 OutputQueue_FreeSamples(pOutputQueue
);
109 pOutputQueue
->bTerminate
= TRUE
;
110 SetEvent(pOutputQueue
->hProcessQueue
);
111 LeaveCriticalSection(&pOutputQueue
->csQueue
);
113 pOutputQueue
->csQueue
.DebugInfo
->Spare
[0] = 0;
114 DeleteCriticalSection(&pOutputQueue
->csQueue
);
115 CloseHandle(pOutputQueue
->hProcessQueue
);
117 IPin_Release(&pOutputQueue
->pInputPin
->pin
.IPin_iface
);
118 HeapFree(GetProcessHeap(),0,pOutputQueue
);
122 HRESULT WINAPI
OutputQueue_ReceiveMultiple(OutputQueue
*pOutputQueue
, IMediaSample
**ppSamples
, LONG nSamples
, LONG
*nSamplesProcessed
)
127 if (!pOutputQueue
->pInputPin
->pin
.pConnectedTo
|| !pOutputQueue
->pInputPin
->pMemInputPin
)
128 return VFW_E_NOT_CONNECTED
;
130 if (!pOutputQueue
->hThread
)
132 IMemInputPin_AddRef(pOutputQueue
->pInputPin
->pMemInputPin
);
133 hr
= IMemInputPin_ReceiveMultiple(pOutputQueue
->pInputPin
->pMemInputPin
,ppSamples
, nSamples
, nSamplesProcessed
);
134 IMemInputPin_Release(pOutputQueue
->pInputPin
->pMemInputPin
);
138 EnterCriticalSection(&pOutputQueue
->csQueue
);
139 *nSamplesProcessed
= 0;
141 for (i
= 0; i
< nSamples
; i
++)
143 QueuedEvent
*qev
= HeapAlloc(GetProcessHeap(),0,sizeof(QueuedEvent
));
146 ERR("Out of Memory\n");
150 qev
->type
= SAMPLE_PACKET
;
151 qev
->pSample
= ppSamples
[i
];
152 IMediaSample_AddRef(ppSamples
[i
]);
153 list_add_tail(&pOutputQueue
->SampleList
, &qev
->entry
);
154 (*nSamplesProcessed
)++;
157 if (!pOutputQueue
->bBatchExact
|| list_count(&pOutputQueue
->SampleList
) >= pOutputQueue
->lBatchSize
)
158 SetEvent(pOutputQueue
->hProcessQueue
);
159 LeaveCriticalSection(&pOutputQueue
->csQueue
);
164 HRESULT WINAPI
OutputQueue_Receive(OutputQueue
*pOutputQueue
, IMediaSample
*pSample
)
167 return OutputQueue_ReceiveMultiple(pOutputQueue
,&pSample
,1,&processed
);
170 VOID WINAPI
OutputQueue_SendAnyway(OutputQueue
*pOutputQueue
)
172 if (pOutputQueue
->hThread
)
174 EnterCriticalSection(&pOutputQueue
->csQueue
);
175 if (!list_empty(&pOutputQueue
->SampleList
))
177 pOutputQueue
->bSendAnyway
= TRUE
;
178 SetEvent(pOutputQueue
->hProcessQueue
);
180 LeaveCriticalSection(&pOutputQueue
->csQueue
);
184 VOID WINAPI
OutputQueue_EOS(OutputQueue
*pOutputQueue
)
186 EnterCriticalSection(&pOutputQueue
->csQueue
);
187 if (pOutputQueue
->hThread
)
189 QueuedEvent
*qev
= HeapAlloc(GetProcessHeap(),0,sizeof(QueuedEvent
));
192 ERR("Out of Memory\n");
193 LeaveCriticalSection(&pOutputQueue
->csQueue
);
196 qev
->type
= EOS_PACKET
;
198 list_add_tail(&pOutputQueue
->SampleList
, &qev
->entry
);
203 IPin_ConnectedTo(&pOutputQueue
->pInputPin
->pin
.IPin_iface
, &ppin
);
206 IPin_EndOfStream(ppin
);
210 LeaveCriticalSection(&pOutputQueue
->csQueue
);
211 /* Covers sending the Event to the worker Thread */
212 OutputQueue_SendAnyway(pOutputQueue
);
215 DWORD WINAPI
OutputQueueImpl_ThreadProc(OutputQueue
*pOutputQueue
)
219 EnterCriticalSection(&pOutputQueue
->csQueue
);
220 if (!list_empty(&pOutputQueue
->SampleList
) &&
221 (!pOutputQueue
->bBatchExact
||
222 list_count(&pOutputQueue
->SampleList
) >= pOutputQueue
->lBatchSize
||
223 pOutputQueue
->bSendAnyway
227 while (!list_empty(&pOutputQueue
->SampleList
))
229 IMediaSample
**ppSamples
;
231 LONG nSamplesProcessed
;
232 struct list
*cursor
, *cursor2
;
235 /* First Pass Process Samples */
236 i
= list_count(&pOutputQueue
->SampleList
);
237 ppSamples
= HeapAlloc(GetProcessHeap(),0,sizeof(IMediaSample
*) * i
);
239 LIST_FOR_EACH_SAFE(cursor
, cursor2
, &pOutputQueue
->SampleList
)
241 QueuedEvent
*qev
= LIST_ENTRY(cursor
, QueuedEvent
, entry
);
242 if (qev
->type
== SAMPLE_PACKET
)
243 ppSamples
[nSamples
++] = qev
->pSample
;
247 HeapFree(GetProcessHeap(),0,qev
);
250 if (pOutputQueue
->pInputPin
->pin
.pConnectedTo
&& pOutputQueue
->pInputPin
->pMemInputPin
)
252 IMemInputPin_AddRef(pOutputQueue
->pInputPin
->pMemInputPin
);
253 LeaveCriticalSection(&pOutputQueue
->csQueue
);
254 IMemInputPin_ReceiveMultiple(pOutputQueue
->pInputPin
->pMemInputPin
, ppSamples
, nSamples
, &nSamplesProcessed
);
255 EnterCriticalSection(&pOutputQueue
->csQueue
);
256 IMemInputPin_Release(pOutputQueue
->pInputPin
->pMemInputPin
);
258 for (i
= 0; i
< nSamples
; i
++)
259 IMediaSample_Release(ppSamples
[i
]);
260 HeapFree(GetProcessHeap(),0,ppSamples
);
262 /* Process Non-Samples */
263 LIST_FOR_EACH_SAFE(cursor
, cursor2
, &pOutputQueue
->SampleList
)
265 QueuedEvent
*qev
= LIST_ENTRY(cursor
, QueuedEvent
, entry
);
266 if (qev
->type
== EOS_PACKET
)
269 IPin_ConnectedTo(&pOutputQueue
->pInputPin
->pin
.IPin_iface
, &ppin
);
272 IPin_EndOfStream(ppin
);
276 else if (qev
->type
== SAMPLE_PACKET
)
279 FIXME("Unhandled Event type %i\n",qev
->type
);
281 HeapFree(GetProcessHeap(),0,qev
);
284 pOutputQueue
->bSendAnyway
= FALSE
;
286 LeaveCriticalSection(&pOutputQueue
->csQueue
);
287 WaitForSingleObject(pOutputQueue
->hProcessQueue
, INFINITE
);
289 while (!pOutputQueue
->bTerminate
);