quartz: Simplify FilterGraph2_AddFilter().

Signed-off-by: Zebediah Figura <z.figura12@gmail.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Zebediah Figura 2020-01-22 00:08:56 -06:00 committed by Alexandre Julliard
parent 7daf030473
commit 228b8b984b
1 changed files with 55 additions and 59 deletions

View File

@ -184,7 +184,9 @@ typedef struct _IFilterGraphImpl {
LONG ref; LONG ref;
IUnknown *punkFilterMapper2; IUnknown *punkFilterMapper2;
struct list filters; struct list filters;
LONG nameIndex;
unsigned int name_index;
IReferenceClock *refClock; IReferenceClock *refClock;
IBaseFilter *refClockProvider; IBaseFilter *refClockProvider;
EventsQueue evqueue; EventsQueue evqueue;
@ -554,74 +556,68 @@ static IBaseFilter *find_filter_by_name(IFilterGraphImpl *graph, const WCHAR *na
} }
/*** IFilterGraph methods ***/ /*** IFilterGraph methods ***/
static HRESULT WINAPI FilterGraph2_AddFilter(IFilterGraph2 *iface, IBaseFilter *pFilter, static HRESULT WINAPI FilterGraph2_AddFilter(IFilterGraph2 *iface,
LPCWSTR pName) IBaseFilter *filter, const WCHAR *name)
{ {
IFilterGraphImpl *This = impl_from_IFilterGraph2(iface); IFilterGraphImpl *graph = impl_from_IFilterGraph2(iface);
struct filter *entry;
HRESULT hr;
int j;
WCHAR* wszFilterName = NULL;
BOOL duplicate_name = FALSE; BOOL duplicate_name = FALSE;
struct filter *entry;
unsigned int i;
HRESULT hr;
TRACE("(%p/%p)->(%p, %s (%p))\n", This, iface, pFilter, debugstr_w(pName), pName); TRACE("graph %p, filter %p, name %s.\n", graph, filter, debugstr_w(name));
if (!pFilter) if (!filter)
return E_POINTER; return E_POINTER;
wszFilterName = CoTaskMemAlloc( (pName ? lstrlenW(pName) + 6 : 5) * sizeof(WCHAR) ); if (!(entry = heap_alloc(sizeof(*entry))))
return E_OUTOFMEMORY;
if (pName && find_filter_by_name(This, pName)) if (!(entry->name = CoTaskMemAlloc((name ? wcslen(name) + 6 : 5) * sizeof(WCHAR))))
{
heap_free(entry);
return E_OUTOFMEMORY;
}
if (name && find_filter_by_name(graph, name))
duplicate_name = TRUE; duplicate_name = TRUE;
/* If no name given or name already existing, generate one */ if (!name || duplicate_name)
if (!pName || duplicate_name)
{ {
static const WCHAR wszFmt1[] = {'%','s',' ','%','0','4','d',0}; for (i = 0; i < 10000 ; ++i)
static const WCHAR wszFmt2[] = {'%','0','4','d',0};
for (j = 0; j < 10000 ; j++)
{ {
/* Create name */ if (name)
if (pName) swprintf(entry->name, name ? wcslen(name) + 6 : 5, L"%s %04u", name, graph->name_index);
swprintf(wszFilterName, pName ? lstrlenW(pName) + 6 : 5, wszFmt1, pName, This->nameIndex);
else else
swprintf(wszFilterName, pName ? lstrlenW(pName) + 6 : 5, wszFmt2, This->nameIndex); swprintf(entry->name, name ? wcslen(name) + 6 : 5, L"%04u", graph->name_index);
TRACE("Generated name %s\n", debugstr_w(wszFilterName));
if (This->nameIndex++ == 10000) graph->name_index = (graph->name_index + 1) % 10000;
This->nameIndex = 1;
if (!find_filter_by_name(This, wszFilterName)) if (!find_filter_by_name(graph, entry->name))
break; break;
} }
/* Unable to find a suitable name */
if (j == 10000) if (i == 10000)
{ {
CoTaskMemFree(wszFilterName); CoTaskMemFree(entry->name);
heap_free(entry);
return VFW_E_DUPLICATE_NAME; return VFW_E_DUPLICATE_NAME;
} }
} }
else else
memcpy(wszFilterName, pName, (lstrlenW(pName) + 1) * sizeof(WCHAR)); wcscpy(entry->name, name);
hr = IBaseFilter_JoinFilterGraph(pFilter, (IFilterGraph *)&This->IFilterGraph2_iface, wszFilterName); if (FAILED(hr = IBaseFilter_JoinFilterGraph(filter,
if (FAILED(hr)) (IFilterGraph *)&graph->IFilterGraph2_iface, entry->name)))
{ {
CoTaskMemFree(wszFilterName); CoTaskMemFree(entry->name);
heap_free(entry);
return hr; return hr;
} }
if (!(entry = heap_alloc(sizeof(*entry)))) IBaseFilter_AddRef(entry->filter = filter);
{ list_add_head(&graph->filters, &entry->entry);
CoTaskMemFree(wszFilterName); ++graph->version;
return E_OUTOFMEMORY;
}
IBaseFilter_AddRef(entry->filter = pFilter);
entry->name = wszFilterName;
list_add_head(&This->filters, &entry->entry);
This->version++;
return duplicate_name ? VFW_S_DUPLICATE_NAME : hr; return duplicate_name ? VFW_S_DUPLICATE_NAME : hr;
} }
@ -5706,7 +5702,7 @@ static HRESULT filter_graph_common_create(IUnknown *outer, void **out, BOOL thre
fimpl->IGraphVersion_iface.lpVtbl = &IGraphVersion_VTable; fimpl->IGraphVersion_iface.lpVtbl = &IGraphVersion_VTable;
fimpl->ref = 1; fimpl->ref = 1;
list_init(&fimpl->filters); list_init(&fimpl->filters);
fimpl->nameIndex = 1; fimpl->name_index = 1;
fimpl->refClock = NULL; fimpl->refClock = NULL;
fimpl->hEventCompletion = CreateEventW(0, TRUE, FALSE, 0); fimpl->hEventCompletion = CreateEventW(0, TRUE, FALSE, 0);
fimpl->HandleEcComplete = TRUE; fimpl->HandleEcComplete = TRUE;