Commit 6f0ea358 authored by Wim Taymans's avatar Wim Taymans

Ported to 0.9.

Original commit message from CVS:
Ported to 0.9.
Set up transports, init UDP ports, init RTP session managers.
parent 6cacd6f6
2005-05-11 Wim Taymans <wim@fluendo.com>
* gst/rtsp/.cvsignore:
* gst/rtsp/Makefile.am:
* gst/rtsp/gstrtsp.c: (plugin_init):
* gst/rtsp/gstrtsp.h:
* gst/rtsp/gstrtspsrc.c: (gst_rtsp_proto_get_type),
(gst_rtspsrc_get_type), (gst_rtspsrc_base_init),
(gst_rtspsrc_class_init), (gst_rtspsrc_init),
(gst_rtspsrc_set_property), (gst_rtspsrc_get_property),
(gst_rtspsrc_create_stream), (rtspsrc_add_element),
(gst_rtspsrc_stream_setup_rtp),
(gst_rtspsrc_stream_configure_transport), (find_stream),
(gst_rtspsrc_loop), (gst_rtspsrc_send), (gst_rtspsrc_open),
(gst_rtspsrc_close), (gst_rtspsrc_play), (gst_rtspsrc_pause),
(gst_rtspsrc_activate), (gst_rtspsrc_change_state):
* gst/rtsp/gstrtspsrc.h:
* gst/rtsp/rtsp.h:
* gst/rtsp/rtspconnection.c: (rtsp_connection_open),
(rtsp_connection_create), (append_header), (rtsp_connection_send),
(read_line), (read_string), (read_key), (parse_response_status),
(parse_line), (read_body), (rtsp_connection_receive),
(rtsp_connection_close):
* gst/rtsp/rtspconnection.h:
* gst/rtsp/rtspdefs.c: (rtsp_init_status), (rtsp_method_as_text),
(rtsp_header_as_text), (rtsp_status_as_text),
(rtsp_status_to_string), (rtsp_find_header_field):
* gst/rtsp/rtspdefs.h:
* gst/rtsp/rtspmessage.c: (rtsp_message_new_request),
(rtsp_message_init_request), (rtsp_message_new_response),
(rtsp_message_init_response), (rtsp_message_init_data),
(rtsp_message_add_header), (rtsp_message_remove_header),
(rtsp_message_get_header), (rtsp_message_get_header_copy),
(rtsp_message_set_body), (rtsp_message_set_body_copy),
(rtsp_message_get_body), (rtsp_message_get_body_copy), (dump_mem),
(dump_key_value), (rtsp_message_dump):
* gst/rtsp/rtspmessage.h:
* gst/rtsp/rtspstream.h:
* gst/rtsp/rtsptransport.c: (rtsp_transport_new),
(rtsp_transport_init), (parse_mode), (parse_range),
(rtsp_transport_parse), (rtsp_transport_free):
* gst/rtsp/rtsptransport.h:
* gst/rtsp/rtspurl.c: (rtsp_url_parse), (rtsp_url_free):
* gst/rtsp/rtspurl.h:
* gst/rtsp/sdp.h:
* gst/rtsp/sdpmessage.c: (sdp_message_new), (sdp_message_init),
(sdp_message_clean), (sdp_message_free), (sdp_media_new),
(sdp_media_init), (sdp_message_set_origin),
(sdp_message_get_origin), (sdp_message_set_connection),
(sdp_message_get_connection), (sdp_message_add_bandwidth),
(sdp_message_add_time), (sdp_message_add_zone),
(sdp_message_set_key), (sdp_message_get_key),
(sdp_message_get_attribute_val), (sdp_message_add_attribute),
(sdp_message_add_media), (sdp_media_add_attribute),
(sdp_media_add_bandwidth), (sdp_media_add_format),
(sdp_media_get_attribute_val), (read_string), (read_string_del),
(sdp_parse_line), (sdp_message_parse_buffer), (print_media),
(sdp_message_dump):
* gst/rtsp/sdpmessage.h:
* gst/rtsp/test.c: (main):
Ported to 0.9.
Set up transports, init UDP ports, init RTP session managers.
2005-05-11 Wim Taymans <wim@fluendo.com>
* gst/rtp/Makefile.am:
......
Makefile
Makefile.in
*.o
*.lo
*.la
.deps
.libs
test
plugin_LTLIBRARIES = libgstrtsp.la
libgstrtsp_la_SOURCES = gstrtsp.c gstrtspsrc.c \
rtspconnection.c \
rtspdefs.c \
rtspmessage.c \
rtsptransport.c \
rtspurl.c \
sdpmessage.c
libgstrtsp_la_CFLAGS = $(GST_CFLAGS)
libgstrtsp_la_LIBADD =
libgstrtsp_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
check_PROGRAMS = test
test_SOURCES = test.c rtspdefs.c rtspurl.c rtspconnection.c rtspmessage.c rtsptransport.c sdpmessage.c
test_CFLAGS = $(GST_CFLAGS)
test_LDFLAGS = $(GST_LIBS)
noinst_HEADERS = gstrtspsrc.h gstrtsp.h rtsptransport.h
/* GStreamer
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstrtspsrc.h"
static gboolean
plugin_init (GstPlugin * plugin)
{
if (!gst_element_register (plugin, "rtspsrc", GST_RANK_NONE,
GST_TYPE_RTSPSRC))
return FALSE;
return TRUE;
}
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
"rtsp",
"transfer data via RTSP",
plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)
/* GStreamer
* 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.
*/
#ifndef __GST_RTSP_H__
#define __GST_RTSP_H__
G_BEGIN_DECLS
G_END_DECLS
#endif /* __GST_RTSP_H__ */
/* GStreamer
* Copyright (C) <2005> Wim Taymans <wim@fluendo.com>
*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <unistd.h>
#include <string.h>
#include "gstrtspsrc.h"
#include "sdp.h"
/* elementfactory information */
static GstElementDetails gst_rtspsrc_details =
GST_ELEMENT_DETAILS ("RTSP packet receiver",
"Source/Network",
"Receive data over the network via RTSP",
"Wim Taymans <wim@fluendo.com>");
static GstStaticPadTemplate rtptemplate =
GST_STATIC_PAD_TEMPLATE ("rtp_stream%d",
GST_PAD_SRC,
GST_PAD_SOMETIMES,
GST_STATIC_CAPS_ANY);
static GstStaticPadTemplate rtcptemplate =
GST_STATIC_PAD_TEMPLATE ("rtcp_stream%d",
GST_PAD_SRC,
GST_PAD_SOMETIMES,
GST_STATIC_CAPS_ANY);
enum
{
/* FILL ME */
LAST_SIGNAL
};
#define DEFAULT_LOCATION NULL
#define DEFAULT_PROTOCOLS GST_RTSP_PROTO_UDP_UNICAST | GST_RTSP_PROTO_UDP_MULTICAST | GST_RTSP_PROTO_TCP
#define DEFAULT_DEBUG FALSE
enum
{
PROP_0,
PROP_LOCATION,
PROP_PROTOCOLS,
PROP_DEBUG,
/* FILL ME */
};
#define GST_TYPE_RTSP_PROTO (gst_rtsp_proto_get_type())
static GType
gst_rtsp_proto_get_type (void)
{
static GType rtsp_proto_type = 0;
static GFlagsValue rtsp_proto[] = {
{GST_RTSP_PROTO_UDP_UNICAST, "UDP Unicast", "UDP unicast mode"},
{GST_RTSP_PROTO_UDP_MULTICAST, "UDP Multicast", "UDP Multicast mode"},
{GST_RTSP_PROTO_TCP, "TCP", "TCP interleaved mode"},
{0, NULL, NULL},
};
if (!rtsp_proto_type) {
rtsp_proto_type = g_flags_register_static ("GstRTSPProto", rtsp_proto);
}
return rtsp_proto_type;
}
static void gst_rtspsrc_base_init (gpointer g_class);
static void gst_rtspsrc_class_init (GstRTSPSrc * klass);
static void gst_rtspsrc_init (GstRTSPSrc * rtspsrc);
static GstElementStateReturn gst_rtspsrc_change_state (GstElement * element);
static gboolean gst_rtspsrc_activate (GstPad * pad, GstActivateMode mode);
static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static void gst_rtspsrc_loop (GstRTSPSrc * src);
static GstElementClass *parent_class = NULL;
/*static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 }; */
GType
gst_rtspsrc_get_type (void)
{
static GType rtspsrc_type = 0;
if (!rtspsrc_type) {
static const GTypeInfo rtspsrc_info = {
sizeof (GstRTSPSrcClass),
gst_rtspsrc_base_init,
NULL,
(GClassInitFunc) gst_rtspsrc_class_init,
NULL,
NULL,
sizeof (GstRTSPSrc),
0,
(GInstanceInitFunc) gst_rtspsrc_init,
NULL
};
rtspsrc_type =
g_type_register_static (GST_TYPE_ELEMENT, "GstRTSPSrc", &rtspsrc_info,
0);
}
return rtspsrc_type;
}
static void
gst_rtspsrc_base_init (gpointer g_class)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&rtptemplate));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&rtcptemplate));
gst_element_class_set_details (element_class, &gst_rtspsrc_details);
}
static void
gst_rtspsrc_class_init (GstRTSPSrc * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
gobject_class->set_property = gst_rtspsrc_set_property;
gobject_class->get_property = gst_rtspsrc_get_property;
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LOCATION,
g_param_spec_string ("location", "RTSP Location",
"Location of the RTSP url to read",
DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PROTOCOLS,
g_param_spec_flags ("protocols", "Protocols", "Allowed protocols",
GST_TYPE_RTSP_PROTO, DEFAULT_PROTOCOLS,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DEBUG,
g_param_spec_boolean ("debug", "Debug",
"Dump request qnd response messages to stdout",
DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
gstelement_class->change_state = gst_rtspsrc_change_state;
}
static void
gst_rtspsrc_init (GstRTSPSrc * src)
{
/*
src->srcpad =
gst_pad_new_from_template (gst_static_pad_template_get (&srctemplate),
"src");
gst_pad_set_loop_function (src->srcpad, gst_rtspsrc_loop);
gst_pad_set_activate_function (src->srcpad, gst_rtspsrc_activate);
gst_element_add_pad (GST_ELEMENT (src), src->srcpad);
*/
}
static void
gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
GParamSpec * pspec)
{
GstRTSPSrc *rtspsrc;
rtspsrc = GST_RTSPSRC (object);
switch (prop_id) {
case PROP_LOCATION:
g_free (rtspsrc->location);
rtspsrc->location = g_value_dup_string (value);
break;
case PROP_PROTOCOLS:
rtspsrc->protocols = g_value_get_flags (value);
break;
case PROP_DEBUG:
rtspsrc->debug = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
GParamSpec * pspec)
{
GstRTSPSrc *rtspsrc;
rtspsrc = GST_RTSPSRC (object);
switch (prop_id) {
case PROP_LOCATION:
g_value_set_string (value, rtspsrc->location);
break;
case PROP_PROTOCOLS:
g_value_set_flags (value, rtspsrc->protocols);
break;
case PROP_DEBUG:
g_value_set_boolean (value, rtspsrc->debug);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static GstRTSPStream *
gst_rtspsrc_create_stream (GstRTSPSrc * src)
{
GstRTSPStream *s;
s = g_new0 (GstRTSPStream, 1);
s->parent = src;
src->streams = g_list_append (src->streams, s);
return s;
}
static gboolean
rtspsrc_add_element (GstRTSPSrc * src, GstElement * element)
{
gst_object_set_parent (GST_OBJECT (element), GST_OBJECT (src));
gst_element_set_manager (element, GST_ELEMENT_MANAGER (src));
gst_element_set_scheduler (element, GST_ELEMENT_SCHEDULER (src));
return TRUE;
}
static gboolean
gst_rtspsrc_stream_setup_rtp (GstRTSPStream * stream, gint * rtpport,
gint * rtcpport)
{
GstElement *rtpsrc;
GstElementStateReturn ret;
GstRTSPSrc *src;
src = stream->parent;
if (!(stream->rtpsrc =
gst_element_make_from_uri (GST_URI_SRC, "udp://0.0.0.0:0", NULL)))
goto no_udp_rtp_protocol;
/* we manage this element */
rtspsrc_add_element (src, stream->rtpsrc);
if ((ret =
gst_element_set_state (stream->rtpsrc,
GST_STATE_PAUSED)) != GST_STATE_SUCCESS)
goto start_rtp_failure;
if (!(stream->rtcpsrc =
gst_element_make_from_uri (GST_URI_SRC, "udp://0.0.0.0:0", NULL)))
goto no_udp_rtcp_protocol;
/* we manage this element */
rtspsrc_add_element (src, stream->rtcpsrc);
if ((ret =
gst_element_set_state (stream->rtcpsrc,
GST_STATE_PAUSED)) != GST_STATE_SUCCESS)
goto start_rtcp_failure;
g_object_get (G_OBJECT (stream->rtpsrc), "port", rtpport, NULL);
g_object_get (G_OBJECT (stream->rtcpsrc), "port", rtcpport, NULL);
return TRUE;
/* ERRORS, FIXME, cleanup */
no_udp_rtp_protocol:
{
GST_DEBUG ("could not get UDP source for rtp");
return FALSE;
}
no_udp_rtcp_protocol:
{
GST_DEBUG ("could not get UDP source for rtcp");
return FALSE;
}
start_rtp_failure:
{
GST_DEBUG ("could not start UDP source for rtp");
return FALSE;
}
start_rtcp_failure:
{
GST_DEBUG ("could not start UDP source for rtcp");
return FALSE;
}
}
static gboolean
gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
RTSPTransport * transport)
{
GstRTSPSrc *src;
src = stream->parent;
if (transport->lower_transport == RTSP_LOWER_TRANS_TCP) {
GstPad *pad;
/* configure for interleaved delivery */
if (!(stream->rtpdec = gst_element_factory_make ("rtpdec", NULL)))
goto no_element;
/* we manage this element */
rtspsrc_add_element (src, stream->rtpdec);
stream->rtpdecrtp = gst_element_get_pad (stream->rtpdec, "sinkrtp");
stream->rtpdecrtcp = gst_element_get_pad (stream->rtpdec, "sinkrtcp");
/* FIXME, make sure it outputs the caps */
pad = gst_element_get_pad (stream->rtpdec, "srcrtp");
gst_element_add_ghost_pad (GST_ELEMENT (src), pad, "srcrtp");
gst_object_unref (GST_OBJECT (pad));
} else {
/* configure for UDP delivery, FIXME */
}
return TRUE;
no_element:
{
GST_DEBUG ("no rtpdec element found");
return FALSE;
}
}
static gint
find_stream (GstRTSPStream * stream, gconstpointer a)
{
gint channel = GPOINTER_TO_INT (a);
if (stream->rtpchannel == channel || stream->rtcpchannel == channel)
return 0;
return -1;
}
static void
gst_rtspsrc_loop (GstRTSPSrc * src)
{
RTSPMessage response = { 0 };
RTSPResult res;
gint channel;
GList *lstream;
GstRTSPStream *stream;
GstPadChainFunction chainfunc;
GstPad *outpad;
guint8 *data;
gint size;
do {
GST_DEBUG ("doing reveive");
if ((res = rtsp_connection_receive (src->connection, &response)) < 0)
goto receive_error;
GST_DEBUG ("got packet");
}
while (response.type != RTSP_MESSAGE_DATA);
channel = response.type_data.data.channel;
lstream = g_list_find_custom (src->streams, GINT_TO_POINTER (channel),
(GCompareFunc) find_stream);
if (!lstream)
goto unknown_stream;
stream = (GstRTSPStream *) lstream->data;
if (channel == stream->rtpchannel)
outpad = stream->rtpdecrtp;
else if (channel == stream->rtcpchannel)
outpad = stream->rtpdecrtcp;
rtsp_message_get_body (&response, &data, &size);
/* channels are not correct on some servers, do extra check */
if (data[1] >= 200 && data[1] <= 204) {
/* hmm RTCP message */
outpad = stream->rtpdecrtcp;
}
if ((chainfunc = GST_RPAD_CHAINFUNC (outpad))) {
GstBuffer *buf;
buf = gst_buffer_new_and_alloc (size);
memcpy (GST_BUFFER_DATA (buf), data, size);
if (chainfunc (outpad, buf) != GST_FLOW_OK)
goto need_pause;
}
unknown_stream:
return;
receive_error:
{
GST_ELEMENT_ERROR (src, RESOURCE, WRITE,
("Could not receive message."), (NULL));
/*
gst_pad_push_event (src->srcpad, gst_event_new (GST_EVENT_EOS));
*/
goto need_pause;
}
need_pause:
{
gst_task_pause (src->task);
return;
}
}
static gboolean
gst_rtspsrc_send (GstRTSPSrc * src, RTSPMessage * request,
RTSPMessage * response)
{
RTSPResult res;
if (src->debug) {
rtsp_message_dump (request);
}
if ((res = rtsp_connection_send (src->connection, request)) < 0)
goto send_error;
if ((res = rtsp_connection_receive (src->connection, response)) < 0)
goto receive_error;
if (response->type_data.response.code != RTSP_STS_OK)
goto error_response;
if (src->debug) {
rtsp_message_dump (response);
}
return TRUE;
send_error:
{
GST_ELEMENT_ERROR (src, RESOURCE, WRITE,
("Could not send message."), (NULL));
return FALSE;
}
receive_error:
{
GST_ELEMENT_ERROR (src, RESOURCE, READ,
("Could not receive message."), (NULL));
return FALSE;
}
error_response:
{
rtsp_message_dump (request);
rtsp_message_dump (response);
GST_ELEMENT_ERROR (src, RESOURCE, READ, ("Got error response."), (NULL));
return FALSE;
}
}
static gboolean
gst_rtspsrc_open (GstRTSPSrc * src)
{
RTSPUrl *url;
RTSPResult res;
RTSPMessage request = { 0 };
RTSPMessage response = { 0 };
guint8 *data;
guint size;
SDPMessage sdp = { 0 };
GstRTSPProto protocols;
/* parse url */
GST_DEBUG ("parsing url...");
if ((res = rtsp_url_parse (src->location, &url)) < 0)
goto invalid_url;
/* open connection */
GST_DEBUG ("opening connection...");
if ((res = rtsp_connection_open (url, &src->connection)) < 0)
goto could_not_open;
/* create DESCRIBE */
GST_DEBUG ("create describe...");
if ((res =
rtsp_message_init_request (RTSP_DESCRIBE, src->location,
&request)) < 0)
goto create_request_failed;
/* we accept SDP for now */
rtsp_message_add_header (&request, RTSP_HDR_ACCEPT, "application/sdp");
/* send DESCRIBE */
GST_DEBUG ("send describe...");
if (!gst_rtspsrc_send (src, &request, &response))
goto send_error;
/* parse SDP */
rtsp_message_get_body (&response, &data, &size);
GST_DEBUG ("parse sdp...");
sdp_message_init (&sdp);
sdp_message_parse_buffer (data, size, &sdp);
/* we allow all configured protocols */
protocols = src->protocols;
/* setup streams */
{
gint i;
for (i = 0; i < sdp_message_medias_len (&sdp); i++) {
SDPMedia *media;
gchar *setup_url;
gchar *control_url;
gchar *transports;
GstRTSPStream *stream;
media = sdp_message_get_media (&sdp, i);
stream = gst_rtspsrc_create_stream (src);
GST_DEBUG ("setup media %d", i);
control_url = sdp_media_get_attribute_val (media, "control");
if (control_url == NULL) {
GST_DEBUG ("no control url found, skipping stream");
continue;
}
/* FIXME, check absolute/relative URL */
setup_url = g_strdup_printf ("%s/%s", src->location, control_url);
GST_DEBUG ("setup %s", setup_url);
/* create SETUP request */
if ((res =
rtsp_message_init_request (RTSP_SETUP, setup_url,
&request)) < 0) {
g_free (setup_url);
goto create_request_failed;
}
g_free (setup_url);
transports = g_strdup ("");
if (protocols & GST_RTSP_PROTO_UDP_UNICAST) {
gchar *new;
gint rtpport, rtcpport;
gchar *trxparams;
/* allocate two udp ports */
gst_rtspsrc_stream_setup_rtp (stream, &rtpport, &rtcpport);