Commit graph

257 commits

Author SHA1 Message Date
Nicolas Dufresne ca61a76987 v4l2: Fix threading issues in orphaning mechanism
The pool orphaning function was colling internal _stop() virtual function
implementation. This is not thread safe, as a private lock inside the buffer
pool is supposed to be held. Fix this by keeping delayed _stop() and orphaning
the GstV4L2Allocator instead (REQBUFS(0)).

Then, protect the orphaned boolean with the object lock for the case a buffer
is being released after we have orphaned the buffer. That would otherwise
cause a QBUF to happen while the queue is no longer owned by the buffer pool.
This boolean is otherwise used and set from the streaming lock, or after
threads have been stopped (final cleanup).

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-good/-/merge_requests/648>
2020-06-28 15:12:07 +00:00
Nicolas Dufresne 71204f2c3b v4l2bufferpoool: Fix requeueue after seek when importing
When the buffer pool is importing buffer, it will requeue num_allocated on
streamon. As this value was not set for DMABuf import and USERPTR, no buffer
was queued  back.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-good/-/merge_requests/651>
2020-06-28 13:40:30 +00:00
Nicolas Dufresne 1beb497aa7 Revert "v4l2bufferpool: request the maximum number of buffers for USERPTR"
This reverts commit 6bf9f4bd77.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-good/-/merge_requests/651>
2020-06-28 13:40:30 +00:00
Nicolas Dufresne d4ac154824 Revert "v4l2bufferpool: request the maximum number of buffers for DMABUF"
This reverts commit 94e323c10f.

Fixes #754

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-good/-/merge_requests/651>
2020-06-28 13:40:30 +00:00
Nicolas Dufresne 83f60aea54 v4l2bufferpool: Only resurrect the right amount of buffers
On streamon, we need to resurrect (queue back) some buffers, as during
flushign seek we'd endup with an empty queued. We initially started with
resurrecting as many as we could without blocking, but that miss-behaved with
dynamic CREATE_BUFS, causing the pool to grow dramatically. This was limited
by the number of allocated buffers, but this still tried to resurrect too many
buffers for the first run, as activating the pool will queued buffers.

In this patch, we calculte the missing detal in the queue and only try and
resurrect that amount of buffers.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-good/-/merge_requests/651>
2020-06-28 13:40:30 +00:00
Nicolas Dufresne b8f1f2195e v4l2bufferpool: Avoid set_flushing warning
The gst_buffer_pool_set_flushing() warns when that function is called
on an inactive pool. Avoid the warning by checking the state, this is
similar to what we do in gst_v4l2_object_unlock().

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-good/-/merge_requests/649>
2020-06-26 19:24:21 +00:00
Nicolas Dufresne 6bf9f4bd77 v4l2bufferpool: request the maximum number of buffers for USERPTR
This is to match what we now do for DMABuf importation.
2020-04-08 16:37:30 +00:00
Michael Olbrich 94e323c10f v4l2bufferpool: request the maximum number of buffers for DMABUF
There are often only two buffers queued in the kernel so no new buffers are
requested.

With every qbuf, the kernel receives a new DMABUF for the specified index.
This most likely differs from the last DMABUF and the old cached entry is
released. This results in a lot of map/unmap overhead if the kernel driver
needs a mapping for the buffer.

With a larger queue, it's quite likely, that both old and new DMABUFs are
also mapped for another index. So the map/unmap is skipped, because the
mapping is reference counted.

The corresponding allocated buffers don't contain any actual memory, so
allocating them is quite cheep. So the log message is updated to clarify
this.
2020-04-08 16:37:30 +00:00
Guillaume Desmottes d1501f0952 v4l2: add alternate interlace mode
When using this mode each frame is split in two fields, each one being
transferred using its own buffer.
This is implemented with the V4L2_FIELD_ALTERNATE field format in v4l2.

This mode is enabled using a caps filter such as
"v4l2src ! video/x-raw\(format:Interlaced\)"

Here are the main changes related to this feature:

- use the INTERLACED caps feature with this mode.

- in this mode both fields of a given frame have the same sequence/offset
so adjust the algorithm checking for lost field/frame accordingly.

- double pool's min number of buffers as each frame requires 2 buffers.

