gsttcpclientsrc.c 13.7 KB
Newer Older
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1 2 3
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
Sebastian Dröge's avatar
Sebastian Dröge committed
4 5
 * Copyright (C) <2011> Collabora Ltd.
 *     Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
19 20
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
21 22
 */

23
/**
24
 * SECTION:element-tcpclientsrc
25 26
 * @see_also: #tcpclientsink
 *
27 28 29 30 31 32
 * <refsect2>
 * <title>Example launch line</title>
 * |[
 * # server:
 * nc -l -p 3000
 * # client:
33
 * gst-launch tcpclientsrc port=3000 ! fdsink fd=2
34 35
 * ]| everything you type in the server is shown on the client
 * </refsect2>
36
 */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
37 38 39 40 41 42 43

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/gst-i18n-plugin.h>
#include "gsttcpclientsrc.h"
Sebastian Dröge's avatar
Sebastian Dröge committed
44
#include "gsttcp.h"
45

46
GST_DEBUG_CATEGORY_STATIC (tcpclientsrc_debug);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
47 48
#define GST_CAT_DEFAULT tcpclientsrc_debug

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
49
#define MAX_READ_SIZE                   4 * 1024
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
50

51

Wim Taymans's avatar
Wim Taymans committed
52 53 54 55 56
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS_ANY);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
57 58 59

enum
{
60 61
  PROP_0,
  PROP_HOST,
62
  PROP_PORT
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
63 64
};

65 66
#define gst_tcp_client_src_parent_class parent_class
G_DEFINE_TYPE (GstTCPClientSrc, gst_tcp_client_src, GST_TYPE_PUSH_SRC);
67 68


Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
69
static void gst_tcp_client_src_finalize (GObject * gobject);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
70

71 72
static GstCaps *gst_tcp_client_src_getcaps (GstBaseSrc * psrc,
    GstCaps * filter);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
73

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
74
static GstFlowReturn gst_tcp_client_src_create (GstPushSrc * psrc,
Wim Taymans's avatar
Wim Taymans committed
75
    GstBuffer ** outbuf);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
76 77 78
static gboolean gst_tcp_client_src_stop (GstBaseSrc * bsrc);
static gboolean gst_tcp_client_src_start (GstBaseSrc * bsrc);
static gboolean gst_tcp_client_src_unlock (GstBaseSrc * bsrc);
79
static gboolean gst_tcp_client_src_unlock_stop (GstBaseSrc * bsrc);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
80

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
81
static void gst_tcp_client_src_set_property (GObject * object, guint prop_id,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
82
    const GValue * value, GParamSpec * pspec);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
