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