Skip to content

Linux: virtual audio support via ALSA Loopback card#8171

Open
aramg wants to merge 2 commits intoobsproject:masterfrom
dev47apps:linux-alsa-output
Open

Linux: virtual audio support via ALSA Loopback card#8171
aramg wants to merge 2 commits intoobsproject:masterfrom
dev47apps:linux-alsa-output

Conversation

@aramg
Copy link

@aramg aramg commented Jan 29, 2023

NOTE: This PR includes the changes in #8008, which is expected to get merged first.

Description

Adds virtual audio support via the ALSA loopback module, snd_aloop. As implemented, audio is optional and the virtualcam depends on v4l2loopback still. snd_aloop is not auto-loaded and an audio-only mode is not available.

Motivation and Context

Allows sharing audio output with other programs as a virtual microphone.

How Has This Been Tested?

  1. Load the loopback modules: modprobe v4l2loopback modprobe snd-aloop
  2. Start OBS and click [Start Virtual Cam]. Both audio and video are activated:
info: v4l2-output: Using device 'Dummy video device (0x0000)' at '/dev/video0'
info: ALSA output: hw:Loopback,1,0
info: Virtual camera starting: audio=1 video=1
  1. Add a media source, and observe video output as before (in this case, via '/dev/video0' **).
  2. Record audio: arecord -D hw:Loopback,1,0 --duration=30 -fS16_LE -c2 -r48000 /tmp/test.pcm
  3. Replay audio: aplay -fS16_LE -c2 -r48000 /tmp/test.pcm
  4. This varies by distro, but to load the device into PluseAudio:
    pacmd load-module module-alsa-source source_properties=device.description=OBS device=hw:Loopback,1,0

** ideally the audio and video device names will be added to the OBS status bar, a to-do.

Types of changes

  • New feature (non-breaking change which adds functionality)

Checklist:

  • My code has been run through clang-format.
  • I have read the contributing document.
  • My code is not on the master branch.
  • The code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.

aramg added 2 commits January 18, 2023 00:57
Try to create & use an OBS specific loopback card;
fallback to any available option if that fails.

Fixes obsproject#3929
Moves v4l2-output and adds alsa-output,
for audio support via ALSA Loopback card
@aramg aramg force-pushed the linux-alsa-output branch from 1ad48e5 to 51f7877 Compare January 29, 2023 09:46
@WizardCM WizardCM added Linux Affects Linux New Feature New feature or plugin labels Feb 4, 2023
@david672orford
Copy link

david672orford commented Mar 5, 2024

I have been testing this PR. Here are some initial observations:

  • This PR includes linux-v4l2: Create & use an OBS specific loopback card #8008 which is not really related. Worse, it changes behavior and is broken. When the virtual camera is started, it attempts to create a new V4L2 loopback interface called "OBS Virtual Camera". This produces nuisance pkexec authorization popup for a command which ultimately fails do to the use of a non-existent "add" subcommand.
  • While it makes a kind of sense that since the video output goes to an in-kernel loopback devices, the audio should too, I am not sure this is the best decision. The only monitoring output for Linux I see in the code goes to Pulseaudio. Shouldn't this use Pulseaudio as well?
  • When I play a video file I hear frequent clicking. I do not hear these clicks on the monitor output for the same videos.

@aramg
Copy link
Author

aramg commented Mar 5, 2024

Thanks for the feedback.

I think you are correct, the fact that video goes through a kernel loopback device does not mean audio should, too.
There have been some alternative options posted, which seem easier [1][2] (have not tested myself).

I am open to closing this PR, or refactoring if this approach is chosen after all.

https://obsproject.com/forum/threads/quick-and-easy-virtual-microphone-for-linux.158340/
https://www.reddit.com/r/obs/comments/n1e9j6/comment/gwco2ec/

@david672orford
Copy link

I am really interested in getting the main audio output working. You can get by with the monitor output, but it bypasses the built-in mixer in OBS and there are no fades for scene transitions.

Audio for the virtual camera was requested nearly three years ago:

https://ideas.obsproject.com/posts/1415/obs-virtual-camera-audio

I don't really understand the response from Lain about why a satisfactory implementation is not currently possible. When I asked for details of what needed to be done, I was told that "someone willing, and available, to spend time to do it". I found that completely uninformative.

Your patch is the closest thing we have to an implementation. I wish we could find a way to move it forward. I can program in C, but if nobody is willing to discuss whatever is blocking a properly-working implementation, I'm stuck. Maybe you have some insights.

@david672orford
Copy link

Wanted to follow up on the clicking. After more tests I find that when I read audio directly from the Alsa loopback device there is no clicking. I get the clicking when I read the audio through PipeWire.

@david672orford
Copy link

Just posted #10495 which implements audio output through PulseAudio. Look a Lua script in the PR decription which starts and stops a virtual audio cable when the virtual camera starts and stops.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Linux Affects Linux New Feature New feature or plugin

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants