A collection of sources (or physical connectors) on an audio or video device. This is used by the Capture class to provide a list of available sources on the currently selected audio and video devices. This class cannot be created directly. This class assumes there is only 1 video and 1 audio crossbar and all input pins route to a single output pin on each crossbar.
Inheritance: System.Collections.CollectionBase, IDisposable
		/// <summary> Initialize collection with property pages from existing graph. </summary>
		internal PropertyPageCollection(
			ICaptureGraphBuilder2 graphBuilder, 
			IBaseFilter videoDeviceFilter, IBaseFilter audioDeviceFilter,
			IBaseFilter videoCompressorFilter, IBaseFilter audioCompressorFilter, 
			SourceCollection videoSources, SourceCollection audioSources)
		{
			addFromGraph( graphBuilder,
				videoDeviceFilter, audioDeviceFilter,
				videoCompressorFilter, audioCompressorFilter,
				videoSources, audioSources );
		}
Beispiel #2
0
        // ------------------ Public Methods --------------------

        /// <summary> Populate the collection by looking for commonly implemented property pages. </summary>
        protected void addFromGraph(
            ICaptureGraphBuilder2 graphBuilder,
            IBaseFilter videoDeviceFilter, IBaseFilter audioDeviceFilter,
            IBaseFilter videoCompressorFilter, IBaseFilter audioCompressorFilter,
            SourceCollection videoSources, SourceCollection audioSources)
        {
            object filter = null;
            DsGuid cat;
            DsGuid med;
            Guid   iid;
            int    hr;

            Trace.Assert(graphBuilder != null);

            // 1. the video capture filter
            addIfSupported(videoDeviceFilter, "Video Capture Device");

            // 2. the video capture pin
            cat = new DsGuid(PinCategory.Capture);
            med = new DsGuid(MediaType.Interleaved);
            iid = typeof(IAMStreamConfig).GUID;
            hr  = graphBuilder.FindInterface(
                cat, med, videoDeviceFilter, iid, out filter);
            if (hr != 0)
            {
                med = MediaType.Video;
                hr  = graphBuilder.FindInterface(
                    cat, med, videoDeviceFilter, iid, out filter);
                if (hr != 0)
                {
                    filter = null;
                }
            }
            addIfSupported(filter, "Video Capture Pin");

            // 3. the video preview pin
            cat = PinCategory.Preview;
            med = MediaType.Interleaved;
            iid = typeof(IAMStreamConfig).GUID;
            hr  = graphBuilder.FindInterface(
                cat, med, videoDeviceFilter, iid, out filter);
            if (hr != 0)
            {
                med = MediaType.Video;
                hr  = graphBuilder.FindInterface(
                    cat, med, videoDeviceFilter, iid, out filter);
                if (hr != 0)
                {
                    filter = null;
                }
            }
            addIfSupported(filter, "Video Preview Pin");

            // 4. the video crossbar(s)
            ArrayList crossbars = new ArrayList();
            int       num       = 1;

            for (int c = 0; c < videoSources.Count; c++)
            {
                CrossbarSource s = videoSources[c] as CrossbarSource;
                if (s != null)
                {
                    if (crossbars.IndexOf(s.Crossbar) < 0)
                    {
                        crossbars.Add(s.Crossbar);
                        if (addIfSupported(s.Crossbar, "Video Crossbar " + (num == 1 ? "" : num.ToString())))
                        {
                            num++;
                        }
                    }
                }
            }
            crossbars.Clear();

            // 5. the video compressor
            addIfSupported(videoCompressorFilter, "Video Compressor");

            // 6. the video TV tuner
            cat = new DsGuid(PinCategory.Capture);
            med = new DsGuid(MediaType.Interleaved);
            iid = typeof(IAMTVTuner).GUID;
            hr  = graphBuilder.FindInterface(
                cat, med, videoDeviceFilter, iid, out filter);
            if (hr != 0)
            {
                med = MediaType.Video;
                hr  = graphBuilder.FindInterface(
                    cat, med, videoDeviceFilter, iid, out filter);
                if (hr != 0)
                {
                    filter = null;
                }
            }
            addIfSupported(filter, "TV Tuner");

            // 7. the video compressor (VFW)
            IAMVfwCompressDialogs compressDialog = videoCompressorFilter as IAMVfwCompressDialogs;

            if (compressDialog != null)
            {
                VfwCompressorPropertyPage page = new VfwCompressorPropertyPage("Video Compressor", compressDialog);
                InnerList.Add(page);
            }

            // 8. the audio capture filter
            addIfSupported(audioDeviceFilter, "Audio Capture Device");

            // 9. the audio capture pin
            cat = new DsGuid(PinCategory.Capture);
            med = new DsGuid(MediaType.Audio);
            iid = typeof(IAMStreamConfig).GUID;
            hr  = graphBuilder.FindInterface(
                cat, med, audioDeviceFilter, iid, out filter);
            if (hr != 0)
            {
                filter = null;
            }
            addIfSupported(filter, "Audio Capture Pin");

            // 9. the audio preview pin
            cat = new DsGuid(PinCategory.Preview);
            med = new DsGuid(MediaType.Audio);
            iid = typeof(IAMStreamConfig).GUID;
            hr  = graphBuilder.FindInterface(
                cat, med, audioDeviceFilter, iid, out filter);
            if (hr != 0)
            {
                filter = null;
            }
            addIfSupported(filter, "Audio Preview Pin");

            // 10. the audio crossbar(s)
            num = 1;
            for (int c = 0; c < audioSources.Count; c++)
            {
                CrossbarSource s = audioSources[c] as CrossbarSource;
                if (s != null)
                {
                    if (crossbars.IndexOf(s.Crossbar) < 0)
                    {
                        crossbars.Add(s.Crossbar);
                        if (addIfSupported(s.Crossbar, "Audio Crossbar " + (num == 1 ? "" : num.ToString())))
                        {
                            num++;
                        }
                    }
                }
            }
            crossbars.Clear();

            // 11. the audio compressor
            addIfSupported(audioCompressorFilter, "Audio Compressor");
        }