Fix #504
Co-authored-by: Zeeshan Ali <zeenix@collabora.co.uk>
2020-03-25 12:42:12 +01:00
Guillaume Desmottes 8a69e26f16 v4l2: pass v4l2object to GST_V4L2_MIN_BUFFERS()
Will be used to double the number of buffers in alternate interlace
mode.
2020-03-25 12:14:41 +01:00
Seungha Yang b0f4831481 v4l2bufferpool: Use unique name for v4l2bufferpool object
Assign unique sequence number to an object name for better debugging
2020-03-23 17:48:19 +00:00
Nicolas Dufresne 3bd544418c v4l2bufferpool: Queue number of allocated buffers to capture
Before we do streamon, we queue all capture buffers by calling
resurrect. When the driver supports CREATE_BUFS, this would lead
to buffers being allocated till the maximum of 32 is reached.

Instead, we now save the number of allocated buffers and queue this
amount.
2019-11-20 11:03:19 -05:00
Aaron Boxer 46989dca96 documentation: fix a number of typos 2019-10-05 22:38:11 +00:00
Damian Hobson-Garcia 50dfbf1c0d v4l2bufferpool: Free orphaned allocator resources when buffers are released
Allocator resources cannot be freed when a buffer pool is orphaned
while its buffers are in use. They should, however, be freed once those
buffers are no longer needed. This patch disposes of any buffers
belonging to an orphaned pool as they are released, and makes sure
that the allocator is cleaned up when the last buffer is returned.
2019-06-03 19:09:24 +00:00
Damian Hobson-Garcia 1b9a0f1c2d v4l2bufferpool: return TRUE when buffer pool orphaning succeeds
When trying to orphan a buffer pool, successfully return and unref
the pool when the pool is either successfully stopped or orphaned.
Indicate failure and leave the pool untouched otherwise.
2019-06-03 19:09:24 +00:00
Philipp Zabel dfc20013cc v4l2bufferpool: support orphaning
Now that the v4l2allocator allows orphaning the V4L2 buffer queue, add
support for orphaning in the v4l2bufferpool. gst_v4l2_buffer_pool_orphan
can be used as a replacement for gst_v4l2_buffer_pool_stop, without
having to wait for buffers to be returned to the pool.
2019-04-05 16:01:55 +00:00
Wonchul Lee 560a6155bd v4l2bufferpool: fix typo resurect to resurrect
https://bugzilla.gnome.org/show_bug.cgi?id=797273
2018-10-24 12:03:13 +01:00
Nicolas Dufresne 480a7bcab8 v4l2bufferpool: Validate stride/offset when importing
This will prevent situation where buffer size allow importing but rendering
goes wrong due to a miss-match in expected stride and offset.

https://bugzilla.gnome.org/show_bug.cgi?id=583890
2018-08-27 13:41:30 -04:00
Nicolas Dufresne 73555b5440 v4l2bufferpool: Fix typo in error message
https://bugzilla.gnome.org/show_bug.cgi?id=583890
2018-08-27 13:41:30 -04:00
Nicolas Dufresne 77c052f0e8 v4l2bufferpool: Only queue buffer if preparation worked
The preparation code imports the buffer, doing bunch of
validation. Only queue the buffer in the driver if the
importation worked. This way we don't rely on the driver
to validate.

https://bugzilla.gnome.org/show_bug.cgi?id=583890
2018-08-27 13:41:30 -04:00
Nicolas Dufresne 90a70e08b7 v4l2bufferpool: Activate the other pool first
This change has no effect. We will need to acquire a buffer from the
pool later in order to validate / adapt with the video alignment for
the downstream buffers.

https://bugzilla.gnome.org/show_bug.cgi?id=583890
2018-08-27 13:41:30 -04:00
Nicolas Dufresne 9e79821b1d v4l2bufferpool: Validate that capture buffers were queued
When the pool is started, we allocate and release buffer, expecting
the pool release-buffer handler to queue them. Though, as we rely
on release function, there is no direct way to detect that this
process didn't work.

To check this, validate that the number of queued buffer is the same
as the number of allocated buffers. This allow returning an error
when buffer importation was refused by the driver.

https://bugzilla.gnome.org/show_bug.cgi?id=583890
2018-07-13 23:13:44 -04:00
Nicolas Dufresne 3b364cea94 v4l2bufferpool: Only return eos for M2M devices
This will avoid sending EOS on v4l2src when a driver sends an empty
buffers. This case would be a bug in the driver, but yet the camera
should keep running.

This also removes the check for corrupted buffers, as this check is
already done later.

