コード例 #1
0
 private void GetInterface()
 {
     IMFMediaSession ms;
     int hr = MFExtern.MFCreateMediaSession(null, out ms);
     MFError.ThrowExceptionForHR(hr);
     m_rc = ms as IMFRateControl;
 }
コード例 #2
0
        /// <summary>
        /// Processes the media session event.
        /// </summary>
        private void ProcessEvent()
        {
            while (m_Session != null)
            {
                try {
                    m_Session.GetEvent(1, out IMFMediaEvent _event);//requests events and returns immediately
                    _event.GetType(out MediaEventType eventtype);
                    switch (eventtype)
                    {
                    case MediaEventType.MESessionEnded:
                        PlaybackState = PlaybackState.Stopped;
                        PlaybackStopped?.Invoke(this, new StoppedEventArgs());
                        break;

                    case MediaEventType.MESessionTopologyStatus:    //topology loaded
                        Guid guidManager = typeof(IAudioSessionManager).GUID;
                        (new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator).
                        GetDefaultAudioEndpoint(CoreAudioApi.DataFlow.Render, CoreAudioApi.Role.Multimedia, out IMMDevice endoint);
                        endoint.Activate(ref guidManager, ClsCtx.ALL, IntPtr.Zero, out object _manager);
                        IAudioSessionManager manager = _manager as IAudioSessionManager;
                        manager.GetSimpleAudioVolume(Guid.Empty, 0, out m_volume);

                        m_Session.GetClock(out m_clock);

                        Guid guid_ratecontrol        = typeof(IMFRateControl).GUID;
                        Guid MF_RATE_CONTROL_SERVICE = Guid.Parse("866fa297-b802-4bf8-9dc9-5e3b6a9f53c9");
                        MediaFoundationInterop.MFGetService(m_Session, ref MF_RATE_CONTROL_SERVICE, ref guid_ratecontrol, out object _control);    //gets rate control
                        m_rate   = _control as IMFRateControl;
                        Prepared = true;
                        break;
                    }
                    _event = null;
                }
                catch (COMException e)
                {
                    if (e.HResult == MediaFoundationErrors.MF_E_NO_EVENTS_AVAILABLE)
                    {
                        continue;
                    }
                    else
                    {
                        throw e;
                    }
                }
                catch (ThreadAbortException)
                {
                    break;
                }
            }
        }
コード例 #3
0
        private void GetRateControl()
        {
            if (RateControl != null)
            {
                return;
            }
            if (m_pSession == null)
            {
                return;
            }
            object RC = null;

            MFExtern.MFGetService(m_pSession, MFServices.MF_RATE_CONTROL_SERVICE, typeof(IMFRateControl).GUID, out RC);

            RateControl = RC as IMFRateControl;
            if (RateControl == null)
            {
                throw new Exception("Couldnt get the rate control interface");
            }
        }