83
static void gst_tcp_client_src_get_property (GObject * object, guint prop_id,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
84 85 86
    GValue * value, GParamSpec * pspec);

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
87
gst_tcp_client_src_class_init (GstTCPClientSrcClass * klass)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
88 89
{
  GObjectClass *gobject_class;
90
  GstElementClass *gstelement_class;
Wim Taymans's avatar
Wim Taymans committed
91
  GstBaseSrcClass *gstbasesrc_class;
92
  GstPushSrcClass *gstpush_src_class;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
93 94

  gobject_class = (GObjectClass *) klass;
95
  gstelement_class = (GstElementClass *) klass;
Wim Taymans's avatar
Wim Taymans committed
96
  gstbasesrc_class = (GstBaseSrcClass *) klass;
97
  gstpush_src_class = (GstPushSrcClass *) klass;
Wim Taymans's avatar
Wim Taymans committed
98

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
99 100 101
  gobject_class->set_property = gst_tcp_client_src_set_property;
  gobject_class->get_property = gst_tcp_client_src_get_property;
  gobject_class->finalize = gst_tcp_client_src_finalize;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
102

103
  g_object_class_install_property (gobject_class, PROP_HOST,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
104 105
      g_param_spec_string ("host", "Host",
          "The host IP address to receive packets from", TCP_DEFAULT_HOST,
106
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
107
  g_object_class_install_property (gobject_class, PROP_PORT,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
108
      g_param_spec_int ("port", "Port", "The port to receive packets from", 0,
109 110
          TCP_HIGHEST_PORT, TCP_DEFAULT_PORT,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
111

112 113 114
  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&srctemplate));

115
  gst_element_class_set_static_metadata (gstelement_class,
116 117 118 119
      "TCP client source", "Source/Network",
      "Receive data as a client over the network via TCP",
      "Thomas Vander Stichele <thomas at apestaart dot org>");

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
120 121 122 123
  gstbasesrc_class->get_caps = gst_tcp_client_src_getcaps;
  gstbasesrc_class->start = gst_tcp_client_src_start;
  gstbasesrc_class->stop = gst_tcp_client_src_stop;
  gstbasesrc_class->unlock = gst_tcp_client_src_unlock;
124
  gstbasesrc_class->unlock_stop = gst_tcp_client_src_unlock_stop;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
125

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
126
  gstpush_src_class->create = gst_tcp_client_src_create;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
127 128 129 130 131 132

  GST_DEBUG_CATEGORY_INIT (tcpclientsrc_debug, "tcpclientsrc", 0,
      "TCP Client Source");
}

static void
133
gst_tcp_client_src_init (GstTCPClientSrc * this)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
134 135 136
{
  this->port = TCP_DEFAULT_PORT;
  this->host = g_strdup (TCP_DEFAULT_HOST);
Sebastian Dröge's avatar
Sebastian Dröge committed
137 138
  this->socket = NULL;
  this->cancellable = g_cancellable_new ();
Wim Taymans's avatar
Wim Taymans committed
139

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
140
  GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SRC_OPEN);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
141 142
}

143
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
144
gst_tcp_client_src_finalize (GObject * gobject)
145
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
146
  GstTCPClientSrc *this = GST_TCP_CLIENT_SRC (gobject);
147

Sebastian Dröge's avatar
Sebastian Dröge committed
148 149 150 151 152 153
  if (this->cancellable)
    g_object_unref (this->cancellable);
  this->cancellable = NULL;
  if (this->socket)
    g_object_unref (this->socket);
  this->socket = NULL;
154
  g_free (this->host);
Sebastian Dröge's avatar
Sebastian Dröge committed
155
  this->host = NULL;
156 157

  G_OBJECT_CLASS (parent_class)->finalize (gobject);
158 159
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
160
static GstCaps *
161
gst_tcp_client_src_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
162 163
{
  GstTCPClientSrc *src;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
164
  GstCaps *caps = NULL;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
165

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
166
  src = GST_TCP_CLIENT_SRC (bsrc);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
167

Sebastian Dröge's avatar
Sebastian Dröge committed
168
  caps = (filter ? gst_caps_ref (filter) : gst_caps_new_any ());
169

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
170 171 172
  GST_DEBUG_OBJECT (src, "returning caps %" GST_PTR_FORMAT, caps);
  g_assert (GST_IS_CAPS (caps));
  return caps;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
173 174
}

Wim Taymans's avatar
Wim Taymans committed
175
static GstFlowReturn
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
176
gst_tcp_client_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
177 178
{
  GstTCPClientSrc *src;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
179
  GstFlowReturn ret = GST_FLOW_OK;
Sebastian Dröge's avatar
Sebastian Dröge committed
180 181
  gssize rret;
  GError *err = NULL;
Wim Taymans's avatar
Wim Taymans committed
182
  GstMapInfo map;
183
  gssize avail, read;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
184

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
185
  src = GST_TCP_CLIENT_SRC (psrc);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
186

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
187
  if (!GST_OBJECT_FLAG_IS_SET (src, GST_TCP_CLIENT_SRC_OPEN))
Wim Taymans's avatar
Wim Taymans committed
188
    goto wrong_state;
189

Wim Taymans's avatar
Wim Taymans committed
190
  GST_LOG_OBJECT (src, "asked for a buffer");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
191

192
  /* read the buffer header */
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  avail = g_socket_get_available_bytes (src->socket);
  if (avail < 0) {
    goto get_available_error;
  } else if (avail == 0) {
    GIOCondition condition;

    if (!g_socket_condition_wait (src->socket,
            G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, src->cancellable, &err))
      goto select_error;

    condition =
        g_socket_condition_check (src->socket,
        G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP);

    if ((condition & G_IO_ERR)) {
      GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
          ("Socket in error state"));
      *outbuf = NULL;
      ret = GST_FLOW_ERROR;
      goto done;
    } else if ((condition & G_IO_HUP)) {
      GST_DEBUG_OBJECT (src, "Connection closed");
      *outbuf = NULL;
      ret = GST_FLOW_EOS;
      goto done;
    }
    avail = g_socket_get_available_bytes (src->socket);
220
    if (avail < 0)
221 222 223
      goto get_available_error;
  }

224 225 226
  if (avail > 0) {
    read = MIN (avail, MAX_READ_SIZE);
    *outbuf = gst_buffer_new_and_alloc (read);
Wim Taymans's avatar
Wim Taymans committed
227
    gst_buffer_map (*outbuf, &map, GST_MAP_READWRITE);
228
    rret =
Wim Taymans's avatar
Wim Taymans committed
229
        g_socket_receive (src->socket, (gchar *) map.data, read,
230 231 232 233
        src->cancellable, &err);
  } else {
    /* Connection closed */
    *outbuf = NULL;
234
    read = 0;
235 236
    rret = 0;
  }
Sebastian Dröge's avatar
Sebastian Dröge committed
237 238 239 240

  if (rret == 0) {
    GST_DEBUG_OBJECT (src, "Connection closed");
    ret = GST_FLOW_EOS;
241
    if (*outbuf) {
Wim Taymans's avatar
Wim Taymans committed
242
      gst_buffer_unmap (*outbuf, &map);
243 244
      gst_buffer_unref (*outbuf);
    }
Sebastian Dröge's avatar
Sebastian Dröge committed
245
    *outbuf = NULL;
246
  } else if (rret < 0) {
Sebastian Dröge's avatar
Sebastian Dröge committed
247
    if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
248
      ret = GST_FLOW_FLUSHING;
Sebastian Dröge's avatar
Sebastian Dröge committed
249 250 251 252 253 254
      GST_DEBUG_OBJECT (src, "Cancelled reading from socket");
    } else {
      ret = GST_FLOW_ERROR;
      GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
          ("Failed to read from socket: %s", err->message));
    }
