Skip to content

pulse: Lazy prototype loopback implementation#691

Closed
nikp123 wants to merge 1 commit intomozilla:masterfrom
nikp123:master
Closed

pulse: Lazy prototype loopback implementation#691
nikp123 wants to merge 1 commit intomozilla:masterfrom
nikp123:master

Conversation

@nikp123
Copy link
Copy Markdown

@nikp123 nikp123 commented Mar 5, 2022

DO NOT MERGE

Context

I've just recently found about this project and I really like the idea of it and thought I'd use it as my main audio library if it manages to do everything I'd want it to be able to do. Unfortunately, loopback mode seems to be WASAPI only despite it being technically possible to pull off (without even user intervention) on both PulseAudio and PipeWire, however these modes are not commonly documented so you may take them with a grain of salt.

What this pull request is?

This is a really hacky implementation of said loopback mode on PulseAudio (ie. monitor mode). Consider it a RFC before I either polish it or you merge it. It is menat to demonstrate possible functionality before anything else.

How does the magic work? ie. how the code works

Basically it's a bit hacky, but the trick is to create a pulseaudio mainloop (ie create a sink) so that we are able to fetch the default sink's name and turn into into a monitor source by appending .monitor to it's name. Any ideas on simplyfing this code would be appreciated.

And of course your opinions/thoughts on this. I, for one, would like to see this functionality enabled in the library (if possible). Thanks.

@nikp123 nikp123 marked this pull request as draft March 5, 2022 21:20
@padenot
Copy link
Copy Markdown
Collaborator

padenot commented Mar 8, 2022

Yes, this would work well, although it's possible to reuse lots of code we have. You can for example enumerate all devices using either the internal or external API, and pick a monitor device as your input device. The "loopback" feature is WASAPI-only historically because it's somewhat possible to achieve this externally (monitor device are real devices, unlike the WASAPI case where you pass an output device as input device with the flag), but I reckon an alignment would be nice. It's not possible to generally implement this on macOS (outside of Blackhole/Dante Virtual Soundcard/etc.).

The current and up to date PulseAudio backend for cubeb is here: https://github.com/mozilla/cubeb-pulse-rs (it has more than a few bug fixes) and is written in Rust. It requires a rust toolchain for compilation, but it's really straightforward to set it up.

@nikp123
Copy link
Copy Markdown
Author

nikp123 commented Mar 8, 2022

@padenot i'll try to get the rust implementation going if that's what's currently used, but yeah i share the same feelings about the current code, this is just hacked together to show how it could be done.

EDIT: on a sidenote, despite it "loopback" being a "windows-only" thing I feel like exposing it to a more general interface is mutually beneficial because the end-user (aka application developer) doesnt need to know all these finicky background details to make it work (if you understand where im going with this)

@padenot
Copy link
Copy Markdown
Collaborator

padenot commented Mar 8, 2022

Dropping cubeb-pulse-rs into cubeb/src/ and configuring the cmake project with BUILD_RUST_LIBS is all you need to do.

nikp123 added a commit to nikp123/cubeb-pulse-rs that referenced this pull request Nov 11, 2022
While not an officially advertized feature in the PulseAudio API,
all PulseAudio "sink"-s have a "monitor mode" where in any of the
audio output devices there's a "loopback" input device which
replicates all audio for said output. This allows us to effectively
achieve recording any audio output as input within cubeb.

This commit is a continuation of said Draft shown in mozilla/cubeb#691
but this time written in Rust. (yes it took me half a year but we're
here)

As I'm quite inexperienced when it comes to Rust (*especially so when it's
demanding code like FFI interfaces*) comments on the code or any
improvements that may be done are greatly appreciated.

Thanks.
nikp123 added a commit to nikp123/cubeb-pulse-rs that referenced this pull request Nov 16, 2022
While not an officially advertized feature in the PulseAudio API,
all PulseAudio "sink"-s have a "monitor mode" where in any of the
audio output devices there's a "loopback" input device which
replicates all audio for said output. This allows us to effectively
achieve recording any audio output as input within cubeb.

This commit is a continuation of said Draft shown in mozilla/cubeb#691
but this time written in Rust. (yes it took me half a year but we're
here)

As I'm quite inexperienced when it comes to Rust (*especially so when it's
demanding code like FFI interfaces*) comments on the code or any
improvements that may be done are greatly appreciated.

Thanks.
@nikp123
Copy link
Copy Markdown
Author

nikp123 commented Feb 14, 2023

Closing this as the priority is on the rust one anyway.

@nikp123 nikp123 closed this Feb 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants