private static IMFTranscodeProfile CreateProfile(AudioFormat audioOutput, VideoFormat videoOutput, Guid containerType)
        {
            IMFTranscodeProfile profile = null;

            // Create a transcode profile
            MFHelper.MFCreateTranscodeProfile(out profile);

            // Create and set the audio attributes
            profile.SetAudioAttributes(SimpleFastEncode.CreateAudioAttributes(audioOutput));

            // Create and set the video attributes
            profile.SetVideoAttributes(SimpleFastEncode.CreateVideoAttributes(videoOutput));

            // Create the container attributes
            IMFAttributes containerAttributes = null;

            MFHelper.MFCreateAttributes(out containerAttributes, 2);
            containerAttributes.SetUINT32(new Guid(Consts.MF_TRANSCODE_TOPOLOGYMODE), 1);
            containerAttributes.SetGUID(new Guid(Consts.MF_TRANSCODE_CONTAINERTYPE), containerType);

            // Set them in the transcoding profile
            profile.SetContainerAttributes(containerAttributes);

            return(profile);
        }
示例#2
0
        private void CreateVideoCaptureSource()
        {
            try
            {
                if (VideoCaptureDevice == null)
                {
                    Trace.WriteLine("Error no videocapturedevice set");
                    return;
                }
                //VideoCaptureSource
                IMFAttributes pAttributes = null;
                int           hr          = MFExtern.MFCreateAttributes(out pAttributes, 2);

                hr = pAttributes.SetGUID(MFAttributesClsid.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, CLSID.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
                MFError.ThrowExceptionForHR(hr);

                hr = pAttributes.SetString(MFAttributesClsid.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, VideoCaptureDevice.DevicePath);
                MFError.ThrowExceptionForHR(hr);

                IMFMediaSource ppMediaSource = null;
                hr = MFExtern.MFCreateDeviceSource(pAttributes, out ppMediaSource);
                MFError.ThrowExceptionForHR(hr);

                m_pSource = ppMediaSource;
                //GetCaptureFormats(m_pSource);
            }
            catch (Exception e)
            {
                Marshal.GetHRForException(e);
                Trace.WriteLine("SetupGraph Exception " + e.ToString());
            }
        }
示例#3
0
        public static IEnumerable <IMFActivate> EnumDeviceSources()
        {
            IMFAttributes devtype = CreateAttributes(2);
            Guid          MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_AUDCAP_GUID = new Guid(0x14dd9a1c, 0x7cff, 0x41be, 0xb1, 0xb9, 0xba, 0x1a, 0xc6, 0xec, 0xb5, 0x71);

            devtype.SetGUID(MediaFoundationAttributes.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_AUDCAP_GUID);
            MediaFoundationInterop.MFEnumDeviceSources(devtype, out IMFActivate[] sources, out _);
            foreach (var i in sources)
            {
                yield return(i);
            }
        }
示例#4
0
        /// <summary>
        /// Returns an array of DsDevices of type devcat.
        /// </summary>
        /// <param name="cat">Any one of FilterCategory</param>
        public static MFDevice[] GetDevicesOfCat(Guid FilterCategory)
        {
            // Use arrayList to build the retun list since it is easily resizable
            MFDevice[]    devret = null;
            IMFActivate[] ppDevices;

            //////////

            HResult       hr          = 0;
            IMFAttributes pAttributes = null;

            // Initialize an attribute store. We will use this to
            // specify the enumeration parameters.

            hr = MFExtern.MFCreateAttributes(out pAttributes, 1);

            // Ask for source type = video capture devices
            if (hr >= 0)
            {
                hr = pAttributes.SetGUID(
                    MFAttributesClsid.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
                    FilterCategory
                    );
            }

            // Enumerate devices.
            int cDevices;

            if (hr >= 0)
            {
                hr = MFExtern.MFEnumDeviceSources(pAttributes, out ppDevices, out cDevices);

                if (hr >= 0)
                {
                    devret = new MFDevice[cDevices];

                    for (int x = 0; x < cDevices; x++)
                    {
                        devret[x] = new MFDevice(ppDevices[x]);
                    }
                }
            }

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

            return(devret);
        }
示例#5
0
        protected void SetTargetStream(Stream stream, MFMediaType inputMediaType, MFMediaType targetMediaType, Guid containerType)
        {
            IMFAttributes attributes = null;

            try
            {
                _targetStream = MediaFoundationCore.IStreamToByteStream(new ComStream(stream));

                MFByteStreamCapsFlags flags = MFByteStreamCapsFlags.None;
                int result = _targetStream.GetCapabilities(ref flags);

                attributes = MediaFoundationCore.CreateEmptyAttributes(2);
                attributes.SetUINT32(MediaFoundationAttributes.MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, 1);
                attributes.SetGUID(MediaFoundationAttributes.MF_TRANSCODE_CONTAINERTYPE, containerType);

                _sinkWriter = MediaFoundationCore.CreateSinkWriterFromMFByteStream(_targetStream, attributes);

                _streamIndex = _sinkWriter.AddStream(targetMediaType);
                _sinkWriter.SetInputMediaType(_streamIndex, inputMediaType, null);

                _inputMediaType       = inputMediaType;
                _targetMediaType      = targetMediaType;
                _sourceBytesPerSecond = inputMediaType.AverageBytesPerSecond;

                //initialize the sinkwriter
                _sinkWriter.BeginWriting();
            }
            catch (Exception)
            {
                if (_sinkWriter != null)
                {
                    _sinkWriter.Dispose();
                    _sinkWriter = null;
                }
                if (_targetStream != null)
                {
                    _targetStream.Close();
                    Marshal.ReleaseComObject(_targetStream);
                    _targetStream = null;
                }
                throw;
            }
            finally
            {
                if (attributes != null)
                {
                    Marshal.ReleaseComObject(attributes);
                }
            }
        }
        private static IMFAttributes CreateVideoAttributes(VideoFormat videoOutput)
        {
            IMFAttributes videoAttributes = null;

            MFHelper.MFCreateAttributes(out videoAttributes, 0);
            videoAttributes.SetGUID(new Guid(Consts.MF_MT_SUBTYPE), videoOutput.Subtype);

            PackedINT32 pixelAspectRatio = new PackedINT32(1, 1);

            // Set the argument attributes
            videoAttributes.SetUINT64(new Guid(Consts.MF_MT_FRAME_RATE), videoOutput.FrameRate.Packed);
            videoAttributes.SetUINT64(new Guid(Consts.MF_MT_FRAME_SIZE), videoOutput.FrameSize.Packed);
            videoAttributes.SetUINT64(new Guid(Consts.MF_MT_PIXEL_ASPECT_RATIO), pixelAspectRatio.Packed);
            videoAttributes.SetUINT32(new Guid(Consts.MF_MT_INTERLACE_MODE), videoOutput.InterlaceMode);
            videoAttributes.SetUINT32(new Guid(Consts.MF_MT_AVG_BITRATE), videoOutput.AvgBitRate);

            return(videoAttributes);
        }
示例#7
0
        private IMFSourceReader CreateSourceReader(IMFMediaSource mfs)
        {
            IMFSourceReader reader = null;

            try
            {
                MFExtern.MFCreateAttributes(out sourcevideoreaderattribute, 2);

                sourcevideoreaderattribute.SetGUID(MFAttributesClsid.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, CLSID.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
                sourcevideoreaderattribute.SetUINT32(MFAttributesClsid.MF_SOURCE_READER_ENABLE_VIDEO_PROCESSING, 1);


                MFExtern.MFCreateSourceReaderFromMediaSource(mfs, sourcevideoreaderattribute, out reader);
                //reader.GetCurrentMediaType((int)__MIDL___MIDL_itf_mfreadwrite_0000_0001_0001.MF_SOURCE_READER_FIRST_VIDEO_STREAM, out sourcemeidatype);
            }
            catch (Exception e)
            {
                Debug.WriteLine("ERROR CreateSourceReader", e.StackTrace);
            }
            return(reader);
        }
示例#8
0
        private static MFDevice[] GetDevices(Guid filterCategory)
        {
            IMFAttributes pAttributes = null;

            IMFActivate[] ppDevices = null;

            // Create an attribute store to specify the enumeration parameters.
            int hr = MFExtern.MFCreateAttributes(out pAttributes, 1);

            MFError.ThrowExceptionForHR(hr);

            //CLSID.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID
            hr = pAttributes.SetGUID(MFAttributesClsid.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, filterCategory);
            MFError.ThrowExceptionForHR(hr);

            int count;

            hr = MFExtern.MFEnumDeviceSources(pAttributes, out ppDevices, out count);

            MFDevice[] devices = new MFDevice[count];

            for (int i = 0; i < count; i++)
            {
                int    ssize        = -1;
                string friendlyname = "";
                hr = ppDevices[i].GetAllocatedString(MFAttributesClsid.MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME, out friendlyname, out ssize);

                int    ssizesym = -1;
                string symlink;
                hr = ppDevices[i].GetAllocatedString(MFAttributesClsid.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, out symlink, out ssizesym);
                //Use this attribute as input to the MFCreateDeviceSourceActivate function.

                devices[i]            = new MFDevice();
                devices[i].Name       = friendlyname;
                devices[i].DevicePath = symlink;
            }

            return(devices);
        }
示例#9
0
 public HResult SetGUID(Guid guidKey, Guid guidValue)
 {
     return(m_Attribs.SetGUID(guidKey, guidValue));
 }