jez
jez

Reputation: 15359

How can I determine which Windows process is the "audio session" governing the current process's output?

I happen to be using Python bindings here, but I suspect that the problem, and the eventual answer, are not Python-specific.

On Windows 10, using Python bindings to Windows' Core Audio library, specifically via pycaw.AudioUtilities.GetAllSessions(), I can see that there is a session with name 'conhost.exe' and pid 11512, and by elementary guesswork/experimentation I can see that this is the session that (a) governs the audio for my current process and (b) corresponds to the "Console Window Host" slider in the Windows 10 Volume Mixer. This is the one I want to manipulate from my program, and pycaw provides the bindings to do that.

So far so good. My problem is that I don't see any way of working backwards from the current process ID so that, on the next launch, my program can ask "which process corresponds to the session that governs my audio output?" Naively I expected that that session pid might appear in the current process's ancestry, but it does not:

import os, psutil

psutil.Process(os.getpid())
# prints: psutil.Process(pid=3628, name='python.exe', started='14:56:41')

psutil.Process(os.getpid()).parent()
# prints: psutil.Process(pid=16676, name='cmd.exe', started='13:00:57')

psutil.Process(os.getpid()).parent().parent()
# prints: psutil.Process(pid=1356, name='explorer.exe', started='12:21:26')

psutil.Process(os.getpid()).parent().parent().parent()
# prints nothing. It's `None`. We've reached the top.

How should I be querying the pid for whichever audio session governs the current process? (I presume the session won't always be named 'conhost.exe—sometimes I run pythonw.exe to execute the same code without a console.)

Upvotes: 5

Views: 869

Answers (0)

Related Questions