PadConnect -> PadLink
[platform/upstream/gstreamer.git] / gst / udp / gstudpsink.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20
21 #include "gstudpsink.h"
22
23 #define UDP_DEFAULT_HOST        "localhost"
24 #define UDP_DEFAULT_PORT        4951
25 #define UDP_DEFAULT_CONTROL     1
26
27 /* elementfactory information */
28 GstElementDetails gst_udpsink_details = {
29   "UDP packet sender",
30   "Sink/Network",
31   "LGPL",
32   "Send data over the network via UDP",
33   VERSION,
34   "Wim Taymans <wim.taymans@chello.be>",
35   "(C) 2001",
36 };
37
38 /* UDPSink signals and args */
39 enum {
40   FRAME_ENCODED,
41   /* FILL ME */
42   LAST_SIGNAL
43 };
44
45 enum {
46   ARG_0,
47   ARG_HOST,
48   ARG_PORT,
49   ARG_CONTROL,
50   ARG_MTU
51   /* FILL ME */
52 };
53
54 #define GST_TYPE_UDPSINK_CONTROL        (gst_udpsink_control_get_type())
55 static GType
56 gst_udpsink_control_get_type(void) {
57   static GType udpsink_control_type = 0;
58   static GEnumValue udpsink_control[] = {
59     {CONTROL_NONE, "1", "none"},
60     {CONTROL_UDP, "2", "udp"},
61     {CONTROL_TCP, "3", "tcp"},
62     {CONTROL_ZERO, NULL, NULL},
63   };
64   if (!udpsink_control_type) {
65     udpsink_control_type = g_enum_register_static("GstUDPSinkControl", udpsink_control);
66   }
67   return udpsink_control_type;
68 }
69
70 static void             gst_udpsink_class_init          (GstUDPSink *klass);
71 static void             gst_udpsink_init                (GstUDPSink *udpsink);
72
73 static void             gst_udpsink_set_clock           (GstElement *element, GstClock *clock);
74
75 static void             gst_udpsink_chain               (GstPad *pad,GstBuffer *buf);
76 static GstElementStateReturn gst_udpsink_change_state   (GstElement *element);
77
78 static void             gst_udpsink_set_property        (GObject *object, guint prop_id, 
79                                                          const GValue *value, GParamSpec *pspec);
80 static void             gst_udpsink_get_property        (GObject *object, guint prop_id, 
81                                                          GValue *value, GParamSpec *pspec);
82
83
84 static GstElementClass *parent_class = NULL;
85 /*static guint gst_udpsink_signals[LAST_SIGNAL] = { 0 }; */
86
87 GType
88 gst_udpsink_get_type (void)
89 {
90   static GType udpsink_type = 0;
91
92
93   if (!udpsink_type) {
94     static const GTypeInfo udpsink_info = {
95       sizeof(GstUDPSinkClass),
96       NULL,
97       NULL,
98       (GClassInitFunc)gst_udpsink_class_init,
99       NULL,
100       NULL,
101       sizeof(GstUDPSink),
102       0,
103       (GInstanceInitFunc)gst_udpsink_init,
104       NULL
105     };
106     udpsink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstUDPSink", &udpsink_info, 0);
107   }
108   return udpsink_type;
109 }
110
111 static void
112 gst_udpsink_class_init (GstUDPSink *klass)
113 {
114   GObjectClass *gobject_class;
115   GstElementClass *gstelement_class;
116
117   gobject_class = (GObjectClass*) klass;
118   gstelement_class = (GstElementClass*) klass;
119
120   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
121
122   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_HOST,
123     g_param_spec_string ("host", "nost", "The host to send the packets to",
124                          UDP_DEFAULT_HOST, G_PARAM_READWRITE)); 
125   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PORT,
126     g_param_spec_int ("port", "port", "The port to send the packets to",
127                        0, 32768, UDP_DEFAULT_PORT, G_PARAM_READWRITE)); 
128   g_object_class_install_property (gobject_class, ARG_CONTROL,
129     g_param_spec_enum ("control", "control", "The type of control",
130                        GST_TYPE_UDPSINK_CONTROL, CONTROL_UDP, G_PARAM_READWRITE));
131   g_object_class_install_property (gobject_class, ARG_MTU, 
132                   g_param_spec_int ("mtu", "mtu", "mtu", G_MININT, G_MAXINT, 
133                           0, G_PARAM_READWRITE)); /* CHECKME */
134
135   gobject_class->set_property = gst_udpsink_set_property;
136   gobject_class->get_property = gst_udpsink_get_property;
137
138   gstelement_class->change_state = gst_udpsink_change_state;
139   gstelement_class->set_clock = gst_udpsink_set_clock;
140 }
141
142
143 static GstPadLinkReturn
144 gst_udpsink_sinkconnect (GstPad *pad, GstCaps *caps)
145 {
146   GstUDPSink *udpsink;
147   struct sockaddr_in serv_addr;
148   struct hostent *serverhost;
149   int fd;
150   FILE *f;
151   guint bc_val;
152 #ifndef GST_DISABLE_LOADSAVE
153   xmlDocPtr doc;
154   xmlChar *buf;
155   int buf_size;
156
157   udpsink = GST_UDPSINK (gst_pad_get_parent (pad));
158   
159   memset(&serv_addr, 0, sizeof(serv_addr));
160   
161   /* its a name rather than an ipnum */
162   serverhost = gethostbyname(udpsink->host);
163   if (serverhost == (struct hostent *)0) {
164         perror("gethostbyname");
165         return GST_PAD_LINK_REFUSED;
166   }
167   
168   memmove(&serv_addr.sin_addr,serverhost->h_addr, serverhost->h_length);
169
170   serv_addr.sin_family = AF_INET;
171   serv_addr.sin_port = htons(udpsink->port+1);
172             
173   doc = xmlNewDoc ("1.0");
174   doc->xmlRootNode = xmlNewDocNode (doc, NULL, "NewCaps", NULL);
175
176   gst_caps_save_thyself (caps, doc->xmlRootNode);
177
178   switch (udpsink->control) {
179     case CONTROL_UDP:
180             if ((fd = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
181                 perror("socket");
182                 return GST_PAD_LINK_REFUSED;
183             }
184
185             /* We can only do broadcast in udp */
186             bc_val = 1;
187             setsockopt (fd,SOL_SOCKET, SO_BROADCAST, &bc_val, sizeof (bc_val));
188             
189             xmlDocDumpMemory(doc, &buf, &buf_size);
190
191             if (sendto (fd, buf, buf_size, 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1)
192             {
193                 perror("sending");
194                 return GST_PAD_LINK_REFUSED;
195             } 
196
197             close (fd);
198             break;
199     case CONTROL_TCP:
200             if ((fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
201                 perror("socket");
202                 return GST_PAD_LINK_REFUSED;
203             }
204   
205             if (connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) != 0) {
206                 g_printerr ("udpsink: connect to %s port %d failed: %s\n",
207                              udpsink->host, udpsink->port, g_strerror(errno));
208                 return GST_PAD_LINK_REFUSED;
209             }
210   
211             f = fdopen (dup (fd), "wb");
212
213             xmlDocDump(f, doc);
214             fclose (f);
215             close (fd);
216             
217             break;
218     case CONTROL_NONE:
219             return GST_PAD_LINK_OK;
220             break;
221     default:
222             return GST_PAD_LINK_REFUSED;
223             break;
224   }
225 #endif
226   
227   return GST_PAD_LINK_OK;
228 }
229
230 static void
231 gst_udpsink_set_clock (GstElement *element, GstClock *clock)
232 {
233   GstUDPSink *udpsink;
234               
235   udpsink = GST_UDPSINK (element);
236
237   udpsink->clock = clock;
238 }
239
240 static void
241 gst_udpsink_init (GstUDPSink *udpsink)
242 {
243   /* create the sink and src pads */
244   udpsink->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
245   gst_element_add_pad (GST_ELEMENT (udpsink), udpsink->sinkpad);
246   gst_pad_set_chain_function (udpsink->sinkpad, gst_udpsink_chain);
247   gst_pad_set_link_function (udpsink->sinkpad, gst_udpsink_sinkconnect);
248
249   udpsink->host = g_strdup (UDP_DEFAULT_HOST);
250   udpsink->port = UDP_DEFAULT_PORT;
251   udpsink->control = CONTROL_UDP;
252   udpsink->mtu = 1024;
253   
254   udpsink->clock = NULL;
255 }
256
257 static void
258 gst_udpsink_chain (GstPad *pad, GstBuffer *buf)
259 {
260   GstUDPSink *udpsink;
261   guint tolen, i;
262
263   g_return_if_fail (pad != NULL);
264   g_return_if_fail (GST_IS_PAD (pad));
265   g_return_if_fail (buf != NULL);
266
267   udpsink = GST_UDPSINK (GST_OBJECT_PARENT (pad));
268   
269   if (udpsink->clock) {
270     GstClockID id = gst_clock_new_single_shot_id (udpsink->clock, GST_BUFFER_TIMESTAMP (buf));
271
272     GST_DEBUG (0, "udpsink: clock wait: %llu\n", GST_BUFFER_TIMESTAMP (buf));
273     gst_element_clock_wait (GST_ELEMENT (udpsink), id, NULL);
274     gst_clock_id_free (id);
275   }
276   
277   tolen = sizeof(udpsink->theiraddr);
278   
279   for (i = 0; i < GST_BUFFER_SIZE (buf); i += udpsink->mtu) {
280     if (GST_BUFFER_SIZE (buf) - i > udpsink->mtu) {
281         if (sendto (udpsink->sock, GST_BUFFER_DATA (buf) + i, 
282             udpsink->mtu, 0, (struct sockaddr *) &udpsink->theiraddr, 
283             tolen) == -1) {
284                 perror("sending");
285         } 
286     }
287     else {
288         if (sendto (udpsink->sock, GST_BUFFER_DATA (buf) + i, 
289             GST_BUFFER_SIZE (buf) -i, 0, 
290             (struct sockaddr *) &udpsink->theiraddr, tolen) == -1) {
291                 perror("sending");
292         } 
293     }
294   }
295
296   gst_buffer_unref(buf);
297 }
298
299 static void
300 gst_udpsink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
301 {
302   GstUDPSink *udpsink;
303
304   /* it's not null if we got it, but it might not be ours */
305   g_return_if_fail(GST_IS_UDPSINK(object));
306   udpsink = GST_UDPSINK(object);
307
308   switch (prop_id) {
309     case ARG_HOST:
310       if (udpsink->host != NULL) g_free(udpsink->host);
311       if (g_value_get_string (value) == NULL)
312         udpsink->host = NULL;
313       else
314         udpsink->host = g_strdup (g_value_get_string (value));
315       break;
316     case ARG_PORT:
317         udpsink->port = g_value_get_int (value);
318       break;
319     case ARG_CONTROL:
320         udpsink->control = g_value_get_enum (value);
321       break;
322     case ARG_MTU:
323       udpsink->mtu = g_value_get_int (value);
324       break;
325     default:
326       break;
327   }
328 }
329
330 static void
331 gst_udpsink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
332 {
333   GstUDPSink *udpsink;
334
335   /* it's not null if we got it, but it might not be ours */
336   g_return_if_fail(GST_IS_UDPSINK(object));
337   udpsink = GST_UDPSINK(object);
338
339   switch (prop_id) {
340     case ARG_HOST:
341       g_value_set_string (value, udpsink->host);
342       break;
343     case ARG_PORT:
344       g_value_set_int (value, udpsink->port);
345       break;
346     case ARG_CONTROL:
347       g_value_set_enum (value, udpsink->control);
348       break;
349     case ARG_MTU:
350       g_value_set_int (value, udpsink->mtu);
351       break;
352     default:
353       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
354       break;
355   }
356 }
357
358
359 /* create a socket for sending to remote machine */
360 static gboolean
361 gst_udpsink_init_send (GstUDPSink *sink)
362 {
363   struct hostent *he;
364   guint bc_val;
365
366   bzero (&sink->theiraddr, sizeof (sink->theiraddr));
367   sink->theiraddr.sin_family = AF_INET;         /* host byte order */
368   sink->theiraddr.sin_port = htons (sink->port);     /* short, network byte order */
369   if ((he = gethostbyname (sink->host)) == NULL) {
370     perror("gethostbyname");
371     return FALSE;
372   }
373   sink->theiraddr.sin_addr = *((struct in_addr *) he->h_addr);
374
375   if ((sink->sock = socket (AF_INET, SOCK_DGRAM, 0)) == -1) {
376      perror("socket");
377      return FALSE;
378   }
379
380   bc_val = 1;
381   setsockopt (sink->sock, SOL_SOCKET, SO_BROADCAST, &bc_val, sizeof (bc_val));
382   
383   GST_FLAG_SET (sink, GST_UDPSINK_OPEN);
384
385   return TRUE;
386 }
387
388 static void
389 gst_udpsink_close (GstUDPSink *sink)
390 {
391   close (sink->sock);
392
393   GST_FLAG_UNSET (sink, GST_UDPSINK_OPEN);
394 }
395
396 static GstElementStateReturn
397 gst_udpsink_change_state (GstElement *element)
398 {
399   g_return_val_if_fail (GST_IS_UDPSINK (element), GST_STATE_FAILURE);
400
401   if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
402     if (GST_FLAG_IS_SET (element, GST_UDPSINK_OPEN))
403       gst_udpsink_close (GST_UDPSINK (element));
404   } else {
405     if (!GST_FLAG_IS_SET (element, GST_UDPSINK_OPEN)) {
406       if (!gst_udpsink_init_send (GST_UDPSINK (element)))
407         return GST_STATE_FAILURE;
408     }
409   }
410
411   if (GST_ELEMENT_CLASS (parent_class)->change_state)
412     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
413
414   return GST_STATE_SUCCESS;
415 }
416