srt: Introduce port allocation

When in listener or rendezvous mode it is useful to be able to allocate
ports in a race free manner. Having the srt elements allocate the UDP
port for us and being able to read it back solves this issue.

This is analogous with how the udpsrc handles this.
This commit is contained in:
Jonas K Danielsson 2023-09-12 15:21:05 +02:00
parent a5cb2ef9cd
commit b5f70619ec
4 changed files with 36 additions and 6 deletions

View file

@ -236854,7 +236854,7 @@
"writable": true
},
"localport": {
"blurb": "Local port to bind",
"blurb": "Local port to bind (0 = allocate)",
"conditionally-available": false,
"construct": false,
"construct-only": false,
@ -237114,7 +237114,7 @@
"writable": true
},
"localport": {
"blurb": "Local port to bind",
"blurb": "Local port to bind (0 = allocate)",
"conditionally-available": false,
"construct": false,
"construct-only": false,
@ -237410,7 +237410,7 @@
"writable": true
},
"localport": {
"blurb": "Local port to bind",
"blurb": "Local port to bind (0 = allocate)",
"conditionally-available": false,
"construct": false,
"construct-only": false,
@ -237659,7 +237659,7 @@
"writable": true
},
"localport": {
"blurb": "Local port to bind",
"blurb": "Local port to bind (0 = allocate)",
"conditionally-available": false,
"construct": false,
"construct-only": false,

View file

@ -617,7 +617,7 @@ gst_srt_object_install_properties_helper (GObjectClass * gobject_class)
*/
g_object_class_install_property (gobject_class, PROP_LOCALPORT,
g_param_spec_uint ("localport", "Local port",
"Local port to bind", 0,
"Local port to bind (0 = allocate)", 0,
65535, GST_SRT_DEFAULT_PORT,
G_PARAM_READWRITE | GST_PARAM_MUTABLE_READY |
G_PARAM_STATIC_STRINGS));
@ -1378,7 +1378,9 @@ gst_srt_object_open_internal (GstSRTObject * srtobject, GError ** error)
" setting listener mode", addr_str);
}
port = gst_uri_get_port (srtobject->uri);
if (!gst_structure_get_uint (srtobject->parameters, "localport", &port)) {
port = gst_uri_get_port (srtobject->uri);
}
GST_DEBUG_OBJECT (srtobject->element,
"Opening SRT socket with parameters: %" GST_PTR_FORMAT,
@ -1405,6 +1407,24 @@ gst_srt_object_open_internal (GstSRTObject * srtobject, GError ** error)
gst_srt_object_connect (srtobject, connection_mode, sa, sa_len, error);
}
if (ret && port == 0) {
struct sockaddr_in addr;
socklen_t len;
len = sizeof (addr);
if (srt_getsockname (srtobject->sock, (struct sockaddr *) &addr,
(int *) &len) < 0) {
ret = FALSE;
g_set_error (error, GST_RESOURCE_ERROR,
GST_RESOURCE_ERROR_OPEN_READ_WRITE,
"Could not retrieve allocated port");
}
gst_structure_set (srtobject->parameters, "localport",
G_TYPE_UINT, (guint) g_ntohs (addr.sin_port), NULL);
GST_DEBUG_OBJECT (srtobject->element, "localport bound to %d",
g_ntohs (addr.sin_port));
}
out:
g_clear_object (&socket_address);

View file

@ -26,6 +26,11 @@
* srtsink is a network sink that sends [SRT](http://www.srtalliance.org/)
* packets to the network.
*
* When in listener or rendezvouz mode the srtsink element supports automatic
* port allocation. This can be achieved by setting the #GstSRTSink:localport
* property to 0. After setting the srtsink to PAUSED, the allocated port can
* be obtained by reading the #GstSRTSink:localport property.
*
* ## Examples</title>
*
* |[

View file

@ -26,6 +26,11 @@
* srtsrc is a network source that reads [SRT](http://www.srtalliance.org/)
* packets from the network.
*
* When in listener or rendezvouz mode the srtsrc element supports automatic
* port allocation. This can be achieved by setting the #GstSRTSrc:localport
* property to 0. After setting the srtsrc to PAUSED, the allocated port can
* be obtained by reading the #GstSRTSrc:localport property.
*
* ## Examples
* |[
* gst-launch-1.0 -v srtsrc uri="srt://127.0.0.1:7001" ! fakesink