https://bugzilla.gnome.org/show_bug.cgi?id=794842
2018-07-13 22:58:57 -04:00
Nicolas Dufresne 38b68cbbd7 v4l2bufferpool: Remove duplicate check
We were calling gst_v4l2_is_buffer_valid() before and inside
gst_v4l2_buffer_pool_qbuf() as we needed to access the group. The second
check failed since the writability of the buffer get inherited from the
GstMemory, which lead to pipeline failure. As we cannot avoid the extra
ref, it would be racy otherwise, just pass the group to _dbuf() so it
does not have to call gst_v4l2_is_buffer_valid() again.

https://bugzilla.gnome.org/show_bug.cgi?id=796692
2018-07-12 15:17:44 -04:00
Nicolas Dufresne 90eade28f2 v4l2bufferpool: Drop truncated frames
Drop truncated frames regardless if they have the ERROR flag or not.
Truncated frame causes video frame map failure in many elements
including cluttersink, glupload etc.
2018-06-07 15:26:32 -04:00
Nicolas Dufresne e943a11bab v4l2bufferpool: Try return input buffer soon
In this patch we use a non-blocking poll in order to return all input
buffers (buffers from v4l2-output queue). This prevent holding too long
on upstreaming buffer in importing.

https://bugzilla.gnome.org/show_bug.cgi?id=794904
2018-06-07 15:26:32 -04:00
Nicolas Dufresne 57d7ae8496 v4l2allocator: Add support for data_offset
In MPLANE mode, the driver may set data_offset, which represent some
padding at the start of the buffer used internally. This portion of the
data need to be skipped, though it is included in bytesused.

This patch removes frame size sanity check as the method used will no
longer work. This check was simply there to help detect broken kernel
drivers. It would be re-implement by estimating the plane size, which is
not totally trivial and may be too much work for a simple debug check.

https://bugzilla.gnome.org/show_bug.cgi?id=733501
2017-11-03 15:47:11 -04:00
Nicolas Dufresne 1f902e2f6e v4l2bufferpool: Don't stop streaming when pool is flushing
The purpose of being able to flush the buffer pool is only to
unlock any blocked operation. Doing streamoff/streamon had the
side effect of turning off and on the camera. As we do a flush_start
/ flush_stop sequence when shutting down, that would cause a really
quick sequence of streamoff/streamon/streamoff/close which was
causing some cameras to stop working.

https://bugzilla.gnome.org/show_bug.cgi?id=783945
2017-09-19 17:33:35 -04:00
Cyril Lashkevich f2372b46bc v4l2bufferpool: Don't mark jpeg frames as deltas
JPEG formats are encoded, but they never have keyframe flag. But in
fact they are keyframes

https://bugzilla.gnome.org/show_bug.cgi?id=785990
2017-08-08 11:09:52 -04:00
Nicolas Dufresne d8b18f6b8f v4l2bufferpool: Don't drop buffer ref on qbuf
This function no longer take ownership of the buffer.

CID 1414800
2017-08-02 17:18:43 -04:00
Nicolas Dufresne ba8aa440bb v4l2bufferpool: Copy flags and timestamp when importing
Whenever we import from downstream pool (userptr or dmabuf-import), we
should copy over the flags and timestamp, otherwise downstream will not
get proper synchronization or will not be able to notice frames that has
corruption in it.

https://bugzilla.gnome.org/show_bug.cgi?id=785680
2017-08-01 09:48:46 -04:00
Nicolas Dufresne 5a6fe4ed29 v4l2: Fix compilation without libv4l2 2017-07-24 16:45:40 -04:00
Nicolas Dufresne 2ee90e2b39 v4l2: Keep ref to element in allocator/pool
Removes the FIXME/Question in the buffer pool and add a ref to the
element in the GstAllocator too. This ref is strictly required to keep
the GstV4l2Object structure around.
2017-07-24 16:27:36 -04:00
Nicolas Dufresne 31d8a1d929 v4l2: Add run-time environment to enable libv4l2
The library has started preventing a lot of interesting use cases,
like CREATE_BUFS, DMABuf, usage of TRY_FMT. As the libv4l2 is totally
inactive and not maintained, we decided to disable it. As a convenience
we added a run-time environment that let you enable it for testing.

  GST_V4L2_USE_LIBV4L2=1

This of course only works if you have enabled libv4l2 at build time.
2017-07-24 14:19:02 -04:00
Nicolas Dufresne bec03858fa v4l2: Merge v4l2_calls.h into gstv4l2object.h
First step of a larger cleanup, all function from v4l2_calls are in fact
methods on GstV4l2Object. This split makes the code really confusing.
This also remove no longer unused macros.
2017-07-17 20:48:28 -04:00
Matt Fischer 52a5658030 v4l2: Block recursive calls to resurect_buffer
When resurrecting a buffer, the subsequent free call can result
in the group-released handler being called again, which causes
a recursive loop.  This patch blocks the signal handler during
the time that it executes, ensuring that the loop will not occur.