Beispiel #3
0
		/// <summary>
		///  Completely tear down a filter graph and 
		///  release all associated resources.
		/// </summary>
		protected void destroyGraph()
		{
			// Derender the graph (This will stop the graph
			// and release preview window. It also destroys
			// half of the graph which is unnecessary but
			// harmless here.) (ignore errors)
			try{ derenderGraph(); } catch {}

			// Update the state after derender because it
			// depends on correct status. But we also want to
			// update the state as early as possible in case
			// of error.
			graphState = GraphState.Null;
			isCaptureRendered = false;
			isPreviewRendered = false;

            // Remove graph from the ROT
#if DSHOWNET
			if ( rotCookie != 0 )
			{
				DsROT.RemoveGraphFromRot( ref rotCookie );
				rotCookie = 0;
			}
#else
            if (rotCookie != null)
            {
                rotCookie.Dispose();
                rotCookie = null;
            }
#endif

            // Remove filters from the graph
			// This should be unnecessary but the Nvidia WDM
			// video driver cannot be used by this application 
			// again unless we remove it. Ideally, we should
			// simply enumerate all the filters in the graph
			// and remove them. (ignore errors)
			if ( muxFilter != null )
				graphBuilder.RemoveFilter( muxFilter );
			if ( videoCompressorFilter != null )
				graphBuilder.RemoveFilter( videoCompressorFilter  );
			if ( audioCompressorFilter != null )
				graphBuilder.RemoveFilter( audioCompressorFilter  );
			if ( videoDeviceFilter != null )
				graphBuilder.RemoveFilter( videoDeviceFilter );
			if ( audioDeviceFilter != null )
				graphBuilder.RemoveFilter( audioDeviceFilter );

			if(this.videoRendererFilter != null)
			{
				this.graphBuilder.RemoveFilter(this.videoRendererFilter);
			}

            // Clean up properties
			if ( videoSources != null )
				videoSources.Dispose(); videoSources = null;
			if ( audioSources != null )
				audioSources.Dispose(); audioSources = null;
            this.PropertyPages = null; // Disposal done within PropertyPages
			if ( tuner != null )
				tuner.Dispose(); tuner = null;

// #if NEWCODE
			if(this.tvAudio != null)
			{
				Marshal.ReleaseComObject(this.tvAudio); tvAudio = null;
			}

			if(this.dxUtils != null)
			{
				this.dxUtils.Dispose();
				this.dxUtils = null;
			}
// #endif

			// Cleanup
			if ( graphBuilder != null )
				Marshal.ReleaseComObject( graphBuilder );  graphBuilder = null;
			if ( captureGraphBuilder != null )
				Marshal.ReleaseComObject( captureGraphBuilder ); captureGraphBuilder = null;
			if ( muxFilter != null )
				Marshal.ReleaseComObject( muxFilter ); muxFilter = null;
			if ( fileWriterFilter != null )
				Marshal.ReleaseComObject( fileWriterFilter ); fileWriterFilter = null;
			if ( videoDeviceFilter != null )
				Marshal.ReleaseComObject( videoDeviceFilter ); videoDeviceFilter = null;
			if ( audioDeviceFilter != null )
				Marshal.ReleaseComObject( audioDeviceFilter ); audioDeviceFilter = null;
			if ( videoCompressorFilter != null )
				Marshal.ReleaseComObject( videoCompressorFilter ); videoCompressorFilter = null;
			if ( audioCompressorFilter != null )
				Marshal.ReleaseComObject( audioCompressorFilter ); audioCompressorFilter = null;
// #if NEWCODE
			this.DisposeSampleGrabber();
// #endif

			if(this.videoRendererFilter != null)
			{
				Marshal.ReleaseComObject(this.videoRendererFilter); this.videoRendererFilter = null;
			}

            // These are copies of graphBuilder
			mediaControl = null;
			videoWindow = null;

			// For unmanaged objects we haven't released explicitly
			GC.Collect();
		}
Beispiel #4
0
		// --------------------- Private Methods -----------------------
		
		/// <summary> 
		///  Create a new filter graph and add filters (devices, compressors, 
		///  misc), but leave the filters unconnected. Call renderGraph()
		///  to connect the filters.
		/// </summary>
		protected void createGraph()
		{
			Guid					cat;
			Guid					med;
			int						hr;

			// Ensure required properties are set
			if ( videoDevice == null && audioDevice == null )
				throw new ArgumentException( "The video and/or audio device have not been set. Please set one or both to valid capture devices.\n" );

			// Skip if we are already created
			if ( (int)graphState < (int)GraphState.Created )
			{
				// Garbage collect, ensure that previous filters are released
				GC.Collect();

				// Make a new filter graph
#if DSHOWNET
                // Make a new filter graph
                graphBuilder = (IGraphBuilder)Activator.CreateInstance(Type.GetTypeFromCLSID(Clsid.FilterGraph, true));

                // Get the Capture Graph Builder
                Guid clsid = Clsid.CaptureGraphBuilder2;
                Guid riid = typeof(ICaptureGraphBuilder2).GUID;
                captureGraphBuilder = (ICaptureGraphBuilder2)DsBugWO.CreateDsInstance(ref clsid, ref riid);
#else
				FilterGraph graph = new FilterGraph();
				graphBuilder = (IGraphBuilder)graph;

				// Get the Capture Graph Builder
				captureGraphBuilder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
#endif

                // Link the CaptureGraphBuilder to the filter graph
                hr = captureGraphBuilder.SetFiltergraph(graphBuilder);
                if (hr < 0) Marshal.ThrowExceptionForHR(hr);

                // Add the graph to the Running Object Table so it can be
                // viewed with GraphEdit
#if DEBUG
#if DSHOWNET
				DsROT.AddGraphToRot(graphBuilder, out rotCookie);
#else
                rotCookie = new DsROTEntry(graphBuilder);
#endif
#endif

                // Get the video device and add it to the filter graph
				if ( VideoDevice != null )
				{
					videoDeviceFilter = (IBaseFilter) Marshal.BindToMoniker( VideoDevice.MonikerString );
					hr = graphBuilder.AddFilter( videoDeviceFilter, "Video Capture Device" );
					if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );
				}

				// Get the audio device and add it to the filter graph
				if ( AudioDevice != null )
				{
					audioDeviceFilter = (IBaseFilter) Marshal.BindToMoniker( AudioDevice.MonikerString );
					hr = graphBuilder.AddFilter( audioDeviceFilter, "Audio Capture Device" );
					if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );
				}

                // Get the video compressor and add it to the filter graph
				if ( VideoCompressor != null )
				{
					videoCompressorFilter = (IBaseFilter) Marshal.BindToMoniker( VideoCompressor.MonikerString ); 
					hr = graphBuilder.AddFilter( videoCompressorFilter, "Video Compressor" );
					if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );
				}

				// Get the audio compressor and add it to the filter graph
				if ( AudioCompressor != null )
				{
					audioCompressorFilter = (IBaseFilter) Marshal.BindToMoniker( AudioCompressor.MonikerString ); 
					hr = graphBuilder.AddFilter( audioCompressorFilter, "Audio Compressor" );
					if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );
				}

				// Retrieve the stream control interface for the video device
				// FindInterface will also add any required filters
				// (WDM devices in particular may need additional
				// upstream filters to function).

				// Try looking for an interleaved media type
				object o;
				cat = PinCategory.Capture;
				med = MediaType.Interleaved;
				Guid iid = typeof(IAMStreamConfig).GUID;