Wim Taymans's avatar
Wim Taymans committed
255
    gst_buffer_unmap (*outbuf, &map);
Sebastian Dröge's avatar
Sebastian Dröge committed
256 257 258 259
    gst_buffer_unref (*outbuf);
    *outbuf = NULL;
  } else {
    ret = GST_FLOW_OK;
Wim Taymans's avatar
Wim Taymans committed
260 261
    gst_buffer_unmap (*outbuf, &map);
    gst_buffer_resize (*outbuf, 0, rret);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
262

263
    GST_LOG_OBJECT (src,
Josep Torra's avatar
Josep Torra committed
264
        "Returning buffer from _get of size %" G_GSIZE_FORMAT ", ts %"
265 266
        GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT
        ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
267
        gst_buffer_get_size (*outbuf),
268 269 270 271
        GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*outbuf)),
        GST_TIME_ARGS (GST_BUFFER_DURATION (*outbuf)),
        GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf));
  }
Sebastian Dröge's avatar
Sebastian Dröge committed
272
  g_clear_error (&err);
Wim Taymans's avatar
Wim Taymans committed
273

274
done:
275
  return ret;
Wim Taymans's avatar
Wim Taymans committed
276

277 278
select_error:
  {
279 280 281 282 283 284 285 286
    if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
      GST_DEBUG_OBJECT (src, "Cancelled");
      ret = GST_FLOW_FLUSHING;
    } else {
      GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
          ("Select failed: %s", err->message));
      ret = GST_FLOW_ERROR;
    }
287
    g_clear_error (&err);
288
    return ret;
289 290 291 292
  }
get_available_error:
  {
    GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
293
        ("Failed to get available bytes from socket"));
