Commit graph

170 commits

Author SHA1 Message Date
François Laignel d4061774a4 ts/Task: return a future for state transitions
State transitions request functions hid the synchronization
details to the caller:

- If the transition was requested from a Context, a subtask was
  added or the transition ack was not awaited if the new transition
  was requested from a running transition or an iteration function.
- If the transition was not requested from a Context, current
  thread was blocked until the ack was received.

This strategy facilitated code in elements, but it suffered from
the following shortcomings:

- The `prepare` transition request didn't comply with the above
  strategy and would always return an `Async` form. This was
  designed to accomodate the `Prepare` function for elements
  such as `ts-tcpclientsrc` which takes times due to the
  TCP socket connection delays. The idea was that the actual
  transition result would be available after calling `start`.
  This was a disadvantage for elements which would prefer to
  error immediately in the event of a preparation failure.
- Hidding the transition request synchronization to the caller
  meant that they had no options but relying on the internal
  mechanism. E.g.: it was not possible to `start` from another
  async runtime without blocking. Also it was not possible
  to request a transition and choose not to await for the
  ack.

This commit introduces a more flexible API for state
transitions requests:

- The transition request function now return a `TransitionStatus`,
  which is a Future.
- When an error occurs immediately (e.g. the transition
  request is not autorized due to current state of the Task),
  the `TransitionStatus` is resolved immediately and can be
  `check`ed for errors. This is useful for functions such as
  `pepare` in the case of `ts-tcpclientsrc` (see above).
  This is also useful for `pause`, because in current design,
  the transition is always async. Note however, that `pause` is
  forseen to adhere to the same behaviour as the other transition
  requests in the near future [1].
- If the caller chooses to await for the ack and they don't know
  if they are running on a ts Context (e.g. in `Pad{Src,Sink}`
  handlers), they can call `await_maybe_on_context`. This is mostly
  the same behaviour as the one that used to be performed internaly.
- If the caller knows for sure they can't possibly block an async
  executor, they can call `block_on` which is more explicite, but
  will nonetheless make sure no ts Context is being blocked. This
  last check was introduced as it was considered low overhead
  while it should ease preventing missues in cases where the above
  functions should be used.

[1]: https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/merge_requests/793#note_1464400
2022-08-09 19:48:06 +02:00
François Laignel 625fce3934 ts/Task: spawn StateMachine on ts Context
Task state machines used to execute in an executor from the Futures
crate. State transitions actions and iteration functions were then
spawned on the target threadshare Context.

This commit directly spawns the task state machine on the threadshare
Context. This simplifies code a bit and paves the way for the changes
described in [1].

Also introduces struct `StateMachineHandle`, which gather together
fields to communicate and synchronize with the StateMachine. Renamed
`StateMachine::run` as `spawn` and return `StateMachineHandle`.

[1]: https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/merge_requests/793#note_1464400
2022-08-09 19:48:06 +02:00
François Laignel 0858dfedb4 ts-udpsrc: align default port with C counterpart
... and also with the default settings for ts-udpsink.
2022-08-09 17:42:53 +02:00
François Laignel 28a62e622e ts/scheduler: rename awake / wake_up as unpark 2022-08-09 13:17:21 +00:00
François Laignel 833331ab66 ts/Task: wake up after the triggering event is pushed
The scheduler is awaken when aborting a task loop, but not after
a triggering event is pushed. This can cause throttling to induce
long state transitions for pipelines with many streams.

Observed for Unprepare with:

GST_DEBUG=ts-benchmark:4 ../../target/debug/examples/benchmark 2000 ts-udpsrc 2 20 5000
2022-08-09 13:17:21 +00:00
François Laignel 374671cb6f ts/udpsink: fix default clients not leading to socket configuration
During MR !793, the socket configuration mechanism was changed to
use commands passed to the Task via a channel. This worked properly
for user changes via settings and signals, however the default
clients setting was not used.

