2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:element-tcpclientsrc
23 * @see_also: #tcpclientsink
26 * <title>Example launch line</title>
31 * gst-launch tcpclientsrc protocol=none port=3000 ! fdsink fd=2
32 * ]| everything you type in the server is shown on the client
40 #include <gst/gst-i18n-plugin.h>
42 #include "gsttcpclientsrc.h"
43 #include <string.h> /* memset */
45 #include <arpa/inet.h>
49 GST_DEBUG_CATEGORY_STATIC (tcpclientsrc_debug);
50 #define GST_CAT_DEFAULT tcpclientsrc_debug
52 #define MAX_READ_SIZE 4 * 1024
55 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
70 GST_BOILERPLATE (GstTCPClientSrc, gst_tcp_client_src, GstPushSrc,
74 static void gst_tcp_client_src_finalize (GObject * gobject);
76 static GstCaps *gst_tcp_client_src_getcaps (GstBaseSrc * psrc);
78 static GstFlowReturn gst_tcp_client_src_create (GstPushSrc * psrc,
80 static gboolean gst_tcp_client_src_stop (GstBaseSrc * bsrc);
81 static gboolean gst_tcp_client_src_start (GstBaseSrc * bsrc);
82 static gboolean gst_tcp_client_src_unlock (GstBaseSrc * bsrc);
83 static gboolean gst_tcp_client_src_unlock_stop (GstBaseSrc * bsrc);
85 static void gst_tcp_client_src_set_property (GObject * object, guint prop_id,
86 const GValue * value, GParamSpec * pspec);
87 static void gst_tcp_client_src_get_property (GObject * object, guint prop_id,
88 GValue * value, GParamSpec * pspec);
92 gst_tcp_client_src_base_init (gpointer g_class)
94 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
96 gst_element_class_add_static_pad_template (element_class, &srctemplate);
98 gst_element_class_set_details_simple (element_class,
99 "TCP client source", "Source/Network",
100 "Receive data as a client over the network via TCP",
101 "Thomas Vander Stichele <thomas at apestaart dot org>");
105 gst_tcp_client_src_class_init (GstTCPClientSrcClass * klass)
107 GObjectClass *gobject_class;
108 GstBaseSrcClass *gstbasesrc_class;
109 GstPushSrcClass *gstpush_src_class;
111 gobject_class = (GObjectClass *) klass;
112 gstbasesrc_class = (GstBaseSrcClass *) klass;
113 gstpush_src_class = (GstPushSrcClass *) klass;
115 gobject_class->set_property = gst_tcp_client_src_set_property;
116 gobject_class->get_property = gst_tcp_client_src_get_property;
117 gobject_class->finalize = gst_tcp_client_src_finalize;
119 g_object_class_install_property (gobject_class, PROP_HOST,
120 g_param_spec_string ("host", "Host",
121 "The host IP address to receive packets from", TCP_DEFAULT_HOST,
122 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
123 g_object_class_install_property (gobject_class, PROP_PORT,
124 g_param_spec_int ("port", "Port", "The port to receive packets from", 0,
125 TCP_HIGHEST_PORT, TCP_DEFAULT_PORT,
126 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
127 g_object_class_install_property (gobject_class, PROP_PROTOCOL,
128 g_param_spec_enum ("protocol", "Protocol", "The protocol to wrap data in",
129 GST_TYPE_TCP_PROTOCOL, GST_TCP_PROTOCOL_NONE,
130 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
132 gstbasesrc_class->get_caps = gst_tcp_client_src_getcaps;
133 gstbasesrc_class->start = gst_tcp_client_src_start;
134 gstbasesrc_class->stop = gst_tcp_client_src_stop;
135 gstbasesrc_class->unlock = gst_tcp_client_src_unlock;
136 gstbasesrc_class->unlock_stop = gst_tcp_client_src_unlock_stop;
138 gstpush_src_class->create = gst_tcp_client_src_create;
140 GST_DEBUG_CATEGORY_INIT (tcpclientsrc_debug, "tcpclientsrc", 0,
141 "TCP Client Source");
145 gst_tcp_client_src_init (GstTCPClientSrc * this, GstTCPClientSrcClass * g_class)
147 this->port = TCP_DEFAULT_PORT;
148 this->host = g_strdup (TCP_DEFAULT_HOST);
149 this->sock_fd.fd = -1;
150 this->protocol = GST_TCP_PROTOCOL_NONE;
153 GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SRC_OPEN);
157 gst_tcp_client_src_finalize (GObject * gobject)
159 GstTCPClientSrc *this = GST_TCP_CLIENT_SRC (gobject);
163 G_OBJECT_CLASS (parent_class)->finalize (gobject);
167 gst_tcp_client_src_getcaps (GstBaseSrc * bsrc)
169 GstTCPClientSrc *src;
170 GstCaps *caps = NULL;
172 src = GST_TCP_CLIENT_SRC (bsrc);
174 if (!GST_OBJECT_FLAG_IS_SET (src, GST_TCP_CLIENT_SRC_OPEN))
175 caps = gst_caps_new_any ();
177 caps = gst_caps_copy (src->caps);
179 caps = gst_caps_new_any ();
180 GST_DEBUG_OBJECT (src, "returning caps %" GST_PTR_FORMAT, caps);
181 g_assert (GST_IS_CAPS (caps));
186 gst_tcp_client_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
188 GstTCPClientSrc *src;
189 GstFlowReturn ret = GST_FLOW_OK;
191 src = GST_TCP_CLIENT_SRC (psrc);
193 if (!GST_OBJECT_FLAG_IS_SET (src, GST_TCP_CLIENT_SRC_OPEN))
196 GST_LOG_OBJECT (src, "asked for a buffer");
198 /* read the buffer header if we're using a protocol */
199 switch (src->protocol) {
200 case GST_TCP_PROTOCOL_NONE:
201 ret = gst_tcp_read_buffer (GST_ELEMENT (src), src->sock_fd.fd,
205 case GST_TCP_PROTOCOL_GDP:
206 /* get the caps if we're using GDP */
207 if (!src->caps_received) {
210 GST_DEBUG_OBJECT (src, "getting caps through GDP");
211 ret = gst_tcp_gdp_read_caps (GST_ELEMENT (src), src->sock_fd.fd,
214 if (ret != GST_FLOW_OK)
217 src->caps_received = TRUE;
221 ret = gst_tcp_gdp_read_buffer (GST_ELEMENT (src), src->sock_fd.fd,
225 /* need to assert as buf == NULL */
226 g_assert ("Unhandled protocol type");
230 if (ret == GST_FLOW_OK) {
232 "Returning buffer from _get of size %d, ts %"
233 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT
234 ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
235 GST_BUFFER_SIZE (*outbuf),
236 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*outbuf)),
237 GST_TIME_ARGS (GST_BUFFER_DURATION (*outbuf)),
238 GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf));
240 gst_buffer_set_caps (*outbuf, src->caps);
247 GST_DEBUG_OBJECT (src, "connection to closed, cannot read data");
248 return GST_FLOW_WRONG_STATE;
252 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
253 ("Could not read caps through GDP"));
259 gst_tcp_client_src_set_property (GObject * object, guint prop_id,
260 const GValue * value, GParamSpec * pspec)
262 GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
266 if (!g_value_get_string (value)) {
267 g_warning ("host property cannot be NULL");
270 g_free (tcpclientsrc->host);
271 tcpclientsrc->host = g_strdup (g_value_get_string (value));
274 tcpclientsrc->port = g_value_get_int (value);
277 tcpclientsrc->protocol = g_value_get_enum (value);
281 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
287 gst_tcp_client_src_get_property (GObject * object, guint prop_id,
288 GValue * value, GParamSpec * pspec)
290 GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
294 g_value_set_string (value, tcpclientsrc->host);
297 g_value_set_int (value, tcpclientsrc->port);
300 g_value_set_enum (value, tcpclientsrc->protocol);
304 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
309 /* create a socket for connecting to remote server */
311 gst_tcp_client_src_start (GstBaseSrc * bsrc)
315 GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
317 if ((src->fdset = gst_poll_new (TRUE)) == NULL)
320 /* create receiving client socket */
321 GST_DEBUG_OBJECT (src, "opening receiving client socket to %s:%d",
322 src->host, src->port);
324 if ((src->sock_fd.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
327 GST_DEBUG_OBJECT (src, "opened receiving client socket with fd %d",
329 GST_OBJECT_FLAG_SET (src, GST_TCP_CLIENT_SRC_OPEN);
331 /* look up name if we need to */
332 if (!(ip = gst_tcp_host_to_ip (GST_ELEMENT (src), src->host)))
335 GST_DEBUG_OBJECT (src, "IP address for host %s is %s", src->host, ip);
337 /* connect to server */
338 memset (&src->server_sin, 0, sizeof (src->server_sin));
339 src->server_sin.sin_family = AF_INET; /* network socket */
340 src->server_sin.sin_port = htons (src->port); /* on port */
341 src->server_sin.sin_addr.s_addr = inet_addr (ip); /* on host ip */
344 GST_DEBUG_OBJECT (src, "connecting to server");
345 ret = connect (src->sock_fd.fd, (struct sockaddr *) &src->server_sin,
346 sizeof (src->server_sin));
350 /* add the socket to the poll */
351 gst_poll_add_fd (src->fdset, &src->sock_fd);
352 gst_poll_fd_ctl_read (src->fdset, &src->sock_fd, TRUE);
358 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
364 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
369 gst_tcp_client_src_stop (GST_BASE_SRC (src));
374 gst_tcp_client_src_stop (GST_BASE_SRC (src));
377 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
378 (_("Connection to %s:%d refused."), src->host, src->port), (NULL));
381 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
382 ("connect to %s:%d failed: %s", src->host, src->port,
383 g_strerror (errno)));
390 /* close the socket and associated resources
392 * used both to recover from errors and go to NULL state */
394 gst_tcp_client_src_stop (GstBaseSrc * bsrc)
396 GstTCPClientSrc *src;
398 src = GST_TCP_CLIENT_SRC (bsrc);
400 GST_DEBUG_OBJECT (src, "closing socket");
402 if (src->fdset != NULL) {
403 gst_poll_free (src->fdset);
407 gst_tcp_socket_close (&src->sock_fd);
408 src->caps_received = FALSE;
410 gst_caps_unref (src->caps);
413 GST_OBJECT_FLAG_UNSET (src, GST_TCP_CLIENT_SRC_OPEN);
418 /* will be called only between calls to start() and stop() */
420 gst_tcp_client_src_unlock (GstBaseSrc * bsrc)
422 GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
424 GST_DEBUG_OBJECT (src, "set to flushing");
425 gst_poll_set_flushing (src->fdset, TRUE);
430 /* will be called only between calls to start() and stop() */
432 gst_tcp_client_src_unlock_stop (GstBaseSrc * bsrc)
434 GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
436 GST_DEBUG_OBJECT (src, "unset flushing");
437 gst_poll_set_flushing (src->fdset, FALSE);