obs-studio icon indicating copy to clipboard operation
obs-studio copied to clipboard

libobs: Improve monitoring deduplication

Open pkviet opened this issue 3 weeks ago • 2 comments

Description

The monitoring deduplication was previously checking at each audio tick the whole audio tree for Audio Output Capture (AOC) devices used for monitoring. The profiling did not show any big impact on the audio callback. But due to reports of a significant slowdown for some users we have implemented a new source call triggered whenever:

  • a monitoring device is changed in Settings > Audio,
  • an Audio Output Capture source changes its device or on startup or is removed;

The main call compares the AOC device with the monitoring device. If they are identical, a signal is sent to add in audio thread an audio task updating the AOC duplicating source pointer. This triggers monitoring deduplication if the ptr is not NULL.

The rest of the logic in obs-audio.c is the same as before these tweaks.

Note that we discovered & fixed also a bug with the function retrieving the default monitoring device on macOS. It was picking the default device used for system sounds instead of the default play device.

Motivation and Context

Improve the previous bugfix. Fix also for the default monitoring device on macOS. The bug was introduced in: https://github.com/obsproject/obs-studio/commit/e006d961a479699921323dc1350939221dc0ed58 Just an 8 yrs old bug. Wink to @admshao

How Has This Been Tested?

Tested on windows 11, linux ubuntu 24.04 & macos 26.1. Monitoring deduplication works & the audio callback is faster. Checked by recording a sine wave which is monitored and duplicated on Desktop Audio; the wav file has the expected level after checking it with ffmpeg volumedetect filter.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Tweak (non-breaking change to improve existing functionality)
  • Performance enhancement (non-breaking change which improves efficiency)

Checklist:

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

pkviet avatar Dec 08 '25 04:12 pkviet

Scene collection with 6 audio sources and nothing else showing immediate and growing rendering lag on Windows 11 25H2 running OBS 32.0.3. Just reporting here to say that this build does indeed fix that.

prgmitchell avatar Dec 09 '25 00:12 prgmitchell

Scene collection with 6 audio sources and nothing else showing immediate and growing rendering lag on Windows 11 25H2 running OBS 32.0.3. Just reporting here to say that this build does indeed fix that.

thanks for the feedback. I am elevating the priority of this PR then in the 'PR triage' infos.

pkviet avatar Dec 09 '25 09:12 pkviet

Additional commit message nit: when splitting "paragraphs" in the commit message body, use a blank line between them and after any lists.

done

pkviet avatar Dec 10 '25 20:12 pkviet

I tested the downloaded artifacts. With this change, using the following configuration (Monitor and Output) causes Scene 2 audio to not be included in the output.

 - scene 'Scene':
    - source: 'Audio Output Capture' (wasapi_output_capture)
 - scene 'Scene 2':
    - source: 'Media Source' (ffmpeg_source)
        - monitoring: monitor and output

Both the Audio Output Capture and the monitoring device are set to Default. Note: the output works normally if monitoring is turned off.

This issue does not occur in version 32.0.3.

style1925 avatar Dec 10 '25 21:12 style1925

I tested the downloaded artifacts. With this change, using the following configuration (Monitor and Output) causes Scene 2 audio to not be included in the output.

 - scene 'Scene':
    - source: 'Audio Output Capture' (wasapi_output_capture)
 - scene 'Scene 2':
    - source: 'Media Source' (ffmpeg_source)
        - monitoring: monitor and output

Both the Audio Output Capture and the monitoring device are set to Default. Note: the output works normally if monitoring is turned off.

This issue does not occur in version 32.0.3.

I'll check. Thanks for the feedback.

pkviet avatar Dec 10 '25 21:12 pkviet

I tested the downloaded artifacts. With this change, using the following configuration (Monitor and Output) causes Scene 2 audio to not be included in the output.

 - scene 'Scene':
    - source: 'Audio Output Capture' (wasapi_output_capture)
 - scene 'Scene 2':
    - source: 'Media Source' (ffmpeg_source)
        - monitoring: monitor and output

Both the Audio Output Capture and the monitoring device are set to Default. Note: the output works normally if monitoring is turned off.

This issue does not occur in version 32.0.3.

I confirm the issue which required a very small fix (a bool test against the active state of the AOC). Test again and it should be ok. Thanks for the report. NB: I also removed a lengthy comment from obs-audio.c and changed a check against aoc->user_muted for aoc->muted which works better with the updated code of the PR.

pkviet avatar Dec 11 '25 00:12 pkviet

I confirm the issue which required a very small fix (a bool test against the active state of the AOC). Test again and it should be ok. Thanks for the report. NB: I also removed a lengthy comment from obs-audio.c and changed a check against aoc->user_muted for aoc->muted which works better with the updated code of the PR.

I downloaded the new artifact and tested it again, and the issue is now resolved.
Thank you very much!

style1925 avatar Dec 11 '25 05:12 style1925

Summary of the updates

  • in should_silence_monitored_source, I added a bool ckeck against the duplicating AOC source being active. In 32.0.3, with previous logic, the AOC source was in the audio tree and therefore active so that check was not needed.
  • in the same function, I changed the check for a muted AOC from aoc->user_muted to aoc->muted . Moving the AOC device check to UI thread, and having the deduplication status updated in an audio task at the end of each audio tick, works better in my last tests with muted rather than user_muted.

The tests were done with a sine tone and an AOC source and checked with Reaper and volumedetect filter from FFmpeg.

pkviet avatar Dec 11 '25 20:12 pkviet

@Warchamp7 found a regression when switching from 2 scenes with a different AOC. The AOC registered for deduplication is not updated.

pkviet avatar Dec 11 '25 20:12 pkviet

Update

  • Fixed the regression. Before the current PR, the monitoring AOC was automatically updated at each audio tick and was active. But with the check of AOC against monitor device being made in UI thread, the active state was not checked and signaled. So I added such a signal callback for 'activate' signal to update the audio->monitoring_duplicating_source.
  • Tests done: scene A with AOC 1 (on default device), switched to scene B with AOC 2 (on same device but set explicitly). Deduplication of a sine source is triggered and produces the expected level.

pkviet avatar Dec 11 '25 22:12 pkviet

@style1925 Can you also confirm that your test case still works?

RytoEX avatar Dec 12 '25 20:12 RytoEX

@RytoEX I downloaded the latest artifact and retested it, and the issue I reported does not occur. Thanks for checking.

style1925 avatar Dec 13 '25 01:12 style1925