A simple solution could have been to send a command at initialization
to add the default clients, but it was considered a better solution
to just wait for the Task preparation to configure the sockets based
on the value of settings.clients at that time, thus avoiding
unnecessary successive removals and additions of clients which could
have happened before preparation.

Of course, users can still add or remove clients as before, before
and after Task preparation.

See also https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/merge_requests/793
2022-08-09 12:43:36 +00:00
Bilal Elmoussaoui 52973d975e Update per glib::SignalBuilder changes 2022-07-21 20:03:13 +02:00
François Laignel d6a9106ffa ts/tcpclientsrc: reduce sync primitives in async hot path 2022-07-09 17:03:21 +00:00
François Laignel 7e826385c7 ts: Queue & Proxy: minor cleanups 2022-07-09 17:03:21 +00:00
François Laignel 5720faa808 ts/appsrc: reduce sync primitives in async hot path 2022-07-09 17:03:21 +00:00
François Laignel a1b89c1fb9 ts/udpsrc: reduce sync primitives in async hot path
- Moved UdpSrcPadHandlerState and related funtions to UdpSrcTask.
- Moved Socket preparation in UdpSrcTask. No longer need for
  Context::enter.
2022-07-09 17:03:21 +00:00
François Laignel 885d3de7bb ts/udpsink: reduce sync primitives in async hot path
The way the runtime::Task is implemented, UdpSinkTask is available
as a mutable ref in all the TaskImpl functions, which offers the
opportunity to avoid using Mutexes.

Main higlights:

- Removed the back and forth calls between UdpSinkPadHandler
  and UdpSinkTask.
- Udp sockets are now part of UdpSinkTask, which is also in
  charge of preparing them instead of leaving this to UdpSink.
  This removed the need for Context::enter since
  TaskImpl::prepare already operates under the target Context.
- In order for the clients list to be visible from the UdpSink,
  this list was maintained by UdpSinkPadHandler which was also
  in charge of (un)configuring the Udp sockets. The sockets are
  now part of UdpSinkTask, which is also in charge of the
  (un)configuration. Add/remove/replace requests are passed as
  commands to the UdpSinkTask via a channel.
- The clients list visible to the UdpSink is now part of the
  Settings (it is also a read/write property). Since the actual
  socket (un)configuration is asynchronously handled by the Task,
  the clients list is updated by the add/remove/replace signals
  and set_property("clients", ..). Should a problem occur during
  the async (un)configuration, and only in this case, the
  UdpSinkTask would update the clients lists in Settings
  accordingly so that it stays consistent with the internal state.
- The function clear_clients was renamed as replace_with_clients.
- clients is now based on a BTreeSet instead of a Vec. All the
  managing functions perform some sort of lookup prior to updating
  the collection. It also ease implementation.
- Removed the UdpSinkPadHandler RwLock. Using flume channels, we
  are able to clone the Receiver so it can be stored in UdpSink
  and reused when preparing the UdpSinkTask.
2022-07-09 17:03:21 +00:00
Sebastian Dröge 51c7d0652e Fix/silence a couple new clippy warnings 2022-06-30 16:07:32 +03:00
François Laignel a45f944edd ts/async_wrapper: remove fd from reactor before dropping its handle
The I/O handle was dropped prior to removing it from the reactor,
which caused `Poller::delete` to fail due to an invalid file
descriptor. This used to happen silently unless the same fd was
added again, e.g. by changing states in the pipeline as follow:

    Null -> Playing -> Null -> Playing.

In which case `Poller::add` failed due to an already existing file.

This commit makes sure the fd is removed from the reactor prior to
dropping the handle. In order to achieve this, a new task is spawned
on the `Context` on which the I/O was originally registered, allowing
it to access the proper `Reactor`. The I/O can then safely be dropped.

Because the I/O handle is moved to the spawned future, this solution
requires adding the `Send + 'static` bounds to the I/O handle used
within the `Async` wrapper. This appears not too restrictive for
existing implementations though. Other attempts were considered,
but they would cause deadlocks.

