/// <summary>
        /// Creates an instance of the sink writer, given an IMFByteStream instance.
        /// </summary>
        /// <param name="factory">A valid IMFReadWriteClassFactory instance.</param>
        /// <param name="mediaSink">A byte streamIndex used by the sink writer to write data.</param>
        /// <param name="attributes">A instance to the IMFAttributes interface. You can use this parameter to configure the sink writer.</param>
        /// <param name="sourceReader">Receives the sink writer.</param>
        /// <returns>If this function succeeds, it returns the S_OK member. Otherwise, it returns another HResult's member that describe the error.</returns>
        public static HResult CreateInstanceFromObject(this IMFReadWriteClassFactory factory, IMFByteStream byteStream, IMFAttributes attributes, out IMFSinkWriter sinkWriter)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }

            object tmp;

            HResult hr = factory.CreateInstanceFromObject(CLSID.CLSID_MFSinkWriter, byteStream, attributes, typeof(IMFSinkWriterExtensions).GUID, out tmp);

            sinkWriter = hr.Succeeded() ? tmp as IMFSinkWriter : null;

            return(hr);
        }
        /// <summary>
        /// Creates an instance of the source reader given a URL.
        /// </summary>
        /// <param name="factory">A valid IMFReadWriteClassFactory instance.</param>
        /// <param name="url">A string that specifies the input file for the source reader.</param>
        /// <param name="attributes">A instance to the IMFAttributes interface. You can use this parameter to configure the source reader.</param></param>
        /// <param name="sourceReader">Receives the source reader</param>
        /// <returns>If this function succeeds, it returns the S_OK member. Otherwise, it returns another HResult's member that describe the error.</returns>
        public static HResult CreateInstanceFromURL(this IMFReadWriteClassFactory factory, string url, IMFAttributes attributes, out IMFSourceReader sourceReader)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }

            object tmp;

            HResult hr = factory.CreateInstanceFromURL(CLSID.CLSID_MFSourceReader, url, attributes, typeof(IMFSourceReader).GUID, out tmp);

            sourceReader = hr.Succeeded() ? tmp as IMFSourceReader : null;

            return(hr);
        }
        private void CreateReaderAndWriter()
        {
            object        sourceReaderObject = null;
            object        sinkWriterObject   = null;
            IMFAttributes attributes         = null;

            // Create the class factory
            IMFReadWriteClassFactory factory = (IMFReadWriteClassFactory)(new MFReadWriteClassFactory());

            // Create the attributes
            MFHelper.MFCreateAttributes(out attributes, 1);
            attributes.SetUINT32(new Guid(Consts.MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS), 1);

            // Use the factory to create the Source Reader
            factory.CreateInstanceFromURL(new Guid(CLSID.MFSourceReader), this.sourceFilename, attributes, new Guid(IID.IMFSourceReader), out sourceReaderObject);
            this.sourceReader = (IMFSourceReader)sourceReaderObject;

            // Use the factory to create the Sink Writer
            factory.CreateInstanceFromURL(new Guid(CLSID.MFSinkWriter), this.targetFilename, attributes, new Guid(IID.IMFSinkWriter), out sinkWriterObject);
            this.sinkWriter = (IMFSinkWriter)sinkWriterObject;
        }