Example #1
0
        // Retrieve capabilities of a video device
        internal static VideoCapabilities[] FromStreamConfig(IAMStreamConfig videoStreamConfig)
        {
            if (videoStreamConfig == null)
            {
                throw new ArgumentNullException(nameof(videoStreamConfig));
            }

            // ensure this device reports capabilities
            int hr = videoStreamConfig.GetNumberOfCapabilities(out int count, out int size);

            if (hr != 0)
            {
                Marshal.ThrowExceptionForHR(hr);
            }

            if (count <= 0)
            {
                throw new NotSupportedException("This video device does not report capabilities.");
            }

            if (size > Marshal.SizeOf(typeof(VideoStreamConfigCaps)))
            {
                throw new NotSupportedException("Unable to retrieve video device capabilities. This video device requires a larger VideoStreamConfigCaps structure.");
            }

            // group capabilities with similar parameters
            var videocapsList = new Dictionary <string, VideoCapabilities>();

            for (int i = 0; i < count; i++)
            {
                try
                {
                    var vc = new VideoCapabilities(videoStreamConfig, i);

                    string key =
                        $"{vc.FrameSize.Width} x {Math.Abs(vc.FrameSize.Height)} ({vc.AverageFrameRate} fps, {vc.BitCount} bit)";

                    if (!videocapsList.ContainsKey(key))
                    {
                        videocapsList.Add(key, vc);
                    }
                    //else
                    //{
                    //    if ( vc.BitCount > videocapsList[key].BitCount )
                    //    {
                    //        videocapsList[key] = vc;
                    //    }
                    //}
                }
                catch
                {
                }
            }

            var videocaps = new VideoCapabilities[videocapsList.Count];

            videocapsList.Values.CopyTo(videocaps, 0);

            return(videocaps);
        }
Example #2
0
        /// <summary>
        /// Check if two video capabilities are equal.
        /// </summary>
        ///
        /// <param name="vc2">Second video capability to compare with.</param>
        ///
        /// <returns>Returns true if both video capabilities are equal or false otherwise.</returns>
        ///
        public bool Equals(VideoCapabilities vc2)
        {
            if ((object)vc2 == null)
            {
                return(false);
            }

            return((FrameSize == vc2.FrameSize) && (BitCount == vc2.BitCount));
        }
Example #3
0
        // Retrieve capabilities of a video device
        static internal VideoCapabilities[] FromStreamConfig( IAMStreamConfig videoStreamConfig )
        {
            if ( videoStreamConfig == null )
                throw new ArgumentNullException( "videoStreamConfig" );

            // ensure this device reports capabilities
            int count, size;
            int hr = videoStreamConfig.GetNumberOfCapabilities( out count, out size );

            if ( hr != 0 )
                Marshal.ThrowExceptionForHR( hr );

            if ( count <= 0 )
                throw new NotSupportedException( "This video device does not report capabilities." );

            if ( size > Marshal.SizeOf( typeof( VideoStreamConfigCaps ) ) )
                throw new NotSupportedException( "Unable to retrieve video device capabilities. This video device requires a larger VideoStreamConfigCaps structure." );

            // group capabilities with similar parameters
            var videocapsList = new Dictionary<string, VideoCapabilities>( );

            for ( int i = 0; i < count; i++ )
            {
                try
                {
                    var vc = new VideoCapabilities( videoStreamConfig, i );

                    string key =
                        $"{vc.FrameSize.Width} x {Math.Abs(vc.FrameSize.Height)} ({vc.AverageFrameRate} fps, {vc.BitCount} bit)";
                    
                    if ( !videocapsList.ContainsKey( key ) )
                    {
                        videocapsList.Add( key, vc );
                    }
                    //else
                    //{
                    //    if ( vc.BitCount > videocapsList[key].BitCount )
                    //    {
                    //        videocapsList[key] = vc;
                    //    }
                    //}
                }
                catch
                {
                }
            }

            var videocaps = new VideoCapabilities[videocapsList.Count];
            videocapsList.Values.CopyTo( videocaps, 0 );

            return videocaps;
        }
