Startup() 공개 정적인 메소드

initializes MediaFoundation - only needs to be called once per process
public static Startup ( ) : void
리턴 void
예제 #1
0
 /// <summary>
 /// Constructs a new MediaFounationSimplePlayer.
 /// </summary>
 /// <param name="url">The file to play.</param>
 /// <param name="m_selectFlags">Specify how the player selects streams.</param>
 public MediaFounationSimplePlayer(string url, StreamSelectFlags selectFlags = StreamSelectFlags.SelectByDefault)
 {
     if (!File.Exists(url))
     {
         throw new FileNotFoundException("This file doesn't exist.");
     }
     MediaFoundationApi.Startup();
     m_selectflag = selectFlags;
     URL          = url;
     Load();
 }
예제 #2
0
        public MediaFoundationCapturer(IMFActivate devsource)
        {
            MediaFoundationApi.Startup();
            WaveFormat format = new WaveFormat();

            try
            {
                devsource.ActivateObject(typeof(IMFMediaSource).GUID, out object _source);
                source = _source as IMFMediaSource;
            }
            catch (COMException)
            {
                throw new ArgumentException("Can't create media source with the devsource.");
            }
            SetFormat(format);
            recordthread = new Thread(DoRecord);
        }
예제 #3
0
        /// <summary>
        /// Loads IWaveProvider.
        /// </summary>
        /// <param name="waveProvider">The waveProvider to be loaded.</param>
        public void Init(IWaveProvider waveProvider)
        {
            MediaFoundationApi.Startup();
            m_sourcewave = waveProvider;
            int          readcount;
            MemoryStream msByteStrem = new MemoryStream();

            byte[] _data;
            do
            {
                readcount = 0;
                _data     = new byte[32767];
                readcount = waveProvider.Read(_data, 0, _data.Length);
                if (readcount < 0)
                {
                    continue;
                }
                msByteStrem.Write(_data, 0, readcount);
            } while (readcount >= _data.Length | readcount < 0);//Creates a IMFByteStream and fills it with the data in waveProvider.
            ComStream     csByteStream = new ComStream(msByteStrem);
            IMFByteStream mfByteStream = MediaFoundationApi.CreateByteStream(csByteStream);

            MediaFoundationInterop.MFCreateSourceResolver(out IMFSourceResolver resolver);
            IMFAttributes streamattributes = mfByteStream as IMFAttributes;

            mfByteStream.GetLength(out long _length);
            resolver.CreateObjectFromByteStream(mfByteStream, null, SourceResolverFlags.MF_RESOLUTION_MEDIASOURCE
                                                | SourceResolverFlags.MF_RESOLUTION_CONTENT_DOES_NOT_HAVE_TO_MATCH_EXTENSION_OR_MIME_TYPE, null, out _, out object _source);//Turns the stream to IMFMediaSource
            IMFMediaSource source = _source as IMFMediaSource;

            source.CreatePresentationDescriptor(out IMFPresentationDescriptor descriptor);
            MediaFoundationInterop.MFCreateTopology(out IMFTopology topo);
            descriptor.GetStreamDescriptorCount(out uint sdcount);
            for (uint i = 0; i < sdcount; i++)//For each stream in the source,creates renderer and adds to the topology.
            {
                descriptor.GetStreamDescriptorByIndex(i, out bool IsSelected, out IMFStreamDescriptor sd);
                if (!IsSelected)
                {
                    if (SelectAllStream)
                    {
                        descriptor.SelectStream(i);
                    }
                    else
                    {
                        continue;
                    }
                }
                sd.GetMediaTypeHandler(out IMFMediaTypeHandler typeHandler);
                typeHandler.GetMajorType(out Guid streamtype);
                IMFActivate renderer;
                if (streamtype == MediaTypes.MFMediaType_Audio)
                {
                    MediaFoundationInterop.MFCreateAudioRendererActivate(out renderer);//Creates renderer for audio streams
                }
                else
                {
                    continue;
                }
                //Creats and equips the topology nodes of the certain stream
                MediaFoundationInterop.MFCreateTopologyNode(MF_TOPOLOGY_TYPE.MF_TOPOLOGY_SOURCESTREAM_NODE, out IMFTopologyNode sourcenode);
                sourcenode.SetUnknown(MediaFoundationAttributes.MF_TOPONODE_SOURCE, source);
                sourcenode.SetUnknown(MediaFoundationAttributes.MF_TOPONODE_PRESENTATION_DESCRIPTOR, descriptor);
                sourcenode.SetUnknown(MediaFoundationAttributes.MF_TOPONODE_STREAM_DESCRIPTOR, sd);
                topo.AddNode(sourcenode);
                MediaFoundationInterop.MFCreateTopologyNode(MF_TOPOLOGY_TYPE.MF_TOPOLOGY_OUTPUT_NODE, out IMFTopologyNode outputnode);
                outputnode.SetObject(renderer);
                topo.AddNode(outputnode);
                sourcenode.ConnectOutput(0, outputnode, 0);
            }
            MediaFoundationInterop.MFCreateMediaSession(IntPtr.Zero, out m_Session);
            m_Session.SetTopology(0, topo);
            m_eventthread = new Thread(ProcessEvent);
            m_eventthread.Start();
        }