294 295
    return GST_FLOW_ERROR;
  }
Wim Taymans's avatar
Wim Taymans committed
296 297
wrong_state:
  {
298
    GST_DEBUG_OBJECT (src, "connection to closed, cannot read data");
299
    return GST_FLOW_FLUSHING;
Wim Taymans's avatar
Wim Taymans committed
300
  }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
301 302 303
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
304
gst_tcp_client_src_set_property (GObject * object, guint prop_id,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
305 306
    const GValue * value, GParamSpec * pspec)
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
307
  GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
308 309

  switch (prop_id) {
310
    case PROP_HOST:
311 312 313 314
      if (!g_value_get_string (value)) {
        g_warning ("host property cannot be NULL");
        break;
      }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
315 316 317
      g_free (tcpclientsrc->host);
      tcpclientsrc->host = g_strdup (g_value_get_string (value));
      break;
318
    case PROP_PORT:
319 320 321
      tcpclientsrc->port = g_value_get_int (value);
      break;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
322 323 324 325 326 327 328
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
329 330
gst_tcp_client_src_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
331
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
332
  GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
333 334

  switch (prop_id) {
335
    case PROP_HOST:
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
336 337
      g_value_set_string (value, tcpclientsrc->host);
      break;
338
    case PROP_PORT:
339 340
      g_value_set_int (value, tcpclientsrc->port);
      break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
341 342 343 344 345 346 347 348
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

/* create a socket for connecting to remote server */
static gboolean
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
349
gst_tcp_client_src_start (GstBaseSrc * bsrc)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
350
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
351
  GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
Sebastian Dröge's avatar
Sebastian Dröge committed
352 353 354
  GError *err = NULL;
  GInetAddress *addr;
  GSocketAddress *saddr;
355
  GResolver *resolver;
356

357
  /* look up name if we need to */
Sebastian Dröge's avatar
Sebastian Dröge committed
358 359 360 361
  addr = g_inet_address_new_from_string (src->host);
  if (!addr) {
    GList *results;

362 363
    resolver = g_resolver_get_default ();

Sebastian Dröge's avatar
Sebastian Dröge committed
364 365 366 367 368 369 370 371 372 373 374 375
    results =
        g_resolver_lookup_by_name (resolver, src->host, src->cancellable, &err);
    if (!results)
      goto name_resolve;
    addr = G_INET_ADDRESS (g_object_ref (results->data));

    g_resolver_free_addresses (results);
    g_object_unref (resolver);
  }
#ifndef GST_DISABLE_GST_DEBUG
  {
    gchar *ip = g_inet_address_to_string (addr);
Wim Taymans's avatar
Wim Taymans committed
376

Sebastian Dröge's avatar
Sebastian Dröge committed
377 378 379 380
    GST_DEBUG_OBJECT (src, "IP address for host %s is %s", src->host, ip);
    g_free (ip);
  }
#endif
381

Sebastian Dröge's avatar
Sebastian Dröge committed
382
  saddr = g_inet_socket_address_new (addr, src->port);
Sebastian Dröge's avatar
Sebastian Dröge committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
  g_object_unref (addr);

  /* create receiving client socket */
  GST_DEBUG_OBJECT (src, "opening receiving client socket to %s:%d",
      src->host, src->port);

  src->socket =
      g_socket_new (g_socket_address_get_family (saddr), G_SOCKET_TYPE_STREAM,
      G_SOCKET_PROTOCOL_TCP, &err);
  if (!src->socket)
    goto no_socket;

  GST_DEBUG_OBJECT (src, "opened receiving client socket");
  GST_OBJECT_FLAG_SET (src, GST_TCP_CLIENT_SRC_OPEN);

  /* connect to server */
Sebastian Dröge's avatar
Sebastian Dröge committed
399
  if (!g_socket_connect (src->socket, saddr, src->cancellable, &err))
400
    goto connect_failed;
401

Sebastian Dröge's avatar
Sebastian Dröge committed
402
  g_object_unref (saddr);
403

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
404
  return TRUE;
Wim Taymans's avatar
Wim Taymans committed
405

406
no_socket:
Wim Taymans's avatar
Wim Taymans committed
407
  {
Sebastian Dröge's avatar
Sebastian Dröge committed
408 409 410
    GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
        ("Failed to create socket: %s", err->message));
    g_clear_error (&err);
Sebastian Dröge's avatar
Sebastian Dröge committed
411
    g_object_unref (saddr);
Wim Taymans's avatar
Wim Taymans committed
412 413
    return FALSE;
  }
Sebastian Dröge's avatar
Sebastian Dröge committed
414
name_resolve:
Wim Taymans's avatar
Wim Taymans committed
415
  {
Sebastian Dröge's avatar
Sebastian Dröge committed
416 417 418 419 420 421 422
    if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
      GST_DEBUG_OBJECT (src, "Cancelled name resolval");
    } else {
      GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
          ("Failed to resolve host '%s': %s", src->host, err->message));
    }
    g_clear_error (&err);
