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>
32 #include <sys/ioctl.h>
34 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
35 #include <sys/filio.h>
38 GST_DEBUG_CATEGORY (tcpclientsrc_debug);
39 #define GST_CAT_DEFAULT tcpclientsrc_debug
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);
67 static void gst_tcpclientsrc_finalize (GObject * gobject);
69 static GstCaps *gst_tcpclientsrc_getcaps (GstPad * pad);
71 static GstData *gst_tcpclientsrc_get (GstPad * pad);
72 static GstElementStateReturn gst_tcpclientsrc_change_state (GstElement *
75 static void gst_tcpclientsrc_set_property (GObject * object, guint prop_id,
76 const GValue * value, GParamSpec * pspec);
77 static void gst_tcpclientsrc_get_property (GObject * object, guint prop_id,
78 GValue * value, GParamSpec * pspec);
79 static void gst_tcpclientsrc_set_clock (GstElement * element, GstClock * clock);
81 static GstElementClass *parent_class = NULL;
83 /*static guint gst_tcpclientsrc_signals[LAST_SIGNAL] = { 0 }; */
86 gst_tcpclientsrc_get_type (void)
88 static GType tcpclientsrc_type = 0;
91 if (!tcpclientsrc_type) {
92 static const GTypeInfo tcpclientsrc_info = {
93 sizeof (GstTCPClientSrcClass),
94 gst_tcpclientsrc_base_init,
96 (GClassInitFunc) gst_tcpclientsrc_class_init,
99 sizeof (GstTCPClientSrc),
101 (GInstanceInitFunc) gst_tcpclientsrc_init,
106 g_type_register_static (GST_TYPE_ELEMENT, "GstTCPClientSrc",
107 &tcpclientsrc_info, 0);
109 return tcpclientsrc_type;
113 gst_tcpclientsrc_base_init (gpointer g_class)
115 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
117 gst_element_class_set_details (element_class, &gst_tcpclientsrc_details);
121 gst_tcpclientsrc_class_init (GstTCPClientSrc * klass)
123 GObjectClass *gobject_class;
124 GstElementClass *gstelement_class;
126 gobject_class = (GObjectClass *) klass;
127 gstelement_class = (GstElementClass *) klass;
129 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
131 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_HOST,
132 g_param_spec_string ("host", "Host",
133 "The host IP address to receive packets from", TCP_DEFAULT_HOST,
135 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PORT,
136 g_param_spec_int ("port", "Port", "The port to receive packets from", 0,
137 TCP_HIGHEST_PORT, TCP_DEFAULT_PORT, G_PARAM_READWRITE));
138 g_object_class_install_property (gobject_class, ARG_PROTOCOL,
139 g_param_spec_enum ("protocol", "Protocol", "The protocol to wrap data in",
140 GST_TYPE_TCP_PROTOCOL_TYPE, GST_TCP_PROTOCOL_TYPE_NONE,
143 gobject_class->set_property = gst_tcpclientsrc_set_property;
144 gobject_class->get_property = gst_tcpclientsrc_get_property;
145 gobject_class->finalize = gst_tcpclientsrc_finalize;
147 gstelement_class->change_state = gst_tcpclientsrc_change_state;
148 gstelement_class->set_clock = gst_tcpclientsrc_set_clock;
150 GST_DEBUG_CATEGORY_INIT (tcpclientsrc_debug, "tcpclientsrc", 0,
151 "TCP Client Source");
155 gst_tcpclientsrc_set_clock (GstElement * element, GstClock * clock)
157 GstTCPClientSrc *tcpclientsrc;
159 tcpclientsrc = GST_TCPCLIENTSRC (element);
161 tcpclientsrc->clock = clock;
165 gst_tcpclientsrc_init (GstTCPClientSrc * this)
167 /* create the src pad */
168 this->srcpad = gst_pad_new ("src", GST_PAD_SRC);
169 gst_element_add_pad (GST_ELEMENT (this), this->srcpad);
170 gst_pad_set_get_function (this->srcpad, gst_tcpclientsrc_get);
171 gst_pad_set_getcaps_function (this->srcpad, gst_tcpclientsrc_getcaps);
173 this->port = TCP_DEFAULT_PORT;
174 this->host = g_strdup (TCP_DEFAULT_HOST);
177 this->protocol = GST_TCP_PROTOCOL_TYPE_NONE;
181 GST_FLAG_UNSET (this, GST_TCPCLIENTSRC_OPEN);
185 gst_tcpclientsrc_finalize (GObject * gobject)
187 GstTCPClientSrc *this = GST_TCPCLIENTSRC (gobject);
193 gst_tcpclientsrc_getcaps (GstPad * pad)
195 GstTCPClientSrc *src;
196 GstCaps *caps = NULL;
198 src = GST_TCPCLIENTSRC (GST_OBJECT_PARENT (pad));
200 if (!GST_FLAG_IS_SET (src, GST_TCPCLIENTSRC_OPEN))
201 caps = gst_caps_new_any ();
203 caps = gst_caps_copy (src->caps);
205 caps = gst_caps_new_any ();
206 GST_DEBUG_OBJECT (src, "returning caps %" GST_PTR_FORMAT, caps);
207 g_assert (GST_IS_CAPS (caps));
211 /* close the socket and associated resources
213 * used both to recover from errors and go to NULL state */
215 gst_tcpclientsrc_close (GstTCPClientSrc * this)
217 GST_DEBUG_OBJECT (this, "closing socket");
218 if (this->sock_fd != -1) {
219 close (this->sock_fd);
222 this->caps_received = FALSE;
224 gst_caps_free (this->caps);
227 GST_FLAG_UNSET (this, GST_TCPCLIENTSRC_OPEN);
230 /* close socket and related items and return an EOS GstData
231 * called from _get */
233 gst_tcpclientsrc_eos (GstTCPClientSrc * src)
235 GST_DEBUG_OBJECT (src, "going to EOS");
236 gst_element_set_eos (GST_ELEMENT (src));
237 gst_tcpclientsrc_close (src);
238 return GST_DATA (gst_event_new (GST_EVENT_EOS));
242 gst_tcpclientsrc_get (GstPad * pad)
244 GstTCPClientSrc *src;
248 GstData *data = NULL;
249 GstBuffer *buf = NULL;
251 g_return_val_if_fail (pad != NULL, NULL);
252 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
253 src = GST_TCPCLIENTSRC (GST_OBJECT_PARENT (pad));
254 if (!GST_FLAG_IS_SET (src, GST_TCPCLIENTSRC_OPEN)) {
255 GST_DEBUG_OBJECT (src, "connection to server closed, cannot give data");
258 GST_LOG_OBJECT (src, "asked for a buffer");
260 /* try to negotiate here */
261 if (!gst_pad_is_negotiated (pad)) {
262 if (GST_PAD_LINK_FAILED (gst_pad_renegotiate (pad))) {
263 GST_ELEMENT_ERROR (src, CORE, NEGOTIATION, (NULL), GST_ERROR_SYSTEM);
264 gst_buffer_unref (buf);
265 return gst_tcpclientsrc_eos (src);
269 /* if we have a left over buffer after a discont, return that */
270 if (src->buffer_after_discont) {
271 buf = src->buffer_after_discont;
273 "Returning buffer after discont of size %d, ts %"
274 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT
275 ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
276 GST_BUFFER_SIZE (buf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
277 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
278 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf));
279 src->buffer_after_discont = NULL;
280 return GST_DATA (buf);
283 /* read the buffer header if we're using a protocol */
284 switch (src->protocol) {
287 case GST_TCP_PROTOCOL_TYPE_NONE:
288 /* do a blocking select on the socket */
290 FD_SET (src->sock_fd, &testfds);
291 ret = select (src->sock_fd + 1, &testfds, (fd_set *) 0, (fd_set *) 0, 0);
292 /* no action (0) is an error too in our case */
294 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
295 ("select failed: %s", g_strerror (errno)));
296 return gst_tcpclientsrc_eos (src);
299 /* ask how much is available for reading on the socket */
300 ret = ioctl (src->sock_fd, FIONREAD, &readsize);
302 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
303 ("ioctl failed: %s", g_strerror (errno)));
304 return gst_tcpclientsrc_eos (src);
306 GST_LOG_OBJECT (src, "ioctl says %d bytes available", readsize);
307 buf = gst_buffer_new_and_alloc (readsize);
309 case GST_TCP_PROTOCOL_TYPE_GDP:
310 if (!(data = gst_tcp_gdp_read_header (GST_ELEMENT (src), src->sock_fd))) {
311 return gst_tcpclientsrc_eos (src);
313 if (GST_IS_EVENT (data)) {
314 /* if we got back an EOS event, then we should go into eos ourselves */
315 if (GST_EVENT_TYPE (data) == GST_EVENT_EOS) {
316 gst_event_unref (data);
317 return gst_tcpclientsrc_eos (src);
322 buf = GST_BUFFER (data);
324 GST_LOG_OBJECT (src, "Going to read data from socket into buffer %p",
326 /* use this new buffer to read data into */
327 readsize = GST_BUFFER_SIZE (buf);
330 g_warning ("Unhandled protocol type");
334 GST_LOG_OBJECT (src, "Reading %d bytes into buffer", readsize);
335 ret = gst_tcp_socket_read (src->sock_fd, GST_BUFFER_DATA (buf), readsize);
337 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
338 gst_buffer_unref (buf);
339 return gst_tcpclientsrc_eos (src);
342 /* if we read 0 bytes, and we're blocking, we hit eos */
344 GST_DEBUG_OBJECT (src, "blocking read returns 0, EOS");
345 gst_buffer_unref (buf);
346 return gst_tcpclientsrc_eos (src);
350 GST_BUFFER_SIZE (buf) = readsize;
351 GST_BUFFER_MAXSIZE (buf) = readsize;
353 /* FIXME: we could decide to set OFFSET and OFFSET_END for non-protocol
354 * streams to mean the bytes processed */
356 /* if this is our first buffer, we need to send a discont with the
357 * given timestamp or the current offset, and store the buffer for
358 * the next iteration through the get loop */
359 if (src->send_discont) {
360 GstClockTime timestamp;
363 src->send_discont = FALSE;
364 src->buffer_after_discont = buf;
365 /* if the timestamp is valid, send a timed discont
366 * taking into account the incoming buffer's timestamps */
367 timestamp = GST_BUFFER_TIMESTAMP (buf);
368 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
369 GST_DEBUG_OBJECT (src,
370 "sending discontinuous with timestamp %" GST_TIME_FORMAT,
371 GST_TIME_ARGS (timestamp));
373 gst_event_new_discontinuous (FALSE, GST_FORMAT_TIME, timestamp, NULL);
374 return GST_DATA (event);
376 /* otherwise, send an offset discont */
377 GST_DEBUG_OBJECT (src, "sending discontinuous with offset %d",
380 gst_event_new_discontinuous (FALSE, GST_FORMAT_BYTES, src->curoffset,
382 return GST_DATA (event);
385 src->curoffset += readsize;
387 "Returning buffer from _get of size %d, ts %"
388 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT
389 ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
390 GST_BUFFER_SIZE (buf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
391 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
392 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf));
393 return GST_DATA (buf);
397 gst_tcpclientsrc_set_property (GObject * object, guint prop_id,
398 const GValue * value, GParamSpec * pspec)
400 GstTCPClientSrc *tcpclientsrc;
402 /* it's not null if we got it, but it might not be ours */
403 g_return_if_fail (GST_IS_TCPCLIENTSRC (object));
404 tcpclientsrc = GST_TCPCLIENTSRC (object);
408 if (!g_value_get_string (value)) {
409 g_warning ("host property cannot be NULL");
412 g_free (tcpclientsrc->host);
413 tcpclientsrc->host = g_strdup (g_value_get_string (value));
416 tcpclientsrc->port = g_value_get_int (value);
419 tcpclientsrc->protocol = g_value_get_enum (value);
423 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
429 gst_tcpclientsrc_get_property (GObject * object, guint prop_id, GValue * value,
432 GstTCPClientSrc *tcpclientsrc;
434 g_return_if_fail (GST_IS_TCPCLIENTSRC (object));
435 tcpclientsrc = GST_TCPCLIENTSRC (object);
439 g_value_set_string (value, tcpclientsrc->host);
442 g_value_set_int (value, tcpclientsrc->port);
445 g_value_set_enum (value, tcpclientsrc->protocol);
449 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
454 /* create a socket for connecting to remote server */
456 gst_tcpclientsrc_init_receive (GstTCPClientSrc * this)
461 /* create receiving client socket */
462 GST_DEBUG_OBJECT (this, "opening receiving client socket to %s:%d",
463 this->host, this->port);
464 if ((this->sock_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1) {
465 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
468 GST_DEBUG_OBJECT (this, "opened receiving client socket with fd %d",
470 GST_FLAG_SET (this, GST_TCPCLIENTSRC_OPEN);
472 /* look up name if we need to */
473 ip = gst_tcp_host_to_ip (GST_ELEMENT (this), this->host);
475 gst_tcpclientsrc_close (this);
478 GST_DEBUG_OBJECT (this, "IP address for host %s is %s", this->host, ip);
480 /* connect to server */
481 memset (&this->server_sin, 0, sizeof (this->server_sin));
482 this->server_sin.sin_family = AF_INET; /* network socket */
483 this->server_sin.sin_port = htons (this->port); /* on port */
484 this->server_sin.sin_addr.s_addr = inet_addr (ip); /* on host ip */
487 GST_DEBUG_OBJECT (this, "connecting to server");
488 ret = connect (this->sock_fd, (struct sockaddr *) &this->server_sin,
489 sizeof (this->server_sin));
492 gst_tcpclientsrc_close (this);
495 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ,
496 (_("Connection to %s:%d refused."), this->host, this->port),
501 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL),
502 ("connect to %s:%d failed: %s", this->host, this->port,
503 g_strerror (errno)));
509 this->send_discont = TRUE;
510 this->buffer_after_discont = NULL;
512 /* get the caps if we're using GDP */
513 if (this->protocol == GST_TCP_PROTOCOL_TYPE_GDP) {
514 /* if we haven't received caps yet, we should get them first */
515 if (!this->caps_received) {
518 GST_DEBUG_OBJECT (this, "getting caps through GDP");
519 if (!(caps = gst_tcp_gdp_read_caps (GST_ELEMENT (this), this->sock_fd))) {
520 gst_tcpclientsrc_close (this);
521 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
522 ("Could not read caps through GDP"));
525 if (!GST_IS_CAPS (caps)) {
526 gst_tcpclientsrc_close (this);
527 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
528 ("Could not read caps through GDP"));
531 GST_DEBUG_OBJECT (this, "Received caps through GDP: %" GST_PTR_FORMAT,
533 this->caps_received = TRUE;
540 static GstElementStateReturn
541 gst_tcpclientsrc_change_state (GstElement * element)
543 g_return_val_if_fail (GST_IS_TCPCLIENTSRC (element), GST_STATE_FAILURE);
545 /* if open and going to NULL, close it */
546 if (GST_FLAG_IS_SET (element, GST_TCPCLIENTSRC_OPEN) &&
547 GST_STATE_PENDING (element) == GST_STATE_NULL) {
548 gst_tcpclientsrc_close (GST_TCPCLIENTSRC (element));
550 /* if closed and going to a state higher than NULL, open it */
551 if (!GST_FLAG_IS_SET (element, GST_TCPCLIENTSRC_OPEN) &&
552 GST_STATE_PENDING (element) > GST_STATE_NULL) {
553 if (!gst_tcpclientsrc_init_receive (GST_TCPCLIENTSRC (element)))
554 return GST_STATE_FAILURE;
557 if (GST_ELEMENT_CLASS (parent_class)->change_state)
558 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
560 return GST_STATE_SUCCESS;