Skip to content

Commit 430ef2c

Browse files
committed
Bugfixes (Memory Leak), fallback mechanism to auto generate graph if manually generated graph doesn't work, fix video playback if no audio device is available, LAV Splitter Source is not hardcorded anymore, EVR is default Video Renderer
1 parent bda877a commit 430ef2c

20 files changed

+825
-126
lines changed

EVR Presenter/EVRPresenter.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
<DataExecutionPrevention>
107107
</DataExecutionPrevention>
108108
<TargetMachine>MachineX86</TargetMachine>
109+
<AssemblyDebug>true</AssemblyDebug>
109110
</Link>
110111
</ItemDefinitionGroup>
111112
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">

EVR Presenter/Helpers.h

+2
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class ThreadSafeQueue
133133
ComPtrList<T> m_list;
134134
};
135135

136+
//#define FILE_LOGGING
136137

137138
#if defined FILE_LOGGING
138139

@@ -176,6 +177,7 @@ static void LogGUID(const char *msg, REFGUID guid )
176177

177178
#else
178179

180+
179181
#define LOG_MSG(msg)
180182
#define LOG_GUID(msg, guid)
181183
#define LOG_IF_FAILED(msg, hr)

Source/DirectShow/Controls/MediaUriElement.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public MediaUriPlayer MediaUriPlayer
2828

