using DirectShow; using Sonic; using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; namespace VideoPlayLib { internal class Graph : BaseObject, IDisposable { //---- fields private readonly FilterGraph _value; private readonly IFilterGraph2 _filterGraph; private readonly IGraphBuilder _graph; private readonly IMediaControl _control; private readonly IMediaPosition _position; //---- ctor public Graph() { _value = new FilterGraph(); _filterGraph = (IFilterGraph2)_value; _graph = (IGraphBuilder)_value; _control = (IMediaControl)_value; _position = (IMediaPosition)_value; } //---- wraps public FilterGraph Value { get { return _value; } } public List GetAllFilters() { var lst = new List(); { IEnumFilters ief; _graph.EnumFilters(out ief); var arr = new IBaseFilter[1]; while (ief.Next(1, arr, IntPtr.Zero) == 0) { Debug.Assert(arr[0] != null, "arr[0] != null"); lst.Add(new DSFilter(arr[0])); } } return lst; } [DebuggerHidden] public DSFilter AddFilter(DSFilter filter, string name) { CheckError(_graph.AddFilter(filter.Value, name)); return filter; } [DebuggerHidden] public DSFilter AddSourceFilter(string filename, string name) { IBaseFilter filter; CheckError(_graph.AddSourceFilter(filename, name, out filter)); return new DSFilter(filter); } [DebuggerHidden] public void Connect(DSPin inoutPin, DSPin outputPin) { CheckError(_graph.Connect(inoutPin.Value, outputPin.Value)); } [DebuggerHidden] public void Disconnect(DSPin inoutPin) { CheckError(_graph.Disconnect(inoutPin.Value)); } [DebuggerHidden] public void RemoveFilter(DSFilter filter) { CheckError(_graph.RemoveFilter(filter.Value)); } [DebuggerHidden] public void Stop() { CheckError(_control.Stop()); } [DebuggerHidden] public void Run() { CheckError(_control.Run()); } [DebuggerHidden] public void Pause() { CheckError(_control.Pause()); } [DebuggerHidden] public void Abort() { Stop(); var lst = GetAllFilters(); lst.ForEach(RemoveFilter); lst.ForEach(p => p.Dispose()); CheckError(_graph.Abort()); Marshal.ReleaseComObject(_graph); } [DebuggerHidden] public FilterState GetState(int timeOut = 0) { FilterState result; CheckError(_control.GetState(timeOut, out result)); return result; } [DebuggerHidden] public double CurrentPosition { get { double tmp; CheckError(_position.get_CurrentPosition(out tmp)); return tmp; } set { CheckError(_position.put_CurrentPosition(value)); } } [DebuggerHidden] public double Duration { get { double tmp; CheckError(_position.get_Duration(out tmp)); return tmp; } } [DebuggerHidden] public DSFilter AddDeviceFilter(DSDevice device) { IBaseFilter filter; CheckError(_filterGraph.AddSourceFilterForMoniker(device.Value, null, device.Name, out filter)); return new DSFilter(filter); } public void Dispose() { //TODO: release resource Abort(); } } }