示例#1
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();
            };
        }
示例#2
0
        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);
            };
        }