#if DSHOWNET
                hr = captureGraphBuilder.FindInterface(
                    ref cat, ref med, videoDeviceFilter, ref iid, out o);
#else
				hr = captureGraphBuilder.FindInterface(
					DsGuid.FromGuid(cat), DsGuid.FromGuid(med), videoDeviceFilter, DsGuid.FromGuid(iid), out o );
#endif

				if ( hr != 0 )
				{
					// If not found, try looking for a video media type
					med = MediaType.Video;
#if DSHOWNET
                    hr = captureGraphBuilder.FindInterface(
                        ref cat, ref med, videoDeviceFilter, ref iid, out o);
#else
					hr = captureGraphBuilder.FindInterface(
						DsGuid.FromGuid(cat), DsGuid.FromGuid(med), videoDeviceFilter, DsGuid.FromGuid(iid), out o );
#endif
				
					if ( hr != 0 )
						o = null;
				}
				videoStreamConfig = o as IAMStreamConfig;

// #if NEWCODE
				// Start of new Brian's Low code
				// Retrieve the stream control interface for the video device
				// FindInterface will also add any required filters
				// (WDM devices in particular may need additional
				// upstream filters to function).

				// Try looking for an interleaved media type
				o = null;
				cat = PinCategory.Preview;
				med = MediaType.Interleaved;
				iid = typeof(IAMStreamConfig).GUID;
#if DSHOWNET
				hr = captureGraphBuilder.FindInterface(
					ref cat, ref med, videoDeviceFilter, ref iid, out o);
#else
				hr = captureGraphBuilder.FindInterface(
					DsGuid.FromGuid(cat), DsGuid.FromGuid(med), videoDeviceFilter, DsGuid.FromGuid(iid), out o );
#endif

				if ( hr != 0 )
				{
					// If not found, try looking for a video media type
					med = MediaType.Video;
#if DSHOWNET
					hr = captureGraphBuilder.FindInterface(
						ref cat, ref med, videoDeviceFilter, ref iid, out o);
#else
					hr = captureGraphBuilder.FindInterface(
						DsGuid.FromGuid(cat), DsGuid.FromGuid(med), videoDeviceFilter, DsGuid.FromGuid(iid), out o );
#endif
				
					if ( hr != 0 )
						o = null;
				}
				this.previewStreamConfig = o as IAMStreamConfig;
				// End of new Brian's Low code

				if( (this.videoStreamConfig != null)||
					(this.previewStreamConfig != null) )
				{
					this.dxUtils = new DxUtils();
					bool result = this.dxUtils.InitDxUtils(this.videoDeviceFilter);

					if((!result)&&(!this.dxUtils.FindMediaData(this.videoStreamConfig)))
					{
						this.dxUtils.Dispose();
						this.dxUtils = null;
					}
				}
// #endif
				// Retrieve the stream control interface for the audio device
				o = null;
				cat = PinCategory.Capture;
				med = MediaType.Audio ;
				iid = typeof(IAMStreamConfig).GUID;
				if( (this.AudioViaPci)&&
					(audioDeviceFilter == null)&&(videoDeviceFilter != null) )
				{
                    hr = captureGraphBuilder.FindInterface(
#if DSHOWNET
						ref cat, ref med, videoDeviceFilter, ref iid, out o );
#else
                        DsGuid.FromGuid(cat), DsGuid.FromGuid(med), videoDeviceFilter, DsGuid.FromGuid(iid), out o);
#endif
				}
				else
				{
#if DSHOWNET
                    hr = captureGraphBuilder.FindInterface(
                        ref cat, ref med, audioDeviceFilter, ref iid, out o);
#else
                    hr = captureGraphBuilder.FindInterface(
	    				DsGuid.FromGuid(cat), DsGuid.FromGuid(med), audioDeviceFilter, DsGuid.FromGuid(iid), out o );
#endif
				}

				if (hr != 0)
					o = null;
				audioStreamConfig = o as IAMStreamConfig;

				// Retreive the media control interface (for starting/stopping graph)
				mediaControl = (IMediaControl) graphBuilder;

				// Reload any video crossbars
				if ( videoSources != null ) videoSources.Dispose(); videoSources = null;

				// Reload any audio crossbars
				if ( audioSources != null ) audioSources.Dispose(); audioSources = null;
				
				// Reload any property pages exposed by filters
                this.PropertyPages = null;

				// Reload capabilities of video device
				videoCaps = null;
				previewCaps = null;

				// Reload capabilities of video device
				audioCaps = null;

				// Retrieve TV Tuner if available
				o = null;
				cat = PinCategory.Capture;
				med = MediaType.Interleaved; 
				iid = typeof(IAMTVTuner).GUID;