423
    g_object_unref (resolver);
Wim Taymans's avatar
Wim Taymans committed
424 425
    return FALSE;
  }
426 427
connect_failed:
  {
Sebastian Dröge's avatar
Sebastian Dröge committed
428 429 430 431 432 433
    if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
      GST_DEBUG_OBJECT (src, "Cancelled connecting");
    } else {
      GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
          ("Failed to connect to host '%s:%d': %s", src->host, src->port,
              err->message));
434
    }
Sebastian Dröge's avatar
Sebastian Dröge committed
435 436 437
    g_clear_error (&err);
    g_object_unref (saddr);
    gst_tcp_client_src_stop (GST_BASE_SRC (src));
438 439
    return FALSE;
  }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
440 441
}

Wim Taymans's avatar
Wim Taymans committed
442 443 444 445
/* close the socket and associated resources
 * unset OPEN flag
 * used both to recover from errors and go to NULL state */
static gboolean
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
446
gst_tcp_client_src_stop (GstBaseSrc * bsrc)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
447
{
Wim Taymans's avatar
Wim Taymans committed
448
  GstTCPClientSrc *src;
Sebastian Dröge's avatar
Sebastian Dröge committed
449
  GError *err = NULL;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
450

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
451
  src = GST_TCP_CLIENT_SRC (bsrc);
Wim Taymans's avatar
Wim Taymans committed
452

Sebastian Dröge's avatar
Sebastian Dröge committed
453 454
  if (src->socket) {
    GST_DEBUG_OBJECT (src, "closing socket");
455

Sebastian Dröge's avatar
Sebastian Dröge committed
456 457 458 459 460 461
    if (!g_socket_close (src->socket, &err)) {
      GST_ERROR_OBJECT (src, "Failed to close socket: %s", err->message);
      g_clear_error (&err);
    }
    g_object_unref (src->socket);
    src->socket = NULL;
462
  }
463

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
464
  GST_OBJECT_FLAG_UNSET (src, GST_TCP_CLIENT_SRC_OPEN);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
465

Wim Taymans's avatar
Wim Taymans committed
466 467
  return TRUE;
}
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
468

469
/* will be called only between calls to start() and stop() */
Wim Taymans's avatar
Wim Taymans committed
470
static gboolean
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
471
gst_tcp_client_src_unlock (GstBaseSrc * bsrc)
Wim Taymans's avatar
Wim Taymans committed
472
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
473
  GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
474

475
  GST_DEBUG_OBJECT (src, "set to flushing");
Sebastian Dröge's avatar
Sebastian Dröge committed
476
  g_cancellable_cancel (src->cancellable);
477

Wim Taymans's avatar
Wim Taymans committed
478
  return TRUE;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
479
}
480 481 482 483 484 485 486 487

/* will be called only between calls to start() and stop() */
static gboolean
gst_tcp_client_src_unlock_stop (GstBaseSrc * bsrc)
{
  GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);

  GST_DEBUG_OBJECT (src, "unset flushing");
Sebastian Dröge's avatar
Sebastian Dröge committed
488
  g_cancellable_reset (src->cancellable);
489 490 491

  return TRUE;
}