Example #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);
                    }
                });
            }
Example #2
0
        public void Open(VideoEncoderSettings encoderSettings)
        {
            logger.Debug("VideoEncoder::Setup(...)");

            //var hwContext = videoSource.hwContext;
            // var hwDevice = hwContext.Device3D11;

            var hwBuffer = videoSource.SharedTexture;

            var hwDescr = hwBuffer.Description;

            var srcSize   = new Size(hwDescr.Width, hwDescr.Height);
            var srcFormat = MfTool.GetVideoFormatGuidFromDXGIFormat(hwDescr.Format);

            var destSize = encoderSettings.Resolution;            //new Size(destParams.Width, destParams.Height);

            var adapterId = videoSource.AdapterId;

            using (var adapter = DxTool.FindAdapter1(adapterId))
            {
                var descr        = adapter.Description;
                int adapterVenId = descr.VendorId;

                logger.Info("Adapter: " + descr.Description + " " + adapterVenId);

                var flags = DeviceCreationFlags.VideoSupport |
                            DeviceCreationFlags.BgraSupport;
                //DeviceCreationFlags.Debug;

                device = new SharpDX.Direct3D11.Device(adapter, flags);
                using (var multiThread = device.QueryInterface <SharpDX.Direct3D11.Multithread>())
                {
                    multiThread.SetMultithreadProtected(true);
                }
            }
            var profile = MfTool.GetMfH264Profile(encoderSettings.Profile);

            var bitrateMode = MfTool.GetMfBitrateMode(encoderSettings.BitrateMode);

            var aspectRatio = encoderSettings.AspectRatio;
            var encArgs     = new MfVideoArgs
            {
                Width  = destSize.Width,                //srcSize.Width,
                Height = destSize.Height,               //srcSize.Height,
                Format = VideoFormatGuids.NV12,         //VideoFormatGuids.Argb32,

                FrameRate = MfTool.PackToLong(encoderSettings.FrameRate),

                MaxBitrate  = encoderSettings.MaxBitrate * 1000, //kbps->bps
                AvgBitrate  = encoderSettings.Bitrate * 1000,
                LowLatency  = encoderSettings.LowLatency,
                AdapterId   = videoSource.AdapterId,
                Profile     = profile,
                BitrateMode = bitrateMode,
                GopSize     = encoderSettings.GOPSize,
                Quality     = encoderSettings.Quality,

                EncoderId = encoderSettings.EncoderId,

                AspectRatio = MfTool.PackToLong(aspectRatio)
            };



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

            var outProcArgs = new MfVideoArgs
            {
                Width  = encArgs.Width,
                Height = encArgs.Height,
                Format = encArgs.Format,                 //VideoFormatGuids.NV12,//.Argb32,
            };

            processor.Setup(inProcArgs, outProcArgs);


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

            processor?.Start();

            var encoderName = encoderSettings.EncoderId;

            if (encoderName == "libx264" || encoderName == "h264_nvenc")
            {
                encoder = new MfFFMpegVideoEncoder();
            }
            else
            {
                encoder = new MfH264EncoderEx(device);
            }

            encoder.Setup(encArgs);

            encoder.DataEncoded += Encoder_DataEncoded;

            ////encoder.DataReady += MfEncoder_DataReady;
            encoder.Start();
        }
