桌面复制API中的AcquireNextFrame()和ReleaseFrame()随机死锁

时间:2018-12-11 21:44:44

标签: directx video-capture ms-media-foundation sharpdx desktop-duplication

我正在尝试编写一个用于将Windows桌面录制到视频文件的应用程序。我正在使用桌面复制API和Media Foundation API。更具体地说,我正在使用Media Foundation的SinkWriter类。我正在使用Desktop Duplication API中的Texture2D个对象,并将它们传递给SinkWriter,这形成了一个很好的硬件加速管道。我还将C#与SharpDX一起使用。

整个项目在大多数情况下都运行良好。它可以在我的Surface Pro上完美运行,可以捕获相当一致的fps视频,而不会出现问题。

问题出在其他硬件上。具体来说,我已经测试了我在带有独立Nvidia GPU的AMD Ryzen上进行的测试。这也很好用..除了通常在8到20秒之间记录桌面之后,该程序将被锁定在outputDuplication.ReleaseFrame()outputDuplication.AcquireNextFrame()上。它不会崩溃,它只会锁定,因此我不确定如何确定发生了什么。我确实知道,这与将桌面纹理传递到sinkWriter.WriteSample()函数有关。如果我删除了这一行,并保持其他所有内容不变,则像往常一样继续创建MediaBuffersMediaSamples,将它们与我的桌面纹理相关联,但不要将示例传递给WriteSample方法,那么应用程序可以愉快地无限期地“记录”。

我已经检查了所有我能想到的潜在错误。我确保应将所有对象都丢弃。我看不到任何内存泄漏。该代码在SetAllocator()上使用MediaSample方法,因此我传递给SinkWriter的纹理被回收了。据我所知,这一切都正常。

我怀疑这里有一些我不了解的基本知识。正确管理SharpDX后面的非托管资源似乎犯了一些错误,但是我真的不知道从哪里开始。此时,任何指针都将不胜感激。

这是主要的捕获代码。这不是全部,而是大部分。我怀疑问题出在这里。

static void WriteFrame(SinkWriter sinkWriter, int steamIndex, long time, long duration, int width, int height, Texture2D texture, TextureAllocator textureAllocator)
{
    MediaBuffer textureBuffer;
    MediaFactory.CreateDXGISurfaceBuffer(texture.GetType().GUID, texture, 0, false, out textureBuffer);
    using (var buffer2d = textureBuffer.QueryInterface<Buffer2D>())
    {
        textureBuffer.CurrentLength = buffer2d.ContiguousLength;
    }

    using (var sample = MediaFactory.CreateVideoSampleFromSurface(null))
    {
        sample.SampleTime = time;
        sample.SampleDuration = duration;
        sample.AddBuffer(textureBuffer);
        using(var trackedSample = sample.QueryInterface<TrackedSample>())
        {
            trackedSample.SetAllocator(textureAllocator, null);
        }
        sinkWriter.WriteSample(steamIndex, sample);
    }
    textureBuffer.Dispose();
}


