tizen 2.3 release
[framework/multimedia/gst-plugins-base0.10.git] / gst / tcp / gsttcpclientsink.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-tcpclientsink
23  * @see_also: #tcpclientsrc
24  *
25  * <refsect2>
26  * <title>Example launch line</title>
27  * |[
28  * # server:
29  * nc -l -p 3000
30  * # client:
31  * gst-launch fdsrc fd=1 ! tcpclientsink protocol=none port=3000
32  * ]| everything you type in the client is shown on the server
33  * </refsect2>
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 #include <gst/gst-i18n-plugin.h>
40 #include <gst/dataprotocol/dataprotocol.h>
41 #include "gsttcp.h"
42 #include "gsttcpclientsink.h"
43 #include <string.h>             /* memset */
44
45 /* TCPClientSink signals and args */
46 enum
47 {
48   FRAME_ENCODED,
49   /* FILL ME */
50   LAST_SIGNAL
51 };
52
53 GST_DEBUG_CATEGORY_STATIC (tcpclientsink_debug);
54 #define GST_CAT_DEFAULT (tcpclientsink_debug)
55
56 enum
57 {
58   ARG_0,
59   ARG_HOST,
60   ARG_PORT,
61   ARG_PROTOCOL
62       /* FILL ME */
63 };
64
65 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
66     GST_PAD_SINK,
67     GST_PAD_ALWAYS,
68     GST_STATIC_CAPS_ANY);
69
70 static void gst_tcp_client_sink_base_init (gpointer g_class);
71 static void gst_tcp_client_sink_class_init (GstTCPClientSink * klass);
72 static void gst_tcp_client_sink_init (GstTCPClientSink * tcpclientsink);
73 static void gst_tcp_client_sink_finalize (GObject * gobject);
74
75 static gboolean gst_tcp_client_sink_setcaps (GstBaseSink * bsink,
76     GstCaps * caps);
77 static GstFlowReturn gst_tcp_client_sink_render (GstBaseSink * bsink,
78     GstBuffer * buf);
79 static GstStateChangeReturn gst_tcp_client_sink_change_state (GstElement *
80     element, GstStateChange transition);
81
82 static void gst_tcp_client_sink_set_property (GObject * object, guint prop_id,
83     const GValue * value, GParamSpec * pspec);
84 static void gst_tcp_client_sink_get_property (GObject * object, guint prop_id,
85     GValue * value, GParamSpec * pspec);
86
87
88 static GstElementClass *parent_class = NULL;
89
90 /*static guint gst_tcp_client_sink_signals[LAST_SIGNAL] = { 0 }; */
91
92 GType
93 gst_tcp_client_sink_get_type (void)
94 {
95   static GType tcpclientsink_type = 0;
96
97
98   if (!tcpclientsink_type) {
99     static const GTypeInfo tcpclientsink_info = {
100       sizeof (GstTCPClientSinkClass),
101       gst_tcp_client_sink_base_init,
102       NULL,
103       (GClassInitFunc) gst_tcp_client_sink_class_init,
104       NULL,
105       NULL,
106       sizeof (GstTCPClientSink),
107       0,
108       (GInstanceInitFunc) gst_tcp_client_sink_init,
109       NULL
110     };
111
112     tcpclientsink_type =
113         g_type_register_static (GST_TYPE_BASE_SINK, "GstTCPClientSink",
114         &tcpclientsink_info, 0);
115   }
116   return tcpclientsink_type;
117 }
118
119 static void
120 gst_tcp_client_sink_base_init (gpointer g_class)
121 {
122   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
123
124   gst_element_class_add_static_pad_template (element_class, &sinktemplate);
125
126   gst_element_class_set_details_simple (element_class,
127       "TCP client sink", "Sink/Network",
128       "Send data as a client over the network via TCP",
129       "Thomas Vander Stichele <thomas at apestaart dot org>");
130 }
131
132 static void
133 gst_tcp_client_sink_class_init (GstTCPClientSink * klass)
134 {
135   GObjectClass *gobject_class;
136   GstElementClass *gstelement_class;
137   GstBaseSinkClass *gstbasesink_class;
138
139   gobject_class = (GObjectClass *) klass;
140   gstelement_class = (GstElementClass *) klass;
141   gstbasesink_class = (GstBaseSinkClass *) klass;
142
143   parent_class = g_type_class_peek_parent (klass);
144
145   gobject_class->set_property = gst_tcp_client_sink_set_property;
146   gobject_class->get_property = gst_tcp_client_sink_get_property;
147   gobject_class->finalize = gst_tcp_client_sink_finalize;
148
149   g_object_class_install_property (gobject_class, ARG_HOST,
150       g_param_spec_string ("host", "Host", "The host/IP to send the packets to",
151           TCP_DEFAULT_HOST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
152   g_object_class_install_property (gobject_class, ARG_PORT,
153       g_param_spec_int ("port", "Port", "The port to send the packets to",
154           0, TCP_HIGHEST_PORT, TCP_DEFAULT_PORT,
155           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
156   g_object_class_install_property (gobject_class, ARG_PROTOCOL,
157       g_param_spec_enum ("protocol", "Protocol", "The protocol to wrap data in",
158           GST_TYPE_TCP_PROTOCOL, GST_TCP_PROTOCOL_NONE,
159           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
160
161   gstelement_class->change_state = gst_tcp_client_sink_change_state;
162
163   gstbasesink_class->set_caps = gst_tcp_client_sink_setcaps;
164   gstbasesink_class->render = gst_tcp_client_sink_render;
165
166   GST_DEBUG_CATEGORY_INIT (tcpclientsink_debug, "tcpclientsink", 0, "TCP sink");
167 }
168
169 static void
170 gst_tcp_client_sink_init (GstTCPClientSink * this)
171 {
172   this->host = g_strdup (TCP_DEFAULT_HOST);
173   this->port = TCP_DEFAULT_PORT;
174
175   this->sock_fd.fd = -1;
176   this->protocol = GST_TCP_PROTOCOL_NONE;
177   GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SINK_OPEN);
178 }
179
180 static void
181 gst_tcp_client_sink_finalize (GObject * gobject)
182 {
183   GstTCPClientSink *this = GST_TCP_CLIENT_SINK (gobject);
184
185   g_free (this->host);
186
187   G_OBJECT_CLASS (parent_class)->finalize (gobject);
188 }
189
190 static gboolean
191 gst_tcp_client_sink_setcaps (GstBaseSink * bsink, GstCaps * caps)
192 {
193   GstTCPClientSink *sink;
194
195   sink = GST_TCP_CLIENT_SINK (bsink);
196
197   /* write the buffer header if we have one */
198   switch (sink->protocol) {
199     case GST_TCP_PROTOCOL_NONE:
200       break;
201
202     case GST_TCP_PROTOCOL_GDP:
203       /* if we haven't send caps yet, send them first */
204       if (!sink->caps_sent) {
205         const GstCaps *caps;
206         gchar *string;
207
208         caps = GST_PAD_CAPS (GST_PAD_PEER (GST_BASE_SINK_PAD (bsink)));
209         string = gst_caps_to_string (caps);
210         GST_DEBUG_OBJECT (sink, "Sending caps %s through GDP", string);
211         g_free (string);
212
213         if (!gst_tcp_gdp_write_caps (GST_ELEMENT (sink), sink->sock_fd.fd,
214                 caps, TRUE, sink->host, sink->port))
215           goto gdp_write_error;
216
217         sink->caps_sent = TRUE;
218       }
219       break;
220     default:
221       g_warning ("Unhandled protocol type");
222       break;
223   }
224
225   return TRUE;
226
227   /* ERRORS */
228 gdp_write_error:
229   {
230     return FALSE;
231   }
232 }
233
234 static GstFlowReturn
235 gst_tcp_client_sink_render (GstBaseSink * bsink, GstBuffer * buf)
236 {
237   size_t wrote = 0;
238   GstTCPClientSink *sink;
239   gint size;
240
241   sink = GST_TCP_CLIENT_SINK (bsink);
242
243   g_return_val_if_fail (GST_OBJECT_FLAG_IS_SET (sink, GST_TCP_CLIENT_SINK_OPEN),
244       GST_FLOW_WRONG_STATE);
245
246   size = GST_BUFFER_SIZE (buf);
247
248   GST_LOG_OBJECT (sink, "writing %d bytes for buffer data", size);
249
250   /* write the buffer header if we have one */
251   switch (sink->protocol) {
252     case GST_TCP_PROTOCOL_NONE:
253       break;
254     case GST_TCP_PROTOCOL_GDP:
255       GST_LOG_OBJECT (sink, "Sending buffer header through GDP");
256       if (!gst_tcp_gdp_write_buffer (GST_ELEMENT (sink), sink->sock_fd.fd, buf,
257               TRUE, sink->host, sink->port))
258         goto gdp_write_error;
259       break;
260     default:
261       break;
262   }
263
264   /* write buffer data */
265   wrote = gst_tcp_socket_write (sink->sock_fd.fd, GST_BUFFER_DATA (buf), size);
266
267   if (wrote < size)
268     goto write_error;
269
270   sink->data_written += wrote;
271
272   return GST_FLOW_OK;
273
274   /* ERRORS */
275 gdp_write_error:
276   {
277     return FALSE;
278   }
279 write_error:
280   {
281     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
282         (_("Error while sending data to \"%s:%d\"."), sink->host, sink->port),
283         ("Only %" G_GSIZE_FORMAT " of %u bytes written: %s",
284             wrote, GST_BUFFER_SIZE (buf), g_strerror (errno)));
285     return GST_FLOW_ERROR;
286   }
287 }
288
289 static void
290 gst_tcp_client_sink_set_property (GObject * object, guint prop_id,
291     const GValue * value, GParamSpec * pspec)
292 {
293   GstTCPClientSink *tcpclientsink;
294
295   g_return_if_fail (GST_IS_TCP_CLIENT_SINK (object));
296   tcpclientsink = GST_TCP_CLIENT_SINK (object);
297
298   switch (prop_id) {
299     case ARG_HOST:
300       if (!g_value_get_string (value)) {
301         g_warning ("host property cannot be NULL");
302         break;
303       }
304       g_free (tcpclientsink->host);
305       tcpclientsink->host = g_strdup (g_value_get_string (value));
306       break;
307     case ARG_PORT:
308       tcpclientsink->port = g_value_get_int (value);
309       break;
310     case ARG_PROTOCOL:
311       tcpclientsink->protocol = g_value_get_enum (value);
312       break;
313
314     default:
315       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
316       break;
317   }
318 }
319
320 static void
321 gst_tcp_client_sink_get_property (GObject * object, guint prop_id,
322     GValue * value, GParamSpec * pspec)
323 {
324   GstTCPClientSink *tcpclientsink;
325
326   g_return_if_fail (GST_IS_TCP_CLIENT_SINK (object));
327   tcpclientsink = GST_TCP_CLIENT_SINK (object);
328
329   switch (prop_id) {
330     case ARG_HOST:
331       g_value_set_string (value, tcpclientsink->host);
332       break;
333     case ARG_PORT:
334       g_value_set_int (value, tcpclientsink->port);
335       break;
336     case ARG_PROTOCOL:
337       g_value_set_enum (value, tcpclientsink->protocol);
338       break;
339
340     default:
341       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342       break;
343   }
344 }
345
346
347 /* create a socket for sending to remote machine */
348 static gboolean
349 gst_tcp_client_sink_start (GstTCPClientSink * this)
350 {
351   int ret;
352   gchar *ip;
353
354   if (GST_OBJECT_FLAG_IS_SET (this, GST_TCP_CLIENT_SINK_OPEN))
355     return TRUE;
356
357   /* reset caps_sent flag */
358   this->caps_sent = FALSE;
359
360   /* create sending client socket */
361   GST_DEBUG_OBJECT (this, "opening sending client socket to %s:%d", this->host,
362       this->port);
363   if ((this->sock_fd.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1) {
364     GST_ELEMENT_ERROR (this, RESOURCE, OPEN_WRITE, (NULL), GST_ERROR_SYSTEM);
365     return FALSE;
366   }
367   GST_DEBUG_OBJECT (this, "opened sending client socket with fd %d",
368       this->sock_fd.fd);
369
370   /* look up name if we need to */
371   ip = gst_tcp_host_to_ip (GST_ELEMENT (this), this->host);
372   if (!ip) {
373     gst_tcp_socket_close (&this->sock_fd);
374     return FALSE;
375   }
376   GST_DEBUG_OBJECT (this, "IP address for host %s is %s", this->host, ip);
377
378   /* connect to server */
379   memset (&this->server_sin, 0, sizeof (this->server_sin));
380   this->server_sin.sin_family = AF_INET;        /* network socket */
381   this->server_sin.sin_port = htons (this->port);       /* on port */
382   this->server_sin.sin_addr.s_addr = inet_addr (ip);    /* on host ip */
383   g_free (ip);
384
385   GST_DEBUG_OBJECT (this, "connecting to server");
386   ret = connect (this->sock_fd.fd, (struct sockaddr *) &this->server_sin,
387       sizeof (this->server_sin));
388
389   if (ret) {
390     gst_tcp_socket_close (&this->sock_fd);
391     switch (errno) {
392       case ECONNREFUSED:
393         GST_ELEMENT_ERROR (this, RESOURCE, OPEN_WRITE,
394             (_("Connection to %s:%d refused."), this->host, this->port),
395             (NULL));
396         return FALSE;
397         break;
398       default:
399         GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL),
400             ("connect to %s:%d failed: %s", this->host, this->port,
401                 g_strerror (errno)));
402         return FALSE;
403         break;
404     }
405   }
406
407   GST_OBJECT_FLAG_SET (this, GST_TCP_CLIENT_SINK_OPEN);
408
409   this->data_written = 0;
410
411   return TRUE;
412 }
413
414 static gboolean
415 gst_tcp_client_sink_stop (GstTCPClientSink * this)
416 {
417   if (!GST_OBJECT_FLAG_IS_SET (this, GST_TCP_CLIENT_SINK_OPEN))
418     return TRUE;
419
420   gst_tcp_socket_close (&this->sock_fd);
421
422   GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SINK_OPEN);
423
424   return TRUE;
425 }
426
427 static GstStateChangeReturn
428 gst_tcp_client_sink_change_state (GstElement * element,
429     GstStateChange transition)
430 {
431   GstTCPClientSink *sink;
432   GstStateChangeReturn res;
433
434   sink = GST_TCP_CLIENT_SINK (element);
435
436   switch (transition) {
437     case GST_STATE_CHANGE_NULL_TO_READY:
438     case GST_STATE_CHANGE_READY_TO_PAUSED:
439       if (!gst_tcp_client_sink_start (sink))
440         goto start_failure;
441       break;
442     default:
443       break;
444   }
445   res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
446
447   switch (transition) {
448     case GST_STATE_CHANGE_READY_TO_NULL:
449       gst_tcp_client_sink_stop (sink);
450     default:
451       break;
452   }
453   return res;
454
455 start_failure:
456   {
457     return GST_STATE_CHANGE_FAILURE;
458   }
459 }