Using DirectX from subprocess executed by windows service
Asked Answered
W

2

6

I need to execute ffmpeg process from windows service and capture it's standard output. It works fine until I use hardware acceleration. Because accessing DirectX from windows service is restricted, the subprocess also fails to access it.

When I'm executing the same code from console application, everything works OK, but the same code executed from windows service fails to use hardware acceleration.

        string ffmpegArgs = /*-hwaccel dxva2 */"-threads 0 -probesize 100512 -i c:/Temp/test.mp4 -vf yadif -vcodec libx264 -preset ultrafast -tune zerolatency -profile baseline -x264-params keyint=20:min-keyint=20:scenecut=-1 -acodec aac -b:a 48k -flags +cgop -f mp4 -movflags empty_moov+default_base_moof+frag_keyframe c:/temp/output.avi";

        var psi = new ProcessStartInfo
        {
            FileName = "c:/Temp/ffmpeg4/ffmpeg.exe",
            Arguments = ffmpegArgs,
            WorkingDirectory = "c:/Temp/ffmpeg4",
            CreateNoWindow = false,
            WindowStyle = ProcessWindowStyle.Hidden,
            RedirectStandardInput = false,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false
        };

        var processVideo = new Process { StartInfo = psi }.Start();

I need somehow break the inherited restrictions to be able to execute ffmpeg with hardware acceleration (access DirectX API). Any suggestions?

Weeden answered 23/5, 2019 at 5:28 Comment(5)
Your code works for me executing from a service with local system account. I test on Windows 10 1903. What's your Windows version?Unanswerable
Can you try with -hwaccel d3d11va to see if ffmpeg could initialize DX 11 device?Urethra
@RitaHan-MSFT Have you uncommented the '-hwaccel dxva2' part? I'm running Win10 Pro 17763Weeden
@Urethra Thanks a lot, your solution works for me!Weeden
@IgorGorelik No, I have not uncommented the '-hwaccel dxva2' part. I use exact code in your question.Unanswerable
U
3

You can instruct FFMPEG process to use DirectX 11 Hardware Video acceleration. In this case, FFMPEG will need to create a DirectX 11 device, by calling D3D11CreateDevice function, which doesn't need any window handles to be supplied.

In order to make FFMPEG utlize DirectX 11 hardware video acceleration, you need to specify the following hwaccel parameter instead:

-hwaccel d3d11va
Urethra answered 26/5, 2019 at 9:57 Comment(0)
B
1

If you right click the service in the Services Dialog -> Properties -> Log on, you should see something like this:

Service Properties example

Check the box "Allow Service to interact with desktop" this should enable you to access these "restricted" APIs

Bissextile answered 23/5, 2019 at 6:1 Comment(1)
Unfortunately, it does not help. Still the same error: [AVHWDeviceContext @ 00000217ef3bfb40] Failed to create Direct3D deviceWeeden

© 2022 - 2024 — McMap. All rights reserved.