Example #4
0
 /// <summary>
 /// Check if two video capabilities are equal.
 /// </summary>
 ///
 /// <param name="vc2">Second video capability to compare with.</param>
 ///
 /// <returns>Returns true if both video capabilities are equal or false otherwise.</returns>
 ///
 public bool Equals(VideoCapabilities vc2) => (object)vc2 == null ? false : (FrameSize == vc2.FrameSize) && (BitCount == vc2.BitCount);
Example #5
0
        // Configure specified pin and collect its capabilities if required
        private void GetPinCapabilitiesAndConfigureSizeAndRate( ICaptureGraphBuilder2 graphBuilder, IBaseFilter baseFilter,
            Guid pinCategory, VideoCapabilities resolutionToSet, ref VideoCapabilities[] capabilities )
        {
            object streamConfigObject;
            graphBuilder.FindInterface( pinCategory, MediaType.Video, baseFilter, typeof( IAMStreamConfig ).GUID, out streamConfigObject );

            if ( streamConfigObject != null )
            {
                IAMStreamConfig streamConfig = null;

                try
                {
                    streamConfig = (IAMStreamConfig) streamConfigObject;
                }
                catch ( InvalidCastException ex)
                {
                    Logger.LogExceptionToFile(ex, "GetPinCapabilities");
                }

                if ( streamConfig != null )
                {
                    if ( capabilities == null )
                    {
                        try
                        {
                            // get all video capabilities
                            capabilities = iSpyPRO.DirectShow.VideoCapabilities.FromStreamConfig( streamConfig );
                        }
                        catch(Exception ex)
                        {
                            Logger.LogExceptionToFile(ex, "Device Caps");

                        }
                    }

                    // check if it is required to change capture settings
                    if ( resolutionToSet != null )
                    {
                        SetResolution( streamConfig, resolutionToSet );
                    }
                }
            }

            // if failed resolving capabilities, then just create empty capabilities array,
            // so we don't try again
            if ( capabilities == null )
            {
                capabilities = new VideoCapabilities[0];
            }
        }
Example #6
0
        // Set resolution for the specified stream configuration
        private static void SetResolution( IAMStreamConfig streamConfig, VideoCapabilities resolution )
        {
            if ( resolution == null )
            {
                return;
            }

            // iterate through device's capabilities to find mediaType for desired resolution
            int capabilitiesCount, capabilitySize;
            AMMediaType newMediaType = null;
            var caps = new VideoStreamConfigCaps( );

            streamConfig.GetNumberOfCapabilities( out capabilitiesCount, out capabilitySize );

            for ( int i = 0; i < capabilitiesCount; i++ )
            {
                try
                {
                    var vc = new VideoCapabilities( streamConfig, i );

                    if ( resolution == vc )
                    {
                        if ( streamConfig.GetStreamCaps( i, out newMediaType, caps ) == 0 )
                        {
                            break;
                        }
                    }
                }
                catch(Exception ex)
                {
                    // ignored
                    Logger.LogExceptionToFile(ex,"SetResolution");
                }
            }

            // set the new format
            if ( newMediaType != null )
            {
                streamConfig.SetFormat( newMediaType );
                newMediaType.Dispose( );
            }
        }
Example #7
0
        /// <summary>
        /// Check if two video capabilities are equal.
        /// </summary>
        /// 
        /// <param name="vc2">Second video capability to compare with.</param>
        /// 
        /// <returns>Returns true if both video capabilities are equal or false otherwise.</returns>
        /// 
        public bool Equals( VideoCapabilities vc2 )
        {
            if ( (object) vc2 == null )
            {
                return false;
            }

            return ( ( FrameSize == vc2.FrameSize ) && ( BitCount == vc2.BitCount ) );
        }