Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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;
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
 public override void Dispose()
 {
     FLLog.Info("Video", "Closing Windows Media Foundation backend");
     if (session != null)
     {
         session.ClearTopologies();
         session.Stop();
         session.Close();
         session.Dispose();
     }
     if (topology != null)
     {
         topology.Dispose();
     }
     if (videoSampler != null)
     {
         videoSampler.Dispose();
     }
     if (clock != null)
     {
         clock.Dispose();
     }
     if (_texture != null)
     {
         _texture.Dispose();
     }
     if (cb != null)
     {
         cb.Dispose();
     }
 }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        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;
        }
 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;
     }
 }
Ejemplo n.º 10
0
        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;
            }
        }
Ejemplo n.º 11
0
        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();
            };
        }