From: Seungha Yang Date: Fri, 20 Jul 2018 07:02:26 +0000 (+0900) Subject: srt: Remove duplicated code for setting server socket X-Git-Tag: 1.19.3~507^2~4041 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=82467a49b9a10eca7ccba0238fadabb0fac19585;p=platform%2Fupstream%2Fgstreamer.git srt: Remove duplicated code for setting server socket https://bugzilla.gnome.org/show_bug.cgi?id=796842 --- diff --git a/ext/srt/gstsrt.c b/ext/srt/gstsrt.c index bdf21b99f6..53c8f51e23 100644 --- a/ext/srt/gstsrt.c +++ b/ext/srt/gstsrt.c @@ -165,6 +165,111 @@ failed: 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) { diff --git a/ext/srt/gstsrt.h b/ext/srt/gstsrt.h index 8d58a71fcf..2b0381e175 100644 --- a/ext/srt/gstsrt.h +++ b/ext/srt/gstsrt.h @@ -43,6 +43,11 @@ gst_srt_client_connect (GstElement * elem, int sender, 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 diff --git a/ext/srt/gstsrtserversink.c b/ext/srt/gstsrtserversink.c index 6ef8af45ba..66cf919165 100644 --- a/ext/srt/gstsrtserversink.c +++ b/ext/srt/gstsrtserversink.c @@ -264,13 +264,9 @@ gst_srt_server_sink_start (GstBaseSink * sink) 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"))); @@ -278,76 +274,13 @@ gst_srt_server_sink_start (GstBaseSink * sink) } 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; } @@ -369,7 +302,6 @@ gst_srt_server_sink_start (GstBaseSink * sink) } g_clear_pointer (&uri, gst_uri_unref); - g_clear_object (&socket_address); return ret; @@ -386,7 +318,6 @@ failed: g_clear_error (&error); g_clear_pointer (&uri, gst_uri_unref); - g_clear_object (&socket_address); return FALSE; } diff --git a/ext/srt/gstsrtserversrc.c b/ext/srt/gstsrtserversrc.c index cfb26ba0c7..05abc3abd8 100644 --- a/ext/srt/gstsrtserversrc.c +++ b/ext/srt/gstsrtserversrc.c @@ -163,10 +163,6 @@ gst_srt_server_src_fill (GstPushSrc * src, GstBuffer * outbuf) 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); @@ -265,12 +261,7 @@ gst_srt_server_src_start (GstBaseSrc * src) 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"))); @@ -278,80 +269,17 @@ gst_srt_server_src_start (GstBaseSrc * src) } 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; @@ -366,9 +294,7 @@ failed: priv->sock = SRT_ERROR; } - g_clear_error (&error); g_clear_pointer (&uri, gst_uri_unref); - g_clear_object (&socket_address); return FALSE; }