Commit graph

163 commits

Author SHA1 Message Date
Wim Taymans
bdb7f3adc8 gst: don't stop streaming when paused
We pause when we are unlinked, don't stop the streaming thread because
then we won't be able to resume when we are linked again.

Fixes #3620
2023-11-02 10:06:03 +01:00
Wim Taymans
3eed0fbe9b gst: mark streams async
From the process callback we signal a cond and don't dequeue/queue
a buffer directly.
2023-10-21 09:42:15 +02:00
Robert Mader
004206db37 gst/pipewiresrc: Let GstBaseSrc handle pseudo-live calculations
Let's avoid doing timestamp math as much as possible and let `GstBaseSrc`
do it for us instead.

This bring the source more in line with others in Gstreamer and
may help to avoid bugs and share concepts or code.
2023-04-19 19:22:24 +00:00
Wim Taymans
0962555291 gst: implement renegotiation
If the stream format changes, set the basesrc caps again before pushing
a new buffer.

See #3147
2023-04-14 10:42:01 +02:00
Robert Mader
06bad5523d gstpipewiresrc: Set stream error on caps negotiation failure
In order to make `wait_started()` error out accordingly, which otherwise
blocks `change_state()` until the timeout is over.
2023-02-23 11:58:52 +00:00
Robert Mader
0b69f37a7c gstpipewiresrc: Do not alter meta plane count
Shared memory buffers may contain multiple planes.
Thus we need to keep the meta plane number as derived from the format.

Closes https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/3045
2023-02-21 08:17:26 +00:00
Mike Playle
23b86761a5 Add autoconnect parameter to pipewiresrc element 2023-02-21 08:16:44 +00:00
Barnabás Pőcze
934ab3036e treewide: use SPDX tags to specify copyright information
SPDX tags make the licensing information easy to understand and clear,
and they are machine parseable.

See https://spdx.dev for more information.
2023-02-16 10:54:48 +00:00
Robert Mader
353c1f709d gstpipewiresrc: make stream errors permanent
When pw_stream receives a proxy error it does not set its state to an
error state on its own, commenting:
```
we just emit the state change here to inform the application.
If this is supposed to be a permanent error, the app should
do a pw_stream_set_error()
```

Until now this was not done in the pipewiresrc, resulting in the
negotiation loop to not bail out after an error as a successive
`pw_stream_get_state()` did not return an error state.

Make the error permanent so negotiation errors make us take the
appropriate error paths.
2023-02-15 14:49:42 +01:00
Huang-Huang Bao
4b60569c4a gst: correct buffer & meta offset calculation
The offset in GstVideoMeta point to location of merge-mapped buffer memories (see "gst_buffer_find_memory()") instead of raw memory location for each plane, make adjustment to comply this rule.

Also some cleanups.

Fixes 023577e391
2023-02-06 06:34:14 +00:00
Huang-Huang Bao
023577e391 gst: add buffer video meta support
Fixes video buffer strides handling.
This enables passing of image datas with right line padding.
2023-02-03 07:23:35 +00:00
Georges Basile Stavracas Neto
0fd44bc9d0 pipewiresrc: Make it live by default
In theory, the pipewiresrc element is a live element by default.
However, that is not reflected in code, as the element never sets
that flag unless explicltly requested through stream properties.

Make it live by default, but still respect if consumers of the
element mark it as not live.
2023-01-25 16:13:03 +00:00
Robert Mader
6bf47e0bfa pipewiresrc: Always advertise support for DmaBuf
Quoting https://docs.pipewire.org/page_dma_buf.html section "v4l2":
```
It's the the responsibility of the producer while the add_buffer event
to choose DMA-BUF as the used buffer type even though no modifier is
present, if it can guarantee, that the used buffer is mmapable.
```

As Gstreamer internally can mmap dmabufs just fine, support this case.
This enables dmabuf support with V4L2 and libcamera.

This was also tested against Gnome-Shell, which correctly handles this
case and only chooses DmaBuf if the negotiated format has a modifier,
i.e. if the Gstreamer peer supports the format with
`GST_CAPS_FEATURE_MEMORY_DMABUF`.

Fixes 602aa7d541
2023-01-12 11:27:27 +00:00
Wim Taymans
cab3e3c1ce clarify deprecated use of NODE_TARGET 2023-01-10 17:21:02 +01:00
Wim Taymans
c3032c70b6 keys: move NODE_TARGET to deprecated properties
Add PW_ENABLE_DEPRECATED to some places where we can not yet
avoid removing support.
2023-01-10 17:17:34 +01:00
Robert Mader
602aa7d541 pipewiresrc: Reenable DMABuf support
This was disabled in 15b4c58e as under various circumstances Gstreamer
pipelines would mmap the DMABufs, which can be very slow in various cases.
One typical example of that is screen casting using a dedicated graphics card.

Thus we only want to use DMABufs if the peer element advertises support for
`GST_CAPS_FEATURE_MEMORY_DMABUF`, ensuring no mapping will happen if we
set the format accordingly.

This allows pipewiresrc to be used with DMABufs for fully accelerated
pipelines or in combination with a download element such as `gldownload`[1]
without regressing the commit above. The `gldownload` approach mirrors
what webrtc (Chromium/Firefox) does, but without duplicating the
functionality into pipewiresrc.

While on it, also implement dmabuf negotiation according to
https://docs.pipewire.org/page_dma_buf.html with the modifiers
`DRM_FORMAT_MOD_INVALID` and `DRM_FORMAT_MOD_LINEAR` in order to allow
dmabuf negotiation with more modern clients, including Gnome-Shell.

1: for now use something like `glupload ! glcolorconvert ! gldownload` -
   `gldownload` does not support importing DMABufs yet but `glupload` does.
2023-01-08 09:17:19 +00:00
Robert Mader
2ed7afb76c gst: Implement SPA_META_VideoTransform support
Many Gstreamer elements support transforming buffers via the
`image-orientation` tag. Use it to implement support for the new
VideoTransform meta.

In order for Gstreamer pipelines to enable support for these tags
usually the rotate method has to be set to `auto` or `automatic`,
e.g. `videoflip method=automatic`, `glimagesink rotate-method=automatic`
or `waylandsink rotate-method=auto`.
2022-12-05 18:40:00 +01:00
James Hilliard
1ea1d525c1 gst: copy buffer memory in dequeue_buffer using gst_memory_copy
When always-copy is enabled we should copy buffer memory instead of
sharing buffer memory and using gst_buffer_copy_deep.

This should ensure we recycle the parent buffer as soon as the memory
is copied.

Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
2022-08-20 09:30:44 -06:00
James Hilliard
d61d787afa gst: log warnings if pw_stream_queue_buffer fails
There seem to be some latent issues with buffer recycling, add some
additional logging to simplify tracing buffer recycling issues.
2022-06-24 09:11:42 +00:00
Wim Taymans
5363bdcfcb gst: protect negotiated field with lock
Move the lock around the negotiated field.

See !1287
2022-06-21 13:19:32 +02:00
Wim Taymans
6c310cf5e2 gst: add client-properties
To update the client properties of the connection.

Fixes #1573
2022-06-03 13:00:52 +02:00
James Hilliard
64e8dee3a7 gst: dup buffer file descriptor before allocating
Since gst_fd_allocator_alloc lazy mmap's the buffer to the assigned
file descriptor we can get downstream mmap failures if the pipewire
src(such as the v4l2 spa plugin) closes the file descriptor before
it gets mmap'd. To prevent the closed original file descriptor from
causing a mmap failure dup the file descriptor so that the original
being closed doesn't invalidate the descriptor passed to
gst_fd_allocator_alloc.

Add some more validation to dequeue_buffer as well.

Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
2022-06-02 06:44:10 +00:00
James Hilliard
a1f33a99df gst: dequeue a shared buffer instead of original pool buffer
This seems to prevent the pool buffer from getting corrupted.

Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
2022-06-01 04:09:59 -06:00
James Hilliard
7305d38b85 gstpipewiresrc: don't mix tabs and spaces
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
2022-05-31 02:46:27 -06:00
James Hilliard
7cc509b117 buffers: ensure buffer size does not exceed maxsize
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
2022-05-31 07:25:31 +00:00
Pauli Virtanen
67a27d80c6 gst: use target.object instead of node.target, soft-deprecate ids
Use target-object=<serial/name> instead of path=<id> for specifying
sink/src targets. Deprecate path= argument.

Change device provider to preferably expose serials instead of ids.
2022-03-06 18:34:47 +00:00
Wim Taymans
35cbe4e939 buffers: make alignment optional
Make the alignment parameter optional when negotiating buffers.
Default to a 16 bytes alignment and adjust for the max cpu
alignment.
Remove the useless align buffer parameter in plugins, we always
set it to 16 anyway.
2022-01-03 12:32:26 +01:00
Jonas Ådahl
15b4c58e80 gst: Don't ask for DMA buffers
Gstreamer simply mmap()s the DMA buffers. This can be very very slow on
some hardware, so don't ask for it. For gstreamer to support DMA
buffers, it will need to support a proper way of transferring the
buffers from the GPU to the CPU, e.g. using EGL/OpenGL.
2021-11-11 10:22:37 +00:00
Michael Olbrich
314ff82ff0 pipewiresrc: fix handling outstanding buffers when stopping
The current code has several issues if a pipewiresrc is stopped and deleted
while a buffer is still in use downstream.

 - pw_stream_queue_buffer() is never called for the corresponding
   pw_buffer. As a result, the busy counter is never decremented and
   pw_stream_dequeue_buffer() of the corresponding pipewire output will
   return NULL whenever it encounters this buffer.

 - The pipewiresrc does not own the buffer reference unless the buffer is
   queued in the source, so calling gst_buffer_unref() unconditionally
   causes refcount issues for the buffer.

 - buffer_recycle() can race with on_remove_buffer() and
   gst_pipewire_src_close(). As a result, buffer_recycle() may access pwsrc
   when it was already deleted.

