ca60578ba50dfa14f2ebf79884f7f485c54432c0
[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_pad_template (element_class,
125       gst_static_pad_template_get (&sinktemplate));
126
127   gst_element_class_set_details_simple (element_class,
128       "TCP client sink", "Sink/Network",
129       "Send data as a client over the network via TCP",
130       "Thomas Vander Stichele <thomas at apestaart dot org>");
131 }
132
133 static void
134 gst_tcp_client_sink_class_init (GstTCPClientSink * klass)
135 {
136   GObjectClass *gobject_class;
137   GstElementClass *gstelement_class;
138   GstBaseSinkClass *gstbasesink_class;
139
140   gobject_class = (GObjectClass *) klass;
141   gstelement_class = (GstElementClass *) klass;
142   gstbasesink_class = (GstBaseSinkClass *) klass;
143
144   parent_class = g_type_class_peek_parent (klass);
145
146   gobject_class->set_property = gst_tcp_client_sink_set_property;
147   gobject_class->get_property = gst_tcp_client_sink_get_property;
148   gobject_class->finalize = gst_tcp_client_sink_finalize;
149
150   g_object_class_install_property (gobject_class, ARG_HOST,
151       g_param_spec_string ("host", "Host", "The host/IP to send the packets to",
152           TCP_DEFAULT_HOST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
153   g_object_class_install_property (gobject_class, ARG_PORT,
154       g_param_spec_int ("port", "Port", "The port to send the packets to",
155           0, TCP_HIGHEST_PORT, TCP_DEFAULT_PORT,
156           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
157   g_object_class_install_property (gobject_class, ARG_PROTOCOL,
158       g_param_spec_enum ("protocol", "Protocol", "The protocol to wrap data in",
159           GST_TYPE_TCP_PROTOCOL, GST_TCP_PROTOCOL_NONE,
160           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
161
162   gstelement_class->change_state = gst_tcp_client_sink_change_state;
163
164   gstbasesink_class->set_caps = gst_tcp_client_sink_setcaps;
165   gstbasesink_class->render = gst_tcp_client_sink_render;
166
167   GST_DEBUG_CATEGORY_INIT (tcpclientsink_debug, "tcpclientsink", 0, "TCP sink");
168 }
169
170 static void
171 gst_tcp_client_sink_init (GstTCPClientSink * this)
172 {
173   this->host = g_strdup (TCP_DEFAULT_HOST);
174   this->port = TCP_DEFAULT_PORT;
175
176   this->sock_fd.fd = -1;
177   this->protocol = GST_TCP_PROTOCOL_NONE;
178   GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SINK_OPEN);
179 }
180
181 static void
182 gst_tcp_client_sink_finalize (GObject * gobject)
183 {
184   GstTCPClientSink *this = GST_TCP_CLIENT_SINK (gobject);
185
186   g_free (this->host);
187
188   G_OBJECT_CLASS (parent_class)->finalize (gobject);
189 }
190
191 static gboolean
192 gst_tcp_client_sink_setcaps (GstBaseSink * bsink, GstCaps * caps)
193 {
194   GstTCPClientSink *sink;
195
196   sink = GST_TCP_CLIENT_SINK (bsink);
197
198   /* write the buffer header if we have one */
199   switch (sink->protocol) {
200     case GST_TCP_PROTOCOL_NONE:
201       break;
202
203     case GST_TCP_PROTOCOL_GDP:
204       /* if we haven't send caps yet, send them first */
205       if (!sink->caps_sent) {
206         const GstCaps *caps;
207         gchar *string;
208
209         caps = GST_PAD_CAPS (GST_PAD_PEER (GST_BASE_SINK_PAD (bsink)));
210         string = gst_caps_to_string (caps);
211         GST_DEBUG_OBJECT (sink, "Sending caps %s through GDP", string);
212         g_free (string);
213
214         if (!gst_tcp_gdp_write_caps (GST_ELEMENT (sink), sink->sock_fd.fd,
215                 caps, TRUE, sink->host, sink->port))
216           goto gdp_write_error;
217
218         sink->caps_sent = TRUE;
219       }
220       break;
221     default:
222       g_warning ("Unhandled protocol type");
223       break;
224   }
225
226   return TRUE;
227
228   /* ERRORS */
229 gdp_write_error:
230   {
231     return FALSE;
232   }
233 }
234
235 static GstFlowReturn
236 gst_tcp_client_sink_render (GstBaseSink * bsink, GstBuffer * buf)
237 {
238   size_t wrote = 0;
239   GstTCPClientSink *sink;
240   gint size;
241
242   sink = GST_TCP_CLIENT_SINK (bsink);
243
244   g_return_val_if_fail (GST_OBJECT_FLAG_IS_SET (sink, GST_TCP_CLIENT_SINK_OPEN),
245       GST_FLOW_WRONG_STATE);
246
247   size = GST_BUFFER_SIZE (buf);
248
249   GST_LOG_OBJECT (sink, "writing %d bytes for buffer data", size);
250
251   /* write the buffer header if we have one */
252   switch (sink->protocol) {
253     case GST_TCP_PROTOCOL_NONE:
254       break;
255     case GST_TCP_PROTOCOL_GDP:
256       GST_LOG_OBJECT (sink, "Sending buffer header through GDP");
257       if (!gst_tcp_gdp_write_buffer (GST_ELEMENT (sink), sink->sock_fd.fd, buf,
258               TRUE, sink->host, sink->port))
259         goto gdp_write_error;
260       break;
261     default:
262       break;
263   }
264
265   /* write buffer data */
266   wrote = gst_tcp_socket_write (sink->sock_fd.fd, GST_BUFFER_DATA (buf), size);
267
268   if (wrote < size)
269     goto write_error;
270
271   sink->data_written += wrote;
272
273   return GST_FLOW_OK;
274
275   /* ERRORS */
276 gdp_write_error:
277   {
278     return FALSE;
279   }
280 write_error:
281   {
282     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
283         (_("Error while sending data to \"%s:%d\"."), sink->host, sink->port),
284         ("Only %" G_GSIZE_FORMAT " of %u bytes written: %s",
285             wrote, GST_BUFFER_SIZE (buf), g_strerror (errno)));
286     return GST_FLOW_ERROR;
287   }
288 }
289
290 static void
291 gst_tcp_client_sink_set_property (GObject * object, guint prop_id,
292     const GValue * value, GParamSpec * pspec)
293 {
294   GstTCPClientSink *tcpclientsink;
295
296   g_return_if_fail (GST_IS_TCP_CLIENT_SINK (object));
297   tcpclientsink = GST_TCP_CLIENT_SINK (object);
298
299   switch (prop_id) {
300     case ARG_HOST:
301       if (!g_value_get_string (value)) {
302         g_warning ("host property cannot be NULL");
303         break;
304       }
305       g_free (tcpclientsink->host);
306       tcpclientsink->host = g_strdup (g_value_get_string (value));
307       break;
308     case ARG_PORT:
309       tcpclientsink->port = g_value_get_int (value);
310       break;
311     case ARG_PROTOCOL:
312       tcpclientsink->protocol = g_value_get_enum (value);
313       break;
314
315     default:
316       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
317       break;
318   }
319 }
320
321 static void
322 gst_tcp_client_sink_get_property (GObject * object, guint prop_id,
323     GValue * value, GParamSpec * pspec)
324 {
325   GstTCPClientSink *tcpclientsink;
326
327   g_return_if_fail (GST_IS_TCP_CLIENT_SINK (object));
328   tcpclientsink = GST_TCP_CLIENT_SINK (object);
329
330   switch (prop_id) {
331     case ARG_HOST:
332       g_value_set_string (value, tcpclientsink->host);
333       break;
334     case ARG_PORT:
335       g_value_set_int (value, tcpclientsink->port);
336       break;
337     case ARG_PROTOCOL:
338       g_value_set_enum (value, tcpclientsink->protocol);
339       break;
340
341     default:
342       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
343       break;
344   }
345 }
346
347
348 /* create a socket for sending to remote machine */
349 static gboolean
350 gst_tcp_client_sink_start (GstTCPClientSink * this)
351 {
352   int ret;
353   gchar *ip;
354
355   if (GST_OBJECT_FLAG_IS_SET (this, GST_TCP_CLIENT_SINK_OPEN))
356     return TRUE;
357
358   /* reset caps_sent flag */
359   this->caps_sent = FALSE;
360
361   /* create sending client socket */
362   GST_DEBUG_OBJECT (this, "opening sending client socket to %s:%d", this->host,
363       this->port);
364   if ((this->sock_fd.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1) {
365     GST_ELEMENT_ERROR (this, RESOURCE, OPEN_WRITE, (NULL), GST_ERROR_SYSTEM);
366     return FALSE;
367   }
368   GST_DEBUG_OBJECT (this, "opened sending client socket with fd %d",
369       this->sock_fd.fd);
370
371   /* look up name if we need to */
372   ip = gst_tcp_host_to_ip (GST_ELEMENT (this), this->host);
373   if (!ip) {
374     gst_tcp_socket_close (&this->sock_fd);
375     return FALSE;
376   }
377   GST_DEBUG_OBJECT (this, "IP address for host %s is %s", this->host, ip);
378
379   /* connect to server */
380   memset (&this->server_sin, 0, sizeof (this->server_sin));
381   this->server_sin.sin_family = AF_INET;        /* network socket */
382   this->server_sin.sin_port = htons (this->port);       /* on port */
383   this->server_sin.sin_addr.s_addr = inet_addr (ip);    /* on host ip */
384   g_free (ip);
385
386   GST_DEBUG_OBJECT (this, "connecting to server");
387   ret = connect (this->sock_fd.fd, (struct sockaddr *) &this->server_sin,
388       sizeof (this->server_sin));
389
390   if (ret) {
391     gst_tcp_socket_close (&this->sock_fd);
392     switch (errno) {
393       case ECONNREFUSED:
394         GST_ELEMENT_ERROR (this, RESOURCE, OPEN_WRITE,
395             (_("Connection to %s:%d refused."), this->host, this->port),
396             (NULL));
397         return FALSE;
398         break;
399       default:
400         GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL),
401             ("connect to %s:%d failed: %s", this->host, this->port,
402                 g_strerror (errno)));
403         return FALSE;
404         break;
405     }
406   }
407
408   GST_OBJECT_FLAG_SET (this, GST_TCP_CLIENT_SINK_OPEN);
409
410   this->data_written = 0;
411
412   return TRUE;
413 }
414
415 static gboolean
416 gst_tcp_client_sink_stop (GstTCPClientSink * this)
417 {
418   if (!GST_OBJECT_FLAG_IS_SET (this, GST_TCP_CLIENT_SINK_OPEN))
419     return TRUE;
420
421   gst_tcp_socket_close (&this->sock_fd);
422
423   GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SINK_OPEN);
424
425   return TRUE;
426 }
427
428 static GstStateChangeReturn
429 gst_tcp_client_sink_change_state (GstElement * element,
430     GstStateChange transition)
431 {
432   GstTCPClientSink *sink;
433   GstStateChangeReturn res;
434
435   sink = GST_TCP_CLIENT_SINK (element);
436
437   switch (transition) {
438     case GST_STATE_CHANGE_NULL_TO_READY:
439     case GST_STATE_CHANGE_READY_TO_PAUSED:
440       if (!gst_tcp_client_sink_start (sink))
441         goto start_failure;
442       break;
443     default:
444       break;
445   }
446   res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
447
448   switch (transition) {
449     case GST_STATE_CHANGE_READY_TO_NULL:
450       gst_tcp_client_sink_stop (sink);
451     default:
452       break;
453   }
454   return res;
455
456 start_failure:
457   {
458     return GST_STATE_CHANGE_FAILURE;
459   }
460 }