Skip to content

Conversation

@columbarius
Copy link
Contributor

Description

This patch checks the buffer received from PipeWire for flags indicating that the buffer is corrupt.

Motivation and Context

Portals can set a buffer as corrupted via flags. This allows obs to skip those frames and avoid trying to import them.

How Has This Been Tested?

Modified version of xdg-desktop-portal-wlr to alternate between those flags for a given time duration. With this patch your screencast will freeze until a new frame without a flag arrives. Mouse pointer should still be updated when it is not embedded.

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.

@kkartaltepe
Copy link
Collaborator

Is this supposed to solve #5468 ?

@columbarius
Copy link
Contributor Author

Is this supposed to solve #5468 ?

No

@columbarius columbarius force-pushed the pipewire-skip-corrupt branch from b0d9355 to 8d39523 Compare December 4, 2021 17:15
@WizardCM WizardCM added Bug Fix Non-breaking change which fixes an issue Linux Affects Linux labels Dec 4, 2021
@columbarius columbarius force-pushed the pipewire-skip-corrupt branch from 8d39523 to 6b37a2d Compare December 5, 2021 03:54
Copy link
Member

@GeorgesStavracas GeorgesStavracas left a comment

Choose a reason for hiding this comment

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

Commit message typo: metadatemetadata

Could you expand a bit more the context around these CORRUPTED flags? The documentation I found is painfully small, and I don't think I understand when nor how this flag is used. Is it PipeWire itself that sets it? The compositor? Is skipping the frame the best strategy to handle it?

@columbarius
Copy link
Contributor Author

Could you expand a bit more the context around these CORRUPTED flags? The documentation I found is painfully small, and I don't think I understand when nor how this flag is used. Is it PipeWire itself that sets it? The compositor? Is skipping the frame the best strategy to handle it?

This flag is set by the producer, in our case the compositor. Afaik this flag can be used to mark "broken" buffers, without specifying what the issue is. xdpw will use that flag if a buffer copy by the compositor failed. Since we manage the buffer the memory won't be invalid but the content might be corrupt (filled with garbage). The v4l2 plugin [1] uses this flag for the same purpose. Since a buffer with this flag won't contain wanted contend I can't think of a better solution than skipping it. In my testing obs just reused the last imported texture and continued after a non corrupted frame was received.

[1] https://gitlab.freedesktop.org/pipewire/pipewire/-/blob/master/spa/plugins/v4l2/v4l2-utils.c#L1264

@columbarius columbarius force-pushed the pipewire-skip-corrupt branch from 6b37a2d to 9deed95 Compare December 27, 2021 23:52
@columbarius columbarius force-pushed the pipewire-skip-corrupt branch from 9deed95 to b55a47d Compare March 7, 2022 22:38
@columbarius
Copy link
Contributor Author

Updated this to incorporate both meta and junk flags and added a revertible commit for current GNOME behaviour.

The hinted documentation for ignoring the size in case of DMA-BUFs will be written by me probably this week, but got an ack from mesa devs. Will link the PipeWire MR here.

@columbarius
Copy link
Contributor Author

The documentation I found is painfully small

Try to extend that at the same time.

@columbarius columbarius force-pushed the pipewire-skip-corrupt branch from 2967c4f to ef6f804 Compare March 8, 2022 00:55
@columbarius
Copy link
Contributor Author

columbarius commented Mar 9, 2022

PipeWire documentation for ignoring the buffer size: https://gitlab.freedesktop.org/pipewire/pipewire/-/merge_requests/1189

@columbarius columbarius force-pushed the pipewire-skip-corrupt branch 2 times, most recently from ab89cc4 to 088e4e6 Compare March 27, 2022 18:11
@columbarius columbarius force-pushed the pipewire-skip-corrupt branch from 088e4e6 to 9bc61fc Compare April 7, 2022 21:22
@RytoEX RytoEX requested a review from GeorgesStavracas April 14, 2022 21:18
@RytoEX RytoEX requested a review from kkartaltepe May 31, 2022 13:43
@columbarius columbarius changed the title linux-capture: check pipewire buffers for corruption linux-pipewire: check pipewire buffers for corruption Jan 1, 2023
Copy link
Member

@GeorgesStavracas GeorgesStavracas left a comment

Choose a reason for hiding this comment

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

Two comments, but this looks generally good to me.

@GeorgesStavracas
Copy link
Member

