diff --git a/Program.cs b/Program.cs index f3a19ff..bea569b 100644 --- a/Program.cs +++ b/Program.cs @@ -13,7 +13,7 @@ namespace DualScreenDemo private static PrimaryForm primaryForm; // 儲存實例的參考 public static Room room = new Room(); - public static string verSion = "Server V2.8 202508081053"; + public static string verSion = "Server V2.8 202508081600"; [STAThread] static void Main() diff --git a/Services/MediaService.cs b/Services/MediaService.cs index dc19cd5..56ef7c5 100644 --- a/Services/MediaService.cs +++ b/Services/MediaService.cs @@ -4,7 +4,7 @@ using System.Runtime.InteropServices; namespace DualScreenDemo.Services { - public class MediaService + public class MediaService : Video { public IGraphBuilder graphBuilder; private IMediaControl mediaControl; @@ -153,71 +153,6 @@ namespace DualScreenDemo.Services videoWindow.put_Visible(OABool.True); // 显示窗口 } } - - private static IBaseFilter AddFilterByClsid(IGraphBuilder graphBuilder, string name, Guid clsid) - { - try - { - // 获取 CLSID 对应的类型 - Type filterType = Type.GetTypeFromCLSID(clsid); - //Console.WriteLine($"Attempting to create filter of type: {filterType.FullName}"); - - // 创建实例 - object filterObject = Activator.CreateInstance(filterType); - - // 尝试转换为 IBaseFilter - IBaseFilter filter = filterObject as IBaseFilter; - - if (filter == null) - { - // 如果转换失败,使用 IUnknown 获取并转换为 IBaseFilter - IntPtr comObjectPointer = Marshal.GetIUnknownForObject(filterObject); - filter = (IBaseFilter)Marshal.GetObjectForIUnknown(comObjectPointer); - // Console.WriteLine($"Successfully converted COM object to IBaseFilter via IUnknown."); - } - else - { - // Console.WriteLine($"Successfully created IBaseFilter directly."); - } - - // 添加过滤器到图形构建器 - int hr = graphBuilder.AddFilter(filter, name); - if (hr != 0) - { - // Console.WriteLine($"Failed to add filter {name} with CLSID {clsid}, HRESULT: {hr}"); - } - - DsError.ThrowExceptionForHR(hr); - // Console.WriteLine($"Successfully added filter {name} with CLSID {clsid}"); - return filter; - } - catch (Exception ex) - { - Console.WriteLine($"Exception in AddFilterByClsid: {ex.Message}"); - throw; // Rethrow the exception to handle it further up the call stack - } - } - private IPin FindPin(IBaseFilter filter, string pinName) - { - IEnumPins enumPins; - IPin[] pins = new IPin[1]; - - filter.EnumPins(out enumPins); - enumPins.Reset(); - - while (enumPins.Next(1, pins, IntPtr.Zero) == 0) - { - PinInfo pinInfo; - pins[0].QueryPinInfo(out pinInfo); - Console.WriteLine(pinInfo); - - if (pinInfo.name == pinName) - { - return pins[0]; - } - } - return null; - } public void SetVolume(int volume) { if (audioRenderer != null) @@ -358,13 +293,5 @@ namespace DualScreenDemo.Services Console.WriteLine(ex.Message); } } - private void SafeRelease(ref T comObject) where T : class - { - if (comObject != null) - { - Marshal.ReleaseComObject(comObject); - comObject = null; - } - } } } \ No newline at end of file diff --git a/Services/MediaServicePrimary.cs b/Services/MediaServicePrimary.cs index e1855ff..78a769b 100644 --- a/Services/MediaServicePrimary.cs +++ b/Services/MediaServicePrimary.cs @@ -4,18 +4,18 @@ using System.Runtime.InteropServices; namespace DualScreenDemo.Services { - public class MediaServicePrimary + public class MediaServicePrimary : Video { IGraphBuilder graphBuilder; - IBaseFilter sourceFilter; IBaseFilter lavSplitter; IBaseFilter lavVideoDecoder; IBaseFilter videoRenderer; IVideoWindow videoWindow; IMediaControl mediaControl; + IBaseFilter sourceFilter; - public MediaServicePrimary(){} + public MediaServicePrimary() { } public int Run() { @@ -122,89 +122,6 @@ namespace DualScreenDemo.Services Console.WriteLine("Error initializing graph builder for primary monitor: " + ex.Message); } } - private static IBaseFilter AddFilterByClsid(IGraphBuilder graphBuilder, string name, Guid clsid) - { - try - { - // 获取 CLSID 对应的类型 - Type filterType = Type.GetTypeFromCLSID(clsid); - //Console.WriteLine($"Attempting to create filter of type: {filterType.FullName}"); - - // 创建实例 - object filterObject = Activator.CreateInstance(filterType); - - // 尝试转换为 IBaseFilter - IBaseFilter filter = filterObject as IBaseFilter; - - if (filter == null) - { - // 如果转换失败,使用 IUnknown 获取并转换为 IBaseFilter - IntPtr comObjectPointer = Marshal.GetIUnknownForObject(filterObject); - filter = (IBaseFilter)Marshal.GetObjectForIUnknown(comObjectPointer); - // Console.WriteLine($"Successfully converted COM object to IBaseFilter via IUnknown."); - } - else - { - // Console.WriteLine($"Successfully created IBaseFilter directly."); - } - - // 添加过滤器到图形构建器 - int hr = graphBuilder.AddFilter(filter, name); - if (hr != 0) - { - // Console.WriteLine($"Failed to add filter {name} with CLSID {clsid}, HRESULT: {hr}"); - } - - DsError.ThrowExceptionForHR(hr); - // Console.WriteLine($"Successfully added filter {name} with CLSID {clsid}"); - return filter; - } - catch (Exception ex) - { - Console.WriteLine($"Exception in AddFilterByClsid: {ex.Message}"); - throw; // Rethrow the exception to handle it further up the call stack - } - } - private int ConnectFilters(IGraphBuilder graphBuilder, IBaseFilter sourceFilter, string sourcePinName, IBaseFilter destFilter, string destPinName) - { - IPin outPin = FindPin(sourceFilter, sourcePinName); - IPin inPin = FindPin(destFilter, destPinName); - if (outPin == null || inPin == null) - { - Console.WriteLine(String.Format("Cannot find pins: {0} or {1}", sourcePinName, destPinName)); - return -1; - } - int hr = graphBuilder.Connect(outPin, inPin); - return hr; - } - private IPin FindPin(IBaseFilter filter, string pinName) - { - IEnumPins enumPins; - IPin[] pins = new IPin[1]; - - filter.EnumPins(out enumPins); - enumPins.Reset(); - - while (enumPins.Next(1, pins, IntPtr.Zero) == 0) - { - PinInfo pinInfo; - pins[0].QueryPinInfo(out pinInfo); - Console.WriteLine(pinInfo); - - if (pinInfo.name == pinName) - { - return pins[0]; - } - } - return null; - } - private void SafeRelease(ref T comObject) where T : class - { - if (comObject != null) - { - Marshal.ReleaseComObject(comObject); - comObject = null; - } - } + } } \ No newline at end of file diff --git a/Services/Video.cs b/Services/Video.cs new file mode 100644 index 0000000..6572a49 --- /dev/null +++ b/Services/Video.cs @@ -0,0 +1,75 @@ +using DirectShowLib; +using System.Runtime.InteropServices; + +namespace DualScreenDemo.Services +{ + public class Video + { + protected IBaseFilter AddFilterByClsid(IGraphBuilder graphBuilder, string name, Guid clsid) + { + try + { + Type filterType = Type.GetTypeFromCLSID(clsid); + object filterObject = Activator.CreateInstance(filterType); + IBaseFilter filter = filterObject as IBaseFilter; + + if (filter == null) + { + IntPtr comObjectPointer = Marshal.GetIUnknownForObject(filterObject); + filter = (IBaseFilter)Marshal.GetObjectForIUnknown(comObjectPointer); + } + + // 添加过滤器到图形构建器 + int hr = graphBuilder.AddFilter(filter, name); + DsError.ThrowExceptionForHR(hr); + return filter; + } + catch (Exception ex) + { + Console.WriteLine($"Exception in AddFilterByClsid: {ex.Message}"); + throw; // Rethrow the exception to handle it further up the call stack + } + } + protected int ConnectFilters(IGraphBuilder graphBuilder, IBaseFilter sourceFilter, string sourcePinName, IBaseFilter destFilter, string destPinName) + { + IPin outPin = FindPin(sourceFilter, sourcePinName); + IPin inPin = FindPin(destFilter, destPinName); + if (outPin == null || inPin == null) + { + Console.WriteLine(String.Format("Cannot find pins: {0} or {1}", sourcePinName, destPinName)); + return -1; + } + int hr = graphBuilder.Connect(outPin, inPin); + return hr; + } + protected IPin FindPin(IBaseFilter filter, string pinName) + { + IEnumPins enumPins; + IPin[] pins = new IPin[1]; + + filter.EnumPins(out enumPins); + enumPins.Reset(); + + while (enumPins.Next(1, pins, IntPtr.Zero) == 0) + { + PinInfo pinInfo; + pins[0].QueryPinInfo(out pinInfo); + Console.WriteLine(pinInfo); + + if (pinInfo.name == pinName) + { + return pins[0]; + } + } + return null; + } + protected void SafeRelease(ref T comObject) where T : class + { + if (comObject != null) + { + Marshal.ReleaseComObject(comObject); + comObject = null; + } + } + } +} \ No newline at end of file