#if DSHOWNET
                hr = captureGraphBuilder.FindInterface(
                    ref cat, ref med, videoDeviceFilter, ref iid, out o);
#else

				hr = captureGraphBuilder.FindInterface( 
					DsGuid.FromGuid(cat), DsGuid.FromGuid(med), videoDeviceFilter, DsGuid.FromGuid(iid), out o );
#endif
				if ( hr != 0 )
				{
					med = MediaType.Video ;
#if DSHOWNET
                    hr = captureGraphBuilder.FindInterface(
                        ref cat, ref med, videoDeviceFilter, ref iid, out o);
#else
					hr = captureGraphBuilder.FindInterface( 
						DsGuid.FromGuid(cat), DsGuid.FromGuid(med), videoDeviceFilter, DsGuid.FromGuid(iid), out o );
#endif
					if ( hr != 0 )
						o = null;
				}
				IAMTVTuner t = o as IAMTVTuner;
				if ( t != null )
				{
					tuner = new Tuner(t);
					// Do not forget to set proper country code (Netherlands is 31)
				}

				// No check on TV Audio needed, it will show up in the
				// PropertyPages when it is available
				// Code for finding the TV audio interface
				o = null;
				cat = PinCategory.Capture;
				med = MediaType.Interleaved;
				iid = typeof(IAMTVAudio).GUID;
				hr = captureGraphBuilder.FindInterface(
#if DSHOWNET
					ref cat, ref med, videoDeviceFilter, ref iid, out o);
#else
                    cat, med, videoDeviceFilter, iid, out o);
#endif
				if ( hr != 0 )
				{
					med = MediaType.Video;
#if DSHOWNET
					hr = captureGraphBuilder.FindInterface(
						ref cat, ref med, videoDeviceFilter, ref iid, out o);
#else
				hr = captureGraphBuilder.FindInterface(
					cat, med, videoDeviceFilter, iid, out o);
#endif
					if ( hr != 0 )
					{
						o = null;
					}
				}

				if((o != null)&&(tuner != null))
				{
					IAMTVAudio a = o as IAMTVAudio;
					TvAudio = a;
#if DEBUG
					Debug.WriteLine("FindInterface tuner.TvAudio");
#endif // DEBUG
				}

				/*
							// ----------- VMR 9 -------------------
							//## check out samples\inc\vmrutil.h :: RenderFileToVMR9

							IBaseFilter vmr = null;
							if ( ( VideoDevice != null ) && ( previewWindow != null ) )
							{
								vmr = (IBaseFilter) Activator.CreateInstance( Type.GetTypeFromCLSID( Clsid.VideoMixingRenderer9, true ) ); 
								hr = graphBuilder.AddFilter( vmr, "VMR" );
								if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

								IVMRFilterConfig9 vmrFilterConfig = (IVMRFilterConfig9) vmr;
								hr = vmrFilterConfig.SetRenderingMode( VMRMode9.Windowless );
								if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

								IVMRWindowlessControl9 vmrWindowsless = (IVMRWindowlessControl9) vmr;	
								hr = vmrWindowsless.SetVideoClippingWindow( previewWindow.Handle );
								if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );
							}
							//------------------------------------------- 

							// ---------- SmartTee ---------------------

							IBaseFilter smartTeeFilter = (IBaseFilter) Activator.CreateInstance( Type.GetTypeFromCLSID( Clsid.SmartTee, true ) ); 
							hr = graphBuilder.AddFilter( smartTeeFilter, "Video Smart Tee" );
							if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

							// Video -> SmartTee
							cat = PinCategory.Capture;
							med = MediaType.Video;
							hr = captureGraphBuilder.RenderStream( DsGuid.FromGuid(cat), DsGuid.FromGuid(med), videoDeviceFilter, null, smartTeeFilter ); 
							if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

							// smarttee -> mux
							cat = PinCategory.Capture;
							med = MediaType.Video;
							hr = captureGraphBuilder.RenderStream( DsGuid.FromGuid(cat), DsGuid.FromGuid(med), smartTeeFilter, null, muxFilter ); 
							if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

							// smarttee -> vmr
							cat = PinCategory.Preview;
							med = MediaType.Video;
							hr = captureGraphBuilder.RenderStream( DsGuid.FromGuid(cat), DsGuid.FromGuid(med), smartTeeFilter, null, vmr ); 
							if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

							// -------------------------------------
				*/		
				// Update the state now that we are done
				graphState = GraphState.Created;
			}
		}
Beispiel #5
0
		/// <summary> 
		///  Calls Stop, releases all references. If a capture is in progress
		///  it will be stopped, but the CaptureComplete event will NOT fire.
		/// </summary>
		public void Dispose()
		{
			wantPreviewRendered = false;
			wantCaptureRendered = false;
			CaptureComplete = null;

			try { destroyGraph(); } catch {}

			if ( videoSources != null )
				videoSources.Dispose(); videoSources = null;
			if ( audioSources != null )
				audioSources.Dispose(); audioSources = null;

		}