I forgot to mention, but could you please rebase against the main branch, and remove the merge commits?

@columbarius columbarius force-pushed the pipewire-skip-corrupt branch from 66bc7d6 to e141d82 Compare January 21, 2023 08:05
@columbarius
Copy link
Contributor Author

I forgot to mention, but could you please rebase against the main branch, and remove the merge commits?

It's gone. Probably didn't changed the strategy when using githubs update feature.

@columbarius columbarius force-pushed the pipewire-skip-corrupt branch from e141d82 to 5758de0 Compare January 21, 2023 08:29
Copy link
Member

@GeorgesStavracas GeorgesStavracas left a comment

Choose a reason for hiding this comment

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

I understand why you split this in 2 separate commits - and it actually helped me review it - but I'd prefer if you could squash the second commit into the first one before merge. Please mention the same GNOME pre 43 behavior in the first commit's message. (The reason I prefer squashing is to not have any point in the commit history where the capture won't work with a subset of consumers.)

@columbarius
Copy link
Contributor Author

I understand why you split this in 2 separate commits - and it actually helped me review it - but I'd prefer if you could squash the second commit into the first one before merge. Please mention the same GNOME pre 43 behavior in the first commit's message. (The reason I prefer squashing is to not have any point in the commit history where the capture won't work with a subset of consumers.)

Squashed the commit and added the note to the first commit message. Additionally I marked the workaround in code with a comment, such that it will be recognized as such and can be easily removed later.

I'll continue to use multiple commits to ease development and review, but I'm totally fine to squash them just before merging.

Copy link
Member

@GeorgesStavracas GeorgesStavracas left a comment

Choose a reason for hiding this comment

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

One last nit: please use linux-pipewire: as the commit prefix; and please start the tile in upper case. In other words, this: linux-pipewire: Reject invalid buffers

PipeWire supports two flags to signal an invalid buffer:
SPA_META_HEADER_FLAG_CORRUPTED signals that the whole buffer is invalid
and should not be used
SPA_CHUNK_FLAG_CORRUPTED signals that one single buffer plane is invalid

Skipping a buffer because of size 0 was moved to only the SHM case.
For DMA-BUFs the size of a single plane is not relevant and should be
ignored [1].

Compatibility note:
GNOME pre 43 sets the chunk size to 0 when a buffer copy failed.
Luckily GNOME doesn't use the META_Header and thus we can detect if we
should use the new or old style of invalid buffer detection.
This workaround should be dropped when reasonable.

[1] https://docs.pipewire.org/page_dma_buf.html
@columbarius columbarius force-pushed the pipewire-skip-corrupt branch from 711da88 to 7e529e2 Compare January 26, 2023 22:38
@columbarius
Copy link
Contributor Author

One last nit: please use linux-pipewire: as the commit prefix; and please start the tile in upper case. In other words, this: linux-pipewire: Reject invalid buffers

Done ... linux-capture... what a nod from the past ^^`

@GeorgesStavracas
Copy link
Member

GeorgesStavracas commented Jan 26, 2023

Done ... linux-capture... what a nod from the past ^^`

Heh the good, old, simpler times 🙂

@GeorgesStavracas GeorgesStavracas merged commit e0a4d86 into obsproject:master Jan 27, 2023
@RytoEX RytoEX changed the title linux-pipewire: check pipewire buffers for corruption linux-pipewire: Reject invalid buffers Jan 27, 2023
@jp9000
Copy link
Member

jp9000 commented Jan 27, 2023

This change spams my log with corruption messages even though it seems to be capturing fine. Fedora 37

@columbarius
Copy link
Contributor Author

@jp9000 is it [pipewire] buffer is corrupt or [pipewire] buffer contains corrupted data ?
The second one should also appear when the buffer only contains an cursor update without new screen data. Maybe we should change the loglevel or message.

@columbarius
Copy link
Contributor Author

@GeorgesStavracas thanks for merging!

@jp9000
Copy link
Member

jp9000 commented Jan 27, 2023

@columbarius It's [pipewire] buffer contains corrupted data, the one on line 533. I just noticed it happens if I have OBS on a monitor different from the one being captured. If OBS is capturing the same monitor it's fine, but when I move it onto my other monitor, it starts spamming that in the logs in spite of seemingly capturing fine.

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

Labels

Bug Fix Non-breaking change which fixes an issue Linux Affects Linux

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants