2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
4 * Copyright (C) <2011> Collabora Ltd.
5 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 * SECTION:element-tcpclientsink
25 * @see_also: #tcpclientsink
28 * <title>Example launch line</title>
33 * gst-launch fdsink fd=1 ! tcpclientsink port=3000
34 * ]| everything you type in the client is shown on the server
41 #include <gst/gst-i18n-plugin.h>
44 #include "gsttcpclientsink.h"
46 /* TCPClientSink signals and args */
54 GST_DEBUG_CATEGORY_STATIC (tcpclientsink_debug);
55 #define GST_CAT_DEFAULT (tcpclientsink_debug)
64 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
69 static void gst_tcp_client_sink_finalize (GObject * gobject);
71 static gboolean gst_tcp_client_sink_setcaps (GstBaseSink * bsink,
73 static GstFlowReturn gst_tcp_client_sink_render (GstBaseSink * bsink,
75 static gboolean gst_tcp_client_sink_start (GstBaseSink * bsink);
76 static gboolean gst_tcp_client_sink_stop (GstBaseSink * bsink);
77 static gboolean gst_tcp_client_sink_unlock (GstBaseSink * bsink);
78 static gboolean gst_tcp_client_sink_unlock_stop (GstBaseSink * bsink);
80 static void gst_tcp_client_sink_set_property (GObject * object, guint prop_id,
81 const GValue * value, GParamSpec * pspec);
82 static void gst_tcp_client_sink_get_property (GObject * object, guint prop_id,
83 GValue * value, GParamSpec * pspec);
86 /*static guint gst_tcp_client_sink_signals[LAST_SIGNAL] = { 0 }; */
88 #define gst_tcp_client_sink_parent_class parent_class
89 G_DEFINE_TYPE (GstTCPClientSink, gst_tcp_client_sink, GST_TYPE_BASE_SINK);
92 gst_tcp_client_sink_class_init (GstTCPClientSinkClass * klass)
94 GObjectClass *gobject_class;
95 GstElementClass *gstelement_class;
96 GstBaseSinkClass *gstbasesink_class;
98 gobject_class = (GObjectClass *) klass;
99 gstelement_class = (GstElementClass *) klass;
100 gstbasesink_class = (GstBaseSinkClass *) klass;
102 parent_class = g_type_class_peek_parent (klass);
104 gobject_class->set_property = gst_tcp_client_sink_set_property;
105 gobject_class->get_property = gst_tcp_client_sink_get_property;
106 gobject_class->finalize = gst_tcp_client_sink_finalize;
108 g_object_class_install_property (gobject_class, PROP_HOST,
109 g_param_spec_string ("host", "Host", "The host/IP to send the packets to",
110 TCP_DEFAULT_HOST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
111 g_object_class_install_property (gobject_class, PROP_PORT,
112 g_param_spec_int ("port", "Port", "The port to send the packets to",
113 0, TCP_HIGHEST_PORT, TCP_DEFAULT_PORT,
114 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
116 gst_element_class_add_pad_template (gstelement_class,
117 gst_static_pad_template_get (&sinktemplate));
119 gst_element_class_set_static_metadata (gstelement_class,
120 "TCP client sink", "Sink/Network",
121 "Send data as a client over the network via TCP",
122 "Thomas Vander Stichele <thomas at apestaart dot org>");
124 gstbasesink_class->start = gst_tcp_client_sink_start;
125 gstbasesink_class->stop = gst_tcp_client_sink_stop;
126 gstbasesink_class->set_caps = gst_tcp_client_sink_setcaps;
127 gstbasesink_class->render = gst_tcp_client_sink_render;
128 gstbasesink_class->unlock = gst_tcp_client_sink_unlock;
129 gstbasesink_class->unlock_stop = gst_tcp_client_sink_unlock_stop;
131 GST_DEBUG_CATEGORY_INIT (tcpclientsink_debug, "tcpclientsink", 0, "TCP sink");
135 gst_tcp_client_sink_init (GstTCPClientSink * this)
137 this->host = g_strdup (TCP_DEFAULT_HOST);
138 this->port = TCP_DEFAULT_PORT;
141 this->cancellable = g_cancellable_new ();
143 GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SINK_OPEN);
147 gst_tcp_client_sink_finalize (GObject * gobject)
149 GstTCPClientSink *this = GST_TCP_CLIENT_SINK (gobject);
151 if (this->cancellable)
152 g_object_unref (this->cancellable);
153 this->cancellable = NULL;
156 g_object_unref (this->socket);
162 G_OBJECT_CLASS (parent_class)->finalize (gobject);
166 gst_tcp_client_sink_setcaps (GstBaseSink * bsink, GstCaps * caps)
172 gst_tcp_client_sink_render (GstBaseSink * bsink, GstBuffer * buf)
174 GstTCPClientSink *sink;
180 sink = GST_TCP_CLIENT_SINK (bsink);
182 g_return_val_if_fail (GST_OBJECT_FLAG_IS_SET (sink, GST_TCP_CLIENT_SINK_OPEN),
185 gst_buffer_map (buf, &map, GST_MAP_READ);
186 GST_LOG_OBJECT (sink, "writing %" G_GSIZE_FORMAT " bytes for buffer data",
189 /* write buffer data */
190 while (written < map.size) {
192 g_socket_send (sink->socket, (gchar *) map.data + written,
193 map.size - written, sink->cancellable, &err);
198 gst_buffer_unmap (buf, &map);
200 sink->data_written += written;
209 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
210 ret = GST_FLOW_FLUSHING;
211 GST_DEBUG_OBJECT (sink, "Cancelled reading from socket");
213 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
214 (_("Error while sending data to \"%s:%d\"."), sink->host, sink->port),
215 ("Only %" G_GSIZE_FORMAT " of %" G_GSIZE_FORMAT " bytes written: %s",
216 written, map.size, err->message));
217 ret = GST_FLOW_ERROR;
219 gst_buffer_unmap (buf, &map);
220 g_clear_error (&err);
226 gst_tcp_client_sink_set_property (GObject * object, guint prop_id,
227 const GValue * value, GParamSpec * pspec)
229 GstTCPClientSink *tcpclientsink;
231 g_return_if_fail (GST_IS_TCP_CLIENT_SINK (object));
232 tcpclientsink = GST_TCP_CLIENT_SINK (object);
236 if (!g_value_get_string (value)) {
237 g_warning ("host property cannot be NULL");
240 g_free (tcpclientsink->host);
241 tcpclientsink->host = g_strdup (g_value_get_string (value));
244 tcpclientsink->port = g_value_get_int (value);
248 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
254 gst_tcp_client_sink_get_property (GObject * object, guint prop_id,
255 GValue * value, GParamSpec * pspec)
257 GstTCPClientSink *tcpclientsink;
259 g_return_if_fail (GST_IS_TCP_CLIENT_SINK (object));
260 tcpclientsink = GST_TCP_CLIENT_SINK (object);
264 g_value_set_string (value, tcpclientsink->host);
267 g_value_set_int (value, tcpclientsink->port);
270 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
276 /* create a socket for sending to remote machine */
278 gst_tcp_client_sink_start (GstBaseSink * bsink)
280 GstTCPClientSink *this = GST_TCP_CLIENT_SINK (bsink);
283 GSocketAddress *saddr;
286 if (GST_OBJECT_FLAG_IS_SET (this, GST_TCP_CLIENT_SINK_OPEN))
289 /* look up name if we need to */
290 addr = g_inet_address_new_from_string (this->host);
294 resolver = g_resolver_get_default ();
297 g_resolver_lookup_by_name (resolver, this->host, this->cancellable,
301 addr = G_INET_ADDRESS (g_object_ref (results->data));
303 g_resolver_free_addresses (results);
304 g_object_unref (resolver);
306 #ifndef GST_DISABLE_GST_DEBUG
308 gchar *ip = g_inet_address_to_string (addr);
310 GST_DEBUG_OBJECT (this, "IP address for host %s is %s", this->host, ip);
314 saddr = g_inet_socket_address_new (addr, this->port);
315 g_object_unref (addr);
317 /* create sending client socket */
318 GST_DEBUG_OBJECT (this, "opening sending client socket to %s:%d", this->host,
321 g_socket_new (g_socket_address_get_family (saddr), G_SOCKET_TYPE_STREAM,
322 G_SOCKET_PROTOCOL_TCP, &err);
326 GST_DEBUG_OBJECT (this, "opened sending client socket");
328 /* connect to server */
329 if (!g_socket_connect (this->socket, saddr, this->cancellable, &err))
332 g_object_unref (saddr);
334 GST_OBJECT_FLAG_SET (this, GST_TCP_CLIENT_SINK_OPEN);
336 this->data_written = 0;
341 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL),
342 ("Failed to create socket: %s", err->message));
343 g_clear_error (&err);
344 g_object_unref (saddr);
349 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
350 GST_DEBUG_OBJECT (this, "Cancelled name resolval");
352 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL),
353 ("Failed to resolve host '%s': %s", this->host, err->message));
355 g_clear_error (&err);
356 g_object_unref (resolver);
361 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
362 GST_DEBUG_OBJECT (this, "Cancelled connecting");
364 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL),
365 ("Failed to connect to host '%s:%d': %s", this->host, this->port,
368 g_clear_error (&err);
369 g_object_unref (saddr);
370 /* pretend we opened ok for proper cleanup to happen */
371 GST_OBJECT_FLAG_SET (this, GST_TCP_CLIENT_SINK_OPEN);
372 gst_tcp_client_sink_stop (GST_BASE_SINK (this));
378 gst_tcp_client_sink_stop (GstBaseSink * bsink)
380 GstTCPClientSink *this = GST_TCP_CLIENT_SINK (bsink);
383 if (!GST_OBJECT_FLAG_IS_SET (this, GST_TCP_CLIENT_SINK_OPEN))
387 GST_DEBUG_OBJECT (this, "closing socket");
389 if (!g_socket_close (this->socket, &err)) {
390 GST_ERROR_OBJECT (this, "Failed to close socket: %s", err->message);
391 g_clear_error (&err);
393 g_object_unref (this->socket);
397 GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SINK_OPEN);
402 /* will be called only between calls to start() and stop() */
404 gst_tcp_client_sink_unlock (GstBaseSink * bsink)
406 GstTCPClientSink *sink = GST_TCP_CLIENT_SINK (bsink);
408 GST_DEBUG_OBJECT (sink, "set to flushing");
409 g_cancellable_cancel (sink->cancellable);
414 /* will be called only between calls to start() and stop() */
416 gst_tcp_client_sink_unlock_stop (GstBaseSink * bsink)
418 GstTCPClientSink *sink = GST_TCP_CLIENT_SINK (bsink);
420 GST_DEBUG_OBJECT (sink, "unset flushing");
421 g_cancellable_reset (sink->cancellable);