private void Config() { IWMWriter writer; WMUtils.WMCreateWriter(IntPtr.Zero, out writer); writer.SetProfileByID(g); m_Writer = writer as IWMWriterAdvanced2; }
/// <summary> /// Adds a IWMWriterNetworkSink to the current AsfWriter output filter /// </summary> /// <param name="streamPort">The port out of which to stream the transcoded file</param> private void AddStreamSinkToCurrentOutputFilter(int streamPort) { int hr; DirectShowLib.IServiceProvider pServiceProvider; // http://msdn.microsoft.com/en-us/library/dd390985%28VS.85%29.aspx WMAsfWriter asfwriter = (WMAsfWriter)currentOutputFilter; pServiceProvider = (DirectShowLib.IServiceProvider)asfwriter; // Get access to WMwriterAdvanced2 object using pServiceProvider (poss not futureproof) (see http://groups.google.com/group/microsoft.public.win32.programmer.directx.video/browse_thread/thread/36b154d41cb76ffd/c571d6ef56de11af?#c571d6ef56de11af ) DsGuid dsgWMwriterAdvanced2 = DsGuid.FromGuid(new Guid(GUIDs.IWMWriterAdvanced2)); IWMWriterAdvanced2 WMWriterAdvanced2 = null; object o = null; hr = pServiceProvider.QueryService(dsgWMwriterAdvanced2, dsgWMwriterAdvanced2, out o); // FAILS IN A STA THREAD DsError.ThrowExceptionForHR(hr); WMWriterAdvanced2 = (IWMWriterAdvanced2)o; IWMWriterNetworkSink nsink; WMUtils.WMCreateWriterNetworkSink(out nsink); NetworkSink = nsink; dc.Add(nsink); nsink.SetMaximumClients(1); nsink.SetNetworkProtocol(NetProtocol.HTTP); NetworkSink.Open(ref streamPort); // Will throw exception if port is in use int nSinks; WMWriterAdvanced2.GetSinkCount(out nSinks); if (nSinks > 0) { IWMWriterSink pSink = null; WMWriterAdvanced2.GetSink(0, out pSink); if (pSink != null) { WMWriterAdvanced2.RemoveSink(pSink); } Marshal.ReleaseComObject(pSink); pSink = null; } WMWriterAdvanced2.AddSink(NetworkSink); }
void ConfigureASFWriter(WMAsfWriter asf_filter, WTVStreamingVideoRequest strq, FrameSize SourceFrameSize) { int hr; // Now it's added to the graph, configure it with the selected WM Profile SendDebugMessage("Getting WM profile with quality of " + strq.Quality.ToString(), 0); WindowsMediaLib.IWMProfileManager profileManager; WMUtils.WMCreateProfileManager(out profileManager); IWMProfile wmProfile; string txtPrxProfile = getPRXProfileForQuality(strq.Quality); if (!(string.IsNullOrEmpty(txtPrxProfile))) { SendDebugMessage("Adjusting WM profile to fit video within designated frame size", 0); // SET VIDEO SIZE TO FIT WITHIN THE RIGHT FRAME SendDebugMessage("Source video size is " + SourceFrameSize.ToString(), 0); FrameSize containerSize = frameSizeForStreamRequest(strq); SendDebugMessage("Container size is " + containerSize.ToString(), 0); FrameSize newVideoSize = new FrameSize(SourceFrameSize, containerSize); SendDebugMessage("Output size is " + newVideoSize.ToString(), 0); SetProfileFrameSize(ref txtPrxProfile, newVideoSize); SetProfileCustomSettings(ref txtPrxProfile, ref strq); // returns immediately if not custom quality SendDebugMessage("Configuring ASF Writer with profile", 0); profileManager.LoadProfileByData(txtPrxProfile, out wmProfile); WindowsMediaLib.IConfigAsfWriter configWriter = (WindowsMediaLib.IConfigAsfWriter)asf_filter; configWriter.ConfigureFilterUsingProfile(wmProfile); configWriter.SetIndexMode(true); // yes index - DEFAULT /* Additional config - TEST * //DirectShowLib.IConfigAsfWriter2 configAsfWriter2 = (DirectShowLib.IConfigAsfWriter2)asf_filter; * //configAsfWriter2.SetParam(ASFWriterConfig.AutoIndex, 0, 0); // IT IS DEFAULT */ // (NOT WORKING) // SET ANAMORPHIC VIDEO MARKERS WITHIN STREAM (ASPECT RATIO) ******************************* UInt32 uiAspectX = (UInt32)SourceFrameSize.Width; byte[] bAspectX = BitConverter.GetBytes(uiAspectX); UInt32 uiAspectY = (UInt32)SourceFrameSize.Height; byte[] bAspectY = BitConverter.GetBytes(uiAspectY); DirectShowLib.IServiceProvider pServiceProvider; // http://msdn.microsoft.com/en-us/library/dd390985%28VS.85%29.aspx pServiceProvider = (DirectShowLib.IServiceProvider)asf_filter; DsGuid dsgIWMHeaderinfo = DsGuid.FromGuid(new Guid(GUIDs.IWMWriterAdvanced2)); object o3 = null; hr = pServiceProvider.QueryService(dsgIWMHeaderinfo, dsgIWMHeaderinfo, out o3); // FAILS IN A STA THREAD DsError.ThrowExceptionForHR(hr); IWMHeaderInfo headerinfo = (IWMHeaderInfo)o3; // Get access to WMwriterAdvanced2 object using pServiceProvider (poss not futureproof) (see http://groups.google.com/group/microsoft.public.win32.programmer.directx.video/browse_thread/thread/36b154d41cb76ffd/c571d6ef56de11af?#c571d6ef56de11af ) DsGuid dsgWMwriterAdvanced2 = DsGuid.FromGuid(new Guid(GUIDs.IWMWriterAdvanced2)); object o = null; hr = pServiceProvider.QueryService(dsgWMwriterAdvanced2, dsgWMwriterAdvanced2, out o); // FAILS IN A STA THREAD DsError.ThrowExceptionForHR(hr); IWMWriterAdvanced2 WMWriterAdvanced2 = null; WMWriterAdvanced2 = (IWMWriterAdvanced2)o; // Get Access to IWMHeaderInfo3 through WMWriterAdvanced2 object o2 = null; //pServiceProvider = (DirectShowLib.IServiceProvider)WMWriterAdvanced2; DsGuid dsgIWMHeaderInfo3 = DsGuid.FromGuid(new Guid(GUIDs.IWMHeaderInfo3)); hr = pServiceProvider.QueryService(dsgWMwriterAdvanced2, dsgIWMHeaderInfo3, out o2); // LET'S SEE DsError.ThrowExceptionForHR(hr); IWMHeaderInfo3 WMHeaderInfo3 = null; WMHeaderInfo3 = (IWMHeaderInfo3)o2; short pwIndex; // Add Aspect Ratio information WMHeaderInfo3.AddAttribute(2, "AspectRatioX", out pwIndex, AttrDataType.DWORD, 0, bAspectX, bAspectX.Length); WMHeaderInfo3.AddAttribute(2, "AspectRatioY", out pwIndex, AttrDataType.DWORD, 0, bAspectY, bAspectY.Length); // Try with other interface too headerinfo.SetAttribute(2, "AspectRatioX", AttrDataType.DWORD, bAspectX, Convert.ToInt16(bAspectX.Length)); headerinfo.SetAttribute(2, "AspectRatioY", AttrDataType.DWORD, bAspectY, Convert.ToInt16(bAspectY.Length)); // ************ DEINTERLACE (experimental) if (strq.DeInterlaceMode > 0) { DeInterlaceModes dimode = DeInterlaceModes.WM_DM_NOTINTERLACED; // Deinterlace Mode if (strq.DeInterlaceMode == 1) { dimode = DeInterlaceModes.WM_DM_DEINTERLACE_NORMAL; } else if (strq.DeInterlaceMode == 2) { dimode = DeInterlaceModes.WM_DM_DEINTERLACE_HALFSIZE; } // Index of video pin int pinIndex = FilterGraphTools.FindPinIndexByMediaType(currentOutputFilter, PinDirection.Input, MediaType.Video, MediaSubType.Null); byte[] bDiMode = BitConverter.GetBytes((int)dimode); short szOf = (short)bDiMode.Length; // Set to use deinterlace mode try { WMWriterAdvanced2.SetInputSetting(pinIndex, g_wszDeinterlaceMode, AttrDataType.DWORD, bDiMode, szOf); } catch (Exception ex) { SendDebugMessageWithException("Could not set interlace mode:", ex); } } } else { SendDebugMessage("Warning - PRX Profile string was empty; using default WM config."); } }
public void Init(Hashtable config = null) { //m_FilterGraph = (IFilterGraph2)new FilterGraph(); m_FilterGraph = (IGraphBuilder)Activator.CreateInstance(Type.GetTypeFromCLSID(Clsid.FilterGraph, true)); // Get the ICaptureGraphBuilder2 Guid clsid = Clsid.CaptureGraphBuilder2; Guid riid = typeof(ICaptureGraphBuilder2).GUID; ICaptureGraphBuilder2 capGraph = (ICaptureGraphBuilder2)DsBugWO.CreateDsInstance(ref clsid, ref riid); IBaseFilter capVideoFilter = null; IBaseFilter capAudioFilter = null; IBaseFilter asfWriter = null; IServiceProvider serviceProvider = null; int hr; object iwmWriter2; try { // Start building the graph hr = capGraph.SetFiltergraph(m_FilterGraph); Marshal.ThrowExceptionForHR(hr); // Add the video device to the graph if (videoDevChosen != null) { capVideoFilter = GetCapFilter(ref videoDevChosen); hr = m_FilterGraph.AddFilter(capVideoFilter, "Video Capture Device"); Marshal.ThrowExceptionForHR(hr); } // Add the audio device to the graph if (audioDevChosen != null) { capAudioFilter = GetCapFilter(ref audioDevChosen); hr = m_FilterGraph.AddFilter(capAudioFilter, "Audio Capture Device"); Marshal.ThrowExceptionForHR(hr); } // if we need some shitty quality if (config.Contains("shitty")) { InitAsfWriter(out asfWriter, true); } else { InitAsfWriter(out asfWriter); } //GEtting IWMAdvancedWriter2; serviceProvider = (IServiceProvider)asfWriter; Guid IID_IWMWriterAdvanced2 = new Guid("{962dc1ec-c046-4db8-9cc7-26ceae500817}"); hr = serviceProvider.QueryService(IID_IWMWriterAdvanced2, IID_IWMWriterAdvanced2, out iwmWriter2); Marshal.ThrowExceptionForHR(hr); m_writerAdvanced2 = (IWMWriterAdvanced2)iwmWriter2; m_writerAdvanced2.SetLiveSource(true); if (config.ContainsKey("cap")) { outputFilename = config["cap"] as string; Console.WriteLine("[MODE] Capturing to a local file: {0}", outputFilename); } IFileSinkFilter cap = (IFileSinkFilter)asfWriter; cap.SetFileName(outputFilename, null); if (!config.ContainsKey("cap")) { //deleting useless sink (writer to a file on a disk). IWMWriterSink uselessSink = null; m_writerAdvanced2.GetSink(0, out uselessSink); m_writerAdvanced2.RemoveSink(uselessSink); if (uselessSink != null) { Marshal.ReleaseComObject(uselessSink); uselessSink = null; } } if (config.Contains("send")) { string url = config["send"] as string; Console.WriteLine("[MODE] Streaming to a remote server: {0}", url); WriterNetworkSink sender = new WriterNetworkSink(url); m_writerAdvanced2.AddSink(sender); } if (config.Contains("share")) { int port = (int)config["share"]; WriterNetworkSink listener = new WriterNetworkSink(port); Console.WriteLine("[MODE] Started listening on port {0}", port); m_writerAdvanced2.AddSink(listener); } //Connecting VideoDev to asfWriter if (videoDevChosen != null) { hr = capGraph.RenderStream(PinCategory.Capture, MediaType.Video, capVideoFilter, null, asfWriter); //hr = capGraph.RenderStream(null, null, capVideoFilter, null, asfWriter); Marshal.ThrowExceptionForHR(hr); } //Connecting AudioDev to asfWriter if (audioDevChosen != null) { hr = capGraph.RenderStream(PinCategory.Capture, MediaType.Audio, capAudioFilter, null, asfWriter); //hr = capGraph.RenderStream(null, null, capAudioFilter, null, asfWriter); Marshal.ThrowExceptionForHR(hr); } m_mediaCtrl = m_FilterGraph as IMediaControl; //debug, dumps graph //DirectShowLib.Utils.FilterGraphTools.SaveGraphFile(m_FilterGraph, ".\\mygraph.grf"); } finally { if (capVideoFilter != null) { Marshal.ReleaseComObject(capVideoFilter); capVideoFilter = null; } if (capAudioFilter != null) { Marshal.ReleaseComObject(capAudioFilter); capAudioFilter = null; } if (asfWriter != null) { Marshal.ReleaseComObject(asfWriter); asfWriter = null; } if (capGraph != null) { Marshal.ReleaseComObject(capGraph); capGraph = null; } if (serviceProvider != null) { Marshal.ReleaseComObject(serviceProvider); serviceProvider = null; } } Console.WriteLine("INIT done"); }