https://bugzilla.gnome.org/show_bug.cgi?id=759292
2017-06-29 15:54:42 -04:00
Ayaka 17e52f6ac2 v4l2pool: Fix wrong error message
https://bugzilla.gnome.org/show_bug.cgi?id=728438
2017-05-24 14:00:22 -04:00
Sebastian Dröge 2c6016667b gst: Clear floating flag in constructor of all GstObject subclasses that are not owned by any parent
https://bugzilla.gnome.org/show_bug.cgi?id=743062
2017-05-17 10:41:01 +03:00
Todor Tomov 081bae2849 v4l2object: Copy timestamp when importing buffers
This is needed for V4L2_OUTPUT interface, and is harmless of
V4L2_CAPTURE interfaces. This will fix timestamp in cases like:

  v4l2src io-mode=dmabuf ! v4l2videoNenc output-io-mode=dmabuf-import !  ...

Same apply for userptr.

https://bugzilla.gnome.org/show_bug.cgi?id=781119
2017-04-10 12:49:39 -04:00
Edgard Lima 8635258046 Update Edgard Lima's email
https://bugzilla.gnome.org/show_bug.cgi?id=779230
2017-02-27 00:34:19 +00:00
Jean-Christophe Trotin 1c2e2d83ce v4l2bufferpool: remove duplicated line of code
https://bugzilla.gnome.org/show_bug.cgi?id=777330
2017-01-16 10:19:34 -05:00
Tim-Philipp Müller d7b2820b73 Fix indentation 2017-01-09 19:05:10 +00:00
Philipp Zabel 86f243bb12 gstv4l2bufferpool: lock flush_stop against regular qbuf
These can be called from different threads and both manipulate the
pool->buffers array. Lock them properly and let flush_stop move the
array contents into a temporary array on the stack to avoid having
to call release_buffer under the object lock.

https://bugzilla.gnome.org/show_bug.cgi?id=775015
2016-11-24 11:47:07 -05:00
Philipp Zabel 6b5807654b gstv4l2bufferpool: remove critical error message when process is called on an inactive pool
If the pool is inactive, it is guaranteed to also be flushing, so the
following check will return GST_FLOW_FLUSHING anyway.
This can happen if a v4l2src is blocking on DQBUF in create and is sent
an EOS event on another thread. In that case the pool is set to
flushing/inactive without locking, the v4l2src is unblocked, and may
call pool_process with a valid buffer on the already inactive pool.

https://bugzilla.gnome.org/show_bug.cgi?id=775014
2016-11-24 11:47:07 -05:00
Reynaldo H. Verdejo Pinochet 4b16b16aaf v4l2: use opened device caps instead of physical device ones
The same physical device can export multiple devices. In
this case, the capabilities field now contains a union of
all caps available from all exported V4L2 devices alongside
a V4L2_CAP_DEVICE_CAPS flag that should be used to decide
what capabilities to consider. In our case, we need the
ones from the exported device we are using.

https://bugzilla.gnome.org/show_bug.cgi?id=768195
2016-07-07 17:46:06 -04:00
Josep Torra b7866315a6 v4l2src: check for valid size on raw video buffers
Discard buffers that doesn't contain enough data when dealing
with raw video inputs.

https://bugzilla.gnome.org/show_bug.cgi?id=767086
2016-06-01 17:35:12 +02:00
Tim-Philipp Müller 3333683f64 v4l2: don't use undeclared core debug category symbols 2016-02-20 11:51:56 +00:00
Wim Taymans b208ae72ee v4l2bufferpool: don't block when resurecting a buffer
When we are resurecting a buffer, don't block. instead let us copy a
buffer.
2015-12-01 18:22:11 +01:00
Hyunjun Ko afcd462918 v4l2bufferpool: set GST_BUFFER_COPY_FLAGS to copy flags also
https://bugzilla.gnome.org/show_bug.cgi?id=752618
2015-07-23 10:19:46 -04:00
Song Bing c5950cd04a v4l2bufferpool: Set timestamp when queue buffer.
Should set timestamp when queue buffer.

https://bugzilla.gnome.org/show_bug.cgi?id=752618
2015-07-20 16:49:41 -04:00