コード例 #4
0
ファイル: MediaManager.cs プロジェクト: ISDHN/USBTool
        public int InitializeMedia(string MediaFile, int dwrate, float dwvolume, bool IsFullScreen)
        {
            int hr;

            MFStartup(MF_VERSION, MFSTARTUP_FULL);
            MFCreateMediaSession(IntPtr.Zero, out mediaSession);

            MFCreateTopology(out IMFTopology topo);
            MFCreateSourceResolver(out IMFSourceResolver resolver);
            IUnknown unknown;

            try
            {
                resolver.CreateObjectFromURL(MediaFile, MF_RESOLUTION_MEDIASOURCE | MF_RESOLUTION_CONTENT_DOES_NOT_HAVE_TO_MATCH_EXTENSION_OR_MIME_TYPE,
                                             null, out uint objtype, out unknown);
            }
            catch
            {
                MessageBox.Show("不支持的媒体格式。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(0);
            }
            IMFMediaSource source = unknown as IMFMediaSource;

            source.CreatePresentationDescriptor(out IMFPresentationDescriptor descriptor);
            if (MFRequireProtectedEnvironment(descriptor) == 0)
            {
                MessageBox.Show("媒体受保护,无法播放。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(0);
            }
            descriptor.GetStreamDescriptorCount(out uint sdcount);
            for (uint i = 0; i < sdcount; i++)
            {
                descriptor.GetStreamDescriptorByIndex(i, out bool IsSelected, out IMFStreamDescriptor sd);
                if (!IsSelected)
                {
                    descriptor.SelectStream(i);
                }
                sd.GetMediaTypeHandler(out IMFMediaTypeHandler typeHandler);
                typeHandler.GetMajorType(out Guid streamtype);
                IMFActivate renderer;
                if (streamtype == MFMediaType_Audio)
                {
                    hr = MFCreateAudioRendererActivate(out renderer);
                }
                else if (streamtype == MFMediaType_Video)
                {
                    hr       = MFCreateVideoRendererActivate(VideoWnd.Handle, out renderer);
                    HasVideo = true;
                }
                else
                {
                    MessageBox.Show("不支持的媒体格式。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(0);
                }
                hr = MFCreateTopologyNode(MF_TOPOLOGY_SOURCESTREAM_NODE, out IMFTopologyNode sourcenode);
                sourcenode.SetUnknown(MF_TOPONODE_SOURCE, source as IUnknown);
                sourcenode.SetUnknown(MF_TOPONODE_PRESENTATION_DESCRIPTOR, descriptor as IUnknown);
                sourcenode.SetUnknown(MF_TOPONODE_STREAM_DESCRIPTOR, sd as IUnknown);
                topo.AddNode(sourcenode);
                MFCreateTopologyNode(MF_TOPOLOGY_OUTPUT_NODE, out IMFTopologyNode outputnode);
                outputnode.SetObject(renderer as IUnknown);
                topo.AddNode(outputnode);
                outputnode.SetUINT32(MF_TOPONODE_NOSHUTDOWN_ON_REMOVE, 0);
                hr = sourcenode.ConnectOutput(0, outputnode, 0);
            }
            mediaSession.SetTopology(0, topo);
            Rate              = dwrate;
            Volume            = dwvolume;
            this.IsFullScreen = IsFullScreen;
            uint eventtype = 0;

            while (eventtype != MESessionTopologyStatus)
            {
                mediaSession.GetEvent(0, out IMFMediaEvent mediaevent);
                mediaevent.GetType(out eventtype);
                mediaevent = null;
            }
            Guid guid_ratecontrol = typeof(IMFRateControl).GUID;

            MFGetService(mediaSession as IUnknown, ref MF_RATE_CONTROL_SERVICE, ref guid_ratecontrol, out IUnknown _ratecontrol);
            IMFRateControl ratecontrol = _ratecontrol as IMFRateControl;

            hr = ratecontrol.SetRate(false, Rate >= 0 ? Rate * 7 / 10 + 1 : 1 / (1 + Rate / -10 * 7));
            try
            {
                Guid guid_volume      = typeof(IMFStreamAudioVolume).GUID;
                Guid guid_audiopolicy = typeof(IMFAudioPolicy).GUID;
                hr = MFGetService(mediaSession as IUnknown, ref MR_STREAM_VOLUME_SERVICE, ref guid_volume, out IUnknown _volume);
                hr = MFGetService(mediaSession as IUnknown, ref MR_AUDIO_POLICY_SERVICE, ref guid_audiopolicy, out IUnknown _policy);
                IMFStreamAudioVolume volumecontrol = _volume as IMFStreamAudioVolume;
                IMFAudioPolicy       policy        = _policy as IMFAudioPolicy;
                volumecontrol.GetChannelCount(out uint channelcount);
                for (uint c = 0; c < channelcount; c++)
                {
                    volumecontrol.SetChannelVolume(c, Volume);
                }
                policy.SetDisplayName(" ");
            }
            catch
            {
            }
            try
            {
                Guid guid_videocontrol = typeof(IMFVideoDisplayControl).GUID;
                hr = MFGetService(mediaSession as IUnknown, ref MR_VIDEO_RENDER_SERVICE, ref guid_videocontrol, out IUnknown _videocontrol);
                IMFVideoDisplayControl videocontrol = _videocontrol as IMFVideoDisplayControl;
                VideoWnd.videocontrol = videocontrol;
                if (IsFullScreen)
                {
                    VideoWnd.Top    = 0;
                    VideoWnd.Left   = 0;
                    VideoWnd.Width  = (int)System.Windows.SystemParameters.PrimaryScreenWidth;
                    VideoWnd.Height = (int)System.Windows.SystemParameters.PrimaryScreenHeight;
                }
                Rectangle videopos = new Rectangle(0, 0, VideoWnd.Width, VideoWnd.Height);
                videocontrol.SetVideoPosition(null, ref videopos);
            }
            catch
            {
            }
            hr = mediaSession.BeginGetEvent((IMFAsyncCallback)this, null);
            return(1);
        }
コード例 #5
0
        public void InitializeMedia(string MediaFile, int dwrate, float dwvolume)
        {
            int hr;

            MFStartup(MF_VERSION, MFSTARTUP_FULL);
            MFCreateMediaSession(IntPtr.Zero, out IMFMediaSession mediaSession);
            MFCreateTopology(out IMFTopology topo);
            MFCreateSourceResolver(out IMFSourceResolver resolver);
            IUnknown unknown;

            try
            {
                resolver.CreateObjectFromURL(MediaFile, MF_RESOLUTION_MEDIASOURCE | MF_RESOLUTION_CONTENT_DOES_NOT_HAVE_TO_MATCH_EXTENSION_OR_MIME_TYPE,
                                             null, out uint objtype, out unknown);
            }
            catch
            {
                MessageBox.Show("不支持的媒体格式。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            IMFMediaSource source = unknown as IMFMediaSource;

            source.CreatePresentationDescriptor(out IMFPresentationDescriptor descriptor);
            if (MFRequireProtectedEnvironment(descriptor) == 0)
            {
                MessageBox.Show("媒体受保护,无法播放。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            descriptor.GetStreamDescriptorCount(out uint sdcount);
            for (uint i = 0; i < sdcount; i++)
            {
                descriptor.GetStreamDescriptorByIndex(i, out bool IsSelected, out IMFStreamDescriptor sd);
                if (!IsSelected)
                {
                    descriptor.SelectStream(i);
                }
                sd.GetMediaTypeHandler(out IMFMediaTypeHandler typeHandler);
                typeHandler.GetMajorType(out Guid streamtype);
                IMFActivate renderer;
                if (streamtype == MFMediaType_Audio)
                {
                    hr = MFCreateAudioRendererActivate(out renderer);
                }
                else if (streamtype == MFMediaType_Video)
                {
                    hr       = MFCreateVideoRendererActivate(host.Handle, out renderer);
                    hasvideo = true;
                }
                else
                {
                    continue;
                }
                hr = MFCreateTopologyNode(MF_TOPOLOGY_SOURCESTREAM_NODE, out IMFTopologyNode sourcenode);
                sourcenode.SetUnknown(MF_TOPONODE_SOURCE, source as IUnknown);
                sourcenode.SetUnknown(MF_TOPONODE_PRESENTATION_DESCRIPTOR, descriptor as IUnknown);
                sourcenode.SetUnknown(MF_TOPONODE_STREAM_DESCRIPTOR, sd as IUnknown);
                topo.AddNode(sourcenode);
                MFCreateTopologyNode(MF_TOPOLOGY_OUTPUT_NODE, out IMFTopologyNode outputnode);
                outputnode.SetObject(renderer as IUnknown);
                topo.AddNode(outputnode);
                hr = sourcenode.ConnectOutput(0, outputnode, 0);
            }
            mediaSession.SetTopology(0, topo);
            Hide();
            uint eventtype = 0;

            while (eventtype != MESessionTopologyStatus)
            {
                mediaSession.GetEvent(0, out IMFMediaEvent mediaevent);
                mediaevent.GetType(out eventtype);
                mediaevent = null;
            }
            Guid guid_ratecontrol = typeof(IMFRateControl).GUID;

            MFGetService(mediaSession as IUnknown, ref MF_RATE_CONTROL_SERVICE, ref guid_ratecontrol, out IUnknown _ratecontrol);
            IMFRateControl ratecontrol = _ratecontrol as IMFRateControl;

            hr = ratecontrol.SetRate(false, dwrate >= 0 ? dwrate * 7 / 10 + 1 : 1 / (1 + dwrate / -10 * 7));
            try
            {
                Guid guid_audiopolicy = typeof(IMFAudioPolicy).GUID;
                hr = MFGetService(mediaSession as IUnknown, ref MR_AUDIO_POLICY_SERVICE, ref guid_audiopolicy, out IUnknown _policy);
                IMFAudioPolicy policy = _policy as IMFAudioPolicy;
                policy.SetDisplayName(" ");
                playvolume = dwvolume;
            }
            catch
            {
            }
            try
            {
                Guid guid_videocontrol = typeof(IMFVideoDisplayControl).GUID;
                hr = MFGetService(mediaSession as IUnknown, ref MR_VIDEO_RENDER_SERVICE, ref guid_videocontrol, out IUnknown _videocontrol);
                IMFVideoDisplayControl videocontrol = _videocontrol as IMFVideoDisplayControl;
                if (FullScreen.Checked)
                {
                    host.Top    = 0;
                    host.Left   = 0;
                    host.Width  = (int)System.Windows.SystemParameters.PrimaryScreenWidth;
                    host.Height = (int)System.Windows.SystemParameters.PrimaryScreenHeight;
                }
                Rectangle videopos = new Rectangle(0, 0, host.Width, host.Height);
                host.videocontrol = videocontrol;
                videocontrol.SetVideoPosition(null, ref videopos);
            }
            catch
            {
            }
            state = "Prepared";
            while (true)
            {
                if (state == "Playing")
                {
                    eventtype = 0;
                    PropVariant prop = new PropVariant()
                    {
                        vt          = (ushort)VarEnum.VT_I8,
                        unionmember = 0,
                    };
                    #region mediasession
                    if (hasvideo)
                    {
                        host.Show();
                    }
                    preventthread = new Thread(() => {
                        while (true)
                        {
                            if (state == "Playing")
                            {
                                while (state == "Playing")
                                {
                                    if (hasvideo)
                                    {
                                        host.BringToFront();
                                    }
                                    SetAppAndSystemVolume(playvolume, false);
                                }
                            }
                        }
                    });
                    preventthread.Start();
                    hr = mediaSession.Start(Guid.Empty, prop);
                    while (eventtype != MESessionEnded)
                    {
                        hr = mediaSession.GetEvent(1, out IMFMediaEvent mediaevent);
                        if (hr == 0 && mediaevent != null)
                        {
                            mediaevent.GetType(out eventtype);
                            mediaevent = null;
                        }
                        Application.DoEvents();
                    }
                    host.Hide();
                    state = "Ended";
                    #endregion
                }
            }
        }