| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#ifdef HAVE_CONFIG_H |
| 36 |
#include "config.h" |
| 37 |
#endif |
| 38 |
|
| 39 |
#include <gst/gst.h> |
| 40 |
#include <gst/rtp/gstrtpbuffer.h> |
| 41 |
|
| 42 |
#include "gstrist.h" |
| 43 |
|
| 44 |
GST_DEBUG_CATEGORY_STATIC (gst_rist_rtx_receive_debug); |
| 45 |
#define GST_CAT_DEFAULT gst_rist_rtx_receive_debug |
| 46 |
|
| 47 |
enum |
| 48 |
{ |
| 49 |
PROP_0, |
| 50 |
PROP_NUM_RTX_REQUESTS, |
| 51 |
PROP_NUM_RTX_PACKETS, |
| 52 |
PROP_RIST |
| 53 |
}; |
| 54 |
|
| 55 |
static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", |
| 56 |
GST_PAD_SRC, |
| 57 |
GST_PAD_ALWAYS, |
| 58 |
GST_STATIC_CAPS ("application/x-rtp") |
| 59 |
); |
| 60 |
|
| 61 |
static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", |
| 62 |
GST_PAD_SINK, |
| 63 |
GST_PAD_ALWAYS, |
| 64 |
GST_STATIC_CAPS ("application/x-rtp") |
| 65 |
); |
| 66 |
|
| 67 |
struct _GstRistRtxReceive |
| 68 |
{ |
| 69 |
GstElement element; |
| 70 |
|
| 71 |
|
| 72 |
GstPad *sinkpad; |
| 73 |
GstPad *srcpad; |
| 74 |
|
| 75 |
|
| 76 |
guint num_rtx_requests; |
| 77 |
guint num_rtx_packets; |
| 78 |
|
| 79 |
GstClockTime last_time; |
| 80 |
}; |
| 81 |
|
| 82 |
static gboolean gst_rist_rtx_receive_src_event (GstPad * pad, |
| 83 |
GstObject * parent, GstEvent * event); |
| 84 |
static GstFlowReturn gst_rist_rtx_receive_chain (GstPad * pad, |
| 85 |
GstObject * parent, GstBuffer * buffer); |
| 86 |
static GstStateChangeReturn gst_rist_rtx_receive_change_state (GstElement * |
| 87 |
element, GstStateChange transition); |
| 88 |
static void gst_rist_rtx_receive_get_property (GObject * object, guint prop_id, |
| 89 |
GValue * value, GParamSpec * pspec); |
| 90 |
|
| 91 |
G_DEFINE_TYPE_WITH_CODE (GstRistRtxReceive, gst_rist_rtx_receive, |
| 92 |
GST_TYPE_ELEMENT, GST_DEBUG_CATEGORY_INIT (gst_rist_rtx_receive_debug, |
| 93 |
"ristrtxreceive", 0, "RIST retransmission receiver")); |
| 94 |
GST_ELEMENT_REGISTER_DEFINE (ristrtxreceive, "ristrtxreceive", |
| 95 |
GST_RANK_NONE, GST_TYPE_RIST_RTX_RECEIVE); |
| 96 |
|
| 97 |
static void |
| 98 |
gst_rist_rtx_receive_class_init (GstRistRtxReceiveClass * klass) |
| 99 |
{ |
| 100 |
GObjectClass *gobject_class; |
| 101 |
GstElementClass *gstelement_class; |
| 102 |
|
| 103 |
gobject_class = (GObjectClass *) klass; |
| 104 |
gstelement_class = (GstElementClass *) klass; |
| 105 |
|
| 106 |
gobject_class->get_property = gst_rist_rtx_receive_get_property; |
| 107 |
|
| 108 |
g_object_class_install_property (gobject_class, PROP_NUM_RTX_REQUESTS, |
| 109 |
g_param_spec_uint ("num-rtx-requests", "Num RTX Requests", |
| 110 |
"Number of retransmission events received", 0, G_MAXUINT, |
| 111 |
0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 112 |
|
| 113 |
g_object_class_install_property (gobject_class, PROP_NUM_RTX_PACKETS, |
| 114 |
g_param_spec_uint ("num-rtx-packets", "Num RTX Packets", |
| 115 |
" Number of retransmission packets received", 0, G_MAXUINT, |
| 116 |
0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 117 |
|
| 118 |
gst_element_class_add_static_pad_template (gstelement_class, &src_factory); |
| 119 |
gst_element_class_add_static_pad_template (gstelement_class, &sink_factory); |
| 120 |
|
| 121 |
gst_element_class_set_static_metadata (gstelement_class, |
| 122 |
"RIST Retransmission receiver", "Codec", |
| 123 |
"Receive retransmitted RIST packets according to VSF TR-06-1", |
| 124 |
"Nicolas Dufresne <[email protected]>"); |
| 125 |
|
| 126 |
gstelement_class->change_state = |
| 127 |
GST_DEBUG_FUNCPTR (gst_rist_rtx_receive_change_state); |
| 128 |
} |
| 129 |
|
| 130 |
static void |
| 131 |
gst_rist_rtx_receive_reset (GstRistRtxReceive * rtx) |
| 132 |
{ |
| 133 |
GST_OBJECT_LOCK (rtx); |
| 134 |
rtx->num_rtx_requests = 0; |
| 135 |
rtx->num_rtx_packets = 0; |
| 136 |
GST_OBJECT_UNLOCK (rtx); |
| 137 |
} |
| 138 |
|
| 139 |
static void |
| 140 |
gst_rist_rtx_receive_init (GstRistRtxReceive * rtx) |
| 141 |
{ |
| 142 |
GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtx); |
| 143 |
|
| 144 |
rtx->srcpad = |
| 145 |
gst_pad_new_from_template (gst_element_class_get_pad_template (klass, |
| 146 |
"src"), "src"); |
| 147 |
GST_PAD_SET_PROXY_CAPS (rtx->srcpad); |
| 148 |
GST_PAD_SET_PROXY_ALLOCATION (rtx->srcpad); |
| 149 |
gst_pad_set_event_function (rtx->srcpad, |
| 150 |
GST_DEBUG_FUNCPTR (gst_rist_rtx_receive_src_event)); |
| 151 |
gst_element_add_pad (GST_ELEMENT (rtx), rtx->srcpad); |
| 152 |
|
| 153 |
rtx->sinkpad = |
| 154 |
gst_pad_new_from_template (gst_element_class_get_pad_template (klass, |
| 155 |
"sink"), "sink"); |
| 156 |
GST_PAD_SET_PROXY_CAPS (rtx->sinkpad); |
| 157 |
GST_PAD_SET_PROXY_ALLOCATION (rtx->sinkpad); |
| 158 |
gst_pad_set_chain_function (rtx->sinkpad, |
| 159 |
GST_DEBUG_FUNCPTR (gst_rist_rtx_receive_chain)); |
| 160 |
gst_element_add_pad (GST_ELEMENT (rtx), rtx->sinkpad); |
| 161 |
} |
| 162 |
|
| 163 |
static gboolean |
| 164 |
gst_rist_rtx_receive_src_event (GstPad * pad, GstObject * parent, |
| 165 |
GstEvent * event) |
| 166 |
{ |
| 167 |
GstRistRtxReceive *rtx = GST_RIST_RTX_RECEIVE (parent); |
| 168 |
|
| 169 |
switch (GST_EVENT_TYPE (event)) { |
| 170 |
case GST_EVENT_CUSTOM_UPSTREAM: |
| 171 |
{ |
| 172 |
const GstStructure *s = gst_event_get_structure (event); |
| 173 |
|
| 174 |
|
| 175 |
if (gst_structure_has_name (s, "GstRTPRetransmissionRequest")) { |
| 176 |
#ifndef GST_DISABLE_GST_DEBUG |
| 177 |
guint seqnum = 0; |
| 178 |
guint ssrc = 0; |
| 179 |
|
| 180 |
|
| 181 |
if (!gst_structure_get_uint (s, "seqnum", &seqnum)) |
| 182 |
seqnum = -1; |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
if (!gst_structure_get_uint (s, "ssrc", &ssrc)) |
| 187 |
ssrc = -1; |
| 188 |
|
| 189 |
GST_DEBUG_OBJECT (rtx, "got rtx request for seqnum: %u, ssrc: %X", |
| 190 |
seqnum, ssrc); |
| 191 |
#endif |
| 192 |
|
| 193 |
GST_OBJECT_LOCK (rtx); |
| 194 |
|
| 195 |
++rtx->num_rtx_requests; |
| 196 |
GST_OBJECT_UNLOCK (rtx); |
| 197 |
} |
| 198 |
break; |
| 199 |
} |
| 200 |
default: |
| 201 |
break; |
| 202 |
} |
| 203 |
|
| 204 |
return gst_pad_event_default (pad, parent, event); |
| 205 |
} |
| 206 |
|
| 207 |
static GstFlowReturn |
| 208 |
gst_rist_rtx_receive_chain (GstPad * pad, GstObject * parent, |
| 209 |
GstBuffer * buffer) |
| 210 |
{ |
| 211 |
GstRistRtxReceive *rtx = GST_RIST_RTX_RECEIVE (parent); |
| 212 |
GstRTPBuffer rtp = GST_RTP_BUFFER_INIT; |
| 213 |
guint32 ssrc = 0; |
| 214 |
guint16 seqnum = 0; |
| 215 |
gboolean is_rtx; |
| 216 |
|
| 217 |
|
| 218 |
if (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp)) |
| 219 |
goto invalid_buffer; |
| 220 |
|
| 221 |
ssrc = gst_rtp_buffer_get_ssrc (&rtp); |
| 222 |
seqnum = gst_rtp_buffer_get_seq (&rtp); |
| 223 |
|
| 224 |
|
| 225 |
GST_OBJECT_LOCK (rtx); |
| 226 |
|
| 227 |
|
| 228 |
is_rtx = ssrc & 0x1; |
| 229 |
rtx->last_time = GST_BUFFER_PTS (buffer); |
| 230 |
|
| 231 |
if (is_rtx) |
| 232 |
|
| 233 |
++rtx->num_rtx_packets; |
| 234 |
|
| 235 |
GST_OBJECT_UNLOCK (rtx); |
| 236 |
|
| 237 |
|
| 238 |
if (is_rtx) { |
| 239 |
GST_DEBUG_OBJECT (rtx, |
| 240 |
"Recovered packet from RIST RTX seqnum:%u ssrc: %u", |
| 241 |
gst_rtp_buffer_get_seq (&rtp), gst_rtp_buffer_get_ssrc (&rtp)); |
| 242 |
gst_rtp_buffer_set_ssrc (&rtp, ssrc & 0xFFFFFFFE); |
| 243 |
GST_BUFFER_FLAG_SET (buffer, GST_RTP_BUFFER_FLAG_RETRANSMISSION); |
| 244 |
} |
| 245 |
|
| 246 |
gst_rtp_buffer_unmap (&rtp); |
| 247 |
|
| 248 |
GST_TRACE_OBJECT (rtx, "pushing packet seqnum:%u from master stream " |
| 249 |
"ssrc: %X", seqnum, ssrc); |
| 250 |
return gst_pad_push (rtx->srcpad, buffer); |
| 251 |
|
| 252 |
invalid_buffer: |
| 253 |
{ |
| 254 |
GST_ELEMENT_WARNING (rtx, STREAM, DECODE, (NULL), |
| 255 |
("Received invalid RTP payload, dropping")); |
| 256 |
gst_buffer_unref (buffer); |
| 257 |
return GST_FLOW_OK; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
static void |
| 262 |
gst_rist_rtx_receive_get_property (GObject * object, |
| 263 |
guint prop_id, GValue * value, GParamSpec * pspec) |
| 264 |
{ |
| 265 |
GstRistRtxReceive *rtx = GST_RIST_RTX_RECEIVE (object); |
| 266 |
|
| 267 |
switch (prop_id) { |
| 268 |
case PROP_NUM_RTX_REQUESTS: |
| 269 |
GST_OBJECT_LOCK (rtx); |
| 270 |
g_value_set_uint (value, rtx->num_rtx_requests); |
| 271 |
GST_OBJECT_UNLOCK (rtx); |
| 272 |
break; |
| 273 |
case PROP_NUM_RTX_PACKETS: |
| 274 |
GST_OBJECT_LOCK (rtx); |
| 275 |
g_value_set_uint (value, rtx->num_rtx_packets); |
| 276 |
GST_OBJECT_UNLOCK (rtx); |
| 277 |
break; |
| 278 |
default: |
| 279 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 280 |
break; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
static GstStateChangeReturn |
| 285 |
gst_rist_rtx_receive_change_state (GstElement * element, |
| 286 |
GstStateChange transition) |
| 287 |
{ |
| 288 |
GstRistRtxReceive *rtx = GST_RIST_RTX_RECEIVE (element); |
| 289 |
GstStateChangeReturn ret; |
| 290 |
|
| 291 |
ret = |
| 292 |
GST_ELEMENT_CLASS (gst_rist_rtx_receive_parent_class)->change_state |
| 293 |
(element, transition); |
| 294 |
|
| 295 |
switch (transition) { |
| 296 |
case GST_STATE_CHANGE_PAUSED_TO_READY: |
| 297 |
gst_rist_rtx_receive_reset (rtx); |
| 298 |
break; |
| 299 |
default: |
| 300 |
break; |
| 301 |
} |
| 302 |
|
| 303 |
return ret; |
| 304 |
} |