首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >桌面复制API中AcquireNextFrame()和ReleaseFrame()的死锁

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

Stack Overflow用户
提问于 2018-12-11 21:44:43
回答 1查看 846关注 0票数 2

我试图编写一个将Windows桌面记录到视频文件的应用程序。我使用桌面复制API和。更具体地说,我使用的是来自媒体基金会的SinkWriter类。我正在使用桌面复制API中的Texture2D对象,并将它们传递给SinkWriter,这就形成了一个硬件加速的管道。我还将C#与SharpDX结合使用。

整个项目大部分运作得很好。它完美地运行在我的Surface上,捕捉相当一致的fps视频,没有问题。

问题出在其他硬件上。具体来说,我已经测试过这个,我已经测试了一个AMD Ryzen与一个离散的Nvidia GPU。这也很管用..。除了记录桌面后通常在8到20秒之间,程序将被锁定在outputDuplication.ReleaseFrame()outputDuplication.AcquireNextFrame()上。它没有崩溃,只是锁了起来,所以我不知道怎么弄清楚到底发生了什么。我确实知道的是,它与将桌面纹理传递给sinkWriter.WriteSample()函数有关。如果我删除了这一行,并保持了所有其他的不变,如继续像往常一样创建MediaBuffersMediaSamples,将它们与我的桌面纹理关联起来,但是不要将示例传递给WriteSample方法,那么应用程序就可以愉快地“记录”,看起来似乎是无限期的。

我检查了我能想到的每一个潜在的错误。我确保所有的对象都在应该的时候被处理掉。没有我能看到的内存泄漏。代码在SetAllocator()上使用MediaSample方法,因此我传递给SinkWriter的纹理被回收。据我所知,这一切都是正确的。

我怀疑这里有些基本的东西我不明白。我觉得在正确管理SharpDX背后的非托管资源方面犯了一些错误,但我真的不知道该从哪里开始查看。在这一点上,任何指示都会受到极大的赞赏。

这是主要的捕获代码。这不是一切,但大部分都是。我怀疑问题就在这里的某个地方。

代码语言:javascript
运行
复制
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();
}

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-19 21:40:41

我修好了。似乎这是一些多线程问题,并通过添加以下行进行了修复:

代码语言:javascript
运行
复制
using(var multiThread = mDevice.QueryInterface<Multithread>())
{
    multiThread.SetMultithreadProtected(true);
}

在创建设备之后。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53732774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档