This new approach also solves a potential race condition where a
fd could be re-registered in a `Reactor` before it was removed.
2022-06-30 11:13:39 +00:00
Sebastian Dröge cb84206457 Fix a couple of new 1.62 clippy warnings 2022-06-28 14:52:20 +03:00
Mathieu Duponchelle 943a138d49 ts-jitterbuffer: set jbuf delay when instantiating it
The internal (C) jitterbuffer needs to know about the configured
latency when calculating a PTS, as it otherwise may consider that
the packet is too late, trigger a resync and cause the element to
discard the packet altogether.

I could not identify when this was broken, but the net effect was
that in the current state, ts-jitterbuffer was discarding up to
half of all the incoming packets.
2022-05-11 06:29:22 +00:00
Sebastian Dröge 2f16b5dd3e threadshare: Use into_glib_ptr() instead of into_ptr() 2022-05-08 13:31:10 +03:00
Tim-Philipp Müller 90c203857a threadshare: fix build on Windows 2022-04-27 00:13:46 +01:00
Sebastian Dröge 5af52f94a8 threadshare: Remove glib::SendUnique usage
It's being removed from the GLib bindings because it does not add much
value.
2022-04-09 08:41:58 +00:00
François Laignel 59ca466081 ts: log max throttling when creating Context 2022-03-28 08:47:32 +00:00
François Laignel 1ef9ae6398 ts/jitterbuffer: don't wake up immediately...
... when next wakeup delay is shorter than the max throttling duration.

See https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/issues/169
2022-03-28 08:47:32 +00:00
François Laignel 8eb8ea0e7d ts/rt/Task: use light weight executor blocking on ack or join handle
Previous version used the Context::block_on_or_add_sub_task which
spawns a full-fledged executor with timer and io Reactor for no
reason when we just need to wait for a Receiver or JoinHandle.
2022-03-28 08:47:32 +00:00
François Laignel c1615d01e6 ts/rt/Task: awake the iteration loop when it needs to be aborted
When the iteration loop is throttling, the call to `abort` on the
`loop_abort_handle` returns immediately, but the actual `Future`
for the iteration loop is aborted only when the scheduler throttling
completes. State transitions which requires the loop to be aborted &
which are serialized at the pipeline level can incur long delays.

This commit makes sure the Task Context's scheduler is awaken as soon
as the task loop is aborted.
2022-03-28 08:47:32 +00:00
François Laignel 97985d6442 ts/examples: add rtp mode with jitter-buffer & trace stop duration 2022-03-28 08:47:32 +00:00
Sebastian Dröge b38f6cc731 Remove now unnecessary Send+Sync impls for element/etc subclasses
This is now automatically implemented.
2022-02-28 18:56:58 +02:00
François Laignel 72d9d3dc58 generic/threadshare: fix for nightly build 2022-02-22 00:18:28 +01:00
François Laignel 422ea740ca Update to gst::_log_macro_
See the details:
https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/980
2022-02-21 20:50:01 +01:00
Sebastian Dröge 65fcd55160 Update for event/message/query view API changes 2022-01-19 15:07:45 +02:00
Sebastian Dröge b2d0172422 Replace Foo::from_instance(foo) with foo.imp() 2022-01-17 19:36:41 +02:00
Sebastian Dröge 51f8e963d6 Add SPDX-License-Identifier to all file headers 2022-01-15 21:18:47 +02:00
Sebastian Dröge ab14c50d1c Ignore clippy::non_send_fields_in_send_ty lint
It's useless in its current shape and wrongly triggering on all types.

See https://github.com/rust-lang/rust-clippy/issues/8045
2022-01-14 12:09:57 +02:00
François Laignel 64dd588734 ts/runtime/scheduler: check must_awake before sleeping 2022-01-05 19:00:00 +01:00
François Laignel 1573522520 ts/runtime: rewrite runnable loop
Previous version relied on a plain loop / match / break because
I experimented different strategies. The while variant is better
for the final solution.
2022-01-05 18:59:43 +01:00
François Laignel 5e4fc8b138 ts/executor: relax the static bound on enter
The function `enter` is executed in a blocking way from the caller's
point of view. This means that we can guaranty that the provided
function and its output will outlive the underlying Scheduler Task
execution. This requires an unsafe call to
`async_task::spawn_unchecked`. See:

