Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / gst / tcp / gsttcpclientsrc.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 /**
22  * SECTION:element-tcpclientsrc
23  * @see_also: #tcpclientsink
24  *
25  * <refsect2>
26  * <title>Example launch line</title>
27  * |[
28  * # server:
29  * nc -l -p 3000
30  * # client:
31  * gst-launch tcpclientsrc protocol=none port=3000 ! fdsink fd=2
32  * ]| everything you type in the server is shown on the client
33  * </refsect2>
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <gst/gst-i18n-plugin.h>
41 #include "gsttcp.h"
42 #include "gsttcpclientsrc.h"
43 #include <string.h>             /* memset */
44 #include <unistd.h>
45 #include <arpa/inet.h>
46 #include <fcntl.h>
47
48
49 GST_DEBUG_CATEGORY_STATIC (tcpclientsrc_debug);
50 #define GST_CAT_DEFAULT tcpclientsrc_debug
51
52 #define MAX_READ_SIZE                   4 * 1024
53
54
55 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
56     GST_PAD_SRC,
57     GST_PAD_ALWAYS,
58     GST_STATIC_CAPS_ANY);
59
60
61 enum
62 {
63   PROP_0,
64   PROP_HOST,
65   PROP_PORT,
66   PROP_PROTOCOL
67 };
68
69
70 GST_BOILERPLATE (GstTCPClientSrc, gst_tcp_client_src, GstPushSrc,
71     GST_TYPE_PUSH_SRC);
72
73
74 static void gst_tcp_client_src_finalize (GObject * gobject);
75
76 static GstCaps *gst_tcp_client_src_getcaps (GstBaseSrc * psrc);
77
78 static GstFlowReturn gst_tcp_client_src_create (GstPushSrc * psrc,
79     GstBuffer ** outbuf);
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);
84
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);
89
90
91 static void
92 gst_tcp_client_src_base_init (gpointer g_class)
93 {
94   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
95
96   gst_element_class_add_pad_template (element_class,
97       gst_static_pad_template_get (&srctemplate));
98
99   gst_element_class_set_details_simple (element_class,
100       "TCP client source", "Source/Network",
101       "Receive data as a client over the network via TCP",
102       "Thomas Vander Stichele <thomas at apestaart dot org>");
103 }
104
105 static void
106 gst_tcp_client_src_class_init (GstTCPClientSrcClass * klass)
107 {
108   GObjectClass *gobject_class;
109   GstBaseSrcClass *gstbasesrc_class;
110   GstPushSrcClass *gstpush_src_class;
111
112   gobject_class = (GObjectClass *) klass;
113   gstbasesrc_class = (GstBaseSrcClass *) klass;
114   gstpush_src_class = (GstPushSrcClass *) klass;
115
116   gobject_class->set_property = gst_tcp_client_src_set_property;
117   gobject_class->get_property = gst_tcp_client_src_get_property;
118   gobject_class->finalize = gst_tcp_client_src_finalize;
119
120   g_object_class_install_property (gobject_class, PROP_HOST,
121       g_param_spec_string ("host", "Host",
122           "The host IP address to receive packets from", TCP_DEFAULT_HOST,
123           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
124   g_object_class_install_property (gobject_class, PROP_PORT,
125       g_param_spec_int ("port", "Port", "The port to receive packets from", 0,
126           TCP_HIGHEST_PORT, TCP_DEFAULT_PORT,
127           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
128   g_object_class_install_property (gobject_class, PROP_PROTOCOL,
129       g_param_spec_enum ("protocol", "Protocol", "The protocol to wrap data in",
130           GST_TYPE_TCP_PROTOCOL, GST_TCP_PROTOCOL_NONE,
131           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
132
133   gstbasesrc_class->get_caps = gst_tcp_client_src_getcaps;
134   gstbasesrc_class->start = gst_tcp_client_src_start;
135   gstbasesrc_class->stop = gst_tcp_client_src_stop;
136   gstbasesrc_class->unlock = gst_tcp_client_src_unlock;
137   gstbasesrc_class->unlock_stop = gst_tcp_client_src_unlock_stop;
138
139   gstpush_src_class->create = gst_tcp_client_src_create;
140
141   GST_DEBUG_CATEGORY_INIT (tcpclientsrc_debug, "tcpclientsrc", 0,
142       "TCP Client Source");
143 }
144
145 static void
146 gst_tcp_client_src_init (GstTCPClientSrc * this, GstTCPClientSrcClass * g_class)
147 {
148   this->port = TCP_DEFAULT_PORT;
149   this->host = g_strdup (TCP_DEFAULT_HOST);
150   this->sock_fd.fd = -1;
151   this->protocol = GST_TCP_PROTOCOL_NONE;
152   this->caps = NULL;
153
154   GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SRC_OPEN);
155 }
156
157 static void
158 gst_tcp_client_src_finalize (GObject * gobject)
159 {
160   GstTCPClientSrc *this = GST_TCP_CLIENT_SRC (gobject);
161
162   g_free (this->host);
163
164   G_OBJECT_CLASS (parent_class)->finalize (gobject);
165 }
166
167 static GstCaps *
168 gst_tcp_client_src_getcaps (GstBaseSrc * bsrc)
169 {
170   GstTCPClientSrc *src;
171   GstCaps *caps = NULL;
172
173   src = GST_TCP_CLIENT_SRC (bsrc);
174
175   if (!GST_OBJECT_FLAG_IS_SET (src, GST_TCP_CLIENT_SRC_OPEN))
176     caps = gst_caps_new_any ();
177   else if (src->caps)
178     caps = gst_caps_copy (src->caps);
179   else
180     caps = gst_caps_new_any ();
181   GST_DEBUG_OBJECT (src, "returning caps %" GST_PTR_FORMAT, caps);
182   g_assert (GST_IS_CAPS (caps));
183   return caps;
184 }
185
186 static GstFlowReturn
187 gst_tcp_client_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
188 {
189   GstTCPClientSrc *src;
190   GstFlowReturn ret = GST_FLOW_OK;
191
192   src = GST_TCP_CLIENT_SRC (psrc);
193
194   if (!GST_OBJECT_FLAG_IS_SET (src, GST_TCP_CLIENT_SRC_OPEN))
195     goto wrong_state;
196
197   GST_LOG_OBJECT (src, "asked for a buffer");
198
199   /* read the buffer header if we're using a protocol */
200   switch (src->protocol) {
201     case GST_TCP_PROTOCOL_NONE:
202       ret = gst_tcp_read_buffer (GST_ELEMENT (src), src->sock_fd.fd,
203           src->fdset, outbuf);
204       break;
205
206     case GST_TCP_PROTOCOL_GDP:
207       /* get the caps if we're using GDP */
208       if (!src->caps_received) {
209         GstCaps *caps;
210
211         GST_DEBUG_OBJECT (src, "getting caps through GDP");
212         ret = gst_tcp_gdp_read_caps (GST_ELEMENT (src), src->sock_fd.fd,
213             src->fdset, &caps);
214
215         if (ret != GST_FLOW_OK)
216           goto no_caps;
217
218         src->caps_received = TRUE;
219         src->caps = caps;
220       }
221
222       ret = gst_tcp_gdp_read_buffer (GST_ELEMENT (src), src->sock_fd.fd,
223           src->fdset, outbuf);
224       break;
225     default:
226       /* need to assert as buf == NULL */
227       g_assert ("Unhandled protocol type");
228       break;
229   }
230
231   if (ret == GST_FLOW_OK) {
232     GST_LOG_OBJECT (src,
233         "Returning buffer from _get of size %d, ts %"
234         GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT
235         ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
236         GST_BUFFER_SIZE (*outbuf),
237         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*outbuf)),
238         GST_TIME_ARGS (GST_BUFFER_DURATION (*outbuf)),
239         GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf));
240
241     gst_buffer_set_caps (*outbuf, src->caps);
242   }
243
244   return ret;
245
246 wrong_state:
247   {
248     GST_DEBUG_OBJECT (src, "connection to closed, cannot read data");
249     return GST_FLOW_WRONG_STATE;
250   }
251 no_caps:
252   {
253     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
254         ("Could not read caps through GDP"));
255     return ret;
256   }
257 }
258
259 static void
260 gst_tcp_client_src_set_property (GObject * object, guint prop_id,
261     const GValue * value, GParamSpec * pspec)
262 {
263   GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
264
265   switch (prop_id) {
266     case PROP_HOST:
267       if (!g_value_get_string (value)) {
268         g_warning ("host property cannot be NULL");
269         break;
270       }
271       g_free (tcpclientsrc->host);
272       tcpclientsrc->host = g_strdup (g_value_get_string (value));
273       break;
274     case PROP_PORT:
275       tcpclientsrc->port = g_value_get_int (value);
276       break;
277     case PROP_PROTOCOL:
278       tcpclientsrc->protocol = g_value_get_enum (value);
279       break;
280
281     default:
282       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
283       break;
284   }
285 }
286
287 static void
288 gst_tcp_client_src_get_property (GObject * object, guint prop_id,
289     GValue * value, GParamSpec * pspec)
290 {
291   GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
292
293   switch (prop_id) {
294     case PROP_HOST:
295       g_value_set_string (value, tcpclientsrc->host);
296       break;
297     case PROP_PORT:
298       g_value_set_int (value, tcpclientsrc->port);
299       break;
300     case PROP_PROTOCOL:
301       g_value_set_enum (value, tcpclientsrc->protocol);
302       break;
303
304     default:
305       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
306       break;
307   }
308 }
309
310 /* create a socket for connecting to remote server */
311 static gboolean
312 gst_tcp_client_src_start (GstBaseSrc * bsrc)
313 {
314   int ret;
315   gchar *ip;
316   GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
317
318   if ((src->fdset = gst_poll_new (TRUE)) == NULL)
319     goto socket_pair;
320
321   /* create receiving client socket */
322   GST_DEBUG_OBJECT (src, "opening receiving client socket to %s:%d",
323       src->host, src->port);
324
325   if ((src->sock_fd.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
326     goto no_socket;
327
328   GST_DEBUG_OBJECT (src, "opened receiving client socket with fd %d",
329       src->sock_fd.fd);
330   GST_OBJECT_FLAG_SET (src, GST_TCP_CLIENT_SRC_OPEN);
331
332   /* look up name if we need to */
333   if (!(ip = gst_tcp_host_to_ip (GST_ELEMENT (src), src->host)))
334     goto name_resolv;
335
336   GST_DEBUG_OBJECT (src, "IP address for host %s is %s", src->host, ip);
337
338   /* connect to server */
339   memset (&src->server_sin, 0, sizeof (src->server_sin));
340   src->server_sin.sin_family = AF_INET; /* network socket */
341   src->server_sin.sin_port = htons (src->port); /* on port */
342   src->server_sin.sin_addr.s_addr = inet_addr (ip);     /* on host ip */
343   g_free (ip);
344
345   GST_DEBUG_OBJECT (src, "connecting to server");
346   ret = connect (src->sock_fd.fd, (struct sockaddr *) &src->server_sin,
347       sizeof (src->server_sin));
348   if (ret)
349     goto connect_failed;
350
351   /* add the socket to the poll */
352   gst_poll_add_fd (src->fdset, &src->sock_fd);
353   gst_poll_fd_ctl_read (src->fdset, &src->sock_fd, TRUE);
354
355   return TRUE;
356
357 socket_pair:
358   {
359     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
360         GST_ERROR_SYSTEM);
361     return FALSE;
362   }
363 no_socket:
364   {
365     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
366     return FALSE;
367   }
368 name_resolv:
369   {
370     gst_tcp_client_src_stop (GST_BASE_SRC (src));
371     return FALSE;
372   }
373 connect_failed:
374   {
375     gst_tcp_client_src_stop (GST_BASE_SRC (src));
376     switch (errno) {
377       case ECONNREFUSED:
378         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
379             (_("Connection to %s:%d refused."), src->host, src->port), (NULL));
380         break;
381       default:
382         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
383             ("connect to %s:%d failed: %s", src->host, src->port,
384                 g_strerror (errno)));
385         break;
386     }
387     return FALSE;
388   }
389 }
390
391 /* close the socket and associated resources
392  * unset OPEN flag
393  * used both to recover from errors and go to NULL state */
394 static gboolean
395 gst_tcp_client_src_stop (GstBaseSrc * bsrc)
396 {
397   GstTCPClientSrc *src;
398
399   src = GST_TCP_CLIENT_SRC (bsrc);
400
401   GST_DEBUG_OBJECT (src, "closing socket");
402
403   if (src->fdset != NULL) {
404     gst_poll_free (src->fdset);
405     src->fdset = NULL;
406   }
407
408   gst_tcp_socket_close (&src->sock_fd);
409   src->caps_received = FALSE;
410   if (src->caps) {
411     gst_caps_unref (src->caps);
412     src->caps = NULL;
413   }
414   GST_OBJECT_FLAG_UNSET (src, GST_TCP_CLIENT_SRC_OPEN);
415
416   return TRUE;
417 }
418
419 /* will be called only between calls to start() and stop() */
420 static gboolean
421 gst_tcp_client_src_unlock (GstBaseSrc * bsrc)
422 {
423   GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
424
425   GST_DEBUG_OBJECT (src, "set to flushing");
426   gst_poll_set_flushing (src->fdset, TRUE);
427
428   return TRUE;
429 }
430
431 /* will be called only between calls to start() and stop() */
432 static gboolean
433 gst_tcp_client_src_unlock_stop (GstBaseSrc * bsrc)
434 {
435   GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
436
437   GST_DEBUG_OBJECT (src, "unset flushing");
438   gst_poll_set_flushing (src->fdset, FALSE);
439
440   return TRUE;
441 }