예제 #4
0
        /// <summary>
        /// Initialize the MediaFoundationProvider with specific file.
        /// </summary>
        public MediaFoundationProvider(string url)
        {
            MediaFoundationApi.Startup();
            if (!File.Exists(url))
            {
                throw new FileNotFoundException("This file doesn't exist");
            }
            MediaFoundationInterop.MFCreateSourceResolver(out IMFSourceResolver resolver);
            //Creates both IMFMediaSource and IMFByteStream.Uses the stream for 'Read' method and uses the source to collect format information.
            resolver.CreateObjectFromURL(url, SourceResolverFlags.MF_RESOLUTION_BYTESTREAM | SourceResolverFlags.MF_RESOLUTION_CONTENT_DOES_NOT_HAVE_TO_MATCH_EXTENSION_OR_MIME_TYPE, null, out _, out object _stream);
            IMFByteStream byteStream = _stream as IMFByteStream;

            resolver.CreateObjectFromByteStream(byteStream, null, SourceResolverFlags.MF_RESOLUTION_MEDIASOURCE | SourceResolverFlags.MF_RESOLUTION_CONTENT_DOES_NOT_HAVE_TO_MATCH_EXTENSION_OR_MIME_TYPE, null, out _, out object _source);
            Marshal.FinalReleaseComObject(resolver);
            IMFMediaSource source = _source as IMFMediaSource;

            source.CreatePresentationDescriptor(out IMFPresentationDescriptor descriptor);
            descriptor.GetStreamDescriptorCount(out uint sdcount);
            for (uint i = 0; i < sdcount; i++)
            {
                descriptor.GetStreamDescriptorByIndex(i, out _, out IMFStreamDescriptor sd);
                sd.GetMediaTypeHandler(out IMFMediaTypeHandler typeHandler);
                typeHandler.GetMediaTypeByIndex(0, out IMFMediaType mediaType);
                mediaType.GetMajorType(out Guid streamtype);
                if (streamtype == MediaTypes.MFMediaType_Audio)
                {
                    mediaType.GetUINT32(MediaFoundationAttributes.MF_MT_AUDIO_SAMPLES_PER_SECOND, out int rate);//SampleRate
                    mediaType.GetUINT32(MediaFoundationAttributes.MF_MT_AUDIO_NUM_CHANNELS, out int channelcount);
                    int samplesize;
                    try
                    {
                        mediaType.GetUINT32(MediaFoundationAttributes.MF_MT_AUDIO_BITS_PER_SAMPLE, out samplesize);
                    }
                    catch (COMException e)
                    {
                        if ((uint)e.HResult != 0xC00D36E6)
                        {
                            throw e;
                        }
                        else
                        {
                            samplesize = 8;
                        }
                    }
                    WaveFormat = new WaveFormat(rate, samplesize, channelcount);
                }
                else
                {
                    continue;
                }
            }
            byteStream.GetLength(out streamlength);
            byteStream.SetCurrentPosition(0);
            //Moves all the bytes in IMFByteStream to MemoryStream.
            MediaFoundationInterop.MFCreateMemoryBuffer(unchecked ((int)streamlength), out IMFMediaBuffer mediabuffer);
            mediabuffer.Lock(out IntPtr pbuffer, out int length, out _);
            byteStream.Read(pbuffer, length, out _);
            byte[] buffer = new byte[length];
            Marshal.Copy(pbuffer, buffer, 0, length);
            datastream = new MemoryStream(buffer);
        }