protected override void AddSourceFilter() { base.AddSourceFilter(); //var muxer = new CodecInfo { CLSID = "{E4B7FAF9-9CFF-4FD5-AC16-0F250F5F97B7}", Name = "SolveigMM Matroska Muxer" }; var muxer = new CodecInfo { CLSID = "{A28F324B-DDC5-4999-AA25-D3A7E25EF7A8}", Name = "Haali Matroska Muxer" }; IBaseFilter muxerFilter = FilterGraphTools.AddFilterByName(_graphBuilder, FilterCategory.LegacyAmFilterCategory, muxer.Name); // FileSink can be supported by muxer directly (Haali), or we need an additional file sink var fileSink = muxerFilter as IFileSinkFilter; if (fileSink == null) { var writer = new CodecInfo { CLSID = "{8596E5F0-0DA5-11D0-BD21-00A0C911CE86}", Name = "File writer" }; fileSink = FilterGraphTools.AddFilterByName(_graphBuilder, FilterCategory.LegacyAmFilterCategory, writer.Name) as IFileSinkFilter; } if (fileSink != null) { fileSink.SetFileName(_targetFilename, null); } using (DSFilter sourceFilter = new DSFilter(_streamSourceFilter)) using (DSFilter muxerF = new DSFilter(muxerFilter)) { for (int index = 0; index < sourceFilter.Output.Count; index++) { DSPin pin = sourceFilter.Output[index]; using (pin) { if (index < muxerF.Input.Count) { pin.Connect(muxerF.Input[index]); } } } if (muxerF.OutputPin != null) { using (DSFilter writerF = new DSFilter((IBaseFilter)fileSink)) muxerF.OutputPin.Connect(writerF.InputPin); } } }
/// <summary> /// QueryConnect checks if two Pins can be connected. /// </summary> /// <param name="pin">Pin 1</param> /// <param name="other">Pin 2</param> /// <returns>True if they accept connection</returns> static bool QueryConnect(IPin pin, IPin other) { var pin1 = new DSPin(pin); var pinOther = new DSPin(other); foreach (var mediaType in pin1.MediaTypes) { if (pinOther.IsAccepted(mediaType)) { return(true); } } PinInfo info; PinInfo infoOther; pin.QueryPinInfo(out info); other.QueryPinInfo(out infoOther); ServiceRegistration.Get <ILogger>().Info("Pins {0} and {1} do not accept each other. Tested {2} media types", info.name, infoOther.name, pin1.MediaTypes.Count); FilterGraphTools.FreePinInfo(info); FilterGraphTools.FreePinInfo(infoOther); return(false); }
/* protected void InitAudioSampleGrabber() { // Get the graph builder IGraphBuilder graphBuilder = (mediaControl as IGraphBuilder); if (graphBuilder == null) return; try { // Build the sample grabber sampleGrabber = Activator.CreateInstance(Type.GetTypeFromCLSID(Filters.SampleGrabber, true)) as ISampleGrabber; if (sampleGrabber == null) return; // Add it to the filter graph int hr = graphBuilder.AddFilter(sampleGrabber as IBaseFilter, "ProTONE_SampleGrabber"); DsError.ThrowExceptionForHR(hr); AMMediaType mtAudio = new AMMediaType(); mtAudio.majorType = MediaType.Audio; mtAudio.subType = MediaSubType.PCM; mtAudio.formatPtr = IntPtr.Zero; _actualAudioFormat = null; hr = sampleGrabber.SetMediaType(mtAudio); DsError.ThrowExceptionForHR(hr); hr = sampleGrabber.SetBufferSamples(true); DsError.ThrowExceptionForHR(hr); hr = sampleGrabber.SetOneShot(false); DsError.ThrowExceptionForHR(hr); hr = sampleGrabber.SetCallback(this, 1); DsError.ThrowExceptionForHR(hr); sampleAnalyzerMustStop.Reset(); sampleAnalyzerThread = new Thread(new ThreadStart(SampleAnalyzerLoop)); sampleAnalyzerThread.Priority = ThreadPriority.Highest; sampleAnalyzerThread.Start(); } catch(Exception ex) { Logger.LogException(ex); } rotEntry = new DsROTEntry(graphBuilder as IFilterGraph); }*/ protected void InitAudioSampleGrabber_v2() { // Get the graph builder IGraphBuilder graphBuilder = (mediaControl as IGraphBuilder); if (graphBuilder == null) return; try { // Build the sample grabber sampleGrabber = Activator.CreateInstance(Type.GetTypeFromCLSID(Filters.SampleGrabber, true)) as ISampleGrabber; if (sampleGrabber == null) return; // Add it to the filter graph int hr = graphBuilder.AddFilter(sampleGrabber as IBaseFilter, "ProTONE_SampleGrabber_v2"); DsError.ThrowExceptionForHR(hr); IBaseFilter ffdAudioDecoder = null; IPin ffdAudioDecoderOutput = null; IPin soundDeviceInput = null; IPin sampleGrabberInput = null; IPin sampleGrabberOutput = null; IntPtr pSoundDeviceInput = IntPtr.Zero; // When using FFDShow, typically we'll find // a ffdshow Audio Decoder connected to the sound device filter // // i.e. [ffdshow Audio Decoder] --> [DirectSound Device] // // Our audio sample grabber supports only PCM sample input and output. // Its entire processing is based on this assumption. // // Thus need to insert the audio sample grabber between the ffdshow Audio Decoder and the sound device // because this is the only place where we can find PCM samples. The sound device only accepts PCM. // // So we need to turn this graph: // // .. -->[ffdshow Audio Decoder]-->[DirectSound Device] // // into this: // // .. -->[ffdshow Audio Decoder]-->[Sample grabber]-->[DirectSound Device] // // Actions to do to achieve the graph change: // // 1. Locate the ffdshow Audio Decoder in the graph // 2. Find its output pin and the pin that it's connected to // 3. Locate the input and output pins of sample grabber // 4. Disconnect the ffdshow Audio Decoder and its correspondent (sound device input pin) // 5. Connect the ffdshow Audio Decoder to sample grabber input // 6. Connect the sample grabber output to sound device input // that's all. // -------------- // 1. Locate the ffdshow Audio Decoder in the graph hr = graphBuilder.FindFilterByName("ffdshow Audio Decoder", out ffdAudioDecoder); DsError.ThrowExceptionForHR(hr); // 2. Find its output pin and the pin that it's connected to hr = ffdAudioDecoder.FindPin("Out", out ffdAudioDecoderOutput); DsError.ThrowExceptionForHR(hr); hr = ffdAudioDecoderOutput.ConnectedTo(out pSoundDeviceInput); DsError.ThrowExceptionForHR(hr); soundDeviceInput = new DSPin(pSoundDeviceInput).Value; // 3. Locate the input and output pins of sample grabber hr = (sampleGrabber as IBaseFilter).FindPin("In", out sampleGrabberInput); DsError.ThrowExceptionForHR(hr); hr = (sampleGrabber as IBaseFilter).FindPin("Out", out sampleGrabberOutput); DsError.ThrowExceptionForHR(hr); // 4. Disconnect the ffdshow Audio Decoder and its correspondent (sound device input pin) hr = ffdAudioDecoderOutput.Disconnect(); DsError.ThrowExceptionForHR(hr); hr = soundDeviceInput.Disconnect(); DsError.ThrowExceptionForHR(hr); // 5. Connect the ffdshow Audio Decoder to sample grabber input hr = graphBuilder.Connect(ffdAudioDecoderOutput, sampleGrabberInput); DsError.ThrowExceptionForHR(hr); // 6. Connect the sample grabber output to sound device input hr = graphBuilder.Connect(sampleGrabberOutput, soundDeviceInput); DsError.ThrowExceptionForHR(hr); AMMediaType mtAudio = new AMMediaType(); mtAudio.majorType = MediaType.Audio; mtAudio.subType = MediaSubType.PCM; mtAudio.formatPtr = IntPtr.Zero; _actualAudioFormat = null; sampleGrabber.SetMediaType(mtAudio); sampleGrabber.SetBufferSamples(true); sampleGrabber.SetOneShot(false); sampleGrabber.SetCallback(this, 1); sampleAnalyzerMustStop.Reset(); sampleAnalyzerThread = new Thread(new ThreadStart(SampleAnalyzerLoop)); sampleAnalyzerThread.Priority = ThreadPriority.Highest; sampleAnalyzerThread.Start(); } catch (Exception ex) { Logger.LogException(ex); } rotEntry = new DsROTEntry(graphBuilder as IFilterGraph); }
protected override HRESULT OnInitInterfaces() { m_Renderer = new VMR9Renderer(); m_Renderer.FilterGraph = m_GraphBuilder; IVMRFilterConfig9 _config = (IVMRFilterConfig9)m_Renderer.QueryInterface(typeof(IVMRFilterConfig9).GUID); HRESULT hr; if (_config != null) { hr = (HRESULT)_config.SetRenderingMode(VMR9Mode.Renderless); hr.Assert(); hr = (HRESULT)_config.SetNumberOfStreams(2); hr.Assert(); } m_mixerControl = (IVMRMixerControl9)m_Renderer.QueryInterface(typeof(IVMRMixerControl9).GUID); m_mixerControl.SetBackgroundClr((int)0xbf7f2f); VMR9MixerPrefs mixerPrefs; m_mixerControl.GetMixingPrefs(out mixerPrefs); m_mixerControl.SetMixingPrefs(mixerPrefs); IVMRSurfaceAllocatorNotify9 _notify = (IVMRSurfaceAllocatorNotify9)m_Renderer.QueryInterface(typeof(IVMRSurfaceAllocatorNotify9).GUID); if (_notify != null) { hr = (HRESULT)_notify.AdviseSurfaceAllocator(new IntPtr(g_ciUsedID), this); hr.Assert(); hr = (HRESULT)this.AdviseNotify(_notify); hr.Assert(); } DSVideoCaptureCategory captureCategory = new DSVideoCaptureCategory(); List <DSFilter> capFilters = new List <DSFilter>(); foreach (var captureDevice in captureCategory.Objects) { if (captureDevice.DevicePath == m_leftEyeDevicePath) { capFilters.Add(captureDevice.Filter); break; } } foreach (var captureDevice in captureCategory.Objects) { if (captureDevice.DevicePath == m_rightEyeDevicePath) { capFilters.Add(captureDevice.Filter); break; } } if (capFilters.Count < 2) { MessageBox.Show("Not enough capture devices found (" + capFilters.Count.ToString() + " device(s))"); throw new Exception(); } for (int i = 0; i < 2; i++) { if (capFilters[i] == null) { return(E_FAIL); } DSPin capturePin = null; foreach (var outputPin in capFilters[i].Output) { if (outputPin.Name == "Capture") { capturePin = outputPin; break; } } AMMediaType mjpgMediaType = null; int maxPixels = -1; foreach (var mediaType in capturePin.MediaTypes) { VideoInfoHeader videoInfo = new VideoInfoHeader(); videoInfo = (VideoInfoHeader)Marshal.PtrToStructure(mediaType.formatPtr, typeof(VideoInfoHeader)); // pick the highest res mode... if ((videoInfo.BmiHeader.Width * videoInfo.BmiHeader.Height) > maxPixels) { maxPixels = videoInfo.BmiHeader.Width * videoInfo.BmiHeader.Height; mjpgMediaType = mediaType; //break; } } capFilters[i].OutputPin.Format = mjpgMediaType; capFilters[i].FilterGraph = m_GraphBuilder; capFilters[i].Connect(m_Renderer); } VMR9NormalizedRect r1 = new VMR9NormalizedRect(0, 0, 0.5f, 1f); VMR9NormalizedRect r2 = new VMR9NormalizedRect(0.5f, 0f, 1f, 1f); int rt0 = m_mixerControl.SetOutputRect(0, ref r1); int rt1 = m_mixerControl.SetOutputRect(1, ref r2); hr = base.OnInitInterfaces(); return(hr); }
/// <summary> /// QueryConnect checks if two Pins can be connected. /// </summary> /// <param name="pin">Pin 1</param> /// <param name="other">Pin 2</param> /// <returns>True if they accept connection</returns> static bool QueryConnect(IPin pin, IPin other) { var pin1 = new DSPin(pin); var pinOther = new DSPin(other); foreach (var mediaType in pin1.MediaTypes) { if (pinOther.IsAccepted(mediaType)) return true; } PinInfo info; PinInfo infoOther; pin.QueryPinInfo(out info); other.QueryPinInfo(out infoOther); ServiceRegistration.Get<ILogger>().Info("Pins {0} and {1} do not accept each other. Tested {2} media types", info.name, infoOther.name, pin1.MediaTypes.Count); FilterGraphTools.FreePinInfo(info); FilterGraphTools.FreePinInfo(infoOther); return false; }
/* * protected void InitAudioSampleGrabber() * { * // Get the graph builder * IGraphBuilder graphBuilder = (mediaControl as IGraphBuilder); * if (graphBuilder == null) * return; * * try * { * // Build the sample grabber * sampleGrabber = Activator.CreateInstance(Type.GetTypeFromCLSID(Filters.SampleGrabber, true)) * as ISampleGrabber; * * if (sampleGrabber == null) * return; * * // Add it to the filter graph * int hr = graphBuilder.AddFilter(sampleGrabber as IBaseFilter, "ProTONE_SampleGrabber"); * DsError.ThrowExceptionForHR(hr); * * AMMediaType mtAudio = new AMMediaType(); * mtAudio.majorType = MediaType.Audio; * mtAudio.subType = MediaSubType.PCM; * mtAudio.formatPtr = IntPtr.Zero; * * _actualAudioFormat = null; * * hr = sampleGrabber.SetMediaType(mtAudio); * DsError.ThrowExceptionForHR(hr); * * hr = sampleGrabber.SetBufferSamples(true); * DsError.ThrowExceptionForHR(hr); * * hr = sampleGrabber.SetOneShot(false); * DsError.ThrowExceptionForHR(hr); * * hr = sampleGrabber.SetCallback(this, 1); * DsError.ThrowExceptionForHR(hr); * * sampleAnalyzerMustStop.Reset(); * sampleAnalyzerThread = new Thread(new ThreadStart(SampleAnalyzerLoop)); * sampleAnalyzerThread.Priority = ThreadPriority.Highest; * sampleAnalyzerThread.Start(); * } * catch(Exception ex) * { * Logger.LogException(ex); * } * * rotEntry = new DsROTEntry(graphBuilder as IFilterGraph); * }*/ protected void InitAudioSampleGrabber_v2() { // Get the graph builder IGraphBuilder graphBuilder = (mediaControl as IGraphBuilder); if (graphBuilder == null) { return; } try { // Build the sample grabber sampleGrabber = Activator.CreateInstance(Type.GetTypeFromCLSID(Filters.SampleGrabber, true)) as ISampleGrabber; if (sampleGrabber == null) { return; } // Add it to the filter graph int hr = graphBuilder.AddFilter(sampleGrabber as IBaseFilter, "ProTONE_SampleGrabber_v2"); DsError.ThrowExceptionForHR(hr); IBaseFilter ffdAudioDecoder = null; IPin ffdAudioDecoderOutput = null; IPin soundDeviceInput = null; IPin sampleGrabberInput = null; IPin sampleGrabberOutput = null; IntPtr pSoundDeviceInput = IntPtr.Zero; // When using FFDShow, typically we'll find // a ffdshow Audio Decoder connected to the sound device filter // // i.e. [ffdshow Audio Decoder] --> [DirectSound Device] // // Our audio sample grabber supports only PCM sample input and output. // Its entire processing is based on this assumption. // // Thus need to insert the audio sample grabber between the ffdshow Audio Decoder and the sound device // because this is the only place where we can find PCM samples. The sound device only accepts PCM. // // So we need to turn this graph: // // .. -->[ffdshow Audio Decoder]-->[DirectSound Device] // // into this: // // .. -->[ffdshow Audio Decoder]-->[Sample grabber]-->[DirectSound Device] // // Actions to do to achieve the graph change: // // 1. Locate the ffdshow Audio Decoder in the graph // 2. Find its output pin and the pin that it's connected to // 3. Locate the input and output pins of sample grabber // 4. Disconnect the ffdshow Audio Decoder and its correspondent (sound device input pin) // 5. Connect the ffdshow Audio Decoder to sample grabber input // 6. Connect the sample grabber output to sound device input // that's all. // -------------- // 1. Locate the ffdshow Audio Decoder in the graph hr = graphBuilder.FindFilterByName("ffdshow Audio Decoder", out ffdAudioDecoder); DsError.ThrowExceptionForHR(hr); // 2. Find its output pin and the pin that it's connected to hr = ffdAudioDecoder.FindPin("Out", out ffdAudioDecoderOutput); DsError.ThrowExceptionForHR(hr); hr = ffdAudioDecoderOutput.ConnectedTo(out pSoundDeviceInput); DsError.ThrowExceptionForHR(hr); soundDeviceInput = new DSPin(pSoundDeviceInput).Value; // 3. Locate the input and output pins of sample grabber hr = (sampleGrabber as IBaseFilter).FindPin("In", out sampleGrabberInput); DsError.ThrowExceptionForHR(hr); hr = (sampleGrabber as IBaseFilter).FindPin("Out", out sampleGrabberOutput); DsError.ThrowExceptionForHR(hr); // 4. Disconnect the ffdshow Audio Decoder and its correspondent (sound device input pin) hr = ffdAudioDecoderOutput.Disconnect(); DsError.ThrowExceptionForHR(hr); hr = soundDeviceInput.Disconnect(); DsError.ThrowExceptionForHR(hr); // 5. Connect the ffdshow Audio Decoder to sample grabber input hr = graphBuilder.Connect(ffdAudioDecoderOutput, sampleGrabberInput); DsError.ThrowExceptionForHR(hr); // 6. Connect the sample grabber output to sound device input hr = graphBuilder.Connect(sampleGrabberOutput, soundDeviceInput); DsError.ThrowExceptionForHR(hr); AMMediaType mtAudio = new AMMediaType(); mtAudio.majorType = MediaType.Audio; mtAudio.subType = MediaSubType.PCM; mtAudio.formatPtr = IntPtr.Zero; _actualAudioFormat = null; sampleGrabber.SetMediaType(mtAudio); sampleGrabber.SetBufferSamples(true); sampleGrabber.SetOneShot(false); sampleGrabber.SetCallback(this, 1); sampleAnalyzerMustStop.Reset(); sampleAnalyzerThread = new Thread(new ThreadStart(SampleAnalyzerLoop)); sampleAnalyzerThread.Priority = ThreadPriority.Highest; sampleAnalyzerThread.Start(); } catch (Exception ex) { Logger.LogException(ex); } rotEntry = new DsROTEntry(graphBuilder as IFilterGraph); }