2011-04-05 19:55:47 +02:00
|
|
|
/*
|
|
|
|
* Generic Implementation of COutputQueue
|
|
|
|
*
|
|
|
|
* Copyright 2011 Aric Stewart, CodeWeavers
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2019-05-09 17:12:32 +02:00
|
|
|
#include "strmbase_private.h"
|
2011-04-05 19:55:47 +02:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(strmbase);
|
|
|
|
|
2011-04-05 19:55:50 +02:00
|
|
|
enum {SAMPLE_PACKET, EOS_PACKET};
|
|
|
|
|
2011-04-05 19:55:47 +02:00
|
|
|
typedef struct tagQueuedEvent {
|
2011-04-05 19:55:50 +02:00
|
|
|
int type;
|
2011-04-05 19:55:47 +02:00
|
|
|
struct list entry;
|
|
|
|
|
|
|
|
IMediaSample *pSample;
|
|
|
|
} QueuedEvent;
|
|
|
|
|
|
|
|
static DWORD WINAPI OutputQueue_InitialThreadProc(LPVOID data)
|
|
|
|
{
|
|
|
|
OutputQueue *This = (OutputQueue *)data;
|
|
|
|
return This->pFuncsTable->pfnThreadProc(This);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void OutputQueue_FreeSamples(OutputQueue *pOutputQueue)
|
|
|
|
{
|
|
|
|
struct list *cursor, *cursor2;
|
2016-11-25 10:10:45 +01:00
|
|
|
LIST_FOR_EACH_SAFE(cursor, cursor2, &pOutputQueue->SampleList)
|
2011-04-05 19:55:47 +02:00
|
|
|
{
|
|
|
|
QueuedEvent *qev = LIST_ENTRY(cursor, QueuedEvent, entry);
|
|
|
|
list_remove(cursor);
|
|
|
|
HeapFree(GetProcessHeap(),0,qev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 21:28:25 +02:00
|
|
|
HRESULT WINAPI OutputQueue_Construct(struct strmbase_source *pInputPin, BOOL bAuto,
|
|
|
|
BOOL bQueue, LONG lBatchSize, BOOL bBatchExact, DWORD dwPriority,
|
|
|
|
const OutputQueueFuncTable *pFuncsTable, OutputQueue **ppOutputQueue)
|
2011-04-05 19:55:47 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
BOOL threaded = FALSE;
|
|
|
|
DWORD tid;
|
|
|
|
|
|
|
|
OutputQueue *This;
|
|
|
|
|
|
|
|
if (!pInputPin || !pFuncsTable || !ppOutputQueue)
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
|
|
|
*ppOutputQueue = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(OutputQueue));
|
|
|
|
if (!*ppOutputQueue)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
This = *ppOutputQueue;
|
|
|
|
This->pFuncsTable = pFuncsTable;
|
|
|
|
This->lBatchSize = lBatchSize;
|
|
|
|
This->bBatchExact = bBatchExact;
|
|
|
|
InitializeCriticalSection(&This->csQueue);
|
|
|
|
This->csQueue.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": OutputQueue.csQueue");
|
2016-11-25 10:10:45 +01:00
|
|
|
list_init(&This->SampleList);
|
2011-04-05 19:55:47 +02:00
|
|
|
|
|
|
|
This->pInputPin = pInputPin;
|
2012-03-24 17:42:32 +01:00
|
|
|
IPin_AddRef(&pInputPin->pin.IPin_iface);
|
2011-04-05 19:55:47 +02:00
|
|
|
|
|
|
|
EnterCriticalSection(&This->csQueue);
|
|
|
|
if (bAuto && pInputPin->pMemInputPin)
|
2015-10-16 09:17:59 +02:00
|
|
|
threaded = IMemInputPin_ReceiveCanBlock(pInputPin->pMemInputPin) == S_OK;
|
2011-04-05 19:55:47 +02:00
|
|
|
else
|
|
|
|
threaded = bQueue;
|
|
|
|
|
|
|
|
if (threaded)
|
|
|
|
{
|
|
|
|
This->hThread = CreateThread(NULL, 0, OutputQueue_InitialThreadProc, This, 0, &tid);
|
|
|
|
if (This->hThread)
|
|
|
|
{
|
|
|
|
SetThreadPriority(This->hThread, dwPriority);
|
|
|
|
This->hProcessQueue = CreateEventW(NULL, 0, 0, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LeaveCriticalSection(&This->csQueue);
|
|
|
|
|
2015-10-19 10:57:50 +02:00
|
|
|
return S_OK;
|
2011-04-05 19:55:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT WINAPI OutputQueue_Destroy(OutputQueue *pOutputQueue)
|
|
|
|
{
|
|
|
|
EnterCriticalSection(&pOutputQueue->csQueue);
|
|
|
|
OutputQueue_FreeSamples(pOutputQueue);
|
|
|
|
pOutputQueue->bTerminate = TRUE;
|
|
|
|
SetEvent(pOutputQueue->hProcessQueue);
|
|
|
|
LeaveCriticalSection(&pOutputQueue->csQueue);
|
|
|
|
|
2011-11-14 11:32:32 +01:00
|
|
|
pOutputQueue->csQueue.DebugInfo->Spare[0] = 0;
|
2011-04-05 19:55:47 +02:00
|
|
|
DeleteCriticalSection(&pOutputQueue->csQueue);
|
|
|
|
CloseHandle(pOutputQueue->hProcessQueue);
|
|
|
|
|
2012-03-24 17:42:32 +01:00
|
|
|
IPin_Release(&pOutputQueue->pInputPin->pin.IPin_iface);
|
2011-04-05 19:55:47 +02:00
|
|
|
HeapFree(GetProcessHeap(),0,pOutputQueue);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT WINAPI OutputQueue_ReceiveMultiple(OutputQueue *pOutputQueue, IMediaSample **ppSamples, LONG nSamples, LONG *nSamplesProcessed)
|
|
|
|
{
|
|
|
|
HRESULT hr = S_OK;
|
|
|
|
int i;
|
|
|
|
|
2019-09-27 04:40:53 +02:00
|
|
|
if (!pOutputQueue->pInputPin->pin.peer || !pOutputQueue->pInputPin->pMemInputPin)
|
2011-04-05 19:55:47 +02:00
|
|
|
return VFW_E_NOT_CONNECTED;
|
|
|
|
|
|
|
|
if (!pOutputQueue->hThread)
|
|
|
|
{
|
|
|
|
IMemInputPin_AddRef(pOutputQueue->pInputPin->pMemInputPin);
|
|
|
|
hr = IMemInputPin_ReceiveMultiple(pOutputQueue->pInputPin->pMemInputPin,ppSamples, nSamples, nSamplesProcessed);
|
|
|
|
IMemInputPin_Release(pOutputQueue->pInputPin->pMemInputPin);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
EnterCriticalSection(&pOutputQueue->csQueue);
|
|
|
|
*nSamplesProcessed = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < nSamples; i++)
|
|
|
|
{
|
|
|
|
QueuedEvent *qev = HeapAlloc(GetProcessHeap(),0,sizeof(QueuedEvent));
|
|
|
|
if (!qev)
|
|
|
|
{
|
|
|
|
ERR("Out of Memory\n");
|
|
|
|
hr = E_OUTOFMEMORY;
|
|
|
|
break;
|
|
|
|
}
|
2011-04-05 19:55:50 +02:00
|
|
|
qev->type = SAMPLE_PACKET;
|
2011-04-05 19:55:47 +02:00
|
|
|
qev->pSample = ppSamples[i];
|
|
|
|
IMediaSample_AddRef(ppSamples[i]);
|
2016-11-25 10:10:45 +01:00
|
|
|
list_add_tail(&pOutputQueue->SampleList, &qev->entry);
|
2011-04-05 19:55:47 +02:00
|
|
|
(*nSamplesProcessed)++;
|
|
|
|
}
|
|
|
|
|
2016-11-25 10:10:45 +01:00
|
|
|
if (!pOutputQueue->bBatchExact || list_count(&pOutputQueue->SampleList) >= pOutputQueue->lBatchSize)
|
2011-04-05 19:55:47 +02:00
|
|
|
SetEvent(pOutputQueue->hProcessQueue);
|
2011-06-15 22:21:50 +02:00
|
|
|
LeaveCriticalSection(&pOutputQueue->csQueue);
|
2011-04-05 19:55:47 +02:00
|
|
|
}
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT WINAPI OutputQueue_Receive(OutputQueue *pOutputQueue, IMediaSample *pSample)
|
|
|
|
{
|
|
|
|
LONG processed;
|
|
|
|
return OutputQueue_ReceiveMultiple(pOutputQueue,&pSample,1,&processed);
|
|
|
|
}
|
|
|
|
|
2011-04-05 19:55:50 +02:00
|
|
|
VOID WINAPI OutputQueue_SendAnyway(OutputQueue *pOutputQueue)
|
|
|
|
{
|
|
|
|
if (pOutputQueue->hThread)
|
|
|
|
{
|
|
|
|
EnterCriticalSection(&pOutputQueue->csQueue);
|
2016-11-25 10:10:45 +01:00
|
|
|
if (!list_empty(&pOutputQueue->SampleList))
|
2011-04-05 19:55:50 +02:00
|
|
|
{
|
|
|
|
pOutputQueue->bSendAnyway = TRUE;
|
|
|
|
SetEvent(pOutputQueue->hProcessQueue);
|
|
|
|
}
|
|
|
|
LeaveCriticalSection(&pOutputQueue->csQueue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID WINAPI OutputQueue_EOS(OutputQueue *pOutputQueue)
|
|
|
|
{
|
2019-11-19 01:51:24 +01:00
|
|
|
IPin *peer;
|
|
|
|
|
2011-04-05 19:55:50 +02:00
|
|
|
EnterCriticalSection(&pOutputQueue->csQueue);
|
|
|
|
if (pOutputQueue->hThread)
|
|
|
|
{
|
|
|
|
QueuedEvent *qev = HeapAlloc(GetProcessHeap(),0,sizeof(QueuedEvent));
|
|
|
|
if (!qev)
|
|
|
|
{
|
|
|
|
ERR("Out of Memory\n");
|
|
|
|
LeaveCriticalSection(&pOutputQueue->csQueue);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
qev->type = EOS_PACKET;
|
|
|
|
qev->pSample = NULL;
|
2016-11-25 10:10:45 +01:00
|
|
|
list_add_tail(&pOutputQueue->SampleList, &qev->entry);
|
2011-04-05 19:55:50 +02:00
|
|
|
}
|
2019-11-19 01:51:24 +01:00
|
|
|
else if ((peer = pOutputQueue->pInputPin->pin.peer))
|
|
|
|
IPin_EndOfStream(peer);
|
2011-04-05 19:55:50 +02:00
|
|
|
LeaveCriticalSection(&pOutputQueue->csQueue);
|
|
|
|
/* Covers sending the Event to the worker Thread */
|
|
|
|
OutputQueue_SendAnyway(pOutputQueue);
|
|
|
|
}
|
|
|
|
|
2011-04-05 19:55:47 +02:00
|
|
|
DWORD WINAPI OutputQueueImpl_ThreadProc(OutputQueue *pOutputQueue)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
EnterCriticalSection(&pOutputQueue->csQueue);
|
2016-11-25 10:10:45 +01:00
|
|
|
if (!list_empty(&pOutputQueue->SampleList) &&
|
2011-04-05 19:55:50 +02:00
|
|
|
(!pOutputQueue->bBatchExact ||
|
2016-11-25 10:10:45 +01:00
|
|
|
list_count(&pOutputQueue->SampleList) >= pOutputQueue->lBatchSize ||
|
2011-04-05 19:55:50 +02:00
|
|
|
pOutputQueue->bSendAnyway
|
|
|
|
)
|
|
|
|
)
|
2011-04-05 19:55:47 +02:00
|
|
|
{
|
2016-11-25 10:10:45 +01:00
|
|
|
while (!list_empty(&pOutputQueue->SampleList))
|
2011-04-05 19:55:47 +02:00
|
|
|
{
|
2011-04-05 19:55:50 +02:00
|
|
|
IMediaSample **ppSamples;
|
|
|
|
LONG nSamples;
|
|
|
|
LONG nSamplesProcessed;
|
|
|
|
struct list *cursor, *cursor2;
|
|
|
|
int i = 0;
|
2011-04-05 19:55:47 +02:00
|
|
|
|
2011-04-05 19:55:50 +02:00
|
|
|
/* First Pass Process Samples */
|
2016-11-25 10:10:45 +01:00
|
|
|
i = list_count(&pOutputQueue->SampleList);
|
2011-04-05 19:55:50 +02:00
|
|
|
ppSamples = HeapAlloc(GetProcessHeap(),0,sizeof(IMediaSample*) * i);
|
|
|
|
nSamples = 0;
|
2016-11-25 10:10:45 +01:00
|
|
|
LIST_FOR_EACH_SAFE(cursor, cursor2, &pOutputQueue->SampleList)
|
2011-04-05 19:55:50 +02:00
|
|
|
{
|
|
|
|
QueuedEvent *qev = LIST_ENTRY(cursor, QueuedEvent, entry);
|
|
|
|
if (qev->type == SAMPLE_PACKET)
|
|
|
|
ppSamples[nSamples++] = qev->pSample;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
list_remove(cursor);
|
|
|
|
HeapFree(GetProcessHeap(),0,qev);
|
|
|
|
}
|
|
|
|
|
2019-09-27 04:40:53 +02:00
|
|
|
if (pOutputQueue->pInputPin->pin.peer && pOutputQueue->pInputPin->pMemInputPin)
|
2011-04-05 19:55:50 +02:00
|
|
|
{
|
|
|
|
IMemInputPin_AddRef(pOutputQueue->pInputPin->pMemInputPin);
|
|
|
|
LeaveCriticalSection(&pOutputQueue->csQueue);
|
|
|
|
IMemInputPin_ReceiveMultiple(pOutputQueue->pInputPin->pMemInputPin, ppSamples, nSamples, &nSamplesProcessed);
|
|
|
|
EnterCriticalSection(&pOutputQueue->csQueue);
|
|
|
|
IMemInputPin_Release(pOutputQueue->pInputPin->pMemInputPin);
|
|
|
|
}
|
|
|
|
for (i = 0; i < nSamples; i++)
|
2012-08-15 11:45:21 +02:00
|
|
|
IMediaSample_Release(ppSamples[i]);
|
2011-04-05 19:55:50 +02:00
|
|
|
HeapFree(GetProcessHeap(),0,ppSamples);
|
|
|
|
|
|
|
|
/* Process Non-Samples */
|
2016-11-25 10:10:45 +01:00
|
|
|
LIST_FOR_EACH_SAFE(cursor, cursor2, &pOutputQueue->SampleList)
|
2011-04-05 19:55:50 +02:00
|
|
|
{
|
2011-06-15 22:21:50 +02:00
|
|
|
QueuedEvent *qev = LIST_ENTRY(cursor, QueuedEvent, entry);
|
|
|
|
if (qev->type == EOS_PACKET)
|
2011-04-05 19:55:50 +02:00
|
|
|
{
|
2019-11-19 01:51:24 +01:00
|
|
|
IPin *peer;
|
|
|
|
if ((peer = pOutputQueue->pInputPin->pin.peer))
|
|
|
|
IPin_EndOfStream(peer);
|
2011-04-05 19:55:50 +02:00
|
|
|
}
|
2011-06-15 22:21:50 +02:00
|
|
|
else if (qev->type == SAMPLE_PACKET)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
FIXME("Unhandled Event type %i\n",qev->type);
|
|
|
|
list_remove(cursor);
|
|
|
|
HeapFree(GetProcessHeap(),0,qev);
|
2011-04-05 19:55:50 +02:00
|
|
|
}
|
2011-04-05 19:55:47 +02:00
|
|
|
}
|
2011-04-05 19:55:50 +02:00
|
|
|
pOutputQueue->bSendAnyway = FALSE;
|
2011-04-05 19:55:47 +02:00
|
|
|
}
|
|
|
|
LeaveCriticalSection(&pOutputQueue->csQueue);
|
|
|
|
WaitForSingleObject(pOutputQueue->hProcessQueue, INFINITE);
|
|
|
|
}
|
|
|
|
while (!pOutputQueue->bTerminate);
|
|
|
|
return S_OK;
|
|
|
|
}
|