return SRT_INVALID_SOCK;
}
+SRTSOCKET
+gst_srt_server_listen (GstElement * elem, int sender, const gchar * host,
+ guint16 port, int latency, gint * poll_id, const gchar * passphrase,
+ int key_length)
+{
+ SRTSOCKET sock = SRT_INVALID_SOCK;
+ GError *error = NULL;
+ struct sockaddr sa;
+ size_t sa_len;
+ GSocketAddress *addr = NULL;
+
+ if (host == NULL) {
+ GInetAddress *any = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
+
+ addr = g_inet_socket_address_new (any, port);
+ g_object_unref (any);
+ } else {
+ addr = g_inet_socket_address_new_from_string (host, port);
+ }
+
+ if (addr == NULL) {
+ GST_WARNING_OBJECT (elem,
+ "failed to extract host or port from the given URI");
+ goto failed;
+ }
+
+ sa_len = g_socket_address_get_native_size (addr);
+ if (!g_socket_address_to_native (addr, &sa, sa_len, &error)) {
+ GST_ELEMENT_ERROR (elem, RESOURCE, OPEN_READ, ("Invalid address"),
+ ("cannot resolve address (reason: %s)", error->message));
+ goto failed;
+ }
+
+ sock = srt_socket (sa.sa_family, SOCK_DGRAM, 0);
+ if (sock == SRT_INVALID_SOCK) {
+ GST_WARNING_OBJECT (elem, "failed to create SRT socket (reason: %s)",
+ srt_getlasterror_str ());
+ goto failed;
+ }
+
+ /* Make SRT server socket non-blocking */
+ /* for non-blocking srt_close() */
+ srt_setsockopt (sock, 0, SRTO_SNDSYN, &(int) {
+ 0}, sizeof (int));
+
+ /* for non-blocking srt_accept() */
+ srt_setsockopt (sock, 0, SRTO_RCVSYN, &(int) {
+ 0}, sizeof (int));
+
+ /* Make sure TSBPD mode is enable (SRT mode) */
+ srt_setsockopt (sock, 0, SRTO_TSBPDMODE, &(int) {
+ 1}, sizeof (int));
+
+ srt_setsockopt (sock, 0, SRTO_SENDER, &sender, sizeof (int));
+ srt_setsockopt (sock, 0, SRTO_TSBPDDELAY, &latency, sizeof (int));
+
+ if (passphrase != NULL && passphrase[0] != '\0') {
+ srt_setsockopt (sock, 0, SRTO_PASSPHRASE, passphrase, strlen (passphrase));
+ srt_setsockopt (sock, 0, SRTO_PBKEYLEN, &key_length, sizeof (int));
+ }
+
+ *poll_id = srt_epoll_create ();
+ if (*poll_id == -1) {
+ GST_ELEMENT_ERROR (elem, LIBRARY, INIT, (NULL),
+ ("failed to create poll id for SRT socket (reason: %s)",
+ srt_getlasterror_str ()));
+ goto failed;
+ }
+
+ srt_epoll_add_usock (*poll_id, sock, &(int) {
+ SRT_EPOLL_IN});
+
+ if (srt_bind (sock, &sa, sa_len) == SRT_ERROR) {
+ GST_WARNING_OBJECT (elem, "failed to bind SRT server socket (reason: %s)",
+ srt_getlasterror_str ());
+ goto failed;
+ }
+
+ if (srt_listen (sock, 1) == SRT_ERROR) {
+ GST_WARNING_OBJECT (elem, "failed to listen SRT socket (reason: %s)",
+ srt_getlasterror_str ());
+ goto failed;
+ }
+
+ g_clear_object (&addr);
+
+ return sock;
+
+failed:
+ if (*poll_id != SRT_ERROR) {
+ srt_epoll_release (*poll_id);
+ *poll_id = SRT_ERROR;
+ }
+
+ if (sock != SRT_INVALID_SOCK) {
+ srt_close (sock);
+ sock = SRT_INVALID_SOCK;
+ }
+
+ g_clear_error (&error);
+ g_clear_object (&addr);
+
+ return SRT_INVALID_SOCK;
+}
+
static gboolean
plugin_init (GstPlugin * plugin)
{
GSocketAddress ** socket_address, gint * poll_id,
gchar * passphrase, int key_length);
+SRTSOCKET
+gst_srt_server_listen (GstElement * elem, int sender,
+ const gchar * host, guint16 port, gint latency, gint * poll_id,
+ const gchar * passphrase, int key_length);
+
G_END_DECLS
GstSRTServerSinkPrivate *priv = GST_SRT_SERVER_SINK_GET_PRIVATE (self);
GstSRTBaseSink *base = GST_SRT_BASE_SINK (sink);
GstUri *uri = gst_uri_ref (GST_SRT_BASE_SINK (self)->uri);
- GSocketAddress *socket_address = NULL;
GError *error = NULL;
gboolean ret = TRUE;
- struct sockaddr sa;
- size_t sa_len;
const gchar *host;
- int lat = base->latency;
if (gst_uri_get_port (uri) == GST_URI_NO_PORT) {
GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, NULL, (("Invalid port")));
}
host = gst_uri_get_host (uri);
- if (host == NULL) {
- GInetAddress *any = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
-
- socket_address = g_inet_socket_address_new (any, gst_uri_get_port (uri));
- g_object_unref (any);
- } else {
- socket_address =
- g_inet_socket_address_new_from_string (host, gst_uri_get_port (uri));
- }
- if (socket_address == NULL) {
- GST_WARNING_OBJECT (self,
- "failed to extract host or port from the given URI");
- goto failed;
- }
-
- sa_len = g_socket_address_get_native_size (socket_address);
- if (!g_socket_address_to_native (socket_address, &sa, sa_len, &error)) {
- GST_WARNING_OBJECT (self, "cannot resolve address (reason: %s)",
- error->message);
- goto failed;
- }
+ priv->sock = gst_srt_server_listen (GST_ELEMENT (self),
+ TRUE, host, gst_uri_get_port (uri),
+ base->latency, &priv->poll_id, base->passphrase, base->key_length);
- priv->sock = srt_socket (sa.sa_family, SOCK_DGRAM, 0);
if (priv->sock == SRT_INVALID_SOCK) {
- GST_WARNING_OBJECT (self, "failed to create SRT socket (reason: %s)",
- srt_getlasterror_str ());
- goto failed;
- }
-
- /* Make SRT non-blocking */
- srt_setsockopt (priv->sock, 0, SRTO_SNDSYN, &(int) {
- 0}, sizeof (int));
-
- /* Make sure TSBPD mode is enable (SRT mode) */
- srt_setsockopt (priv->sock, 0, SRTO_TSBPDMODE, &(int) {
- 1}, sizeof (int));
-
- /* This is a sink, we're always a sender */
- srt_setsockopt (priv->sock, 0, SRTO_SENDER, &(int) {
- 1}, sizeof (int));
-
- srt_setsockopt (priv->sock, 0, SRTO_TSBPDDELAY, &lat, sizeof (int));
-
- if (base->passphrase != NULL && base->passphrase[0] != '\0') {
- srt_setsockopt (priv->sock, 0, SRTO_PASSPHRASE,
- base->passphrase, strlen (base->passphrase));
- srt_setsockopt (priv->sock, 0, SRTO_PBKEYLEN,
- &base->key_length, sizeof (int));
- }
-
- priv->poll_id = srt_epoll_create ();
- if (priv->poll_id == -1) {
- GST_WARNING_OBJECT (self,
- "failed to create poll id for SRT socket (reason: %s)",
- srt_getlasterror_str ());
- goto failed;
- }
- srt_epoll_add_usock (priv->poll_id, priv->sock, &(int) {
- SRT_EPOLL_IN});
-
- if (srt_bind (priv->sock, &sa, sa_len) == SRT_ERROR) {
- GST_WARNING_OBJECT (self, "failed to bind SRT server socket (reason: %s)",
- srt_getlasterror_str ());
- goto failed;
- }
-
- if (srt_listen (priv->sock, 1) == SRT_ERROR) {
- GST_WARNING_OBJECT (self, "failed to listen SRT socket (reason: %s)",
- srt_getlasterror_str ());
+ GST_ERROR_OBJECT (sink, "Failed to create srt socket");
goto failed;
}
}
g_clear_pointer (&uri, gst_uri_unref);
- g_clear_object (&socket_address);
return ret;
g_clear_error (&error);
g_clear_pointer (&uri, gst_uri_unref);
- g_clear_object (&socket_address);
return FALSE;
}
while (!priv->has_client) {
GST_DEBUG_OBJECT (self, "poll wait (timeout: %d)", priv->poll_timeout);
- /* Make SRT server socket non-blocking */
- srt_setsockopt (priv->sock, 0, SRTO_SNDSYN, &(int) {
- 0}, sizeof (int));
-
if (srt_epoll_wait (priv->poll_id, ready, &(int) {
2}, 0, 0, priv->poll_timeout, 0, 0, 0, 0) == -1) {
int srt_errno = srt_getlasterror (NULL);
GstSRTServerSrcPrivate *priv = GST_SRT_SERVER_SRC_GET_PRIVATE (self);
GstSRTBaseSrc *base = GST_SRT_BASE_SRC (src);
GstUri *uri = gst_uri_ref (base->uri);
- GError *error = NULL;
- struct sockaddr sa;
- size_t sa_len;
- GSocketAddress *socket_address;
const gchar *host;
- int lat = base->latency;
if (gst_uri_get_port (uri) == GST_URI_NO_PORT) {
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_WRITE, NULL, (("Invalid port")));
}
host = gst_uri_get_host (uri);
- if (host == NULL) {
- GInetAddress *any = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
-
- socket_address = g_inet_socket_address_new (any, gst_uri_get_port (uri));
- g_object_unref (any);
- } else {
- socket_address =
- g_inet_socket_address_new_from_string (host, gst_uri_get_port (uri));
- }
-
- if (socket_address == NULL) {
- GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ, ("Invalid URI"),
- ("failed to extract host or port from the given URI"));
- goto failed;
- }
-
- sa_len = g_socket_address_get_native_size (socket_address);
- if (!g_socket_address_to_native (socket_address, &sa, sa_len, &error)) {
- GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ, ("Invalid URI"),
- ("cannot resolve address (reason: %s)", error->message));
- goto failed;
- }
-
- priv->sock = srt_socket (sa.sa_family, SOCK_DGRAM, 0);
- if (priv->sock == SRT_ERROR) {
- GST_ELEMENT_ERROR (self, LIBRARY, INIT, (NULL),
- ("failed to create poll id for SRT socket (reason: %s)",
- srt_getlasterror_str ()));
- goto failed;
- }
- /* Make sure TSBPD mode is enable (SRT mode) */
- srt_setsockopt (priv->sock, 0, SRTO_TSBPDMODE, &(int) {
- 1}, sizeof (int));
-
- /* This is a sink, we're always a receiver */
- srt_setsockopt (priv->sock, 0, SRTO_SENDER, &(int) {
- 0}, sizeof (int));
-
- srt_setsockopt (priv->sock, 0, SRTO_TSBPDDELAY, &lat, sizeof (int));
-
- if (base->passphrase != NULL && base->passphrase[0] != '\0') {
- srt_setsockopt (priv->sock, 0, SRTO_PASSPHRASE,
- base->passphrase, strlen (base->passphrase));
- srt_setsockopt (priv->sock, 0, SRTO_PBKEYLEN,
- &base->key_length, sizeof (int));
- }
-
- priv->poll_id = srt_epoll_create ();
- if (priv->poll_id == -1) {
- GST_ELEMENT_ERROR (self, LIBRARY, INIT, (NULL),
- ("failed to create poll id for SRT socket (reason: %s)",
- srt_getlasterror_str ()));
- goto failed;
- }
-
- srt_epoll_add_usock (priv->poll_id, priv->sock, &(int) {
- SRT_EPOLL_IN});
-
- if (srt_bind (priv->sock, &sa, sa_len) == SRT_ERROR) {
- GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ, (NULL),
- ("failed to bind SRT server socket (reason: %s)",
- srt_getlasterror_str ()));
- goto failed;
- }
+ priv->sock = gst_srt_server_listen (GST_ELEMENT (self),
+ FALSE, host, gst_uri_get_port (uri),
+ base->latency, &priv->poll_id, base->passphrase, base->key_length);
- if (srt_listen (priv->sock, 1) == SRT_ERROR) {
- GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ, (NULL),
- ("failed to listen SRT socket (reason: %s)", srt_getlasterror_str ()));
+ if (priv->sock == SRT_INVALID_SOCK) {
+ GST_ERROR_OBJECT (src, "Failed to create srt socket");
goto failed;
}
g_clear_pointer (&uri, gst_uri_unref);
- g_clear_object (&socket_address);
return TRUE;
priv->sock = SRT_ERROR;
}
- g_clear_error (&error);
g_clear_pointer (&uri, gst_uri_unref);
- g_clear_object (&socket_address);
return FALSE;
}