Skip to content

Commit

Permalink
More API improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanreg committed Nov 22, 2023
1 parent ca69598 commit f8ec840
Show file tree
Hide file tree
Showing 17 changed files with 320 additions and 161 deletions.
8 changes: 8 additions & 0 deletions draft-avtransport-spec.bs
Original file line number Diff line number Diff line change
Expand Up @@ -2408,6 +2408,14 @@ The [=colorspace=] field must be interpreted in the following way:
Note: [matrix] must be equal to ''0xFF'' and the [=custom_matrix=] must be a valid matrix to transform XYB into RGB.
: <dfn>CSP_ICTCP</dfn> = <i>0x5</i>
:: Video contains [[BT2100]] ICtCp color data.
: <dfn>CSP_BAYER_BGGR</dfn> = <i>0x6</i>
:: Video contains raw CMOS voltages, from 4x4 cells with a pattern 'Blue', 'Green', 'Green', 'Red', in raster-order.
: <dfn>CSP_BAYER_RGGB</dfn> = <i>0x7</i>
:: Same as above, with a 'Red', 'Green', 'Green', 'Blue' order.
: <dfn>CSP_BAYER_GBRG</dfn> = <i>0x8</i>
:: Same as above, with a 'Green', 'Blue', 'Red', 'Green' order.
: <dfn>CSP_BAYER_GRBG</dfn> = <i>0x9</i>
:: Same as above, with a 'Green', 'Red', 'Blue', 'Green' order.
</div>


Expand Down
2 changes: 1 addition & 1 deletion libavtransport/avtransport.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void avt_close(AVTContext **ctx)
}
}

