Skip to content

Add pause callbacks for coreaudio - #8147

Merged
PatTheMav merged 2 commits into
obsproject:masterfrom
usakhelo:fix_#7552
Jul 6, 2023
Merged

PatTheMav merged 2 commits into
obsproject:masterfrom
usakhelo:fix_#7552

Conversation

@usakhelo

@usakhelo usakhelo commented Jan 25, 2023 •

Copy link
Copy Markdown

Description

When playing VLC source on MacOS audio starts lagging after pause, stop or timeline seeking actions.
This is caused by VLC delaying the call to buffer_audio() callback. After each pause or stop the delay keeps adding up resulting in audio lag.
This change is adding an additional callback for "media_pause" and "media_stop" events to clean the monitor->new_data buffer and prevent audio data accumulation caused by VLC callback delay.

Motivation and Context

Fixes issue #7552

How Has This Been Tested?

Tested on MacBook Air M1 with different files in multiple sources including VLC

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

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.

@RytoEX

RytoEX commented Jan 26, 2023

Copy link
Copy Markdown
Member

Please use one of our template options under "Types of changes" so that we can more easily categorize this PR.

@usakhelo

Copy link
Copy Markdown
Author

Please use one of our template options under "Types of changes" so that we can more easily categorize this PR.

Done.

@WizardCM WizardCM added kind/bug Categorizes issue or PR as related to a bug. kind/enhancement Enhancements are not bugs or new features but can improve usability or performance. labels Jan 28, 2023
@RytoEX
RytoEX requested a review from PatTheMav February 15, 2023 18:53
@PatTheMav

Copy link
Copy Markdown
Member

On the whole this seems to work as advertised - I guess the issue this fixes is the VLC source not being able to feed new data fast enough before the buffer_audio callback empties it again and all buffers are "unused" and it pauses the audio queue again.

@usakhelo

Copy link
Copy Markdown
Author

If I remember correctly buffer_audio is not called for a few seconds when VLC source starts playback. When it's called for the first time circle buffer already contains few extra seconds of audio data. On pause or seek VLC delays calling buffer_audio for another few seconds. buffer_audio keeps calling fill_buffer until circle buffer is empty. But because it contains extra audio data pause or seek starts to lag. My fix just empties circle buffer on every pause or seek.

@PatTheMav

Copy link
Copy Markdown
Member

IIRC buffer_audio is never called by any source - it's a callback method registered with CoreAudio by the macOS monitoring implementation. It will be called by CoreAudio independently of anything else in OBS and will pause its queue as soon as there are 3 empty buffers in the monitoring data struct.

The buffers are filled via on_audio_playback (which is called via vlcs_audio_play) and what happens is that CoreAudio empties the 3 buffers quicker than on_audio_playback can fill them, resulting in a flip-flop between pausing and starting the monitoring queue until a race condition ensures that there are less than 3 empty buffers at any time.

@usakhelo

Copy link
Copy Markdown
Author

I can be wrong but I think VLC has some issues opening files on macOS M1. It shows errors in the log. That's why I thought it can be that VLC somehow affects CoreAudio and delays buffer_audio calls.
image

BTW I added the mutex lock/unlock. But I didn't rebase the branch. Please let me know if it's needed.

@RytoEX RytoEX left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A question about naming.

@PatTheMav PatTheMav left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@PatTheMav

Copy link
Copy Markdown
Member

Final thing before we can merge this:

  • Please combine all commits adding the callbacks for libobs into a single commit
  • Leave a second commit that implements the functionality to the coreaudio monitoring

@usakhelo

usakhelo commented Mar 2, 2023

Copy link
Copy Markdown
Author

Combined commits. Hope commit naming is correct.

@RytoEX RytoEX left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final nit: the first commit message subject is too long.

Please reword the commit message so that the commit message subject is no longer than 72 characters. Ideally, the commit message subject text after "libobs: " would be 50 characters or less. For additional detail, you can add a commit message body.

@usakhelo

usakhelo commented Apr 5, 2023

Copy link
Copy Markdown
Author

Reworded the first commit.

@usakhelo

usakhelo commented May 7, 2023

Copy link
Copy Markdown
Author

Just a friendly reminder that this PR is ready for merge.

@RytoEX

RytoEX commented May 7, 2023

Copy link
Copy Markdown
Member

Just a friendly reminder that this PR is ready for merge.

We're aware of this PR, thank you. We'll merge it when we have time to circle back around to it and re-review it and find that it's good to merge.

@PatTheMav

Copy link
Copy Markdown
Member

Please rename the first commit - it should say something like "libobs: Add pause callback support for sources" because the current title doesn't seem to make sense given the changes.

The second commit should probably be called something like "libobs: Add use of pause callbacks to CoreAudio monitoring".

@usakhelo

usakhelo commented Jul 4, 2023

Copy link
Copy Markdown
Author

Renamed commits, rebased the branch.

@usakhelo
usakhelo requested a review from RytoEX July 4, 2023 19:37
@PatTheMav
PatTheMav merged commit e4a3f80 into obsproject:master Jul 6, 2023
@RytoEX RytoEX added this to the OBS Studio (Next Release) milestone Jul 6, 2023
@usakhelo
usakhelo deleted the fix_#7552 branch July 7, 2023 18:54
@derrod derrod mentioned this pull request Jul 23, 2023
@derrod

derrod commented Jun 19, 2024

Copy link
Copy Markdown
Member

This was merged and is part of release versions, what you are experiencing is probably unrelated.

@derrod

derrod commented Jun 19, 2024

Copy link
Copy Markdown
Member

#3410 isn't closed.

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

Labels

kind/bug Categorizes issue or PR as related to a bug. kind/enhancement Enhancements are not bugs or new features but can improve usability or performance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants