Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native pipewire AO #9587

Merged
merged 1 commit into from
Jan 17, 2022
Merged

Native pipewire AO #9587

merged 1 commit into from
Jan 17, 2022

Conversation

t-8ch
Copy link
Contributor

@t-8ch t-8ch commented Dec 10, 2021

This PR adds a native AO for pipewire and closes #8569.

The original code has been written by @Oschowa which was extended by me. (See the different commits)
@Oschowa did not propose the PR themselves as they were experiencing vsync jitter with this AO.
For other users however this AO works as good as or better than the existing ones, and they have been using it for some time with custom builds.

When running on a native pipewire system this AO saves intermediate steps through libpulse and pipewires pulseaudio server.

  • set_pause
  • uau wonders if there is a problem in accurately reporting the remaining buffered audio.

@etircopyh
Copy link
Contributor

You might also want to make docs changes.

@t-8ch
Copy link
Contributor Author

t-8ch commented Dec 11, 2021

You might also want to make docs changes.

@etircopyh I added a mention to the manpage. It's really tiny, but there is not much to document.

audio/out/ao_pipewire.c Outdated Show resolved Hide resolved
@@ -87,6 +88,9 @@ static const struct ao_driver * const audio_out_drivers[] = {
#endif
#if HAVE_SDL2_AUDIO
&audio_out_sdl,
#endif
#if HAVE_PIPEWIRE
&audio_out_pipewire,
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a nitpick. Why is pipewire placed under wrappers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The goal was to make it explicitly opt-in, to give it some time to stabilize

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I see. Makes sense, especially with this issue not being resolved.

@etircopyh
Copy link
Contributor

etircopyh commented Dec 13, 2021

Trying to figure out why I'm having much lower volume with ao_pipewire. The funny thing it only occurs on my KDE setup, the Sway one doesn't have the problem. And I don't know what's the culprit. I have two separate installations of Arch on my laptop. KDE has the volume issue and Sway doesn't. I even tried to run mpv with --no-config, no luck.

UPD: The cause seems to be channelVolumes. No matter what volume I set in mpv I get "channelVolumes": [ 0.068210, 0.068210 ] from pw-dump.

@t-8ch
Copy link
Contributor Author

t-8ch commented Dec 14, 2021

@etircopyh Please make sure you are modifying ao-volume on both machines (look in input.conf).

@etircopyh
Copy link
Contributor

@t-8ch
Copy link
Contributor Author

t-8ch commented Dec 16, 2021

@etircopyh Please test with the latest commit on this PR.

@etircopyh
Copy link
Contributor

No, it doesn't work with this property and it won't. PW_KEY_NODE_RATE is used to suggest a sample rate.

Here's some changes: ao_pipewire-mpv.patch.txt

@t-8ch
Copy link
Contributor Author

t-8ch commented Dec 17, 2021

@etircopyh Is this patch supposed to fix the issue or just do some cleanups?
If it is supposed to fix it, could you point out the specific part doing that.
If not, could you provide a way to reproduce the issue?

@etircopyh
Copy link
Contributor

@etircopyh Is this patch supposed to fix the issue or just do some cleanups? If it is supposed to fix it, could you point out the specific part doing that. If not, could you provide a way to reproduce the issue?

It does fix the issue and doing a bit of cleanup along the way. It does not interfere with anything. You can reproduce the issue with current state of PR with playing some 44.1 kHz file and running pw-top where you'll see something like this:
image
Device doesn't follow file's sample rate.

With fix you'll have this:
image

And yeah, you need to have 44100 in your allowed-rates in your pipewire.conf.

@etircopyh
Copy link
Contributor

If it is supposed to fix it, could you point out the specific part doing that.

It's the part that sets PW_KEY_NODE_RATE as I said before.

@t-8ch
Copy link
Contributor Author

t-8ch commented Dec 17, 2021

Thanks for the information. I messed up PW_KEY_NODE_RATE vs PW_KEY_AUDIO_RATE...
I'll incorporate your changes.

A few more questions:

  • Why is "PW_KEY_NODE_ALWAYS_PROCESS" needed?
  • Why are some of the properties that are set to ao->client_name set in pw_properties_new() and some with pw_properties_set()?

@etircopyh
Copy link
Contributor

etircopyh commented Dec 17, 2021

Why is "PW_KEY_NODE_ALWAYS_PROCESS" needed?

As far as I understand it's supposed to make life easier for people that have no device node or are playing around with virtual sinks/sources, like connecting them together, etc. Some explanation. It seems like there's more to come on this one.

Why are some of the properties that are set to ao->client_name set in pw_properties_new() and some with pw_properties_set()?

I kind of looked it up but I think it's to make sure these properties don't collide with ones that pipewire sets. Because pw_properties_set() does set a property and overrides it.

@t-8ch
Copy link
Contributor Author

t-8ch commented Dec 17, 2021

Why is "PW_KEY_NODE_ALWAYS_PROCESS" needed?

As far as I understand it's supposed to make life easier for people that have no device node or are playing around with virtual sinks/sources, like connecting them together, etc. Some explanation. It seems like there's more to come on this one.

Good, thanks for the explanation!

Why are some of the properties that are set to ao->client_name set in pw_properties_new() and some with pw_properties_set()?

I kind of looked it up but I think it's to make sure these properties don't collide with ones that pipewire sets. Because pw_properties_set() does set a property and overrides it.

Yes it would replace existing values. But as we are constructing the properties from scratch there should be no conflict.
After the properties have been populated and send to Pipewire there is no difference.

@LaserEyess
Copy link
Contributor

Just wanted to say I've been testing this patch since the latest changes, and while I can't comment on the code, I can say it works as advertised. No audio glitches as far as I can tell. Hopefully someone will have time for a more thorough review soon. Thank you for making this PR.

@etircopyh
Copy link
Contributor

Well, as soon as the issue resolved I hope this PR will make it to upstream, more so as prioritized AO if pipewire is driving audio.

@philipl
Copy link
Member

philipl commented Jan 4, 2022

I've been using this for a couple of weeks now (after experiencing the mentioned issue about detecting if pipewire will be useful or not...) and it's been working as well as the pulse AO for me.

Copy link
Member

@philipl philipl left a comment

Choose a reason for hiding this comment

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

I'm no pipewire expert, but the change looks reasonable from an mpv perspective, and it's worked fine in my local testing.

audio/out/ao_pipewire.c Show resolved Hide resolved
audio/out/ao_pipewire.c Outdated Show resolved Hide resolved
@philipl
Copy link
Member

philipl commented Jan 6, 2022

Can you squash all the changes please. This should only need to be one commit.

@t-8ch t-8ch force-pushed the ao-pipewire-2 branch 2 times, most recently from ae7efa4 to e9a44b1 Compare January 6, 2022 20:54
@t-8ch
Copy link
Contributor Author

t-8ch commented Jan 6, 2022

@philipl Done.
I added some tags to attribute @andreaskem , @Oschowa and @etircopyh for their original development , I hope that is fine.

audio/out/ao_pipewire.c Outdated Show resolved Hide resolved
@philipl
Copy link
Member

philipl commented Jan 11, 2022

Perhaps we are looking at similar symptoms. Maybe the issue here is that the quantum calcuation is off? @t-8ch Can you push another branch with the buffer size still being managed by size rather than time? Maybe I didn't notice the juddering before because that was testing before that change (which I admit I asked you to do :-P)

@t-8ch
Copy link
Contributor Author

t-8ch commented Jan 11, 2022

@etircopyh Did you test the latest state including 158d0f5 for pausing and seeking?
@philipl be678a0 should be the commit with the old buffer parameter, although it should not make much difference. Now we have the same default as pw-cat.

@etircopyh
Copy link
Contributor

@t-8ch yes, I'm currently testing with latest PR changes.

Now we have the same default as pw-cat.

It seems to be kind of "just in case" value. Here's a comment from OpenAL code.

@t-8ch
Copy link
Contributor Author

t-8ch commented Jan 11, 2022

The default is 100ms.
For me it works flawlessly with buffer sizes from 2ms to 2s. Is there anything special about your setup?

@etircopyh
Copy link
Contributor

Is there anything special about your setup?

No, I don't think so.

@philipl
Copy link
Member

philipl commented Jan 11, 2022

@t-8ch If I revert the change to express buffers in miliseconds, the juddering goes away. So it's clearly related to that. Did you set the default to keep the total buffer size the same? Or does it result in a different (larger) buffer. As I noted previously, a small buffer (I tested with 10ms) does not judder, while the default value does, and larger buffers judder more badly. I notice @etircopyh said OpenAL had the smallest default buffer size and the best performance in his tests. Maybe that's related.

So, it's not clear if the problem is with the calculation or that big buffers are bad.

@t-8ch
Copy link
Contributor Author

t-8ch commented Jan 11, 2022

@philipl The previous default was 2048 samples. The current default is 100ms, so probably 4800 samples.
The calculation should be fine, as the quantum is shown correctly in pw-top.
How about reducing the default to 20ms? When going through libpulse the default is 60ms.
(Please note that the OpenAL comment is for their input stream)

@v-fox
Copy link

v-fox commented Jan 11, 2022

The default is 100ms.
For me it works flawlessly with buffer sizes from 2ms to 2s. Is there anything special about your setup?

Isn't it considered that buffer for communicating with PW needs to be as small as possible while internal buffer may be whatever app wants ? Meaning that sane values would be somewhere around 4-24ms. OpenAL defaults to 20ms so it's on a safe side from both missing its time-frame and being too unwieldy.

@philipl
Copy link
Member

philipl commented Jan 11, 2022

Yea, 20ms is smooth for me. I remain confused that @t-8ch can't see anything wrong at higher buffer sizes but if it's more correct to have the small buffer, then let's just make 20ms the default and be happy with that.

@t-8ch
Copy link
Contributor Author

t-8ch commented Jan 11, 2022

@philipl Sounds good, it's commited. I'll rebase before the final merge

@philipl
Copy link
Member

philipl commented Jan 11, 2022

Thanks. Let's give it a few more days to see if we get any more feedback.

@notgood
Copy link

notgood commented Jan 12, 2022

Tested this PR on a few videos while debugging pipewire issue https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/1200

No visible/audible problems so far, at least to my untrained eyes and ears.

@etircopyh
Copy link
Contributor

@haasn
Copy link
Member

haasn commented Jan 13, 2022

Thanks. Let's give it a few more days to see if we get any more feedback.

My issue is still not fixed

Edit: nvm, I see, this is a pipewire issue, not an issue with the AO

@haasn
Copy link
Member

haasn commented Jan 13, 2022

Is there any way you could add --audio-device support to this AO? So you can choose which sink to play on. Right now it always plays on the default sink, but I want to be able to control that.

@philipl
Copy link
Member

philipl commented Jan 17, 2022

@t-8ch Is @haasn's request possible? In the mean time, I think you can squash the current set of changes.

The AO provides a way for mpv to directly submit audio to the PipeWire
audio server.
Doing this directly instead of going through the various compatibility
layers provided by PipeWire has the following advantages:

* It reduces complexity of going through the compatibility layers
* It allows a richer integration between mpv and PipeWire
  (for example for metadata)
* Some users report issues with the compatibility layers that to not
  occur with the native AO

For now the AO is ordered after all the other relevant AOs, so it will
most probably not be picked up by default.
This is for the following reasons:

* Currently it is not possible to detect if the PipeWire daemon that mpv
  connects to is actually driving the system audio.
  (https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/1835)
* It gives the AO time to stabilize before it is used by everyone.

Based-on-patch-by: Oschowa <oschowa@web.de>
Based-on-patch-by: Andreas Kempf <aakempf@gmail.com>
Helped-by: Ivan <etircopyhdot@gmail.com>
@t-8ch
Copy link
Contributor Author

t-8ch commented Jan 17, 2022

@t-8ch Is @haasn's request possible?

Sorry, I missed this comment. Yes that's possible, I have a halfway done branch for it.
Should it be part of this PR? It's getting already a bit long.
This should not be a critical as the output can always be changed afterwards.

In the mean time, I think you can squash the current set of changes.

Done

@philipl philipl merged commit 87aba14 into mpv-player:master Jan 17, 2022
@philipl
Copy link
Member

philipl commented Jan 17, 2022

@t-8ch Thanks so much for working on this and addressing all the feedback! Please open a new PR for the audio-device change.

philipl added a commit that referenced this pull request Jan 17, 2022
Sometimes the most obvious things can be missed.

Reflects authorship described in the original commit.

* #7902
* Oschowa@fddb143
* #9587
@t-8ch t-8ch deleted the ao-pipewire-2 branch January 17, 2022 22:58
@laichiaheng
Copy link

Does it output audio to pipewire by default?

@Dudemanguy
Copy link
Member

It does not. You need to specifiy --ao=pipewire. The reason is because there's currently no way to detect if pipewire is running.

@t-8ch
Copy link
Contributor Author

t-8ch commented Jan 20, 2022

There is now a mostly-functionial PR for the device selection at #9734

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.

PipeWire support
9 participants