Exemplo n.º 1
0
 public static NvEncInputPtr AsInputPointer(
     this NvEncRegisterResource resource)
 {
     return(new NvEncInputPtr
     {
         Handle = resource.RegisteredResource.Handle
     });
 }
Exemplo n.º 2
0
        private void Run(ProgramArguments args)
        {
            using var duplicate = GetDisplayDuplicate(
                      args.DisplayName, out var outputDescription);
            using var output = File.OpenWrite(args.OutputPath);

            Console.WriteLine($"Process: {(Environment.Is64BitProcess ? "64" : "32")} bits");
            Console.WriteLine($"Display: {outputDescription.DeviceName}");
            Console.WriteLine($"Output: {output.Name}");

            while (true)
            {
                // Get the next screen image.
                duplicate.AcquireNextFrame(500,
                                           out var frameInfo, out var resourceOut);

                // If the frame has not changed, there's no reason to encode
                // a new frame.
                if (frameInfo.LastPresentTime == 0)
                {
                    duplicate.ReleaseFrame();
                    resourceOut.Dispose();
                    Thread.Sleep(_frameDuration);
                    continue;
                }

                var desktopTexture = resourceOut.QueryInterface <Texture2D>();
                var encoder        = _initialized
                    ? _encoder
                    : CreateEncoder(desktopTexture);

                var desc = desktopTexture.Description;

                var reg = new NvEncRegisterResource
                {
                    Version            = NV_ENC_REGISTER_RESOURCE_VER,
                    BufferFormat       = NvEncBufferFormat.Abgr,
                    BufferUsage        = NvEncBufferUsage.NvEncInputImage,
                    ResourceToRegister = desktopTexture.NativePointer,
                    Width  = (uint)desc.Width,
                    Height = (uint)desc.Height,
                    Pitch  = 0
                };

                // Registers the hardware texture surface as a resource for
                // NvEnc to use.
                using var _ = _encoder.RegisterResource(ref reg);

                var pic = new NvEncPicParams
                {
                    Version         = NV_ENC_PIC_PARAMS_VER,
                    PictureStruct   = NvEncPicStruct.Frame,
                    InputBuffer     = reg.AsInputPointer(),
                    BufferFmt       = NvEncBufferFormat.Abgr,
                    InputWidth      = (uint)desc.Width,
                    InputHeight     = (uint)desc.Height,
                    OutputBitstream = _bitstreamBuffer.BitstreamBuffer,
                    InputTimeStamp  = (ulong)frameInfo.LastPresentTime,
                    InputDuration   = _frameDuration
                };

                // Do the actual encoding. With this configuration this is done
                // sync (blocking).
                encoder.EncodePicture(ref pic);

                // The output is written to the bitstream, which is now copied
                // to the output file.
                using (var sm = encoder.LockBitstreamAndCreateStream(
                           ref _bitstreamBuffer))
                {
                    lock (_writeMutex)
                    {
                        sm.CopyTo(output);
                    }
                }

                desktopTexture.Dispose();
                duplicate.ReleaseFrame();
                resourceOut.Dispose();

                Thread.Sleep(_frameDuration);
            }

            // ReSharper disable once FunctionNeverReturns
        }