mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-11-02 09:01:50 -05:00
node: improve async handling
Remove the done and error callbacks. The error callback is in an error message. The done callback is replace with spa_pending. Make enum_params take a callback and data for the results. This allows us to push the results one after another to the app and avoids ownership issues of the passed data. We can then extend this to handle the async case by doing a _wait call with a spa_pending+callback+data that will be called when the _enum_params returns and async result. Add a sync method. All methods can now return SPA_RESULT_IS_ASYNC return values and you can use spa_node_wait() to register a callback when they complete with optional extra parameters. This makes it easier to sync and handle the reply. Make helper methods to simulate the sync enum_params behaviour for sync nodes. Let the transport generate the sequence number for pw_resource_sync() and pw_proxy_sync(). That way we don't need to keep track of numbers ourselves and we can match the reply to the request easily.
This commit is contained in:
parent
b743518f78
commit
7b12212eeb
67 changed files with 1894 additions and 1209 deletions
|
|
@ -32,6 +32,7 @@ extern "C" {
|
|||
struct spa_node;
|
||||
|
||||
#include <spa/utils/defs.h>
|
||||
#include <spa/utils/result.h>
|
||||
#include <spa/utils/type.h>
|
||||
|
||||
#include <spa/support/plugin.h>
|
||||
|
|
@ -97,14 +98,6 @@ struct spa_node_callbacks {
|
|||
#define SPA_VERSION_NODE_CALLBACKS 0
|
||||
uint32_t version; /**< version of this structure */
|
||||
|
||||
/** Emited as a reply to a sync method with \a seq.
|
||||
*
|
||||
* Will be called from the main thread. */
|
||||
int (*done) (void *data, uint32_t seq);
|
||||
|
||||
/** an asynchronous error occured */
|
||||
int (*error) (void *data, int res, const char *message);
|
||||
|
||||
/** Emited when info changes */
|
||||
int (*info) (void *data, const struct spa_node_info *info);
|
||||
|
||||
|
|
@ -156,6 +149,12 @@ struct spa_node_callbacks {
|
|||
#define SPA_NODE_PARAM_FLAG_NEAREST (1 << 2) /* allow set fields to be rounded to the
|
||||
* nearest allowed field value. */
|
||||
|
||||
/** the result of enum_param. */
|
||||
struct spa_result_node_enum_params {
|
||||
uint32_t next; /**< next index of iteration */
|
||||
struct spa_pod *param; /**< the result param */
|
||||
};
|
||||
|
||||
/**
|
||||
* A spa_node is a component that can consume and produce buffers.
|
||||
*/
|
||||
|
|
@ -182,13 +181,46 @@ struct spa_node {
|
|||
/**
|
||||
* Perform a sync operation.
|
||||
*
|
||||
* Calling this method will emit the done event or -EIO when
|
||||
* no callbacks are installed.
|
||||
* This method will complete when all previous methods on the node
|
||||
* are completed.
|
||||
*
|
||||
* Because all methods are serialized in the node, this can be used
|
||||
* to wait for completion of all previous method calls.
|
||||
*
|
||||
* If this function returns an async result, use spa_node_wait() to
|
||||
* install a hook to receive the completion notification.
|
||||
*
|
||||
* \return 0 on success
|
||||
* -EINVAL when node is NULL
|
||||
* an async result
|
||||
*/
|
||||
int (*sync) (struct spa_node *node, uint32_t seq);
|
||||
int (*sync) (struct spa_node *node);
|
||||
|
||||
/**
|
||||
* Wait for an async function to complete and signal the result
|
||||
* callback with result parameters.
|
||||
*
|
||||
* When a function returns an sync result, pass the async result
|
||||
* to this function to create a hook that will be called when
|
||||
* the operation completes.
|
||||
*
|
||||
* The generic completion callback can have an optional result.
|
||||
* Depending on the method, this result will be NULL or point to
|
||||
* a structure with extra return values. See the documentation
|
||||
* of the function to see what result structure will be passed.
|
||||
*
|
||||
* The hook is automatically removed after the hook is called.
|
||||
*
|
||||
* \param node a spa_node
|
||||
* \param res an async return value to wait for
|
||||
* \param pending a spa_pending structure
|
||||
* \param func a result callback
|
||||
* \param data data passed to \a func
|
||||
* \return 0 on success
|
||||
* -EINVAL when node is NULL
|
||||
*/
|
||||
int (*wait) (struct spa_node *node, int res, struct spa_pending *pending,
|
||||
spa_result_func_t func, void *data);
|
||||
|
||||
/**
|
||||
* Enumerate the parameters of a node.
|
||||
|
|
@ -202,23 +234,26 @@ struct spa_node {
|
|||
*
|
||||
* \param node a \ref spa_node
|
||||
* \param id the param id to enumerate
|
||||
* \param index the index of enumeration, pass 0 for the first item and the
|
||||
* index is updated to retrieve the next item.
|
||||
* \param start the index of enumeration, pass 0 for the first item
|
||||
* \param num the number of parameters to enumerate
|
||||
* \param filter and optional filter to use
|
||||
* \param param result param or NULL
|
||||
* \param builder builder for the param object.
|
||||
* \return 1 on success and \a param contains the result
|
||||
* 0 when there are no more parameters to enumerate
|
||||
* \param func the callback with the result. The result will be
|
||||
* of type struct spa_result_node_enum_params. The next field
|
||||
* can be used to continue the enumeration.
|
||||
* \param data first argument to \a func
|
||||
*
|
||||
* \return the return value of \a func or 0 when no more
|
||||
* items can be iterated.
|
||||
* -EINVAL when invalid arguments are given
|
||||
* -ENOENT the parameter \a id is unknown
|
||||
* -ENOTSUP when there are no parameters
|
||||
* implemented on \a node
|
||||
*/
|
||||
int (*enum_params) (struct spa_node *node,
|
||||
uint32_t id, uint32_t *index,
|
||||
uint32_t id, uint32_t start, uint32_t max,
|
||||
const struct spa_pod *filter,
|
||||
struct spa_pod **param,
|
||||
struct spa_pod_builder *builder);
|
||||
spa_result_func_t func, void *data);
|
||||
|
||||
/**
|
||||
* Set the configurable parameter in \a node.
|
||||
*
|
||||
|
|
@ -318,22 +353,25 @@ struct spa_node {
|
|||
* \param direction an spa_direction
|
||||
* \param port_id the port to query
|
||||
* \param id the parameter id to query
|
||||
* \param index an index state variable, 0 to get the first item
|
||||
* \param start the first index to query, 0 to get the first item
|
||||
* \param num the maximum number of params to query
|
||||
* \param filter a parameter filter or NULL for no filter
|
||||
* \param param result parameter
|
||||
* \param builder a builder for the result parameter object
|
||||
* \return 1 on success
|
||||
* \param func the callback with the result. The result will be
|
||||
* of type struct spa_result_node_enum_params. The next field
|
||||
* can be used to continue the enumeration.
|
||||
* \param data first argument to \a func
|
||||
*
|
||||
* \return the return value of \a func or 0 when no more
|
||||
* items can be iterated.
|
||||
* 0 when no more parameters exists
|
||||
* -EINVAL when invalid parameters are given
|
||||
* -ENOENT when \a id is unknown
|
||||
*/
|
||||
int (*port_enum_params) (struct spa_node *node,
|
||||
enum spa_direction direction, uint32_t port_id,
|
||||
uint32_t id, uint32_t *index,
|
||||
uint32_t id, uint32_t start, uint32_t num,
|
||||
const struct spa_pod *filter,
|
||||
struct spa_pod **param,
|
||||
struct spa_pod_builder *builder);
|
||||
|
||||
spa_result_func_t func, void *data);
|
||||
/**
|
||||
* Set a parameter on \a port_id of \a node.
|
||||
*
|
||||
|
|
@ -497,7 +535,8 @@ struct spa_node {
|
|||
};
|
||||
|
||||
#define spa_node_set_callbacks(n,...) (n)->set_callbacks((n),__VA_ARGS__)
|
||||
#define spa_node_sync(n,...) (n)->sync((n),__VA_ARGS__)
|
||||
#define spa_node_sync(n) (n)->sync((n))
|
||||
#define spa_node_wait(n,...) (n)->wait((n),__VA_ARGS__)
|
||||
#define spa_node_enum_params(n,...) (n)->enum_params((n),__VA_ARGS__)
|
||||
#define spa_node_set_param(n,...) (n)->set_param((n),__VA_ARGS__)
|
||||
#define spa_node_set_io(n,...) (n)->set_io((n),__VA_ARGS__)
|
||||
|
|
|
|||
95
spa/include/spa/node/utils.h
Normal file
95
spa/include/spa/node/utils.h
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
/* Simple Plugin API
|
||||
*
|
||||
* Copyright © 2019 Wim Taymans
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef SPA_NODE_UTILS_H
|
||||
#define SPA_NODE_UTILS_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <spa/node/node.h>
|
||||
|
||||
struct spa_result_node_enum_params_data {
|
||||
struct spa_pod_builder *builder;
|
||||
struct spa_result_node_enum_params data;
|
||||
};
|
||||
|
||||
static inline int spa_result_func_node_enum_params(void *data,
|
||||
int seq, int res, const void *result)
|
||||
{
|
||||
struct spa_result_node_enum_params_data *d =
|
||||
(struct spa_result_node_enum_params_data *)data;
|
||||
const struct spa_result_node_enum_params *r =
|
||||
(const struct spa_result_node_enum_params *)result;
|
||||
|
||||
if (res == 1) {
|
||||
uint32_t offset = d->builder->state.offset;
|
||||
spa_pod_builder_raw_padded(d->builder, r->param, SPA_POD_SIZE(r->param));
|
||||
d->data.next = r->next;
|
||||
d->data.param = SPA_MEMBER(d->builder->data, offset, struct spa_pod);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline int spa_node_enum_params_sync(struct spa_node *node,
|
||||
uint32_t id, uint32_t *index,
|
||||
const struct spa_pod *filter,
|
||||
struct spa_pod **param,
|
||||
struct spa_pod_builder *builder)
|
||||
{
|
||||
struct spa_result_node_enum_params_data data = { builder, };
|
||||
int res;
|
||||
|
||||
if ((res = spa_node_enum_params(node, id, *index, 1,
|
||||
filter, spa_result_func_node_enum_params, &data)) != 1)
|
||||
return res;
|
||||
*index = data.data.next;
|
||||
*param = data.data.param;
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline int spa_node_port_enum_params_sync(struct spa_node *node,
|
||||
enum spa_direction direction, uint32_t port_id,
|
||||
uint32_t id, uint32_t *index,
|
||||
const struct spa_pod *filter,
|
||||
struct spa_pod **param,
|
||||
struct spa_pod_builder *builder)
|
||||
{
|
||||
struct spa_result_node_enum_params_data data = { builder, };
|
||||
int res;
|
||||
|
||||
if ((res = spa_node_port_enum_params(node, direction, port_id, id, *index, 1,
|
||||
filter, spa_result_func_node_enum_params, &data)) != 1)
|
||||
return res;
|
||||
*index = data.data.next;
|
||||
*param = data.data.param;
|
||||
return res;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* SPA_NODE_UTILS_H */
|
||||
|
|
@ -629,6 +629,19 @@ static inline int spa_pod_builder_add(struct spa_pod_builder *builder, ...)
|
|||
spa_pod_builder_pop(b, &f); \
|
||||
})
|
||||
|
||||
/** Copy a pod structure */
|
||||
static inline struct spa_pod *
|
||||
spa_pod_copy(const struct spa_pod *pod)
|
||||
{
|
||||
size_t size;
|
||||
struct spa_pod *c;
|
||||
|
||||
size = SPA_POD_SIZE(pod);
|
||||
if ((c = (struct spa_pod *) malloc(size)) == NULL)
|
||||
return NULL;
|
||||
return (struct spa_pod *) memcpy(c, pod, size);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ struct spa_source;
|
|||
|
||||
#include <spa/utils/defs.h>
|
||||
#include <spa/utils/hook.h>
|
||||
#include <spa/utils/result.h>
|
||||
|
||||
enum spa_io {
|
||||
SPA_IO_IN = (1 << 0),
|
||||
|
|
|
|||
|
|
@ -36,18 +36,6 @@ extern "C" {
|
|||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
#define SPA_ASYNC_BIT (1 << 30)
|
||||
#define SPA_ASYNC_MASK (3 << 30)
|
||||
#define SPA_ASYNC_SEQ_MASK (SPA_ASYNC_BIT - 1)
|
||||
|
||||
#define SPA_RESULT_IS_OK(res) ((res) >= 0)
|
||||
#define SPA_RESULT_IS_ERROR(res) ((res) < 0)
|
||||
#define SPA_RESULT_IS_ASYNC(res) (((res) & SPA_ASYNC_MASK) == SPA_ASYNC_BIT)
|
||||
|
||||
#define SPA_RESULT_ASYNC_SEQ(res) ((res) & SPA_ASYNC_SEQ_MASK)
|
||||
#define SPA_RESULT_RETURN_ASYNC(seq) (SPA_ASYNC_BIT | ((seq) & SPA_ASYNC_SEQ_MASK))
|
||||
|
||||
#define SPA_FLAG_MASK(field,mask,flag) (((field) & (mask)) == (flag))
|
||||
#define SPA_FLAG_CHECK(field,flag) SPA_FLAG_MASK(field,flag,flag)
|
||||
#define SPA_FLAG_SET(field,flag) ((field) |= (flag))
|
||||
|
|
|
|||
61
spa/include/spa/utils/result.h
Normal file
61
spa/include/spa/utils/result.h
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
/* Simple Plugin API
|
||||
*
|
||||
* Copyright © 2018 Wim Taymans
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef SPA_UTILS_RESULT_H
|
||||
#define SPA_UTILS_RESULT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <spa/utils/defs.h>
|
||||
#include <spa/utils/list.h>
|
||||
|
||||
#define SPA_ASYNC_BIT (1 << 30)
|
||||
#define SPA_ASYNC_MASK (3 << 30)
|
||||
#define SPA_ASYNC_SEQ_MASK (SPA_ASYNC_BIT - 1)
|
||||
|
||||
#define SPA_RESULT_IS_OK(res) ((res) >= 0)
|
||||
#define SPA_RESULT_IS_ERROR(res) ((res) < 0)
|
||||
#define SPA_RESULT_IS_ASYNC(res) (((res) & SPA_ASYNC_MASK) == SPA_ASYNC_BIT)
|
||||
|
||||
#define SPA_RESULT_ASYNC_SEQ(res) ((res) & SPA_ASYNC_SEQ_MASK)
|
||||
#define SPA_RESULT_RETURN_ASYNC(seq) (SPA_ASYNC_BIT | SPA_RESULT_ASYNC_SEQ(seq))
|
||||
|
||||
typedef int (*spa_result_func_t) (void *data, int seq, int res, const void *result);
|
||||
|
||||
struct spa_pending {
|
||||
struct spa_list link;
|
||||
int seq;
|
||||
int res;
|
||||
spa_result_func_t func;
|
||||
void *data;
|
||||
void (*removed) (struct spa_pending *pending);
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* SPA_UTILS_RESULT_H */
|
||||
Loading…
Add table
Add a link
Reference in a new issue