Beispiel #6
0
 protected void destroyGraph()
 {
     try
     {
         this.derenderGraph();
     }
     catch
     {
     }
     this.graphState = GraphState.Null;
     this.isCaptureRendered = false;
     this.isPreviewRendered = false;
     if (this.rotCookie != 0)
     {
         DsROT.RemoveGraphFromRot(ref this.rotCookie);
         this.rotCookie = 0;
     }
     if (this.muxFilter != null)
     {
         this.graphBuilder.RemoveFilter(this.muxFilter);
     }
     if (this.videoCompressorFilter != null)
     {
         this.graphBuilder.RemoveFilter(this.videoCompressorFilter);
     }
     if (this.audioCompressorFilter != null)
     {
         this.graphBuilder.RemoveFilter(this.audioCompressorFilter);
     }
     if (this.videoDeviceFilter != null)
     {
         this.graphBuilder.RemoveFilter(this.videoDeviceFilter);
     }
     if (this.audioDeviceFilter != null)
     {
         this.graphBuilder.RemoveFilter(this.audioDeviceFilter);
     }
     if (this.videoSources != null)
     {
         this.videoSources.Dispose();
     }
     this.videoSources = null;
     if (this.audioSources != null)
     {
         this.audioSources.Dispose();
     }
     this.audioSources = null;
     if (this.propertyPages != null)
     {
         this.propertyPages.Dispose();
     }
     this.propertyPages = null;
     if (this.tuner != null)
     {
         this.tuner.Dispose();
     }
     this.tuner = null;
     if (this.graphBuilder != null)
     {
         Marshal.ReleaseComObject(this.graphBuilder);
     }
     this.graphBuilder = null;
     if (this.captureGraphBuilder != null)
     {
         Marshal.ReleaseComObject(this.captureGraphBuilder);
     }
     this.captureGraphBuilder = null;
     if (this.muxFilter != null)
     {
         Marshal.ReleaseComObject(this.muxFilter);
     }
     this.muxFilter = null;
     if (this.fileWriterFilter != null)
     {
         Marshal.ReleaseComObject(this.fileWriterFilter);
     }
     this.fileWriterFilter = null;
     if (this.videoDeviceFilter != null)
     {
         Marshal.ReleaseComObject(this.videoDeviceFilter);
     }
     this.videoDeviceFilter = null;
     if (this.audioDeviceFilter != null)
     {
         Marshal.ReleaseComObject(this.audioDeviceFilter);
     }
     this.audioDeviceFilter = null;
     if (this.videoCompressorFilter != null)
     {
         Marshal.ReleaseComObject(this.videoCompressorFilter);
     }
     this.videoCompressorFilter = null;
     if (this.audioCompressorFilter != null)
     {
         Marshal.ReleaseComObject(this.audioCompressorFilter);
     }
     this.audioCompressorFilter = null;
     this.mediaControl = null;
     this.videoWindow = null;
     GC.Collect();
 }
Beispiel #7
0
 protected void createGraph()
 {
     if ((this.videoDevice == null) && (this.audioDevice == null))
     {
         throw new ArgumentException("The video and/or audio device have not been set. Please set one or both to valid capture devices.\n");
     }
     if (this.graphState < GraphState.Created)
     {
         object obj2;
         GC.Collect();
         this.graphBuilder = (IGraphBuilder) Activator.CreateInstance(System.Type.GetTypeFromCLSID(Clsid.FilterGraph, true));
         Guid clsid = Clsid.CaptureGraphBuilder2;
         Guid gUID = typeof(ICaptureGraphBuilder2).GUID;
         this.captureGraphBuilder = (ICaptureGraphBuilder2) DsBugWO.CreateDsInstance(ref clsid, ref gUID);
         int errorCode = this.captureGraphBuilder.SetFiltergraph(this.graphBuilder);
         if (errorCode < 0)
         {
             Marshal.ThrowExceptionForHR(errorCode);
         }
         if (this.VideoDevice != null)
         {
             this.videoDeviceFilter = (IBaseFilter) Marshal.BindToMoniker(this.VideoDevice.MonikerString);
             errorCode = this.graphBuilder.AddFilter(this.videoDeviceFilter, "Video Capture Device");
             if (errorCode < 0)
             {
                 Marshal.ThrowExceptionForHR(errorCode);
             }
         }
         if (this.AudioDevice != null)
         {
             this.audioDeviceFilter = (IBaseFilter) Marshal.BindToMoniker(this.AudioDevice.MonikerString);
             errorCode = this.graphBuilder.AddFilter(this.audioDeviceFilter, "Audio Capture Device");
             if (errorCode < 0)
             {
                 Marshal.ThrowExceptionForHR(errorCode);
             }
         }
         if (this.VideoCompressor != null)
         {
             this.videoCompressorFilter = (IBaseFilter) Marshal.BindToMoniker(this.VideoCompressor.MonikerString);
             errorCode = this.graphBuilder.AddFilter(this.videoCompressorFilter, "Video Compressor");
             if (errorCode < 0)
             {
                 Marshal.ThrowExceptionForHR(errorCode);
             }
         }
         if (this.AudioCompressor != null)
         {
             this.audioCompressorFilter = (IBaseFilter) Marshal.BindToMoniker(this.AudioCompressor.MonikerString);
             errorCode = this.graphBuilder.AddFilter(this.audioCompressorFilter, "Audio Compressor");
             if (errorCode < 0)
             {
                 Marshal.ThrowExceptionForHR(errorCode);
             }
         }
         Guid capture = PinCategory.Capture;
         Guid interleaved = MediaType.Interleaved;
         Guid riid = typeof(IAMStreamConfig).GUID;
         if (this.captureGraphBuilder.FindInterface(ref capture, ref interleaved, this.videoDeviceFilter, ref riid, out obj2) != 0)
         {
             interleaved = MediaType.Video;
             if (this.captureGraphBuilder.FindInterface(ref capture, ref interleaved, this.videoDeviceFilter, ref riid, out obj2) != 0)
             {
                 obj2 = null;
             }
         }
         this.videoStreamConfig = obj2 as IAMStreamConfig;
         obj2 = null;
         capture = PinCategory.Capture;
         interleaved = MediaType.Audio;
         riid = typeof(IAMStreamConfig).GUID;
         if (this.captureGraphBuilder.FindInterface(ref capture, ref interleaved, this.audioDeviceFilter, ref riid, out obj2) != 0)
         {
             obj2 = null;
         }
         this.audioStreamConfig = obj2 as IAMStreamConfig;
         this.mediaControl = (IMediaControl) this.graphBuilder;
         if (this.videoSources != null)
         {
             this.videoSources.Dispose();
         }
         this.videoSources = null;
         if (this.audioSources != null)
         {
             this.audioSources.Dispose();
         }
         this.audioSources = null;
         if (this.propertyPages != null)
         {
             this.propertyPages.Dispose();
         }
         this.propertyPages = null;
         this.videoCaps = null;
         this.audioCaps = null;
         obj2 = null;
         capture = PinCategory.Capture;
         interleaved = MediaType.Interleaved;
         riid = typeof(IAMTVTuner).GUID;
         if (this.captureGraphBuilder.FindInterface(ref capture, ref interleaved, this.videoDeviceFilter, ref riid, out obj2) != 0)
         {
             interleaved = MediaType.Video;
             if (this.captureGraphBuilder.FindInterface(ref capture, ref interleaved, this.videoDeviceFilter, ref riid, out obj2) != 0)
             {
                 obj2 = null;
             }
         }
         IAMTVTuner tuner = obj2 as IAMTVTuner;
         if (tuner != null)
         {
             this.tuner = new DirectX.Capture.Tuner(tuner);
         }
         this.graphState = GraphState.Created;
     }
 }
