expand tabs
[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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <gst/gst-i18n-plugin.h>
27 #include "gsttcp.h"
28 #include "gsttcpclientsrc.h"
29 #include <string.h>             /* memset */
30 #include <unistd.h>
31 #include <arpa/inet.h>
32 #include <fcntl.h>
33
34
35 /* control stuff stolen from fdsrc */
36 #define CONTROL_STOP            'S'     /* stop the select call */
37 #define CONTROL_SOCKETS(o)      o->control_fds
38 #define WRITE_SOCKET(o)         o->control_fds[1]
39 #define READ_SOCKET(o)          o->control_fds[0]
40
41 #define SEND_COMMAND(o, command)          \
42 G_STMT_START {                              \
43   unsigned char c; c = command;             \
44   write (WRITE_SOCKET(o), &c, 1);         \
45 } G_STMT_END
46
47 #define READ_COMMAND(o, command, res)        \
48 G_STMT_START {                                 \
49   res = read(READ_SOCKET(o), &command, 1);   \
50 } G_STMT_END
51
52
53 GST_DEBUG_CATEGORY (tcpclientsrc_debug);
54 #define GST_CAT_DEFAULT tcpclientsrc_debug
55
56 #define MAX_READ_SIZE                   4 * 1024
57
58
59 static GstElementDetails gst_tcp_client_src_details =
60 GST_ELEMENT_DETAILS ("TCP Client source",
61     "Source/Network",
62     "Receive data as a client over the network via TCP",
63     "Thomas Vander Stichele <thomas at apestaart dot org>");
64
65 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
66     GST_PAD_SRC,
67     GST_PAD_ALWAYS,
68     GST_STATIC_CAPS_ANY);
69
70
71 enum
72 {
73   PROP_0,
74   PROP_HOST,
75   PROP_PORT,
76   PROP_PROTOCOL
77 };
78
79
80 GST_BOILERPLATE (GstTCPClientSrc, gst_tcp_client_src, GstPushSrc,
81     GST_TYPE_PUSH_SRC);
82
83
84 static void gst_tcp_client_src_finalize (GObject * gobject);
85
86 static GstCaps *gst_tcp_client_src_getcaps (GstBaseSrc * psrc);
87
88 static GstFlowReturn gst_tcp_client_src_create (GstPushSrc * psrc,
89     GstBuffer ** outbuf);
90 static gboolean gst_tcp_client_src_stop (GstBaseSrc * bsrc);
91 static gboolean gst_tcp_client_src_start (GstBaseSrc * bsrc);
92 static gboolean gst_tcp_client_src_unlock (GstBaseSrc * bsrc);
93
94 static void gst_tcp_client_src_set_property (GObject * object, guint prop_id,
95     const GValue * value, GParamSpec * pspec);
96 static void gst_tcp_client_src_get_property (GObject * object, guint prop_id,
97     GValue * value, GParamSpec * pspec);
98
99
100 static void
101 gst_tcp_client_src_base_init (gpointer g_class)
102 {
103   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
104
105   gst_element_class_add_pad_template (element_class,
106       gst_static_pad_template_get (&srctemplate));
107
108   gst_element_class_set_details (element_class, &gst_tcp_client_src_details);
109 }
110
111 static void
112 gst_tcp_client_src_class_init (GstTCPClientSrcClass * klass)
113 {
114   GObjectClass *gobject_class;
115   GstBaseSrcClass *gstbasesrc_class;
116   GstPushSrcClass *gstpush_src_class;
117
118   gobject_class = (GObjectClass *) klass;
119   gstbasesrc_class = (GstBaseSrcClass *) klass;
120   gstpush_src_class = (GstPushSrcClass *) klass;
121
122   gobject_class->set_property = gst_tcp_client_src_set_property;
123   gobject_class->get_property = gst_tcp_client_src_get_property;
124   gobject_class->finalize = gst_tcp_client_src_finalize;
125
126   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HOST,
127       g_param_spec_string ("host", "Host",
128           "The host IP address to receive packets from", TCP_DEFAULT_HOST,
129           G_PARAM_READWRITE));
130   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT,
131       g_param_spec_int ("port", "Port", "The port to receive packets from", 0,
132           TCP_HIGHEST_PORT, TCP_DEFAULT_PORT, G_PARAM_READWRITE));
133   g_object_class_install_property (gobject_class, PROP_PROTOCOL,
134       g_param_spec_enum ("protocol", "Protocol", "The protocol to wrap data in",
135           GST_TYPE_TCP_PROTOCOL, GST_TCP_PROTOCOL_NONE, G_PARAM_READWRITE));
136
137   gstbasesrc_class->get_caps = gst_tcp_client_src_getcaps;
138   gstbasesrc_class->start = gst_tcp_client_src_start;
139   gstbasesrc_class->stop = gst_tcp_client_src_stop;
140   gstbasesrc_class->unlock = gst_tcp_client_src_unlock;
141
142   gstpush_src_class->create = gst_tcp_client_src_create;
143
144   GST_DEBUG_CATEGORY_INIT (tcpclientsrc_debug, "tcpclientsrc", 0,
145       "TCP Client Source");
146 }
147
148 static void
149 gst_tcp_client_src_init (GstTCPClientSrc * this, GstTCPClientSrcClass * g_class)
150 {
151   this->port = TCP_DEFAULT_PORT;
152   this->host = g_strdup (TCP_DEFAULT_HOST);
153   this->sock_fd = -1;
154   this->protocol = GST_TCP_PROTOCOL_NONE;
155   this->caps = NULL;
156
157   READ_SOCKET (this) = -1;
158   WRITE_SOCKET (this) = -1;
159
160   gst_base_src_set_live (GST_BASE_SRC (this), TRUE);
161
162   GST_OBJECT_FLAG_UNSET (this, GST_TCP_CLIENT_SRC_OPEN);
163 }
164
165 static void
166 gst_tcp_client_src_finalize (GObject * gobject)
167 {
168   GstTCPClientSrc *this = GST_TCP_CLIENT_SRC (gobject);
169
170   g_free (this->host);
171 }
172
173 static GstCaps *
174 gst_tcp_client_src_getcaps (GstBaseSrc * bsrc)
175 {
176   GstTCPClientSrc *src;
177   GstCaps *caps = NULL;
178
179   src = GST_TCP_CLIENT_SRC (bsrc);
180
181   if (!GST_OBJECT_FLAG_IS_SET (src, GST_TCP_CLIENT_SRC_OPEN))
182     caps = gst_caps_new_any ();
183   else if (src->caps)
184     caps = gst_caps_copy (src->caps);
185   else
186     caps = gst_caps_new_any ();
187   GST_DEBUG_OBJECT (src, "returning caps %" GST_PTR_FORMAT, caps);
188   g_assert (GST_IS_CAPS (caps));
189   return caps;
190 }
191
192 static GstFlowReturn
193 gst_tcp_client_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
194 {
195   GstTCPClientSrc *src;
196   GstFlowReturn ret = GST_FLOW_OK;
197
198   src = GST_TCP_CLIENT_SRC (psrc);
199
200   if (!GST_OBJECT_FLAG_IS_SET (src, GST_TCP_CLIENT_SRC_OPEN))
201     goto wrong_state;
202
203   GST_LOG_OBJECT (src, "asked for a buffer");
204
205   /* read the buffer header if we're using a protocol */
206   switch (src->protocol) {
207     case GST_TCP_PROTOCOL_NONE:
208       ret = gst_tcp_read_buffer (GST_ELEMENT (src), src->sock_fd,
209           READ_SOCKET (src), outbuf);
210       break;
211
212     case GST_TCP_PROTOCOL_GDP:
213       /* get the caps if we're using GDP */
214       if (!src->caps_received) {
215         GstCaps *caps;
216
217         GST_DEBUG_OBJECT (src, "getting caps through GDP");
218         ret = gst_tcp_gdp_read_caps (GST_ELEMENT (src), src->sock_fd,
219             READ_SOCKET (src), &caps);
220
221         if (ret != GST_FLOW_OK)
222           goto no_caps;
223
224         src->caps_received = TRUE;
225         src->caps = caps;
226       }
227
228       ret = gst_tcp_gdp_read_buffer (GST_ELEMENT (src), src->sock_fd,
229           READ_SOCKET (src), outbuf);
230       break;
231     default:
232       /* need to assert as buf == NULL */
233       g_assert ("Unhandled protocol type");
234       break;
235   }
236
237   if (ret == GST_FLOW_OK) {
238     GST_LOG_OBJECT (src,
239         "Returning buffer from _get of size %d, ts %"
240         GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT
241         ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
242         GST_BUFFER_SIZE (*outbuf),
243         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*outbuf)),
244         GST_TIME_ARGS (GST_BUFFER_DURATION (*outbuf)),
245         GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf));
246
247     gst_buffer_set_caps (*outbuf, src->caps);
248   }
249
250   return ret;
251
252 wrong_state:
253   {
254     GST_DEBUG_OBJECT (src, "connection to closed, cannot read data");
255     return GST_FLOW_WRONG_STATE;
256   }
257 no_caps:
258   {
259     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
260         ("Could not read caps through GDP"));
261     return ret;
262   }
263 }
264
265 static void
266 gst_tcp_client_src_set_property (GObject * object, guint prop_id,
267     const GValue * value, GParamSpec * pspec)
268 {
269   GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
270
271   switch (prop_id) {
272     case PROP_HOST:
273       if (!g_value_get_string (value)) {
274         g_warning ("host property cannot be NULL");
275         break;
276       }
277       g_free (tcpclientsrc->host);
278       tcpclientsrc->host = g_strdup (g_value_get_string (value));
279       break;
280     case PROP_PORT:
281       tcpclientsrc->port = g_value_get_int (value);
282       break;
283     case PROP_PROTOCOL:
284       tcpclientsrc->protocol = g_value_get_enum (value);
285       break;
286
287     default:
288       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289       break;
290   }
291 }
292
293 static void
294 gst_tcp_client_src_get_property (GObject * object, guint prop_id,
295     GValue * value, GParamSpec * pspec)
296 {
297   GstTCPClientSrc *tcpclientsrc = GST_TCP_CLIENT_SRC (object);
298
299   switch (prop_id) {
300     case PROP_HOST:
301       g_value_set_string (value, tcpclientsrc->host);
302       break;
303     case PROP_PORT:
304       g_value_set_int (value, tcpclientsrc->port);
305       break;
306     case PROP_PROTOCOL:
307       g_value_set_enum (value, tcpclientsrc->protocol);
308       break;
309
310     default:
311       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
312       break;
313   }
314 }
315
316 /* create a socket for connecting to remote server */
317 static gboolean
318 gst_tcp_client_src_start (GstBaseSrc * bsrc)
319 {
320   int ret;
321   gchar *ip;
322   GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
323
324   /* create the control sockets before anything */
325   if (socketpair (PF_UNIX, SOCK_STREAM, 0, CONTROL_SOCKETS (src)) < 0)
326     goto socket_pair;
327
328   fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK);
329   fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK);
330
331   /* create receiving client socket */
332   GST_DEBUG_OBJECT (src, "opening receiving client socket to %s:%d",
333       src->host, src->port);
334
335   if ((src->sock_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
336     goto no_socket;
337
338   GST_DEBUG_OBJECT (src, "opened receiving client socket with fd %d",
339       src->sock_fd);
340   GST_OBJECT_FLAG_SET (src, GST_TCP_CLIENT_SRC_OPEN);
341
342   /* look up name if we need to */
343   if (!(ip = gst_tcp_host_to_ip (GST_ELEMENT (src), src->host)))
344     goto name_resolv;
345
346   GST_DEBUG_OBJECT (src, "IP address for host %s is %s", src->host, ip);
347
348   /* connect to server */
349   memset (&src->server_sin, 0, sizeof (src->server_sin));
350   src->server_sin.sin_family = AF_INET; /* network socket */
351   src->server_sin.sin_port = htons (src->port); /* on port */
352   src->server_sin.sin_addr.s_addr = inet_addr (ip);     /* on host ip */
353   g_free (ip);
354
355   GST_DEBUG_OBJECT (src, "connecting to server");
356   ret = connect (src->sock_fd, (struct sockaddr *) &src->server_sin,
357       sizeof (src->server_sin));
358
359   if (ret) {
360     gst_tcp_client_src_stop (GST_BASE_SRC (src));
361     switch (errno) {
362       case ECONNREFUSED:
363         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
364             (_("Connection to %s:%d refused."), src->host, src->port), (NULL));
365         return FALSE;
366         break;
367       default:
368         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
369             ("connect to %s:%d failed: %s", src->host, src->port,
370                 g_strerror (errno)));
371         return FALSE;
372         break;
373     }
374   }
375
376   return TRUE;
377
378 socket_pair:
379   {
380     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
381         GST_ERROR_SYSTEM);
382     return FALSE;
383   }
384 no_socket:
385   {
386     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
387     return FALSE;
388   }
389 name_resolv:
390   {
391     gst_tcp_client_src_stop (GST_BASE_SRC (src));
392     return FALSE;
393   }
394 }
395
396 /* close the socket and associated resources
397  * unset OPEN flag
398  * used both to recover from errors and go to NULL state */
399 static gboolean
400 gst_tcp_client_src_stop (GstBaseSrc * bsrc)
401 {
402   GstTCPClientSrc *src;
403
404   src = GST_TCP_CLIENT_SRC (bsrc);
405
406   GST_DEBUG_OBJECT (src, "closing socket");
407   if (src->sock_fd != -1) {
408     close (src->sock_fd);
409     src->sock_fd = -1;
410   }
411   src->caps_received = FALSE;
412   if (src->caps) {
413     gst_caps_unref (src->caps);
414     src->caps = NULL;
415   }
416   GST_OBJECT_FLAG_UNSET (src, GST_TCP_CLIENT_SRC_OPEN);
417
418   close (READ_SOCKET (src));
419   close (WRITE_SOCKET (src));
420   READ_SOCKET (src) = -1;
421   WRITE_SOCKET (src) = -1;
422
423   return TRUE;
424 }
425
426 /* will be called only between calls to start() and stop() */
427 static gboolean
428 gst_tcp_client_src_unlock (GstBaseSrc * bsrc)
429 {
430   GstTCPClientSrc *src = GST_TCP_CLIENT_SRC (bsrc);
431
432   SEND_COMMAND (src, CONTROL_STOP);
433
434   return TRUE;
435 }