https://docs.rs/async-task/latest/async_task/fn.spawn_unchecked.html
2021-12-25 11:25:56 +00:00
François Laignel 6163589ac7 ts/executor: replace tokio with smol-like implementation
The threadshare executor was based on a modified version of tokio
which implemented the throttling strategy in the BasicScheduler.
Upstream tokio codebase has significantly diverged from what it
was when the throttling strategy was implemented making it hard
to follow. This means that we can hardly get updates from the
upstream project and when we cherry pick fixes, we can't reflect
the state of the project on our fork's version. As a consequence,
tools such as cargo-deny can't check for RUSTSEC fixes in our fork.

The smol ecosystem makes it quite easy to implement and maintain
a custom async executor. This MR imports the smol parts that
need modifications to comply with the threadshare model and implements
a throttling executor in place of the tokio fork.

Networking tokio specific types are replaced with Async wrappers
in the spirit of [smol-rs/async-io]. Note however that the Async
wrappers needed modifications in order to use the per thread
Reactor model. This means that higher level upstream networking
crates such as [async-net] can not be used with our Async
implementation.

Based on the example benchmark with ts-udpsrc, performances seem on par
with what we achieved using the tokio fork.

Fixes https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/issues/118

Related to https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/merge_requests/604
2021-12-25 11:25:56 +00:00
François Laignel db9c38aa93 ts/runtime: shuffle some structs to dedicated modules 2021-12-25 11:25:56 +00:00
François Laignel c5ef83d5b0 ts/runtime: use a directory for executor module
This will ease the introduction of other concepts which are required
for our own executor implementation.
2021-12-25 11:25:56 +00:00
François Laignel cd0773662f ts: migrate most tests so that they don't use tokio 2021-12-25 11:25:56 +00:00
Sebastian Dröge 13923051a0 Fix compilation after gst::Pad::sticky_events_foreach API changes 2021-12-01 15:33:45 +02:00
Sebastian Dröge c46901d150 Fix or silence various new 1.57 clippy warnings 2021-11-30 16:31:50 +02:00
Sebastian Dröge c68f6b2631 Update for GLib signal emit_by_name() API changes 2021-11-21 18:15:04 +02:00
Sebastian Dröge 55aad51141 Update for glib constructor renames
See https://github.com/gtk-rs/gtk-rs-core/pull/384
2021-11-20 14:31:06 +02:00
Bilal Elmoussaoui 82be7b3ac5 adapt to ObjectExt improvements 2021-11-08 14:43:53 +02:00
Sebastian Dröge d9bda62a47 Update for GLib/GStreamer API changes
And clean up a lot of related property/caps/structure code.
2021-11-06 09:34:10 +02:00
Sebastian Dröge 0a7d1639e7 Update to Rust edition 2021 and minimum supported Rust version to 1.56 2021-10-31 17:40:05 +02:00
Sebastian Dröge b9541b2ca4 Update for GstObjectImpl API change 2021-10-23 12:31:33 +03:00
François Laignel 27b9f0d868 Improve usability thanks to opt-ops
The crate option-operations simplifies usage when dealing with
`Option`s, which is often the case with `ClockTime`.
2021-10-18 15:09:47 +02:00
François Laignel ed90b338f8 ts: runtime: add delay_for_at_least
The time driver for the threadshare runtime assigns the timer
entries to the nearest throttling time frame so that the timer
fires as close as possible to the expected instant. This means
that the timer might fire before or after the expected instant
(at most `wait / 2` away).

