├── .github └── workflows │ └── auto-publish.yml ├── .gitignore ├── LICENSE.md ├── README.md ├── draft-avtransport-spec.bs ├── libavtransport ├── README.md ├── address.c ├── address.h ├── attributes.h ├── avtransport.c ├── buffer.c ├── buffer.h ├── bytestream.h ├── common.h ├── connection.c ├── connection_internal.h ├── decode.c ├── extern │ └── xxhash.h ├── include │ └── avtransport │ │ ├── avtransport.h │ │ ├── connection.h │ │ ├── meson.build │ │ ├── rational.h │ │ ├── receive.h │ │ ├── send.h │ │ ├── stream.h │ │ ├── utils.h │ │ └── version.h.in ├── io_common.c ├── io_common.h ├── io_dcb.c ├── io_fd.c ├── io_file.c ├── io_mmap.c ├── io_null.c ├── io_socket_common.c ├── io_socket_common.h ├── io_template.c ├── io_udp.c ├── io_unix.c ├── io_utils.h ├── ldpc.c ├── ldpc.h ├── ldpc_decode.c ├── ldpc_decode.h ├── ldpc_encode.c ├── ldpc_encode.h ├── mem.h ├── merger.c ├── merger.h ├── meson.build ├── os_compat.h ├── output.c ├── output_internal.h ├── output_packet.c ├── output_packet.h ├── packet_common.h ├── protocol_common.c ├── protocol_common.h ├── protocol_datagram.c ├── protocol_quic.c ├── protocol_stream.c ├── raptor.c ├── raptor.h ├── rational.c ├── reorder.c ├── reorder.h ├── scheduler.c ├── scheduler.h ├── tests │ ├── address.c │ ├── file_io_common.c │ ├── file_io_common.h │ ├── io_fd.c │ ├── io_file.c │ ├── io_mmap.c │ ├── io_udp.c │ ├── io_udp_lite.c │ ├── io_unix.c │ ├── ldpc_encode.c │ ├── merger.c │ ├── meson.build │ ├── net_io_common.c │ ├── net_io_common.h │ ├── packet_encode_decode.c │ ├── proto_quic.c │ └── udp_common.c ├── tools │ └── spec2c.py ├── utils.c ├── utils_internal.h ├── utils_packet.h ├── utils_packet_template.h ├── version.c.in ├── version.h └── x86 │ ├── ldpc_encode.asm │ ├── meson.build │ └── x86inc.asm ├── meson.build ├── meson_options.txt └── tools ├── README.md ├── avcat.c ├── avcat ├── io.c └── io.h ├── avtdump.c ├── genopt.h └── meson.build /.github/workflows/auto-publish.yml: -------------------------------------------------------------------------------- 1 | name: Auto-Publish 2 | on: 3 | push: 4 | branches: [master] 5 | jobs: 6 | main: 7 | name: Deploy to GitHub pages 8 | runs-on: ubuntu-latest 9 | steps: 10 | - uses: actions/checkout@v2 11 | - uses: w3c/spec-prod@v2 12 | with: 13 | GH_PAGES_BRANCH: gh-pages 14 | TOOLCHAIN: bikeshed 15 | SOURCE: draft-avtransport-spec.bs 16 | DESTINATION: index.html 17 | VALIDATE_MARKUP: false 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | subprojects 3 | *.log 4 | *.av 5 | *.avt 6 | *.avts 7 | *.html 8 | .github/_build 9 | .github/_install 10 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | BSD 2-clause license 2 | ==================== 3 | 4 | Copyright © 2024, Lynne 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are met: 9 | 10 | 1. Redistributions of source code must retain the above copyright notice, this 11 | list of conditions and the following disclaimer. 12 | 13 | 2. Redistributions in binary form must reproduce the above copyright notice, 14 | this list of conditions and the following disclaimer in the documentation 15 | and/or other materials provided with the distribution. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 21 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /libavtransport/README.md: -------------------------------------------------------------------------------- 1 | # libavtransport 2 | 3 | The reference code for the implementation of the AVTransport protocol can be found here. 4 | 5 | As well as being clean, segmented and commented, the code is a fully usable and optimized library, 6 | rather than only being a reference. 7 | The library is meant for embedding into different projects. 8 | 9 | # API 10 | 11 | The external interface of the library is C89-compliant. 12 | 13 | To start with, users first have to allocate a shared context from which all other 14 | contexts can be created. This is done by calling `avt_init()`. 15 | 16 | Unlike most other APIs, libavtransport manages connections for you, though you still have 17 | the option of doing this yourself. 18 | Call `avt_connection_create()` with a suitable connection parameters. 19 | 20 | Connections are the central object to which inputs and outputs are attached to. 21 | A connection may have at most a single sender attached, but can have multiple receivers 22 | active at the same time. 23 | 24 | To create a sender context, simply call `avt_send_open()` on a connection and begin 25 | scheduling data to be sent. 26 | `avt_send_open()` can be called with a valid, non-NULL `AVTSender` object, 27 | in which case, the output will be linked to multiple connections. 28 | 29 | To create a receiver context, call `avt_receive_open()`. The callbacks will 30 | automatically be called. 31 | 32 | Once everything is initialized, if the `async` option was not set during 33 | `avvt_connection_create()`, users MUST call `avt_connection_process()` to 34 | both send and receive scheduled packets. 35 | 36 | # Internals 37 | 38 | The output flow graph is as follows: 39 | 40 | ``` 41 | - output.c (high-level API) 42 | * output_packet.c (high-level packetizer) 43 | - avt_connection_process (user must call this) 44 | - connection.c (high-level management + buffering) 45 | * scheduler.c (segmenter + interleaver + actual packet byte encoding) 46 | - protocol_*.c (encapsulation) 47 | - io_*.c (low-level reader/writer) 48 | ``` 49 | 50 | In certain IO cases where io_uring or mmapping can be used, the packet data path is done 51 | entirely with zero copies. 52 | 53 | The input flow graph is as follows: 54 | ``` 55 | - avt_connection_process 56 | - connection.c 57 | - protocol_*.c 58 | - io_*.c 59 | * Read bytes or packets 60 | - protocol_*.c 61 | * (optional header FEC) 62 | - connection 63 | * reordering, merging, and buffering 64 | - input 65 | * high-level stream management 66 | * calls users's callbacks 67 | ``` 68 | -------------------------------------------------------------------------------- /libavtransport/address.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_ADDRESS_H 28 | #define AVTRANSPORT_ADDRESS_H 29 | 30 | #include 31 | 32 | #include 33 | 34 | #define AVT_PROTOCOL_DATAGRAM (0) 35 | #define AVT_PROTOCOL_STREAM (AVT_PROTOCOL_QUIC + 1) 36 | #define AVT_PROTOCOL_FILE (AVT_PROTOCOL_QUIC + 2) 37 | #define AVT_PROTOCOL_CALLBACK_PKT (AVT_PROTOCOL_QUIC + 3) 38 | #define AVT_PROTOCOL_MAX (AVT_PROTOCOL_QUIC + 4) 39 | 40 | enum AVTAddressConnection { 41 | AVT_ADDRESS_NULL, 42 | AVT_ADDRESS_FILE, 43 | AVT_ADDRESS_FD, 44 | AVT_ADDRESS_UNIX, 45 | AVT_ADDRESS_URL, 46 | AVT_ADDRESS_SOCKET, 47 | AVT_ADDRESS_CALLBACK, 48 | }; 49 | 50 | typedef struct AVTCallbacksPacket { 51 | void *opaque; 52 | int (*out)(void *opaque, union AVTPacketData pkt, AVTBuffer *buf); 53 | int (*in)(void *opaque, union AVTPacketData *pkt, AVTBuffer **buf, uint64_t seq); 54 | } AVTCallbacksPacket; 55 | 56 | typedef struct AVTCallbacksData { 57 | void *opaque; 58 | int64_t (*write)(void *opaque, uint8_t hdr[AVT_MAX_HEADER_LEN], size_t hdr_len, 59 | AVTBuffer *payload); 60 | int64_t (*read)(void *opaque, AVTBuffer **data, size_t len, int64_t offset); 61 | } AVTCallbacksData; 62 | 63 | typedef struct AVTAddress { 64 | enum AVTAddressConnection type; 65 | 66 | /** FILE PATH/SOCKET PATH **/ 67 | char8_t *path; 68 | 69 | /** FILE DESCRIPTOR/SOCKET/NETWORK SOCKET **/ 70 | int fd; 71 | 72 | /** NETWORK **/ 73 | uint8_t host[256]; 74 | uint8_t uuid[16]; 75 | bool listen; /* Server or client */ 76 | 77 | /* Interface */ 78 | char *interface; 79 | uint32_t interface_idx; 80 | 81 | /* Address */ 82 | uint8_t ip[16]; /* Always mapped to IPv6 */ 83 | uint16_t port; 84 | uint32_t scope; /* sin6_scope_id */ 85 | 86 | enum AVTProtocolType proto; 87 | enum AVTProtocolMode mode; 88 | 89 | /* Options */ 90 | struct { 91 | /* UDP receive/transmit buffer sizes */ 92 | int rx_buf; 93 | int tx_buf; 94 | 95 | /* Default stream IDs */ 96 | uint16_t *default_sid; 97 | int nb_default_sid; 98 | 99 | int64_t start_time; // 1ns timebase 100 | 101 | /* For QUIC */ 102 | char *certfile; 103 | char *keyfile; 104 | } opts; 105 | 106 | /** CALLBACKS **/ 107 | AVTCallbacksPacket pcb; 108 | AVTCallbacksData dcb; 109 | } AVTAddress; 110 | 111 | /* Utilities */ 112 | int avt_parse_uuid(uint8_t out[16], const char *src); 113 | 114 | int avt_addr_4to6(uint8_t ip6[16], uint32_t ip4); 115 | 116 | int avt_addr_from_url(void *log_ctx, AVTAddress *addr, 117 | bool server, const char *path); 118 | 119 | int avt_addr_from_info(void *log_ctx, AVTAddress *addr, AVTConnectionInfo *info); 120 | 121 | void avt_addr_free(AVTAddress *addr); 122 | 123 | #endif /* AVTRANSPORT_ADDRESS_H */ 124 | -------------------------------------------------------------------------------- /libavtransport/attributes.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_ATTRIBUTES_H 28 | #define AVTRANSPORT_ATTRIBUTES_H 29 | 30 | #include 31 | #include "config.h" 32 | 33 | #ifndef __has_attribute 34 | #define __has_attribute(x) 0 35 | #endif 36 | 37 | #ifndef __has_feature 38 | #define __has_feature(x) 0 39 | #endif 40 | 41 | #ifdef __GNUC__ 42 | #define ATTR_ALIAS __attribute__((may_alias)) 43 | #define COLD __attribute__((cold)) 44 | #else 45 | #define ATTR_ALIAS 46 | #define COLD 47 | #endif 48 | 49 | #ifdef _MSC_VER 50 | #define ALWAYS_INLINE __forceinline 51 | #else 52 | #define ALWAYS_INLINE __attribute__((always_inline)) inline 53 | #endif 54 | 55 | /* Define tables with EXTERN to enable the compiler to optimize them better */ 56 | #if (defined(__ELF__) || defined(__MACH__) || (defined(_WIN32) && defined(__clang__))) && __has_attribute(visibility) 57 | #define EXTERN extern __attribute__((visibility("hidden"))) 58 | #else 59 | #define EXTERN extern 60 | #endif 61 | 62 | #if defined(NDEBUG) && (defined(__GNUC__) || defined(__clang__)) 63 | #undef assert 64 | #define assert(x) do { if (!(x)) __builtin_unreachable(); } while (0) 65 | #elif defined(NDEBUG) && defined(_MSC_VER) 66 | #undef assert 67 | #define assert __assume 68 | #endif 69 | 70 | #define avt_assert0(cond) assert(cond) 71 | 72 | #if CONFIG_ASSERT_LEVEL > 0 73 | #define avt_assert1(cond) assert(cond) 74 | #else 75 | #define avt_assert1(cond) 76 | #endif 77 | 78 | #if CONFIG_ASSERT_LEVEL > 1 79 | #define avt_assert2(cond) assert(cond) 80 | #else 81 | #define avt_assert2(cond) 82 | #endif 83 | 84 | #ifdef _MSC_VER 85 | #define PACKED(...) __pragma(pack(push, 1)) __VA_ARGS__ __pragma(pack(pop)) 86 | #else 87 | #define PACKED(...) __VA_ARGS__ __attribute__((__packed__)) 88 | #endif 89 | 90 | #endif /* AVTRANSPORT_ATTRIBUTES_H */ 91 | -------------------------------------------------------------------------------- /libavtransport/avtransport.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | #include 33 | 34 | #include "common.h" 35 | 36 | int avt_init(AVTContext **ctx, AVTContextOptions *opts) 37 | { 38 | AVTContext *tmp = malloc(sizeof(*tmp)); 39 | if (!tmp) 40 | return AVT_ERROR(ENOMEM); 41 | 42 | if (opts) 43 | tmp->opts = *opts; 44 | 45 | *ctx = tmp; 46 | return 0; 47 | } 48 | 49 | void avt_close(AVTContext **ctx) 50 | { 51 | if (ctx) { 52 | free(*ctx); 53 | *ctx = NULL; 54 | } 55 | } 56 | 57 | void avt_log(void *ctx, enum AVTLogLevel level, const char *fmt, ...) 58 | { 59 | va_list args; 60 | va_start(args, fmt); 61 | 62 | FILE *std = level == AVT_LOG_ERROR ? stderr : stdout; 63 | 64 | bool with_color = true; 65 | bool colored_message = 0; 66 | if (with_color) { 67 | switch (level) { 68 | case AVT_LOG_FATAL: 69 | fprintf(std, "\033[1;031m"); colored_message = true; break; 70 | case AVT_LOG_ERROR: 71 | fprintf(std, "\033[1;031m"); colored_message = true; break; 72 | case AVT_LOG_WARN: 73 | fprintf(std, "\033[1;033m"); colored_message = true; break; 74 | case AVT_LOG_VERBOSE: 75 | fprintf(std, "\033[38;5;46m"); colored_message = true; break; 76 | case AVT_LOG_DEBUG: 77 | fprintf(std, "\033[38;5;34m"); colored_message = true; break; 78 | case AVT_LOG_TRACE: 79 | fprintf(std, "\033[38;5;28m"); colored_message = true; break; 80 | default: 81 | break; 82 | } 83 | } 84 | 85 | vfprintf(std, fmt, args); 86 | 87 | if (colored_message) 88 | fprintf(std, "\033[0m"); 89 | 90 | va_end(args); 91 | } 92 | 93 | uint32_t avt_version_int(void) 94 | { 95 | return (AVTRANSPORT_VERSION_MAJOR << 16) | 96 | (AVTRANSPORT_VERSION_MINOR << 8) | 97 | (AVTRANSPORT_VERSION_MICRO << 0); 98 | 99 | } 100 | -------------------------------------------------------------------------------- /libavtransport/buffer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include "buffer.h" 32 | #include "utils_internal.h" 33 | 34 | AVT_API AVTBuffer *avt_buffer_create(uint8_t *data, size_t len, 35 | void *opaque, avt_free_cb free_cb) 36 | { 37 | AVTBuffer *buf = malloc(sizeof(*buf)); 38 | if (!buf) 39 | return NULL; 40 | 41 | int err = avt_buffer_quick_create(buf, data, len, opaque, free_cb, 0); 42 | if (err < 0) { 43 | free(buf); 44 | return NULL; 45 | } 46 | 47 | return buf; 48 | } 49 | 50 | void avt_buffer_update(AVTBuffer *buf, void *data, size_t len) 51 | { 52 | avt_assert0(avt_buffer_get_refcount(buf) == 1); 53 | buf->data = ((uint8_t *)data) + (buf->data - buf->base_data); 54 | buf->base_data = data; 55 | buf->end_data = ((uint8_t *)data) + len; 56 | buf->len = len; 57 | } 58 | 59 | int avt_buffer_resize(AVTBuffer *buf, size_t len) 60 | { 61 | /* Simple downsize, or there's enough allocated already */ 62 | if ((buf->len >= len) || 63 | (len < (buf->end_data - buf->data))) { 64 | buf->len = len; 65 | return 0; 66 | } 67 | 68 | /* Sanity checking */ 69 | avt_assert0(avt_buffer_get_refcount(buf) == 1); 70 | avt_assert0(buf->free == avt_buffer_default_free); 71 | 72 | /* Account for data before the current ref's slice */ 73 | size_t tmp; 74 | size_t pre_data = buf->data - buf->base_data; 75 | if (ckd_add(&tmp, len, pre_data)) 76 | return AVT_ERROR(EINVAL); 77 | 78 | uint8_t *newdata = realloc(buf->base_data, tmp); 79 | if (!newdata) 80 | return AVT_ERROR(ENOMEM); 81 | 82 | buf->base_data = newdata; 83 | buf->data = newdata + pre_data; 84 | buf->end_data = newdata + tmp; 85 | buf->len = len; 86 | 87 | return 0; 88 | } 89 | 90 | void avt_buffer_default_free(void *opaque, void *base_data, size_t len) 91 | { 92 | free(base_data); 93 | } 94 | 95 | AVTBuffer *avt_buffer_alloc(size_t len) 96 | { 97 | void *alloc = malloc(len); 98 | if (!alloc) 99 | return NULL; 100 | 101 | AVTBuffer *buf = avt_buffer_create(alloc, len, NULL, avt_buffer_default_free); 102 | if (!buf) { 103 | free(alloc); 104 | return NULL; 105 | } 106 | 107 | return buf; 108 | } 109 | 110 | AVTBuffer *avt_buffer_ref(AVTBuffer *buf, ptrdiff_t offset, size_t len) 111 | { 112 | if (!buf || !buf->refcnt) 113 | return NULL; 114 | 115 | if (!len) 116 | len = buf->end_data - (buf->data + offset); 117 | if (len < 0) 118 | return NULL; 119 | if (buf->base_data + offset > buf->end_data) 120 | return NULL; 121 | 122 | AVTBuffer *ret = malloc(sizeof(*ret)); 123 | if (!ret) 124 | return NULL; 125 | 126 | atomic_fetch_add_explicit(buf->refcnt, 1, memory_order_relaxed); 127 | 128 | memcpy(ret, buf, sizeof(*ret)); 129 | 130 | ret->data += offset; 131 | ret->len = !len ? ret->end_data - ret->data : len; 132 | 133 | return ret; 134 | } 135 | 136 | int avt_buffer_offset(AVTBuffer *buf, ptrdiff_t offset) 137 | { 138 | if (buf->base_data + offset > buf->end_data) 139 | return AVT_ERROR(EINVAL); 140 | 141 | buf->data += offset; 142 | 143 | return 0; 144 | } 145 | 146 | int avt_buffer_get_refcount(AVTBuffer *buf) 147 | { 148 | if (!buf || !buf->refcnt) 149 | return 0; 150 | 151 | return atomic_load_explicit(buf->refcnt, memory_order_relaxed); 152 | } 153 | 154 | void *avt_buffer_get_data(AVTBuffer *buf, size_t *len) 155 | { 156 | if (!buf || !buf->refcnt) { 157 | if (len) 158 | *len = 0; 159 | return NULL; 160 | } 161 | 162 | if (len) 163 | *len = buf->len; 164 | return buf->data; 165 | } 166 | 167 | size_t avt_buffer_get_data_len(const AVTBuffer *buf) 168 | { 169 | if (!buf || !buf->refcnt) 170 | return 0; 171 | 172 | return buf->len; 173 | } 174 | 175 | bool avt_buffer_read_only(AVTBuffer *buffer) 176 | { 177 | return !!(buffer->flags & AVT_BUFFER_FLAG_READ_ONLY); 178 | } 179 | 180 | void avt_buffer_unref(AVTBuffer **_buf) 181 | { 182 | AVTBuffer *buf = *_buf; 183 | if (!buf || !buf->refcnt) 184 | return; 185 | 186 | avt_buffer_quick_unref(buf); 187 | 188 | free(buf); 189 | 190 | *_buf = NULL; 191 | } 192 | -------------------------------------------------------------------------------- /libavtransport/buffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef LIBAVTRANSPORT_BUFFER 28 | #define LIBAVTRANSPORT_BUFFER 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | #include 35 | #include "attributes.h" 36 | 37 | enum AVTBufferFlags { 38 | AVT_BUFFER_FLAG_READ_ONLY = 1 << 0, 39 | }; 40 | 41 | struct AVTBuffer { 42 | uint8_t *data; /* Current ref's view of the buffer */ 43 | size_t len; /* Current ref's size of the view of the buffer */ 44 | 45 | uint8_t *base_data; /* Buffer's actual start data */ 46 | uint8_t *end_data; /* Buffer's end of data */ 47 | 48 | avt_free_cb free; 49 | void *opaque; 50 | enum AVTBufferFlags flags; 51 | atomic_int *refcnt; 52 | }; 53 | 54 | void avt_buffer_update(AVTBuffer *buf, void *data, size_t len); 55 | int avt_buffer_resize(AVTBuffer *buf, size_t len); 56 | 57 | static inline int avt_buffer_quick_create(AVTBuffer *buf, uint8_t *data, 58 | size_t len, void *opaque, 59 | avt_free_cb free_cb, 60 | enum AVTBufferFlags flags) 61 | { 62 | buf->refcnt = malloc(sizeof(*buf->refcnt)); 63 | if (!buf->refcnt) 64 | return AVT_ERROR(ENOMEM); 65 | 66 | atomic_init(buf->refcnt, 1); 67 | 68 | buf->base_data = data; 69 | buf->end_data = data + len; 70 | buf->data = data; 71 | buf->len = len; 72 | buf->opaque = opaque; 73 | if (!free_cb) 74 | buf->free = avt_buffer_default_free; 75 | else 76 | buf->free = free_cb; 77 | 78 | return 0; 79 | } 80 | 81 | static inline uint8_t *avt_buffer_quick_alloc(AVTBuffer *buf, size_t len) 82 | { 83 | void *alloc = malloc(len); 84 | if (!alloc) 85 | return NULL; 86 | 87 | int err = avt_buffer_quick_create(buf, alloc, len, NULL, 88 | avt_buffer_default_free , 0); 89 | if (err < 0) { 90 | free(alloc); 91 | return NULL; 92 | } 93 | 94 | return alloc; 95 | } 96 | 97 | static inline void avt_buffer_quick_unref(AVTBuffer *buf) 98 | { 99 | if (!buf || !buf->refcnt) 100 | return; 101 | 102 | if (atomic_fetch_sub_explicit(buf->refcnt, 1, memory_order_acq_rel) <= 1) { 103 | buf->free(buf->opaque, buf->data, buf->end_data - buf->base_data); 104 | free(buf->refcnt); 105 | } 106 | 107 | /* Zero out to avoid leaks */ 108 | memset(buf, 0, sizeof(*buf)); 109 | } 110 | 111 | static inline void avt_buffer_quick_ref(AVTBuffer *dst, AVTBuffer *buf, 112 | ptrdiff_t offset, size_t len) 113 | { 114 | avt_buffer_quick_unref(dst); 115 | 116 | if (!buf || !buf->refcnt) 117 | return; 118 | 119 | avt_assert0(buf->base_data + offset < buf->end_data); 120 | 121 | atomic_fetch_add_explicit(buf->refcnt, 1, memory_order_relaxed); 122 | memcpy(dst, buf, sizeof(*dst)); 123 | 124 | dst->data += offset; 125 | dst->len = (len == AVT_BUFFER_REF_ALL) ? (dst->end_data - dst->data) : len; 126 | } 127 | 128 | int avt_buffer_offset(AVTBuffer *buf, ptrdiff_t offset); 129 | 130 | #endif 131 | -------------------------------------------------------------------------------- /libavtransport/common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_COMMON 28 | #define AVTRANSPORT_COMMON 29 | 30 | #include 31 | 32 | #include 33 | 34 | typedef struct AVTStreamPriv { 35 | bool active; 36 | 37 | enum AVTCodecID codec_id; 38 | 39 | struct AVTSender *out; 40 | } AVTStreamPriv; 41 | 42 | struct AVTContext { 43 | AVTContextOptions opts; 44 | }; 45 | 46 | #endif /* AVTRANSPORT_COMMON */ 47 | -------------------------------------------------------------------------------- /libavtransport/connection.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include 29 | 30 | #include "connection_internal.h" 31 | #include "protocol_common.h" 32 | #include "io_common.h" 33 | #include "utils_internal.h" 34 | #include "scheduler.h" 35 | 36 | struct AVTConnection { 37 | AVTAddress addr; 38 | uint32_t session_seq; 39 | 40 | /* I/O */ 41 | const AVTIO *io; 42 | AVTIOCtx *io_ctx; 43 | 44 | /* Protocol */ 45 | const AVTProtocol *p; 46 | AVTProtocolCtx *p_ctx; 47 | 48 | /* File mirror */ 49 | const AVTProtocol *mirror; 50 | AVTProtocolCtx *mirror_ctx; 51 | 52 | /* Input buffer */ 53 | AVTPacketFifo in_fifo; 54 | 55 | /* Output FIFO, pre-scheduler */ 56 | AVTPacketFifo out_fifo_pre; 57 | AVTPacketFifo out_fifo_post; 58 | AVTScheduler out_scheduler; 59 | }; 60 | 61 | int avt_connection_destroy(AVTConnection **_conn) 62 | { 63 | AVTConnection *conn = *_conn; 64 | if (conn) 65 | return 0; 66 | 67 | int err = conn->p->close(&conn->p_ctx); 68 | 69 | avt_pkt_fifo_free(&conn->out_fifo_post); 70 | avt_scheduler_free(&conn->out_scheduler); 71 | avt_pkt_fifo_free(&conn->out_fifo_pre); 72 | avt_addr_free(&conn->addr); 73 | 74 | if (conn->p_ctx) 75 | conn->p->close(&conn->p_ctx); 76 | 77 | if (conn->io_ctx) 78 | conn->io->close(&conn->io_ctx); 79 | 80 | free(conn); 81 | *_conn = NULL; 82 | return err; 83 | } 84 | 85 | static int send_session_start_pkt(AVTConnection *conn) 86 | { 87 | AVTPktd p = { 88 | .pkt = AVT_SESSION_START_HDR( 89 | .session_seq = conn->session_seq, 90 | .session_flags = 0x0, 91 | .producer_major = AVTRANSPORT_VERSION_MAJOR, 92 | .producer_minor = AVTRANSPORT_VERSION_MINOR, 93 | .producer_micro = AVTRANSPORT_VERSION_MICRO, 94 | ), 95 | }; 96 | 97 | memcpy(p.pkt.session_start.session_uuid, conn->addr.uuid, 16); 98 | memccpy(p.pkt.session_start.producer_name, "avtransport", 99 | '\0', sizeof(p.pkt.session_start.producer_name)); 100 | 101 | int err = avt_scheduler_push(&conn->out_scheduler, &p); 102 | if (err < 0) 103 | return err; 104 | 105 | return 0; 106 | } 107 | 108 | int avt_connection_init(AVTContext *ctx, AVTConnection **_conn, 109 | AVTConnectionInfo *info) 110 | { 111 | int ret; 112 | AVTConnection *conn = calloc(1, sizeof(*conn)); 113 | if (!conn) 114 | return AVT_ERROR(ENOMEM); 115 | 116 | /* Address parsing */ 117 | ret = avt_addr_from_info(ctx, &conn->addr, info); 118 | if (ret < 0) 119 | goto fail; 120 | 121 | /* I/O init */ 122 | ret = avt_io_init(ctx, &conn->io, &conn->io_ctx, &conn->addr); 123 | if (ret < 0) 124 | goto fail; 125 | 126 | /* Protocol init */ 127 | AVTProtocolOpts opts = { 128 | .ldpc_iterations = info->input_opts.ldpc_iterations, 129 | }; 130 | ret = avt_protocol_init(ctx, &conn->p, &conn->p_ctx, &conn->addr, 131 | conn->io, conn->io_ctx, &opts); 132 | if (ret < 0) 133 | goto fail; 134 | 135 | /* Get max packet size */ 136 | size_t max_pkt_size; 137 | ret = conn->p->get_max_pkt_len(conn->p_ctx, &max_pkt_size); 138 | if (ret < 0) 139 | goto fail; 140 | 141 | /* Output scheduler */ 142 | ret = avt_scheduler_init(&conn->out_scheduler, max_pkt_size, 143 | info->output_opts.bandwidth); 144 | if (ret < 0) 145 | goto fail; 146 | 147 | /* Write a session start packet */ 148 | conn->session_seq = avt_get_time_ns() & 0xFFFFFFFF; 149 | ret = send_session_start_pkt(conn); 150 | if (ret < 0) 151 | goto fail; 152 | 153 | *_conn = conn; 154 | 155 | return 0; 156 | fail: 157 | avt_connection_destroy(&conn); 158 | return ret; 159 | } 160 | 161 | int avt_connection_send(AVTConnection *conn, AVTPktd *p) 162 | { 163 | int err; 164 | 165 | err = avt_scheduler_push(&conn->out_scheduler, p); 166 | if (err < 0) 167 | return err; 168 | 169 | return 0; 170 | } 171 | 172 | int avt_connection_process(AVTConnection *conn, int64_t timeout) 173 | { 174 | int err; 175 | 176 | AVTPacketFifo *seq; 177 | err = avt_scheduler_pop(&conn->out_scheduler, &seq); 178 | if (err < 0) 179 | return err; 180 | 181 | /* Ref segmented output packets for retransmission purposes */ 182 | err = avt_pkt_fifo_copy(&conn->out_fifo_pre, seq); 183 | if (err < 0) 184 | return err; 185 | 186 | err = conn->p->send_seq(conn->p_ctx, seq, timeout); 187 | if (err < 0) 188 | avt_scheduler_done(&conn->out_scheduler, seq); 189 | 190 | return err; 191 | } 192 | 193 | int avt_connection_flush(AVTConnection *conn, int64_t timeout) 194 | { 195 | int err; 196 | 197 | AVTPacketFifo *seq; 198 | err = avt_scheduler_flush(&conn->out_scheduler, &seq); 199 | if (err < 0) 200 | return err; 201 | 202 | if (seq) { 203 | err = conn->p->send_seq(conn->p_ctx, seq, timeout); 204 | if (err < 0) 205 | avt_scheduler_done(&conn->out_scheduler, seq); 206 | } 207 | 208 | return conn->p->flush(conn->p_ctx, timeout); 209 | } 210 | 211 | int avt_connection_mirror_open(AVTContext *ctx, AVTConnection *conn, 212 | AVTConnectionInfo *info) 213 | { 214 | return 0; 215 | } 216 | 217 | int avt_connection_mirror_close(AVTContext *ctx, AVTConnection *conn) 218 | { 219 | return 0; 220 | } 221 | -------------------------------------------------------------------------------- /libavtransport/connection_internal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_CONNECTION_INTERNAL_H 28 | #define AVTRANSPORT_CONNECTION_INTERNAL_H 29 | 30 | #include 31 | #include 32 | #include "packet_common.h" 33 | 34 | int avt_connection_register_sender(AVTConnection *conn, AVTSender *s); 35 | 36 | int avt_connection_send(AVTConnection *conn, AVTPktd *p); 37 | 38 | #endif /* AVTRANSPORT_CONNECTION_INTERNAL_H */ 39 | -------------------------------------------------------------------------------- /libavtransport/decode.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include "packet_decode.h" 28 | 29 | static int avt_decode_pkt(AVTBuffer *buf) 30 | { 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /libavtransport/include/avtransport/avtransport.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_H 28 | #define AVTRANSPORT_H 29 | 30 | #include "connection.h" 31 | #include "send.h" 32 | #include "receive.h" 33 | #include "version.h" 34 | 35 | enum AVTLogLevel { 36 | AVT_LOG_QUIET = -(1 << 0), 37 | AVT_LOG_FATAL = (0 << 0), 38 | AVT_LOG_ERROR = +(1 << 0), 39 | AVT_LOG_WARN = +(1 << 1), 40 | AVT_LOG_INFO = +(1 << 2), 41 | AVT_LOG_VERBOSE = +(1 << 3), 42 | AVT_LOG_DEBUG = +(1 << 4), 43 | AVT_LOG_TRACE = +(1 << 5), 44 | }; 45 | 46 | /* Library context-level options */ 47 | typedef struct AVTContextOptions { 48 | /* Logging context */ 49 | void *log_opaque; 50 | 51 | /* Logging callback */ 52 | void (*log_cb)(void *log_opaque, enum AVTLogLevel level, 53 | const char *format, va_list args, int error); 54 | 55 | char producer_name[16]; /* Name of the project linking to libavtransport */ 56 | uint16_t producer_ver[3]; /* Major, minor, micro version */ 57 | 58 | /* Padding to allow for future options. Must always be set to 0. */ 59 | uint8_t padding[1024 - 16*1 - 3*2 - 0*4 - 2*8]; 60 | } AVTContextOptions; 61 | 62 | /* Allocate an AVTransport context with the given context options. */ 63 | AVT_API int avt_init(AVTContext **ctx, AVTContextOptions *opts); 64 | 65 | /* Uninitialize a context, closing all connections and files gracefully, 66 | * and free all memory used. */ 67 | AVT_API void avt_close(AVTContext **ctx); 68 | 69 | /* Main logging function */ 70 | AVT_API void avt_log(void *ctx, enum AVTLogLevel level, const char *fmt, ...) avt_printf_format(3, 4); 71 | 72 | #endif /* AVTRANSPORT_H */ 73 | -------------------------------------------------------------------------------- /libavtransport/include/avtransport/meson.build: -------------------------------------------------------------------------------- 1 | # Copyright © 2024, Lynne 2 | # All rights reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions are met: 6 | # 7 | # 1. Redistributions of source code must retain the above copyright notice, this 8 | # list of conditions and the following disclaimer. 9 | # 10 | # 2. Redistributions in binary form must reproduce the above copyright notice, 11 | # this list of conditions and the following disclaimer in the documentation 12 | # and/or other materials provided with the distribution. 13 | # 14 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | 25 | # Structs and enums generated from the specs 26 | inc_dir = get_option('includedir') + '/avtransport' 27 | 28 | avtransport_spec_pub_headers = custom_target('packet*', 29 | install: true, 30 | install_dir: [ inc_dir, inc_dir ], 31 | input: spec_file, 32 | output: ['packet_enums.h', 'packet_data.h'], 33 | command: [python_exe, spec2c, 'packet_enums,packet_data', '@INPUT@', '@OUTPUT0@', '@OUTPUT1@'] 34 | ) 35 | 36 | # Version header 37 | version_h_data = configuration_data() 38 | version_h_data.set('AVTRANSPORT_VERSION_MAJOR', avtransport_version_major) 39 | version_h_data.set('AVTRANSPORT_VERSION_MINOR', avtransport_version_minor) 40 | version_h_data.set('AVTRANSPORT_VERSION_MICRO', avtransport_version_micro) 41 | version_h_target = configure_file(input: 'version.h.in', 42 | output: 'version.h', 43 | configuration: version_h_data 44 | ) 45 | 46 | # Headers 47 | lib_headers = [ 48 | 'avtransport.h', 49 | 'connection.h', 50 | 'send.h', 51 | 'receive.h', 52 | 'stream.h', 53 | 'rational.h', 54 | 'utils.h', 55 | 56 | version_h_target, 57 | ] 58 | 59 | install_headers(lib_headers, subdir: meson.project_name()) 60 | -------------------------------------------------------------------------------- /libavtransport/include/avtransport/rational.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_RATIONAL_H 28 | #define AVTRANSPORT_RATIONAL_H 29 | 30 | #include 31 | 32 | /* Rational data type structure. */ 33 | typedef struct AVTRational { 34 | int num; 35 | int den; 36 | } AVTRational; 37 | 38 | /* Rounding methods */ 39 | enum AVTRoundingMode { 40 | AVT_ROUND_ZERO = 0, /* Round toward zero */ 41 | AVT_ROUND_INF = 1, /* Round away from zero */ 42 | AVT_ROUND_DOWN = 2, /* Round toward -infinity */ 43 | AVT_ROUND_UP = 3, /* Round toward +infinity */ 44 | AVT_ROUND_NEAR_INF = 5, /* Round to nearest and halfway cases away from zero */ 45 | }; 46 | 47 | /* Reduces num/den by their common factors and returns a rational structure */ 48 | AVTRational avt_make_rational(int num, int den); 49 | 50 | /* Perform "a * b / c" in an overflow-free way, with a specific rounding */ 51 | int64_t avt_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVTRoundingMode rnd); 52 | int64_t avt_rescale_rational_rnd(int64_t a, AVTRational b, AVTRational c, 53 | enum AVTRoundingMode rnd); 54 | 55 | /* Same as above, but with rnd equal to AVT_ROUND_NEAR_INF */ 56 | int64_t avt_rescale(int64_t a, int64_t b, int64_t c); 57 | int64_t avt_rescale_rational(int64_t a, AVTRational b, AVTRational c); 58 | 59 | /** 60 | * Compare two timestamps "ts_a" and "ts_b", 61 | * which have timebases of "tb_a" and "tb_b", respectively. 62 | * Returns: -1 if ts_a is before ts_b 63 | * 0 if ts_a is equal to ts_b 64 | * +1 if ts_a is after ts_b 65 | */ 66 | int avt_compare_ts(int64_t ts_a, AVTRational tb_a, int64_t ts_b, AVTRational tb_b); 67 | 68 | /* Adds two rational numbers. Guarantees reversibility. */ 69 | int64_t avt_add_stable(AVTRational ts_tb, int64_t ts, 70 | AVTRational inc_tb, int64_t inc); 71 | 72 | /* Convert a rational to a float */ 73 | static inline double avt_r2d(AVTRational a) 74 | { 75 | return a.num / (double) a.den; 76 | } 77 | 78 | #endif /* AVTRANSPORT_RATIONAL_H */ 79 | -------------------------------------------------------------------------------- /libavtransport/include/avtransport/receive.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_RECEIVE_H 28 | #define AVTRANSPORT_RECEIVE_H 29 | 30 | #include "connection.h" 31 | #include "stream.h" 32 | #include "utils.h" 33 | 34 | typedef struct AVTReceiveOptions { 35 | /* Setting this flag to 1 enables receival of incomplete packets. 36 | * Regions with no data are set to 0. 37 | * 38 | * NOTE: Data from compressed packets will only be passed if decompression 39 | * (partially) succeeds. 40 | * NOTE: raw audio and video packets are always passed, even if incomplete, 41 | * as long as they are uncompressed. 42 | */ 43 | bool accept_incomplete; 44 | 45 | /* Padding to allow for future options. Must always be set to 0. */ 46 | uint8_t padding[4096 - 1*1 - 0*2 - 0*4 - 0*8]; 47 | } AVTReceiveOptions; 48 | 49 | /* List of callbacks. All are optional. */ 50 | typedef struct AVTReceiveCallbacks { 51 | /** 52 | * Stream registration/updating callbacks. 53 | */ 54 | int (*stream_register_cb)(void *opaque, AVTStream *st); 55 | void (*stream_update_cb)(void *opaque, AVTStream *st); 56 | 57 | /** 58 | * Font data 59 | * st can be NULL, in which case the font applies to all streams. 60 | */ 61 | void (*font_register_cb)(void *opaque, AVTStream *st, 62 | AVTBuffer *data, const char name[252]); 63 | 64 | void (*time_sync_cb)(void *opaque, uint64_t epoch); 65 | 66 | void (*metadata_cb)(void *opaque, AVTStream *st, AVTMetadata *meta); 67 | int (*user_pkt_cb)(void *opaque, AVTBuffer *data, uint16_t descriptor, 68 | uint16_t user, uint32_t seq); 69 | 70 | void (*duration_cb)(void *opaque, int64_t duration_nsec); 71 | void (*stream_close_cb)(void *opaque, AVTStream *st); 72 | 73 | /** 74 | * ============== Callbacks for complete packets ============== 75 | * This provides a full packet, continuous, error corrected and 76 | * called in incremental order. 77 | */ 78 | int (*stream_pkt_cb)(void *opaque, AVTStream *st, AVTPacket pkt); 79 | 80 | #if 0 81 | /* 82 | * ============== Callbacks for incomplete packets ============== 83 | * This API provides users the ability to receive and present packets 84 | * with minimal latency. Error correction will not be performed 85 | * (unless error correction for a segment is available immediately), 86 | * nor will any concatenation of segments be done. 87 | * 88 | * Users can use AVTPacket->total_size to know the total finished size 89 | * of the packet, and using the offset argument, can determine the 90 | * position of the segment. 91 | * 92 | * stream_pkt_cb will still be called with final, assembled, corrected 93 | * and incrementing packets. 94 | */ 95 | int (*stream_pkt_start_cb)(void *opaque, AVTStream *st, AVTPacket pkt, 96 | int present); 97 | int (*stream_pkt_seg_cb)(void *opaque, AVTStream *st, AVTPacket pkt, 98 | AVTBuffer *seg, size_t offset); 99 | #endif 100 | 101 | /* Reports a timeout, with the number of nanoseconds since the last packet */ 102 | void (*timeout)(void *opaque, uint64_t last_received); 103 | } AVTReceiveCallbacks; 104 | 105 | /* Open an AVTransport stream or a file for reading. */ 106 | AVT_API int avt_receive_open(AVTContext *ctx, AVTConnection *conn, 107 | AVTReceiveCallbacks *cb, void *cb_opaque, 108 | AVTReceiveOptions *opts); 109 | 110 | /* Adjusts input options on the fly. */ 111 | AVT_API int avt_receive_set_options(AVTContext *ctx, AVTReceiveOptions *opts); 112 | 113 | /* Close input and free all associated data with it. */ 114 | AVT_API int avt_receive_close(AVTContext *ctx); 115 | 116 | #endif /* AVTRANSPORT_RECEIVE_H */ 117 | -------------------------------------------------------------------------------- /libavtransport/include/avtransport/stream.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_STREAM_H 28 | #define AVTRANSPORT_STREAM_H 29 | 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #include "utils.h" 37 | #include "rational.h" 38 | #include "packet_enums.h" 39 | #include "packet_data.h" 40 | 41 | /* Stream context. 42 | * Current values reflect what should be used for the **next frame** to be 43 | * presented. 44 | * Users can tune into individual packets and maintain their own state. */ 45 | typedef struct AVTStream { 46 | uint32_t id; 47 | enum AVTCodecID codec_id; 48 | AVTMetadata *meta; 49 | 50 | /* Timebase of all timestamps in this structure */ 51 | AVTRational timebase; 52 | 53 | /* Duration in timebase units, if known. */ 54 | uint64_t duration; 55 | 56 | AVTVideoInfo video_info; 57 | AVTVideoOrientation video_orientation; 58 | 59 | AVTFontData font_info; 60 | 61 | AVTLutIcc icc_info; 62 | AVTBuffer *icc_data; 63 | int nb_icc; 64 | 65 | AVTLutIcc lut_info; 66 | AVTBuffer *lut_data; 67 | int nb_lut; 68 | 69 | enum AVTStreamFlags flags; 70 | uint64_t bitrate; 71 | 72 | AVTBuffer *init_data; 73 | 74 | struct AVTStream *related_to; 75 | struct AVTStream *derived_from; 76 | 77 | /* libavtransport private stream data. Do not touch or use. */ 78 | struct AVTStreamPriv *priv; 79 | 80 | /* Padding to allow for future options. Must always be set to 0. */ 81 | uint8_t padding[4096]; 82 | } AVTStream; 83 | 84 | typedef struct AVTPacket { 85 | AVTBuffer *data; 86 | size_t total_size; 87 | 88 | enum AVTFrameType type; 89 | int64_t pts; 90 | int64_t dts; 91 | int64_t duration; 92 | 93 | /* Padding to allow for future options. Must always be set to 0. */ 94 | uint8_t padding[64]; 95 | } AVTPacket; 96 | 97 | #endif 98 | -------------------------------------------------------------------------------- /libavtransport/include/avtransport/utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_UTILS_H 28 | #define AVTRANSPORT_UTILS_H 29 | 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | typedef struct AVTContext AVTContext; 36 | typedef struct AVTMetadata AVTMetadata; 37 | typedef struct AVTBuffer AVTBuffer; 38 | 39 | /* All functions return negative values for errors. 40 | * This is a wrapper that's used to convert standard stderr values into 41 | * negatives. */ 42 | #define AVT_ERROR(err) (-(err)) 43 | 44 | #define AVT_MIN(v1, v2) ((v1) < (v2) ? (v1) : (v2)) 45 | #define AVT_MAX(v1, v2) ((v1) > (v2) ? (v1) : (v2)) 46 | #define AVT_ARRAY_ELEMS(arr) (sizeof(arr) / sizeof((arr)[0])) 47 | 48 | /* Any function flagged with AVT_API is exported as a symbol */ 49 | #ifndef AVT_API 50 | #if defined _WIN32 51 | #if defined AVT_BUILDING_DLL 52 | #define AVT_API __declspec(dllexport) 53 | #else 54 | #define AVT_API 55 | #endif 56 | #else 57 | #if __GNUC__ >= 4 58 | #define AVT_API __attribute__ ((visibility ("default"))) 59 | #else 60 | #define AVT_API 61 | #endif 62 | #endif 63 | #endif 64 | 65 | #if defined(__GNUC__) || defined(__clang__) 66 | #define avt_printf_format(fmtpos, attrpos) __attribute__((__format__(__printf__, fmtpos, attrpos))) 67 | #else 68 | #define avt_printf_format(fmtpos, attrpos) 69 | #endif 70 | 71 | /* Shortcut to reference the entire length of data in the buffer. */ 72 | #define AVT_BUFFER_REF_ALL (0) 73 | 74 | /* Probe if the sequence of 36 bytes represents a valid AVT packet, or 75 | * the start of one. Returns 'true' if so. Result is trustworthy. */ 76 | AVT_API bool avt_data_probe(uint8_t data[36]); 77 | 78 | /* Default freeing callback for buffers that simply calls free(base_data) */ 79 | typedef void (*avt_free_cb)(void *opaque, void *base_data, size_t len); 80 | AVT_API void avt_buffer_default_free(void *opaque, void *base_data, size_t len); 81 | 82 | /* Create a reference counted buffer from existing data. */ 83 | AVT_API AVTBuffer *avt_buffer_create(uint8_t *data, size_t len, 84 | void *opaque, avt_free_cb free); 85 | 86 | /* Check if buffer is writable */ 87 | AVT_API bool avt_buffer_read_only(AVTBuffer *buffer); 88 | 89 | /* Create and allocate a reference counted buffer. */ 90 | AVT_API AVTBuffer *avt_buffer_alloc(size_t len); 91 | 92 | /* References the buffer. Returns a new reference at the offset and length requested. 93 | * If offset AND length are 0, references the whole buffer. */ 94 | AVT_API AVTBuffer *avt_buffer_ref(AVTBuffer *buffer, ptrdiff_t offset, size_t len); 95 | 96 | /* Returns the current numer of references */ 97 | AVT_API int avt_buffer_get_refcount(AVTBuffer *buffer); 98 | 99 | /* Access the data in a buffer. */ 100 | AVT_API void *avt_buffer_get_data(AVTBuffer *buffer, size_t *len); 101 | 102 | /* Get the data length */ 103 | AVT_API size_t avt_buffer_get_data_len(const AVTBuffer *buffer); 104 | 105 | /* Unreference a reference counted buffer. */ 106 | AVT_API void avt_buffer_unref(AVTBuffer **buffer); 107 | 108 | #endif /* AVTRANSPORT_UTILS_H */ 109 | -------------------------------------------------------------------------------- /libavtransport/include/avtransport/version.h.in: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_VERSION_H 28 | #define AVTRANSPORT_VERSION_H 29 | 30 | #include 31 | #include 32 | 33 | /** 34 | * Version from which the headers were built. 35 | */ 36 | #define AVTRANSPORT_VERSION_MAJOR @AVTRANSPORT_VERSION_MAJOR@ 37 | #define AVTRANSPORT_VERSION_MINOR @AVTRANSPORT_VERSION_MINOR@ 38 | #define AVTRANSPORT_VERSION_MICRO @AVTRANSPORT_VERSION_MICRO@ 39 | 40 | /** 41 | * Extract versions from the value returned by 42 | * avtransport_version_int() 43 | */ 44 | #define AVTRANSPORT_GET_VER_MAJOR(v) (((v) >> 16) & 0xFF) 45 | #define AVTRANSPORT_GET_VER_MINOR(v) (((v) >> 8) & 0xFF) 46 | #define AVTRANSPORT_GET_VER_MICRO(v) (((v) >> 0) & 0xFF) 47 | 48 | /** 49 | * Returns the version of the library. 50 | */ 51 | AVT_API uint32_t avt_version_int(void); 52 | 53 | #endif /* AVTRANSPORT_VERSION_H */ 54 | -------------------------------------------------------------------------------- /libavtransport/io_common.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include "io_common.h" 29 | #include "attributes.h" 30 | 31 | extern const AVTIO avt_io_null; 32 | extern const AVTIO avt_io_dcb; 33 | 34 | extern const AVTIO avt_io_file; 35 | #ifndef _WIN32 36 | extern const AVTIO avt_io_fd; 37 | extern const AVTIO avt_io_fd_path; 38 | extern const AVTIO avt_io_mmap; 39 | extern const AVTIO avt_io_mmap_path; 40 | #endif 41 | 42 | extern const AVTIO avt_io_udp; 43 | extern const AVTIO avt_io_udp_lite; 44 | 45 | extern const AVTIO avt_io_unix; 46 | 47 | #define MAX_NB_BACKENDS 4 48 | 49 | /* In order of preference */ 50 | static const AVTIO *avt_io_list[AVT_IO_INVALID][MAX_NB_BACKENDS + 1] = { 51 | [AVT_IO_NULL] = { 52 | &avt_io_null, 53 | }, 54 | [AVT_IO_CALLBACK] = { 55 | &avt_io_dcb, 56 | }, 57 | [AVT_IO_FILE] = { 58 | #ifndef _WIN32 59 | &avt_io_mmap_path, 60 | &avt_io_fd_path, 61 | #endif 62 | &avt_io_file, 63 | }, 64 | [AVT_IO_FD] = { 65 | #ifndef _WIN32 66 | &avt_io_mmap, 67 | &avt_io_fd, 68 | #endif 69 | }, 70 | [AVT_IO_UDP] = { 71 | &avt_io_udp, 72 | }, 73 | [AVT_IO_UNIX] = { 74 | &avt_io_unix, 75 | }, 76 | [AVT_IO_UDP_LITE] = { 77 | &avt_io_udp_lite, 78 | }, 79 | }; 80 | 81 | static inline enum AVTIOType map_addr_to_io(AVTAddress *addr) 82 | { 83 | switch (addr->type) { 84 | case AVT_ADDRESS_NULL: 85 | return AVT_IO_NULL; 86 | case AVT_ADDRESS_FILE: 87 | return AVT_IO_FILE; 88 | case AVT_ADDRESS_FD: 89 | return AVT_IO_FD; 90 | case AVT_ADDRESS_CALLBACK: 91 | return AVT_IO_CALLBACK; 92 | case AVT_ADDRESS_UNIX: 93 | return AVT_IO_UNIX; 94 | case AVT_ADDRESS_URL: 95 | [[fallthrough]]; 96 | case AVT_ADDRESS_SOCKET: 97 | if (addr->proto == AVT_PROTOCOL_UDP || addr->proto == AVT_PROTOCOL_QUIC) 98 | return AVT_IO_UDP; 99 | else if (addr->proto == AVT_PROTOCOL_UDP_LITE) 100 | return AVT_IO_UDP_LITE; 101 | [[fallthrough]]; 102 | default: 103 | return AVT_IO_INVALID; 104 | } 105 | } 106 | 107 | /* For protocols to call */ 108 | COLD int avt_io_init(AVTContext *ctx, const AVTIO **_io, AVTIOCtx **io_ctx, 109 | AVTAddress *addr) 110 | { 111 | enum AVTIOType io_type = map_addr_to_io(addr); 112 | if (io_type == AVT_IO_INVALID) 113 | return AVT_ERROR(EINVAL); 114 | 115 | int err; 116 | const AVTIO *io, **io_list = avt_io_list[io_type]; 117 | while ((io = *io_list)) { 118 | err = io->init(ctx, io_ctx, addr); 119 | if (err == AVT_ERROR(ENOMEM)) { 120 | return err; 121 | } else if (err < 0) { 122 | avt_log(ctx, AVT_LOG_TRACE, "Unable to open with I/O \"%s\": %i\n", 123 | io->name, err); 124 | continue; 125 | } 126 | avt_log(ctx, AVT_LOG_VERBOSE, "Using I/O \"%s\"\n", io->name); 127 | *_io = io; 128 | return err; 129 | } 130 | 131 | return AVT_ERROR(EINVAL); 132 | } 133 | -------------------------------------------------------------------------------- /libavtransport/io_common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_IO_COMMON 28 | #define AVTRANSPORT_IO_COMMON 29 | 30 | #include "address.h" 31 | #include "packet_common.h" 32 | 33 | /* Main type for offsets */ 34 | typedef int64_t avt_pos; 35 | 36 | enum AVTIOType { 37 | AVT_IO_NULL, /* Takes nothing */ 38 | AVT_IO_FILE, /* Takes a path */ 39 | AVT_IO_FD, /* Takes an integer handle fd */ 40 | AVT_IO_UNIX, /* Takes a Unix-domain integer handle */ 41 | AVT_IO_UDP, /* UDP network connection */ 42 | AVT_IO_UDP_LITE, /* UDP-Lite network connection */ 43 | AVT_IO_CALLBACK, /* Data-level callback */ 44 | AVT_IO_INVALID, /* Invalid */ 45 | }; 46 | 47 | enum AVTConnectionStateFlags { 48 | AVT_CONN_STATE_NEW_MTU, 49 | AVT_CONN_STATE_NEW_DROPPED_IN, 50 | }; 51 | 52 | typedef struct AVTConnectionState { 53 | enum AVTConnectionStateFlags flags; 54 | 55 | /* Raw MTU of the connection */ 56 | uint32_t mtu; 57 | 58 | /* Number of dropped packets on the input */ 59 | uint64_t nb_dropped_in; 60 | } AVTConnectionState; 61 | 62 | enum AVTIOReadFlags { 63 | /* Indicates that the read must be mutable. The IO must not modify 64 | * the buffer it receives, and must use it. */ 65 | AVT_IO_READ_MUTABLE = 1 << 0, 66 | }; 67 | 68 | /* Low level interface */ 69 | typedef struct AVTIOCtx AVTIOCtx; 70 | typedef struct AVTIO { 71 | const char *name; 72 | enum AVTIOType type; 73 | 74 | /* Use avt_io_open instead, which autodetects the best backend */ 75 | int (*init)(AVTContext *ctx, AVTIOCtx **io, AVTAddress *addr); 76 | 77 | /* Get maximum packet size, excluding any headers */ 78 | int (*get_max_pkt_len)(AVTIOCtx *io, size_t *mtu); 79 | 80 | /* Attempt to add a secondary destination, NULL if unsupported */ 81 | int (*add_dst)(AVTIOCtx *io, AVTAddress *addr); 82 | 83 | /* Removes a secondary destination, NULL if unsupported */ 84 | int (*del_dst)(AVTIOCtx *io, AVTAddress *addr); 85 | 86 | /* Write multiple packets. 87 | * Returns positive offset after writing on success, otherwise negative error. 88 | * May be NULL if unsupported. */ 89 | avt_pos (*write_vec)(AVTIOCtx *io, AVTPktd *pkt, uint32_t nb_pkt, 90 | int64_t timeout); 91 | 92 | /* Write a single packet to the output. 93 | * Returns positive offset after writing on success, otherwise negative error. */ 94 | avt_pos (*write_pkt)(AVTIOCtx *io, AVTPktd *p, int64_t timeout); 95 | 96 | /* Rewrite a packet at a specific location. 97 | * The old packet's size must exactly match the new packet. */ 98 | avt_pos (*rewrite)(AVTIOCtx *io, AVTPktd *p, avt_pos off, int64_t timeout); 99 | 100 | /* Read input from IO. Must be called with a valid AVTBuffer, which 101 | * has enough capacity to hold len number of bytes. 102 | * IOs are allowed to replace the buffer unless AVT_IO_READ_MUTABLE is set. 103 | * 104 | * The amount of bytes read may not match len. 105 | * 106 | * Returns positive current offset after reading on success, 107 | * otherwise negative error. */ 108 | avt_pos (*read_input)(AVTIOCtx *io, AVTBuffer *buf, size_t len, 109 | int64_t timeout, enum AVTIOReadFlags flags); 110 | 111 | /* Set the read position */ 112 | avt_pos (*seek)(AVTIOCtx *io, avt_pos off); 113 | 114 | /* Flush data written */ 115 | int (*flush)(AVTIOCtx *io, int64_t timeout); 116 | 117 | /* Close */ 118 | int (*close)(AVTIOCtx **io); 119 | } AVTIO; 120 | 121 | /* Initialize an IO (protocols-use, mainly) */ 122 | int avt_io_init(AVTContext *ctx, const AVTIO **io, AVTIOCtx **io_ctx, 123 | AVTAddress *addr); 124 | 125 | #endif /* AVTRANSPORT_IO_COMMON */ 126 | -------------------------------------------------------------------------------- /libavtransport/io_dcb.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include "io_common.h" 30 | #include "attributes.h" 31 | 32 | struct AVTIOCtx { 33 | AVTCallbacksData cb; 34 | int64_t rpos; 35 | }; 36 | 37 | static int dcb_close(AVTIOCtx **_io) 38 | { 39 | AVTIOCtx *io = *_io; 40 | free(io); 41 | *_io = NULL; 42 | return 0; 43 | } 44 | 45 | static COLD int dcb_init(AVTContext *ctx, AVTIOCtx **_io, AVTAddress *addr) 46 | { 47 | AVTIOCtx *io = malloc(sizeof(*io)); 48 | if (!io) 49 | return AVT_ERROR(ENOMEM); 50 | 51 | io->cb = addr->dcb; 52 | io->rpos = 0; 53 | 54 | *_io = io; 55 | return 0; 56 | } 57 | 58 | static int dcb_max_pkt_len(AVTIOCtx *io, size_t *mtu) 59 | { 60 | *mtu = SIZE_MAX; 61 | return 0; 62 | } 63 | 64 | static int64_t dcb_seek(AVTIOCtx *io, int64_t off) 65 | { 66 | return (io->rpos = off); 67 | } 68 | 69 | static int64_t dcb_input(AVTIOCtx *io, AVTBuffer *buf, size_t len, 70 | int64_t timeout, enum AVTIOReadFlags flags) 71 | { 72 | AVTBuffer *tmp; 73 | int64_t ret = io->cb.read(io->cb.opaque, &tmp, len, io->rpos); 74 | if (ret < 0) 75 | return ret; 76 | 77 | if (flags & AVT_IO_READ_MUTABLE) { 78 | uint8_t *dst_data = avt_buffer_get_data(buf, NULL); 79 | 80 | size_t src_len; 81 | uint8_t *src_data = avt_buffer_get_data(tmp, &src_len); 82 | 83 | memcpy(dst_data, src_data, AVT_MIN(len, src_len)); 84 | } else { 85 | avt_buffer_quick_ref(buf, tmp, 0, AVT_BUFFER_REF_ALL); 86 | } 87 | 88 | return (io->rpos = ret); 89 | } 90 | 91 | static int64_t dcb_write_pkt(AVTIOCtx *io, AVTPktd *p, int64_t timeout) 92 | { 93 | return io->cb.write(io->cb.opaque, p->hdr, p->hdr_len, &p->pl); 94 | } 95 | 96 | static int64_t dcb_write_vec(AVTIOCtx *io, AVTPktd *iov, uint32_t nb_iov, 97 | int64_t timeout) 98 | { 99 | int64_t ret; 100 | for (int i = 0; i < nb_iov; i++) { 101 | ret = io->cb.write(io->cb.opaque, iov[i].hdr, iov[i].hdr_len, &iov[i].pl); 102 | if (ret < 0) 103 | return ret; 104 | } 105 | return 0; 106 | } 107 | 108 | const AVTIO avt_io_dcb = { 109 | .name = "dcb", 110 | .type = AVT_IO_CALLBACK, 111 | .init = dcb_init, 112 | .get_max_pkt_len = dcb_max_pkt_len, 113 | .read_input = dcb_input, 114 | .write_vec = dcb_write_vec, 115 | .write_pkt = dcb_write_pkt, 116 | .rewrite = NULL, 117 | .seek = dcb_seek, 118 | .close = dcb_close, 119 | }; 120 | -------------------------------------------------------------------------------- /libavtransport/io_file.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include "os_compat.h" 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #include "io_common.h" 34 | #include "io_utils.h" 35 | #include "utils_internal.h" 36 | 37 | struct AVTIOCtx { 38 | FILE *f; 39 | avt_pos rpos; 40 | avt_pos wpos; 41 | bool is_write; 42 | }; 43 | 44 | static int file_handle_error(AVTIOCtx *io, const char *msg) 45 | { 46 | char8_t err_info[256]; 47 | strerror_safe(err_info, sizeof(err_info), errno); 48 | avt_log(io, AVT_LOG_ERROR, msg, err_info); 49 | return AVT_ERROR(errno); 50 | } 51 | 52 | static COLD int file_close(AVTIOCtx **_io) 53 | { 54 | AVTIOCtx *io = *_io; 55 | int ret = fclose(io->f); 56 | if (ret) 57 | ret = file_handle_error(io, "Error closing: %s\n"); 58 | 59 | free(io); 60 | *_io = NULL; 61 | 62 | return ret; 63 | } 64 | 65 | static COLD int file_init(AVTContext *ctx, AVTIOCtx **_io, AVTAddress *addr) 66 | { 67 | int ret; 68 | AVTIOCtx *io = calloc(1, sizeof(*io)); 69 | if (!io) 70 | return AVT_ERROR(ENOMEM); 71 | 72 | io->f = fopen(addr->path, "w+"); 73 | if (!io->f) { 74 | ret = file_handle_error(io, "Error opening: %s\n"); 75 | free(io); 76 | return ret; 77 | } 78 | 79 | *_io = io; 80 | 81 | return 0; 82 | } 83 | 84 | static inline avt_pos file_seek_to(AVTIOCtx *io, avt_pos pos) 85 | { 86 | return fseeko(io->f, pos, SEEK_SET); 87 | } 88 | 89 | static inline avt_pos file_read(AVTIOCtx *io, uint8_t *dst, size_t len, 90 | int64_t timeout) 91 | { 92 | return fread(dst, 1, len, io->f); 93 | } 94 | 95 | static inline avt_pos file_write(AVTIOCtx *io, uint8_t *src, size_t len, 96 | int64_t timeout) 97 | { 98 | return fwrite(src, 1, len, io->f); 99 | } 100 | 101 | static inline avt_pos file_offset(AVTIOCtx *io) 102 | { 103 | return ftello(io->f); 104 | } 105 | 106 | static int file_flush(AVTIOCtx *io, int64_t timeout) 107 | { 108 | int ret = fflush(io->f); 109 | if (ret) 110 | ret = file_handle_error(io, "Error flushing: %s\n"); 111 | 112 | return ret; 113 | } 114 | 115 | #define RENAME(x) file_ ## x 116 | 117 | #include "io_template.c" 118 | 119 | const AVTIO avt_io_file = { 120 | .name = "file", 121 | .type = AVT_IO_FILE, 122 | .init = file_init, 123 | .get_max_pkt_len = file_max_pkt_len, 124 | .read_input = file_read_input, 125 | .write_vec = file_write_vec, 126 | .write_pkt = file_write_pkt, 127 | .rewrite = file_rewrite, 128 | .seek = file_seek, 129 | .flush = file_flush, 130 | .close = file_close, 131 | }; 132 | -------------------------------------------------------------------------------- /libavtransport/io_null.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | #include "io_common.h" 34 | #include "utils_internal.h" 35 | #include "bytestream.h" 36 | #include "packet_encode.h" 37 | 38 | struct AVTIOCtx { 39 | uint64_t seq; 40 | avt_pos rpos; 41 | avt_pos wpos; 42 | }; 43 | 44 | static int null_close(AVTIOCtx **_io) 45 | { 46 | AVTIOCtx *io = *_io; 47 | free(io); 48 | *_io = NULL; 49 | return 0; 50 | } 51 | 52 | static int null_init(AVTContext *ctx, AVTIOCtx **_io, AVTAddress *addr) 53 | { 54 | AVTIOCtx *io = calloc(1, sizeof(*io)); 55 | if (!io) 56 | return AVT_ERROR(ENOMEM); 57 | 58 | *_io = io; 59 | return 0; 60 | } 61 | 62 | static int null_max_pkt_len(AVTIOCtx *io, size_t *mtu) 63 | { 64 | *mtu = SIZE_MAX; 65 | return 0; 66 | } 67 | 68 | static int null_add_dst(AVTIOCtx *io, AVTAddress *addr) 69 | { 70 | return 0; 71 | } 72 | 73 | static int null_del_dst(AVTIOCtx *io, AVTAddress *addr) 74 | { 75 | return 0; 76 | } 77 | 78 | static avt_pos null_input(AVTIOCtx *io, AVTBuffer *buf, size_t len, 79 | int64_t timeout, enum AVTIOReadFlags flags) 80 | { 81 | avt_assert1(len == AVT_MAX_HEADER_LEN); 82 | uint8_t *data = avt_buffer_get_data(buf, NULL); 83 | AVTBytestream bs = avt_bs_init(data, AVT_MAX_HEADER_LEN); 84 | 85 | union AVTPacketData pkt = { .session_start = { 86 | .global_seq = io->seq++, 87 | .session_flags = 0x0, 88 | .producer_major = AVTRANSPORT_VERSION_MAJOR, 89 | .producer_minor = AVTRANSPORT_VERSION_MINOR, 90 | .producer_micro = AVTRANSPORT_VERSION_MICRO, 91 | }}; 92 | memcpy(pkt.session_start.producer_name, "avtransport", strlen("avtransport")); 93 | 94 | avt_encode_session_start(&bs, pkt.session_start); 95 | 96 | avt_pos tmp = io->rpos + avt_bs_offs(&bs); 97 | AVT_SWAP(io->rpos, tmp); 98 | return tmp; 99 | } 100 | 101 | static avt_pos null_write_vec(AVTIOCtx *io, AVTPktd *iov, uint32_t nb_iov, 102 | int64_t timeout) 103 | { 104 | avt_pos acc = 0; 105 | for (auto i = 0; i < nb_iov; i++) 106 | acc += iov[i].hdr_len + avt_buffer_get_data_len(&iov[i].pl); 107 | 108 | acc = io->wpos + acc; 109 | AVT_SWAP(io->wpos, acc); 110 | return acc; 111 | } 112 | 113 | static avt_pos null_write_pkt(AVTIOCtx *io, AVTPktd *p, int64_t timeout) 114 | { 115 | avt_pos prev = io->wpos; 116 | io->wpos += p->hdr_len + avt_buffer_get_data_len(&p->pl); 117 | return prev; 118 | } 119 | 120 | static avt_pos null_rewrite(AVTIOCtx *io, AVTPktd *p, avt_pos off, 121 | int64_t timeout) 122 | { 123 | return off; 124 | } 125 | 126 | static avt_pos null_seek(AVTIOCtx *io, avt_pos off) 127 | { 128 | io->rpos = off; 129 | return off; 130 | } 131 | 132 | static int null_flush(AVTIOCtx *io, int64_t timeout) 133 | { 134 | return 0; 135 | } 136 | 137 | const AVTIO avt_io_null = { 138 | .name = "null", 139 | .type = AVT_IO_NULL, 140 | .init = null_init, 141 | .get_max_pkt_len = null_max_pkt_len, 142 | .add_dst = null_add_dst, 143 | .del_dst = null_del_dst, 144 | .read_input = null_input, 145 | .write_vec = null_write_vec, 146 | .write_pkt = null_write_pkt, 147 | .rewrite = null_rewrite, 148 | .seek = null_seek, 149 | .flush = null_flush, 150 | .close = null_close, 151 | }; 152 | -------------------------------------------------------------------------------- /libavtransport/io_socket_common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_IO_SOCKET_COMMON_H 28 | #define AVTRANSPORT_IO_SOCKET_COMMON_H 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | #include 35 | #include "address.h" 36 | 37 | typedef struct AVTSocketCommon { 38 | int socket; 39 | 40 | struct sockaddr *local_addr; 41 | struct sockaddr *remote_addr; 42 | socklen_t addr_size; 43 | 44 | union { 45 | struct { 46 | struct sockaddr_in6 local_addr; 47 | struct sockaddr_in6 remote_addr; 48 | } ip; 49 | struct { 50 | struct sockaddr_un local_addr; 51 | struct sockaddr_un remote_addr; 52 | } un; 53 | }; 54 | } AVTSocketCommon; 55 | 56 | /* Open a socket with a specific address */ 57 | int avt_socket_open(void *log_ctx, AVTSocketCommon *sc, AVTAddress *addr); 58 | 59 | /* Close a socket context */ 60 | int avt_socket_close(void *log_ctx, AVTSocketCommon *sc); 61 | 62 | /* Macro to set socket options */ 63 | #define SET_SOCKET_OPT(log_ctx, socket, lvl, opt, val) \ 64 | do { \ 65 | [[maybe_unused]] auto tempval = (val); \ 66 | ret = setsockopt(socket, lvl, opt, \ 67 | _Generic((val), \ 68 | int32_t: &(tempval), \ 69 | uint32_t: &(tempval), \ 70 | int64_t: &(tempval), \ 71 | uint64_t: &(tempval), \ 72 | default: (val)), \ 73 | sizeof(val)); \ 74 | if (ret < 0) { \ 75 | ret = avt_handle_errno(log_ctx, \ 76 | "setsockopt(" #lvl ", " #opt ") " \ 77 | "failed: %i %s\n"); \ 78 | return ret; \ 79 | } \ 80 | } while (0) 81 | 82 | /* Read a socket option */ 83 | int avt_socket_get_opt(void *log_ctx, int socket, 84 | int lvl, int opt, void *data, int len, 85 | const char *errmsg); 86 | 87 | /* Get MTU */ 88 | int avt_socket_get_mtu(void *log_ctx, AVTSocketCommon *sc, size_t *mtu); 89 | 90 | #endif /* AVTRANSPORT_IO_SOCKET_COMMON_H */ 91 | -------------------------------------------------------------------------------- /libavtransport/io_utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_IO_UTILS_H 28 | #define AVTRANSPORT_IO_UTILS_H 29 | 30 | #include 31 | #include "os_compat.h" 32 | 33 | static inline int avt_handle_errno(void *log_ctx, const char *msg) 34 | { 35 | char8_t err_info[256]; 36 | avt_log(log_ctx, AVT_LOG_ERROR, msg, errno, 37 | strerror_safe(err_info, sizeof(err_info), errno)); 38 | return AVT_ERROR(errno); 39 | } 40 | 41 | #endif /* AVTRANSPORT_IO_UTILS_H */ 42 | -------------------------------------------------------------------------------- /libavtransport/ldpc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include "ldpc.h" 28 | -------------------------------------------------------------------------------- /libavtransport/ldpc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef LIBAVTRANSPORT_LDPC 28 | #define LIBAVTRANSPORT_LDPC 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /libavtransport/ldpc_decode.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include "ldpc_decode.h" 28 | 29 | void avt_ldpc_decode_288_224(uint8_t *dst, int iterations) 30 | { 31 | } 32 | 33 | void avt_ldpc_decode_2784_2016(uint8_t *dst, int iterations) 34 | { 35 | } 36 | -------------------------------------------------------------------------------- /libavtransport/ldpc_decode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_LDPC_DECODE 28 | #define AVTRANSPORT_LDPC_DECODE 29 | 30 | #include 31 | 32 | /* Decodes systematic LDPC codes. dst must point to the start of the message. */ 33 | 34 | void avt_ldpc_decode_288_224(uint8_t *dst, int iterations); 35 | 36 | void avt_ldpc_decode_2784_2016(uint8_t *dst, int iterations); 37 | 38 | #endif /* AVTRANSPORT_LDPC_DECODE */ 39 | -------------------------------------------------------------------------------- /libavtransport/ldpc_encode.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include "ldpc_encode.h" 28 | #include "ldpc_tables.h" 29 | #include "bytestream.h" 30 | 31 | void avt_ldpc_encode_288_224(uint8_t *src) 32 | { 33 | ldpc_encode(src, ldpc_h_matrix_288_224, 224, 64); 34 | } 35 | 36 | void avt_ldpc_encode_2784_2016(uint8_t *src) 37 | { 38 | ldpc_encode(src, ldpc_h_matrix_2784_2016, 2016, 768); 39 | } 40 | -------------------------------------------------------------------------------- /libavtransport/ldpc_encode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_LDPC_ENCODE 28 | #define AVTRANSPORT_LDPC_ENCODE 29 | 30 | #include 31 | 32 | /* Computes and writes an LDPC code at the end of the data buffer. 33 | * dst pointer given must point to the start of the sequence. */ 34 | 35 | void avt_ldpc_encode_288_224(uint8_t *dst); 36 | 37 | void avt_ldpc_encode_2784_2016(uint8_t *dst); 38 | 39 | #endif /* AVTRANSPORT_LDPC_ENCODE */ 40 | -------------------------------------------------------------------------------- /libavtransport/mem.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_MEM_H 28 | #define AVTRANSPORT_MEM_H 29 | 30 | #include 31 | #include 32 | 33 | #if defined(__GNUC__) 34 | #define avt_alloc_attrib(...) __attribute__((__malloc__, alloc_size(__VA_ARGS__))) 35 | #else 36 | #define avt_alloc_attrib(...) 37 | #endif 38 | 39 | avt_alloc_attrib(2, 3) static inline void *avt_reallocarray(void *ptr, 40 | size_t nmemb, size_t size) 41 | { 42 | size_t nbytes; 43 | 44 | if (ckd_mul(&nbytes, nmemb, size)) 45 | return NULL; 46 | 47 | return realloc(ptr, nbytes); 48 | } 49 | 50 | /* Generic macro for creating contexts which need to keep their addresses 51 | * if another context is created. */ 52 | #define FN_CREATING(prefix, ctx, type, shortname, array, num) \ 53 | static inline type *prefix##_##create_##shortname(ctx *dctx) \ 54 | { \ 55 | type **array, *sctx = calloc(1, sizeof(*sctx)); \ 56 | if (!sctx) \ 57 | return NULL; \ 58 | \ 59 | array = avt_reallocarray(dctx->array, dctx->num + 1, sizeof(type *)); \ 60 | if (!array) { \ 61 | free(sctx); \ 62 | return NULL; \ 63 | } \ 64 | \ 65 | dctx->array = array; \ 66 | dctx->array[dctx->num++] = sctx; \ 67 | \ 68 | return sctx; \ 69 | } 70 | 71 | #endif /* AVTRANSPORT_MEM_H */ 72 | -------------------------------------------------------------------------------- /libavtransport/merger.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_MERGER_H 28 | #define AVTRANSPORT_MERGER_H 29 | 30 | #include "packet_common.h" 31 | 32 | typedef struct AVTMergerRange { 33 | uint32_t offset; 34 | uint32_t size; 35 | } AVTMergerRange; 36 | 37 | /* One merger per seq ID */ 38 | typedef struct AVTMerger { 39 | bool active; /* If there's an active packet that needs more segments */ 40 | uint32_t target; /* Main packet's sequence number */ 41 | uint32_t last; /* Most recent (timeline-wise, not submit-wise) packet's ID */ 42 | uint32_t nb_tgt_packets; /* Total number of packets consumed for merger */ 43 | 44 | /* Phantom header recovery */ 45 | bool p_avail; /* If the header is available */ 46 | uint8_t hdr_mask; /* 1 bit per 32-bits, 7th bit first, 0th bit last */ 47 | 48 | /* Packet data */ 49 | AVTPktd p; 50 | uint32_t pkt_len_track; 51 | uint32_t target_tot_len; 52 | /* Packet data ranges */ 53 | AVTMergerRange *ranges; 54 | uint32_t nb_ranges; 55 | uint32_t ranges_allocated; 56 | 57 | /* Parity data for the packet */ 58 | AVTBuffer parity; // Preserved between resets 59 | uint32_t pkt_parity_len_track; 60 | uint32_t parity_tot_len; 61 | /* Parity date ranges */ 62 | AVTMergerRange *parity_ranges; 63 | uint32_t nb_parity_ranges; 64 | uint32_t parity_ranges_allocated; 65 | } AVTMerger; 66 | 67 | /* Basic merger function. 68 | * Returns the payload size once an output is possible. 69 | * Returns AVT_ERROR(EAGAIN) if more segments are needed. 70 | * Returns AVT_ERROR(EBUSY) if a packet belonging to a different target is 71 | * given. 72 | * Returns an error code in all other circumstances. */ 73 | int avt_pkt_merge_seg(void *log_ctx, AVTMerger *m, AVTPktd *p); 74 | 75 | /* Output, if possible. */ 76 | int avt_pkt_merge_out(void *log_ctx, AVTMerger *m, AVTPktd *p, int force); 77 | 78 | /* avt_pkt_merge_seg() will reject any packet part of another group. 79 | * If there's a packet which cannot be output, call this to reset the context. */ 80 | void avt_pkt_merge_done(AVTMerger *m); 81 | 82 | /* Free up and reset the context upon uninitialization entirely. 83 | * Frees up the parity data buffer as well. */ 84 | void avt_pkt_merge_free(AVTMerger *m); 85 | 86 | #endif /* AVTRANSPORT_MERGER_H */ 87 | -------------------------------------------------------------------------------- /libavtransport/meson.build: -------------------------------------------------------------------------------- 1 | # Copyright © 2024, Lynne 2 | # All rights reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions are met: 6 | # 7 | # 1. Redistributions of source code must retain the above copyright notice, this 8 | # list of conditions and the following disclaimer. 9 | # 10 | # 2. Redistributions in binary form must reproduce the above copyright notice, 11 | # this list of conditions and the following disclaimer in the documentation 12 | # and/or other materials provided with the distribution. 13 | # 14 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | 25 | spec2c = files('./tools/spec2c.py') 26 | 27 | avtransport_spec_pkt_headers = custom_target( 28 | 'packet_encode_decode', 29 | input: spec_file, 30 | output: ['packet_encode.h', 'packet_decode.h', 'ldpc_tables.h', 'ldpc_tables.c'], 31 | command: [python_exe, spec2c, 'packet_encode,packet_decode,ldpc_tables_h,ldpc_tables_c', '@INPUT@', '@OUTPUT0@', '@OUTPUT1@', '@OUTPUT2@', '@OUTPUT3@'] 32 | ) 33 | 34 | # Generate additional headers from spec and install them 35 | subdir('include/avtransport') 36 | 37 | # Base files 38 | sources = [ 39 | 'avtransport.c', 40 | 'buffer.c', 41 | 'utils.c', 42 | 'rational.c', 43 | 44 | 'ldpc.c', 45 | 'raptor.c', 46 | 47 | 'address.c', 48 | 'connection.c', 49 | 50 | 'protocol_common.c', 51 | 'protocol_stream.c', 52 | 'protocol_datagram.c', 53 | 54 | 'io_common.c', 55 | 'io_null.c', 56 | 'io_file.c', 57 | 'io_unix.c', 58 | 'io_dcb.c', 59 | 60 | 'io_udp.c', 61 | 'io_socket_common.c', 62 | 63 | 'output.c', 64 | 'output_packet.c', 65 | 'scheduler.c', 66 | 'ldpc_encode.c', 67 | 68 | 'reorder.c', 69 | 'merger.c', 70 | 'ldpc_decode.c', 71 | 72 | avtransport_spec_pkt_headers, 73 | 74 | # Version 75 | vcs_tag(command: ['git', 'rev-parse', '--short', 'HEAD'], 76 | input: 'version.c.in', output: 'version.c', 77 | fallback: 'release') 78 | ] 79 | 80 | if openssl_dep.found() 81 | sources += 'protocol_quic.c' 82 | endif 83 | 84 | if host_machine.system() != 'windows' 85 | sources += 'io_fd.c' 86 | sources += 'io_mmap.c' 87 | endif 88 | 89 | if get_option('enable_asm').enabled() 90 | if host_machine.cpu_family().startswith('x86') 91 | if add_languages('nasm', required: false, native: false) 92 | subdir('x86') 93 | else 94 | warning('nasm not found! libavtransport will be compiled without assembly optimizations!') 95 | endif 96 | endif 97 | endif 98 | 99 | if host_machine.system() == 'windows' 100 | avtransport_soversion = '' 101 | else 102 | avtransport_soversion = meson.project_version() 103 | endif 104 | 105 | avtransport_inc = include_directories('./include') 106 | 107 | # Check for headers 108 | conf_libavt = conf 109 | 110 | # Generate local configure file 111 | configure_file( 112 | output: 'config.h', 113 | configuration: conf_libavt, 114 | ) 115 | 116 | # Deps 117 | avtransport_deps_list = [ 118 | xxh_dep, 119 | cbor_dep, 120 | openssl_dep, 121 | uring_dep, 122 | zstd_dep, 123 | brotlienc_dep, 124 | brotlidec_dep, 125 | ] 126 | 127 | avtransport_lib = library('avtransport', 128 | sources, 129 | install: true, 130 | 131 | sources: avtransport_spec_pub_headers, 132 | include_directories: avtransport_inc, 133 | dependencies: avtransport_deps_list, 134 | version: meson.project_version(), 135 | soversion: avtransport_soversion, 136 | ) 137 | 138 | avtransport_dep = declare_dependency( 139 | link_with: avtransport_lib, 140 | include_directories: avtransport_inc, 141 | sources: avtransport_spec_pub_headers, 142 | ) 143 | 144 | # Do tests if enabled 145 | if get_option('tests').auto() 146 | subdir('tests') 147 | endif 148 | 149 | # Allows projects to build libavtransport by cloning into ./subprojects/libavtransport 150 | meson.override_dependency('libavtransport', avtransport_dep) 151 | 152 | pkg = import('pkgconfig') 153 | pkg.generate( 154 | name: meson.project_name(), 155 | description: 'AVTransport reference library', 156 | libraries: avtransport_lib, 157 | version: meson.project_version(), 158 | ) 159 | 160 | summary({ 161 | 'xxhash': xxh_dep.found(), 162 | 'cbor': cbor_dep.found(), 163 | 'openssl': openssl_dep.found(), 164 | 'uring': uring_dep.found(), 165 | 'zstd': zstd_dep.found(), 166 | 'brotli (enc)': brotlienc_dep.found(), 167 | 'brotli (dec)': brotlidec_dep.found(), 168 | }, section: 'libavtransport', bool_yn: true) 169 | -------------------------------------------------------------------------------- /libavtransport/os_compat.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_OS_COMPAT 28 | #define AVTRANSPORT_OS_COMPAT 29 | 30 | #include 31 | 32 | #include "config.h" 33 | 34 | #if !defined(_GNU_SOURCE) && !defined(_XOPEN_SOURCE) && !defined(_POSIX_C_SOURCE) 35 | #define _POSIX_C_SOURCE 200809L 36 | #endif 37 | 38 | #include 39 | #include 40 | 41 | static inline const char8_t *strerror_safe(char8_t *buf, size_t bufsize, int err) 42 | { 43 | #ifdef _WIN32 44 | strerror_s(buf, bufsize, err); 45 | return buf; 46 | #else 47 | auto ret = strerror_r(err, buf, bufsize); 48 | return _Generic(ret, const char *: ret, char *: ret, default: buf); 49 | #endif 50 | } 51 | 52 | #endif /* AVTRANSPORT_OS_COMPAT */ 53 | -------------------------------------------------------------------------------- /libavtransport/output.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include "utils_internal.h" 32 | #include "output_internal.h" 33 | #include "output_packet.h" 34 | #include "mem.h" 35 | 36 | #include "config.h" 37 | 38 | int avt_send_close(AVTSender **_s) 39 | { 40 | AVTSender *s = *_s; 41 | 42 | #ifdef CONFIG_HAVE_LIBZSTD 43 | ZSTD_freeCCtx(s->zstd_ctx); 44 | #endif 45 | 46 | XXH3_freeState(s->xxh_state); 47 | 48 | free(s); 49 | 50 | *_s = NULL; 51 | 52 | return 0; 53 | } 54 | 55 | static inline int alloc_output_context(AVTSender **_s, AVTSenderOptions *opts) 56 | { 57 | AVTSender *s = calloc(1, sizeof(*s)); 58 | if (!s) 59 | return AVT_ERROR(ENOMEM); 60 | 61 | s->epoch = avt_get_time_ns(); 62 | s->opts = *opts; 63 | 64 | if (!(s->opts.compress & (~AVT_SENDER_COMPRESS_FORCE))) { 65 | s->opts.compress |= AVT_SENDER_COMPRESS_META | 66 | AVT_SENDER_COMPRESS_AUX | 67 | AVT_SENDER_COMPRESS_SUBS | 68 | AVT_SENDER_COMPRESS_AUDIO | 69 | AVT_SENDER_COMPRESS_VIDEO; 70 | } 71 | 72 | /* Allocate one here just so we don't have to tear this down later in 73 | * case malloc fails */ 74 | s->conn = calloc(1, sizeof(*s->conn)); 75 | if (!s->conn) { 76 | avt_send_close(&s); 77 | return AVT_ERROR(ENOMEM); 78 | } 79 | s->nb_conn_alloc = 1; 80 | 81 | /* Init xxHash state */ 82 | s->xxh_state = XXH3_createState(); 83 | if (!s->xxh_state) { 84 | avt_send_close(&s); 85 | return AVT_ERROR(ENOMEM); 86 | } 87 | 88 | #ifdef CONFIG_HAVE_LIBZSTD 89 | /* Init Zstd context */ 90 | s->zstd_ctx = ZSTD_createCCtx(); 91 | if (!s->zstd_ctx) { 92 | avt_send_close(&s); 93 | return AVT_ERROR(ENOMEM); 94 | } 95 | #endif 96 | 97 | *_s = s; 98 | 99 | return 0; 100 | } 101 | 102 | int avt_send_open(AVTContext *ctx, AVTSender **_s, 103 | AVTConnection *conn, AVTSenderOptions *opts) 104 | { 105 | int err; 106 | AVTSender *s = NULL; 107 | 108 | // TODO: query connection status here 109 | 110 | /* Allocate state, if not already existing */ 111 | if (!(*_s)) { 112 | err = alloc_output_context(&s, opts); 113 | if (err < 0) 114 | return err; 115 | *_s = s; 116 | } else { 117 | s = *_s; 118 | } 119 | 120 | /* Register connection for output */ 121 | if (s->nb_conn_alloc < (s->nb_conn + 1)) { 122 | AVTConnection **tmp = avt_reallocarray(s->conn, 123 | s->nb_conn_alloc + 1, 124 | sizeof(*tmp)); 125 | if (!tmp) 126 | return AVT_ERROR(ENOMEM); 127 | 128 | s->conn = tmp; 129 | s->nb_conn_alloc++; 130 | } 131 | 132 | s->conn[s->nb_conn++] = conn; 133 | 134 | return 0; 135 | } 136 | 137 | AVTStream *avt_send_stream_add(AVTSender *out, uint16_t id) 138 | { 139 | if (id == UINT16_MAX) { 140 | avt_log(out, AVT_LOG_ERROR, "Invalid stream ID: 0x%X is reserved!\n", id); 141 | return NULL; 142 | } 143 | 144 | AVTStream *st = &out->streams[id]; 145 | if (st->priv && st->priv->active) { 146 | avt_log(out, AVT_LOG_ERROR, "Stream 0x%X is already active!\n", id); 147 | return NULL; 148 | } 149 | 150 | st->id = id; 151 | if (!st->priv) { 152 | st->priv = calloc(1, sizeof(*st->priv)); 153 | if (!st->priv) 154 | return NULL; 155 | } 156 | 157 | st->priv->active = true; 158 | st->priv->out = out; 159 | out->active_stream_idx[out->nb_streams++] = id; 160 | 161 | return st; 162 | } 163 | 164 | int avt_send_stream_close(AVTStream **_st) 165 | { 166 | if (!_st) 167 | return 0; 168 | 169 | AVTStream *st = *_st; 170 | if (!st) 171 | return 0; 172 | 173 | if (!st->priv || !st->priv->active) { 174 | avt_log(st->priv->out, AVT_LOG_ERROR, "Stream 0x%X is not active!\n", st->id); 175 | return AVT_ERROR(EINVAL); 176 | } 177 | 178 | st->priv->active = false; 179 | 180 | return 0; 181 | } 182 | 183 | int avt_send_stream_update(AVTStream *st) 184 | { 185 | return avt_send_pkt_stream_register(st->priv->out, st); 186 | } 187 | 188 | int avt_send_stream_data(AVTStream *st, AVTPacket *pkt) 189 | { 190 | return avt_send_pkt_stream_data(st->priv->out, st, pkt); 191 | } 192 | -------------------------------------------------------------------------------- /libavtransport/output_internal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_OUTPUT_INTERNAL_H 28 | #define AVTRANSPORT_OUTPUT_INTERNAL_H 29 | 30 | #include 31 | 32 | #include "common.h" 33 | #include "connection_internal.h" 34 | 35 | #include "config.h" 36 | 37 | #ifdef CONFIG_HAVE_LIBXXH 38 | #include 39 | #else 40 | #define XXH_INLINE_ALL 41 | #include "extern/xxhash.h" 42 | #endif 43 | 44 | #ifdef CONFIG_HAVE_LIBZSTD 45 | #include 46 | #endif 47 | 48 | typedef struct AVTSender { 49 | AVTContext *ctx; 50 | AVTSenderOptions opts; 51 | 52 | AVTConnection **conn; 53 | uint32_t nb_conn; 54 | uint32_t nb_conn_alloc; 55 | 56 | AVTStream streams[UINT16_MAX]; 57 | uint16_t active_stream_idx[UINT16_MAX]; 58 | int nb_streams; 59 | 60 | uint64_t epoch; 61 | 62 | XXH3_state_t *xxh_state; 63 | #ifdef CONFIG_HAVE_LIBZSTD 64 | ZSTD_CCtx *zstd_ctx; 65 | #endif 66 | } AVTSender; 67 | 68 | #endif /* AVTRANSPORT_OUTPUT_INTERNAL_H */ 69 | -------------------------------------------------------------------------------- /libavtransport/output_packet.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include "output_internal.h" 30 | 31 | /* Session start */ 32 | int avt_send_pkt_session_start(AVTSender *s); 33 | 34 | /* Time sync */ 35 | int avt_send_pkt_time_sync(AVTSender *s); 36 | 37 | /* Stream registration and data */ 38 | int avt_send_pkt_stream_register(AVTSender *s, AVTStream *st); 39 | int avt_send_pkt_stream_data(AVTSender *s, AVTStream *st, AVTPacket *pkt); 40 | 41 | /* Generic data */ 42 | int avt_send_pkt_generic_data(AVTSender *s, 43 | AVTStream *st, AVTBuffer *data, int64_t pts, 44 | uint32_t init_desc, uint32_t seg_desc); 45 | 46 | /* LUT/ICC */ 47 | int avt_send_pkt_lut_data(AVTSender *s, AVTStream *st); 48 | int avt_send_pkt_icc_data(AVTSender *s, AVTStream *st); 49 | 50 | /* Video info/orientation */ 51 | int avt_send_pkt_video_info(AVTSender *s, AVTStream *st); 52 | int avt_send_pkt_video_orientation(AVTSender *s, AVTStream *st); 53 | -------------------------------------------------------------------------------- /libavtransport/packet_common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_PACKET_COMMON_H 28 | #define AVTRANSPORT_PACKET_COMMON_H 29 | 30 | #include 31 | #include "buffer.h" 32 | 33 | #define AVT_MAX_HEADER_BUF ((1024 - AVT_MAX_HEADER_LEN) + AVT_MAX_HEADER_LEN) 34 | 35 | /* Internal packet representation */ 36 | typedef struct AVTPktd { 37 | uint8_t hdr[AVT_MAX_HEADER_BUF]; 38 | union AVTPacketData pkt; 39 | AVTBuffer pl; 40 | uint8_t pl_hash[16]; 41 | uint16_t hdr_len; 42 | uint16_t hdr_off; 43 | bool pl_has_hash; 44 | } AVTPktd; 45 | 46 | #endif /* AVTRANSPORT_PACKET_COMMON_H */ 47 | -------------------------------------------------------------------------------- /libavtransport/protocol_common.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include "packet_decode.h" 29 | #include "protocol_common.h" 30 | #include "mem.h" 31 | 32 | extern const AVTProtocol avt_protocol_datagram; 33 | extern const AVTProtocol avt_protocol_stream; 34 | extern const AVTProtocol avt_protocol_pkt; 35 | 36 | #ifdef CONFIG_HAVE_OPENSSL 37 | extern const AVTProtocol avt_protocol_quic; 38 | #endif 39 | 40 | static const AVTProtocol *avt_protocol_list[AVT_PROTOCOL_MAX] = { 41 | [AVT_PROTOCOL_DATAGRAM] = &avt_protocol_datagram, 42 | [AVT_PROTOCOL_UDP] = &avt_protocol_datagram, 43 | [AVT_PROTOCOL_UDP_LITE] = &avt_protocol_datagram, 44 | [AVT_PROTOCOL_STREAM] = &avt_protocol_stream, 45 | [AVT_PROTOCOL_FILE] = &avt_protocol_stream, 46 | #ifdef CONFIG_HAVE_OPENSSL 47 | [AVT_PROTOCOL_QUIC] = &avt_protocol_quic, 48 | #endif 49 | // [AVT_PROTOCOL_CALLBACK_PKT] = &avt_protocol_pkt, 50 | }; 51 | 52 | /* For connections to call */ 53 | int avt_protocol_init(AVTContext *ctx, const AVTProtocol **_p, 54 | AVTProtocolCtx **p_ctx, AVTAddress *addr, 55 | const AVTIO *io, AVTIOCtx *io_ctx, AVTProtocolOpts *opts) 56 | { 57 | const AVTProtocol *p = avt_protocol_list[addr->proto]; 58 | if (!p) { 59 | avt_log(ctx, AVT_LOG_ERROR, "No support for protocol #%i\n", 60 | addr->proto); 61 | return AVT_ERROR(ENOTSUP); 62 | } 63 | 64 | int err = p->init(ctx, p_ctx, addr, io, io_ctx, opts); 65 | if (err >= 0) 66 | *_p = p; 67 | 68 | return err; 69 | } 70 | 71 | int avt_index_list_parse(AVTIndexContext *ic, AVTBytestream *bs, 72 | AVTStreamIndex *pkt) 73 | { 74 | for (auto i = 0; i < pkt->nb_indices; i++) { 75 | auto dst = ic->nb_index; 76 | if (ic->nb_index >= ic->nb_index_max) { 77 | dst = ic->nb_index_total % ic->nb_index_max; 78 | } else if ((ic->nb_index + 1) > ic->nb_alloc_index) { 79 | AVTIndexEntry *tmp = avt_reallocarray(ic->index, 80 | ic->nb_alloc_index + 1, 81 | sizeof(*tmp)); 82 | if (!tmp) 83 | return AVT_ERROR(ENOMEM); 84 | 85 | ic->index = tmp; 86 | ic->nb_index++; 87 | ic->nb_alloc_index++; 88 | } 89 | 90 | avt_decode_index_entry(bs, &ic->index[dst]); 91 | ic->nb_index_total++; 92 | } 93 | 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /libavtransport/protocol_common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_PROTOCOL_COMMON 28 | #define AVTRANSPORT_PROTOCOL_COMMON 29 | 30 | #include "bytestream.h" 31 | #include "utils_internal.h" 32 | #include "io_common.h" 33 | 34 | typedef struct AVTProtocolOpts { 35 | int ldpc_iterations; 36 | } AVTProtocolOpts; 37 | 38 | /* High level interface */ 39 | typedef struct AVTProtocolCtx AVTProtocolCtx; 40 | typedef struct AVTProtocol { 41 | const char *name; 42 | enum AVTProtocolType type; 43 | 44 | /* Initialize a context */ 45 | int (*init)(AVTContext *ctx, AVTProtocolCtx **p, AVTAddress *addr, 46 | const AVTIO *io, AVTIOCtx *io_ctx, AVTProtocolOpts *opts); 47 | 48 | /* Attempt to add a secondary destination, NULL if unsupported */ 49 | int (*add_dst)(AVTProtocolCtx *p, AVTAddress *addr); 50 | /* Removes a secondary destination, NULL if unsupported */ 51 | int (*rm_dst)(AVTProtocolCtx *p, AVTAddress *addr); 52 | 53 | /* Return the maximum packet length */ 54 | int (*get_max_pkt_len)(AVTProtocolCtx *p, size_t *mtu); 55 | 56 | /* Send. Returns positive offset on success, otherwise negative error. 57 | * Returns offset to which packet was written to. */ 58 | int (*send_packet)(AVTProtocolCtx *p, AVTPktd *pkt, int64_t timeout); 59 | 60 | /* Send a sequence of packets. Returns positive offset on success, 61 | otherwise negative error */ 62 | int (*send_seq)(AVTProtocolCtx *p, AVTPacketFifo *seq, int64_t timeout); 63 | 64 | /* Overwrite a packet at a given offset */ 65 | int (*update_packet)(AVTProtocolCtx *p, 66 | union AVTPacketData pkt, AVTBuffer *pl, 67 | void **series, int64_t pos); 68 | 69 | /* Receive a packet. Returns offset after reading. */ 70 | int (*receive)(AVTProtocolCtx *s, AVTPacketFifo *fifo, int64_t timeout); 71 | 72 | /* Seek to a place in the stream */ 73 | int (*seek)(AVTProtocolCtx *p, int64_t off, uint32_t seq, 74 | int64_t ts, bool ts_is_dts); 75 | 76 | /* Flush buffered data */ 77 | int (*flush)(AVTProtocolCtx *p, int64_t timeout); 78 | 79 | /* Close */ 80 | int (*close)(AVTProtocolCtx **p); 81 | } AVTProtocol; 82 | 83 | COLD int avt_protocol_init(AVTContext *ctx, const AVTProtocol **_p, 84 | AVTProtocolCtx **p_ctx, AVTAddress *addr, 85 | const AVTIO *io, AVTIOCtx *io_ctx, 86 | AVTProtocolOpts *opts); 87 | 88 | typedef struct AVTIndexContext { 89 | AVTIndexEntry *index; 90 | int nb_index; 91 | int nb_alloc_index; 92 | uint64_t nb_index_total; 93 | uint64_t nb_index_max; 94 | } AVTIndexContext; 95 | 96 | int avt_index_list_config(AVTIndexContext *ic, uint64_t nb_index_max); 97 | int avt_index_list_parse(AVTIndexContext *ic, AVTBytestream *bs, 98 | AVTStreamIndex *pkt); 99 | 100 | #endif /* AVTRANSPORT_PROTOCOL_COMMON */ 101 | -------------------------------------------------------------------------------- /libavtransport/protocol_datagram.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | #include "protocol_common.h" 33 | #include "io_common.h" 34 | 35 | struct AVTProtocolCtx { 36 | const AVTIO *io; 37 | AVTIOCtx *io_ctx; 38 | AVTProtocolOpts opts; 39 | }; 40 | 41 | static COLD int datagram_proto_close(AVTProtocolCtx **p) 42 | { 43 | AVTProtocolCtx *priv = *p; 44 | free(priv); 45 | *p = NULL; 46 | return 0; 47 | } 48 | 49 | static COLD int datagram_proto_init(AVTContext *ctx, AVTProtocolCtx **_p, AVTAddress *addr, 50 | const AVTIO *io, AVTIOCtx *io_ctx, AVTProtocolOpts *opts) 51 | { 52 | AVTProtocolCtx *p = malloc(sizeof(*p)); 53 | if (!p) 54 | return AVT_ERROR(ENOMEM); 55 | 56 | p->io = io; 57 | p->io_ctx = io_ctx; 58 | p->opts = *opts; 59 | 60 | *_p = p; 61 | 62 | return 0; 63 | } 64 | 65 | static int datagram_proto_add_dst(AVTProtocolCtx *p, AVTAddress *addr) 66 | { 67 | if (!p->io->add_dst) 68 | return AVT_ERROR(ENOTSUP); 69 | return p->io->add_dst(p->io_ctx, addr); 70 | } 71 | 72 | static int datagram_proto_rm_dst(AVTProtocolCtx *p, AVTAddress *addr) 73 | { 74 | if (!p->io->del_dst) 75 | return AVT_ERROR(ENOTSUP); 76 | return p->io->del_dst(p->io_ctx, addr); 77 | } 78 | 79 | static int datagram_proto_send_packet(AVTProtocolCtx *p, AVTPktd *pkt, 80 | int64_t timeout) 81 | { 82 | int64_t ret = p->io->write_pkt(p->io_ctx, pkt, timeout); 83 | if (ret < 0) 84 | return ret; 85 | return 0; 86 | } 87 | 88 | static int datagram_proto_send_seq(AVTProtocolCtx *p, AVTPacketFifo *seq, 89 | int64_t timeout) 90 | { 91 | int64_t ret = p->io->write_vec(p->io_ctx, seq->data, seq->nb, timeout); 92 | if (ret < 0) 93 | return ret; 94 | return 0; 95 | } 96 | 97 | static int datagram_proto_receive(AVTProtocolCtx *s, AVTPacketFifo *fifo, 98 | int64_t timeout) 99 | { 100 | 101 | return 0; 102 | } 103 | 104 | static int datagram_proto_max_pkt_len(AVTProtocolCtx *p, size_t *mtu) 105 | { 106 | size_t tmp; 107 | const size_t udp_hdr_size = 8; 108 | 109 | int ret = p->io->get_max_pkt_len(p->io_ctx, &tmp); 110 | if (ret < 0 || (tmp < (AVT_MIN_HEADER_LEN - udp_hdr_size))) 111 | return ret; 112 | 113 | *mtu = tmp - udp_hdr_size; 114 | 115 | return 0; 116 | } 117 | 118 | static int datagram_proto_flush(AVTProtocolCtx *p, int64_t timeout) 119 | { 120 | if (p->io->flush) 121 | return p->io->flush(p->io_ctx, timeout); 122 | return AVT_ERROR(ENOTSUP); 123 | } 124 | 125 | const AVTProtocol avt_protocol_datagram = { 126 | .name = "datagram", 127 | .type = AVT_PROTOCOL_DATAGRAM, 128 | .init = datagram_proto_init, 129 | .add_dst = datagram_proto_add_dst, 130 | .rm_dst = datagram_proto_rm_dst, 131 | .get_max_pkt_len = datagram_proto_max_pkt_len, 132 | .send_packet = datagram_proto_send_packet, 133 | .send_seq = datagram_proto_send_seq, 134 | .update_packet = NULL, 135 | .receive = datagram_proto_receive, 136 | .seek = NULL, 137 | .flush = datagram_proto_flush, 138 | .close = datagram_proto_close, 139 | }; 140 | -------------------------------------------------------------------------------- /libavtransport/raptor.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include "raptor.h" 30 | 31 | uint64_t pq_calc_raptor_224(uint8_t *data) 32 | { 33 | return 0; 34 | } 35 | 36 | uint64_t pq_calc_raptor_160(uint8_t *data) 37 | { 38 | return 0; 39 | } 40 | 41 | uint8_t *pq_calc_raptor_short(uint8_t *data, uint8_t *code, 42 | size_t data_len, size_t code_len) 43 | { 44 | memset(code, 0, code_len); 45 | return code; 46 | } 47 | -------------------------------------------------------------------------------- /libavtransport/raptor.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef LIBAVTRANSPORT_RAPTOR 28 | #define LIBAVTRANSPORT_RAPTOR 29 | 30 | #include 31 | #include 32 | 33 | uint64_t pq_calc_raptor_224(uint8_t *data); 34 | uint64_t pq_calc_raptor_160(uint8_t *data); 35 | 36 | uint8_t *pq_calc_raptor_short(uint8_t *data, uint8_t *code, 37 | size_t data_len, size_t code_len); 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /libavtransport/reorder.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include "reorder.h" 30 | 31 | int avt_reorder_init(AVTContext *ctx, AVTReorder *r, size_t max_size) 32 | { 33 | return 0; 34 | } 35 | 36 | int avt_reorder_push(AVTReorder *r, AVTPacketFifo *in) 37 | { 38 | int ret; 39 | 40 | for (auto i = 0; i < in->nb; i++) { 41 | AVTPktd *p = &in->data[i]; 42 | AVTReorderStream *rs = &r->st[p->pkt.stream_id]; 43 | if (!rs->nb_groups) { 44 | ret = avt_pkt_merge_seg(r->ctx, &rs->m[0], p); 45 | if (ret == AVT_ERROR(EAGAIN)) { 46 | rs->nb_groups++; 47 | continue; 48 | } else if (ret < 0) { 49 | return ret; 50 | } 51 | 52 | /* ret == 0 -> standalone packet */ 53 | ret = avt_pkt_fifo_push(r->staging, p); 54 | if (ret < 0) 55 | return ret; 56 | } 57 | 58 | for (auto j = 0; j < rs->nb_groups; j++) { 59 | ret = avt_pkt_merge_seg(r->ctx, &rs->m[j], p); 60 | /* Need more packets */ 61 | if (ret == AVT_ERROR(EAGAIN)) 62 | goto next; 63 | else if (ret == AVT_ERROR(EBUSY)) 64 | continue; 65 | else if (ret < 0) 66 | return ret; 67 | 68 | /* ret == 1 -> done */ 69 | } 70 | next: 71 | } 72 | 73 | return 0; 74 | } 75 | 76 | int avt_reorder_pop(AVTReorder *r, AVTPacketFifo *out) 77 | { 78 | return 0; 79 | } 80 | 81 | int avt_reorder_done(AVTPacketFifo *out) 82 | { 83 | return 0; 84 | } 85 | 86 | int avt_reorder_free(AVTReorder *r) 87 | { 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /libavtransport/reorder.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_REORDER_H 28 | #define AVTRANSPORT_REORDER_H 29 | 30 | #include "merger.h" 31 | #include "utils_internal.h" 32 | 33 | /* Maximum number of rejections for a single stream ID */ 34 | #define AVT_REORDER_GROUP_NB 8 35 | 36 | typedef struct AVTReorderStream { 37 | AVTMerger m[AVT_REORDER_GROUP_NB]; 38 | int nb_groups; 39 | } AVTReorderStream; 40 | 41 | typedef struct AVTReorder { 42 | AVTContext *ctx; 43 | 44 | /* One context per stream */ 45 | AVTReorderStream st[UINT16_MAX]; 46 | 47 | /* One context per FEC group */ 48 | AVTReorderStream fg[UINT16_MAX]; 49 | 50 | AVTPacketFifo *staging; /* Staging bucket, next for output */ 51 | 52 | /* If a stream has no outstanding packets, it is considered inactive. 53 | * State is managed by an upper layer */ 54 | uint16_t active_stream_indices[UINT16_MAX]; 55 | uint16_t nb_active_stream_indices; 56 | 57 | /* Available output buckets */ 58 | AVTPacketFifo **avail_buckets; 59 | int nb_avail_buckets; 60 | int nb_alloc_avail_buckets; 61 | 62 | /* All allocated buckets */ 63 | AVTPacketFifo **buckets; 64 | int nb_buckets; 65 | } AVTReorder; 66 | 67 | /* Initialize a reorder buffer with a given max_size which 68 | * is the approximate bound of all packets and their payloads 69 | * contained within. */ 70 | int avt_reorder_init(AVTContext *ctx, AVTReorder *r, size_t max_size); 71 | 72 | /* Push data to the reorder */ 73 | int avt_reorder_push(AVTReorder *r, AVTPacketFifo *in); 74 | 75 | /* Pop data from the reorder */ 76 | int avt_reorder_pop(AVTReorder *r, AVTPacketFifo *out); 77 | 78 | /* Mark a bucket as being available to use again */ 79 | int avt_reorder_done(AVTPacketFifo *out); 80 | 81 | /* Free all data. All buckets become invalid */ 82 | int avt_reorder_free(AVTReorder *r); 83 | 84 | #endif /* AVTRANSPORT_REORDER_H */ 85 | -------------------------------------------------------------------------------- /libavtransport/scheduler.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_CONNECTION_SCHEDULER_H 28 | #define AVTRANSPORT_CONNECTION_SCHEDULER_H 29 | 30 | #include 31 | #include "utils_internal.h" 32 | 33 | typedef struct AVTSchedulerPacketContext { 34 | /* Unlike with a normal packet, this is state, 35 | * and the payload may not match the packet's contents */ 36 | AVTPktd p; 37 | bool hash_sent; 38 | 39 | uint32_t seg_offset; 40 | uint32_t pl_left; 41 | uint32_t seg_hdr_size; 42 | bool present; 43 | 44 | int64_t pts; // in 1ns timebase 45 | int64_t duration; 46 | size_t size; 47 | } AVTSchedulerPacketContext; 48 | 49 | typedef struct AVTSchedulerStream { 50 | /* For timebase keeping */ 51 | union AVTPacketData reg; 52 | 53 | /* Top packet */ 54 | AVTSchedulerPacketContext cur; 55 | /* Rest of packets, in a FIFO */ 56 | AVTPacketFifo fifo; 57 | 58 | /* Stream has had packets without a closure */ 59 | bool active; 60 | uint16_t active_id; 61 | } AVTSchedulerStream; 62 | 63 | typedef struct AVTScheduler { 64 | /* Settings */ 65 | size_t max_pkt_size; 66 | int64_t bandwidth; 67 | int64_t protocol_header; 68 | 69 | /* Scheduling state */ 70 | uint64_t seq; /* Next packet seq */ 71 | AVTPacketFifo *staging; /* Staging bucket, next for output */ 72 | AVTSlidingWinCtx sw; /* Sliding window state */ 73 | int64_t avail; 74 | int64_t time; 75 | 76 | /* Streams state */ 77 | AVTSchedulerStream streams[UINT16_MAX]; 78 | uint16_t active_stream_indices[UINT16_MAX]; 79 | uint16_t nb_active_stream_indices; 80 | struct { 81 | uint16_t overlap[UINT16_MAX]; 82 | uint16_t nb_overlap; 83 | } tmp; 84 | 85 | /* Available output buckets */ 86 | AVTPacketFifo **avail_buckets; 87 | int nb_avail_buckets; 88 | int nb_alloc_avail_buckets; 89 | 90 | /* All allocated buckets */ 91 | AVTPacketFifo **buckets; 92 | int nb_buckets; 93 | } AVTScheduler; 94 | 95 | /* Initialization function. If max_pkt_size changes, everything must 96 | * be torn down and recreated. */ 97 | int avt_scheduler_init(AVTScheduler *s, 98 | size_t max_pkt_size, int64_t bandwidth); 99 | 100 | int avt_scheduler_push(AVTScheduler *s, AVTPktd *p); 101 | 102 | int avt_scheduler_pop(AVTScheduler *s, AVTPacketFifo **seq); 103 | 104 | int avt_scheduler_flush(AVTScheduler *s, AVTPacketFifo **seq); 105 | 106 | int avt_scheduler_done(AVTScheduler *s, AVTPacketFifo *seq); 107 | 108 | void avt_scheduler_free(AVTScheduler *s); 109 | 110 | #endif /* AVTRANSPORT_CONNECTION_SCHEDULER_H */ 111 | -------------------------------------------------------------------------------- /libavtransport/tests/file_io_common.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include 29 | 30 | #include "file_io_common.h" 31 | 32 | #include 33 | #include "packet_common.h" 34 | 35 | static int read_fn(AVTContext *avt, const AVTIO *io, AVTIOCtx *io_ctx, 36 | AVTPktd *test_pkt, int bytes) 37 | { 38 | int64_t ret; 39 | size_t test_buf_size; 40 | uint8_t *test_buf_data; 41 | 42 | AVTBuffer *buf = avt_buffer_alloc(32768); 43 | if (!buf) 44 | return AVT_ERROR(ENOMEM); 45 | 46 | /* Read */ 47 | ret = io->read_input(io_ctx, buf, bytes, INT64_MAX, 0); 48 | 49 | test_buf_data = avt_buffer_get_data(buf, &test_buf_size); 50 | if (test_buf_size != bytes) { 51 | avt_log(avt, AVT_LOG_ERROR, "Too few bytes read at offset %" PRIi64 ": got %" PRIi64 "; wanted %i\n", 52 | ret, test_buf_size, bytes); 53 | avt_buffer_unref(&buf); 54 | return AVT_ERROR(EINVAL); 55 | } 56 | 57 | /* Check data read */ 58 | if (memcmp(test_buf_data, test_pkt->hdr, test_buf_size)) { 59 | avt_log(avt, AVT_LOG_ERROR, "Mismatch between read and written data!\n"); 60 | avt_buffer_unref(&buf); 61 | return AVT_ERROR(EINVAL); 62 | } 63 | avt_buffer_unref(&buf); 64 | 65 | return 0; 66 | } 67 | 68 | int file_io_test(AVTContext *avt, const AVTIO *io, AVTIOCtx *io_ctx) 69 | { 70 | int64_t ret; 71 | 72 | /* Packet data */ 73 | int64_t sum = 0; 74 | AVTPktd test_pkt[16] = { }; 75 | for (int i = 0; i < AVT_ARRAY_ELEMS(test_pkt); i++) { 76 | test_pkt[i].hdr_len = sizeof(test_pkt[i].hdr); 77 | sum += test_pkt[i].hdr_len; 78 | for (int j = 0; j < test_pkt[i].hdr_len; j++) 79 | test_pkt[i].hdr[j] = (i + j) & 0xFF; 80 | } 81 | 82 | ret = io->write_vec(io_ctx, test_pkt, AVT_ARRAY_ELEMS(test_pkt), 83 | INT64_MAX); 84 | if (ret < 0) 85 | goto fail; 86 | 87 | /* Flush test */ 88 | ret = io->flush(io_ctx, INT64_MAX); 89 | if (ret < 0) 90 | goto fail; 91 | 92 | /* Seek test */ 93 | ret = io->seek(io_ctx, 0); 94 | if (ret < 0) 95 | goto fail; 96 | 97 | /* Read test */ 98 | for (int i = 0; i < AVT_ARRAY_ELEMS(test_pkt); i++) { 99 | AVTBuffer *buf = avt_buffer_alloc(32768); 100 | if (!buf) 101 | return AVT_ERROR(ENOMEM); 102 | 103 | int64_t off = io->read_input(io_ctx, buf, test_pkt[i].hdr_len, 104 | INT64_MAX, 0x0); 105 | if (off < 0) { 106 | avt_log(avt, AVT_LOG_ERROR,"Error reading\n"); 107 | avt_buffer_unref(&buf); 108 | ret = off; 109 | goto fail; 110 | } 111 | 112 | size_t len = avt_buffer_get_data_len(buf); 113 | 114 | if (len != test_pkt[i].hdr_len) { 115 | avt_log(avt, AVT_LOG_ERROR,"Too few bytes read: got %" PRIi64 "; wanted %i\n", 116 | len, test_pkt[i].hdr_len); 117 | avt_buffer_unref(&buf); 118 | ret = AVT_ERROR(EINVAL); 119 | goto fail; 120 | } 121 | 122 | size_t test_buf_size; 123 | uint8_t *test_buf_data = avt_buffer_get_data(buf, &test_buf_size); 124 | if (memcmp(test_buf_data, test_pkt[i].hdr, test_buf_size)) { 125 | avt_log(avt, AVT_LOG_ERROR,"Mismatch between read and written data!\n"); 126 | avt_buffer_unref(&buf); 127 | goto fail; 128 | } 129 | 130 | avt_buffer_unref(&buf); 131 | } 132 | 133 | /* Seek test */ 134 | ret = io->seek(io_ctx, 0); 135 | if (ret < 0) 136 | goto fail; 137 | 138 | /* Read 32 bytes */ 139 | ret = read_fn(avt, io, io_ctx, &test_pkt[0], 32); 140 | if (ret < 0) 141 | goto fail; 142 | 143 | /* Seek test */ 144 | ret = io->seek(io_ctx, 0); 145 | if (ret < 0) 146 | goto fail; 147 | 148 | /* Rewrite test */ 149 | for (int i = 0; i < test_pkt[0].hdr_len; i++) 150 | test_pkt[0].hdr[i] = ~test_pkt[0].hdr[i]; 151 | 152 | ret = io->rewrite(io_ctx, &test_pkt[0], 0, 153 | INT64_MAX); 154 | if (ret < 0) 155 | goto fail; 156 | 157 | /* Read again */ 158 | ret = read_fn(avt, io, io_ctx, &test_pkt[0], test_pkt[0].hdr_len); 159 | if (ret < 0) 160 | goto fail; 161 | 162 | ret = 0; 163 | 164 | fail: 165 | return ret; 166 | } 167 | -------------------------------------------------------------------------------- /libavtransport/tests/file_io_common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef TEST_FILE_IO_COMMON_H 28 | #define TEST_FILE_IO_COMMON_H 29 | 30 | #include "io_common.h" 31 | 32 | int file_io_test(AVTContext *avt, const AVTIO *io, AVTIOCtx *io_ctx); 33 | 34 | #endif /* TEST_FILE_IO_COMMON_H */ 35 | -------------------------------------------------------------------------------- /libavtransport/tests/io_fd.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include "io_common.h" 31 | 32 | #include "file_io_common.h" 33 | 34 | extern const AVTIO avt_io_fd_path; 35 | 36 | int main(void) 37 | { 38 | int64_t ret; 39 | 40 | /* Open context */ 41 | AVTContext *avt; 42 | ret = avt_init(&avt, NULL); 43 | if (ret < 0) 44 | return AVT_ERROR(ret); 45 | 46 | /* Open io context */ 47 | const AVTIO *io = &avt_io_fd_path; 48 | AVTIOCtx *io_ctx; 49 | AVTAddress addr = { .path = "io_fd_test.avt" }; 50 | 51 | ret = io->init(avt, &io_ctx, &addr); 52 | if (ret < 0) { 53 | printf("Unable to create test file: %s\n", addr.path); 54 | avt_close(&avt); 55 | return AVT_ERROR(ret); 56 | } 57 | 58 | ret = file_io_test(avt, io, io_ctx); 59 | 60 | if (ret) 61 | io->close(&io_ctx); 62 | else 63 | ret = io->close(&io_ctx); 64 | avt_close(&avt); 65 | return AVT_ERROR(ret); 66 | } 67 | -------------------------------------------------------------------------------- /libavtransport/tests/io_file.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include "io_common.h" 31 | 32 | #include "file_io_common.h" 33 | 34 | /* Always available, on all platforms */ 35 | extern const AVTIO avt_io_file; 36 | 37 | int main(void) 38 | { 39 | int64_t ret; 40 | 41 | /* Open context */ 42 | AVTContext *avt; 43 | ret = avt_init(&avt, NULL); 44 | if (ret < 0) 45 | return AVT_ERROR(ret); 46 | 47 | /* Open io context */ 48 | const AVTIO *io = &avt_io_file; 49 | AVTIOCtx *io_ctx; 50 | AVTAddress addr = { .path = "io_file_test.avt" }; 51 | 52 | ret = io->init(avt, &io_ctx, &addr); 53 | if (ret < 0) { 54 | printf("Unable to create test file: %s\n", addr.path); 55 | avt_close(&avt); 56 | return AVT_ERROR(ret); 57 | } 58 | 59 | ret = file_io_test(avt, io, io_ctx); 60 | 61 | if (ret) 62 | io->close(&io_ctx); 63 | else 64 | ret = io->close(&io_ctx); 65 | avt_close(&avt); 66 | return AVT_ERROR(ret); 67 | } 68 | -------------------------------------------------------------------------------- /libavtransport/tests/io_mmap.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include "io_common.h" 31 | 32 | #include "file_io_common.h" 33 | 34 | extern const AVTIO avt_io_mmap_path; 35 | 36 | int main(void) 37 | { 38 | int64_t ret; 39 | 40 | /* Open context */ 41 | AVTContext *avt; 42 | ret = avt_init(&avt, NULL); 43 | if (ret < 0) 44 | return AVT_ERROR(ret); 45 | 46 | /* Open io context */ 47 | const AVTIO *io = &avt_io_mmap_path; 48 | AVTIOCtx *io_ctx; 49 | AVTAddress addr = { .path = "io_mmap_test.avt" }; 50 | 51 | ret = io->init(avt, &io_ctx, &addr); 52 | if (ret < 0) { 53 | printf("Unable to create test file: %s\n", addr.path); 54 | avt_close(&avt); 55 | return AVT_ERROR(ret); 56 | } 57 | 58 | ret = file_io_test(avt, io, io_ctx); 59 | 60 | if (ret) 61 | io->close(&io_ctx); 62 | else 63 | ret = io->close(&io_ctx); 64 | avt_close(&avt); 65 | return AVT_ERROR(ret); 66 | } 67 | -------------------------------------------------------------------------------- /libavtransport/tests/io_udp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include "net_io_common.h" 28 | 29 | extern const AVTIO avt_io_udp; 30 | 31 | int main(void) 32 | { 33 | NetTestContext ntc; 34 | int ret = net_io_init(&ntc, &avt_io_udp, "udp://[::1]"); 35 | if (ret < 0) 36 | return AVT_ERROR(ret); 37 | 38 | ret = net_io_test(&ntc); 39 | 40 | net_io_free(&ntc); 41 | return AVT_ERROR(ret); 42 | } 43 | -------------------------------------------------------------------------------- /libavtransport/tests/io_udp_lite.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include "net_io_common.h" 28 | 29 | extern const AVTIO avt_io_udp_lite; 30 | 31 | int main(void) 32 | { 33 | NetTestContext ntc; 34 | int ret = net_io_init(&ntc, &avt_io_udp_lite, "udplite://[::1]"); 35 | if (ret < 0) 36 | return AVT_ERROR(ret); 37 | 38 | ret = net_io_test(&ntc); 39 | 40 | net_io_free(&ntc); 41 | return AVT_ERROR(ret); 42 | } 43 | -------------------------------------------------------------------------------- /libavtransport/tests/io_unix.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include 29 | 30 | #include "net_io_common.h" 31 | 32 | extern const AVTIO avt_io_unix; 33 | 34 | int main(void) 35 | { 36 | NetTestContext ntc; 37 | int ret = net_io_init(&ntc, &avt_io_unix, "socket://test.sock"); 38 | if (ret < 0) 39 | return AVT_ERROR(ret); 40 | 41 | ret = net_io_test(&ntc); 42 | 43 | net_io_free(&ntc); 44 | return AVT_ERROR(ret); 45 | } 46 | -------------------------------------------------------------------------------- /libavtransport/tests/ldpc_encode.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include "ldpc_tables.h" 32 | 33 | #define DATA_LEN 1024 34 | #define PARITY_LEN 1024 35 | 36 | int main(void) 37 | { 38 | uint8_t data[DATA_LEN + PARITY_LEN]; 39 | 40 | /* Get random data */ 41 | srand(time(NULL)); 42 | for (int i = 0; i < DATA_LEN; i++) 43 | data[i] = rand() & 0xFF; 44 | 45 | /* Make a matrix filled with 1s. 46 | * Encoding with this is equivalent to summing all bits in GF(2). */ 47 | uint64_t *parity_matrix = malloc(((DATA_LEN + PARITY_LEN) * PARITY_LEN) * 8); 48 | for (int i = 0; i < ((DATA_LEN + PARITY_LEN) * PARITY_LEN); i++) 49 | parity_matrix[i] = UINT64_MAX; 50 | 51 | uint8_t parity = 0; 52 | for (int i = 0; i < DATA_LEN; i++) { 53 | uint8_t p = data[i]; 54 | 55 | parity ^= (p >> 7) & 1; 56 | parity ^= (p >> 6) & 1; 57 | parity ^= (p >> 5) & 1; 58 | parity ^= (p >> 4) & 1; 59 | parity ^= (p >> 3) & 1; 60 | parity ^= (p >> 2) & 1; 61 | parity ^= (p >> 1) & 1; 62 | parity ^= (p >> 0) & 1; 63 | } 64 | parity *= 0xFF; 65 | 66 | ldpc_encode(data, parity_matrix, DATA_LEN * 8, PARITY_LEN * 8); 67 | free(parity_matrix); 68 | 69 | for (int i = 0; i < PARITY_LEN; i++) { 70 | if (data[DATA_LEN + i] != parity) { 71 | printf("Mismatch, 0x%x vs 0x%x\n", data[DATA_LEN + i], parity); 72 | return 1; 73 | } 74 | } 75 | 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /libavtransport/tests/meson.build: -------------------------------------------------------------------------------- 1 | # Copyright © 2024, Lynne 2 | # All rights reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions are met: 6 | # 7 | # 1. Redistributions of source code must retain the above copyright notice, this 8 | # list of conditions and the following disclaimer. 9 | # 10 | # 2. Redistributions in binary form must reproduce the above copyright notice, 11 | # this list of conditions and the following disclaimer in the documentation 12 | # and/or other materials provided with the distribution. 13 | # 14 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | 25 | ## Address tests 26 | ## ============= 27 | address_test = executable('address', 28 | sources : [ 'address.c' ], 29 | include_directories : [ '../' ], 30 | objects : [ avtransport_lib.extract_objects([ 'address.c' ]) ], 31 | dependencies : [ avtransport_dep ], 32 | ) 33 | test('Address parsing', address_test) 34 | 35 | ## LDPC tests 36 | ## ========== 37 | ldpc_encode_test = executable('ldpc_encode', 38 | sources : [ 'ldpc_encode.c' ], 39 | include_directories : [ '../' ], 40 | objects : [ avtransport_lib.extract_objects([ avtransport_spec_pkt_headers ]) ], 41 | dependencies : [ avtransport_dep ], 42 | ) 43 | test('LDPC encoding', ldpc_encode_test) 44 | 45 | ## Merger tests 46 | ## ============ 47 | merger_test = executable('merger', 48 | sources : [ 'merger.c' ], 49 | include_directories : [ '../' ], 50 | objects : [ avtransport_lib.extract_objects([ avtransport_spec_pkt_headers, 'ldpc_encode.c', 'merger.c', 'buffer.c' ]) ], 51 | dependencies : [ avtransport_dep ], 52 | ) 53 | test('Packet merging', merger_test) 54 | 55 | ## Packet encode/decode primitives tests 56 | ## ===================================== 57 | packet_encode_decode_test = executable('packet_encode_decode', 58 | sources : [ 'packet_encode_decode.c' ], 59 | include_directories : [ '../' ], 60 | objects : [ avtransport_lib.extract_objects([ avtransport_spec_pkt_headers, 'ldpc_encode.c' ]) ], 61 | dependencies : [ avtransport_dep ], 62 | ) 63 | test('Packet encode/decode', packet_encode_decode_test) 64 | 65 | ## I/O tests 66 | ## ========= 67 | io_file_test = executable('io_file', 68 | sources : [ 'file_io_common.c', 'io_file.c' ], 69 | include_directories : [ '../' ], 70 | objects : [ avtransport_lib.extract_objects([ 'buffer.c', 'io_file.c' ]) ], 71 | dependencies : [ avtransport_dep ], 72 | ) 73 | test('File I/O', io_file_test) 74 | 75 | io_unix_test = executable('io_unix', 76 | sources : [ 'net_io_common.c', 'io_unix.c' ], 77 | include_directories : [ '../' ], 78 | objects : [ avtransport_lib.extract_objects([ 'buffer.c', 'io_unix.c', 'io_socket_common.c', 'address.c' ]) ], 79 | dependencies : [ avtransport_dep ], 80 | ) 81 | test('UNIX I/O', io_unix_test) 82 | 83 | if host_machine.system() != 'windows' 84 | io_fd_test = executable('io_fd', 85 | sources : [ 'file_io_common.c', 'io_fd.c' ], 86 | include_directories : [ '../' ], 87 | objects : [ avtransport_lib.extract_objects([ 'buffer.c', 'io_fd.c' ]) ], 88 | dependencies : [ avtransport_dep ], 89 | ) 90 | test('FD I/O', io_fd_test) 91 | 92 | io_mmap_test = executable('io_mmap', 93 | sources : [ 'file_io_common.c', 'io_mmap.c' ], 94 | include_directories : [ '../' ], 95 | objects : [ avtransport_lib.extract_objects([ 'buffer.c', 'io_mmap.c' ]) ], 96 | dependencies : [ avtransport_dep ], 97 | ) 98 | test('mmap I/O', io_mmap_test) 99 | endif 100 | 101 | io_udp_test = executable('io_udp', 102 | sources : [ 'net_io_common.c', 'io_udp.c' ], 103 | include_directories : [ '../' ], 104 | objects : [ avtransport_lib.extract_objects([ 'address.c', 'io_udp.c', 'io_socket_common.c', 'buffer.c']) ], 105 | dependencies : [ avtransport_dep ], 106 | ) 107 | test('UDP I/O', io_udp_test) 108 | 109 | io_udp_lite_test = executable('io_udp_lite', 110 | sources : [ 'net_io_common.c', 'io_udp_lite.c' ], 111 | include_directories : [ '../' ], 112 | objects : [ avtransport_lib.extract_objects([ 'address.c', 'io_udp.c', 'io_socket_common.c', 'buffer.c']) ], 113 | dependencies : [ avtransport_dep ], 114 | ) 115 | test('UDP-Lite I/O', io_udp_lite_test) 116 | 117 | ## Protocol tests 118 | ## ============== 119 | if openssl_dep.found() 120 | protocol_quic_test = executable('protocol_quic', 121 | sources : [ 'proto_quic.c' ], 122 | include_directories : [ '../' ], 123 | objects : [ avtransport_lib.extract_objects([ 'protocol_quic.c', 'io_dcb.c', 124 | 'address.c', 'buffer.c' ]) ], 125 | dependencies : [ avtransport_dep, openssl_dep ], 126 | ) 127 | test('QUIC protocol', protocol_quic_test) 128 | endif 129 | -------------------------------------------------------------------------------- /libavtransport/tests/net_io_common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef TEST_NET_COMMON_H 28 | #define TEST_NET_COMMON_H 29 | 30 | #include "io_common.h" 31 | 32 | typedef struct NetTestContext { 33 | AVTContext *avt; 34 | 35 | const AVTIO *io; 36 | AVTIOCtx *ioctx_sender; 37 | AVTIOCtx *ioctx_listener; 38 | } NetTestContext; 39 | 40 | int net_io_init(NetTestContext *ntc, const AVTIO *io, const char *url); 41 | int net_io_test(NetTestContext *ntc); 42 | int net_io_free(NetTestContext *ntc); 43 | 44 | #endif /* TEST_NET_COMMON_H */ 45 | -------------------------------------------------------------------------------- /libavtransport/tests/packet_encode_decode.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include "utils_packet.h" 32 | #include "packet_decode.h" 33 | 34 | int main(void) 35 | { 36 | AVTPktd in = { }; 37 | AVTPktd out = { }; 38 | size_t buf_size; 39 | AVTBuffer *buf = avt_buffer_alloc(AVT_MAX_HEADER_LEN); 40 | uint8_t *buf_data = avt_buffer_get_data(buf, &buf_size); 41 | 42 | { 43 | in.hdr_off = 0; 44 | in.pkt = AVT_SESSION_START_HDR(); 45 | avt_packet_encode_header(&in); 46 | memcpy(buf_data, in.hdr, in.hdr_len); 47 | 48 | AVTBytestream bs = avt_bs_init(buf_data, buf_size); 49 | avt_decode_session_start(&bs, &out.pkt.session_start); 50 | 51 | if (memcmp(&in.pkt, &out.pkt, in.hdr_len)) 52 | return EINVAL; 53 | } 54 | 55 | { 56 | in.hdr_off = 0; 57 | in.pkt = AVT_STREAM_REGISTRATION_HDR(); 58 | avt_packet_encode_header(&in); 59 | memcpy(buf_data, in.hdr, in.hdr_len); 60 | 61 | AVTBytestream bs = avt_bs_init(buf_data, buf_size); 62 | avt_decode_stream_registration(&bs, &out.pkt.stream_registration); 63 | 64 | if (memcmp(&in.pkt, &out.pkt, in.hdr_len)) 65 | return EINVAL; 66 | } 67 | 68 | { 69 | in.hdr_off = 0; 70 | in.pkt = AVT_VIDEO_INFO_HDR(); 71 | avt_packet_encode_header(&in); 72 | memcpy(buf_data, in.hdr, in.hdr_len); 73 | 74 | AVTBytestream bs = avt_bs_init(buf_data, buf_size); 75 | avt_decode_video_info(&bs, &out.pkt.video_info); 76 | 77 | if (memcmp(&in.pkt, &out.pkt, in.hdr_len)) 78 | return EINVAL; 79 | } 80 | 81 | avt_buffer_unref(&buf); 82 | 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /libavtransport/tests/proto_quic.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | #include "address.h" 34 | #include "protocol_common.h" 35 | #include "io_common.h" 36 | 37 | extern const AVTIO avt_io_dcb; 38 | extern const AVTProtocol avt_protocol_quic; 39 | 40 | static void add_x509_ext(X509 *cert, X509V3_CTX *ctx, int ext_nid, const char *value) 41 | { 42 | X509_EXTENSION *ex = X509V3_EXT_conf_nid(NULL, ctx, ext_nid, value); 43 | X509_add_ext(cert, ex, -1); 44 | X509_EXTENSION_free(ex); 45 | } 46 | 47 | static int create_cert(AVTContext *avt, const char *certfile, const char *keyfile) 48 | { 49 | EVP_PKEY *p_key = NULL; 50 | EVP_PKEY_CTX *p_key_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_ED25519, NULL); 51 | EVP_PKEY_keygen_init(p_key_ctx); 52 | 53 | if (EVP_PKEY_keygen(p_key_ctx, &p_key) <= 0) { 54 | avt_log(avt, AVT_LOG_ERROR, "Error generating key!\n"); 55 | return AVT_ERROR(EINVAL); 56 | } 57 | 58 | /* Write the private key file */ 59 | FILE *f_key = fopen(keyfile, "wb"); 60 | int ret = PEM_write_PrivateKey(f_key, p_key, NULL, NULL, 0, NULL, NULL); 61 | fclose(f_key); 62 | if (ret <= 0) { 63 | avt_log(avt, AVT_LOG_ERROR, "Error writing private key!\n"); 64 | return AVT_ERROR(EINVAL); 65 | } 66 | 67 | /* Create the certificate object */ 68 | X509 *cert = X509_new(); 69 | X509_set_version(cert, 3); 70 | 71 | ASN1_INTEGER_set(X509_get_serialNumber(cert), 1); 72 | X509_gmtime_adj(X509_get_notBefore(cert), 0); 73 | X509_gmtime_adj(X509_get_notAfter(cert), 60L); /* 60 seconds */ 74 | X509_set_pubkey(cert, p_key); 75 | 76 | X509_NAME *cert_name = X509_get_subject_name(cert); 77 | X509_NAME_add_entry_by_txt(cert_name, "CN", MBSTRING_ASC, (unsigned char *)"localhost", -1, -1, 0); 78 | X509_NAME_add_entry_by_txt(cert_name, "C", MBSTRING_ASC, (unsigned char *)"US", -1, -1, 0); 79 | X509_NAME_add_entry_by_txt(cert_name, "O", MBSTRING_ASC, (unsigned char *)"HANMA", -1, -1, 0); 80 | X509_NAME_add_entry_by_txt(cert_name, "OU", MBSTRING_ASC, (unsigned char *)"KANMA", -1, -1, 0); 81 | X509_NAME_add_entry_by_txt(cert_name, "ST", MBSTRING_ASC, (unsigned char *)"GOOOS", -1, -1, 0); 82 | X509_NAME_add_entry_by_txt(cert_name, "SN", MBSTRING_ASC, (unsigned char *)"DESANE", -1, -1, 0); 83 | X509_NAME_add_entry_by_txt(cert_name, "GN", MBSTRING_ASC, (unsigned char *)"HAAH", -1, -1, 0); 84 | X509_set_issuer_name(cert, cert_name); 85 | 86 | X509V3_CTX v3ctx; 87 | X509V3_set_ctx_nodb(&v3ctx); 88 | X509V3_set_ctx(&v3ctx, cert, cert, NULL, NULL, 0); 89 | 90 | add_x509_ext(cert, &v3ctx, NID_authority_key_identifier, "keyid"); 91 | add_x509_ext(cert, &v3ctx, NID_basic_constraints, "critical,CA:TRUE"); 92 | 93 | if (X509_sign(cert, p_key, NULL /* No digest for Ed25519 keys */) <= 0) { 94 | avt_log(avt, AVT_LOG_ERROR, "Error signing X509 certificate with key!\n"); 95 | return AVT_ERROR(EINVAL); 96 | } 97 | 98 | /* Write the certificate file */ 99 | FILE *f_cert = fopen(certfile, "wb"); 100 | PEM_write_X509(f_cert, cert); 101 | fclose(f_cert); 102 | 103 | EVP_PKEY_free(p_key); 104 | EVP_PKEY_CTX_free(p_key_ctx); 105 | X509_free(cert); 106 | 107 | return 0; 108 | } 109 | 110 | int main(void) 111 | { 112 | int ret; 113 | AVTContext *avt; 114 | AVTAddress addr_server; 115 | AVTAddress addr_client; 116 | 117 | const AVTIO *io = &avt_io_dcb; 118 | AVTIOCtx *io_server_ctx = NULL; 119 | AVTIOCtx *io_client_ctx = NULL; 120 | 121 | const AVTProtocol *p = &avt_protocol_quic; 122 | AVTProtocolCtx *p_server_ctx = NULL; 123 | AVTProtocolCtx *p_client_ctx = NULL; 124 | 125 | ret = avt_init(&avt, NULL); 126 | if (ret < 0) 127 | return AVT_ERROR(ret); 128 | 129 | ret = create_cert(avt, "test_cert.pem", "test_key.pem"); 130 | if (ret < 0) 131 | goto end; 132 | 133 | { /* Server */ 134 | ret = avt_addr_from_url(avt, &addr_server, 1, "quic://localhost/#certfile=test_cert.pem&keyfile=test_key.pem"); 135 | if (ret < 0) 136 | goto end; 137 | ret = io->init(avt, &io_server_ctx, &addr_server); 138 | if (ret < 0) 139 | goto end; 140 | ret = p->init(avt, &p_server_ctx, &addr_server, io, io_server_ctx, NULL); 141 | if (ret < 0) 142 | goto end; 143 | } 144 | 145 | { /* Client */ 146 | ret = avt_addr_from_url(avt, &addr_client, 0, "quic://localhost/#certfile=test_cert.pem"); 147 | if (ret < 0) 148 | goto end; 149 | ret = io->init(avt, &io_client_ctx, &addr_client); 150 | if (ret < 0) 151 | goto end; 152 | ret = p->init(avt, &p_client_ctx, &addr_client, io, io_client_ctx, NULL); 153 | if (ret < 0) 154 | goto end; 155 | } 156 | 157 | 158 | 159 | 160 | end: 161 | if (p_client_ctx) 162 | p->close(&p_client_ctx); 163 | if (io_client_ctx) 164 | io->close(&io_client_ctx); 165 | 166 | if (p_server_ctx) 167 | p->close(&p_server_ctx); 168 | if (io_server_ctx) 169 | io->close(&io_server_ctx); 170 | 171 | avt_addr_free(&addr_client); 172 | avt_addr_free(&addr_server); 173 | avt_close(&avt); 174 | 175 | return 0; 176 | } 177 | -------------------------------------------------------------------------------- /libavtransport/tests/udp_common.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | -------------------------------------------------------------------------------- /libavtransport/utils_internal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVTRANSPORT_UTILS_INTERNAL_H 28 | #define AVTRANSPORT_UTILS_INTERNAL_H 29 | 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #include 37 | 38 | #include 39 | #include 40 | #include "buffer.h" 41 | #include "packet_common.h" 42 | 43 | #include "config.h" 44 | #include "attributes.h" 45 | 46 | #define AVT_SWAP(a, b) \ 47 | do { \ 48 | typeof(a) swaptmp = (a); \ 49 | a = b; \ 50 | b = swaptmp; \ 51 | } while (0) 52 | 53 | int64_t avt_get_time_ns(void); 54 | 55 | static inline int avt_tolower(int c) 56 | { 57 | if (c >= 'A' && c <= 'Z') 58 | return c ^ 0x20; 59 | return c; 60 | } 61 | 62 | static inline int avt_ascii_to_int(int c) 63 | { 64 | c = avt_tolower(c); 65 | if (c >= 'a' && c <= 'f') 66 | return c - 'a' + 10; 67 | else if (c >= '0' && c <= '9') 68 | return c - '0'; 69 | return -1; 70 | } 71 | 72 | #define avt_hamming_dist(a, b) \ 73 | stdc_count_ones((a) ^ (b)) 74 | 75 | /* Zero (usually) alloc FIFO. Payload is ref'd, and leaves with a ref. */ 76 | typedef struct AVTPacketFifo { 77 | AVTPktd *data; 78 | unsigned int nb; 79 | unsigned int alloc; 80 | } AVTPacketFifo; 81 | 82 | /* Push a packet to the FIFO */ 83 | int avt_pkt_fifo_push_d(AVTPacketFifo *fifo, AVTPktd *p); 84 | int avt_pkt_fifo_push_p(AVTPacketFifo *fifo, 85 | union AVTPacketData pkt, AVTBuffer *pl); 86 | #define avt_pkt_fifo_push(f, x, ...) \ 87 | _Generic((x), \ 88 | AVTPktd *: avt_pkt_fifo_push_d, \ 89 | union AVTPacketData: avt_pkt_fifo_push_p \ 90 | ) (f, x __VA_OPT__(,) __VA_ARGS__) 91 | 92 | /* Push a referenced packet to the FIFO (skips a quick ref, takes ownership) */ 93 | int avt_pkt_fifo_push_refd_d(AVTPacketFifo *fifo, AVTPktd *p); 94 | int avt_pkt_fifo_push_refd_p(AVTPacketFifo *fifo, 95 | union AVTPacketData pkt, AVTBuffer *pl); 96 | #define avt_pkt_fifo_push_refd(f, x, ...) \ 97 | _Generic((x), \ 98 | AVTPktd *: avt_pkt_fifo_push_refd_d, \ 99 | union AVTPacketData: avt_pkt_fifo_push_refd_p \ 100 | ) (f, x __VA_OPT__(,) __VA_ARGS__) 101 | 102 | /* Returns a blank AVTPktd or NULL on error. 103 | * pl is an optional parameter which is quick_ref'd 104 | * within the provided parameters */ 105 | AVTPktd *avt_pkt_fifo_push_new(AVTPacketFifo *fifo, AVTBuffer *pl, 106 | ptrdiff_t offset, size_t len); 107 | 108 | /* Pop a packet from the FIFO. quick_ref'd into pl */ 109 | int avt_pkt_fifo_pop_d(AVTPacketFifo *fifo, AVTPktd *p); 110 | int avt_pkt_fifo_pop_p(AVTPacketFifo *fifo, 111 | union AVTPacketData *pkt, AVTBuffer *pl); 112 | #define avt_pkt_fifo_pop(f, x, ...) \ 113 | _Generic((x), \ 114 | AVTPktd *: avt_pkt_fifo_pop_d, \ 115 | union AVTPacketData *: avt_pkt_fifo_pop_p \ 116 | ) (f, x __VA_OPT__(,) __VA_ARGS__) 117 | 118 | /* Peek a packet from the FIFO. quick_ref'd into pl */ 119 | int avt_pkt_fifo_peek(const AVTPacketFifo *fifo, 120 | union AVTPacketData *pkt, AVTBuffer *pl); 121 | 122 | /* Copy and ref all packets from src to dst */ 123 | int avt_pkt_fifo_copy(AVTPacketFifo *dst, const AVTPacketFifo *src); 124 | 125 | /* Move all packets from src to dst */ 126 | int avt_pkt_fifo_move(AVTPacketFifo *dst, AVTPacketFifo *src); 127 | 128 | /* Drop packets from the tail. 129 | * If nb_pkts == 0, the ceiling is a size limit. */ 130 | int avt_pkt_fifo_drop(AVTPacketFifo *fifo, 131 | unsigned nb_pkts, size_t ceiling); 132 | 133 | /* Get the current size of the FIFO */ 134 | size_t avt_pkt_fifo_size(AVTPacketFifo *fifo); 135 | 136 | /* Clear all packets in the fifo */ 137 | void avt_pkt_fifo_clear(AVTPacketFifo *fifo); 138 | 139 | /* Free all resources in/for a fifo */ 140 | void avt_pkt_fifo_free(AVTPacketFifo *fifo); 141 | 142 | /* Sliding window */ 143 | #define AVT_SLIDING_WINDOW_MAX_ENTRIES (1024*512) 144 | 145 | typedef struct AVTSlidingWinCtx { 146 | int num_entries; 147 | PACKED(struct AVTSlidingWinEntry { 148 | int64_t val; 149 | int64_t ts; 150 | AVTRational tb; 151 | }) entries[AVT_SLIDING_WINDOW_MAX_ENTRIES]; 152 | } AVTSlidingWinCtx; 153 | 154 | /* 155 | * Run a sliding window calculation and produce an output. 156 | * val is the value for which to average or sum 157 | * ts is the timestamp for the value 158 | * tb is the timebase for the timestamp of the value, as well as the period 159 | * period is the value in tb units over which to average or sum up 160 | * do_avg specifies that instead of summing, an average is to be performed 161 | */ 162 | int64_t avt_sliding_win(AVTSlidingWinCtx *ctx, int64_t val, int64_t ts, 163 | AVTRational tb, int64_t period, bool do_avg); 164 | 165 | #endif /* AVTRANSPORT_UTILS_INTERNAL_H */ 166 | -------------------------------------------------------------------------------- /libavtransport/version.c.in: -------------------------------------------------------------------------------- 1 | const char *vcstag = "@VCS_TAG@"; 2 | -------------------------------------------------------------------------------- /libavtransport/version.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | extern const char *vcstag; 28 | -------------------------------------------------------------------------------- /libavtransport/x86/ldpc_encode.asm: -------------------------------------------------------------------------------- 1 | ;********************************************************************************* 2 | ;* Copyright © 2024, Lynne 3 | ;* All rights reserved. 4 | ;* 5 | ;* Redistribution and use in source and binary forms, with or without 6 | ;* modification, are permitted provided that the following conditions are met: 7 | ;* 8 | ;* 1. Redistributions of source code must retain the above copyright notice, this 9 | ;* list of conditions and the following disclaimer. 10 | ;* 11 | ;* 2. Redistributions in binary form must reproduce the above copyright notice, 12 | ;* this list of conditions and the following disclaimer in the documentation 13 | ;* and/or other materials provided with the distribution. 14 | ;* 15 | ;* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | ;* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | ;* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | ;* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | ;* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | ;* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | ;* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | ;* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | ;* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | ;* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | ;********************************************************************************* 26 | 27 | %include "x86/config.asm" 28 | %include "x86/x86inc.asm" 29 | 30 | SECTION_RODATA 31 | 32 | SECTION .text 33 | -------------------------------------------------------------------------------- /libavtransport/x86/meson.build: -------------------------------------------------------------------------------- 1 | conf_x86 = conf 2 | conf_x86.set10('ARCH_X86', host_machine.cpu_family().startswith('x86')) 3 | conf_x86.set10('ARCH_X86_64', host_machine.cpu_family() == 'x86_64') 4 | conf_x86.set10('ARCH_X86_32', host_machine.cpu_family() == 'x86') 5 | conf_x86.set('private_prefix', 'avt') 6 | 7 | # Convert SSE asm into (128-bit) AVX when compiler flags are set to use AVX instructions 8 | conf_x86.set10('FORCE_VEX_ENCODING', cc.get_define('__AVX__').strip() != '') 9 | 10 | nasm_config = configure_file( 11 | output: 'config.asm', 12 | configuration: conf_x86, 13 | output_format: 'nasm', 14 | ) 15 | 16 | #sources = sources.without([ 17 | # 'ldpc_encode.c' 18 | #]) 19 | sources += [ 20 | 'x86/ldpc_encode.asm', 21 | ] 22 | -------------------------------------------------------------------------------- /meson_options.txt: -------------------------------------------------------------------------------- 1 | option('assert', 2 | type: 'integer', 3 | min : -1, 4 | max : 2, 5 | value : -1, 6 | description: 'Assert level with which to build the library (-1: depending on the build type)' 7 | ) 8 | 9 | option('enable_asm', 10 | type: 'feature', 11 | value: 'enabled', 12 | description: 'Build libavtransport with assembly optimizations' 13 | ) 14 | 15 | option('tools', 16 | type: 'feature', 17 | value: 'auto', 18 | description: 'Build libavtransport CLI tools' 19 | ) 20 | 21 | option('tests', 22 | type: 'feature', 23 | value: 'auto', 24 | description: 'Build tests' 25 | ) 26 | 27 | option('protocols', 28 | type : 'array', 29 | value : ['all'], 30 | choices : [ 'all' ], 31 | description : 'List of protocol types to build with.' 32 | ) 33 | 34 | option('io', 35 | type : 'array', 36 | value : ['all'], 37 | choices : [ 'all' ], 38 | description : 'List of inputs/outputs to build with.' 39 | ) 40 | 41 | option('ffmpeg', 42 | type: 'feature', 43 | value: 'auto', 44 | description: 'Enable demuxing and muxing of non-AVT files via FFmpeg (in CLI tools only)' 45 | ) 46 | 47 | option('fuzzing_engine', 48 | type: 'combo', 49 | choices : ['none', 'libfuzzer', 'oss-fuzz'], 50 | value: 'none', 51 | description: 'Select the fuzzing engine' 52 | ) 53 | 54 | option('fuzzer_ldflags', 55 | type: 'string', 56 | description: 'Extra LDFLAGS used during linking of fuzzing binaries' 57 | ) 58 | -------------------------------------------------------------------------------- /tools/README.md: -------------------------------------------------------------------------------- 1 | AVTransport tools 2 | ================= 3 | 4 | avcat 5 | ----- 6 | 7 | avcat is a swiss army-knife program, similar to [mkvmerge](https://github.com/skyunix/mkvtoolnix). 8 | It takes one, or many, inputs, and a single output. 9 | 10 | If ffmpeg support has been enabled during compilation, either inputs or outputs can be 11 | anything libavformat supports (mkv, mp4, MPEG-TS). 12 | 13 | avcat can combine or append inputs, extract streams, add metadata, reconstruct timestamps, 14 | fix errors, and verify integrity. 15 | 16 | 17 | avtdump 18 | ------- 19 | 20 | avtdump is a utility to inspect AVTransport streams and files, logging any issues, 21 | stream changes, packet sizes and so on. It can also provide such information as a JSON 22 | stream. 23 | -------------------------------------------------------------------------------- /tools/avcat.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include "config.h" 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #include 34 | 35 | #ifdef HAVE_FFMPEG 36 | #include 37 | #endif 38 | 39 | #include "avcat/io.h" 40 | 41 | #define GEN_OPT_MAX_ARR 16 42 | #define GEN_OPT_LOG avt_log 43 | #define GEN_OPT_LOG_INFO AVT_LOG_INFO 44 | #define GEN_OPT_LOG_ERROR AVT_LOG_ERROR 45 | #define GEN_OPT_RATIONAL AVTRational 46 | #define GEN_OPT_HELPSTRING "avcat " PROJECT_VERSION_STRING 47 | #include "genopt.h" 48 | 49 | #define MAX_INPUTS 4 50 | 51 | static volatile sig_atomic_t signal_terminate; 52 | static void on_quit_signal(const int signo) 53 | { 54 | signal_terminate = 1; 55 | } 56 | 57 | int main(int argc, char **argv) 58 | { 59 | int ret; 60 | AVTContext *avt; 61 | AVTContextOptions ctx_opts = { 62 | .producer_name = "avcat", 63 | .producer_ver = { AVTRANSPORT_VERSION_MAJOR, 64 | AVTRANSPORT_VERSION_MICRO, 65 | AVTRANSPORT_VERSION_MINOR }, 66 | }; 67 | 68 | ret = avt_init(&avt, &ctx_opts); 69 | if (ret < 0) 70 | return AVT_ERROR(ret); 71 | 72 | GEN_OPT_INIT(opts_list, 16); 73 | GEN_OPT_SEC(opts_list, "Input/Output"); 74 | GEN_OPT_ARR(opts_list, char *, input, "i", 1, MAX_INPUTS, 0, 0, "Input (files or URLs)"); 75 | GEN_OPT_ONE(opts_list, char *, output, "o", 1, 1, NULL, 0, 0, "Destination (file or URL)"); 76 | GEN_OPT_SEC(opts_list, "Miscellaneous"); 77 | GEN_OPT_ONE(opts_list, bool , unround, "u", 0, 0, false, 0, 0, "Unround timestamps (for remuxing from Matroska)"); 78 | GEN_OPT_ONE(opts_list, char *, mirror, "m", 1, 1, NULL, 0, 0, "Mirror input and output to a file for monitoring and caching"); 79 | 80 | if ((ret = GEN_OPT_PARSE(opts_list, argc, argv)) < 0) 81 | goto end; 82 | 83 | if (!input[0]) { 84 | avt_log(NULL, AVT_LOG_ERROR, "At least one input required!\n"); 85 | return EINVAL; 86 | } 87 | 88 | #ifdef _WIN32 89 | if (signal(SIGINT, on_quit_signal) < 0 || 90 | signal(SIGTERM, on_quit_signal) < 0) 91 | #else 92 | static const struct sigaction sa = { 93 | .sa_handler = on_quit_signal, 94 | .sa_flags = SA_RESETHAND, 95 | }; 96 | if (sigaction(SIGINT, &sa, NULL) < 0 || 97 | sigaction(SIGTERM, &sa, NULL) < 0) 98 | #endif 99 | avt_log(NULL, AVT_LOG_ERROR, "Can't init signal handler!\n"); 100 | 101 | /* Create inputs */ 102 | IOContext in[MAX_INPUTS] = { 0 }; 103 | for (int i = 0; input[i]; i++) { 104 | ret = io_open(&in[i], avt, input[i], 0); 105 | if (ret < 0) 106 | goto end; 107 | } 108 | 109 | IOContext out = { 0 }; 110 | if (output) { 111 | ret = io_open(&out, avt, output, 1); 112 | if (ret < 0) 113 | goto end; 114 | } 115 | 116 | switch (in[0].mode) { 117 | case IO_AVT: { 118 | 119 | break; 120 | } 121 | case IO_RAW: { 122 | fseek(in[0].raw, 0L, SEEK_END); 123 | size_t len = ftell(in[0].raw); 124 | fseek(in[0].raw, 0L, SEEK_SET); 125 | uint8_t *data = malloc(len); 126 | if (!data) 127 | return AVT_ERROR(ENOMEM); 128 | 129 | fread(data, len, 1, in[0].raw); 130 | 131 | AVTBuffer *buf = avt_buffer_create(data, len, NULL, NULL); 132 | AVTPacket pkt = { 133 | .type = AVT_FRAME_TYPE_KEY, 134 | .data = buf, 135 | .pts = 0, 136 | }; 137 | 138 | avt_send_stream_data(out.st, &pkt); 139 | 140 | break; 141 | } 142 | case IO_LAVF: { 143 | #ifdef HAVE_FFMPEG 144 | AVPacket *out_packet = av_packet_alloc(); 145 | av_read_frame(in[0].avf, out_packet); 146 | 147 | /* Todo: import */ 148 | AVTBuffer *buf = avt_buffer_create(out_packet->data, out_packet->size, NULL, NULL); 149 | 150 | AVTPacket pkt = { 151 | .type = AVT_FRAME_TYPE_KEY, 152 | .data = buf, 153 | }; 154 | 155 | avt_send_stream_data(out.st, &pkt); 156 | #endif 157 | break; 158 | } 159 | }; 160 | 161 | for (int i = 0; input[i]; i++) 162 | io_close(&in[i], 0); 163 | io_close(&out, 1); 164 | 165 | end: 166 | avt_close(&avt); 167 | return 0; 168 | } 169 | -------------------------------------------------------------------------------- /tools/avcat/io.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2024, Lynne 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef AVCAT_UTILS_H 28 | #define AVCAT_UTILS_H 29 | 30 | #include "config.h" 31 | 32 | #include 33 | 34 | #ifdef HAVE_FFMPEG 35 | #include 36 | #endif 37 | 38 | enum IOMode { 39 | IO_RAW, 40 | IO_AVT, 41 | IO_LAVF, 42 | }; 43 | 44 | typedef struct IOContext { 45 | enum IOMode mode; 46 | int64_t val; 47 | 48 | enum AVTConnectionType type; 49 | AVTConnection *conn; 50 | AVTStream *st; 51 | 52 | AVTSender *out; 53 | 54 | #ifdef HAVE_FFMPEG 55 | AVFormatContext *avf; 56 | #endif 57 | 58 | // TODO: replace with AVTIO 59 | FILE *raw; 60 | } IOContext; 61 | 62 | int io_open(IOContext *io, AVTContext *avt, const char *path, int is_out); 63 | int io_close(IOContext *io, int is_out); 64 | 65 | #endif /* AVCAT_UTILS_H */ 66 | -------------------------------------------------------------------------------- /tools/avtdump.c: -------------------------------------------------------------------------------- 1 | /* Copyright © 2024, Lynne 2 | * All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | 26 | #include 27 | 28 | int main(int argc, char **argv) 29 | { 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /tools/meson.build: -------------------------------------------------------------------------------- 1 | # Copyright © 2024, Lynne 2 | # All rights reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions are met: 6 | # 7 | # 1. Redistributions of source code must retain the above copyright notice, this 8 | # list of conditions and the following disclaimer. 9 | # 10 | # 2. Redistributions in binary form must reproduce the above copyright notice, 11 | # this list of conditions and the following disclaimer in the documentation 12 | # and/or other materials provided with the distribution. 13 | # 14 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | 25 | lavc_dep = dependency('', required: false) 26 | lavf_dep = dependency('', required: false) 27 | lavu_dep = dependency('', required: false) 28 | 29 | if get_option('ffmpeg').auto() 30 | lavc_dep = dependency('libavcodec', version: '>= 59.4.100', required: false) 31 | lavf_dep = dependency('libavformat', version: '>= 58.42.100', required: false) 32 | lavu_dep = dependency('libavutil', version: '>= 56.43.100', required: false) 33 | endif 34 | 35 | # Generate local configure file 36 | conf_tools = conf 37 | 38 | configure_file( 39 | output: 'config.h', 40 | configuration: conf_tools, 41 | ) 42 | 43 | # avcat 44 | #============================================================================ 45 | avcat_sources = files( 46 | 'avcat.c', 47 | 'avcat/io.c', 48 | ) 49 | 50 | avcat_deps = [ 51 | lavc_dep, 52 | lavf_dep, 53 | lavu_dep, 54 | ] 55 | 56 | avcat_cflags = [ ] 57 | if lavc_dep.found() and lavf_dep.found() and lavu_dep.found() 58 | avcat_cflags += '-DHAVE_FFMPEG' 59 | endif 60 | 61 | executable('avcat', 62 | install: true, 63 | sources: avcat_sources + avtransport_spec_pub_headers, 64 | dependencies: avcat_deps, 65 | c_args: avcat_cflags, 66 | 67 | include_directories: avtransport_inc, 68 | link_with: [ avtransport_lib ] 69 | ) 70 | 71 | summary({ 72 | 'ffmpeg libs': lavc_dep.found() and lavf_dep.found() and lavu_dep.found(), 73 | }, section: 'avcat', bool_yn: true) 74 | 75 | # avtdump 76 | #============================================================================ 77 | avtdump_sources = files( 78 | 'avtdump.c', 79 | ) 80 | 81 | executable('avtdump', 82 | install: true, 83 | sources: avtdump_sources + avtransport_spec_pub_headers, 84 | 85 | include_directories: avtransport_inc, 86 | link_with: [ avtransport_lib ] 87 | ) 88 | --------------------------------------------------------------------------------