Example #3
0
        static void Main(string[] args)
        {
            MediaToolkitManager.Startup();


            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);
            }
            var featureLevel = _device.FeatureLevel;

            using (var dxgiDevice = _device.QueryInterface <SharpDX.DXGI.Device>())
            {
                using (var adapter = dxgiDevice.Adapter)
                {
                    var descr = adapter.Description;
                    Console.WriteLine(string.Join(" ", descr.Description));
                }
            }

            Console.WriteLine(DxTool.LogDxInfo());

            Console.ReadKey();
            return;

            // NewMethod1();



            ////DxTool.FindAdapter1(4313);

            var videoEncoders = MfTool.FindVideoEncoders();

            foreach (var enc in videoEncoders)
            {
                Console.WriteLine(enc.ToString());
            }


            foreach (var enc in videoEncoders)
            {
                if (enc.Format == VideoCodingFormat.H264 && enc.Activatable)
                {
                    Console.WriteLine(enc.Name + " " + enc.ClsId + " isHardware: " + enc.IsHardware);
                }
            }

            Console.ReadKey();
            Console.WriteLine("-------------------------------");
            return;


            //NewMethod();


            //return;



            //MarshalHelper.ToArray(ppOutputTypes, (int)inputTypesNum, out MFTRegisterTypeInfo[] outputTypes);

            //MediaAttributes mediaAttributes = new MediaAttributes(ppAttributes);


            //Console.WriteLine(MfTool.LogMediaAttributes(mediaAttributes));



            //Guid VideoProcessorMFT = new Guid("88753B26-5B24-49BD-B2E7-0C445C78C982");

            //SharpDX.MediaFoundation.MediaFactory.TGetInfo(VideoProcessorMFT, IntPtr.Zero,
            //	null, out int cInputTypesRef,
            //	null, out int cOutputTypesRef,
            //	out SharpDX.MediaFoundation.MediaAttributes attrs);


            //var transformFlags = TransformEnumFlag.All | // TransformEnumFlag.All |
            //		 TransformEnumFlag.SortAndFilter;

            //var outputType = new TRegisterTypeInformation
            //{
            //	GuidMajorType = MediaTypeGuids.Video,
            //	GuidSubtype = VideoFormatGuids.H264
            //	// GuidSubtype = VideoFormatGuids.Hevc
            //};
            //var category = SharpDX.MediaFoundation.TransformCategoryGuids.VideoDecoder;
            //MediaAttributes mediaAttributes = new MediaAttributes();
            //Activate[] activates = new Activate[1024];

            //SharpDX.MediaFoundation.MediaFactory.TEnum2(category, (int)transformFlags, null, outputType, mediaAttributes, activates, out int activatesNum);



            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);
            }


            //var descr = new SharpDX.Direct3D11.Texture2DDescription
            //{
            //	Width = 1920,
            //	Height = 1080,
            //	MipLevels = 1,
            //	ArraySize = 1,
            //	SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
            //	Usage = SharpDX.Direct3D11.ResourceUsage.Default,
            //	Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
            //	BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource,
            //	CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
            //	OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None,

            //};

            //var texture = new SharpDX.Direct3D11.Texture2D(device, descr);


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

            //Texture2D stagingTexture = null;
            //try
            //{
            //    // Create Staging texture CPU-accessible
            //    stagingTexture = new Texture2D(device,
            //        new Texture2DDescription
            //        {
            //            CpuAccessFlags = CpuAccessFlags.Read,
            //            BindFlags = BindFlags.None,
            //            Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
            //            Width = 1920,
            //            Height = 1080,
            //            MipLevels = 1,
            //            ArraySize = 1,
            //            SampleDescription = { Count = 1, Quality = 0 },
            //            Usage = ResourceUsage.Staging,
            //            OptionFlags = ResourceOptionFlags.None,
            //        });

            //    device.ImmediateContext.CopyResource(texture, stagingTexture);
            //    device.ImmediateContext.Flush();

            //    //var destBmp = new System.Drawing.Bitmap(1920, 1080, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            //    System.Drawing.Bitmap destBmp = null;
            //    int count = 10;

            //    byte[] bytes = null;
            //    while (count-- > 0)
            //    {
            //        DxTool.TextureToBitmap(stagingTexture, ref destBmp);

            //        bytes = DxTool.GetTextureBytes(stagingTexture);



            //        Thread.Sleep(10);
            //    }

            //    File.WriteAllBytes("d:\\test_argb32.raw", bytes);

            //    destBmp.Save("d:\\test.bmp");

            //    destBmp.Dispose();

            //}
            //finally
            //{
            //    stagingTexture?.Dispose();
            //}

            //texture.Dispose();
            //device.Dispose();
            //bmp.Dispose();

            //var report = SharpDX.Diagnostics.ObjectTracker.ReportActiveObjects();
            //Console.WriteLine(report);
            //Console.ReadKey();


            //return;



            //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);
            //}



            do
            {
                // var videoSource = new ScreenSource();
                // ScreenCaptureDeviceDescription captureParams = new ScreenCaptureDeviceDescription
                // {
                //     CaptureRegion = new System.Drawing.Rectangle(0, 0, 1920, 1080),
                //     Resolution = new System.Drawing.Size(1920, 1080),
                // };

                // captureParams.CaptureType = VideoCaptureType.DXGIDeskDupl;
                // captureParams.Fps = 10;
                // captureParams.CaptureMouse = true;

                // videoSource.Setup(captureParams);

                // VideoEncoderSettings encodingParams = new VideoEncoderSettings
                // {
                //     //Width = destSize.Width, // options.Width,
                //     //Height = destSize.Height, // options.Height,
                //     Resolution = new System.Drawing.Size(1920, 1080),
                //     FrameRate = 10,


                // };

                // //var videoEncoder = new VideoEncoder(videoSource);
                // //videoEncoder.Open(encodingParams);
                // var hwBuffer = videoSource.SharedTexture;
                // var hwDevice = hwBuffer.Device;

                // using (var dxgiDevice = hwDevice.QueryInterface<SharpDX.DXGI.Device>())
                // {
                //     using (var adapter = dxgiDevice.Adapter)
                //     {
                //         var adapterLuid = adapter.Description.Luid;
                //     }
                // }
                //// hwDevice.Dispose();
                // // Marshal.Release(hwDevice.NativePointer);

                // //ComObject comObject = new ComObject();
                // //comObject.

                //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);
                //}


                var encoder = new MfH264Encoder(null);
                encoder.Setup(new MfVideoArgs
                {
                    Width     = 1920,
                    Height    = 1080,
                    FrameRate = 30,
                    Format    = SharpDX.MediaFoundation.VideoFormatGuids.Argb32,
                });

                var encDevice = encoder.device;


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



                //var processor = new MfVideoProcessor(encDevice);
                //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();

                encoder.Start();

                int count = 3;
                while (count-- > 0)
                {
                    //encDevice.ImmediateContext.CopyResource(texture, bufTexture);

                    //encoder.WriteTexture(texture);

                    Thread.Sleep(100);

                    Console.WriteLine("Try next " + count);
                }

                encoder.Stop();


                //processor.Stop();
                //processor.Close();

                bufTexture.Dispose();
                texture.Dispose();
                // videoEncoder.Close();



                //videoSource.Close();
                //hwDevice.Dispose();

                //Console.WriteLine(MfTool.GetActiveObjectsReport());

                Thread.Sleep(300);
                //encoder.Close();
                GC.Collect();

                Console.WriteLine("----------------------------------------\r\n" + MfTool.GetActiveObjectsReport());
                Console.WriteLine("Spacebar to next...");
                var key = Console.ReadKey();

                if (key.Key == ConsoleKey.Spacebar)
                {
                    continue;
                }
                else
                {
                    break;
                }
            } while (true);

            Console.WriteLine("Any key to exit...");
            Console.ReadKey();

            MediaToolkitManager.Shutdown();
        }