Beispiel #8
0
 public void Dispose()
 {
     this.wantPreviewRendered = false;
     this.wantCaptureRendered = false;
     this.CaptureComplete = null;
     try
     {
         this.destroyGraph();
     }
     catch
     {
     }
     if (this.videoSources != null)
     {
         this.videoSources.Dispose();
     }
     this.videoSources = null;
     if (this.audioSources != null)
     {
         this.audioSources.Dispose();
     }
     this.audioSources = null;
 }
Beispiel #9
0
		// --------------------- Private Methods -----------------------
		
		/// <summary> 
		///  Create a new filter graph and add filters (devices, compressors, 
		///  misc), but leave the filters unconnected. Call renderGraph()
		///  to connect the filters.
		/// </summary>
		protected void createGraph()
		{
			Guid					cat;
			Guid					med;
			int						hr;
			Type comType = null;
			object comObj = null;

			// Ensure required properties are set
			if ( videoDevice == null && audioDevice == null )
				throw new ArgumentException( "The video and/or audio device have not been set. Please set one or both to valid capture devices.\n" );

			// Skip if we are already created
			if ( (int)graphState < (int)GraphState.Created )
			{
				// Garbage collect, ensure that previous filters are released
				GC.Collect();

				// Make a new filter graph
				graphBuilder = (IGraphBuilder) Activator.CreateInstance( Type.GetTypeFromCLSID( Clsid.FilterGraph, true ) ); 

				// Get the Capture Graph Builder
				Guid clsid = Clsid.CaptureGraphBuilder2;
				Guid riid = typeof(ICaptureGraphBuilder2).GUID;
				captureGraphBuilder = (ICaptureGraphBuilder2) DsBugWO.CreateDsInstance( ref clsid, ref riid ); 

				// sampGrabber, ISampleGrabber to capture frames
				comType=Type.GetTypeFromCLSID( Clsid.SampleGrabber, true );
				if(comType==null)
					throw new NotImplementedException (@"DirectShow SampleGrabber not installed/registered");
				comObj=Activator.CreateInstance( comType );
				sampGrabber = (ISampleGrabber) comObj; comObj = null;

				// Link the CaptureGraphBuilder to the filter graph
				hr = captureGraphBuilder.SetFiltergraph( graphBuilder );
				//if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

				// Begin set up of SampGrabber	<<<<<<----------------------------------------------------
				AMMediaType media = new AMMediaType();
				media.majorType= MediaType.Video;
				media.subType = MediaSubType.RGB24;
				media.formatType = FormatType.VideoInfo;
				hr = sampGrabber.SetMediaType( media );
				//if( hr<0 ) Marshal.ThrowExceptionForHR( hr );
				// Finish set up of SampGrabber	<<<<<<----------------------------------------------------
			
				// Add the graph to the Running Object Table so it can be
				// viewed with GraphEdit
				#if DEBUG
				DsROT.AddGraphToRot( graphBuilder, out rotCookie );
				#endif

				// Get the video device and add it to the filter graph
				if ( VideoDevice != null )
				{
					videoDeviceFilter = (IBaseFilter) Marshal.BindToMoniker( VideoDevice.MonikerString );
					hr = graphBuilder.AddFilter( videoDeviceFilter, "Video Capture Device" );
					//if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

					// Add SampGrabber Filter	<<<<<<----------------------------------------------------
					mediaEvt	= (IMediaEventEx)	graphBuilder;
					baseGrabFlt = (IBaseFilter) sampGrabber;
					hr = graphBuilder.AddFilter( baseGrabFlt, "DS.NET Grabber" );
					//if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

				}

				// Get the audio device and add it to the filter graph
				if ( AudioDevice != null )
				{
					audioDeviceFilter = (IBaseFilter) Marshal.BindToMoniker( AudioDevice.MonikerString );
					hr = graphBuilder.AddFilter( audioDeviceFilter, "Audio Capture Device" );
					//if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );
				}

				// Get the video compressor and add it to the filter graph
				if ( VideoCompressor != null )
				{
					videoCompressorFilter = (IBaseFilter) Marshal.BindToMoniker( VideoCompressor.MonikerString ); 
					hr = graphBuilder.AddFilter( videoCompressorFilter, "Video Compressor" );
					//if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );
				}

				// Get the audio compressor and add it to the filter graph
				if ( AudioCompressor != null )
				{
					audioCompressorFilter = (IBaseFilter) Marshal.BindToMoniker( AudioCompressor.MonikerString ); 
					hr = graphBuilder.AddFilter( audioCompressorFilter, "Audio Compressor" );
					//if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );
				}
				
				// Retrieve the stream control interface for the video device
				// FindInterface will also add any required filters
				// (WDM devices in particular may need additional
				// upstream filters to function).

				// Try looking for an interleaved media type
				object o;
				cat = PinCategory.Capture;
				med = MediaType.Interleaved;
				Guid iid = typeof(IAMStreamConfig).GUID;
				hr = captureGraphBuilder.FindInterface(
					ref cat, ref med, videoDeviceFilter, ref iid, out o );

				if ( hr != 0 )
				{
					// If not found, try looking for a video media type
					med = MediaType.Video;
					hr = captureGraphBuilder.FindInterface(
						ref cat, ref med, videoDeviceFilter, ref iid, out o );
				
					if ( hr != 0 )
						o = null;
				}
				videoStreamConfig = o as IAMStreamConfig;

				// Retrieve the stream control interface for the audio device
				o = null;
				cat = PinCategory.Capture;
				med = MediaType.Audio ;
				iid = typeof(IAMStreamConfig).GUID;
				hr = captureGraphBuilder.FindInterface(
					ref cat, ref med, audioDeviceFilter, ref iid, out o );
				if ( hr != 0 )
					o = null;
				audioStreamConfig = o as IAMStreamConfig;

				// Retreive the media control interface (for starting/stopping graph)
				mediaControl = (IMediaControl) graphBuilder;

				// Reload any video crossbars
				if ( videoSources != null ) videoSources.Dispose(); videoSources = null;

				// Reload any audio crossbars
				if ( audioSources != null ) audioSources.Dispose(); audioSources = null;
				
				// Reload any property pages exposed by filters
				if ( propertyPages != null ) propertyPages.Dispose(); propertyPages = null;

				// Reload capabilities of video device
				videoCaps = null;

				// Reload capabilities of video device
				audioCaps = null;

				// Retrieve TV Tuner if available
				o = null;
				cat = PinCategory.Capture;
				med = MediaType.Interleaved; 
				iid = typeof(IAMTVTuner).GUID;
				hr = captureGraphBuilder.FindInterface( 
					ref cat, ref med, videoDeviceFilter, ref iid, out o );
				if ( hr != 0 )
				{
					med = MediaType.Video ;
					hr = captureGraphBuilder.FindInterface( 
						ref cat, ref med, videoDeviceFilter, ref iid, out o );
					if ( hr != 0 )
						o = null;
				}
				IAMTVTuner t = o as IAMTVTuner;
				if ( t != null )
					tuner = new Tuner( t );


				/*
							// ----------- VMR 9 -------------------
							//## check out samples\inc\vmrutil.h :: RenderFileToVMR9

							IBaseFilter vmr = null;
							if ( ( VideoDevice != null ) && ( previewWindow != null ) )
							{
								vmr = (IBaseFilter) Activator.CreateInstance( Type.GetTypeFromCLSID( Clsid.VideoMixingRenderer9, true ) ); 
								hr = graphBuilder.AddFilter( vmr, "VMR" );
								if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

								IVMRFilterConfig9 vmrFilterConfig = (IVMRFilterConfig9) vmr;
								hr = vmrFilterConfig.SetRenderingMode( VMRMode9.Windowless );
								if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

								IVMRWindowlessControl9 vmrWindowsless = (IVMRWindowlessControl9) vmr;	
								hr = vmrWindowsless.SetVideoClippingWindow( previewWindow.Handle );
								if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );
							}
							//------------------------------------------- 

							// ---------- SmartTee ---------------------

							IBaseFilter smartTeeFilter = (IBaseFilter) Activator.CreateInstance( Type.GetTypeFromCLSID( Clsid.SmartTee, true ) ); 
							hr = graphBuilder.AddFilter( smartTeeFilter, "Video Smart Tee" );
							if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

							// Video -> SmartTee
							cat = PinCategory.Capture;
							med = MediaType.Video;
							hr = captureGraphBuilder.RenderStream( ref cat, ref med, videoDeviceFilter, null, smartTeeFilter ); 
							if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

							// smarttee -> mux
							cat = PinCategory.Capture;
							med = MediaType.Video;
							hr = captureGraphBuilder.RenderStream( ref cat, ref med, smartTeeFilter, null, muxFilter ); 
							if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

							// smarttee -> vmr
							cat = PinCategory.Preview;
							med = MediaType.Video;
							hr = captureGraphBuilder.RenderStream( ref cat, ref med, smartTeeFilter, null, vmr ); 
							if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

							// -------------------------------------
				*/

				// Update the state now that we are done
				graphState = GraphState.Created;
			}
		}
		// ------------------ Public Methods --------------------

		/// <summary> Populate the collection by looking for commonly implemented property pages. </summary>
		protected void addFromGraph(
			ICaptureGraphBuilder2 graphBuilder,
			IBaseFilter videoDeviceFilter, IBaseFilter audioDeviceFilter,
			IBaseFilter videoCompressorFilter, IBaseFilter audioCompressorFilter, 
			SourceCollection videoSources, SourceCollection audioSources)
		{
			object filter = null;
			Guid cat;
			Guid med;
			Guid iid;
			int hr;

			Trace.Assert( graphBuilder != null );

			// 1. the video capture filter
			addIfSupported( videoDeviceFilter, "Video Capture Device" );

			// 2. the video capture pin
			cat = PinCategory.Capture;
			med = MediaType.Interleaved; 
			iid = typeof(IAMStreamConfig).GUID;
#if DSHOWNET
			hr = graphBuilder.FindInterface( 
				ref cat, ref med, videoDeviceFilter, ref iid, out filter );
#else
            hr = graphBuilder.FindInterface(cat, med, videoDeviceFilter, iid, out filter);
#endif
			if ( hr != 0 )
			{
				med = MediaType.Video ;
#if DSHOWNET
				hr = graphBuilder.FindInterface( 
					ref cat, ref med, videoDeviceFilter, ref iid, out filter );
#else
                hr = graphBuilder.FindInterface(cat, med, videoDeviceFilter, iid, out filter);
#endif
				if ( hr != 0 )
					filter = null;
			}
			addIfSupported( filter, "Video Capture Pin" );

			// 3. the video preview pin
			cat = PinCategory.Preview;
			med = MediaType.Interleaved; 
			iid = typeof(IAMStreamConfig).GUID;
#if DSHOWNET
			hr = graphBuilder.FindInterface( 
				ref cat, ref med, videoDeviceFilter, ref iid, out filter );
#else
            hr = graphBuilder.FindInterface(cat, med, videoDeviceFilter, iid, out filter);
#endif
			if ( hr != 0 )
			{
				med = MediaType.Video ;
#if DSHOWNET
				hr = graphBuilder.FindInterface( 
					ref cat, ref med, videoDeviceFilter, ref iid, out filter );
#else
                hr = graphBuilder.FindInterface(cat, med, videoDeviceFilter, iid, out filter);
#endif
				if ( hr != 0 )
					filter = null;
			}
			addIfSupported( filter, "Video Preview Pin" );

			// 4. the video crossbar(s)
			ArrayList crossbars = new ArrayList();
			int num = 1;
			for ( int c = 0; c < videoSources.Count; c++ )
			{
				CrossbarSource s = videoSources[c] as CrossbarSource;
				if ( s != null )
				{
					if ( crossbars.IndexOf( s.Crossbar ) < 0 )
					{
						crossbars.Add( s.Crossbar );
						if ( addIfSupported( s.Crossbar, "Video Crossbar " + ( num==1 ? "" : num.ToString() ) ) )
							num++;
					}
				}
			}
			crossbars.Clear();

			// 5. the video compressor
			addIfSupported( videoCompressorFilter, "Video Compressor" );

			// 6. the video TV tuner
			cat = PinCategory.Capture;
			med = MediaType.Interleaved; 
			iid = typeof(IAMTVTuner).GUID;
#if DSHOWNET 
			hr = graphBuilder.FindInterface(
				ref cat, ref med, videoDeviceFilter, ref iid, out filter );
#else
            hr = graphBuilder.FindInterface(cat, med, videoDeviceFilter, iid, out filter);
#endif
			if ( hr != 0 )
			{
				med = MediaType.Video ;
#if DSHOWNET
				hr = graphBuilder.FindInterface( 
					ref cat, ref med, videoDeviceFilter, ref iid, out filter );
#else
            hr = graphBuilder.FindInterface(cat, med, videoDeviceFilter, iid, out filter);
#endif
				if ( hr != 0 )
					filter = null;
			}
			addIfSupported( filter, "TV Tuner" );

//#if NEWCODE
			cat = PinCategory.Capture;
			med = MediaType.Interleaved; 
#if DSHOWNET
			iid = typeof(DirectX.Capture.Capture.IAMTVAudio).GUID;
			hr = graphBuilder.FindInterface( 
				ref cat, ref med, videoDeviceFilter, ref iid, out filter );
#else
			iid = typeof(IAMTVAudio).GUID;
			hr = graphBuilder.FindInterface(cat, med, videoDeviceFilter, iid, out filter);
#endif
			if ( hr != 0 )
			{
				med = MediaType.Video ;
#if DSHOWNET
				hr = graphBuilder.FindInterface( 
					ref cat, ref med, videoDeviceFilter, ref iid, out filter );
#else
                hr = graphBuilder.FindInterface(cat, med, videoDeviceFilter, iid, out filter);
#endif
				if ( hr != 0 )
				{
					filter = null;
				}
			}
			addIfSupported( filter, "TV Audio" );
//#endif

			// 7. the video compressor (VFW)
			IAMVfwCompressDialogs compressDialog = videoCompressorFilter as IAMVfwCompressDialogs;
			if ( compressDialog != null )
			{
				VfwCompressorPropertyPage page = new VfwCompressorPropertyPage( "Video Compressor", compressDialog );
				InnerList.Add( page );
			}

			// 8. the audio capture filter
			addIfSupported( audioDeviceFilter, "Audio Capture Device" );

			// 9. the audio capture pin
			cat = PinCategory.Capture;
			med = MediaType.Audio; 
			iid = typeof(IAMStreamConfig).GUID;
#if DSHOWNET
			hr = graphBuilder.FindInterface( 
				ref cat, ref med, audioDeviceFilter, ref iid, out filter );
#else
            hr = graphBuilder.FindInterface(cat, med, audioDeviceFilter, iid, out filter);
#endif
			if ( hr != 0 )
			{
				filter = null;
			}
			addIfSupported( filter, "Audio Capture Pin" );

			// 9. the audio preview pin
			cat = PinCategory.Preview;
			med = MediaType.Audio; 
			iid = typeof(IAMStreamConfig).GUID;
#if DSHOWNET
			hr = graphBuilder.FindInterface( 
				ref cat, ref med, audioDeviceFilter, ref iid, out filter );
#else
            hr = graphBuilder.FindInterface(cat, med, audioDeviceFilter, iid, out filter);
#endif
			if ( hr != 0 )
			{
				filter = null;
			}
			addIfSupported( filter, "Audio Preview Pin" );

			// 10. the audio crossbar(s)
			num = 1;
			for ( int c = 0; c < audioSources.Count; c++ )
			{
				CrossbarSource s = audioSources[c] as CrossbarSource;
				if ( s != null )
				{
					if ( crossbars.IndexOf( s.Crossbar ) < 0 )
					{
						crossbars.Add( s.Crossbar );
						if ( addIfSupported( s.Crossbar, "Audio Crossbar " + ( num==1 ? "" : num.ToString() ) ) )
							num++;
					}
				}
			}
			crossbars.Clear();

			// 11. the audio compressor
			addIfSupported( audioCompressorFilter, "Audio Compressor" );

		}