Skip to content

Commit

Permalink
rtpmanager: update for rtp header extensions
Browse files Browse the repository at this point in the history
Provide an implementation of the transport-wide-cc header extension and
use it in rtpfunnel.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-good/-/merge_requests/808>
  • Loading branch information
ystreet committed Dec 4, 2020
1 parent 2a5d7b1 commit 656af79
Show file tree
Hide file tree
Showing 6 changed files with 394 additions and 32 deletions.
32 changes: 32 additions & 0 deletions docs/gst_plugins_cache.json
Original file line number Diff line number Diff line change
Expand Up @@ -17001,6 +17001,38 @@
},
"rank": "none"
},
"rtphdrexttwcc": {
"RTP-Header-Extension-URI": "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01",
"author": "Matthew Waters <matthew@centricular.com>",
"description": "Extends RTP packets to add sequence number transport wide.",
"hierarchy": [
"GstRTPHeaderExtensionTWCC",
"GstRTPHeaderExtension",
"GstElement",
"GstObject",
"GInitiallyUnowned",
"GObject"
],
"klass": "Network/Extension/RTPHeader",
"long-name": "Transport Wide Congestion Control",
"properties": {
"n-streams": {
"blurb": "The number of separate RTP streams this header applies to",
"conditionally-available": false,
"construct": false,
"construct-only": false,
"controllable": false,
"default": "1",
"max": "-1",
"min": "1",
"mutable": "null",
"readable": true,
"type": "guint",
"writable": true
}
},
"rank": "marginal"
},
"rtpjitterbuffer": {
"author": "Philippe Kalaf <philippe.kalaf@collabora.co.uk>, Wim Taymans <wim.taymans@gmail.com>",
"description": "A buffer that deals with network jitter and other transmission faults",
Expand Down
74 changes: 42 additions & 32 deletions gst/rtpmanager/gstrtpfunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,13 @@
#endif

#include <gst/rtp/gstrtpbuffer.h>
#include <gst/rtp/gstrtphdrext.h>

#include "gstrtpfunnel.h"

GST_DEBUG_CATEGORY_STATIC (gst_rtp_funnel_debug);
#define GST_CAT_DEFAULT gst_rtp_funnel_debug

#define TWCC_EXTMAP_STR "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01"

/**************** GstRTPFunnelPad ****************/

struct _GstRtpFunnelPadClass
Expand Down Expand Up @@ -127,9 +126,8 @@ struct _GstRtpFunnel
/* The last pad data was chained on */
GstPad *current_pad;

guint8 twcc_ext_id; /* the negotiated twcc extmap id */
guint16 twcc_seqnum; /* our internal twcc seqnum */
guint twcc_pads; /* numer of sinkpads with negotiated twcc */
GstRTPHeaderExtension *twcc_ext;

/* properties */
gint common_ts_offset;
Expand Down Expand Up @@ -222,36 +220,40 @@ gst_rtp_funnel_set_twcc_seqnum (GstRtpFunnel * funnel,
GstPad * pad, GstBuffer ** buf)
{
GstRtpFunnelPad *fpad = GST_RTP_FUNNEL_PAD_CAST (pad);
guint8 twcc_seq[2] = { 0, };
GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
guint ext_id = gst_rtp_header_extension_get_id (funnel->twcc_ext);
guint8 *existing;
guint size;

if (!funnel->twcc_ext_id || !fpad->has_twcc)
if (!funnel->twcc_ext || !fpad->has_twcc)
return;

*buf = gst_buffer_make_writable (*buf);

if (gst_rtp_buffer_map (*buf, GST_MAP_READWRITE, &rtp)) {
gpointer data;
gst_rtp_header_extension_write (funnel->twcc_ext, *buf,
GST_RTP_HEADER_EXTENSION_ONE_BYTE, *buf, twcc_seq, sizeof (twcc_seq));

/* if there already is a twcc-seqnum inside the packet */
if (gst_rtp_buffer_get_extension_onebyte_header (&rtp, funnel->twcc_ext_id,
0, &data, NULL)) {
if (!gst_rtp_buffer_map (*buf, GST_MAP_READWRITE, &rtp))
goto map_failed;

/* with only one pad, we read the twcc-seqnum instead of writing it */
if (funnel->twcc_pads == 1) {
funnel->twcc_seqnum = GST_READ_UINT16_BE (data);
} else {
GST_WRITE_UINT16_BE (data, funnel->twcc_seqnum);
}
} else {
guint16 seq_be;
GST_WRITE_UINT16_BE (&seq_be, funnel->twcc_seqnum);
gst_rtp_buffer_add_extension_onebyte_header (&rtp, funnel->twcc_ext_id,
&seq_be, 2);
if (gst_rtp_buffer_get_extension_onebyte_header (&rtp, ext_id,
0, (gpointer) & existing, &size)) {
if (size >= gst_rtp_header_extension_get_max_size (funnel->twcc_ext, *buf)) {
existing[0] = twcc_seq[0];
existing[1] = twcc_seq[1];
}
}
/* TODO: two-byte variant */

gst_rtp_buffer_unmap (&rtp);

funnel->twcc_seqnum++;
return;

map_failed:
{
GST_ERROR ("failed to map buffer %p", *buf);
}
}

static GstFlowReturn
Expand Down Expand Up @@ -302,24 +304,25 @@ static void
gst_rtp_funnel_set_twcc_ext_id (GstRtpFunnel * funnel, guint8 twcc_ext_id)
{
gchar *name;
guint current_ext_id;

current_ext_id = gst_rtp_header_extension_get_id (funnel->twcc_ext);
g_object_set (funnel->twcc_ext, "n-streams", funnel->twcc_pads, NULL);

if (funnel->twcc_ext_id == twcc_ext_id)
if (current_ext_id == twcc_ext_id)
return;

name = g_strdup_printf ("extmap-%u", twcc_ext_id);

GST_OBJECT_LOCK (funnel);
gst_caps_set_simple (funnel->srccaps, name, G_TYPE_STRING, TWCC_EXTMAP_STR,
NULL);
GST_OBJECT_UNLOCK (funnel);
gst_caps_set_simple (funnel->srccaps, name, G_TYPE_STRING,
gst_rtp_header_extension_get_uri (funnel->twcc_ext), NULL);

g_free (name);

/* make sure we update the sticky with the new caps */
funnel->send_sticky_events = TRUE;

GST_INFO_OBJECT (funnel, "Setting twcc-ext-id to %u", twcc_ext_id);
funnel->twcc_ext_id = twcc_ext_id;
gst_rtp_header_extension_set_id (funnel->twcc_ext, twcc_ext_id);
}

static guint8
Expand All @@ -345,6 +348,8 @@ _get_extmap_id_for_attribute (const GstStructure * s, const gchar * ext_name)
return extmap_id;
}

#define TWCC_EXTMAP_STR "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01"

static gboolean
gst_rtp_funnel_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
{
Expand Down Expand Up @@ -376,22 +381,25 @@ gst_rtp_funnel_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
caps);
g_assert_not_reached ();
}
GST_OBJECT_UNLOCK (funnel);

s = gst_caps_get_structure (caps, 0);
if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
fpad->ssrc = ssrc;
GST_DEBUG_OBJECT (pad, "Got ssrc: %u", ssrc);
GST_OBJECT_LOCK (funnel);
g_hash_table_insert (funnel->ssrc_to_pad, GUINT_TO_POINTER (ssrc), pad);
GST_OBJECT_UNLOCK (funnel);
}

if (!funnel->twcc_ext)
funnel->twcc_ext =
gst_rtp_header_extension_create_from_uri (TWCC_EXTMAP_STR);

ext_id = _get_extmap_id_for_attribute (s, TWCC_EXTMAP_STR);
if (ext_id > 0) {
fpad->has_twcc = TRUE;
funnel->twcc_pads++;
gst_rtp_funnel_set_twcc_ext_id (funnel, ext_id);
}
GST_OBJECT_UNLOCK (funnel);

forward = FALSE;
break;
Expand Down Expand Up @@ -625,6 +633,8 @@ gst_rtp_funnel_finalize (GObject * object)
gst_caps_unref (funnel->srccaps);
g_hash_table_destroy (funnel->ssrc_to_pad);

gst_clear_object (&funnel->twcc_ext);

G_OBJECT_CLASS (parent_class)->finalize (object);
}

Expand Down
Loading

0 comments on commit 656af79

Please sign in to comment.