AVTStream *avt_alloc_stream(AVTContext *ctx, uint16_t id)
AVTStream *avt_alloc_stream(AVTContext *ctx, uint16_t id, AVTStream **stl, int *nb_st)
{
if (!ctx->output.ctx)
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion libavtransport/bytestream.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

#include <avtransport/utils.h>
#include <avtransport/rational.h>
#include "utils.h"
#include "utils_internal.h"

#ifndef AVT_RB8
#define AVT_RB8(x) \
Expand Down
2 changes: 2 additions & 0 deletions libavtransport/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

typedef struct AVTStreamPriv {
enum AVTCodecID codec_id;

struct AVTOutput *out;
} AVTStreamPriv;

struct AVTContext {
Expand Down
55 changes: 28 additions & 27 deletions libavtransport/encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include <zstd.h>
#endif

static inline int avt_encode_compress(AVTContext *ctx, AVTOutput *out,
static inline int avt_encode_compress(AVTOutput *out,
AVTBuffer **data, enum AVTPktDescriptors desc,
enum AVTDataCompression *data_compression)
{
Expand All @@ -52,6 +52,8 @@ static inline int avt_encode_compress(AVTContext *ctx, AVTOutput *out,
uint8_t *src = avt_buffer_get_data(*data, &src_len);

size_t dst_size = ZSTD_compressBound(src_len);

/* TODO: use a buffer pool */
uint8_t *dst = malloc(dst_size);
if (!dst)
return AVT_ERROR(ENOMEM);
Expand Down Expand Up @@ -91,7 +93,7 @@ static inline int avt_encode_compress(AVTContext *ctx, AVTOutput *out,
return err;
}

int avt_output_session_start(AVTContext *ctx, AVTOutput *out)
int avt_send_session_start(AVTOutput *out)
{
uint8_t hdr[AVT_MAX_HEADER_LEN];
AVTBytestream bs = avt_bs_init(hdr, sizeof(hdr));
Expand All @@ -107,10 +109,10 @@ int avt_output_session_start(AVTContext *ctx, AVTOutput *out)

avt_encode_session_start(&bs, &pkt);

return avt_packet_send(ctx, out, hdr, avt_bs_offs(&bs), NULL);
return avt_packet_send(out, hdr, avt_bs_offs(&bs), NULL);
}

int avt_output_time_sync(AVTContext *ctx, AVTOutput *out)
int avt_send_time_sync(AVTOutput *out)
{
uint8_t hdr[AVT_MAX_HEADER_LEN];
AVTBytestream bs = avt_bs_init(hdr, sizeof(hdr));
Expand All @@ -124,11 +126,10 @@ int avt_output_time_sync(AVTContext *ctx, AVTOutput *out)
.ts_clock_hz = 0,
});

return avt_packet_send(ctx, out, hdr, avt_bs_offs(&bs), NULL);
return avt_packet_send(out, hdr, avt_bs_offs(&bs), NULL);
}

int avt_output_stream_register(AVTContext *ctx, AVTOutput *out,
AVTStream *st)
int avt_send_stream_register(AVTOutput *out, AVTStream *st)
{
uint8_t hdr[AVT_MAX_HEADER_LEN];
AVTBytestream bs = avt_bs_init(hdr, sizeof(hdr));
Expand All @@ -148,18 +149,18 @@ int avt_output_stream_register(AVTContext *ctx, AVTOutput *out,
.init_packets = 0,
});

return avt_packet_send(ctx, out, hdr, avt_bs_offs(&bs), NULL);
return avt_packet_send(out, hdr, avt_bs_offs(&bs), NULL);
}

#define INIT_SEGMENTED(buf, desc) \
int err; \
size_t maxp = avt_packet_get_max_size(ctx, out); \
size_t maxp = avt_packet_get_max_size(out); \
size_t payload_size = avt_buffer_get_data_len(buf); \
size_t pbytes = payload_size; \
size_t seg_len = AVT_MIN(pbytes, maxp); \
\
enum AVTDataCompression data_compression; \
err = avt_encode_compress(ctx, out, &buf, desc, &data_compression); \
err = avt_encode_compress(out, &buf, desc, &data_compression); \
if (err < 0) \
return err; \
\
Expand All @@ -171,7 +172,7 @@ int avt_output_stream_register(AVTContext *ctx, AVTOutput *out,
uint32_t init_seq = atomic_fetch_add(&out->seq, 1ULL) & UINT32_MAX;

#define SEGMENT(buf, seg_desc) \
err = avt_packet_send(ctx, out, hdr, avt_bs_offs(&bs), &tmp); \
err = avt_packet_send(out, hdr, avt_bs_offs(&bs), &tmp); \
avt_buffer_quick_unref(&tmp); \
if (err < 0) \
return err; \
Expand All @@ -195,7 +196,7 @@ int avt_output_stream_register(AVTContext *ctx, AVTOutput *out,
.header_7 = { hdr[h7o + 0], hdr[h7o + 1], hdr[h7o + 2], hdr[h7o + 3] }, \
}); \
\
err = avt_packet_send(ctx, out, seg, avt_bs_offs(&bs), &tmp); \
err = avt_packet_send(out, seg, avt_bs_offs(&bs), &tmp); \
avt_buffer_quick_unref(&tmp); \
if (err < 0) \
break; \
Expand All @@ -206,8 +207,8 @@ int avt_output_stream_register(AVTContext *ctx, AVTOutput *out,
\
return err;

int avt_output_stream_data(AVTContext *ctx, AVTOutput *out,
AVTStream *st, AVTPacket *pkt)
int avt_send_stream_data(AVTOutput *out,
AVTStream *st, AVTPacket *pkt)
{
INIT_SEGMENTED(pkt->data, AVT_PKT_STREAM_DATA)

Expand All @@ -227,9 +228,9 @@ int avt_output_stream_data(AVTContext *ctx, AVTOutput *out,
SEGMENT(pkt->data, AVT_PKT_STREAM_DATA_SEGMENT)
}

int avt_output_generic_data(AVTContext *ctx, AVTOutput *out,
AVTStream *st, AVTBuffer *data, int64_t pts,
uint32_t hdr_desc, uint32_t seg_desc)
int avt_send_generic_data(AVTOutput *out,
AVTStream *st, AVTBuffer *data, int64_t pts,
uint32_t hdr_desc, uint32_t seg_desc)
{
INIT_SEGMENTED(data, hdr_desc)

Expand All @@ -246,8 +247,8 @@ int avt_output_generic_data(AVTContext *ctx, AVTOutput *out,
SEGMENT(data, seg_desc)
}

int avt_output_lut_data(AVTContext *ctx, AVTOutput *out,
AVTStream *st, int64_t pts)
int avt_send_lut_data(AVTOutput *out,
AVTStream *st, int64_t pts)
{
INIT_SEGMENTED(st->lut_data, AVT_PKT_LUT_ICC)

Expand All @@ -262,8 +263,8 @@ int avt_output_lut_data(AVTContext *ctx, AVTOutput *out,
SEGMENT(st->lut_data, AVT_PKT_STREAM_DATA_SEGMENT)
}

int avt_output_icc_data(AVTContext *ctx, AVTOutput *out,
AVTStream *st, int64_t pts)
int avt_send_icc_data(AVTOutput *out,
AVTStream *st, int64_t pts)
{
INIT_SEGMENTED(st->icc_data, AVT_PKT_LUT_ICC)

Expand All @@ -278,8 +279,8 @@ int avt_output_icc_data(AVTContext *ctx, AVTOutput *out,
SEGMENT(st->icc_data, AVT_PKT_STREAM_DATA_SEGMENT)
}

int avt_output_video_info(AVTContext *ctx, AVTOutput *out,
AVTStream *st, int64_t pts)
int avt_send_video_info(AVTOutput *out,
AVTStream *st, int64_t pts)
{
uint8_t hdr[AVT_MAX_HEADER_LEN];
AVTBytestream bs = avt_bs_init(hdr, sizeof(hdr));
Expand All @@ -291,11 +292,11 @@ int avt_output_video_info(AVTContext *ctx, AVTOutput *out,

avt_encode_video_info(&bs, &pkt);

return avt_packet_send(ctx, out, hdr, avt_bs_offs(&bs), NULL);
return avt_packet_send(out, hdr, avt_bs_offs(&bs), NULL);
}

int avt_output_video_orientation(AVTContext *ctx, AVTOutput *out,
AVTStream *st, int64_t pts)
int avt_send_video_orientation(AVTOutput *out,
AVTStream *st, int64_t pts)
{
uint8_t hdr[AVT_MAX_HEADER_LEN];
AVTBytestream bs = avt_bs_init(hdr, sizeof(hdr));
Expand All @@ -307,5 +308,5 @@ int avt_output_video_orientation(AVTContext *ctx, AVTOutput *out,

avt_encode_video_orientation(&bs, &pkt);

return avt_packet_send(ctx, out, hdr, avt_bs_offs(&bs), NULL);
return avt_packet_send(out, hdr, avt_bs_offs(&bs), NULL);
}
33 changes: 16 additions & 17 deletions libavtransport/encode.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,31 @@
#include "output_internal.h"

/* Session start */
int avt_output_session_start(AVTContext *ctx, AVTOutput *out);
int avt_send_session_start(AVTOutput *out);

/* Time sync */
int avt_output_time_sync(AVTContext *ctx, AVTOutput *out);
int avt_send_time_sync(AVTOutput *out);

/* Stream registration and data */
int avt_output_stream_register(AVTContext *ctx, AVTOutput *out,
AVTStream *st);
int avt_output_stream_data(AVTContext *ctx, AVTOutput *out,
AVTStream *st, AVTPacket *pkt);
int avt_send_stream_register(AVTOutput *out, AVTStream *st);
int avt_send_stream_data(AVTOutput *out,
AVTStream *st, AVTPacket *pkt);

/* Generic data */
int avt_output_generic_data(AVTContext *ctx, AVTOutput *out,
AVTStream *st, AVTBuffer *data, int64_t pts,
uint32_t first_desd, uint32_t seg_desc);
int avt_send_generic_data(AVTOutput *out,
AVTStream *st, AVTBuffer *data, int64_t pts,
uint32_t first_desd, uint32_t seg_desc);

/* LUT/ICC */
int avt_output_lut_data(AVTContext *ctx, AVTOutput *out,
AVTStream *st, int64_t pts);
int avt_output_icc_data(AVTContext *ctx, AVTOutput *out,
AVTStream *st, int64_t pts);
int avt_send_lut_data(AVTOutput *out,
AVTStream *st, int64_t pts);
int avt_send_icc_data(AVTOutput *out,
AVTStream *st, int64_t pts);

/* Video info/orientation */
int avt_output_video_info(AVTContext *ctx, AVTOutput *out,
AVTStream *st, int64_t pts);
int avt_output_video_orientation(AVTContext *ctx, AVTOutput *out,
AVTStream *st, int64_t pts);
int avt_send_video_info(AVTOutput *out,
AVTStream *st, int64_t pts);
int avt_send_video_orientation(AVTOutput *out,
AVTStream *st, int64_t pts);

#endif
11 changes: 8 additions & 3 deletions libavtransport/include/avtransport/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
#ifndef AVTRANSPORT_CONNECTION_H
#define AVTRANSPORT_CONNECTION_H

#include "utils.h"
#include <avtransport/packet_enums.h>
#include <avtransport/utils.h>

typedef struct AVTConnection AVTConnection;

Expand Down Expand Up @@ -108,11 +109,15 @@ typedef struct AVTConnectionInfo {
struct {
/* Called by libavtransport in strictly sequential order,
* with no holes, to write data. */
int (*write)(void *opaque, AVTBuffer *buf);
int (*write)(void *opaque,
uint8_t hdr[AVT_MAX_HEADER_LEN], size_t hdr_len,
AVTBuffer *buf);

/* Called by libavtransport to retrieve a piece of
* data at a particular offset. */
int (*read)(void *opaque, AVTBuffer **buf, uint64_t offset);
int (*read)(void *opaque,
uint8_t hdr[AVT_MAX_HEADER_LEN], size_t hdr_len,
AVTBuffer **buf, uint64_t offset);

/* Opaque pointer which will be used for the data() callback. */
void *opaque;
Expand Down
58 changes: 30 additions & 28 deletions libavtransport/include/avtransport/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef AVTRANSPORT_OUTPUT_H
#define AVTRANSPORT_OUTPUT_H
#ifndef AVTRANSPORT_SEND_H
#define AVTRANSPORT_SEND_H

#include "connection.h"
#include "stream.h"
#include "utils.h"

typedef struct AVTOutput AVTOutput;

typedef struct AVTOutputOptions {
/* Period in nanoseconds for sending session_start packets to the receiver.
* Default: 250000000 (every 250 milliseconds). */
Expand All @@ -52,48 +54,48 @@ typedef struct AVTOutputOptions {
int threads;
} AVTOutputOptions;

/* All functions listed here are thread-safe. */

/* Open an output and immediately send/write a stream session packet.
*
* NOTE: Multiple connections may be bound for output to enable
* one-to-many streaming or writing */
AVT_API int avt_output_open(AVTContext *ctx, AVTConnection *conn);
AVT_API int avt_output_open(AVTContext *ctx, AVTOutput **out,
AVTConnection *conn, AVTOutputOptions *opts);

/* Send an epoch packet and set the epoch to use. */
AVT_API int avt_output_set_epoch(AVTContext *ctx, uint64_t epoch);
/* Set the epoch to use, as nanoseconds after 00:00:00 UTC on 1 January 1970.
* Should be called once, at the start of streaming.
* If zero, or not called, the current time will be used. */
AVT_API int avt_output_set_epoch(AVTOutput *out, uint64_t epoch);

/* Register a stream and allocate internal state for it.
* To automatically assign a stream ID, set id to 65535.
* To automatically assign a stream ID, set id to UINT16_MAX.
* If there's an existing stream with the same ID, will return NULL. */
AVT_API AVTStream *avt_output_add_stream(AVTContext *ctx, uint16_t id);
AVT_API AVTStream *avt_output_stream_add(AVTOutput *out, uint16_t id);

/* Update a stream, (re-)emmitting a stream registration packet.
* The id MUST match the one from avt_output_add_stream(). */
AVT_API int avt_output_update_stream(AVTContext *ctx, AVTStream *st);

AVT_API int avt_output_add_font(AVTContext *ctx, AVTBuffer *data, const char *name);
AVT_API int avt_output_stream_update(AVTOutput *out, AVTStream *st);

/* Write data to output. Can be called from multiple threads at once.
* If compiled with threads, actual output happens in a different thread. */
AVT_API int avt_output_write_stream_data(AVTContext *ctx, AVTStream *st,
AVTPacket *pkt);
/* Attach a font to a stream */
AVT_API int avt_output_font_attachment(AVTStream *st, AVTBuffer *file,
const char *filename, enum AVTFontType type);

/* Write user data packets */
AVT_API int avt_output_write_user_data(AVTContext *ctx, AVTBuffer *data,
uint8_t descriptor_flags, uint16_t user,
int prioritize);
/* Write stream data to the output. */
AVT_API int avt_output_stream_data(AVTStream *st, AVTPacket *pkt);

AVT_API int avt_output_close_stream(AVTContext *ctx, AVTStream *st);
/* Write user data packets. The immediate flag can be used to skip
* any potential queueing. */
AVT_API int avt_output_user_data(AVTOutput *out, AVTBuffer *data,
uint64_t opaque, int immediate);

AVT_API int avt_output_control(AVTContext *ctx, void *opaque, int cease,
int resend_init, int error, uint8_t redirect[16],
uint16_t redirect_port, int seek_requested,
int64_t seek_offset, uint32_t seek_seq);
/* Immediately refresh all stream data */
AVT_API int avt_output_refresh(AVTOutput *out);

AVT_API int avt_output_feedback(AVTContext *ctx, void *opaque, AVTStream *st,
uint64_t epoch_offset, uint64_t bandwidth,
uint32_t fec_corrections, uint32_t corrupt_packets,
uint32_t missing_packets);
/* Close a single stream */
AVT_API int avt_output_stream_close(AVTStream **st);

AVT_API int avt_output_close(AVTContext *ctx);
/* Close all streams and free resources */
AVT_API int avt_output_close(AVTOutput **out);

#endif
Loading

0 comments on commit f8ec840

Please sign in to comment.