In some cases, we don't want the timer to fire early. The new
function `delay_for_at_least` ensures that the timer is assigned
to the time frame after the expected instant.
2021-09-30 09:00:05 +02:00
Sebastian Dröge 052365ba1a Fix various needless-borrow clippy warnings and others 2021-07-30 13:53:35 +03:00
François Laignel 5439f14e57 fix clippy warnings 2021-06-05 10:36:22 +02:00
François Laignel 8f81cb8812 generic: migrate to new ClockTime design 2021-06-05 10:36:21 +02:00
François Laignel 8dfc872544 use gst::glib where applicable 2021-06-03 20:53:16 +02:00
Bilal Elmoussaoui 78d9fb521d rename ToGlib into IntoGlib 2021-04-27 20:45:47 +02:00
Sebastian Dröge 15cf738616 Update for Value trait refactoring 2021-04-25 15:48:55 +03:00
François Laignel 95cdd43f4f manual fixes remove get prefix round 2 2021-04-20 18:19:58 +02:00
François Laignel 67c5871957 fix-getters-calls 0.3.0 pass 2021-04-20 18:19:58 +02:00
François Laignel 27bc5c89ca fix-getters-def 0.3.0 pass 2021-04-20 18:19:58 +02:00
François Laignel c81213b83c clippy pass 2021-04-13 17:24:20 +02:00
François Laignel 7d17f88941 post fix-getters manual updates 2021-04-13 17:24:20 +02:00
François Laignel 06accc8d98 fix-getters-{def,calls} pass 2021-04-12 15:57:19 +02:00
Sebastian Dröge b919d226b1 threadshare: Update to socket2 0.4 2021-03-21 12:57:10 +02:00
Sebastian Dröge 2cada57efc Update for the subclassing glib/gstreamer bindings API changes 2021-03-09 17:07:13 +02:00
Sebastian Dröge 39a75632c8 threadshare: Update for glib subclass API cleanups 2021-03-08 12:50:03 +02:00
Sebastian Dröge dc0c5f7611 Update for new #[glib::object_subclass] attribute macro 2021-03-07 18:27:00 +02:00
Sebastian Dröge 5dd0a23986 threadshare: Update for glib::Type API changes 2021-02-25 13:12:12 +02:00
Sebastian Dröge 0616c18703 Update for changed GLib signals API 2021-02-22 17:20:40 +02:00
Sebastian Dröge cbda137fbf Fix various warnings from clippy 1.50 2021-02-09 18:57:34 +02:00
Sebastian Dröge b649e9b076 Use gst::PARAM_FLAG_MUTABLE_PLAYING and others consistently everywhere
Fixes https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/issues/139
2021-01-31 15:43:00 +02:00
Sebastian Dröge d4ce1a33f2 Update for glib/gstreamer bindings API changes 2021-01-25 14:43:05 +02:00
Sebastian Dröge 7e1181ab84 threadshare: Return a proper error type instead of () 2020-12-29 17:29:27 +02:00
Sebastian Dröge 3d617371af Update for macro renames 2020-12-20 20:43:45 +02:00
Sebastian Dröge ea6c05e16c Update everything for glib macro renamings 2020-12-18 00:44:49 +02:00
Mathieu Duponchelle 09561686b8 jitterbuffer: mark from_glib fn as unsafe
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/merge_requests/446>
2020-12-08 20:03:20 +01:00
Abdul Rehman 87d7b44dd2 ts-udpsink: Post Message on SinkMessage event 2020-11-24 20:01:50 +05:00
Sebastian Dröge 1c9c22df0c generic: Update to 2018 edition 2020-11-23 10:28:33 +02:00
Sebastian Dröge d56ae71e0e Update for ObjectImpl::get_property() being infallible now 2020-11-19 18:25:53 +02:00
Sebastian Dröge df6a229f58 Fix or silence various clippy warnings 2020-11-19 15:31:50 +00:00
Abdul Rehman 7e64652fa7 threadshare/udpsrc: Fix retrieve-sender-address default value 2020-11-17 09:18:26 +00:00
Sebastian Dröge af0337c26c generic: Update for subclassing API changes 2020-11-15 18:25:42 +02:00
Abdul Rehman 178c7155a3 threadshare/udpsrc: SocketAddr to InetSocketAddress direct conversion 2020-11-10 22:31:41 +05:00
Sebastian Dröge 9c48de75d8 Update for glib_wrapper! API changes 2020-11-07 13:14:00 +02:00
Wonchul Lee 469ab53377 threadshare/udpsrc: Fix to update settings
It fixes to update settings along with prepared socket and port number
2020-11-06 16:42:38 +09:00
François Laignel 4eacce80a5 threadshare: runtime: simplify a function thanks to clippy 2020-10-20 23:45:01 +02:00
François Laignel 7c3e69bb4a Fix ClockTime comparisons not being Ord and use saturating_sub
See:
https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/607
2020-10-20 23:45:01 +02:00
Seungha Yang 5cba2b002b threadshare/socket: Fix build error on Windows
error[E0412]: cannot find type `Error` in this scope
   --> generic\threadshare\src\socket.rs:237:56
    |
