2002-03-20 21:45:03 +00:00
|
|
|
/* GStreamer
|
2001-12-22 23:27:31 +00:00
|
|
|
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-06-29 19:46:12 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2001-12-22 23:27:31 +00:00
|
|
|
#include "gstudpsrc.h"
|
2003-06-29 19:46:12 +00:00
|
|
|
#include <unistd.h>
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2003-06-09 23:13:40 +00:00
|
|
|
#define UDP_DEFAULT_PORT 4951
|
|
|
|
#define UDP_DEFAULT_MULTICAST_GROUP "0.0.0.0"
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
/* elementfactory information */
|
2004-03-14 22:34:33 +00:00
|
|
|
static GstElementDetails gst_udpsrc_details =
|
|
|
|
GST_ELEMENT_DETAILS ("UDP packet receiver",
|
|
|
|
"Source/Network",
|
|
|
|
"Receive data over the network via UDP",
|
|
|
|
"Wim Taymans <wim.taymans@chello.be>");
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
/* UDPSrc signals and args */
|
2004-03-14 22:34:33 +00:00
|
|
|
enum
|
|
|
|
{
|
2001-12-22 23:27:31 +00:00
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
enum
|
|
|
|
{
|
2001-12-22 23:27:31 +00:00
|
|
|
ARG_0,
|
|
|
|
ARG_PORT,
|
2003-06-09 23:13:40 +00:00
|
|
|
ARG_CONTROL,
|
|
|
|
ARG_MULTICAST_GROUP
|
2004-03-14 22:34:33 +00:00
|
|
|
/* FILL ME */
|
2001-12-22 23:27:31 +00:00
|
|
|
};
|
|
|
|
|
2002-11-16 18:55:43 +00:00
|
|
|
#define GST_TYPE_UDPSRC_CONTROL (gst_udpsrc_control_get_type())
|
|
|
|
static GType
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_udpsrc_control_get_type (void)
|
|
|
|
{
|
2002-11-16 18:55:43 +00:00
|
|
|
static GType udpsrc_control_type = 0;
|
|
|
|
static GEnumValue udpsrc_control[] = {
|
|
|
|
{CONTROL_NONE, "1", "none"},
|
|
|
|
{CONTROL_UDP, "2", "udp"},
|
|
|
|
{CONTROL_TCP, "3", "tcp"},
|
|
|
|
{CONTROL_ZERO, NULL, NULL},
|
|
|
|
};
|
2004-03-15 19:32:27 +00:00
|
|
|
|
2002-11-16 18:55:43 +00:00
|
|
|
if (!udpsrc_control_type) {
|
2004-03-14 22:34:33 +00:00
|
|
|
udpsrc_control_type =
|
2004-03-15 19:32:27 +00:00
|
|
|
g_enum_register_static ("GstUDPSrcControl", udpsrc_control);
|
2002-11-16 18:55:43 +00:00
|
|
|
}
|
|
|
|
return udpsrc_control_type;
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
static void gst_udpsrc_base_init (gpointer g_class);
|
|
|
|
static void gst_udpsrc_class_init (GstUDPSrc * klass);
|
|
|
|
static void gst_udpsrc_init (GstUDPSrc * udpsrc);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
static GstData *gst_udpsrc_get (GstPad * pad);
|
|
|
|
static GstElementStateReturn gst_udpsrc_change_state (GstElement * element);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
static void gst_udpsrc_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_udpsrc_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_udpsrc_set_clock (GstElement * element, GstClock * clock);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
static GstElementClass *parent_class = NULL;
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2002-03-19 04:10:05 +00:00
|
|
|
/*static guint gst_udpsrc_signals[LAST_SIGNAL] = { 0 }; */
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
GType
|
|
|
|
gst_udpsrc_get_type (void)
|
|
|
|
{
|
|
|
|
static GType udpsrc_type = 0;
|
|
|
|
|
|
|
|
if (!udpsrc_type) {
|
|
|
|
static const GTypeInfo udpsrc_info = {
|
2004-03-14 22:34:33 +00:00
|
|
|
sizeof (GstUDPSrcClass),
|
2003-11-02 22:34:11 +00:00
|
|
|
gst_udpsrc_base_init,
|
2001-12-22 23:27:31 +00:00
|
|
|
NULL,
|
2004-03-14 22:34:33 +00:00
|
|
|
(GClassInitFunc) gst_udpsrc_class_init,
|
2001-12-22 23:27:31 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2004-03-14 22:34:33 +00:00
|
|
|
sizeof (GstUDPSrc),
|
2001-12-22 23:27:31 +00:00
|
|
|
0,
|
2004-03-14 22:34:33 +00:00
|
|
|
(GInstanceInitFunc) gst_udpsrc_init,
|
2001-12-22 23:27:31 +00:00
|
|
|
NULL
|
|
|
|
};
|
2004-03-15 19:32:27 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
udpsrc_type =
|
2004-03-15 19:32:27 +00:00
|
|
|
g_type_register_static (GST_TYPE_ELEMENT, "GstUDPSrc", &udpsrc_info, 0);
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
|
|
|
return udpsrc_type;
|
|
|
|
}
|
|
|
|
|
2003-11-02 22:34:11 +00:00
|
|
|
static void
|
|
|
|
gst_udpsrc_base_init (gpointer g_class)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
|
|
|
|
|
|
|
gst_element_class_set_details (element_class, &gst_udpsrc_details);
|
|
|
|
}
|
|
|
|
|
2001-12-22 23:27:31 +00:00
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_udpsrc_class_init (GstUDPSrc * klass)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
gstelement_class = (GstElementClass *) klass;
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
|
|
|
|
|
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PORT,
|
2004-03-14 22:34:33 +00:00
|
|
|
g_param_spec_int ("port", "port", "The port to receive the packets from",
|
2004-03-15 19:32:27 +00:00
|
|
|
0, 32768, UDP_DEFAULT_PORT, G_PARAM_READWRITE));
|
2002-11-16 18:55:43 +00:00
|
|
|
g_object_class_install_property (gobject_class, ARG_CONTROL,
|
2004-03-14 22:34:33 +00:00
|
|
|
g_param_spec_enum ("control", "control", "The type of control",
|
2004-03-15 19:32:27 +00:00
|
|
|
GST_TYPE_UDPSRC_CONTROL, CONTROL_UDP, G_PARAM_READWRITE));
|
2003-06-09 23:13:40 +00:00
|
|
|
g_object_class_install_property (gobject_class, ARG_MULTICAST_GROUP,
|
2004-03-14 22:34:33 +00:00
|
|
|
g_param_spec_string ("multicast_group", "multicast_group",
|
2004-03-15 19:32:27 +00:00
|
|
|
"The Address of multicast group to join",
|
|
|
|
UDP_DEFAULT_MULTICAST_GROUP, G_PARAM_READWRITE));
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
gobject_class->set_property = gst_udpsrc_set_property;
|
|
|
|
gobject_class->get_property = gst_udpsrc_get_property;
|
|
|
|
|
|
|
|
gstelement_class->change_state = gst_udpsrc_change_state;
|
2002-12-15 22:45:53 +00:00
|
|
|
gstelement_class->set_clock = gst_udpsrc_set_clock;
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
|
|
|
|
2002-12-15 22:45:53 +00:00
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_udpsrc_set_clock (GstElement * element, GstClock * clock)
|
2002-12-15 22:45:53 +00:00
|
|
|
{
|
|
|
|
GstUDPSrc *udpsrc;
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2002-12-15 22:45:53 +00:00
|
|
|
udpsrc = GST_UDPSRC (element);
|
|
|
|
|
|
|
|
udpsrc->clock = clock;
|
|
|
|
}
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_udpsrc_init (GstUDPSrc * udpsrc)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
|
|
|
/* create the src and src pads */
|
|
|
|
udpsrc->srcpad = gst_pad_new ("src", GST_PAD_SRC);
|
|
|
|
gst_element_add_pad (GST_ELEMENT (udpsrc), udpsrc->srcpad);
|
|
|
|
gst_pad_set_get_function (udpsrc->srcpad, gst_udpsrc_get);
|
|
|
|
|
|
|
|
udpsrc->port = UDP_DEFAULT_PORT;
|
2002-11-16 18:55:43 +00:00
|
|
|
udpsrc->control = CONTROL_UDP;
|
2002-12-15 22:45:53 +00:00
|
|
|
udpsrc->clock = NULL;
|
2003-04-06 22:20:42 +00:00
|
|
|
udpsrc->sock = -1;
|
|
|
|
udpsrc->control_sock = -1;
|
2003-06-09 23:13:40 +00:00
|
|
|
udpsrc->multi_group = g_strdup (UDP_DEFAULT_MULTICAST_GROUP);
|
2002-12-15 22:45:53 +00:00
|
|
|
|
|
|
|
udpsrc->first_buf = TRUE;
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
static GstData *
|
|
|
|
gst_udpsrc_get (GstPad * pad)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
|
|
|
GstUDPSrc *udpsrc;
|
|
|
|
GstBuffer *outbuf;
|
|
|
|
struct sockaddr_in tmpaddr;
|
2002-11-08 01:56:49 +00:00
|
|
|
socklen_t len;
|
|
|
|
gint numbytes;
|
2001-12-22 23:27:31 +00:00
|
|
|
fd_set read_fds;
|
2002-11-16 18:55:43 +00:00
|
|
|
guint max_sock;
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (pad != NULL, NULL);
|
|
|
|
g_return_val_if_fail (GST_IS_PAD (pad), NULL);
|
|
|
|
|
|
|
|
udpsrc = GST_UDPSRC (GST_OBJECT_PARENT (pad));
|
|
|
|
|
|
|
|
FD_ZERO (&read_fds);
|
|
|
|
FD_SET (udpsrc->sock, &read_fds);
|
2002-11-16 18:55:43 +00:00
|
|
|
if (udpsrc->control != CONTROL_NONE) {
|
2004-03-14 22:34:33 +00:00
|
|
|
FD_SET (udpsrc->control_sock, &read_fds);
|
2002-11-16 18:55:43 +00:00
|
|
|
}
|
2004-03-14 22:34:33 +00:00
|
|
|
max_sock = MAX (udpsrc->sock, udpsrc->control_sock);
|
2002-11-16 18:55:43 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
if (select (max_sock + 1, &read_fds, NULL, NULL, NULL) > 0) {
|
2003-04-06 22:20:42 +00:00
|
|
|
if ((udpsrc->control_sock != -1) &&
|
2004-03-15 19:32:27 +00:00
|
|
|
FD_ISSET (udpsrc->control_sock, &read_fds)) {
|
2001-12-22 23:27:31 +00:00
|
|
|
#ifndef GST_DISABLE_LOADSAVE
|
|
|
|
guchar *buf;
|
|
|
|
int ret;
|
|
|
|
int fdread;
|
|
|
|
struct sockaddr addr;
|
|
|
|
xmlDocPtr doc;
|
|
|
|
GstCaps *caps;
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
buf = g_malloc (1024 * 10);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2002-11-16 18:55:43 +00:00
|
|
|
switch (udpsrc->control) {
|
2004-03-15 19:32:27 +00:00
|
|
|
case CONTROL_TCP:
|
|
|
|
len = sizeof (struct sockaddr);
|
|
|
|
fdread = accept (udpsrc->control_sock, &addr, &len);
|
|
|
|
if (fdread < 0) {
|
|
|
|
perror ("accept");
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = read (fdread, buf, 1024 * 10);
|
|
|
|
break;
|
|
|
|
case CONTROL_UDP:
|
|
|
|
len = sizeof (struct sockaddr);
|
|
|
|
ret =
|
|
|
|
recvfrom (udpsrc->control_sock, buf, 1024 * 10, 0,
|
|
|
|
(struct sockaddr *) &tmpaddr, &len);
|
|
|
|
if (ret < 0) {
|
|
|
|
perror ("recvfrom");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CONTROL_NONE:
|
|
|
|
default:
|
|
|
|
g_free (buf);
|
|
|
|
return NULL;
|
|
|
|
break;
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
2002-11-16 18:55:43 +00:00
|
|
|
|
2001-12-22 23:27:31 +00:00
|
|
|
buf[ret] = '\0';
|
2004-03-14 22:34:33 +00:00
|
|
|
doc = xmlParseMemory (buf, ret);
|
|
|
|
caps = gst_caps_load_thyself (doc->xmlRootNode);
|
2004-03-06 01:01:28 +00:00
|
|
|
if (caps == NULL) {
|
2004-03-15 19:32:27 +00:00
|
|
|
return NULL;
|
2004-03-06 01:01:28 +00:00
|
|
|
}
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2002-12-18 23:07:09 +00:00
|
|
|
/* foward the connect, we don't signal back the result here... */
|
2004-03-06 01:01:28 +00:00
|
|
|
if (gst_caps_is_fixed (caps)) {
|
2004-03-15 19:32:27 +00:00
|
|
|
gst_pad_try_set_caps (udpsrc->srcpad, caps);
|
2004-03-06 01:01:28 +00:00
|
|
|
} else {
|
2004-03-15 19:32:27 +00:00
|
|
|
GST_ERROR ("caps %" GST_PTR_FORMAT, caps);
|
|
|
|
GST_ELEMENT_ERROR (udpsrc, CORE, NEGOTIATION, (NULL),
|
|
|
|
("Got unfixed caps from peer"));
|
2004-03-06 01:01:28 +00:00
|
|
|
}
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
#endif
|
2002-11-16 18:55:43 +00:00
|
|
|
g_free (buf);
|
2001-12-22 23:27:31 +00:00
|
|
|
outbuf = NULL;
|
2004-03-14 22:34:33 +00:00
|
|
|
} else {
|
2001-12-22 23:27:31 +00:00
|
|
|
outbuf = gst_buffer_new ();
|
|
|
|
GST_BUFFER_DATA (outbuf) = g_malloc (24000);
|
|
|
|
GST_BUFFER_SIZE (outbuf) = 24000;
|
|
|
|
|
2002-12-15 22:45:53 +00:00
|
|
|
if (udpsrc->first_buf) {
|
2004-03-15 19:32:27 +00:00
|
|
|
if (udpsrc->clock) {
|
|
|
|
GstClockTime current_time;
|
|
|
|
GstEvent *discont;
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2004-03-15 19:32:27 +00:00
|
|
|
current_time = gst_clock_get_time (udpsrc->clock);
|
2002-12-15 22:45:53 +00:00
|
|
|
|
2004-03-15 19:32:27 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (outbuf) = current_time;
|
2002-12-15 22:45:53 +00:00
|
|
|
|
2004-03-15 19:32:27 +00:00
|
|
|
discont = gst_event_new_discontinuous (FALSE, GST_FORMAT_TIME,
|
|
|
|
current_time, NULL);
|
2002-12-15 22:45:53 +00:00
|
|
|
|
2004-03-15 19:32:27 +00:00
|
|
|
gst_pad_push (udpsrc->srcpad, GST_DATA (discont));
|
|
|
|
}
|
2002-12-15 22:45:53 +00:00
|
|
|
|
2004-03-15 19:32:27 +00:00
|
|
|
udpsrc->first_buf = FALSE;
|
2002-12-15 22:45:53 +00:00
|
|
|
}
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2002-12-15 22:45:53 +00:00
|
|
|
else {
|
2004-03-15 19:32:27 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE;
|
2002-12-15 22:45:53 +00:00
|
|
|
}
|
|
|
|
|
2003-04-06 22:20:42 +00:00
|
|
|
len = sizeof (struct sockaddr);
|
2001-12-22 23:27:31 +00:00
|
|
|
numbytes = recvfrom (udpsrc->sock, GST_BUFFER_DATA (outbuf),
|
2004-03-15 19:32:27 +00:00
|
|
|
GST_BUFFER_SIZE (outbuf), 0, (struct sockaddr *) &tmpaddr, &len);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
if (numbytes != -1) {
|
2004-03-15 19:32:27 +00:00
|
|
|
GST_BUFFER_SIZE (outbuf) = numbytes;
|
2004-03-14 22:34:33 +00:00
|
|
|
} else {
|
2004-03-15 19:32:27 +00:00
|
|
|
perror ("recvfrom");
|
|
|
|
gst_buffer_unref (outbuf);
|
|
|
|
outbuf = NULL;
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
2004-03-14 22:34:33 +00:00
|
|
|
} else {
|
2001-12-22 23:27:31 +00:00
|
|
|
perror ("select");
|
|
|
|
outbuf = NULL;
|
|
|
|
}
|
2003-10-08 16:08:18 +00:00
|
|
|
return GST_DATA (outbuf);
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_udpsrc_set_property (GObject * object, guint prop_id, const GValue * value,
|
|
|
|
GParamSpec * pspec)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
|
|
|
GstUDPSrc *udpsrc;
|
|
|
|
|
|
|
|
/* it's not null if we got it, but it might not be ours */
|
2004-03-14 22:34:33 +00:00
|
|
|
g_return_if_fail (GST_IS_UDPSRC (object));
|
|
|
|
udpsrc = GST_UDPSRC (object);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case ARG_PORT:
|
2004-03-14 22:34:33 +00:00
|
|
|
udpsrc->port = g_value_get_int (value);
|
2001-12-22 23:27:31 +00:00
|
|
|
break;
|
2003-06-09 23:13:40 +00:00
|
|
|
case ARG_MULTICAST_GROUP:
|
2004-03-14 22:34:33 +00:00
|
|
|
g_free (udpsrc->multi_group);
|
|
|
|
|
2003-06-09 23:13:40 +00:00
|
|
|
if (g_value_get_string (value) == NULL)
|
2004-03-15 19:32:27 +00:00
|
|
|
udpsrc->multi_group = g_strdup (UDP_DEFAULT_MULTICAST_GROUP);
|
2003-06-09 23:13:40 +00:00
|
|
|
else
|
2004-03-15 19:32:27 +00:00
|
|
|
udpsrc->multi_group = g_strdup (g_value_get_string (value));
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2003-06-09 23:13:40 +00:00
|
|
|
break;
|
2002-11-16 18:55:43 +00:00
|
|
|
case ARG_CONTROL:
|
2004-03-14 22:34:33 +00:00
|
|
|
udpsrc->control = g_value_get_enum (value);
|
2002-11-16 18:55:43 +00:00
|
|
|
break;
|
2001-12-22 23:27:31 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_udpsrc_get_property (GObject * object, guint prop_id, GValue * value,
|
|
|
|
GParamSpec * pspec)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
|
|
|
GstUDPSrc *udpsrc;
|
|
|
|
|
|
|
|
/* it's not null if we got it, but it might not be ours */
|
2004-03-14 22:34:33 +00:00
|
|
|
g_return_if_fail (GST_IS_UDPSRC (object));
|
|
|
|
udpsrc = GST_UDPSRC (object);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case ARG_PORT:
|
|
|
|
g_value_set_int (value, udpsrc->port);
|
|
|
|
break;
|
2003-06-09 23:13:40 +00:00
|
|
|
case ARG_MULTICAST_GROUP:
|
|
|
|
g_value_set_string (value, udpsrc->multi_group);
|
|
|
|
break;
|
2002-11-16 18:55:43 +00:00
|
|
|
case ARG_CONTROL:
|
|
|
|
g_value_set_enum (value, udpsrc->control);
|
|
|
|
break;
|
2001-12-22 23:27:31 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-19 04:10:05 +00:00
|
|
|
/* create a socket for sending to remote machine */
|
2001-12-22 23:27:31 +00:00
|
|
|
static gboolean
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_udpsrc_init_receive (GstUDPSrc * src)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
2002-11-16 18:55:43 +00:00
|
|
|
guint bc_val;
|
2004-03-14 22:34:33 +00:00
|
|
|
gint reuse = 1;
|
|
|
|
|
2004-02-02 20:09:09 +00:00
|
|
|
memset (&src->myaddr, 0, sizeof (src->myaddr));
|
2004-03-15 19:32:27 +00:00
|
|
|
src->myaddr.sin_family = AF_INET; /* host byte order */
|
|
|
|
src->myaddr.sin_port = htons (src->port); /* short, network byte order */
|
2001-12-22 23:27:31 +00:00
|
|
|
src->myaddr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
|
|
|
if ((src->sock = socket (AF_INET, SOCK_DGRAM, 0)) == -1) {
|
2004-03-14 22:34:33 +00:00
|
|
|
perror ("socket");
|
2001-12-22 23:27:31 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2004-03-14 22:34:33 +00:00
|
|
|
|
|
|
|
if (setsockopt (src->sock, SOL_SOCKET, SO_REUSEADDR, &reuse,
|
2004-03-15 19:32:27 +00:00
|
|
|
sizeof (reuse)) == -1) {
|
2004-03-14 22:34:33 +00:00
|
|
|
perror ("setsockopt");
|
|
|
|
return FALSE;
|
2003-08-20 05:35:48 +00:00
|
|
|
}
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
if (bind (src->sock, (struct sockaddr *) &src->myaddr,
|
2004-03-15 19:32:27 +00:00
|
|
|
sizeof (src->myaddr)) == -1) {
|
2004-03-14 22:34:33 +00:00
|
|
|
perror ("bind");
|
2001-12-22 23:27:31 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2003-06-09 23:13:40 +00:00
|
|
|
if (inet_aton (src->multi_group, &(src->multi_addr.imr_multiaddr))) {
|
|
|
|
if (src->multi_addr.imr_multiaddr.s_addr) {
|
|
|
|
src->multi_addr.imr_interface.s_addr = INADDR_ANY;
|
2004-03-14 22:34:33 +00:00
|
|
|
setsockopt (src->sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &src->multi_addr,
|
2004-03-15 19:32:27 +00:00
|
|
|
sizeof (src->multi_addr));
|
2003-06-09 23:13:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-16 18:55:43 +00:00
|
|
|
bc_val = 1;
|
|
|
|
setsockopt (src->sock, SOL_SOCKET, SO_BROADCAST, &bc_val, sizeof (bc_val));
|
2004-03-14 22:34:33 +00:00
|
|
|
src->myaddr.sin_port = htons (src->port + 1);
|
|
|
|
|
2002-11-16 18:55:43 +00:00
|
|
|
switch (src->control) {
|
|
|
|
case CONTROL_TCP:
|
2004-03-14 22:34:33 +00:00
|
|
|
if ((src->control_sock = socket (AF_INET, SOCK_STREAM, 0)) == -1) {
|
2004-03-15 19:32:27 +00:00
|
|
|
perror ("control_socket");
|
|
|
|
return FALSE;
|
2004-03-14 22:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bind (src->control_sock, (struct sockaddr *) &src->myaddr,
|
2004-03-15 19:32:27 +00:00
|
|
|
sizeof (src->myaddr)) == -1) {
|
|
|
|
perror ("control_bind");
|
|
|
|
return FALSE;
|
2004-03-14 22:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (listen (src->control_sock, 5) == -1) {
|
2004-03-15 19:32:27 +00:00
|
|
|
perror ("listen");
|
|
|
|
return FALSE;
|
2004-03-14 22:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fcntl (src->control_sock, F_SETFL, O_NONBLOCK);
|
|
|
|
|
|
|
|
break;
|
2002-11-16 18:55:43 +00:00
|
|
|
case CONTROL_UDP:
|
2004-03-14 22:34:33 +00:00
|
|
|
if ((src->control_sock = socket (AF_INET, SOCK_DGRAM, 0)) == -1) {
|
2004-03-15 19:32:27 +00:00
|
|
|
perror ("socket");
|
|
|
|
return FALSE;
|
2004-03-14 22:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bind (src->control_sock, (struct sockaddr *) &src->myaddr,
|
2004-03-15 19:32:27 +00:00
|
|
|
sizeof (src->myaddr)) == -1) {
|
|
|
|
perror ("control_bind");
|
|
|
|
return FALSE;
|
2004-03-14 22:34:33 +00:00
|
|
|
}
|
|
|
|
/* We can only do broadcast in udp */
|
|
|
|
bc_val = 1;
|
|
|
|
setsockopt (src->control_sock, SOL_SOCKET, SO_BROADCAST, &bc_val,
|
2004-03-15 19:32:27 +00:00
|
|
|
sizeof (bc_val));
|
2004-03-14 22:34:33 +00:00
|
|
|
break;
|
2002-11-16 18:55:43 +00:00
|
|
|
case CONTROL_NONE:
|
2004-03-14 22:34:33 +00:00
|
|
|
GST_FLAG_SET (src, GST_UDPSRC_OPEN);
|
|
|
|
return TRUE;
|
|
|
|
break;
|
2002-11-16 18:55:43 +00:00
|
|
|
default:
|
2004-03-14 22:34:33 +00:00
|
|
|
return FALSE;
|
|
|
|
break;
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_FLAG_SET (src, GST_UDPSRC_OPEN);
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2001-12-22 23:27:31 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_udpsrc_close (GstUDPSrc * src)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
2003-04-06 22:20:42 +00:00
|
|
|
if (src->sock != -1) {
|
|
|
|
close (src->sock);
|
|
|
|
src->sock = -1;
|
|
|
|
}
|
|
|
|
if (src->control_sock != -1) {
|
|
|
|
close (src->control_sock);
|
|
|
|
src->control_sock = -1;
|
|
|
|
}
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
GST_FLAG_UNSET (src, GST_UDPSRC_OPEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstElementStateReturn
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_udpsrc_change_state (GstElement * element)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (GST_IS_UDPSRC (element), GST_STATE_FAILURE);
|
|
|
|
|
|
|
|
if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
|
|
|
|
if (GST_FLAG_IS_SET (element, GST_UDPSRC_OPEN))
|
|
|
|
gst_udpsrc_close (GST_UDPSRC (element));
|
|
|
|
} else {
|
|
|
|
if (!GST_FLAG_IS_SET (element, GST_UDPSRC_OPEN)) {
|
|
|
|
if (!gst_udpsrc_init_receive (GST_UDPSRC (element)))
|
2004-03-15 19:32:27 +00:00
|
|
|
return GST_STATE_FAILURE;
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GST_ELEMENT_CLASS (parent_class)->change_state)
|
|
|
|
return GST_ELEMENT_CLASS (parent_class)->change_state (element);
|
|
|
|
|
|
|
|
return GST_STATE_SUCCESS;
|
|
|
|
}
|