Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / 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 };
67
68
69 GST_BOILERPLATE (GstTCPClientSrc, gst_tcp_client_src, GstPushSrc,
70     GST_TYPE_PUSH_SRC);
71
72
73 static void gst_tcp_client_src_finalize (GObject * gobject);
74
75 static GstCaps *gst_tcp_client_src_getcaps (GstBaseSrc * psrc);
76
77 static GstFlowReturn gst_tcp_client_src_create (GstPushSrc * psrc,
78     GstBuffer ** outbuf);
79 static gboolean gst_tcp_client_src_stop (GstBaseSrc * bsrc);
80 static gboolean gst_tcp_client_src_start (GstBaseSrc * bsrc);
81 static gboolean gst_tcp_client_src_unlock (GstBaseSrc * bsrc);
82 static gboolean gst_tcp_client_src_unlock_stop (GstBaseSrc * bsrc);
83
84 static void gst_tcp_client_src_set_property (GObject * object, guint prop_id,
85     const GValue * value, GParamSpec * pspec);
86 static void gst_tcp_client_src_get_property (GObject * object, guint prop_id,
87     GValue * value, GParamSpec * pspec);
88
89
90 static void
91 gst_tcp_client_src_base_init (gpointer g_class)
92 {
93   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
94
95   gst_element_class_add_pad_template (element_class,
96       gst_static_pad_template_get (&srctemplate));
97
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>");
102 }
103
104 static void
105 gst_tcp_client_src_class_init (GstTCPClientSrcClass * klass)
106 {
107   GObjectClass *gobject_class;
108   GstBaseSrcClass *gstbasesrc_class;
109   GstPushSrcClass *gstpush_src_class;
110
111   gobject_class = (GObjectClass *) klass;
112   gstbasesrc_class = (GstBaseSrcClass *) klass;
113   gstpush_src_class = (GstPushSrcClass *) klass;
114
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;
118
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
128   gstbasesrc_class->get_caps = gst_tcp_client_src_getcaps;
129   gstbasesrc_class->start = gst_tcp_client_src_start;
130   gstbasesrc_class->stop = gst_tcp_client_src_stop;
131   gstbasesrc_class->unlock = gst_tcp_client_src_unlock;
132   gstbasesrc_class->unlock_stop = gst_tcp_client_src_unlock_stop;
133
134   gstpush_src_class->create = gst_tcp_client_src_create;
135
136   GST_DEBUG_CATEGORY_INIT (tcpclientsrc_debug, "tcpclientsrc", 0,
137       "TCP Client Source");
138 }
139
140 static void
141 gst_tcp_client_src_init (GstTCPClientSrc * this, GstTCPClientSrcClass * g_class)
142 {
143   this->port = TCP_DEFAULT_PORT;
144   this->host = g_strdup (TCP_DEFAULT_HOST);
145   this->sock_fd.fd = -1;
146   this->caps = NULL;
147
148   GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SRC_OPEN);
149 }
150
151 static void
152 gst_tcp_client_src_finalize (GObject * gobject)
153 {
154   GstTCPClientSrc *this = GST_TCP_CLIENT_SRC (gobject);
155
156   g_free (this->host);
157
158   G_OBJECT_CLASS (parent_class)->finalize (gobject);
159 }
160
161 static GstCaps *
162 gst_tcp_client_src_getcaps (GstBaseSrc * bsrc)
163 {
164   GstTCPClientSrc *src;
165   GstCaps *caps = NULL;
166
167   src = GST_TCP_CLIENT_SRC (bsrc);
168
169   if (!GST_OBJECT_FLAG_IS_SET (src, GST_TCP_CLIENT_SRC_OPEN))
170     caps = gst_caps_new_any ();
171   else if (src->caps)
172     caps = gst_caps_copy (src->caps);
173   else
174     caps = gst_caps_new_any ();
175   GST_DEBUG_OBJECT (src, "returning caps %" GST_PTR_FORMAT, caps);
176   g_assert (GST_IS_CAPS (caps));
177   return caps;
178 }
179
180 static GstFlowReturn
181 gst_tcp_client_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
182 {
183   GstTCPClientSrc *src;
184   GstFlowReturn ret = GST_FLOW_OK;
185
186   src = GST_TCP_CLIENT_SRC (psrc);
187
188   if (!GST_OBJECT_FLAG_IS_SET (src, GST_TCP_CLIENT_SRC_OPEN))
189     goto wrong_state;
190
191   GST_LOG_OBJECT (src, "asked for a buffer");
192
193   /* read the buffer header */
194   ret = gst_tcp_read_buffer (GST_ELEMENT (src), src->sock_fd.fd,
195       src->fdset, outbuf);
196
197   if (ret == GST_FLOW_OK) {
198     GST_LOG_OBJECT (src,
199         "Returning buffer from _get of size %d, ts %"
200         GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT
201         ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
202         GST_BUFFER_SIZE (*outbuf),
203         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*outbuf)),
204         GST_TIME_ARGS (GST_BUFFER_DURATION (*outbuf)),
205         GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf));
206
207     gst_buffer_set_caps (*outbuf, src->caps);
208   }
209
210   return ret;
211
212 wrong_state:
213   {
214     GST_DEBUG_OBJECT (src, "connection to closed, cannot read data");
215     return GST_FLOW_WRONG_STATE;
216   }
217 }
218
219 static void
220 gst_tcp_client_src_set_property (GObject * object, guint prop_id,
221     const GValue * value, GParamSpec * pspec)
222 {
223   GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
224
225   switch (prop_id) {
226     case PROP_HOST:
227       if (!g_value_get_string (value)) {
228         g_warning ("host property cannot be NULL");
229         break;
230       }
231       g_free (tcpclientsrc->host);
232       tcpclientsrc->host = g_strdup (g_value_get_string (value));
233       break;
234     case PROP_PORT:
235       tcpclientsrc->port = g_value_get_int (value);
236       break;
237
238     default:
239       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
240       break;
241   }
242 }
243
244 static void
245 gst_tcp_client_src_get_property (GObject * object, guint prop_id,
246     GValue * value, GParamSpec * pspec)
247 {
248   GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
249
250   switch (prop_id) {
251     case PROP_HOST:
252       g_value_set_string (value, tcpclientsrc->host);
253       break;
254     case PROP_PORT:
255       g_value_set_int (value, tcpclientsrc->port);
256       break;
257
258     default:
259       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
260       break;
261   }
262 }
263
264 /* create a socket for connecting to remote server */
265 static gboolean
266 gst_tcp_client_src_start (GstBaseSrc * bsrc)
267 {
268   int ret;
269   gchar *ip;
270   GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
271
272   if ((src->fdset = gst_poll_new (TRUE)) == NULL)
273     goto socket_pair;
274
275   /* create receiving client socket */
276   GST_DEBUG_OBJECT (src, "opening receiving client socket to %s:%d",
277       src->host, src->port);
278
279   if ((src->sock_fd.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
280     goto no_socket;
281
282   GST_DEBUG_OBJECT (src, "opened receiving client socket with fd %d",
283       src->sock_fd.fd);
284   GST_OBJECT_FLAG_SET (src, GST_TCP_CLIENT_SRC_OPEN);
285
286   /* look up name if we need to */
287   if (!(ip = gst_tcp_host_to_ip (GST_ELEMENT (src), src->host)))
288     goto name_resolv;
289
290   GST_DEBUG_OBJECT (src, "IP address for host %s is %s", src->host, ip);
291
292   /* connect to server */
293   memset (&src->server_sin, 0, sizeof (src->server_sin));
294   src->server_sin.sin_family = AF_INET; /* network socket */
295   src->server_sin.sin_port = htons (src->port); /* on port */
296   src->server_sin.sin_addr.s_addr = inet_addr (ip);     /* on host ip */
297   g_free (ip);
298
299   GST_DEBUG_OBJECT (src, "connecting to server");
300   ret = connect (src->sock_fd.fd, (struct sockaddr *) &src->server_sin,
301       sizeof (src->server_sin));
302   if (ret)
303     goto connect_failed;
304
305   /* add the socket to the poll */
306   gst_poll_add_fd (src->fdset, &src->sock_fd);
307   gst_poll_fd_ctl_read (src->fdset, &src->sock_fd, TRUE);
308
309   return TRUE;
310
311 socket_pair:
312   {
313     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
314         GST_ERROR_SYSTEM);
315     return FALSE;
316   }
317 no_socket:
318   {
319     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
320     return FALSE;
321   }
322 name_resolv:
323   {
324     gst_tcp_client_src_stop (GST_BASE_SRC (src));
325     return FALSE;
326   }
327 connect_failed:
328   {
329     gst_tcp_client_src_stop (GST_BASE_SRC (src));
330     switch (errno) {
331       case ECONNREFUSED:
332         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
333             (_("Connection to %s:%d refused."), src->host, src->port), (NULL));
334         break;
335       default:
336         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
337             ("connect to %s:%d failed: %s", src->host, src->port,
338                 g_strerror (errno)));
339         break;
340     }
341     return FALSE;
342   }
343 }
344
345 /* close the socket and associated resources
346  * unset OPEN flag
347  * used both to recover from errors and go to NULL state */
348 static gboolean
349 gst_tcp_client_src_stop (GstBaseSrc * bsrc)
350 {
351   GstTCPClientSrc *src;
352
353   src = GST_TCP_CLIENT_SRC (bsrc);
354
355   GST_DEBUG_OBJECT (src, "closing socket");
356
357   if (src->fdset != NULL) {
358     gst_poll_free (src->fdset);
359     src->fdset = NULL;
360   }
361
362   gst_tcp_socket_close (&src->sock_fd);
363   src->caps_received = FALSE;
364   if (src->caps) {
365     gst_caps_unref (src->caps);
366     src->caps = NULL;
367   }
368   GST_OBJECT_FLAG_UNSET (src, GST_TCP_CLIENT_SRC_OPEN);
369
370   return TRUE;
371 }
372
373 /* will be called only between calls to start() and stop() */
374 static gboolean
375 gst_tcp_client_src_unlock (GstBaseSrc * bsrc)
376 {
377   GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
378
379   GST_DEBUG_OBJECT (src, "set to flushing");
380   gst_poll_set_flushing (src->fdset, TRUE);
381
382   return TRUE;
383 }
384
385 /* will be called only between calls to start() and stop() */
386 static gboolean
387 gst_tcp_client_src_unlock_stop (GstBaseSrc * bsrc)
388 {
389   GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
390
391   GST_DEBUG_OBJECT (src, "unset flushing");
392   gst_poll_set_flushing (src->fdset, FALSE);
393
394   return TRUE;
395 }