The buffer has its own reference to the pool. So the pool object lock can
be used to ensure that the pwsrc, core and stream remain valid in.
buffer_recycle(). If the 'dispose' function pointer was already cleared,
then on_remove_buffer() has already finished, so abort early.

With the pool lock held, it is save to access the pipewire loop. Now the
loop lock can be used to synchronize with on_remove_buffer(). 'dispose'
must be checked again in case on_remove_buffer() was triggered by something
other than gst_pipewire_src_close().

In on_remove_buffer() unref the buffer if it is queued. Otherwise call
pw_stream_queue_buffer() to ensure that the busy counter is decremented
correctly.
2021-10-14 19:16:56 +00:00
columbarius
2aabc4610b gst: Don't reconnect gstpipewiresrc
Clients connecting via the gstreamer plugin can stall a stream when
reconnecting to it. Disable reconnection until the gstreamer plugin
has improved.
2021-10-09 13:12:06 +00:00
Wim Taymans
803763ed01 gst: add 30 seconds timeout
Add a timeout to all blocking wait operations so that we don't wait
forever but error out after 30 seconds.
2021-09-16 10:05:58 +02:00
Wim Taymans
a2202de679 gst: also stop waiting when we are flushing 2021-09-15 20:57:10 +02:00
Wim Taymans
077808b09a pipewiresrc: also notify waiters when start fails 2021-09-15 20:57:10 +02:00
Philippe Normand
8b5601947c gst: Fix logging in buffer_recycle()
The first argument should be a GObject. GstMiniObjects are not GObject.
2021-07-25 11:17:18 +00:00
Peter Hutterer
da339c286f meson.build: drop HAVE_CONFIG_H
This is an autotools leftover, with meson we're always guaranteed to have
the config.h file.
2021-06-10 09:04:16 +10:00
Wim Taymans
ec331f4e4c gst: enable DMABUF explicitly to avoid mmap issues
PipeWire does not give DMABUF anymore to clients unless they
explicitly ask for it. This results in v4l2 sending an fd and
the mmapoffset + size fields in the buffer.

GStreamer does not handle the mmapoffset in the fd allocator and
thus runs into mmap problems when using the memory.

Enable DMABuf explicitly to work around the problem until
GStreamer is fixed.

Fixes #1115
2021-05-05 11:23:44 +02:00
Gabriel Ebner
ad9d485d1b gst: set node.name and node.description based on client-name property 2020-12-26 10:11:42 +01:00
Wim Taymans
799fe7f90c gst: reset negotiated state before connect
Right before connecting, set ourselves to not negotiated.
First look for errors, then the negotiated state.
Handle NULL caps after negotiation.
2020-08-11 16:10:44 +02:00
Wim Taymans
b9862868c1 gst: convert properties to strings when we can 2020-08-06 15:10:44 +02:00
Wim Taymans
98b7dc7c0c gst: don't do set_caps from the pipewire callback
Don't do any GStreamer data transport from the PipeWire callback
because it might block in preroll and block our communication with
PipeWire. Instead, take the caps and wake up the caller to continue
with negotiation.
2020-08-06 13:03:40 +02:00
Wim Taymans
5f40bc6d4e gst: support VideoCrop meta
Convert the VideoCrop metadata to GStreamer metadata.
2020-07-31 11:44:46 +02:00
Wim Taymans
6dcb19bbdf pipewiresrc: use more accurate timeout 2020-07-14 17:25:13 +02:00
Wim Taymans
36511a5d8e gst: measure keepalive-time in milliseconds
And round up to the next integer for now.
2020-07-14 14:29:45 +02:00
Wim Taymans
0344e09905 gst: add some more debug 2020-07-14 14:15:55 +02:00
Wim Taymans
993aa89030 gst: add keepalive-time property
Add a property to periodically send the last buffer to keep the
stream alive. Useful for sparse streams that need to keep the
encoder busy every once and a while.
2020-07-14 13:54:21 +02:00
Wim Taymans
70652d1a37 gst: share the core between connections
Make all sources in the same process with the same fd share the
connection to the server. This makes it possible to set the same
fd on multiple sources/sinks and have them all use the same
connection, like when capturing multiple monitors from screencast
with the portal.

Fixes #241
2020-07-13 18:38:19 +02:00
Wim Taymans
8e9bbaf3dc pipewiresrc: add option to resend last buffer on EOS
Add an option to resend the last buffer on EOS with an updated
timestamp. This can be used to make sure encoders fill up the
gap between last buffer and EOS, like with sparse streams from
screen capture.
2020-07-13 12:11:34 +02:00
Wim Taymans
1b4b8a9322 gst: check for NULL stream
The stream can already be closed when the buffers are recycled.

Fixes: #243
2020-06-04 20:29:41 +02:00
Wim Taymans
871bb47eb7 gst: handle pause/resume 2020-05-21 12:26:39 +02:00
Wim Taymans
6eba010d38 gst: do a sync before disconnect
This makes sure we first nicely remove the stream from the server
and then close the socket.

If we don't do this, the disconnect might not have flushed out our
disconnect and the server is left with a non-responsive node,
especially if the disconnect on the core was done with a socket from
the portal that is still open.
2020-05-21 12:26:37 +02:00