+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
main
| 1 | /* Simple Plugin API */ |
| 2 | /* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */ |
| 3 | /* SPDX-License-Identifier: MIT */ |
| 4 | |
| 5 | #ifndef SPA_IO_H |
| 6 | #define SPA_IO_H |
| 7 | |
| 8 | #include <spa/utils/defs.h> |
| 9 | #include <spa/pod/pod.h> |
| 10 | |
| 11 | #ifdef __cplusplus |
| 12 | extern "C" { |
| 13 | #endif |
| 14 | |
| 15 | /** |
| 16 | * \addtogroup spa_node |
| 17 | * \{ |
| 18 | */ |
| 19 | |
| 20 | /** IO areas |
| 21 | * |
| 22 | * IO information for a port on a node. This is allocated |
| 23 | * by the host and configured on a node or all ports for which |
| 24 | * IO is requested. |
| 25 | * |
| 26 | * The plugin will communicate with the host through the IO |
| 27 | * areas. |
| 28 | */ |
| 29 | |
| 30 | /** Different IO area types */ |
| 31 | enum spa_io_type { |
| 32 | SPA_IO_Invalid, |
| 33 | SPA_IO_Buffers, /**< area to exchange buffers, struct spa_io_buffers */ |
| 34 | SPA_IO_Range, /**< expected byte range, struct spa_io_range (currently not used in PipeWire) */ |
| 35 | SPA_IO_Clock, /**< area to update clock information, struct spa_io_clock */ |
| 36 | SPA_IO_Latency, /**< latency reporting, struct spa_io_latency (currently not used in |
| 37 | * PipeWire). \see spa_param_latency */ |
| 38 | SPA_IO_Control, /**< area for control messages, struct spa_io_sequence */ |
| 39 | SPA_IO_Notify, /**< area for notify messages, struct spa_io_sequence */ |
| 40 | SPA_IO_Position, /**< position information in the graph, struct spa_io_position */ |
| 41 | SPA_IO_RateMatch, /**< rate matching between nodes, struct spa_io_rate_match */ |
| 42 | SPA_IO_Memory, /**< memory pointer, struct spa_io_memory (currently not used in PipeWire) */ |
| 43 | SPA_IO_AsyncBuffers, /**< async area to exchange buffers, struct spa_io_async_buffers */ |
| 44 | }; |
| 45 | |
| 46 | /** |
| 47 | * IO area to exchange buffers. |
| 48 | * |
| 49 | * A set of buffers should first be configured on the node/port. |
| 50 | * Further references to those buffers will be made by using the |
| 51 | * id of the buffer. |
| 52 | * |
| 53 | * If status is SPA_STATUS_OK, the host should ignore |
| 54 | * the io area. |
| 55 | * |
| 56 | * If status is SPA_STATUS_NEED_DATA, the host should: |
| 57 | * 1) recycle the buffer in buffer_id, if possible |
| 58 | * 2) prepare a new buffer and place the id in buffer_id. |
| 59 | * |
| 60 | * If status is SPA_STATUS_HAVE_DATA, the host should consume |
| 61 | * the buffer in buffer_id and set the state to |
| 62 | * SPA_STATUS_NEED_DATA when new data is requested. |
| 63 | * |
| 64 | * If status is SPA_STATUS_STOPPED, some error occurred on the |
| 65 | * port. |
| 66 | * |
| 67 | * If status is SPA_STATUS_DRAINED, data from the io area was |
| 68 | * used to drain. |
| 69 | * |
| 70 | * Status can also be a negative errno value to indicate errors. |
| 71 | * such as: |
| 72 | * -EINVAL: buffer_id is invalid |
| 73 | * -EPIPE: no more buffers available |
| 74 | */ |
| 75 | struct spa_io_buffers { |
| 76 | #define SPA_STATUS_OK 0 |
| 77 | #define SPA_STATUS_NEED_DATA (1<<0) |
| 78 | #define SPA_STATUS_HAVE_DATA (1<<1) |
| 79 | #define SPA_STATUS_STOPPED (1<<2) |
| 80 | #define SPA_STATUS_DRAINED (1<<3) |
| 81 | int32_t status; /**< the status code */ |
| 82 | uint32_t buffer_id; /**< a buffer id */ |
| 83 | }; |
| 84 | |
| 85 | #define SPA_IO_BUFFERS_INIT ((struct spa_io_buffers) { SPA_STATUS_OK, SPA_ID_INVALID, }) |
| 86 | |
| 87 | /** |
| 88 | * IO area to exchange a memory region |
| 89 | */ |
| 90 | struct spa_io_memory { |
| 91 | int32_t status; /**< the status code */ |
| 92 | uint32_t size; /**< the size of \a data */ |
| 93 | void *data; /**< a memory pointer */ |
| 94 | }; |
| 95 | #define SPA_IO_MEMORY_INIT ((struct spa_io_memory) { SPA_STATUS_OK, 0, NULL, }) |
| 96 | |
| 97 | /** A range, suitable for input ports that can suggest a range to output ports */ |
| 98 | struct spa_io_range { |
| 99 | uint64_t offset; /**< offset in range */ |
| 100 | uint32_t min_size; /**< minimum size of data */ |
| 101 | uint32_t max_size; /**< maximum size of data */ |
| 102 | }; |
| 103 | |
| 104 | /** |
| 105 | * Absolute time reporting. |
| 106 | * |
| 107 | * Nodes that can report clocking information will receive this io block. |
| 108 | * The application sets the id. This is usually set as part of the |
| 109 | * position information but can also be set separately. |
| 110 | * |
| 111 | * The clock counts the elapsed time according to the clock provider |
| 112 | * since the provider was last started. |
| 113 | * |
| 114 | * Driver nodes are supposed to update the contents of \ref SPA_IO_Clock before |
| 115 | * signaling the start of a graph cycle. These updated clock values become |
| 116 | * visible to other nodes in \ref SPA_IO_Position. Non-driver nodes do |
| 117 | * not need to update the contents of their \ref SPA_IO_Clock. Also |
| 118 | * see \ref page_driver for further details. |
| 119 | * |
| 120 | * The host generally gives each node a separate \ref spa_io_clock in \ref |
| 121 | * SPA_IO_Clock, so that updates made by the driver are not visible in the |
| 122 | * contents of \ref SPA_IO_Clock of other nodes. Instead, \ref SPA_IO_Position |
| 123 | * is used to look up the current graph time. |
| 124 | * |
| 125 | * A node is a driver when \ref spa_io_clock::id and the ID in |
| 126 | * \ref spa_io_position.clock in \ref SPA_IO_Position are the same. |
| 127 | * |
| 128 | * The flags are set by the graph driver at the start of each cycle. |
| 129 | */ |
| 130 | struct spa_io_clock { |
| 131 | #define SPA_IO_CLOCK_FLAG_FREEWHEEL (1u<<0) /**< Graph is freewheeling. It runs at the maximum |
| 132 | * possible rate, only constrained by the processing |
| 133 | * power of the machine it runs on. This can be useful |
| 134 | * for offline processing, where processing in real |
| 135 | * time is not desired. */ |
| 136 | #define SPA_IO_CLOCK_FLAG_XRUN_RECOVER (1u<<1) /**< A node's process callback did not complete within |
| 137 | * the last cycle's deadline, resulting in an xrun. |
| 138 | * This flag is not set for the entire graph. Instead, |
| 139 | * it is set at the start of the current cycle before |
| 140 | * a node that experienced an xrun has its process |
| 141 | * callback invoked. After said callback finished, the |
| 142 | * flag is cleared again. That way, the node knows that |
| 143 | * during the last cycle it experienced an xrun. They |
| 144 | * can use this information for example to resynchronize |
| 145 | * or clear custom stale states. */ |
| 146 | #define SPA_IO_CLOCK_FLAG_LAZY (1u<<2) /**< The driver uses lazy scheduling. For details, see |
| 147 | * \ref PW_KEY_NODE_SUPPORTS_LAZY . */ |
| 148 | #define SPA_IO_CLOCK_FLAG_NO_RATE (1u<<3) /**< The rate of the clock is only approximately. |
| 149 | * It is recommended to use the nsec as a clock source. |
| 150 | * The rate_diff contains the measured inaccuracy. */ |
| 151 | #define SPA_IO_CLOCK_FLAG_DISCONT (1u<<4) /**< The clock experienced a discontinuity in its |
| 152 | * timestamps since the last cycle. If this is set, |
| 153 | * nodes know that timestamps between the last and |
| 154 | * the current cycle cannot be assumed to be |
| 155 | * continuous. Nodes that synchronize playback against |
| 156 | * clock timestamps should resynchronize (for example |
| 157 | * by flushing buffers to avoid incorrect delays). |
| 158 | * This differs from an xrun in that it is not necessariy |
| 159 | * an error and that it is not caused by missed process |
| 160 | * deadlines. If for example a custom network time |
| 161 | * based driver starts to follow a different time |
| 162 | * server, and the offset between that server and its |
| 163 | * local clock consequently suddenly changes, then that |
| 164 | * driver should set this flag. */ |
| 165 | uint32_t flags; /**< Clock flags */ |
| 166 | uint32_t id; /**< Unique clock id, set by host application */ |
| 167 | char name[64]; /**< Clock name prefixed with API, set by node when it receives |
| 168 | * \ref SPA_IO_Clock. The clock name is unique per clock and |
| 169 | * can be used to check if nodes share the same clock. */ |
| 170 | uint64_t nsec; /**< Time in nanoseconds against monotonic clock |
| 171 | * (CLOCK_MONOTONIC). This fields reflects a real time instant |
| 172 | * in the past, when the current cycle started. The value may |
| 173 | * have jitter. */ |
| 174 | struct spa_fraction rate; /**< Rate for position/duration/delay/xrun */ |
| 175 | uint64_t position; /**< Current position, in samples @ \ref rate */ |
| 176 | uint64_t duration; /**< Duration of current cycle, in samples @ \ref rate */ |
| 177 | int64_t delay; /**< Delay between position and hardware, in samples @ \ref rate */ |
| 178 | double rate_diff; /**< Rate difference between clock and monotonic time, as a ratio of |
| 179 | * clock speeds. A value higher than 1.0 means that the driver's |
| 180 | * internal clock is faster than the monotonic clock (by that |
| 181 | * factor), and vice versa. */ |
| 182 | uint64_t next_nsec; /**< Estimated next wakeup time in nanoseconds. |
| 183 | * This time is a logical start time of the next cycle, and |
| 184 | * is not necessarily in the future. |
| 185 | */ |
| 186 | |
| 187 | struct spa_fraction target_rate; /**< Target rate of next cycle */ |
| 188 | uint64_t target_duration; /**< Target duration of next cycle */ |
| 189 | uint32_t target_seq; /**< Seq counter. must be equal at start and |
| 190 | * end of read and lower bit must be 0 */ |
| 191 | uint32_t cycle; /**< incremented each time the graph is started */ |
| 192 | uint64_t xrun; /**< Estimated accumulated xrun duration */ |
| 193 | }; |
| 194 | |
| 195 | /* the size of the video in this cycle */ |
| 196 | struct spa_io_video_size { |
| 197 | #define SPA_IO_VIDEO_SIZE_VALID (1<<0) |
| 198 | uint32_t flags; /**< optional flags */ |
| 199 | uint32_t stride; /**< video stride in bytes */ |
| 200 | struct spa_rectangle size; /**< the video size */ |
| 201 | struct spa_fraction framerate; /**< the minimum framerate, the cycle duration is |
| 202 | * always smaller to ensure there is only one |
| 203 | * video frame per cycle. */ |
| 204 | uint32_t padding[4]; |
| 205 | }; |
| 206 | |
| 207 | /** |
| 208 | * Latency reporting |
| 209 | * |
| 210 | * Currently not used in PipeWire. Instead, \see spa_param_latency |
| 211 | */ |
| 212 | struct spa_io_latency { |
| 213 | struct spa_fraction rate; /**< rate for min/max */ |
| 214 | uint64_t min; /**< min latency */ |
| 215 | uint64_t max; /**< max latency */ |
| 216 | }; |
| 217 | |
| 218 | /** control stream, io area for SPA_IO_Control and SPA_IO_Notify */ |
| 219 | struct spa_io_sequence { |
| 220 | struct spa_pod_sequence sequence; /**< sequence of timed events */ |
| 221 | }; |
| 222 | |
| 223 | /** bar and beat segment */ |
| 224 | struct spa_io_segment_bar { |
| 225 | #define SPA_IO_SEGMENT_BAR_FLAG_VALID (1<<0) |
| 226 | uint32_t flags; /**< extra flags */ |
| 227 | uint32_t offset; /**< offset in segment of this beat */ |
| 228 | float signature_num; /**< time signature numerator */ |
| 229 | float signature_denom; /**< time signature denominator */ |
| 230 | double bpm; /**< beats per minute */ |
| 231 | double beat; /**< current beat in segment */ |
| 232 | double bar_start_tick; |
| 233 | double ticks_per_beat; |
| 234 | uint32_t padding[4]; |
| 235 | }; |
| 236 | |
| 237 | /** video frame segment */ |
| 238 | struct spa_io_segment_video { |
| 239 | #define SPA_IO_SEGMENT_VIDEO_FLAG_VALID (1<<0) |
| 240 | #define SPA_IO_SEGMENT_VIDEO_FLAG_DROP_FRAME (1<<1) |
| 241 | #define SPA_IO_SEGMENT_VIDEO_FLAG_PULL_DOWN (1<<2) |
| 242 | #define SPA_IO_SEGMENT_VIDEO_FLAG_INTERLACED (1<<3) |
| 243 | uint32_t flags; /**< flags */ |
| 244 | uint32_t offset; /**< offset in segment */ |
| 245 | struct spa_fraction framerate; |
| 246 | uint32_t hours; |
| 247 | uint32_t minutes; |
| 248 | uint32_t seconds; |
| 249 | uint32_t frames; |
| 250 | uint32_t field_count; /**< 0 for progressive, 1 and 2 for interlaced */ |
| 251 | uint32_t padding[11]; |
| 252 | }; |
| 253 | |
| 254 | /** |
| 255 | * A segment converts a running time to a segment (stream) position. |
| 256 | * |
| 257 | * The segment position is valid when the current running time is between |
| 258 | * start and start + duration. The position is then |
| 259 | * calculated as: |
| 260 | * |
| 261 | * (running time - start) * rate + position; |
| 262 | * |
| 263 | * Support for looping is done by specifying the LOOPING flags with a |
| 264 | * non-zero duration. When the running time reaches start + duration, |
| 265 | * duration is added to start and the loop repeats. |
| 266 | * |
| 267 | * Care has to be taken when the running time + clock.duration extends |
| 268 | * past the start + duration from the segment; the user should correctly |
| 269 | * wrap around and partially repeat the loop in the current cycle. |
| 270 | * |
| 271 | * Extra information can be placed in the segment by setting the valid flags |
| 272 | * and filling up the corresponding structures. |
| 273 | */ |
| 274 | struct spa_io_segment { |
| 275 | uint32_t version; |
| 276 | #define SPA_IO_SEGMENT_FLAG_LOOPING (1<<0) /**< after the duration, the segment repeats */ |
| 277 | #define SPA_IO_SEGMENT_FLAG_NO_POSITION (1<<1) /**< position is invalid. The position can be invalid |
| 278 | * after a seek, for example, when the exact mapping |
| 279 | * of the extra segment info (bar, video, ...) to |
| 280 | * position has not been determined yet */ |
| 281 | uint32_t flags; /**< extra flags */ |
| 282 | uint64_t start; /**< value of running time when this |
| 283 | * info is active. Can be in the future for |
| 284 | * pending changes. It does not have to be in |
| 285 | * exact multiples of the clock duration. */ |
| 286 | uint64_t duration; /**< duration when this info becomes invalid expressed |
| 287 | * in running time. If the duration is 0, this |
| 288 | * segment extends to the next segment. If the |
| 289 | * segment becomes invalid and the looping flag is |
| 290 | * set, the segment repeats. */ |
| 291 | double rate; /**< overall rate of the segment, can be negative for |
| 292 | * backwards time reporting. */ |
| 293 | uint64_t position; /**< The position when the running time == start. |
| 294 | * can be invalid when the owner of the extra segment |
| 295 | * information has not yet made the mapping. */ |
| 296 | |
| 297 | struct spa_io_segment_bar bar; |
| 298 | struct spa_io_segment_video video; |
| 299 | }; |
| 300 | |
| 301 | enum spa_io_position_state { |
| 302 | SPA_IO_POSITION_STATE_STOPPED, |
| 303 | SPA_IO_POSITION_STATE_STARTING, |
| 304 | SPA_IO_POSITION_STATE_RUNNING, |
| 305 | }; |
| 306 | |
| 307 | /** the maximum number of segments visible in the future */ |
| 308 | #define SPA_IO_POSITION_MAX_SEGMENTS 8 |
| 309 | |
| 310 | /** |
| 311 | * The position information adds extra meaning to the raw clock times. |
| 312 | * |
| 313 | * It is set on all nodes in \ref SPA_IO_Position, and the contents of \ref |
| 314 | * spa_io_position.clock contain the clock updates made by the driving node in |
| 315 | * the graph in its \ref SPA_IO_Clock. Also, the ID in \ref spa_io_position.clock |
| 316 | * will be the clock id of the driving node in the graph. |
| 317 | * |
| 318 | * The position clock indicates the logical start time of the current graph |
| 319 | * cycle. |
| 320 | * |
| 321 | * The position information contains 1 or more segments that convert the |
| 322 | * raw clock times to a stream time. They are sorted based on their |
| 323 | * start times, and thus the order in which they will activate in |
| 324 | * the future. This makes it possible to look ahead in the scheduled |
| 325 | * segments and anticipate the changes in the timeline. |
| 326 | */ |
| 327 | struct spa_io_position { |
| 328 | struct spa_io_clock clock; /**< clock position of driver, always valid and |
| 329 | * read only */ |
| 330 | struct spa_io_video_size video; /**< size of the video in the current cycle */ |
| 331 | int64_t offset; /**< an offset to subtract from the clock position |
| 332 | * to get a running time. This is the time that |
| 333 | * the state has been in the RUNNING state and the |
| 334 | * time that should be used to compare the segment |
| 335 | * start values against. */ |
| 336 | uint32_t state; /**< one of enum spa_io_position_state */ |
| 337 | |
| 338 | uint32_t n_segments; /**< number of segments */ |
| 339 | struct spa_io_segment segments[SPA_IO_POSITION_MAX_SEGMENTS]; /**< segments */ |
| 340 | }; |
| 341 | |
| 342 | /** |
| 343 | * Rate matching. |
| 344 | * |
| 345 | * It is usually set on the nodes that process resampled data, by |
| 346 | * the component (audioadapter) that handles resampling between graph |
| 347 | * and node rates. The \a flags and \a rate fields may be modified by the node. |
| 348 | * |
| 349 | * The node can request a correction to the resampling rate in its process(), by setting |
| 350 | * \ref SPA_IO_RATE_MATCH_FLAG_ACTIVE on \a flags, and setting \a rate to the desired rate |
| 351 | * correction. Usually the rate is obtained from DLL or other adaptive mechanism that |
| 352 | * e.g. drives the node buffer fill level toward a specific value. |
| 353 | * |
| 354 | * When resampling to (graph->node) direction, the number of samples produced |
| 355 | * by the resampler varies on each cycle, as the rates are not commensurate. |
| 356 | * |
| 357 | * When resampling to (node->graph) direction, the number of samples consumed by the |
| 358 | * resampler varies. Node output ports in process() should produce \a size number of |
| 359 | * samples to match what the resampler needs to produce one graph quantum of output |
| 360 | * samples. |
| 361 | * |
| 362 | * Resampling filters introduce processing delay, given by \a delay and \a delay_frac, in |
| 363 | * samples at node rate. The delay varies on each cycle e.g. when resampling between |
| 364 | * noncommensurate rates. |
| 365 | * |
| 366 | * The first sample output (graph->node) or consumed (node->graph) by the resampler is |
| 367 | * offset by \a delay + \a delay_frac / 1e9 node samples relative to the nominal graph |
| 368 | * cycle start position: |
| 369 | * |
| 370 | * \code{.unparsed} |
| 371 | * first_resampled_sample_nsec = |
| 372 | * first_original_sample_nsec |
| 373 | * - (rate_match->delay * SPA_NSEC_PER_SEC + rate_match->delay_frac) / node_rate |
| 374 | * \endcode |
| 375 | */ |
| 376 | struct spa_io_rate_match { |
| 377 | uint32_t delay; /**< resampling delay, in samples at |
| 378 | * node rate */ |
| 379 | uint32_t size; /**< requested input size for resampler */ |
| 380 | double rate; /**< rate for resampler (set by node) */ |
| 381 | #define SPA_IO_RATE_MATCH_FLAG_ACTIVE (1 << 0) |
| 382 | uint32_t flags; /**< extra flags (set by node) */ |
| 383 | int32_t delay_frac; /**< resampling delay fractional part, |
| 384 | * in units of nanosamples (1/10^9 sample) at node rate */ |
| 385 | uint32_t padding[6]; |
| 386 | }; |
| 387 | |
| 388 | /** async buffers */ |
| 389 | struct spa_io_async_buffers { |
| 390 | struct spa_io_buffers buffers[2]; /**< async buffers, writers write to current (cycle+1)&1, |
| 391 | * readers read from (cycle)&1 */ |
| 392 | }; |
| 393 | |
| 394 | /** |
| 395 | * \} |
| 396 | */ |
| 397 | |
| 398 | #ifdef __cplusplus |
| 399 | } /* extern "C" */ |
| 400 | #endif |
| 401 | |
| 402 | #endif /* SPA_IO_H */ |