Example #4
0
        private static void StartTest()
        {
            MediaToolkit.MediaToolkitManager.Startup();

            //var res = DesktopManager.OpenInteractiveProcess("Notepad.exe", "default", false, out AdvApi32.PROCESS_INFORMATION procInfo);
            //Console.WriteLine("OpenInteractiveProcess(...) " + res + " " +  procInfo.dwProcessId);
            //Console.ReadKey();

            SystemEvents.SessionSwitch += (o, a) =>
            {
                Console.WriteLine("SystemEvents.SessionSwitch " + a.Reason);
            };

            SystemEvents.SessionEnded += (o, a) =>
            {
                Console.WriteLine("SystemEvents.SessionEnded " + a.Reason);
            };

            SystemEvents.DisplaySettingsChanged += (o, a) =>
            {
                Console.WriteLine("SystemEvents.DisplaySettingsChanged");
            };

            SystemEvents.UserPreferenceChanged += (o, a) =>
            {
                Console.WriteLine("SystemEvents.UserPreferenceChanged " + a.Category);
            };

            SystemEvents.PowerModeChanged += (o, a) =>
            {
                Console.WriteLine("SystemEvents.PowerModeChanged " + a.Mode);
            };

            var sessions = DesktopManager.GetActiveSessions();

            foreach (var s in sessions)
            {
                Console.WriteLine(string.Join(", ", s.ID, s.Name, s.Type, s.Username));
            }


            var screen = Screen.PrimaryScreen;



            GDICapture capture = new GDICapture();

            capture.UseHwContext = false;

            var srcRect  = screen.Bounds;
            var destSize = srcRect.Size;

            capture.Init(srcRect, destSize);


            var dir = @"D:\Temp\ScreenShots\" + DateTime.Now.ToString("HH_mm_ss_fff");

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            var _decktopName = "";

            while (true)
            {
                try
                {
                    bool result = DesktopManager.SwitchToInputDesktop();

                    if (!result)
                    {
                        Console.WriteLine("DesktopManager.SwitchToInputDesktop() " + result);
                    }

                    result = DesktopManager.GetCurrentDesktop(out string desktopName);
                    if (!result)
                    {
                        Console.WriteLine("DesktopManager.GetCurrentDesktop() " + result);
                    }

                    if (desktopName != _decktopName)
                    {
                        _decktopName = desktopName;
                        Console.WriteLine("Current desktop: " + _decktopName);

                        Console.WriteLine("--------------------------------------");
                    }

                    result = capture.UpdateBuffer();

                    if (result)
                    {
                        var buffer = capture.VideoBuffer;

                        Bitmap bmp = buffer.bitmap;
                        if (capture.UseHwContext)
                        {
                            var texture = capture.SharedTexture;

                            Texture2D stagingTexture = null;
                            try
                            {
                                var device = texture.Device;
                                var descr  = texture.Description;

                                stagingTexture = new Texture2D(device,
                                                               new Texture2DDescription
                                {
                                    CpuAccessFlags    = CpuAccessFlags.Read,
                                    BindFlags         = BindFlags.None,
                                    Format            = Format.B8G8R8A8_UNorm,
                                    Width             = descr.Width,
                                    Height            = descr.Height,
                                    MipLevels         = 1,
                                    ArraySize         = 1,
                                    SampleDescription = { Count = 1, Quality = 0 },
                                    Usage             = ResourceUsage.Staging,
                                    OptionFlags       = ResourceOptionFlags.None,
                                });

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

                                DxTool.TextureToBitmap(stagingTexture, ref bmp);
                            }
                            finally
                            {
                                stagingTexture?.Dispose();
                            }
                        }


                        var timeNow = DateTime.Now.ToString("HH_mm_ss_fff");

                        var fileName = _decktopName + " " + timeNow + ".jpg";

                        var fullName = Path.Combine(dir, fileName);

                        bmp.Save(fullName);
                    }
                    else
                    {
                        Console.WriteLine("capture.UpdateBuffer() " + result);
                    }

                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }



            MediaToolkit.MediaToolkitManager.Shutdown();
        }