237 |     pub fn set_tos(&self, qos_dscp: i32) -> Result<(), Error> {
    |                                                        ^^^^^ not found in this scope
2020-10-01 01:40:08 +09:00
Sebastian Dröge eaaf4bcb21 threadshare/jitterbuffer: Remove unused imports
Not required anymore after a fix in glib.
2020-09-01 09:54:41 +03:00
Sebastian Dröge b8f9e0efc8 threadshare/udpsink: Don't hold settings lock while getting clients property
Otherwise we can deadlock between this and render().
2020-08-11 12:23:16 +03:00
Sebastian Dröge e1784ea01b threadshare/udpsink: Remove host/port properties
In combination with the clients property and the add/remove signals this
behaves very inconsistent as there are multiple ways to do the same.
2020-08-11 12:04:36 +03:00
Sebastian Dröge 89346fa945 threadshare/udpsink: Don't hold settings lock while calling into the sink pad handler to clear the clients
Otherwise we can deadlock because of a lock order issue:
 - render() is called with the sink pad handler lock and takes the
   settings lock
 - clearing clients takes the sink pad handler lock
2020-08-10 09:39:07 +03:00
Sebastian Dröge a022bbe260 Fix some new clippy warnings 2020-07-28 18:52:11 +03:00
Sebastian Dröge 0eb777cf5a Update for removal of ObjectImpl::get_type_data() 2020-07-26 18:46:32 +03:00
Sebastian Dröge a28455f0ce Update for Element::post_message() signature change 2020-06-30 21:28:02 +00:00
François Laignel e40267e95d event,message,query: update instantiation
See https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/532
2020-06-25 11:26:32 +02:00
Sebastian Dröge 9bb3e75fb9 Update to use the new pad builders for safely setting pad functions
Only two uses of unsafely setting the pad functions is left:
- fallbacksrc for overriding the chain function of the proxy pad of a
  ghost pad
- threadshare for overriding the pad functions after creationg, which
  probably needs some fixing at some point
2020-06-22 11:28:19 +03:00
Sebastian Dröge 60321edb8c Update for new_with_XXX/new_from_XXX function renaming 2020-06-16 11:56:48 +03:00
Guillaume Desmottes e85799b9d6 use new constructor names 2020-06-11 13:07:01 +02:00
Philippe Normand 737bd459e4 jitterbuffer: Remove un-needed mut qualifiers 2020-06-06 10:26:06 +01:00
Mathieu Duponchelle 3d26d2f27b sync elements: implement provide_clock
Since those are using the clock for sync, they need to also
provide a clock for good measure. The reason is that even if
downstream elements provide a clock, we don't want to have
that clock selected because it might not be running yet.
2020-06-02 19:31:58 +02:00
François Laignel dfaf59a59b threadshare: simplify Pad{Src,Sink} implementations
Pad{Src,Sink}[Ref] delegate some functions to their respective
Pad{Src,Sink}Inner. Since they act as smart pointers, we can
safely implement the Deref trait to simplify the implementations.
2020-05-30 08:30:27 +00:00
Sebastian Dröge 08da51744b threadshare: Update from the deprecated net2 to the socket2 crate 2020-05-29 13:07:14 +03:00