tizen 2.3 release
[framework/multimedia/gst-plugins-base0.10.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   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_static_pad_template (element_class, &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   g_object_class_install_property (gobject_class, PROP_PROTOCOL,
128       g_param_spec_enum ("protocol", "Protocol", "The protocol to wrap data in",
129           GST_TYPE_TCP_PROTOCOL, GST_TCP_PROTOCOL_NONE,
130           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
131
132   gstbasesrc_class->get_caps = gst_tcp_client_src_getcaps;
133   gstbasesrc_class->start = gst_tcp_client_src_start;
134   gstbasesrc_class->stop = gst_tcp_client_src_stop;
135   gstbasesrc_class->unlock = gst_tcp_client_src_unlock;
136   gstbasesrc_class->unlock_stop = gst_tcp_client_src_unlock_stop;
137
138   gstpush_src_class->create = gst_tcp_client_src_create;
139
140   GST_DEBUG_CATEGORY_INIT (tcpclientsrc_debug, "tcpclientsrc", 0,
141       "TCP Client Source");
142 }
143
144 static void
145 gst_tcp_client_src_init (GstTCPClientSrc * this, GstTCPClientSrcClass * g_class)
146 {
147   this->port = TCP_DEFAULT_PORT;
148   this->host = g_strdup (TCP_DEFAULT_HOST);
149   this->sock_fd.fd = -1;
150   this->protocol = GST_TCP_PROTOCOL_NONE;
151   this->caps = NULL;
152
153   GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SRC_OPEN);
154 }
155
156 static void
157 gst_tcp_client_src_finalize (GObject * gobject)
158 {
159   GstTCPClientSrc *this = GST_TCP_CLIENT_SRC (gobject);
160
161   g_free (this->host);
162
163   G_OBJECT_CLASS (parent_class)->finalize (gobject);
164 }
165
166 static GstCaps *
167 gst_tcp_client_src_getcaps (GstBaseSrc * bsrc)
168 {
169   GstTCPClientSrc *src;
170   GstCaps *caps = NULL;
171
172   src = GST_TCP_CLIENT_SRC (bsrc);
173
174   if (!GST_OBJECT_FLAG_IS_SET (src, GST_TCP_CLIENT_SRC_OPEN))
175     caps = gst_caps_new_any ();
176   else if (src->caps)
177     caps = gst_caps_copy (src->caps);
178   else
179     caps = gst_caps_new_any ();
180   GST_DEBUG_OBJECT (src, "returning caps %" GST_PTR_FORMAT, caps);
181   g_assert (GST_IS_CAPS (caps));
182   return caps;
183 }
184
185 static GstFlowReturn
186 gst_tcp_client_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
187 {
188   GstTCPClientSrc *src;
189   GstFlowReturn ret = GST_FLOW_OK;
190
191   src = GST_TCP_CLIENT_SRC (psrc);
192
193   if (!GST_OBJECT_FLAG_IS_SET (src, GST_TCP_CLIENT_SRC_OPEN))
194     goto wrong_state;
195
196   GST_LOG_OBJECT (src, "asked for a buffer");
197
198   /* read the buffer header if we're using a protocol */
199   switch (src->protocol) {
200     case GST_TCP_PROTOCOL_NONE:
201       ret = gst_tcp_read_buffer (GST_ELEMENT (src), src->sock_fd.fd,
202           src->fdset, outbuf);
203       break;
204
205     case GST_TCP_PROTOCOL_GDP:
206       /* get the caps if we're using GDP */
207       if (!src->caps_received) {
208         GstCaps *caps;
209
210         GST_DEBUG_OBJECT (src, "getting caps through GDP");
211         ret = gst_tcp_gdp_read_caps (GST_ELEMENT (src), src->sock_fd.fd,
212             src->fdset, &caps);
213
214         if (ret != GST_FLOW_OK)
215           goto no_caps;
216
217         src->caps_received = TRUE;
218         src->caps = caps;
219       }
220
221       ret = gst_tcp_gdp_read_buffer (GST_ELEMENT (src), src->sock_fd.fd,
222           src->fdset, outbuf);
223       break;
224     default:
225       /* need to assert as buf == NULL */
226       g_assert ("Unhandled protocol type");
227       break;
228   }
229
230   if (ret == GST_FLOW_OK) {
231     GST_LOG_OBJECT (src,
232         "Returning buffer from _get of size %d, ts %"
233         GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT
234         ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
235         GST_BUFFER_SIZE (*outbuf),
236         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*outbuf)),
237         GST_TIME_ARGS (GST_BUFFER_DURATION (*outbuf)),
238         GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf));
239
240     gst_buffer_set_caps (*outbuf, src->caps);
241   }
242
243   return ret;
244
245 wrong_state:
246   {
247     GST_DEBUG_OBJECT (src, "connection to closed, cannot read data");
248     return GST_FLOW_WRONG_STATE;
249   }
250 no_caps:
251   {
252     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
253         ("Could not read caps through GDP"));
254     return ret;
255   }
256 }
257
258 static void
259 gst_tcp_client_src_set_property (GObject * object, guint prop_id,
260     const GValue * value, GParamSpec * pspec)
261 {
262   GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
263
264   switch (prop_id) {
265     case PROP_HOST:
266       if (!g_value_get_string (value)) {
267         g_warning ("host property cannot be NULL");
268         break;
269       }
270       g_free (tcpclientsrc->host);
271       tcpclientsrc->host = g_strdup (g_value_get_string (value));
272       break;
273     case PROP_PORT:
274       tcpclientsrc->port = g_value_get_int (value);
275       break;
276     case PROP_PROTOCOL:
277       tcpclientsrc->protocol = g_value_get_enum (value);
278       break;
279
280     default:
281       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
282       break;
283   }
284 }
285
286 static void
287 gst_tcp_client_src_get_property (GObject * object, guint prop_id,
288     GValue * value, GParamSpec * pspec)
289 {
290   GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
291
292   switch (prop_id) {
293     case PROP_HOST:
294       g_value_set_string (value, tcpclientsrc->host);
295       break;
296     case PROP_PORT:
297       g_value_set_int (value, tcpclientsrc->port);
298       break;
299     case PROP_PROTOCOL:
300       g_value_set_enum (value, tcpclientsrc->protocol);
301       break;
302
303     default:
304       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
305       break;
306   }
307 }
308
309 /* create a socket for connecting to remote server */
310 static gboolean
311 gst_tcp_client_src_start (GstBaseSrc * bsrc)
312 {
313   int ret;
314   gchar *ip;
315   GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
316
317   if ((src->fdset = gst_poll_new (TRUE)) == NULL)
318     goto socket_pair;
319
320   /* create receiving client socket */
321   GST_DEBUG_OBJECT (src, "opening receiving client socket to %s:%d",
322       src->host, src->port);
323
324   if ((src->sock_fd.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
325     goto no_socket;
326
327   GST_DEBUG_OBJECT (src, "opened receiving client socket with fd %d",
328       src->sock_fd.fd);
329   GST_OBJECT_FLAG_SET (src, GST_TCP_CLIENT_SRC_OPEN);
330
331   /* look up name if we need to */
332   if (!(ip = gst_tcp_host_to_ip (GST_ELEMENT (src), src->host)))
333     goto name_resolv;
334
335   GST_DEBUG_OBJECT (src, "IP address for host %s is %s", src->host, ip);
336
337   /* connect to server */
338   memset (&src->server_sin, 0, sizeof (src->server_sin));
339   src->server_sin.sin_family = AF_INET; /* network socket */
340   src->server_sin.sin_port = htons (src->port); /* on port */
341   src->server_sin.sin_addr.s_addr = inet_addr (ip);     /* on host ip */
342   g_free (ip);
343
344   GST_DEBUG_OBJECT (src, "connecting to server");
345   ret = connect (src->sock_fd.fd, (struct sockaddr *) &src->server_sin,
346       sizeof (src->server_sin));
347   if (ret)
348     goto connect_failed;
349
350   /* add the socket to the poll */
351   gst_poll_add_fd (src->fdset, &src->sock_fd);
352   gst_poll_fd_ctl_read (src->fdset, &src->sock_fd, TRUE);
353
354   return TRUE;
355
356 socket_pair:
357   {
358     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
359         GST_ERROR_SYSTEM);
360     return FALSE;
361   }
362 no_socket:
363   {
364     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
365     return FALSE;
366   }
367 name_resolv:
368   {
369     gst_tcp_client_src_stop (GST_BASE_SRC (src));
370     return FALSE;
371   }
372 connect_failed:
373   {
374     gst_tcp_client_src_stop (GST_BASE_SRC (src));
375     switch (errno) {
376       case ECONNREFUSED:
377         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
378             (_("Connection to %s:%d refused."), src->host, src->port), (NULL));
379         break;
380       default:
381         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
382             ("connect to %s:%d failed: %s", src->host, src->port,
383                 g_strerror (errno)));
384         break;
385     }
386     return FALSE;
387   }
388 }
389
390 /* close the socket and associated resources
391  * unset OPEN flag
392  * used both to recover from errors and go to NULL state */
393 static gboolean
394 gst_tcp_client_src_stop (GstBaseSrc * bsrc)
395 {
396   GstTCPClientSrc *src;
397
398   src = GST_TCP_CLIENT_SRC (bsrc);
399
400   GST_DEBUG_OBJECT (src, "closing socket");
401
402   if (src->fdset != NULL) {
403     gst_poll_free (src->fdset);
404     src->fdset = NULL;
405   }
406
407   gst_tcp_socket_close (&src->sock_fd);
408   src->caps_received = FALSE;
409   if (src->caps) {
410     gst_caps_unref (src->caps);
411     src->caps = NULL;
412   }
413   GST_OBJECT_FLAG_UNSET (src, GST_TCP_CLIENT_SRC_OPEN);
414
415   return TRUE;
416 }
417
418 /* will be called only between calls to start() and stop() */
419 static gboolean
420 gst_tcp_client_src_unlock (GstBaseSrc * bsrc)
421 {
422   GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
423
424   GST_DEBUG_OBJECT (src, "set to flushing");
425   gst_poll_set_flushing (src->fdset, TRUE);
426
427   return TRUE;
428 }
429
430 /* will be called only between calls to start() and stop() */
431 static gboolean
432 gst_tcp_client_src_unlock_stop (GstBaseSrc * bsrc)
433 {
434   GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
435
436   GST_DEBUG_OBJECT (src, "unset flushing");
437   gst_poll_set_flushing (src->fdset, FALSE);
438
439   return TRUE;
440 }