Ejemplo n.º 1
0
            public void Start1()
            {
                var flags = DeviceCreationFlags.VideoSupport |
                            DeviceCreationFlags.BgraSupport |
                            DeviceCreationFlags.Debug;

                var device = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware, flags);

                using (var multiThread = device.QueryInterface <SharpDX.Direct3D11.Multithread>())
                {
                    multiThread.SetMultithreadProtected(true);
                }


                System.Drawing.Bitmap bmp        = new System.Drawing.Bitmap(@"D:\Temp\4.bmp");
                Texture2D             rgbTexture = DxTool.GetTexture(bmp, device);

                var bufTexture = new Texture2D(device,
                                               new Texture2DDescription
                {
                    // Format = Format.NV12,
                    Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                    Width             = 1920,
                    Height            = 1080,
                    MipLevels         = 1,
                    ArraySize         = 1,
                    SampleDescription = { Count = 1 },
                });

                device.ImmediateContext.CopyResource(rgbTexture, bufTexture);

                var processor  = new MfVideoProcessor(device);
                var inProcArgs = new MfVideoArgs
                {
                    Width  = 1920,
                    Height = 1080,
                    Format = SharpDX.MediaFoundation.VideoFormatGuids.Argb32,
                };



                var outProcArgs = new MfVideoArgs
                {
                    Width  = 1920,
                    Height = 1080,
                    Format = SharpDX.MediaFoundation.VideoFormatGuids.NV12,                    //.Argb32,
                };

                processor.Setup(inProcArgs, outProcArgs);
                processor.Start();


                var rgbSample = MediaFactory.CreateVideoSampleFromSurface(null);

                // Create the media buffer from the texture
                MediaFactory.CreateDXGISurfaceBuffer(typeof(Texture2D).GUID, bufTexture, 0, false, out var mediaBuffer);

                using (var buffer2D = mediaBuffer.QueryInterface <Buffer2D>())
                {
                    mediaBuffer.CurrentLength = buffer2D.ContiguousLength;
                }

                rgbSample.AddBuffer(mediaBuffer);

                rgbSample.SampleTime     = 0;
                rgbSample.SampleDuration = 0;

                var result = processor.ProcessSample(rgbSample, out var nv12Sample);

                Task.Run(() =>
                {
                    Stopwatch sw = new Stopwatch();
                    int fps      = 60;
                    int interval = (int)(1000.0 / fps);

                    int _count = 1;

                    long globalTime = 0;


                    while (true)
                    {
                        if (result)
                        {
                            globalTime += sw.ElapsedMilliseconds;
                            sw.Restart();


                            nv12Sample.SampleTime     = MfTool.SecToMfTicks((globalTime / 1000.0));
                            nv12Sample.SampleDuration = MfTool.SecToMfTicks(((int)interval / 1000.0));

                            //sample.SampleTime = MfTool.SecToMfTicks((globalTime / 1000.0));
                            //sample.SampleDuration = MfTool.SecToMfTicks(((int)interval / 1000.0));

                            SampleReady?.Invoke(nv12Sample);


                            var msec = sw.ElapsedMilliseconds;

                            var delay = interval - msec;
                            if (delay < 0)
                            {
                                delay = 1;
                            }

                            // var delay = 1;
                            Thread.Sleep((int)delay);
                            var elapsedMilliseconds = sw.ElapsedMilliseconds;
                            globalTime += elapsedMilliseconds;
                            _count++;
                        }

                        //nv12Sample?.Dispose();

                        //Thread.Sleep(30);
                    }
                });
            }
Ejemplo n.º 2
0
        public void Start()
        {
            logger.Debug("VideoCaptureSource::Start()");
            running = true;

            Task.Run(() =>
            {
                processor.Start();

                int sampleCount = 0;

                try
                {
                    while (running)
                    {
                        int actualIndex         = 0;
                        SourceReaderFlags flags = SourceReaderFlags.None;
                        long timestamp          = 0;
                        var sample = sourceReader.ReadSample(SourceReaderIndex.FirstVideoStream, SourceReaderControlFlags.None, out actualIndex, out flags, out timestamp);

                        try
                        {
                            //Console.WriteLine("#" + sampleCount + " Timestamp " + timestamp + " Flags " + flags);

                            if (sample != null)
                            {
                                //Console.WriteLine("SampleTime " + sample.SampleTime + " SampleDuration " + sample.SampleDuration + " SampleFlags " + sample.SampleFlags);
                                Sample outputSample = null;
                                try
                                {
                                    var res = processor.ProcessSample(sample, out outputSample);

                                    if (res)
                                    {
                                        //Console.WriteLine("outputSample!=null" + (outputSample != null));

                                        var mediaBuffer = outputSample.ConvertToContiguousBuffer();
                                        var ptr         = mediaBuffer.Lock(out int cbMaxLengthRef, out int cbCurrentLengthRef);

                                        //var width = outProcArgs.Width;
                                        //var height = outProcArgs.Height;

                                        var dataBox = device.ImmediateContext.MapSubresource(texture, 0, MapMode.Read, MapFlags.None);

                                        Kernel32.CopyMemory(dataBox.DataPointer, ptr, (uint)cbCurrentLengthRef);

                                        device.ImmediateContext.UnmapSubresource(texture, 0);


                                        device.ImmediateContext.CopyResource(texture, SharedTexture);
                                        device.ImmediateContext.Flush();

                                        OnBufferUpdated();

                                        //GDI.Bitmap bmp = new GDI.Bitmap(width, height, GDI.Imaging.PixelFormat.Format32bppArgb);

                                        //DxTool.TextureToBitmap(texture, bmp);

                                        ////var bmpData = bmp.LockBits(new GDI.Rectangle(0, 0, width, height), GDI.Imaging.ImageLockMode.WriteOnly, bmp.PixelFormat);
                                        ////uint size = (uint)(bmpData.Stride * height);
                                        ////Kernel32.CopyMemory(bmpData.Scan0, ptr, size);
                                        ////bmp.UnlockBits(bmpData);

                                        ////var fileName = @"d:\BMP\" + "#" + sampleCount + "_" + timestamp + ".bmp";
                                        ////bmp.Save(fileName, GDI.Imaging.ImageFormat.Bmp);

                                        //bmp.Dispose();

                                        mediaBuffer.Unlock();
                                        mediaBuffer?.Dispose();
                                    }
                                }
                                catch (Exception ex)
                                {
                                    logger.Error(ex);
                                }
                                finally
                                {
                                    if (outputSample != null)
                                    {
                                        outputSample.Dispose();
                                    }
                                }
                            }
                        }
                        finally
                        {
                            sample?.Dispose();
                        }

                        sampleCount++;
                    }
                }
                catch (Exception ex)
                {
                    logger.Error(ex);
                }
                finally
                {
                    if (processor != null)
                    {
                        processor.Stop();
                    }
                }
            });
        }