void captureAction()
{
    MediaManager.Startup(true);

    int streamIndex = 0;

    var whichOutputDevice = 0;
    var whichAdapter = 0;
    using (var factory = new Factory1())
    {

        using (var adapter = factory.GetAdapter1(whichAdapter))
        {
            using (var mDevice = new SharpDX.Direct3D11.Device(adapter))
            {
                using (var dXGIDeviceManager = new DXGIDeviceManager())
                {
                    dXGIDeviceManager.ResetDevice(mDevice);
                    using (var output = adapter.GetOutput(whichOutputDevice))
                    {
                        using (var output1 = output.QueryInterface<Output1>())
                        {
                            var mOutputDesc = output.Description;
                            var mTextureDesc = new Texture2DDescription()
                            {
                                CpuAccessFlags = CpuAccessFlags.Read,
                                BindFlags = BindFlags.None,
                                Format = Format.B8G8R8A8_UNorm,
                                Width = mOutputDesc.DesktopBounds.Right - mOutputDesc.DesktopBounds.Left,
                                Height = mOutputDesc.DesktopBounds.Bottom - mOutputDesc.DesktopBounds.Top,
                                OptionFlags = ResourceOptionFlags.None,
                                MipLevels = 1,
                                ArraySize = 1,
                                SampleDescription = { Count = 1, Quality = 0 },
                                Usage = ResourceUsage.Staging
                            };
                            using (var outputDuplication = output1.DuplicateOutput(mDevice))
                            {
                                using (var sinkWriter = InitialiseSinkWriter(path, out streamIndex, mTextureDesc.Width, mTextureDesc.Height, frameRate, mDevice, dXGIDeviceManager, preferHardwareAcceleration))
                                {
                                    using (var rgbToNv12 = new RGBToNV12ConverterD3D11(mDevice, mDevice.ImmediateContext, mTextureDesc.Width, mTextureDesc.Height))
                                    {
                                        var newDesc = mTextureDesc;
                                        if (forceNV12)
                                            newDesc.Format = Format.NV12;
                                        newDesc.BindFlags = BindFlags.RenderTarget;
                                        newDesc.Usage = ResourceUsage.Default;
                                        newDesc.CpuAccessFlags = CpuAccessFlags.None;
                                        var textureAllocator = new TextureAllocator(mDevice, newDesc);
                                        {

                                            var timeStart = 0L;
                                            var frameCount = 0;

                                            var totalAFMilliseconds = 0.0;
                                            var lastTimeStamp = 0L;
                                            var frameAquired = false;
                                            for (int i = 0; !stopCapture; i++)
                                            {
                                                var sleepAmount = Math.Max(0, (1000 / frameRate) / 2);
                                                System.Threading.Thread.Sleep(sleepAmount);
                                                frameCount++;
                                                SharpDX.DXGI.Resource desktopResource = null;
                                                var frameInfo_ = new OutputDuplicateFrameInformation();
                                                var sw = new Stopwatch();
                                                sw.Start();

                                                var outputLoopCount = 0;
                                                if (outputDuplication != null)
                                                {//desktop duplication
                                                    int aquireFrameLoopCount = 0;
                                                    while (frameInfo_.LastPresentTime == 0)
                                                    {
                                                        aquireFrameLoopCount++;
                                                        if (aquireFrameLoopCount > 1)
                                                        {
                                                            System.Threading.Thread.Sleep(1);
                                                        }
                                                        if (frameAquired)
                                                            try
                                                            {
                                                                outputDuplication.ReleaseFrame();
                                                                frameAquired = false;
                                                            }
                                                            catch (Exception e)
                                                            {
                                                            }
                                                        try
                                                        {
                                                            outputDuplication.AcquireNextFrame(500, out frameInfo_, out desktopResource);
                                                            frameAquired = true;
                                                        }
                                                        catch (Exception e)
                                                        {
                                                        }
                                                        outputLoopCount++;
                                                    }
                                                    Debug.Assert(frameInfo_.LastPresentTime != 0);
                                                    Debug.Assert(frameInfo_.AccumulatedFrames != 0);
                                                }
                                                sw.Stop();
                                                var frameCaptureTime = frameCount * TimeSpan.FromSeconds(1.0 / frameRate).Ticks;
                                                frameCaptureTime = DateTime.Now.ToFileTimeUtc();
                                                var aqTime = TimeSpan.FromSeconds((double)sw.ElapsedTicks / Stopwatch.Frequency);
                                                totalAFMilliseconds += aqTime.TotalMilliseconds;

                                                if (desktopResource != null)
                                                    using (var desktopTexture = desktopResource?.QueryInterface<Texture2D>())
                                                    {
                                                        Texture2D desktopTextureCopy = null;
                                                        try
                                                        {
                                                            desktopTextureCopy = textureAllocator.AllocateTexture();
                                                        }
                                                        catch (SharpDX.SharpDXException e)
                                                        {
                                                            var result = mDevice.DeviceRemovedReason;
                                                            result.CheckError();
                                                            throw e;
                                                        }
                                                        if (outputDuplication != null)
                                                            rgbToNv12.ConvertRGBToNV12(desktopTexture, desktopTextureCopy);

                                                        if (frameCount == 2)
                                                            timeStart = lastTimeStamp;
                                                        Console.WriteLine("Writing frame {0}", TimeSpan.FromTicks(frameCaptureTime - lastTimeStamp).ToString());

                                                        if (frameCount > 0)
                                                        {
                                                            var frameDuration = TimeSpan.FromSeconds(1.0 / frameRate).Ticks;
                                                            var time = (frameCount - 2) * frameDuration;
                                                            WriteFrame(sinkWriter, streamIndex, lastTimeStamp - timeStart, frameCaptureTime - lastTimeStamp, desktopTextureCopy.Description.Width, desktopTextureCopy.Description.Height, desktopTextureCopy, textureAllocator);

                                                            Console.WriteLine("frame written");
                                                        }
                                                        desktopTextureCopy.Dispose();
                                                    }
                                                lastTimeStamp = frameCaptureTime;
                                                CaptureTime = TimeSpan.FromTicks(lastTimeStamp - timeStart);

                                                if (CaptureFramesChanged != null)
                                                {
                                                    CaptureFramesChanged.Invoke(null, new EventArgs());
                                                }

                                                Console.WriteLine("end of loop");
                                            }
                                            stopCapture = false;
                                            Console.WriteLine("Total AquireFrame time: {0}", totalAFMilliseconds / (300 * (1000 / frameRate)));
                                            Console.WriteLine("Begining finalise");
                                            sinkWriter.Finalize();
                                        }
                                    }
                                }
                            }
                        }
                    } 
                }
            }
        }
    }
    MediaManager.Shutdown();
}

谢谢

1 个答案:

答案 0 :(得分:1)

我修复了它。似乎这是一个多线程问题,并通过添加以下行得以解决:

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

在创建设备之后。