private void TestLatency() { int hr; hr = m_igs.SyncUsingStreamOffset(true); DsError.ThrowExceptionForHR(hr); hr = m_igs.SetMaxGraphLatency(100); Debug.Assert(hr == 0, "SetMaxGraphLatency2"); hr = m_igs.SyncUsingStreamOffset(false); DsError.ThrowExceptionForHR(hr); hr = m_igs.SetMaxGraphLatency(100); Debug.Assert(hr < 0, "SetMaxGraphLatency1"); }
public void CaptureVideo() { int hr = 0; try { int width = INI.Default["DirectShow Player"]["Video Player/Device Source Width", "352"].Integer; int Height = INI.Default["DirectShow Player"]["Video Player/Device Source Height", "240"].Integer; int fps = INI.Default["DirectShow Player"]["Video Player/Frames Per Second (0 unlimited)", "30"].Integer; bool antenna_input = INI.Default[Options.ProgramName]["Video Player/Capture Tuner from antenna", "true"].Bool; bool capture_TV = INI.Default[Options.ProgramName]["Video Player/Capture Tuner", "true"].Bool; bool capture_is_audio = INI.Default[Options.ProgramName]["Video Player/Capture Video is Audio also", "true"].Bool; IPin cap_pin = null; IPin cap_audio_pin; crossbar_to_tuner = capture_TV; // Get DirectShow interfaces GetInterfaces(); // Attach the filter graph to the capture graph hr = this.captureGraphBuilder.SetFiltergraph(this.graphBuilder); DsError.ThrowExceptionForHR(hr); // Use the system device enumerator and class enumerator to find // a video capture/preview device, such as a desktop USB video camera. sourceFilter = this.DeviceFinder.FindVideoCaptureDevice(false); if (sourceFilter == null) { Network.SendStatus(channel, volume, false); return; } #if use_bda bda_filter = FindVideoCaptureDevice(true); #endif if (!capture_is_audio) { sourceAudioFilter = this.DeviceFinder.FindAudioCaptureDevice(); } else { sourceAudioFilter = sourceFilter; } //reclock_video_filter = FindVideoRenderDevice(); //scale_filter = FindVideoScaleDevice(); reclock_filter = this.DeviceFinder.FindAudioRenderDevice(); IAMAnalogVideoDecoder decoder = sourceFilter as IAMAnalogVideoDecoder; if (decoder != null) { AnalogVideoStandard oldStandard; decoder.get_TVFormat(out oldStandard); if (oldStandard != AnalogVideoStandard.NTSC_M) { decoder.put_TVFormat(AnalogVideoStandard.NTSC_M); } decoder = null; } // this is really for which input - the tuner we shouldn't adjust //if( !capture_TV ) // Add Capture filter to our graph. hr = this.graphBuilder.AddFilter(sourceFilter, "Video Capture"); DsError.ThrowExceptionForHR(hr); if (scale_filter != null) { hr = this.graphBuilder.AddFilter(scale_filter, "Video Scaler"); DsError.ThrowExceptionForHR(hr); } this.graphBuilder.Connect(null, null); #if use_bda if (bda_filter != null) { hr = this.graphBuilder.AddFilter(bda_filter, "Video Tuner"); DsError.ThrowExceptionForHR(hr); } #endif if (capture_TV && !capture_is_audio) { if (sourceAudioFilter != null) { hr = this.graphBuilder.AddFilter(sourceAudioFilter, "Audio Capture"); DsError.ThrowExceptionForHR(hr); } } if (reclock_filter != null) { Log.log("Adding 'reclock' which is the audio output device?"); hr = this.graphBuilder.AddFilter(reclock_filter, "Audio Renderer"); DsError.ThrowExceptionForHR(hr); } //this.graphBuilder.AddFilter( AdjustCrossbarPin(); bool cap_is_preview; { // set the video input size on the preview pin. cap_audio_pin = DsFindPin.ByCategory((IBaseFilter)sourceAudioFilter, PinCategory.Preview, 0); cap_pin = DsFindPin.ByCategory((IBaseFilter)sourceFilter, PinCategory.Preview, 0); if (cap_pin == null) { cap_is_preview = false; cap_audio_pin = DsFindPin.ByCategory((IBaseFilter)sourceAudioFilter, PinCategory.Capture, 0); cap_pin = DsFindPin.ByCategory((IBaseFilter)sourceFilter, PinCategory.Capture, 0); } else { cap_is_preview = true; } //Log.log( "Cap pin + " + cap_pin ); } // Render the preview pin on the video capture filter // Use this instead of this.graphBuilder.RenderFile if (cap_is_preview) { //hr = this.captureGraphBuilder.RenderStream( PinCategory.Preview, MediaType.Video, scale_filter, null, null ); //DsError.ThrowExceptionForHR( hr ); hr = this.captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, sourceFilter, null, null); DsError.ThrowExceptionForHR(hr); if (sourceAudioFilter != null) { hr = this.captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Audio, sourceAudioFilter, null, reclock_filter); DsError.ThrowExceptionForHR(hr); } } else { //hr = this.captureGraphBuilder.RenderStream( PinCategory.Capture, MediaType.Video, scale_filter, null, null ); //DsError.ThrowExceptionForHR( hr ); hr = this.captureGraphBuilder.RenderStream(PinCategory.Capture, MediaType.Video, sourceFilter, null, null); DsError.ThrowExceptionForHR(hr); if (sourceAudioFilter != null) { //IBaseFilter renderer = null; //Log.log( "reclock is " + reclock_filter ); hr = this.captureGraphBuilder.RenderStream(PinCategory.Capture, MediaType.Audio, sourceAudioFilter, null, reclock_filter); if (hr != 0) { Log.log("Bad audio stream"); } //DsError.ThrowExceptionForHR( hr ); } } IAMStreamConfig stream = cap_pin as IAMStreamConfig; if (stream != null) { // 352x240 AMMediaType media; VideoInfoHeader vih = new VideoInfoHeader(); stream.GetFormat(out media); Marshal.PtrToStructure(media.formatPtr, vih); //vih.BmiHeader.Width = width; //vih.BmiHeader.Height = Height; if (fps > 0) { vih.AvgTimePerFrame = (10000000L / fps); } //Log.log( "set the bitmap override..." ); Marshal.StructureToPtr(vih, media.formatPtr, false); hr = stream.SetFormat(media); if (hr != 0) { Log.log("Failed to set format (preview)." + hr); } } else { Log.log("Failed to get stream config from source filter"); } //graph_filter.SetSyncSource( ref_clock ); object o; hr = captureGraphBuilder.FindInterface(null, null, sourceFilter, typeof(IReferenceClock).GUID, out o); if (hr == 0) { ref_clock = (IReferenceClock)o; } if (ref_clock == null) { hr = captureGraphBuilder.FindInterface(null, null, sourceAudioFilter, typeof(IReferenceClock).GUID, out o); ref_clock = (IReferenceClock)o; } hr = captureGraphBuilder.FindInterface(null, null, sourceFilter, typeof(IAMTVTuner).GUID, out o); //graphBuilder.sa. if (hr >= 0) { tuner = (IAMTVTuner)o; o = null; } if (tuner != null) { if (antenna_input) { TunerInputType type; hr = tuner.get_InputType(0, out type); if (type != TunerInputType.Antenna) { tuner.put_InputType(0, TunerInputType.Antenna); hr = tuner.get_InputType(0, out type); } } else { if (tuner != null) { TunerInputType type; hr = tuner.get_InputType(0, out type); if (type != TunerInputType.Cable) { tuner.put_InputType(0, TunerInputType.Cable); hr = tuner.get_InputType(0, out type); } } } tuner.ChannelMinMax(out min_channel, out max_channel); min_channel = INI.Default["DirectShow Player"]["Video Player/Minimum Channel", min_channel.ToString()].Integer; max_channel = INI.Default["DirectShow Player"]["Video Player/Maximum Channel", max_channel.ToString()].Integer; } // Now that the filter has been added to the graph and we have // rendered its stream, we can release this reference to the filter. if (sourceAudioFilter != null) { //hr = captureGraphBuilder.FindInterface( null, null, sourceFilter, typeof( IAMTVAudio ).GUID, out o ); hr = captureGraphBuilder.FindInterface(null, null, sourceAudioFilter, typeof(IBasicAudio).GUID, out o); if (hr >= 0) { audio_mixer = (IBasicAudio)o; o = null; } } Marshal.ReleaseComObject(sourceFilter); if (audio_mixer != null) { audio_mixer.get_Volume(out volume); } if (tuner != null) { tuner.get_Channel(out channel, out sub_channel, out sub_channel2); } //this.graphBuilder.SetDefaultSyncSource(); if (ref_clock != null) { this.graph_filter.SetSyncSource(ref_clock); } graph_streams.SyncUsingStreamOffset(true); // Set video window style and position SetupVideoWindow(); // Add our graph to the running object table, which will allow // the GraphEdit application to "spy" on our graph rot = new DsROTEntry(this.graphBuilder); //this.mediaControl.set // Start previewing video data hr = this.mediaControl.Run(); DsError.ThrowExceptionForHR(hr); // Remember current state this.currentState = PlayState.Running; Network.SendStatus(channel, volume, (this.currentState == PlayState.Running)); } catch (Exception e) { MessageBox.Show("An unrecoverable error has occurred : " + e.Message); this.DialogResult = DialogResult.Abort; this.Close(); Application.Exit(); } }