Skip to content

Commit

Permalink
Add selection for rtp marker bit (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorxda authored Mar 15, 2024
1 parent 69186c9 commit c7ee868
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
6 changes: 6 additions & 0 deletions include/smolrtsp/nal_transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ typedef struct {
* The maximum size of an H.265 NAL unit (including the header).
*/
size_t max_h265_nalu_size;

/**
* The encoder uses slice segments.
*/
bool is_coded_slice;
} SmolRTSP_NalTransportConfig;

/**
Expand All @@ -51,6 +56,7 @@ typedef struct {
*
* - `max_h264_nalu_size` is #SMOLRTSP_MAX_H264_NALU_SIZE.
* - `max_h265_nalu_size` is #SMOLRTSP_MAX_H265_NALU_SIZE.
* - `is_coded_slice` is `false`.
*/
SmolRTSP_NalTransportConfig
SmolRTSP_NalTransportConfig_default(void) SMOLRTSP_PRIV_MUST_USE;
Expand Down
30 changes: 16 additions & 14 deletions src/nal_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
#include <slice99.h>

static int send_fragmentized_nal_data(
SmolRTSP_RtpTransport *t, SmolRTSP_RtpTimestamp ts, size_t max_packet_size,
SmolRTSP_NalUnit nalu);
SmolRTSP_NalTransport *self, SmolRTSP_RtpTimestamp ts,
size_t max_packet_size, SmolRTSP_NalUnit nalu);
static int send_fu(
SmolRTSP_RtpTransport *t, SmolRTSP_RtpTimestamp ts, SmolRTSP_NalUnit fu,
SmolRTSP_NalTransport *self, SmolRTSP_RtpTimestamp ts, SmolRTSP_NalUnit fu,
bool is_first_fragment, bool is_last_fragment);

SmolRTSP_NalTransportConfig SmolRTSP_NalTransportConfig_default(void) {
return (SmolRTSP_NalTransportConfig){
.max_h264_nalu_size = SMOLRTSP_MAX_H264_NALU_SIZE,
.max_h265_nalu_size = SMOLRTSP_MAX_H265_NALU_SIZE,
.is_coded_slice = false,
};
}

Expand Down Expand Up @@ -75,8 +76,10 @@ int SmolRTSP_NalTransport_send_packet(

if (nalu_size < max_packet_size) {
const bool marker =
SmolRTSP_NalHeader_is_coded_slice_idr(nalu.header) ||
SmolRTSP_NalHeader_is_coded_slice_non_idr(nalu.header);
self->config.is_coded_slice
? false
: SmolRTSP_NalHeader_is_coded_slice_idr(nalu.header) ||
SmolRTSP_NalHeader_is_coded_slice_non_idr(nalu.header);

const size_t header_buf_size = SmolRTSP_NalHeader_size(nalu.header);
uint8_t *header_buf = alloca(header_buf_size);
Expand All @@ -87,15 +90,14 @@ int SmolRTSP_NalTransport_send_packet(
U8Slice99_new(header_buf, header_buf_size), nalu.payload);
}

return send_fragmentized_nal_data(
self->transport, ts, max_packet_size, nalu);
return send_fragmentized_nal_data(self, ts, max_packet_size, nalu);
}

// See <https://tools.ietf.org/html/rfc6184#section-5.8> (H.264),
// <https://tools.ietf.org/html/rfc7798#section-4.4.3> (H.265).
static int send_fragmentized_nal_data(
SmolRTSP_RtpTransport *t, SmolRTSP_RtpTimestamp ts, size_t max_packet_size,
SmolRTSP_NalUnit nalu) {
SmolRTSP_NalTransport *self, SmolRTSP_RtpTimestamp ts,
size_t max_packet_size, SmolRTSP_NalUnit nalu) {
const size_t rem = nalu.payload.len % max_packet_size,
packets_count = (nalu.payload.len - rem) / max_packet_size;

Expand All @@ -110,7 +112,7 @@ static int send_fragmentized_nal_data(
: (packet_idx + 1) * max_packet_size);
const SmolRTSP_NalUnit fu = {nalu.header, fu_data};

if (send_fu(t, ts, fu, is_first_fragment, is_last_fragment) == -1) {
if (send_fu(self, ts, fu, is_first_fragment, is_last_fragment) == -1) {
return -1;
}
}
Expand All @@ -122,7 +124,7 @@ static int send_fragmentized_nal_data(
const bool is_first_fragment = 0 == packets_count,
is_last_fragment = true;

if (send_fu(t, ts, fu, is_first_fragment, is_last_fragment) == -1) {
if (send_fu(self, ts, fu, is_first_fragment, is_last_fragment) == -1) {
return -1;
}
}
Expand All @@ -131,16 +133,16 @@ static int send_fragmentized_nal_data(
}

static int send_fu(
SmolRTSP_RtpTransport *t, SmolRTSP_RtpTimestamp ts, SmolRTSP_NalUnit fu,
SmolRTSP_NalTransport *self, SmolRTSP_RtpTimestamp ts, SmolRTSP_NalUnit fu,
bool is_first_fragment, bool is_last_fragment) {
const size_t fu_header_size = SmolRTSP_NalHeader_fu_size(fu.header);
U8Slice99 fu_header = U8Slice99_new(alloca(fu_header_size), fu_header_size);

SmolRTSP_NalHeader_write_fu_header(
fu.header, fu_header.ptr, is_first_fragment, is_last_fragment);

const bool marker = is_last_fragment;
const bool marker = self->config.is_coded_slice ? false : is_last_fragment;

return SmolRTSP_RtpTransport_send_packet(
t, ts, marker, fu_header, fu.payload);
self->transport, ts, marker, fu_header, fu.payload);
}

0 comments on commit c7ee868

Please sign in to comment.