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 <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <sys/ioctl.h>
36 GST_DEBUG_CATEGORY (tcpclientsrc_debug);
37 #define GST_CAT_DEFAULT tcpclientsrc_debug
39 #define TCP_DEFAULT_PORT 4953
40 #define TCP_DEFAULT_HOST "localhost"
41 #define MAX_READ_SIZE 4 * 1024
43 /* elementfactory information */
44 static GstElementDetails gst_tcpclientsrc_details =
45 GST_ELEMENT_DETAILS ("TCP Client source",
47 "Receive data as a client over the network via TCP",
48 "Thomas Vander Stichele <thomas at apestaart dot org>");
50 /* TCPClientSrc signals and args */
64 static void gst_tcpclientsrc_base_init (gpointer g_class);
65 static void gst_tcpclientsrc_class_init (GstTCPClientSrc * klass);
66 static void gst_tcpclientsrc_init (GstTCPClientSrc * tcpclientsrc);
68 static GstData *gst_tcpclientsrc_get (GstPad * pad);
69 static GstElementStateReturn gst_tcpclientsrc_change_state (GstElement *
72 static void gst_tcpclientsrc_set_property (GObject * object, guint prop_id,
73 const GValue * value, GParamSpec * pspec);
74 static void gst_tcpclientsrc_get_property (GObject * object, guint prop_id,
75 GValue * value, GParamSpec * pspec);
76 static void gst_tcpclientsrc_set_clock (GstElement * element, GstClock * clock);
78 static GstElementClass *parent_class = NULL;
80 /*static guint gst_tcpclientsrc_signals[LAST_SIGNAL] = { 0 }; */
83 gst_tcpclientsrc_get_type (void)
85 static GType tcpclientsrc_type = 0;
88 if (!tcpclientsrc_type) {
89 static const GTypeInfo tcpclientsrc_info = {
90 sizeof (GstTCPClientSrcClass),
91 gst_tcpclientsrc_base_init,
93 (GClassInitFunc) gst_tcpclientsrc_class_init,
96 sizeof (GstTCPClientSrc),
98 (GInstanceInitFunc) gst_tcpclientsrc_init,
103 g_type_register_static (GST_TYPE_ELEMENT, "GstTCPClientSrc",
104 &tcpclientsrc_info, 0);
106 return tcpclientsrc_type;
110 gst_tcpclientsrc_base_init (gpointer g_class)
112 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
114 gst_element_class_set_details (element_class, &gst_tcpclientsrc_details);
118 gst_tcpclientsrc_class_init (GstTCPClientSrc * klass)
120 GObjectClass *gobject_class;
121 GstElementClass *gstelement_class;
123 gobject_class = (GObjectClass *) klass;
124 gstelement_class = (GstElementClass *) klass;
126 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
128 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_HOST,
129 g_param_spec_string ("host", "Host",
130 "The host IP address to receive packets from", TCP_DEFAULT_HOST,
132 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PORT,
133 g_param_spec_int ("port", "Port", "The port to receive packets from", 0,
134 32768, TCP_DEFAULT_PORT, G_PARAM_READWRITE));
135 g_object_class_install_property (gobject_class, ARG_PROTOCOL,
136 g_param_spec_enum ("protocol", "Protocol", "The protocol to wrap data in",
137 GST_TYPE_TCP_PROTOCOL_TYPE, GST_TCP_PROTOCOL_TYPE_GDP,
140 gobject_class->set_property = gst_tcpclientsrc_set_property;
141 gobject_class->get_property = gst_tcpclientsrc_get_property;
143 gstelement_class->change_state = gst_tcpclientsrc_change_state;
144 gstelement_class->set_clock = gst_tcpclientsrc_set_clock;
146 GST_DEBUG_CATEGORY_INIT (tcpclientsrc_debug, "tcpclientsrc", 0,
147 "TCP Client Source");
151 gst_tcpclientsrc_set_clock (GstElement * element, GstClock * clock)
153 GstTCPClientSrc *tcpclientsrc;
155 tcpclientsrc = GST_TCPCLIENTSRC (element);
157 tcpclientsrc->clock = clock;
161 gst_tcpclientsrc_init (GstTCPClientSrc * this)
163 /* create the src pad */
164 this->srcpad = gst_pad_new ("src", GST_PAD_SRC);
165 gst_element_add_pad (GST_ELEMENT (this), this->srcpad);
166 gst_pad_set_get_function (this->srcpad, gst_tcpclientsrc_get);
168 this->port = TCP_DEFAULT_PORT;
169 this->host = g_strdup (TCP_DEFAULT_HOST);
172 this->protocol = GST_TCP_PROTOCOL_TYPE_GDP;
175 GST_FLAG_UNSET (this, GST_TCPCLIENTSRC_OPEN);
179 gst_tcpclientsrc_get (GstPad * pad)
181 GstTCPClientSrc *src;
185 GstData *data = NULL;
186 GstBuffer *buf = NULL;
189 g_return_val_if_fail (pad != NULL, NULL);
190 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
191 src = GST_TCPCLIENTSRC (GST_OBJECT_PARENT (pad));
192 g_return_val_if_fail (GST_FLAG_IS_SET (src, GST_TCPCLIENTSRC_OPEN), NULL);
194 /* if we have a left over buffer after a discont, return that */
195 if (src->buffer_after_discont) {
196 buf = src->buffer_after_discont;
198 "Returning buffer after discont of size %d with timestamp %"
199 GST_TIME_FORMAT " and duration %" GST_TIME_FORMAT,
200 GST_BUFFER_SIZE (buf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
201 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
202 src->buffer_after_discont = NULL;
203 return GST_DATA (buf);
206 /* read the buffer header if we're using a protocol */
207 switch (src->protocol) {
210 case GST_TCP_PROTOCOL_TYPE_NONE:
211 /* do a blocking select on the socket */
213 FD_SET (src->sock_fd, &testfds);
214 ret = select (src->sock_fd + 1, &testfds, (fd_set *) 0, (fd_set *) 0, 0);
215 /* no action (0) is an error too in our case */
217 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
218 ("select failed: %s", g_strerror (errno)));
222 /* ask how much is available for reading on the socket */
223 ret = ioctl (src->sock_fd, FIONREAD, &readsize);
225 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
226 ("ioctl failed: %s", g_strerror (errno)));
229 buf = gst_buffer_new_and_alloc (readsize);
231 case GST_TCP_PROTOCOL_TYPE_GDP:
232 /* if we haven't received caps yet, we should get them first */
233 if (!src->caps_received) {
236 if (!(caps = gst_tcp_gdp_read_caps (GST_ELEMENT (src), src->sock_fd))) {
237 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
238 ("Could not read caps through GDP"));
241 src->caps_received = TRUE;
242 string = gst_caps_to_string (caps);
243 GST_DEBUG_OBJECT (src, "Received caps through GDP: %s", string);
246 if (!gst_pad_try_set_caps (pad, caps)) {
247 g_warning ("Could not set caps");
252 /* now receive the buffer header */
253 if (!(data = gst_tcp_gdp_read_header (GST_ELEMENT (src), src->sock_fd))) {
254 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
255 ("Could not read data header through GDP"));
258 if (GST_IS_EVENT (data))
260 buf = GST_BUFFER (data);
262 GST_LOG_OBJECT (src, "Going to read data from socket into buffer %p",
264 /* use this new buffer to read data into */
265 readsize = GST_BUFFER_SIZE (buf);
268 g_warning ("Unhandled protocol type");
272 GST_LOG_OBJECT (src, "Reading %d bytes", readsize);
273 ret = gst_tcp_socket_read (src->sock_fd, GST_BUFFER_DATA (buf), readsize);
275 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
279 /* if we read 0 bytes, and we're blocking, we hit eos */
281 GST_DEBUG ("blocking read returns 0, EOS");
282 gst_buffer_unref (buf);
283 gst_element_set_eos (GST_ELEMENT (src));
284 return GST_DATA (gst_event_new (GST_EVENT_EOS));
288 GST_BUFFER_SIZE (buf) = readsize;
289 GST_BUFFER_MAXSIZE (buf) = readsize;
290 GST_BUFFER_OFFSET (buf) = src->curoffset;
291 GST_BUFFER_OFFSET_END (buf) = src->curoffset + readsize;
293 /* if this is our first buffer, we need to send a discont with the
294 * given timestamp or the current offset, and store the buffer for
295 * the next iteration through the get loop */
296 if (src->send_discont) {
297 GstClockTime timestamp;
300 src->send_discont = FALSE;
301 src->buffer_after_discont = buf;
302 /* if the timestamp is valid, send a timed discont
303 * taking into account the incoming buffer's timestamps */
304 timestamp = GST_BUFFER_TIMESTAMP (buf);
305 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
306 GST_DEBUG_OBJECT (src,
307 "sending discontinuous with timestamp %" GST_TIME_FORMAT,
308 GST_TIME_ARGS (timestamp));
310 gst_event_new_discontinuous (FALSE, GST_FORMAT_TIME, timestamp, NULL);
311 return GST_DATA (event);
313 /* otherwise, send an offset discont */
314 GST_DEBUG_OBJECT (src, "sending discontinuous with offset %d",
317 gst_event_new_discontinuous (FALSE, GST_FORMAT_BYTES, src->curoffset,
319 return GST_DATA (event);
322 src->curoffset += readsize;
324 "Returning buffer of size %d with timestamp %" GST_TIME_FORMAT
325 " and duration %" GST_TIME_FORMAT, readsize,
326 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
327 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
328 return GST_DATA (buf);
333 gst_tcpclientsrc_set_property (GObject * object, guint prop_id,
334 const GValue * value, GParamSpec * pspec)
336 GstTCPClientSrc *tcpclientsrc;
338 /* it's not null if we got it, but it might not be ours */
339 g_return_if_fail (GST_IS_TCPCLIENTSRC (object));
340 tcpclientsrc = GST_TCPCLIENTSRC (object);
344 tcpclientsrc->port = g_value_get_int (value);
347 /* FIXME: create a setter and handle changes correctly */
348 g_free (tcpclientsrc->host);
349 tcpclientsrc->host = g_strdup (g_value_get_string (value));
352 tcpclientsrc->protocol = g_value_get_enum (value);
355 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
361 gst_tcpclientsrc_get_property (GObject * object, guint prop_id, GValue * value,
364 GstTCPClientSrc *tcpclientsrc;
366 g_return_if_fail (GST_IS_TCPCLIENTSRC (object));
367 tcpclientsrc = GST_TCPCLIENTSRC (object);
371 g_value_set_int (value, tcpclientsrc->port);
374 g_value_set_string (value, tcpclientsrc->host);
377 g_value_set_enum (value, tcpclientsrc->protocol);
381 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
386 /* create a socket for connecting to remote server */
388 gst_tcpclientsrc_init_receive (GstTCPClientSrc * this)
393 /* create receiving client socket */
394 GST_DEBUG_OBJECT (this, "opening receiving client socket to %s:%d",
395 this->host, this->port);
396 if ((this->sock_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1) {
397 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
400 GST_DEBUG_OBJECT (this, "opened receiving client socket with fd %d",
403 /* look up name if we need to */
404 ip = gst_tcp_host_to_ip (GST_ELEMENT (this), this->host);
407 GST_DEBUG_OBJECT (this, "IP address for host %s is %s", this->host, ip);
409 /* connect to server */
410 memset (&this->server_sin, 0, sizeof (this->server_sin));
411 this->server_sin.sin_family = AF_INET; /* network socket */
412 this->server_sin.sin_port = htons (this->port); /* on port */
413 this->server_sin.sin_addr.s_addr = inet_addr (ip); /* on host ip */
415 GST_DEBUG_OBJECT (this, "connecting to server");
416 ret = connect (this->sock_fd, (struct sockaddr *) &this->server_sin,
417 sizeof (this->server_sin));
422 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ,
423 (_("Connection to %s:%d refused."), this->host, this->port),
428 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL),
429 ("connect to %s:%d failed: %s", this->host, this->port,
430 g_strerror (errno)));
436 this->send_discont = TRUE;
437 this->buffer_after_discont = NULL;
438 GST_FLAG_SET (this, GST_TCPCLIENTSRC_OPEN);
444 gst_tcpclientsrc_close (GstTCPClientSrc * this)
446 if (this->sock_fd != -1) {
447 close (this->sock_fd);
450 GST_FLAG_UNSET (this, GST_TCPCLIENTSRC_OPEN);
453 static GstElementStateReturn
454 gst_tcpclientsrc_change_state (GstElement * element)
456 g_return_val_if_fail (GST_IS_TCPCLIENTSRC (element), GST_STATE_FAILURE);
458 if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
459 if (GST_FLAG_IS_SET (element, GST_TCPCLIENTSRC_OPEN))
460 gst_tcpclientsrc_close (GST_TCPCLIENTSRC (element));
462 if (!GST_FLAG_IS_SET (element, GST_TCPCLIENTSRC_OPEN)) {
463 if (!gst_tcpclientsrc_init_receive (GST_TCPCLIENTSRC (element)))
464 return GST_STATE_FAILURE;
468 if (GST_ELEMENT_CLASS (parent_class)->change_state)
469 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
471 return GST_STATE_SUCCESS;