2929
public static readonly DependencyProperty VideoRendererProperty =
3030
DependencyProperty.Register("VideoRenderer", typeof(VideoRendererType), typeof(MediaUriElement),
31-
new FrameworkPropertyMetadata(VideoRendererType.VideoMixingRenderer9,
31+
new FrameworkPropertyMetadata(VideoRendererType.EnhancedVideoRenderer,
3232
new PropertyChangedCallback(OnVideoRendererChanged)));
3333

3434
public VideoRendererType VideoRenderer

Source/DirectShow/MediaPlayers/BaseClasses.cs

+78-77
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#region Includes
22
using System;
33
using System.Collections.Generic;
4-
using System.ComponentModel;
54
using System.Drawing;
65
using System.Linq;
76
using System.Runtime.CompilerServices;
@@ -12,7 +11,7 @@
1211
using WPFMediaKit.MediaFoundation;
1312
using WPFMediaKit.MediaFoundation.Interop;
1413
using WPFMediaKit.Threading;
15-
using Size=System.Windows.Size;
14+
using Size = System.Windows.Size;
1615
#endregion
1716

1817
namespace WPFMediaKit.DirectShow.MediaPlayers
@@ -305,7 +304,7 @@ private void GetMainWindowHwndHelper()
305304

306305
if (m_window.Handle == IntPtr.Zero)
307306
{
308-
lock(m_window)
307+
lock (m_window)
309308
{
310309
m_window.CreateHandle(new CreateParams());
311310
}
@@ -495,7 +494,7 @@ protected virtual void Dispose(bool disposing)
495494
//if (m_disposed)
496495
// return;
497496

498-
if(!disposing)
497+
if (!disposing)
499498
return;
500499

501500
if (m_window != null)
@@ -509,8 +508,8 @@ protected virtual void Dispose(bool disposing)
509508
m_timer.Dispose();
510509

511510
m_timer = null;
512-
513-
if(CheckAccess())
511+
512+
if (CheckAccess())
514513
{
515514
FreeResources();
516515
Dispatcher.BeginInvokeShutdown();
@@ -637,7 +636,7 @@ private void RemoveWndProcHook()
637636
/// </summary>
638637
private void AddWndProcHook()
639638
{
640-
// HwndHelper.AddHook(WndProcHook);
639+
// HwndHelper.AddHook(WndProcHook);
641640
}
642641

643642
/// <summary>
@@ -771,14 +770,16 @@ protected void FreeCustomAllocator()
771770
if (m_customAllocator == null)
772771
return;
773772

774-
m_customAllocator.Dispose();
775-
776773
m_customAllocator.NewAllocatorFrame -= CustomAllocatorNewAllocatorFrame;
777774
m_customAllocator.NewAllocatorSurface -= CustomAllocatorNewAllocatorSurface;
778775

779-
if(Marshal.IsComObject(m_customAllocator))
776+
m_customAllocator.Dispose();
777+
778+
779+
780+
if (Marshal.IsComObject(m_customAllocator))
780781
Marshal.ReleaseComObject(m_customAllocator);
781-
782+
782783
m_customAllocator = null;
783784
}
784785

@@ -798,7 +799,7 @@ private void ResetLocalGraphResources()
798799
Marshal.ReleaseComObject(m_mediaControl);
799800
m_mediaControl = null;
800801

801-
if(m_mediaEvent != null)
802+
if (m_mediaEvent != null)
802803
Marshal.ReleaseComObject(m_mediaEvent);
803804
m_mediaEvent = null;
804805
}
@@ -859,7 +860,7 @@ private IBaseFilter CreateEnhancedVideoRenderer(IGraphBuilder graph, int streamC
859860
EvrPresenter presenter;
860861
IBaseFilter filter;
861862

862-
lock(m_videoRendererInitLock)
863+
lock (m_videoRendererInitLock)
863864
{
864865
var evr = new EnhancedVideoRenderer();
865866
filter = evr as IBaseFilter;
@@ -883,7 +884,7 @@ private IBaseFilter CreateEnhancedVideoRenderer(IGraphBuilder graph, int streamC
883884
var presenterSettings = presenter.VideoPresenter as IEVRPresenterSettings;
884885
if (presenterSettings == null)
885886
throw new Exception("Could not QueryInterface for the IEVRPresenterSettings");
886-
887+
887888
presenterSettings.SetBufferCount(3);
888889

889890
/* Use our interop hWnd */
@@ -900,12 +901,12 @@ private IBaseFilter CreateEnhancedVideoRenderer(IGraphBuilder graph, int streamC
900901
DsError.ThrowExceptionForHR(hr);
901902

902903
var filterConfig = filter as IEVRFilterConfig;
903-
904+
904905
if (filterConfig != null)
905906
filterConfig.SetNumberOfStreams(streamCount);
906907
}
907-
908-
908+
909+
909910
RegisterCustomAllocator(presenter);
910911

911912
return filter;
@@ -939,7 +940,7 @@ private IBaseFilter CreateVideoMixingRenderer9(IGraphBuilder graph, int streamCo
939940

940941
if (vmrSurfAllocNotify == null)
941942
throw new Exception("Could not query the VMR surface allocator.");
942-
943+
943944
var allocator = new Vmr9Allocator();
944945

945946
/* We supply our custom allocator to the renderer */
@@ -951,7 +952,7 @@ private IBaseFilter CreateVideoMixingRenderer9(IGraphBuilder graph, int streamCo
951952

952953
RegisterCustomAllocator(allocator);
953954

954-
hr = graph.AddFilter(vmr9,
955+
hr = graph.AddFilter(vmr9,
955956
string.Format("Renderer: {0}", VideoRendererType.VideoMixingRenderer9));
956957

957958
DsError.ThrowExceptionForHR(hr);
@@ -1104,7 +1105,7 @@ protected void InvokeNewAllocatorSurface(IntPtr pSurface)
11041105
}
11051106

11061107
#endregion
1107-
1108+
11081109
#region Helper Methods
11091110
/// <summary>
11101111
/// Sets the natural pixel resolution the video in the graph
@@ -1136,14 +1137,14 @@ protected static Size GetVideoSize(IBaseFilter renderer, PinDirection direction,
11361137

11371138
if (pin == null)
11381139
goto done;
1139-
1140+
11401141
int hr = pin.ConnectionMediaType(mediaType);
1141-
1142+
11421143
if (hr != 0)
11431144
goto done;
11441145

11451146
/* Check to see if its a video media type */
1146-
if (mediaType.formatType != FormatType.VideoInfo2 &&
1147+
if (mediaType.formatType != FormatType.VideoInfo2 &&
11471148
mediaType.formatType != FormatType.VideoInfo)
11481149
{
11491150
goto done;
@@ -1159,8 +1160,8 @@ protected static Size GetVideoSize(IBaseFilter renderer, PinDirection direction,
11591160

11601161
done:
11611162
DsUtils.FreeAMMediaType(mediaType);
1162-
1163-
if(pin != null)
1163+
1164+
if (pin != null)
11641165
Marshal.ReleaseComObject(pin);
11651166
return size;
11661167
}
@@ -1210,69 +1211,69 @@ protected static void RemoveAllFilters(IGraphBuilder graphBuilder)
12101211
{
12111212
graphBuilder.RemoveFilter(filtersArray[i]);
12121213
while (Marshal.ReleaseComObject(filtersArray[i]) > 0)
1213-
{}
1214+
{ }
12141215
}
12151216
}
12161217

1217-
/// <summary>
1218-
/// Adds a filter to a DirectShow graph based on it's name and filter category
1219-
/// </summary>
1220-
/// <param name="graphBuilder">The graph builder to add the filter to</param>
1221-
/// <param name="deviceCategory">The category the filter belongs to</param>
1222-
/// <param name="friendlyName">The friendly name of the filter</param>
1223-
/// <returns>Reference to the IBaseFilter that was added to the graph or returns null if unsuccessful</returns>
1224-
protected static IBaseFilter AddFilterByName(IGraphBuilder graphBuilder, Guid deviceCategory, string friendlyName)
1225-
{
1226-
var devices = DsDevice.GetDevicesOfCat(deviceCategory);
1227-
1228-
var deviceList = (from d in devices
1229-
where d.Name == friendlyName
1230-
select d);
1231-
DsDevice device = null;
1232-
if (deviceList.Count() > 0)
1233-
device = deviceList.Take(1).Single();
1234-
1235-
foreach (var item in deviceList)
1236-
{
1218+
/// <summary>
1219+
/// Adds a filter to a DirectShow graph based on it's name and filter category
1220+
/// </summary>
1221+
/// <param name="graphBuilder">The graph builder to add the filter to</param>
1222+
/// <param name="deviceCategory">The category the filter belongs to</param>
1223+
/// <param name="friendlyName">The friendly name of the filter</param>
1224+
/// <returns>Reference to the IBaseFilter that was added to the graph or returns null if unsuccessful</returns>
1225+
protected static IBaseFilter AddFilterByName(IGraphBuilder graphBuilder, Guid deviceCategory, string friendlyName)
1226+
{
1227+
var devices = DsDevice.GetDevicesOfCat(deviceCategory);
1228+
1229+
var deviceList = (from d in devices
1230+
where d.Name == friendlyName
1231+
select d);
1232+
DsDevice device = null;
1233+
if (deviceList.Count() > 0)
1234+
device = deviceList.Take(1).Single();
1235+
1236+
foreach (var item in deviceList)
1237+
{
12371238
if (item != device)
12381239
item.Dispose();
1239-
}
1240+
}
12401241

1241-
return AddFilterByDevice(graphBuilder, device);
1242-
}
1242+
return AddFilterByDevice(graphBuilder, device);
1243+
}
12431244

1244-
protected static IBaseFilter AddFilterByDevicePath(IGraphBuilder graphBuilder, Guid deviceCategory, string devicePath)
1245-
{
1246-
var devices = DsDevice.GetDevicesOfCat(deviceCategory);
1245+
protected static IBaseFilter AddFilterByDevicePath(IGraphBuilder graphBuilder, Guid deviceCategory, string devicePath)
1246+
{
1247+
var devices = DsDevice.GetDevicesOfCat(deviceCategory);
12471248

1248-
var deviceList = (from d in devices
1249-
where d.DevicePath == devicePath
1250-
select d);
1251-
DsDevice device = null;
1252-
if (deviceList.Count() > 0)
1253-
device = deviceList.Take(1).Single();
1249+
var deviceList = (from d in devices
1250+
where d.DevicePath == devicePath
1251+
select d);
1252+
DsDevice device = null;
1253+
if (deviceList.Count() > 0)
1254+
device = deviceList.Take(1).Single();
12541255

1255-
return AddFilterByDevice(graphBuilder, device);
1256-
}
1256+
return AddFilterByDevice(graphBuilder, device);
1257+
}
12571258

1258-
private static IBaseFilter AddFilterByDevice(IGraphBuilder graphBuilder, DsDevice device)
1259-
{
1260-
if (graphBuilder == null)
1261-
throw new ArgumentNullException("graphBuilder");
1259+
private static IBaseFilter AddFilterByDevice(IGraphBuilder graphBuilder, DsDevice device)
1260+
{
1261+
if (graphBuilder == null)
1262+
throw new ArgumentNullException("graphBuilder");
12621263

1263-
var filterGraph = graphBuilder as IFilterGraph2;
1264+
var filterGraph = graphBuilder as IFilterGraph2;
12641265

1265-
if (filterGraph == null)
1266-
return null;
1266+
if (filterGraph == null)
1267+
return null;
12671268

1268-
IBaseFilter filter = null;
1269-
if (device != null)
1270-
{
1271-
int hr = filterGraph.AddSourceFilterForMoniker(device.Mon, null, device.Name, out filter);
1272-
DsError.ThrowExceptionForHR(hr);
1273-
}
1274-
return filter;
1275-
}
1269+
IBaseFilter filter = null;
1270+
if (device != null)
1271+
{
1272+
int hr = filterGraph.AddSourceFilterForMoniker(device.Mon, null, device.Name, out filter);
1273+
DsError.ThrowExceptionForHR(hr);
1274+
}
1275+
return filter;
1276+
}
12761277

12771278
/// <summary>
12781279
/// Finds a pin that exists in a graph.
@@ -1284,7 +1285,7 @@ private static IBaseFilter AddFilterByDevice(IGraphBuilder graphBuilder, DsDevic
12841285
protected static IPin FindPinInGraphByMediaType(Guid majorOrMinorMediaType, PinDirection pinDirection, IGraphBuilder graph)
12851286
{
12861287
IEnumFilters enumFilters;
1287-
1288+
12881289
/* Get the filter enum */
12891290
graph.EnumFilters(out enumFilters);
12901291

@@ -1308,7 +1309,7 @@ protected static IPin FindPinInGraphByMediaType(Guid majorOrMinorMediaType, PinD
13081309
pin.EnumMediaTypes(out mediaTypesEnum);
13091310
var mediaTypesFetched = IntPtr.Zero;
13101311
var mediaTypes = new AMMediaType[1];
1311-
1312+
13121313
/* Enumerate the media types on the pin */
13131314
while (mediaTypesEnum.Next(1, mediaTypes, mediaTypesFetched) == 0)
13141315
{

0 commit comments

Comments
 (0)