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.
26 #include <gst/gst-i18n-plugin.h>
28 #include "gsttcpclientsrc.h"
29 #include <string.h> /* memset */
31 #include <arpa/inet.h>
35 /* control stuff stolen from fdsrc */
36 #define CONTROL_STOP 'S' /* stop the select call */
37 #define CONTROL_SOCKETS(o) o->control_fds
38 #define WRITE_SOCKET(o) o->control_fds[1]
39 #define READ_SOCKET(o) o->control_fds[0]
41 #define SEND_COMMAND(o, command) \
43 unsigned char c; c = command; \
44 write (WRITE_SOCKET(o), &c, 1); \
47 #define READ_COMMAND(o, command, res) \
49 res = read(READ_SOCKET(o), &command, 1); \
53 GST_DEBUG_CATEGORY (tcpclientsrc_debug);
54 #define GST_CAT_DEFAULT tcpclientsrc_debug
56 #define MAX_READ_SIZE 4 * 1024
59 static GstElementDetails gst_tcp_client_src_details =
60 GST_ELEMENT_DETAILS ("TCP Client source",
62 "Receive data as a client over the network via TCP",
63 "Thomas Vander Stichele <thomas at apestaart dot org>");
65 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
80 GST_BOILERPLATE (GstTCPClientSrc, gst_tcp_client_src, GstPushSrc,
84 static void gst_tcp_client_src_finalize (GObject * gobject);
86 static GstCaps *gst_tcp_client_src_getcaps (GstBaseSrc * psrc);
88 static GstFlowReturn gst_tcp_client_src_create (GstPushSrc * psrc,
90 static gboolean gst_tcp_client_src_stop (GstBaseSrc * bsrc);
91 static gboolean gst_tcp_client_src_start (GstBaseSrc * bsrc);
92 static gboolean gst_tcp_client_src_unlock (GstBaseSrc * bsrc);
94 static void gst_tcp_client_src_set_property (GObject * object, guint prop_id,
95 const GValue * value, GParamSpec * pspec);
96 static void gst_tcp_client_src_get_property (GObject * object, guint prop_id,
97 GValue * value, GParamSpec * pspec);
101 gst_tcp_client_src_base_init (gpointer g_class)
103 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
105 gst_element_class_add_pad_template (element_class,
106 gst_static_pad_template_get (&srctemplate));
108 gst_element_class_set_details (element_class, &gst_tcp_client_src_details);
112 gst_tcp_client_src_class_init (GstTCPClientSrcClass * klass)
114 GObjectClass *gobject_class;
115 GstBaseSrcClass *gstbasesrc_class;
116 GstPushSrcClass *gstpush_src_class;
118 gobject_class = (GObjectClass *) klass;
119 gstbasesrc_class = (GstBaseSrcClass *) klass;
120 gstpush_src_class = (GstPushSrcClass *) klass;
122 gobject_class->set_property = gst_tcp_client_src_set_property;
123 gobject_class->get_property = gst_tcp_client_src_get_property;
124 gobject_class->finalize = gst_tcp_client_src_finalize;
126 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HOST,
127 g_param_spec_string ("host", "Host",
128 "The host IP address to receive packets from", TCP_DEFAULT_HOST,
130 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT,
131 g_param_spec_int ("port", "Port", "The port to receive packets from", 0,
132 TCP_HIGHEST_PORT, TCP_DEFAULT_PORT, G_PARAM_READWRITE));
133 g_object_class_install_property (gobject_class, PROP_PROTOCOL,
134 g_param_spec_enum ("protocol", "Protocol", "The protocol to wrap data in",
135 GST_TYPE_TCP_PROTOCOL, GST_TCP_PROTOCOL_NONE, G_PARAM_READWRITE));
137 gstbasesrc_class->get_caps = gst_tcp_client_src_getcaps;
138 gstbasesrc_class->start = gst_tcp_client_src_start;
139 gstbasesrc_class->stop = gst_tcp_client_src_stop;
140 gstbasesrc_class->unlock = gst_tcp_client_src_unlock;
142 gstpush_src_class->create = gst_tcp_client_src_create;
144 GST_DEBUG_CATEGORY_INIT (tcpclientsrc_debug, "tcpclientsrc", 0,
145 "TCP Client Source");
149 gst_tcp_client_src_init (GstTCPClientSrc * this, GstTCPClientSrcClass * g_class)
151 this->port = TCP_DEFAULT_PORT;
152 this->host = g_strdup (TCP_DEFAULT_HOST);
154 this->protocol = GST_TCP_PROTOCOL_NONE;
157 READ_SOCKET (this) = -1;
158 WRITE_SOCKET (this) = -1;
160 gst_base_src_set_live (GST_BASE_SRC (this), TRUE);
162 GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SRC_OPEN);
166 gst_tcp_client_src_finalize (GObject * gobject)
168 GstTCPClientSrc *this = GST_TCP_CLIENT_SRC (gobject);
174 gst_tcp_client_src_getcaps (GstBaseSrc * bsrc)
176 GstTCPClientSrc *src;
177 GstCaps *caps = NULL;
179 src = GST_TCP_CLIENT_SRC (bsrc);
181 if (!GST_OBJECT_FLAG_IS_SET (src, GST_TCP_CLIENT_SRC_OPEN))
182 caps = gst_caps_new_any ();
184 caps = gst_caps_copy (src->caps);
186 caps = gst_caps_new_any ();
187 GST_DEBUG_OBJECT (src, "returning caps %" GST_PTR_FORMAT, caps);
188 g_assert (GST_IS_CAPS (caps));
193 gst_tcp_client_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
195 GstTCPClientSrc *src;
196 GstFlowReturn ret = GST_FLOW_OK;
198 src = GST_TCP_CLIENT_SRC (psrc);
200 if (!GST_OBJECT_FLAG_IS_SET (src, GST_TCP_CLIENT_SRC_OPEN))
203 GST_LOG_OBJECT (src, "asked for a buffer");
205 /* read the buffer header if we're using a protocol */
206 switch (src->protocol) {
207 case GST_TCP_PROTOCOL_NONE:
208 ret = gst_tcp_read_buffer (GST_ELEMENT (src), src->sock_fd,
209 READ_SOCKET (src), outbuf);
212 case GST_TCP_PROTOCOL_GDP:
213 /* get the caps if we're using GDP */
214 if (!src->caps_received) {
217 GST_DEBUG_OBJECT (src, "getting caps through GDP");
218 ret = gst_tcp_gdp_read_caps (GST_ELEMENT (src), src->sock_fd,
219 READ_SOCKET (src), &caps);
221 if (ret != GST_FLOW_OK)
224 src->caps_received = TRUE;
228 ret = gst_tcp_gdp_read_buffer (GST_ELEMENT (src), src->sock_fd,
229 READ_SOCKET (src), outbuf);
232 /* need to assert as buf == NULL */
233 g_assert ("Unhandled protocol type");
237 if (ret == GST_FLOW_OK) {
239 "Returning buffer from _get of size %d, ts %"
240 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT
241 ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
242 GST_BUFFER_SIZE (*outbuf),
243 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*outbuf)),
244 GST_TIME_ARGS (GST_BUFFER_DURATION (*outbuf)),
245 GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf));
247 gst_buffer_set_caps (*outbuf, src->caps);
254 GST_DEBUG_OBJECT (src, "connection to closed, cannot read data");
255 return GST_FLOW_WRONG_STATE;
259 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
260 ("Could not read caps through GDP"));
266 gst_tcp_client_src_set_property (GObject * object, guint prop_id,
267 const GValue * value, GParamSpec * pspec)
269 GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
273 if (!g_value_get_string (value)) {
274 g_warning ("host property cannot be NULL");
277 g_free (tcpclientsrc->host);
278 tcpclientsrc->host = g_strdup (g_value_get_string (value));
281 tcpclientsrc->port = g_value_get_int (value);
284 tcpclientsrc->protocol = g_value_get_enum (value);
288 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
294 gst_tcp_client_src_get_property (GObject * object, guint prop_id,
295 GValue * value, GParamSpec * pspec)
297 GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
301 g_value_set_string (value, tcpclientsrc->host);
304 g_value_set_int (value, tcpclientsrc->port);
307 g_value_set_enum (value, tcpclientsrc->protocol);
311 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
316 /* create a socket for connecting to remote server */
318 gst_tcp_client_src_start (GstBaseSrc * bsrc)
322 GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
324 /* create the control sockets before anything */
325 if (socketpair (PF_UNIX, SOCK_STREAM, 0, CONTROL_SOCKETS (src)) < 0)
328 fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK);
329 fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK);
331 /* create receiving client socket */
332 GST_DEBUG_OBJECT (src, "opening receiving client socket to %s:%d",
333 src->host, src->port);
335 if ((src->sock_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
338 GST_DEBUG_OBJECT (src, "opened receiving client socket with fd %d",
340 GST_OBJECT_FLAG_SET (src, GST_TCP_CLIENT_SRC_OPEN);
342 /* look up name if we need to */
343 if (!(ip = gst_tcp_host_to_ip (GST_ELEMENT (src), src->host)))
346 GST_DEBUG_OBJECT (src, "IP address for host %s is %s", src->host, ip);
348 /* connect to server */
349 memset (&src->server_sin, 0, sizeof (src->server_sin));
350 src->server_sin.sin_family = AF_INET; /* network socket */
351 src->server_sin.sin_port = htons (src->port); /* on port */
352 src->server_sin.sin_addr.s_addr = inet_addr (ip); /* on host ip */
355 GST_DEBUG_OBJECT (src, "connecting to server");
356 ret = connect (src->sock_fd, (struct sockaddr *) &src->server_sin,
357 sizeof (src->server_sin));
360 gst_tcp_client_src_stop (GST_BASE_SRC (src));
363 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
364 (_("Connection to %s:%d refused."), src->host, src->port), (NULL));
368 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
369 ("connect to %s:%d failed: %s", src->host, src->port,
370 g_strerror (errno)));
380 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
386 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
391 gst_tcp_client_src_stop (GST_BASE_SRC (src));
396 /* close the socket and associated resources
398 * used both to recover from errors and go to NULL state */
400 gst_tcp_client_src_stop (GstBaseSrc * bsrc)
402 GstTCPClientSrc *src;
404 src = GST_TCP_CLIENT_SRC (bsrc);
406 GST_DEBUG_OBJECT (src, "closing socket");
407 if (src->sock_fd != -1) {
408 close (src->sock_fd);
411 src->caps_received = FALSE;
413 gst_caps_unref (src->caps);
416 GST_OBJECT_FLAG_UNSET (src, GST_TCP_CLIENT_SRC_OPEN);
418 close (READ_SOCKET (src));
419 close (WRITE_SOCKET (src));
420 READ_SOCKET (src) = -1;
421 WRITE_SOCKET (src) = -1;
426 /* will be called only between calls to start() and stop() */
428 gst_tcp_client_src_unlock (GstBaseSrc * bsrc)
430 GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
432 SEND_COMMAND (src, CONTROL_STOP);