private void PlatformPlay() { // Cleanup the last song first. if (State != MediaState.Stopped) { _session.Stop(); _session.ClearTopologies(); _session.Close(); if (_volumeController != null) { _volumeController.Dispose(); _volumeController = null; } _clock.Dispose(); } //create the callback if it hasn't been created yet if (_callback == null) { _callback = new Callback(this); _session.BeginGetEvent(_callback, null); } // Set the new song. _session.SetTopology(SessionSetTopologyFlags.Immediate, _currentVideo.Topology); // Get the clock. _clock = _session.Clock.QueryInterface <PresentationClock>(); // Start playing. var varStart = new Variant(); _session.Start(null, varStart); }
private void PlatformPlay() { // Cleanup the last song first. if (State != MediaState.Stopped) { _session.Stop(); _volumeController.Dispose(); _clock.Dispose(); } // Set the new song. _session.SetTopology(0, _currentVideo.Topology); _volumeController = CppObject.FromPointer <SimpleAudioVolume>(GetVolumeObj(_session)); _volumeController.Mute = IsMuted; _volumeController.MasterVolume = _volume; // Get the clock. _clock = _session.Clock.QueryInterface <PresentationClock>(); //create the callback if it hasn't been created yet if (_callback == null) { _callback = new Callback(); _session.BeginGetEvent(_callback, null); } // Start playing. var varStart = new Variant(); _session.Start(null, varStart); }
private static void PlatformPlaySong(Song song) { // Cleanup the last song first. if (State != MediaState.Stopped) { _session.Stop(); _session.ClearTopologies(); _session.Close(); _volumeController.Dispose(); _clock.Dispose(); } // Set the new song. _session.SetTopology(SessionSetTopologyFlags.Immediate, song.Topology); // Get the volume interface. _volumeController = CppObject.FromPointer <AudioStreamVolume>(MediaPlayer.GetVolumeObj(_session)); SetChannelVolumes(); // Get the clock. _clock = _session.Clock.QueryInterface <PresentationClock>(); //create the callback if it hasn't been created yet if (_callback == null) { _callback = new Callback(); _session.BeginGetEvent(_callback, null); } // Start playing. var varStart = new Variant(); _session.Start(null, varStart); }
public static void Stop() { if (State == MediaState.Stopped) { return; } #if WINDOWS_MEDIA_ENGINE _mediaEngineEx.Source = null; #elif WINDOWS_MEDIA_SESSION _session.ClearTopologies(); _session.Stop(); _volumeController.Dispose(); _volumeController = null; _clock.Dispose(); _clock = null; #else // Loop through so that we reset the PlayCount as well foreach (var song in Queue.Songs) { _queue.ActiveSong.Stop(); } #endif State = MediaState.Stopped; }
private static void PlaySong(Song song) { #if WINDOWS_MEDIA_ENGINE _mediaEngineEx.Source = song.FilePath; _mediaEngineEx.Load(); _mediaEngineEx.Play(); #elif WINDOWS_MEDIA_SESSION // Cleanup the last song first. if (State != MediaState.Stopped) { _session.Stop(); _volumeController.Dispose(); _clock.Dispose(); } // Set the new song. _session.SetTopology(0, song.GetTopology()); // Get the volume interface. IntPtr volumeObj; try { MediaFactory.GetService(_session, MRPolicyVolumeService, SimpleAudioVolumeGuid, out volumeObj); } catch (Exception e) { MediaFactory.GetService(_session, MRPolicyVolumeService, SimpleAudioVolumeGuid, out volumeObj); } _volumeController = CppObject.FromPointer <SimpleAudioVolume>(volumeObj); _volumeController.Mute = _isMuted; _volumeController.MasterVolume = _volume; // Get the clock. _clock = _session.Clock.QueryInterface <PresentationClock>(); // Start playing. var varStart = new Variant(); _session.Start(null, varStart); #elif WINDOWS_PHONE Deployment.Current.Dispatcher.BeginInvoke(() => { _mediaElement.Source = new Uri(song.FilePath, UriKind.Relative); _mediaElement.Play(); // Ensure only one subscribe _mediaElement.MediaEnded -= OnSongFinishedPlaying; _mediaElement.MediaEnded += OnSongFinishedPlaying; }); #else song.SetEventHandler(OnSongFinishedPlaying); song.Volume = _isMuted ? 0.0f : _volume; song.Play(); #endif State = MediaState.Playing; }
private static void PlatformStop() { _session.ClearTopologies(); _session.Stop(); _session.Close(); _volumeController.Dispose(); _volumeController = null; _clock.Dispose(); _clock = null; }
private static void PlatformInitialize() { // The GUID is specified in a GuidAttribute attached to the class AudioStreamVolumeGuid = Guid.Parse(((GuidAttribute)typeof(AudioStreamVolume).GetCustomAttributes(typeof(GuidAttribute), false)[0]).Value); MediaManagerState.CheckStartup(); MediaFactory.CreateMediaSession(null, out _session); _callback = new Callback(); _session.BeginGetEvent(_callback, null); _clock = _session.Clock.QueryInterface<PresentationClock>(); }
private static void PlatformInitialize() { // The GUID is specified in a GuidAttribute attached to the class AudioStreamVolumeGuid = Guid.Parse(((GuidAttribute)typeof(AudioStreamVolume).GetCustomAttributes(typeof(GuidAttribute), false)[0]).Value); MediaManagerState.CheckStartup(); MediaFactory.CreateMediaSession(null, out _session); _callback = new Callback(); _session.BeginGetEvent(_callback, null); _clock = _session.Clock.QueryInterface <PresentationClock>(); }
private static void PlatformPlaySong(Song song) { // Cleanup the last song first. if (State != MediaState.Stopped) { _session.Stop(); _session.ClearTopologies(); _session.Close(); _volumeController.Dispose(); _clock.Dispose(); } // Set the new song. _session.SetTopology(SessionSetTopologyFlags.Immediate, song.Topology); // Get the volume interface. IntPtr volumeObj; try { MediaFactory.GetService(_session, MRPolicyVolumeService, SimpleAudioVolumeGuid, out volumeObj); } catch { MediaFactory.GetService(_session, MRPolicyVolumeService, SimpleAudioVolumeGuid, out volumeObj); } _volumeController = CppObject.FromPointer <SimpleAudioVolume>(volumeObj); _volumeController.Mute = _isMuted; _volumeController.MasterVolume = _volume; // Get the clock. _clock = _session.Clock.QueryInterface <PresentationClock>(); //create the callback if it hasn't been created yet if (_callback == null) { _callback = new Callback(); _session.BeginGetEvent(_callback, null); } // Start playing. var varStart = new Variant(); _session.Start(null, varStart); }
private void PlatformStop() { _session.ClearTopologies(); _session.Stop(); _session.Close(); if (_volumeController != null) { _volumeController.Dispose(); _volumeController = null; } if (_clock != null) { _clock.Dispose(); } _clock = null; }
private void PlatformPlay() { // Cleanup the last song first. if (State != MediaState.Stopped) { _session.Stop(); _session.ClearTopologies(); _session.Close(); if (_volumeController != null) { _volumeController.Dispose(); _volumeController = null; } _clock.Dispose(); } //create the callback if it hasn't been created yet if (_callback == null) { _callback = new Callback(this); _session.BeginGetEvent(_callback, null); } // Set the new song. _session.SetTopology(SessionSetTopologyFlags.Immediate, _currentVideo.Topology); // Get the clock. _clock = _session.Clock.QueryInterface <PresentationClock>(); // Start playing. var varStart = new Variant(); _session.Start(null, varStart); // we need to dispose of the old texture if we have one if (_videoCache != null) { _videoCache.Dispose(); } // Create cached texture _videoCache = new Texture2D(_graphicsDevice, _currentVideo.Width, _currentVideo.Height, false, SurfaceFormat.Bgr32); }
public override void Dispose() { FLLog.Info("Video", "Closing Windows Media Foundation backend"); if (session != null) { session.Stop(); session.ClearTopologies(); //Sample grabber thread works asynchronously (as task), so we need give him a time, to understand, that session is closed //minimal time to wait: 33 ms (1000 ms / 30 fps), but I decide to use a little more System.Threading.Thread.Sleep(100); session.Close(); session.Dispose(); session = null; } if (topology != null) { topology.Dispose(); topology = null; } if (videoSampler != null) { videoSampler.Dispose(); videoSampler = null; } if (clock != null) { clock.Dispose(); clock = null; } if (_texture != null) { _texture.Dispose(); _texture = null; } if (cb != null) { cb.Dispose(); cb = null; } }
public void Close() { logger.Debug("D3D9RendererSink::Close()"); if (presentationClock != null) { presentationClock.Dispose(); presentationClock = null; } if (videoRenderer != null) { videoRenderer.Close(); videoRenderer = null; } if (decoder != null) { decoder.Close(); decoder = null; } }
public HResult SetPresentationClock(IMFPresentationClock pPresentationClock) { Debug.WriteLine("MediaSink:SetPresentationClock"); HResult hr = S_OK; lock (this) { if (IsShutdown) { return(MF_E_SHUTDOWN); } if (PresentationClock != null) { hr = PresentationClock.RemoveClockStateSink(this); if (Failed(hr)) { return(hr); } } if (pPresentationClock != null) { hr = pPresentationClock.AddClockStateSink(this); if (Failed(hr)) { return(hr); } } if (pPresentationClock != null) { PresentationClock = pPresentationClock; } } return(hr); }
private static void PlaySong(Song song) { #if WINDOWS_MEDIA_ENGINE _mediaEngineEx.Source = song.FilePath; _mediaEngineEx.Load(); _mediaEngineEx.Play(); #elif WINDOWS_MEDIA_SESSION // Cleanup the last song first. if (State != MediaState.Stopped) { _session.Stop(); _volumeController.Dispose(); _clock.Dispose(); } // Set the new song. _session.SetTopology(0, song.GetTopology()); // Get the volume interface. IntPtr volumeObj; MediaFactory.GetService(_session, MRPolicyVolumeService, SimpleAudioVolumeGuid, out volumeObj); _volumeController = CppObject.FromPointer <SimpleAudioVolume>(volumeObj); _volumeController.Mute = _isMuted; _volumeController.MasterVolume = _volume; // Get the clock. _clock = _session.Clock.QueryInterface <PresentationClock>(); // Start playing. var varStart = new Variant(); _session.Start(null, varStart); #else song.SetEventHandler(OnSongFinishedPlaying); song.Volume = _isMuted ? 0.0f : _volume; song.Play(); #endif State = MediaState.Playing; }
private void PlatformPlay() { // Cleanup the last song first. if (State != MediaState.Stopped) { _session.Stop(); _volumeController.Dispose(); _clock.Dispose(); } // Set the new song. _session.SetTopology(0, _currentVideo.Topology); _volumeController = CppObject.FromPointer<SimpleAudioVolume>(GetVolumeObj(_session)); _volumeController.Mute = IsMuted; _volumeController.MasterVolume = _volume; // Get the clock. _clock = _session.Clock.QueryInterface<PresentationClock>(); //create the callback if it hasn't been created yet if (_callback == null) { _callback = new Callback(); _session.BeginGetEvent(_callback, null); } // Start playing. var varStart = new Variant(); _session.Start(null, varStart); }
private void buttonSetup_Click(object sender, EventArgs e) { logger.Debug("buttonSetup_Click(...)"); sourceVideoFile = textBox1.Text; videoForm = new VideoForm { BackColor = Color.Black, //ClientSize = new Size(sampleArgs.Width, sampleArgs.Height), StartPosition = FormStartPosition.CenterScreen, }; videoForm.Visible = true; if (presentationClock != null) { presentationClock.Dispose(); presentationClock = null; } MediaFactory.CreatePresentationClock(out presentationClock); PresentationTimeSource timeSource = null; try { MediaFactory.CreateSystemTimeSource(out timeSource); presentationClock.TimeSource = timeSource; } finally { timeSource?.Dispose(); } videoRenderer = new MfVideoRendererEx(); videoRenderer.Init(videoForm.Handle, presentationClock); int sinkRequestSample = 0; int count = 0; fileSource = new VideoFileSource(); fileSource.Setup(sourceVideoFile, videoRenderer.D3DDeviceManager); //videoRenderer.Prerolled += () => //{ // presentationClock.Start(MfTool.SecToMfTicks(1)); //}; fileSource.SampleReady += (flags, sample) => { var _flags = (SourceReaderFlags)flags; if (_flags == SourceReaderFlags.StreamTick) { } else if (_flags == SourceReaderFlags.Currentmediatypechanged) { var newMediaType = fileSource.GetCurrentMediaType(); //var log = MfTool.LogMediaType(newMediaType); //Console.WriteLine("================Currentmediatypechanged======================"); //Console.WriteLine(log); videoRenderer?.SetMediaType(newMediaType); newMediaType.Dispose(); } if (sample != null) { var presentationTime = presentationClock.Time; var sampleTime = sample.SampleTime; var diff = sampleTime - presentationTime; if (diff > 0) { var delay = (int)(MfTool.MfTicksToSec(diff) * 1000); Console.WriteLine("Delay " + delay); Thread.Sleep(delay); } sample.SampleTime = 0; sample.SampleDuration = 0; //videoRenderer.ProcessDxva2Sample(sample); videoRenderer._ProcessDxva2Sample(sample); //videoRenderer.ProcessSample(sample); sample.Dispose(); count++; } }; fileSource.SourceStopped += () => { Console.WriteLine("fileSource.SourceStopped()"); fileSource.Close(); }; var srcMediaType = fileSource.GetCurrentMediaType(); var subType = srcMediaType.Get(MediaTypeAttributeKeys.Subtype); var frameSize = srcMediaType.Get(MediaTypeAttributeKeys.FrameSize); var frameRate = srcMediaType.Get(MediaTypeAttributeKeys.FrameRate); using (var mediaType = new SharpDX.MediaFoundation.MediaType()) { mediaType.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Video); mediaType.Set(MediaTypeAttributeKeys.Subtype, VideoFormatGuids.NV12); // mediaType.Set(MediaTypeAttributeKeys.FrameSize, frameSize); mediaType.Set(MediaTypeAttributeKeys.InterlaceMode, 2); mediaType.Set(MediaTypeAttributeKeys.AllSamplesIndependent, 1); mediaType.Set(MediaTypeAttributeKeys.FrameRate, frameRate); videoRenderer.SetMediaType(mediaType); } videoRenderer.RequestSample += () => { //if (presentationClock != null) //{ // presentationClock.GetState(0, out var clockState); // if(clockState == ClockState.Running) // { // //fileSource.NextSample(); // } //} fileSource.NextSample(); sinkRequestSample++; }; videoRenderer.RendererStopped += () => { videoRenderer.Close(); GC.Collect(); }; videoRenderer.Resize(videoForm.ClientRectangle); videoForm.Paint += (o, a) => { videoRenderer.Repaint(); }; videoForm.SizeChanged += (o, a) => { var rect = videoForm.ClientRectangle; //Console.WriteLine(rect); videoRenderer.Resize(rect); }; }
private void PlatformStop() { _session.ClearTopologies(); _session.Stop(); _session.Close(); if (_volumeController != null) { _volumeController.Dispose(); _volumeController = null; } _clock.Dispose(); _clock = null; }
private void PlatformPlay() { // Cleanup the last song first. if (State != MediaState.Stopped) { _session.Stop(); _session.ClearTopologies(); _session.Close(); if (_volumeController != null) { _volumeController.Dispose(); _volumeController = null; } _clock.Dispose(); } //create the callback if it hasn't been created yet if (_callback == null) { _callback = new Callback(this); _session.BeginGetEvent(_callback, null); } // Set the new song. _session.SetTopology(SessionSetTopologyFlags.Immediate, _currentVideo.Topology); // Get the clock. _clock = _session.Clock.QueryInterface<PresentationClock>(); // Start playing. var varStart = new Variant(); _session.Start(null, varStart); }
private void buttonSetup_Click(object sender, EventArgs e) { logger.Debug("buttonSetup_Click(...)"); if (presentationClock != null) { presentationClock.Dispose(); presentationClock = null; } MediaFactory.CreatePresentationClock(out presentationClock); PresentationTimeSource timeSource = null; try { MediaFactory.CreateSystemTimeSource(out timeSource); presentationClock.TimeSource = timeSource; } finally { timeSource?.Dispose(); } videoForm = new VideoForm { BackColor = Color.Black, //ClientSize = new Size(sampleArgs.Width, sampleArgs.Height), StartPosition = FormStartPosition.CenterScreen, }; videoRenderer = new MfVideoRenderer(); videoRenderer.RendererStarted += Renderer_RendererStarted; videoRenderer.RendererStopped += Renderer_RendererStopped; videoForm.Paint += (o, a) => { videoRenderer.Repaint(); }; videoForm.SizeChanged += (o, a) => { var rect = videoForm.ClientRectangle; //Console.WriteLine(rect); videoRenderer.Resize(rect); }; videoForm.Visible = true; videoRenderer.Setup(new VideoRendererArgs { hWnd = videoForm.Handle, // FourCC = new FourCC("NV12"), //FourCC = 0x59565955, //"UYVY", FourCC = new FourCC((int)Format.A8R8G8B8), Resolution = new Size(1920, 1080), FrameRate = new Tuple <int, int>(30, 1), }); videoRenderer.SetPresentationClock(presentationClock); videoRenderer.RendererStopped += () => { videoRenderer.Close(); GC.Collect(); }; videoRenderer.Resize(videoForm.ClientRectangle); sampleSource = new SampleSource(); bool isFirstTimestamp = true; long timeAdjust = 0; sampleSource.SampleReady += (sample) => { if (isFirstTimestamp) { var _sampleTime = sample.SampleTime; var presetnationTime = presentationClock.Time; var stopwatchTime = MfTool.SecToMfTicks(stopwatch.ElapsedMilliseconds / 1000.0); timeAdjust = presetnationTime - _sampleTime; //stopwatchTime; Console.WriteLine(presetnationTime + " - " + _sampleTime + " = " + timeAdjust); isFirstTimestamp = false; } //var sampleTime = sample.SampleTime; //var presetnationTime = presentationClock.Time; //var diff = sampleTime - presetnationTime; //Console.WriteLine(MfTool.MfTicksToSec(sampleTime) + " " + MfTool.MfTicksToSec(presetnationTime) + " " + MfTool.MfTicksToSec(diff)); //var stopwatchTime = MfTool.SecToMfTicks(stopwatch.ElapsedMilliseconds / 1000.0); //var diff2 = stopwatchTime - presetnationTime; //Console.WriteLine (MfTool.MfTicksToSec(stopwatchTime) + " " + MfTool.MfTicksToSec(presetnationTime) + " " + MfTool.MfTicksToSec(diff2)); var sampleTime = sample.SampleTime; sample.SampleTime = sampleTime + timeAdjust; //sample.SampleDuration = 0; videoRenderer?.ProcessSample(sample); //sample?.Dispose(); }; }
public void OnSetPresentationClock(PresentationClock presentationClockRef) { }
private static void PlatformPlaySong(Song song) { // Cleanup the last song first. if (State != MediaState.Stopped) { _session.Stop(); _session.ClearTopologies(); _session.Close(); _volumeController.Dispose(); _clock.Dispose(); } // Set the new song. _session.SetTopology(SessionSetTopologyFlags.Immediate, song.Topology); // Get the volume interface. _volumeController = CppObject.FromPointer<AudioStreamVolume>(MediaPlayer.GetVolumeObj(_session)); SetChannelVolumes(); // Get the clock. _clock = _session.Clock.QueryInterface<PresentationClock>(); //create the callback if it hasn't been created yet if (_callback == null) { _callback = new Callback(); _session.BeginGetEvent(_callback, null); } // Start playing. var varStart = new Variant(); _session.Start(null, varStart); }
public override void PlayFile(string filename) { //Load the file MediaSource mediaSource; { var resolver = new SourceResolver(); ObjectType otype; var source = new ComObject(resolver.CreateObjectFromURL(filename, SourceResolverFlags.MediaSource, null, out otype)); try { // Sometimes throws HRESULT: [0x80004002], Module: [General], ApiCode: [E_NOINTERFACE/No such interface supported], Message: No such interface supported. Bug? mediaSource = source.QueryInterface <MediaSource>(); } catch (SharpDXException) { mediaSource = null; FLLog.Error("VideoPlayerWMF", "QueryInterface failed on Media Foundation"); } resolver.Dispose(); source.Dispose(); } if (mediaSource is null) { return; } PresentationDescriptor presDesc; mediaSource.CreatePresentationDescriptor(out presDesc); for (int i = 0; i < presDesc.StreamDescriptorCount; i++) { SharpDX.Mathematics.Interop.RawBool selected; StreamDescriptor desc; presDesc.GetStreamDescriptorByIndex(i, out selected, out desc); if (selected) { TopologyNode sourceNode; MediaFactory.CreateTopologyNode(TopologyType.SourceStreamNode, out sourceNode); sourceNode.Set(TopologyNodeAttributeKeys.Source, mediaSource); sourceNode.Set(TopologyNodeAttributeKeys.PresentationDescriptor, presDesc); sourceNode.Set(TopologyNodeAttributeKeys.StreamDescriptor, desc); TopologyNode outputNode; MediaFactory.CreateTopologyNode(TopologyType.OutputNode, out outputNode); var majorType = desc.MediaTypeHandler.MajorType; if (majorType == MediaTypeGuids.Video) { Activate activate; videoSampler = new MFSamples(); //retrieve size of video long sz = desc.MediaTypeHandler.CurrentMediaType.Get <long>(new Guid("{1652c33d-d6b2-4012-b834-72030849a37d}")); int height = (int)(sz & uint.MaxValue), width = (int)(sz >> 32); _texture = new Texture2D(width, height, false, SurfaceFormat.Color); mt = new MediaType(); mt.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Video); // Specify that we want the data to come in as RGB32. mt.Set(MediaTypeAttributeKeys.Subtype, new Guid("00000016-0000-0010-8000-00AA00389B71")); GetMethods(); MFCreateSampleGrabberSinkActivate(mt, videoSampler, out activate); outputNode.Object = activate; } if (majorType == MediaTypeGuids.Audio) { Activate activate; MediaFactory.CreateAudioRendererActivate(out activate); outputNode.Object = activate; } topology.AddNode(sourceNode); topology.AddNode(outputNode); sourceNode.ConnectOutput(0, outputNode, 0); sourceNode.Dispose(); outputNode.Dispose(); } desc.Dispose(); } presDesc.Dispose(); mediaSource.Dispose(); //Play the file cb = new MFCallback(this, session); session.BeginGetEvent(cb, null); session.SetTopology(SessionSetTopologyFlags.Immediate, topology); // Get the clock clock = session.Clock.QueryInterface <PresentationClock>(); // Start playing. Playing = true; }
private static void PlatformPlaySong(Song song) { // Cleanup the last song first. if (State != MediaState.Stopped) { _session.Stop(); _session.ClearTopologies(); _session.Close(); _volumeController.Dispose(); _clock.Dispose(); } // Set the new song. _session.SetTopology(SessionSetTopologyFlags.Immediate, song.Topology); // Get the volume interface. IntPtr volumeObj; try { MediaFactory.GetService(_session, MRPolicyVolumeService, SimpleAudioVolumeGuid, out volumeObj); } catch { MediaFactory.GetService(_session, MRPolicyVolumeService, SimpleAudioVolumeGuid, out volumeObj); } _volumeController = CppObject.FromPointer<SimpleAudioVolume>(volumeObj); _volumeController.Mute = _isMuted; _volumeController.MasterVolume = _volume; // Get the clock. _clock = _session.Clock.QueryInterface<PresentationClock>(); //create the callback if it hasn't been created yet if (_callback == null) { _callback = new Callback(); _session.BeginGetEvent(_callback, null); } // Start playing. var varStart = new Variant(); _session.Start(null, varStart); }