public void Resize(System.Drawing.Rectangle rect)
 {
     if (videoRenderer != null)
     {
         videoRenderer.Resize(rect);
     }
 }
Example #2
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();
            };
        }
        public void Setup(VideoEncoderSettings settings, IntPtr hwnd)
        {
            logger.Debug("D3D9RendererSink::Setup()");

            this.hWnd            = hwnd;
            this.EncoderSettings = settings;


            var avgTimePerFrame = MfTool.FrameRateToAverageTimePerFrame(EncoderSettings.FrameRate);

            this.EncoderSettings.AverageTimePerFrame = avgTimePerFrame;

            MediaFactory.CreatePresentationClock(out presentationClock);

            PresentationTimeSource timeSource = null;

            try
            {
                MediaFactory.CreateSystemTimeSource(out timeSource);
                presentationClock.TimeSource = timeSource;
            }
            finally
            {
                timeSource?.Dispose();
            }


            videoRenderer = new MfVideoRenderer();


            //TODO: нужно настраивать когда декодер пришлет свой формат
            videoRenderer.Setup(new VideoRendererArgs
            {
                hWnd   = hWnd,
                FourCC = new SharpDX.Multimedia.FourCC("NV12"),
                //FourCC = 0x59565955, //"UYVY",
                Resolution = settings.Resolution, //


                //Resolution = new System.Drawing.Size(1920, 1088),
                FrameRate = settings.FrameRate, //new Tuple<int, int>(settings.FrameRate, 1),
            });

            videoRenderer.RendererStarted += VideoRenderer_RendererStarted;
            videoRenderer.RendererStopped += VideoRenderer_RendererStopped;

            videoRenderer.SetPresentationClock(presentationClock);
            videoRenderer.Resize(new System.Drawing.Rectangle(0, 0, 100, 100));

            SharpDX.MediaFoundation.DirectX.Direct3DDeviceManager d3dManager = null;
            if (EncoderSettings.UseHardware)
            {
                d3dManager = videoRenderer.D3DDeviceManager;
            }

            decoder = new MfH264Dxva2Decoder(d3dManager);

            var inputArgs = new MfVideoArgs
            {
                Width      = EncoderSettings.Resolution.Width,
                Height     = EncoderSettings.Resolution.Height,
                FrameRate  = MfTool.PackToLong(EncoderSettings.FrameRate),
                LowLatency = EncoderSettings.LowLatency,
            };

            decoder.Setup(inputArgs);
        }